]>
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 | -- -- | |
b943a971 | 9 | -- Copyright (C) 1992-2016, 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 RK |
33 | with Exp_Util; use Exp_Util; |
34 | with Fname; use Fname; | |
35 | with Freeze; use Freeze; | |
8636f52f | 36 | with Ghost; use Ghost; |
16ca248a | 37 | with Impunit; use Impunit; |
996ae0b0 RK |
38 | with Lib; use Lib; |
39 | with Lib.Load; use Lib.Load; | |
40 | with Lib.Xref; use Lib.Xref; | |
41 | with Namet; use Namet; | |
294ccb21 | 42 | with Namet.Sp; use Namet.Sp; |
996ae0b0 RK |
43 | with Nlists; use Nlists; |
44 | with Nmake; use Nmake; | |
45 | with Opt; use Opt; | |
46 | with Output; use Output; | |
47 | with Restrict; use Restrict; | |
6e937c1c | 48 | with Rident; use Rident; |
996ae0b0 RK |
49 | with Rtsfind; use Rtsfind; |
50 | with Sem; use Sem; | |
a4100e55 | 51 | with Sem_Aux; use Sem_Aux; |
fbf5a39b | 52 | with Sem_Cat; use Sem_Cat; |
996ae0b0 RK |
53 | with Sem_Ch3; use Sem_Ch3; |
54 | with Sem_Ch4; use Sem_Ch4; | |
55 | with Sem_Ch6; use Sem_Ch6; | |
56 | with Sem_Ch12; use Sem_Ch12; | |
ef992452 | 57 | with Sem_Ch13; use Sem_Ch13; |
dec6faf1 | 58 | with Sem_Dim; use Sem_Dim; |
82c80734 | 59 | with Sem_Disp; use Sem_Disp; |
a77842bd | 60 | with Sem_Dist; use Sem_Dist; |
a2dc5812 | 61 | with Sem_Eval; use Sem_Eval; |
996ae0b0 RK |
62 | with Sem_Res; use Sem_Res; |
63 | with Sem_Util; use Sem_Util; | |
64 | with Sem_Type; use Sem_Type; | |
65 | with Stand; use Stand; | |
66 | with Sinfo; use Sinfo; | |
67 | with Sinfo.CN; use Sinfo.CN; | |
68 | with Snames; use Snames; | |
69 | with Style; use Style; | |
70 | with Table; | |
71 | with Tbuild; use Tbuild; | |
72 | with Uintp; use Uintp; | |
73 | ||
996ae0b0 RK |
74 | package body Sem_Ch8 is |
75 | ||
76 | ------------------------------------ | |
77 | -- Visibility and Name Resolution -- | |
78 | ------------------------------------ | |
79 | ||
2c1b72d7 | 80 | -- This package handles name resolution and the collection of possible |
996ae0b0 RK |
81 | -- interpretations for overloaded names, prior to overload resolution. |
82 | ||
83 | -- Name resolution is the process that establishes a mapping between source | |
84 | -- identifiers and the entities they denote at each point in the program. | |
85 | -- Each entity is represented by a defining occurrence. Each identifier | |
86 | -- that denotes an entity points to the corresponding defining occurrence. | |
87 | -- This is the entity of the applied occurrence. Each occurrence holds | |
88 | -- an index into the names table, where source identifiers are stored. | |
89 | ||
90 | -- Each entry in the names table for an identifier or designator uses the | |
91 | -- Info pointer to hold a link to the currently visible entity that has | |
92 | -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id | |
93 | -- in package Sem_Util). The visibility is initialized at the beginning of | |
94 | -- semantic processing to make entities in package Standard immediately | |
95 | -- visible. The visibility table is used in a more subtle way when | |
96 | -- compiling subunits (see below). | |
97 | ||
98 | -- Entities that have the same name (i.e. homonyms) are chained. In the | |
99 | -- case of overloaded entities, this chain holds all the possible meanings | |
100 | -- of a given identifier. The process of overload resolution uses type | |
101 | -- information to select from this chain the unique meaning of a given | |
102 | -- identifier. | |
103 | ||
104 | -- Entities are also chained in their scope, through the Next_Entity link. | |
105 | -- As a consequence, the name space is organized as a sparse matrix, where | |
106 | -- each row corresponds to a scope, and each column to a source identifier. | |
107 | -- Open scopes, that is to say scopes currently being compiled, have their | |
108 | -- corresponding rows of entities in order, innermost scope first. | |
109 | ||
21d7ef70 | 110 | -- The scopes of packages that are mentioned in context clauses appear in |
996ae0b0 RK |
111 | -- no particular order, interspersed among open scopes. This is because |
112 | -- in the course of analyzing the context of a compilation, a package | |
113 | -- declaration is first an open scope, and subsequently an element of the | |
114 | -- context. If subunits or child units are present, a parent unit may | |
115 | -- appear under various guises at various times in the compilation. | |
116 | ||
117 | -- When the compilation of the innermost scope is complete, the entities | |
118 | -- defined therein are no longer visible. If the scope is not a package | |
119 | -- declaration, these entities are never visible subsequently, and can be | |
120 | -- removed from visibility chains. If the scope is a package declaration, | |
121 | -- its visible declarations may still be accessible. Therefore the entities | |
122 | -- defined in such a scope are left on the visibility chains, and only | |
123 | -- their visibility (immediately visibility or potential use-visibility) | |
124 | -- is affected. | |
125 | ||
126 | -- The ordering of homonyms on their chain does not necessarily follow | |
127 | -- the order of their corresponding scopes on the scope stack. For | |
128 | -- example, if package P and the enclosing scope both contain entities | |
129 | -- named E, then when compiling the package body the chain for E will | |
130 | -- hold the global entity first, and the local one (corresponding to | |
131 | -- the current inner scope) next. As a result, name resolution routines | |
132 | -- do not assume any relative ordering of the homonym chains, either | |
133 | -- for scope nesting or to order of appearance of context clauses. | |
134 | ||
135 | -- When compiling a child unit, entities in the parent scope are always | |
136 | -- immediately visible. When compiling the body of a child unit, private | |
137 | -- entities in the parent must also be made immediately visible. There | |
138 | -- are separate routines to make the visible and private declarations | |
139 | -- visible at various times (see package Sem_Ch7). | |
140 | ||
141 | -- +--------+ +-----+ | |
142 | -- | In use |-------->| EU1 |--------------------------> | |
143 | -- +--------+ +-----+ | |
144 | -- | | | |
145 | -- +--------+ +-----+ +-----+ | |
146 | -- | Stand. |---------------->| ES1 |--------------->| ES2 |---> | |
147 | -- +--------+ +-----+ +-----+ | |
148 | -- | | | |
149 | -- +---------+ | +-----+ | |
150 | -- | with'ed |------------------------------>| EW2 |---> | |
151 | -- +---------+ | +-----+ | |
152 | -- | | | |
153 | -- +--------+ +-----+ +-----+ | |
154 | -- | Scope2 |---------------->| E12 |--------------->| E22 |---> | |
155 | -- +--------+ +-----+ +-----+ | |
156 | -- | | | |
157 | -- +--------+ +-----+ +-----+ | |
158 | -- | Scope1 |---------------->| E11 |--------------->| E12 |---> | |
159 | -- +--------+ +-----+ +-----+ | |
160 | -- ^ | | | |
161 | -- | | | | |
162 | -- | +---------+ | | | |
163 | -- | | with'ed |-----------------------------------------> | |
164 | -- | +---------+ | | | |
165 | -- | | | | |
166 | -- Scope stack | | | |
167 | -- (innermost first) | | | |
168 | -- +----------------------------+ | |
169 | -- Names table => | Id1 | | | | Id2 | | |
170 | -- +----------------------------+ | |
171 | ||
172 | -- Name resolution must deal with several syntactic forms: simple names, | |
173 | -- qualified names, indexed names, and various forms of calls. | |
174 | ||
175 | -- Each identifier points to an entry in the names table. The resolution | |
176 | -- of a simple name consists in traversing the homonym chain, starting | |
177 | -- from the names table. If an entry is immediately visible, it is the one | |
fbf5a39b | 178 | -- designated by the identifier. If only potentially use-visible entities |
996ae0b0 RK |
179 | -- are on the chain, we must verify that they do not hide each other. If |
180 | -- the entity we find is overloadable, we collect all other overloadable | |
181 | -- entities on the chain as long as they are not hidden. | |
182 | -- | |
183 | -- To resolve expanded names, we must find the entity at the intersection | |
184 | -- of the entity chain for the scope (the prefix) and the homonym chain | |
185 | -- for the selector. In general, homonym chains will be much shorter than | |
186 | -- entity chains, so it is preferable to start from the names table as | |
187 | -- well. If the entity found is overloadable, we must collect all other | |
188 | -- interpretations that are defined in the scope denoted by the prefix. | |
189 | ||
190 | -- For records, protected types, and tasks, their local entities are | |
191 | -- removed from visibility chains on exit from the corresponding scope. | |
192 | -- From the outside, these entities are always accessed by selected | |
193 | -- notation, and the entity chain for the record type, protected type, | |
21d7ef70 | 194 | -- etc. is traversed sequentially in order to find the designated entity. |
996ae0b0 RK |
195 | |
196 | -- The discriminants of a type and the operations of a protected type or | |
197 | -- task are unchained on exit from the first view of the type, (such as | |
198 | -- a private or incomplete type declaration, or a protected type speci- | |
fbf5a39b | 199 | -- fication) and re-chained when compiling the second view. |
996ae0b0 RK |
200 | |
201 | -- In the case of operators, we do not make operators on derived types | |
202 | -- explicit. As a result, the notation P."+" may denote either a user- | |
203 | -- defined function with name "+", or else an implicit declaration of the | |
204 | -- operator "+" in package P. The resolution of expanded names always | |
205 | -- tries to resolve an operator name as such an implicitly defined entity, | |
206 | -- in addition to looking for explicit declarations. | |
207 | ||
208 | -- All forms of names that denote entities (simple names, expanded names, | |
209 | -- character literals in some cases) have a Entity attribute, which | |
210 | -- identifies the entity denoted by the name. | |
211 | ||
212 | --------------------- | |
213 | -- The Scope Stack -- | |
214 | --------------------- | |
215 | ||
216 | -- The Scope stack keeps track of the scopes currently been compiled. | |
217 | -- Every entity that contains declarations (including records) is placed | |
218 | -- on the scope stack while it is being processed, and removed at the end. | |
219 | -- Whenever a non-package scope is exited, the entities defined therein | |
220 | -- are removed from the visibility table, so that entities in outer scopes | |
221 | -- become visible (see previous description). On entry to Sem, the scope | |
222 | -- stack only contains the package Standard. As usual, subunits complicate | |
223 | -- this picture ever so slightly. | |
224 | ||
225 | -- The Rtsfind mechanism can force a call to Semantics while another | |
226 | -- compilation is in progress. The unit retrieved by Rtsfind must be | |
21d7ef70 | 227 | -- compiled in its own context, and has no access to the visibility of |
996ae0b0 RK |
228 | -- the unit currently being compiled. The procedures Save_Scope_Stack and |
229 | -- Restore_Scope_Stack make entities in current open scopes invisible | |
230 | -- before compiling the retrieved unit, and restore the compilation | |
231 | -- environment afterwards. | |
232 | ||
233 | ------------------------ | |
234 | -- Compiling subunits -- | |
235 | ------------------------ | |
236 | ||
16ca248a ES |
237 | -- Subunits must be compiled in the environment of the corresponding stub, |
238 | -- that is to say with the same visibility into the parent (and its | |
996ae0b0 RK |
239 | -- context) that is available at the point of the stub declaration, but |
240 | -- with the additional visibility provided by the context clause of the | |
241 | -- subunit itself. As a result, compilation of a subunit forces compilation | |
242 | -- of the parent (see description in lib-). At the point of the stub | |
16ca248a ES |
243 | -- declaration, Analyze is called recursively to compile the proper body of |
244 | -- the subunit, but without reinitializing the names table, nor the scope | |
245 | -- stack (i.e. standard is not pushed on the stack). In this fashion the | |
246 | -- context of the subunit is added to the context of the parent, and the | |
247 | -- subunit is compiled in the correct environment. Note that in the course | |
248 | -- of processing the context of a subunit, Standard will appear twice on | |
249 | -- the scope stack: once for the parent of the subunit, and once for the | |
250 | -- unit in the context clause being compiled. However, the two sets of | |
251 | -- entities are not linked by homonym chains, so that the compilation of | |
252 | -- any context unit happens in a fresh visibility environment. | |
996ae0b0 RK |
253 | |
254 | ------------------------------- | |
255 | -- Processing of USE Clauses -- | |
256 | ------------------------------- | |
257 | ||
258 | -- Every defining occurrence has a flag indicating if it is potentially use | |
259 | -- visible. Resolution of simple names examines this flag. The processing | |
260 | -- of use clauses consists in setting this flag on all visible entities | |
261 | -- defined in the corresponding package. On exit from the scope of the use | |
262 | -- clause, the corresponding flag must be reset. However, a package may | |
a90bd866 | 263 | -- appear in several nested use clauses (pathological but legal, alas) |
996ae0b0 RK |
264 | -- which forces us to use a slightly more involved scheme: |
265 | ||
266 | -- a) The defining occurrence for a package holds a flag -In_Use- to | |
267 | -- indicate that it is currently in the scope of a use clause. If a | |
44d6a706 | 268 | -- redundant use clause is encountered, then the corresponding occurrence |
996ae0b0 RK |
269 | -- of the package name is flagged -Redundant_Use-. |
270 | ||
271 | -- b) On exit from a scope, the use clauses in its declarative part are | |
272 | -- scanned. The visibility flag is reset in all entities declared in | |
273 | -- package named in a use clause, as long as the package is not flagged | |
274 | -- as being in a redundant use clause (in which case the outer use | |
275 | -- clause is still in effect, and the direct visibility of its entities | |
276 | -- must be retained). | |
277 | ||
278 | -- Note that entities are not removed from their homonym chains on exit | |
279 | -- from the package specification. A subsequent use clause does not need | |
280 | -- to rechain the visible entities, but only to establish their direct | |
281 | -- visibility. | |
282 | ||
283 | ----------------------------------- | |
284 | -- Handling private declarations -- | |
285 | ----------------------------------- | |
286 | ||
287 | -- The principle that each entity has a single defining occurrence clashes | |
288 | -- with the presence of two separate definitions for private types: the | |
289 | -- first is the private type declaration, and second is the full type | |
290 | -- declaration. It is important that all references to the type point to | |
44d6a706 | 291 | -- the same defining occurrence, namely the first one. To enforce the two |
996ae0b0 RK |
292 | -- separate views of the entity, the corresponding information is swapped |
293 | -- between the two declarations. Outside of the package, the defining | |
44d6a706 | 294 | -- occurrence only contains the private declaration information, while in |
996ae0b0 RK |
295 | -- the private part and the body of the package the defining occurrence |
296 | -- contains the full declaration. To simplify the swap, the defining | |
297 | -- occurrence that currently holds the private declaration points to the | |
44d6a706 | 298 | -- full declaration. During semantic processing the defining occurrence |
16ca248a ES |
299 | -- also points to a list of private dependents, that is to say access types |
300 | -- or composite types whose designated types or component types are | |
996ae0b0 RK |
301 | -- subtypes or derived types of the private type in question. After the |
302 | -- full declaration has been seen, the private dependents are updated to | |
303 | -- indicate that they have full definitions. | |
304 | ||
305 | ------------------------------------ | |
306 | -- Handling of Undefined Messages -- | |
307 | ------------------------------------ | |
308 | ||
309 | -- In normal mode, only the first use of an undefined identifier generates | |
310 | -- a message. The table Urefs is used to record error messages that have | |
311 | -- been issued so that second and subsequent ones do not generate further | |
312 | -- messages. However, the second reference causes text to be added to the | |
313 | -- original undefined message noting "(more references follow)". The | |
314 | -- full error list option (-gnatf) forces messages to be generated for | |
315 | -- every reference and disconnects the use of this table. | |
316 | ||
317 | type Uref_Entry is record | |
318 | Node : Node_Id; | |
319 | -- Node for identifier for which original message was posted. The | |
320 | -- Chars field of this identifier is used to detect later references | |
321 | -- to the same identifier. | |
322 | ||
323 | Err : Error_Msg_Id; | |
324 | -- Records error message Id of original undefined message. Reset to | |
325 | -- No_Error_Msg after the second occurrence, where it is used to add | |
326 | -- text to the original message as described above. | |
327 | ||
328 | Nvis : Boolean; | |
329 | -- Set if the message is not visible rather than undefined | |
330 | ||
331 | Loc : Source_Ptr; | |
332 | -- Records location of error message. Used to make sure that we do | |
333 | -- not consider a, b : undefined as two separate instances, which | |
334 | -- would otherwise happen, since the parser converts this sequence | |
335 | -- to a : undefined; b : undefined. | |
336 | ||
337 | end record; | |
338 | ||
339 | package Urefs is new Table.Table ( | |
340 | Table_Component_Type => Uref_Entry, | |
341 | Table_Index_Type => Nat, | |
342 | Table_Low_Bound => 1, | |
343 | Table_Initial => 10, | |
344 | Table_Increment => 100, | |
345 | Table_Name => "Urefs"); | |
346 | ||
347 | Candidate_Renaming : Entity_Id; | |
348 | -- Holds a candidate interpretation that appears in a subprogram renaming | |
349 | -- declaration and does not match the given specification, but matches at | |
350 | -- least on the first formal. Allows better error message when given | |
351 | -- specification omits defaulted parameters, a common error. | |
352 | ||
353 | ----------------------- | |
354 | -- Local Subprograms -- | |
355 | ----------------------- | |
356 | ||
357 | procedure Analyze_Generic_Renaming | |
358 | (N : Node_Id; | |
359 | K : Entity_Kind); | |
360 | -- Common processing for all three kinds of generic renaming declarations. | |
361 | -- Enter new name and indicate that it renames the generic unit. | |
362 | ||
363 | procedure Analyze_Renamed_Character | |
364 | (N : Node_Id; | |
365 | New_S : Entity_Id; | |
366 | Is_Body : Boolean); | |
367 | -- Renamed entity is given by a character literal, which must belong | |
368 | -- to the return type of the new entity. Is_Body indicates whether the | |
369 | -- declaration is a renaming_as_body. If the original declaration has | |
370 | -- already been frozen (because of an intervening body, e.g.) the body of | |
371 | -- the function must be built now. The same applies to the following | |
372 | -- various renaming procedures. | |
373 | ||
374 | procedure Analyze_Renamed_Dereference | |
375 | (N : Node_Id; | |
376 | New_S : Entity_Id; | |
377 | Is_Body : Boolean); | |
378 | -- Renamed entity is given by an explicit dereference. Prefix must be a | |
379 | -- conformant access_to_subprogram type. | |
380 | ||
381 | procedure Analyze_Renamed_Entry | |
382 | (N : Node_Id; | |
383 | New_S : Entity_Id; | |
384 | Is_Body : Boolean); | |
385 | -- If the renamed entity in a subprogram renaming is an entry or protected | |
386 | -- subprogram, build a body for the new entity whose only statement is a | |
387 | -- call to the renamed entity. | |
388 | ||
389 | procedure Analyze_Renamed_Family_Member | |
390 | (N : Node_Id; | |
391 | New_S : Entity_Id; | |
392 | Is_Body : Boolean); | |
393 | -- Used when the renamed entity is an indexed component. The prefix must | |
394 | -- denote an entry family. | |
395 | ||
294ccb21 RD |
396 | procedure Analyze_Renamed_Primitive_Operation |
397 | (N : Node_Id; | |
398 | New_S : Entity_Id; | |
399 | Is_Body : Boolean); | |
400 | -- If the renamed entity in a subprogram renaming is a primitive operation | |
c5d00db0 AC |
401 | -- or a class-wide operation in prefix form, save the target object, |
402 | -- which must be added to the list of actuals in any subsequent call. | |
403 | -- The renaming operation is intrinsic because the compiler must in | |
404 | -- fact generate a wrapper for it (6.3.1 (10 1/2)). | |
294ccb21 | 405 | |
fbf5a39b | 406 | function Applicable_Use (Pack_Name : Node_Id) return Boolean; |
c42bfef2 RD |
407 | -- Common code to Use_One_Package and Set_Use, to determine whether use |
408 | -- clause must be processed. Pack_Name is an entity name that references | |
409 | -- the package in question. | |
fbf5a39b | 410 | |
996ae0b0 | 411 | procedure Attribute_Renaming (N : Node_Id); |
4c8a5bb8 AC |
412 | -- Analyze renaming of attribute as subprogram. The renaming declaration N |
413 | -- is rewritten as a subprogram body that returns the attribute reference | |
996ae0b0 RK |
414 | -- applied to the formals of the function. |
415 | ||
4c484f40 | 416 | procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id); |
c42bfef2 RD |
417 | -- Set Entity, with style check if need be. For a discriminant reference, |
418 | -- replace by the corresponding discriminal, i.e. the parameter of the | |
419 | -- initialization procedure that corresponds to the discriminant. | |
4c484f40 | 420 | |
996ae0b0 RK |
421 | procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id); |
422 | -- A renaming_as_body may occur after the entity of the original decla- | |
423 | -- ration has been frozen. In that case, the body of the new entity must | |
424 | -- be built now, because the usual mechanism of building the renamed | |
425 | -- body at the point of freezing will not work. Subp is the subprogram | |
426 | -- for which N provides the Renaming_As_Body. | |
427 | ||
fbf5a39b AC |
428 | procedure Check_In_Previous_With_Clause |
429 | (N : Node_Id; | |
430 | Nam : Node_Id); | |
431 | -- N is a use_package clause and Nam the package name, or N is a use_type | |
432 | -- clause and Nam is the prefix of the type name. In either case, verify | |
433 | -- that the package is visible at that point in the context: either it | |
434 | -- appears in a previous with_clause, or because it is a fully qualified | |
435 | -- name and the root ancestor appears in a previous with_clause. | |
436 | ||
996ae0b0 RK |
437 | procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id); |
438 | -- Verify that the entity in a renaming declaration that is a library unit | |
439 | -- is itself a library unit and not a nested unit or subunit. Also check | |
440 | -- that if the renaming is a child unit of a generic parent, then the | |
441 | -- renamed unit must also be a child unit of that parent. Finally, verify | |
442 | -- that a renamed generic unit is not an implicit child declared within | |
443 | -- an instance of the parent. | |
444 | ||
445 | procedure Chain_Use_Clause (N : Node_Id); | |
d4810530 ES |
446 | -- Chain use clause onto list of uses clauses headed by First_Use_Clause in |
447 | -- the proper scope table entry. This is usually the current scope, but it | |
448 | -- will be an inner scope when installing the use clauses of the private | |
449 | -- declarations of a parent unit prior to compiling the private part of a | |
450 | -- child unit. This chain is traversed when installing/removing use clauses | |
451 | -- when compiling a subunit or instantiating a generic body on the fly, | |
452 | -- when it is necessary to save and restore full environments. | |
996ae0b0 | 453 | |
ac7d724d ES |
454 | function Enclosing_Instance return Entity_Id; |
455 | -- In an instance nested within another one, several semantic checks are | |
456 | -- unnecessary because the legality of the nested instance has been checked | |
457 | -- in the enclosing generic unit. This applies in particular to legality | |
458 | -- checks on actuals for formal subprograms of the inner instance, which | |
459 | -- are checked as subprogram renamings, and may be complicated by confusion | |
460 | -- in private/full views. This function returns the instance enclosing the | |
461 | -- current one if there is such, else it returns Empty. | |
462 | -- | |
463 | -- If the renaming determines the entity for the default of a formal | |
464 | -- subprogram nested within another instance, choose the innermost | |
465 | -- candidate. This is because if the formal has a box, and we are within | |
466 | -- an enclosing instance where some candidate interpretations are local | |
467 | -- to this enclosing instance, we know that the default was properly | |
468 | -- resolved when analyzing the generic, so we prefer the local | |
469 | -- candidates to those that are external. This is not always the case | |
470 | -- but is a reasonable heuristic on the use of nested generics. The | |
471 | -- proper solution requires a full renaming model. | |
472 | ||
996ae0b0 RK |
473 | function Has_Implicit_Character_Literal (N : Node_Id) return Boolean; |
474 | -- Find a type derived from Character or Wide_Character in the prefix of N. | |
475 | -- Used to resolved qualified names whose selector is a character literal. | |
476 | ||
9bc856dd | 477 | function Has_Private_With (E : Entity_Id) return Boolean; |
0ab80019 | 478 | -- Ada 2005 (AI-262): Determines if the current compilation unit has a |
cdc8c54c | 479 | -- private with on E. |
9bc856dd | 480 | |
fbf5a39b | 481 | procedure Find_Expanded_Name (N : Node_Id); |
1d801f21 | 482 | -- The input is a selected component known to be an expanded name. Verify |
30196a76 RD |
483 | -- legality of selector given the scope denoted by prefix, and change node |
484 | -- N into a expanded name with a properly set Entity field. | |
fbf5a39b | 485 | |
996ae0b0 RK |
486 | function Find_Renamed_Entity |
487 | (N : Node_Id; | |
488 | Nam : Node_Id; | |
489 | New_S : Entity_Id; | |
490 | Is_Actual : Boolean := False) return Entity_Id; | |
491 | -- Find the renamed entity that corresponds to the given parameter profile | |
492 | -- in a subprogram renaming declaration. The renamed entity may be an | |
493 | -- operator, a subprogram, an entry, or a protected operation. Is_Actual | |
494 | -- indicates that the renaming is the one generated for an actual subpro- | |
495 | -- gram in an instance, for which special visibility checks apply. | |
496 | ||
fbf5a39b | 497 | function Has_Implicit_Operator (N : Node_Id) return Boolean; |
f3d57416 | 498 | -- N is an expanded name whose selector is an operator name (e.g. P."+"). |
16ca248a ES |
499 | -- declarative part contains an implicit declaration of an operator if it |
500 | -- has a declaration of a type to which one of the predefined operators | |
501 | -- apply. The existence of this routine is an implementation artifact. A | |
502 | -- more straightforward but more space-consuming choice would be to make | |
503 | -- all inherited operators explicit in the symbol table. | |
fbf5a39b | 504 | |
996ae0b0 RK |
505 | procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id); |
506 | -- A subprogram defined by a renaming declaration inherits the parameter | |
507 | -- profile of the renamed entity. The subtypes given in the subprogram | |
508 | -- specification are discarded and replaced with those of the renamed | |
509 | -- subprogram, which are then used to recheck the default values. | |
510 | ||
fbf5a39b | 511 | function Is_Appropriate_For_Record (T : Entity_Id) return Boolean; |
16ca248a ES |
512 | -- Prefix is appropriate for record if it is of a record type, or an access |
513 | -- to such. | |
fbf5a39b AC |
514 | |
515 | function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean; | |
16ca248a ES |
516 | -- True if it is of a task type, a protected type, or else an access to one |
517 | -- of these types. | |
fbf5a39b | 518 | |
d4810530 | 519 | procedure Note_Redundant_Use (Clause : Node_Id); |
16ca248a ES |
520 | -- Mark the name in a use clause as redundant if the corresponding entity |
521 | -- is already use-visible. Emit a warning if the use clause comes from | |
522 | -- source and the proper warnings are enabled. | |
d4810530 | 523 | |
996ae0b0 | 524 | procedure Premature_Usage (N : Node_Id); |
bc41faa2 | 525 | -- Diagnose usage of an entity before it is visible |
996ae0b0 | 526 | |
fbf5a39b AC |
527 | procedure Use_One_Package (P : Entity_Id; N : Node_Id); |
528 | -- Make visible entities declared in package P potentially use-visible | |
529 | -- in the current context. Also used in the analysis of subunits, when | |
530 | -- re-installing use clauses of parent units. N is the use_clause that | |
531 | -- names P (and possibly other packages). | |
532 | ||
7ff2d234 | 533 | procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False); |
fbf5a39b | 534 | -- Id is the subtype mark from a use type clause. This procedure makes |
7ff2d234 AC |
535 | -- the primitive operators of the type potentially use-visible. The |
536 | -- boolean flag Installed indicates that the clause is being reinstalled | |
537 | -- after previous analysis, and primitive operations are already chained | |
538 | -- on the Used_Operations list of the clause. | |
fbf5a39b | 539 | |
996ae0b0 RK |
540 | procedure Write_Info; |
541 | -- Write debugging information on entities declared in current scope | |
542 | ||
996ae0b0 RK |
543 | -------------------------------- |
544 | -- Analyze_Exception_Renaming -- | |
545 | -------------------------------- | |
546 | ||
16ca248a ES |
547 | -- The language only allows a single identifier, but the tree holds an |
548 | -- identifier list. The parser has already issued an error message if | |
549 | -- there is more than one element in the list. | |
996ae0b0 RK |
550 | |
551 | procedure Analyze_Exception_Renaming (N : Node_Id) is | |
1af4455a HK |
552 | Id : constant Entity_Id := Defining_Entity (N); |
553 | Nam : constant Node_Id := Name (N); | |
996ae0b0 RK |
554 | |
555 | begin | |
ce5ba43a | 556 | Check_SPARK_05_Restriction ("exception renaming is not allowed", N); |
1d801f21 | 557 | |
996ae0b0 RK |
558 | Enter_Name (Id); |
559 | Analyze (Nam); | |
560 | ||
241ebe89 HK |
561 | Set_Ekind (Id, E_Exception); |
562 | Set_Etype (Id, Standard_Exception_Type); | |
563 | Set_Is_Pure (Id, Is_Pure (Current_Scope)); | |
996ae0b0 | 564 | |
241ebe89 HK |
565 | if Is_Entity_Name (Nam) |
566 | and then Present (Entity (Nam)) | |
567 | and then Ekind (Entity (Nam)) = E_Exception | |
996ae0b0 | 568 | then |
996ae0b0 RK |
569 | if Present (Renamed_Object (Entity (Nam))) then |
570 | Set_Renamed_Object (Id, Renamed_Object (Entity (Nam))); | |
571 | else | |
572 | Set_Renamed_Object (Id, Entity (Nam)); | |
573 | end if; | |
c5cec2fe | 574 | |
241ebe89 HK |
575 | -- The exception renaming declaration may become Ghost if it renames |
576 | -- a Ghost entity. | |
c5cec2fe | 577 | |
241ebe89 HK |
578 | Mark_Renaming_As_Ghost (N, Entity (Nam)); |
579 | else | |
580 | Error_Msg_N ("invalid exception name in renaming", Nam); | |
996ae0b0 | 581 | end if; |
42f1d661 AC |
582 | |
583 | -- Implementation-defined aspect specifications can appear in a renaming | |
584 | -- declaration, but not language-defined ones. The call to procedure | |
585 | -- Analyze_Aspect_Specifications will take care of this error check. | |
586 | ||
587 | if Has_Aspects (N) then | |
588 | Analyze_Aspect_Specifications (N, Id); | |
589 | end if; | |
996ae0b0 RK |
590 | end Analyze_Exception_Renaming; |
591 | ||
592 | --------------------------- | |
593 | -- Analyze_Expanded_Name -- | |
594 | --------------------------- | |
595 | ||
596 | procedure Analyze_Expanded_Name (N : Node_Id) is | |
597 | begin | |
16ca248a ES |
598 | -- If the entity pointer is already set, this is an internal node, or a |
599 | -- node that is analyzed more than once, after a tree modification. In | |
cdabbb52 HK |
600 | -- such a case there is no resolution to perform, just set the type. In |
601 | -- either case, start by analyzing the prefix. | |
f6f40114 AC |
602 | |
603 | Analyze (Prefix (N)); | |
996ae0b0 RK |
604 | |
605 | if Present (Entity (N)) then | |
606 | if Is_Type (Entity (N)) then | |
607 | Set_Etype (N, Entity (N)); | |
608 | else | |
609 | Set_Etype (N, Etype (Entity (N))); | |
610 | end if; | |
611 | ||
996ae0b0 RK |
612 | return; |
613 | else | |
614 | Find_Expanded_Name (N); | |
615 | end if; | |
0929eaeb AC |
616 | |
617 | Analyze_Dimension (N); | |
996ae0b0 RK |
618 | end Analyze_Expanded_Name; |
619 | ||
15ce9ca2 AC |
620 | --------------------------------------- |
621 | -- Analyze_Generic_Function_Renaming -- | |
622 | --------------------------------------- | |
996ae0b0 RK |
623 | |
624 | procedure Analyze_Generic_Function_Renaming (N : Node_Id) is | |
625 | begin | |
626 | Analyze_Generic_Renaming (N, E_Generic_Function); | |
627 | end Analyze_Generic_Function_Renaming; | |
628 | ||
15ce9ca2 AC |
629 | -------------------------------------- |
630 | -- Analyze_Generic_Package_Renaming -- | |
631 | -------------------------------------- | |
996ae0b0 RK |
632 | |
633 | procedure Analyze_Generic_Package_Renaming (N : Node_Id) is | |
634 | begin | |
e80f0cb0 RD |
635 | -- Test for the Text_IO special unit case here, since we may be renaming |
636 | -- one of the subpackages of Text_IO, then join common routine. | |
996ae0b0 | 637 | |
2bd67690 | 638 | Check_Text_IO_Special_Unit (Name (N)); |
996ae0b0 RK |
639 | |
640 | Analyze_Generic_Renaming (N, E_Generic_Package); | |
641 | end Analyze_Generic_Package_Renaming; | |
642 | ||
15ce9ca2 AC |
643 | ---------------------------------------- |
644 | -- Analyze_Generic_Procedure_Renaming -- | |
645 | ---------------------------------------- | |
996ae0b0 RK |
646 | |
647 | procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is | |
648 | begin | |
649 | Analyze_Generic_Renaming (N, E_Generic_Procedure); | |
650 | end Analyze_Generic_Procedure_Renaming; | |
651 | ||
652 | ------------------------------ | |
653 | -- Analyze_Generic_Renaming -- | |
654 | ------------------------------ | |
655 | ||
656 | procedure Analyze_Generic_Renaming | |
657 | (N : Node_Id; | |
658 | K : Entity_Kind) | |
659 | is | |
1af4455a | 660 | New_P : constant Entity_Id := Defining_Entity (N); |
996ae0b0 | 661 | Old_P : Entity_Id; |
ad4ba28b AC |
662 | |
663 | Inst : Boolean := False; | |
664 | -- Prevent junk warning | |
996ae0b0 RK |
665 | |
666 | begin | |
ed4a1468 RD |
667 | if Name (N) = Error then |
668 | return; | |
669 | end if; | |
670 | ||
ce5ba43a | 671 | Check_SPARK_05_Restriction ("generic renaming is not allowed", N); |
fe5d3068 | 672 | |
996ae0b0 RK |
673 | Generate_Definition (New_P); |
674 | ||
675 | if Current_Scope /= Standard_Standard then | |
676 | Set_Is_Pure (New_P, Is_Pure (Current_Scope)); | |
677 | end if; | |
678 | ||
679 | if Nkind (Name (N)) = N_Selected_Component then | |
680 | Check_Generic_Child_Unit (Name (N), Inst); | |
681 | else | |
682 | Analyze (Name (N)); | |
683 | end if; | |
684 | ||
685 | if not Is_Entity_Name (Name (N)) then | |
686 | Error_Msg_N ("expect entity name in renaming declaration", Name (N)); | |
687 | Old_P := Any_Id; | |
688 | else | |
689 | Old_P := Entity (Name (N)); | |
690 | end if; | |
691 | ||
692 | Enter_Name (New_P); | |
693 | Set_Ekind (New_P, K); | |
694 | ||
695 | if Etype (Old_P) = Any_Type then | |
696 | null; | |
697 | ||
698 | elsif Ekind (Old_P) /= K then | |
699 | Error_Msg_N ("invalid generic unit name", Name (N)); | |
700 | ||
701 | else | |
702 | if Present (Renamed_Object (Old_P)) then | |
241ebe89 | 703 | Set_Renamed_Object (New_P, Renamed_Object (Old_P)); |
996ae0b0 RK |
704 | else |
705 | Set_Renamed_Object (New_P, Old_P); | |
706 | end if; | |
707 | ||
923fa078 RD |
708 | Set_Is_Pure (New_P, Is_Pure (Old_P)); |
709 | Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P)); | |
710 | ||
996ae0b0 RK |
711 | Set_Etype (New_P, Etype (Old_P)); |
712 | Set_Has_Completion (New_P); | |
713 | ||
241ebe89 HK |
714 | -- The generic renaming declaration may become Ghost if it renames a |
715 | -- Ghost entity. | |
c5cec2fe | 716 | |
241ebe89 | 717 | Mark_Renaming_As_Ghost (N, Old_P); |
c5cec2fe | 718 | |
996ae0b0 RK |
719 | if In_Open_Scopes (Old_P) then |
720 | Error_Msg_N ("within its scope, generic denotes its instance", N); | |
721 | end if; | |
722 | ||
7b966a95 AC |
723 | -- For subprograms, propagate the Intrinsic flag, to allow, e.g. |
724 | -- renamings and subsequent instantiations of Unchecked_Conversion. | |
725 | ||
726 | if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then | |
727 | Set_Is_Intrinsic_Subprogram | |
728 | (New_P, Is_Intrinsic_Subprogram (Old_P)); | |
729 | end if; | |
730 | ||
996ae0b0 RK |
731 | Check_Library_Unit_Renaming (N, Old_P); |
732 | end if; | |
42f1d661 AC |
733 | |
734 | -- Implementation-defined aspect specifications can appear in a renaming | |
735 | -- declaration, but not language-defined ones. The call to procedure | |
736 | -- Analyze_Aspect_Specifications will take care of this error check. | |
737 | ||
738 | if Has_Aspects (N) then | |
739 | Analyze_Aspect_Specifications (N, New_P); | |
740 | end if; | |
996ae0b0 RK |
741 | end Analyze_Generic_Renaming; |
742 | ||
743 | ----------------------------- | |
744 | -- Analyze_Object_Renaming -- | |
745 | ----------------------------- | |
746 | ||
747 | procedure Analyze_Object_Renaming (N : Node_Id) is | |
1378bf10 | 748 | Id : constant Entity_Id := Defining_Identifier (N); |
241ebe89 | 749 | Loc : constant Source_Ptr := Sloc (N); |
1378bf10 | 750 | Nam : constant Node_Id := Name (N); |
241ebe89 | 751 | Dec : Node_Id; |
996ae0b0 RK |
752 | T : Entity_Id; |
753 | T2 : Entity_Id; | |
754 | ||
3f5a8fee AC |
755 | procedure Check_Constrained_Object; |
756 | -- If the nominal type is unconstrained but the renamed object is | |
757 | -- constrained, as can happen with renaming an explicit dereference or | |
758 | -- a function return, build a constrained subtype from the object. If | |
759 | -- the renaming is for a formal in an accept statement, the analysis | |
760 | -- has already established its actual subtype. This is only relevant | |
761 | -- if the renamed object is an explicit dereference. | |
762 | ||
3f5a8fee AC |
763 | ------------------------------ |
764 | -- Check_Constrained_Object -- | |
765 | ------------------------------ | |
766 | ||
767 | procedure Check_Constrained_Object is | |
b887f1a0 | 768 | Typ : constant Entity_Id := Etype (Nam); |
3f5a8fee AC |
769 | Subt : Entity_Id; |
770 | ||
771 | begin | |
e917aec2 | 772 | if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference) |
3f5a8fee AC |
773 | and then Is_Composite_Type (Etype (Nam)) |
774 | and then not Is_Constrained (Etype (Nam)) | |
775 | and then not Has_Unknown_Discriminants (Etype (Nam)) | |
776 | and then Expander_Active | |
777 | then | |
e917aec2 | 778 | -- If Actual_Subtype is already set, nothing to do |
3f5a8fee | 779 | |
e917aec2 | 780 | if Ekind_In (Id, E_Variable, E_Constant) |
3f5a8fee AC |
781 | and then Present (Actual_Subtype (Id)) |
782 | then | |
783 | null; | |
784 | ||
42f1d661 | 785 | -- A renaming of an unchecked union has no actual subtype |
14848f57 | 786 | |
b887f1a0 | 787 | elsif Is_Unchecked_Union (Typ) then |
14848f57 AC |
788 | null; |
789 | ||
2780d174 AC |
790 | -- If a record is limited its size is invariant. This is the case |
791 | -- in particular with record types with an access discirminant | |
792 | -- that are used in iterators. This is an optimization, but it | |
793 | -- also prevents typing anomalies when the prefix is further | |
b887f1a0 | 794 | -- expanded. Limited types with discriminants are included. |
2780d174 | 795 | |
b887f1a0 | 796 | elsif Is_Limited_Record (Typ) |
150ac76e AC |
797 | or else |
798 | (Ekind (Typ) = E_Limited_Private_Type | |
799 | and then Has_Discriminants (Typ) | |
800 | and then Is_Access_Type (Etype (First_Discriminant (Typ)))) | |
b887f1a0 | 801 | then |
2780d174 AC |
802 | null; |
803 | ||
3f5a8fee AC |
804 | else |
805 | Subt := Make_Temporary (Loc, 'T'); | |
806 | Remove_Side_Effects (Nam); | |
807 | Insert_Action (N, | |
808 | Make_Subtype_Declaration (Loc, | |
809 | Defining_Identifier => Subt, | |
810 | Subtype_Indication => | |
b887f1a0 | 811 | Make_Subtype_From_Expr (Nam, Typ))); |
3f5a8fee AC |
812 | Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc)); |
813 | Set_Etype (Nam, Subt); | |
303fbb20 AC |
814 | |
815 | -- Freeze subtype at once, to prevent order of elaboration | |
816 | -- issues in the backend. The renamed object exists, so its | |
817 | -- type is already frozen in any case. | |
818 | ||
819 | Freeze_Before (N, Subt); | |
3f5a8fee AC |
820 | end if; |
821 | end if; | |
822 | end Check_Constrained_Object; | |
823 | ||
923fa078 RD |
824 | -- Start of processing for Analyze_Object_Renaming |
825 | ||
996ae0b0 | 826 | begin |
ed4a1468 RD |
827 | if Nam = Error then |
828 | return; | |
829 | end if; | |
830 | ||
ce5ba43a | 831 | Check_SPARK_05_Restriction ("object renaming is not allowed", N); |
fe5d3068 | 832 | |
996ae0b0 RK |
833 | Set_Is_Pure (Id, Is_Pure (Current_Scope)); |
834 | Enter_Name (Id); | |
835 | ||
16ca248a ES |
836 | -- The renaming of a component that depends on a discriminant requires |
837 | -- an actual subtype, because in subsequent use of the object Gigi will | |
838 | -- be unable to locate the actual bounds. This explicit step is required | |
839 | -- when the renaming is generated in removing side effects of an | |
840 | -- already-analyzed expression. | |
996ae0b0 | 841 | |
42f1d661 | 842 | if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then |
996ae0b0 | 843 | T := Etype (Nam); |
c5cec2fe | 844 | Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam); |
996ae0b0 RK |
845 | |
846 | if Present (Dec) then | |
847 | Insert_Action (N, Dec); | |
848 | T := Defining_Identifier (Dec); | |
849 | Set_Etype (Nam, T); | |
850 | end if; | |
851 | ||
11560bcc | 852 | -- Complete analysis of the subtype mark in any case, for ASIS use |
fbe627af RD |
853 | |
854 | if Present (Subtype_Mark (N)) then | |
855 | Find_Type (Subtype_Mark (N)); | |
856 | end if; | |
857 | ||
6e937c1c AC |
858 | elsif Present (Subtype_Mark (N)) then |
859 | Find_Type (Subtype_Mark (N)); | |
860 | T := Entity (Subtype_Mark (N)); | |
fbe627af RD |
861 | Analyze (Nam); |
862 | ||
6cce2156 GD |
863 | -- Reject renamings of conversions unless the type is tagged, or |
864 | -- the conversion is implicit (which can occur for cases of anonymous | |
865 | -- access types in Ada 2012). | |
866 | ||
fbe627af | 867 | if Nkind (Nam) = N_Type_Conversion |
6cce2156 GD |
868 | and then Comes_From_Source (Nam) |
869 | and then not Is_Tagged_Type (T) | |
fbe627af RD |
870 | then |
871 | Error_Msg_N | |
872 | ("renaming of conversion only allowed for tagged types", Nam); | |
873 | end if; | |
874 | ||
875 | Resolve (Nam, T); | |
6e937c1c | 876 | |
1378bf10 AC |
877 | -- If the renamed object is a function call of a limited type, |
878 | -- the expansion of the renaming is complicated by the presence | |
879 | -- of various temporaries and subtypes that capture constraints | |
880 | -- of the renamed object. Rewrite node as an object declaration, | |
881 | -- whose expansion is simpler. Given that the object is limited | |
882 | -- there is no copy involved and no performance hit. | |
883 | ||
884 | if Nkind (Nam) = N_Function_Call | |
51245e2d | 885 | and then Is_Limited_View (Etype (Nam)) |
92db5dee | 886 | and then not Is_Constrained (Etype (Nam)) |
1378bf10 AC |
887 | and then Comes_From_Source (N) |
888 | then | |
889 | Set_Etype (Id, T); | |
890 | Set_Ekind (Id, E_Constant); | |
891 | Rewrite (N, | |
892 | Make_Object_Declaration (Loc, | |
893 | Defining_Identifier => Id, | |
894 | Constant_Present => True, | |
92db5dee | 895 | Object_Definition => New_Occurrence_Of (Etype (Nam), Loc), |
1378bf10 AC |
896 | Expression => Relocate_Node (Nam))); |
897 | return; | |
898 | end if; | |
899 | ||
6cce2156 GD |
900 | -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object |
901 | -- when renaming declaration has a named access type. The Ada 2012 | |
902 | -- coverage rules allow an anonymous access type in the context of | |
903 | -- an expected named general access type, but the renaming rules | |
904 | -- require the types to be the same. (An exception is when the type | |
905 | -- of the renaming is also an anonymous access type, which can only | |
906 | -- happen due to a renaming created by the expander.) | |
907 | ||
908 | if Nkind (Nam) = N_Type_Conversion | |
909 | and then not Comes_From_Source (Nam) | |
910 | and then Ekind (Etype (Expression (Nam))) = E_Anonymous_Access_Type | |
911 | and then Ekind (T) /= E_Anonymous_Access_Type | |
912 | then | |
913 | Wrong_Type (Expression (Nam), T); -- Should we give better error??? | |
914 | end if; | |
915 | ||
7d823354 ES |
916 | -- Check that a class-wide object is not being renamed as an object |
917 | -- of a specific type. The test for access types is needed to exclude | |
918 | -- cases where the renamed object is a dynamically tagged access | |
919 | -- result, such as occurs in certain expansions. | |
920 | ||
4755cce9 JM |
921 | if Is_Tagged_Type (T) then |
922 | Check_Dynamically_Tagged_Expression | |
923 | (Expr => Nam, | |
924 | Typ => T, | |
925 | Related_Nod => N); | |
7d823354 ES |
926 | end if; |
927 | ||
0ab80019 | 928 | -- Ada 2005 (AI-230/AI-254): Access renaming |
6e937c1c | 929 | |
9bc856dd | 930 | else pragma Assert (Present (Access_Definition (N))); |
6e937c1c AC |
931 | T := Access_Definition |
932 | (Related_Nod => N, | |
933 | N => Access_Definition (N)); | |
35b7fa6a | 934 | |
f16d05d9 AC |
935 | Analyze (Nam); |
936 | ||
937 | -- Ada 2005 AI05-105: if the declaration has an anonymous access | |
938 | -- type, the renamed object must also have an anonymous type, and | |
92db5dee AC |
939 | -- this is a name resolution rule. This was implicit in the last part |
940 | -- of the first sentence in 8.5.1(3/2), and is made explicit by this | |
941 | -- recent AI. | |
f16d05d9 AC |
942 | |
943 | if not Is_Overloaded (Nam) then | |
944 | if Ekind (Etype (Nam)) /= Ekind (T) then | |
945 | Error_Msg_N | |
a2dc5812 | 946 | ("expect anonymous access type in object renaming", N); |
f16d05d9 | 947 | end if; |
3c19e9be | 948 | |
f16d05d9 AC |
949 | else |
950 | declare | |
3c19e9be ES |
951 | I : Interp_Index; |
952 | It : Interp; | |
953 | Typ : Entity_Id := Empty; | |
e34ca162 | 954 | Seen : Boolean := False; |
f16d05d9 AC |
955 | |
956 | begin | |
957 | Get_First_Interp (Nam, I, It); | |
958 | while Present (It.Typ) loop | |
3c19e9be ES |
959 | |
960 | -- Renaming is ambiguous if more than one candidate | |
961 | -- interpretation is type-conformant with the context. | |
962 | ||
963 | if Ekind (It.Typ) = Ekind (T) then | |
964 | if Ekind (T) = E_Anonymous_Access_Subprogram_Type | |
e34ca162 AC |
965 | and then |
966 | Type_Conformant | |
967 | (Designated_Type (T), Designated_Type (It.Typ)) | |
f16d05d9 | 968 | then |
3c19e9be ES |
969 | if not Seen then |
970 | Seen := True; | |
971 | else | |
972 | Error_Msg_N | |
973 | ("ambiguous expression in renaming", Nam); | |
974 | end if; | |
975 | ||
976 | elsif Ekind (T) = E_Anonymous_Access_Type | |
e34ca162 AC |
977 | and then |
978 | Covers (Designated_Type (T), Designated_Type (It.Typ)) | |
3c19e9be ES |
979 | then |
980 | if not Seen then | |
981 | Seen := True; | |
982 | else | |
983 | Error_Msg_N | |
984 | ("ambiguous expression in renaming", Nam); | |
985 | end if; | |
986 | end if; | |
987 | ||
988 | if Covers (T, It.Typ) then | |
f16d05d9 AC |
989 | Typ := It.Typ; |
990 | Set_Etype (Nam, Typ); | |
991 | Set_Is_Overloaded (Nam, False); | |
992 | end if; | |
f16d05d9 AC |
993 | end if; |
994 | ||
995 | Get_Next_Interp (I, It); | |
996 | end loop; | |
997 | end; | |
998 | end if; | |
999 | ||
1000 | Resolve (Nam, T); | |
6e937c1c | 1001 | |
405b907c AC |
1002 | -- Do not perform the legality checks below when the resolution of |
1003 | -- the renaming name failed because the associated type is Any_Type. | |
1004 | ||
1005 | if Etype (Nam) = Any_Type then | |
1006 | null; | |
1007 | ||
d18bbd25 | 1008 | -- Ada 2005 (AI-231): In the case where the type is defined by an |
0fb2ea01 AC |
1009 | -- access_definition, the renamed entity shall be of an access-to- |
1010 | -- constant type if and only if the access_definition defines an | |
d18bbd25 | 1011 | -- access-to-constant type. ARM 8.5.1(4) |
35b7fa6a | 1012 | |
405b907c | 1013 | elsif Constant_Present (Access_Definition (N)) |
35b7fa6a AC |
1014 | and then not Is_Access_Constant (Etype (Nam)) |
1015 | then | |
405b907c | 1016 | Error_Msg_N |
d1aea5dd AC |
1017 | ("(Ada 2005): the renamed object is not access-to-constant " |
1018 | & "(RM 8.5.1(6))", N); | |
a2dc5812 AC |
1019 | |
1020 | elsif not Constant_Present (Access_Definition (N)) | |
1021 | and then Is_Access_Constant (Etype (Nam)) | |
1022 | then | |
405b907c AC |
1023 | Error_Msg_N |
1024 | ("(Ada 2005): the renamed object is not access-to-variable " | |
1025 | & "(RM 8.5.1(6))", N); | |
a2dc5812 AC |
1026 | end if; |
1027 | ||
1028 | if Is_Access_Subprogram_Type (Etype (Nam)) then | |
1029 | Check_Subtype_Conformant | |
1030 | (Designated_Type (T), Designated_Type (Etype (Nam))); | |
1031 | ||
1032 | elsif not Subtypes_Statically_Match | |
df3e68b1 HK |
1033 | (Designated_Type (T), |
1034 | Available_View (Designated_Type (Etype (Nam)))) | |
a2dc5812 AC |
1035 | then |
1036 | Error_Msg_N | |
1037 | ("subtype of renamed object does not statically match", N); | |
35b7fa6a | 1038 | end if; |
996ae0b0 RK |
1039 | end if; |
1040 | ||
426d2717 AC |
1041 | -- Special processing for renaming function return object. Some errors |
1042 | -- and warnings are produced only for calls that come from source. | |
fbe627af | 1043 | |
426d2717 | 1044 | if Nkind (Nam) = N_Function_Call then |
fbe627af RD |
1045 | case Ada_Version is |
1046 | ||
c05ba1f1 AC |
1047 | -- Usage is illegal in Ada 83, but renamings are also introduced |
1048 | -- during expansion, and error does not apply to those. | |
fbe627af RD |
1049 | |
1050 | when Ada_83 => | |
c05ba1f1 | 1051 | if Comes_From_Source (N) then |
426d2717 AC |
1052 | Error_Msg_N |
1053 | ("(Ada 83) cannot rename function return object", Nam); | |
1054 | end if; | |
fbe627af RD |
1055 | |
1056 | -- In Ada 95, warn for odd case of renaming parameterless function | |
426d2717 | 1057 | -- call if this is not a limited type (where this is useful). |
fbe627af RD |
1058 | |
1059 | when others => | |
1060 | if Warn_On_Object_Renames_Function | |
1061 | and then No (Parameter_Associations (Nam)) | |
1062 | and then not Is_Limited_Type (Etype (Nam)) | |
426d2717 | 1063 | and then Comes_From_Source (Nam) |
fbe627af RD |
1064 | then |
1065 | Error_Msg_N | |
dbfeb4fa | 1066 | ("renaming function result object is suspicious?R?", Nam); |
fbe627af | 1067 | Error_Msg_NE |
dbfeb4fa | 1068 | ("\function & will be called only once?R?", Nam, |
426d2717 | 1069 | Entity (Name (Nam))); |
ed2233dc | 1070 | Error_Msg_N -- CODEFIX |
dbfeb4fa RD |
1071 | ("\suggest using an initialized constant " |
1072 | & "object instead?R?", Nam); | |
fbe627af | 1073 | end if; |
fd366a46 | 1074 | |
fbe627af RD |
1075 | end case; |
1076 | end if; | |
1077 | ||
3f5a8fee AC |
1078 | Check_Constrained_Object; |
1079 | ||
16ca248a ES |
1080 | -- An object renaming requires an exact match of the type. Class-wide |
1081 | -- matching is not allowed. | |
996ae0b0 RK |
1082 | |
1083 | if Is_Class_Wide_Type (T) | |
1084 | and then Base_Type (Etype (Nam)) /= Base_Type (T) | |
1085 | then | |
1086 | Wrong_Type (Nam, T); | |
1087 | end if; | |
1088 | ||
1089 | T2 := Etype (Nam); | |
4de287c4 | 1090 | |
92db5dee | 1091 | -- Ada 2005 (AI-326): Handle wrong use of incomplete type |
4de287c4 ES |
1092 | |
1093 | if Nkind (Nam) = N_Explicit_Dereference | |
1094 | and then Ekind (Etype (T2)) = E_Incomplete_Type | |
1095 | then | |
3c829e3c ES |
1096 | Error_Msg_NE ("invalid use of incomplete type&", Id, T2); |
1097 | return; | |
426d2717 | 1098 | |
3c829e3c ES |
1099 | elsif Ekind (Etype (T)) = E_Incomplete_Type then |
1100 | Error_Msg_NE ("invalid use of incomplete type&", Id, T); | |
4de287c4 ES |
1101 | return; |
1102 | end if; | |
1103 | ||
923fa078 RD |
1104 | -- Ada 2005 (AI-327) |
1105 | ||
0791fbe9 | 1106 | if Ada_Version >= Ada_2005 |
923fa078 RD |
1107 | and then Nkind (Nam) = N_Attribute_Reference |
1108 | and then Attribute_Name (Nam) = Name_Priority | |
1109 | then | |
1110 | null; | |
1111 | ||
ac7d724d | 1112 | elsif Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then |
923fa078 | 1113 | declare |
426d2717 AC |
1114 | Nam_Decl : Node_Id; |
1115 | Nam_Ent : Entity_Id; | |
923fa078 RD |
1116 | |
1117 | begin | |
1118 | if Nkind (Nam) = N_Attribute_Reference then | |
1119 | Nam_Ent := Entity (Prefix (Nam)); | |
1120 | else | |
1121 | Nam_Ent := Entity (Nam); | |
1122 | end if; | |
1123 | ||
426d2717 | 1124 | Nam_Decl := Parent (Nam_Ent); |
923fa078 RD |
1125 | |
1126 | if Has_Null_Exclusion (N) | |
1127 | and then not Has_Null_Exclusion (Nam_Decl) | |
1128 | then | |
1129 | -- Ada 2005 (AI-423): If the object name denotes a generic | |
1130 | -- formal object of a generic unit G, and the object renaming | |
1131 | -- declaration occurs within the body of G or within the body | |
1132 | -- of a generic unit declared within the declarative region | |
fbe627af | 1133 | -- of G, then the declaration of the formal object of G must |
a3c39f83 | 1134 | -- have a null exclusion or a null-excluding subtype. |
923fa078 RD |
1135 | |
1136 | if Is_Formal_Object (Nam_Ent) | |
ac7d724d | 1137 | and then In_Generic_Scope (Id) |
923fa078 | 1138 | then |
a3c39f83 ES |
1139 | if not Can_Never_Be_Null (Etype (Nam_Ent)) then |
1140 | Error_Msg_N | |
1141 | ("renamed formal does not exclude `NULL` " | |
1142 | & "(RM 8.5.1(4.6/2))", N); | |
1143 | ||
1144 | elsif In_Package_Body (Scope (Id)) then | |
1145 | Error_Msg_N | |
1146 | ("formal object does not have a null exclusion" | |
1147 | & "(RM 8.5.1(4.6/2))", N); | |
1148 | end if; | |
923fa078 | 1149 | |
16ca248a ES |
1150 | -- Ada 2005 (AI-423): Otherwise, the subtype of the object name |
1151 | -- shall exclude null. | |
923fa078 | 1152 | |
21d27997 | 1153 | elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then |
fbe627af | 1154 | Error_Msg_N |
21d27997 RD |
1155 | ("renamed object does not exclude `NULL` " |
1156 | & "(RM 8.5.1(4.6/2))", N); | |
aecf0203 | 1157 | |
a3c39f83 ES |
1158 | -- An instance is illegal if it contains a renaming that |
1159 | -- excludes null, and the actual does not. The renaming | |
1160 | -- declaration has already indicated that the declaration | |
1161 | -- of the renamed actual in the instance will raise | |
1162 | -- constraint_error. | |
1163 | ||
426d2717 | 1164 | elsif Nkind (Nam_Decl) = N_Object_Declaration |
a3c39f83 | 1165 | and then In_Instance |
ac7d724d ES |
1166 | and then |
1167 | Present (Corresponding_Generic_Association (Nam_Decl)) | |
1168 | and then Nkind (Expression (Nam_Decl)) = | |
1169 | N_Raise_Constraint_Error | |
a3c39f83 ES |
1170 | then |
1171 | Error_Msg_N | |
1172 | ("renamed actual does not exclude `NULL` " | |
1173 | & "(RM 8.5.1(4.6/2))", N); | |
1174 | ||
1175 | -- Finally, if there is a null exclusion, the subtype mark | |
1176 | -- must not be null-excluding. | |
1177 | ||
1178 | elsif No (Access_Definition (N)) | |
426d2717 | 1179 | and then Can_Never_Be_Null (T) |
a3c39f83 | 1180 | then |
aecf0203 | 1181 | Error_Msg_NE |
a3c39f83 ES |
1182 | ("`NOT NULL` not allowed (& already excludes null)", |
1183 | N, T); | |
fa961f76 | 1184 | |
923fa078 | 1185 | end if; |
fa961f76 | 1186 | |
a3c39f83 ES |
1187 | elsif Can_Never_Be_Null (T) |
1188 | and then not Can_Never_Be_Null (Etype (Nam_Ent)) | |
1189 | then | |
1190 | Error_Msg_N | |
1191 | ("renamed object does not exclude `NULL` " | |
1192 | & "(RM 8.5.1(4.6/2))", N); | |
1193 | ||
fa961f76 ES |
1194 | elsif Has_Null_Exclusion (N) |
1195 | and then No (Access_Definition (N)) | |
1196 | and then Can_Never_Be_Null (T) | |
1197 | then | |
1198 | Error_Msg_NE | |
1199 | ("`NOT NULL` not allowed (& already excludes null)", N, T); | |
923fa078 RD |
1200 | end if; |
1201 | end; | |
1202 | end if; | |
1203 | ||
2db5b47e AC |
1204 | -- Set the Ekind of the entity, unless it has been set already, as is |
1205 | -- the case for the iteration object over a container with no variable | |
3e247e58 RD |
1206 | -- indexing. In that case it's been marked as a constant, and we do not |
1207 | -- want to change it to a variable. | |
2db5b47e AC |
1208 | |
1209 | if Ekind (Id) /= E_Constant then | |
1210 | Set_Ekind (Id, E_Variable); | |
1211 | end if; | |
996c8821 RD |
1212 | |
1213 | -- Initialize the object size and alignment. Note that we used to call | |
1214 | -- Init_Size_Align here, but that's wrong for objects which have only | |
a90bd866 | 1215 | -- an Esize, not an RM_Size field. |
996c8821 | 1216 | |
d15f9422 | 1217 | Init_Object_Size_Align (Id); |
996ae0b0 RK |
1218 | |
1219 | if T = Any_Type or else Etype (Nam) = Any_Type then | |
1220 | return; | |
1221 | ||
923fa078 RD |
1222 | -- Verify that the renamed entity is an object or a function call. It |
1223 | -- may have been rewritten in several ways. | |
996ae0b0 RK |
1224 | |
1225 | elsif Is_Object_Reference (Nam) then | |
124092ee AC |
1226 | if Comes_From_Source (N) then |
1227 | if Is_Dependent_Component_Of_Mutable_Object (Nam) then | |
1228 | Error_Msg_N | |
1229 | ("illegal renaming of discriminant-dependent component", Nam); | |
1230 | end if; | |
1231 | ||
1232 | -- If the renaming comes from source and the renamed object is a | |
1233 | -- dereference, then mark the prefix as needing debug information, | |
1234 | -- since it might have been rewritten hence internally generated | |
1235 | -- and Debug_Renaming_Declaration will link the renaming to it. | |
1236 | ||
1237 | if Nkind (Nam) = N_Explicit_Dereference | |
1238 | and then Is_Entity_Name (Prefix (Nam)) | |
1239 | then | |
1240 | Set_Debug_Info_Needed (Entity (Prefix (Nam))); | |
1241 | end if; | |
996ae0b0 RK |
1242 | end if; |
1243 | ||
1244 | -- A static function call may have been folded into a literal | |
1245 | ||
1246 | elsif Nkind (Original_Node (Nam)) = N_Function_Call | |
1247 | ||
28e18b4f AC |
1248 | -- When expansion is disabled, attribute reference is not rewritten |
1249 | -- as function call. Otherwise it may be rewritten as a conversion, | |
1250 | -- so check original node. | |
996ae0b0 RK |
1251 | |
1252 | or else (Nkind (Original_Node (Nam)) = N_Attribute_Reference | |
1253 | and then Is_Function_Attribute_Name | |
294ccb21 | 1254 | (Attribute_Name (Original_Node (Nam)))) |
996ae0b0 | 1255 | |
28e18b4f AC |
1256 | -- Weird but legal, equivalent to renaming a function call. Illegal |
1257 | -- if the literal is the result of constant-folding an attribute | |
1258 | -- reference that is not a function. | |
996ae0b0 RK |
1259 | |
1260 | or else (Is_Entity_Name (Nam) | |
d4810530 ES |
1261 | and then Ekind (Entity (Nam)) = E_Enumeration_Literal |
1262 | and then | |
1263 | Nkind (Original_Node (Nam)) /= N_Attribute_Reference) | |
996ae0b0 RK |
1264 | |
1265 | or else (Nkind (Nam) = N_Type_Conversion | |
ac7d724d | 1266 | and then Is_Tagged_Type (Entity (Subtype_Mark (Nam)))) |
996ae0b0 RK |
1267 | then |
1268 | null; | |
1269 | ||
923fa078 RD |
1270 | elsif Nkind (Nam) = N_Type_Conversion then |
1271 | Error_Msg_N | |
1272 | ("renaming of conversion only allowed for tagged types", Nam); | |
996ae0b0 | 1273 | |
923fa078 RD |
1274 | -- Ada 2005 (AI-327) |
1275 | ||
0791fbe9 | 1276 | elsif Ada_Version >= Ada_2005 |
923fa078 RD |
1277 | and then Nkind (Nam) = N_Attribute_Reference |
1278 | and then Attribute_Name (Nam) = Name_Priority | |
1279 | then | |
1280 | null; | |
1281 | ||
c42e1b17 | 1282 | -- Allow internally generated x'Ref resulting in N_Reference node |
21d27997 RD |
1283 | |
1284 | elsif Nkind (Nam) = N_Reference then | |
1285 | null; | |
1286 | ||
923fa078 RD |
1287 | else |
1288 | Error_Msg_N ("expect object name in renaming", Nam); | |
996ae0b0 RK |
1289 | end if; |
1290 | ||
1291 | Set_Etype (Id, T2); | |
1292 | ||
1293 | if not Is_Variable (Nam) then | |
1294 | Set_Ekind (Id, E_Constant); | |
fbf5a39b | 1295 | Set_Never_Set_In_Source (Id, True); |
996ae0b0 RK |
1296 | Set_Is_True_Constant (Id, True); |
1297 | end if; | |
1298 | ||
241ebe89 HK |
1299 | -- The object renaming declaration may become Ghost if it renames a |
1300 | -- Ghost entity. | |
c5cec2fe | 1301 | |
241ebe89 HK |
1302 | if Is_Entity_Name (Nam) then |
1303 | Mark_Renaming_As_Ghost (N, Entity (Nam)); | |
c5cec2fe AC |
1304 | end if; |
1305 | ||
28e18b4f AC |
1306 | -- The entity of the renaming declaration needs to reflect whether the |
1307 | -- renamed object is volatile. Is_Volatile is set if the renamed object | |
1308 | -- is volatile in the RM legality sense. | |
1309 | ||
1310 | Set_Is_Volatile (Id, Is_Volatile_Object (Nam)); | |
1311 | ||
c2cfccb1 | 1312 | -- Also copy settings of Atomic/Independent/Volatile_Full_Access |
6333ad3d AC |
1313 | |
1314 | if Is_Entity_Name (Nam) then | |
57abdadd EB |
1315 | Set_Is_Atomic (Id, Is_Atomic (Entity (Nam))); |
1316 | Set_Is_Independent (Id, Is_Independent (Entity (Nam))); | |
1317 | Set_Is_Volatile_Full_Access (Id, | |
1318 | Is_Volatile_Full_Access (Entity (Nam))); | |
6333ad3d AC |
1319 | end if; |
1320 | ||
28e18b4f AC |
1321 | -- Treat as volatile if we just set the Volatile flag |
1322 | ||
1323 | if Is_Volatile (Id) | |
1324 | ||
1325 | -- Or if we are renaming an entity which was marked this way | |
1326 | ||
1327 | -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ??? | |
1328 | ||
1329 | or else (Is_Entity_Name (Nam) | |
1330 | and then Treat_As_Volatile (Entity (Nam))) | |
1331 | then | |
1332 | Set_Treat_As_Volatile (Id, True); | |
1333 | end if; | |
1334 | ||
1335 | -- Now make the link to the renamed object | |
1336 | ||
996ae0b0 | 1337 | Set_Renamed_Object (Id, Nam); |
42f1d661 AC |
1338 | |
1339 | -- Implementation-defined aspect specifications can appear in a renaming | |
1340 | -- declaration, but not language-defined ones. The call to procedure | |
1341 | -- Analyze_Aspect_Specifications will take care of this error check. | |
1342 | ||
1343 | if Has_Aspects (N) then | |
1344 | Analyze_Aspect_Specifications (N, Id); | |
1345 | end if; | |
1346 | ||
1347 | -- Deal with dimensions | |
1348 | ||
dec6faf1 | 1349 | Analyze_Dimension (N); |
996ae0b0 RK |
1350 | end Analyze_Object_Renaming; |
1351 | ||
1352 | ------------------------------ | |
1353 | -- Analyze_Package_Renaming -- | |
1354 | ------------------------------ | |
1355 | ||
1356 | procedure Analyze_Package_Renaming (N : Node_Id) is | |
1357 | New_P : constant Entity_Id := Defining_Entity (N); | |
1358 | Old_P : Entity_Id; | |
1359 | Spec : Node_Id; | |
1360 | ||
1361 | begin | |
ed4a1468 RD |
1362 | if Name (N) = Error then |
1363 | return; | |
1364 | end if; | |
1365 | ||
e80f0cb0 | 1366 | -- Check for Text_IO special unit (we may be renaming a Text_IO child) |
996ae0b0 | 1367 | |
2bd67690 | 1368 | Check_Text_IO_Special_Unit (Name (N)); |
996ae0b0 RK |
1369 | |
1370 | if Current_Scope /= Standard_Standard then | |
1371 | Set_Is_Pure (New_P, Is_Pure (Current_Scope)); | |
1372 | end if; | |
1373 | ||
1374 | Enter_Name (New_P); | |
1375 | Analyze (Name (N)); | |
16ca248a | 1376 | |
996ae0b0 RK |
1377 | if Is_Entity_Name (Name (N)) then |
1378 | Old_P := Entity (Name (N)); | |
1379 | else | |
1380 | Old_P := Any_Id; | |
1381 | end if; | |
1382 | ||
1383 | if Etype (Old_P) = Any_Type then | |
426d2717 | 1384 | Error_Msg_N ("expect package name in renaming", Name (N)); |
657a9dd9 | 1385 | |
996ae0b0 RK |
1386 | elsif Ekind (Old_P) /= E_Package |
1387 | and then not (Ekind (Old_P) = E_Generic_Package | |
1388 | and then In_Open_Scopes (Old_P)) | |
1389 | then | |
1390 | if Ekind (Old_P) = E_Generic_Package then | |
1391 | Error_Msg_N | |
1392 | ("generic package cannot be renamed as a package", Name (N)); | |
1393 | else | |
1394 | Error_Msg_Sloc := Sloc (Old_P); | |
1395 | Error_Msg_NE | |
1396 | ("expect package name in renaming, found& declared#", | |
1397 | Name (N), Old_P); | |
1398 | end if; | |
1399 | ||
bc41faa2 | 1400 | -- Set basic attributes to minimize cascaded errors |
996ae0b0 RK |
1401 | |
1402 | Set_Ekind (New_P, E_Package); | |
1403 | Set_Etype (New_P, Standard_Void_Type); | |
1404 | ||
294ccb21 RD |
1405 | -- Here for OK package renaming |
1406 | ||
996ae0b0 | 1407 | else |
923fa078 RD |
1408 | -- Entities in the old package are accessible through the renaming |
1409 | -- entity. The simplest implementation is to have both packages share | |
1410 | -- the entity list. | |
996ae0b0 RK |
1411 | |
1412 | Set_Ekind (New_P, E_Package); | |
1413 | Set_Etype (New_P, Standard_Void_Type); | |
1414 | ||
1415 | if Present (Renamed_Object (Old_P)) then | |
caa64a44 | 1416 | Set_Renamed_Object (New_P, Renamed_Object (Old_P)); |
996ae0b0 | 1417 | else |
4de287c4 | 1418 | Set_Renamed_Object (New_P, Old_P); |
996ae0b0 RK |
1419 | end if; |
1420 | ||
1421 | Set_Has_Completion (New_P); | |
1422 | ||
caa64a44 AC |
1423 | Set_First_Entity (New_P, First_Entity (Old_P)); |
1424 | Set_Last_Entity (New_P, Last_Entity (Old_P)); | |
996ae0b0 RK |
1425 | Set_First_Private_Entity (New_P, First_Private_Entity (Old_P)); |
1426 | Check_Library_Unit_Renaming (N, Old_P); | |
1427 | Generate_Reference (Old_P, Name (N)); | |
1428 | ||
241ebe89 HK |
1429 | -- The package renaming declaration may become Ghost if it renames a |
1430 | -- Ghost entity. | |
c5cec2fe | 1431 | |
241ebe89 | 1432 | Mark_Renaming_As_Ghost (N, Old_P); |
c5cec2fe | 1433 | |
294ccb21 | 1434 | -- If the renaming is in the visible part of a package, then we set |
7d823354 | 1435 | -- Renamed_In_Spec for the renamed package, to prevent giving |
294ccb21 RD |
1436 | -- warnings about no entities referenced. Such a warning would be |
1437 | -- overenthusiastic, since clients can see entities in the renamed | |
1438 | -- package via the visible package renaming. | |
1439 | ||
1440 | declare | |
1441 | Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit); | |
1442 | begin | |
1443 | if Ekind (Ent) = E_Package | |
1444 | and then not In_Private_Part (Ent) | |
1445 | and then In_Extended_Main_Source_Unit (N) | |
1446 | and then Ekind (Old_P) = E_Package | |
1447 | then | |
1448 | Set_Renamed_In_Spec (Old_P); | |
1449 | end if; | |
1450 | end; | |
1451 | ||
996ae0b0 RK |
1452 | -- If this is the renaming declaration of a package instantiation |
1453 | -- within itself, it is the declaration that ends the list of actuals | |
1454 | -- for the instantiation. At this point, the subtypes that rename | |
1455 | -- the actuals are flagged as generic, to avoid spurious ambiguities | |
1456 | -- if the actuals for two distinct formals happen to coincide. If | |
1457 | -- the actual is a private type, the subtype has a private completion | |
1458 | -- that is flagged in the same fashion. | |
1459 | ||
1460 | -- Resolution is identical to what is was in the original generic. | |
1461 | -- On exit from the generic instance, these are turned into regular | |
1462 | -- subtypes again, so they are compatible with types in their class. | |
1463 | ||
1464 | if not Is_Generic_Instance (Old_P) then | |
1465 | return; | |
1466 | else | |
1467 | Spec := Specification (Unit_Declaration_Node (Old_P)); | |
1468 | end if; | |
1469 | ||
1470 | if Nkind (Spec) = N_Package_Specification | |
1471 | and then Present (Generic_Parent (Spec)) | |
1472 | and then Old_P = Current_Scope | |
1473 | and then Chars (New_P) = Chars (Generic_Parent (Spec)) | |
1474 | then | |
1475 | declare | |
16ca248a ES |
1476 | E : Entity_Id; |
1477 | ||
996ae0b0 | 1478 | begin |
16ca248a | 1479 | E := First_Entity (Old_P); |
ac7d724d | 1480 | while Present (E) and then E /= New_P loop |
996ae0b0 RK |
1481 | if Is_Type (E) |
1482 | and then Nkind (Parent (E)) = N_Subtype_Declaration | |
1483 | then | |
1484 | Set_Is_Generic_Actual_Type (E); | |
1485 | ||
1486 | if Is_Private_Type (E) | |
1487 | and then Present (Full_View (E)) | |
1488 | then | |
1489 | Set_Is_Generic_Actual_Type (Full_View (E)); | |
1490 | end if; | |
1491 | end if; | |
1492 | ||
1493 | Next_Entity (E); | |
1494 | end loop; | |
1495 | end; | |
1496 | end if; | |
1497 | end if; | |
42f1d661 AC |
1498 | |
1499 | -- Implementation-defined aspect specifications can appear in a renaming | |
1500 | -- declaration, but not language-defined ones. The call to procedure | |
1501 | -- Analyze_Aspect_Specifications will take care of this error check. | |
1502 | ||
1503 | if Has_Aspects (N) then | |
1504 | Analyze_Aspect_Specifications (N, New_P); | |
1505 | end if; | |
996ae0b0 RK |
1506 | end Analyze_Package_Renaming; |
1507 | ||
1508 | ------------------------------- | |
1509 | -- Analyze_Renamed_Character -- | |
1510 | ------------------------------- | |
1511 | ||
1512 | procedure Analyze_Renamed_Character | |
1513 | (N : Node_Id; | |
1514 | New_S : Entity_Id; | |
1515 | Is_Body : Boolean) | |
1516 | is | |
1517 | C : constant Node_Id := Name (N); | |
1518 | ||
1519 | begin | |
1520 | if Ekind (New_S) = E_Function then | |
1521 | Resolve (C, Etype (New_S)); | |
1522 | ||
1523 | if Is_Body then | |
1524 | Check_Frozen_Renaming (N, New_S); | |
1525 | end if; | |
1526 | ||
1527 | else | |
1528 | Error_Msg_N ("character literal can only be renamed as function", N); | |
1529 | end if; | |
1530 | end Analyze_Renamed_Character; | |
1531 | ||
1532 | --------------------------------- | |
1533 | -- Analyze_Renamed_Dereference -- | |
1534 | --------------------------------- | |
1535 | ||
1536 | procedure Analyze_Renamed_Dereference | |
1537 | (N : Node_Id; | |
1538 | New_S : Entity_Id; | |
1539 | Is_Body : Boolean) | |
1540 | is | |
1541 | Nam : constant Node_Id := Name (N); | |
1542 | P : constant Node_Id := Prefix (Nam); | |
1543 | Typ : Entity_Id; | |
fbf5a39b | 1544 | Ind : Interp_Index; |
996ae0b0 RK |
1545 | It : Interp; |
1546 | ||
1547 | begin | |
1548 | if not Is_Overloaded (P) then | |
996ae0b0 | 1549 | if Ekind (Etype (Nam)) /= E_Subprogram_Type |
bce79204 AC |
1550 | or else not Type_Conformant (Etype (Nam), New_S) |
1551 | then | |
996ae0b0 RK |
1552 | Error_Msg_N ("designated type does not match specification", P); |
1553 | else | |
fbf5a39b | 1554 | Resolve (P); |
996ae0b0 RK |
1555 | end if; |
1556 | ||
1557 | return; | |
1558 | ||
1559 | else | |
1560 | Typ := Any_Type; | |
fbf5a39b | 1561 | Get_First_Interp (Nam, Ind, It); |
996ae0b0 RK |
1562 | |
1563 | while Present (It.Nam) loop | |
1564 | ||
1565 | if Ekind (It.Nam) = E_Subprogram_Type | |
bce79204 AC |
1566 | and then Type_Conformant (It.Nam, New_S) |
1567 | then | |
996ae0b0 RK |
1568 | if Typ /= Any_Id then |
1569 | Error_Msg_N ("ambiguous renaming", P); | |
1570 | return; | |
1571 | else | |
1572 | Typ := It.Nam; | |
1573 | end if; | |
1574 | end if; | |
1575 | ||
fbf5a39b | 1576 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
1577 | end loop; |
1578 | ||
1579 | if Typ = Any_Type then | |
1580 | Error_Msg_N ("designated type does not match specification", P); | |
1581 | else | |
1582 | Resolve (N, Typ); | |
1583 | ||
1584 | if Is_Body then | |
1585 | Check_Frozen_Renaming (N, New_S); | |
1586 | end if; | |
1587 | end if; | |
1588 | end if; | |
1589 | end Analyze_Renamed_Dereference; | |
1590 | ||
1591 | --------------------------- | |
1592 | -- Analyze_Renamed_Entry -- | |
1593 | --------------------------- | |
1594 | ||
1595 | procedure Analyze_Renamed_Entry | |
1596 | (N : Node_Id; | |
1597 | New_S : Entity_Id; | |
1598 | Is_Body : Boolean) | |
1599 | is | |
c92e8586 AC |
1600 | Nam : constant Node_Id := Name (N); |
1601 | Sel : constant Node_Id := Selector_Name (Nam); | |
1602 | Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N)); | |
1603 | Old_S : Entity_Id; | |
996ae0b0 RK |
1604 | |
1605 | begin | |
1606 | if Entity (Sel) = Any_Id then | |
1607 | ||
bc41faa2 | 1608 | -- Selector is undefined on prefix. Error emitted already |
996ae0b0 RK |
1609 | |
1610 | Set_Has_Completion (New_S); | |
1611 | return; | |
1612 | end if; | |
1613 | ||
16ca248a | 1614 | -- Otherwise find renamed entity and build body of New_S as a call to it |
996ae0b0 RK |
1615 | |
1616 | Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S); | |
1617 | ||
1618 | if Old_S = Any_Id then | |
1619 | Error_Msg_N (" no subprogram or entry matches specification", N); | |
1620 | else | |
1621 | if Is_Body then | |
1622 | Check_Subtype_Conformant (New_S, Old_S, N); | |
1623 | Generate_Reference (New_S, Defining_Entity (N), 'b'); | |
1624 | Style.Check_Identifier (Defining_Entity (N), New_S); | |
725393ea ES |
1625 | |
1626 | else | |
d4810530 | 1627 | -- Only mode conformance required for a renaming_as_declaration |
725393ea ES |
1628 | |
1629 | Check_Mode_Conformant (New_S, Old_S, N); | |
996ae0b0 RK |
1630 | end if; |
1631 | ||
1632 | Inherit_Renamed_Profile (New_S, Old_S); | |
294ccb21 | 1633 | |
c92e8586 AC |
1634 | -- The prefix can be an arbitrary expression that yields a task or |
1635 | -- protected object, so it must be resolved. | |
294ccb21 RD |
1636 | |
1637 | Resolve (Prefix (Nam), Scope (Old_S)); | |
996ae0b0 RK |
1638 | end if; |
1639 | ||
1640 | Set_Convention (New_S, Convention (Old_S)); | |
1641 | Set_Has_Completion (New_S, Inside_A_Generic); | |
1642 | ||
c92e8586 AC |
1643 | -- AI05-0225: If the renamed entity is a procedure or entry of a |
1644 | -- protected object, the target object must be a variable. | |
1645 | ||
1646 | if Ekind (Scope (Old_S)) in Protected_Kind | |
1647 | and then Ekind (New_S) = E_Procedure | |
1648 | and then not Is_Variable (Prefix (Nam)) | |
1649 | then | |
1650 | if Is_Actual then | |
1651 | Error_Msg_N | |
1652 | ("target object of protected operation used as actual for " | |
1653 | & "formal procedure must be a variable", Nam); | |
1654 | else | |
1655 | Error_Msg_N | |
1656 | ("target object of protected operation renamed as procedure, " | |
1657 | & "must be a variable", Nam); | |
1658 | end if; | |
1659 | end if; | |
1660 | ||
996ae0b0 RK |
1661 | if Is_Body then |
1662 | Check_Frozen_Renaming (N, New_S); | |
1663 | end if; | |
1664 | end Analyze_Renamed_Entry; | |
1665 | ||
1666 | ----------------------------------- | |
1667 | -- Analyze_Renamed_Family_Member -- | |
1668 | ----------------------------------- | |
1669 | ||
1670 | procedure Analyze_Renamed_Family_Member | |
1671 | (N : Node_Id; | |
1672 | New_S : Entity_Id; | |
1673 | Is_Body : Boolean) | |
1674 | is | |
fbf5a39b AC |
1675 | Nam : constant Node_Id := Name (N); |
1676 | P : constant Node_Id := Prefix (Nam); | |
996ae0b0 RK |
1677 | Old_S : Entity_Id; |
1678 | ||
1679 | begin | |
1680 | if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family) | |
1681 | or else (Nkind (P) = N_Selected_Component | |
ac7d724d | 1682 | and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family) |
996ae0b0 RK |
1683 | then |
1684 | if Is_Entity_Name (P) then | |
1685 | Old_S := Entity (P); | |
1686 | else | |
1687 | Old_S := Entity (Selector_Name (P)); | |
1688 | end if; | |
1689 | ||
1690 | if not Entity_Matches_Spec (Old_S, New_S) then | |
1691 | Error_Msg_N ("entry family does not match specification", N); | |
1692 | ||
1693 | elsif Is_Body then | |
1694 | Check_Subtype_Conformant (New_S, Old_S, N); | |
1695 | Generate_Reference (New_S, Defining_Entity (N), 'b'); | |
1696 | Style.Check_Identifier (Defining_Entity (N), New_S); | |
1697 | end if; | |
16ca248a | 1698 | |
996ae0b0 RK |
1699 | else |
1700 | Error_Msg_N ("no entry family matches specification", N); | |
1701 | end if; | |
1702 | ||
1703 | Set_Has_Completion (New_S, Inside_A_Generic); | |
1704 | ||
1705 | if Is_Body then | |
1706 | Check_Frozen_Renaming (N, New_S); | |
1707 | end if; | |
1708 | end Analyze_Renamed_Family_Member; | |
1709 | ||
294ccb21 RD |
1710 | ----------------------------------------- |
1711 | -- Analyze_Renamed_Primitive_Operation -- | |
1712 | ----------------------------------------- | |
1713 | ||
1714 | procedure Analyze_Renamed_Primitive_Operation | |
1715 | (N : Node_Id; | |
1716 | New_S : Entity_Id; | |
1717 | Is_Body : Boolean) | |
1718 | is | |
1719 | Old_S : Entity_Id; | |
1720 | ||
1721 | function Conforms | |
1722 | (Subp : Entity_Id; | |
1723 | Ctyp : Conformance_Type) return Boolean; | |
1724 | -- Verify that the signatures of the renamed entity and the new entity | |
1725 | -- match. The first formal of the renamed entity is skipped because it | |
1726 | -- is the target object in any subsequent call. | |
1727 | ||
c5d00db0 AC |
1728 | -------------- |
1729 | -- Conforms -- | |
1730 | -------------- | |
1731 | ||
294ccb21 RD |
1732 | function Conforms |
1733 | (Subp : Entity_Id; | |
1734 | Ctyp : Conformance_Type) return Boolean | |
1735 | is | |
1736 | Old_F : Entity_Id; | |
1737 | New_F : Entity_Id; | |
1738 | ||
1739 | begin | |
1740 | if Ekind (Subp) /= Ekind (New_S) then | |
1741 | return False; | |
1742 | end if; | |
1743 | ||
1744 | Old_F := Next_Formal (First_Formal (Subp)); | |
1745 | New_F := First_Formal (New_S); | |
1746 | while Present (Old_F) and then Present (New_F) loop | |
1747 | if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then | |
1748 | return False; | |
1749 | end if; | |
1750 | ||
1751 | if Ctyp >= Mode_Conformant | |
1752 | and then Ekind (Old_F) /= Ekind (New_F) | |
1753 | then | |
1754 | return False; | |
1755 | end if; | |
1756 | ||
1757 | Next_Formal (New_F); | |
1758 | Next_Formal (Old_F); | |
1759 | end loop; | |
1760 | ||
1761 | return True; | |
1762 | end Conforms; | |
1763 | ||
c5d00db0 AC |
1764 | -- Start of processing for Analyze_Renamed_Primitive_Operation |
1765 | ||
294ccb21 RD |
1766 | begin |
1767 | if not Is_Overloaded (Selector_Name (Name (N))) then | |
1768 | Old_S := Entity (Selector_Name (Name (N))); | |
1769 | ||
1770 | if not Conforms (Old_S, Type_Conformant) then | |
1771 | Old_S := Any_Id; | |
1772 | end if; | |
1773 | ||
1774 | else | |
1775 | -- Find the operation that matches the given signature | |
1776 | ||
1777 | declare | |
1778 | It : Interp; | |
1779 | Ind : Interp_Index; | |
1780 | ||
1781 | begin | |
1782 | Old_S := Any_Id; | |
1783 | Get_First_Interp (Selector_Name (Name (N)), Ind, It); | |
1784 | ||
1785 | while Present (It.Nam) loop | |
1786 | if Conforms (It.Nam, Type_Conformant) then | |
1787 | Old_S := It.Nam; | |
1788 | end if; | |
1789 | ||
1790 | Get_Next_Interp (Ind, It); | |
1791 | end loop; | |
1792 | end; | |
1793 | end if; | |
1794 | ||
1795 | if Old_S = Any_Id then | |
1796 | Error_Msg_N (" no subprogram or entry matches specification", N); | |
1797 | ||
1798 | else | |
1799 | if Is_Body then | |
1800 | if not Conforms (Old_S, Subtype_Conformant) then | |
1801 | Error_Msg_N ("subtype conformance error in renaming", N); | |
1802 | end if; | |
1803 | ||
1804 | Generate_Reference (New_S, Defining_Entity (N), 'b'); | |
1805 | Style.Check_Identifier (Defining_Entity (N), New_S); | |
1806 | ||
1807 | else | |
1808 | -- Only mode conformance required for a renaming_as_declaration | |
1809 | ||
1810 | if not Conforms (Old_S, Mode_Conformant) then | |
1811 | Error_Msg_N ("mode conformance error in renaming", N); | |
1812 | end if; | |
c5d00db0 AC |
1813 | |
1814 | -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed | |
1815 | -- view of a subprogram is intrinsic, because the compiler has | |
1816 | -- to generate a wrapper for any call to it. If the name in a | |
1817 | -- subprogram renaming is a prefixed view, the entity is thus | |
1818 | -- intrinsic, and 'Access cannot be applied to it. | |
1819 | ||
1820 | Set_Convention (New_S, Convention_Intrinsic); | |
294ccb21 RD |
1821 | end if; |
1822 | ||
1823 | -- Inherit_Renamed_Profile (New_S, Old_S); | |
1824 | ||
1825 | -- The prefix can be an arbitrary expression that yields an | |
1826 | -- object, so it must be resolved. | |
1827 | ||
1828 | Resolve (Prefix (Name (N))); | |
1829 | end if; | |
1830 | end Analyze_Renamed_Primitive_Operation; | |
1831 | ||
996ae0b0 RK |
1832 | --------------------------------- |
1833 | -- Analyze_Subprogram_Renaming -- | |
1834 | --------------------------------- | |
1835 | ||
1836 | procedure Analyze_Subprogram_Renaming (N : Node_Id) is | |
fc193526 HK |
1837 | Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N); |
1838 | Is_Actual : constant Boolean := Present (Formal_Spec); | |
0ab80019 | 1839 | Nam : constant Node_Id := Name (N); |
923fa078 | 1840 | Save_AV : constant Ada_Version_Type := Ada_Version; |
fb620b37 | 1841 | Save_AVP : constant Node_Id := Ada_Version_Pragma; |
923fa078 RD |
1842 | Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit; |
1843 | Spec : constant Node_Id := Specification (N); | |
1844 | ||
fc193526 HK |
1845 | Old_S : Entity_Id := Empty; |
1846 | Rename_Spec : Entity_Id; | |
1847 | ||
1848 | procedure Build_Class_Wide_Wrapper | |
1849 | (Ren_Id : out Entity_Id; | |
1850 | Wrap_Id : out Entity_Id); | |
1851 | -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal | |
1852 | -- type with unknown discriminants and a generic primitive operation of | |
1853 | -- the said type with a box require special processing when the actual | |
1854 | -- is a class-wide type: | |
2feb1f84 | 1855 | -- |
fc193526 HK |
1856 | -- generic |
1857 | -- type Formal_Typ (<>) is private; | |
1858 | -- with procedure Prim_Op (Param : Formal_Typ) is <>; | |
1859 | -- package Gen is ... | |
2feb1f84 | 1860 | -- |
fc193526 | 1861 | -- package Inst is new Gen (Actual_Typ'Class); |
2feb1f84 | 1862 | -- |
fc193526 HK |
1863 | -- In this case the general renaming mechanism used in the prologue of |
1864 | -- an instance no longer applies: | |
2feb1f84 | 1865 | -- |
fc193526 | 1866 | -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op; |
2feb1f84 | 1867 | -- |
fc193526 | 1868 | -- The above is replaced the following wrapper/renaming combination: |
2feb1f84 | 1869 | -- |
6d0b56ad | 1870 | -- procedure Wrapper (Param : Formal_Typ) is -- wrapper |
fc193526 HK |
1871 | -- begin |
1872 | -- Prim_Op (Param); -- primitive | |
1873 | -- end Wrapper; | |
2feb1f84 | 1874 | -- |
6d0b56ad | 1875 | -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper; |
2feb1f84 | 1876 | -- |
fc193526 HK |
1877 | -- This transformation applies only if there is no explicit visible |
1878 | -- class-wide operation at the point of the instantiation. Ren_Id is | |
1879 | -- the entity of the renaming declaration. Wrap_Id is the entity of | |
1880 | -- the generated class-wide wrapper (or Any_Id). | |
1881 | ||
923fa078 RD |
1882 | procedure Check_Null_Exclusion |
1883 | (Ren : Entity_Id; | |
1884 | Sub : Entity_Id); | |
1885 | -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the | |
1886 | -- following AI rules: | |
16ca248a ES |
1887 | -- |
1888 | -- If Ren is a renaming of a formal subprogram and one of its | |
1889 | -- parameters has a null exclusion, then the corresponding formal | |
1890 | -- in Sub must also have one. Otherwise the subtype of the Sub's | |
1891 | -- formal parameter must exclude null. | |
1892 | -- | |
f3d57416 | 1893 | -- If Ren is a renaming of a formal function and its return |
16ca248a ES |
1894 | -- profile has a null exclusion, then Sub's return profile must |
1895 | -- have one. Otherwise the subtype of Sub's return profile must | |
1896 | -- exclude null. | |
996ae0b0 | 1897 | |
02e4edea AC |
1898 | procedure Freeze_Actual_Profile; |
1899 | -- In Ada 2012, enforce the freezing rule concerning formal incomplete | |
1900 | -- types: a callable entity freezes its profile, unless it has an | |
1901 | -- incomplete untagged formal (RM 13.14(10.2/3)). | |
1902 | ||
fc193526 HK |
1903 | function Has_Class_Wide_Actual return Boolean; |
1904 | -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a | |
1905 | -- defaulted formal subprogram where the actual for the controlling | |
1906 | -- formal type is class-wide. | |
1907 | ||
07fc65c4 | 1908 | function Original_Subprogram (Subp : Entity_Id) return Entity_Id; |
16ca248a ES |
1909 | -- Find renamed entity when the declaration is a renaming_as_body and |
1910 | -- the renamed entity may itself be a renaming_as_body. Used to enforce | |
1911 | -- rule that a renaming_as_body is illegal if the declaration occurs | |
1912 | -- before the subprogram it completes is frozen, and renaming indirectly | |
1913 | -- renames the subprogram itself.(Defect Report 8652/0027). | |
07fc65c4 | 1914 | |
fc193526 HK |
1915 | ------------------------------ |
1916 | -- Build_Class_Wide_Wrapper -- | |
1917 | ------------------------------ | |
1138cf59 | 1918 | |
fc193526 HK |
1919 | procedure Build_Class_Wide_Wrapper |
1920 | (Ren_Id : out Entity_Id; | |
1921 | Wrap_Id : out Entity_Id) | |
1922 | is | |
1138cf59 AC |
1923 | Loc : constant Source_Ptr := Sloc (N); |
1924 | ||
fc193526 HK |
1925 | function Build_Call |
1926 | (Subp_Id : Entity_Id; | |
1927 | Params : List_Id) return Node_Id; | |
1928 | -- Create a dispatching call to invoke routine Subp_Id with actuals | |
1929 | -- built from the parameter specifications of list Params. | |
1138cf59 | 1930 | |
fc193526 HK |
1931 | function Build_Spec (Subp_Id : Entity_Id) return Node_Id; |
1932 | -- Create a subprogram specification based on the subprogram profile | |
1933 | -- of Subp_Id. | |
1138cf59 | 1934 | |
fc193526 HK |
1935 | function Find_Primitive (Typ : Entity_Id) return Entity_Id; |
1936 | -- Find a primitive subprogram of type Typ which matches the profile | |
1937 | -- of the renaming declaration. | |
1138cf59 | 1938 | |
fc193526 HK |
1939 | procedure Interpretation_Error (Subp_Id : Entity_Id); |
1940 | -- Emit a continuation error message suggesting subprogram Subp_Id as | |
1941 | -- a possible interpretation. | |
1138cf59 | 1942 | |
b6621d10 AC |
1943 | function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean; |
1944 | -- Determine whether subprogram Subp_Id denotes the intrinsic "=" | |
1945 | -- operator. | |
1946 | ||
1947 | function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean; | |
1948 | -- Determine whether subprogram Subp_Id is a suitable candidate for | |
1949 | -- the role of a wrapped subprogram. | |
1950 | ||
fc193526 HK |
1951 | ---------------- |
1952 | -- Build_Call -- | |
1953 | ---------------- | |
1954 | ||
1955 | function Build_Call | |
1956 | (Subp_Id : Entity_Id; | |
1957 | Params : List_Id) return Node_Id | |
1958 | is | |
1959 | Actuals : constant List_Id := New_List; | |
1960 | Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc); | |
1961 | Formal : Node_Id; | |
1138cf59 AC |
1962 | |
1963 | begin | |
fc193526 HK |
1964 | -- Build the actual parameters of the call |
1965 | ||
1966 | Formal := First (Params); | |
1967 | while Present (Formal) loop | |
1138cf59 | 1968 | Append_To (Actuals, |
fc193526 | 1969 | Make_Identifier (Loc, Chars (Defining_Identifier (Formal)))); |
fc193526 | 1970 | Next (Formal); |
1138cf59 AC |
1971 | end loop; |
1972 | ||
fc193526 HK |
1973 | -- Generate: |
1974 | -- return Subp_Id (Actuals); | |
1975 | ||
1976 | if Ekind_In (Subp_Id, E_Function, E_Operator) then | |
1977 | return | |
1978 | Make_Simple_Return_Statement (Loc, | |
1979 | Expression => | |
1980 | Make_Function_Call (Loc, | |
1981 | Name => Call_Ref, | |
1982 | Parameter_Associations => Actuals)); | |
1983 | ||
1984 | -- Generate: | |
1985 | -- Subp_Id (Actuals); | |
1986 | ||
1138cf59 AC |
1987 | else |
1988 | return | |
1989 | Make_Procedure_Call_Statement (Loc, | |
fc193526 HK |
1990 | Name => Call_Ref, |
1991 | Parameter_Associations => Actuals); | |
1138cf59 | 1992 | end if; |
fc193526 | 1993 | end Build_Call; |
1138cf59 | 1994 | |
fc193526 HK |
1995 | ---------------- |
1996 | -- Build_Spec -- | |
1997 | ---------------- | |
1138cf59 | 1998 | |
fc193526 HK |
1999 | function Build_Spec (Subp_Id : Entity_Id) return Node_Id is |
2000 | Params : constant List_Id := Copy_Parameter_List (Subp_Id); | |
2001 | Spec_Id : constant Entity_Id := | |
6d0b56ad AC |
2002 | Make_Defining_Identifier (Loc, |
2003 | Chars => New_External_Name (Chars (Subp_Id), 'R')); | |
1138cf59 AC |
2004 | |
2005 | begin | |
fc193526 | 2006 | if Ekind (Formal_Spec) = E_Procedure then |
1138cf59 AC |
2007 | return |
2008 | Make_Procedure_Specification (Loc, | |
fc193526 HK |
2009 | Defining_Unit_Name => Spec_Id, |
2010 | Parameter_Specifications => Params); | |
1138cf59 AC |
2011 | else |
2012 | return | |
fc193526 HK |
2013 | Make_Function_Specification (Loc, |
2014 | Defining_Unit_Name => Spec_Id, | |
2015 | Parameter_Specifications => Params, | |
2016 | Result_Definition => | |
2017 | New_Copy_Tree (Result_Definition (Spec))); | |
1138cf59 | 2018 | end if; |
fc193526 HK |
2019 | end Build_Spec; |
2020 | ||
2021 | -------------------- | |
2022 | -- Find_Primitive -- | |
2023 | -------------------- | |
2024 | ||
2025 | function Find_Primitive (Typ : Entity_Id) return Entity_Id is | |
2026 | procedure Replace_Parameter_Types (Spec : Node_Id); | |
2027 | -- Given a specification Spec, replace all class-wide parameter | |
2028 | -- types with reference to type Typ. | |
1138cf59 | 2029 | |
fc193526 HK |
2030 | ----------------------------- |
2031 | -- Replace_Parameter_Types -- | |
2032 | ----------------------------- | |
2033 | ||
2034 | procedure Replace_Parameter_Types (Spec : Node_Id) is | |
2035 | Formal : Node_Id; | |
2036 | Formal_Id : Entity_Id; | |
2037 | Formal_Typ : Node_Id; | |
2038 | ||
2039 | begin | |
2040 | Formal := First (Parameter_Specifications (Spec)); | |
2041 | while Present (Formal) loop | |
2042 | Formal_Id := Defining_Identifier (Formal); | |
2043 | Formal_Typ := Parameter_Type (Formal); | |
2044 | ||
2045 | -- Create a new entity for each class-wide formal to prevent | |
2046 | -- aliasing with the original renaming. Replace the type of | |
2047 | -- such a parameter with the candidate type. | |
2048 | ||
2049 | if Nkind (Formal_Typ) = N_Identifier | |
2050 | and then Is_Class_Wide_Type (Etype (Formal_Typ)) | |
2051 | then | |
2052 | Set_Defining_Identifier (Formal, | |
2053 | Make_Defining_Identifier (Loc, Chars (Formal_Id))); | |
2054 | ||
2055 | Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc)); | |
2056 | end if; | |
2057 | ||
2058 | Next (Formal); | |
2059 | end loop; | |
2060 | end Replace_Parameter_Types; | |
2061 | ||
2062 | -- Local variables | |
2063 | ||
2064 | Alt_Ren : constant Node_Id := New_Copy_Tree (N); | |
2065 | Alt_Nam : constant Node_Id := Name (Alt_Ren); | |
2066 | Alt_Spec : constant Node_Id := Specification (Alt_Ren); | |
2067 | Subp_Id : Entity_Id; | |
2068 | ||
2069 | -- Start of processing for Find_Primitive | |
2070 | ||
2071 | begin | |
2072 | -- Each attempt to find a suitable primitive of a particular type | |
2073 | -- operates on its own copy of the original renaming. As a result | |
2074 | -- the original renaming is kept decoration and side-effect free. | |
2075 | ||
2076 | -- Inherit the overloaded status of the renamed subprogram name | |
2077 | ||
2078 | if Is_Overloaded (Nam) then | |
2079 | Set_Is_Overloaded (Alt_Nam); | |
2080 | Save_Interps (Nam, Alt_Nam); | |
2081 | end if; | |
2082 | ||
2083 | -- The copied renaming is hidden from visibility to prevent the | |
2084 | -- pollution of the enclosing context. | |
2085 | ||
2086 | Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R')); | |
2087 | ||
2088 | -- The types of all class-wide parameters must be changed to the | |
2089 | -- candidate type. | |
2090 | ||
2091 | Replace_Parameter_Types (Alt_Spec); | |
2092 | ||
2093 | -- Try to find a suitable primitive which matches the altered | |
2094 | -- profile of the renaming specification. | |
2095 | ||
2096 | Subp_Id := | |
2097 | Find_Renamed_Entity | |
2098 | (N => Alt_Ren, | |
2099 | Nam => Name (Alt_Ren), | |
2100 | New_S => Analyze_Subprogram_Specification (Alt_Spec), | |
2101 | Is_Actual => Is_Actual); | |
2102 | ||
2103 | -- Do not return Any_Id if the resolion of the altered profile | |
2104 | -- failed as this complicates further checks on the caller side, | |
2105 | -- return Empty instead. | |
2106 | ||
2107 | if Subp_Id = Any_Id then | |
2108 | return Empty; | |
2109 | else | |
2110 | return Subp_Id; | |
2111 | end if; | |
2112 | end Find_Primitive; | |
2113 | ||
2114 | -------------------------- | |
2115 | -- Interpretation_Error -- | |
2116 | -------------------------- | |
2117 | ||
2118 | procedure Interpretation_Error (Subp_Id : Entity_Id) is | |
2119 | begin | |
2120 | Error_Msg_Sloc := Sloc (Subp_Id); | |
b6621d10 AC |
2121 | |
2122 | if Is_Internal (Subp_Id) then | |
2123 | Error_Msg_NE | |
2124 | ("\\possible interpretation: predefined & #", | |
2125 | Spec, Formal_Spec); | |
2126 | else | |
2127 | Error_Msg_NE | |
2128 | ("\\possible interpretation: & defined #", Spec, Formal_Spec); | |
2129 | end if; | |
fc193526 HK |
2130 | end Interpretation_Error; |
2131 | ||
b6621d10 AC |
2132 | --------------------------- |
2133 | -- Is_Intrinsic_Equality -- | |
2134 | --------------------------- | |
2135 | ||
2136 | function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is | |
2137 | begin | |
2138 | return | |
2139 | Ekind (Subp_Id) = E_Operator | |
2140 | and then Chars (Subp_Id) = Name_Op_Eq | |
2141 | and then Is_Intrinsic_Subprogram (Subp_Id); | |
2142 | end Is_Intrinsic_Equality; | |
2143 | ||
2144 | --------------------------- | |
2145 | -- Is_Suitable_Candidate -- | |
2146 | --------------------------- | |
2147 | ||
2148 | function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is | |
2149 | begin | |
2150 | if No (Subp_Id) then | |
2151 | return False; | |
2152 | ||
2153 | -- An intrinsic subprogram is never a good candidate. This is an | |
2154 | -- indication of a missing primitive, either defined directly or | |
2155 | -- inherited from a parent tagged type. | |
2156 | ||
2157 | elsif Is_Intrinsic_Subprogram (Subp_Id) then | |
2158 | return False; | |
2159 | ||
2160 | else | |
2161 | return True; | |
2162 | end if; | |
2163 | end Is_Suitable_Candidate; | |
2164 | ||
fc193526 HK |
2165 | -- Local variables |
2166 | ||
2167 | Actual_Typ : Entity_Id := Empty; | |
2168 | -- The actual class-wide type for Formal_Typ | |
2169 | ||
b6621d10 | 2170 | CW_Prim_OK : Boolean; |
fc193526 | 2171 | CW_Prim_Op : Entity_Id; |
b6621d10 AC |
2172 | -- The class-wide subprogram (if available) which corresponds to the |
2173 | -- renamed generic formal subprogram. | |
fc193526 HK |
2174 | |
2175 | Formal_Typ : Entity_Id := Empty; | |
b6621d10 | 2176 | -- The generic formal type with unknown discriminants |
fc193526 | 2177 | |
b6621d10 | 2178 | Root_Prim_OK : Boolean; |
fc193526 | 2179 | Root_Prim_Op : Entity_Id; |
b6621d10 AC |
2180 | -- The root type primitive (if available) which corresponds to the |
2181 | -- renamed generic formal subprogram. | |
2182 | ||
2183 | Root_Typ : Entity_Id := Empty; | |
2184 | -- The root type of Actual_Typ | |
fc193526 HK |
2185 | |
2186 | Body_Decl : Node_Id; | |
2187 | Formal : Node_Id; | |
2188 | Prim_Op : Entity_Id; | |
2189 | Spec_Decl : Node_Id; | |
2190 | ||
2191 | -- Start of processing for Build_Class_Wide_Wrapper | |
1138cf59 AC |
2192 | |
2193 | begin | |
fc193526 HK |
2194 | -- Analyze the specification of the renaming in case the generation |
2195 | -- of the class-wide wrapper fails. | |
2196 | ||
2197 | Ren_Id := Analyze_Subprogram_Specification (Spec); | |
2198 | Wrap_Id := Any_Id; | |
2199 | ||
2200 | -- Do not attempt to build a wrapper if the renaming is in error | |
2201 | ||
2202 | if Error_Posted (Nam) then | |
2203 | return; | |
2204 | end if; | |
2205 | ||
2206 | -- Analyze the renamed name, but do not resolve it. The resolution is | |
b6621d10 | 2207 | -- completed once a suitable subprogram is found. |
fc193526 HK |
2208 | |
2209 | Analyze (Nam); | |
2210 | ||
b6621d10 AC |
2211 | -- When the renamed name denotes the intrinsic operator equals, the |
2212 | -- name must be treated as overloaded. This allows for a potential | |
2213 | -- match against the root type's predefined equality function. | |
2214 | ||
2215 | if Is_Intrinsic_Equality (Entity (Nam)) then | |
2216 | Set_Is_Overloaded (Nam); | |
2217 | Collect_Interps (Nam); | |
2218 | end if; | |
2219 | ||
fc193526 HK |
2220 | -- Step 1: Find the generic formal type with unknown discriminants |
2221 | -- and its corresponding class-wide actual type from the renamed | |
2222 | -- generic formal subprogram. | |
2223 | ||
2224 | Formal := First_Formal (Formal_Spec); | |
2225 | while Present (Formal) loop | |
2226 | if Has_Unknown_Discriminants (Etype (Formal)) | |
2227 | and then not Is_Class_Wide_Type (Etype (Formal)) | |
2228 | and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal))) | |
1138cf59 | 2229 | then |
fc193526 HK |
2230 | Formal_Typ := Etype (Formal); |
2231 | Actual_Typ := Get_Instance_Of (Formal_Typ); | |
b6621d10 | 2232 | Root_Typ := Etype (Actual_Typ); |
1138cf59 AC |
2233 | exit; |
2234 | end if; | |
2235 | ||
fc193526 | 2236 | Next_Formal (Formal); |
1138cf59 AC |
2237 | end loop; |
2238 | ||
fc193526 HK |
2239 | -- The specification of the generic formal subprogram should always |
2240 | -- contain a formal type with unknown discriminants whose actual is | |
2241 | -- a class-wide type, otherwise this indicates a failure in routine | |
2242 | -- Has_Class_Wide_Actual. | |
1138cf59 | 2243 | |
fc193526 | 2244 | pragma Assert (Present (Formal_Typ)); |
1138cf59 | 2245 | |
b6621d10 AC |
2246 | -- Step 2: Find the proper class-wide subprogram or primitive which |
2247 | -- corresponds to the renamed generic formal subprogram. | |
1138cf59 | 2248 | |
fc193526 | 2249 | CW_Prim_Op := Find_Primitive (Actual_Typ); |
b6621d10 AC |
2250 | CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op); |
2251 | Root_Prim_Op := Find_Primitive (Root_Typ); | |
2252 | Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op); | |
1138cf59 | 2253 | |
b6621d10 | 2254 | -- The class-wide actual type has two subprograms which correspond to |
fc193526 | 2255 | -- the renamed generic formal subprogram: |
1138cf59 | 2256 | |
fc193526 HK |
2257 | -- with procedure Prim_Op (Param : Formal_Typ); |
2258 | ||
2259 | -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited | |
2260 | -- procedure Prim_Op (Param : Actual_Typ'Class); | |
2261 | ||
b6621d10 AC |
2262 | -- Even though the declaration of the two subprograms is legal, a |
2263 | -- call to either one is ambiguous and therefore illegal. | |
fc193526 | 2264 | |
b6621d10 | 2265 | if CW_Prim_OK and Root_Prim_OK then |
fc193526 | 2266 | |
b6621d10 | 2267 | -- A user-defined primitive has precedence over a predefined one |
fc193526 | 2268 | |
b6621d10 AC |
2269 | if Is_Internal (CW_Prim_Op) |
2270 | and then not Is_Internal (Root_Prim_Op) | |
fc193526 | 2271 | then |
fc193526 | 2272 | Prim_Op := Root_Prim_Op; |
1138cf59 | 2273 | |
b6621d10 AC |
2274 | elsif Is_Internal (Root_Prim_Op) |
2275 | and then not Is_Internal (CW_Prim_Op) | |
2276 | then | |
fc193526 | 2277 | Prim_Op := CW_Prim_Op; |
1138cf59 | 2278 | |
fc193526 HK |
2279 | elsif CW_Prim_Op = Root_Prim_Op then |
2280 | Prim_Op := Root_Prim_Op; | |
1138cf59 | 2281 | |
b6621d10 AC |
2282 | -- Otherwise both candidate subprograms are user-defined and |
2283 | -- ambiguous. | |
1138cf59 | 2284 | |
fc193526 HK |
2285 | else |
2286 | Error_Msg_NE | |
2287 | ("ambiguous actual for generic subprogram &", | |
b6621d10 | 2288 | Spec, Formal_Spec); |
fc193526 | 2289 | Interpretation_Error (Root_Prim_Op); |
b6621d10 | 2290 | Interpretation_Error (CW_Prim_Op); |
fc193526 | 2291 | return; |
f4f92d9d | 2292 | end if; |
1138cf59 | 2293 | |
b6621d10 | 2294 | elsif CW_Prim_OK and not Root_Prim_OK then |
fc193526 HK |
2295 | Prim_Op := CW_Prim_Op; |
2296 | ||
b6621d10 AC |
2297 | elsif not CW_Prim_OK and Root_Prim_OK then |
2298 | Prim_Op := Root_Prim_Op; | |
2299 | ||
2300 | -- An intrinsic equality may act as a suitable candidate in the case | |
2301 | -- of a null type extension where the parent's equality is hidden. A | |
2302 | -- call to an intrinsic equality is expanded as dispatching. | |
2303 | ||
2304 | elsif Present (Root_Prim_Op) | |
2305 | and then Is_Intrinsic_Equality (Root_Prim_Op) | |
2306 | then | |
fc193526 HK |
2307 | Prim_Op := Root_Prim_Op; |
2308 | ||
b6621d10 | 2309 | -- Otherwise there are no candidate subprograms. Let the caller |
fc193526 HK |
2310 | -- diagnose the error. |
2311 | ||
2312 | else | |
2313 | return; | |
2314 | end if; | |
2315 | ||
6d0b56ad AC |
2316 | -- At this point resolution has taken place and the name is no longer |
2317 | -- overloaded. Mark the primitive as referenced. | |
fc193526 | 2318 | |
6d0b56ad | 2319 | Set_Is_Overloaded (Name (N), False); |
ad5edba5 | 2320 | Set_Referenced (Prim_Op); |
fc193526 HK |
2321 | |
2322 | -- Step 3: Create the declaration and the body of the wrapper, insert | |
2323 | -- all the pieces into the tree. | |
2324 | ||
2325 | Spec_Decl := | |
2326 | Make_Subprogram_Declaration (Loc, | |
2327 | Specification => Build_Spec (Ren_Id)); | |
6d0b56ad AC |
2328 | Insert_Before_And_Analyze (N, Spec_Decl); |
2329 | ||
2330 | -- If the operator carries an Eliminated pragma, indicate that the | |
2331 | -- wrapper is also to be eliminated, to prevent spurious error when | |
2332 | -- using gnatelim on programs that include box-initialization of | |
2333 | -- equality operators. | |
2334 | ||
2335 | Wrap_Id := Defining_Entity (Spec_Decl); | |
2336 | Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op)); | |
fc193526 HK |
2337 | |
2338 | Body_Decl := | |
2339 | Make_Subprogram_Body (Loc, | |
2340 | Specification => Build_Spec (Ren_Id), | |
2341 | Declarations => New_List, | |
2342 | Handled_Statement_Sequence => | |
2343 | Make_Handled_Sequence_Of_Statements (Loc, | |
2344 | Statements => New_List ( | |
2345 | Build_Call | |
2346 | (Subp_Id => Prim_Op, | |
2347 | Params => | |
2348 | Parameter_Specifications | |
2349 | (Specification (Spec_Decl)))))); | |
2350 | ||
fc193526 HK |
2351 | -- The generated body does not freeze and must be analyzed when the |
2352 | -- class-wide wrapper is frozen. The body is only needed if expansion | |
2353 | -- is enabled. | |
2354 | ||
2355 | if Expander_Active then | |
2356 | Append_Freeze_Action (Wrap_Id, Body_Decl); | |
1138cf59 AC |
2357 | end if; |
2358 | ||
6d0b56ad | 2359 | -- Step 4: The subprogram renaming aliases the wrapper |
1138cf59 | 2360 | |
6d0b56ad | 2361 | Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc)); |
fc193526 | 2362 | end Build_Class_Wide_Wrapper; |
1138cf59 | 2363 | |
923fa078 RD |
2364 | -------------------------- |
2365 | -- Check_Null_Exclusion -- | |
2366 | -------------------------- | |
2367 | ||
2368 | procedure Check_Null_Exclusion | |
2369 | (Ren : Entity_Id; | |
2370 | Sub : Entity_Id) | |
2371 | is | |
16ca248a ES |
2372 | Ren_Formal : Entity_Id; |
2373 | Sub_Formal : Entity_Id; | |
923fa078 RD |
2374 | |
2375 | begin | |
2376 | -- Parameter check | |
2377 | ||
16ca248a ES |
2378 | Ren_Formal := First_Formal (Ren); |
2379 | Sub_Formal := First_Formal (Sub); | |
ac7d724d | 2380 | while Present (Ren_Formal) and then Present (Sub_Formal) loop |
923fa078 RD |
2381 | if Has_Null_Exclusion (Parent (Ren_Formal)) |
2382 | and then | |
2383 | not (Has_Null_Exclusion (Parent (Sub_Formal)) | |
ac7d724d | 2384 | or else Can_Never_Be_Null (Etype (Sub_Formal))) |
923fa078 | 2385 | then |
fbe627af RD |
2386 | Error_Msg_NE |
2387 | ("`NOT NULL` required for parameter &", | |
2388 | Parent (Sub_Formal), Sub_Formal); | |
923fa078 RD |
2389 | end if; |
2390 | ||
2391 | Next_Formal (Ren_Formal); | |
2392 | Next_Formal (Sub_Formal); | |
2393 | end loop; | |
2394 | ||
2395 | -- Return profile check | |
2396 | ||
2397 | if Nkind (Parent (Ren)) = N_Function_Specification | |
2398 | and then Nkind (Parent (Sub)) = N_Function_Specification | |
2399 | and then Has_Null_Exclusion (Parent (Ren)) | |
ac7d724d ES |
2400 | and then not (Has_Null_Exclusion (Parent (Sub)) |
2401 | or else Can_Never_Be_Null (Etype (Sub))) | |
923fa078 | 2402 | then |
fbe627af RD |
2403 | Error_Msg_N |
2404 | ("return must specify `NOT NULL`", | |
2405 | Result_Definition (Parent (Sub))); | |
923fa078 RD |
2406 | end if; |
2407 | end Check_Null_Exclusion; | |
2408 | ||
02e4edea AC |
2409 | --------------------------- |
2410 | -- Freeze_Actual_Profile -- | |
2411 | --------------------------- | |
2412 | ||
2413 | procedure Freeze_Actual_Profile is | |
2414 | F : Entity_Id; | |
2415 | Has_Untagged_Inc : Boolean; | |
2416 | Instantiation_Node : constant Node_Id := Parent (N); | |
2417 | ||
2418 | begin | |
2419 | if Ada_Version >= Ada_2012 then | |
2420 | F := First_Formal (Formal_Spec); | |
2421 | Has_Untagged_Inc := False; | |
2422 | while Present (F) loop | |
2423 | if Ekind (Etype (F)) = E_Incomplete_Type | |
2424 | and then not Is_Tagged_Type (Etype (F)) | |
2425 | then | |
2426 | Has_Untagged_Inc := True; | |
2427 | exit; | |
2428 | end if; | |
2429 | ||
2430 | F := Next_Formal (F); | |
2431 | end loop; | |
2432 | ||
2433 | if Ekind (Formal_Spec) = E_Function | |
598a56c0 | 2434 | and then not Is_Tagged_Type (Etype (Formal_Spec)) |
02e4edea AC |
2435 | then |
2436 | Has_Untagged_Inc := True; | |
2437 | end if; | |
2438 | ||
2439 | if not Has_Untagged_Inc then | |
2440 | F := First_Formal (Old_S); | |
2441 | while Present (F) loop | |
2442 | Freeze_Before (Instantiation_Node, Etype (F)); | |
2443 | ||
2444 | if Is_Incomplete_Or_Private_Type (Etype (F)) | |
2445 | and then No (Underlying_Type (Etype (F))) | |
02e4edea | 2446 | then |
6ee07c61 AC |
2447 | -- Exclude generic types, or types derived from them. |
2448 | -- They will be frozen in the enclosing instance. | |
2449 | ||
2450 | if Is_Generic_Type (Etype (F)) | |
2451 | or else Is_Generic_Type (Root_Type (Etype (F))) | |
2452 | then | |
2453 | null; | |
598a56c0 ES |
2454 | |
2455 | -- A limited view of a type declared elsewhere needs no | |
2456 | -- freezing actions. | |
2457 | ||
2458 | elsif From_Limited_With (Etype (F)) then | |
2459 | null; | |
2460 | ||
6ee07c61 AC |
2461 | else |
2462 | Error_Msg_NE | |
2463 | ("type& must be frozen before this point", | |
67a90476 | 2464 | Instantiation_Node, Etype (F)); |
6ee07c61 | 2465 | end if; |
02e4edea AC |
2466 | end if; |
2467 | ||
2468 | F := Next_Formal (F); | |
2469 | end loop; | |
2470 | end if; | |
2471 | end if; | |
2472 | end Freeze_Actual_Profile; | |
2473 | ||
fd3d2680 AC |
2474 | --------------------------- |
2475 | -- Has_Class_Wide_Actual -- | |
2476 | --------------------------- | |
2477 | ||
2478 | function Has_Class_Wide_Actual return Boolean is | |
fc193526 HK |
2479 | Formal : Entity_Id; |
2480 | Formal_Typ : Entity_Id; | |
fd3d2680 AC |
2481 | |
2482 | begin | |
fc193526 HK |
2483 | if Is_Actual then |
2484 | Formal := First_Formal (Formal_Spec); | |
2485 | while Present (Formal) loop | |
2486 | Formal_Typ := Etype (Formal); | |
2487 | ||
2488 | if Has_Unknown_Discriminants (Formal_Typ) | |
2489 | and then not Is_Class_Wide_Type (Formal_Typ) | |
2490 | and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ)) | |
fd3d2680 AC |
2491 | then |
2492 | return True; | |
2493 | end if; | |
2494 | ||
fc193526 | 2495 | Next_Formal (Formal); |
fd3d2680 AC |
2496 | end loop; |
2497 | end if; | |
2498 | ||
2499 | return False; | |
2500 | end Has_Class_Wide_Actual; | |
2501 | ||
07fc65c4 GB |
2502 | ------------------------- |
2503 | -- Original_Subprogram -- | |
2504 | ------------------------- | |
2505 | ||
2506 | function Original_Subprogram (Subp : Entity_Id) return Entity_Id is | |
2507 | Orig_Decl : Node_Id; | |
2508 | Orig_Subp : Entity_Id; | |
2509 | ||
2510 | begin | |
2511 | -- First case: renamed entity is itself a renaming | |
2512 | ||
2513 | if Present (Alias (Subp)) then | |
2514 | return Alias (Subp); | |
2515 | ||
ac7d724d ES |
2516 | elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration |
2517 | and then Present (Corresponding_Body (Unit_Declaration_Node (Subp))) | |
07fc65c4 GB |
2518 | then |
2519 | -- Check if renamed entity is a renaming_as_body | |
2520 | ||
2521 | Orig_Decl := | |
2522 | Unit_Declaration_Node | |
2523 | (Corresponding_Body (Unit_Declaration_Node (Subp))); | |
2524 | ||
2525 | if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then | |
2526 | Orig_Subp := Entity (Name (Orig_Decl)); | |
2527 | ||
2528 | if Orig_Subp = Rename_Spec then | |
2529 | ||
bc41faa2 | 2530 | -- Circularity detected |
07fc65c4 GB |
2531 | |
2532 | return Orig_Subp; | |
2533 | ||
2534 | else | |
2535 | return (Original_Subprogram (Orig_Subp)); | |
2536 | end if; | |
2537 | else | |
2538 | return Subp; | |
2539 | end if; | |
2540 | else | |
2541 | return Subp; | |
2542 | end if; | |
2543 | end Original_Subprogram; | |
2544 | ||
fc193526 HK |
2545 | -- Local variables |
2546 | ||
fd3d2680 AC |
2547 | CW_Actual : constant Boolean := Has_Class_Wide_Actual; |
2548 | -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a | |
2549 | -- defaulted formal subprogram when the actual for a related formal | |
2550 | -- type is class-wide. | |
2551 | ||
1af4455a | 2552 | Inst_Node : Node_Id := Empty; |
fc193526 HK |
2553 | New_S : Entity_Id; |
2554 | ||
fbf5a39b | 2555 | -- Start of processing for Analyze_Subprogram_Renaming |
07fc65c4 | 2556 | |
996ae0b0 RK |
2557 | begin |
2558 | -- We must test for the attribute renaming case before the Analyze | |
2559 | -- call because otherwise Sem_Attr will complain that the attribute | |
2560 | -- is missing an argument when it is analyzed. | |
2561 | ||
2562 | if Nkind (Nam) = N_Attribute_Reference then | |
d239991f | 2563 | |
16ca248a ES |
2564 | -- In the case of an abstract formal subprogram association, rewrite |
2565 | -- an actual given by a stream attribute as the name of the | |
2566 | -- corresponding stream primitive of the type. | |
d239991f | 2567 | |
16ca248a ES |
2568 | -- In a generic context the stream operations are not generated, and |
2569 | -- this must be treated as a normal attribute reference, to be | |
2570 | -- expanded in subsequent instantiations. | |
d4810530 | 2571 | |
ac7d724d ES |
2572 | if Is_Actual |
2573 | and then Is_Abstract_Subprogram (Formal_Spec) | |
4460a9bc | 2574 | and then Expander_Active |
d4810530 | 2575 | then |
d239991f GD |
2576 | declare |
2577 | Stream_Prim : Entity_Id; | |
2578 | Prefix_Type : constant Entity_Id := Entity (Prefix (Nam)); | |
2579 | ||
2580 | begin | |
2581 | -- The class-wide forms of the stream attributes are not | |
2582 | -- primitive dispatching operations (even though they | |
2583 | -- internally dispatch to a stream attribute). | |
2584 | ||
2585 | if Is_Class_Wide_Type (Prefix_Type) then | |
2586 | Error_Msg_N | |
2587 | ("attribute must be a primitive dispatching operation", | |
2588 | Nam); | |
2589 | return; | |
2590 | end if; | |
2591 | ||
2592 | -- Retrieve the primitive subprogram associated with the | |
16ca248a ES |
2593 | -- attribute. This can only be a stream attribute, since those |
2594 | -- are the only ones that are dispatching (and the actual for | |
2595 | -- an abstract formal subprogram must be dispatching | |
2596 | -- operation). | |
d239991f | 2597 | |
ca811241 BD |
2598 | case Attribute_Name (Nam) is |
2599 | when Name_Input => | |
2600 | Stream_Prim := | |
2601 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input); | |
2602 | when Name_Output => | |
2603 | Stream_Prim := | |
2604 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output); | |
2605 | when Name_Read => | |
2606 | Stream_Prim := | |
2607 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read); | |
2608 | when Name_Write => | |
2609 | Stream_Prim := | |
2610 | Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write); | |
2611 | when others => | |
2612 | Error_Msg_N | |
2613 | ("attribute must be a primitive" | |
2614 | & " dispatching operation", Nam); | |
2615 | return; | |
2616 | end case; | |
6a4d72a6 | 2617 | |
ca811241 BD |
2618 | -- If no operation was found, and the type is limited, |
2619 | -- the user should have defined one. | |
6a4d72a6 | 2620 | |
ca811241 BD |
2621 | if No (Stream_Prim) then |
2622 | if Is_Limited_Type (Prefix_Type) then | |
2623 | Error_Msg_NE | |
2624 | ("stream operation not defined for type&", | |
2625 | N, Prefix_Type); | |
2626 | return; | |
2627 | ||
2628 | -- Otherwise, compiler should have generated default | |
2629 | ||
2630 | else | |
2631 | raise Program_Error; | |
2632 | end if; | |
2633 | end if; | |
d239991f GD |
2634 | |
2635 | -- Rewrite the attribute into the name of its corresponding | |
2636 | -- primitive dispatching subprogram. We can then proceed with | |
2637 | -- the usual processing for subprogram renamings. | |
2638 | ||
2639 | declare | |
2640 | Prim_Name : constant Node_Id := | |
2641 | Make_Identifier (Sloc (Nam), | |
2642 | Chars => Chars (Stream_Prim)); | |
2643 | begin | |
2644 | Set_Entity (Prim_Name, Stream_Prim); | |
2645 | Rewrite (Nam, Prim_Name); | |
2646 | Analyze (Nam); | |
2647 | end; | |
2648 | end; | |
2649 | ||
2650 | -- Normal processing for a renaming of an attribute | |
2651 | ||
2652 | else | |
2653 | Attribute_Renaming (N); | |
2654 | return; | |
2655 | end if; | |
996ae0b0 RK |
2656 | end if; |
2657 | ||
2658 | -- Check whether this declaration corresponds to the instantiation | |
82c80734 | 2659 | -- of a formal subprogram. |
996ae0b0 | 2660 | |
16ca248a ES |
2661 | -- If this is an instantiation, the corresponding actual is frozen and |
2662 | -- error messages can be made more precise. If this is a default | |
2663 | -- subprogram, the entity is already established in the generic, and is | |
2664 | -- not retrieved by visibility. If it is a default with a box, the | |
996ae0b0 | 2665 | -- candidate interpretations, if any, have been collected when building |
16ca248a ES |
2666 | -- the renaming declaration. If overloaded, the proper interpretation is |
2667 | -- determined in Find_Renamed_Entity. If the entity is an operator, | |
996ae0b0 RK |
2668 | -- Find_Renamed_Entity applies additional visibility checks. |
2669 | ||
d239991f GD |
2670 | if Is_Actual then |
2671 | Inst_Node := Unit_Declaration_Node (Formal_Spec); | |
996ae0b0 | 2672 | |
fd3d2680 AC |
2673 | -- Check whether the renaming is for a defaulted actual subprogram |
2674 | -- with a class-wide actual. | |
2675 | ||
1c85591c AC |
2676 | -- The class-wide wrapper is not needed in GNATprove_Mode and there |
2677 | -- is an external axiomatization on the package. | |
c3831524 AC |
2678 | |
2679 | if CW_Actual | |
2680 | and then Box_Present (Inst_Node) | |
1c85591c AC |
2681 | and then not |
2682 | (GNATprove_Mode | |
2683 | and then | |
2684 | Present (Containing_Package_With_Ext_Axioms (Formal_Spec))) | |
c3831524 | 2685 | then |
fc193526 | 2686 | Build_Class_Wide_Wrapper (New_S, Old_S); |
fd3d2680 AC |
2687 | |
2688 | elsif Is_Entity_Name (Nam) | |
996ae0b0 RK |
2689 | and then Present (Entity (Nam)) |
2690 | and then not Comes_From_Source (Nam) | |
2691 | and then not Is_Overloaded (Nam) | |
2692 | then | |
2693 | Old_S := Entity (Nam); | |
fbf5a39b | 2694 | New_S := Analyze_Subprogram_Specification (Spec); |
996ae0b0 | 2695 | |
2e071734 AC |
2696 | -- Operator case |
2697 | ||
2698 | if Ekind (Entity (Nam)) = E_Operator then | |
2699 | ||
2700 | -- Box present | |
2701 | ||
2702 | if Box_Present (Inst_Node) then | |
2703 | Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual); | |
2704 | ||
2705 | -- If there is an immediately visible homonym of the operator | |
2706 | -- and the declaration has a default, this is worth a warning | |
2707 | -- because the user probably did not intend to get the pre- | |
16ca248a ES |
2708 | -- defined operator, visible in the generic declaration. To |
2709 | -- find if there is an intended candidate, analyze the renaming | |
2710 | -- again in the current context. | |
2e071734 AC |
2711 | |
2712 | elsif Scope (Old_S) = Standard_Standard | |
2713 | and then Present (Default_Name (Inst_Node)) | |
2714 | then | |
2715 | declare | |
2716 | Decl : constant Node_Id := New_Copy_Tree (N); | |
2717 | Hidden : Entity_Id; | |
2718 | ||
2719 | begin | |
2720 | Set_Entity (Name (Decl), Empty); | |
2721 | Analyze (Name (Decl)); | |
2722 | Hidden := | |
2723 | Find_Renamed_Entity (Decl, Name (Decl), New_S, True); | |
2724 | ||
2725 | if Present (Hidden) | |
2726 | and then In_Open_Scopes (Scope (Hidden)) | |
2727 | and then Is_Immediately_Visible (Hidden) | |
2728 | and then Comes_From_Source (Hidden) | |
923fa078 | 2729 | and then Hidden /= Old_S |
2e071734 AC |
2730 | then |
2731 | Error_Msg_Sloc := Sloc (Hidden); | |
dbfeb4fa | 2732 | Error_Msg_N ("default subprogram is resolved " & |
2e071734 | 2733 | "in the generic declaration " & |
dbfeb4fa RD |
2734 | "(RM 12.6(17))??", N); |
2735 | Error_Msg_NE ("\and will not use & #??", N, Hidden); | |
2e071734 AC |
2736 | end if; |
2737 | end; | |
2738 | end if; | |
996ae0b0 RK |
2739 | end if; |
2740 | ||
2741 | else | |
2742 | Analyze (Nam); | |
fbf5a39b | 2743 | New_S := Analyze_Subprogram_Specification (Spec); |
996ae0b0 RK |
2744 | end if; |
2745 | ||
996ae0b0 RK |
2746 | else |
2747 | -- Renamed entity must be analyzed first, to avoid being hidden by | |
2748 | -- new name (which might be the same in a generic instance). | |
2749 | ||
2750 | Analyze (Nam); | |
2751 | ||
2752 | -- The renaming defines a new overloaded entity, which is analyzed | |
2753 | -- like a subprogram declaration. | |
2754 | ||
fbf5a39b | 2755 | New_S := Analyze_Subprogram_Specification (Spec); |
996ae0b0 RK |
2756 | end if; |
2757 | ||
2758 | if Current_Scope /= Standard_Standard then | |
2759 | Set_Is_Pure (New_S, Is_Pure (Current_Scope)); | |
2760 | end if; | |
2761 | ||
74c10109 AC |
2762 | -- Set SPARK mode from current context |
2763 | ||
2764 | Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma); | |
f145ece7 | 2765 | Set_SPARK_Pragma_Inherited (New_S); |
74c10109 | 2766 | |
996ae0b0 RK |
2767 | Rename_Spec := Find_Corresponding_Spec (N); |
2768 | ||
294ccb21 RD |
2769 | -- Case of Renaming_As_Body |
2770 | ||
996ae0b0 RK |
2771 | if Present (Rename_Spec) then |
2772 | ||
294ccb21 RD |
2773 | -- Renaming declaration is the completion of the declaration of |
2774 | -- Rename_Spec. We build an actual body for it at the freezing point. | |
996ae0b0 RK |
2775 | |
2776 | Set_Corresponding_Spec (N, Rename_Spec); | |
fbe627af | 2777 | |
ff81221b ES |
2778 | -- Deal with special case of stream functions of abstract types |
2779 | -- and interfaces. | |
294ccb21 | 2780 | |
d4810530 ES |
2781 | if Nkind (Unit_Declaration_Node (Rename_Spec)) = |
2782 | N_Abstract_Subprogram_Declaration | |
2783 | then | |
ff81221b ES |
2784 | -- Input stream functions are abstract if the object type is |
2785 | -- abstract. Similarly, all default stream functions for an | |
30783513 | 2786 | -- interface type are abstract. However, these subprograms may |
ff81221b ES |
2787 | -- receive explicit declarations in representation clauses, making |
2788 | -- the attribute subprograms usable as defaults in subsequent | |
2789 | -- type extensions. | |
d4810530 ES |
2790 | -- In this case we rewrite the declaration to make the subprogram |
2791 | -- non-abstract. We remove the previous declaration, and insert | |
2792 | -- the new one at the point of the renaming, to prevent premature | |
2793 | -- access to unfrozen types. The new declaration reuses the | |
2794 | -- specification of the previous one, and must not be analyzed. | |
2795 | ||
ff81221b ES |
2796 | pragma Assert |
2797 | (Is_Primitive (Entity (Nam)) | |
ac7d724d ES |
2798 | and then |
2799 | Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam)))); | |
d4810530 ES |
2800 | declare |
2801 | Old_Decl : constant Node_Id := | |
2802 | Unit_Declaration_Node (Rename_Spec); | |
2803 | New_Decl : constant Node_Id := | |
2804 | Make_Subprogram_Declaration (Sloc (N), | |
2805 | Specification => | |
2806 | Relocate_Node (Specification (Old_Decl))); | |
2807 | begin | |
2808 | Remove (Old_Decl); | |
2809 | Insert_After (N, New_Decl); | |
16ca248a | 2810 | Set_Is_Abstract_Subprogram (Rename_Spec, False); |
d4810530 ES |
2811 | Set_Analyzed (New_Decl); |
2812 | end; | |
2813 | end if; | |
2814 | ||
996ae0b0 RK |
2815 | Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S); |
2816 | ||
0ab80019 | 2817 | if Ada_Version = Ada_83 and then Comes_From_Source (N) then |
996ae0b0 RK |
2818 | Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N); |
2819 | end if; | |
2820 | ||
923fa078 | 2821 | Set_Convention (New_S, Convention (Rename_Spec)); |
996ae0b0 RK |
2822 | Check_Fully_Conformant (New_S, Rename_Spec); |
2823 | Set_Public_Status (New_S); | |
2824 | ||
294ccb21 RD |
2825 | -- The specification does not introduce new formals, but only |
2826 | -- repeats the formals of the original subprogram declaration. | |
2827 | -- For cross-reference purposes, and for refactoring tools, we | |
2828 | -- treat the formals of the renaming declaration as body formals. | |
2829 | ||
2830 | Reference_Body_Formals (Rename_Spec, New_S); | |
2831 | ||
4de287c4 ES |
2832 | -- Indicate that the entity in the declaration functions like the |
2833 | -- corresponding body, and is not a new entity. The body will be | |
2834 | -- constructed later at the freeze point, so indicate that the | |
2835 | -- completion has not been seen yet. | |
996ae0b0 RK |
2836 | |
2837 | Set_Ekind (New_S, E_Subprogram_Body); | |
2838 | New_S := Rename_Spec; | |
5eb10f25 | 2839 | Set_Has_Completion (Rename_Spec, False); |
996ae0b0 | 2840 | |
4de287c4 | 2841 | -- Ada 2005: check overriding indicator |
edd63e9b | 2842 | |
038140ed | 2843 | if Present (Overridden_Operation (Rename_Spec)) then |
235f4375 AC |
2844 | if Must_Not_Override (Specification (N)) then |
2845 | Error_Msg_NE | |
2846 | ("subprogram& overrides inherited operation", | |
2847 | N, Rename_Spec); | |
2848 | elsif | |
2849 | Style_Check and then not Must_Override (Specification (N)) | |
2850 | then | |
2851 | Style.Missing_Overriding (N, Rename_Spec); | |
2852 | end if; | |
edd63e9b | 2853 | |
235f4375 AC |
2854 | elsif Must_Override (Specification (N)) then |
2855 | Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec); | |
edd63e9b ES |
2856 | end if; |
2857 | ||
294ccb21 RD |
2858 | -- Normal subprogram renaming (not renaming as body) |
2859 | ||
996ae0b0 RK |
2860 | else |
2861 | Generate_Definition (New_S); | |
2862 | New_Overloaded_Entity (New_S); | |
edd63e9b | 2863 | |
996ae0b0 RK |
2864 | if Is_Entity_Name (Nam) |
2865 | and then Is_Intrinsic_Subprogram (Entity (Nam)) | |
2866 | then | |
2867 | null; | |
2868 | else | |
2869 | Check_Delayed_Subprogram (New_S); | |
2870 | end if; | |
2871 | end if; | |
2872 | ||
4de287c4 ES |
2873 | -- There is no need for elaboration checks on the new entity, which may |
2874 | -- be called before the next freezing point where the body will appear. | |
2875 | -- Elaboration checks refer to the real entity, not the one created by | |
2876 | -- the renaming declaration. | |
996ae0b0 | 2877 | |
ee6208f2 AC |
2878 | Set_Kill_Elaboration_Checks (New_S, True); |
2879 | ||
2880 | -- If we had a previous error, indicate a completely is present to stop | |
2881 | -- junk cascaded messages, but don't take any further action. | |
996ae0b0 RK |
2882 | |
2883 | if Etype (Nam) = Any_Type then | |
2884 | Set_Has_Completion (New_S); | |
2885 | return; | |
2886 | ||
ee6208f2 AC |
2887 | -- Case where name has the form of a selected component |
2888 | ||
996ae0b0 RK |
2889 | elsif Nkind (Nam) = N_Selected_Component then |
2890 | ||
ee6208f2 | 2891 | -- A name which has the form A.B can designate an entry of task A, a |
294ccb21 RD |
2892 | -- protected operation of protected object A, or finally a primitive |
2893 | -- operation of object A. In the later case, A is an object of some | |
2894 | -- tagged type, or an access type that denotes one such. To further | |
2895 | -- distinguish these cases, note that the scope of a task entry or | |
2896 | -- protected operation is type of the prefix. | |
996ae0b0 | 2897 | |
294ccb21 RD |
2898 | -- The prefix could be an overloaded function call that returns both |
2899 | -- kinds of operations. This overloading pathology is left to the | |
2900 | -- dedicated reader ??? | |
2901 | ||
2902 | declare | |
2903 | T : constant Entity_Id := Etype (Prefix (Nam)); | |
2904 | ||
2905 | begin | |
2906 | if Present (T) | |
2907 | and then | |
2908 | (Is_Tagged_Type (T) | |
2909 | or else | |
2910 | (Is_Access_Type (T) | |
ac7d724d | 2911 | and then Is_Tagged_Type (Designated_Type (T)))) |
294ccb21 RD |
2912 | and then Scope (Entity (Selector_Name (Nam))) /= T |
2913 | then | |
2914 | Analyze_Renamed_Primitive_Operation | |
2915 | (N, New_S, Present (Rename_Spec)); | |
2916 | return; | |
2917 | ||
2918 | else | |
2919 | -- Renamed entity is an entry or protected operation. For those | |
2920 | -- cases an explicit body is built (at the point of freezing of | |
2921 | -- this entity) that contains a call to the renamed entity. | |
2922 | ||
2923 | -- This is not allowed for renaming as body if the renamed | |
2924 | -- spec is already frozen (see RM 8.5.4(5) for details). | |
2925 | ||
ac7d724d | 2926 | if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then |
294ccb21 RD |
2927 | Error_Msg_N |
2928 | ("renaming-as-body cannot rename entry as subprogram", N); | |
2929 | Error_Msg_NE | |
2930 | ("\since & is already frozen (RM 8.5.4(5))", | |
2931 | N, Rename_Spec); | |
2932 | else | |
2933 | Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec)); | |
2934 | end if; | |
2935 | ||
2936 | return; | |
2937 | end if; | |
2938 | end; | |
996ae0b0 | 2939 | |
ee6208f2 AC |
2940 | -- Case where name is an explicit dereference X.all |
2941 | ||
996ae0b0 RK |
2942 | elsif Nkind (Nam) = N_Explicit_Dereference then |
2943 | ||
2944 | -- Renamed entity is designated by access_to_subprogram expression. | |
2945 | -- Must build body to encapsulate call, as in the entry case. | |
2946 | ||
2947 | Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec)); | |
2948 | return; | |
2949 | ||
ee6208f2 AC |
2950 | -- Indexed component |
2951 | ||
996ae0b0 RK |
2952 | elsif Nkind (Nam) = N_Indexed_Component then |
2953 | Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec)); | |
2954 | return; | |
2955 | ||
ee6208f2 AC |
2956 | -- Character literal |
2957 | ||
996ae0b0 RK |
2958 | elsif Nkind (Nam) = N_Character_Literal then |
2959 | Analyze_Renamed_Character (N, New_S, Present (Rename_Spec)); | |
2960 | return; | |
2961 | ||
fc193526 HK |
2962 | -- Only remaining case is where we have a non-entity name, or a renaming |
2963 | -- of some other non-overloadable entity. | |
ee6208f2 | 2964 | |
a3f2babd | 2965 | elsif not Is_Entity_Name (Nam) |
996ae0b0 RK |
2966 | or else not Is_Overloadable (Entity (Nam)) |
2967 | then | |
4674dd21 AC |
2968 | -- Do not mention the renaming if it comes from an instance |
2969 | ||
2970 | if not Is_Actual then | |
2971 | Error_Msg_N ("expect valid subprogram name in renaming", N); | |
4674dd21 AC |
2972 | else |
2973 | Error_Msg_NE ("no visible subprogram for formal&", N, Nam); | |
2974 | end if; | |
2975 | ||
996ae0b0 | 2976 | return; |
996ae0b0 RK |
2977 | end if; |
2978 | ||
11560bcc TQ |
2979 | -- Find the renamed entity that matches the given specification. Disable |
2980 | -- Ada_83 because there is no requirement of full conformance between | |
2981 | -- renamed entity and new entity, even though the same circuit is used. | |
2982 | ||
e80f0cb0 RD |
2983 | -- This is a bit of an odd case, which introduces a really irregular use |
2984 | -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do | |
2985 | -- this. ??? | |
11560bcc TQ |
2986 | |
2987 | Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95); | |
fb620b37 | 2988 | Ada_Version_Pragma := Empty; |
11560bcc TQ |
2989 | Ada_Version_Explicit := Ada_Version; |
2990 | ||
2991 | if No (Old_S) then | |
2992 | Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual); | |
2993 | ||
1366997b AC |
2994 | -- The visible operation may be an inherited abstract operation that |
2995 | -- was overridden in the private part, in which case a call will | |
2996 | -- dispatch to the overriding operation. Use the overriding one in | |
2997 | -- the renaming declaration, to prevent spurious errors below. | |
2998 | ||
2999 | if Is_Overloadable (Old_S) | |
3000 | and then Is_Abstract_Subprogram (Old_S) | |
3001 | and then No (DTC_Entity (Old_S)) | |
3002 | and then Present (Alias (Old_S)) | |
3003 | and then not Is_Abstract_Subprogram (Alias (Old_S)) | |
038140ed | 3004 | and then Present (Overridden_Operation (Alias (Old_S))) |
1366997b AC |
3005 | then |
3006 | Old_S := Alias (Old_S); | |
3007 | end if; | |
3008 | ||
11560bcc TQ |
3009 | -- When the renamed subprogram is overloaded and used as an actual |
3010 | -- of a generic, its entity is set to the first available homonym. | |
3011 | -- We must first disambiguate the name, then set the proper entity. | |
3012 | ||
1378bf10 | 3013 | if Is_Actual and then Is_Overloaded (Nam) then |
11560bcc TQ |
3014 | Set_Entity (Nam, Old_S); |
3015 | end if; | |
3016 | end if; | |
3017 | ||
4de287c4 ES |
3018 | -- Most common case: subprogram renames subprogram. No body is generated |
3019 | -- in this case, so we must indicate the declaration is complete as is. | |
b2c4d56d | 3020 | -- and inherit various attributes of the renamed subprogram. |
996ae0b0 RK |
3021 | |
3022 | if No (Rename_Spec) then | |
923fa078 | 3023 | Set_Has_Completion (New_S); |
b2c4d56d | 3024 | Set_Is_Imported (New_S, Is_Imported (Entity (Nam))); |
923fa078 RD |
3025 | Set_Is_Pure (New_S, Is_Pure (Entity (Nam))); |
3026 | Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam))); | |
3027 | ||
241ebe89 HK |
3028 | -- The subprogram renaming declaration may become Ghost if it renames |
3029 | -- a Ghost entity. | |
c5cec2fe | 3030 | |
241ebe89 | 3031 | Mark_Renaming_As_Ghost (N, Entity (Nam)); |
c5cec2fe | 3032 | |
923fa078 | 3033 | -- Ada 2005 (AI-423): Check the consistency of null exclusions |
11560bcc | 3034 | -- between a subprogram and its correct renaming. |
923fa078 | 3035 | |
11560bcc TQ |
3036 | -- Note: the Any_Id check is a guard that prevents compiler crashes |
3037 | -- when performing a null exclusion check between a renaming and a | |
3038 | -- renamed subprogram that has been found to be illegal. | |
3039 | ||
ac7d724d | 3040 | if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then |
923fa078 RD |
3041 | Check_Null_Exclusion |
3042 | (Ren => New_S, | |
3043 | Sub => Entity (Nam)); | |
3044 | end if; | |
90067a15 ES |
3045 | |
3046 | -- Enforce the Ada 2005 rule that the renamed entity cannot require | |
3047 | -- overriding. The flag Requires_Overriding is set very selectively | |
3048 | -- and misses some other illegal cases. The additional conditions | |
3049 | -- checked below are sufficient but not necessary ??? | |
3050 | ||
3051 | -- The rule does not apply to the renaming generated for an actual | |
3052 | -- subprogram in an instance. | |
3053 | ||
3054 | if Is_Actual then | |
3055 | null; | |
3056 | ||
f3d57416 | 3057 | -- Guard against previous errors, and omit renamings of predefined |
90067a15 ES |
3058 | -- operators. |
3059 | ||
bce79204 | 3060 | elsif not Ekind_In (Old_S, E_Function, E_Procedure) then |
90067a15 ES |
3061 | null; |
3062 | ||
3063 | elsif Requires_Overriding (Old_S) | |
3064 | or else | |
3065 | (Is_Abstract_Subprogram (Old_S) | |
3066 | and then Present (Find_Dispatching_Type (Old_S)) | |
3067 | and then | |
3068 | not Is_Abstract_Type (Find_Dispatching_Type (Old_S))) | |
3069 | then | |
3070 | Error_Msg_N | |
3071 | ("renamed entity cannot be " | |
3072 | & "subprogram that requires overriding (RM 8.5.4 (5.1))", N); | |
3073 | end if; | |
996ae0b0 RK |
3074 | end if; |
3075 | ||
996ae0b0 | 3076 | if Old_S /= Any_Id then |
1378bf10 | 3077 | if Is_Actual and then From_Default (N) then |
ff4f0ed0 | 3078 | |
996ae0b0 RK |
3079 | -- This is an implicit reference to the default actual |
3080 | ||
3081 | Generate_Reference (Old_S, Nam, Typ => 'i', Force => True); | |
ff4f0ed0 | 3082 | |
996ae0b0 RK |
3083 | else |
3084 | Generate_Reference (Old_S, Nam); | |
3085 | end if; | |
3086 | ||
c92e8586 AC |
3087 | Check_Internal_Protected_Use (N, Old_S); |
3088 | ||
edd63e9b ES |
3089 | -- For a renaming-as-body, require subtype conformance, but if the |
3090 | -- declaration being completed has not been frozen, then inherit the | |
3091 | -- convention of the renamed subprogram prior to checking conformance | |
3092 | -- (unless the renaming has an explicit convention established; the | |
996ae0b0 RK |
3093 | -- rule stated in the RM doesn't seem to address this ???). |
3094 | ||
3095 | if Present (Rename_Spec) then | |
3096 | Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b'); | |
3097 | Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec); | |
3098 | ||
07fc65c4 GB |
3099 | if not Is_Frozen (Rename_Spec) then |
3100 | if not Has_Convention_Pragma (Rename_Spec) then | |
3101 | Set_Convention (New_S, Convention (Old_S)); | |
3102 | end if; | |
3103 | ||
3104 | if Ekind (Old_S) /= E_Operator then | |
3105 | Check_Mode_Conformant (New_S, Old_S, Spec); | |
3106 | end if; | |
3107 | ||
3108 | if Original_Subprogram (Old_S) = Rename_Spec then | |
3109 | Error_Msg_N ("unfrozen subprogram cannot rename itself ", N); | |
3110 | end if; | |
3111 | else | |
3112 | Check_Subtype_Conformant (New_S, Old_S, Spec); | |
996ae0b0 RK |
3113 | end if; |
3114 | ||
3115 | Check_Frozen_Renaming (N, Rename_Spec); | |
996ae0b0 | 3116 | |
5eb10f25 | 3117 | -- Check explicitly that renamed entity is not intrinsic, because |
16b05213 | 3118 | -- in a generic the renamed body is not built. In this case, |
5eb10f25 ES |
3119 | -- the renaming_as_body is a completion. |
3120 | ||
3121 | if Inside_A_Generic then | |
3122 | if Is_Frozen (Rename_Spec) | |
3123 | and then Is_Intrinsic_Subprogram (Old_S) | |
3124 | then | |
3125 | Error_Msg_N | |
3126 | ("subprogram in renaming_as_body cannot be intrinsic", | |
3127 | Name (N)); | |
3128 | end if; | |
3129 | ||
3130 | Set_Has_Completion (Rename_Spec); | |
3131 | end if; | |
3132 | ||
996ae0b0 | 3133 | elsif Ekind (Old_S) /= E_Operator then |
1138cf59 | 3134 | |
2ba431e5 YM |
3135 | -- If this a defaulted subprogram for a class-wide actual there is |
3136 | -- no check for mode conformance, given that the signatures don't | |
3e24afaa | 3137 | -- match (the source mentions T but the actual mentions T'Class). |
1138cf59 | 3138 | |
afc8324d | 3139 | if CW_Actual then |
1138cf59 | 3140 | null; |
ac7d724d | 3141 | elsif not Is_Actual or else No (Enclosing_Instance) then |
1138cf59 AC |
3142 | Check_Mode_Conformant (New_S, Old_S); |
3143 | end if; | |
996ae0b0 | 3144 | |
ac7d724d | 3145 | if Is_Actual and then Error_Posted (New_S) then |
996ae0b0 RK |
3146 | Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S); |
3147 | end if; | |
3148 | end if; | |
3149 | ||
3150 | if No (Rename_Spec) then | |
3151 | ||
3152 | -- The parameter profile of the new entity is that of the renamed | |
3153 | -- entity: the subtypes given in the specification are irrelevant. | |
3154 | ||
3155 | Inherit_Renamed_Profile (New_S, Old_S); | |
3156 | ||
3157 | -- A call to the subprogram is transformed into a call to the | |
3158 | -- renamed entity. This is transitive if the renamed entity is | |
3159 | -- itself a renaming. | |
3160 | ||
3161 | if Present (Alias (Old_S)) then | |
3162 | Set_Alias (New_S, Alias (Old_S)); | |
3163 | else | |
3164 | Set_Alias (New_S, Old_S); | |
3165 | end if; | |
3166 | ||
edd63e9b ES |
3167 | -- Note that we do not set Is_Intrinsic_Subprogram if we have a |
3168 | -- renaming as body, since the entity in this case is not an | |
3169 | -- intrinsic (it calls an intrinsic, but we have a real body for | |
3170 | -- this call, and it is in this body that the required intrinsic | |
3171 | -- processing will take place). | |
996ae0b0 | 3172 | |
edd63e9b ES |
3173 | -- Also, if this is a renaming of inequality, the renamed operator |
3174 | -- is intrinsic, but what matters is the corresponding equality | |
3175 | -- operator, which may be user-defined. | |
fbf5a39b | 3176 | |
996ae0b0 | 3177 | Set_Is_Intrinsic_Subprogram |
fbf5a39b | 3178 | (New_S, |
ac7d724d ES |
3179 | Is_Intrinsic_Subprogram (Old_S) |
3180 | and then | |
3181 | (Chars (Old_S) /= Name_Op_Ne | |
3182 | or else Ekind (Old_S) = E_Operator | |
3183 | or else Is_Intrinsic_Subprogram | |
3184 | (Corresponding_Equality (Old_S)))); | |
996ae0b0 RK |
3185 | |
3186 | if Ekind (Alias (New_S)) = E_Operator then | |
3187 | Set_Has_Delayed_Freeze (New_S, False); | |
3188 | end if; | |
3189 | ||
82c80734 RD |
3190 | -- If the renaming corresponds to an association for an abstract |
3191 | -- formal subprogram, then various attributes must be set to | |
3192 | -- indicate that the renaming is an abstract dispatching operation | |
3193 | -- with a controlling type. | |
3194 | ||
16ca248a ES |
3195 | if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then |
3196 | ||
82c80734 RD |
3197 | -- Mark the renaming as abstract here, so Find_Dispatching_Type |
3198 | -- see it as corresponding to a generic association for a | |
3199 | -- formal abstract subprogram | |
3200 | ||
16ca248a | 3201 | Set_Is_Abstract_Subprogram (New_S); |
82c80734 RD |
3202 | |
3203 | declare | |
3204 | New_S_Ctrl_Type : constant Entity_Id := | |
3205 | Find_Dispatching_Type (New_S); | |
3206 | Old_S_Ctrl_Type : constant Entity_Id := | |
3207 | Find_Dispatching_Type (Old_S); | |
3208 | ||
3209 | begin | |
46ee0270 AC |
3210 | |
3211 | -- The actual must match the (instance of the) formal, | |
3212 | -- and must be a controlling type. | |
3213 | ||
3214 | if Old_S_Ctrl_Type /= New_S_Ctrl_Type | |
3215 | or else No (New_S_Ctrl_Type) | |
3216 | then | |
82c80734 RD |
3217 | Error_Msg_NE |
3218 | ("actual must be dispatching subprogram for type&", | |
3219 | Nam, New_S_Ctrl_Type); | |
3220 | ||
3221 | else | |
3222 | Set_Is_Dispatching_Operation (New_S); | |
3223 | Check_Controlling_Formals (New_S_Ctrl_Type, New_S); | |
3224 | ||
16ca248a ES |
3225 | -- If the actual in the formal subprogram is itself a |
3226 | -- formal abstract subprogram association, there's no | |
3227 | -- dispatch table component or position to inherit. | |
82c80734 RD |
3228 | |
3229 | if Present (DTC_Entity (Old_S)) then | |
3230 | Set_DTC_Entity (New_S, DTC_Entity (Old_S)); | |
024d33d8 | 3231 | Set_DT_Position_Value (New_S, DT_Position (Old_S)); |
82c80734 RD |
3232 | end if; |
3233 | end if; | |
3234 | end; | |
3235 | end if; | |
996ae0b0 RK |
3236 | end if; |
3237 | ||
c91dbd18 AC |
3238 | if Is_Actual then |
3239 | null; | |
3240 | ||
3241 | -- The following is illegal, because F hides whatever other F may | |
3242 | -- be around: | |
a1390a6e | 3243 | -- function F (...) renames F; |
c91dbd18 AC |
3244 | |
3245 | elsif Old_S = New_S | |
3246 | or else (Nkind (Nam) /= N_Expanded_Name | |
3247 | and then Chars (Old_S) = Chars (New_S)) | |
3248 | then | |
3249 | Error_Msg_N ("subprogram cannot rename itself", N); | |
3250 | ||
a1390a6e AC |
3251 | -- This is illegal even if we use a selector: |
3252 | -- function F (...) renames Pkg.F; | |
3253 | -- because F is still hidden. | |
3254 | ||
c91dbd18 AC |
3255 | elsif Nkind (Nam) = N_Expanded_Name |
3256 | and then Entity (Prefix (Nam)) = Current_Scope | |
3257 | and then Chars (Selector_Name (Nam)) = Chars (New_S) | |
996ae0b0 | 3258 | then |
354c3840 AC |
3259 | -- This is an error, but we overlook the error and accept the |
3260 | -- renaming if the special Overriding_Renamings mode is in effect. | |
9b7424a7 | 3261 | |
354c3840 | 3262 | if not Overriding_Renamings then |
9b7424a7 | 3263 | Error_Msg_NE |
354c3840 AC |
3264 | ("implicit operation& is not visible (RM 8.3 (15))", |
3265 | Nam, Old_S); | |
9b7424a7 | 3266 | end if; |
996ae0b0 RK |
3267 | end if; |
3268 | ||
3269 | Set_Convention (New_S, Convention (Old_S)); | |
16ca248a ES |
3270 | |
3271 | if Is_Abstract_Subprogram (Old_S) then | |
3272 | if Present (Rename_Spec) then | |
3273 | Error_Msg_N | |
3274 | ("a renaming-as-body cannot rename an abstract subprogram", | |
3275 | N); | |
3276 | Set_Has_Completion (Rename_Spec); | |
3277 | else | |
3278 | Set_Is_Abstract_Subprogram (New_S); | |
3279 | end if; | |
3280 | end if; | |
3281 | ||
996ae0b0 RK |
3282 | Check_Library_Unit_Renaming (N, Old_S); |
3283 | ||
edd63e9b ES |
3284 | -- Pathological case: procedure renames entry in the scope of its |
3285 | -- task. Entry is given by simple name, but body must be built for | |
3286 | -- procedure. Of course if called it will deadlock. | |
996ae0b0 RK |
3287 | |
3288 | if Ekind (Old_S) = E_Entry then | |
3289 | Set_Has_Completion (New_S, False); | |
3290 | Set_Alias (New_S, Empty); | |
3291 | end if; | |
3292 | ||
3293 | if Is_Actual then | |
3294 | Freeze_Before (N, Old_S); | |
02e4edea | 3295 | Freeze_Actual_Profile; |
996ae0b0 RK |
3296 | Set_Has_Delayed_Freeze (New_S, False); |
3297 | Freeze_Before (N, New_S); | |
3298 | ||
82c80734 RD |
3299 | -- An abstract subprogram is only allowed as an actual in the case |
3300 | -- where the formal subprogram is also abstract. | |
3301 | ||
996ae0b0 | 3302 | if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function) |
16ca248a ES |
3303 | and then Is_Abstract_Subprogram (Old_S) |
3304 | and then not Is_Abstract_Subprogram (Formal_Spec) | |
996ae0b0 RK |
3305 | then |
3306 | Error_Msg_N | |
3307 | ("abstract subprogram not allowed as generic actual", Nam); | |
3308 | end if; | |
3309 | end if; | |
3310 | ||
3311 | else | |
edd63e9b ES |
3312 | -- A common error is to assume that implicit operators for types are |
3313 | -- defined in Standard, or in the scope of a subtype. In those cases | |
3314 | -- where the renamed entity is given with an expanded name, it is | |
3315 | -- worth mentioning that operators for the type are not declared in | |
3316 | -- the scope given by the prefix. | |
996ae0b0 RK |
3317 | |
3318 | if Nkind (Nam) = N_Expanded_Name | |
3319 | and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol | |
3320 | and then Scope (Entity (Nam)) = Standard_Standard | |
3321 | then | |
3322 | declare | |
3323 | T : constant Entity_Id := | |
3324 | Base_Type (Etype (First_Formal (New_S))); | |
996ae0b0 RK |
3325 | begin |
3326 | Error_Msg_Node_2 := Prefix (Nam); | |
fbf5a39b AC |
3327 | Error_Msg_NE |
3328 | ("operator for type& is not declared in&", Prefix (Nam), T); | |
996ae0b0 | 3329 | end; |
fbf5a39b | 3330 | |
996ae0b0 RK |
3331 | else |
3332 | Error_Msg_NE | |
3333 | ("no visible subprogram matches the specification for&", | |
3334 | Spec, New_S); | |
3335 | end if; | |
3336 | ||
3337 | if Present (Candidate_Renaming) then | |
3338 | declare | |
3339 | F1 : Entity_Id; | |
3340 | F2 : Entity_Id; | |
70b70ce8 | 3341 | T1 : Entity_Id; |
996ae0b0 RK |
3342 | |
3343 | begin | |
3344 | F1 := First_Formal (Candidate_Renaming); | |
3345 | F2 := First_Formal (New_S); | |
70b70ce8 | 3346 | T1 := First_Subtype (Etype (F1)); |
996ae0b0 RK |
3347 | while Present (F1) and then Present (F2) loop |
3348 | Next_Formal (F1); | |
3349 | Next_Formal (F2); | |
3350 | end loop; | |
3351 | ||
3352 | if Present (F1) and then Present (Default_Value (F1)) then | |
3353 | if Present (Next_Formal (F1)) then | |
3354 | Error_Msg_NE | |
241ebe89 HK |
3355 | ("\missing specification for & and other formals with " |
3356 | & "defaults", Spec, F1); | |
996ae0b0 | 3357 | else |
241ebe89 | 3358 | Error_Msg_NE ("\missing specification for &", Spec, F1); |
996ae0b0 RK |
3359 | end if; |
3360 | end if; | |
70b70ce8 AC |
3361 | |
3362 | if Nkind (Nam) = N_Operator_Symbol | |
3363 | and then From_Default (N) | |
3364 | then | |
3365 | Error_Msg_Node_2 := T1; | |
3366 | Error_Msg_NE | |
3367 | ("default & on & is not directly visible", | |
3368 | Nam, Nam); | |
3369 | end if; | |
996ae0b0 RK |
3370 | end; |
3371 | end if; | |
3372 | end if; | |
3373 | ||
edd63e9b ES |
3374 | -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that |
3375 | -- controlling access parameters are known non-null for the renamed | |
3376 | -- subprogram. Test also applies to a subprogram instantiation that | |
cdc8c54c BD |
3377 | -- is dispatching. Test is skipped if some previous error was detected |
3378 | -- that set Old_S to Any_Id. | |
edd63e9b | 3379 | |
0791fbe9 | 3380 | if Ada_Version >= Ada_2005 |
cdc8c54c | 3381 | and then Old_S /= Any_Id |
edd63e9b ES |
3382 | and then not Is_Dispatching_Operation (Old_S) |
3383 | and then Is_Dispatching_Operation (New_S) | |
3384 | then | |
3385 | declare | |
3386 | Old_F : Entity_Id; | |
3387 | New_F : Entity_Id; | |
3388 | ||
3389 | begin | |
3390 | Old_F := First_Formal (Old_S); | |
3391 | New_F := First_Formal (New_S); | |
3392 | while Present (Old_F) loop | |
3393 | if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type | |
3394 | and then Is_Controlling_Formal (New_F) | |
3395 | and then not Can_Never_Be_Null (Old_F) | |
3396 | then | |
3397 | Error_Msg_N ("access parameter is controlling,", New_F); | |
fbe627af RD |
3398 | Error_Msg_NE |
3399 | ("\corresponding parameter of& " | |
3400 | & "must be explicitly null excluding", New_F, Old_S); | |
edd63e9b ES |
3401 | end if; |
3402 | ||
3403 | Next_Formal (Old_F); | |
3404 | Next_Formal (New_F); | |
3405 | end loop; | |
3406 | end; | |
3407 | end if; | |
3408 | ||
725393ea | 3409 | -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005) |
b5c739f9 | 3410 | -- is to warn if an operator is being renamed as a different operator. |
880dabb5 AC |
3411 | -- If the operator is predefined, examine the kind of the entity, not |
3412 | -- the abbreviated declaration in Standard. | |
725393ea ES |
3413 | |
3414 | if Comes_From_Source (N) | |
3415 | and then Present (Old_S) | |
ac7d724d ES |
3416 | and then (Nkind (Old_S) = N_Defining_Operator_Symbol |
3417 | or else Ekind (Old_S) = E_Operator) | |
725393ea ES |
3418 | and then Nkind (New_S) = N_Defining_Operator_Symbol |
3419 | and then Chars (Old_S) /= Chars (New_S) | |
3420 | then | |
3421 | Error_Msg_NE | |
dbfeb4fa | 3422 | ("& is being renamed as a different operator??", N, Old_S); |
725393ea ES |
3423 | end if; |
3424 | ||
b5c739f9 RD |
3425 | -- Check for renaming of obsolescent subprogram |
3426 | ||
3427 | Check_Obsolescent_2005_Entity (Entity (Nam), Nam); | |
3428 | ||
11560bcc TQ |
3429 | -- Another warning or some utility: if the new subprogram as the same |
3430 | -- name as the old one, the old one is not hidden by an outer homograph, | |
3431 | -- the new one is not a public symbol, and the old one is otherwise | |
3432 | -- directly visible, the renaming is superfluous. | |
3433 | ||
3434 | if Chars (Old_S) = Chars (New_S) | |
3435 | and then Comes_From_Source (N) | |
3436 | and then Scope (Old_S) /= Standard_Standard | |
3437 | and then Warn_On_Redundant_Constructs | |
ac7d724d ES |
3438 | and then (Is_Immediately_Visible (Old_S) |
3439 | or else Is_Potentially_Use_Visible (Old_S)) | |
11560bcc TQ |
3440 | and then Is_Overloadable (Current_Scope) |
3441 | and then Chars (Current_Scope) /= Chars (Old_S) | |
3442 | then | |
3443 | Error_Msg_N | |
241ebe89 | 3444 | ("redundant renaming, entity is directly visible?r?", Name (N)); |
11560bcc TQ |
3445 | end if; |
3446 | ||
ef992452 | 3447 | -- Implementation-defined aspect specifications can appear in a renaming |
cc96a1b8 AC |
3448 | -- declaration, but not language-defined ones. The call to procedure |
3449 | -- Analyze_Aspect_Specifications will take care of this error check. | |
ef992452 AC |
3450 | |
3451 | if Has_Aspects (N) then | |
3452 | Analyze_Aspect_Specifications (N, New_S); | |
3453 | end if; | |
3454 | ||
0ab80019 | 3455 | Ada_Version := Save_AV; |
fb620b37 | 3456 | Ada_Version_Pragma := Save_AVP; |
edd63e9b | 3457 | Ada_Version_Explicit := Save_AV_Exp; |
948ed277 AC |
3458 | |
3459 | -- In GNATprove mode, the renamings of actual subprograms are replaced | |
3460 | -- with wrapper functions that make it easier to propagate axioms to the | |
a1954e97 AC |
3461 | -- points of call within an instance. Wrappers are generated if formal |
3462 | -- subprogram is subject to axiomatization. | |
948ed277 | 3463 | |
1a779058 AC |
3464 | -- The types in the wrapper profiles are obtained from (instances of) |
3465 | -- the types of the formal subprogram. | |
3466 | ||
948ed277 AC |
3467 | if Is_Actual |
3468 | and then GNATprove_Mode | |
a1954e97 | 3469 | and then Present (Containing_Package_With_Ext_Axioms (Formal_Spec)) |
948ed277 AC |
3470 | and then not Inside_A_Generic |
3471 | then | |
3472 | if Ekind (Old_S) = E_Function then | |
1a779058 | 3473 | Rewrite (N, Build_Function_Wrapper (Formal_Spec, Old_S)); |
948ed277 | 3474 | Analyze (N); |
fc6d9796 | 3475 | |
948ed277 | 3476 | elsif Ekind (Old_S) = E_Operator then |
fc6d9796 | 3477 | Rewrite (N, Build_Operator_Wrapper (Formal_Spec, Old_S)); |
948ed277 AC |
3478 | Analyze (N); |
3479 | end if; | |
3480 | end if; | |
996ae0b0 RK |
3481 | end Analyze_Subprogram_Renaming; |
3482 | ||
3483 | ------------------------- | |
3484 | -- Analyze_Use_Package -- | |
3485 | ------------------------- | |
3486 | ||
3487 | -- Resolve the package names in the use clause, and make all the visible | |
3488 | -- entities defined in the package potentially use-visible. If the package | |
3489 | -- is already in use from a previous use clause, its visible entities are | |
3490 | -- already use-visible. In that case, mark the occurrence as a redundant | |
3491 | -- use. If the package is an open scope, i.e. if the use clause occurs | |
3492 | -- within the package itself, ignore it. | |
3493 | ||
3494 | procedure Analyze_Use_Package (N : Node_Id) is | |
3495 | Pack_Name : Node_Id; | |
3496 | Pack : Entity_Id; | |
3497 | ||
996ae0b0 RK |
3498 | -- Start of processing for Analyze_Use_Package |
3499 | ||
3500 | begin | |
ce5ba43a | 3501 | Check_SPARK_05_Restriction ("use clause is not allowed", N); |
1d801f21 | 3502 | |
996ae0b0 RK |
3503 | Set_Hidden_By_Use_Clause (N, No_Elist); |
3504 | ||
7394c8cc AC |
3505 | -- Use clause not allowed in a spec of a predefined package declaration |
3506 | -- except that packages whose file name starts a-n are OK (these are | |
3507 | -- children of Ada.Numerics, which are never loaded by Rtsfind). | |
996ae0b0 RK |
3508 | |
3509 | if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit)) | |
3510 | and then Name_Buffer (1 .. 3) /= "a-n" | |
3511 | and then | |
3512 | Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration | |
3513 | then | |
3514 | Error_Msg_N ("use clause not allowed in predefined spec", N); | |
3515 | end if; | |
3516 | ||
bc41faa2 | 3517 | -- Chain clause to list of use clauses in current scope |
996ae0b0 RK |
3518 | |
3519 | if Nkind (Parent (N)) /= N_Compilation_Unit then | |
3520 | Chain_Use_Clause (N); | |
3521 | end if; | |
3522 | ||
3523 | -- Loop through package names to identify referenced packages | |
3524 | ||
3525 | Pack_Name := First (Names (N)); | |
996ae0b0 RK |
3526 | while Present (Pack_Name) loop |
3527 | Analyze (Pack_Name); | |
3528 | ||
3529 | if Nkind (Parent (N)) = N_Compilation_Unit | |
3530 | and then Nkind (Pack_Name) = N_Expanded_Name | |
3531 | then | |
3532 | declare | |
16ca248a | 3533 | Pref : Node_Id; |
996ae0b0 RK |
3534 | |
3535 | begin | |
16ca248a | 3536 | Pref := Prefix (Pack_Name); |
996ae0b0 RK |
3537 | while Nkind (Pref) = N_Expanded_Name loop |
3538 | Pref := Prefix (Pref); | |
3539 | end loop; | |
3540 | ||
3541 | if Entity (Pref) = Standard_Standard then | |
3542 | Error_Msg_N | |
3543 | ("predefined package Standard cannot appear" | |
3544 | & " in a context clause", Pref); | |
3545 | end if; | |
3546 | end; | |
3547 | end if; | |
3548 | ||
3549 | Next (Pack_Name); | |
3550 | end loop; | |
3551 | ||
3552 | -- Loop through package names to mark all entities as potentially | |
3553 | -- use visible. | |
3554 | ||
3555 | Pack_Name := First (Names (N)); | |
996ae0b0 | 3556 | while Present (Pack_Name) loop |
996ae0b0 RK |
3557 | if Is_Entity_Name (Pack_Name) then |
3558 | Pack := Entity (Pack_Name); | |
3559 | ||
ac7d724d | 3560 | if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then |
996ae0b0 | 3561 | if Ekind (Pack) = E_Generic_Package then |
483c78cb | 3562 | Error_Msg_N -- CODEFIX |
0566484a AC |
3563 | ("a generic package is not allowed in a use clause", |
3564 | Pack_Name); | |
3565 | ||
3566 | elsif Ekind_In (Pack, E_Generic_Function, E_Generic_Package) | |
3567 | then | |
3568 | Error_Msg_N -- CODEFIX | |
3569 | ("a generic subprogram is not allowed in a use clause", | |
3570 | Pack_Name); | |
3571 | ||
3572 | elsif Ekind_In (Pack, E_Function, E_Procedure, E_Operator) then | |
3573 | Error_Msg_N -- CODEFIX | |
3574 | ("a subprogram is not allowed in a use clause", | |
3575 | Pack_Name); | |
3576 | ||
996ae0b0 | 3577 | else |
0566484a | 3578 | Error_Msg_N ("& is not allowed in a use clause", Pack_Name); |
996ae0b0 RK |
3579 | end if; |
3580 | ||
fbf5a39b AC |
3581 | else |
3582 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
3583 | Check_In_Previous_With_Clause (N, Pack_Name); | |
3584 | end if; | |
996ae0b0 | 3585 | |
fbf5a39b AC |
3586 | if Applicable_Use (Pack_Name) then |
3587 | Use_One_Package (Pack, N); | |
3588 | end if; | |
996ae0b0 | 3589 | end if; |
462c31ef ST |
3590 | |
3591 | -- Report error because name denotes something other than a package | |
3592 | ||
3593 | else | |
3594 | Error_Msg_N ("& is not a package", Pack_Name); | |
996ae0b0 RK |
3595 | end if; |
3596 | ||
3597 | Next (Pack_Name); | |
3598 | end loop; | |
996ae0b0 RK |
3599 | end Analyze_Use_Package; |
3600 | ||
3601 | ---------------------- | |
3602 | -- Analyze_Use_Type -- | |
3603 | ---------------------- | |
3604 | ||
3605 | procedure Analyze_Use_Type (N : Node_Id) is | |
954c111a | 3606 | E : Entity_Id; |
ecc4ddde | 3607 | Id : Node_Id; |
996ae0b0 RK |
3608 | |
3609 | begin | |
3610 | Set_Hidden_By_Use_Clause (N, No_Elist); | |
3611 | ||
bc41faa2 | 3612 | -- Chain clause to list of use clauses in current scope |
996ae0b0 RK |
3613 | |
3614 | if Nkind (Parent (N)) /= N_Compilation_Unit then | |
3615 | Chain_Use_Clause (N); | |
3616 | end if; | |
3617 | ||
780d052e RD |
3618 | -- If the Used_Operations list is already initialized, the clause has |
3619 | -- been analyzed previously, and it is begin reinstalled, for example | |
3620 | -- when the clause appears in a package spec and we are compiling the | |
3621 | -- corresponding package body. In that case, make the entities on the | |
806f6d37 | 3622 | -- existing list use_visible, and mark the corresponding types In_Use. |
29efbb8c | 3623 | |
780d052e | 3624 | if Present (Used_Operations (N)) then |
29efbb8c | 3625 | declare |
806f6d37 | 3626 | Mark : Node_Id; |
29efbb8c | 3627 | Elmt : Elmt_Id; |
806f6d37 | 3628 | |
29efbb8c | 3629 | begin |
806f6d37 AC |
3630 | Mark := First (Subtype_Marks (N)); |
3631 | while Present (Mark) loop | |
7ff2d234 | 3632 | Use_One_Type (Mark, Installed => True); |
806f6d37 AC |
3633 | Next (Mark); |
3634 | end loop; | |
3635 | ||
29efbb8c ES |
3636 | Elmt := First_Elmt (Used_Operations (N)); |
3637 | while Present (Elmt) loop | |
3638 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
3639 | Next_Elmt (Elmt); | |
3640 | end loop; | |
3641 | end; | |
3642 | ||
3643 | return; | |
3644 | end if; | |
3645 | ||
780d052e RD |
3646 | -- Otherwise, create new list and attach to it the operations that |
3647 | -- are made use-visible by the clause. | |
3648 | ||
29efbb8c | 3649 | Set_Used_Operations (N, New_Elmt_List); |
996ae0b0 | 3650 | Id := First (Subtype_Marks (N)); |
996ae0b0 RK |
3651 | while Present (Id) loop |
3652 | Find_Type (Id); | |
954c111a | 3653 | E := Entity (Id); |
996ae0b0 | 3654 | |
954c111a | 3655 | if E /= Any_Type then |
07fc65c4 | 3656 | Use_One_Type (Id); |
fbf5a39b AC |
3657 | |
3658 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
923fa078 | 3659 | if Nkind (Id) = N_Identifier then |
edd63e9b | 3660 | Error_Msg_N ("type is not directly visible", Id); |
fbf5a39b | 3661 | |
954c111a HK |
3662 | elsif Is_Child_Unit (Scope (E)) |
3663 | and then Scope (E) /= System_Aux_Id | |
fbf5a39b AC |
3664 | then |
3665 | Check_In_Previous_With_Clause (N, Prefix (Id)); | |
3666 | end if; | |
3667 | end if; | |
ecc4ddde AC |
3668 | |
3669 | else | |
4c8a5bb8 | 3670 | -- If the use_type_clause appears in a compilation unit context, |
ecc4ddde | 3671 | -- check whether it comes from a unit that may appear in a |
4c8a5bb8 | 3672 | -- limited_with_clause, for a better error message. |
ecc4ddde AC |
3673 | |
3674 | if Nkind (Parent (N)) = N_Compilation_Unit | |
3675 | and then Nkind (Id) /= N_Identifier | |
3676 | then | |
3677 | declare | |
3678 | Item : Node_Id; | |
3679 | Pref : Node_Id; | |
3680 | ||
3681 | function Mentioned (Nam : Node_Id) return Boolean; | |
4c8a5bb8 AC |
3682 | -- Check whether the prefix of expanded name for the type |
3683 | -- appears in the prefix of some limited_with_clause. | |
3684 | ||
3685 | --------------- | |
3686 | -- Mentioned -- | |
3687 | --------------- | |
ecc4ddde AC |
3688 | |
3689 | function Mentioned (Nam : Node_Id) return Boolean is | |
3690 | begin | |
4c8a5bb8 | 3691 | return Nkind (Name (Item)) = N_Selected_Component |
ac7d724d | 3692 | and then Chars (Prefix (Name (Item))) = Chars (Nam); |
ecc4ddde AC |
3693 | end Mentioned; |
3694 | ||
3695 | begin | |
3696 | Pref := Prefix (Id); | |
3697 | Item := First (Context_Items (Parent (N))); | |
4c8a5bb8 | 3698 | while Present (Item) and then Item /= N loop |
ecc4ddde AC |
3699 | if Nkind (Item) = N_With_Clause |
3700 | and then Limited_Present (Item) | |
3701 | and then Mentioned (Pref) | |
3702 | then | |
4c8a5bb8 AC |
3703 | Change_Error_Text |
3704 | (Get_Msg_Id, "premature usage of incomplete type"); | |
ecc4ddde AC |
3705 | end if; |
3706 | ||
3707 | Next (Item); | |
3708 | end loop; | |
3709 | end; | |
3710 | end if; | |
996ae0b0 RK |
3711 | end if; |
3712 | ||
3713 | Next (Id); | |
3714 | end loop; | |
3715 | end Analyze_Use_Type; | |
3716 | ||
3717 | -------------------- | |
3718 | -- Applicable_Use -- | |
3719 | -------------------- | |
3720 | ||
3721 | function Applicable_Use (Pack_Name : Node_Id) return Boolean is | |
3722 | Pack : constant Entity_Id := Entity (Pack_Name); | |
3723 | ||
3724 | begin | |
3725 | if In_Open_Scopes (Pack) then | |
ac7d724d | 3726 | if Warn_On_Redundant_Constructs and then Pack = Current_Scope then |
ed2233dc | 3727 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 3728 | ("& is already use-visible within itself?r?", Pack_Name, Pack); |
954c111a HK |
3729 | end if; |
3730 | ||
996ae0b0 RK |
3731 | return False; |
3732 | ||
3733 | elsif In_Use (Pack) then | |
d4810530 | 3734 | Note_Redundant_Use (Pack_Name); |
996ae0b0 RK |
3735 | return False; |
3736 | ||
3737 | elsif Present (Renamed_Object (Pack)) | |
3738 | and then In_Use (Renamed_Object (Pack)) | |
3739 | then | |
d4810530 | 3740 | Note_Redundant_Use (Pack_Name); |
996ae0b0 RK |
3741 | return False; |
3742 | ||
3743 | else | |
3744 | return True; | |
3745 | end if; | |
3746 | end Applicable_Use; | |
3747 | ||
3748 | ------------------------ | |
3749 | -- Attribute_Renaming -- | |
3750 | ------------------------ | |
3751 | ||
3752 | procedure Attribute_Renaming (N : Node_Id) is | |
8d80ff64 AC |
3753 | Loc : constant Source_Ptr := Sloc (N); |
3754 | Nam : constant Node_Id := Name (N); | |
3755 | Spec : constant Node_Id := Specification (N); | |
3756 | New_S : constant Entity_Id := Defining_Unit_Name (Spec); | |
3757 | Aname : constant Name_Id := Attribute_Name (Nam); | |
996ae0b0 | 3758 | |
8d80ff64 AC |
3759 | Form_Num : Nat := 0; |
3760 | Expr_List : List_Id := No_List; | |
996ae0b0 RK |
3761 | |
3762 | Attr_Node : Node_Id; | |
3763 | Body_Node : Node_Id; | |
3764 | Param_Spec : Node_Id; | |
3765 | ||
3766 | begin | |
3767 | Generate_Definition (New_S); | |
3768 | ||
4c8a5bb8 AC |
3769 | -- This procedure is called in the context of subprogram renaming, and |
3770 | -- thus the attribute must be one that is a subprogram. All of those | |
ba0c6e47 RD |
3771 | -- have at least one formal parameter, with the exceptions of the GNAT |
3772 | -- attribute 'Img, which GNAT treats as renameable. | |
996ae0b0 RK |
3773 | |
3774 | if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then | |
ba0c6e47 | 3775 | if Aname /= Name_Img then |
996ae0b0 RK |
3776 | Error_Msg_N |
3777 | ("subprogram renaming an attribute must have formals", N); | |
3778 | return; | |
3779 | end if; | |
3780 | ||
3781 | else | |
3782 | Param_Spec := First (Parameter_Specifications (Spec)); | |
996ae0b0 RK |
3783 | while Present (Param_Spec) loop |
3784 | Form_Num := Form_Num + 1; | |
3785 | ||
3786 | if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then | |
3787 | Find_Type (Parameter_Type (Param_Spec)); | |
3788 | ||
3789 | -- The profile of the new entity denotes the base type (s) of | |
3790 | -- the types given in the specification. For access parameters | |
3791 | -- there are no subtypes involved. | |
3792 | ||
3793 | Rewrite (Parameter_Type (Param_Spec), | |
e4494292 | 3794 | New_Occurrence_Of |
8d80ff64 | 3795 | (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc)); |
996ae0b0 RK |
3796 | end if; |
3797 | ||
3798 | if No (Expr_List) then | |
3799 | Expr_List := New_List; | |
3800 | end if; | |
3801 | ||
3802 | Append_To (Expr_List, | |
3803 | Make_Identifier (Loc, | |
3804 | Chars => Chars (Defining_Identifier (Param_Spec)))); | |
3805 | ||
fbf5a39b | 3806 | -- The expressions in the attribute reference are not freeze |
4c8a5bb8 | 3807 | -- points. Neither is the attribute as a whole, see below. |
fbf5a39b AC |
3808 | |
3809 | Set_Must_Not_Freeze (Last (Expr_List)); | |
996ae0b0 RK |
3810 | Next (Param_Spec); |
3811 | end loop; | |
3812 | end if; | |
3813 | ||
4c8a5bb8 AC |
3814 | -- Immediate error if too many formals. Other mismatches in number or |
3815 | -- types of parameters are detected when we analyze the body of the | |
3816 | -- subprogram that we construct. | |
996ae0b0 RK |
3817 | |
3818 | if Form_Num > 2 then | |
3819 | Error_Msg_N ("too many formals for attribute", N); | |
3820 | ||
4c8a5bb8 AC |
3821 | -- Error if the attribute reference has expressions that look like |
3822 | -- formal parameters. | |
0da2c8ac AC |
3823 | |
3824 | elsif Present (Expressions (Nam)) then | |
3825 | Error_Msg_N ("illegal expressions in attribute reference", Nam); | |
3826 | ||
996ae0b0 | 3827 | elsif |
b69cd36a AC |
3828 | Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part, |
3829 | Name_Pos, Name_Round, Name_Scaling, | |
3830 | Name_Val) | |
996ae0b0 RK |
3831 | then |
3832 | if Nkind (N) = N_Subprogram_Renaming_Declaration | |
82c80734 | 3833 | and then Present (Corresponding_Formal_Spec (N)) |
996ae0b0 RK |
3834 | then |
3835 | Error_Msg_N | |
3836 | ("generic actual cannot be attribute involving universal type", | |
3837 | Nam); | |
3838 | else | |
3839 | Error_Msg_N | |
3840 | ("attribute involving a universal type cannot be renamed", | |
3841 | Nam); | |
3842 | end if; | |
3843 | end if; | |
3844 | ||
ba0c6e47 RD |
3845 | -- Rewrite attribute node to have a list of expressions corresponding to |
3846 | -- the subprogram formals. A renaming declaration is not a freeze point, | |
3847 | -- and the analysis of the attribute reference should not freeze the | |
3848 | -- type of the prefix. We use the original node in the renaming so that | |
3849 | -- its source location is preserved, and checks on stream attributes are | |
3850 | -- properly applied. | |
996ae0b0 | 3851 | |
ba0c6e47 RD |
3852 | Attr_Node := Relocate_Node (Nam); |
3853 | Set_Expressions (Attr_Node, Expr_List); | |
996ae0b0 | 3854 | |
ba0c6e47 RD |
3855 | Set_Must_Not_Freeze (Attr_Node); |
3856 | Set_Must_Not_Freeze (Prefix (Nam)); | |
996ae0b0 RK |
3857 | |
3858 | -- Case of renaming a function | |
3859 | ||
3860 | if Nkind (Spec) = N_Function_Specification then | |
996ae0b0 RK |
3861 | if Is_Procedure_Attribute_Name (Aname) then |
3862 | Error_Msg_N ("attribute can only be renamed as procedure", Nam); | |
3863 | return; | |
3864 | end if; | |
3865 | ||
725393ea ES |
3866 | Find_Type (Result_Definition (Spec)); |
3867 | Rewrite (Result_Definition (Spec), | |
e4494292 | 3868 | New_Occurrence_Of |
4bcf6815 | 3869 | (Base_Type (Entity (Result_Definition (Spec))), Loc)); |
996ae0b0 RK |
3870 | |
3871 | Body_Node := | |
3872 | Make_Subprogram_Body (Loc, | |
3873 | Specification => Spec, | |
3874 | Declarations => New_List, | |
3875 | Handled_Statement_Sequence => | |
3876 | Make_Handled_Sequence_Of_Statements (Loc, | |
3877 | Statements => New_List ( | |
11560bcc | 3878 | Make_Simple_Return_Statement (Loc, |
996ae0b0 RK |
3879 | Expression => Attr_Node)))); |
3880 | ||
3881 | -- Case of renaming a procedure | |
3882 | ||
3883 | else | |
3884 | if not Is_Procedure_Attribute_Name (Aname) then | |
3885 | Error_Msg_N ("attribute can only be renamed as function", Nam); | |
3886 | return; | |
3887 | end if; | |
3888 | ||
3889 | Body_Node := | |
3890 | Make_Subprogram_Body (Loc, | |
3891 | Specification => Spec, | |
3892 | Declarations => New_List, | |
3893 | Handled_Statement_Sequence => | |
3894 | Make_Handled_Sequence_Of_Statements (Loc, | |
3895 | Statements => New_List (Attr_Node))); | |
3896 | end if; | |
3897 | ||
fbe627af RD |
3898 | -- In case of tagged types we add the body of the generated function to |
3899 | -- the freezing actions of the type (because in the general case such | |
3900 | -- type is still not frozen). We exclude from this processing generic | |
ba0c6e47 | 3901 | -- formal subprograms found in instantiations. |
fbe627af | 3902 | |
535a8637 | 3903 | -- We must exclude restricted run-time libraries because |
6b81741c | 3904 | -- entity AST_Handler is defined in package System.Aux_Dec which is not |
9d1e0e72 JR |
3905 | -- available in those platforms. Note that we cannot use the function |
3906 | -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because | |
3907 | -- the ZFP run-time library is not defined as a profile, and we do not | |
3908 | -- want to deal with AST_Handler in ZFP mode. | |
ddc1515a | 3909 | |
535a8637 | 3910 | if not Configurable_Run_Time_Mode |
ddc1515a | 3911 | and then not Present (Corresponding_Formal_Spec (N)) |
fbe627af RD |
3912 | and then Etype (Nam) /= RTE (RE_AST_Handler) |
3913 | then | |
3914 | declare | |
a530b8bb | 3915 | P : constant Node_Id := Prefix (Nam); |
fbe627af RD |
3916 | |
3917 | begin | |
8d80ff64 AC |
3918 | -- The prefix of 'Img is an object that is evaluated for each call |
3919 | -- of the function that renames it. | |
a530b8bb AC |
3920 | |
3921 | if Aname = Name_Img then | |
3922 | Preanalyze_And_Resolve (P); | |
3923 | ||
8d80ff64 | 3924 | -- For all other attribute renamings, the prefix is a subtype |
a530b8bb AC |
3925 | |
3926 | else | |
3927 | Find_Type (P); | |
3928 | end if; | |
fbe627af | 3929 | |
4bcf6815 AC |
3930 | -- If the target type is not yet frozen, add the body to the |
3931 | -- actions to be elaborated at freeze time. | |
3932 | ||
3933 | if Is_Tagged_Type (Etype (P)) | |
3934 | and then In_Open_Scopes (Scope (Etype (P))) | |
3935 | then | |
fbe627af RD |
3936 | Ensure_Freeze_Node (Etype (P)); |
3937 | Append_Freeze_Action (Etype (P), Body_Node); | |
3938 | else | |
3939 | Rewrite (N, Body_Node); | |
3940 | Analyze (N); | |
3941 | Set_Etype (New_S, Base_Type (Etype (New_S))); | |
3942 | end if; | |
3943 | end; | |
3944 | ||
3945 | -- Generic formal subprograms or AST_Handler renaming | |
3946 | ||
3947 | else | |
3948 | Rewrite (N, Body_Node); | |
3949 | Analyze (N); | |
3950 | Set_Etype (New_S, Base_Type (Etype (New_S))); | |
3951 | end if; | |
996ae0b0 | 3952 | |
615cbd95 AC |
3953 | if Is_Compilation_Unit (New_S) then |
3954 | Error_Msg_N | |
3955 | ("a library unit can only rename another library unit", N); | |
3956 | end if; | |
3957 | ||
996ae0b0 RK |
3958 | -- We suppress elaboration warnings for the resulting entity, since |
3959 | -- clearly they are not needed, and more particularly, in the case | |
3960 | -- of a generic formal subprogram, the resulting entity can appear | |
3961 | -- after the instantiation itself, and thus look like a bogus case | |
3962 | -- of access before elaboration. | |
3963 | ||
3964 | Set_Suppress_Elaboration_Warnings (New_S); | |
3965 | ||
3966 | end Attribute_Renaming; | |
3967 | ||
3968 | ---------------------- | |
3969 | -- Chain_Use_Clause -- | |
3970 | ---------------------- | |
3971 | ||
3972 | procedure Chain_Use_Clause (N : Node_Id) is | |
d4810530 ES |
3973 | Pack : Entity_Id; |
3974 | Level : Int := Scope_Stack.Last; | |
3975 | ||
996ae0b0 | 3976 | begin |
d4810530 ES |
3977 | if not Is_Compilation_Unit (Current_Scope) |
3978 | or else not Is_Child_Unit (Current_Scope) | |
3979 | then | |
3980 | null; -- Common case | |
3981 | ||
3982 | elsif Defining_Entity (Parent (N)) = Current_Scope then | |
3983 | null; -- Common case for compilation unit | |
3984 | ||
3985 | else | |
3986 | -- If declaration appears in some other scope, it must be in some | |
3987 | -- parent unit when compiling a child. | |
3988 | ||
3989 | Pack := Defining_Entity (Parent (N)); | |
3990 | if not In_Open_Scopes (Pack) then | |
3991 | null; -- default as well | |
3992 | ||
3830827c AC |
3993 | -- If the use clause appears in an ancestor and we are in the |
3994 | -- private part of the immediate parent, the use clauses are | |
3995 | -- already installed. | |
3996 | ||
3997 | elsif Pack /= Scope (Current_Scope) | |
3998 | and then In_Private_Part (Scope (Current_Scope)) | |
3999 | then | |
4000 | null; | |
4001 | ||
d4810530 ES |
4002 | else |
4003 | -- Find entry for parent unit in scope stack | |
4004 | ||
4005 | while Scope_Stack.Table (Level).Entity /= Pack loop | |
4006 | Level := Level - 1; | |
4007 | end loop; | |
4008 | end if; | |
4009 | end if; | |
4010 | ||
996ae0b0 | 4011 | Set_Next_Use_Clause (N, |
d4810530 ES |
4012 | Scope_Stack.Table (Level).First_Use_Clause); |
4013 | Scope_Stack.Table (Level).First_Use_Clause := N; | |
996ae0b0 RK |
4014 | end Chain_Use_Clause; |
4015 | ||
15ce9ca2 AC |
4016 | --------------------------- |
4017 | -- Check_Frozen_Renaming -- | |
4018 | --------------------------- | |
996ae0b0 RK |
4019 | |
4020 | procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is | |
4021 | B_Node : Node_Id; | |
4022 | Old_S : Entity_Id; | |
4023 | ||
4024 | begin | |
ac7d724d | 4025 | if Is_Frozen (Subp) and then not Has_Completion (Subp) then |
996ae0b0 RK |
4026 | B_Node := |
4027 | Build_Renamed_Body | |
4028 | (Parent (Declaration_Node (Subp)), Defining_Entity (N)); | |
4029 | ||
4030 | if Is_Entity_Name (Name (N)) then | |
4031 | Old_S := Entity (Name (N)); | |
4032 | ||
fbf5a39b AC |
4033 | if not Is_Frozen (Old_S) |
4034 | and then Operating_Mode /= Check_Semantics | |
4035 | then | |
4036 | Append_Freeze_Action (Old_S, B_Node); | |
996ae0b0 RK |
4037 | else |
4038 | Insert_After (N, B_Node); | |
4039 | Analyze (B_Node); | |
4040 | end if; | |
4041 | ||
ac7d724d | 4042 | if Is_Intrinsic_Subprogram (Old_S) and then not In_Instance then |
996ae0b0 RK |
4043 | Error_Msg_N |
4044 | ("subprogram used in renaming_as_body cannot be intrinsic", | |
ac7d724d | 4045 | Name (N)); |
996ae0b0 RK |
4046 | end if; |
4047 | ||
4048 | else | |
4049 | Insert_After (N, B_Node); | |
4050 | Analyze (B_Node); | |
4051 | end if; | |
4052 | end if; | |
4053 | end Check_Frozen_Renaming; | |
4054 | ||
4c484f40 AC |
4055 | ------------------------------- |
4056 | -- Set_Entity_Or_Discriminal -- | |
4057 | ------------------------------- | |
4058 | ||
4059 | procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is | |
4060 | P : Node_Id; | |
4061 | ||
4062 | begin | |
4063 | -- If the entity is not a discriminant, or else expansion is disabled, | |
4064 | -- simply set the entity. | |
4065 | ||
4066 | if not In_Spec_Expression | |
4067 | or else Ekind (E) /= E_Discriminant | |
4068 | or else Inside_A_Generic | |
4069 | then | |
e7ba564f | 4070 | Set_Entity_With_Checks (N, E); |
4c484f40 AC |
4071 | |
4072 | -- The replacement of a discriminant by the corresponding discriminal | |
4073 | -- is not done for a task discriminant that appears in a default | |
8779dffa | 4074 | -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant |
4c484f40 AC |
4075 | -- for details on their handling. |
4076 | ||
4077 | elsif Is_Concurrent_Type (Scope (E)) then | |
4c484f40 AC |
4078 | P := Parent (N); |
4079 | while Present (P) | |
4080 | and then not Nkind_In (P, N_Parameter_Specification, | |
ac7d724d | 4081 | N_Component_Declaration) |
4c484f40 AC |
4082 | loop |
4083 | P := Parent (P); | |
4084 | end loop; | |
4085 | ||
4086 | if Present (P) | |
4087 | and then Nkind (P) = N_Parameter_Specification | |
4088 | then | |
4089 | null; | |
4090 | ||
4091 | else | |
4092 | Set_Entity (N, Discriminal (E)); | |
4093 | end if; | |
4094 | ||
4095 | -- Otherwise, this is a discriminant in a context in which | |
4096 | -- it is a reference to the corresponding parameter of the | |
4097 | -- init proc for the enclosing type. | |
4098 | ||
4099 | else | |
4100 | Set_Entity (N, Discriminal (E)); | |
4101 | end if; | |
4102 | end Set_Entity_Or_Discriminal; | |
4103 | ||
fbf5a39b AC |
4104 | ----------------------------------- |
4105 | -- Check_In_Previous_With_Clause -- | |
4106 | ----------------------------------- | |
4107 | ||
4108 | procedure Check_In_Previous_With_Clause | |
4109 | (N : Node_Id; | |
4110 | Nam : Entity_Id) | |
4111 | is | |
4112 | Pack : constant Entity_Id := Entity (Original_Node (Nam)); | |
4113 | Item : Node_Id; | |
4114 | Par : Node_Id; | |
4115 | ||
4116 | begin | |
4117 | Item := First (Context_Items (Parent (N))); | |
ac7d724d | 4118 | while Present (Item) and then Item /= N loop |
fbf5a39b | 4119 | if Nkind (Item) = N_With_Clause |
edd63e9b | 4120 | |
ac7d724d | 4121 | -- Protect the frontend against previous critical errors |
edd63e9b ES |
4122 | |
4123 | and then Nkind (Name (Item)) /= N_Selected_Component | |
fbf5a39b AC |
4124 | and then Entity (Name (Item)) = Pack |
4125 | then | |
4126 | Par := Nam; | |
4127 | ||
bc41faa2 | 4128 | -- Find root library unit in with_clause |
fbf5a39b AC |
4129 | |
4130 | while Nkind (Par) = N_Expanded_Name loop | |
4131 | Par := Prefix (Par); | |
4132 | end loop; | |
4133 | ||
4134 | if Is_Child_Unit (Entity (Original_Node (Par))) then | |
ed2233dc | 4135 | Error_Msg_NE ("& is not directly visible", Par, Entity (Par)); |
fbf5a39b AC |
4136 | else |
4137 | return; | |
4138 | end if; | |
4139 | end if; | |
4140 | ||
4141 | Next (Item); | |
4142 | end loop; | |
4143 | ||
4144 | -- On exit, package is not mentioned in a previous with_clause. | |
4145 | -- Check if its prefix is. | |
4146 | ||
4147 | if Nkind (Nam) = N_Expanded_Name then | |
4148 | Check_In_Previous_With_Clause (N, Prefix (Nam)); | |
4149 | ||
4150 | elsif Pack /= Any_Id then | |
4151 | Error_Msg_NE ("& is not visible", Nam, Pack); | |
4152 | end if; | |
4153 | end Check_In_Previous_With_Clause; | |
4154 | ||
996ae0b0 RK |
4155 | --------------------------------- |
4156 | -- Check_Library_Unit_Renaming -- | |
4157 | --------------------------------- | |
4158 | ||
4159 | procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is | |
4160 | New_E : Entity_Id; | |
4161 | ||
4162 | begin | |
4163 | if Nkind (Parent (N)) /= N_Compilation_Unit then | |
4164 | return; | |
4165 | ||
fbe627af RD |
4166 | -- Check for library unit. Note that we used to check for the scope |
4167 | -- being Standard here, but that was wrong for Standard itself. | |
4168 | ||
4169 | elsif not Is_Compilation_Unit (Old_E) | |
996ae0b0 RK |
4170 | and then not Is_Child_Unit (Old_E) |
4171 | then | |
4172 | Error_Msg_N ("renamed unit must be a library unit", Name (N)); | |
4173 | ||
615cbd95 AC |
4174 | -- Entities defined in Standard (operators and boolean literals) cannot |
4175 | -- be renamed as library units. | |
4176 | ||
4177 | elsif Scope (Old_E) = Standard_Standard | |
4178 | and then Sloc (Old_E) = Standard_Location | |
4179 | then | |
4180 | Error_Msg_N ("renamed unit must be a library unit", Name (N)); | |
4181 | ||
996ae0b0 RK |
4182 | elsif Present (Parent_Spec (N)) |
4183 | and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration | |
4184 | and then not Is_Child_Unit (Old_E) | |
4185 | then | |
4186 | Error_Msg_N | |
4187 | ("renamed unit must be a child unit of generic parent", Name (N)); | |
4188 | ||
4189 | elsif Nkind (N) in N_Generic_Renaming_Declaration | |
ac7d724d ES |
4190 | and then Nkind (Name (N)) = N_Expanded_Name |
4191 | and then Is_Generic_Instance (Entity (Prefix (Name (N)))) | |
4192 | and then Is_Generic_Unit (Old_E) | |
996ae0b0 RK |
4193 | then |
4194 | Error_Msg_N | |
4195 | ("renamed generic unit must be a library unit", Name (N)); | |
4196 | ||
b9b2405f ST |
4197 | elsif Is_Package_Or_Generic_Package (Old_E) then |
4198 | ||
996ae0b0 RK |
4199 | -- Inherit categorization flags |
4200 | ||
4201 | New_E := Defining_Entity (N); | |
4202 | Set_Is_Pure (New_E, Is_Pure (Old_E)); | |
4203 | Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E)); | |
4204 | Set_Is_Remote_Call_Interface (New_E, | |
4205 | Is_Remote_Call_Interface (Old_E)); | |
4206 | Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E)); | |
4207 | Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E)); | |
4208 | end if; | |
4209 | end Check_Library_Unit_Renaming; | |
4210 | ||
ac7d724d ES |
4211 | ------------------------ |
4212 | -- Enclosing_Instance -- | |
4213 | ------------------------ | |
4214 | ||
4215 | function Enclosing_Instance return Entity_Id is | |
4216 | S : Entity_Id; | |
4217 | ||
4218 | begin | |
4219 | if not Is_Generic_Instance (Current_Scope) then | |
4220 | return Empty; | |
4221 | end if; | |
4222 | ||
4223 | S := Scope (Current_Scope); | |
4224 | while S /= Standard_Standard loop | |
4225 | if Is_Generic_Instance (S) then | |
4226 | return S; | |
4227 | end if; | |
4228 | ||
4229 | S := Scope (S); | |
4230 | end loop; | |
4231 | ||
4232 | return Empty; | |
4233 | end Enclosing_Instance; | |
4234 | ||
996ae0b0 RK |
4235 | --------------- |
4236 | -- End_Scope -- | |
4237 | --------------- | |
4238 | ||
4239 | procedure End_Scope is | |
4240 | Id : Entity_Id; | |
4241 | Prev : Entity_Id; | |
4242 | Outer : Entity_Id; | |
4243 | ||
4244 | begin | |
4245 | Id := First_Entity (Current_Scope); | |
996ae0b0 RK |
4246 | while Present (Id) loop |
4247 | -- An entity in the current scope is not necessarily the first one | |
4248 | -- on its homonym chain. Find its predecessor if any, | |
4249 | -- If it is an internal entity, it will not be in the visibility | |
4250 | -- chain altogether, and there is nothing to unchain. | |
4251 | ||
4252 | if Id /= Current_Entity (Id) then | |
4253 | Prev := Current_Entity (Id); | |
4254 | while Present (Prev) | |
4255 | and then Present (Homonym (Prev)) | |
4256 | and then Homonym (Prev) /= Id | |
4257 | loop | |
4258 | Prev := Homonym (Prev); | |
4259 | end loop; | |
4260 | ||
4261 | -- Skip to end of loop if Id is not in the visibility chain | |
4262 | ||
4263 | if No (Prev) or else Homonym (Prev) /= Id then | |
4264 | goto Next_Ent; | |
4265 | end if; | |
4266 | ||
4267 | else | |
4268 | Prev := Empty; | |
4269 | end if; | |
4270 | ||
996ae0b0 RK |
4271 | Set_Is_Immediately_Visible (Id, False); |
4272 | ||
16ca248a | 4273 | Outer := Homonym (Id); |
996ae0b0 RK |
4274 | while Present (Outer) and then Scope (Outer) = Current_Scope loop |
4275 | Outer := Homonym (Outer); | |
4276 | end loop; | |
4277 | ||
4278 | -- Reset homonym link of other entities, but do not modify link | |
4279 | -- between entities in current scope, so that the back-end can have | |
4280 | -- a proper count of local overloadings. | |
4281 | ||
4282 | if No (Prev) then | |
4283 | Set_Name_Entity_Id (Chars (Id), Outer); | |
4284 | ||
4285 | elsif Scope (Prev) /= Scope (Id) then | |
4286 | Set_Homonym (Prev, Outer); | |
4287 | end if; | |
4288 | ||
4289 | <<Next_Ent>> | |
4290 | Next_Entity (Id); | |
4291 | end loop; | |
4292 | ||
4293 | -- If the scope generated freeze actions, place them before the | |
4294 | -- current declaration and analyze them. Type declarations and | |
4295 | -- the bodies of initialization procedures can generate such nodes. | |
4296 | -- We follow the parent chain until we reach a list node, which is | |
4297 | -- the enclosing list of declarations. If the list appears within | |
4298 | -- a protected definition, move freeze nodes outside the protected | |
4299 | -- type altogether. | |
4300 | ||
4301 | if Present | |
4302 | (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions) | |
4303 | then | |
4304 | declare | |
4305 | Decl : Node_Id; | |
4306 | L : constant List_Id := Scope_Stack.Table | |
4307 | (Scope_Stack.Last).Pending_Freeze_Actions; | |
4308 | ||
4309 | begin | |
4310 | if Is_Itype (Current_Scope) then | |
4311 | Decl := Associated_Node_For_Itype (Current_Scope); | |
4312 | else | |
4313 | Decl := Parent (Current_Scope); | |
4314 | end if; | |
4315 | ||
4316 | Pop_Scope; | |
4317 | ||
4318 | while not (Is_List_Member (Decl)) | |
294ccb21 RD |
4319 | or else Nkind_In (Parent (Decl), N_Protected_Definition, |
4320 | N_Task_Definition) | |
996ae0b0 RK |
4321 | loop |
4322 | Decl := Parent (Decl); | |
4323 | end loop; | |
4324 | ||
4325 | Insert_List_Before_And_Analyze (Decl, L); | |
4326 | end; | |
4327 | ||
4328 | else | |
4329 | Pop_Scope; | |
4330 | end if; | |
996ae0b0 RK |
4331 | end End_Scope; |
4332 | ||
4333 | --------------------- | |
4334 | -- End_Use_Clauses -- | |
4335 | --------------------- | |
4336 | ||
4337 | procedure End_Use_Clauses (Clause : Node_Id) is | |
fbf5a39b | 4338 | U : Node_Id; |
996ae0b0 RK |
4339 | |
4340 | begin | |
fbf5a39b AC |
4341 | -- Remove Use_Type clauses first, because they affect the |
4342 | -- visibility of operators in subsequent used packages. | |
4343 | ||
4344 | U := Clause; | |
4345 | while Present (U) loop | |
4346 | if Nkind (U) = N_Use_Type_Clause then | |
4347 | End_Use_Type (U); | |
4348 | end if; | |
4349 | ||
4350 | Next_Use_Clause (U); | |
4351 | end loop; | |
4352 | ||
4353 | U := Clause; | |
996ae0b0 RK |
4354 | while Present (U) loop |
4355 | if Nkind (U) = N_Use_Package_Clause then | |
4356 | End_Use_Package (U); | |
996ae0b0 RK |
4357 | end if; |
4358 | ||
4359 | Next_Use_Clause (U); | |
4360 | end loop; | |
4361 | end End_Use_Clauses; | |
4362 | ||
4363 | --------------------- | |
4364 | -- End_Use_Package -- | |
4365 | --------------------- | |
4366 | ||
4367 | procedure End_Use_Package (N : Node_Id) is | |
4368 | Pack_Name : Node_Id; | |
4369 | Pack : Entity_Id; | |
4370 | Id : Entity_Id; | |
4371 | Elmt : Elmt_Id; | |
4372 | ||
a780db15 | 4373 | function Is_Primitive_Operator_In_Use |
2e071734 AC |
4374 | (Op : Entity_Id; |
4375 | F : Entity_Id) return Boolean; | |
fbf5a39b AC |
4376 | -- Check whether Op is a primitive operator of a use-visible type |
4377 | ||
a780db15 AC |
4378 | ---------------------------------- |
4379 | -- Is_Primitive_Operator_In_Use -- | |
4380 | ---------------------------------- | |
fbf5a39b | 4381 | |
a780db15 | 4382 | function Is_Primitive_Operator_In_Use |
2e071734 AC |
4383 | (Op : Entity_Id; |
4384 | F : Entity_Id) return Boolean | |
fbf5a39b | 4385 | is |
95eb8b69 | 4386 | T : constant Entity_Id := Base_Type (Etype (F)); |
fbf5a39b | 4387 | begin |
95eb8b69 | 4388 | return In_Use (T) and then Scope (T) = Scope (Op); |
a780db15 | 4389 | end Is_Primitive_Operator_In_Use; |
fbf5a39b AC |
4390 | |
4391 | -- Start of processing for End_Use_Package | |
4392 | ||
996ae0b0 RK |
4393 | begin |
4394 | Pack_Name := First (Names (N)); | |
996ae0b0 | 4395 | while Present (Pack_Name) loop |
996ae0b0 | 4396 | |
462c31ef ST |
4397 | -- Test that Pack_Name actually denotes a package before processing |
4398 | ||
4399 | if Is_Entity_Name (Pack_Name) | |
4400 | and then Ekind (Entity (Pack_Name)) = E_Package | |
4401 | then | |
4402 | Pack := Entity (Pack_Name); | |
4403 | ||
996ae0b0 RK |
4404 | if In_Open_Scopes (Pack) then |
4405 | null; | |
4406 | ||
4407 | elsif not Redundant_Use (Pack_Name) then | |
4408 | Set_In_Use (Pack, False); | |
d4810530 | 4409 | Set_Current_Use_Clause (Pack, Empty); |
996ae0b0 | 4410 | |
16ca248a | 4411 | Id := First_Entity (Pack); |
996ae0b0 RK |
4412 | while Present (Id) loop |
4413 | ||
fbf5a39b | 4414 | -- Preserve use-visibility of operators that are primitive |
954c111a | 4415 | -- operators of a type that is use-visible through an active |
fbf5a39b | 4416 | -- use_type clause. |
996ae0b0 RK |
4417 | |
4418 | if Nkind (Id) = N_Defining_Operator_Symbol | |
4419 | and then | |
ac7d724d ES |
4420 | (Is_Primitive_Operator_In_Use (Id, First_Formal (Id)) |
4421 | or else | |
4422 | (Present (Next_Formal (First_Formal (Id))) | |
4423 | and then | |
4424 | Is_Primitive_Operator_In_Use | |
4425 | (Id, Next_Formal (First_Formal (Id))))) | |
996ae0b0 RK |
4426 | then |
4427 | null; | |
996ae0b0 RK |
4428 | else |
4429 | Set_Is_Potentially_Use_Visible (Id, False); | |
4430 | end if; | |
4431 | ||
4432 | if Is_Private_Type (Id) | |
4433 | and then Present (Full_View (Id)) | |
4434 | then | |
4435 | Set_Is_Potentially_Use_Visible (Full_View (Id), False); | |
4436 | end if; | |
4437 | ||
4438 | Next_Entity (Id); | |
4439 | end loop; | |
4440 | ||
4441 | if Present (Renamed_Object (Pack)) then | |
4442 | Set_In_Use (Renamed_Object (Pack), False); | |
d4810530 | 4443 | Set_Current_Use_Clause (Renamed_Object (Pack), Empty); |
996ae0b0 RK |
4444 | end if; |
4445 | ||
4446 | if Chars (Pack) = Name_System | |
4447 | and then Scope (Pack) = Standard_Standard | |
4448 | and then Present_System_Aux | |
4449 | then | |
4450 | Id := First_Entity (System_Aux_Id); | |
996ae0b0 RK |
4451 | while Present (Id) loop |
4452 | Set_Is_Potentially_Use_Visible (Id, False); | |
4453 | ||
4454 | if Is_Private_Type (Id) | |
4455 | and then Present (Full_View (Id)) | |
4456 | then | |
4457 | Set_Is_Potentially_Use_Visible (Full_View (Id), False); | |
4458 | end if; | |
4459 | ||
4460 | Next_Entity (Id); | |
4461 | end loop; | |
4462 | ||
4463 | Set_In_Use (System_Aux_Id, False); | |
4464 | end if; | |
4465 | ||
4466 | else | |
4467 | Set_Redundant_Use (Pack_Name, False); | |
4468 | end if; | |
996ae0b0 RK |
4469 | end if; |
4470 | ||
4471 | Next (Pack_Name); | |
4472 | end loop; | |
4473 | ||
4474 | if Present (Hidden_By_Use_Clause (N)) then | |
4475 | Elmt := First_Elmt (Hidden_By_Use_Clause (N)); | |
996ae0b0 | 4476 | while Present (Elmt) loop |
e69614ad AC |
4477 | declare |
4478 | E : constant Entity_Id := Node (Elmt); | |
4479 | ||
4480 | begin | |
4481 | -- Reset either Use_Visibility or Direct_Visibility, depending | |
4482 | -- on how the entity was hidden by the use clause. | |
4483 | ||
4484 | if In_Use (Scope (E)) | |
4485 | and then Used_As_Generic_Actual (Scope (E)) | |
4486 | then | |
4487 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
4488 | else | |
4489 | Set_Is_Immediately_Visible (Node (Elmt)); | |
4490 | end if; | |
4491 | ||
4492 | Next_Elmt (Elmt); | |
4493 | end; | |
996ae0b0 RK |
4494 | end loop; |
4495 | ||
4496 | Set_Hidden_By_Use_Clause (N, No_Elist); | |
4497 | end if; | |
4498 | end End_Use_Package; | |
4499 | ||
4500 | ------------------ | |
4501 | -- End_Use_Type -- | |
4502 | ------------------ | |
4503 | ||
4504 | procedure End_Use_Type (N : Node_Id) is | |
4a214958 | 4505 | Elmt : Elmt_Id; |
996ae0b0 | 4506 | Id : Entity_Id; |
996ae0b0 RK |
4507 | T : Entity_Id; |
4508 | ||
4a214958 AC |
4509 | -- Start of processing for End_Use_Type |
4510 | ||
996ae0b0 RK |
4511 | begin |
4512 | Id := First (Subtype_Marks (N)); | |
996ae0b0 | 4513 | while Present (Id) loop |
fbf5a39b | 4514 | |
4a214958 | 4515 | -- A call to Rtsfind may occur while analyzing a use_type clause, |
fbf5a39b AC |
4516 | -- in which case the type marks are not resolved yet, and there is |
4517 | -- nothing to remove. | |
4518 | ||
4a214958 | 4519 | if not Is_Entity_Name (Id) or else No (Entity (Id)) then |
fbf5a39b AC |
4520 | goto Continue; |
4521 | end if; | |
4522 | ||
996ae0b0 RK |
4523 | T := Entity (Id); |
4524 | ||
7b56a91b | 4525 | if T = Any_Type or else From_Limited_With (T) then |
996ae0b0 RK |
4526 | null; |
4527 | ||
4a214958 | 4528 | -- Note that the use_type clause may mention a subtype of the type |
16ca248a ES |
4529 | -- whose primitive operations have been made visible. Here as |
4530 | -- elsewhere, it is the base type that matters for visibility. | |
996ae0b0 RK |
4531 | |
4532 | elsif In_Open_Scopes (Scope (Base_Type (T))) then | |
4533 | null; | |
4534 | ||
4535 | elsif not Redundant_Use (Id) then | |
4536 | Set_In_Use (T, False); | |
4537 | Set_In_Use (Base_Type (T), False); | |
21d27997 RD |
4538 | Set_Current_Use_Clause (T, Empty); |
4539 | Set_Current_Use_Clause (Base_Type (T), Empty); | |
996ae0b0 RK |
4540 | end if; |
4541 | ||
fbf5a39b | 4542 | <<Continue>> |
29efbb8c | 4543 | Next (Id); |
996ae0b0 | 4544 | end loop; |
29efbb8c ES |
4545 | |
4546 | if Is_Empty_Elmt_List (Used_Operations (N)) then | |
4547 | return; | |
4548 | ||
4549 | else | |
4550 | Elmt := First_Elmt (Used_Operations (N)); | |
4551 | while Present (Elmt) loop | |
4552 | Set_Is_Potentially_Use_Visible (Node (Elmt), False); | |
4553 | Next_Elmt (Elmt); | |
4554 | end loop; | |
4555 | end if; | |
996ae0b0 RK |
4556 | end End_Use_Type; |
4557 | ||
4558 | ---------------------- | |
4559 | -- Find_Direct_Name -- | |
4560 | ---------------------- | |
4561 | ||
4562 | procedure Find_Direct_Name (N : Node_Id) is | |
4563 | E : Entity_Id; | |
4564 | E2 : Entity_Id; | |
4565 | Msg : Boolean; | |
4566 | ||
4567 | Inst : Entity_Id := Empty; | |
bc41faa2 | 4568 | -- Enclosing instance, if any |
996ae0b0 RK |
4569 | |
4570 | Homonyms : Entity_Id; | |
4571 | -- Saves start of homonym chain | |
4572 | ||
4573 | Nvis_Entity : Boolean; | |
3764bb00 BD |
4574 | -- Set True to indicate that there is at least one entity on the homonym |
4575 | -- chain which, while not visible, is visible enough from the user point | |
4576 | -- of view to warrant an error message of "not visible" rather than | |
4577 | -- undefined. | |
996ae0b0 | 4578 | |
9bc856dd | 4579 | Nvis_Is_Private_Subprg : Boolean := False; |
0ab80019 | 4580 | -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais |
9bc856dd AC |
4581 | -- effect concerning library subprograms has been detected. Used to |
4582 | -- generate the precise error message. | |
4583 | ||
996ae0b0 | 4584 | function From_Actual_Package (E : Entity_Id) return Boolean; |
fba9ebfc | 4585 | -- Returns true if the entity is an actual for a package that is itself |
996ae0b0 | 4586 | -- an actual for a formal package of the current instance. Such an |
fba9ebfc AC |
4587 | -- entity requires special handling because it may be use-visible but |
4588 | -- hides directly visible entities defined outside the instance, because | |
4589 | -- the corresponding formal did so in the generic. | |
996ae0b0 | 4590 | |
67ce0d7e RD |
4591 | function Is_Actual_Parameter return Boolean; |
4592 | -- This function checks if the node N is an identifier that is an actual | |
4593 | -- parameter of a procedure call. If so it returns True, otherwise it | |
4594 | -- return False. The reason for this check is that at this stage we do | |
4595 | -- not know what procedure is being called if the procedure might be | |
4596 | -- overloaded, so it is premature to go setting referenced flags or | |
4597 | -- making calls to Generate_Reference. We will wait till Resolve_Actuals | |
4598 | -- for that processing | |
4599 | ||
996ae0b0 | 4600 | function Known_But_Invisible (E : Entity_Id) return Boolean; |
a43f6434 AC |
4601 | -- This function determines whether a reference to the entity E, which |
4602 | -- is not visible, can reasonably be considered to be known to the | |
4603 | -- writer of the reference. This is a heuristic test, used only for | |
4604 | -- the purposes of figuring out whether we prefer to complain that an | |
4605 | -- entity is undefined or invisible (and identify the declaration of | |
4606 | -- the invisible entity in the latter case). The point here is that we | |
4607 | -- don't want to complain that something is invisible and then point to | |
4608 | -- something entirely mysterious to the writer. | |
996ae0b0 RK |
4609 | |
4610 | procedure Nvis_Messages; | |
4611 | -- Called if there are no visible entries for N, but there is at least | |
4612 | -- one non-directly visible, or hidden declaration. This procedure | |
4613 | -- outputs an appropriate set of error messages. | |
4614 | ||
4615 | procedure Undefined (Nvis : Boolean); | |
4616 | -- This function is called if the current node has no corresponding | |
4617 | -- visible entity or entities. The value set in Msg indicates whether | |
4618 | -- an error message was generated (multiple error messages for the | |
4619 | -- same variable are generally suppressed, see body for details). | |
4620 | -- Msg is True if an error message was generated, False if not. This | |
4621 | -- value is used by the caller to determine whether or not to output | |
4622 | -- additional messages where appropriate. The parameter is set False | |
4623 | -- to get the message "X is undefined", and True to get the message | |
4624 | -- "X is not visible". | |
4625 | ||
4626 | ------------------------- | |
4627 | -- From_Actual_Package -- | |
4628 | ------------------------- | |
4629 | ||
4630 | function From_Actual_Package (E : Entity_Id) return Boolean is | |
4631 | Scop : constant Entity_Id := Scope (E); | |
fba9ebfc AC |
4632 | -- Declared scope of candidate entity |
4633 | ||
4634 | Act : Entity_Id; | |
4635 | ||
4636 | function Declared_In_Actual (Pack : Entity_Id) return Boolean; | |
4637 | -- Recursive function that does the work and examines actuals of | |
4638 | -- actual packages of current instance. | |
4639 | ||
4640 | ------------------------ | |
4641 | -- Declared_In_Actual -- | |
4642 | ------------------------ | |
4643 | ||
4644 | function Declared_In_Actual (Pack : Entity_Id) return Boolean is | |
4645 | Act : Entity_Id; | |
4646 | ||
4647 | begin | |
4648 | if No (Associated_Formal_Package (Pack)) then | |
4649 | return False; | |
4650 | ||
4651 | else | |
4652 | Act := First_Entity (Pack); | |
4653 | while Present (Act) loop | |
4654 | if Renamed_Object (Pack) = Scop then | |
4655 | return True; | |
4656 | ||
4657 | -- Check for end of list of actuals. | |
4658 | ||
4659 | elsif Ekind (Act) = E_Package | |
4660 | and then Renamed_Object (Act) = Pack | |
4661 | then | |
4662 | return False; | |
4663 | ||
4664 | elsif Ekind (Act) = E_Package | |
4665 | and then Declared_In_Actual (Act) | |
4666 | then | |
4667 | return True; | |
4668 | end if; | |
4669 | ||
4670 | Next_Entity (Act); | |
4671 | end loop; | |
4672 | ||
4673 | return False; | |
4674 | end if; | |
4675 | end Declared_In_Actual; | |
4676 | ||
4677 | -- Start of processing for From_Actual_Package | |
996ae0b0 RK |
4678 | |
4679 | begin | |
4680 | if not In_Instance then | |
4681 | return False; | |
fba9ebfc | 4682 | |
996ae0b0 RK |
4683 | else |
4684 | Inst := Current_Scope; | |
996ae0b0 RK |
4685 | while Present (Inst) |
4686 | and then Ekind (Inst) /= E_Package | |
4687 | and then not Is_Generic_Instance (Inst) | |
4688 | loop | |
4689 | Inst := Scope (Inst); | |
4690 | end loop; | |
4691 | ||
4692 | if No (Inst) then | |
4693 | return False; | |
4694 | end if; | |
4695 | ||
4696 | Act := First_Entity (Inst); | |
996ae0b0 | 4697 | while Present (Act) loop |
fba9ebfc AC |
4698 | if Ekind (Act) = E_Package |
4699 | and then Declared_In_Actual (Act) | |
4700 | then | |
4701 | return True; | |
996ae0b0 | 4702 | end if; |
fba9ebfc AC |
4703 | |
4704 | Next_Entity (Act); | |
996ae0b0 RK |
4705 | end loop; |
4706 | ||
4707 | return False; | |
4708 | end if; | |
4709 | end From_Actual_Package; | |
4710 | ||
67ce0d7e RD |
4711 | ------------------------- |
4712 | -- Is_Actual_Parameter -- | |
4713 | ------------------------- | |
4714 | ||
4715 | function Is_Actual_Parameter return Boolean is | |
4716 | begin | |
4717 | return | |
4718 | Nkind (N) = N_Identifier | |
4719 | and then | |
4720 | (Nkind (Parent (N)) = N_Procedure_Call_Statement | |
ac7d724d ES |
4721 | or else |
4722 | (Nkind (Parent (N)) = N_Parameter_Association | |
4723 | and then N = Explicit_Actual_Parameter (Parent (N)) | |
4724 | and then Nkind (Parent (Parent (N))) = | |
67ce0d7e RD |
4725 | N_Procedure_Call_Statement)); |
4726 | end Is_Actual_Parameter; | |
4727 | ||
996ae0b0 RK |
4728 | ------------------------- |
4729 | -- Known_But_Invisible -- | |
4730 | ------------------------- | |
4731 | ||
4732 | function Known_But_Invisible (E : Entity_Id) return Boolean is | |
4733 | Fname : File_Name_Type; | |
4734 | ||
4735 | begin | |
4736 | -- Entities in Standard are always considered to be known | |
4737 | ||
4738 | if Sloc (E) <= Standard_Location then | |
4739 | return True; | |
4740 | ||
4741 | -- An entity that does not come from source is always considered | |
4742 | -- to be unknown, since it is an artifact of code expansion. | |
4743 | ||
4744 | elsif not Comes_From_Source (E) then | |
4745 | return False; | |
4746 | ||
a43f6434 AC |
4747 | -- In gnat internal mode, we consider all entities known. The |
4748 | -- historical reason behind this discrepancy is not known??? But the | |
4749 | -- only effect is to modify the error message given, so it is not | |
4750 | -- critical. Since it only affects the exact wording of error | |
4751 | -- messages in illegal programs, we do not mention this as an | |
4752 | -- effect of -gnatg, since it is not a language modification. | |
996ae0b0 RK |
4753 | |
4754 | elsif GNAT_Mode then | |
4755 | return True; | |
4756 | end if; | |
4757 | ||
4758 | -- Here we have an entity that is not from package Standard, and | |
4759 | -- which comes from Source. See if it comes from an internal file. | |
4760 | ||
4761 | Fname := Unit_File_Name (Get_Source_Unit (E)); | |
4762 | ||
4763 | -- Case of from internal file | |
4764 | ||
4765 | if Is_Internal_File_Name (Fname) then | |
4766 | ||
4767 | -- Private part entities in internal files are never considered | |
4768 | -- to be known to the writer of normal application code. | |
4769 | ||
4770 | if Is_Hidden (E) then | |
4771 | return False; | |
4772 | end if; | |
4773 | ||
4774 | -- Entities from System packages other than System and | |
4775 | -- System.Storage_Elements are not considered to be known. | |
4776 | -- System.Auxxxx files are also considered known to the user. | |
4777 | ||
4778 | -- Should refine this at some point to generally distinguish | |
4779 | -- between known and unknown internal files ??? | |
4780 | ||
4781 | Get_Name_String (Fname); | |
4782 | ||
4783 | return | |
4784 | Name_Len < 2 | |
4785 | or else | |
4786 | Name_Buffer (1 .. 2) /= "s-" | |
4787 | or else | |
4788 | Name_Buffer (3 .. 8) = "stoele" | |
4789 | or else | |
4790 | Name_Buffer (3 .. 5) = "aux"; | |
4791 | ||
497a660d AC |
4792 | -- If not an internal file, then entity is definitely known, even if |
4793 | -- it is in a private part (the message generated will note that it | |
4794 | -- is in a private part). | |
996ae0b0 RK |
4795 | |
4796 | else | |
4797 | return True; | |
4798 | end if; | |
4799 | end Known_But_Invisible; | |
4800 | ||
4801 | ------------------- | |
4802 | -- Nvis_Messages -- | |
4803 | ------------------- | |
4804 | ||
4805 | procedure Nvis_Messages is | |
9bc856dd AC |
4806 | Comp_Unit : Node_Id; |
4807 | Ent : Entity_Id; | |
1175f0b6 | 4808 | Found : Boolean := False; |
9bc856dd AC |
4809 | Hidden : Boolean := False; |
4810 | Item : Node_Id; | |
996ae0b0 RK |
4811 | |
4812 | begin | |
0ab80019 | 4813 | -- Ada 2005 (AI-262): Generate a precise error concerning the |
9bc856dd AC |
4814 | -- Beaujolais effect that was previously detected |
4815 | ||
4816 | if Nvis_Is_Private_Subprg then | |
4817 | ||
4818 | pragma Assert (Nkind (E2) = N_Defining_Identifier | |
16ca248a ES |
4819 | and then Ekind (E2) = E_Function |
4820 | and then Scope (E2) = Standard_Standard | |
4821 | and then Has_Private_With (E2)); | |
9bc856dd AC |
4822 | |
4823 | -- Find the sloc corresponding to the private with'ed unit | |
4824 | ||
16ca248a | 4825 | Comp_Unit := Cunit (Current_Sem_Unit); |
9bc856dd AC |
4826 | Error_Msg_Sloc := No_Location; |
4827 | ||
16ca248a | 4828 | Item := First (Context_Items (Comp_Unit)); |
9bc856dd AC |
4829 | while Present (Item) loop |
4830 | if Nkind (Item) = N_With_Clause | |
4831 | and then Private_Present (Item) | |
4832 | and then Entity (Name (Item)) = E2 | |
4833 | then | |
4834 | Error_Msg_Sloc := Sloc (Item); | |
4835 | exit; | |
4836 | end if; | |
4837 | ||
4838 | Next (Item); | |
4839 | end loop; | |
4840 | ||
4841 | pragma Assert (Error_Msg_Sloc /= No_Location); | |
4842 | ||
0ab80019 | 4843 | Error_Msg_N ("(Ada 2005): hidden by private with clause #", N); |
9bc856dd AC |
4844 | return; |
4845 | end if; | |
4846 | ||
996ae0b0 RK |
4847 | Undefined (Nvis => True); |
4848 | ||
4849 | if Msg then | |
4850 | ||
4851 | -- First loop does hidden declarations | |
4852 | ||
4853 | Ent := Homonyms; | |
4854 | while Present (Ent) loop | |
4855 | if Is_Potentially_Use_Visible (Ent) then | |
996ae0b0 | 4856 | if not Hidden then |
deef4289 AC |
4857 | Error_Msg_N -- CODEFIX |
4858 | ("multiple use clauses cause hiding!", N); | |
996ae0b0 RK |
4859 | Hidden := True; |
4860 | end if; | |
4861 | ||
4862 | Error_Msg_Sloc := Sloc (Ent); | |
4e7a4f6e AC |
4863 | Error_Msg_N -- CODEFIX |
4864 | ("hidden declaration#!", N); | |
996ae0b0 RK |
4865 | end if; |
4866 | ||
4867 | Ent := Homonym (Ent); | |
4868 | end loop; | |
4869 | ||
4870 | -- If we found hidden declarations, then that's enough, don't | |
4871 | -- bother looking for non-visible declarations as well. | |
4872 | ||
4873 | if Hidden then | |
4874 | return; | |
4875 | end if; | |
4876 | ||
4877 | -- Second loop does non-directly visible declarations | |
4878 | ||
4879 | Ent := Homonyms; | |
4880 | while Present (Ent) loop | |
4881 | if not Is_Potentially_Use_Visible (Ent) then | |
4882 | ||
4883 | -- Do not bother the user with unknown entities | |
4884 | ||
4885 | if not Known_But_Invisible (Ent) then | |
4886 | goto Continue; | |
4887 | end if; | |
4888 | ||
4889 | Error_Msg_Sloc := Sloc (Ent); | |
4890 | ||
4891 | -- Output message noting that there is a non-visible | |
4892 | -- declaration, distinguishing the private part case. | |
4893 | ||
4894 | if Is_Hidden (Ent) then | |
4895 | Error_Msg_N ("non-visible (private) declaration#!", N); | |
1175f0b6 AC |
4896 | |
4897 | -- If the entity is declared in a generic package, it | |
4898 | -- cannot be visible, so there is no point in adding it | |
4899 | -- to the list of candidates if another homograph from a | |
4900 | -- non-generic package has been seen. | |
4901 | ||
4902 | elsif Ekind (Scope (Ent)) = E_Generic_Package | |
4903 | and then Found | |
4904 | then | |
4905 | null; | |
4906 | ||
996ae0b0 | 4907 | else |
483c78cb RD |
4908 | Error_Msg_N -- CODEFIX |
4909 | ("non-visible declaration#!", N); | |
fbf5a39b | 4910 | |
1175f0b6 AC |
4911 | if Ekind (Scope (Ent)) /= E_Generic_Package then |
4912 | Found := True; | |
4913 | end if; | |
4914 | ||
fbf5a39b AC |
4915 | if Is_Compilation_Unit (Ent) |
4916 | and then | |
4917 | Nkind (Parent (Parent (N))) = N_Use_Package_Clause | |
4918 | then | |
16ca248a | 4919 | Error_Msg_Qual_Level := 99; |
ed2233dc AC |
4920 | Error_Msg_NE -- CODEFIX |
4921 | ("\\missing `WITH &;`", N, Ent); | |
16ca248a | 4922 | Error_Msg_Qual_Level := 0; |
fbf5a39b | 4923 | end if; |
e1b871e9 AC |
4924 | |
4925 | if Ekind (Ent) = E_Discriminant | |
4926 | and then Present (Corresponding_Discriminant (Ent)) | |
4927 | and then Scope (Corresponding_Discriminant (Ent)) = | |
4928 | Etype (Scope (Ent)) | |
4929 | then | |
4930 | Error_Msg_N | |
4931 | ("inherited discriminant not allowed here" & | |
4932 | " (RM 3.8 (12), 3.8.1 (6))!", N); | |
4933 | end if; | |
996ae0b0 | 4934 | end if; |
07fc65c4 GB |
4935 | |
4936 | -- Set entity and its containing package as referenced. We | |
4937 | -- can't be sure of this, but this seems a better choice | |
4938 | -- to avoid unused entity messages. | |
4939 | ||
4940 | if Comes_From_Source (Ent) then | |
4941 | Set_Referenced (Ent); | |
4942 | Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent))); | |
4943 | end if; | |
996ae0b0 RK |
4944 | end if; |
4945 | ||
4946 | <<Continue>> | |
4947 | Ent := Homonym (Ent); | |
4948 | end loop; | |
996ae0b0 RK |
4949 | end if; |
4950 | end Nvis_Messages; | |
4951 | ||
4952 | --------------- | |
4953 | -- Undefined -- | |
4954 | --------------- | |
4955 | ||
4956 | procedure Undefined (Nvis : Boolean) is | |
4957 | Emsg : Error_Msg_Id; | |
4958 | ||
4959 | begin | |
fbf5a39b AC |
4960 | -- We should never find an undefined internal name. If we do, then |
4961 | -- see if we have previous errors. If so, ignore on the grounds that | |
4962 | -- it is probably a cascaded message (e.g. a block label from a badly | |
4963 | -- formed block). If no previous errors, then we have a real internal | |
4964 | -- error of some kind so raise an exception. | |
4965 | ||
4966 | if Is_Internal_Name (Chars (N)) then | |
4967 | if Total_Errors_Detected /= 0 then | |
4968 | return; | |
4969 | else | |
4970 | raise Program_Error; | |
4971 | end if; | |
4972 | end if; | |
4973 | ||
996ae0b0 RK |
4974 | -- A very specialized error check, if the undefined variable is |
4975 | -- a case tag, and the case type is an enumeration type, check | |
4976 | -- for a possible misspelling, and if so, modify the identifier | |
4977 | ||
4978 | -- Named aggregate should also be handled similarly ??? | |
4979 | ||
4980 | if Nkind (N) = N_Identifier | |
4981 | and then Nkind (Parent (N)) = N_Case_Statement_Alternative | |
4982 | then | |
996ae0b0 | 4983 | declare |
996ae0b0 RK |
4984 | Case_Stm : constant Node_Id := Parent (Parent (N)); |
4985 | Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm)); | |
4986 | ||
4987 | Lit : Node_Id; | |
4988 | ||
4989 | begin | |
4990 | if Is_Enumeration_Type (Case_Typ) | |
21d27997 | 4991 | and then not Is_Standard_Character_Type (Case_Typ) |
996ae0b0 RK |
4992 | then |
4993 | Lit := First_Literal (Case_Typ); | |
4994 | Get_Name_String (Chars (Lit)); | |
4995 | ||
4996 | if Chars (Lit) /= Chars (N) | |
0b7f0f0e AC |
4997 | and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit)) |
4998 | then | |
996ae0b0 | 4999 | Error_Msg_Node_2 := Lit; |
ed2233dc | 5000 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
5001 | ("& is undefined, assume misspelling of &", N); |
5002 | Rewrite (N, New_Occurrence_Of (Lit, Sloc (N))); | |
5003 | return; | |
5004 | end if; | |
5005 | ||
5006 | Lit := Next_Literal (Lit); | |
5007 | end if; | |
5008 | end; | |
5009 | end if; | |
5010 | ||
5011 | -- Normal processing | |
5012 | ||
5013 | Set_Entity (N, Any_Id); | |
5014 | Set_Etype (N, Any_Type); | |
5015 | ||
5016 | -- We use the table Urefs to keep track of entities for which we | |
5017 | -- have issued errors for undefined references. Multiple errors | |
5018 | -- for a single name are normally suppressed, however we modify | |
5019 | -- the error message to alert the programmer to this effect. | |
5020 | ||
5021 | for J in Urefs.First .. Urefs.Last loop | |
5022 | if Chars (N) = Chars (Urefs.Table (J).Node) then | |
5023 | if Urefs.Table (J).Err /= No_Error_Msg | |
5024 | and then Sloc (N) /= Urefs.Table (J).Loc | |
5025 | then | |
5026 | Error_Msg_Node_1 := Urefs.Table (J).Node; | |
5027 | ||
5028 | if Urefs.Table (J).Nvis then | |
5029 | Change_Error_Text (Urefs.Table (J).Err, | |
5030 | "& is not visible (more references follow)"); | |
5031 | else | |
5032 | Change_Error_Text (Urefs.Table (J).Err, | |
5033 | "& is undefined (more references follow)"); | |
5034 | end if; | |
5035 | ||
5036 | Urefs.Table (J).Err := No_Error_Msg; | |
5037 | end if; | |
5038 | ||
5039 | -- Although we will set Msg False, and thus suppress the | |
5040 | -- message, we also set Error_Posted True, to avoid any | |
5041 | -- cascaded messages resulting from the undefined reference. | |
5042 | ||
5043 | Msg := False; | |
5044 | Set_Error_Posted (N, True); | |
5045 | return; | |
5046 | end if; | |
5047 | end loop; | |
5048 | ||
5049 | -- If entry not found, this is first undefined occurrence | |
5050 | ||
5051 | if Nvis then | |
5052 | Error_Msg_N ("& is not visible!", N); | |
5053 | Emsg := Get_Msg_Id; | |
5054 | ||
5055 | else | |
5056 | Error_Msg_N ("& is undefined!", N); | |
5057 | Emsg := Get_Msg_Id; | |
5058 | ||
5059 | -- A very bizarre special check, if the undefined identifier | |
5060 | -- is put or put_line, then add a special error message (since | |
5061 | -- this is a very common error for beginners to make). | |
5062 | ||
b69cd36a | 5063 | if Nam_In (Chars (N), Name_Put, Name_Put_Line) then |
ed2233dc | 5064 | Error_Msg_N -- CODEFIX |
16ca248a ES |
5065 | ("\\possible missing `WITH Ada.Text_'I'O; " & |
5066 | "USE Ada.Text_'I'O`!", N); | |
5067 | ||
5068 | -- Another special check if N is the prefix of a selected | |
5069 | -- component which is a known unit, add message complaining | |
fbe627af | 5070 | -- about missing with for this unit. |
16ca248a ES |
5071 | |
5072 | elsif Nkind (Parent (N)) = N_Selected_Component | |
5073 | and then N = Prefix (Parent (N)) | |
5074 | and then Is_Known_Unit (Parent (N)) | |
5075 | then | |
5076 | Error_Msg_Node_2 := Selector_Name (Parent (N)); | |
ed2233dc AC |
5077 | Error_Msg_N -- CODEFIX |
5078 | ("\\missing `WITH &.&;`", Prefix (Parent (N))); | |
996ae0b0 RK |
5079 | end if; |
5080 | ||
5081 | -- Now check for possible misspellings | |
5082 | ||
996ae0b0 RK |
5083 | declare |
5084 | E : Entity_Id; | |
5085 | Ematch : Entity_Id := Empty; | |
5086 | ||
5087 | Last_Name_Id : constant Name_Id := | |
5088 | Name_Id (Nat (First_Name_Id) + | |
5089 | Name_Entries_Count - 1); | |
5090 | ||
996ae0b0 | 5091 | begin |
294ccb21 RD |
5092 | for Nam in First_Name_Id .. Last_Name_Id loop |
5093 | E := Get_Name_Entity_Id (Nam); | |
996ae0b0 RK |
5094 | |
5095 | if Present (E) | |
5096 | and then (Is_Immediately_Visible (E) | |
5097 | or else | |
5098 | Is_Potentially_Use_Visible (E)) | |
5099 | then | |
294ccb21 | 5100 | if Is_Bad_Spelling_Of (Chars (N), Nam) then |
996ae0b0 RK |
5101 | Ematch := E; |
5102 | exit; | |
5103 | end if; | |
5104 | end if; | |
5105 | end loop; | |
5106 | ||
5107 | if Present (Ematch) then | |
deef4289 AC |
5108 | Error_Msg_NE -- CODEFIX |
5109 | ("\possible misspelling of&", N, Ematch); | |
996ae0b0 RK |
5110 | end if; |
5111 | end; | |
5112 | end if; | |
5113 | ||
16ca248a ES |
5114 | -- Make entry in undefined references table unless the full errors |
5115 | -- switch is set, in which case by refraining from generating the | |
5116 | -- table entry, we guarantee that we get an error message for every | |
5117 | -- undefined reference. | |
996ae0b0 RK |
5118 | |
5119 | if not All_Errors_Mode then | |
11560bcc TQ |
5120 | Urefs.Append ( |
5121 | (Node => N, | |
5122 | Err => Emsg, | |
5123 | Nvis => Nvis, | |
5124 | Loc => Sloc (N))); | |
996ae0b0 RK |
5125 | end if; |
5126 | ||
5127 | Msg := True; | |
5128 | end Undefined; | |
5129 | ||
5130 | -- Start of processing for Find_Direct_Name | |
5131 | ||
5132 | begin | |
5133 | -- If the entity pointer is already set, this is an internal node, or | |
5134 | -- a node that is analyzed more than once, after a tree modification. | |
5135 | -- In such a case there is no resolution to perform, just set the type. | |
5136 | ||
5137 | if Present (Entity (N)) then | |
5138 | if Is_Type (Entity (N)) then | |
5139 | Set_Etype (N, Entity (N)); | |
5140 | ||
5141 | else | |
5142 | declare | |
5143 | Entyp : constant Entity_Id := Etype (Entity (N)); | |
5144 | ||
5145 | begin | |
5146 | -- One special case here. If the Etype field is already set, | |
5147 | -- and references the packed array type corresponding to the | |
5148 | -- etype of the referenced entity, then leave it alone. This | |
5149 | -- happens for trees generated from Exp_Pakd, where expressions | |
5150 | -- can be deliberately "mis-typed" to the packed array type. | |
5151 | ||
5152 | if Is_Array_Type (Entyp) | |
5153 | and then Is_Packed (Entyp) | |
5154 | and then Present (Etype (N)) | |
8ca597af | 5155 | and then Etype (N) = Packed_Array_Impl_Type (Entyp) |
996ae0b0 RK |
5156 | then |
5157 | null; | |
5158 | ||
5159 | -- If not that special case, then just reset the Etype | |
5160 | ||
5161 | else | |
5162 | Set_Etype (N, Etype (Entity (N))); | |
5163 | end if; | |
5164 | end; | |
5165 | end if; | |
5166 | ||
5167 | return; | |
5168 | end if; | |
5169 | ||
5170 | -- Here if Entity pointer was not set, we need full visibility analysis | |
5171 | -- First we generate debugging output if the debug E flag is set. | |
5172 | ||
5173 | if Debug_Flag_E then | |
5174 | Write_Str ("Looking for "); | |
5175 | Write_Name (Chars (N)); | |
5176 | Write_Eol; | |
5177 | end if; | |
5178 | ||
5179 | Homonyms := Current_Entity (N); | |
5180 | Nvis_Entity := False; | |
5181 | ||
5182 | E := Homonyms; | |
5183 | while Present (E) loop | |
5184 | ||
ff81221b ES |
5185 | -- If entity is immediately visible or potentially use visible, then |
5186 | -- process the entity and we are done. | |
996ae0b0 RK |
5187 | |
5188 | if Is_Immediately_Visible (E) then | |
5189 | goto Immediately_Visible_Entity; | |
5190 | ||
5191 | elsif Is_Potentially_Use_Visible (E) then | |
5192 | goto Potentially_Use_Visible_Entity; | |
5193 | ||
5194 | -- Note if a known but invisible entity encountered | |
5195 | ||
5196 | elsif Known_But_Invisible (E) then | |
5197 | Nvis_Entity := True; | |
5198 | end if; | |
5199 | ||
5200 | -- Move to next entity in chain and continue search | |
5201 | ||
5202 | E := Homonym (E); | |
5203 | end loop; | |
5204 | ||
5205 | -- If no entries on homonym chain that were potentially visible, | |
5206 | -- and no entities reasonably considered as non-visible, then | |
5207 | -- we have a plain undefined reference, with no additional | |
a90bd866 | 5208 | -- explanation required. |
996ae0b0 RK |
5209 | |
5210 | if not Nvis_Entity then | |
5211 | Undefined (Nvis => False); | |
996ae0b0 RK |
5212 | |
5213 | -- Otherwise there is at least one entry on the homonym chain that | |
5214 | -- is reasonably considered as being known and non-visible. | |
5215 | ||
5216 | else | |
5217 | Nvis_Messages; | |
996ae0b0 RK |
5218 | end if; |
5219 | ||
18dae814 | 5220 | goto Done; |
fbf5a39b | 5221 | |
996ae0b0 RK |
5222 | -- Processing for a potentially use visible entry found. We must search |
5223 | -- the rest of the homonym chain for two reasons. First, if there is a | |
5224 | -- directly visible entry, then none of the potentially use-visible | |
5225 | -- entities are directly visible (RM 8.4(10)). Second, we need to check | |
5226 | -- for the case of multiple potentially use-visible entries hiding one | |
5227 | -- another and as a result being non-directly visible (RM 8.4(11)). | |
5228 | ||
5229 | <<Potentially_Use_Visible_Entity>> declare | |
5230 | Only_One_Visible : Boolean := True; | |
5231 | All_Overloadable : Boolean := Is_Overloadable (E); | |
5232 | ||
5233 | begin | |
5234 | E2 := Homonym (E); | |
996ae0b0 RK |
5235 | while Present (E2) loop |
5236 | if Is_Immediately_Visible (E2) then | |
5237 | ||
5238 | -- If the use-visible entity comes from the actual for a | |
5239 | -- formal package, it hides a directly visible entity from | |
5240 | -- outside the instance. | |
5241 | ||
5242 | if From_Actual_Package (E) | |
5243 | and then Scope_Depth (E2) < Scope_Depth (Inst) | |
5244 | then | |
5245 | goto Found; | |
5246 | else | |
5247 | E := E2; | |
5248 | goto Immediately_Visible_Entity; | |
5249 | end if; | |
5250 | ||
5251 | elsif Is_Potentially_Use_Visible (E2) then | |
5252 | Only_One_Visible := False; | |
5253 | All_Overloadable := All_Overloadable and Is_Overloadable (E2); | |
9bc856dd | 5254 | |
30783513 | 5255 | -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect |
f3d57416 | 5256 | -- that can occur in private_with clauses. Example: |
9bc856dd AC |
5257 | |
5258 | -- with A; | |
5259 | -- private with B; package A is | |
5260 | -- package C is function B return Integer; | |
5261 | -- use A; end A; | |
5262 | -- V1 : Integer := B; | |
5263 | -- private function B return Integer; | |
5264 | -- V2 : Integer := B; | |
5265 | -- end C; | |
5266 | ||
bc41faa2 | 5267 | -- V1 resolves to A.B, but V2 resolves to library unit B |
9bc856dd AC |
5268 | |
5269 | elsif Ekind (E2) = E_Function | |
5270 | and then Scope (E2) = Standard_Standard | |
5271 | and then Has_Private_With (E2) | |
5272 | then | |
5273 | Only_One_Visible := False; | |
5274 | All_Overloadable := False; | |
5275 | Nvis_Is_Private_Subprg := True; | |
5276 | exit; | |
996ae0b0 RK |
5277 | end if; |
5278 | ||
5279 | E2 := Homonym (E2); | |
5280 | end loop; | |
5281 | ||
5282 | -- On falling through this loop, we have checked that there are no | |
5283 | -- immediately visible entities. Only_One_Visible is set if exactly | |
5284 | -- one potentially use visible entity exists. All_Overloadable is | |
5285 | -- set if all the potentially use visible entities are overloadable. | |
5286 | -- The condition for legality is that either there is one potentially | |
5287 | -- use visible entity, or if there is more than one, then all of them | |
5288 | -- are overloadable. | |
5289 | ||
5290 | if Only_One_Visible or All_Overloadable then | |
5291 | goto Found; | |
5292 | ||
5293 | -- If there is more than one potentially use-visible entity and at | |
4ff4293f | 5294 | -- least one of them non-overloadable, we have an error (RM 8.4(11)). |
996ae0b0 RK |
5295 | -- Note that E points to the first such entity on the homonym list. |
5296 | -- Special case: if one of the entities is declared in an actual | |
5297 | -- package, it was visible in the generic, and takes precedence over | |
fbf5a39b AC |
5298 | -- other entities that are potentially use-visible. Same if it is |
5299 | -- declared in a local instantiation of the current instance. | |
996ae0b0 RK |
5300 | |
5301 | else | |
5302 | if In_Instance then | |
fbf5a39b | 5303 | |
bc41faa2 | 5304 | -- Find current instance |
fbf5a39b | 5305 | |
16ca248a | 5306 | Inst := Current_Scope; |
ac7d724d | 5307 | while Present (Inst) and then Inst /= Standard_Standard loop |
fbf5a39b AC |
5308 | if Is_Generic_Instance (Inst) then |
5309 | exit; | |
5310 | end if; | |
5311 | ||
5312 | Inst := Scope (Inst); | |
5313 | end loop; | |
5314 | ||
996ae0b0 | 5315 | E2 := E; |
996ae0b0 | 5316 | while Present (E2) loop |
fbf5a39b AC |
5317 | if From_Actual_Package (E2) |
5318 | or else | |
5319 | (Is_Generic_Instance (Scope (E2)) | |
5320 | and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst)) | |
5321 | then | |
996ae0b0 RK |
5322 | E := E2; |
5323 | goto Found; | |
5324 | end if; | |
5325 | ||
5326 | E2 := Homonym (E2); | |
5327 | end loop; | |
5328 | ||
5329 | Nvis_Messages; | |
18dae814 | 5330 | goto Done; |
996ae0b0 | 5331 | |
5eb10f25 ES |
5332 | elsif |
5333 | Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit)) | |
5334 | then | |
4de287c4 ES |
5335 | -- A use-clause in the body of a system file creates conflict |
5336 | -- with some entity in a user scope, while rtsfind is active. | |
5337 | -- Keep only the entity coming from another predefined unit. | |
5eb10f25 ES |
5338 | |
5339 | E2 := E; | |
5340 | while Present (E2) loop | |
5341 | if Is_Predefined_File_Name | |
5342 | (Unit_File_Name (Get_Source_Unit (Sloc (E2)))) | |
5343 | then | |
5344 | E := E2; | |
5345 | goto Found; | |
5346 | end if; | |
5347 | ||
5348 | E2 := Homonym (E2); | |
5349 | end loop; | |
5350 | ||
4de287c4 | 5351 | -- Entity must exist because predefined unit is correct |
5eb10f25 ES |
5352 | |
5353 | raise Program_Error; | |
5354 | ||
996ae0b0 RK |
5355 | else |
5356 | Nvis_Messages; | |
18dae814 | 5357 | goto Done; |
996ae0b0 RK |
5358 | end if; |
5359 | end if; | |
5360 | end; | |
5361 | ||
5362 | -- Come here with E set to the first immediately visible entity on | |
5363 | -- the homonym chain. This is the one we want unless there is another | |
ff81221b ES |
5364 | -- immediately visible entity further on in the chain for an inner |
5365 | -- scope (RM 8.3(8)). | |
996ae0b0 RK |
5366 | |
5367 | <<Immediately_Visible_Entity>> declare | |
5368 | Level : Int; | |
5369 | Scop : Entity_Id; | |
5370 | ||
5371 | begin | |
ff81221b | 5372 | -- Find scope level of initial entity. When compiling through |
996ae0b0 RK |
5373 | -- Rtsfind, the previous context is not completely invisible, and |
5374 | -- an outer entity may appear on the chain, whose scope is below | |
5375 | -- the entry for Standard that delimits the current scope stack. | |
5376 | -- Indicate that the level for this spurious entry is outside of | |
5377 | -- the current scope stack. | |
5378 | ||
5379 | Level := Scope_Stack.Last; | |
5380 | loop | |
5381 | Scop := Scope_Stack.Table (Level).Entity; | |
5382 | exit when Scop = Scope (E); | |
5383 | Level := Level - 1; | |
5384 | exit when Scop = Standard_Standard; | |
5385 | end loop; | |
5386 | ||
5387 | -- Now search remainder of homonym chain for more inner entry | |
5388 | -- If the entity is Standard itself, it has no scope, and we | |
5389 | -- compare it with the stack entry directly. | |
5390 | ||
5391 | E2 := Homonym (E); | |
5392 | while Present (E2) loop | |
5393 | if Is_Immediately_Visible (E2) then | |
4de287c4 ES |
5394 | |
5395 | -- If a generic package contains a local declaration that | |
5396 | -- has the same name as the generic, there may be a visibility | |
5397 | -- conflict in an instance, where the local declaration must | |
5398 | -- also hide the name of the corresponding package renaming. | |
5399 | -- We check explicitly for a package declared by a renaming, | |
5400 | -- whose renamed entity is an instance that is on the scope | |
5401 | -- stack, and that contains a homonym in the same scope. Once | |
5402 | -- we have found it, we know that the package renaming is not | |
5403 | -- immediately visible, and that the identifier denotes the | |
5404 | -- other entity (and its homonyms if overloaded). | |
5405 | ||
5406 | if Scope (E) = Scope (E2) | |
5407 | and then Ekind (E) = E_Package | |
5408 | and then Present (Renamed_Object (E)) | |
5409 | and then Is_Generic_Instance (Renamed_Object (E)) | |
5410 | and then In_Open_Scopes (Renamed_Object (E)) | |
5411 | and then Comes_From_Source (N) | |
5412 | then | |
5413 | Set_Is_Immediately_Visible (E, False); | |
5414 | E := E2; | |
5415 | ||
5416 | else | |
5417 | for J in Level + 1 .. Scope_Stack.Last loop | |
5418 | if Scope_Stack.Table (J).Entity = Scope (E2) | |
5419 | or else Scope_Stack.Table (J).Entity = E2 | |
5420 | then | |
5421 | Level := J; | |
5422 | E := E2; | |
5423 | exit; | |
5424 | end if; | |
5425 | end loop; | |
5426 | end if; | |
996ae0b0 RK |
5427 | end if; |
5428 | ||
5429 | E2 := Homonym (E2); | |
5430 | end loop; | |
5431 | ||
5432 | -- At the end of that loop, E is the innermost immediately | |
5433 | -- visible entity, so we are all set. | |
5434 | end; | |
5435 | ||
5436 | -- Come here with entity found, and stored in E | |
5437 | ||
5438 | <<Found>> begin | |
5439 | ||
30196a76 RD |
5440 | -- Check violation of No_Wide_Characters restriction |
5441 | ||
5442 | Check_Wide_Character_Restriction (E, N); | |
5443 | ||
294ccb21 RD |
5444 | -- When distribution features are available (Get_PCS_Name /= |
5445 | -- Name_No_DSA), a remote access-to-subprogram type is converted | |
5446 | -- into a record type holding whatever information is needed to | |
f3d57416 | 5447 | -- perform a remote call on an RCI subprogram. In that case we |
294ccb21 RD |
5448 | -- rewrite any occurrence of the RAS type into the equivalent record |
5449 | -- type here. 'Access attribute references and RAS dereferences are | |
5450 | -- then implemented using specific TSSs. However when distribution is | |
5451 | -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the | |
5452 | -- generation of these TSSs, and we must keep the RAS type in its | |
5453 | -- original access-to-subprogram form (since all calls through a | |
5454 | -- value of such type will be local anyway in the absence of a PCS). | |
5455 | ||
996ae0b0 RK |
5456 | if Comes_From_Source (N) |
5457 | and then Is_Remote_Access_To_Subprogram_Type (E) | |
d1d81616 | 5458 | and then Ekind (E) = E_Access_Subprogram_Type |
996ae0b0 | 5459 | and then Expander_Active |
a77842bd | 5460 | and then Get_PCS_Name /= Name_No_DSA |
996ae0b0 | 5461 | then |
18dae814 RD |
5462 | Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N))); |
5463 | goto Done; | |
996ae0b0 RK |
5464 | end if; |
5465 | ||
6989bc1f | 5466 | -- Set the entity. Note that the reason we call Set_Entity for the |
e7ba564f | 5467 | -- overloadable case, as opposed to Set_Entity_With_Checks is |
6989bc1f AC |
5468 | -- that in the overloaded case, the initial call can set the wrong |
5469 | -- homonym. The call that sets the right homonym is in Sem_Res and | |
e7ba564f | 5470 | -- that call does use Set_Entity_With_Checks, so we don't miss |
6989bc1f AC |
5471 | -- a style check. |
5472 | ||
5473 | if Is_Overloadable (E) then | |
5474 | Set_Entity (N, E); | |
5475 | else | |
e7ba564f | 5476 | Set_Entity_With_Checks (N, E); |
6989bc1f | 5477 | end if; |
996ae0b0 RK |
5478 | |
5479 | if Is_Type (E) then | |
5480 | Set_Etype (N, E); | |
5481 | else | |
5482 | Set_Etype (N, Get_Full_View (Etype (E))); | |
5483 | end if; | |
5484 | ||
5485 | if Debug_Flag_E then | |
5486 | Write_Str (" found "); | |
5487 | Write_Entity_Info (E, " "); | |
5488 | end if; | |
5489 | ||
5490 | -- If the Ekind of the entity is Void, it means that all homonyms | |
5491 | -- are hidden from all visibility (RM 8.3(5,14-20)). However, this | |
5492 | -- test is skipped if the current scope is a record and the name is | |
5493 | -- a pragma argument expression (case of Atomic and Volatile pragmas | |
5494 | -- and possibly other similar pragmas added later, which are allowed | |
5495 | -- to reference components in the current record). | |
5496 | ||
5497 | if Ekind (E) = E_Void | |
5498 | and then | |
5499 | (not Is_Record_Type (Current_Scope) | |
5500 | or else Nkind (Parent (N)) /= N_Pragma_Argument_Association) | |
5501 | then | |
5502 | Premature_Usage (N); | |
5503 | ||
16ca248a ES |
5504 | -- If the entity is overloadable, collect all interpretations of the |
5505 | -- name for subsequent overload resolution. We optimize a bit here to | |
5506 | -- do this only if we have an overloadable entity that is not on its | |
5507 | -- own on the homonym chain. | |
996ae0b0 RK |
5508 | |
5509 | elsif Is_Overloadable (E) | |
5510 | and then (Present (Homonym (E)) or else Current_Entity (N) /= E) | |
5511 | then | |
5512 | Collect_Interps (N); | |
5513 | ||
bc41faa2 | 5514 | -- If no homonyms were visible, the entity is unambiguous |
996ae0b0 RK |
5515 | |
5516 | if not Is_Overloaded (N) then | |
67ce0d7e RD |
5517 | if not Is_Actual_Parameter then |
5518 | Generate_Reference (E, N); | |
5519 | end if; | |
996ae0b0 RK |
5520 | end if; |
5521 | ||
5522 | -- Case of non-overloadable entity, set the entity providing that | |
5523 | -- we do not have the case of a discriminant reference within a | |
5524 | -- default expression. Such references are replaced with the | |
5525 | -- corresponding discriminal, which is the formal corresponding to | |
5526 | -- to the discriminant in the initialization procedure. | |
5527 | ||
996ae0b0 | 5528 | else |
294ccb21 RD |
5529 | -- Entity is unambiguous, indicate that it is referenced here |
5530 | ||
5531 | -- For a renaming of an object, always generate simple reference, | |
5114f3ff AC |
5532 | -- we don't try to keep track of assignments in this case, except |
5533 | -- in SPARK mode where renamings are traversed for generating | |
5534 | -- local effects of subprograms. | |
294ccb21 | 5535 | |
5114f3ff AC |
5536 | if Is_Object (E) |
5537 | and then Present (Renamed_Object (E)) | |
f5da7a97 | 5538 | and then not GNATprove_Mode |
5114f3ff | 5539 | then |
294ccb21 | 5540 | Generate_Reference (E, N); |
996ae0b0 | 5541 | |
21d27997 RD |
5542 | -- If the renamed entity is a private protected component, |
5543 | -- reference the original component as well. This needs to be | |
5544 | -- done because the private renamings are installed before any | |
f3d0f304 | 5545 | -- analysis has occurred. Reference to a private component will |
21d27997 RD |
5546 | -- resolve to the renaming and the original component will be |
5547 | -- left unreferenced, hence the following. | |
5548 | ||
5549 | if Is_Prival (E) then | |
5550 | Generate_Reference (Prival_Link (E), N); | |
5551 | end if; | |
5552 | ||
294ccb21 RD |
5553 | -- One odd case is that we do not want to set the Referenced flag |
5554 | -- if the entity is a label, and the identifier is the label in | |
5555 | -- the source, since this is not a reference from the point of | |
5556 | -- view of the user. | |
5557 | ||
5558 | elsif Nkind (Parent (N)) = N_Label then | |
996ae0b0 RK |
5559 | declare |
5560 | R : constant Boolean := Referenced (E); | |
294ccb21 | 5561 | |
996ae0b0 | 5562 | begin |
294ccb21 RD |
5563 | -- Generate reference unless this is an actual parameter |
5564 | -- (see comment below) | |
5565 | ||
5566 | if Is_Actual_Parameter then | |
67ce0d7e RD |
5567 | Generate_Reference (E, N); |
5568 | Set_Referenced (E, R); | |
5569 | end if; | |
996ae0b0 RK |
5570 | end; |
5571 | ||
11560bcc TQ |
5572 | -- Normal case, not a label: generate reference |
5573 | ||
a54ffd6c AC |
5574 | else |
5575 | if not Is_Actual_Parameter then | |
61c161b2 | 5576 | |
a54ffd6c | 5577 | -- Package or generic package is always a simple reference |
61c161b2 | 5578 | |
a54ffd6c AC |
5579 | if Ekind_In (E, E_Package, E_Generic_Package) then |
5580 | Generate_Reference (E, N, 'r'); | |
5581 | ||
5582 | -- Else see if we have a left hand side | |
fbf5a39b | 5583 | |
84df40f7 | 5584 | else |
a54ffd6c AC |
5585 | case Is_LHS (N) is |
5586 | when Yes => | |
5587 | Generate_Reference (E, N, 'm'); | |
5588 | ||
5589 | when No => | |
5590 | Generate_Reference (E, N, 'r'); | |
5591 | ||
5592 | -- If we don't know now, generate reference later | |
5593 | ||
5594 | when Unknown => | |
5595 | Deferred_References.Append ((E, N)); | |
5596 | end case; | |
84df40f7 | 5597 | end if; |
67ce0d7e | 5598 | end if; |
996ae0b0 RK |
5599 | end if; |
5600 | ||
4c484f40 | 5601 | Set_Entity_Or_Discriminal (N, E); |
d50f4827 | 5602 | |
27dd0dde | 5603 | -- The name may designate a generalized reference, in which case |
f6f40114 AC |
5604 | -- the dereference interpretation will be included. Context is |
5605 | -- one in which a name is legal. | |
27dd0dde | 5606 | |
d50f4827 | 5607 | if Ada_Version >= Ada_2012 |
444acbdd AC |
5608 | and then |
5609 | (Nkind (Parent (N)) in N_Subexpr | |
cdabbb52 HK |
5610 | or else Nkind_In (Parent (N), N_Assignment_Statement, |
5611 | N_Object_Declaration, | |
5612 | N_Parameter_Association)) | |
d50f4827 AC |
5613 | then |
5614 | Check_Implicit_Dereference (N, Etype (E)); | |
5615 | end if; | |
996ae0b0 RK |
5616 | end if; |
5617 | end; | |
18dae814 RD |
5618 | |
5619 | -- Come here with entity set | |
5620 | ||
5621 | <<Done>> | |
5622 | Check_Restriction_No_Use_Of_Entity (N); | |
996ae0b0 RK |
5623 | end Find_Direct_Name; |
5624 | ||
5625 | ------------------------ | |
5626 | -- Find_Expanded_Name -- | |
5627 | ------------------------ | |
5628 | ||
5629 | -- This routine searches the homonym chain of the entity until it finds | |
5630 | -- an entity declared in the scope denoted by the prefix. If the entity | |
5631 | -- is private, it may nevertheless be immediately visible, if we are in | |
5632 | -- the scope of its declaration. | |
5633 | ||
5634 | procedure Find_Expanded_Name (N : Node_Id) is | |
95fef24f AC |
5635 | function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean; |
5636 | -- Determine whether expanded name Nod appears within a pragma which is | |
5637 | -- a suitable context for an abstract view of a state or variable. The | |
5638 | -- following pragmas fall in this category: | |
5639 | -- Depends | |
5640 | -- Global | |
5641 | -- Initializes | |
5642 | -- Refined_Depends | |
5643 | -- Refined_Global | |
5644 | -- | |
5645 | -- In addition, pragma Abstract_State is also considered suitable even | |
5646 | -- though it is an illegal context for an abstract view as this allows | |
5647 | -- for proper resolution of abstract views of variables. This illegal | |
5648 | -- context is later flagged in the analysis of indicator Part_Of. | |
dc726757 | 5649 | |
95fef24f AC |
5650 | ----------------------------- |
5651 | -- In_Abstract_View_Pragma -- | |
5652 | ----------------------------- | |
dc726757 | 5653 | |
95fef24f | 5654 | function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is |
dc726757 HK |
5655 | Par : Node_Id; |
5656 | ||
5657 | begin | |
5658 | -- Climb the parent chain looking for a pragma | |
5659 | ||
95fef24f | 5660 | Par := Nod; |
dc726757 | 5661 | while Present (Par) loop |
95fef24f | 5662 | if Nkind (Par) = N_Pragma then |
6e759c2a BD |
5663 | if Nam_In (Pragma_Name_Unmapped (Par), |
5664 | Name_Abstract_State, | |
5665 | Name_Depends, | |
5666 | Name_Global, | |
5667 | Name_Initializes, | |
5668 | Name_Refined_Depends, | |
5669 | Name_Refined_Global) | |
95fef24f AC |
5670 | then |
5671 | return True; | |
5672 | ||
5673 | -- Otherwise the pragma is not a legal context for an abstract | |
5674 | -- view. | |
5675 | ||
5676 | else | |
5677 | exit; | |
5678 | end if; | |
dc726757 HK |
5679 | |
5680 | -- Prevent the search from going too far | |
5681 | ||
5682 | elsif Is_Body_Or_Package_Declaration (Par) then | |
95fef24f | 5683 | exit; |
dc726757 HK |
5684 | end if; |
5685 | ||
5686 | Par := Parent (Par); | |
5687 | end loop; | |
5688 | ||
5689 | return False; | |
95fef24f | 5690 | end In_Abstract_View_Pragma; |
dc726757 HK |
5691 | |
5692 | -- Local variables | |
5693 | ||
07fc65c4 GB |
5694 | Selector : constant Node_Id := Selector_Name (N); |
5695 | Candidate : Entity_Id := Empty; | |
996ae0b0 | 5696 | P_Name : Entity_Id; |
996ae0b0 RK |
5697 | Id : Entity_Id; |
5698 | ||
dc726757 HK |
5699 | -- Start of processing for Find_Expanded_Name |
5700 | ||
996ae0b0 RK |
5701 | begin |
5702 | P_Name := Entity (Prefix (N)); | |
996ae0b0 | 5703 | |
ff81221b ES |
5704 | -- If the prefix is a renamed package, look for the entity in the |
5705 | -- original package. | |
996ae0b0 RK |
5706 | |
5707 | if Ekind (P_Name) = E_Package | |
5708 | and then Present (Renamed_Object (P_Name)) | |
5709 | then | |
5710 | P_Name := Renamed_Object (P_Name); | |
5711 | ||
5712 | -- Rewrite node with entity field pointing to renamed object | |
5713 | ||
5714 | Rewrite (Prefix (N), New_Copy (Prefix (N))); | |
5715 | Set_Entity (Prefix (N), P_Name); | |
5716 | ||
5717 | -- If the prefix is an object of a concurrent type, look for | |
5718 | -- the entity in the associated task or protected type. | |
5719 | ||
5720 | elsif Is_Concurrent_Type (Etype (P_Name)) then | |
5721 | P_Name := Etype (P_Name); | |
5722 | end if; | |
5723 | ||
5724 | Id := Current_Entity (Selector); | |
5725 | ||
923fa078 RD |
5726 | declare |
5727 | Is_New_Candidate : Boolean; | |
996ae0b0 | 5728 | |
923fa078 RD |
5729 | begin |
5730 | while Present (Id) loop | |
5731 | if Scope (Id) = P_Name then | |
5732 | Candidate := Id; | |
5733 | Is_New_Candidate := True; | |
5734 | ||
dc726757 | 5735 | -- Handle abstract views of states and variables. These are |
95fef24f AC |
5736 | -- acceptable candidates only when the reference to the view |
5737 | -- appears in certain pragmas. | |
dc726757 HK |
5738 | |
5739 | if Ekind (Id) = E_Abstract_State | |
5740 | and then From_Limited_With (Id) | |
5741 | and then Present (Non_Limited_View (Id)) | |
5742 | then | |
95fef24f | 5743 | if In_Abstract_View_Pragma (N) then |
dc726757 HK |
5744 | Candidate := Non_Limited_View (Id); |
5745 | Is_New_Candidate := True; | |
5746 | ||
95fef24f AC |
5747 | -- Hide the candidate because it is not used in a proper |
5748 | -- context. | |
dc726757 HK |
5749 | |
5750 | else | |
5751 | Candidate := Empty; | |
5752 | Is_New_Candidate := False; | |
5753 | end if; | |
5754 | end if; | |
5755 | ||
47346923 AC |
5756 | -- Ada 2005 (AI-217): Handle shadow entities associated with |
5757 | -- types declared in limited-withed nested packages. We don't need | |
5758 | -- to handle E_Incomplete_Subtype entities because the entities | |
5759 | -- in the limited view are always E_Incomplete_Type and | |
5760 | -- E_Class_Wide_Type entities (see Build_Limited_Views). | |
5761 | ||
5762 | -- Regarding the expression used to evaluate the scope, it | |
5763 | -- is important to note that the limited view also has shadow | |
5764 | -- entities associated nested packages. For this reason the | |
5765 | -- correct scope of the entity is the scope of the real entity. | |
11560bcc TQ |
5766 | -- The non-limited view may itself be incomplete, in which case |
5767 | -- get the full view if available. | |
923fa078 | 5768 | |
47346923 | 5769 | elsif Ekind_In (Id, E_Incomplete_Type, E_Class_Wide_Type) |
dc726757 | 5770 | and then From_Limited_With (Id) |
923fa078 RD |
5771 | and then Present (Non_Limited_View (Id)) |
5772 | and then Scope (Non_Limited_View (Id)) = P_Name | |
5773 | then | |
11560bcc | 5774 | Candidate := Get_Full_View (Non_Limited_View (Id)); |
923fa078 | 5775 | Is_New_Candidate := True; |
996ae0b0 RK |
5776 | |
5777 | else | |
923fa078 | 5778 | Is_New_Candidate := False; |
996ae0b0 | 5779 | end if; |
996ae0b0 | 5780 | |
923fa078 | 5781 | if Is_New_Candidate then |
ddbc55d8 AC |
5782 | |
5783 | -- If entity is a child unit, either it is a visible child of | |
5784 | -- the prefix, or we are in the body of a generic prefix, as | |
5785 | -- will happen when a child unit is instantiated in the body | |
5786 | -- of a generic parent. This is because the instance body does | |
5787 | -- not restore the full compilation context, given that all | |
5788 | -- non-local references have been captured. | |
5789 | ||
8ca1ee5d | 5790 | if Is_Child_Unit (Id) or else P_Name = Standard_Standard then |
ddbc55d8 AC |
5791 | exit when Is_Visible_Lib_Unit (Id) |
5792 | or else (Is_Child_Unit (Id) | |
5793 | and then In_Open_Scopes (Scope (Id)) | |
5794 | and then In_Instance_Body); | |
923fa078 | 5795 | else |
8398e82e | 5796 | exit when not Is_Hidden (Id); |
923fa078 | 5797 | end if; |
8398e82e AC |
5798 | |
5799 | exit when Is_Immediately_Visible (Id); | |
923fa078 RD |
5800 | end if; |
5801 | ||
5802 | Id := Homonym (Id); | |
5803 | end loop; | |
5804 | end; | |
996ae0b0 RK |
5805 | |
5806 | if No (Id) | |
ac7d724d | 5807 | and then Ekind_In (P_Name, E_Procedure, E_Function) |
996ae0b0 RK |
5808 | and then Is_Generic_Instance (P_Name) |
5809 | then | |
5810 | -- Expanded name denotes entity in (instance of) generic subprogram. | |
5811 | -- The entity may be in the subprogram instance, or may denote one of | |
5812 | -- the formals, which is declared in the enclosing wrapper package. | |
5813 | ||
5814 | P_Name := Scope (P_Name); | |
996ae0b0 | 5815 | |
bc41faa2 | 5816 | Id := Current_Entity (Selector); |
996ae0b0 | 5817 | while Present (Id) loop |
bc41faa2 | 5818 | exit when Scope (Id) = P_Name; |
996ae0b0 RK |
5819 | Id := Homonym (Id); |
5820 | end loop; | |
5821 | end if; | |
5822 | ||
bc41faa2 | 5823 | if No (Id) or else Chars (Id) /= Chars (Selector) then |
996ae0b0 RK |
5824 | Set_Etype (N, Any_Type); |
5825 | ||
ff81221b ES |
5826 | -- If we are looking for an entity defined in System, try to find it |
5827 | -- in the child package that may have been provided as an extension | |
5828 | -- to System. The Extend_System pragma will have supplied the name of | |
5829 | -- the extension, which may have to be loaded. | |
996ae0b0 RK |
5830 | |
5831 | if Chars (P_Name) = Name_System | |
5832 | and then Scope (P_Name) = Standard_Standard | |
fbf5a39b | 5833 | and then Present (System_Extend_Unit) |
996ae0b0 RK |
5834 | and then Present_System_Aux (N) |
5835 | then | |
5836 | Set_Entity (Prefix (N), System_Aux_Id); | |
5837 | Find_Expanded_Name (N); | |
5838 | return; | |
5839 | ||
95fef24f AC |
5840 | -- There is an implicit instance of the predefined operator in |
5841 | -- the given scope. The operator entity is defined in Standard. | |
5842 | -- Has_Implicit_Operator makes the node into an Expanded_Name. | |
5843 | ||
fbf5a39b AC |
5844 | elsif Nkind (Selector) = N_Operator_Symbol |
5845 | and then Has_Implicit_Operator (N) | |
996ae0b0 | 5846 | then |
996ae0b0 RK |
5847 | return; |
5848 | ||
95fef24f AC |
5849 | -- If there is no literal defined in the scope denoted by the |
5850 | -- prefix, the literal may belong to (a type derived from) | |
5851 | -- Standard_Character, for which we have no explicit literals. | |
5852 | ||
996ae0b0 RK |
5853 | elsif Nkind (Selector) = N_Character_Literal |
5854 | and then Has_Implicit_Character_Literal (N) | |
5855 | then | |
996ae0b0 RK |
5856 | return; |
5857 | ||
5858 | else | |
ff81221b ES |
5859 | -- If the prefix is a single concurrent object, use its name in |
5860 | -- the error message, rather than that of the anonymous type. | |
996ae0b0 RK |
5861 | |
5862 | if Is_Concurrent_Type (P_Name) | |
5863 | and then Is_Internal_Name (Chars (P_Name)) | |
5864 | then | |
5865 | Error_Msg_Node_2 := Entity (Prefix (N)); | |
5866 | else | |
5867 | Error_Msg_Node_2 := P_Name; | |
5868 | end if; | |
5869 | ||
5870 | if P_Name = System_Aux_Id then | |
5871 | P_Name := Scope (P_Name); | |
5872 | Set_Entity (Prefix (N), P_Name); | |
5873 | end if; | |
5874 | ||
5875 | if Present (Candidate) then | |
5876 | ||
16ca248a ES |
5877 | -- If we know that the unit is a child unit we can give a more |
5878 | -- accurate error message. | |
5879 | ||
996ae0b0 | 5880 | if Is_Child_Unit (Candidate) then |
edd63e9b | 5881 | |
16ca248a ES |
5882 | -- If the candidate is a private child unit and we are in |
5883 | -- the visible part of a public unit, specialize the error | |
5884 | -- message. There might be a private with_clause for it, | |
5885 | -- but it is not currently active. | |
edd63e9b ES |
5886 | |
5887 | if Is_Private_Descendant (Candidate) | |
5888 | and then Ekind (Current_Scope) = E_Package | |
5889 | and then not In_Private_Part (Current_Scope) | |
5890 | and then not Is_Private_Descendant (Current_Scope) | |
5891 | then | |
95fef24f AC |
5892 | Error_Msg_N |
5893 | ("private child unit& is not visible here", Selector); | |
16ca248a ES |
5894 | |
5895 | -- Normal case where we have a missing with for a child unit | |
5896 | ||
edd63e9b | 5897 | else |
16ca248a | 5898 | Error_Msg_Qual_Level := 99; |
ed2233dc AC |
5899 | Error_Msg_NE -- CODEFIX |
5900 | ("missing `WITH &;`", Selector, Candidate); | |
16ca248a | 5901 | Error_Msg_Qual_Level := 0; |
edd63e9b | 5902 | end if; |
16ca248a ES |
5903 | |
5904 | -- Here we don't know that this is a child unit | |
5905 | ||
996ae0b0 RK |
5906 | else |
5907 | Error_Msg_NE ("& is not a visible entity of&", N, Selector); | |
5908 | end if; | |
5909 | ||
5910 | else | |
5911 | -- Within the instantiation of a child unit, the prefix may | |
16ca248a | 5912 | -- denote the parent instance, but the selector has the name |
58747e48 AC |
5913 | -- of the original child. That is to say, when A.B appears |
5914 | -- within an instantiation of generic child unit B, the scope | |
5915 | -- stack includes an instance of A (P_Name) and an instance | |
5916 | -- of B under some other name. We scan the scope to find this | |
5917 | -- child instance, which is the desired entity. | |
5918 | -- Note that the parent may itself be a child instance, if | |
5919 | -- the reference is of the form A.B.C, in which case A.B has | |
5920 | -- already been rewritten with the proper entity. | |
5921 | ||
5922 | if In_Open_Scopes (P_Name) | |
996ae0b0 RK |
5923 | and then Is_Generic_Instance (P_Name) |
5924 | then | |
5925 | declare | |
58747e48 AC |
5926 | Gen_Par : constant Entity_Id := |
5927 | Generic_Parent (Specification | |
5928 | (Unit_Declaration_Node (P_Name))); | |
996ae0b0 RK |
5929 | S : Entity_Id := Current_Scope; |
5930 | P : Entity_Id; | |
5931 | ||
5932 | begin | |
5933 | for J in reverse 0 .. Scope_Stack.Last loop | |
5934 | S := Scope_Stack.Table (J).Entity; | |
5935 | ||
5936 | exit when S = Standard_Standard; | |
5937 | ||
bce79204 AC |
5938 | if Ekind_In (S, E_Function, |
5939 | E_Package, | |
5940 | E_Procedure) | |
996ae0b0 | 5941 | then |
95fef24f AC |
5942 | P := |
5943 | Generic_Parent (Specification | |
5944 | (Unit_Declaration_Node (S))); | |
996ae0b0 | 5945 | |
58747e48 AC |
5946 | -- Check that P is a generic child of the generic |
5947 | -- parent of the prefix. | |
5948 | ||
996ae0b0 | 5949 | if Present (P) |
996ae0b0 | 5950 | and then Chars (P) = Chars (Selector) |
58747e48 | 5951 | and then Scope (P) = Gen_Par |
996ae0b0 RK |
5952 | then |
5953 | Id := S; | |
a77842bd | 5954 | goto Found; |
996ae0b0 RK |
5955 | end if; |
5956 | end if; | |
5957 | ||
5958 | end loop; | |
5959 | end; | |
5960 | end if; | |
5961 | ||
16ca248a ES |
5962 | -- If this is a selection from Ada, System or Interfaces, then |
5963 | -- we assume a missing with for the corresponding package. | |
5964 | ||
5965 | if Is_Known_Unit (N) then | |
fbe627af RD |
5966 | if not Error_Posted (N) then |
5967 | Error_Msg_Node_2 := Selector; | |
ed2233dc AC |
5968 | Error_Msg_N -- CODEFIX |
5969 | ("missing `WITH &.&;`", Prefix (N)); | |
fbe627af | 5970 | end if; |
996ae0b0 | 5971 | |
16ca248a ES |
5972 | -- If this is a selection from a dummy package, then suppress |
5973 | -- the error message, of course the entity is missing if the | |
a90bd866 | 5974 | -- package is missing. |
996ae0b0 RK |
5975 | |
5976 | elsif Sloc (Error_Msg_Node_2) = No_Location then | |
5977 | null; | |
5978 | ||
5979 | -- Here we have the case of an undefined component | |
5980 | ||
5981 | else | |
d6a24cdb AC |
5982 | -- The prefix may hide a homonym in the context that |
5983 | -- declares the desired entity. This error can use a | |
5984 | -- specialized message. | |
5985 | ||
8398e82e | 5986 | if In_Open_Scopes (P_Name) then |
d6a24cdb AC |
5987 | declare |
5988 | H : constant Entity_Id := Homonym (P_Name); | |
0d354370 | 5989 | |
d6a24cdb | 5990 | begin |
8398e82e AC |
5991 | if Present (H) |
5992 | and then Is_Compilation_Unit (H) | |
5993 | and then | |
5994 | (Is_Immediately_Visible (H) | |
5995 | or else Is_Visible_Lib_Unit (H)) | |
5996 | then | |
5997 | Id := First_Entity (H); | |
5998 | while Present (Id) loop | |
5999 | if Chars (Id) = Chars (Selector) then | |
6000 | Error_Msg_Qual_Level := 99; | |
6001 | Error_Msg_Name_1 := Chars (Selector); | |
6002 | Error_Msg_NE | |
6003 | ("% not declared in&", N, P_Name); | |
6004 | Error_Msg_NE | |
6005 | ("\use fully qualified name starting with " | |
6006 | & "Standard to make& visible", N, H); | |
6007 | Error_Msg_Qual_Level := 0; | |
6008 | goto Done; | |
6009 | end if; | |
d6a24cdb | 6010 | |
8398e82e AC |
6011 | Next_Entity (Id); |
6012 | end loop; | |
6013 | end if; | |
689cb4ac | 6014 | |
690943fc | 6015 | -- If not found, standard error message |
689cb4ac AC |
6016 | |
6017 | Error_Msg_NE ("& not declared in&", N, Selector); | |
6018 | ||
6019 | <<Done>> null; | |
d6a24cdb AC |
6020 | end; |
6021 | ||
6022 | else | |
6023 | Error_Msg_NE ("& not declared in&", N, Selector); | |
6024 | end if; | |
996ae0b0 | 6025 | |
bc41faa2 | 6026 | -- Check for misspelling of some entity in prefix |
996ae0b0 RK |
6027 | |
6028 | Id := First_Entity (P_Name); | |
294ccb21 RD |
6029 | while Present (Id) loop |
6030 | if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector)) | |
6031 | and then not Is_Internal_Name (Chars (Id)) | |
6032 | then | |
deef4289 | 6033 | Error_Msg_NE -- CODEFIX |
294ccb21 RD |
6034 | ("possible misspelling of&", Selector, Id); |
6035 | exit; | |
6036 | end if; | |
996ae0b0 | 6037 | |
294ccb21 RD |
6038 | Next_Entity (Id); |
6039 | end loop; | |
996ae0b0 RK |
6040 | |
6041 | -- Specialize the message if this may be an instantiation | |
6042 | -- of a child unit that was not mentioned in the context. | |
6043 | ||
6044 | if Nkind (Parent (N)) = N_Package_Instantiation | |
6045 | and then Is_Generic_Instance (Entity (Prefix (N))) | |
6046 | and then Is_Compilation_Unit | |
294ccb21 | 6047 | (Generic_Parent (Parent (Entity (Prefix (N))))) |
996ae0b0 | 6048 | then |
16ca248a | 6049 | Error_Msg_Node_2 := Selector; |
ed2233dc AC |
6050 | Error_Msg_N -- CODEFIX |
6051 | ("\missing `WITH &.&;`", Prefix (N)); | |
996ae0b0 RK |
6052 | end if; |
6053 | end if; | |
6054 | end if; | |
6055 | ||
6056 | Id := Any_Id; | |
6057 | end if; | |
6058 | end if; | |
6059 | ||
a77842bd | 6060 | <<Found>> |
996ae0b0 RK |
6061 | if Comes_From_Source (N) |
6062 | and then Is_Remote_Access_To_Subprogram_Type (Id) | |
d1d81616 | 6063 | and then Ekind (Id) = E_Access_Subprogram_Type |
a77842bd | 6064 | and then Present (Equivalent_Type (Id)) |
996ae0b0 | 6065 | then |
16ca248a ES |
6066 | -- If we are not actually generating distribution code (i.e. the |
6067 | -- current PCS is the dummy non-distributed version), then the | |
6068 | -- Equivalent_Type will be missing, and Id should be treated as | |
6069 | -- a regular access-to-subprogram type. | |
a77842bd | 6070 | |
996ae0b0 RK |
6071 | Id := Equivalent_Type (Id); |
6072 | Set_Chars (Selector, Chars (Id)); | |
6073 | end if; | |
6074 | ||
0ab80019 | 6075 | -- Ada 2005 (AI-50217): Check usage of entities in limited withed units |
19f0526a | 6076 | |
7b56a91b AC |
6077 | if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then |
6078 | if From_Limited_With (Id) | |
0fb2ea01 AC |
6079 | or else Is_Type (Id) |
6080 | or else Ekind (Id) = E_Package | |
996ae0b0 RK |
6081 | then |
6082 | null; | |
6083 | else | |
6084 | Error_Msg_N | |
497a660d AC |
6085 | ("limited withed package can only be used to access incomplete " |
6086 | & "types", N); | |
996ae0b0 RK |
6087 | end if; |
6088 | end if; | |
6089 | ||
6090 | if Is_Task_Type (P_Name) | |
6091 | and then ((Ekind (Id) = E_Entry | |
ac7d724d | 6092 | and then Nkind (Parent (N)) /= N_Attribute_Reference) |
294ccb21 | 6093 | or else |
ac7d724d ES |
6094 | (Ekind (Id) = E_Entry_Family |
6095 | and then | |
6096 | Nkind (Parent (Parent (N))) /= N_Attribute_Reference)) | |
996ae0b0 | 6097 | then |
a53c5613 AC |
6098 | -- If both the task type and the entry are in scope, this may still |
6099 | -- be the expanded name of an entry formal. | |
996ae0b0 | 6100 | |
a53c5613 AC |
6101 | if In_Open_Scopes (Id) |
6102 | and then Nkind (Parent (N)) = N_Selected_Component | |
6103 | then | |
6104 | null; | |
6105 | ||
6106 | else | |
6107 | -- It is an entry call after all, either to the current task | |
6108 | -- (which will deadlock) or to an enclosing task. | |
6109 | ||
6110 | Analyze_Selected_Component (N); | |
6111 | return; | |
6112 | end if; | |
996ae0b0 RK |
6113 | end if; |
6114 | ||
6115 | Change_Selected_Component_To_Expanded_Name (N); | |
07fc65c4 | 6116 | |
a54ffd6c AC |
6117 | -- Set appropriate type |
6118 | ||
6119 | if Is_Type (Id) then | |
6120 | Set_Etype (N, Id); | |
6121 | else | |
6122 | Set_Etype (N, Get_Full_View (Etype (Id))); | |
6123 | end if; | |
6124 | ||
07fc65c4 | 6125 | -- Do style check and generate reference, but skip both steps if this |
16ca248a ES |
6126 | -- entity has homonyms, since we may not have the right homonym set yet. |
6127 | -- The proper homonym will be set during the resolve phase. | |
07fc65c4 GB |
6128 | |
6129 | if Has_Homonym (Id) then | |
6130 | Set_Entity (N, Id); | |
a54ffd6c | 6131 | |
07fc65c4 | 6132 | else |
4c484f40 | 6133 | Set_Entity_Or_Discriminal (N, Id); |
84df40f7 | 6134 | |
a54ffd6c AC |
6135 | case Is_LHS (N) is |
6136 | when Yes => | |
6137 | Generate_Reference (Id, N, 'm'); | |
6138 | when No => | |
6139 | Generate_Reference (Id, N, 'r'); | |
6140 | when Unknown => | |
6141 | Deferred_References.Append ((Id, N)); | |
6142 | end case; | |
996ae0b0 RK |
6143 | end if; |
6144 | ||
30196a76 RD |
6145 | -- Check for violation of No_Wide_Characters |
6146 | ||
6147 | Check_Wide_Character_Restriction (Id, N); | |
6148 | ||
16ca248a ES |
6149 | -- If the Ekind of the entity is Void, it means that all homonyms are |
6150 | -- hidden from all visibility (RM 8.3(5,14-20)). | |
996ae0b0 RK |
6151 | |
6152 | if Ekind (Id) = E_Void then | |
6153 | Premature_Usage (N); | |
6154 | ||
ac7d724d | 6155 | elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then |
996ae0b0 RK |
6156 | declare |
6157 | H : Entity_Id := Homonym (Id); | |
6158 | ||
6159 | begin | |
6160 | while Present (H) loop | |
35ae2ed8 | 6161 | if Scope (H) = Scope (Id) |
ac7d724d ES |
6162 | and then (not Is_Hidden (H) |
6163 | or else Is_Immediately_Visible (H)) | |
35ae2ed8 | 6164 | then |
996ae0b0 RK |
6165 | Collect_Interps (N); |
6166 | exit; | |
6167 | end if; | |
6168 | ||
6169 | H := Homonym (H); | |
6170 | end loop; | |
fbf5a39b | 6171 | |
16ca248a ES |
6172 | -- If an extension of System is present, collect possible explicit |
6173 | -- overloadings declared in the extension. | |
fbf5a39b AC |
6174 | |
6175 | if Chars (P_Name) = Name_System | |
6176 | and then Scope (P_Name) = Standard_Standard | |
6177 | and then Present (System_Extend_Unit) | |
6178 | and then Present_System_Aux (N) | |
6179 | then | |
6180 | H := Current_Entity (Id); | |
6181 | ||
6182 | while Present (H) loop | |
6183 | if Scope (H) = System_Aux_Id then | |
6184 | Add_One_Interp (N, H, Etype (H)); | |
6185 | end if; | |
6186 | ||
6187 | H := Homonym (H); | |
6188 | end loop; | |
6189 | end if; | |
996ae0b0 RK |
6190 | end; |
6191 | end if; | |
6192 | ||
6193 | if Nkind (Selector_Name (N)) = N_Operator_Symbol | |
6194 | and then Scope (Id) /= Standard_Standard | |
6195 | then | |
16ca248a ES |
6196 | -- In addition to user-defined operators in the given scope, there |
6197 | -- may be an implicit instance of the predefined operator. The | |
6198 | -- operator (defined in Standard) is found in Has_Implicit_Operator, | |
6199 | -- and added to the interpretations. Procedure Add_One_Interp will | |
6200 | -- determine which hides which. | |
996ae0b0 RK |
6201 | |
6202 | if Has_Implicit_Operator (N) then | |
6203 | null; | |
6204 | end if; | |
6205 | end if; | |
be4c5193 AC |
6206 | |
6207 | -- If there is a single interpretation for N we can generate a | |
6208 | -- reference to the unique entity found. | |
6209 | ||
6210 | if Is_Overloadable (Id) and then not Is_Overloaded (N) then | |
6211 | Generate_Reference (Id, N); | |
6212 | end if; | |
fb757f7d AC |
6213 | |
6214 | Check_Restriction_No_Use_Of_Entity (N); | |
996ae0b0 RK |
6215 | end Find_Expanded_Name; |
6216 | ||
6217 | ------------------------- | |
6218 | -- Find_Renamed_Entity -- | |
6219 | ------------------------- | |
6220 | ||
6221 | function Find_Renamed_Entity | |
6222 | (N : Node_Id; | |
6223 | Nam : Node_Id; | |
6224 | New_S : Entity_Id; | |
6225 | Is_Actual : Boolean := False) return Entity_Id | |
6226 | is | |
fbf5a39b | 6227 | Ind : Interp_Index; |
996ae0b0 RK |
6228 | I1 : Interp_Index := 0; -- Suppress junk warnings |
6229 | It : Interp; | |
6230 | It1 : Interp; | |
6231 | Old_S : Entity_Id; | |
6232 | Inst : Entity_Id; | |
6233 | ||
996ae0b0 RK |
6234 | function Is_Visible_Operation (Op : Entity_Id) return Boolean; |
6235 | -- If the renamed entity is an implicit operator, check whether it is | |
16ca248a ES |
6236 | -- visible because its operand type is properly visible. This check |
6237 | -- applies to explicit renamed entities that appear in the source in a | |
6238 | -- renaming declaration or a formal subprogram instance, but not to | |
6239 | -- default generic actuals with a name. | |
996ae0b0 | 6240 | |
923fa078 RD |
6241 | function Report_Overload return Entity_Id; |
6242 | -- List possible interpretations, and specialize message in the | |
6243 | -- case of a generic actual. | |
6244 | ||
6245 | function Within (Inner, Outer : Entity_Id) return Boolean; | |
16ca248a ES |
6246 | -- Determine whether a candidate subprogram is defined within the |
6247 | -- enclosing instance. If yes, it has precedence over outer candidates. | |
923fa078 | 6248 | |
996ae0b0 RK |
6249 | -------------------------- |
6250 | -- Is_Visible_Operation -- | |
6251 | -------------------------- | |
6252 | ||
6253 | function Is_Visible_Operation (Op : Entity_Id) return Boolean is | |
6254 | Scop : Entity_Id; | |
6255 | Typ : Entity_Id; | |
6256 | Btyp : Entity_Id; | |
6257 | ||
6258 | begin | |
6259 | if Ekind (Op) /= E_Operator | |
6260 | or else Scope (Op) /= Standard_Standard | |
6261 | or else (In_Instance | |
ac7d724d ES |
6262 | and then (not Is_Actual |
6263 | or else Present (Enclosing_Instance))) | |
996ae0b0 RK |
6264 | then |
6265 | return True; | |
6266 | ||
6267 | else | |
6268 | -- For a fixed point type operator, check the resulting type, | |
6269 | -- because it may be a mixed mode integer * fixed operation. | |
6270 | ||
6271 | if Present (Next_Formal (First_Formal (New_S))) | |
6272 | and then Is_Fixed_Point_Type (Etype (New_S)) | |
6273 | then | |
6274 | Typ := Etype (New_S); | |
6275 | else | |
6276 | Typ := Etype (First_Formal (New_S)); | |
6277 | end if; | |
6278 | ||
6279 | Btyp := Base_Type (Typ); | |
6280 | ||
6281 | if Nkind (Nam) /= N_Expanded_Name then | |
6282 | return (In_Open_Scopes (Scope (Btyp)) | |
6283 | or else Is_Potentially_Use_Visible (Btyp) | |
6284 | or else In_Use (Btyp) | |
6285 | or else In_Use (Scope (Btyp))); | |
6286 | ||
6287 | else | |
6288 | Scop := Entity (Prefix (Nam)); | |
6289 | ||
6290 | if Ekind (Scop) = E_Package | |
6291 | and then Present (Renamed_Object (Scop)) | |
6292 | then | |
6293 | Scop := Renamed_Object (Scop); | |
6294 | end if; | |
6295 | ||
6296 | -- Operator is visible if prefix of expanded name denotes | |
16b05213 | 6297 | -- scope of type, or else type is defined in System_Aux |
996ae0b0 RK |
6298 | -- and the prefix denotes System. |
6299 | ||
6300 | return Scope (Btyp) = Scop | |
6301 | or else (Scope (Btyp) = System_Aux_Id | |
6302 | and then Scope (Scope (Btyp)) = Scop); | |
6303 | end if; | |
6304 | end if; | |
6305 | end Is_Visible_Operation; | |
6306 | ||
6307 | ------------ | |
6308 | -- Within -- | |
6309 | ------------ | |
6310 | ||
6311 | function Within (Inner, Outer : Entity_Id) return Boolean is | |
16ca248a | 6312 | Sc : Entity_Id; |
996ae0b0 RK |
6313 | |
6314 | begin | |
16ca248a | 6315 | Sc := Scope (Inner); |
996ae0b0 | 6316 | while Sc /= Standard_Standard loop |
996ae0b0 RK |
6317 | if Sc = Outer then |
6318 | return True; | |
6319 | else | |
6320 | Sc := Scope (Sc); | |
6321 | end if; | |
6322 | end loop; | |
6323 | ||
6324 | return False; | |
6325 | end Within; | |
6326 | ||
923fa078 RD |
6327 | --------------------- |
6328 | -- Report_Overload -- | |
6329 | --------------------- | |
fbf5a39b AC |
6330 | |
6331 | function Report_Overload return Entity_Id is | |
6332 | begin | |
6333 | if Is_Actual then | |
ed2233dc | 6334 | Error_Msg_NE -- CODEFIX |
fbf5a39b | 6335 | ("ambiguous actual subprogram&, " & |
923fa078 | 6336 | "possible interpretations:", N, Nam); |
fbf5a39b | 6337 | else |
ed2233dc | 6338 | Error_Msg_N -- CODEFIX |
fbf5a39b | 6339 | ("ambiguous subprogram, " & |
923fa078 | 6340 | "possible interpretations:", N); |
fbf5a39b AC |
6341 | end if; |
6342 | ||
6343 | List_Interps (Nam, N); | |
6344 | return Old_S; | |
6345 | end Report_Overload; | |
6346 | ||
0187b60e | 6347 | -- Start of processing for Find_Renamed_Entity |
996ae0b0 RK |
6348 | |
6349 | begin | |
6350 | Old_S := Any_Id; | |
6351 | Candidate_Renaming := Empty; | |
6352 | ||
fc193526 | 6353 | if Is_Overloaded (Nam) then |
fbf5a39b | 6354 | Get_First_Interp (Nam, Ind, It); |
996ae0b0 | 6355 | while Present (It.Nam) loop |
996ae0b0 | 6356 | if Entity_Matches_Spec (It.Nam, New_S) |
fc193526 | 6357 | and then Is_Visible_Operation (It.Nam) |
996ae0b0 RK |
6358 | then |
6359 | if Old_S /= Any_Id then | |
6360 | ||
6361 | -- Note: The call to Disambiguate only happens if a | |
6362 | -- previous interpretation was found, in which case I1 | |
6363 | -- has received a value. | |
6364 | ||
fbf5a39b | 6365 | It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S)); |
996ae0b0 RK |
6366 | |
6367 | if It1 = No_Interp then | |
996ae0b0 RK |
6368 | Inst := Enclosing_Instance; |
6369 | ||
6370 | if Present (Inst) then | |
996ae0b0 | 6371 | if Within (It.Nam, Inst) then |
444acbdd AC |
6372 | if Within (Old_S, Inst) then |
6373 | ||
6374 | -- Choose the innermost subprogram, which would | |
6375 | -- have hidden the outer one in the generic. | |
6376 | ||
6377 | if Scope_Depth (It.Nam) < | |
6378 | Scope_Depth (Old_S) | |
6379 | then | |
6380 | return Old_S; | |
444acbdd AC |
6381 | else |
6382 | return It.Nam; | |
6383 | end if; | |
6384 | end if; | |
6385 | ||
996ae0b0 RK |
6386 | elsif Within (Old_S, Inst) then |
6387 | return (Old_S); | |
444acbdd | 6388 | |
996ae0b0 | 6389 | else |
fbf5a39b | 6390 | return Report_Overload; |
996ae0b0 RK |
6391 | end if; |
6392 | ||
3ddd922e | 6393 | -- If not within an instance, ambiguity is real |
444acbdd | 6394 | |
996ae0b0 | 6395 | else |
fbf5a39b | 6396 | return Report_Overload; |
996ae0b0 RK |
6397 | end if; |
6398 | ||
6399 | else | |
6400 | Old_S := It1.Nam; | |
6401 | exit; | |
6402 | end if; | |
6403 | ||
6404 | else | |
fbf5a39b | 6405 | I1 := Ind; |
996ae0b0 RK |
6406 | Old_S := It.Nam; |
6407 | end if; | |
6408 | ||
6409 | elsif | |
6410 | Present (First_Formal (It.Nam)) | |
6411 | and then Present (First_Formal (New_S)) | |
ac7d724d ES |
6412 | and then (Base_Type (Etype (First_Formal (It.Nam))) = |
6413 | Base_Type (Etype (First_Formal (New_S)))) | |
996ae0b0 RK |
6414 | then |
6415 | Candidate_Renaming := It.Nam; | |
6416 | end if; | |
6417 | ||
fbf5a39b | 6418 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
6419 | end loop; |
6420 | ||
6421 | Set_Entity (Nam, Old_S); | |
1138cf59 AC |
6422 | |
6423 | if Old_S /= Any_Id then | |
6424 | Set_Is_Overloaded (Nam, False); | |
6425 | end if; | |
fc193526 HK |
6426 | |
6427 | -- Non-overloaded case | |
6428 | ||
6429 | else | |
60aa5228 AC |
6430 | if Is_Actual |
6431 | and then Present (Enclosing_Instance) | |
6432 | and then Entity_Matches_Spec (Entity (Nam), New_S) | |
6433 | then | |
fc193526 HK |
6434 | Old_S := Entity (Nam); |
6435 | ||
6436 | elsif Entity_Matches_Spec (Entity (Nam), New_S) then | |
6437 | Candidate_Renaming := New_S; | |
6438 | ||
6439 | if Is_Visible_Operation (Entity (Nam)) then | |
6440 | Old_S := Entity (Nam); | |
6441 | end if; | |
6442 | ||
6443 | elsif Present (First_Formal (Entity (Nam))) | |
6444 | and then Present (First_Formal (New_S)) | |
6445 | and then (Base_Type (Etype (First_Formal (Entity (Nam)))) = | |
6446 | Base_Type (Etype (First_Formal (New_S)))) | |
6447 | then | |
6448 | Candidate_Renaming := Entity (Nam); | |
6449 | end if; | |
996ae0b0 RK |
6450 | end if; |
6451 | ||
6452 | return Old_S; | |
6453 | end Find_Renamed_Entity; | |
6454 | ||
6455 | ----------------------------- | |
6456 | -- Find_Selected_Component -- | |
6457 | ----------------------------- | |
6458 | ||
6459 | procedure Find_Selected_Component (N : Node_Id) is | |
fbf5a39b | 6460 | P : constant Node_Id := Prefix (N); |
996ae0b0 RK |
6461 | |
6462 | P_Name : Entity_Id; | |
6463 | -- Entity denoted by prefix | |
6464 | ||
6465 | P_Type : Entity_Id; | |
6466 | -- and its type | |
6467 | ||
6468 | Nam : Node_Id; | |
6469 | ||
95e00a3a ES |
6470 | function Available_Subtype return Boolean; |
6471 | -- A small optimization: if the prefix is constrained and the component | |
6472 | -- is an array type we may already have a usable subtype for it, so we | |
6473 | -- can use it rather than generating a new one, because the bounds | |
6474 | -- will be the values of the discriminants and not discriminant refs. | |
c9d70ab1 AC |
6475 | -- This simplifies value tracing in GNATProve. For consistency, both |
6476 | -- the entity name and the subtype come from the constrained component. | |
95e00a3a | 6477 | |
c0dd5b38 AC |
6478 | -- This is only used in GNATProve mode: when generating code it may be |
6479 | -- necessary to create an itype in the scope of use of the selected | |
6480 | -- component, e.g. in the context of a expanded record equality. | |
6481 | ||
2e70d415 AC |
6482 | function Is_Reference_In_Subunit return Boolean; |
6483 | -- In a subunit, the scope depth is not a proper measure of hiding, | |
6484 | -- because the context of the proper body may itself hide entities in | |
6485 | -- parent units. This rare case requires inspecting the tree directly | |
6486 | -- because the proper body is inserted in the main unit and its context | |
6487 | -- is simply added to that of the parent. | |
6488 | ||
95e00a3a ES |
6489 | ----------------------- |
6490 | -- Available_Subtype -- | |
6491 | ----------------------- | |
6492 | ||
6493 | function Available_Subtype return Boolean is | |
6494 | Comp : Entity_Id; | |
c9d70ab1 | 6495 | |
95e00a3a | 6496 | begin |
c0dd5b38 AC |
6497 | if GNATprove_Mode then |
6498 | Comp := First_Entity (Etype (P)); | |
6499 | while Present (Comp) loop | |
6500 | if Chars (Comp) = Chars (Selector_Name (N)) then | |
6501 | Set_Etype (N, Etype (Comp)); | |
6502 | Set_Entity (Selector_Name (N), Comp); | |
6503 | Set_Etype (Selector_Name (N), Etype (Comp)); | |
6504 | return True; | |
6505 | end if; | |
95e00a3a | 6506 | |
c0dd5b38 AC |
6507 | Next_Component (Comp); |
6508 | end loop; | |
6509 | end if; | |
95e00a3a ES |
6510 | |
6511 | return False; | |
6512 | end Available_Subtype; | |
6513 | ||
2e70d415 AC |
6514 | ----------------------------- |
6515 | -- Is_Reference_In_Subunit -- | |
6516 | ----------------------------- | |
6517 | ||
6518 | function Is_Reference_In_Subunit return Boolean is | |
6519 | Clause : Node_Id; | |
6520 | Comp_Unit : Node_Id; | |
6521 | ||
6522 | begin | |
6523 | Comp_Unit := N; | |
6524 | while Present (Comp_Unit) | |
f5f6d8d7 | 6525 | and then Nkind (Comp_Unit) /= N_Compilation_Unit |
2e70d415 AC |
6526 | loop |
6527 | Comp_Unit := Parent (Comp_Unit); | |
6528 | end loop; | |
6529 | ||
f5f6d8d7 | 6530 | if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then |
2e70d415 AC |
6531 | return False; |
6532 | end if; | |
6533 | ||
6534 | -- Now check whether the package is in the context of the subunit | |
6535 | ||
6536 | Clause := First (Context_Items (Comp_Unit)); | |
2e70d415 AC |
6537 | while Present (Clause) loop |
6538 | if Nkind (Clause) = N_With_Clause | |
6539 | and then Entity (Name (Clause)) = P_Name | |
6540 | then | |
6541 | return True; | |
6542 | end if; | |
6543 | ||
6544 | Clause := Next (Clause); | |
6545 | end loop; | |
6546 | ||
6547 | return False; | |
6548 | end Is_Reference_In_Subunit; | |
6549 | ||
f5f6d8d7 AC |
6550 | -- Start of processing for Find_Selected_Component |
6551 | ||
996ae0b0 RK |
6552 | begin |
6553 | Analyze (P); | |
6554 | ||
6555 | if Nkind (P) = N_Error then | |
6556 | return; | |
23685ae6 AC |
6557 | end if; |
6558 | ||
6559 | -- Selector name cannot be a character literal or an operator symbol in | |
db72f10a | 6560 | -- SPARK, except for the operator symbol in a renaming. |
23685ae6 | 6561 | |
6480338a | 6562 | if Restriction_Check_Required (SPARK_05) then |
23685ae6 | 6563 | if Nkind (Selector_Name (N)) = N_Character_Literal then |
ce5ba43a | 6564 | Check_SPARK_05_Restriction |
23685ae6 | 6565 | ("character literal cannot be prefixed", N); |
db72f10a AC |
6566 | elsif Nkind (Selector_Name (N)) = N_Operator_Symbol |
6567 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
6568 | then | |
ce5ba43a AC |
6569 | Check_SPARK_05_Restriction |
6570 | ("operator symbol cannot be prefixed", N); | |
23685ae6 AC |
6571 | end if; |
6572 | end if; | |
996ae0b0 | 6573 | |
16ca248a ES |
6574 | -- If the selector already has an entity, the node has been constructed |
6575 | -- in the course of expansion, and is known to be valid. Do not verify | |
6576 | -- that it is defined for the type (it may be a private component used | |
6577 | -- in the expansion of record equality). | |
996ae0b0 | 6578 | |
23685ae6 | 6579 | if Present (Entity (Selector_Name (N))) then |
f5f6d8d7 | 6580 | if No (Etype (N)) or else Etype (N) = Any_Type then |
996ae0b0 | 6581 | declare |
fbf5a39b AC |
6582 | Sel_Name : constant Node_Id := Selector_Name (N); |
6583 | Selector : constant Entity_Id := Entity (Sel_Name); | |
996ae0b0 RK |
6584 | C_Etype : Node_Id; |
6585 | ||
6586 | begin | |
6587 | Set_Etype (Sel_Name, Etype (Selector)); | |
6588 | ||
6589 | if not Is_Entity_Name (P) then | |
fbf5a39b | 6590 | Resolve (P); |
996ae0b0 RK |
6591 | end if; |
6592 | ||
6593 | -- Build an actual subtype except for the first parameter | |
fbf5a39b | 6594 | -- of an init proc, where this actual subtype is by |
996ae0b0 RK |
6595 | -- definition incorrect, since the object is uninitialized |
6596 | -- (and does not even have defined discriminants etc.) | |
6597 | ||
6598 | if Is_Entity_Name (P) | |
6599 | and then Ekind (Entity (P)) = E_Function | |
6600 | then | |
6601 | Nam := New_Copy (P); | |
6602 | ||
6603 | if Is_Overloaded (P) then | |
6604 | Save_Interps (P, Nam); | |
6605 | end if; | |
6606 | ||
f5f6d8d7 | 6607 | Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam)); |
996ae0b0 RK |
6608 | Analyze_Call (P); |
6609 | Analyze_Selected_Component (N); | |
6610 | return; | |
6611 | ||
6612 | elsif Ekind (Selector) = E_Component | |
6613 | and then (not Is_Entity_Name (P) | |
6614 | or else Chars (Entity (P)) /= Name_uInit) | |
6615 | then | |
5a271a7f RD |
6616 | -- Check if we already have an available subtype we can use |
6617 | ||
e0c23ac7 | 6618 | if Ekind (Etype (P)) = E_Record_Subtype |
95e00a3a ES |
6619 | and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration |
6620 | and then Is_Array_Type (Etype (Selector)) | |
6621 | and then not Is_Packed (Etype (Selector)) | |
6622 | and then Available_Subtype | |
6623 | then | |
6624 | return; | |
6625 | ||
9fc2854d AC |
6626 | -- Do not build the subtype when referencing components of |
6627 | -- dispatch table wrappers. Required to avoid generating | |
535a8637 | 6628 | -- elaboration code with HI runtimes. |
9fc2854d | 6629 | |
535a8637 | 6630 | elsif RTU_Loaded (Ada_Tags) |
df3e68b1 HK |
6631 | and then |
6632 | ((RTE_Available (RE_Dispatch_Table_Wrapper) | |
2c1b72d7 | 6633 | and then Scope (Selector) = |
df3e68b1 | 6634 | RTE (RE_Dispatch_Table_Wrapper)) |
f5f6d8d7 AC |
6635 | or else |
6636 | (RTE_Available (RE_No_Dispatch_Table_Wrapper) | |
6637 | and then Scope (Selector) = | |
6638 | RTE (RE_No_Dispatch_Table_Wrapper))) | |
9fc2854d AC |
6639 | then |
6640 | C_Etype := Empty; | |
9fc2854d AC |
6641 | else |
6642 | C_Etype := | |
2c1b72d7 AC |
6643 | Build_Actual_Subtype_Of_Component |
6644 | (Etype (Selector), N); | |
9fc2854d AC |
6645 | end if; |
6646 | ||
996ae0b0 RK |
6647 | else |
6648 | C_Etype := Empty; | |
6649 | end if; | |
6650 | ||
6651 | if No (C_Etype) then | |
6652 | C_Etype := Etype (Selector); | |
6653 | else | |
6654 | Insert_Action (N, C_Etype); | |
6655 | C_Etype := Defining_Identifier (C_Etype); | |
6656 | end if; | |
6657 | ||
6658 | Set_Etype (N, C_Etype); | |
6659 | end; | |
6660 | ||
6661 | -- If this is the name of an entry or protected operation, and | |
6662 | -- the prefix is an access type, insert an explicit dereference, | |
6663 | -- so that entry calls are treated uniformly. | |
6664 | ||
6665 | if Is_Access_Type (Etype (P)) | |
6666 | and then Is_Concurrent_Type (Designated_Type (Etype (P))) | |
6667 | then | |
6668 | declare | |
fbf5a39b AC |
6669 | New_P : constant Node_Id := |
6670 | Make_Explicit_Dereference (Sloc (P), | |
6671 | Prefix => Relocate_Node (P)); | |
996ae0b0 RK |
6672 | begin |
6673 | Rewrite (P, New_P); | |
6674 | Set_Etype (P, Designated_Type (Etype (Prefix (P)))); | |
6675 | end; | |
6676 | end if; | |
6677 | ||
6678 | -- If the selected component appears within a default expression | |
6679 | -- and it has an actual subtype, the pre-analysis has not yet | |
6680 | -- completed its analysis, because Insert_Actions is disabled in | |
fbf5a39b | 6681 | -- that context. Within the init proc of the enclosing type we |
996ae0b0 RK |
6682 | -- must complete this analysis, if an actual subtype was created. |
6683 | ||
6684 | elsif Inside_Init_Proc then | |
6685 | declare | |
6686 | Typ : constant Entity_Id := Etype (N); | |
6687 | Decl : constant Node_Id := Declaration_Node (Typ); | |
996ae0b0 RK |
6688 | begin |
6689 | if Nkind (Decl) = N_Subtype_Declaration | |
6690 | and then not Analyzed (Decl) | |
6691 | and then Is_List_Member (Decl) | |
6692 | and then No (Parent (Decl)) | |
6693 | then | |
6694 | Remove (Decl); | |
6695 | Insert_Action (N, Decl); | |
6696 | end if; | |
6697 | end; | |
6698 | end if; | |
6699 | ||
6700 | return; | |
6701 | ||
6702 | elsif Is_Entity_Name (P) then | |
6703 | P_Name := Entity (P); | |
6704 | ||
6705 | -- The prefix may denote an enclosing type which is the completion | |
6706 | -- of an incomplete type declaration. | |
6707 | ||
6708 | if Is_Type (P_Name) then | |
6709 | Set_Entity (P, Get_Full_View (P_Name)); | |
6710 | Set_Etype (P, Entity (P)); | |
6711 | P_Name := Entity (P); | |
6712 | end if; | |
6713 | ||
6714 | P_Type := Base_Type (Etype (P)); | |
6715 | ||
6716 | if Debug_Flag_E then | |
6717 | Write_Str ("Found prefix type to be "); | |
6718 | Write_Entity_Info (P_Type, " "); Write_Eol; | |
6719 | end if; | |
6720 | ||
38cea80d ES |
6721 | -- The designated type may be a limited view with no components. |
6722 | -- Check whether the non-limited view is available, because in some | |
b943a971 AC |
6723 | -- cases this will not be set when installing the context. Rewrite |
6724 | -- the node by introducing an explicit dereference at once, and | |
6725 | -- setting the type of the rewritten prefix to the non-limited view | |
6726 | -- of the original designated type. | |
38cea80d ES |
6727 | |
6728 | if Is_Access_Type (P_Type) then | |
6729 | declare | |
b943a971 AC |
6730 | Desig_Typ : constant Entity_Id := |
6731 | Directly_Designated_Type (P_Type); | |
6732 | ||
38cea80d | 6733 | begin |
b943a971 AC |
6734 | if Is_Incomplete_Type (Desig_Typ) |
6735 | and then From_Limited_With (Desig_Typ) | |
6736 | and then Present (Non_Limited_View (Desig_Typ)) | |
38cea80d | 6737 | then |
b943a971 AC |
6738 | Rewrite (P, |
6739 | Make_Explicit_Dereference (Sloc (P), | |
6740 | Prefix => Relocate_Node (P))); | |
6741 | ||
6742 | Set_Etype (P, Get_Full_View (Non_Limited_View (Desig_Typ))); | |
6743 | P_Type := Etype (P); | |
38cea80d ES |
6744 | end if; |
6745 | end; | |
6746 | end if; | |
6747 | ||
996ae0b0 RK |
6748 | -- First check for components of a record object (not the |
6749 | -- result of a call, which is handled below). | |
6750 | ||
6751 | if Is_Appropriate_For_Record (P_Type) | |
6752 | and then not Is_Overloadable (P_Name) | |
6753 | and then not Is_Type (P_Name) | |
6754 | then | |
6755 | -- Selected component of record. Type checking will validate | |
6756 | -- name of selector. | |
dbfeb4fa RD |
6757 | |
6758 | -- ??? Could we rewrite an implicit dereference into an explicit | |
11560bcc | 6759 | -- one here? |
996ae0b0 RK |
6760 | |
6761 | Analyze_Selected_Component (N); | |
6762 | ||
f6b5dc8e AC |
6763 | -- Reference to type name in predicate/invariant expression |
6764 | ||
996ae0b0 RK |
6765 | elsif Is_Appropriate_For_Entry_Prefix (P_Type) |
6766 | and then not In_Open_Scopes (P_Name) | |
6767 | and then (not Is_Concurrent_Type (Etype (P_Name)) | |
ac7d724d | 6768 | or else not In_Open_Scopes (Etype (P_Name))) |
996ae0b0 RK |
6769 | then |
6770 | -- Call to protected operation or entry. Type checking is | |
6771 | -- needed on the prefix. | |
6772 | ||
6773 | Analyze_Selected_Component (N); | |
6774 | ||
6775 | elsif (In_Open_Scopes (P_Name) | |
f6b5dc8e AC |
6776 | and then Ekind (P_Name) /= E_Void |
6777 | and then not Is_Overloadable (P_Name)) | |
996ae0b0 | 6778 | or else (Is_Concurrent_Type (Etype (P_Name)) |
f6b5dc8e | 6779 | and then In_Open_Scopes (Etype (P_Name))) |
996ae0b0 RK |
6780 | then |
6781 | -- Prefix denotes an enclosing loop, block, or task, i.e. an | |
6782 | -- enclosing construct that is not a subprogram or accept. | |
6783 | ||
549cc9c2 AC |
6784 | -- A special case: a protected body may call an operation |
6785 | -- on an external object of the same type, in which case it | |
6786 | -- is not an expanded name. If the prefix is the type itself, | |
6787 | -- or the context is a single synchronized object it can only | |
6788 | -- be interpreted as an expanded name. | |
6789 | ||
6790 | if Is_Concurrent_Type (Etype (P_Name)) then | |
6791 | if Is_Type (P_Name) | |
6792 | or else Present (Anonymous_Object (Etype (P_Name))) | |
6793 | then | |
6794 | Find_Expanded_Name (N); | |
6795 | ||
6796 | else | |
6797 | Analyze_Selected_Component (N); | |
6798 | return; | |
6799 | end if; | |
6800 | ||
6801 | else | |
6802 | Find_Expanded_Name (N); | |
6803 | end if; | |
996ae0b0 RK |
6804 | |
6805 | elsif Ekind (P_Name) = E_Package then | |
6806 | Find_Expanded_Name (N); | |
6807 | ||
6808 | elsif Is_Overloadable (P_Name) then | |
6809 | ||
6810 | -- The subprogram may be a renaming (of an enclosing scope) as | |
6811 | -- in the case of the name of the generic within an instantiation. | |
6812 | ||
f6b5dc8e | 6813 | if Ekind_In (P_Name, E_Procedure, E_Function) |
996ae0b0 RK |
6814 | and then Present (Alias (P_Name)) |
6815 | and then Is_Generic_Instance (Alias (P_Name)) | |
6816 | then | |
6817 | P_Name := Alias (P_Name); | |
6818 | end if; | |
6819 | ||
6820 | if Is_Overloaded (P) then | |
6821 | ||
bc41faa2 | 6822 | -- The prefix must resolve to a unique enclosing construct |
996ae0b0 RK |
6823 | |
6824 | declare | |
6825 | Found : Boolean := False; | |
fbf5a39b | 6826 | Ind : Interp_Index; |
996ae0b0 RK |
6827 | It : Interp; |
6828 | ||
6829 | begin | |
fbf5a39b | 6830 | Get_First_Interp (P, Ind, It); |
996ae0b0 | 6831 | while Present (It.Nam) loop |
996ae0b0 RK |
6832 | if In_Open_Scopes (It.Nam) then |
6833 | if Found then | |
6834 | Error_Msg_N ( | |
6835 | "prefix must be unique enclosing scope", N); | |
6836 | Set_Entity (N, Any_Id); | |
6837 | Set_Etype (N, Any_Type); | |
6838 | return; | |
6839 | ||
6840 | else | |
6841 | Found := True; | |
6842 | P_Name := It.Nam; | |
6843 | end if; | |
6844 | end if; | |
6845 | ||
fbf5a39b | 6846 | Get_Next_Interp (Ind, It); |
996ae0b0 RK |
6847 | end loop; |
6848 | end; | |
6849 | end if; | |
6850 | ||
6851 | if In_Open_Scopes (P_Name) then | |
6852 | Set_Entity (P, P_Name); | |
6853 | Set_Is_Overloaded (P, False); | |
6854 | Find_Expanded_Name (N); | |
6855 | ||
6856 | else | |
6857 | -- If no interpretation as an expanded name is possible, it | |
6858 | -- must be a selected component of a record returned by a | |
16ca248a | 6859 | -- function call. Reformat prefix as a function call, the rest |
2757c5bf AC |
6860 | -- is done by type resolution. |
6861 | ||
6862 | -- Error if the prefix is procedure or entry, as is P.X | |
996ae0b0 RK |
6863 | |
6864 | if Ekind (P_Name) /= E_Function | |
ac7d724d ES |
6865 | and then |
6866 | (not Is_Overloaded (P) | |
6867 | or else Nkind (Parent (N)) = N_Procedure_Call_Statement) | |
996ae0b0 | 6868 | then |
996ae0b0 | 6869 | -- Prefix may mention a package that is hidden by a local |
290986ed GB |
6870 | -- declaration: let the user know. Scan the full homonym |
6871 | -- chain, the candidate package may be anywhere on it. | |
996ae0b0 | 6872 | |
290986ed | 6873 | if Present (Homonym (Current_Entity (P_Name))) then |
290986ed | 6874 | P_Name := Current_Entity (P_Name); |
996ae0b0 RK |
6875 | |
6876 | while Present (P_Name) loop | |
6877 | exit when Ekind (P_Name) = E_Package; | |
6878 | P_Name := Homonym (P_Name); | |
6879 | end loop; | |
6880 | ||
6881 | if Present (P_Name) then | |
2e70d415 AC |
6882 | if not Is_Reference_In_Subunit then |
6883 | Error_Msg_Sloc := Sloc (Entity (Prefix (N))); | |
2e70d415 | 6884 | Error_Msg_NE |
f5f6d8d7 | 6885 | ("package& is hidden by declaration#", N, P_Name); |
2e70d415 | 6886 | end if; |
996ae0b0 RK |
6887 | |
6888 | Set_Entity (Prefix (N), P_Name); | |
6889 | Find_Expanded_Name (N); | |
6890 | return; | |
2757c5bf | 6891 | |
996ae0b0 RK |
6892 | else |
6893 | P_Name := Entity (Prefix (N)); | |
6894 | end if; | |
6895 | end if; | |
6896 | ||
6897 | Error_Msg_NE | |
6898 | ("invalid prefix in selected component&", N, P_Name); | |
6899 | Change_Selected_Component_To_Expanded_Name (N); | |
6900 | Set_Entity (N, Any_Id); | |
6901 | Set_Etype (N, Any_Type); | |
6902 | ||
2757c5bf AC |
6903 | -- Here we have a function call, so do the reformatting |
6904 | ||
996ae0b0 RK |
6905 | else |
6906 | Nam := New_Copy (P); | |
6907 | Save_Interps (P, Nam); | |
2757c5bf AC |
6908 | |
6909 | -- We use Replace here because this is one of those cases | |
6910 | -- where the parser has missclassified the node, and we | |
6911 | -- fix things up and then do the semantic analysis on the | |
6912 | -- fixed up node. Normally we do this using one of the | |
6913 | -- Sinfo.CN routines, but this is too tricky for that. | |
6914 | ||
6915 | -- Note that using Rewrite would be wrong, because we | |
6916 | -- would have a tree where the original node is unanalyzed, | |
6917 | -- and this violates the required interface for ASIS. | |
6918 | ||
6919 | Replace (P, | |
996ae0b0 | 6920 | Make_Function_Call (Sloc (P), Name => Nam)); |
2757c5bf AC |
6921 | |
6922 | -- Now analyze the reformatted node | |
6923 | ||
996ae0b0 RK |
6924 | Analyze_Call (P); |
6925 | Analyze_Selected_Component (N); | |
6926 | end if; | |
6927 | end if; | |
6928 | ||
6929 | -- Remaining cases generate various error messages | |
6930 | ||
6931 | else | |
6932 | -- Format node as expanded name, to avoid cascaded errors | |
6933 | ||
b943a971 AC |
6934 | -- If the limited_with transformation was applied earlier, |
6935 | -- restore source for proper error reporting. | |
6936 | ||
6937 | if not Comes_From_Source (P) | |
6938 | and then Nkind (P) = N_Explicit_Dereference | |
6939 | then | |
6940 | Rewrite (P, Prefix (P)); | |
6941 | P_Type := Etype (P); | |
6942 | end if; | |
6943 | ||
fbf5a39b | 6944 | Change_Selected_Component_To_Expanded_Name (N); |
b943a971 AC |
6945 | Set_Entity (N, Any_Id); |
6946 | Set_Etype (N, Any_Type); | |
996ae0b0 | 6947 | |
996ae0b0 RK |
6948 | -- Issue error message, but avoid this if error issued already. |
6949 | -- Use identifier of prefix if one is available. | |
6950 | ||
bcb0389e | 6951 | if P_Name = Any_Id then |
996ae0b0 RK |
6952 | null; |
6953 | ||
596b25f9 AC |
6954 | -- It is not an error if the prefix is the current instance of |
6955 | -- type name, e.g. the expression of a type aspect, when it is | |
6956 | -- analyzed for ASIS use. | |
6957 | ||
6958 | elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then | |
6959 | null; | |
6960 | ||
996ae0b0 RK |
6961 | elsif Ekind (P_Name) = E_Void then |
6962 | Premature_Usage (P); | |
6963 | ||
6964 | elsif Nkind (P) /= N_Attribute_Reference then | |
67c0e662 RD |
6965 | |
6966 | -- This may have been meant as a prefixed call to a primitive | |
96df3ff4 AC |
6967 | -- of an untagged type. If it is a function call check type of |
6968 | -- its first formal and add explanation. | |
67c0e662 RD |
6969 | |
6970 | declare | |
6971 | F : constant Entity_Id := | |
6972 | Current_Entity (Selector_Name (N)); | |
6973 | begin | |
6974 | if Present (F) | |
6975 | and then Is_Overloadable (F) | |
6976 | and then Present (First_Entity (F)) | |
96df3ff4 | 6977 | and then not Is_Tagged_Type (Etype (First_Entity (F))) |
67c0e662 RD |
6978 | then |
6979 | Error_Msg_N | |
c308e762 HK |
6980 | ("prefixed call is only allowed for objects of a " |
6981 | & "tagged type", N); | |
67c0e662 RD |
6982 | end if; |
6983 | end; | |
6984 | ||
6985 | Error_Msg_N ("invalid prefix in selected component&", P); | |
996ae0b0 | 6986 | |
9596236a AC |
6987 | if Is_Access_Type (P_Type) |
6988 | and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type | |
6989 | then | |
65356e64 | 6990 | Error_Msg_N |
67c0e662 RD |
6991 | ("\dereference must not be of an incomplete type " |
6992 | & "(RM 3.10.1)", P); | |
9596236a AC |
6993 | end if; |
6994 | ||
996ae0b0 | 6995 | else |
67c0e662 | 6996 | Error_Msg_N ("invalid prefix in selected component", P); |
996ae0b0 RK |
6997 | end if; |
6998 | end if; | |
6999 | ||
db72f10a AC |
7000 | -- Selector name is restricted in SPARK |
7001 | ||
7002 | if Nkind (N) = N_Expanded_Name | |
6480338a | 7003 | and then Restriction_Check_Required (SPARK_05) |
db72f10a AC |
7004 | then |
7005 | if Is_Subprogram (P_Name) then | |
ce5ba43a | 7006 | Check_SPARK_05_Restriction |
db72f10a AC |
7007 | ("prefix of expanded name cannot be a subprogram", P); |
7008 | elsif Ekind (P_Name) = E_Loop then | |
ce5ba43a | 7009 | Check_SPARK_05_Restriction |
db72f10a AC |
7010 | ("prefix of expanded name cannot be a loop statement", P); |
7011 | end if; | |
7012 | end if; | |
7013 | ||
996ae0b0 RK |
7014 | else |
7015 | -- If prefix is not the name of an entity, it must be an expression, | |
7016 | -- whose type is appropriate for a record. This is determined by | |
7017 | -- type resolution. | |
7018 | ||
7019 | Analyze_Selected_Component (N); | |
7020 | end if; | |
0929eaeb AC |
7021 | |
7022 | Analyze_Dimension (N); | |
996ae0b0 RK |
7023 | end Find_Selected_Component; |
7024 | ||
7025 | --------------- | |
7026 | -- Find_Type -- | |
7027 | --------------- | |
7028 | ||
7029 | procedure Find_Type (N : Node_Id) is | |
7030 | C : Entity_Id; | |
7031 | Typ : Entity_Id; | |
7032 | T : Entity_Id; | |
7033 | T_Name : Entity_Id; | |
7034 | ||
7035 | begin | |
7036 | if N = Error then | |
7037 | return; | |
7038 | ||
7039 | elsif Nkind (N) = N_Attribute_Reference then | |
7040 | ||
11560bcc TQ |
7041 | -- Class attribute. This is not valid in Ada 83 mode, but we do not |
7042 | -- need to enforce that at this point, since the declaration of the | |
7043 | -- tagged type in the prefix would have been flagged already. | |
996ae0b0 RK |
7044 | |
7045 | if Attribute_Name (N) = Name_Class then | |
7046 | Check_Restriction (No_Dispatch, N); | |
7047 | Find_Type (Prefix (N)); | |
7048 | ||
7049 | -- Propagate error from bad prefix | |
7050 | ||
7051 | if Etype (Prefix (N)) = Any_Type then | |
7052 | Set_Entity (N, Any_Type); | |
7053 | Set_Etype (N, Any_Type); | |
7054 | return; | |
7055 | end if; | |
7056 | ||
7057 | T := Base_Type (Entity (Prefix (N))); | |
7058 | ||
11560bcc TQ |
7059 | -- Case where type is not known to be tagged. Its appearance in |
7060 | -- the prefix of the 'Class attribute indicates that the full view | |
16ca248a | 7061 | -- will be tagged. |
996ae0b0 RK |
7062 | |
7063 | if not Is_Tagged_Type (T) then | |
7064 | if Ekind (T) = E_Incomplete_Type then | |
7065 | ||
7066 | -- It is legal to denote the class type of an incomplete | |
7067 | -- type. The full type will have to be tagged, of course. | |
90067a15 | 7068 | -- In Ada 2005 this usage is declared obsolescent, so we |
ae247488 ES |
7069 | -- warn accordingly. This usage is only legal if the type |
7070 | -- is completed in the current scope, and not for a limited | |
7071 | -- view of a type. | |
7072 | ||
2f7b7467 AC |
7073 | if Ada_Version >= Ada_2005 then |
7074 | ||
7075 | -- Test whether the Available_View of a limited type view | |
7076 | -- is tagged, since the limited view may not be marked as | |
7077 | -- tagged if the type itself has an untagged incomplete | |
7078 | -- type view in its package. | |
7079 | ||
7b56a91b | 7080 | if From_Limited_With (T) |
2f7b7467 AC |
7081 | and then not Is_Tagged_Type (Available_View (T)) |
7082 | then | |
ae247488 ES |
7083 | Error_Msg_N |
7084 | ("prefix of Class attribute must be tagged", N); | |
7085 | Set_Etype (N, Any_Type); | |
7086 | Set_Entity (N, Any_Type); | |
7087 | return; | |
11560bcc | 7088 | |
dbfeb4fa RD |
7089 | -- ??? This test is temporarily disabled (always |
7090 | -- False) because it causes an unwanted warning on | |
7091 | -- GNAT sources (built with -gnatg, which includes | |
7092 | -- Warn_On_Obsolescent_ Feature). Once this issue | |
7093 | -- is cleared in the sources, it can be enabled. | |
11560bcc | 7094 | |
ac7d724d | 7095 | elsif Warn_On_Obsolescent_Feature and then False then |
ae247488 ES |
7096 | Error_Msg_N |
7097 | ("applying 'Class to an untagged incomplete type" | |
dbfeb4fa | 7098 | & " is an obsolescent feature (RM J.11)?r?", N); |
ae247488 | 7099 | end if; |
11560bcc | 7100 | end if; |
996ae0b0 RK |
7101 | |
7102 | Set_Is_Tagged_Type (T); | |
ef2a63ba | 7103 | Set_Direct_Primitive_Operations (T, New_Elmt_List); |
996ae0b0 RK |
7104 | Make_Class_Wide_Type (T); |
7105 | Set_Entity (N, Class_Wide_Type (T)); | |
7106 | Set_Etype (N, Class_Wide_Type (T)); | |
7107 | ||
7108 | elsif Ekind (T) = E_Private_Type | |
7109 | and then not Is_Generic_Type (T) | |
7110 | and then In_Private_Part (Scope (T)) | |
7111 | then | |
16ca248a ES |
7112 | -- The Class attribute can be applied to an untagged private |
7113 | -- type fulfilled by a tagged type prior to the full type | |
7114 | -- declaration (but only within the parent package's private | |
7115 | -- part). Create the class-wide type now and check that the | |
7116 | -- full type is tagged later during its analysis. Note that | |
7117 | -- we do not mark the private type as tagged, unlike the | |
7118 | -- case of incomplete types, because the type must still | |
996ae0b0 RK |
7119 | -- appear untagged to outside units. |
7120 | ||
cdc8c54c | 7121 | if No (Class_Wide_Type (T)) then |
996ae0b0 RK |
7122 | Make_Class_Wide_Type (T); |
7123 | end if; | |
7124 | ||
7125 | Set_Entity (N, Class_Wide_Type (T)); | |
7126 | Set_Etype (N, Class_Wide_Type (T)); | |
7127 | ||
7128 | else | |
16ca248a ES |
7129 | -- Should we introduce a type Any_Tagged and use Wrong_Type |
7130 | -- here, it would be a bit more consistent??? | |
996ae0b0 RK |
7131 | |
7132 | Error_Msg_NE | |
7133 | ("tagged type required, found}", | |
7134 | Prefix (N), First_Subtype (T)); | |
7135 | Set_Entity (N, Any_Type); | |
7136 | return; | |
7137 | end if; | |
7138 | ||
7139 | -- Case of tagged type | |
7140 | ||
7141 | else | |
39edfb45 | 7142 | if Is_Concurrent_Type (T) then |
cdc8c54c BD |
7143 | if No (Corresponding_Record_Type (Entity (Prefix (N)))) then |
7144 | ||
7145 | -- Previous error. Use current type, which at least | |
7146 | -- provides some operations. | |
7147 | ||
7148 | C := Entity (Prefix (N)); | |
7149 | ||
7150 | else | |
7151 | C := Class_Wide_Type | |
7152 | (Corresponding_Record_Type (Entity (Prefix (N)))); | |
7153 | end if; | |
7154 | ||
39edfb45 JM |
7155 | else |
7156 | C := Class_Wide_Type (Entity (Prefix (N))); | |
7157 | end if; | |
7158 | ||
e7ba564f | 7159 | Set_Entity_With_Checks (N, C); |
996ae0b0 RK |
7160 | Generate_Reference (C, N); |
7161 | Set_Etype (N, C); | |
996ae0b0 RK |
7162 | end if; |
7163 | ||
0ab80019 | 7164 | -- Base attribute, not allowed in Ada 83 |
996ae0b0 RK |
7165 | |
7166 | elsif Attribute_Name (N) = Name_Base then | |
7a489a2b | 7167 | Error_Msg_Name_1 := Name_Base; |
ce5ba43a | 7168 | Check_SPARK_05_Restriction |
7a489a2b AC |
7169 | ("attribute% is only allowed as prefix of another attribute", N); |
7170 | ||
0ab80019 | 7171 | if Ada_Version = Ada_83 and then Comes_From_Source (N) then |
996ae0b0 RK |
7172 | Error_Msg_N |
7173 | ("(Ada 83) Base attribute not allowed in subtype mark", N); | |
7174 | ||
7175 | else | |
7176 | Find_Type (Prefix (N)); | |
7177 | Typ := Entity (Prefix (N)); | |
7178 | ||
0ab80019 | 7179 | if Ada_Version >= Ada_95 |
fbf5a39b AC |
7180 | and then not Is_Scalar_Type (Typ) |
7181 | and then not Is_Generic_Type (Typ) | |
7182 | then | |
7183 | Error_Msg_N | |
bc41faa2 AC |
7184 | ("prefix of Base attribute must be scalar type", |
7185 | Prefix (N)); | |
fbf5a39b | 7186 | |
a8930b80 | 7187 | elsif Warn_On_Redundant_Constructs |
996ae0b0 | 7188 | and then Base_Type (Typ) = Typ |
996ae0b0 | 7189 | then |
ed2233dc | 7190 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 7191 | ("redundant attribute, & is its own base type?r?", N, Typ); |
996ae0b0 RK |
7192 | end if; |
7193 | ||
7194 | T := Base_Type (Typ); | |
996ae0b0 RK |
7195 | |
7196 | -- Rewrite attribute reference with type itself (see similar | |
a8930b80 AC |
7197 | -- processing in Analyze_Attribute, case Base). Preserve prefix |
7198 | -- if present, for other legality checks. | |
fbf5a39b AC |
7199 | |
7200 | if Nkind (Prefix (N)) = N_Expanded_Name then | |
7201 | Rewrite (N, | |
7202 | Make_Expanded_Name (Sloc (N), | |
11560bcc TQ |
7203 | Chars => Chars (T), |
7204 | Prefix => New_Copy (Prefix (Prefix (N))), | |
e4494292 | 7205 | Selector_Name => New_Occurrence_Of (T, Sloc (N)))); |
996ae0b0 | 7206 | |
fbf5a39b | 7207 | else |
e4494292 | 7208 | Rewrite (N, New_Occurrence_Of (T, Sloc (N))); |
fbf5a39b AC |
7209 | end if; |
7210 | ||
7211 | Set_Entity (N, T); | |
996ae0b0 RK |
7212 | Set_Etype (N, T); |
7213 | end if; | |
7214 | ||
923fa078 RD |
7215 | elsif Attribute_Name (N) = Name_Stub_Type then |
7216 | ||
7217 | -- This is handled in Analyze_Attribute | |
7218 | ||
7219 | Analyze (N); | |
7220 | ||
996ae0b0 RK |
7221 | -- All other attributes are invalid in a subtype mark |
7222 | ||
7223 | else | |
7224 | Error_Msg_N ("invalid attribute in subtype mark", N); | |
7225 | end if; | |
7226 | ||
7227 | else | |
7228 | Analyze (N); | |
7229 | ||
7230 | if Is_Entity_Name (N) then | |
7231 | T_Name := Entity (N); | |
7232 | else | |
7233 | Error_Msg_N ("subtype mark required in this context", N); | |
7234 | Set_Etype (N, Any_Type); | |
7235 | return; | |
7236 | end if; | |
7237 | ||
7238 | if T_Name = Any_Id or else Etype (N) = Any_Type then | |
7239 | ||
7240 | -- Undefined id. Make it into a valid type | |
7241 | ||
7242 | Set_Entity (N, Any_Type); | |
7243 | ||
7244 | elsif not Is_Type (T_Name) | |
7245 | and then T_Name /= Standard_Void_Type | |
7246 | then | |
7247 | Error_Msg_Sloc := Sloc (T_Name); | |
7248 | Error_Msg_N ("subtype mark required in this context", N); | |
923fa078 | 7249 | Error_Msg_NE ("\\found & declared#", N, T_Name); |
996ae0b0 RK |
7250 | Set_Entity (N, Any_Type); |
7251 | ||
7252 | else | |
11560bcc TQ |
7253 | -- If the type is an incomplete type created to handle |
7254 | -- anonymous access components of a record type, then the | |
7255 | -- incomplete type is the visible entity and subsequent | |
7256 | -- references will point to it. Mark the original full | |
7257 | -- type as referenced, to prevent spurious warnings. | |
7258 | ||
7259 | if Is_Incomplete_Type (T_Name) | |
7260 | and then Present (Full_View (T_Name)) | |
7261 | and then not Comes_From_Source (T_Name) | |
7262 | then | |
7263 | Set_Referenced (Full_View (T_Name)); | |
7264 | end if; | |
7265 | ||
996ae0b0 RK |
7266 | T_Name := Get_Full_View (T_Name); |
7267 | ||
11560bcc TQ |
7268 | -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through |
7269 | -- limited-with clauses | |
7270 | ||
7b56a91b | 7271 | if From_Limited_With (T_Name) |
11560bcc TQ |
7272 | and then Ekind (T_Name) in Incomplete_Kind |
7273 | and then Present (Non_Limited_View (T_Name)) | |
7274 | and then Is_Interface (Non_Limited_View (T_Name)) | |
7275 | then | |
7276 | T_Name := Non_Limited_View (T_Name); | |
7277 | end if; | |
7278 | ||
996ae0b0 RK |
7279 | if In_Open_Scopes (T_Name) then |
7280 | if Ekind (Base_Type (T_Name)) = E_Task_Type then | |
923fa078 RD |
7281 | |
7282 | -- In Ada 2005, a task name can be used in an access | |
e264efcc AC |
7283 | -- definition within its own body. It cannot be used |
7284 | -- in the discriminant part of the task declaration, | |
7285 | -- nor anywhere else in the declaration because entries | |
7286 | -- cannot have access parameters. | |
923fa078 | 7287 | |
0791fbe9 | 7288 | if Ada_Version >= Ada_2005 |
923fa078 RD |
7289 | and then Nkind (Parent (N)) = N_Access_Definition |
7290 | then | |
7291 | Set_Entity (N, T_Name); | |
7292 | Set_Etype (N, T_Name); | |
e264efcc AC |
7293 | |
7294 | if Has_Completion (T_Name) then | |
7295 | return; | |
7296 | ||
7297 | else | |
7298 | Error_Msg_N | |
7299 | ("task type cannot be used as type mark " & | |
7300 | "within its own declaration", N); | |
7301 | end if; | |
923fa078 RD |
7302 | |
7303 | else | |
7304 | Error_Msg_N | |
7305 | ("task type cannot be used as type mark " & | |
fbe627af | 7306 | "within its own spec or body", N); |
923fa078 | 7307 | end if; |
fbe627af RD |
7308 | |
7309 | elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then | |
7310 | ||
7311 | -- In Ada 2005, a protected name can be used in an access | |
7312 | -- definition within its own body. | |
7313 | ||
0791fbe9 | 7314 | if Ada_Version >= Ada_2005 |
fbe627af RD |
7315 | and then Nkind (Parent (N)) = N_Access_Definition |
7316 | then | |
7317 | Set_Entity (N, T_Name); | |
7318 | Set_Etype (N, T_Name); | |
7319 | return; | |
7320 | ||
7321 | else | |
7322 | Error_Msg_N | |
7323 | ("protected type cannot be used as type mark " & | |
7324 | "within its own spec or body", N); | |
7325 | end if; | |
7326 | ||
996ae0b0 RK |
7327 | else |
7328 | Error_Msg_N ("type declaration cannot refer to itself", N); | |
7329 | end if; | |
7330 | ||
7331 | Set_Etype (N, Any_Type); | |
7332 | Set_Entity (N, Any_Type); | |
7333 | Set_Error_Posted (T_Name); | |
7334 | return; | |
7335 | end if; | |
7336 | ||
7337 | Set_Entity (N, T_Name); | |
7338 | Set_Etype (N, T_Name); | |
7339 | end if; | |
7340 | end if; | |
7341 | ||
07fc65c4 | 7342 | if Present (Etype (N)) and then Comes_From_Source (N) then |
996ae0b0 RK |
7343 | if Is_Fixed_Point_Type (Etype (N)) then |
7344 | Check_Restriction (No_Fixed_Point, N); | |
7345 | elsif Is_Floating_Point_Type (Etype (N)) then | |
7346 | Check_Restriction (No_Floating_Point, N); | |
7347 | end if; | |
8636f52f HK |
7348 | |
7349 | -- A Ghost type must appear in a specific context | |
7350 | ||
7351 | if Is_Ghost_Entity (Etype (N)) then | |
7352 | Check_Ghost_Context (Etype (N), N); | |
7353 | end if; | |
996ae0b0 RK |
7354 | end if; |
7355 | end Find_Type; | |
7356 | ||
996ae0b0 RK |
7357 | ------------------------------------ |
7358 | -- Has_Implicit_Character_Literal -- | |
7359 | ------------------------------------ | |
7360 | ||
7361 | function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is | |
7362 | Id : Entity_Id; | |
7363 | Found : Boolean := False; | |
7364 | P : constant Entity_Id := Entity (Prefix (N)); | |
7365 | Priv_Id : Entity_Id := Empty; | |
7366 | ||
7367 | begin | |
ac7d724d | 7368 | if Ekind (P) = E_Package and then not In_Open_Scopes (P) then |
996ae0b0 RK |
7369 | Priv_Id := First_Private_Entity (P); |
7370 | end if; | |
7371 | ||
7372 | if P = Standard_Standard then | |
7373 | Change_Selected_Component_To_Expanded_Name (N); | |
7374 | Rewrite (N, Selector_Name (N)); | |
7375 | Analyze (N); | |
7376 | Set_Etype (Original_Node (N), Standard_Character); | |
7377 | return True; | |
7378 | end if; | |
7379 | ||
7380 | Id := First_Entity (P); | |
ac7d724d | 7381 | while Present (Id) and then Id /= Priv_Id loop |
d347f572 AC |
7382 | if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then |
7383 | ||
996ae0b0 RK |
7384 | -- We replace the node with the literal itself, resolve as a |
7385 | -- character, and set the type correctly. | |
7386 | ||
7387 | if not Found then | |
7388 | Change_Selected_Component_To_Expanded_Name (N); | |
7389 | Rewrite (N, Selector_Name (N)); | |
7390 | Analyze (N); | |
7391 | Set_Etype (N, Id); | |
7392 | Set_Etype (Original_Node (N), Id); | |
7393 | Found := True; | |
7394 | ||
7395 | else | |
7396 | -- More than one type derived from Character in given scope. | |
7397 | -- Collect all possible interpretations. | |
7398 | ||
7399 | Add_One_Interp (N, Id, Id); | |
7400 | end if; | |
7401 | end if; | |
7402 | ||
7403 | Next_Entity (Id); | |
7404 | end loop; | |
7405 | ||
7406 | return Found; | |
7407 | end Has_Implicit_Character_Literal; | |
7408 | ||
9bc856dd AC |
7409 | ---------------------- |
7410 | -- Has_Private_With -- | |
7411 | ---------------------- | |
7412 | ||
7413 | function Has_Private_With (E : Entity_Id) return Boolean is | |
7414 | Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit); | |
7415 | Item : Node_Id; | |
7416 | ||
7417 | begin | |
7418 | Item := First (Context_Items (Comp_Unit)); | |
7419 | while Present (Item) loop | |
7420 | if Nkind (Item) = N_With_Clause | |
7421 | and then Private_Present (Item) | |
7422 | and then Entity (Name (Item)) = E | |
7423 | then | |
7424 | return True; | |
7425 | end if; | |
7426 | ||
7427 | Next (Item); | |
7428 | end loop; | |
7429 | ||
7430 | return False; | |
7431 | end Has_Private_With; | |
7432 | ||
996ae0b0 RK |
7433 | --------------------------- |
7434 | -- Has_Implicit_Operator -- | |
7435 | --------------------------- | |
7436 | ||
7437 | function Has_Implicit_Operator (N : Node_Id) return Boolean is | |
7438 | Op_Id : constant Name_Id := Chars (Selector_Name (N)); | |
7439 | P : constant Entity_Id := Entity (Prefix (N)); | |
7440 | Id : Entity_Id; | |
7441 | Priv_Id : Entity_Id := Empty; | |
7442 | ||
fbf5a39b AC |
7443 | procedure Add_Implicit_Operator |
7444 | (T : Entity_Id; | |
7445 | Op_Type : Entity_Id := Empty); | |
fbe627af RD |
7446 | -- Add implicit interpretation to node N, using the type for which a |
7447 | -- predefined operator exists. If the operator yields a boolean type, | |
7448 | -- the Operand_Type is implicitly referenced by the operator, and a | |
7449 | -- reference to it must be generated. | |
996ae0b0 RK |
7450 | |
7451 | --------------------------- | |
7452 | -- Add_Implicit_Operator -- | |
7453 | --------------------------- | |
7454 | ||
fbf5a39b AC |
7455 | procedure Add_Implicit_Operator |
7456 | (T : Entity_Id; | |
7457 | Op_Type : Entity_Id := Empty) | |
7458 | is | |
996ae0b0 RK |
7459 | Predef_Op : Entity_Id; |
7460 | ||
7461 | begin | |
7462 | Predef_Op := Current_Entity (Selector_Name (N)); | |
996ae0b0 RK |
7463 | while Present (Predef_Op) |
7464 | and then Scope (Predef_Op) /= Standard_Standard | |
7465 | loop | |
7466 | Predef_Op := Homonym (Predef_Op); | |
7467 | end loop; | |
7468 | ||
7469 | if Nkind (N) = N_Selected_Component then | |
7470 | Change_Selected_Component_To_Expanded_Name (N); | |
7471 | end if; | |
7472 | ||
719aaf4d AC |
7473 | -- If the context is an unanalyzed function call, determine whether |
7474 | -- a binary or unary interpretation is required. | |
996ae0b0 | 7475 | |
719aaf4d AC |
7476 | if Nkind (Parent (N)) = N_Indexed_Component then |
7477 | declare | |
964f13da RD |
7478 | Is_Binary_Call : constant Boolean := |
7479 | Present | |
7480 | (Next (First (Expressions (Parent (N))))); | |
7481 | Is_Binary_Op : constant Boolean := | |
7482 | First_Entity | |
7483 | (Predef_Op) /= Last_Entity (Predef_Op); | |
719aaf4d AC |
7484 | Predef_Op2 : constant Entity_Id := Homonym (Predef_Op); |
7485 | ||
7486 | begin | |
7487 | if Is_Binary_Call then | |
7488 | if Is_Binary_Op then | |
7489 | Add_One_Interp (N, Predef_Op, T); | |
7490 | else | |
7491 | Add_One_Interp (N, Predef_Op2, T); | |
7492 | end if; | |
996ae0b0 | 7493 | |
719aaf4d AC |
7494 | else |
7495 | if not Is_Binary_Op then | |
7496 | Add_One_Interp (N, Predef_Op, T); | |
7497 | else | |
7498 | Add_One_Interp (N, Predef_Op2, T); | |
7499 | end if; | |
7500 | end if; | |
7501 | end; | |
7502 | ||
7503 | else | |
7504 | Add_One_Interp (N, Predef_Op, T); | |
7505 | ||
7506 | -- For operators with unary and binary interpretations, if | |
7507 | -- context is not a call, add both | |
7508 | ||
7509 | if Present (Homonym (Predef_Op)) then | |
7510 | Add_One_Interp (N, Homonym (Predef_Op), T); | |
7511 | end if; | |
996ae0b0 | 7512 | end if; |
fbf5a39b AC |
7513 | |
7514 | -- The node is a reference to a predefined operator, and | |
7515 | -- an implicit reference to the type of its operands. | |
7516 | ||
7517 | if Present (Op_Type) then | |
7518 | Generate_Operator_Reference (N, Op_Type); | |
7519 | else | |
7520 | Generate_Operator_Reference (N, T); | |
7521 | end if; | |
996ae0b0 RK |
7522 | end Add_Implicit_Operator; |
7523 | ||
7524 | -- Start of processing for Has_Implicit_Operator | |
7525 | ||
7526 | begin | |
ac7d724d | 7527 | if Ekind (P) = E_Package and then not In_Open_Scopes (P) then |
996ae0b0 RK |
7528 | Priv_Id := First_Private_Entity (P); |
7529 | end if; | |
7530 | ||
7531 | Id := First_Entity (P); | |
7532 | ||
7533 | case Op_Id is | |
7534 | ||
7535 | -- Boolean operators: an implicit declaration exists if the scope | |
7536 | -- contains a declaration for a derived Boolean type, or for an | |
7537 | -- array of Boolean type. | |
7538 | ||
7539 | when Name_Op_And | Name_Op_Not | Name_Op_Or | Name_Op_Xor => | |
fd22e260 | 7540 | while Id /= Priv_Id loop |
d347f572 | 7541 | if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then |
996ae0b0 RK |
7542 | Add_Implicit_Operator (Id); |
7543 | return True; | |
7544 | end if; | |
7545 | ||
7546 | Next_Entity (Id); | |
7547 | end loop; | |
7548 | ||
bc41faa2 | 7549 | -- Equality: look for any non-limited type (result is Boolean) |
996ae0b0 RK |
7550 | |
7551 | when Name_Op_Eq | Name_Op_Ne => | |
fd22e260 | 7552 | while Id /= Priv_Id loop |
996ae0b0 RK |
7553 | if Is_Type (Id) |
7554 | and then not Is_Limited_Type (Id) | |
d347f572 | 7555 | and then Is_Base_Type (Id) |
996ae0b0 | 7556 | then |
fbf5a39b | 7557 | Add_Implicit_Operator (Standard_Boolean, Id); |
996ae0b0 RK |
7558 | return True; |
7559 | end if; | |
7560 | ||
7561 | Next_Entity (Id); | |
7562 | end loop; | |
7563 | ||
bc41faa2 | 7564 | -- Comparison operators: scalar type, or array of scalar |
996ae0b0 RK |
7565 | |
7566 | when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge => | |
fd22e260 | 7567 | while Id /= Priv_Id loop |
996ae0b0 | 7568 | if (Is_Scalar_Type (Id) |
d347f572 AC |
7569 | or else (Is_Array_Type (Id) |
7570 | and then Is_Scalar_Type (Component_Type (Id)))) | |
7571 | and then Is_Base_Type (Id) | |
996ae0b0 | 7572 | then |
fbf5a39b | 7573 | Add_Implicit_Operator (Standard_Boolean, Id); |
996ae0b0 RK |
7574 | return True; |
7575 | end if; | |
7576 | ||
7577 | Next_Entity (Id); | |
7578 | end loop; | |
7579 | ||
7580 | -- Arithmetic operators: any numeric type | |
7581 | ||
7582 | when Name_Op_Abs | | |
7583 | Name_Op_Add | | |
7584 | Name_Op_Mod | | |
7585 | Name_Op_Rem | | |
7586 | Name_Op_Subtract | | |
7587 | Name_Op_Multiply | | |
7588 | Name_Op_Divide | | |
7589 | Name_Op_Expon => | |
fd22e260 | 7590 | while Id /= Priv_Id loop |
d347f572 | 7591 | if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then |
996ae0b0 RK |
7592 | Add_Implicit_Operator (Id); |
7593 | return True; | |
7594 | end if; | |
7595 | ||
7596 | Next_Entity (Id); | |
7597 | end loop; | |
7598 | ||
7599 | -- Concatenation: any one-dimensional array type | |
7600 | ||
7601 | when Name_Op_Concat => | |
fd22e260 | 7602 | while Id /= Priv_Id loop |
d347f572 AC |
7603 | if Is_Array_Type (Id) |
7604 | and then Number_Dimensions (Id) = 1 | |
7605 | and then Is_Base_Type (Id) | |
996ae0b0 RK |
7606 | then |
7607 | Add_Implicit_Operator (Id); | |
7608 | return True; | |
7609 | end if; | |
7610 | ||
7611 | Next_Entity (Id); | |
7612 | end loop; | |
7613 | ||
7614 | -- What is the others condition here? Should we be using a | |
7615 | -- subtype of Name_Id that would restrict to operators ??? | |
7616 | ||
7617 | when others => null; | |
996ae0b0 RK |
7618 | end case; |
7619 | ||
7620 | -- If we fall through, then we do not have an implicit operator | |
7621 | ||
7622 | return False; | |
7623 | ||
7624 | end Has_Implicit_Operator; | |
7625 | ||
607d0635 AC |
7626 | ----------------------------------- |
7627 | -- Has_Loop_In_Inner_Open_Scopes -- | |
7628 | ----------------------------------- | |
7629 | ||
7630 | function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is | |
7631 | begin | |
7632 | -- Several scope stacks are maintained by Scope_Stack. The base of the | |
7633 | -- currently active scope stack is denoted by the Is_Active_Stack_Base | |
7634 | -- flag in the scope stack entry. Note that the scope stacks used to | |
7635 | -- simply be delimited implicitly by the presence of Standard_Standard | |
7636 | -- at their base, but there now are cases where this is not sufficient | |
7637 | -- because Standard_Standard actually may appear in the middle of the | |
7638 | -- active set of scopes. | |
7639 | ||
7640 | for J in reverse 0 .. Scope_Stack.Last loop | |
8d606a78 RD |
7641 | |
7642 | -- S was reached without seing a loop scope first | |
7643 | ||
607d0635 | 7644 | if Scope_Stack.Table (J).Entity = S then |
607d0635 | 7645 | return False; |
8d606a78 RD |
7646 | |
7647 | -- S was not yet reached, so it contains at least one inner loop | |
7648 | ||
607d0635 | 7649 | elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then |
607d0635 AC |
7650 | return True; |
7651 | end if; | |
7652 | ||
7653 | -- Check Is_Active_Stack_Base to tell us when to stop, as there are | |
7654 | -- cases where Standard_Standard appears in the middle of the active | |
7655 | -- set of scopes. This affects the declaration and overriding of | |
7656 | -- private inherited operations in instantiations of generic child | |
7657 | -- units. | |
7658 | ||
7659 | pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base); | |
7660 | end loop; | |
7661 | ||
2010d078 | 7662 | raise Program_Error; -- unreachable |
607d0635 AC |
7663 | end Has_Loop_In_Inner_Open_Scopes; |
7664 | ||
996ae0b0 RK |
7665 | -------------------- |
7666 | -- In_Open_Scopes -- | |
7667 | -------------------- | |
7668 | ||
7669 | function In_Open_Scopes (S : Entity_Id) return Boolean is | |
7670 | begin | |
923fa078 RD |
7671 | -- Several scope stacks are maintained by Scope_Stack. The base of the |
7672 | -- currently active scope stack is denoted by the Is_Active_Stack_Base | |
7673 | -- flag in the scope stack entry. Note that the scope stacks used to | |
7674 | -- simply be delimited implicitly by the presence of Standard_Standard | |
7675 | -- at their base, but there now are cases where this is not sufficient | |
7676 | -- because Standard_Standard actually may appear in the middle of the | |
7677 | -- active set of scopes. | |
996ae0b0 RK |
7678 | |
7679 | for J in reverse 0 .. Scope_Stack.Last loop | |
7680 | if Scope_Stack.Table (J).Entity = S then | |
7681 | return True; | |
7682 | end if; | |
7683 | ||
923fa078 RD |
7684 | -- Check Is_Active_Stack_Base to tell us when to stop, as there are |
7685 | -- cases where Standard_Standard appears in the middle of the active | |
7686 | -- set of scopes. This affects the declaration and overriding of | |
7687 | -- private inherited operations in instantiations of generic child | |
7688 | -- units. | |
996ae0b0 RK |
7689 | |
7690 | exit when Scope_Stack.Table (J).Is_Active_Stack_Base; | |
7691 | end loop; | |
7692 | ||
7693 | return False; | |
7694 | end In_Open_Scopes; | |
7695 | ||
7696 | ----------------------------- | |
7697 | -- Inherit_Renamed_Profile -- | |
7698 | ----------------------------- | |
7699 | ||
7700 | procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is | |
7701 | New_F : Entity_Id; | |
7702 | Old_F : Entity_Id; | |
7703 | Old_T : Entity_Id; | |
7704 | New_T : Entity_Id; | |
7705 | ||
7706 | begin | |
7707 | if Ekind (Old_S) = E_Operator then | |
996ae0b0 RK |
7708 | New_F := First_Formal (New_S); |
7709 | ||
7710 | while Present (New_F) loop | |
7711 | Set_Etype (New_F, Base_Type (Etype (New_F))); | |
7712 | Next_Formal (New_F); | |
7713 | end loop; | |
7714 | ||
7715 | Set_Etype (New_S, Base_Type (Etype (New_S))); | |
7716 | ||
7717 | else | |
7718 | New_F := First_Formal (New_S); | |
7719 | Old_F := First_Formal (Old_S); | |
7720 | ||
7721 | while Present (New_F) loop | |
7722 | New_T := Etype (New_F); | |
7723 | Old_T := Etype (Old_F); | |
7724 | ||
ed3fe8cc AC |
7725 | -- If the new type is a renaming of the old one, as is the case |
7726 | -- for actuals in instances, retain its name, to simplify later | |
7727 | -- disambiguation. | |
996ae0b0 RK |
7728 | |
7729 | if Nkind (Parent (New_T)) = N_Subtype_Declaration | |
7730 | and then Is_Entity_Name (Subtype_Indication (Parent (New_T))) | |
7731 | and then Entity (Subtype_Indication (Parent (New_T))) = Old_T | |
7732 | then | |
7733 | null; | |
7734 | else | |
7735 | Set_Etype (New_F, Old_T); | |
7736 | end if; | |
7737 | ||
7738 | Next_Formal (New_F); | |
7739 | Next_Formal (Old_F); | |
7740 | end loop; | |
ed3fe8cc | 7741 | |
60aa5228 | 7742 | pragma Assert (No (Old_F)); |
996ae0b0 | 7743 | |
bce79204 | 7744 | if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then |
996ae0b0 RK |
7745 | Set_Etype (New_S, Etype (Old_S)); |
7746 | end if; | |
7747 | end if; | |
7748 | end Inherit_Renamed_Profile; | |
7749 | ||
7750 | ---------------- | |
7751 | -- Initialize -- | |
7752 | ---------------- | |
7753 | ||
7754 | procedure Initialize is | |
7755 | begin | |
7756 | Urefs.Init; | |
7757 | end Initialize; | |
7758 | ||
7759 | ------------------------- | |
7760 | -- Install_Use_Clauses -- | |
7761 | ------------------------- | |
7762 | ||
0da2c8ac AC |
7763 | procedure Install_Use_Clauses |
7764 | (Clause : Node_Id; | |
7765 | Force_Installation : Boolean := False) | |
7766 | is | |
16ca248a | 7767 | U : Node_Id; |
996ae0b0 RK |
7768 | P : Node_Id; |
7769 | Id : Entity_Id; | |
7770 | ||
7771 | begin | |
16ca248a | 7772 | U := Clause; |
996ae0b0 RK |
7773 | while Present (U) loop |
7774 | ||
7775 | -- Case of USE package | |
7776 | ||
7777 | if Nkind (U) = N_Use_Package_Clause then | |
7778 | P := First (Names (U)); | |
996ae0b0 RK |
7779 | while Present (P) loop |
7780 | Id := Entity (P); | |
7781 | ||
7782 | if Ekind (Id) = E_Package then | |
996ae0b0 | 7783 | if In_Use (Id) then |
d4810530 | 7784 | Note_Redundant_Use (P); |
996ae0b0 RK |
7785 | |
7786 | elsif Present (Renamed_Object (Id)) | |
7787 | and then In_Use (Renamed_Object (Id)) | |
7788 | then | |
d4810530 | 7789 | Note_Redundant_Use (P); |
996ae0b0 | 7790 | |
0da2c8ac | 7791 | elsif Force_Installation or else Applicable_Use (P) then |
996ae0b0 | 7792 | Use_One_Package (Id, U); |
0da2c8ac | 7793 | |
996ae0b0 RK |
7794 | end if; |
7795 | end if; | |
7796 | ||
7797 | Next (P); | |
7798 | end loop; | |
7799 | ||
16ca248a | 7800 | -- Case of USE TYPE |
996ae0b0 RK |
7801 | |
7802 | else | |
7803 | P := First (Subtype_Marks (U)); | |
996ae0b0 | 7804 | while Present (P) loop |
fbf5a39b AC |
7805 | if not Is_Entity_Name (P) |
7806 | or else No (Entity (P)) | |
7807 | then | |
7808 | null; | |
996ae0b0 | 7809 | |
fbf5a39b | 7810 | elsif Entity (P) /= Any_Type then |
07fc65c4 | 7811 | Use_One_Type (P); |
996ae0b0 RK |
7812 | end if; |
7813 | ||
7814 | Next (P); | |
7815 | end loop; | |
7816 | end if; | |
7817 | ||
7818 | Next_Use_Clause (U); | |
7819 | end loop; | |
7820 | end Install_Use_Clauses; | |
7821 | ||
7822 | ------------------------------------- | |
7823 | -- Is_Appropriate_For_Entry_Prefix -- | |
7824 | ------------------------------------- | |
7825 | ||
7826 | function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is | |
7827 | P_Type : Entity_Id := T; | |
7828 | ||
7829 | begin | |
7830 | if Is_Access_Type (P_Type) then | |
7831 | P_Type := Designated_Type (P_Type); | |
7832 | end if; | |
7833 | ||
7834 | return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type); | |
7835 | end Is_Appropriate_For_Entry_Prefix; | |
7836 | ||
7837 | ------------------------------- | |
7838 | -- Is_Appropriate_For_Record -- | |
7839 | ------------------------------- | |
7840 | ||
2e071734 AC |
7841 | function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is |
7842 | ||
996ae0b0 RK |
7843 | function Has_Components (T1 : Entity_Id) return Boolean; |
7844 | -- Determine if given type has components (i.e. is either a record | |
7845 | -- type or a type that has discriminants). | |
7846 | ||
16ca248a ES |
7847 | -------------------- |
7848 | -- Has_Components -- | |
7849 | -------------------- | |
7850 | ||
996ae0b0 RK |
7851 | function Has_Components (T1 : Entity_Id) return Boolean is |
7852 | begin | |
7853 | return Is_Record_Type (T1) | |
7854 | or else (Is_Private_Type (T1) and then Has_Discriminants (T1)) | |
16ca248a ES |
7855 | or else (Is_Task_Type (T1) and then Has_Discriminants (T1)) |
7856 | or else (Is_Incomplete_Type (T1) | |
7b56a91b | 7857 | and then From_Limited_With (T1) |
16ca248a | 7858 | and then Present (Non_Limited_View (T1)) |
11560bcc TQ |
7859 | and then Is_Record_Type |
7860 | (Get_Full_View (Non_Limited_View (T1)))); | |
996ae0b0 RK |
7861 | end Has_Components; |
7862 | ||
7863 | -- Start of processing for Is_Appropriate_For_Record | |
7864 | ||
7865 | begin | |
7866 | return | |
7867 | Present (T) | |
7868 | and then (Has_Components (T) | |
16ca248a ES |
7869 | or else (Is_Access_Type (T) |
7870 | and then Has_Components (Designated_Type (T)))); | |
996ae0b0 RK |
7871 | end Is_Appropriate_For_Record; |
7872 | ||
d4810530 ES |
7873 | ------------------------ |
7874 | -- Note_Redundant_Use -- | |
7875 | ------------------------ | |
7876 | ||
7877 | procedure Note_Redundant_Use (Clause : Node_Id) is | |
7878 | Pack_Name : constant Entity_Id := Entity (Clause); | |
7879 | Cur_Use : constant Node_Id := Current_Use_Clause (Pack_Name); | |
7880 | Decl : constant Node_Id := Parent (Clause); | |
7881 | ||
7882 | Prev_Use : Node_Id := Empty; | |
7883 | Redundant : Node_Id := Empty; | |
f7ca1d04 AC |
7884 | -- The Use_Clause which is actually redundant. In the simplest case it |
7885 | -- is Pack itself, but when we compile a body we install its context | |
7886 | -- before that of its spec, in which case it is the use_clause in the | |
7887 | -- spec that will appear to be redundant, and we want the warning to be | |
7888 | -- placed on the body. Similar complications appear when the redundancy | |
7889 | -- is between a child unit and one of its ancestors. | |
d4810530 ES |
7890 | |
7891 | begin | |
7892 | Set_Redundant_Use (Clause, True); | |
7893 | ||
7894 | if not Comes_From_Source (Clause) | |
7895 | or else In_Instance | |
7896 | or else not Warn_On_Redundant_Constructs | |
7897 | then | |
7898 | return; | |
7899 | end if; | |
7900 | ||
7901 | if not Is_Compilation_Unit (Current_Scope) then | |
7902 | ||
f7ca1d04 AC |
7903 | -- If the use_clause is in an inner scope, it is made redundant by |
7904 | -- some clause in the current context, with one exception: If we're | |
7905 | -- compiling a nested package body, and the use_clause comes from the | |
7906 | -- corresponding spec, the clause is not necessarily fully redundant, | |
7907 | -- so we should not warn. If a warning was warranted, it would have | |
7908 | -- been given when the spec was processed. | |
cdc8c54c BD |
7909 | |
7910 | if Nkind (Parent (Decl)) = N_Package_Specification then | |
7911 | declare | |
7912 | Package_Spec_Entity : constant Entity_Id := | |
7913 | Defining_Unit_Name (Parent (Decl)); | |
7914 | begin | |
7915 | if In_Package_Body (Package_Spec_Entity) then | |
7916 | return; | |
7917 | end if; | |
7918 | end; | |
7919 | end if; | |
d4810530 ES |
7920 | |
7921 | Redundant := Clause; | |
7922 | Prev_Use := Cur_Use; | |
7923 | ||
7924 | elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then | |
7925 | declare | |
7926 | Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use); | |
7927 | New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause); | |
7928 | Scop : Entity_Id; | |
7929 | ||
7930 | begin | |
7931 | if Cur_Unit = New_Unit then | |
7932 | ||
7933 | -- Redundant clause in same body | |
7934 | ||
7935 | Redundant := Clause; | |
7936 | Prev_Use := Cur_Use; | |
7937 | ||
7938 | elsif Cur_Unit = Current_Sem_Unit then | |
7939 | ||
7940 | -- If the new clause is not in the current unit it has been | |
7941 | -- analyzed first, and it makes the other one redundant. | |
7942 | -- However, if the new clause appears in a subunit, Cur_Unit | |
7943 | -- is still the parent, and in that case the redundant one | |
7944 | -- is the one appearing in the subunit. | |
7945 | ||
7946 | if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then | |
7947 | Redundant := Clause; | |
7948 | Prev_Use := Cur_Use; | |
7949 | ||
7950 | -- Most common case: redundant clause in body, | |
7951 | -- original clause in spec. Current scope is spec entity. | |
7952 | ||
7953 | elsif | |
7954 | Current_Scope = | |
7955 | Defining_Entity ( | |
7956 | Unit (Library_Unit (Cunit (Current_Sem_Unit)))) | |
7957 | then | |
7958 | Redundant := Cur_Use; | |
7959 | Prev_Use := Clause; | |
7960 | ||
7961 | else | |
7962 | -- The new clause may appear in an unrelated unit, when | |
7963 | -- the parents of a generic are being installed prior to | |
7964 | -- instantiation. In this case there must be no warning. | |
7965 | -- We detect this case by checking whether the current top | |
7966 | -- of the stack is related to the current compilation. | |
7967 | ||
7968 | Scop := Current_Scope; | |
ac7d724d | 7969 | while Present (Scop) and then Scop /= Standard_Standard loop |
d4810530 ES |
7970 | if Is_Compilation_Unit (Scop) |
7971 | and then not Is_Child_Unit (Scop) | |
7972 | then | |
7973 | return; | |
7974 | ||
7975 | elsif Scop = Cunit_Entity (Current_Sem_Unit) then | |
7976 | exit; | |
7977 | end if; | |
7978 | ||
7979 | Scop := Scope (Scop); | |
7980 | end loop; | |
7981 | ||
7982 | Redundant := Cur_Use; | |
7983 | Prev_Use := Clause; | |
7984 | end if; | |
7985 | ||
7986 | elsif New_Unit = Current_Sem_Unit then | |
7987 | Redundant := Clause; | |
7988 | Prev_Use := Cur_Use; | |
7989 | ||
7990 | else | |
7991 | -- Neither is the current unit, so they appear in parent or | |
7992 | -- sibling units. Warning will be emitted elsewhere. | |
7993 | ||
7994 | return; | |
7995 | end if; | |
7996 | end; | |
7997 | ||
7998 | elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration | |
7999 | and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit)))) | |
8000 | then | |
f7ca1d04 AC |
8001 | -- Use_clause is in child unit of current unit, and the child unit |
8002 | -- appears in the context of the body of the parent, so it has been | |
8003 | -- installed first, even though it is the redundant one. Depending on | |
8004 | -- their placement in the context, the visible or the private parts | |
8005 | -- of the two units, either might appear as redundant, but the | |
8006 | -- message has to be on the current unit. | |
d4810530 ES |
8007 | |
8008 | if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then | |
8009 | Redundant := Cur_Use; | |
8010 | Prev_Use := Clause; | |
8011 | else | |
8012 | Redundant := Clause; | |
8013 | Prev_Use := Cur_Use; | |
8014 | end if; | |
8015 | ||
8016 | -- If the new use clause appears in the private part of a parent unit | |
f3d57416 | 8017 | -- it may appear to be redundant w.r.t. a use clause in a child unit, |
d4810530 ES |
8018 | -- but the previous use clause was needed in the visible part of the |
8019 | -- child, and no warning should be emitted. | |
8020 | ||
8021 | if Nkind (Parent (Decl)) = N_Package_Specification | |
8022 | and then | |
8023 | List_Containing (Decl) = Private_Declarations (Parent (Decl)) | |
8024 | then | |
8025 | declare | |
8026 | Par : constant Entity_Id := Defining_Entity (Parent (Decl)); | |
8027 | Spec : constant Node_Id := | |
8028 | Specification (Unit (Cunit (Current_Sem_Unit))); | |
8029 | ||
8030 | begin | |
8031 | if Is_Compilation_Unit (Par) | |
8032 | and then Par /= Cunit_Entity (Current_Sem_Unit) | |
8033 | and then Parent (Cur_Use) = Spec | |
8034 | and then | |
8035 | List_Containing (Cur_Use) = Visible_Declarations (Spec) | |
8036 | then | |
8037 | return; | |
8038 | end if; | |
8039 | end; | |
8040 | end if; | |
8041 | ||
ff81221b ES |
8042 | -- Finally, if the current use clause is in the context then |
8043 | -- the clause is redundant when it is nested within the unit. | |
8044 | ||
8045 | elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit | |
8046 | and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit | |
8047 | and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause) | |
8048 | then | |
8049 | Redundant := Clause; | |
8050 | Prev_Use := Cur_Use; | |
8051 | ||
d4810530 ES |
8052 | else |
8053 | null; | |
8054 | end if; | |
8055 | ||
8056 | if Present (Redundant) then | |
8057 | Error_Msg_Sloc := Sloc (Prev_Use); | |
ed2233dc | 8058 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 8059 | ("& is already use-visible through previous use clause #??", |
954c111a | 8060 | Redundant, Pack_Name); |
d4810530 ES |
8061 | end if; |
8062 | end Note_Redundant_Use; | |
8063 | ||
996ae0b0 RK |
8064 | --------------- |
8065 | -- Pop_Scope -- | |
8066 | --------------- | |
8067 | ||
8068 | procedure Pop_Scope is | |
fbf5a39b | 8069 | SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); |
fab2daeb | 8070 | S : constant Entity_Id := SST.Entity; |
996ae0b0 RK |
8071 | |
8072 | begin | |
8073 | if Debug_Flag_E then | |
8074 | Write_Info; | |
8075 | end if; | |
8076 | ||
fab2daeb AC |
8077 | -- Set Default_Storage_Pool field of the library unit if necessary |
8078 | ||
8079 | if Ekind_In (S, E_Package, E_Generic_Package) | |
8080 | and then | |
8081 | Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit | |
8082 | then | |
8083 | declare | |
8084 | Aux : constant Node_Id := | |
4adf3c50 | 8085 | Aux_Decls_Node (Parent (Unit_Declaration_Node (S))); |
fab2daeb AC |
8086 | begin |
8087 | if No (Default_Storage_Pool (Aux)) then | |
8088 | Set_Default_Storage_Pool (Aux, Default_Pool); | |
8089 | end if; | |
8090 | end; | |
8091 | end if; | |
8092 | ||
21d27997 | 8093 | Scope_Suppress := SST.Save_Scope_Suppress; |
11560bcc | 8094 | Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top; |
21d27997 | 8095 | Check_Policy_List := SST.Save_Check_Policy_List; |
fab2daeb | 8096 | Default_Pool := SST.Save_Default_Storage_Pool; |
49d41397 | 8097 | No_Tagged_Streams := SST.Save_No_Tagged_Streams; |
43417b90 | 8098 | SPARK_Mode := SST.Save_SPARK_Mode; |
579847c2 | 8099 | SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma; |
220d1fd9 | 8100 | Default_SSO := SST.Save_Default_SSO; |
96e90ac1 | 8101 | Uneval_Old := SST.Save_Uneval_Old; |
996ae0b0 RK |
8102 | |
8103 | if Debug_Flag_W then | |
fab2daeb | 8104 | Write_Str ("<-- exiting scope: "); |
996ae0b0 RK |
8105 | Write_Name (Chars (Current_Scope)); |
8106 | Write_Str (", Depth="); | |
8107 | Write_Int (Int (Scope_Stack.Last)); | |
8108 | Write_Eol; | |
8109 | end if; | |
8110 | ||
fbf5a39b | 8111 | End_Use_Clauses (SST.First_Use_Clause); |
996ae0b0 RK |
8112 | |
8113 | -- If the actions to be wrapped are still there they will get lost | |
8114 | -- causing incomplete code to be generated. It is better to abort in | |
fbf5a39b | 8115 | -- this case (and we do the abort even with assertions off since the |
5eeeed5e | 8116 | -- penalty is incorrect code generation). |
996ae0b0 | 8117 | |
36295779 | 8118 | if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then |
46202729 | 8119 | raise Program_Error; |
fbf5a39b | 8120 | end if; |
996ae0b0 RK |
8121 | |
8122 | -- Free last subprogram name if allocated, and pop scope | |
8123 | ||
fbf5a39b | 8124 | Free (SST.Last_Subprogram_Name); |
996ae0b0 RK |
8125 | Scope_Stack.Decrement_Last; |
8126 | end Pop_Scope; | |
8127 | ||
fbe627af RD |
8128 | --------------- |
8129 | -- Push_Scope -- | |
8130 | --------------- | |
8131 | ||
8132 | procedure Push_Scope (S : Entity_Id) is | |
fab2daeb | 8133 | E : constant Entity_Id := Scope (S); |
fbe627af RD |
8134 | |
8135 | begin | |
8136 | if Ekind (S) = E_Void then | |
8137 | null; | |
8138 | ||
f7ca1d04 AC |
8139 | -- Set scope depth if not a non-concurrent type, and we have not yet set |
8140 | -- the scope depth. This means that we have the first occurrence of the | |
8141 | -- scope, and this is where the depth is set. | |
fbe627af RD |
8142 | |
8143 | elsif (not Is_Type (S) or else Is_Concurrent_Type (S)) | |
8144 | and then not Scope_Depth_Set (S) | |
8145 | then | |
8146 | if S = Standard_Standard then | |
8147 | Set_Scope_Depth_Value (S, Uint_0); | |
8148 | ||
8149 | elsif Is_Child_Unit (S) then | |
8150 | Set_Scope_Depth_Value (S, Uint_1); | |
8151 | ||
8152 | elsif not Is_Record_Type (Current_Scope) then | |
8153 | if Ekind (S) = E_Loop then | |
8154 | Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope)); | |
8155 | else | |
8156 | Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1); | |
8157 | end if; | |
8158 | end if; | |
8159 | end if; | |
8160 | ||
8161 | Scope_Stack.Increment_Last; | |
8162 | ||
8163 | declare | |
8164 | SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last); | |
8165 | ||
8166 | begin | |
11560bcc TQ |
8167 | SST.Entity := S; |
8168 | SST.Save_Scope_Suppress := Scope_Suppress; | |
8169 | SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top; | |
21d27997 | 8170 | SST.Save_Check_Policy_List := Check_Policy_List; |
fab2daeb | 8171 | SST.Save_Default_Storage_Pool := Default_Pool; |
49d41397 | 8172 | SST.Save_No_Tagged_Streams := No_Tagged_Streams; |
43417b90 | 8173 | SST.Save_SPARK_Mode := SPARK_Mode; |
579847c2 | 8174 | SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma; |
220d1fd9 | 8175 | SST.Save_Default_SSO := Default_SSO; |
96e90ac1 | 8176 | SST.Save_Uneval_Old := Uneval_Old; |
fbe627af | 8177 | |
194d6f3f JS |
8178 | -- Each new scope pushed onto the scope stack inherits the component |
8179 | -- alignment of the previous scope. This emulates the "visibility" | |
8180 | -- semantics of pragma Component_Alignment. | |
8181 | ||
fbe627af | 8182 | if Scope_Stack.Last > Scope_Stack.First then |
5dc203d2 AC |
8183 | SST.Component_Alignment_Default := |
8184 | Scope_Stack.Table | |
8185 | (Scope_Stack.Last - 1). Component_Alignment_Default; | |
194d6f3f JS |
8186 | |
8187 | -- Otherwise, this is the first scope being pushed on the scope | |
8188 | -- stack. Inherit the component alignment from the configuration | |
8189 | -- form of pragma Component_Alignment (if any). | |
8190 | ||
8191 | else | |
8192 | SST.Component_Alignment_Default := | |
8193 | Configuration_Component_Alignment; | |
fbe627af RD |
8194 | end if; |
8195 | ||
8196 | SST.Last_Subprogram_Name := null; | |
8197 | SST.Is_Transient := False; | |
8198 | SST.Node_To_Be_Wrapped := Empty; | |
8199 | SST.Pending_Freeze_Actions := No_List; | |
36295779 | 8200 | SST.Actions_To_Be_Wrapped := (others => No_List); |
fbe627af RD |
8201 | SST.First_Use_Clause := Empty; |
8202 | SST.Is_Active_Stack_Base := False; | |
8203 | SST.Previous_Visibility := False; | |
8071b771 | 8204 | SST.Locked_Shared_Objects := No_Elist; |
fbe627af RD |
8205 | end; |
8206 | ||
8207 | if Debug_Flag_W then | |
8208 | Write_Str ("--> new scope: "); | |
8209 | Write_Name (Chars (Current_Scope)); | |
8210 | Write_Str (", Id="); | |
8211 | Write_Int (Int (Current_Scope)); | |
8212 | Write_Str (", Depth="); | |
8213 | Write_Int (Int (Scope_Stack.Last)); | |
8214 | Write_Eol; | |
8215 | end if; | |
8216 | ||
f7ca1d04 AC |
8217 | -- Deal with copying flags from the previous scope to this one. This is |
8218 | -- not necessary if either scope is standard, or if the new scope is a | |
8219 | -- child unit. | |
fbe627af RD |
8220 | |
8221 | if S /= Standard_Standard | |
8222 | and then Scope (S) /= Standard_Standard | |
8223 | and then not Is_Child_Unit (S) | |
8224 | then | |
fbe627af RD |
8225 | if Nkind (E) not in N_Entity then |
8226 | return; | |
8227 | end if; | |
8228 | ||
8229 | -- Copy categorization flags from Scope (S) to S, this is not done | |
8230 | -- when Scope (S) is Standard_Standard since propagation is from | |
8231 | -- library unit entity inwards. Copy other relevant attributes as | |
8232 | -- well (Discard_Names in particular). | |
8233 | ||
8234 | -- We only propagate inwards for library level entities, | |
8235 | -- inner level subprograms do not inherit the categorization. | |
8236 | ||
8237 | if Is_Library_Level_Entity (S) then | |
8238 | Set_Is_Preelaborated (S, Is_Preelaborated (E)); | |
8239 | Set_Is_Shared_Passive (S, Is_Shared_Passive (E)); | |
8240 | Set_Discard_Names (S, Discard_Names (E)); | |
8241 | Set_Suppress_Value_Tracking_On_Call | |
8242 | (S, Suppress_Value_Tracking_On_Call (E)); | |
8243 | Set_Categorization_From_Scope (E => S, Scop => E); | |
8244 | end if; | |
8245 | end if; | |
fab2daeb AC |
8246 | |
8247 | if Is_Child_Unit (S) | |
8248 | and then Present (E) | |
8249 | and then Ekind_In (E, E_Package, E_Generic_Package) | |
8250 | and then | |
8251 | Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit | |
8252 | then | |
8253 | declare | |
8254 | Aux : constant Node_Id := | |
4adf3c50 | 8255 | Aux_Decls_Node (Parent (Unit_Declaration_Node (E))); |
fab2daeb AC |
8256 | begin |
8257 | if Present (Default_Storage_Pool (Aux)) then | |
8258 | Default_Pool := Default_Storage_Pool (Aux); | |
8259 | end if; | |
8260 | end; | |
8261 | end if; | |
fbe627af RD |
8262 | end Push_Scope; |
8263 | ||
996ae0b0 RK |
8264 | --------------------- |
8265 | -- Premature_Usage -- | |
8266 | --------------------- | |
8267 | ||
8268 | procedure Premature_Usage (N : Node_Id) is | |
fbf5a39b | 8269 | Kind : constant Node_Kind := Nkind (Parent (Entity (N))); |
996ae0b0 RK |
8270 | E : Entity_Id := Entity (N); |
8271 | ||
8272 | begin | |
8273 | -- Within an instance, the analysis of the actual for a formal object | |
16ca248a ES |
8274 | -- does not see the name of the object itself. This is significant only |
8275 | -- if the object is an aggregate, where its analysis does not do any | |
8276 | -- name resolution on component associations. (see 4717-008). In such a | |
8277 | -- case, look for the visible homonym on the chain. | |
996ae0b0 | 8278 | |
ac7d724d | 8279 | if In_Instance and then Present (Homonym (E)) then |
996ae0b0 | 8280 | E := Homonym (E); |
ac7d724d | 8281 | while Present (E) and then not In_Open_Scopes (Scope (E)) loop |
996ae0b0 RK |
8282 | E := Homonym (E); |
8283 | end loop; | |
8284 | ||
8285 | if Present (E) then | |
8286 | Set_Entity (N, E); | |
8287 | Set_Etype (N, Etype (E)); | |
8288 | return; | |
8289 | end if; | |
8290 | end if; | |
8291 | ||
8292 | if Kind = N_Component_Declaration then | |
8293 | Error_Msg_N | |
8294 | ("component&! cannot be used before end of record declaration", N); | |
8295 | ||
8296 | elsif Kind = N_Parameter_Specification then | |
8297 | Error_Msg_N | |
8298 | ("formal parameter&! cannot be used before end of specification", | |
8299 | N); | |
8300 | ||
8301 | elsif Kind = N_Discriminant_Specification then | |
8302 | Error_Msg_N | |
8303 | ("discriminant&! cannot be used before end of discriminant part", | |
8304 | N); | |
8305 | ||
8306 | elsif Kind = N_Procedure_Specification | |
8307 | or else Kind = N_Function_Specification | |
8308 | then | |
8309 | Error_Msg_N | |
8310 | ("subprogram&! cannot be used before end of its declaration", | |
8311 | N); | |
90067a15 ES |
8312 | |
8313 | elsif Kind = N_Full_Type_Declaration then | |
8314 | Error_Msg_N | |
8315 | ("type& cannot be used before end of its declaration!", N); | |
8316 | ||
996ae0b0 RK |
8317 | else |
8318 | Error_Msg_N | |
8319 | ("object& cannot be used before end of its declaration!", N); | |
8320 | end if; | |
8321 | end Premature_Usage; | |
8322 | ||
8323 | ------------------------ | |
8324 | -- Present_System_Aux -- | |
8325 | ------------------------ | |
8326 | ||
8327 | function Present_System_Aux (N : Node_Id := Empty) return Boolean is | |
8328 | Loc : Source_Ptr; | |
fbe627af | 8329 | Aux_Name : Unit_Name_Type; |
996ae0b0 RK |
8330 | Unum : Unit_Number_Type; |
8331 | Withn : Node_Id; | |
8332 | With_Sys : Node_Id; | |
8333 | The_Unit : Node_Id; | |
8334 | ||
8335 | function Find_System (C_Unit : Node_Id) return Entity_Id; | |
16ca248a | 8336 | -- Scan context clause of compilation unit to find with_clause |
996ae0b0 RK |
8337 | -- for System. |
8338 | ||
2e071734 AC |
8339 | ----------------- |
8340 | -- Find_System -- | |
8341 | ----------------- | |
8342 | ||
996ae0b0 RK |
8343 | function Find_System (C_Unit : Node_Id) return Entity_Id is |
8344 | With_Clause : Node_Id; | |
8345 | ||
8346 | begin | |
8347 | With_Clause := First (Context_Items (C_Unit)); | |
996ae0b0 RK |
8348 | while Present (With_Clause) loop |
8349 | if (Nkind (With_Clause) = N_With_Clause | |
8350 | and then Chars (Name (With_Clause)) = Name_System) | |
8351 | and then Comes_From_Source (With_Clause) | |
8352 | then | |
8353 | return With_Clause; | |
8354 | end if; | |
8355 | ||
8356 | Next (With_Clause); | |
8357 | end loop; | |
8358 | ||
8359 | return Empty; | |
8360 | end Find_System; | |
8361 | ||
8362 | -- Start of processing for Present_System_Aux | |
8363 | ||
8364 | begin | |
bc41faa2 | 8365 | -- The child unit may have been loaded and analyzed already |
996ae0b0 RK |
8366 | |
8367 | if Present (System_Aux_Id) then | |
8368 | return True; | |
8369 | ||
8370 | -- If no previous pragma for System.Aux, nothing to load | |
8371 | ||
fbf5a39b | 8372 | elsif No (System_Extend_Unit) then |
996ae0b0 RK |
8373 | return False; |
8374 | ||
8375 | -- Use the unit name given in the pragma to retrieve the unit. | |
8376 | -- Verify that System itself appears in the context clause of the | |
8377 | -- current compilation. If System is not present, an error will | |
8378 | -- have been reported already. | |
8379 | ||
8380 | else | |
8381 | With_Sys := Find_System (Cunit (Current_Sem_Unit)); | |
8382 | ||
8383 | The_Unit := Unit (Cunit (Current_Sem_Unit)); | |
8384 | ||
8385 | if No (With_Sys) | |
294ccb21 RD |
8386 | and then |
8387 | (Nkind (The_Unit) = N_Package_Body | |
ac7d724d ES |
8388 | or else (Nkind (The_Unit) = N_Subprogram_Body |
8389 | and then not Acts_As_Spec (Cunit (Current_Sem_Unit)))) | |
996ae0b0 RK |
8390 | then |
8391 | With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit))); | |
8392 | end if; | |
8393 | ||
ac7d724d ES |
8394 | if No (With_Sys) and then Present (N) then |
8395 | ||
996ae0b0 RK |
8396 | -- If we are compiling a subunit, we need to examine its |
8397 | -- context as well (Current_Sem_Unit is the parent unit); | |
8398 | ||
8399 | The_Unit := Parent (N); | |
996ae0b0 RK |
8400 | while Nkind (The_Unit) /= N_Compilation_Unit loop |
8401 | The_Unit := Parent (The_Unit); | |
8402 | end loop; | |
8403 | ||
8404 | if Nkind (Unit (The_Unit)) = N_Subunit then | |
8405 | With_Sys := Find_System (The_Unit); | |
8406 | end if; | |
8407 | end if; | |
8408 | ||
8409 | if No (With_Sys) then | |
8410 | return False; | |
8411 | end if; | |
8412 | ||
8413 | Loc := Sloc (With_Sys); | |
fbf5a39b | 8414 | Get_Name_String (Chars (Expression (System_Extend_Unit))); |
996ae0b0 RK |
8415 | Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len); |
8416 | Name_Buffer (1 .. 7) := "system."; | |
8417 | Name_Buffer (Name_Len + 8) := '%'; | |
8418 | Name_Buffer (Name_Len + 9) := 's'; | |
8419 | Name_Len := Name_Len + 9; | |
8420 | Aux_Name := Name_Find; | |
8421 | ||
8422 | Unum := | |
8423 | Load_Unit | |
8424 | (Load_Name => Aux_Name, | |
8425 | Required => False, | |
8426 | Subunit => False, | |
8427 | Error_Node => With_Sys); | |
8428 | ||
8429 | if Unum /= No_Unit then | |
8430 | Semantics (Cunit (Unum)); | |
8431 | System_Aux_Id := | |
8432 | Defining_Entity (Specification (Unit (Cunit (Unum)))); | |
8433 | ||
16ca248a ES |
8434 | Withn := |
8435 | Make_With_Clause (Loc, | |
8436 | Name => | |
8437 | Make_Expanded_Name (Loc, | |
8438 | Chars => Chars (System_Aux_Id), | |
e4494292 RD |
8439 | Prefix => New_Occurrence_Of (Scope (System_Aux_Id), Loc), |
8440 | Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc))); | |
996ae0b0 RK |
8441 | |
8442 | Set_Entity (Name (Withn), System_Aux_Id); | |
8443 | ||
16ca248a ES |
8444 | Set_Library_Unit (Withn, Cunit (Unum)); |
8445 | Set_Corresponding_Spec (Withn, System_Aux_Id); | |
8446 | Set_First_Name (Withn, True); | |
8447 | Set_Implicit_With (Withn, True); | |
996ae0b0 RK |
8448 | |
8449 | Insert_After (With_Sys, Withn); | |
8450 | Mark_Rewrite_Insertion (Withn); | |
8451 | Set_Context_Installed (Withn); | |
8452 | ||
8453 | return True; | |
8454 | ||
8455 | -- Here if unit load failed | |
8456 | ||
8457 | else | |
8458 | Error_Msg_Name_1 := Name_System; | |
fbf5a39b | 8459 | Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit)); |
996ae0b0 RK |
8460 | Error_Msg_N |
8461 | ("extension package `%.%` does not exist", | |
fbf5a39b | 8462 | Opt.System_Extend_Unit); |
996ae0b0 RK |
8463 | return False; |
8464 | end if; | |
8465 | end if; | |
8466 | end Present_System_Aux; | |
8467 | ||
8468 | ------------------------- | |
8469 | -- Restore_Scope_Stack -- | |
8470 | ------------------------- | |
8471 | ||
e530a2d1 AC |
8472 | procedure Restore_Scope_Stack |
8473 | (List : Elist_Id; | |
8474 | Handle_Use : Boolean := True) | |
8475 | is | |
8476 | SS_Last : constant Int := Scope_Stack.Last; | |
8477 | Elmt : Elmt_Id; | |
996ae0b0 RK |
8478 | |
8479 | begin | |
9e40f163 AC |
8480 | -- Restore visibility of previous scope stack, if any, using the list |
8481 | -- we saved (we use Remove, since this list will not be used again). | |
996ae0b0 | 8482 | |
9e40f163 AC |
8483 | loop |
8484 | Elmt := Last_Elmt (List); | |
8485 | exit when Elmt = No_Elmt; | |
e530a2d1 | 8486 | Set_Is_Immediately_Visible (Node (Elmt)); |
9e40f163 | 8487 | Remove_Last_Elmt (List); |
996ae0b0 | 8488 | end loop; |
fbf5a39b | 8489 | |
9e40f163 AC |
8490 | -- Restore use clauses |
8491 | ||
fbf5a39b AC |
8492 | if SS_Last >= Scope_Stack.First |
8493 | and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard | |
ecad994d | 8494 | and then Handle_Use |
fbf5a39b AC |
8495 | then |
8496 | Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause); | |
8497 | end if; | |
996ae0b0 RK |
8498 | end Restore_Scope_Stack; |
8499 | ||
8500 | ---------------------- | |
8501 | -- Save_Scope_Stack -- | |
8502 | ---------------------- | |
8503 | ||
9e40f163 AC |
8504 | -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid |
8505 | -- consuming any memory. That is, Save_Scope_Stack took care of removing | |
8506 | -- from immediate visibility entities and Restore_Scope_Stack took care | |
8507 | -- of restoring their visibility analyzing the context of each entity. The | |
8508 | -- problem of such approach is that it was fragile and caused unexpected | |
8509 | -- visibility problems, and indeed one test was found where there was a | |
8510 | -- real problem. | |
8511 | ||
8512 | -- Furthermore, the following experiment was carried out: | |
8513 | ||
8514 | -- - Save_Scope_Stack was modified to store in an Elist1 all those | |
8515 | -- entities whose attribute Is_Immediately_Visible is modified | |
8516 | -- from True to False. | |
8517 | ||
8518 | -- - Restore_Scope_Stack was modified to store in another Elist2 | |
8519 | -- all the entities whose attribute Is_Immediately_Visible is | |
8520 | -- modified from False to True. | |
8521 | ||
8522 | -- - Extra code was added to verify that all the elements of Elist1 | |
8523 | -- are found in Elist2 | |
8524 | ||
a35017dc | 8525 | -- This test shows that there may be more occurrences of this problem which |
9e40f163 AC |
8526 | -- have not yet been detected. As a result, we replaced that approach by |
8527 | -- the current one in which Save_Scope_Stack returns the list of entities | |
a35017dc | 8528 | -- whose visibility is changed, and that list is passed to Restore_Scope_ |
9e40f163 AC |
8529 | -- Stack to undo that change. This approach is simpler and safer, although |
8530 | -- it consumes more memory. | |
8531 | ||
e530a2d1 AC |
8532 | function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is |
8533 | Result : constant Elist_Id := New_Elmt_List; | |
996ae0b0 RK |
8534 | E : Entity_Id; |
8535 | S : Entity_Id; | |
8536 | SS_Last : constant Int := Scope_Stack.Last; | |
8537 | ||
e530a2d1 AC |
8538 | procedure Remove_From_Visibility (E : Entity_Id); |
8539 | -- If E is immediately visible then append it to the result and remove | |
85f6a831 RD |
8540 | -- it temporarily from visibility. |
8541 | ||
8542 | ---------------------------- | |
8543 | -- Remove_From_Visibility -- | |
8544 | ---------------------------- | |
8545 | ||
e530a2d1 AC |
8546 | procedure Remove_From_Visibility (E : Entity_Id) is |
8547 | begin | |
8548 | if Is_Immediately_Visible (E) then | |
8549 | Append_Elmt (E, Result); | |
8550 | Set_Is_Immediately_Visible (E, False); | |
8551 | end if; | |
8552 | end Remove_From_Visibility; | |
8553 | ||
85f6a831 RD |
8554 | -- Start of processing for Save_Scope_Stack |
8555 | ||
996ae0b0 RK |
8556 | begin |
8557 | if SS_Last >= Scope_Stack.First | |
8558 | and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard | |
8559 | then | |
ecad994d AC |
8560 | if Handle_Use then |
8561 | End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause); | |
8562 | end if; | |
fbf5a39b | 8563 | |
16ca248a ES |
8564 | -- If the call is from within a compilation unit, as when called from |
8565 | -- Rtsfind, make current entries in scope stack invisible while we | |
8566 | -- analyze the new unit. | |
996ae0b0 RK |
8567 | |
8568 | for J in reverse 0 .. SS_Last loop | |
8569 | exit when Scope_Stack.Table (J).Entity = Standard_Standard | |
8570 | or else No (Scope_Stack.Table (J).Entity); | |
8571 | ||
8572 | S := Scope_Stack.Table (J).Entity; | |
e530a2d1 AC |
8573 | |
8574 | Remove_From_Visibility (S); | |
996ae0b0 | 8575 | |
16ca248a | 8576 | E := First_Entity (S); |
996ae0b0 | 8577 | while Present (E) loop |
e530a2d1 | 8578 | Remove_From_Visibility (E); |
996ae0b0 RK |
8579 | Next_Entity (E); |
8580 | end loop; | |
8581 | end loop; | |
8582 | ||
8583 | end if; | |
e530a2d1 AC |
8584 | |
8585 | return Result; | |
996ae0b0 RK |
8586 | end Save_Scope_Stack; |
8587 | ||
8588 | ------------- | |
8589 | -- Set_Use -- | |
8590 | ------------- | |
8591 | ||
8592 | procedure Set_Use (L : List_Id) is | |
8593 | Decl : Node_Id; | |
8594 | Pack_Name : Node_Id; | |
8595 | Pack : Entity_Id; | |
8596 | Id : Entity_Id; | |
8597 | ||
8598 | begin | |
8599 | if Present (L) then | |
8600 | Decl := First (L); | |
996ae0b0 RK |
8601 | while Present (Decl) loop |
8602 | if Nkind (Decl) = N_Use_Package_Clause then | |
8603 | Chain_Use_Clause (Decl); | |
996ae0b0 | 8604 | |
16ca248a | 8605 | Pack_Name := First (Names (Decl)); |
996ae0b0 RK |
8606 | while Present (Pack_Name) loop |
8607 | Pack := Entity (Pack_Name); | |
8608 | ||
8609 | if Ekind (Pack) = E_Package | |
8610 | and then Applicable_Use (Pack_Name) | |
8611 | then | |
8612 | Use_One_Package (Pack, Decl); | |
8613 | end if; | |
8614 | ||
8615 | Next (Pack_Name); | |
8616 | end loop; | |
8617 | ||
bcb0389e | 8618 | elsif Nkind (Decl) = N_Use_Type_Clause then |
996ae0b0 | 8619 | Chain_Use_Clause (Decl); |
996ae0b0 | 8620 | |
16ca248a | 8621 | Id := First (Subtype_Marks (Decl)); |
996ae0b0 RK |
8622 | while Present (Id) loop |
8623 | if Entity (Id) /= Any_Type then | |
07fc65c4 | 8624 | Use_One_Type (Id); |
996ae0b0 RK |
8625 | end if; |
8626 | ||
8627 | Next (Id); | |
8628 | end loop; | |
8629 | end if; | |
8630 | ||
8631 | Next (Decl); | |
8632 | end loop; | |
8633 | end if; | |
8634 | end Set_Use; | |
8635 | ||
8636 | --------------------- | |
8637 | -- Use_One_Package -- | |
8638 | --------------------- | |
8639 | ||
8640 | procedure Use_One_Package (P : Entity_Id; N : Node_Id) is | |
8641 | Id : Entity_Id; | |
8642 | Prev : Entity_Id; | |
8643 | Current_Instance : Entity_Id := Empty; | |
8644 | Real_P : Entity_Id; | |
9bc856dd | 8645 | Private_With_OK : Boolean := False; |
996ae0b0 RK |
8646 | |
8647 | begin | |
8648 | if Ekind (P) /= E_Package then | |
8649 | return; | |
8650 | end if; | |
8651 | ||
8652 | Set_In_Use (P); | |
d4810530 | 8653 | Set_Current_Use_Clause (P, N); |
996ae0b0 | 8654 | |
0ab80019 | 8655 | -- Ada 2005 (AI-50217): Check restriction |
19f0526a | 8656 | |
7b56a91b | 8657 | if From_Limited_With (P) then |
657a9dd9 | 8658 | Error_Msg_N ("limited withed package cannot appear in use clause", N); |
996ae0b0 RK |
8659 | end if; |
8660 | ||
bc41faa2 | 8661 | -- Find enclosing instance, if any |
996ae0b0 RK |
8662 | |
8663 | if In_Instance then | |
8664 | Current_Instance := Current_Scope; | |
996ae0b0 RK |
8665 | while not Is_Generic_Instance (Current_Instance) loop |
8666 | Current_Instance := Scope (Current_Instance); | |
8667 | end loop; | |
8668 | ||
8669 | if No (Hidden_By_Use_Clause (N)) then | |
8670 | Set_Hidden_By_Use_Clause (N, New_Elmt_List); | |
8671 | end if; | |
8672 | end if; | |
8673 | ||
8674 | -- If unit is a package renaming, indicate that the renamed | |
8675 | -- package is also in use (the flags on both entities must | |
8676 | -- remain consistent, and a subsequent use of either of them | |
8677 | -- should be recognized as redundant). | |
8678 | ||
8679 | if Present (Renamed_Object (P)) then | |
8680 | Set_In_Use (Renamed_Object (P)); | |
d4810530 | 8681 | Set_Current_Use_Clause (Renamed_Object (P), N); |
996ae0b0 RK |
8682 | Real_P := Renamed_Object (P); |
8683 | else | |
8684 | Real_P := P; | |
8685 | end if; | |
8686 | ||
0ab80019 | 8687 | -- Ada 2005 (AI-262): Check the use_clause of a private withed package |
9bc856dd AC |
8688 | -- found in the private part of a package specification |
8689 | ||
8690 | if In_Private_Part (Current_Scope) | |
8691 | and then Has_Private_With (P) | |
8692 | and then Is_Child_Unit (Current_Scope) | |
8693 | and then Is_Child_Unit (P) | |
8694 | and then Is_Ancestor_Package (Scope (Current_Scope), P) | |
8695 | then | |
8696 | Private_With_OK := True; | |
8697 | end if; | |
8698 | ||
996ae0b0 RK |
8699 | -- Loop through entities in one package making them potentially |
8700 | -- use-visible. | |
8701 | ||
8702 | Id := First_Entity (P); | |
8703 | while Present (Id) | |
9bc856dd | 8704 | and then (Id /= First_Private_Entity (P) |
ac7d724d | 8705 | or else Private_With_OK) -- Ada 2005 (AI-262) |
996ae0b0 RK |
8706 | loop |
8707 | Prev := Current_Entity (Id); | |
996ae0b0 RK |
8708 | while Present (Prev) loop |
8709 | if Is_Immediately_Visible (Prev) | |
8710 | and then (not Is_Overloadable (Prev) | |
8711 | or else not Is_Overloadable (Id) | |
8712 | or else (Type_Conformant (Id, Prev))) | |
8713 | then | |
8714 | if No (Current_Instance) then | |
8715 | ||
8716 | -- Potentially use-visible entity remains hidden | |
8717 | ||
8718 | goto Next_Usable_Entity; | |
8719 | ||
16ca248a ES |
8720 | -- A use clause within an instance hides outer global entities, |
8721 | -- which are not used to resolve local entities in the | |
8722 | -- instance. Note that the predefined entities in Standard | |
8723 | -- could not have been hidden in the generic by a use clause, | |
8724 | -- and therefore remain visible. Other compilation units whose | |
8725 | -- entities appear in Standard must be hidden in an instance. | |
996ae0b0 RK |
8726 | |
8727 | -- To determine whether an entity is external to the instance | |
8728 | -- we compare the scope depth of its scope with that of the | |
8729 | -- current instance. However, a generic actual of a subprogram | |
8730 | -- instance is declared in the wrapper package but will not be | |
e074d476 AC |
8731 | -- hidden by a use-visible entity. similarly, an entity that is |
8732 | -- declared in an enclosing instance will not be hidden by an | |
8733 | -- an entity declared in a generic actual, which can only have | |
8734 | -- been use-visible in the generic and will not have hidden the | |
8735 | -- entity in the generic parent. | |
996ae0b0 | 8736 | |
82c80734 RD |
8737 | -- If Id is called Standard, the predefined package with the |
8738 | -- same name is in the homonym chain. It has to be ignored | |
8739 | -- because it has no defined scope (being the only entity in | |
8740 | -- the system with this mandated behavior). | |
8741 | ||
996ae0b0 | 8742 | elsif not Is_Hidden (Id) |
82c80734 | 8743 | and then Present (Scope (Prev)) |
996ae0b0 RK |
8744 | and then not Is_Wrapper_Package (Scope (Prev)) |
8745 | and then Scope_Depth (Scope (Prev)) < | |
8746 | Scope_Depth (Current_Instance) | |
8747 | and then (Scope (Prev) /= Standard_Standard | |
8748 | or else Sloc (Prev) > Standard_Location) | |
8749 | then | |
e074d476 AC |
8750 | if In_Open_Scopes (Scope (Prev)) |
8751 | and then Is_Generic_Instance (Scope (Prev)) | |
c94a0b9d AC |
8752 | and then Present (Associated_Formal_Package (P)) |
8753 | then | |
8754 | null; | |
8755 | ||
8756 | else | |
8757 | Set_Is_Potentially_Use_Visible (Id); | |
8758 | Set_Is_Immediately_Visible (Prev, False); | |
8759 | Append_Elmt (Prev, Hidden_By_Use_Clause (N)); | |
8760 | end if; | |
996ae0b0 RK |
8761 | end if; |
8762 | ||
16ca248a ES |
8763 | -- A user-defined operator is not use-visible if the predefined |
8764 | -- operator for the type is immediately visible, which is the case | |
8765 | -- if the type of the operand is in an open scope. This does not | |
8766 | -- apply to user-defined operators that have operands of different | |
8767 | -- types, because the predefined mixed mode operations (multiply | |
8768 | -- and divide) apply to universal types and do not hide anything. | |
996ae0b0 RK |
8769 | |
8770 | elsif Ekind (Prev) = E_Operator | |
8771 | and then Operator_Matches_Spec (Prev, Id) | |
8772 | and then In_Open_Scopes | |
ac7d724d | 8773 | (Scope (Base_Type (Etype (First_Formal (Id))))) |
996ae0b0 | 8774 | and then (No (Next_Formal (First_Formal (Id))) |
ac7d724d ES |
8775 | or else Etype (First_Formal (Id)) = |
8776 | Etype (Next_Formal (First_Formal (Id))) | |
996ae0b0 RK |
8777 | or else Chars (Prev) = Name_Op_Expon) |
8778 | then | |
8779 | goto Next_Usable_Entity; | |
e69614ad AC |
8780 | |
8781 | -- In an instance, two homonyms may become use_visible through the | |
8782 | -- actuals of distinct formal packages. In the generic, only the | |
8783 | -- current one would have been visible, so make the other one | |
8784 | -- not use_visible. | |
8785 | ||
8786 | elsif Present (Current_Instance) | |
8787 | and then Is_Potentially_Use_Visible (Prev) | |
8788 | and then not Is_Overloadable (Prev) | |
8789 | and then Scope (Id) /= Scope (Prev) | |
8790 | and then Used_As_Generic_Actual (Scope (Prev)) | |
8791 | and then Used_As_Generic_Actual (Scope (Id)) | |
30196a76 RD |
8792 | and then not In_Same_List (Current_Use_Clause (Scope (Prev)), |
8793 | Current_Use_Clause (Scope (Id))) | |
e69614ad AC |
8794 | then |
8795 | Set_Is_Potentially_Use_Visible (Prev, False); | |
8796 | Append_Elmt (Prev, Hidden_By_Use_Clause (N)); | |
996ae0b0 RK |
8797 | end if; |
8798 | ||
8799 | Prev := Homonym (Prev); | |
8800 | end loop; | |
8801 | ||
bc41faa2 | 8802 | -- On exit, we know entity is not hidden, unless it is private |
996ae0b0 RK |
8803 | |
8804 | if not Is_Hidden (Id) | |
ac7d724d | 8805 | and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id)) |
996ae0b0 RK |
8806 | then |
8807 | Set_Is_Potentially_Use_Visible (Id); | |
8808 | ||
ac7d724d | 8809 | if Is_Private_Type (Id) and then Present (Full_View (Id)) then |
996ae0b0 RK |
8810 | Set_Is_Potentially_Use_Visible (Full_View (Id)); |
8811 | end if; | |
8812 | end if; | |
8813 | ||
8814 | <<Next_Usable_Entity>> | |
8815 | Next_Entity (Id); | |
8816 | end loop; | |
8817 | ||
16ca248a ES |
8818 | -- Child units are also made use-visible by a use clause, but they may |
8819 | -- appear after all visible declarations in the parent entity list. | |
996ae0b0 RK |
8820 | |
8821 | while Present (Id) loop | |
8398e82e | 8822 | if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then |
996ae0b0 RK |
8823 | Set_Is_Potentially_Use_Visible (Id); |
8824 | end if; | |
8825 | ||
8826 | Next_Entity (Id); | |
8827 | end loop; | |
8828 | ||
8829 | if Chars (Real_P) = Name_System | |
8830 | and then Scope (Real_P) = Standard_Standard | |
8831 | and then Present_System_Aux (N) | |
8832 | then | |
8833 | Use_One_Package (System_Aux_Id, N); | |
8834 | end if; | |
8835 | ||
8836 | end Use_One_Package; | |
8837 | ||
8838 | ------------------ | |
8839 | -- Use_One_Type -- | |
8840 | ------------------ | |
8841 | ||
7ff2d234 | 8842 | procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False) is |
954c111a HK |
8843 | Elmt : Elmt_Id; |
8844 | Is_Known_Used : Boolean; | |
8845 | Op_List : Elist_Id; | |
8846 | T : Entity_Id; | |
8847 | ||
8848 | function Spec_Reloaded_For_Body return Boolean; | |
8849 | -- Determine whether the compilation unit is a package body and the use | |
8850 | -- type clause is in the spec of the same package. Even though the spec | |
8851 | -- was analyzed first, its context is reloaded when analysing the body. | |
8852 | ||
29efbb8c ES |
8853 | procedure Use_Class_Wide_Operations (Typ : Entity_Id); |
8854 | -- AI05-150: if the use_type_clause carries the "all" qualifier, | |
8855 | -- class-wide operations of ancestor types are use-visible if the | |
8856 | -- ancestor type is visible. | |
8857 | ||
954c111a HK |
8858 | ---------------------------- |
8859 | -- Spec_Reloaded_For_Body -- | |
8860 | ---------------------------- | |
8861 | ||
8862 | function Spec_Reloaded_For_Body return Boolean is | |
8863 | begin | |
8864 | if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then | |
8865 | declare | |
8866 | Spec : constant Node_Id := | |
8867 | Parent (List_Containing (Parent (Id))); | |
f146302c | 8868 | |
d99ff0f4 | 8869 | begin |
f146302c AC |
8870 | -- Check whether type is declared in a package specification, |
8871 | -- and current unit is the corresponding package body. The | |
8872 | -- use clauses themselves may be within a nested package. | |
8873 | ||
954c111a HK |
8874 | return |
8875 | Nkind (Spec) = N_Package_Specification | |
d99ff0f4 AC |
8876 | and then |
8877 | In_Same_Source_Unit (Corresponding_Body (Parent (Spec)), | |
8878 | Cunit_Entity (Current_Sem_Unit)); | |
954c111a HK |
8879 | end; |
8880 | end if; | |
8881 | ||
8882 | return False; | |
8883 | end Spec_Reloaded_For_Body; | |
8884 | ||
29efbb8c ES |
8885 | ------------------------------- |
8886 | -- Use_Class_Wide_Operations -- | |
8887 | ------------------------------- | |
8888 | ||
8889 | procedure Use_Class_Wide_Operations (Typ : Entity_Id) is | |
8890 | Scop : Entity_Id; | |
8891 | Ent : Entity_Id; | |
8892 | ||
8893 | function Is_Class_Wide_Operation_Of | |
8894 | (Op : Entity_Id; | |
8895 | T : Entity_Id) return Boolean; | |
8896 | -- Determine whether a subprogram has a class-wide parameter or | |
8897 | -- result that is T'Class. | |
8898 | ||
8899 | --------------------------------- | |
8900 | -- Is_Class_Wide_Operation_Of -- | |
8901 | --------------------------------- | |
8902 | ||
8903 | function Is_Class_Wide_Operation_Of | |
8904 | (Op : Entity_Id; | |
8905 | T : Entity_Id) return Boolean | |
8906 | is | |
8907 | Formal : Entity_Id; | |
8908 | ||
8909 | begin | |
8910 | Formal := First_Formal (Op); | |
8911 | while Present (Formal) loop | |
8912 | if Etype (Formal) = Class_Wide_Type (T) then | |
8913 | return True; | |
8914 | end if; | |
8915 | Next_Formal (Formal); | |
8916 | end loop; | |
8917 | ||
8918 | if Etype (Op) = Class_Wide_Type (T) then | |
8919 | return True; | |
8920 | end if; | |
8921 | ||
8922 | return False; | |
8923 | end Is_Class_Wide_Operation_Of; | |
8924 | ||
8925 | -- Start of processing for Use_Class_Wide_Operations | |
8926 | ||
8927 | begin | |
8928 | Scop := Scope (Typ); | |
8929 | if not Is_Hidden (Scop) then | |
8930 | Ent := First_Entity (Scop); | |
8931 | while Present (Ent) loop | |
8932 | if Is_Overloadable (Ent) | |
8933 | and then Is_Class_Wide_Operation_Of (Ent, Typ) | |
8934 | and then not Is_Potentially_Use_Visible (Ent) | |
8935 | then | |
8936 | Set_Is_Potentially_Use_Visible (Ent); | |
8937 | Append_Elmt (Ent, Used_Operations (Parent (Id))); | |
8938 | end if; | |
8939 | ||
8940 | Next_Entity (Ent); | |
8941 | end loop; | |
8942 | end if; | |
8943 | ||
8944 | if Is_Derived_Type (Typ) then | |
8945 | Use_Class_Wide_Operations (Etype (Base_Type (Typ))); | |
8946 | end if; | |
8947 | end Use_Class_Wide_Operations; | |
8948 | ||
dea1d3dc | 8949 | -- Start of processing for Use_One_Type |
996ae0b0 RK |
8950 | |
8951 | begin | |
8952 | -- It is the type determined by the subtype mark (8.4(8)) whose | |
8953 | -- operations become potentially use-visible. | |
8954 | ||
8955 | T := Base_Type (Entity (Id)); | |
8956 | ||
954c111a HK |
8957 | -- Either the type itself is used, the package where it is declared |
8958 | -- is in use or the entity is declared in the current package, thus | |
8959 | -- use-visible. | |
8960 | ||
8961 | Is_Known_Used := | |
8962 | In_Use (T) | |
8963 | or else In_Use (Scope (T)) | |
8964 | or else Scope (T) = Current_Scope; | |
8965 | ||
8966 | Set_Redundant_Use (Id, | |
8967 | Is_Known_Used or else Is_Potentially_Use_Visible (T)); | |
996ae0b0 | 8968 | |
ecc4ddde AC |
8969 | if Ekind (T) = E_Incomplete_Type then |
8970 | Error_Msg_N ("premature usage of incomplete type", Id); | |
8971 | ||
8972 | elsif In_Open_Scopes (Scope (T)) then | |
996ae0b0 RK |
8973 | null; |
8974 | ||
f7ca1d04 AC |
8975 | -- A limited view cannot appear in a use_type clause. However, an access |
8976 | -- type whose designated type is limited has the flag but is not itself | |
8977 | -- a limited view unless we only have a limited view of its enclosing | |
8978 | -- package. | |
294ccb21 | 8979 | |
7b56a91b | 8980 | elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then |
923fa078 RD |
8981 | Error_Msg_N |
8982 | ("incomplete type from limited view " | |
ac7d724d | 8983 | & "cannot appear in use clause", Id); |
923fa078 | 8984 | |
fbf5a39b AC |
8985 | -- If the subtype mark designates a subtype in a different package, |
8986 | -- we have to check that the parent type is visible, otherwise the | |
8987 | -- use type clause is a noop. Not clear how to do that??? | |
8988 | ||
996ae0b0 RK |
8989 | elsif not Redundant_Use (Id) then |
8990 | Set_In_Use (T); | |
c3b36d48 AC |
8991 | |
8992 | -- If T is tagged, primitive operators on class-wide operands | |
8993 | -- are also available. | |
8994 | ||
8995 | if Is_Tagged_Type (T) then | |
8996 | Set_In_Use (Class_Wide_Type (T)); | |
8997 | end if; | |
8998 | ||
21d27997 | 8999 | Set_Current_Use_Clause (T, Parent (Id)); |
996ae0b0 | 9000 | |
29efbb8c ES |
9001 | -- Iterate over primitive operations of the type. If an operation is |
9002 | -- already use_visible, it is the result of a previous use_clause, | |
7ff2d234 AC |
9003 | -- and already appears on the corresponding entity chain. If the |
9004 | -- clause is being reinstalled, operations are already use-visible. | |
29efbb8c | 9005 | |
7ff2d234 AC |
9006 | if Installed then |
9007 | null; | |
29efbb8c | 9008 | |
7ff2d234 AC |
9009 | else |
9010 | Op_List := Collect_Primitive_Operations (T); | |
9011 | Elmt := First_Elmt (Op_List); | |
9012 | while Present (Elmt) loop | |
9013 | if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol | |
9014 | or else Chars (Node (Elmt)) in Any_Operator_Name) | |
9015 | and then not Is_Hidden (Node (Elmt)) | |
9016 | and then not Is_Potentially_Use_Visible (Node (Elmt)) | |
9017 | then | |
9018 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
9019 | Append_Elmt (Node (Elmt), Used_Operations (Parent (Id))); | |
996ae0b0 | 9020 | |
7ff2d234 AC |
9021 | elsif Ada_Version >= Ada_2012 |
9022 | and then All_Present (Parent (Id)) | |
9023 | and then not Is_Hidden (Node (Elmt)) | |
9024 | and then not Is_Potentially_Use_Visible (Node (Elmt)) | |
9025 | then | |
9026 | Set_Is_Potentially_Use_Visible (Node (Elmt)); | |
9027 | Append_Elmt (Node (Elmt), Used_Operations (Parent (Id))); | |
9028 | end if; | |
954c111a | 9029 | |
7ff2d234 AC |
9030 | Next_Elmt (Elmt); |
9031 | end loop; | |
9032 | end if; | |
9033 | ||
9034 | if Ada_Version >= Ada_2012 | |
9035 | and then All_Present (Parent (Id)) | |
9036 | and then Is_Tagged_Type (T) | |
9037 | then | |
9038 | Use_Class_Wide_Operations (T); | |
9039 | end if; | |
29efbb8c ES |
9040 | end if; |
9041 | ||
954c111a HK |
9042 | -- If warning on redundant constructs, check for unnecessary WITH |
9043 | ||
9044 | if Warn_On_Redundant_Constructs | |
9045 | and then Is_Known_Used | |
9046 | ||
ac7d724d ES |
9047 | -- with P; with P; use P; |
9048 | -- package P is package X is package body X is | |
9049 | -- type T ... use P.T; | |
954c111a | 9050 | |
ac7d724d ES |
9051 | -- The compilation unit is the body of X. GNAT first compiles the |
9052 | -- spec of X, then proceeds to the body. At that point P is marked | |
9053 | -- as use visible. The analysis then reinstalls the spec along with | |
9054 | -- its context. The use clause P.T is now recognized as redundant, | |
9055 | -- but in the wrong context. Do not emit a warning in such cases. | |
9056 | -- Do not emit a warning either if we are in an instance, there is | |
9057 | -- no redundancy between an outer use_clause and one that appears | |
9058 | -- within the generic. | |
954c111a HK |
9059 | |
9060 | and then not Spec_Reloaded_For_Body | |
c56094bd | 9061 | and then not In_Instance |
954c111a HK |
9062 | then |
9063 | -- The type already has a use clause | |
9064 | ||
9065 | if In_Use (T) then | |
3ea52b2e ES |
9066 | |
9067 | -- Case where we know the current use clause for the type | |
9068 | ||
21d27997 | 9069 | if Present (Current_Use_Clause (T)) then |
44b90160 | 9070 | Use_Clause_Known : declare |
21d27997 RD |
9071 | Clause1 : constant Node_Id := Parent (Id); |
9072 | Clause2 : constant Node_Id := Current_Use_Clause (T); | |
3ea52b2e ES |
9073 | Ent1 : Entity_Id; |
9074 | Ent2 : Entity_Id; | |
21d27997 RD |
9075 | Err_No : Node_Id; |
9076 | Unit1 : Node_Id; | |
9077 | Unit2 : Node_Id; | |
9078 | ||
7ca139d3 TQ |
9079 | function Entity_Of_Unit (U : Node_Id) return Entity_Id; |
9080 | -- Return the appropriate entity for determining which unit | |
9081 | -- has a deeper scope: the defining entity for U, unless U | |
9082 | -- is a package instance, in which case we retrieve the | |
9083 | -- entity of the instance spec. | |
9084 | ||
9085 | -------------------- | |
9086 | -- Entity_Of_Unit -- | |
9087 | -------------------- | |
9088 | ||
9089 | function Entity_Of_Unit (U : Node_Id) return Entity_Id is | |
9090 | begin | |
1b1d88b1 | 9091 | if Nkind (U) = N_Package_Instantiation |
7ca139d3 TQ |
9092 | and then Analyzed (U) |
9093 | then | |
9094 | return Defining_Entity (Instance_Spec (U)); | |
9095 | else | |
9096 | return Defining_Entity (U); | |
9097 | end if; | |
9098 | end Entity_Of_Unit; | |
9099 | ||
44b90160 RD |
9100 | -- Start of processing for Use_Clause_Known |
9101 | ||
21d27997 | 9102 | begin |
f7ca1d04 AC |
9103 | -- If both current use type clause and the use type clause |
9104 | -- for the type are at the compilation unit level, one of | |
9105 | -- the units must be an ancestor of the other, and the | |
9106 | -- warning belongs on the descendant. | |
3ea52b2e | 9107 | |
21d27997 | 9108 | if Nkind (Parent (Clause1)) = N_Compilation_Unit |
3ea52b2e ES |
9109 | and then |
9110 | Nkind (Parent (Clause2)) = N_Compilation_Unit | |
21d27997 | 9111 | then |
b0d3b11d AC |
9112 | -- If the unit is a subprogram body that acts as spec, |
9113 | -- the context clause is shared with the constructed | |
9114 | -- subprogram spec. Clearly there is no redundancy. | |
9115 | ||
9116 | if Clause1 = Clause2 then | |
9117 | return; | |
9118 | end if; | |
9119 | ||
3ea52b2e ES |
9120 | Unit1 := Unit (Parent (Clause1)); |
9121 | Unit2 := Unit (Parent (Clause2)); | |
9122 | ||
32beb1f3 AC |
9123 | -- If both clauses are on same unit, or one is the body |
9124 | -- of the other, or one of them is in a subunit, report | |
9125 | -- redundancy on the later one. | |
f7ca1d04 AC |
9126 | |
9127 | if Unit1 = Unit2 then | |
9128 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); | |
ed2233dc | 9129 | Error_Msg_NE -- CODEFIX |
f7ca1d04 | 9130 | ("& is already use-visible through previous " |
dbfeb4fa | 9131 | & "use_type_clause #??", Clause1, T); |
f7ca1d04 | 9132 | return; |
32beb1f3 AC |
9133 | |
9134 | elsif Nkind (Unit1) = N_Subunit then | |
9135 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); | |
ed2233dc | 9136 | Error_Msg_NE -- CODEFIX |
32beb1f3 | 9137 | ("& is already use-visible through previous " |
dbfeb4fa | 9138 | & "use_type_clause #??", Clause1, T); |
32beb1f3 AC |
9139 | return; |
9140 | ||
9141 | elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body) | |
9142 | and then Nkind (Unit1) /= Nkind (Unit2) | |
9143 | and then Nkind (Unit1) /= N_Subunit | |
9144 | then | |
9145 | Error_Msg_Sloc := Sloc (Clause1); | |
ed2233dc | 9146 | Error_Msg_NE -- CODEFIX |
32beb1f3 | 9147 | ("& is already use-visible through previous " |
dbfeb4fa | 9148 | & "use_type_clause #??", Current_Use_Clause (T), T); |
32beb1f3 | 9149 | return; |
f7ca1d04 AC |
9150 | end if; |
9151 | ||
21d27997 RD |
9152 | -- There is a redundant use type clause in a child unit. |
9153 | -- Determine which of the units is more deeply nested. | |
3ea52b2e | 9154 | -- If a unit is a package instance, retrieve the entity |
7ca139d3 | 9155 | -- and its scope from the instance spec. |
21d27997 | 9156 | |
7ca139d3 TQ |
9157 | Ent1 := Entity_Of_Unit (Unit1); |
9158 | Ent2 := Entity_Of_Unit (Unit2); | |
3ea52b2e | 9159 | |
bcb0389e | 9160 | if Scope (Ent2) = Standard_Standard then |
21d27997 RD |
9161 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); |
9162 | Err_No := Clause1; | |
9163 | ||
3ea52b2e | 9164 | elsif Scope (Ent1) = Standard_Standard then |
21d27997 RD |
9165 | Error_Msg_Sloc := Sloc (Id); |
9166 | Err_No := Clause2; | |
9167 | ||
7ca139d3 TQ |
9168 | -- If both units are child units, we determine which one |
9169 | -- is the descendant by the scope distance to the | |
3ea52b2e | 9170 | -- ultimate parent unit. |
21d27997 | 9171 | |
3ea52b2e | 9172 | else |
21d27997 RD |
9173 | declare |
9174 | S1, S2 : Entity_Id; | |
9175 | ||
9176 | begin | |
3ea52b2e ES |
9177 | S1 := Scope (Ent1); |
9178 | S2 := Scope (Ent2); | |
c97c0163 AC |
9179 | while Present (S1) |
9180 | and then Present (S2) | |
9181 | and then S1 /= Standard_Standard | |
9182 | and then S2 /= Standard_Standard | |
21d27997 RD |
9183 | loop |
9184 | S1 := Scope (S1); | |
9185 | S2 := Scope (S2); | |
9186 | end loop; | |
9187 | ||
9188 | if S1 = Standard_Standard then | |
9189 | Error_Msg_Sloc := Sloc (Id); | |
9190 | Err_No := Clause2; | |
9191 | else | |
9192 | Error_Msg_Sloc := Sloc (Current_Use_Clause (T)); | |
9193 | Err_No := Clause1; | |
9194 | end if; | |
9195 | end; | |
9196 | end if; | |
9197 | ||
ed2233dc | 9198 | Error_Msg_NE -- CODEFIX |
21d27997 | 9199 | ("& is already use-visible through previous " |
dbfeb4fa | 9200 | & "use_type_clause #??", Err_No, Id); |
3ea52b2e ES |
9201 | |
9202 | -- Case where current use type clause and the use type | |
9203 | -- clause for the type are not both at the compilation unit | |
9204 | -- level. In this case we don't have location information. | |
9205 | ||
21d27997 | 9206 | else |
ed2233dc | 9207 | Error_Msg_NE -- CODEFIX |
3ea52b2e | 9208 | ("& is already use-visible through previous " |
dbfeb4fa | 9209 | & "use type clause??", Id, T); |
21d27997 | 9210 | end if; |
44b90160 | 9211 | end Use_Clause_Known; |
3ea52b2e ES |
9212 | |
9213 | -- Here if Current_Use_Clause is not set for T, another case | |
9214 | -- where we do not have the location information available. | |
9215 | ||
21d27997 | 9216 | else |
ed2233dc | 9217 | Error_Msg_NE -- CODEFIX |
3ea52b2e | 9218 | ("& is already use-visible through previous " |
dbfeb4fa | 9219 | & "use type clause??", Id, T); |
21d27997 | 9220 | end if; |
954c111a HK |
9221 | |
9222 | -- The package where T is declared is already used | |
9223 | ||
9224 | elsif In_Use (Scope (T)) then | |
9225 | Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T))); | |
ed2233dc | 9226 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 9227 | ("& is already use-visible through package use clause #??", |
37951d8e | 9228 | Id, T); |
954c111a HK |
9229 | |
9230 | -- The current scope is the package where T is declared | |
9231 | ||
9232 | else | |
9233 | Error_Msg_Node_2 := Scope (T); | |
ed2233dc | 9234 | Error_Msg_NE -- CODEFIX |
dbfeb4fa | 9235 | ("& is already use-visible inside package &??", Id, T); |
954c111a HK |
9236 | end if; |
9237 | end if; | |
996ae0b0 RK |
9238 | end Use_One_Type; |
9239 | ||
9240 | ---------------- | |
9241 | -- Write_Info -- | |
9242 | ---------------- | |
9243 | ||
9244 | procedure Write_Info is | |
9245 | Id : Entity_Id := First_Entity (Current_Scope); | |
9246 | ||
9247 | begin | |
9248 | -- No point in dumping standard entities | |
9249 | ||
9250 | if Current_Scope = Standard_Standard then | |
9251 | return; | |
9252 | end if; | |
9253 | ||
9254 | Write_Str ("========================================================"); | |
9255 | Write_Eol; | |
9256 | Write_Str (" Defined Entities in "); | |
9257 | Write_Name (Chars (Current_Scope)); | |
9258 | Write_Eol; | |
9259 | Write_Str ("========================================================"); | |
9260 | Write_Eol; | |
9261 | ||
9262 | if No (Id) then | |
9263 | Write_Str ("-- none --"); | |
9264 | Write_Eol; | |
9265 | ||
9266 | else | |
9267 | while Present (Id) loop | |
9268 | Write_Entity_Info (Id, " "); | |
9269 | Next_Entity (Id); | |
9270 | end loop; | |
9271 | end if; | |
9272 | ||
9273 | if Scope (Current_Scope) = Standard_Standard then | |
9274 | ||
9275 | -- Print information on the current unit itself | |
9276 | ||
9277 | Write_Entity_Info (Current_Scope, " "); | |
9278 | end if; | |
9279 | ||
9280 | Write_Eol; | |
9281 | end Write_Info; | |
9282 | ||
67536dcb RD |
9283 | -------- |
9284 | -- ws -- | |
9285 | -------- | |
996ae0b0 | 9286 | |
67536dcb | 9287 | procedure ws is |
996ae0b0 | 9288 | S : Entity_Id; |
996ae0b0 RK |
9289 | begin |
9290 | for J in reverse 1 .. Scope_Stack.Last loop | |
c8307596 | 9291 | S := Scope_Stack.Table (J).Entity; |
996ae0b0 RK |
9292 | Write_Int (Int (S)); |
9293 | Write_Str (" === "); | |
9294 | Write_Name (Chars (S)); | |
9295 | Write_Eol; | |
9296 | end loop; | |
67536dcb | 9297 | end ws; |
996ae0b0 | 9298 | |
c1ce0691 ES |
9299 | -------- |
9300 | -- we -- | |
9301 | -------- | |
9302 | ||
9303 | procedure we (S : Entity_Id) is | |
9304 | E : Entity_Id; | |
9305 | begin | |
9306 | E := First_Entity (S); | |
9307 | while Present (E) loop | |
9308 | Write_Int (Int (E)); | |
9309 | Write_Str (" === "); | |
9310 | Write_Name (Chars (E)); | |
9311 | Write_Eol; | |
c1ce0691 ES |
9312 | Next_Entity (E); |
9313 | end loop; | |
9314 | end we; | |
996ae0b0 | 9315 | end Sem_Ch8; |