]>
Commit | Line | Data |
---|---|---|
996ae0b0 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- S E M _ R E S -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
c28408b7 | 9 | -- Copyright (C) 1992-2010, 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 Checks; use Checks; | |
28 | with Debug; use Debug; | |
29 | with Debug_A; use Debug_A; | |
30 | with Einfo; use Einfo; | |
b7d1f17f | 31 | with Elists; use Elists; |
996ae0b0 RK |
32 | with Errout; use Errout; |
33 | with Expander; use Expander; | |
758c442c | 34 | with Exp_Disp; use Exp_Disp; |
0669bebe | 35 | with Exp_Ch6; use Exp_Ch6; |
996ae0b0 | 36 | with Exp_Ch7; use Exp_Ch7; |
fbf5a39b | 37 | with Exp_Tss; use Exp_Tss; |
996ae0b0 | 38 | with Exp_Util; use Exp_Util; |
dae2b8ea | 39 | with Fname; use Fname; |
996ae0b0 RK |
40 | with Freeze; use Freeze; |
41 | with Itypes; use Itypes; | |
42 | with Lib; use Lib; | |
43 | with Lib.Xref; use Lib.Xref; | |
44 | with Namet; use Namet; | |
45 | with Nmake; use Nmake; | |
46 | with Nlists; use Nlists; | |
47 | with Opt; use Opt; | |
48 | with Output; use Output; | |
49 | with Restrict; use Restrict; | |
6e937c1c | 50 | with Rident; use Rident; |
996ae0b0 RK |
51 | with Rtsfind; use Rtsfind; |
52 | with Sem; use Sem; | |
a4100e55 | 53 | with Sem_Aux; use Sem_Aux; |
996ae0b0 RK |
54 | with Sem_Aggr; use Sem_Aggr; |
55 | with Sem_Attr; use Sem_Attr; | |
56 | with Sem_Cat; use Sem_Cat; | |
57 | with Sem_Ch4; use Sem_Ch4; | |
58 | with Sem_Ch6; use Sem_Ch6; | |
59 | with Sem_Ch8; use Sem_Ch8; | |
4b92fd3c | 60 | with Sem_Ch13; use Sem_Ch13; |
996ae0b0 RK |
61 | with Sem_Disp; use Sem_Disp; |
62 | with Sem_Dist; use Sem_Dist; | |
16212e89 | 63 | with Sem_Elim; use Sem_Elim; |
996ae0b0 RK |
64 | with Sem_Elab; use Sem_Elab; |
65 | with Sem_Eval; use Sem_Eval; | |
66 | with Sem_Intr; use Sem_Intr; | |
67 | with Sem_Util; use Sem_Util; | |
68 | with Sem_Type; use Sem_Type; | |
69 | with Sem_Warn; use Sem_Warn; | |
70 | with Sinfo; use Sinfo; | |
f4b049db | 71 | with Sinfo.CN; use Sinfo.CN; |
fbf5a39b | 72 | with Snames; use Snames; |
996ae0b0 RK |
73 | with Stand; use Stand; |
74 | with Stringt; use Stringt; | |
45fc7ddb | 75 | with Style; use Style; |
996ae0b0 RK |
76 | with Tbuild; use Tbuild; |
77 | with Uintp; use Uintp; | |
78 | with Urealp; use Urealp; | |
79 | ||
80 | package body Sem_Res is | |
81 | ||
82 | ----------------------- | |
83 | -- Local Subprograms -- | |
84 | ----------------------- | |
85 | ||
86 | -- Second pass (top-down) type checking and overload resolution procedures | |
87 | -- Typ is the type required by context. These procedures propagate the | |
88 | -- type information recursively to the descendants of N. If the node | |
89 | -- is not overloaded, its Etype is established in the first pass. If | |
90 | -- overloaded, the Resolve routines set the correct type. For arith. | |
91 | -- operators, the Etype is the base type of the context. | |
92 | ||
93 | -- Note that Resolve_Attribute is separated off in Sem_Attr | |
94 | ||
bd29d519 AC |
95 | function Bad_Unordered_Enumeration_Reference |
96 | (N : Node_Id; | |
97 | T : Entity_Id) return Boolean; | |
98 | -- Node N contains a potentially dubious reference to type T, either an | |
99 | -- explicit comparison, or an explicit range. This function returns True | |
100 | -- if the type T is an enumeration type for which No pragma Order has been | |
101 | -- given, and the reference N is not in the same extended source unit as | |
102 | -- the declaration of T. | |
103 | ||
996ae0b0 RK |
104 | procedure Check_Discriminant_Use (N : Node_Id); |
105 | -- Enforce the restrictions on the use of discriminants when constraining | |
106 | -- a component of a discriminated type (record or concurrent type). | |
107 | ||
108 | procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id); | |
109 | -- Given a node for an operator associated with type T, check that | |
110 | -- the operator is visible. Operators all of whose operands are | |
111 | -- universal must be checked for visibility during resolution | |
112 | -- because their type is not determinable based on their operands. | |
113 | ||
c8ef728f ES |
114 | procedure Check_Fully_Declared_Prefix |
115 | (Typ : Entity_Id; | |
116 | Pref : Node_Id); | |
117 | -- Check that the type of the prefix of a dereference is not incomplete | |
118 | ||
996ae0b0 RK |
119 | function Check_Infinite_Recursion (N : Node_Id) return Boolean; |
120 | -- Given a call node, N, which is known to occur immediately within the | |
121 | -- subprogram being called, determines whether it is a detectable case of | |
122 | -- an infinite recursion, and if so, outputs appropriate messages. Returns | |
123 | -- True if an infinite recursion is detected, and False otherwise. | |
124 | ||
125 | procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id); | |
126 | -- If the type of the object being initialized uses the secondary stack | |
127 | -- directly or indirectly, create a transient scope for the call to the | |
fbf5a39b AC |
128 | -- init proc. This is because we do not create transient scopes for the |
129 | -- initialization of individual components within the init proc itself. | |
996ae0b0 RK |
130 | -- Could be optimized away perhaps? |
131 | ||
f61580d4 | 132 | procedure Check_No_Direct_Boolean_Operators (N : Node_Id); |
6fb4cdde AC |
133 | -- N is the node for a logical operator. If the operator is predefined, and |
134 | -- the root type of the operands is Standard.Boolean, then a check is made | |
a36c1c3e RD |
135 | -- for restriction No_Direct_Boolean_Operators. This procedure also handles |
136 | -- the style check for Style_Check_Boolean_And_Or. | |
f61580d4 | 137 | |
67ce0d7e RD |
138 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean; |
139 | -- Determine whether E is an access type declared by an access | |
140 | -- declaration, and not an (anonymous) allocator type. | |
141 | ||
996ae0b0 | 142 | function Is_Predefined_Op (Nam : Entity_Id) return Boolean; |
6a497607 AC |
143 | -- Utility to check whether the entity for an operator is a predefined |
144 | -- operator, in which case the expression is left as an operator in the | |
145 | -- tree (else it is rewritten into a call). An instance of an intrinsic | |
146 | -- conversion operation may be given an operator name, but is not treated | |
147 | -- like an operator. Note that an operator that is an imported back-end | |
148 | -- builtin has convention Intrinsic, but is expected to be rewritten into | |
149 | -- a call, so such an operator is not treated as predefined by this | |
150 | -- predicate. | |
996ae0b0 RK |
151 | |
152 | procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id); | |
153 | -- If a default expression in entry call N depends on the discriminants | |
154 | -- of the task, it must be replaced with a reference to the discriminant | |
155 | -- of the task being called. | |
156 | ||
10303118 BD |
157 | procedure Resolve_Op_Concat_Arg |
158 | (N : Node_Id; | |
159 | Arg : Node_Id; | |
160 | Typ : Entity_Id; | |
161 | Is_Comp : Boolean); | |
162 | -- Internal procedure for Resolve_Op_Concat to resolve one operand of | |
163 | -- concatenation operator. The operand is either of the array type or of | |
164 | -- the component type. If the operand is an aggregate, and the component | |
165 | -- type is composite, this is ambiguous if component type has aggregates. | |
166 | ||
167 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id); | |
168 | -- Does the first part of the work of Resolve_Op_Concat | |
169 | ||
170 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id); | |
171 | -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand | |
172 | -- has been resolved. See Resolve_Op_Concat for details. | |
173 | ||
996ae0b0 RK |
174 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id); |
175 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id); | |
176 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id); | |
19d846a0 | 177 | procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id); |
996ae0b0 RK |
178 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id); |
179 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id); | |
180 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id); | |
955871d3 | 181 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id); |
996ae0b0 RK |
182 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id); |
183 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id); | |
955871d3 | 184 | procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id); |
996ae0b0 RK |
185 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id); |
186 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id); | |
187 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id); | |
188 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id); | |
189 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id); | |
190 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id); | |
191 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id); | |
192 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id); | |
193 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id); | |
194 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id); | |
195 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id); | |
196 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id); | |
197 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id); | |
198 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id); | |
199 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id); | |
200 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id); | |
201 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id); | |
202 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id); | |
203 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id); | |
204 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id); | |
205 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id); | |
206 | procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id); | |
207 | procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id); | |
208 | ||
209 | function Operator_Kind | |
210 | (Op_Name : Name_Id; | |
0ab80019 | 211 | Is_Binary : Boolean) return Node_Kind; |
996ae0b0 RK |
212 | -- Utility to map the name of an operator into the corresponding Node. Used |
213 | -- by other node rewriting procedures. | |
214 | ||
215 | procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id); | |
bc5f3720 RD |
216 | -- Resolve actuals of call, and add default expressions for missing ones. |
217 | -- N is the Node_Id for the subprogram call, and Nam is the entity of the | |
218 | -- called subprogram. | |
996ae0b0 RK |
219 | |
220 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id); | |
221 | -- Called from Resolve_Call, when the prefix denotes an entry or element | |
222 | -- of entry family. Actuals are resolved as for subprograms, and the node | |
223 | -- is rebuilt as an entry call. Also called for protected operations. Typ | |
224 | -- is the context type, which is used when the operation is a protected | |
225 | -- function with no arguments, and the return value is indexed. | |
226 | ||
227 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id); | |
228 | -- A call to a user-defined intrinsic operator is rewritten as a call | |
229 | -- to the corresponding predefined operator, with suitable conversions. | |
6a497607 | 230 | -- Note that this applies only for intrinsic operators that denote |
841dd0f5 | 231 | -- predefined operators, not operators that are intrinsic imports of |
6a497607 | 232 | -- back-end builtins. |
996ae0b0 | 233 | |
fbf5a39b | 234 | procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id); |
305caf42 AC |
235 | -- Ditto, for unary operators (arithmetic ones and "not" on signed |
236 | -- integer types for VMS). | |
fbf5a39b | 237 | |
996ae0b0 RK |
238 | procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id); |
239 | -- If an operator node resolves to a call to a user-defined operator, | |
240 | -- rewrite the node as a function call. | |
241 | ||
242 | procedure Make_Call_Into_Operator | |
243 | (N : Node_Id; | |
244 | Typ : Entity_Id; | |
245 | Op_Id : Entity_Id); | |
246 | -- Inverse transformation: if an operator is given in functional notation, | |
247 | -- then after resolving the node, transform into an operator node, so | |
248 | -- that operands are resolved properly. Recall that predefined operators | |
249 | -- do not have a full signature and special resolution rules apply. | |
250 | ||
0ab80019 AC |
251 | procedure Rewrite_Renamed_Operator |
252 | (N : Node_Id; | |
253 | Op : Entity_Id; | |
254 | Typ : Entity_Id); | |
996ae0b0 | 255 | -- An operator can rename another, e.g. in an instantiation. In that |
0ab80019 | 256 | -- case, the proper operator node must be constructed and resolved. |
996ae0b0 RK |
257 | |
258 | procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id); | |
259 | -- The String_Literal_Subtype is built for all strings that are not | |
07fc65c4 GB |
260 | -- operands of a static concatenation operation. If the argument is |
261 | -- not a N_String_Literal node, then the call has no effect. | |
996ae0b0 RK |
262 | |
263 | procedure Set_Slice_Subtype (N : Node_Id); | |
fbf5a39b | 264 | -- Build subtype of array type, with the range specified by the slice |
996ae0b0 | 265 | |
0669bebe GB |
266 | procedure Simplify_Type_Conversion (N : Node_Id); |
267 | -- Called after N has been resolved and evaluated, but before range checks | |
268 | -- have been applied. Currently simplifies a combination of floating-point | |
269 | -- to integer conversion and Truncation attribute. | |
270 | ||
996ae0b0 | 271 | function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id; |
07fc65c4 GB |
272 | -- A universal_fixed expression in an universal context is unambiguous |
273 | -- if there is only one applicable fixed point type. Determining whether | |
996ae0b0 RK |
274 | -- there is only one requires a search over all visible entities, and |
275 | -- happens only in very pathological cases (see 6115-006). | |
276 | ||
277 | function Valid_Conversion | |
278 | (N : Node_Id; | |
279 | Target : Entity_Id; | |
0ab80019 | 280 | Operand : Node_Id) return Boolean; |
996ae0b0 RK |
281 | -- Verify legality rules given in 4.6 (8-23). Target is the target |
282 | -- type of the conversion, which may be an implicit conversion of | |
283 | -- an actual parameter to an anonymous access type (in which case | |
284 | -- N denotes the actual parameter and N = Operand). | |
285 | ||
286 | ------------------------- | |
287 | -- Ambiguous_Character -- | |
288 | ------------------------- | |
289 | ||
290 | procedure Ambiguous_Character (C : Node_Id) is | |
291 | E : Entity_Id; | |
292 | ||
293 | begin | |
294 | if Nkind (C) = N_Character_Literal then | |
ed2233dc | 295 | Error_Msg_N ("ambiguous character literal", C); |
b7d1f17f HK |
296 | |
297 | -- First the ones in Standard | |
298 | ||
ed2233dc AC |
299 | Error_Msg_N ("\\possible interpretation: Character!", C); |
300 | Error_Msg_N ("\\possible interpretation: Wide_Character!", C); | |
b7d1f17f HK |
301 | |
302 | -- Include Wide_Wide_Character in Ada 2005 mode | |
303 | ||
304 | if Ada_Version >= Ada_05 then | |
ed2233dc | 305 | Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C); |
b7d1f17f HK |
306 | end if; |
307 | ||
308 | -- Now any other types that match | |
996ae0b0 RK |
309 | |
310 | E := Current_Entity (C); | |
1420b484 | 311 | while Present (E) loop |
ed2233dc | 312 | Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E)); |
1420b484 JM |
313 | E := Homonym (E); |
314 | end loop; | |
996ae0b0 RK |
315 | end if; |
316 | end Ambiguous_Character; | |
317 | ||
318 | ------------------------- | |
319 | -- Analyze_And_Resolve -- | |
320 | ------------------------- | |
321 | ||
322 | procedure Analyze_And_Resolve (N : Node_Id) is | |
323 | begin | |
324 | Analyze (N); | |
fbf5a39b | 325 | Resolve (N); |
996ae0b0 RK |
326 | end Analyze_And_Resolve; |
327 | ||
328 | procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is | |
329 | begin | |
330 | Analyze (N); | |
331 | Resolve (N, Typ); | |
332 | end Analyze_And_Resolve; | |
333 | ||
334 | -- Version withs check(s) suppressed | |
335 | ||
336 | procedure Analyze_And_Resolve | |
337 | (N : Node_Id; | |
338 | Typ : Entity_Id; | |
339 | Suppress : Check_Id) | |
340 | is | |
fbf5a39b | 341 | Scop : constant Entity_Id := Current_Scope; |
996ae0b0 RK |
342 | |
343 | begin | |
344 | if Suppress = All_Checks then | |
345 | declare | |
fbf5a39b | 346 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
347 | begin |
348 | Scope_Suppress := (others => True); | |
349 | Analyze_And_Resolve (N, Typ); | |
350 | Scope_Suppress := Svg; | |
351 | end; | |
352 | ||
353 | else | |
354 | declare | |
fbf5a39b | 355 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 RK |
356 | |
357 | begin | |
fbf5a39b | 358 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 359 | Analyze_And_Resolve (N, Typ); |
fbf5a39b | 360 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
361 | end; |
362 | end if; | |
363 | ||
364 | if Current_Scope /= Scop | |
365 | and then Scope_Is_Transient | |
366 | then | |
367 | -- This can only happen if a transient scope was created | |
368 | -- for an inner expression, which will be removed upon | |
369 | -- completion of the analysis of an enclosing construct. | |
370 | -- The transient scope must have the suppress status of | |
371 | -- the enclosing environment, not of this Analyze call. | |
372 | ||
373 | Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress := | |
374 | Scope_Suppress; | |
375 | end if; | |
376 | end Analyze_And_Resolve; | |
377 | ||
378 | procedure Analyze_And_Resolve | |
379 | (N : Node_Id; | |
380 | Suppress : Check_Id) | |
381 | is | |
fbf5a39b | 382 | Scop : constant Entity_Id := Current_Scope; |
996ae0b0 RK |
383 | |
384 | begin | |
385 | if Suppress = All_Checks then | |
386 | declare | |
fbf5a39b | 387 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
388 | begin |
389 | Scope_Suppress := (others => True); | |
390 | Analyze_And_Resolve (N); | |
391 | Scope_Suppress := Svg; | |
392 | end; | |
393 | ||
394 | else | |
395 | declare | |
fbf5a39b | 396 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 RK |
397 | |
398 | begin | |
fbf5a39b | 399 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 400 | Analyze_And_Resolve (N); |
fbf5a39b | 401 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
402 | end; |
403 | end if; | |
404 | ||
405 | if Current_Scope /= Scop | |
406 | and then Scope_Is_Transient | |
407 | then | |
408 | Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress := | |
409 | Scope_Suppress; | |
410 | end if; | |
411 | end Analyze_And_Resolve; | |
412 | ||
bd29d519 AC |
413 | ---------------------------------------- |
414 | -- Bad_Unordered_Enumeration_Reference -- | |
415 | ---------------------------------------- | |
416 | ||
417 | function Bad_Unordered_Enumeration_Reference | |
418 | (N : Node_Id; | |
419 | T : Entity_Id) return Boolean | |
420 | is | |
421 | begin | |
422 | return Is_Enumeration_Type (T) | |
423 | and then Comes_From_Source (N) | |
424 | and then Warn_On_Unordered_Enumeration_Type | |
425 | and then not Has_Pragma_Ordered (T) | |
426 | and then not In_Same_Extended_Unit (N, T); | |
427 | end Bad_Unordered_Enumeration_Reference; | |
428 | ||
996ae0b0 RK |
429 | ---------------------------- |
430 | -- Check_Discriminant_Use -- | |
431 | ---------------------------- | |
432 | ||
433 | procedure Check_Discriminant_Use (N : Node_Id) is | |
434 | PN : constant Node_Id := Parent (N); | |
435 | Disc : constant Entity_Id := Entity (N); | |
436 | P : Node_Id; | |
437 | D : Node_Id; | |
438 | ||
439 | begin | |
f3d0f304 | 440 | -- Any use in a spec-expression is legal |
996ae0b0 | 441 | |
45fc7ddb | 442 | if In_Spec_Expression then |
996ae0b0 RK |
443 | null; |
444 | ||
445 | elsif Nkind (PN) = N_Range then | |
446 | ||
a77842bd | 447 | -- Discriminant cannot be used to constrain a scalar type |
996ae0b0 RK |
448 | |
449 | P := Parent (PN); | |
450 | ||
451 | if Nkind (P) = N_Range_Constraint | |
452 | and then Nkind (Parent (P)) = N_Subtype_Indication | |
a397db96 | 453 | and then Nkind (Parent (Parent (P))) = N_Component_Definition |
996ae0b0 RK |
454 | then |
455 | Error_Msg_N ("discriminant cannot constrain scalar type", N); | |
456 | ||
457 | elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then | |
458 | ||
459 | -- The following check catches the unusual case where | |
460 | -- a discriminant appears within an index constraint | |
461 | -- that is part of a larger expression within a constraint | |
462 | -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))". | |
463 | -- For now we only check case of record components, and | |
464 | -- note that a similar check should also apply in the | |
465 | -- case of discriminant constraints below. ??? | |
466 | ||
467 | -- Note that the check for N_Subtype_Declaration below is to | |
468 | -- detect the valid use of discriminants in the constraints of a | |
469 | -- subtype declaration when this subtype declaration appears | |
470 | -- inside the scope of a record type (which is syntactically | |
471 | -- illegal, but which may be created as part of derived type | |
472 | -- processing for records). See Sem_Ch3.Build_Derived_Record_Type | |
473 | -- for more info. | |
474 | ||
475 | if Ekind (Current_Scope) = E_Record_Type | |
476 | and then Scope (Disc) = Current_Scope | |
477 | and then not | |
478 | (Nkind (Parent (P)) = N_Subtype_Indication | |
45fc7ddb HK |
479 | and then |
480 | Nkind_In (Parent (Parent (P)), N_Component_Definition, | |
481 | N_Subtype_Declaration) | |
996ae0b0 RK |
482 | and then Paren_Count (N) = 0) |
483 | then | |
484 | Error_Msg_N | |
485 | ("discriminant must appear alone in component constraint", N); | |
486 | return; | |
487 | end if; | |
488 | ||
a0ac3932 | 489 | -- Detect a common error: |
9bc43c53 | 490 | |
996ae0b0 | 491 | -- type R (D : Positive := 100) is record |
9bc43c53 | 492 | -- Name : String (1 .. D); |
996ae0b0 RK |
493 | -- end record; |
494 | ||
a0ac3932 RD |
495 | -- The default value causes an object of type R to be allocated |
496 | -- with room for Positive'Last characters. The RM does not mandate | |
497 | -- the allocation of the maximum size, but that is what GNAT does | |
498 | -- so we should warn the programmer that there is a problem. | |
996ae0b0 | 499 | |
a0ac3932 | 500 | Check_Large : declare |
996ae0b0 RK |
501 | SI : Node_Id; |
502 | T : Entity_Id; | |
503 | TB : Node_Id; | |
504 | CB : Entity_Id; | |
505 | ||
506 | function Large_Storage_Type (T : Entity_Id) return Boolean; | |
507 | -- Return True if type T has a large enough range that | |
508 | -- any array whose index type covered the whole range of | |
509 | -- the type would likely raise Storage_Error. | |
510 | ||
fbf5a39b AC |
511 | ------------------------ |
512 | -- Large_Storage_Type -- | |
513 | ------------------------ | |
514 | ||
996ae0b0 RK |
515 | function Large_Storage_Type (T : Entity_Id) return Boolean is |
516 | begin | |
4b92fd3c ST |
517 | -- The type is considered large if its bounds are known at |
518 | -- compile time and if it requires at least as many bits as | |
519 | -- a Positive to store the possible values. | |
520 | ||
521 | return Compile_Time_Known_Value (Type_Low_Bound (T)) | |
522 | and then Compile_Time_Known_Value (Type_High_Bound (T)) | |
523 | and then | |
524 | Minimum_Size (T, Biased => True) >= | |
a0ac3932 | 525 | RM_Size (Standard_Positive); |
996ae0b0 RK |
526 | end Large_Storage_Type; |
527 | ||
a0ac3932 RD |
528 | -- Start of processing for Check_Large |
529 | ||
996ae0b0 RK |
530 | begin |
531 | -- Check that the Disc has a large range | |
532 | ||
533 | if not Large_Storage_Type (Etype (Disc)) then | |
534 | goto No_Danger; | |
535 | end if; | |
536 | ||
537 | -- If the enclosing type is limited, we allocate only the | |
538 | -- default value, not the maximum, and there is no need for | |
539 | -- a warning. | |
540 | ||
541 | if Is_Limited_Type (Scope (Disc)) then | |
542 | goto No_Danger; | |
543 | end if; | |
544 | ||
545 | -- Check that it is the high bound | |
546 | ||
547 | if N /= High_Bound (PN) | |
c8ef728f | 548 | or else No (Discriminant_Default_Value (Disc)) |
996ae0b0 RK |
549 | then |
550 | goto No_Danger; | |
551 | end if; | |
552 | ||
553 | -- Check the array allows a large range at this bound. | |
554 | -- First find the array | |
555 | ||
556 | SI := Parent (P); | |
557 | ||
558 | if Nkind (SI) /= N_Subtype_Indication then | |
559 | goto No_Danger; | |
560 | end if; | |
561 | ||
562 | T := Entity (Subtype_Mark (SI)); | |
563 | ||
564 | if not Is_Array_Type (T) then | |
565 | goto No_Danger; | |
566 | end if; | |
567 | ||
568 | -- Next, find the dimension | |
569 | ||
570 | TB := First_Index (T); | |
571 | CB := First (Constraints (P)); | |
572 | while True | |
573 | and then Present (TB) | |
574 | and then Present (CB) | |
575 | and then CB /= PN | |
576 | loop | |
577 | Next_Index (TB); | |
578 | Next (CB); | |
579 | end loop; | |
580 | ||
581 | if CB /= PN then | |
582 | goto No_Danger; | |
583 | end if; | |
584 | ||
585 | -- Now, check the dimension has a large range | |
586 | ||
587 | if not Large_Storage_Type (Etype (TB)) then | |
588 | goto No_Danger; | |
589 | end if; | |
590 | ||
591 | -- Warn about the danger | |
592 | ||
593 | Error_Msg_N | |
aa5147f0 | 594 | ("?creation of & object may raise Storage_Error!", |
fbf5a39b | 595 | Scope (Disc)); |
996ae0b0 RK |
596 | |
597 | <<No_Danger>> | |
598 | null; | |
599 | ||
a0ac3932 | 600 | end Check_Large; |
996ae0b0 RK |
601 | end if; |
602 | ||
603 | -- Legal case is in index or discriminant constraint | |
604 | ||
45fc7ddb HK |
605 | elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint, |
606 | N_Discriminant_Association) | |
996ae0b0 RK |
607 | then |
608 | if Paren_Count (N) > 0 then | |
609 | Error_Msg_N | |
610 | ("discriminant in constraint must appear alone", N); | |
758c442c GD |
611 | |
612 | elsif Nkind (N) = N_Expanded_Name | |
613 | and then Comes_From_Source (N) | |
614 | then | |
615 | Error_Msg_N | |
616 | ("discriminant must appear alone as a direct name", N); | |
996ae0b0 RK |
617 | end if; |
618 | ||
619 | return; | |
620 | ||
621 | -- Otherwise, context is an expression. It should not be within | |
622 | -- (i.e. a subexpression of) a constraint for a component. | |
623 | ||
624 | else | |
625 | D := PN; | |
626 | P := Parent (PN); | |
45fc7ddb HK |
627 | while not Nkind_In (P, N_Component_Declaration, |
628 | N_Subtype_Indication, | |
629 | N_Entry_Declaration) | |
996ae0b0 RK |
630 | loop |
631 | D := P; | |
632 | P := Parent (P); | |
633 | exit when No (P); | |
634 | end loop; | |
635 | ||
636 | -- If the discriminant is used in an expression that is a bound | |
637 | -- of a scalar type, an Itype is created and the bounds are attached | |
638 | -- to its range, not to the original subtype indication. Such use | |
639 | -- is of course a double fault. | |
640 | ||
641 | if (Nkind (P) = N_Subtype_Indication | |
45fc7ddb HK |
642 | and then Nkind_In (Parent (P), N_Component_Definition, |
643 | N_Derived_Type_Definition) | |
996ae0b0 RK |
644 | and then D = Constraint (P)) |
645 | ||
646 | -- The constraint itself may be given by a subtype indication, | |
647 | -- rather than by a more common discrete range. | |
648 | ||
649 | or else (Nkind (P) = N_Subtype_Indication | |
fbf5a39b AC |
650 | and then |
651 | Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint) | |
996ae0b0 RK |
652 | or else Nkind (P) = N_Entry_Declaration |
653 | or else Nkind (D) = N_Defining_Identifier | |
654 | then | |
655 | Error_Msg_N | |
656 | ("discriminant in constraint must appear alone", N); | |
657 | end if; | |
658 | end if; | |
659 | end Check_Discriminant_Use; | |
660 | ||
661 | -------------------------------- | |
662 | -- Check_For_Visible_Operator -- | |
663 | -------------------------------- | |
664 | ||
665 | procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is | |
996ae0b0 | 666 | begin |
fbf5a39b | 667 | if Is_Invisible_Operator (N, T) then |
305caf42 | 668 | Error_Msg_NE -- CODEFIX |
996ae0b0 | 669 | ("operator for} is not directly visible!", N, First_Subtype (T)); |
305caf42 AC |
670 | Error_Msg_N -- CODEFIX |
671 | ("use clause would make operation legal!", N); | |
996ae0b0 RK |
672 | end if; |
673 | end Check_For_Visible_Operator; | |
674 | ||
c8ef728f ES |
675 | ---------------------------------- |
676 | -- Check_Fully_Declared_Prefix -- | |
677 | ---------------------------------- | |
678 | ||
679 | procedure Check_Fully_Declared_Prefix | |
680 | (Typ : Entity_Id; | |
681 | Pref : Node_Id) | |
682 | is | |
683 | begin | |
684 | -- Check that the designated type of the prefix of a dereference is | |
685 | -- not an incomplete type. This cannot be done unconditionally, because | |
686 | -- dereferences of private types are legal in default expressions. This | |
687 | -- case is taken care of in Check_Fully_Declared, called below. There | |
688 | -- are also 2005 cases where it is legal for the prefix to be unfrozen. | |
689 | ||
690 | -- This consideration also applies to similar checks for allocators, | |
691 | -- qualified expressions, and type conversions. | |
692 | ||
693 | -- An additional exception concerns other per-object expressions that | |
694 | -- are not directly related to component declarations, in particular | |
695 | -- representation pragmas for tasks. These will be per-object | |
696 | -- expressions if they depend on discriminants or some global entity. | |
697 | -- If the task has access discriminants, the designated type may be | |
698 | -- incomplete at the point the expression is resolved. This resolution | |
699 | -- takes place within the body of the initialization procedure, where | |
700 | -- the discriminant is replaced by its discriminal. | |
701 | ||
702 | if Is_Entity_Name (Pref) | |
703 | and then Ekind (Entity (Pref)) = E_In_Parameter | |
704 | then | |
705 | null; | |
706 | ||
707 | -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages | |
708 | -- are handled by Analyze_Access_Attribute, Analyze_Assignment, | |
709 | -- Analyze_Object_Renaming, and Freeze_Entity. | |
710 | ||
711 | elsif Ada_Version >= Ada_05 | |
712 | and then Is_Entity_Name (Pref) | |
811c6a85 | 713 | and then Is_Access_Type (Etype (Pref)) |
c8ef728f ES |
714 | and then Ekind (Directly_Designated_Type (Etype (Pref))) = |
715 | E_Incomplete_Type | |
716 | and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref))) | |
717 | then | |
718 | null; | |
719 | else | |
720 | Check_Fully_Declared (Typ, Parent (Pref)); | |
721 | end if; | |
722 | end Check_Fully_Declared_Prefix; | |
723 | ||
996ae0b0 RK |
724 | ------------------------------ |
725 | -- Check_Infinite_Recursion -- | |
726 | ------------------------------ | |
727 | ||
728 | function Check_Infinite_Recursion (N : Node_Id) return Boolean is | |
729 | P : Node_Id; | |
730 | C : Node_Id; | |
731 | ||
07fc65c4 GB |
732 | function Same_Argument_List return Boolean; |
733 | -- Check whether list of actuals is identical to list of formals | |
734 | -- of called function (which is also the enclosing scope). | |
735 | ||
736 | ------------------------ | |
737 | -- Same_Argument_List -- | |
738 | ------------------------ | |
739 | ||
740 | function Same_Argument_List return Boolean is | |
741 | A : Node_Id; | |
742 | F : Entity_Id; | |
743 | Subp : Entity_Id; | |
744 | ||
745 | begin | |
746 | if not Is_Entity_Name (Name (N)) then | |
747 | return False; | |
748 | else | |
749 | Subp := Entity (Name (N)); | |
750 | end if; | |
751 | ||
752 | F := First_Formal (Subp); | |
753 | A := First_Actual (N); | |
07fc65c4 GB |
754 | while Present (F) and then Present (A) loop |
755 | if not Is_Entity_Name (A) | |
756 | or else Entity (A) /= F | |
757 | then | |
758 | return False; | |
759 | end if; | |
760 | ||
761 | Next_Actual (A); | |
762 | Next_Formal (F); | |
763 | end loop; | |
764 | ||
765 | return True; | |
766 | end Same_Argument_List; | |
767 | ||
768 | -- Start of processing for Check_Infinite_Recursion | |
769 | ||
996ae0b0 | 770 | begin |
26570b21 RD |
771 | -- Special case, if this is a procedure call and is a call to the |
772 | -- current procedure with the same argument list, then this is for | |
773 | -- sure an infinite recursion and we insert a call to raise SE. | |
774 | ||
775 | if Is_List_Member (N) | |
776 | and then List_Length (List_Containing (N)) = 1 | |
777 | and then Same_Argument_List | |
778 | then | |
779 | declare | |
780 | P : constant Node_Id := Parent (N); | |
781 | begin | |
782 | if Nkind (P) = N_Handled_Sequence_Of_Statements | |
783 | and then Nkind (Parent (P)) = N_Subprogram_Body | |
784 | and then Is_Empty_List (Declarations (Parent (P))) | |
785 | then | |
786 | Error_Msg_N ("!?infinite recursion", N); | |
787 | Error_Msg_N ("\!?Storage_Error will be raised at run time", N); | |
788 | Insert_Action (N, | |
789 | Make_Raise_Storage_Error (Sloc (N), | |
790 | Reason => SE_Infinite_Recursion)); | |
791 | return True; | |
792 | end if; | |
793 | end; | |
794 | end if; | |
795 | ||
796 | -- If not that special case, search up tree, quitting if we reach a | |
797 | -- construct (e.g. a conditional) that tells us that this is not a | |
798 | -- case for an infinite recursion warning. | |
996ae0b0 RK |
799 | |
800 | C := N; | |
801 | loop | |
802 | P := Parent (C); | |
9a7da240 RD |
803 | |
804 | -- If no parent, then we were not inside a subprogram, this can for | |
805 | -- example happen when processing certain pragmas in a spec. Just | |
806 | -- return False in this case. | |
807 | ||
808 | if No (P) then | |
809 | return False; | |
810 | end if; | |
811 | ||
812 | -- Done if we get to subprogram body, this is definitely an infinite | |
813 | -- recursion case if we did not find anything to stop us. | |
814 | ||
996ae0b0 | 815 | exit when Nkind (P) = N_Subprogram_Body; |
9a7da240 RD |
816 | |
817 | -- If appearing in conditional, result is false | |
818 | ||
45fc7ddb HK |
819 | if Nkind_In (P, N_Or_Else, |
820 | N_And_Then, | |
821 | N_If_Statement, | |
822 | N_Case_Statement) | |
996ae0b0 RK |
823 | then |
824 | return False; | |
825 | ||
826 | elsif Nkind (P) = N_Handled_Sequence_Of_Statements | |
827 | and then C /= First (Statements (P)) | |
828 | then | |
26570b21 RD |
829 | -- If the call is the expression of a return statement and the |
830 | -- actuals are identical to the formals, it's worth a warning. | |
831 | -- However, we skip this if there is an immediately preceding | |
832 | -- raise statement, since the call is never executed. | |
07fc65c4 GB |
833 | |
834 | -- Furthermore, this corresponds to a common idiom: | |
835 | ||
836 | -- function F (L : Thing) return Boolean is | |
837 | -- begin | |
838 | -- raise Program_Error; | |
839 | -- return F (L); | |
840 | -- end F; | |
841 | ||
842 | -- for generating a stub function | |
843 | ||
aa5147f0 | 844 | if Nkind (Parent (N)) = N_Simple_Return_Statement |
07fc65c4 GB |
845 | and then Same_Argument_List |
846 | then | |
9ebe3743 HK |
847 | exit when not Is_List_Member (Parent (N)); |
848 | ||
849 | -- OK, return statement is in a statement list, look for raise | |
850 | ||
851 | declare | |
852 | Nod : Node_Id; | |
853 | ||
854 | begin | |
855 | -- Skip past N_Freeze_Entity nodes generated by expansion | |
856 | ||
857 | Nod := Prev (Parent (N)); | |
858 | while Present (Nod) | |
859 | and then Nkind (Nod) = N_Freeze_Entity | |
860 | loop | |
861 | Prev (Nod); | |
862 | end loop; | |
863 | ||
864 | -- If no raise statement, give warning | |
865 | ||
866 | exit when Nkind (Nod) /= N_Raise_Statement | |
867 | and then | |
868 | (Nkind (Nod) not in N_Raise_xxx_Error | |
869 | or else Present (Condition (Nod))); | |
870 | end; | |
07fc65c4 GB |
871 | end if; |
872 | ||
996ae0b0 RK |
873 | return False; |
874 | ||
875 | else | |
876 | C := P; | |
877 | end if; | |
878 | end loop; | |
879 | ||
aa5147f0 ES |
880 | Error_Msg_N ("!?possible infinite recursion", N); |
881 | Error_Msg_N ("\!?Storage_Error may be raised at run time", N); | |
996ae0b0 RK |
882 | |
883 | return True; | |
884 | end Check_Infinite_Recursion; | |
885 | ||
886 | ------------------------------- | |
887 | -- Check_Initialization_Call -- | |
888 | ------------------------------- | |
889 | ||
890 | procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is | |
fbf5a39b | 891 | Typ : constant Entity_Id := Etype (First_Formal (Nam)); |
996ae0b0 RK |
892 | |
893 | function Uses_SS (T : Entity_Id) return Boolean; | |
07fc65c4 GB |
894 | -- Check whether the creation of an object of the type will involve |
895 | -- use of the secondary stack. If T is a record type, this is true | |
f3d57416 | 896 | -- if the expression for some component uses the secondary stack, e.g. |
07fc65c4 GB |
897 | -- through a call to a function that returns an unconstrained value. |
898 | -- False if T is controlled, because cleanups occur elsewhere. | |
899 | ||
900 | ------------- | |
901 | -- Uses_SS -- | |
902 | ------------- | |
996ae0b0 RK |
903 | |
904 | function Uses_SS (T : Entity_Id) return Boolean is | |
aa5147f0 ES |
905 | Comp : Entity_Id; |
906 | Expr : Node_Id; | |
907 | Full_Type : Entity_Id := Underlying_Type (T); | |
996ae0b0 RK |
908 | |
909 | begin | |
aa5147f0 ES |
910 | -- Normally we want to use the underlying type, but if it's not set |
911 | -- then continue with T. | |
912 | ||
913 | if not Present (Full_Type) then | |
914 | Full_Type := T; | |
915 | end if; | |
916 | ||
917 | if Is_Controlled (Full_Type) then | |
996ae0b0 RK |
918 | return False; |
919 | ||
aa5147f0 ES |
920 | elsif Is_Array_Type (Full_Type) then |
921 | return Uses_SS (Component_Type (Full_Type)); | |
996ae0b0 | 922 | |
aa5147f0 ES |
923 | elsif Is_Record_Type (Full_Type) then |
924 | Comp := First_Component (Full_Type); | |
996ae0b0 | 925 | while Present (Comp) loop |
996ae0b0 RK |
926 | if Ekind (Comp) = E_Component |
927 | and then Nkind (Parent (Comp)) = N_Component_Declaration | |
928 | then | |
aa5147f0 ES |
929 | -- The expression for a dynamic component may be rewritten |
930 | -- as a dereference, so retrieve original node. | |
931 | ||
932 | Expr := Original_Node (Expression (Parent (Comp))); | |
996ae0b0 | 933 | |
aa5147f0 | 934 | -- Return True if the expression is a call to a function |
1d57c04f AC |
935 | -- (including an attribute function such as Image, or a |
936 | -- user-defined operator) with a result that requires a | |
937 | -- transient scope. | |
fbf5a39b | 938 | |
aa5147f0 | 939 | if (Nkind (Expr) = N_Function_Call |
1d57c04f | 940 | or else Nkind (Expr) in N_Op |
aa5147f0 ES |
941 | or else (Nkind (Expr) = N_Attribute_Reference |
942 | and then Present (Expressions (Expr)))) | |
996ae0b0 RK |
943 | and then Requires_Transient_Scope (Etype (Expr)) |
944 | then | |
945 | return True; | |
946 | ||
947 | elsif Uses_SS (Etype (Comp)) then | |
948 | return True; | |
949 | end if; | |
950 | end if; | |
951 | ||
952 | Next_Component (Comp); | |
953 | end loop; | |
954 | ||
955 | return False; | |
956 | ||
957 | else | |
958 | return False; | |
959 | end if; | |
960 | end Uses_SS; | |
961 | ||
07fc65c4 GB |
962 | -- Start of processing for Check_Initialization_Call |
963 | ||
996ae0b0 | 964 | begin |
0669bebe | 965 | -- Establish a transient scope if the type needs it |
07fc65c4 | 966 | |
0669bebe | 967 | if Uses_SS (Typ) then |
996ae0b0 RK |
968 | Establish_Transient_Scope (First_Actual (N), Sec_Stack => True); |
969 | end if; | |
970 | end Check_Initialization_Call; | |
971 | ||
f61580d4 AC |
972 | --------------------------------------- |
973 | -- Check_No_Direct_Boolean_Operators -- | |
974 | --------------------------------------- | |
975 | ||
976 | procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is | |
977 | begin | |
978 | if Scope (Entity (N)) = Standard_Standard | |
979 | and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean | |
980 | then | |
6fb4cdde | 981 | -- Restriction only applies to original source code |
f61580d4 | 982 | |
6fb4cdde | 983 | if Comes_From_Source (N) then |
f61580d4 AC |
984 | Check_Restriction (No_Direct_Boolean_Operators, N); |
985 | end if; | |
986 | end if; | |
a36c1c3e RD |
987 | |
988 | if Style_Check then | |
989 | Check_Boolean_Operator (N); | |
990 | end if; | |
f61580d4 AC |
991 | end Check_No_Direct_Boolean_Operators; |
992 | ||
996ae0b0 RK |
993 | ------------------------------ |
994 | -- Check_Parameterless_Call -- | |
995 | ------------------------------ | |
996 | ||
997 | procedure Check_Parameterless_Call (N : Node_Id) is | |
998 | Nam : Node_Id; | |
999 | ||
bc5f3720 RD |
1000 | function Prefix_Is_Access_Subp return Boolean; |
1001 | -- If the prefix is of an access_to_subprogram type, the node must be | |
1002 | -- rewritten as a call. Ditto if the prefix is overloaded and all its | |
1003 | -- interpretations are access to subprograms. | |
1004 | ||
1005 | --------------------------- | |
1006 | -- Prefix_Is_Access_Subp -- | |
1007 | --------------------------- | |
1008 | ||
1009 | function Prefix_Is_Access_Subp return Boolean is | |
1010 | I : Interp_Index; | |
1011 | It : Interp; | |
1012 | ||
1013 | begin | |
22b77f68 | 1014 | -- If the context is an attribute reference that can apply to |
b4a4936b | 1015 | -- functions, this is never a parameterless call (RM 4.1.4(6)). |
96d2756f AC |
1016 | |
1017 | if Nkind (Parent (N)) = N_Attribute_Reference | |
1018 | and then (Attribute_Name (Parent (N)) = Name_Address | |
1019 | or else Attribute_Name (Parent (N)) = Name_Code_Address | |
1020 | or else Attribute_Name (Parent (N)) = Name_Access) | |
1021 | then | |
1022 | return False; | |
1023 | end if; | |
1024 | ||
bc5f3720 RD |
1025 | if not Is_Overloaded (N) then |
1026 | return | |
1027 | Ekind (Etype (N)) = E_Subprogram_Type | |
1028 | and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type; | |
1029 | else | |
1030 | Get_First_Interp (N, I, It); | |
1031 | while Present (It.Typ) loop | |
1032 | if Ekind (It.Typ) /= E_Subprogram_Type | |
1033 | or else Base_Type (Etype (It.Typ)) = Standard_Void_Type | |
1034 | then | |
1035 | return False; | |
1036 | end if; | |
1037 | ||
1038 | Get_Next_Interp (I, It); | |
1039 | end loop; | |
1040 | ||
1041 | return True; | |
1042 | end if; | |
1043 | end Prefix_Is_Access_Subp; | |
1044 | ||
1045 | -- Start of processing for Check_Parameterless_Call | |
1046 | ||
996ae0b0 | 1047 | begin |
07fc65c4 GB |
1048 | -- Defend against junk stuff if errors already detected |
1049 | ||
1050 | if Total_Errors_Detected /= 0 then | |
1051 | if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then | |
1052 | return; | |
1053 | elsif Nkind (N) in N_Has_Chars | |
1054 | and then Chars (N) in Error_Name_Or_No_Name | |
1055 | then | |
1056 | return; | |
1057 | end if; | |
fbf5a39b AC |
1058 | |
1059 | Require_Entity (N); | |
996ae0b0 RK |
1060 | end if; |
1061 | ||
45fc7ddb HK |
1062 | -- If the context expects a value, and the name is a procedure, this is |
1063 | -- most likely a missing 'Access. Don't try to resolve the parameterless | |
1064 | -- call, error will be caught when the outer call is analyzed. | |
18c0ecbe AC |
1065 | |
1066 | if Is_Entity_Name (N) | |
1067 | and then Ekind (Entity (N)) = E_Procedure | |
1068 | and then not Is_Overloaded (N) | |
1069 | and then | |
45fc7ddb HK |
1070 | Nkind_In (Parent (N), N_Parameter_Association, |
1071 | N_Function_Call, | |
1072 | N_Procedure_Call_Statement) | |
18c0ecbe AC |
1073 | then |
1074 | return; | |
1075 | end if; | |
1076 | ||
45fc7ddb HK |
1077 | -- Rewrite as call if overloadable entity that is (or could be, in the |
1078 | -- overloaded case) a function call. If we know for sure that the entity | |
1079 | -- is an enumeration literal, we do not rewrite it. | |
f4b049db | 1080 | |
e1d9659d AC |
1081 | -- If the entity is the name of an operator, it cannot be a call because |
1082 | -- operators cannot have default parameters. In this case, this must be | |
1083 | -- a string whose contents coincide with an operator name. Set the kind | |
96d2756f | 1084 | -- of the node appropriately. |
996ae0b0 RK |
1085 | |
1086 | if (Is_Entity_Name (N) | |
e1d9659d | 1087 | and then Nkind (N) /= N_Operator_Symbol |
996ae0b0 RK |
1088 | and then Is_Overloadable (Entity (N)) |
1089 | and then (Ekind (Entity (N)) /= E_Enumeration_Literal | |
964f13da | 1090 | or else Is_Overloaded (N))) |
996ae0b0 | 1091 | |
09494c32 | 1092 | -- Rewrite as call if it is an explicit dereference of an expression of |
f3d57416 | 1093 | -- a subprogram access type, and the subprogram type is not that of a |
996ae0b0 RK |
1094 | -- procedure or entry. |
1095 | ||
1096 | or else | |
bc5f3720 | 1097 | (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp) |
996ae0b0 RK |
1098 | |
1099 | -- Rewrite as call if it is a selected component which is a function, | |
1100 | -- this is the case of a call to a protected function (which may be | |
1101 | -- overloaded with other protected operations). | |
1102 | ||
1103 | or else | |
1104 | (Nkind (N) = N_Selected_Component | |
1105 | and then (Ekind (Entity (Selector_Name (N))) = E_Function | |
964f13da RD |
1106 | or else |
1107 | (Ekind_In (Entity (Selector_Name (N)), E_Entry, | |
1108 | E_Procedure) | |
1109 | and then Is_Overloaded (Selector_Name (N))))) | |
996ae0b0 RK |
1110 | |
1111 | -- If one of the above three conditions is met, rewrite as call. | |
1112 | -- Apply the rewriting only once. | |
1113 | ||
1114 | then | |
1115 | if Nkind (Parent (N)) /= N_Function_Call | |
1116 | or else N /= Name (Parent (N)) | |
1117 | then | |
1118 | Nam := New_Copy (N); | |
1119 | ||
bc5f3720 | 1120 | -- If overloaded, overload set belongs to new copy |
996ae0b0 RK |
1121 | |
1122 | Save_Interps (N, Nam); | |
1123 | ||
1124 | -- Change node to parameterless function call (note that the | |
1125 | -- Parameter_Associations associations field is left set to Empty, | |
1126 | -- its normal default value since there are no parameters) | |
1127 | ||
1128 | Change_Node (N, N_Function_Call); | |
1129 | Set_Name (N, Nam); | |
1130 | Set_Sloc (N, Sloc (Nam)); | |
1131 | Analyze_Call (N); | |
1132 | end if; | |
1133 | ||
1134 | elsif Nkind (N) = N_Parameter_Association then | |
1135 | Check_Parameterless_Call (Explicit_Actual_Parameter (N)); | |
e1d9659d AC |
1136 | |
1137 | elsif Nkind (N) = N_Operator_Symbol then | |
1138 | Change_Operator_Symbol_To_String_Literal (N); | |
1139 | Set_Is_Overloaded (N, False); | |
1140 | Set_Etype (N, Any_String); | |
996ae0b0 RK |
1141 | end if; |
1142 | end Check_Parameterless_Call; | |
1143 | ||
67ce0d7e RD |
1144 | ----------------------------- |
1145 | -- Is_Definite_Access_Type -- | |
1146 | ----------------------------- | |
1147 | ||
1148 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean is | |
1149 | Btyp : constant Entity_Id := Base_Type (E); | |
1150 | begin | |
1151 | return Ekind (Btyp) = E_Access_Type | |
1152 | or else (Ekind (Btyp) = E_Access_Subprogram_Type | |
b4a4936b | 1153 | and then Comes_From_Source (Btyp)); |
67ce0d7e RD |
1154 | end Is_Definite_Access_Type; |
1155 | ||
996ae0b0 RK |
1156 | ---------------------- |
1157 | -- Is_Predefined_Op -- | |
1158 | ---------------------- | |
1159 | ||
1160 | function Is_Predefined_Op (Nam : Entity_Id) return Boolean is | |
1161 | begin | |
6a497607 AC |
1162 | -- Predefined operators are intrinsic subprograms |
1163 | ||
1164 | if not Is_Intrinsic_Subprogram (Nam) then | |
1165 | return False; | |
1166 | end if; | |
1167 | ||
1168 | -- A call to a back-end builtin is never a predefined operator | |
1169 | ||
1170 | if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then | |
1171 | return False; | |
1172 | end if; | |
1173 | ||
1174 | return not Is_Generic_Instance (Nam) | |
996ae0b0 | 1175 | and then Chars (Nam) in Any_Operator_Name |
6a497607 | 1176 | and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam))); |
996ae0b0 RK |
1177 | end Is_Predefined_Op; |
1178 | ||
1179 | ----------------------------- | |
1180 | -- Make_Call_Into_Operator -- | |
1181 | ----------------------------- | |
1182 | ||
1183 | procedure Make_Call_Into_Operator | |
1184 | (N : Node_Id; | |
1185 | Typ : Entity_Id; | |
1186 | Op_Id : Entity_Id) | |
1187 | is | |
1188 | Op_Name : constant Name_Id := Chars (Op_Id); | |
1189 | Act1 : Node_Id := First_Actual (N); | |
1190 | Act2 : Node_Id := Next_Actual (Act1); | |
1191 | Error : Boolean := False; | |
2820d220 AC |
1192 | Func : constant Entity_Id := Entity (Name (N)); |
1193 | Is_Binary : constant Boolean := Present (Act2); | |
996ae0b0 RK |
1194 | Op_Node : Node_Id; |
1195 | Opnd_Type : Entity_Id; | |
1196 | Orig_Type : Entity_Id := Empty; | |
1197 | Pack : Entity_Id; | |
1198 | ||
1199 | type Kind_Test is access function (E : Entity_Id) return Boolean; | |
1200 | ||
996ae0b0 | 1201 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean; |
b4a4936b AC |
1202 | -- If the operand is not universal, and the operator is given by an |
1203 | -- expanded name, verify that the operand has an interpretation with a | |
1204 | -- type defined in the given scope of the operator. | |
996ae0b0 RK |
1205 | |
1206 | function Type_In_P (Test : Kind_Test) return Entity_Id; | |
b4a4936b AC |
1207 | -- Find a type of the given class in package Pack that contains the |
1208 | -- operator. | |
996ae0b0 | 1209 | |
996ae0b0 RK |
1210 | --------------------------- |
1211 | -- Operand_Type_In_Scope -- | |
1212 | --------------------------- | |
1213 | ||
1214 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean is | |
1215 | Nod : constant Node_Id := Right_Opnd (Op_Node); | |
1216 | I : Interp_Index; | |
1217 | It : Interp; | |
1218 | ||
1219 | begin | |
1220 | if not Is_Overloaded (Nod) then | |
1221 | return Scope (Base_Type (Etype (Nod))) = S; | |
1222 | ||
1223 | else | |
1224 | Get_First_Interp (Nod, I, It); | |
996ae0b0 | 1225 | while Present (It.Typ) loop |
996ae0b0 RK |
1226 | if Scope (Base_Type (It.Typ)) = S then |
1227 | return True; | |
1228 | end if; | |
1229 | ||
1230 | Get_Next_Interp (I, It); | |
1231 | end loop; | |
1232 | ||
1233 | return False; | |
1234 | end if; | |
1235 | end Operand_Type_In_Scope; | |
1236 | ||
1237 | --------------- | |
1238 | -- Type_In_P -- | |
1239 | --------------- | |
1240 | ||
1241 | function Type_In_P (Test : Kind_Test) return Entity_Id is | |
1242 | E : Entity_Id; | |
1243 | ||
1244 | function In_Decl return Boolean; | |
1245 | -- Verify that node is not part of the type declaration for the | |
1246 | -- candidate type, which would otherwise be invisible. | |
1247 | ||
1248 | ------------- | |
1249 | -- In_Decl -- | |
1250 | ------------- | |
1251 | ||
1252 | function In_Decl return Boolean is | |
1253 | Decl_Node : constant Node_Id := Parent (E); | |
1254 | N2 : Node_Id; | |
1255 | ||
1256 | begin | |
1257 | N2 := N; | |
1258 | ||
1259 | if Etype (E) = Any_Type then | |
1260 | return True; | |
1261 | ||
1262 | elsif No (Decl_Node) then | |
1263 | return False; | |
1264 | ||
1265 | else | |
1266 | while Present (N2) | |
1267 | and then Nkind (N2) /= N_Compilation_Unit | |
1268 | loop | |
1269 | if N2 = Decl_Node then | |
1270 | return True; | |
1271 | else | |
1272 | N2 := Parent (N2); | |
1273 | end if; | |
1274 | end loop; | |
1275 | ||
1276 | return False; | |
1277 | end if; | |
1278 | end In_Decl; | |
1279 | ||
1280 | -- Start of processing for Type_In_P | |
1281 | ||
1282 | begin | |
b4a4936b AC |
1283 | -- If the context type is declared in the prefix package, this is the |
1284 | -- desired base type. | |
996ae0b0 | 1285 | |
b4a4936b | 1286 | if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then |
996ae0b0 RK |
1287 | return Base_Type (Typ); |
1288 | ||
1289 | else | |
1290 | E := First_Entity (Pack); | |
996ae0b0 | 1291 | while Present (E) loop |
996ae0b0 RK |
1292 | if Test (E) |
1293 | and then not In_Decl | |
1294 | then | |
1295 | return E; | |
1296 | end if; | |
1297 | ||
1298 | Next_Entity (E); | |
1299 | end loop; | |
1300 | ||
1301 | return Empty; | |
1302 | end if; | |
1303 | end Type_In_P; | |
1304 | ||
996ae0b0 RK |
1305 | -- Start of processing for Make_Call_Into_Operator |
1306 | ||
1307 | begin | |
1308 | Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N)); | |
1309 | ||
1310 | -- Binary operator | |
1311 | ||
1312 | if Is_Binary then | |
1313 | Set_Left_Opnd (Op_Node, Relocate_Node (Act1)); | |
1314 | Set_Right_Opnd (Op_Node, Relocate_Node (Act2)); | |
1315 | Save_Interps (Act1, Left_Opnd (Op_Node)); | |
1316 | Save_Interps (Act2, Right_Opnd (Op_Node)); | |
1317 | Act1 := Left_Opnd (Op_Node); | |
1318 | Act2 := Right_Opnd (Op_Node); | |
1319 | ||
1320 | -- Unary operator | |
1321 | ||
1322 | else | |
1323 | Set_Right_Opnd (Op_Node, Relocate_Node (Act1)); | |
1324 | Save_Interps (Act1, Right_Opnd (Op_Node)); | |
1325 | Act1 := Right_Opnd (Op_Node); | |
1326 | end if; | |
1327 | ||
1328 | -- If the operator is denoted by an expanded name, and the prefix is | |
1329 | -- not Standard, but the operator is a predefined one whose scope is | |
1330 | -- Standard, then this is an implicit_operator, inserted as an | |
1331 | -- interpretation by the procedure of the same name. This procedure | |
1332 | -- overestimates the presence of implicit operators, because it does | |
1333 | -- not examine the type of the operands. Verify now that the operand | |
1334 | -- type appears in the given scope. If right operand is universal, | |
1335 | -- check the other operand. In the case of concatenation, either | |
1336 | -- argument can be the component type, so check the type of the result. | |
1337 | -- If both arguments are literals, look for a type of the right kind | |
1338 | -- defined in the given scope. This elaborate nonsense is brought to | |
1339 | -- you courtesy of b33302a. The type itself must be frozen, so we must | |
1340 | -- find the type of the proper class in the given scope. | |
1341 | ||
06f2efd7 TQ |
1342 | -- A final wrinkle is the multiplication operator for fixed point types, |
1343 | -- which is defined in Standard only, and not in the scope of the | |
b4a4936b | 1344 | -- fixed point type itself. |
996ae0b0 RK |
1345 | |
1346 | if Nkind (Name (N)) = N_Expanded_Name then | |
1347 | Pack := Entity (Prefix (Name (N))); | |
1348 | ||
06f2efd7 TQ |
1349 | -- If the entity being called is defined in the given package, it is |
1350 | -- a renaming of a predefined operator, and known to be legal. | |
996ae0b0 RK |
1351 | |
1352 | if Scope (Entity (Name (N))) = Pack | |
1353 | and then Pack /= Standard_Standard | |
1354 | then | |
1355 | null; | |
1356 | ||
9ebe3743 HK |
1357 | -- Visibility does not need to be checked in an instance: if the |
1358 | -- operator was not visible in the generic it has been diagnosed | |
1359 | -- already, else there is an implicit copy of it in the instance. | |
1360 | ||
1361 | elsif In_Instance then | |
1362 | null; | |
1363 | ||
12577815 | 1364 | elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide) |
996ae0b0 RK |
1365 | and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node))) |
1366 | and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node))) | |
1367 | then | |
1368 | if Pack /= Standard_Standard then | |
1369 | Error := True; | |
1370 | end if; | |
1371 | ||
b4a4936b | 1372 | -- Ada 2005 AI-420: Predefined equality on Universal_Access is |
06f2efd7 | 1373 | -- available. |
c8ef728f ES |
1374 | |
1375 | elsif Ada_Version >= Ada_05 | |
1376 | and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) | |
1377 | and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type | |
1378 | then | |
1379 | null; | |
1380 | ||
996ae0b0 RK |
1381 | else |
1382 | Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node))); | |
1383 | ||
1384 | if Op_Name = Name_Op_Concat then | |
1385 | Opnd_Type := Base_Type (Typ); | |
1386 | ||
1387 | elsif (Scope (Opnd_Type) = Standard_Standard | |
1388 | and then Is_Binary) | |
1389 | or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference | |
1390 | and then Is_Binary | |
1391 | and then not Comes_From_Source (Opnd_Type)) | |
1392 | then | |
1393 | Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node))); | |
1394 | end if; | |
1395 | ||
1396 | if Scope (Opnd_Type) = Standard_Standard then | |
1397 | ||
1398 | -- Verify that the scope contains a type that corresponds to | |
1399 | -- the given literal. Optimize the case where Pack is Standard. | |
1400 | ||
1401 | if Pack /= Standard_Standard then | |
1402 | ||
1403 | if Opnd_Type = Universal_Integer then | |
06f2efd7 | 1404 | Orig_Type := Type_In_P (Is_Integer_Type'Access); |
996ae0b0 RK |
1405 | |
1406 | elsif Opnd_Type = Universal_Real then | |
1407 | Orig_Type := Type_In_P (Is_Real_Type'Access); | |
1408 | ||
1409 | elsif Opnd_Type = Any_String then | |
1410 | Orig_Type := Type_In_P (Is_String_Type'Access); | |
1411 | ||
1412 | elsif Opnd_Type = Any_Access then | |
06f2efd7 | 1413 | Orig_Type := Type_In_P (Is_Definite_Access_Type'Access); |
996ae0b0 RK |
1414 | |
1415 | elsif Opnd_Type = Any_Composite then | |
1416 | Orig_Type := Type_In_P (Is_Composite_Type'Access); | |
1417 | ||
1418 | if Present (Orig_Type) then | |
1419 | if Has_Private_Component (Orig_Type) then | |
1420 | Orig_Type := Empty; | |
1421 | else | |
1422 | Set_Etype (Act1, Orig_Type); | |
1423 | ||
1424 | if Is_Binary then | |
1425 | Set_Etype (Act2, Orig_Type); | |
1426 | end if; | |
1427 | end if; | |
1428 | end if; | |
1429 | ||
1430 | else | |
1431 | Orig_Type := Empty; | |
1432 | end if; | |
1433 | ||
1434 | Error := No (Orig_Type); | |
1435 | end if; | |
1436 | ||
1437 | elsif Ekind (Opnd_Type) = E_Allocator_Type | |
1438 | and then No (Type_In_P (Is_Definite_Access_Type'Access)) | |
1439 | then | |
1440 | Error := True; | |
1441 | ||
1442 | -- If the type is defined elsewhere, and the operator is not | |
1443 | -- defined in the given scope (by a renaming declaration, e.g.) | |
1444 | -- then this is an error as well. If an extension of System is | |
1445 | -- present, and the type may be defined there, Pack must be | |
1446 | -- System itself. | |
1447 | ||
1448 | elsif Scope (Opnd_Type) /= Pack | |
1449 | and then Scope (Op_Id) /= Pack | |
1450 | and then (No (System_Aux_Id) | |
1451 | or else Scope (Opnd_Type) /= System_Aux_Id | |
1452 | or else Pack /= Scope (System_Aux_Id)) | |
1453 | then | |
244e5a2c AC |
1454 | if not Is_Overloaded (Right_Opnd (Op_Node)) then |
1455 | Error := True; | |
1456 | else | |
1457 | Error := not Operand_Type_In_Scope (Pack); | |
1458 | end if; | |
996ae0b0 RK |
1459 | |
1460 | elsif Pack = Standard_Standard | |
1461 | and then not Operand_Type_In_Scope (Standard_Standard) | |
1462 | then | |
1463 | Error := True; | |
1464 | end if; | |
1465 | end if; | |
1466 | ||
1467 | if Error then | |
1468 | Error_Msg_Node_2 := Pack; | |
1469 | Error_Msg_NE | |
1470 | ("& not declared in&", N, Selector_Name (Name (N))); | |
1471 | Set_Etype (N, Any_Type); | |
1472 | return; | |
88b17d45 AC |
1473 | |
1474 | -- Detect a mismatch between the context type and the result type | |
1475 | -- in the named package, which is otherwise not detected if the | |
1476 | -- operands are universal. Check is only needed if source entity is | |
1477 | -- an operator, not a function that renames an operator. | |
1478 | ||
1479 | elsif Nkind (Parent (N)) /= N_Type_Conversion | |
1480 | and then Ekind (Entity (Name (N))) = E_Operator | |
1481 | and then Is_Numeric_Type (Typ) | |
1482 | and then not Is_Universal_Numeric_Type (Typ) | |
1483 | and then Scope (Base_Type (Typ)) /= Pack | |
1484 | and then not In_Instance | |
1485 | then | |
1486 | if Is_Fixed_Point_Type (Typ) | |
1487 | and then (Op_Name = Name_Op_Multiply | |
1488 | or else | |
1489 | Op_Name = Name_Op_Divide) | |
1490 | then | |
1491 | -- Already checked above | |
1492 | ||
1493 | null; | |
1494 | ||
e86a3a7e | 1495 | -- Operator may be defined in an extension of System |
80c3be7a AC |
1496 | |
1497 | elsif Present (System_Aux_Id) | |
1498 | and then Scope (Opnd_Type) = System_Aux_Id | |
1499 | then | |
1500 | null; | |
1501 | ||
88b17d45 | 1502 | else |
be5a1b93 TQ |
1503 | -- Could we use Wrong_Type here??? (this would require setting |
1504 | -- Etype (N) to the actual type found where Typ was expected). | |
1505 | ||
e86a3a7e | 1506 | Error_Msg_NE ("expect }", N, Typ); |
88b17d45 | 1507 | end if; |
996ae0b0 RK |
1508 | end if; |
1509 | end if; | |
1510 | ||
1511 | Set_Chars (Op_Node, Op_Name); | |
fbf5a39b AC |
1512 | |
1513 | if not Is_Private_Type (Etype (N)) then | |
1514 | Set_Etype (Op_Node, Base_Type (Etype (N))); | |
1515 | else | |
1516 | Set_Etype (Op_Node, Etype (N)); | |
1517 | end if; | |
1518 | ||
2820d220 AC |
1519 | -- If this is a call to a function that renames a predefined equality, |
1520 | -- the renaming declaration provides a type that must be used to | |
1521 | -- resolve the operands. This must be done now because resolution of | |
1522 | -- the equality node will not resolve any remaining ambiguity, and it | |
1523 | -- assumes that the first operand is not overloaded. | |
1524 | ||
1525 | if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) | |
1526 | and then Ekind (Func) = E_Function | |
1527 | and then Is_Overloaded (Act1) | |
1528 | then | |
1529 | Resolve (Act1, Base_Type (Etype (First_Formal (Func)))); | |
1530 | Resolve (Act2, Base_Type (Etype (First_Formal (Func)))); | |
1531 | end if; | |
1532 | ||
996ae0b0 RK |
1533 | Set_Entity (Op_Node, Op_Id); |
1534 | Generate_Reference (Op_Id, N, ' '); | |
45fc7ddb HK |
1535 | |
1536 | -- Do rewrite setting Comes_From_Source on the result if the original | |
1537 | -- call came from source. Although it is not strictly the case that the | |
1538 | -- operator as such comes from the source, logically it corresponds | |
1539 | -- exactly to the function call in the source, so it should be marked | |
1540 | -- this way (e.g. to make sure that validity checks work fine). | |
1541 | ||
1542 | declare | |
1543 | CS : constant Boolean := Comes_From_Source (N); | |
1544 | begin | |
1545 | Rewrite (N, Op_Node); | |
1546 | Set_Comes_From_Source (N, CS); | |
1547 | end; | |
fbf5a39b AC |
1548 | |
1549 | -- If this is an arithmetic operator and the result type is private, | |
1550 | -- the operands and the result must be wrapped in conversion to | |
1551 | -- expose the underlying numeric type and expand the proper checks, | |
1552 | -- e.g. on division. | |
1553 | ||
1554 | if Is_Private_Type (Typ) then | |
1555 | case Nkind (N) is | |
1556 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | | |
1557 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
1558 | Resolve_Intrinsic_Operator (N, Typ); | |
1559 | ||
1560 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => | |
1561 | Resolve_Intrinsic_Unary_Operator (N, Typ); | |
1562 | ||
1563 | when others => | |
1564 | Resolve (N, Typ); | |
1565 | end case; | |
1566 | else | |
1567 | Resolve (N, Typ); | |
1568 | end if; | |
996ae0b0 RK |
1569 | end Make_Call_Into_Operator; |
1570 | ||
1571 | ------------------- | |
1572 | -- Operator_Kind -- | |
1573 | ------------------- | |
1574 | ||
1575 | function Operator_Kind | |
1576 | (Op_Name : Name_Id; | |
0ab80019 | 1577 | Is_Binary : Boolean) return Node_Kind |
996ae0b0 RK |
1578 | is |
1579 | Kind : Node_Kind; | |
1580 | ||
1581 | begin | |
1582 | if Is_Binary then | |
aa5147f0 ES |
1583 | if Op_Name = Name_Op_And then |
1584 | Kind := N_Op_And; | |
1585 | elsif Op_Name = Name_Op_Or then | |
1586 | Kind := N_Op_Or; | |
1587 | elsif Op_Name = Name_Op_Xor then | |
1588 | Kind := N_Op_Xor; | |
1589 | elsif Op_Name = Name_Op_Eq then | |
1590 | Kind := N_Op_Eq; | |
1591 | elsif Op_Name = Name_Op_Ne then | |
1592 | Kind := N_Op_Ne; | |
1593 | elsif Op_Name = Name_Op_Lt then | |
1594 | Kind := N_Op_Lt; | |
1595 | elsif Op_Name = Name_Op_Le then | |
1596 | Kind := N_Op_Le; | |
1597 | elsif Op_Name = Name_Op_Gt then | |
1598 | Kind := N_Op_Gt; | |
1599 | elsif Op_Name = Name_Op_Ge then | |
1600 | Kind := N_Op_Ge; | |
1601 | elsif Op_Name = Name_Op_Add then | |
1602 | Kind := N_Op_Add; | |
1603 | elsif Op_Name = Name_Op_Subtract then | |
1604 | Kind := N_Op_Subtract; | |
1605 | elsif Op_Name = Name_Op_Concat then | |
1606 | Kind := N_Op_Concat; | |
1607 | elsif Op_Name = Name_Op_Multiply then | |
1608 | Kind := N_Op_Multiply; | |
1609 | elsif Op_Name = Name_Op_Divide then | |
1610 | Kind := N_Op_Divide; | |
1611 | elsif Op_Name = Name_Op_Mod then | |
1612 | Kind := N_Op_Mod; | |
1613 | elsif Op_Name = Name_Op_Rem then | |
1614 | Kind := N_Op_Rem; | |
1615 | elsif Op_Name = Name_Op_Expon then | |
1616 | Kind := N_Op_Expon; | |
996ae0b0 RK |
1617 | else |
1618 | raise Program_Error; | |
1619 | end if; | |
1620 | ||
1621 | -- Unary operators | |
1622 | ||
1623 | else | |
aa5147f0 ES |
1624 | if Op_Name = Name_Op_Add then |
1625 | Kind := N_Op_Plus; | |
1626 | elsif Op_Name = Name_Op_Subtract then | |
1627 | Kind := N_Op_Minus; | |
1628 | elsif Op_Name = Name_Op_Abs then | |
1629 | Kind := N_Op_Abs; | |
1630 | elsif Op_Name = Name_Op_Not then | |
1631 | Kind := N_Op_Not; | |
996ae0b0 RK |
1632 | else |
1633 | raise Program_Error; | |
1634 | end if; | |
1635 | end if; | |
1636 | ||
1637 | return Kind; | |
1638 | end Operator_Kind; | |
1639 | ||
45fc7ddb HK |
1640 | ---------------------------- |
1641 | -- Preanalyze_And_Resolve -- | |
1642 | ---------------------------- | |
996ae0b0 | 1643 | |
45fc7ddb | 1644 | procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is |
996ae0b0 RK |
1645 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1646 | ||
1647 | begin | |
1648 | Full_Analysis := False; | |
1649 | Expander_Mode_Save_And_Set (False); | |
1650 | ||
1651 | -- We suppress all checks for this analysis, since the checks will | |
1652 | -- be applied properly, and in the right location, when the default | |
1653 | -- expression is reanalyzed and reexpanded later on. | |
1654 | ||
1655 | Analyze_And_Resolve (N, T, Suppress => All_Checks); | |
1656 | ||
1657 | Expander_Mode_Restore; | |
1658 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1659 | end Preanalyze_And_Resolve; |
996ae0b0 | 1660 | |
a77842bd | 1661 | -- Version without context type |
996ae0b0 | 1662 | |
45fc7ddb | 1663 | procedure Preanalyze_And_Resolve (N : Node_Id) is |
996ae0b0 RK |
1664 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1665 | ||
1666 | begin | |
1667 | Full_Analysis := False; | |
1668 | Expander_Mode_Save_And_Set (False); | |
1669 | ||
1670 | Analyze (N); | |
1671 | Resolve (N, Etype (N), Suppress => All_Checks); | |
1672 | ||
1673 | Expander_Mode_Restore; | |
1674 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1675 | end Preanalyze_And_Resolve; |
996ae0b0 RK |
1676 | |
1677 | ---------------------------------- | |
1678 | -- Replace_Actual_Discriminants -- | |
1679 | ---------------------------------- | |
1680 | ||
1681 | procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is | |
1682 | Loc : constant Source_Ptr := Sloc (N); | |
1683 | Tsk : Node_Id := Empty; | |
1684 | ||
1685 | function Process_Discr (Nod : Node_Id) return Traverse_Result; | |
1686 | ||
1687 | ------------------- | |
1688 | -- Process_Discr -- | |
1689 | ------------------- | |
1690 | ||
1691 | function Process_Discr (Nod : Node_Id) return Traverse_Result is | |
1692 | Ent : Entity_Id; | |
1693 | ||
1694 | begin | |
1695 | if Nkind (Nod) = N_Identifier then | |
1696 | Ent := Entity (Nod); | |
1697 | ||
1698 | if Present (Ent) | |
1699 | and then Ekind (Ent) = E_Discriminant | |
1700 | then | |
1701 | Rewrite (Nod, | |
1702 | Make_Selected_Component (Loc, | |
1703 | Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc), | |
1704 | Selector_Name => Make_Identifier (Loc, Chars (Ent)))); | |
1705 | ||
1706 | Set_Etype (Nod, Etype (Ent)); | |
1707 | end if; | |
1708 | ||
1709 | end if; | |
1710 | ||
1711 | return OK; | |
1712 | end Process_Discr; | |
1713 | ||
1714 | procedure Replace_Discrs is new Traverse_Proc (Process_Discr); | |
1715 | ||
1716 | -- Start of processing for Replace_Actual_Discriminants | |
1717 | ||
1718 | begin | |
1719 | if not Expander_Active then | |
1720 | return; | |
1721 | end if; | |
1722 | ||
1723 | if Nkind (Name (N)) = N_Selected_Component then | |
1724 | Tsk := Prefix (Name (N)); | |
1725 | ||
1726 | elsif Nkind (Name (N)) = N_Indexed_Component then | |
1727 | Tsk := Prefix (Prefix (Name (N))); | |
1728 | end if; | |
1729 | ||
1730 | if No (Tsk) then | |
1731 | return; | |
1732 | else | |
1733 | Replace_Discrs (Default); | |
1734 | end if; | |
1735 | end Replace_Actual_Discriminants; | |
1736 | ||
1737 | ------------- | |
1738 | -- Resolve -- | |
1739 | ------------- | |
1740 | ||
1741 | procedure Resolve (N : Node_Id; Typ : Entity_Id) is | |
dae2b8ea HK |
1742 | Ambiguous : Boolean := False; |
1743 | Ctx_Type : Entity_Id := Typ; | |
1744 | Expr_Type : Entity_Id := Empty; -- prevent junk warning | |
1745 | Err_Type : Entity_Id := Empty; | |
1746 | Found : Boolean := False; | |
1747 | From_Lib : Boolean; | |
996ae0b0 | 1748 | I : Interp_Index; |
dae2b8ea | 1749 | I1 : Interp_Index := 0; -- prevent junk warning |
996ae0b0 RK |
1750 | It : Interp; |
1751 | It1 : Interp; | |
996ae0b0 | 1752 | Seen : Entity_Id := Empty; -- prevent junk warning |
dae2b8ea HK |
1753 | |
1754 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean; | |
1755 | -- Determine whether a node comes from a predefined library unit or | |
1756 | -- Standard. | |
996ae0b0 RK |
1757 | |
1758 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id); | |
1759 | -- Try and fix up a literal so that it matches its expected type. New | |
1760 | -- literals are manufactured if necessary to avoid cascaded errors. | |
1761 | ||
7415029d AC |
1762 | procedure Report_Ambiguous_Argument; |
1763 | -- Additional diagnostics when an ambiguous call has an ambiguous | |
1764 | -- argument (typically a controlling actual). | |
1765 | ||
996ae0b0 RK |
1766 | procedure Resolution_Failed; |
1767 | -- Called when attempt at resolving current expression fails | |
1768 | ||
dae2b8ea HK |
1769 | ------------------------------------ |
1770 | -- Comes_From_Predefined_Lib_Unit -- | |
1771 | ------------------------------------- | |
1772 | ||
1773 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is | |
1774 | begin | |
1775 | return | |
1776 | Sloc (Nod) = Standard_Location | |
1777 | or else Is_Predefined_File_Name (Unit_File_Name ( | |
1778 | Get_Source_Unit (Sloc (Nod)))); | |
1779 | end Comes_From_Predefined_Lib_Unit; | |
1780 | ||
996ae0b0 RK |
1781 | -------------------- |
1782 | -- Patch_Up_Value -- | |
1783 | -------------------- | |
1784 | ||
1785 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is | |
1786 | begin | |
1787 | if Nkind (N) = N_Integer_Literal | |
1788 | and then Is_Real_Type (Typ) | |
1789 | then | |
1790 | Rewrite (N, | |
1791 | Make_Real_Literal (Sloc (N), | |
1792 | Realval => UR_From_Uint (Intval (N)))); | |
1793 | Set_Etype (N, Universal_Real); | |
1794 | Set_Is_Static_Expression (N); | |
1795 | ||
1796 | elsif Nkind (N) = N_Real_Literal | |
1797 | and then Is_Integer_Type (Typ) | |
1798 | then | |
1799 | Rewrite (N, | |
1800 | Make_Integer_Literal (Sloc (N), | |
1801 | Intval => UR_To_Uint (Realval (N)))); | |
1802 | Set_Etype (N, Universal_Integer); | |
1803 | Set_Is_Static_Expression (N); | |
45fc7ddb | 1804 | |
996ae0b0 RK |
1805 | elsif Nkind (N) = N_String_Literal |
1806 | and then Is_Character_Type (Typ) | |
1807 | then | |
1808 | Set_Character_Literal_Name (Char_Code (Character'Pos ('A'))); | |
1809 | Rewrite (N, | |
1810 | Make_Character_Literal (Sloc (N), | |
1811 | Chars => Name_Find, | |
82c80734 RD |
1812 | Char_Literal_Value => |
1813 | UI_From_Int (Character'Pos ('A')))); | |
996ae0b0 RK |
1814 | Set_Etype (N, Any_Character); |
1815 | Set_Is_Static_Expression (N); | |
1816 | ||
1817 | elsif Nkind (N) /= N_String_Literal | |
1818 | and then Is_String_Type (Typ) | |
1819 | then | |
1820 | Rewrite (N, | |
1821 | Make_String_Literal (Sloc (N), | |
1822 | Strval => End_String)); | |
1823 | ||
1824 | elsif Nkind (N) = N_Range then | |
1825 | Patch_Up_Value (Low_Bound (N), Typ); | |
1826 | Patch_Up_Value (High_Bound (N), Typ); | |
1827 | end if; | |
1828 | end Patch_Up_Value; | |
1829 | ||
7415029d AC |
1830 | ------------------------------- |
1831 | -- Report_Ambiguous_Argument -- | |
1832 | ------------------------------- | |
1833 | ||
1834 | procedure Report_Ambiguous_Argument is | |
1835 | Arg : constant Node_Id := First (Parameter_Associations (N)); | |
1836 | I : Interp_Index; | |
1837 | It : Interp; | |
1838 | ||
1839 | begin | |
1840 | if Nkind (Arg) = N_Function_Call | |
1841 | and then Is_Entity_Name (Name (Arg)) | |
1842 | and then Is_Overloaded (Name (Arg)) | |
1843 | then | |
ed2233dc | 1844 | Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg)); |
7415029d | 1845 | |
bfc07071 AC |
1846 | -- Could use comments on what is going on here ??? |
1847 | ||
7415029d AC |
1848 | Get_First_Interp (Name (Arg), I, It); |
1849 | while Present (It.Nam) loop | |
1850 | Error_Msg_Sloc := Sloc (It.Nam); | |
1851 | ||
1852 | if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then | |
ed2233dc | 1853 | Error_Msg_N ("interpretation (inherited) #!", Arg); |
7415029d | 1854 | else |
ed2233dc | 1855 | Error_Msg_N ("interpretation #!", Arg); |
7415029d AC |
1856 | end if; |
1857 | ||
1858 | Get_Next_Interp (I, It); | |
1859 | end loop; | |
1860 | end if; | |
1861 | end Report_Ambiguous_Argument; | |
1862 | ||
996ae0b0 RK |
1863 | ----------------------- |
1864 | -- Resolution_Failed -- | |
1865 | ----------------------- | |
1866 | ||
1867 | procedure Resolution_Failed is | |
1868 | begin | |
1869 | Patch_Up_Value (N, Typ); | |
1870 | Set_Etype (N, Typ); | |
1871 | Debug_A_Exit ("resolving ", N, " (done, resolution failed)"); | |
1872 | Set_Is_Overloaded (N, False); | |
1873 | ||
1874 | -- The caller will return without calling the expander, so we need | |
1875 | -- to set the analyzed flag. Note that it is fine to set Analyzed | |
1876 | -- to True even if we are in the middle of a shallow analysis, | |
1877 | -- (see the spec of sem for more details) since this is an error | |
1878 | -- situation anyway, and there is no point in repeating the | |
1879 | -- analysis later (indeed it won't work to repeat it later, since | |
1880 | -- we haven't got a clear resolution of which entity is being | |
1881 | -- referenced.) | |
1882 | ||
1883 | Set_Analyzed (N, True); | |
1884 | return; | |
1885 | end Resolution_Failed; | |
1886 | ||
1887 | -- Start of processing for Resolve | |
1888 | ||
1889 | begin | |
5c736541 RD |
1890 | if N = Error then |
1891 | return; | |
1892 | end if; | |
1893 | ||
996ae0b0 RK |
1894 | -- Access attribute on remote subprogram cannot be used for |
1895 | -- a non-remote access-to-subprogram type. | |
1896 | ||
1897 | if Nkind (N) = N_Attribute_Reference | |
1898 | and then (Attribute_Name (N) = Name_Access | |
ea985d95 RD |
1899 | or else Attribute_Name (N) = Name_Unrestricted_Access |
1900 | or else Attribute_Name (N) = Name_Unchecked_Access) | |
996ae0b0 RK |
1901 | and then Comes_From_Source (N) |
1902 | and then Is_Entity_Name (Prefix (N)) | |
1903 | and then Is_Subprogram (Entity (Prefix (N))) | |
1904 | and then Is_Remote_Call_Interface (Entity (Prefix (N))) | |
1905 | and then not Is_Remote_Access_To_Subprogram_Type (Typ) | |
1906 | then | |
1907 | Error_Msg_N | |
1908 | ("prefix must statically denote a non-remote subprogram", N); | |
1909 | end if; | |
1910 | ||
dae2b8ea HK |
1911 | From_Lib := Comes_From_Predefined_Lib_Unit (N); |
1912 | ||
996ae0b0 RK |
1913 | -- If the context is a Remote_Access_To_Subprogram, access attributes |
1914 | -- must be resolved with the corresponding fat pointer. There is no need | |
1915 | -- to check for the attribute name since the return type of an | |
1916 | -- attribute is never a remote type. | |
1917 | ||
1918 | if Nkind (N) = N_Attribute_Reference | |
1919 | and then Comes_From_Source (N) | |
1920 | and then (Is_Remote_Call_Interface (Typ) | |
1921 | or else Is_Remote_Types (Typ)) | |
1922 | then | |
1923 | declare | |
1924 | Attr : constant Attribute_Id := | |
1925 | Get_Attribute_Id (Attribute_Name (N)); | |
1926 | Pref : constant Node_Id := Prefix (N); | |
1927 | Decl : Node_Id; | |
1928 | Spec : Node_Id; | |
1929 | Is_Remote : Boolean := True; | |
1930 | ||
1931 | begin | |
a77842bd | 1932 | -- Check that Typ is a remote access-to-subprogram type |
996ae0b0 | 1933 | |
a77842bd | 1934 | if Is_Remote_Access_To_Subprogram_Type (Typ) then |
955871d3 | 1935 | |
996ae0b0 RK |
1936 | -- Prefix (N) must statically denote a remote subprogram |
1937 | -- declared in a package specification. | |
1938 | ||
1939 | if Attr = Attribute_Access then | |
1940 | Decl := Unit_Declaration_Node (Entity (Pref)); | |
1941 | ||
1942 | if Nkind (Decl) = N_Subprogram_Body then | |
1943 | Spec := Corresponding_Spec (Decl); | |
1944 | ||
1945 | if not No (Spec) then | |
1946 | Decl := Unit_Declaration_Node (Spec); | |
1947 | end if; | |
1948 | end if; | |
1949 | ||
1950 | Spec := Parent (Decl); | |
1951 | ||
1952 | if not Is_Entity_Name (Prefix (N)) | |
1953 | or else Nkind (Spec) /= N_Package_Specification | |
1954 | or else | |
1955 | not Is_Remote_Call_Interface (Defining_Entity (Spec)) | |
1956 | then | |
1957 | Is_Remote := False; | |
1958 | Error_Msg_N | |
1959 | ("prefix must statically denote a remote subprogram ", | |
1960 | N); | |
1961 | end if; | |
1962 | end if; | |
1963 | ||
fbf5a39b AC |
1964 | -- If we are generating code for a distributed program. |
1965 | -- perform semantic checks against the corresponding | |
1966 | -- remote entities. | |
1967 | ||
1968 | if (Attr = Attribute_Access | |
1969 | or else Attr = Attribute_Unchecked_Access | |
1970 | or else Attr = Attribute_Unrestricted_Access) | |
1971 | and then Expander_Active | |
a77842bd | 1972 | and then Get_PCS_Name /= Name_No_DSA |
996ae0b0 RK |
1973 | then |
1974 | Check_Subtype_Conformant | |
1975 | (New_Id => Entity (Prefix (N)), | |
1976 | Old_Id => Designated_Type | |
1977 | (Corresponding_Remote_Type (Typ)), | |
1978 | Err_Loc => N); | |
b7d1f17f | 1979 | |
996ae0b0 RK |
1980 | if Is_Remote then |
1981 | Process_Remote_AST_Attribute (N, Typ); | |
1982 | end if; | |
1983 | end if; | |
1984 | end if; | |
1985 | end; | |
1986 | end if; | |
1987 | ||
1988 | Debug_A_Entry ("resolving ", N); | |
1989 | ||
07fc65c4 GB |
1990 | if Comes_From_Source (N) then |
1991 | if Is_Fixed_Point_Type (Typ) then | |
1992 | Check_Restriction (No_Fixed_Point, N); | |
996ae0b0 | 1993 | |
07fc65c4 GB |
1994 | elsif Is_Floating_Point_Type (Typ) |
1995 | and then Typ /= Universal_Real | |
1996 | and then Typ /= Any_Real | |
1997 | then | |
1998 | Check_Restriction (No_Floating_Point, N); | |
1999 | end if; | |
996ae0b0 RK |
2000 | end if; |
2001 | ||
2002 | -- Return if already analyzed | |
2003 | ||
2004 | if Analyzed (N) then | |
2005 | Debug_A_Exit ("resolving ", N, " (done, already analyzed)"); | |
2006 | return; | |
2007 | ||
2008 | -- Return if type = Any_Type (previous error encountered) | |
2009 | ||
2010 | elsif Etype (N) = Any_Type then | |
2011 | Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)"); | |
2012 | return; | |
2013 | end if; | |
2014 | ||
2015 | Check_Parameterless_Call (N); | |
2016 | ||
2017 | -- If not overloaded, then we know the type, and all that needs doing | |
2018 | -- is to check that this type is compatible with the context. | |
2019 | ||
2020 | if not Is_Overloaded (N) then | |
2021 | Found := Covers (Typ, Etype (N)); | |
2022 | Expr_Type := Etype (N); | |
2023 | ||
2024 | -- In the overloaded case, we must select the interpretation that | |
2025 | -- is compatible with the context (i.e. the type passed to Resolve) | |
2026 | ||
2027 | else | |
996ae0b0 RK |
2028 | -- Loop through possible interpretations |
2029 | ||
1420b484 | 2030 | Get_First_Interp (N, I, It); |
996ae0b0 RK |
2031 | Interp_Loop : while Present (It.Typ) loop |
2032 | ||
2033 | -- We are only interested in interpretations that are compatible | |
aa5147f0 | 2034 | -- with the expected type, any other interpretations are ignored. |
996ae0b0 | 2035 | |
fbf5a39b AC |
2036 | if not Covers (Typ, It.Typ) then |
2037 | if Debug_Flag_V then | |
2038 | Write_Str (" interpretation incompatible with context"); | |
2039 | Write_Eol; | |
2040 | end if; | |
996ae0b0 | 2041 | |
fbf5a39b | 2042 | else |
aa5147f0 ES |
2043 | -- Skip the current interpretation if it is disabled by an |
2044 | -- abstract operator. This action is performed only when the | |
2045 | -- type against which we are resolving is the same as the | |
2046 | -- type of the interpretation. | |
2047 | ||
2048 | if Ada_Version >= Ada_05 | |
2049 | and then It.Typ = Typ | |
2050 | and then Typ /= Universal_Integer | |
2051 | and then Typ /= Universal_Real | |
2052 | and then Present (It.Abstract_Op) | |
2053 | then | |
2054 | goto Continue; | |
2055 | end if; | |
2056 | ||
996ae0b0 RK |
2057 | -- First matching interpretation |
2058 | ||
2059 | if not Found then | |
2060 | Found := True; | |
2061 | I1 := I; | |
2062 | Seen := It.Nam; | |
2063 | Expr_Type := It.Typ; | |
2064 | ||
fbf5a39b | 2065 | -- Matching interpretation that is not the first, maybe an |
996ae0b0 RK |
2066 | -- error, but there are some cases where preference rules are |
2067 | -- used to choose between the two possibilities. These and | |
2068 | -- some more obscure cases are handled in Disambiguate. | |
2069 | ||
2070 | else | |
dae2b8ea HK |
2071 | -- If the current statement is part of a predefined library |
2072 | -- unit, then all interpretations which come from user level | |
2073 | -- packages should not be considered. | |
2074 | ||
2075 | if From_Lib | |
2076 | and then not Comes_From_Predefined_Lib_Unit (It.Nam) | |
2077 | then | |
2078 | goto Continue; | |
2079 | end if; | |
2080 | ||
996ae0b0 RK |
2081 | Error_Msg_Sloc := Sloc (Seen); |
2082 | It1 := Disambiguate (N, I1, I, Typ); | |
2083 | ||
fbf5a39b AC |
2084 | -- Disambiguation has succeeded. Skip the remaining |
2085 | -- interpretations. | |
996ae0b0 | 2086 | |
fbf5a39b AC |
2087 | if It1 /= No_Interp then |
2088 | Seen := It1.Nam; | |
2089 | Expr_Type := It1.Typ; | |
2090 | ||
2091 | while Present (It.Typ) loop | |
2092 | Get_Next_Interp (I, It); | |
2093 | end loop; | |
2094 | ||
2095 | else | |
996ae0b0 RK |
2096 | -- Before we issue an ambiguity complaint, check for |
2097 | -- the case of a subprogram call where at least one | |
2098 | -- of the arguments is Any_Type, and if so, suppress | |
2099 | -- the message, since it is a cascaded error. | |
2100 | ||
45fc7ddb HK |
2101 | if Nkind_In (N, N_Function_Call, |
2102 | N_Procedure_Call_Statement) | |
996ae0b0 RK |
2103 | then |
2104 | declare | |
1420b484 | 2105 | A : Node_Id; |
996ae0b0 RK |
2106 | E : Node_Id; |
2107 | ||
2108 | begin | |
1420b484 | 2109 | A := First_Actual (N); |
996ae0b0 RK |
2110 | while Present (A) loop |
2111 | E := A; | |
2112 | ||
2113 | if Nkind (E) = N_Parameter_Association then | |
2114 | E := Explicit_Actual_Parameter (E); | |
2115 | end if; | |
2116 | ||
2117 | if Etype (E) = Any_Type then | |
2118 | if Debug_Flag_V then | |
2119 | Write_Str ("Any_Type in call"); | |
2120 | Write_Eol; | |
2121 | end if; | |
2122 | ||
2123 | exit Interp_Loop; | |
2124 | end if; | |
2125 | ||
2126 | Next_Actual (A); | |
2127 | end loop; | |
2128 | end; | |
2129 | ||
aa5147f0 | 2130 | elsif Nkind (N) in N_Binary_Op |
996ae0b0 RK |
2131 | and then (Etype (Left_Opnd (N)) = Any_Type |
2132 | or else Etype (Right_Opnd (N)) = Any_Type) | |
2133 | then | |
2134 | exit Interp_Loop; | |
2135 | ||
2136 | elsif Nkind (N) in N_Unary_Op | |
2137 | and then Etype (Right_Opnd (N)) = Any_Type | |
2138 | then | |
2139 | exit Interp_Loop; | |
2140 | end if; | |
2141 | ||
2142 | -- Not that special case, so issue message using the | |
2143 | -- flag Ambiguous to control printing of the header | |
2144 | -- message only at the start of an ambiguous set. | |
2145 | ||
2146 | if not Ambiguous then | |
aa180613 RD |
2147 | if Nkind (N) = N_Function_Call |
2148 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2149 | then | |
ed2233dc | 2150 | Error_Msg_N |
aa180613 RD |
2151 | ("ambiguous expression " |
2152 | & "(cannot resolve indirect call)!", N); | |
2153 | else | |
483c78cb | 2154 | Error_Msg_NE -- CODEFIX |
aa180613 RD |
2155 | ("ambiguous expression (cannot resolve&)!", |
2156 | N, It.Nam); | |
2157 | end if; | |
fbf5a39b | 2158 | |
996ae0b0 | 2159 | Ambiguous := True; |
0669bebe GB |
2160 | |
2161 | if Nkind (Parent (Seen)) = N_Full_Type_Declaration then | |
ed2233dc | 2162 | Error_Msg_N |
0669bebe GB |
2163 | ("\\possible interpretation (inherited)#!", N); |
2164 | else | |
4e7a4f6e AC |
2165 | Error_Msg_N -- CODEFIX |
2166 | ("\\possible interpretation#!", N); | |
0669bebe | 2167 | end if; |
7415029d AC |
2168 | |
2169 | if Nkind_In | |
4519314c | 2170 | (N, N_Procedure_Call_Statement, N_Function_Call) |
7415029d AC |
2171 | and then Present (Parameter_Associations (N)) |
2172 | then | |
2173 | Report_Ambiguous_Argument; | |
2174 | end if; | |
996ae0b0 RK |
2175 | end if; |
2176 | ||
2177 | Error_Msg_Sloc := Sloc (It.Nam); | |
996ae0b0 | 2178 | |
fbf5a39b | 2179 | -- By default, the error message refers to the candidate |
0669bebe GB |
2180 | -- interpretation. But if it is a predefined operator, it |
2181 | -- is implicitly declared at the declaration of the type | |
2182 | -- of the operand. Recover the sloc of that declaration | |
2183 | -- for the error message. | |
fbf5a39b AC |
2184 | |
2185 | if Nkind (N) in N_Op | |
2186 | and then Scope (It.Nam) = Standard_Standard | |
2187 | and then not Is_Overloaded (Right_Opnd (N)) | |
0669bebe GB |
2188 | and then Scope (Base_Type (Etype (Right_Opnd (N)))) /= |
2189 | Standard_Standard | |
fbf5a39b AC |
2190 | then |
2191 | Err_Type := First_Subtype (Etype (Right_Opnd (N))); | |
2192 | ||
2193 | if Comes_From_Source (Err_Type) | |
2194 | and then Present (Parent (Err_Type)) | |
2195 | then | |
2196 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2197 | end if; | |
2198 | ||
2199 | elsif Nkind (N) in N_Binary_Op | |
2200 | and then Scope (It.Nam) = Standard_Standard | |
2201 | and then not Is_Overloaded (Left_Opnd (N)) | |
0669bebe GB |
2202 | and then Scope (Base_Type (Etype (Left_Opnd (N)))) /= |
2203 | Standard_Standard | |
fbf5a39b AC |
2204 | then |
2205 | Err_Type := First_Subtype (Etype (Left_Opnd (N))); | |
2206 | ||
2207 | if Comes_From_Source (Err_Type) | |
2208 | and then Present (Parent (Err_Type)) | |
2209 | then | |
2210 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2211 | end if; | |
aa180613 RD |
2212 | |
2213 | -- If this is an indirect call, use the subprogram_type | |
2214 | -- in the message, to have a meaningful location. | |
4519314c | 2215 | -- Also indicate if this is an inherited operation, |
aa180613 RD |
2216 | -- created by a type declaration. |
2217 | ||
2218 | elsif Nkind (N) = N_Function_Call | |
2219 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2220 | and then Is_Type (It.Nam) | |
2221 | then | |
2222 | Err_Type := It.Nam; | |
2223 | Error_Msg_Sloc := | |
2224 | Sloc (Associated_Node_For_Itype (Err_Type)); | |
fbf5a39b AC |
2225 | else |
2226 | Err_Type := Empty; | |
2227 | end if; | |
2228 | ||
2229 | if Nkind (N) in N_Op | |
2230 | and then Scope (It.Nam) = Standard_Standard | |
2231 | and then Present (Err_Type) | |
2232 | then | |
aa5147f0 ES |
2233 | -- Special-case the message for universal_fixed |
2234 | -- operators, which are not declared with the type | |
2235 | -- of the operand, but appear forever in Standard. | |
2236 | ||
2237 | if It.Typ = Universal_Fixed | |
2238 | and then Scope (It.Nam) = Standard_Standard | |
2239 | then | |
ed2233dc | 2240 | Error_Msg_N |
aa5147f0 ES |
2241 | ("\\possible interpretation as " & |
2242 | "universal_fixed operation " & | |
2243 | "(RM 4.5.5 (19))", N); | |
2244 | else | |
ed2233dc | 2245 | Error_Msg_N |
aa5147f0 ES |
2246 | ("\\possible interpretation (predefined)#!", N); |
2247 | end if; | |
aa180613 RD |
2248 | |
2249 | elsif | |
2250 | Nkind (Parent (It.Nam)) = N_Full_Type_Declaration | |
2251 | then | |
ed2233dc | 2252 | Error_Msg_N |
aa180613 | 2253 | ("\\possible interpretation (inherited)#!", N); |
fbf5a39b | 2254 | else |
4e7a4f6e AC |
2255 | Error_Msg_N -- CODEFIX |
2256 | ("\\possible interpretation#!", N); | |
fbf5a39b | 2257 | end if; |
996ae0b0 | 2258 | |
996ae0b0 RK |
2259 | end if; |
2260 | end if; | |
2261 | ||
0669bebe GB |
2262 | -- We have a matching interpretation, Expr_Type is the type |
2263 | -- from this interpretation, and Seen is the entity. | |
996ae0b0 | 2264 | |
0669bebe GB |
2265 | -- For an operator, just set the entity name. The type will be |
2266 | -- set by the specific operator resolution routine. | |
996ae0b0 RK |
2267 | |
2268 | if Nkind (N) in N_Op then | |
2269 | Set_Entity (N, Seen); | |
2270 | Generate_Reference (Seen, N); | |
2271 | ||
19d846a0 RD |
2272 | elsif Nkind (N) = N_Case_Expression then |
2273 | Set_Etype (N, Expr_Type); | |
2274 | ||
996ae0b0 RK |
2275 | elsif Nkind (N) = N_Character_Literal then |
2276 | Set_Etype (N, Expr_Type); | |
2277 | ||
e0ba1bfd ES |
2278 | elsif Nkind (N) = N_Conditional_Expression then |
2279 | Set_Etype (N, Expr_Type); | |
2280 | ||
996ae0b0 | 2281 | -- For an explicit dereference, attribute reference, range, |
0669bebe GB |
2282 | -- short-circuit form (which is not an operator node), or call |
2283 | -- with a name that is an explicit dereference, there is | |
2284 | -- nothing to be done at this point. | |
996ae0b0 | 2285 | |
45fc7ddb HK |
2286 | elsif Nkind_In (N, N_Explicit_Dereference, |
2287 | N_Attribute_Reference, | |
2288 | N_And_Then, | |
2289 | N_Indexed_Component, | |
2290 | N_Or_Else, | |
2291 | N_Range, | |
2292 | N_Selected_Component, | |
2293 | N_Slice) | |
996ae0b0 RK |
2294 | or else Nkind (Name (N)) = N_Explicit_Dereference |
2295 | then | |
2296 | null; | |
2297 | ||
0669bebe | 2298 | -- For procedure or function calls, set the type of the name, |
4519314c | 2299 | -- and also the entity pointer for the prefix. |
996ae0b0 | 2300 | |
45fc7ddb | 2301 | elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call) |
a3f2babd | 2302 | and then Is_Entity_Name (Name (N)) |
996ae0b0 RK |
2303 | then |
2304 | Set_Etype (Name (N), Expr_Type); | |
2305 | Set_Entity (Name (N), Seen); | |
2306 | Generate_Reference (Seen, Name (N)); | |
2307 | ||
2308 | elsif Nkind (N) = N_Function_Call | |
2309 | and then Nkind (Name (N)) = N_Selected_Component | |
2310 | then | |
2311 | Set_Etype (Name (N), Expr_Type); | |
2312 | Set_Entity (Selector_Name (Name (N)), Seen); | |
2313 | Generate_Reference (Seen, Selector_Name (Name (N))); | |
2314 | ||
2315 | -- For all other cases, just set the type of the Name | |
2316 | ||
2317 | else | |
2318 | Set_Etype (Name (N), Expr_Type); | |
2319 | end if; | |
2320 | ||
996ae0b0 RK |
2321 | end if; |
2322 | ||
aa5147f0 ES |
2323 | <<Continue>> |
2324 | ||
996ae0b0 RK |
2325 | -- Move to next interpretation |
2326 | ||
c8ef728f | 2327 | exit Interp_Loop when No (It.Typ); |
996ae0b0 RK |
2328 | |
2329 | Get_Next_Interp (I, It); | |
2330 | end loop Interp_Loop; | |
2331 | end if; | |
2332 | ||
2333 | -- At this stage Found indicates whether or not an acceptable | |
4519314c AC |
2334 | -- interpretation exists. If not, then we have an error, except that if |
2335 | -- the context is Any_Type as a result of some other error, then we | |
2336 | -- suppress the error report. | |
996ae0b0 RK |
2337 | |
2338 | if not Found then | |
2339 | if Typ /= Any_Type then | |
2340 | ||
0669bebe GB |
2341 | -- If type we are looking for is Void, then this is the procedure |
2342 | -- call case, and the error is simply that what we gave is not a | |
2343 | -- procedure name (we think of procedure calls as expressions with | |
2344 | -- types internally, but the user doesn't think of them this way!) | |
996ae0b0 RK |
2345 | |
2346 | if Typ = Standard_Void_Type then | |
91b1417d AC |
2347 | |
2348 | -- Special case message if function used as a procedure | |
2349 | ||
2350 | if Nkind (N) = N_Procedure_Call_Statement | |
2351 | and then Is_Entity_Name (Name (N)) | |
2352 | and then Ekind (Entity (Name (N))) = E_Function | |
2353 | then | |
2354 | Error_Msg_NE | |
2355 | ("cannot use function & in a procedure call", | |
2356 | Name (N), Entity (Name (N))); | |
2357 | ||
0669bebe GB |
2358 | -- Otherwise give general message (not clear what cases this |
2359 | -- covers, but no harm in providing for them!) | |
91b1417d AC |
2360 | |
2361 | else | |
2362 | Error_Msg_N ("expect procedure name in procedure call", N); | |
2363 | end if; | |
2364 | ||
996ae0b0 RK |
2365 | Found := True; |
2366 | ||
2367 | -- Otherwise we do have a subexpression with the wrong type | |
2368 | ||
0669bebe GB |
2369 | -- Check for the case of an allocator which uses an access type |
2370 | -- instead of the designated type. This is a common error and we | |
2371 | -- specialize the message, posting an error on the operand of the | |
2372 | -- allocator, complaining that we expected the designated type of | |
2373 | -- the allocator. | |
996ae0b0 RK |
2374 | |
2375 | elsif Nkind (N) = N_Allocator | |
2376 | and then Ekind (Typ) in Access_Kind | |
2377 | and then Ekind (Etype (N)) in Access_Kind | |
2378 | and then Designated_Type (Etype (N)) = Typ | |
2379 | then | |
2380 | Wrong_Type (Expression (N), Designated_Type (Typ)); | |
2381 | Found := True; | |
2382 | ||
0669bebe GB |
2383 | -- Check for view mismatch on Null in instances, for which the |
2384 | -- view-swapping mechanism has no identifier. | |
17be0cdf ES |
2385 | |
2386 | elsif (In_Instance or else In_Inlined_Body) | |
2387 | and then (Nkind (N) = N_Null) | |
2388 | and then Is_Private_Type (Typ) | |
2389 | and then Is_Access_Type (Full_View (Typ)) | |
2390 | then | |
2391 | Resolve (N, Full_View (Typ)); | |
2392 | Set_Etype (N, Typ); | |
2393 | return; | |
2394 | ||
aa180613 RD |
2395 | -- Check for an aggregate. Sometimes we can get bogus aggregates |
2396 | -- from misuse of parentheses, and we are about to complain about | |
2397 | -- the aggregate without even looking inside it. | |
996ae0b0 | 2398 | |
aa180613 RD |
2399 | -- Instead, if we have an aggregate of type Any_Composite, then |
2400 | -- analyze and resolve the component fields, and then only issue | |
2401 | -- another message if we get no errors doing this (otherwise | |
2402 | -- assume that the errors in the aggregate caused the problem). | |
996ae0b0 RK |
2403 | |
2404 | elsif Nkind (N) = N_Aggregate | |
2405 | and then Etype (N) = Any_Composite | |
2406 | then | |
996ae0b0 RK |
2407 | -- Disable expansion in any case. If there is a type mismatch |
2408 | -- it may be fatal to try to expand the aggregate. The flag | |
2409 | -- would otherwise be set to false when the error is posted. | |
2410 | ||
2411 | Expander_Active := False; | |
2412 | ||
2413 | declare | |
2414 | procedure Check_Aggr (Aggr : Node_Id); | |
aa180613 RD |
2415 | -- Check one aggregate, and set Found to True if we have a |
2416 | -- definite error in any of its elements | |
996ae0b0 RK |
2417 | |
2418 | procedure Check_Elmt (Aelmt : Node_Id); | |
aa180613 RD |
2419 | -- Check one element of aggregate and set Found to True if |
2420 | -- we definitely have an error in the element. | |
2421 | ||
2422 | ---------------- | |
2423 | -- Check_Aggr -- | |
2424 | ---------------- | |
996ae0b0 RK |
2425 | |
2426 | procedure Check_Aggr (Aggr : Node_Id) is | |
2427 | Elmt : Node_Id; | |
2428 | ||
2429 | begin | |
2430 | if Present (Expressions (Aggr)) then | |
2431 | Elmt := First (Expressions (Aggr)); | |
2432 | while Present (Elmt) loop | |
2433 | Check_Elmt (Elmt); | |
2434 | Next (Elmt); | |
2435 | end loop; | |
2436 | end if; | |
2437 | ||
2438 | if Present (Component_Associations (Aggr)) then | |
2439 | Elmt := First (Component_Associations (Aggr)); | |
2440 | while Present (Elmt) loop | |
aa180613 | 2441 | |
0669bebe GB |
2442 | -- If this is a default-initialized component, then |
2443 | -- there is nothing to check. The box will be | |
2444 | -- replaced by the appropriate call during late | |
2445 | -- expansion. | |
aa180613 RD |
2446 | |
2447 | if not Box_Present (Elmt) then | |
2448 | Check_Elmt (Expression (Elmt)); | |
2449 | end if; | |
2450 | ||
996ae0b0 RK |
2451 | Next (Elmt); |
2452 | end loop; | |
2453 | end if; | |
2454 | end Check_Aggr; | |
2455 | ||
fbf5a39b AC |
2456 | ---------------- |
2457 | -- Check_Elmt -- | |
2458 | ---------------- | |
2459 | ||
996ae0b0 RK |
2460 | procedure Check_Elmt (Aelmt : Node_Id) is |
2461 | begin | |
2462 | -- If we have a nested aggregate, go inside it (to | |
2463 | -- attempt a naked analyze-resolve of the aggregate | |
2464 | -- can cause undesirable cascaded errors). Do not | |
2465 | -- resolve expression if it needs a type from context, | |
2466 | -- as for integer * fixed expression. | |
2467 | ||
2468 | if Nkind (Aelmt) = N_Aggregate then | |
2469 | Check_Aggr (Aelmt); | |
2470 | ||
2471 | else | |
2472 | Analyze (Aelmt); | |
2473 | ||
2474 | if not Is_Overloaded (Aelmt) | |
2475 | and then Etype (Aelmt) /= Any_Fixed | |
2476 | then | |
fbf5a39b | 2477 | Resolve (Aelmt); |
996ae0b0 RK |
2478 | end if; |
2479 | ||
2480 | if Etype (Aelmt) = Any_Type then | |
2481 | Found := True; | |
2482 | end if; | |
2483 | end if; | |
2484 | end Check_Elmt; | |
2485 | ||
2486 | begin | |
2487 | Check_Aggr (N); | |
2488 | end; | |
2489 | end if; | |
2490 | ||
2491 | -- If an error message was issued already, Found got reset | |
2492 | -- to True, so if it is still False, issue the standard | |
2493 | -- Wrong_Type message. | |
2494 | ||
2495 | if not Found then | |
2496 | if Is_Overloaded (N) | |
2497 | and then Nkind (N) = N_Function_Call | |
2498 | then | |
65356e64 AC |
2499 | declare |
2500 | Subp_Name : Node_Id; | |
2501 | begin | |
2502 | if Is_Entity_Name (Name (N)) then | |
2503 | Subp_Name := Name (N); | |
2504 | ||
2505 | elsif Nkind (Name (N)) = N_Selected_Component then | |
2506 | ||
a77842bd | 2507 | -- Protected operation: retrieve operation name |
65356e64 AC |
2508 | |
2509 | Subp_Name := Selector_Name (Name (N)); | |
2510 | else | |
2511 | raise Program_Error; | |
2512 | end if; | |
2513 | ||
2514 | Error_Msg_Node_2 := Typ; | |
2515 | Error_Msg_NE ("no visible interpretation of&" & | |
2516 | " matches expected type&", N, Subp_Name); | |
2517 | end; | |
996ae0b0 RK |
2518 | |
2519 | if All_Errors_Mode then | |
2520 | declare | |
2521 | Index : Interp_Index; | |
2522 | It : Interp; | |
2523 | ||
2524 | begin | |
aa180613 | 2525 | Error_Msg_N ("\\possible interpretations:", N); |
996ae0b0 | 2526 | |
1420b484 | 2527 | Get_First_Interp (Name (N), Index, It); |
996ae0b0 | 2528 | while Present (It.Nam) loop |
ea985d95 | 2529 | Error_Msg_Sloc := Sloc (It.Nam); |
aa5147f0 ES |
2530 | Error_Msg_Node_2 := It.Nam; |
2531 | Error_Msg_NE | |
2532 | ("\\ type& for & declared#", N, It.Typ); | |
996ae0b0 RK |
2533 | Get_Next_Interp (Index, It); |
2534 | end loop; | |
2535 | end; | |
aa5147f0 | 2536 | |
996ae0b0 RK |
2537 | else |
2538 | Error_Msg_N ("\use -gnatf for details", N); | |
2539 | end if; | |
2540 | else | |
2541 | Wrong_Type (N, Typ); | |
2542 | end if; | |
2543 | end if; | |
2544 | end if; | |
2545 | ||
2546 | Resolution_Failed; | |
2547 | return; | |
2548 | ||
2549 | -- Test if we have more than one interpretation for the context | |
2550 | ||
2551 | elsif Ambiguous then | |
2552 | Resolution_Failed; | |
2553 | return; | |
2554 | ||
2555 | -- Here we have an acceptable interpretation for the context | |
2556 | ||
2557 | else | |
996ae0b0 RK |
2558 | -- Propagate type information and normalize tree for various |
2559 | -- predefined operations. If the context only imposes a class of | |
2560 | -- types, rather than a specific type, propagate the actual type | |
2561 | -- downward. | |
2562 | ||
2563 | if Typ = Any_Integer | |
2564 | or else Typ = Any_Boolean | |
2565 | or else Typ = Any_Modular | |
2566 | or else Typ = Any_Real | |
2567 | or else Typ = Any_Discrete | |
2568 | then | |
2569 | Ctx_Type := Expr_Type; | |
2570 | ||
2571 | -- Any_Fixed is legal in a real context only if a specific | |
2572 | -- fixed point type is imposed. If Norman Cohen can be | |
2573 | -- confused by this, it deserves a separate message. | |
2574 | ||
2575 | if Typ = Any_Real | |
2576 | and then Expr_Type = Any_Fixed | |
2577 | then | |
758c442c | 2578 | Error_Msg_N ("illegal context for mixed mode operation", N); |
996ae0b0 RK |
2579 | Set_Etype (N, Universal_Real); |
2580 | Ctx_Type := Universal_Real; | |
2581 | end if; | |
2582 | end if; | |
2583 | ||
f3d57416 | 2584 | -- A user-defined operator is transformed into a function call at |
0ab80019 AC |
2585 | -- this point, so that further processing knows that operators are |
2586 | -- really operators (i.e. are predefined operators). User-defined | |
2587 | -- operators that are intrinsic are just renamings of the predefined | |
2588 | -- ones, and need not be turned into calls either, but if they rename | |
2589 | -- a different operator, we must transform the node accordingly. | |
2590 | -- Instantiations of Unchecked_Conversion are intrinsic but are | |
2591 | -- treated as functions, even if given an operator designator. | |
2592 | ||
2593 | if Nkind (N) in N_Op | |
2594 | and then Present (Entity (N)) | |
2595 | and then Ekind (Entity (N)) /= E_Operator | |
2596 | then | |
2597 | ||
2598 | if not Is_Predefined_Op (Entity (N)) then | |
2599 | Rewrite_Operator_As_Call (N, Entity (N)); | |
2600 | ||
615cbd95 AC |
2601 | elsif Present (Alias (Entity (N))) |
2602 | and then | |
45fc7ddb HK |
2603 | Nkind (Parent (Parent (Entity (N)))) = |
2604 | N_Subprogram_Renaming_Declaration | |
615cbd95 | 2605 | then |
0ab80019 AC |
2606 | Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ); |
2607 | ||
2608 | -- If the node is rewritten, it will be fully resolved in | |
2609 | -- Rewrite_Renamed_Operator. | |
2610 | ||
2611 | if Analyzed (N) then | |
2612 | return; | |
2613 | end if; | |
2614 | end if; | |
2615 | end if; | |
2616 | ||
996ae0b0 RK |
2617 | case N_Subexpr'(Nkind (N)) is |
2618 | ||
2619 | when N_Aggregate => Resolve_Aggregate (N, Ctx_Type); | |
2620 | ||
2621 | when N_Allocator => Resolve_Allocator (N, Ctx_Type); | |
2622 | ||
514d0fc5 | 2623 | when N_Short_Circuit |
996ae0b0 RK |
2624 | => Resolve_Short_Circuit (N, Ctx_Type); |
2625 | ||
2626 | when N_Attribute_Reference | |
2627 | => Resolve_Attribute (N, Ctx_Type); | |
2628 | ||
19d846a0 RD |
2629 | when N_Case_Expression |
2630 | => Resolve_Case_Expression (N, Ctx_Type); | |
2631 | ||
996ae0b0 RK |
2632 | when N_Character_Literal |
2633 | => Resolve_Character_Literal (N, Ctx_Type); | |
2634 | ||
2635 | when N_Conditional_Expression | |
2636 | => Resolve_Conditional_Expression (N, Ctx_Type); | |
2637 | ||
2638 | when N_Expanded_Name | |
2639 | => Resolve_Entity_Name (N, Ctx_Type); | |
2640 | ||
996ae0b0 RK |
2641 | when N_Explicit_Dereference |
2642 | => Resolve_Explicit_Dereference (N, Ctx_Type); | |
2643 | ||
955871d3 AC |
2644 | when N_Expression_With_Actions |
2645 | => Resolve_Expression_With_Actions (N, Ctx_Type); | |
2646 | ||
2647 | when N_Extension_Aggregate | |
2648 | => Resolve_Extension_Aggregate (N, Ctx_Type); | |
2649 | ||
996ae0b0 RK |
2650 | when N_Function_Call |
2651 | => Resolve_Call (N, Ctx_Type); | |
2652 | ||
2653 | when N_Identifier | |
2654 | => Resolve_Entity_Name (N, Ctx_Type); | |
2655 | ||
996ae0b0 RK |
2656 | when N_Indexed_Component |
2657 | => Resolve_Indexed_Component (N, Ctx_Type); | |
2658 | ||
2659 | when N_Integer_Literal | |
2660 | => Resolve_Integer_Literal (N, Ctx_Type); | |
2661 | ||
0669bebe GB |
2662 | when N_Membership_Test |
2663 | => Resolve_Membership_Op (N, Ctx_Type); | |
2664 | ||
996ae0b0 RK |
2665 | when N_Null => Resolve_Null (N, Ctx_Type); |
2666 | ||
2667 | when N_Op_And | N_Op_Or | N_Op_Xor | |
2668 | => Resolve_Logical_Op (N, Ctx_Type); | |
2669 | ||
2670 | when N_Op_Eq | N_Op_Ne | |
2671 | => Resolve_Equality_Op (N, Ctx_Type); | |
2672 | ||
2673 | when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge | |
2674 | => Resolve_Comparison_Op (N, Ctx_Type); | |
2675 | ||
2676 | when N_Op_Not => Resolve_Op_Not (N, Ctx_Type); | |
2677 | ||
2678 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | | |
2679 | N_Op_Divide | N_Op_Mod | N_Op_Rem | |
2680 | ||
2681 | => Resolve_Arithmetic_Op (N, Ctx_Type); | |
2682 | ||
2683 | when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type); | |
2684 | ||
2685 | when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type); | |
2686 | ||
2687 | when N_Op_Plus | N_Op_Minus | N_Op_Abs | |
2688 | => Resolve_Unary_Op (N, Ctx_Type); | |
2689 | ||
2690 | when N_Op_Shift => Resolve_Shift (N, Ctx_Type); | |
2691 | ||
2692 | when N_Procedure_Call_Statement | |
2693 | => Resolve_Call (N, Ctx_Type); | |
2694 | ||
2695 | when N_Operator_Symbol | |
2696 | => Resolve_Operator_Symbol (N, Ctx_Type); | |
2697 | ||
2698 | when N_Qualified_Expression | |
2699 | => Resolve_Qualified_Expression (N, Ctx_Type); | |
2700 | ||
2701 | when N_Raise_xxx_Error | |
2702 | => Set_Etype (N, Ctx_Type); | |
2703 | ||
2704 | when N_Range => Resolve_Range (N, Ctx_Type); | |
2705 | ||
2706 | when N_Real_Literal | |
2707 | => Resolve_Real_Literal (N, Ctx_Type); | |
2708 | ||
2709 | when N_Reference => Resolve_Reference (N, Ctx_Type); | |
2710 | ||
2711 | when N_Selected_Component | |
2712 | => Resolve_Selected_Component (N, Ctx_Type); | |
2713 | ||
2714 | when N_Slice => Resolve_Slice (N, Ctx_Type); | |
2715 | ||
2716 | when N_String_Literal | |
2717 | => Resolve_String_Literal (N, Ctx_Type); | |
2718 | ||
2719 | when N_Subprogram_Info | |
2720 | => Resolve_Subprogram_Info (N, Ctx_Type); | |
2721 | ||
2722 | when N_Type_Conversion | |
2723 | => Resolve_Type_Conversion (N, Ctx_Type); | |
2724 | ||
2725 | when N_Unchecked_Expression => | |
2726 | Resolve_Unchecked_Expression (N, Ctx_Type); | |
2727 | ||
2728 | when N_Unchecked_Type_Conversion => | |
2729 | Resolve_Unchecked_Type_Conversion (N, Ctx_Type); | |
996ae0b0 RK |
2730 | end case; |
2731 | ||
2732 | -- If the subexpression was replaced by a non-subexpression, then | |
2733 | -- all we do is to expand it. The only legitimate case we know of | |
2734 | -- is converting procedure call statement to entry call statements, | |
2735 | -- but there may be others, so we are making this test general. | |
2736 | ||
2737 | if Nkind (N) not in N_Subexpr then | |
2738 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2739 | Expand (N); | |
2740 | return; | |
2741 | end if; | |
2742 | ||
2743 | -- The expression is definitely NOT overloaded at this point, so | |
2744 | -- we reset the Is_Overloaded flag to avoid any confusion when | |
2745 | -- reanalyzing the node. | |
2746 | ||
2747 | Set_Is_Overloaded (N, False); | |
2748 | ||
2749 | -- Freeze expression type, entity if it is a name, and designated | |
fbf5a39b | 2750 | -- type if it is an allocator (RM 13.14(10,11,13)). |
996ae0b0 RK |
2751 | |
2752 | -- Now that the resolution of the type of the node is complete, | |
2753 | -- and we did not detect an error, we can expand this node. We | |
2754 | -- skip the expand call if we are in a default expression, see | |
2755 | -- section "Handling of Default Expressions" in Sem spec. | |
2756 | ||
2757 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2758 | ||
2759 | -- We unconditionally freeze the expression, even if we are in | |
2760 | -- default expression mode (the Freeze_Expression routine tests | |
2761 | -- this flag and only freezes static types if it is set). | |
2762 | ||
2763 | Freeze_Expression (N); | |
2764 | ||
2765 | -- Now we can do the expansion | |
2766 | ||
2767 | Expand (N); | |
2768 | end if; | |
996ae0b0 RK |
2769 | end Resolve; |
2770 | ||
fbf5a39b AC |
2771 | ------------- |
2772 | -- Resolve -- | |
2773 | ------------- | |
2774 | ||
996ae0b0 RK |
2775 | -- Version with check(s) suppressed |
2776 | ||
2777 | procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is | |
2778 | begin | |
2779 | if Suppress = All_Checks then | |
2780 | declare | |
fbf5a39b | 2781 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
2782 | begin |
2783 | Scope_Suppress := (others => True); | |
2784 | Resolve (N, Typ); | |
2785 | Scope_Suppress := Svg; | |
2786 | end; | |
2787 | ||
2788 | else | |
2789 | declare | |
fbf5a39b | 2790 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 | 2791 | begin |
fbf5a39b | 2792 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 2793 | Resolve (N, Typ); |
fbf5a39b | 2794 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
2795 | end; |
2796 | end if; | |
2797 | end Resolve; | |
2798 | ||
fbf5a39b AC |
2799 | ------------- |
2800 | -- Resolve -- | |
2801 | ------------- | |
2802 | ||
2803 | -- Version with implicit type | |
2804 | ||
2805 | procedure Resolve (N : Node_Id) is | |
2806 | begin | |
2807 | Resolve (N, Etype (N)); | |
2808 | end Resolve; | |
2809 | ||
996ae0b0 RK |
2810 | --------------------- |
2811 | -- Resolve_Actuals -- | |
2812 | --------------------- | |
2813 | ||
2814 | procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is | |
2815 | Loc : constant Source_Ptr := Sloc (N); | |
2816 | A : Node_Id; | |
2817 | F : Entity_Id; | |
2818 | A_Typ : Entity_Id; | |
2819 | F_Typ : Entity_Id; | |
2820 | Prev : Node_Id := Empty; | |
67ce0d7e | 2821 | Orig_A : Node_Id; |
996ae0b0 | 2822 | |
45fc7ddb HK |
2823 | procedure Check_Argument_Order; |
2824 | -- Performs a check for the case where the actuals are all simple | |
2825 | -- identifiers that correspond to the formal names, but in the wrong | |
2826 | -- order, which is considered suspicious and cause for a warning. | |
2827 | ||
b7d1f17f HK |
2828 | procedure Check_Prefixed_Call; |
2829 | -- If the original node is an overloaded call in prefix notation, | |
2830 | -- insert an 'Access or a dereference as needed over the first actual. | |
2831 | -- Try_Object_Operation has already verified that there is a valid | |
2832 | -- interpretation, but the form of the actual can only be determined | |
2833 | -- once the primitive operation is identified. | |
2834 | ||
996ae0b0 RK |
2835 | procedure Insert_Default; |
2836 | -- If the actual is missing in a call, insert in the actuals list | |
2837 | -- an instance of the default expression. The insertion is always | |
2838 | -- a named association. | |
2839 | ||
fbf5a39b AC |
2840 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean; |
2841 | -- Check whether T1 and T2, or their full views, are derived from a | |
2842 | -- common type. Used to enforce the restrictions on array conversions | |
2843 | -- of AI95-00246. | |
2844 | ||
a7a3cf5c AC |
2845 | function Static_Concatenation (N : Node_Id) return Boolean; |
2846 | -- Predicate to determine whether an actual that is a concatenation | |
2847 | -- will be evaluated statically and does not need a transient scope. | |
2848 | -- This must be determined before the actual is resolved and expanded | |
2849 | -- because if needed the transient scope must be introduced earlier. | |
2850 | ||
45fc7ddb HK |
2851 | -------------------------- |
2852 | -- Check_Argument_Order -- | |
2853 | -------------------------- | |
2854 | ||
2855 | procedure Check_Argument_Order is | |
2856 | begin | |
2857 | -- Nothing to do if no parameters, or original node is neither a | |
2858 | -- function call nor a procedure call statement (happens in the | |
2859 | -- operator-transformed-to-function call case), or the call does | |
2860 | -- not come from source, or this warning is off. | |
2861 | ||
2862 | if not Warn_On_Parameter_Order | |
2863 | or else | |
2864 | No (Parameter_Associations (N)) | |
2865 | or else | |
2866 | not Nkind_In (Original_Node (N), N_Procedure_Call_Statement, | |
2867 | N_Function_Call) | |
2868 | or else | |
2869 | not Comes_From_Source (N) | |
2870 | then | |
2871 | return; | |
2872 | end if; | |
2873 | ||
2874 | declare | |
2875 | Nargs : constant Nat := List_Length (Parameter_Associations (N)); | |
2876 | ||
2877 | begin | |
2878 | -- Nothing to do if only one parameter | |
2879 | ||
2880 | if Nargs < 2 then | |
2881 | return; | |
2882 | end if; | |
2883 | ||
2884 | -- Here if at least two arguments | |
2885 | ||
2886 | declare | |
2887 | Actuals : array (1 .. Nargs) of Node_Id; | |
2888 | Actual : Node_Id; | |
2889 | Formal : Node_Id; | |
2890 | ||
2891 | Wrong_Order : Boolean := False; | |
2892 | -- Set True if an out of order case is found | |
2893 | ||
2894 | begin | |
2895 | -- Collect identifier names of actuals, fail if any actual is | |
2896 | -- not a simple identifier, and record max length of name. | |
2897 | ||
2898 | Actual := First (Parameter_Associations (N)); | |
2899 | for J in Actuals'Range loop | |
2900 | if Nkind (Actual) /= N_Identifier then | |
2901 | return; | |
2902 | else | |
2903 | Actuals (J) := Actual; | |
2904 | Next (Actual); | |
2905 | end if; | |
2906 | end loop; | |
2907 | ||
2908 | -- If we got this far, all actuals are identifiers and the list | |
2909 | -- of their names is stored in the Actuals array. | |
2910 | ||
2911 | Formal := First_Formal (Nam); | |
2912 | for J in Actuals'Range loop | |
2913 | ||
2914 | -- If we ran out of formals, that's odd, probably an error | |
2915 | -- which will be detected elsewhere, but abandon the search. | |
2916 | ||
2917 | if No (Formal) then | |
2918 | return; | |
2919 | end if; | |
2920 | ||
2921 | -- If name matches and is in order OK | |
2922 | ||
2923 | if Chars (Formal) = Chars (Actuals (J)) then | |
2924 | null; | |
2925 | ||
2926 | else | |
2927 | -- If no match, see if it is elsewhere in list and if so | |
2928 | -- flag potential wrong order if type is compatible. | |
2929 | ||
2930 | for K in Actuals'Range loop | |
2931 | if Chars (Formal) = Chars (Actuals (K)) | |
2932 | and then | |
2933 | Has_Compatible_Type (Actuals (K), Etype (Formal)) | |
2934 | then | |
2935 | Wrong_Order := True; | |
2936 | goto Continue; | |
2937 | end if; | |
2938 | end loop; | |
2939 | ||
2940 | -- No match | |
2941 | ||
2942 | return; | |
2943 | end if; | |
2944 | ||
2945 | <<Continue>> Next_Formal (Formal); | |
2946 | end loop; | |
2947 | ||
2948 | -- If Formals left over, also probably an error, skip warning | |
2949 | ||
2950 | if Present (Formal) then | |
2951 | return; | |
2952 | end if; | |
2953 | ||
2954 | -- Here we give the warning if something was out of order | |
2955 | ||
2956 | if Wrong_Order then | |
2957 | Error_Msg_N | |
2958 | ("actuals for this call may be in wrong order?", N); | |
2959 | end if; | |
2960 | end; | |
2961 | end; | |
2962 | end Check_Argument_Order; | |
2963 | ||
b7d1f17f HK |
2964 | ------------------------- |
2965 | -- Check_Prefixed_Call -- | |
2966 | ------------------------- | |
2967 | ||
2968 | procedure Check_Prefixed_Call is | |
2969 | Act : constant Node_Id := First_Actual (N); | |
2970 | A_Type : constant Entity_Id := Etype (Act); | |
2971 | F_Type : constant Entity_Id := Etype (First_Formal (Nam)); | |
2972 | Orig : constant Node_Id := Original_Node (N); | |
2973 | New_A : Node_Id; | |
2974 | ||
2975 | begin | |
2976 | -- Check whether the call is a prefixed call, with or without | |
2977 | -- additional actuals. | |
2978 | ||
2979 | if Nkind (Orig) = N_Selected_Component | |
2980 | or else | |
2981 | (Nkind (Orig) = N_Indexed_Component | |
2982 | and then Nkind (Prefix (Orig)) = N_Selected_Component | |
2983 | and then Is_Entity_Name (Prefix (Prefix (Orig))) | |
2984 | and then Is_Entity_Name (Act) | |
2985 | and then Chars (Act) = Chars (Prefix (Prefix (Orig)))) | |
2986 | then | |
2987 | if Is_Access_Type (A_Type) | |
2988 | and then not Is_Access_Type (F_Type) | |
2989 | then | |
2990 | -- Introduce dereference on object in prefix | |
2991 | ||
2992 | New_A := | |
2993 | Make_Explicit_Dereference (Sloc (Act), | |
2994 | Prefix => Relocate_Node (Act)); | |
2995 | Rewrite (Act, New_A); | |
2996 | Analyze (Act); | |
2997 | ||
2998 | elsif Is_Access_Type (F_Type) | |
2999 | and then not Is_Access_Type (A_Type) | |
3000 | then | |
3001 | -- Introduce an implicit 'Access in prefix | |
3002 | ||
3003 | if not Is_Aliased_View (Act) then | |
ed2233dc | 3004 | Error_Msg_NE |
b7d1f17f | 3005 | ("object in prefixed call to& must be aliased" |
aa5147f0 | 3006 | & " (RM-2005 4.3.1 (13))", |
b7d1f17f HK |
3007 | Prefix (Act), Nam); |
3008 | end if; | |
3009 | ||
3010 | Rewrite (Act, | |
3011 | Make_Attribute_Reference (Loc, | |
3012 | Attribute_Name => Name_Access, | |
3013 | Prefix => Relocate_Node (Act))); | |
3014 | end if; | |
3015 | ||
3016 | Analyze (Act); | |
3017 | end if; | |
3018 | end Check_Prefixed_Call; | |
3019 | ||
996ae0b0 RK |
3020 | -------------------- |
3021 | -- Insert_Default -- | |
3022 | -------------------- | |
3023 | ||
3024 | procedure Insert_Default is | |
3025 | Actval : Node_Id; | |
3026 | Assoc : Node_Id; | |
3027 | ||
3028 | begin | |
fbf5a39b | 3029 | -- Missing argument in call, nothing to insert |
996ae0b0 | 3030 | |
fbf5a39b AC |
3031 | if No (Default_Value (F)) then |
3032 | return; | |
3033 | ||
3034 | else | |
3035 | -- Note that we do a full New_Copy_Tree, so that any associated | |
3036 | -- Itypes are properly copied. This may not be needed any more, | |
3037 | -- but it does no harm as a safety measure! Defaults of a generic | |
3038 | -- formal may be out of bounds of the corresponding actual (see | |
3039 | -- cc1311b) and an additional check may be required. | |
996ae0b0 | 3040 | |
b7d1f17f HK |
3041 | Actval := |
3042 | New_Copy_Tree | |
3043 | (Default_Value (F), | |
3044 | New_Scope => Current_Scope, | |
3045 | New_Sloc => Loc); | |
996ae0b0 RK |
3046 | |
3047 | if Is_Concurrent_Type (Scope (Nam)) | |
3048 | and then Has_Discriminants (Scope (Nam)) | |
3049 | then | |
3050 | Replace_Actual_Discriminants (N, Actval); | |
3051 | end if; | |
3052 | ||
3053 | if Is_Overloadable (Nam) | |
3054 | and then Present (Alias (Nam)) | |
3055 | then | |
3056 | if Base_Type (Etype (F)) /= Base_Type (Etype (Actval)) | |
3057 | and then not Is_Tagged_Type (Etype (F)) | |
3058 | then | |
3059 | -- If default is a real literal, do not introduce a | |
3060 | -- conversion whose effect may depend on the run-time | |
3061 | -- size of universal real. | |
3062 | ||
3063 | if Nkind (Actval) = N_Real_Literal then | |
3064 | Set_Etype (Actval, Base_Type (Etype (F))); | |
3065 | else | |
3066 | Actval := Unchecked_Convert_To (Etype (F), Actval); | |
3067 | end if; | |
3068 | end if; | |
3069 | ||
3070 | if Is_Scalar_Type (Etype (F)) then | |
3071 | Enable_Range_Check (Actval); | |
3072 | end if; | |
3073 | ||
996ae0b0 RK |
3074 | Set_Parent (Actval, N); |
3075 | ||
3076 | -- Resolve aggregates with their base type, to avoid scope | |
f3d57416 | 3077 | -- anomalies: the subtype was first built in the subprogram |
996ae0b0 RK |
3078 | -- declaration, and the current call may be nested. |
3079 | ||
76b84bf0 AC |
3080 | if Nkind (Actval) = N_Aggregate then |
3081 | Analyze_And_Resolve (Actval, Etype (F)); | |
996ae0b0 RK |
3082 | else |
3083 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
3084 | end if; | |
fbf5a39b AC |
3085 | |
3086 | else | |
3087 | Set_Parent (Actval, N); | |
3088 | ||
a77842bd | 3089 | -- See note above concerning aggregates |
fbf5a39b AC |
3090 | |
3091 | if Nkind (Actval) = N_Aggregate | |
3092 | and then Has_Discriminants (Etype (Actval)) | |
3093 | then | |
3094 | Analyze_And_Resolve (Actval, Base_Type (Etype (Actval))); | |
3095 | ||
3096 | -- Resolve entities with their own type, which may differ | |
3097 | -- from the type of a reference in a generic context (the | |
3098 | -- view swapping mechanism did not anticipate the re-analysis | |
3099 | -- of default values in calls). | |
3100 | ||
3101 | elsif Is_Entity_Name (Actval) then | |
3102 | Analyze_And_Resolve (Actval, Etype (Entity (Actval))); | |
3103 | ||
3104 | else | |
3105 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
3106 | end if; | |
996ae0b0 RK |
3107 | end if; |
3108 | ||
3109 | -- If default is a tag indeterminate function call, propagate | |
3110 | -- tag to obtain proper dispatching. | |
3111 | ||
3112 | if Is_Controlling_Formal (F) | |
3113 | and then Nkind (Default_Value (F)) = N_Function_Call | |
3114 | then | |
3115 | Set_Is_Controlling_Actual (Actval); | |
3116 | end if; | |
3117 | ||
996ae0b0 RK |
3118 | end if; |
3119 | ||
3120 | -- If the default expression raises constraint error, then just | |
3121 | -- silently replace it with an N_Raise_Constraint_Error node, | |
3122 | -- since we already gave the warning on the subprogram spec. | |
3123 | ||
3124 | if Raises_Constraint_Error (Actval) then | |
3125 | Rewrite (Actval, | |
07fc65c4 GB |
3126 | Make_Raise_Constraint_Error (Loc, |
3127 | Reason => CE_Range_Check_Failed)); | |
996ae0b0 RK |
3128 | Set_Raises_Constraint_Error (Actval); |
3129 | Set_Etype (Actval, Etype (F)); | |
3130 | end if; | |
3131 | ||
3132 | Assoc := | |
3133 | Make_Parameter_Association (Loc, | |
3134 | Explicit_Actual_Parameter => Actval, | |
3135 | Selector_Name => Make_Identifier (Loc, Chars (F))); | |
3136 | ||
3137 | -- Case of insertion is first named actual | |
3138 | ||
3139 | if No (Prev) or else | |
3140 | Nkind (Parent (Prev)) /= N_Parameter_Association | |
3141 | then | |
3142 | Set_Next_Named_Actual (Assoc, First_Named_Actual (N)); | |
3143 | Set_First_Named_Actual (N, Actval); | |
3144 | ||
3145 | if No (Prev) then | |
c8ef728f | 3146 | if No (Parameter_Associations (N)) then |
996ae0b0 RK |
3147 | Set_Parameter_Associations (N, New_List (Assoc)); |
3148 | else | |
3149 | Append (Assoc, Parameter_Associations (N)); | |
3150 | end if; | |
3151 | ||
3152 | else | |
3153 | Insert_After (Prev, Assoc); | |
3154 | end if; | |
3155 | ||
3156 | -- Case of insertion is not first named actual | |
3157 | ||
3158 | else | |
3159 | Set_Next_Named_Actual | |
3160 | (Assoc, Next_Named_Actual (Parent (Prev))); | |
3161 | Set_Next_Named_Actual (Parent (Prev), Actval); | |
3162 | Append (Assoc, Parameter_Associations (N)); | |
3163 | end if; | |
3164 | ||
3165 | Mark_Rewrite_Insertion (Assoc); | |
3166 | Mark_Rewrite_Insertion (Actval); | |
3167 | ||
3168 | Prev := Actval; | |
3169 | end Insert_Default; | |
3170 | ||
fbf5a39b AC |
3171 | ------------------- |
3172 | -- Same_Ancestor -- | |
3173 | ------------------- | |
3174 | ||
3175 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is | |
3176 | FT1 : Entity_Id := T1; | |
3177 | FT2 : Entity_Id := T2; | |
3178 | ||
3179 | begin | |
3180 | if Is_Private_Type (T1) | |
3181 | and then Present (Full_View (T1)) | |
3182 | then | |
3183 | FT1 := Full_View (T1); | |
3184 | end if; | |
3185 | ||
3186 | if Is_Private_Type (T2) | |
3187 | and then Present (Full_View (T2)) | |
3188 | then | |
3189 | FT2 := Full_View (T2); | |
3190 | end if; | |
3191 | ||
3192 | return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2)); | |
3193 | end Same_Ancestor; | |
3194 | ||
a7a3cf5c AC |
3195 | -------------------------- |
3196 | -- Static_Concatenation -- | |
3197 | -------------------------- | |
3198 | ||
3199 | function Static_Concatenation (N : Node_Id) return Boolean is | |
3200 | begin | |
c72a85f2 TQ |
3201 | case Nkind (N) is |
3202 | when N_String_Literal => | |
3203 | return True; | |
a7a3cf5c | 3204 | |
d81b4bfe TQ |
3205 | when N_Op_Concat => |
3206 | ||
4342eda9 TQ |
3207 | -- Concatenation is static when both operands are static |
3208 | -- and the concatenation operator is a predefined one. | |
3209 | ||
3210 | return Scope (Entity (N)) = Standard_Standard | |
3211 | and then | |
3212 | Static_Concatenation (Left_Opnd (N)) | |
c72a85f2 TQ |
3213 | and then |
3214 | Static_Concatenation (Right_Opnd (N)); | |
3215 | ||
3216 | when others => | |
3217 | if Is_Entity_Name (N) then | |
3218 | declare | |
3219 | Ent : constant Entity_Id := Entity (N); | |
3220 | begin | |
3221 | return Ekind (Ent) = E_Constant | |
3222 | and then Present (Constant_Value (Ent)) | |
d81b4bfe TQ |
3223 | and then |
3224 | Is_Static_Expression (Constant_Value (Ent)); | |
c72a85f2 | 3225 | end; |
a7a3cf5c | 3226 | |
a7a3cf5c AC |
3227 | else |
3228 | return False; | |
3229 | end if; | |
c72a85f2 | 3230 | end case; |
a7a3cf5c AC |
3231 | end Static_Concatenation; |
3232 | ||
996ae0b0 RK |
3233 | -- Start of processing for Resolve_Actuals |
3234 | ||
3235 | begin | |
45fc7ddb HK |
3236 | Check_Argument_Order; |
3237 | ||
b7d1f17f HK |
3238 | if Present (First_Actual (N)) then |
3239 | Check_Prefixed_Call; | |
3240 | end if; | |
3241 | ||
996ae0b0 RK |
3242 | A := First_Actual (N); |
3243 | F := First_Formal (Nam); | |
996ae0b0 | 3244 | while Present (F) loop |
fbf5a39b AC |
3245 | if No (A) and then Needs_No_Actuals (Nam) then |
3246 | null; | |
996ae0b0 | 3247 | |
d81b4bfe TQ |
3248 | -- If we have an error in any actual or formal, indicated by a type |
3249 | -- of Any_Type, then abandon resolution attempt, and set result type | |
3250 | -- to Any_Type. | |
07fc65c4 | 3251 | |
fbf5a39b AC |
3252 | elsif (Present (A) and then Etype (A) = Any_Type) |
3253 | or else Etype (F) = Any_Type | |
07fc65c4 GB |
3254 | then |
3255 | Set_Etype (N, Any_Type); | |
3256 | return; | |
3257 | end if; | |
3258 | ||
e65f50ec ES |
3259 | -- Case where actual is present |
3260 | ||
45fc7ddb | 3261 | -- If the actual is an entity, generate a reference to it now. We |
36fcf362 RD |
3262 | -- do this before the actual is resolved, because a formal of some |
3263 | -- protected subprogram, or a task discriminant, will be rewritten | |
3264 | -- during expansion, and the reference to the source entity may | |
3265 | -- be lost. | |
3266 | ||
3267 | if Present (A) | |
3268 | and then Is_Entity_Name (A) | |
3269 | and then Comes_From_Source (N) | |
3270 | then | |
3271 | Orig_A := Entity (A); | |
3272 | ||
3273 | if Present (Orig_A) then | |
3274 | if Is_Formal (Orig_A) | |
3275 | and then Ekind (F) /= E_In_Parameter | |
3276 | then | |
3277 | Generate_Reference (Orig_A, A, 'm'); | |
36fcf362 RD |
3278 | elsif not Is_Overloaded (A) then |
3279 | Generate_Reference (Orig_A, A); | |
3280 | end if; | |
3281 | end if; | |
3282 | end if; | |
3283 | ||
996ae0b0 RK |
3284 | if Present (A) |
3285 | and then (Nkind (Parent (A)) /= N_Parameter_Association | |
3286 | or else | |
3287 | Chars (Selector_Name (Parent (A))) = Chars (F)) | |
3288 | then | |
45fc7ddb HK |
3289 | -- If style checking mode on, check match of formal name |
3290 | ||
3291 | if Style_Check then | |
3292 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3293 | Check_Identifier (Selector_Name (Parent (A)), F); | |
3294 | end if; | |
3295 | end if; | |
3296 | ||
996ae0b0 RK |
3297 | -- If the formal is Out or In_Out, do not resolve and expand the |
3298 | -- conversion, because it is subsequently expanded into explicit | |
3299 | -- temporaries and assignments. However, the object of the | |
ea985d95 RD |
3300 | -- conversion can be resolved. An exception is the case of tagged |
3301 | -- type conversion with a class-wide actual. In that case we want | |
3302 | -- the tag check to occur and no temporary will be needed (no | |
3303 | -- representation change can occur) and the parameter is passed by | |
3304 | -- reference, so we go ahead and resolve the type conversion. | |
c8ef728f | 3305 | -- Another exception is the case of reference to component or |
ea985d95 RD |
3306 | -- subcomponent of a bit-packed array, in which case we want to |
3307 | -- defer expansion to the point the in and out assignments are | |
3308 | -- performed. | |
996ae0b0 RK |
3309 | |
3310 | if Ekind (F) /= E_In_Parameter | |
3311 | and then Nkind (A) = N_Type_Conversion | |
3312 | and then not Is_Class_Wide_Type (Etype (Expression (A))) | |
3313 | then | |
07fc65c4 GB |
3314 | if Ekind (F) = E_In_Out_Parameter |
3315 | and then Is_Array_Type (Etype (F)) | |
07fc65c4 | 3316 | then |
fbf5a39b AC |
3317 | if Has_Aliased_Components (Etype (Expression (A))) |
3318 | /= Has_Aliased_Components (Etype (F)) | |
3319 | then | |
758c442c | 3320 | |
45fc7ddb HK |
3321 | -- In a view conversion, the conversion must be legal in |
3322 | -- both directions, and thus both component types must be | |
3323 | -- aliased, or neither (4.6 (8)). | |
758c442c | 3324 | |
45fc7ddb | 3325 | -- The additional rule 4.6 (24.9.2) seems unduly |
d81b4bfe TQ |
3326 | -- restrictive: the privacy requirement should not apply |
3327 | -- to generic types, and should be checked in an | |
3328 | -- instance. ARG query is in order ??? | |
45fc7ddb HK |
3329 | |
3330 | Error_Msg_N | |
3331 | ("both component types in a view conversion must be" | |
3332 | & " aliased, or neither", A); | |
3333 | ||
3334 | elsif | |
3335 | not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3336 | then | |
3337 | if Is_By_Reference_Type (Etype (F)) | |
3338 | or else Is_By_Reference_Type (Etype (Expression (A))) | |
758c442c GD |
3339 | then |
3340 | Error_Msg_N | |
45fc7ddb HK |
3341 | ("view conversion between unrelated by reference " & |
3342 | "array types not allowed (\'A'I-00246)", A); | |
3343 | else | |
3344 | declare | |
3345 | Comp_Type : constant Entity_Id := | |
3346 | Component_Type | |
3347 | (Etype (Expression (A))); | |
3348 | begin | |
3349 | if Comes_From_Source (A) | |
3350 | and then Ada_Version >= Ada_05 | |
3351 | and then | |
3352 | ((Is_Private_Type (Comp_Type) | |
3353 | and then not Is_Generic_Type (Comp_Type)) | |
3354 | or else Is_Tagged_Type (Comp_Type) | |
3355 | or else Is_Volatile (Comp_Type)) | |
3356 | then | |
3357 | Error_Msg_N | |
3358 | ("component type of a view conversion cannot" | |
3359 | & " be private, tagged, or volatile" | |
3360 | & " (RM 4.6 (24))", | |
3361 | Expression (A)); | |
3362 | end if; | |
3363 | end; | |
758c442c | 3364 | end if; |
fbf5a39b | 3365 | end if; |
07fc65c4 GB |
3366 | end if; |
3367 | ||
16397eff TQ |
3368 | if (Conversion_OK (A) |
3369 | or else Valid_Conversion (A, Etype (A), Expression (A))) | |
3370 | and then not Is_Ref_To_Bit_Packed_Array (Expression (A)) | |
996ae0b0 | 3371 | then |
fbf5a39b | 3372 | Resolve (Expression (A)); |
996ae0b0 RK |
3373 | end if; |
3374 | ||
b7d1f17f HK |
3375 | -- If the actual is a function call that returns a limited |
3376 | -- unconstrained object that needs finalization, create a | |
3377 | -- transient scope for it, so that it can receive the proper | |
3378 | -- finalization list. | |
3379 | ||
3380 | elsif Nkind (A) = N_Function_Call | |
3381 | and then Is_Limited_Record (Etype (F)) | |
3382 | and then not Is_Constrained (Etype (F)) | |
3383 | and then Expander_Active | |
3384 | and then | |
3385 | (Is_Controlled (Etype (F)) or else Has_Task (Etype (F))) | |
3386 | then | |
3387 | Establish_Transient_Scope (A, False); | |
3388 | ||
a52fefe6 AC |
3389 | -- A small optimization: if one of the actuals is a concatenation |
3390 | -- create a block around a procedure call to recover stack space. | |
3391 | -- This alleviates stack usage when several procedure calls in | |
76e776e5 AC |
3392 | -- the same statement list use concatenation. We do not perform |
3393 | -- this wrapping for code statements, where the argument is a | |
3394 | -- static string, and we want to preserve warnings involving | |
3395 | -- sequences of such statements. | |
a52fefe6 AC |
3396 | |
3397 | elsif Nkind (A) = N_Op_Concat | |
3398 | and then Nkind (N) = N_Procedure_Call_Statement | |
3399 | and then Expander_Active | |
76e776e5 AC |
3400 | and then |
3401 | not (Is_Intrinsic_Subprogram (Nam) | |
3402 | and then Chars (Nam) = Name_Asm) | |
a7a3cf5c | 3403 | and then not Static_Concatenation (A) |
a52fefe6 AC |
3404 | then |
3405 | Establish_Transient_Scope (A, False); | |
3406 | Resolve (A, Etype (F)); | |
3407 | ||
996ae0b0 | 3408 | else |
fbf5a39b AC |
3409 | if Nkind (A) = N_Type_Conversion |
3410 | and then Is_Array_Type (Etype (F)) | |
3411 | and then not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3412 | and then | |
3413 | (Is_Limited_Type (Etype (F)) | |
3414 | or else Is_Limited_Type (Etype (Expression (A)))) | |
3415 | then | |
3416 | Error_Msg_N | |
758c442c GD |
3417 | ("conversion between unrelated limited array types " & |
3418 | "not allowed (\A\I-00246)", A); | |
fbf5a39b | 3419 | |
758c442c GD |
3420 | if Is_Limited_Type (Etype (F)) then |
3421 | Explain_Limited_Type (Etype (F), A); | |
3422 | end if; | |
fbf5a39b | 3423 | |
758c442c GD |
3424 | if Is_Limited_Type (Etype (Expression (A))) then |
3425 | Explain_Limited_Type (Etype (Expression (A)), A); | |
3426 | end if; | |
fbf5a39b AC |
3427 | end if; |
3428 | ||
c8ef728f ES |
3429 | -- (Ada 2005: AI-251): If the actual is an allocator whose |
3430 | -- directly designated type is a class-wide interface, we build | |
3431 | -- an anonymous access type to use it as the type of the | |
3432 | -- allocator. Later, when the subprogram call is expanded, if | |
3433 | -- the interface has a secondary dispatch table the expander | |
3434 | -- will add a type conversion to force the correct displacement | |
3435 | -- of the pointer. | |
3436 | ||
3437 | if Nkind (A) = N_Allocator then | |
3438 | declare | |
3439 | DDT : constant Entity_Id := | |
3440 | Directly_Designated_Type (Base_Type (Etype (F))); | |
45fc7ddb | 3441 | |
c8ef728f | 3442 | New_Itype : Entity_Id; |
45fc7ddb | 3443 | |
c8ef728f ES |
3444 | begin |
3445 | if Is_Class_Wide_Type (DDT) | |
3446 | and then Is_Interface (DDT) | |
3447 | then | |
3448 | New_Itype := Create_Itype (E_Anonymous_Access_Type, A); | |
45fc7ddb | 3449 | Set_Etype (New_Itype, Etype (A)); |
c8ef728f ES |
3450 | Set_Directly_Designated_Type (New_Itype, |
3451 | Directly_Designated_Type (Etype (A))); | |
3452 | Set_Etype (A, New_Itype); | |
3453 | end if; | |
0669bebe GB |
3454 | |
3455 | -- Ada 2005, AI-162:If the actual is an allocator, the | |
3456 | -- innermost enclosing statement is the master of the | |
b7d1f17f HK |
3457 | -- created object. This needs to be done with expansion |
3458 | -- enabled only, otherwise the transient scope will not | |
3459 | -- be removed in the expansion of the wrapped construct. | |
0669bebe | 3460 | |
45fc7ddb | 3461 | if (Is_Controlled (DDT) or else Has_Task (DDT)) |
b7d1f17f | 3462 | and then Expander_Active |
0669bebe GB |
3463 | then |
3464 | Establish_Transient_Scope (A, False); | |
3465 | end if; | |
c8ef728f ES |
3466 | end; |
3467 | end if; | |
3468 | ||
b7d1f17f HK |
3469 | -- (Ada 2005): The call may be to a primitive operation of |
3470 | -- a tagged synchronized type, declared outside of the type. | |
3471 | -- In this case the controlling actual must be converted to | |
3472 | -- its corresponding record type, which is the formal type. | |
45fc7ddb HK |
3473 | -- The actual may be a subtype, either because of a constraint |
3474 | -- or because it is a generic actual, so use base type to | |
3475 | -- locate concurrent type. | |
b7d1f17f | 3476 | |
15e4986c JM |
3477 | A_Typ := Base_Type (Etype (A)); |
3478 | F_Typ := Base_Type (Etype (F)); | |
3479 | ||
3480 | declare | |
3481 | Full_A_Typ : Entity_Id; | |
3482 | ||
3483 | begin | |
3484 | if Present (Full_View (A_Typ)) then | |
3485 | Full_A_Typ := Base_Type (Full_View (A_Typ)); | |
3486 | else | |
3487 | Full_A_Typ := A_Typ; | |
3488 | end if; | |
b7d1f17f | 3489 | |
15e4986c JM |
3490 | -- Tagged synchronized type (case 1): the actual is a |
3491 | -- concurrent type | |
3492 | ||
3493 | if Is_Concurrent_Type (A_Typ) | |
3494 | and then Corresponding_Record_Type (A_Typ) = F_Typ | |
3495 | then | |
3496 | Rewrite (A, | |
3497 | Unchecked_Convert_To | |
3498 | (Corresponding_Record_Type (A_Typ), A)); | |
3499 | Resolve (A, Etype (F)); | |
3500 | ||
3501 | -- Tagged synchronized type (case 2): the formal is a | |
3502 | -- concurrent type | |
3503 | ||
3504 | elsif Ekind (Full_A_Typ) = E_Record_Type | |
3505 | and then Present | |
3506 | (Corresponding_Concurrent_Type (Full_A_Typ)) | |
3507 | and then Is_Concurrent_Type (F_Typ) | |
3508 | and then Present (Corresponding_Record_Type (F_Typ)) | |
3509 | and then Full_A_Typ = Corresponding_Record_Type (F_Typ) | |
3510 | then | |
3511 | Resolve (A, Corresponding_Record_Type (F_Typ)); | |
3512 | ||
3513 | -- Common case | |
3514 | ||
3515 | else | |
3516 | Resolve (A, Etype (F)); | |
3517 | end if; | |
3518 | end; | |
996ae0b0 RK |
3519 | end if; |
3520 | ||
3521 | A_Typ := Etype (A); | |
3522 | F_Typ := Etype (F); | |
3523 | ||
bb481772 AC |
3524 | -- Save actual for subsequent check on order dependence, |
3525 | -- and indicate whether actual is modifiable. For AI05-0144 | |
3526 | ||
3527 | -- Save_Actual (A, | |
3528 | -- Ekind (F) /= E_In_Parameter or else Is_Access_Type (F_Typ)); | |
87dc09cb | 3529 | -- Why is this code commented out ??? |
bb481772 | 3530 | |
26570b21 RD |
3531 | -- For mode IN, if actual is an entity, and the type of the formal |
3532 | -- has warnings suppressed, then we reset Never_Set_In_Source for | |
3533 | -- the calling entity. The reason for this is to catch cases like | |
3534 | -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram | |
3535 | -- uses trickery to modify an IN parameter. | |
3536 | ||
3537 | if Ekind (F) = E_In_Parameter | |
3538 | and then Is_Entity_Name (A) | |
3539 | and then Present (Entity (A)) | |
3540 | and then Ekind (Entity (A)) = E_Variable | |
3541 | and then Has_Warnings_Off (F_Typ) | |
3542 | then | |
3543 | Set_Never_Set_In_Source (Entity (A), False); | |
3544 | end if; | |
3545 | ||
fbf5a39b AC |
3546 | -- Perform error checks for IN and IN OUT parameters |
3547 | ||
3548 | if Ekind (F) /= E_Out_Parameter then | |
3549 | ||
3550 | -- Check unset reference. For scalar parameters, it is clearly | |
3551 | -- wrong to pass an uninitialized value as either an IN or | |
3552 | -- IN-OUT parameter. For composites, it is also clearly an | |
3553 | -- error to pass a completely uninitialized value as an IN | |
3554 | -- parameter, but the case of IN OUT is trickier. We prefer | |
3555 | -- not to give a warning here. For example, suppose there is | |
3556 | -- a routine that sets some component of a record to False. | |
3557 | -- It is perfectly reasonable to make this IN-OUT and allow | |
3558 | -- either initialized or uninitialized records to be passed | |
3559 | -- in this case. | |
3560 | ||
3561 | -- For partially initialized composite values, we also avoid | |
3562 | -- warnings, since it is quite likely that we are passing a | |
3563 | -- partially initialized value and only the initialized fields | |
3564 | -- will in fact be read in the subprogram. | |
3565 | ||
3566 | if Is_Scalar_Type (A_Typ) | |
3567 | or else (Ekind (F) = E_In_Parameter | |
3568 | and then not Is_Partially_Initialized_Type (A_Typ)) | |
996ae0b0 | 3569 | then |
fbf5a39b | 3570 | Check_Unset_Reference (A); |
996ae0b0 | 3571 | end if; |
996ae0b0 | 3572 | |
758c442c GD |
3573 | -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT |
3574 | -- actual to a nested call, since this is case of reading an | |
3575 | -- out parameter, which is not allowed. | |
996ae0b0 | 3576 | |
0ab80019 | 3577 | if Ada_Version = Ada_83 |
996ae0b0 RK |
3578 | and then Is_Entity_Name (A) |
3579 | and then Ekind (Entity (A)) = E_Out_Parameter | |
3580 | then | |
3581 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", A); | |
3582 | end if; | |
3583 | end if; | |
3584 | ||
67ce0d7e RD |
3585 | -- Case of OUT or IN OUT parameter |
3586 | ||
36fcf362 | 3587 | if Ekind (F) /= E_In_Parameter then |
67ce0d7e RD |
3588 | |
3589 | -- For an Out parameter, check for useless assignment. Note | |
45fc7ddb HK |
3590 | -- that we can't set Last_Assignment this early, because we may |
3591 | -- kill current values in Resolve_Call, and that call would | |
3592 | -- clobber the Last_Assignment field. | |
67ce0d7e | 3593 | |
45fc7ddb HK |
3594 | -- Note: call Warn_On_Useless_Assignment before doing the check |
3595 | -- below for Is_OK_Variable_For_Out_Formal so that the setting | |
3596 | -- of Referenced_As_LHS/Referenced_As_Out_Formal properly | |
3597 | -- reflects the last assignment, not this one! | |
36fcf362 | 3598 | |
67ce0d7e | 3599 | if Ekind (F) = E_Out_Parameter then |
36fcf362 | 3600 | if Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
3601 | and then Is_Entity_Name (A) |
3602 | and then Present (Entity (A)) | |
36fcf362 | 3603 | and then Comes_From_Source (N) |
67ce0d7e | 3604 | then |
36fcf362 | 3605 | Warn_On_Useless_Assignment (Entity (A), A); |
67ce0d7e RD |
3606 | end if; |
3607 | end if; | |
3608 | ||
36fcf362 RD |
3609 | -- Validate the form of the actual. Note that the call to |
3610 | -- Is_OK_Variable_For_Out_Formal generates the required | |
3611 | -- reference in this case. | |
3612 | ||
3613 | if not Is_OK_Variable_For_Out_Formal (A) then | |
3614 | Error_Msg_NE ("actual for& must be a variable", A, F); | |
3615 | end if; | |
3616 | ||
67ce0d7e | 3617 | -- What's the following about??? |
fbf5a39b AC |
3618 | |
3619 | if Is_Entity_Name (A) then | |
3620 | Kill_Checks (Entity (A)); | |
3621 | else | |
3622 | Kill_All_Checks; | |
3623 | end if; | |
3624 | end if; | |
3625 | ||
3626 | if Etype (A) = Any_Type then | |
3627 | Set_Etype (N, Any_Type); | |
3628 | return; | |
3629 | end if; | |
3630 | ||
996ae0b0 RK |
3631 | -- Apply appropriate range checks for in, out, and in-out |
3632 | -- parameters. Out and in-out parameters also need a separate | |
3633 | -- check, if there is a type conversion, to make sure the return | |
3634 | -- value meets the constraints of the variable before the | |
3635 | -- conversion. | |
3636 | ||
3637 | -- Gigi looks at the check flag and uses the appropriate types. | |
3638 | -- For now since one flag is used there is an optimization which | |
3639 | -- might not be done in the In Out case since Gigi does not do | |
3640 | -- any analysis. More thought required about this ??? | |
3641 | ||
8a95f4e8 | 3642 | if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then |
996ae0b0 RK |
3643 | if Is_Scalar_Type (Etype (A)) then |
3644 | Apply_Scalar_Range_Check (A, F_Typ); | |
3645 | ||
3646 | elsif Is_Array_Type (Etype (A)) then | |
3647 | Apply_Length_Check (A, F_Typ); | |
3648 | ||
3649 | elsif Is_Record_Type (F_Typ) | |
3650 | and then Has_Discriminants (F_Typ) | |
3651 | and then Is_Constrained (F_Typ) | |
3652 | and then (not Is_Derived_Type (F_Typ) | |
3653 | or else Comes_From_Source (Nam)) | |
3654 | then | |
3655 | Apply_Discriminant_Check (A, F_Typ); | |
3656 | ||
3657 | elsif Is_Access_Type (F_Typ) | |
3658 | and then Is_Array_Type (Designated_Type (F_Typ)) | |
3659 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3660 | then | |
3661 | Apply_Length_Check (A, F_Typ); | |
3662 | ||
3663 | elsif Is_Access_Type (F_Typ) | |
3664 | and then Has_Discriminants (Designated_Type (F_Typ)) | |
3665 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3666 | then | |
3667 | Apply_Discriminant_Check (A, F_Typ); | |
3668 | ||
3669 | else | |
3670 | Apply_Range_Check (A, F_Typ); | |
3671 | end if; | |
2820d220 | 3672 | |
0ab80019 | 3673 | -- Ada 2005 (AI-231) |
2820d220 | 3674 | |
0ab80019 | 3675 | if Ada_Version >= Ada_05 |
2820d220 | 3676 | and then Is_Access_Type (F_Typ) |
1420b484 | 3677 | and then Can_Never_Be_Null (F_Typ) |
aa5147f0 | 3678 | and then Known_Null (A) |
2820d220 | 3679 | then |
1420b484 JM |
3680 | Apply_Compile_Time_Constraint_Error |
3681 | (N => A, | |
aa5147f0 | 3682 | Msg => "(Ada 2005) null not allowed in " |
1420b484 JM |
3683 | & "null-excluding formal?", |
3684 | Reason => CE_Null_Not_Allowed); | |
2820d220 | 3685 | end if; |
996ae0b0 RK |
3686 | end if; |
3687 | ||
8a95f4e8 | 3688 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then |
996ae0b0 RK |
3689 | if Nkind (A) = N_Type_Conversion then |
3690 | if Is_Scalar_Type (A_Typ) then | |
3691 | Apply_Scalar_Range_Check | |
3692 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3693 | else | |
3694 | Apply_Range_Check | |
3695 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3696 | end if; | |
3697 | ||
3698 | else | |
3699 | if Is_Scalar_Type (F_Typ) then | |
3700 | Apply_Scalar_Range_Check (A, A_Typ, F_Typ); | |
3701 | ||
3702 | elsif Is_Array_Type (F_Typ) | |
3703 | and then Ekind (F) = E_Out_Parameter | |
3704 | then | |
3705 | Apply_Length_Check (A, F_Typ); | |
3706 | ||
3707 | else | |
3708 | Apply_Range_Check (A, A_Typ, F_Typ); | |
3709 | end if; | |
3710 | end if; | |
3711 | end if; | |
3712 | ||
3713 | -- An actual associated with an access parameter is implicitly | |
45fc7ddb HK |
3714 | -- converted to the anonymous access type of the formal and must |
3715 | -- satisfy the legality checks for access conversions. | |
996ae0b0 RK |
3716 | |
3717 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
3718 | if not Valid_Conversion (A, F_Typ, A) then | |
3719 | Error_Msg_N | |
3720 | ("invalid implicit conversion for access parameter", A); | |
3721 | end if; | |
3722 | end if; | |
3723 | ||
3724 | -- Check bad case of atomic/volatile argument (RM C.6(12)) | |
3725 | ||
3726 | if Is_By_Reference_Type (Etype (F)) | |
3727 | and then Comes_From_Source (N) | |
3728 | then | |
3729 | if Is_Atomic_Object (A) | |
3730 | and then not Is_Atomic (Etype (F)) | |
3731 | then | |
3732 | Error_Msg_N | |
3733 | ("cannot pass atomic argument to non-atomic formal", | |
3734 | N); | |
3735 | ||
3736 | elsif Is_Volatile_Object (A) | |
3737 | and then not Is_Volatile (Etype (F)) | |
3738 | then | |
3739 | Error_Msg_N | |
3740 | ("cannot pass volatile argument to non-volatile formal", | |
3741 | N); | |
3742 | end if; | |
3743 | end if; | |
3744 | ||
3745 | -- Check that subprograms don't have improper controlling | |
d81b4bfe | 3746 | -- arguments (RM 3.9.2 (9)). |
996ae0b0 | 3747 | |
0669bebe GB |
3748 | -- A primitive operation may have an access parameter of an |
3749 | -- incomplete tagged type, but a dispatching call is illegal | |
3750 | -- if the type is still incomplete. | |
3751 | ||
996ae0b0 RK |
3752 | if Is_Controlling_Formal (F) then |
3753 | Set_Is_Controlling_Actual (A); | |
0669bebe GB |
3754 | |
3755 | if Ekind (Etype (F)) = E_Anonymous_Access_Type then | |
3756 | declare | |
3757 | Desig : constant Entity_Id := Designated_Type (Etype (F)); | |
3758 | begin | |
3759 | if Ekind (Desig) = E_Incomplete_Type | |
3760 | and then No (Full_View (Desig)) | |
3761 | and then No (Non_Limited_View (Desig)) | |
3762 | then | |
3763 | Error_Msg_NE | |
3764 | ("premature use of incomplete type& " & | |
3765 | "in dispatching call", A, Desig); | |
3766 | end if; | |
3767 | end; | |
3768 | end if; | |
3769 | ||
996ae0b0 RK |
3770 | elsif Nkind (A) = N_Explicit_Dereference then |
3771 | Validate_Remote_Access_To_Class_Wide_Type (A); | |
3772 | end if; | |
3773 | ||
3774 | if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A)) | |
3775 | and then not Is_Class_Wide_Type (F_Typ) | |
3776 | and then not Is_Controlling_Formal (F) | |
3777 | then | |
3778 | Error_Msg_N ("class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3779 | |
3780 | if Is_Subprogram (Nam) | |
3781 | and then Comes_From_Source (Nam) | |
3782 | then | |
996ae0b0 RK |
3783 | Error_Msg_Node_2 := F_Typ; |
3784 | Error_Msg_NE | |
82c80734 | 3785 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3786 | end if; |
3787 | ||
3788 | elsif Is_Access_Type (A_Typ) | |
3789 | and then Is_Access_Type (F_Typ) | |
3790 | and then Ekind (F_Typ) /= E_Access_Subprogram_Type | |
aa5147f0 | 3791 | and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type |
996ae0b0 | 3792 | and then (Is_Class_Wide_Type (Designated_Type (A_Typ)) |
07fc65c4 GB |
3793 | or else (Nkind (A) = N_Attribute_Reference |
3794 | and then | |
46fe0142 | 3795 | Is_Class_Wide_Type (Etype (Prefix (A))))) |
996ae0b0 RK |
3796 | and then not Is_Class_Wide_Type (Designated_Type (F_Typ)) |
3797 | and then not Is_Controlling_Formal (F) | |
ae65d635 | 3798 | |
46fe0142 | 3799 | -- Disable these checks for call to imported C++ subprograms |
ae65d635 | 3800 | |
46fe0142 AC |
3801 | and then not |
3802 | (Is_Entity_Name (Name (N)) | |
3803 | and then Is_Imported (Entity (Name (N))) | |
3804 | and then Convention (Entity (Name (N))) = Convention_CPP) | |
996ae0b0 RK |
3805 | then |
3806 | Error_Msg_N | |
3807 | ("access to class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3808 | |
3809 | if Is_Subprogram (Nam) | |
3810 | and then Comes_From_Source (Nam) | |
3811 | then | |
996ae0b0 RK |
3812 | Error_Msg_Node_2 := Designated_Type (F_Typ); |
3813 | Error_Msg_NE | |
82c80734 | 3814 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3815 | end if; |
3816 | end if; | |
3817 | ||
3818 | Eval_Actual (A); | |
3819 | ||
3820 | -- If it is a named association, treat the selector_name as | |
3821 | -- a proper identifier, and mark the corresponding entity. | |
3822 | ||
3823 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3824 | Set_Entity (Selector_Name (Parent (A)), F); | |
3825 | Generate_Reference (F, Selector_Name (Parent (A))); | |
3826 | Set_Etype (Selector_Name (Parent (A)), F_Typ); | |
3827 | Generate_Reference (F_Typ, N, ' '); | |
3828 | end if; | |
3829 | ||
3830 | Prev := A; | |
fbf5a39b AC |
3831 | |
3832 | if Ekind (F) /= E_Out_Parameter then | |
3833 | Check_Unset_Reference (A); | |
3834 | end if; | |
3835 | ||
996ae0b0 RK |
3836 | Next_Actual (A); |
3837 | ||
fbf5a39b AC |
3838 | -- Case where actual is not present |
3839 | ||
996ae0b0 RK |
3840 | else |
3841 | Insert_Default; | |
3842 | end if; | |
3843 | ||
3844 | Next_Formal (F); | |
3845 | end loop; | |
996ae0b0 RK |
3846 | end Resolve_Actuals; |
3847 | ||
3848 | ----------------------- | |
3849 | -- Resolve_Allocator -- | |
3850 | ----------------------- | |
3851 | ||
3852 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is | |
3853 | E : constant Node_Id := Expression (N); | |
3854 | Subtyp : Entity_Id; | |
3855 | Discrim : Entity_Id; | |
3856 | Constr : Node_Id; | |
b7d1f17f HK |
3857 | Aggr : Node_Id; |
3858 | Assoc : Node_Id := Empty; | |
996ae0b0 RK |
3859 | Disc_Exp : Node_Id; |
3860 | ||
b7d1f17f HK |
3861 | procedure Check_Allocator_Discrim_Accessibility |
3862 | (Disc_Exp : Node_Id; | |
3863 | Alloc_Typ : Entity_Id); | |
3864 | -- Check that accessibility level associated with an access discriminant | |
3865 | -- initialized in an allocator by the expression Disc_Exp is not deeper | |
3866 | -- than the level of the allocator type Alloc_Typ. An error message is | |
3867 | -- issued if this condition is violated. Specialized checks are done for | |
3868 | -- the cases of a constraint expression which is an access attribute or | |
3869 | -- an access discriminant. | |
3870 | ||
07fc65c4 | 3871 | function In_Dispatching_Context return Boolean; |
b7d1f17f HK |
3872 | -- If the allocator is an actual in a call, it is allowed to be class- |
3873 | -- wide when the context is not because it is a controlling actual. | |
3874 | ||
3875 | procedure Propagate_Coextensions (Root : Node_Id); | |
3876 | -- Propagate all nested coextensions which are located one nesting | |
3877 | -- level down the tree to the node Root. Example: | |
3878 | -- | |
3879 | -- Top_Record | |
3880 | -- Level_1_Coextension | |
3881 | -- Level_2_Coextension | |
3882 | -- | |
3883 | -- The algorithm is paired with delay actions done by the Expander. In | |
3884 | -- the above example, assume all coextensions are controlled types. | |
3885 | -- The cycle of analysis, resolution and expansion will yield: | |
3886 | -- | |
3887 | -- 1) Analyze Top_Record | |
3888 | -- 2) Analyze Level_1_Coextension | |
3889 | -- 3) Analyze Level_2_Coextension | |
f3d57416 | 3890 | -- 4) Resolve Level_2_Coextension. The allocator is marked as a |
b7d1f17f HK |
3891 | -- coextension. |
3892 | -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is | |
3893 | -- generated to capture the allocated object. Temp_1 is attached | |
3894 | -- to the coextension chain of Level_2_Coextension. | |
3895 | -- 6) Resolve Level_1_Coextension. The allocator is marked as a | |
3896 | -- coextension. A forward tree traversal is performed which finds | |
3897 | -- Level_2_Coextension's list and copies its contents into its | |
3898 | -- own list. | |
3899 | -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is | |
3900 | -- generated to capture the allocated object. Temp_2 is attached | |
3901 | -- to the coextension chain of Level_1_Coextension. Currently, the | |
3902 | -- contents of the list are [Temp_2, Temp_1]. | |
3903 | -- 8) Resolve Top_Record. A forward tree traversal is performed which | |
3904 | -- finds Level_1_Coextension's list and copies its contents into | |
3905 | -- its own list. | |
3906 | -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and | |
3907 | -- Temp_2 and attach them to Top_Record's finalization list. | |
3908 | ||
3909 | ------------------------------------------- | |
3910 | -- Check_Allocator_Discrim_Accessibility -- | |
3911 | ------------------------------------------- | |
3912 | ||
3913 | procedure Check_Allocator_Discrim_Accessibility | |
3914 | (Disc_Exp : Node_Id; | |
3915 | Alloc_Typ : Entity_Id) | |
3916 | is | |
3917 | begin | |
3918 | if Type_Access_Level (Etype (Disc_Exp)) > | |
3919 | Type_Access_Level (Alloc_Typ) | |
3920 | then | |
3921 | Error_Msg_N | |
3922 | ("operand type has deeper level than allocator type", Disc_Exp); | |
3923 | ||
3924 | -- When the expression is an Access attribute the level of the prefix | |
3925 | -- object must not be deeper than that of the allocator's type. | |
3926 | ||
3927 | elsif Nkind (Disc_Exp) = N_Attribute_Reference | |
3928 | and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) | |
3929 | = Attribute_Access | |
3930 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
3931 | > Type_Access_Level (Alloc_Typ) | |
3932 | then | |
3933 | Error_Msg_N | |
3934 | ("prefix of attribute has deeper level than allocator type", | |
3935 | Disc_Exp); | |
3936 | ||
3937 | -- When the expression is an access discriminant the check is against | |
3938 | -- the level of the prefix object. | |
3939 | ||
3940 | elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type | |
3941 | and then Nkind (Disc_Exp) = N_Selected_Component | |
3942 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
3943 | > Type_Access_Level (Alloc_Typ) | |
3944 | then | |
3945 | Error_Msg_N | |
3946 | ("access discriminant has deeper level than allocator type", | |
3947 | Disc_Exp); | |
3948 | ||
3949 | -- All other cases are legal | |
3950 | ||
3951 | else | |
3952 | null; | |
3953 | end if; | |
3954 | end Check_Allocator_Discrim_Accessibility; | |
07fc65c4 GB |
3955 | |
3956 | ---------------------------- | |
3957 | -- In_Dispatching_Context -- | |
3958 | ---------------------------- | |
3959 | ||
3960 | function In_Dispatching_Context return Boolean is | |
3961 | Par : constant Node_Id := Parent (N); | |
07fc65c4 | 3962 | begin |
45fc7ddb | 3963 | return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement) |
07fc65c4 GB |
3964 | and then Is_Entity_Name (Name (Par)) |
3965 | and then Is_Dispatching_Operation (Entity (Name (Par))); | |
3966 | end In_Dispatching_Context; | |
3967 | ||
b7d1f17f HK |
3968 | ---------------------------- |
3969 | -- Propagate_Coextensions -- | |
3970 | ---------------------------- | |
3971 | ||
3972 | procedure Propagate_Coextensions (Root : Node_Id) is | |
3973 | ||
3974 | procedure Copy_List (From : Elist_Id; To : Elist_Id); | |
3975 | -- Copy the contents of list From into list To, preserving the | |
3976 | -- order of elements. | |
3977 | ||
3978 | function Process_Allocator (Nod : Node_Id) return Traverse_Result; | |
3979 | -- Recognize an allocator or a rewritten allocator node and add it | |
f3d57416 | 3980 | -- along with its nested coextensions to the list of Root. |
b7d1f17f HK |
3981 | |
3982 | --------------- | |
3983 | -- Copy_List -- | |
3984 | --------------- | |
3985 | ||
3986 | procedure Copy_List (From : Elist_Id; To : Elist_Id) is | |
3987 | From_Elmt : Elmt_Id; | |
3988 | begin | |
3989 | From_Elmt := First_Elmt (From); | |
3990 | while Present (From_Elmt) loop | |
3991 | Append_Elmt (Node (From_Elmt), To); | |
3992 | Next_Elmt (From_Elmt); | |
3993 | end loop; | |
3994 | end Copy_List; | |
3995 | ||
3996 | ----------------------- | |
3997 | -- Process_Allocator -- | |
3998 | ----------------------- | |
3999 | ||
4000 | function Process_Allocator (Nod : Node_Id) return Traverse_Result is | |
4001 | Orig_Nod : Node_Id := Nod; | |
4002 | ||
4003 | begin | |
4004 | -- This is a possible rewritten subtype indication allocator. Any | |
4005 | -- nested coextensions will appear as discriminant constraints. | |
4006 | ||
4007 | if Nkind (Nod) = N_Identifier | |
4008 | and then Present (Original_Node (Nod)) | |
4009 | and then Nkind (Original_Node (Nod)) = N_Subtype_Indication | |
4010 | then | |
4011 | declare | |
4012 | Discr : Node_Id; | |
4013 | Discr_Elmt : Elmt_Id; | |
4014 | ||
4015 | begin | |
4016 | if Is_Record_Type (Entity (Nod)) then | |
4017 | Discr_Elmt := | |
4018 | First_Elmt (Discriminant_Constraint (Entity (Nod))); | |
4019 | while Present (Discr_Elmt) loop | |
4020 | Discr := Node (Discr_Elmt); | |
4021 | ||
4022 | if Nkind (Discr) = N_Identifier | |
4023 | and then Present (Original_Node (Discr)) | |
4024 | and then Nkind (Original_Node (Discr)) = N_Allocator | |
4025 | and then Present (Coextensions ( | |
4026 | Original_Node (Discr))) | |
4027 | then | |
4028 | if No (Coextensions (Root)) then | |
4029 | Set_Coextensions (Root, New_Elmt_List); | |
4030 | end if; | |
4031 | ||
4032 | Copy_List | |
4033 | (From => Coextensions (Original_Node (Discr)), | |
4034 | To => Coextensions (Root)); | |
4035 | end if; | |
4036 | ||
4037 | Next_Elmt (Discr_Elmt); | |
4038 | end loop; | |
4039 | ||
4040 | -- There is no need to continue the traversal of this | |
4041 | -- subtree since all the information has already been | |
4042 | -- propagated. | |
4043 | ||
4044 | return Skip; | |
4045 | end if; | |
4046 | end; | |
4047 | ||
4048 | -- Case of either a stand alone allocator or a rewritten allocator | |
4049 | -- with an aggregate. | |
4050 | ||
4051 | else | |
4052 | if Present (Original_Node (Nod)) then | |
4053 | Orig_Nod := Original_Node (Nod); | |
4054 | end if; | |
4055 | ||
4056 | if Nkind (Orig_Nod) = N_Allocator then | |
4057 | ||
4058 | -- Propagate the list of nested coextensions to the Root | |
4059 | -- allocator. This is done through list copy since a single | |
4060 | -- allocator may have multiple coextensions. Do not touch | |
4061 | -- coextensions roots. | |
4062 | ||
4063 | if not Is_Coextension_Root (Orig_Nod) | |
4064 | and then Present (Coextensions (Orig_Nod)) | |
4065 | then | |
4066 | if No (Coextensions (Root)) then | |
4067 | Set_Coextensions (Root, New_Elmt_List); | |
4068 | end if; | |
4069 | ||
4070 | Copy_List | |
4071 | (From => Coextensions (Orig_Nod), | |
4072 | To => Coextensions (Root)); | |
4073 | end if; | |
4074 | ||
4075 | -- There is no need to continue the traversal of this | |
4076 | -- subtree since all the information has already been | |
4077 | -- propagated. | |
4078 | ||
4079 | return Skip; | |
4080 | end if; | |
4081 | end if; | |
4082 | ||
4083 | -- Keep on traversing, looking for the next allocator | |
4084 | ||
4085 | return OK; | |
4086 | end Process_Allocator; | |
4087 | ||
4088 | procedure Process_Allocators is | |
4089 | new Traverse_Proc (Process_Allocator); | |
4090 | ||
4091 | -- Start of processing for Propagate_Coextensions | |
4092 | ||
4093 | begin | |
4094 | Process_Allocators (Expression (Root)); | |
4095 | end Propagate_Coextensions; | |
4096 | ||
07fc65c4 GB |
4097 | -- Start of processing for Resolve_Allocator |
4098 | ||
996ae0b0 RK |
4099 | begin |
4100 | -- Replace general access with specific type | |
4101 | ||
4102 | if Ekind (Etype (N)) = E_Allocator_Type then | |
4103 | Set_Etype (N, Base_Type (Typ)); | |
4104 | end if; | |
4105 | ||
0669bebe | 4106 | if Is_Abstract_Type (Typ) then |
996ae0b0 RK |
4107 | Error_Msg_N ("type of allocator cannot be abstract", N); |
4108 | end if; | |
4109 | ||
4110 | -- For qualified expression, resolve the expression using the | |
4111 | -- given subtype (nothing to do for type mark, subtype indication) | |
4112 | ||
4113 | if Nkind (E) = N_Qualified_Expression then | |
4114 | if Is_Class_Wide_Type (Etype (E)) | |
4115 | and then not Is_Class_Wide_Type (Designated_Type (Typ)) | |
07fc65c4 | 4116 | and then not In_Dispatching_Context |
996ae0b0 RK |
4117 | then |
4118 | Error_Msg_N | |
4119 | ("class-wide allocator not allowed for this access type", N); | |
4120 | end if; | |
4121 | ||
4122 | Resolve (Expression (E), Etype (E)); | |
4123 | Check_Unset_Reference (Expression (E)); | |
4124 | ||
fbf5a39b | 4125 | -- A qualified expression requires an exact match of the type, |
7b4db06c | 4126 | -- class-wide matching is not allowed. |
fbf5a39b | 4127 | |
7b4db06c | 4128 | if (Is_Class_Wide_Type (Etype (Expression (E))) |
b46be8a2 | 4129 | or else Is_Class_Wide_Type (Etype (E))) |
fbf5a39b AC |
4130 | and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E)) |
4131 | then | |
4132 | Wrong_Type (Expression (E), Etype (E)); | |
4133 | end if; | |
4134 | ||
b7d1f17f HK |
4135 | -- A special accessibility check is needed for allocators that |
4136 | -- constrain access discriminants. The level of the type of the | |
4137 | -- expression used to constrain an access discriminant cannot be | |
f3d57416 | 4138 | -- deeper than the type of the allocator (in contrast to access |
b7d1f17f HK |
4139 | -- parameters, where the level of the actual can be arbitrary). |
4140 | ||
4141 | -- We can't use Valid_Conversion to perform this check because | |
4142 | -- in general the type of the allocator is unrelated to the type | |
4143 | -- of the access discriminant. | |
4144 | ||
4145 | if Ekind (Typ) /= E_Anonymous_Access_Type | |
4146 | or else Is_Local_Anonymous_Access (Typ) | |
4147 | then | |
4148 | Subtyp := Entity (Subtype_Mark (E)); | |
4149 | ||
4150 | Aggr := Original_Node (Expression (E)); | |
4151 | ||
4152 | if Has_Discriminants (Subtyp) | |
45fc7ddb | 4153 | and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate) |
b7d1f17f HK |
4154 | then |
4155 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4156 | ||
4157 | -- Get the first component expression of the aggregate | |
4158 | ||
4159 | if Present (Expressions (Aggr)) then | |
4160 | Disc_Exp := First (Expressions (Aggr)); | |
4161 | ||
4162 | elsif Present (Component_Associations (Aggr)) then | |
4163 | Assoc := First (Component_Associations (Aggr)); | |
4164 | ||
4165 | if Present (Assoc) then | |
4166 | Disc_Exp := Expression (Assoc); | |
4167 | else | |
4168 | Disc_Exp := Empty; | |
4169 | end if; | |
4170 | ||
4171 | else | |
4172 | Disc_Exp := Empty; | |
4173 | end if; | |
4174 | ||
4175 | while Present (Discrim) and then Present (Disc_Exp) loop | |
4176 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4177 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); | |
4178 | end if; | |
4179 | ||
4180 | Next_Discriminant (Discrim); | |
4181 | ||
4182 | if Present (Discrim) then | |
4183 | if Present (Assoc) then | |
4184 | Next (Assoc); | |
4185 | Disc_Exp := Expression (Assoc); | |
4186 | ||
4187 | elsif Present (Next (Disc_Exp)) then | |
4188 | Next (Disc_Exp); | |
4189 | ||
4190 | else | |
4191 | Assoc := First (Component_Associations (Aggr)); | |
4192 | ||
4193 | if Present (Assoc) then | |
4194 | Disc_Exp := Expression (Assoc); | |
4195 | else | |
4196 | Disc_Exp := Empty; | |
4197 | end if; | |
4198 | end if; | |
4199 | end if; | |
4200 | end loop; | |
4201 | end if; | |
4202 | end if; | |
4203 | ||
996ae0b0 RK |
4204 | -- For a subtype mark or subtype indication, freeze the subtype |
4205 | ||
4206 | else | |
4207 | Freeze_Expression (E); | |
4208 | ||
4209 | if Is_Access_Constant (Typ) and then not No_Initialization (N) then | |
4210 | Error_Msg_N | |
4211 | ("initialization required for access-to-constant allocator", N); | |
4212 | end if; | |
4213 | ||
4214 | -- A special accessibility check is needed for allocators that | |
4215 | -- constrain access discriminants. The level of the type of the | |
b7d1f17f | 4216 | -- expression used to constrain an access discriminant cannot be |
f3d57416 | 4217 | -- deeper than the type of the allocator (in contrast to access |
996ae0b0 RK |
4218 | -- parameters, where the level of the actual can be arbitrary). |
4219 | -- We can't use Valid_Conversion to perform this check because | |
4220 | -- in general the type of the allocator is unrelated to the type | |
b7d1f17f | 4221 | -- of the access discriminant. |
996ae0b0 RK |
4222 | |
4223 | if Nkind (Original_Node (E)) = N_Subtype_Indication | |
b7d1f17f HK |
4224 | and then (Ekind (Typ) /= E_Anonymous_Access_Type |
4225 | or else Is_Local_Anonymous_Access (Typ)) | |
996ae0b0 RK |
4226 | then |
4227 | Subtyp := Entity (Subtype_Mark (Original_Node (E))); | |
4228 | ||
4229 | if Has_Discriminants (Subtyp) then | |
4230 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4231 | Constr := First (Constraints (Constraint (Original_Node (E)))); | |
996ae0b0 RK |
4232 | while Present (Discrim) and then Present (Constr) loop |
4233 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4234 | if Nkind (Constr) = N_Discriminant_Association then | |
4235 | Disc_Exp := Original_Node (Expression (Constr)); | |
4236 | else | |
4237 | Disc_Exp := Original_Node (Constr); | |
4238 | end if; | |
4239 | ||
b7d1f17f | 4240 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); |
996ae0b0 | 4241 | end if; |
b7d1f17f | 4242 | |
996ae0b0 RK |
4243 | Next_Discriminant (Discrim); |
4244 | Next (Constr); | |
4245 | end loop; | |
4246 | end if; | |
4247 | end if; | |
4248 | end if; | |
4249 | ||
758c442c GD |
4250 | -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility |
4251 | -- check that the level of the type of the created object is not deeper | |
4252 | -- than the level of the allocator's access type, since extensions can | |
4253 | -- now occur at deeper levels than their ancestor types. This is a | |
4254 | -- static accessibility level check; a run-time check is also needed in | |
4255 | -- the case of an initialized allocator with a class-wide argument (see | |
4256 | -- Expand_Allocator_Expression). | |
4257 | ||
4258 | if Ada_Version >= Ada_05 | |
4259 | and then Is_Class_Wide_Type (Designated_Type (Typ)) | |
4260 | then | |
4261 | declare | |
b7d1f17f | 4262 | Exp_Typ : Entity_Id; |
758c442c GD |
4263 | |
4264 | begin | |
4265 | if Nkind (E) = N_Qualified_Expression then | |
4266 | Exp_Typ := Etype (E); | |
4267 | elsif Nkind (E) = N_Subtype_Indication then | |
4268 | Exp_Typ := Entity (Subtype_Mark (Original_Node (E))); | |
4269 | else | |
4270 | Exp_Typ := Entity (E); | |
4271 | end if; | |
4272 | ||
4273 | if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then | |
4274 | if In_Instance_Body then | |
4275 | Error_Msg_N ("?type in allocator has deeper level than" & | |
4276 | " designated class-wide type", E); | |
c8ef728f ES |
4277 | Error_Msg_N ("\?Program_Error will be raised at run time", |
4278 | E); | |
758c442c GD |
4279 | Rewrite (N, |
4280 | Make_Raise_Program_Error (Sloc (N), | |
4281 | Reason => PE_Accessibility_Check_Failed)); | |
4282 | Set_Etype (N, Typ); | |
aa180613 RD |
4283 | |
4284 | -- Do not apply Ada 2005 accessibility checks on a class-wide | |
4285 | -- allocator if the type given in the allocator is a formal | |
4286 | -- type. A run-time check will be performed in the instance. | |
4287 | ||
4288 | elsif not Is_Generic_Type (Exp_Typ) then | |
758c442c GD |
4289 | Error_Msg_N ("type in allocator has deeper level than" & |
4290 | " designated class-wide type", E); | |
4291 | end if; | |
4292 | end if; | |
4293 | end; | |
4294 | end if; | |
4295 | ||
996ae0b0 RK |
4296 | -- Check for allocation from an empty storage pool |
4297 | ||
4298 | if No_Pool_Assigned (Typ) then | |
4299 | declare | |
4300 | Loc : constant Source_Ptr := Sloc (N); | |
996ae0b0 | 4301 | begin |
ed2233dc | 4302 | Error_Msg_N ("?allocation from empty storage pool!", N); |
aa5147f0 | 4303 | Error_Msg_N ("\?Storage_Error will be raised at run time!", N); |
996ae0b0 | 4304 | Insert_Action (N, |
07fc65c4 GB |
4305 | Make_Raise_Storage_Error (Loc, |
4306 | Reason => SE_Empty_Storage_Pool)); | |
996ae0b0 | 4307 | end; |
1420b484 JM |
4308 | |
4309 | -- If the context is an unchecked conversion, as may happen within | |
4310 | -- an inlined subprogram, the allocator is being resolved with its | |
4311 | -- own anonymous type. In that case, if the target type has a specific | |
4312 | -- storage pool, it must be inherited explicitly by the allocator type. | |
4313 | ||
4314 | elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion | |
4315 | and then No (Associated_Storage_Pool (Typ)) | |
4316 | then | |
4317 | Set_Associated_Storage_Pool | |
4318 | (Typ, Associated_Storage_Pool (Etype (Parent (N)))); | |
996ae0b0 | 4319 | end if; |
b7d1f17f HK |
4320 | |
4321 | -- An erroneous allocator may be rewritten as a raise Program_Error | |
4322 | -- statement. | |
4323 | ||
4324 | if Nkind (N) = N_Allocator then | |
4325 | ||
4326 | -- An anonymous access discriminant is the definition of a | |
aa5147f0 | 4327 | -- coextension. |
b7d1f17f HK |
4328 | |
4329 | if Ekind (Typ) = E_Anonymous_Access_Type | |
4330 | and then Nkind (Associated_Node_For_Itype (Typ)) = | |
4331 | N_Discriminant_Specification | |
4332 | then | |
4333 | -- Avoid marking an allocator as a dynamic coextension if it is | |
aa5147f0 | 4334 | -- within a static construct. |
b7d1f17f HK |
4335 | |
4336 | if not Is_Static_Coextension (N) then | |
aa5147f0 | 4337 | Set_Is_Dynamic_Coextension (N); |
b7d1f17f HK |
4338 | end if; |
4339 | ||
4340 | -- Cleanup for potential static coextensions | |
4341 | ||
4342 | else | |
aa5147f0 ES |
4343 | Set_Is_Dynamic_Coextension (N, False); |
4344 | Set_Is_Static_Coextension (N, False); | |
b7d1f17f HK |
4345 | end if; |
4346 | ||
aa5147f0 ES |
4347 | -- There is no need to propagate any nested coextensions if they |
4348 | -- are marked as static since they will be rewritten on the spot. | |
4349 | ||
4350 | if not Is_Static_Coextension (N) then | |
4351 | Propagate_Coextensions (N); | |
4352 | end if; | |
b7d1f17f | 4353 | end if; |
996ae0b0 RK |
4354 | end Resolve_Allocator; |
4355 | ||
4356 | --------------------------- | |
4357 | -- Resolve_Arithmetic_Op -- | |
4358 | --------------------------- | |
4359 | ||
4360 | -- Used for resolving all arithmetic operators except exponentiation | |
4361 | ||
4362 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
4363 | L : constant Node_Id := Left_Opnd (N); |
4364 | R : constant Node_Id := Right_Opnd (N); | |
4365 | TL : constant Entity_Id := Base_Type (Etype (L)); | |
4366 | TR : constant Entity_Id := Base_Type (Etype (R)); | |
4367 | T : Entity_Id; | |
4368 | Rop : Node_Id; | |
996ae0b0 RK |
4369 | |
4370 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
4371 | -- We do the resolution using the base type, because intermediate values | |
4372 | -- in expressions always are of the base type, not a subtype of it. | |
4373 | ||
aa180613 RD |
4374 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean; |
4375 | -- Returns True if N is in a context that expects "any real type" | |
4376 | ||
996ae0b0 RK |
4377 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean; |
4378 | -- Return True iff given type is Integer or universal real/integer | |
4379 | ||
4380 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id); | |
4381 | -- Choose type of integer literal in fixed-point operation to conform | |
4382 | -- to available fixed-point type. T is the type of the other operand, | |
4383 | -- which is needed to determine the expected type of N. | |
4384 | ||
4385 | procedure Set_Operand_Type (N : Node_Id); | |
4386 | -- Set operand type to T if universal | |
4387 | ||
aa180613 RD |
4388 | ------------------------------- |
4389 | -- Expected_Type_Is_Any_Real -- | |
4390 | ------------------------------- | |
4391 | ||
4392 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is | |
4393 | begin | |
4394 | -- N is the expression after "delta" in a fixed_point_definition; | |
4395 | -- see RM-3.5.9(6): | |
4396 | ||
45fc7ddb HK |
4397 | return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition, |
4398 | N_Decimal_Fixed_Point_Definition, | |
aa180613 RD |
4399 | |
4400 | -- N is one of the bounds in a real_range_specification; | |
4401 | -- see RM-3.5.7(5): | |
4402 | ||
45fc7ddb | 4403 | N_Real_Range_Specification, |
aa180613 RD |
4404 | |
4405 | -- N is the expression of a delta_constraint; | |
4406 | -- see RM-J.3(3): | |
4407 | ||
45fc7ddb | 4408 | N_Delta_Constraint); |
aa180613 RD |
4409 | end Expected_Type_Is_Any_Real; |
4410 | ||
996ae0b0 RK |
4411 | ----------------------------- |
4412 | -- Is_Integer_Or_Universal -- | |
4413 | ----------------------------- | |
4414 | ||
4415 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean is | |
4416 | T : Entity_Id; | |
4417 | Index : Interp_Index; | |
4418 | It : Interp; | |
4419 | ||
4420 | begin | |
4421 | if not Is_Overloaded (N) then | |
4422 | T := Etype (N); | |
4423 | return Base_Type (T) = Base_Type (Standard_Integer) | |
4424 | or else T = Universal_Integer | |
4425 | or else T = Universal_Real; | |
4426 | else | |
4427 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4428 | while Present (It.Typ) loop |
996ae0b0 RK |
4429 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) |
4430 | or else It.Typ = Universal_Integer | |
4431 | or else It.Typ = Universal_Real | |
4432 | then | |
4433 | return True; | |
4434 | end if; | |
4435 | ||
4436 | Get_Next_Interp (Index, It); | |
4437 | end loop; | |
4438 | end if; | |
4439 | ||
4440 | return False; | |
4441 | end Is_Integer_Or_Universal; | |
4442 | ||
4443 | ---------------------------- | |
4444 | -- Set_Mixed_Mode_Operand -- | |
4445 | ---------------------------- | |
4446 | ||
4447 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is | |
4448 | Index : Interp_Index; | |
4449 | It : Interp; | |
4450 | ||
4451 | begin | |
4452 | if Universal_Interpretation (N) = Universal_Integer then | |
4453 | ||
4454 | -- A universal integer literal is resolved as standard integer | |
758c442c GD |
4455 | -- except in the case of a fixed-point result, where we leave it |
4456 | -- as universal (to be handled by Exp_Fixd later on) | |
996ae0b0 RK |
4457 | |
4458 | if Is_Fixed_Point_Type (T) then | |
4459 | Resolve (N, Universal_Integer); | |
4460 | else | |
4461 | Resolve (N, Standard_Integer); | |
4462 | end if; | |
4463 | ||
4464 | elsif Universal_Interpretation (N) = Universal_Real | |
4465 | and then (T = Base_Type (Standard_Integer) | |
4466 | or else T = Universal_Integer | |
4467 | or else T = Universal_Real) | |
4468 | then | |
4469 | -- A universal real can appear in a fixed-type context. We resolve | |
4470 | -- the literal with that context, even though this might raise an | |
4471 | -- exception prematurely (the other operand may be zero). | |
4472 | ||
4473 | Resolve (N, B_Typ); | |
4474 | ||
4475 | elsif Etype (N) = Base_Type (Standard_Integer) | |
4476 | and then T = Universal_Real | |
4477 | and then Is_Overloaded (N) | |
4478 | then | |
4479 | -- Integer arg in mixed-mode operation. Resolve with universal | |
4480 | -- type, in case preference rule must be applied. | |
4481 | ||
4482 | Resolve (N, Universal_Integer); | |
4483 | ||
4484 | elsif Etype (N) = T | |
4485 | and then B_Typ /= Universal_Fixed | |
4486 | then | |
a77842bd | 4487 | -- Not a mixed-mode operation, resolve with context |
996ae0b0 RK |
4488 | |
4489 | Resolve (N, B_Typ); | |
4490 | ||
4491 | elsif Etype (N) = Any_Fixed then | |
4492 | ||
a77842bd | 4493 | -- N may itself be a mixed-mode operation, so use context type |
996ae0b0 RK |
4494 | |
4495 | Resolve (N, B_Typ); | |
4496 | ||
4497 | elsif Is_Fixed_Point_Type (T) | |
4498 | and then B_Typ = Universal_Fixed | |
4499 | and then Is_Overloaded (N) | |
4500 | then | |
4501 | -- Must be (fixed * fixed) operation, operand must have one | |
4502 | -- compatible interpretation. | |
4503 | ||
4504 | Resolve (N, Any_Fixed); | |
4505 | ||
4506 | elsif Is_Fixed_Point_Type (B_Typ) | |
4507 | and then (T = Universal_Real | |
4508 | or else Is_Fixed_Point_Type (T)) | |
4509 | and then Is_Overloaded (N) | |
4510 | then | |
4511 | -- C * F(X) in a fixed context, where C is a real literal or a | |
4512 | -- fixed-point expression. F must have either a fixed type | |
4513 | -- interpretation or an integer interpretation, but not both. | |
4514 | ||
4515 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4516 | while Present (It.Typ) loop |
996ae0b0 RK |
4517 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) then |
4518 | ||
4519 | if Analyzed (N) then | |
4520 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4521 | else | |
4522 | Resolve (N, Standard_Integer); | |
4523 | end if; | |
4524 | ||
4525 | elsif Is_Fixed_Point_Type (It.Typ) then | |
4526 | ||
4527 | if Analyzed (N) then | |
4528 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4529 | else | |
4530 | Resolve (N, It.Typ); | |
4531 | end if; | |
4532 | end if; | |
4533 | ||
4534 | Get_Next_Interp (Index, It); | |
4535 | end loop; | |
4536 | ||
758c442c GD |
4537 | -- Reanalyze the literal with the fixed type of the context. If |
4538 | -- context is Universal_Fixed, we are within a conversion, leave | |
4539 | -- the literal as a universal real because there is no usable | |
4540 | -- fixed type, and the target of the conversion plays no role in | |
4541 | -- the resolution. | |
996ae0b0 | 4542 | |
0ab80019 AC |
4543 | declare |
4544 | Op2 : Node_Id; | |
4545 | T2 : Entity_Id; | |
4546 | ||
4547 | begin | |
4548 | if N = L then | |
4549 | Op2 := R; | |
4550 | else | |
4551 | Op2 := L; | |
4552 | end if; | |
4553 | ||
4554 | if B_Typ = Universal_Fixed | |
4555 | and then Nkind (Op2) = N_Real_Literal | |
4556 | then | |
4557 | T2 := Universal_Real; | |
4558 | else | |
4559 | T2 := B_Typ; | |
4560 | end if; | |
4561 | ||
4562 | Set_Analyzed (Op2, False); | |
4563 | Resolve (Op2, T2); | |
4564 | end; | |
996ae0b0 RK |
4565 | |
4566 | else | |
fbf5a39b | 4567 | Resolve (N); |
996ae0b0 RK |
4568 | end if; |
4569 | end Set_Mixed_Mode_Operand; | |
4570 | ||
4571 | ---------------------- | |
4572 | -- Set_Operand_Type -- | |
4573 | ---------------------- | |
4574 | ||
4575 | procedure Set_Operand_Type (N : Node_Id) is | |
4576 | begin | |
4577 | if Etype (N) = Universal_Integer | |
4578 | or else Etype (N) = Universal_Real | |
4579 | then | |
4580 | Set_Etype (N, T); | |
4581 | end if; | |
4582 | end Set_Operand_Type; | |
4583 | ||
996ae0b0 RK |
4584 | -- Start of processing for Resolve_Arithmetic_Op |
4585 | ||
4586 | begin | |
4587 | if Comes_From_Source (N) | |
4588 | and then Ekind (Entity (N)) = E_Function | |
4589 | and then Is_Imported (Entity (N)) | |
fbf5a39b | 4590 | and then Is_Intrinsic_Subprogram (Entity (N)) |
996ae0b0 RK |
4591 | then |
4592 | Resolve_Intrinsic_Operator (N, Typ); | |
4593 | return; | |
4594 | ||
4595 | -- Special-case for mixed-mode universal expressions or fixed point | |
4596 | -- type operation: each argument is resolved separately. The same | |
4597 | -- treatment is required if one of the operands of a fixed point | |
4598 | -- operation is universal real, since in this case we don't do a | |
4599 | -- conversion to a specific fixed-point type (instead the expander | |
4600 | -- takes care of the case). | |
4601 | ||
45fc7ddb | 4602 | elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real) |
996ae0b0 RK |
4603 | and then Present (Universal_Interpretation (L)) |
4604 | and then Present (Universal_Interpretation (R)) | |
4605 | then | |
4606 | Resolve (L, Universal_Interpretation (L)); | |
4607 | Resolve (R, Universal_Interpretation (R)); | |
4608 | Set_Etype (N, B_Typ); | |
4609 | ||
4610 | elsif (B_Typ = Universal_Real | |
45fc7ddb HK |
4611 | or else Etype (N) = Universal_Fixed |
4612 | or else (Etype (N) = Any_Fixed | |
4613 | and then Is_Fixed_Point_Type (B_Typ)) | |
4614 | or else (Is_Fixed_Point_Type (B_Typ) | |
4615 | and then (Is_Integer_Or_Universal (L) | |
4616 | or else | |
4617 | Is_Integer_Or_Universal (R)))) | |
4618 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) | |
996ae0b0 RK |
4619 | then |
4620 | if TL = Universal_Integer or else TR = Universal_Integer then | |
4621 | Check_For_Visible_Operator (N, B_Typ); | |
4622 | end if; | |
4623 | ||
4624 | -- If context is a fixed type and one operand is integer, the | |
4625 | -- other is resolved with the type of the context. | |
4626 | ||
4627 | if Is_Fixed_Point_Type (B_Typ) | |
4628 | and then (Base_Type (TL) = Base_Type (Standard_Integer) | |
4629 | or else TL = Universal_Integer) | |
4630 | then | |
4631 | Resolve (R, B_Typ); | |
4632 | Resolve (L, TL); | |
4633 | ||
4634 | elsif Is_Fixed_Point_Type (B_Typ) | |
4635 | and then (Base_Type (TR) = Base_Type (Standard_Integer) | |
4636 | or else TR = Universal_Integer) | |
4637 | then | |
4638 | Resolve (L, B_Typ); | |
4639 | Resolve (R, TR); | |
4640 | ||
4641 | else | |
4642 | Set_Mixed_Mode_Operand (L, TR); | |
4643 | Set_Mixed_Mode_Operand (R, TL); | |
4644 | end if; | |
4645 | ||
45fc7ddb HK |
4646 | -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed |
4647 | -- multiplying operators from being used when the expected type is | |
4648 | -- also universal_fixed. Note that B_Typ will be Universal_Fixed in | |
4649 | -- some cases where the expected type is actually Any_Real; | |
4650 | -- Expected_Type_Is_Any_Real takes care of that case. | |
aa180613 | 4651 | |
996ae0b0 RK |
4652 | if Etype (N) = Universal_Fixed |
4653 | or else Etype (N) = Any_Fixed | |
4654 | then | |
4655 | if B_Typ = Universal_Fixed | |
aa180613 | 4656 | and then not Expected_Type_Is_Any_Real (N) |
45fc7ddb HK |
4657 | and then not Nkind_In (Parent (N), N_Type_Conversion, |
4658 | N_Unchecked_Type_Conversion) | |
996ae0b0 | 4659 | then |
45fc7ddb HK |
4660 | Error_Msg_N ("type cannot be determined from context!", N); |
4661 | Error_Msg_N ("\explicit conversion to result type required", N); | |
996ae0b0 RK |
4662 | |
4663 | Set_Etype (L, Any_Type); | |
4664 | Set_Etype (R, Any_Type); | |
4665 | ||
4666 | else | |
0ab80019 | 4667 | if Ada_Version = Ada_83 |
45fc7ddb HK |
4668 | and then Etype (N) = Universal_Fixed |
4669 | and then not | |
4670 | Nkind_In (Parent (N), N_Type_Conversion, | |
4671 | N_Unchecked_Type_Conversion) | |
996ae0b0 RK |
4672 | then |
4673 | Error_Msg_N | |
45fc7ddb HK |
4674 | ("(Ada 83) fixed-point operation " |
4675 | & "needs explicit conversion", N); | |
996ae0b0 RK |
4676 | end if; |
4677 | ||
aa180613 RD |
4678 | -- The expected type is "any real type" in contexts like |
4679 | -- type T is delta <universal_fixed-expression> ... | |
4680 | -- in which case we need to set the type to Universal_Real | |
4681 | -- so that static expression evaluation will work properly. | |
4682 | ||
4683 | if Expected_Type_Is_Any_Real (N) then | |
4684 | Set_Etype (N, Universal_Real); | |
4685 | else | |
4686 | Set_Etype (N, B_Typ); | |
4687 | end if; | |
996ae0b0 RK |
4688 | end if; |
4689 | ||
4690 | elsif Is_Fixed_Point_Type (B_Typ) | |
4691 | and then (Is_Integer_Or_Universal (L) | |
4692 | or else Nkind (L) = N_Real_Literal | |
4693 | or else Nkind (R) = N_Real_Literal | |
45fc7ddb | 4694 | or else Is_Integer_Or_Universal (R)) |
996ae0b0 RK |
4695 | then |
4696 | Set_Etype (N, B_Typ); | |
4697 | ||
4698 | elsif Etype (N) = Any_Fixed then | |
4699 | ||
4700 | -- If no previous errors, this is only possible if one operand | |
4701 | -- is overloaded and the context is universal. Resolve as such. | |
4702 | ||
4703 | Set_Etype (N, B_Typ); | |
4704 | end if; | |
4705 | ||
4706 | else | |
4707 | if (TL = Universal_Integer or else TL = Universal_Real) | |
45fc7ddb HK |
4708 | and then |
4709 | (TR = Universal_Integer or else TR = Universal_Real) | |
996ae0b0 RK |
4710 | then |
4711 | Check_For_Visible_Operator (N, B_Typ); | |
4712 | end if; | |
4713 | ||
4714 | -- If the context is Universal_Fixed and the operands are also | |
4715 | -- universal fixed, this is an error, unless there is only one | |
841dd0f5 | 4716 | -- applicable fixed_point type (usually Duration). |
996ae0b0 | 4717 | |
45fc7ddb | 4718 | if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then |
996ae0b0 RK |
4719 | T := Unique_Fixed_Point_Type (N); |
4720 | ||
4721 | if T = Any_Type then | |
4722 | Set_Etype (N, T); | |
4723 | return; | |
4724 | else | |
4725 | Resolve (L, T); | |
4726 | Resolve (R, T); | |
4727 | end if; | |
4728 | ||
4729 | else | |
4730 | Resolve (L, B_Typ); | |
4731 | Resolve (R, B_Typ); | |
4732 | end if; | |
4733 | ||
4734 | -- If one of the arguments was resolved to a non-universal type. | |
4735 | -- label the result of the operation itself with the same type. | |
4736 | -- Do the same for the universal argument, if any. | |
4737 | ||
4738 | T := Intersect_Types (L, R); | |
4739 | Set_Etype (N, Base_Type (T)); | |
4740 | Set_Operand_Type (L); | |
4741 | Set_Operand_Type (R); | |
4742 | end if; | |
4743 | ||
fbf5a39b | 4744 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
4745 | Eval_Arithmetic_Op (N); |
4746 | ||
4747 | -- Set overflow and division checking bit. Much cleverer code needed | |
4748 | -- here eventually and perhaps the Resolve routines should be separated | |
4749 | -- for the various arithmetic operations, since they will need | |
4750 | -- different processing. ??? | |
4751 | ||
4752 | if Nkind (N) in N_Op then | |
4753 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 4754 | Enable_Overflow_Check (N); |
996ae0b0 RK |
4755 | end if; |
4756 | ||
fbf5a39b AC |
4757 | -- Give warning if explicit division by zero |
4758 | ||
45fc7ddb | 4759 | if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod) |
996ae0b0 RK |
4760 | and then not Division_Checks_Suppressed (Etype (N)) |
4761 | then | |
fbf5a39b AC |
4762 | Rop := Right_Opnd (N); |
4763 | ||
4764 | if Compile_Time_Known_Value (Rop) | |
4765 | and then ((Is_Integer_Type (Etype (Rop)) | |
45fc7ddb | 4766 | and then Expr_Value (Rop) = Uint_0) |
fbf5a39b AC |
4767 | or else |
4768 | (Is_Real_Type (Etype (Rop)) | |
45fc7ddb | 4769 | and then Expr_Value_R (Rop) = Ureal_0)) |
fbf5a39b | 4770 | then |
aa180613 RD |
4771 | -- Specialize the warning message according to the operation |
4772 | ||
4773 | case Nkind (N) is | |
4774 | when N_Op_Divide => | |
4775 | Apply_Compile_Time_Constraint_Error | |
4776 | (N, "division by zero?", CE_Divide_By_Zero, | |
4777 | Loc => Sloc (Right_Opnd (N))); | |
4778 | ||
4779 | when N_Op_Rem => | |
4780 | Apply_Compile_Time_Constraint_Error | |
4781 | (N, "rem with zero divisor?", CE_Divide_By_Zero, | |
4782 | Loc => Sloc (Right_Opnd (N))); | |
4783 | ||
4784 | when N_Op_Mod => | |
4785 | Apply_Compile_Time_Constraint_Error | |
4786 | (N, "mod with zero divisor?", CE_Divide_By_Zero, | |
4787 | Loc => Sloc (Right_Opnd (N))); | |
4788 | ||
4789 | -- Division by zero can only happen with division, rem, | |
4790 | -- and mod operations. | |
4791 | ||
4792 | when others => | |
4793 | raise Program_Error; | |
4794 | end case; | |
fbf5a39b AC |
4795 | |
4796 | -- Otherwise just set the flag to check at run time | |
4797 | ||
4798 | else | |
b7d1f17f | 4799 | Activate_Division_Check (N); |
fbf5a39b | 4800 | end if; |
996ae0b0 | 4801 | end if; |
45fc7ddb HK |
4802 | |
4803 | -- If Restriction No_Implicit_Conditionals is active, then it is | |
4804 | -- violated if either operand can be negative for mod, or for rem | |
4805 | -- if both operands can be negative. | |
4806 | ||
7a963087 | 4807 | if Restriction_Check_Required (No_Implicit_Conditionals) |
45fc7ddb HK |
4808 | and then Nkind_In (N, N_Op_Rem, N_Op_Mod) |
4809 | then | |
4810 | declare | |
4811 | Lo : Uint; | |
4812 | Hi : Uint; | |
4813 | OK : Boolean; | |
4814 | ||
4815 | LNeg : Boolean; | |
4816 | RNeg : Boolean; | |
4817 | -- Set if corresponding operand might be negative | |
4818 | ||
4819 | begin | |
5d5e9775 AC |
4820 | Determine_Range |
4821 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
4822 | LNeg := (not OK) or else Lo < 0; |
4823 | ||
5d5e9775 AC |
4824 | Determine_Range |
4825 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
4826 | RNeg := (not OK) or else Lo < 0; |
4827 | ||
5d5e9775 AC |
4828 | -- Check if we will be generating conditionals. There are two |
4829 | -- cases where that can happen, first for REM, the only case | |
4830 | -- is largest negative integer mod -1, where the division can | |
4831 | -- overflow, but we still have to give the right result. The | |
4832 | -- front end generates a test for this annoying case. Here we | |
4833 | -- just test if both operands can be negative (that's what the | |
4834 | -- expander does, so we match its logic here). | |
4835 | ||
4836 | -- The second case is mod where either operand can be negative. | |
4837 | -- In this case, the back end has to generate additonal tests. | |
4838 | ||
45fc7ddb HK |
4839 | if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg)) |
4840 | or else | |
4841 | (Nkind (N) = N_Op_Mod and then (LNeg or RNeg)) | |
4842 | then | |
4843 | Check_Restriction (No_Implicit_Conditionals, N); | |
4844 | end if; | |
4845 | end; | |
4846 | end if; | |
996ae0b0 RK |
4847 | end if; |
4848 | ||
4849 | Check_Unset_Reference (L); | |
4850 | Check_Unset_Reference (R); | |
996ae0b0 RK |
4851 | end Resolve_Arithmetic_Op; |
4852 | ||
4853 | ------------------ | |
4854 | -- Resolve_Call -- | |
4855 | ------------------ | |
4856 | ||
4857 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is | |
4858 | Loc : constant Source_Ptr := Sloc (N); | |
4859 | Subp : constant Node_Id := Name (N); | |
4860 | Nam : Entity_Id; | |
4861 | I : Interp_Index; | |
4862 | It : Interp; | |
4863 | Norm_OK : Boolean; | |
4864 | Scop : Entity_Id; | |
aa180613 | 4865 | Rtype : Entity_Id; |
996ae0b0 | 4866 | |
ee81cbe9 AC |
4867 | function Same_Or_Aliased_Subprograms |
4868 | (S : Entity_Id; | |
4869 | E : Entity_Id) return Boolean; | |
4870 | -- Returns True if the subprogram entity S is the same as E or else | |
4871 | -- S is an alias of E. | |
4872 | ||
001c7783 AC |
4873 | --------------------------------- |
4874 | -- Same_Or_Aliased_Subprograms -- | |
4875 | --------------------------------- | |
4876 | ||
ee81cbe9 AC |
4877 | function Same_Or_Aliased_Subprograms |
4878 | (S : Entity_Id; | |
4879 | E : Entity_Id) return Boolean | |
4880 | is | |
4881 | Subp_Alias : constant Entity_Id := Alias (S); | |
ee81cbe9 AC |
4882 | begin |
4883 | return S = E | |
4884 | or else (Present (Subp_Alias) and then Subp_Alias = E); | |
4885 | end Same_Or_Aliased_Subprograms; | |
4886 | ||
4887 | -- Start of processing for Resolve_Call | |
4888 | ||
996ae0b0 | 4889 | begin |
758c442c GD |
4890 | -- The context imposes a unique interpretation with type Typ on a |
4891 | -- procedure or function call. Find the entity of the subprogram that | |
4892 | -- yields the expected type, and propagate the corresponding formal | |
4893 | -- constraints on the actuals. The caller has established that an | |
4894 | -- interpretation exists, and emitted an error if not unique. | |
996ae0b0 RK |
4895 | |
4896 | -- First deal with the case of a call to an access-to-subprogram, | |
4897 | -- dereference made explicit in Analyze_Call. | |
4898 | ||
4899 | if Ekind (Etype (Subp)) = E_Subprogram_Type then | |
996ae0b0 RK |
4900 | if not Is_Overloaded (Subp) then |
4901 | Nam := Etype (Subp); | |
4902 | ||
4903 | else | |
758c442c GD |
4904 | -- Find the interpretation whose type (a subprogram type) has a |
4905 | -- return type that is compatible with the context. Analysis of | |
4906 | -- the node has established that one exists. | |
996ae0b0 | 4907 | |
996ae0b0 RK |
4908 | Nam := Empty; |
4909 | ||
1420b484 | 4910 | Get_First_Interp (Subp, I, It); |
996ae0b0 | 4911 | while Present (It.Typ) loop |
996ae0b0 RK |
4912 | if Covers (Typ, Etype (It.Typ)) then |
4913 | Nam := It.Typ; | |
4914 | exit; | |
4915 | end if; | |
4916 | ||
4917 | Get_Next_Interp (I, It); | |
4918 | end loop; | |
4919 | ||
4920 | if No (Nam) then | |
4921 | raise Program_Error; | |
4922 | end if; | |
4923 | end if; | |
4924 | ||
4925 | -- If the prefix is not an entity, then resolve it | |
4926 | ||
4927 | if not Is_Entity_Name (Subp) then | |
4928 | Resolve (Subp, Nam); | |
4929 | end if; | |
4930 | ||
758c442c GD |
4931 | -- For an indirect call, we always invalidate checks, since we do not |
4932 | -- know whether the subprogram is local or global. Yes we could do | |
4933 | -- better here, e.g. by knowing that there are no local subprograms, | |
aa180613 | 4934 | -- but it does not seem worth the effort. Similarly, we kill all |
758c442c | 4935 | -- knowledge of current constant values. |
fbf5a39b AC |
4936 | |
4937 | Kill_Current_Values; | |
4938 | ||
b7d1f17f HK |
4939 | -- If this is a procedure call which is really an entry call, do |
4940 | -- the conversion of the procedure call to an entry call. Protected | |
4941 | -- operations use the same circuitry because the name in the call | |
4942 | -- can be an arbitrary expression with special resolution rules. | |
996ae0b0 | 4943 | |
45fc7ddb | 4944 | elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component) |
996ae0b0 RK |
4945 | or else (Is_Entity_Name (Subp) |
4946 | and then Ekind (Entity (Subp)) = E_Entry) | |
4947 | then | |
4948 | Resolve_Entry_Call (N, Typ); | |
4949 | Check_Elab_Call (N); | |
fbf5a39b AC |
4950 | |
4951 | -- Kill checks and constant values, as above for indirect case | |
4952 | -- Who knows what happens when another task is activated? | |
4953 | ||
4954 | Kill_Current_Values; | |
996ae0b0 RK |
4955 | return; |
4956 | ||
4957 | -- Normal subprogram call with name established in Resolve | |
4958 | ||
4959 | elsif not (Is_Type (Entity (Subp))) then | |
4960 | Nam := Entity (Subp); | |
4961 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
4962 | |
4963 | -- Otherwise we must have the case of an overloaded call | |
4964 | ||
4965 | else | |
4966 | pragma Assert (Is_Overloaded (Subp)); | |
d81b4bfe TQ |
4967 | |
4968 | -- Initialize Nam to prevent warning (we know it will be assigned | |
4969 | -- in the loop below, but the compiler does not know that). | |
4970 | ||
4971 | Nam := Empty; | |
996ae0b0 RK |
4972 | |
4973 | Get_First_Interp (Subp, I, It); | |
996ae0b0 RK |
4974 | while Present (It.Typ) loop |
4975 | if Covers (Typ, It.Typ) then | |
4976 | Nam := It.Nam; | |
4977 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
4978 | exit; |
4979 | end if; | |
4980 | ||
4981 | Get_Next_Interp (I, It); | |
4982 | end loop; | |
4983 | end if; | |
4984 | ||
c9b99571 ES |
4985 | if Is_Access_Subprogram_Type (Base_Type (Etype (Nam))) |
4986 | and then not Is_Access_Subprogram_Type (Base_Type (Typ)) | |
53cf4600 ES |
4987 | and then Nkind (Subp) /= N_Explicit_Dereference |
4988 | and then Present (Parameter_Associations (N)) | |
4989 | then | |
66aa7643 TQ |
4990 | -- The prefix is a parameterless function call that returns an access |
4991 | -- to subprogram. If parameters are present in the current call, add | |
4992 | -- add an explicit dereference. We use the base type here because | |
4993 | -- within an instance these may be subtypes. | |
53cf4600 ES |
4994 | |
4995 | -- The dereference is added either in Analyze_Call or here. Should | |
4996 | -- be consolidated ??? | |
4997 | ||
4998 | Set_Is_Overloaded (Subp, False); | |
4999 | Set_Etype (Subp, Etype (Nam)); | |
5000 | Insert_Explicit_Dereference (Subp); | |
5001 | Nam := Designated_Type (Etype (Nam)); | |
5002 | Resolve (Subp, Nam); | |
5003 | end if; | |
5004 | ||
996ae0b0 RK |
5005 | -- Check that a call to Current_Task does not occur in an entry body |
5006 | ||
5007 | if Is_RTE (Nam, RE_Current_Task) then | |
5008 | declare | |
5009 | P : Node_Id; | |
5010 | ||
5011 | begin | |
5012 | P := N; | |
5013 | loop | |
5014 | P := Parent (P); | |
45fc7ddb HK |
5015 | |
5016 | -- Exclude calls that occur within the default of a formal | |
5017 | -- parameter of the entry, since those are evaluated outside | |
5018 | -- of the body. | |
5019 | ||
5020 | exit when No (P) or else Nkind (P) = N_Parameter_Specification; | |
996ae0b0 | 5021 | |
aa180613 RD |
5022 | if Nkind (P) = N_Entry_Body |
5023 | or else (Nkind (P) = N_Subprogram_Body | |
45fc7ddb | 5024 | and then Is_Entry_Barrier_Function (P)) |
aa180613 RD |
5025 | then |
5026 | Rtype := Etype (N); | |
996ae0b0 | 5027 | Error_Msg_NE |
aa5147f0 | 5028 | ("?& should not be used in entry body (RM C.7(17))", |
996ae0b0 | 5029 | N, Nam); |
aa180613 RD |
5030 | Error_Msg_NE |
5031 | ("\Program_Error will be raised at run time?", N, Nam); | |
5032 | Rewrite (N, | |
5033 | Make_Raise_Program_Error (Loc, | |
5034 | Reason => PE_Current_Task_In_Entry_Body)); | |
5035 | Set_Etype (N, Rtype); | |
e65f50ec | 5036 | return; |
996ae0b0 RK |
5037 | end if; |
5038 | end loop; | |
5039 | end; | |
5040 | end if; | |
5041 | ||
758c442c GD |
5042 | -- Check that a procedure call does not occur in the context of the |
5043 | -- entry call statement of a conditional or timed entry call. Note that | |
5044 | -- the case of a call to a subprogram renaming of an entry will also be | |
5045 | -- rejected. The test for N not being an N_Entry_Call_Statement is | |
5046 | -- defensive, covering the possibility that the processing of entry | |
5047 | -- calls might reach this point due to later modifications of the code | |
5048 | -- above. | |
996ae0b0 RK |
5049 | |
5050 | if Nkind (Parent (N)) = N_Entry_Call_Alternative | |
5051 | and then Nkind (N) /= N_Entry_Call_Statement | |
5052 | and then Entry_Call_Statement (Parent (N)) = N | |
5053 | then | |
1420b484 JM |
5054 | if Ada_Version < Ada_05 then |
5055 | Error_Msg_N ("entry call required in select statement", N); | |
5056 | ||
5057 | -- Ada 2005 (AI-345): If a procedure_call_statement is used | |
66aa7643 TQ |
5058 | -- for a procedure_or_entry_call, the procedure_name or |
5059 | -- procedure_prefix of the procedure_call_statement shall denote | |
1420b484 JM |
5060 | -- an entry renamed by a procedure, or (a view of) a primitive |
5061 | -- subprogram of a limited interface whose first parameter is | |
5062 | -- a controlling parameter. | |
5063 | ||
5064 | elsif Nkind (N) = N_Procedure_Call_Statement | |
5065 | and then not Is_Renamed_Entry (Nam) | |
5066 | and then not Is_Controlling_Limited_Procedure (Nam) | |
5067 | then | |
5068 | Error_Msg_N | |
c8ef728f | 5069 | ("entry call or dispatching primitive of interface required", N); |
1420b484 | 5070 | end if; |
996ae0b0 RK |
5071 | end if; |
5072 | ||
66aa7643 TQ |
5073 | -- Check that this is not a call to a protected procedure or entry from |
5074 | -- within a protected function. | |
fbf5a39b AC |
5075 | |
5076 | if Ekind (Current_Scope) = E_Function | |
5077 | and then Ekind (Scope (Current_Scope)) = E_Protected_Type | |
5078 | and then Ekind (Nam) /= E_Function | |
5079 | and then Scope (Nam) = Scope (Current_Scope) | |
5080 | then | |
5081 | Error_Msg_N ("within protected function, protected " & | |
5082 | "object is constant", N); | |
5083 | Error_Msg_N ("\cannot call operation that may modify it", N); | |
5084 | end if; | |
5085 | ||
45fc7ddb | 5086 | -- Freeze the subprogram name if not in a spec-expression. Note that we |
758c442c GD |
5087 | -- freeze procedure calls as well as function calls. Procedure calls are |
5088 | -- not frozen according to the rules (RM 13.14(14)) because it is | |
5089 | -- impossible to have a procedure call to a non-frozen procedure in pure | |
5090 | -- Ada, but in the code that we generate in the expander, this rule | |
5091 | -- needs extending because we can generate procedure calls that need | |
5092 | -- freezing. | |
996ae0b0 | 5093 | |
45fc7ddb | 5094 | if Is_Entity_Name (Subp) and then not In_Spec_Expression then |
996ae0b0 RK |
5095 | Freeze_Expression (Subp); |
5096 | end if; | |
5097 | ||
758c442c GD |
5098 | -- For a predefined operator, the type of the result is the type imposed |
5099 | -- by context, except for a predefined operation on universal fixed. | |
5100 | -- Otherwise The type of the call is the type returned by the subprogram | |
5101 | -- being called. | |
996ae0b0 RK |
5102 | |
5103 | if Is_Predefined_Op (Nam) then | |
996ae0b0 RK |
5104 | if Etype (N) /= Universal_Fixed then |
5105 | Set_Etype (N, Typ); | |
5106 | end if; | |
5107 | ||
758c442c GD |
5108 | -- If the subprogram returns an array type, and the context requires the |
5109 | -- component type of that array type, the node is really an indexing of | |
5110 | -- the parameterless call. Resolve as such. A pathological case occurs | |
5111 | -- when the type of the component is an access to the array type. In | |
5112 | -- this case the call is truly ambiguous. | |
996ae0b0 | 5113 | |
0669bebe | 5114 | elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam)) |
996ae0b0 RK |
5115 | and then |
5116 | ((Is_Array_Type (Etype (Nam)) | |
5117 | and then Covers (Typ, Component_Type (Etype (Nam)))) | |
5118 | or else (Is_Access_Type (Etype (Nam)) | |
5119 | and then Is_Array_Type (Designated_Type (Etype (Nam))) | |
5120 | and then | |
5121 | Covers (Typ, | |
5122 | Component_Type (Designated_Type (Etype (Nam)))))) | |
5123 | then | |
5124 | declare | |
5125 | Index_Node : Node_Id; | |
fbf5a39b AC |
5126 | New_Subp : Node_Id; |
5127 | Ret_Type : constant Entity_Id := Etype (Nam); | |
996ae0b0 RK |
5128 | |
5129 | begin | |
fbf5a39b AC |
5130 | if Is_Access_Type (Ret_Type) |
5131 | and then Ret_Type = Component_Type (Designated_Type (Ret_Type)) | |
5132 | then | |
5133 | Error_Msg_N | |
5134 | ("cannot disambiguate function call and indexing", N); | |
5135 | else | |
5136 | New_Subp := Relocate_Node (Subp); | |
5137 | Set_Entity (Subp, Nam); | |
5138 | ||
7205254b | 5139 | if (Is_Array_Type (Ret_Type) |
5d5e9775 | 5140 | and then Component_Type (Ret_Type) /= Any_Type) |
7205254b JM |
5141 | or else |
5142 | (Is_Access_Type (Ret_Type) | |
5d5e9775 AC |
5143 | and then |
5144 | Component_Type (Designated_Type (Ret_Type)) /= Any_Type) | |
7205254b | 5145 | then |
0669bebe GB |
5146 | if Needs_No_Actuals (Nam) then |
5147 | ||
5148 | -- Indexed call to a parameterless function | |
5149 | ||
5150 | Index_Node := | |
5151 | Make_Indexed_Component (Loc, | |
5152 | Prefix => | |
5153 | Make_Function_Call (Loc, | |
5154 | Name => New_Subp), | |
5155 | Expressions => Parameter_Associations (N)); | |
5156 | else | |
5157 | -- An Ada 2005 prefixed call to a primitive operation | |
5158 | -- whose first parameter is the prefix. This prefix was | |
5159 | -- prepended to the parameter list, which is actually a | |
5160 | -- list of indices. Remove the prefix in order to build | |
5161 | -- the proper indexed component. | |
5162 | ||
5163 | Index_Node := | |
5164 | Make_Indexed_Component (Loc, | |
5165 | Prefix => | |
5166 | Make_Function_Call (Loc, | |
5167 | Name => New_Subp, | |
5168 | Parameter_Associations => | |
5169 | New_List | |
5170 | (Remove_Head (Parameter_Associations (N)))), | |
5171 | Expressions => Parameter_Associations (N)); | |
5172 | end if; | |
fbf5a39b | 5173 | |
74e7891f RD |
5174 | -- Preserve the parenthesis count of the node |
5175 | ||
5176 | Set_Paren_Count (Index_Node, Paren_Count (N)); | |
5177 | ||
fbf5a39b AC |
5178 | -- Since we are correcting a node classification error made |
5179 | -- by the parser, we call Replace rather than Rewrite. | |
5180 | ||
5181 | Replace (N, Index_Node); | |
74e7891f | 5182 | |
fbf5a39b AC |
5183 | Set_Etype (Prefix (N), Ret_Type); |
5184 | Set_Etype (N, Typ); | |
5185 | Resolve_Indexed_Component (N, Typ); | |
5186 | Check_Elab_Call (Prefix (N)); | |
5187 | end if; | |
996ae0b0 RK |
5188 | end if; |
5189 | ||
5190 | return; | |
5191 | end; | |
5192 | ||
5193 | else | |
5194 | Set_Etype (N, Etype (Nam)); | |
5195 | end if; | |
5196 | ||
5197 | -- In the case where the call is to an overloaded subprogram, Analyze | |
5198 | -- calls Normalize_Actuals once per overloaded subprogram. Therefore in | |
5199 | -- such a case Normalize_Actuals needs to be called once more to order | |
5200 | -- the actuals correctly. Otherwise the call will have the ordering | |
5201 | -- given by the last overloaded subprogram whether this is the correct | |
5202 | -- one being called or not. | |
5203 | ||
5204 | if Is_Overloaded (Subp) then | |
5205 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
5206 | pragma Assert (Norm_OK); | |
5207 | end if; | |
5208 | ||
5209 | -- In any case, call is fully resolved now. Reset Overload flag, to | |
5210 | -- prevent subsequent overload resolution if node is analyzed again | |
5211 | ||
5212 | Set_Is_Overloaded (Subp, False); | |
5213 | Set_Is_Overloaded (N, False); | |
5214 | ||
758c442c GD |
5215 | -- If we are calling the current subprogram from immediately within its |
5216 | -- body, then that is the case where we can sometimes detect cases of | |
5217 | -- infinite recursion statically. Do not try this in case restriction | |
b7d1f17f | 5218 | -- No_Recursion is in effect anyway, and do it only for source calls. |
996ae0b0 | 5219 | |
b7d1f17f HK |
5220 | if Comes_From_Source (N) then |
5221 | Scop := Current_Scope; | |
996ae0b0 | 5222 | |
26570b21 RD |
5223 | -- Issue warning for possible infinite recursion in the absence |
5224 | -- of the No_Recursion restriction. | |
5225 | ||
ee81cbe9 | 5226 | if Same_Or_Aliased_Subprograms (Nam, Scop) |
b7d1f17f HK |
5227 | and then not Restriction_Active (No_Recursion) |
5228 | and then Check_Infinite_Recursion (N) | |
5229 | then | |
5230 | -- Here we detected and flagged an infinite recursion, so we do | |
26570b21 RD |
5231 | -- not need to test the case below for further warnings. Also if |
5232 | -- we now have a raise SE node, we are all done. | |
996ae0b0 | 5233 | |
26570b21 RD |
5234 | if Nkind (N) = N_Raise_Storage_Error then |
5235 | return; | |
5236 | end if; | |
996ae0b0 | 5237 | |
26570b21 RD |
5238 | -- If call is to immediately containing subprogram, then check for |
5239 | -- the case of a possible run-time detectable infinite recursion. | |
996ae0b0 | 5240 | |
b7d1f17f HK |
5241 | else |
5242 | Scope_Loop : while Scop /= Standard_Standard loop | |
ee81cbe9 | 5243 | if Same_Or_Aliased_Subprograms (Nam, Scop) then |
b7d1f17f HK |
5244 | |
5245 | -- Although in general case, recursion is not statically | |
5246 | -- checkable, the case of calling an immediately containing | |
5247 | -- subprogram is easy to catch. | |
5248 | ||
5249 | Check_Restriction (No_Recursion, N); | |
5250 | ||
5251 | -- If the recursive call is to a parameterless subprogram, | |
5252 | -- then even if we can't statically detect infinite | |
5253 | -- recursion, this is pretty suspicious, and we output a | |
5254 | -- warning. Furthermore, we will try later to detect some | |
5255 | -- cases here at run time by expanding checking code (see | |
5256 | -- Detect_Infinite_Recursion in package Exp_Ch6). | |
5257 | ||
5258 | -- If the recursive call is within a handler, do not emit a | |
5259 | -- warning, because this is a common idiom: loop until input | |
5260 | -- is correct, catch illegal input in handler and restart. | |
5261 | ||
5262 | if No (First_Formal (Nam)) | |
5263 | and then Etype (Nam) = Standard_Void_Type | |
5264 | and then not Error_Posted (N) | |
5265 | and then Nkind (Parent (N)) /= N_Exception_Handler | |
aa180613 | 5266 | then |
b7d1f17f HK |
5267 | -- For the case of a procedure call. We give the message |
5268 | -- only if the call is the first statement in a sequence | |
5269 | -- of statements, or if all previous statements are | |
5270 | -- simple assignments. This is simply a heuristic to | |
5271 | -- decrease false positives, without losing too many good | |
5272 | -- warnings. The idea is that these previous statements | |
5273 | -- may affect global variables the procedure depends on. | |
5274 | ||
5275 | if Nkind (N) = N_Procedure_Call_Statement | |
5276 | and then Is_List_Member (N) | |
5277 | then | |
5278 | declare | |
5279 | P : Node_Id; | |
5280 | begin | |
5281 | P := Prev (N); | |
5282 | while Present (P) loop | |
5283 | if Nkind (P) /= N_Assignment_Statement then | |
5284 | exit Scope_Loop; | |
5285 | end if; | |
5286 | ||
5287 | Prev (P); | |
5288 | end loop; | |
5289 | end; | |
5290 | end if; | |
5291 | ||
5292 | -- Do not give warning if we are in a conditional context | |
5293 | ||
aa180613 | 5294 | declare |
b7d1f17f | 5295 | K : constant Node_Kind := Nkind (Parent (N)); |
aa180613 | 5296 | begin |
b7d1f17f | 5297 | if (K = N_Loop_Statement |
b5c739f9 | 5298 | and then Present (Iteration_Scheme (Parent (N)))) |
b7d1f17f HK |
5299 | or else K = N_If_Statement |
5300 | or else K = N_Elsif_Part | |
5301 | or else K = N_Case_Statement_Alternative | |
5302 | then | |
5303 | exit Scope_Loop; | |
5304 | end if; | |
aa180613 | 5305 | end; |
aa180613 | 5306 | |
b7d1f17f | 5307 | -- Here warning is to be issued |
aa180613 | 5308 | |
b7d1f17f HK |
5309 | Set_Has_Recursive_Call (Nam); |
5310 | Error_Msg_N | |
aa5147f0 | 5311 | ("?possible infinite recursion!", N); |
b7d1f17f | 5312 | Error_Msg_N |
aa5147f0 | 5313 | ("\?Storage_Error may be raised at run time!", N); |
b7d1f17f | 5314 | end if; |
aa180613 | 5315 | |
b7d1f17f | 5316 | exit Scope_Loop; |
996ae0b0 RK |
5317 | end if; |
5318 | ||
b7d1f17f HK |
5319 | Scop := Scope (Scop); |
5320 | end loop Scope_Loop; | |
5321 | end if; | |
996ae0b0 RK |
5322 | end if; |
5323 | ||
b5c739f9 RD |
5324 | -- Check obsolescent reference to Ada.Characters.Handling subprogram |
5325 | ||
5326 | Check_Obsolescent_2005_Entity (Nam, Subp); | |
5327 | ||
996ae0b0 RK |
5328 | -- If subprogram name is a predefined operator, it was given in |
5329 | -- functional notation. Replace call node with operator node, so | |
5330 | -- that actuals can be resolved appropriately. | |
5331 | ||
5332 | if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then | |
5333 | Make_Call_Into_Operator (N, Typ, Entity (Name (N))); | |
5334 | return; | |
5335 | ||
5336 | elsif Present (Alias (Nam)) | |
5337 | and then Is_Predefined_Op (Alias (Nam)) | |
5338 | then | |
5339 | Resolve_Actuals (N, Nam); | |
5340 | Make_Call_Into_Operator (N, Typ, Alias (Nam)); | |
5341 | return; | |
5342 | end if; | |
5343 | ||
fbf5a39b AC |
5344 | -- Create a transient scope if the resulting type requires it |
5345 | ||
4017021b AC |
5346 | -- There are several notable exceptions: |
5347 | ||
4d2907fd | 5348 | -- a) In init procs, the transient scope overhead is not needed, and is |
4017021b AC |
5349 | -- even incorrect when the call is a nested initialization call for a |
5350 | -- component whose expansion may generate adjust calls. However, if the | |
5351 | -- call is some other procedure call within an initialization procedure | |
5352 | -- (for example a call to Create_Task in the init_proc of the task | |
5353 | -- run-time record) a transient scope must be created around this call. | |
5354 | ||
4d2907fd | 5355 | -- b) Enumeration literal pseudo-calls need no transient scope |
4017021b | 5356 | |
4d2907fd | 5357 | -- c) Intrinsic subprograms (Unchecked_Conversion and source info |
4017021b | 5358 | -- functions) do not use the secondary stack even though the return |
4d2907fd | 5359 | -- type may be unconstrained. |
4017021b | 5360 | |
4d2907fd | 5361 | -- d) Calls to a build-in-place function, since such functions may |
4017021b AC |
5362 | -- allocate their result directly in a target object, and cases where |
5363 | -- the result does get allocated in the secondary stack are checked for | |
5364 | -- within the specialized Exp_Ch6 procedures for expanding those | |
5365 | -- build-in-place calls. | |
5366 | ||
5367 | -- e) If the subprogram is marked Inline_Always, then even if it returns | |
c8ef728f | 5368 | -- an unconstrained type the call does not require use of the secondary |
45fc7ddb HK |
5369 | -- stack. However, inlining will only take place if the body to inline |
5370 | -- is already present. It may not be available if e.g. the subprogram is | |
5371 | -- declared in a child instance. | |
c8ef728f | 5372 | |
4017021b AC |
5373 | -- If this is an initialization call for a type whose construction |
5374 | -- uses the secondary stack, and it is not a nested call to initialize | |
5375 | -- a component, we do need to create a transient scope for it. We | |
5376 | -- check for this by traversing the type in Check_Initialization_Call. | |
5377 | ||
c8ef728f | 5378 | if Is_Inlined (Nam) |
45fc7ddb HK |
5379 | and then Has_Pragma_Inline_Always (Nam) |
5380 | and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration | |
5381 | and then Present (Body_To_Inline (Unit_Declaration_Node (Nam))) | |
c8ef728f ES |
5382 | then |
5383 | null; | |
5384 | ||
4017021b AC |
5385 | elsif Ekind (Nam) = E_Enumeration_Literal |
5386 | or else Is_Build_In_Place_Function (Nam) | |
5387 | or else Is_Intrinsic_Subprogram (Nam) | |
5388 | then | |
5389 | null; | |
5390 | ||
c8ef728f | 5391 | elsif Expander_Active |
996ae0b0 RK |
5392 | and then Is_Type (Etype (Nam)) |
5393 | and then Requires_Transient_Scope (Etype (Nam)) | |
4017021b AC |
5394 | and then |
5395 | (not Within_Init_Proc | |
5396 | or else | |
5397 | (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function)) | |
996ae0b0 | 5398 | then |
0669bebe | 5399 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 5400 | |
a9f4e3d2 AC |
5401 | -- If the call appears within the bounds of a loop, it will |
5402 | -- be rewritten and reanalyzed, nothing left to do here. | |
5403 | ||
5404 | if Nkind (N) /= N_Function_Call then | |
5405 | return; | |
5406 | end if; | |
5407 | ||
fbf5a39b | 5408 | elsif Is_Init_Proc (Nam) |
996ae0b0 RK |
5409 | and then not Within_Init_Proc |
5410 | then | |
5411 | Check_Initialization_Call (N, Nam); | |
5412 | end if; | |
5413 | ||
5414 | -- A protected function cannot be called within the definition of the | |
5415 | -- enclosing protected type. | |
5416 | ||
5417 | if Is_Protected_Type (Scope (Nam)) | |
5418 | and then In_Open_Scopes (Scope (Nam)) | |
5419 | and then not Has_Completion (Scope (Nam)) | |
5420 | then | |
5421 | Error_Msg_NE | |
5422 | ("& cannot be called before end of protected definition", N, Nam); | |
5423 | end if; | |
5424 | ||
5425 | -- Propagate interpretation to actuals, and add default expressions | |
5426 | -- where needed. | |
5427 | ||
5428 | if Present (First_Formal (Nam)) then | |
5429 | Resolve_Actuals (N, Nam); | |
5430 | ||
d81b4bfe TQ |
5431 | -- Overloaded literals are rewritten as function calls, for purpose of |
5432 | -- resolution. After resolution, we can replace the call with the | |
5433 | -- literal itself. | |
996ae0b0 RK |
5434 | |
5435 | elsif Ekind (Nam) = E_Enumeration_Literal then | |
5436 | Copy_Node (Subp, N); | |
5437 | Resolve_Entity_Name (N, Typ); | |
5438 | ||
fbf5a39b | 5439 | -- Avoid validation, since it is a static function call |
996ae0b0 | 5440 | |
e65f50ec | 5441 | Generate_Reference (Nam, Subp); |
996ae0b0 RK |
5442 | return; |
5443 | end if; | |
5444 | ||
b7d1f17f HK |
5445 | -- If the subprogram is not global, then kill all saved values and |
5446 | -- checks. This is a bit conservative, since in many cases we could do | |
5447 | -- better, but it is not worth the effort. Similarly, we kill constant | |
5448 | -- values. However we do not need to do this for internal entities | |
5449 | -- (unless they are inherited user-defined subprograms), since they | |
5450 | -- are not in the business of molesting local values. | |
5451 | ||
5452 | -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also | |
5453 | -- kill all checks and values for calls to global subprograms. This | |
5454 | -- takes care of the case where an access to a local subprogram is | |
5455 | -- taken, and could be passed directly or indirectly and then called | |
5456 | -- from almost any context. | |
aa180613 RD |
5457 | |
5458 | -- Note: we do not do this step till after resolving the actuals. That | |
5459 | -- way we still take advantage of the current value information while | |
5460 | -- scanning the actuals. | |
5461 | ||
45fc7ddb HK |
5462 | -- We suppress killing values if we are processing the nodes associated |
5463 | -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged | |
5464 | -- type kills all the values as part of analyzing the code that | |
5465 | -- initializes the dispatch tables. | |
5466 | ||
5467 | if Inside_Freezing_Actions = 0 | |
5468 | and then (not Is_Library_Level_Entity (Nam) | |
24357840 RD |
5469 | or else Suppress_Value_Tracking_On_Call |
5470 | (Nearest_Dynamic_Scope (Current_Scope))) | |
aa180613 RD |
5471 | and then (Comes_From_Source (Nam) |
5472 | or else (Present (Alias (Nam)) | |
5473 | and then Comes_From_Source (Alias (Nam)))) | |
5474 | then | |
5475 | Kill_Current_Values; | |
5476 | end if; | |
5477 | ||
36fcf362 RD |
5478 | -- If we are warning about unread OUT parameters, this is the place to |
5479 | -- set Last_Assignment for OUT and IN OUT parameters. We have to do this | |
5480 | -- after the above call to Kill_Current_Values (since that call clears | |
5481 | -- the Last_Assignment field of all local variables). | |
67ce0d7e | 5482 | |
36fcf362 | 5483 | if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters) |
67ce0d7e RD |
5484 | and then Comes_From_Source (N) |
5485 | and then In_Extended_Main_Source_Unit (N) | |
5486 | then | |
5487 | declare | |
5488 | F : Entity_Id; | |
5489 | A : Node_Id; | |
5490 | ||
5491 | begin | |
5492 | F := First_Formal (Nam); | |
5493 | A := First_Actual (N); | |
5494 | while Present (F) and then Present (A) loop | |
964f13da | 5495 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) |
36fcf362 | 5496 | and then Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
5497 | and then Is_Entity_Name (A) |
5498 | and then Present (Entity (A)) | |
36fcf362 | 5499 | and then Comes_From_Source (N) |
67ce0d7e RD |
5500 | and then Safe_To_Capture_Value (N, Entity (A)) |
5501 | then | |
5502 | Set_Last_Assignment (Entity (A), A); | |
5503 | end if; | |
5504 | ||
5505 | Next_Formal (F); | |
5506 | Next_Actual (A); | |
5507 | end loop; | |
5508 | end; | |
5509 | end if; | |
5510 | ||
996ae0b0 RK |
5511 | -- If the subprogram is a primitive operation, check whether or not |
5512 | -- it is a correct dispatching call. | |
5513 | ||
5514 | if Is_Overloadable (Nam) | |
5515 | and then Is_Dispatching_Operation (Nam) | |
5516 | then | |
5517 | Check_Dispatching_Call (N); | |
5518 | ||
0669bebe GB |
5519 | elsif Ekind (Nam) /= E_Subprogram_Type |
5520 | and then Is_Abstract_Subprogram (Nam) | |
996ae0b0 RK |
5521 | and then not In_Instance |
5522 | then | |
5523 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam); | |
5524 | end if; | |
5525 | ||
e65f50ec ES |
5526 | -- If this is a dispatching call, generate the appropriate reference, |
5527 | -- for better source navigation in GPS. | |
5528 | ||
5529 | if Is_Overloadable (Nam) | |
5530 | and then Present (Controlling_Argument (N)) | |
5531 | then | |
5532 | Generate_Reference (Nam, Subp, 'R'); | |
c5d91669 AC |
5533 | |
5534 | -- Normal case, not a dispatching call | |
5535 | ||
e65f50ec ES |
5536 | else |
5537 | Generate_Reference (Nam, Subp); | |
5538 | end if; | |
5539 | ||
996ae0b0 RK |
5540 | if Is_Intrinsic_Subprogram (Nam) then |
5541 | Check_Intrinsic_Call (N); | |
5542 | end if; | |
5543 | ||
5b2217f8 | 5544 | -- Check for violation of restriction No_Specific_Termination_Handlers |
dce86910 | 5545 | -- and warn on a potentially blocking call to Abort_Task. |
5b2217f8 RD |
5546 | |
5547 | if Is_RTE (Nam, RE_Set_Specific_Handler) | |
5548 | or else | |
5549 | Is_RTE (Nam, RE_Specific_Handler) | |
5550 | then | |
5551 | Check_Restriction (No_Specific_Termination_Handlers, N); | |
dce86910 AC |
5552 | |
5553 | elsif Is_RTE (Nam, RE_Abort_Task) then | |
5554 | Check_Potentially_Blocking_Operation (N); | |
5b2217f8 RD |
5555 | end if; |
5556 | ||
9cbfc269 AC |
5557 | -- Issue an error for a call to an eliminated subprogram. We skip this |
5558 | -- in a spec expression, e.g. a call in a default parameter value, since | |
5559 | -- we are not really doing a call at this time. That's important because | |
5560 | -- the spec expression may itself belong to an eliminated subprogram. | |
16212e89 | 5561 | |
9cbfc269 AC |
5562 | if not In_Spec_Expression then |
5563 | Check_For_Eliminated_Subprogram (Subp, Nam); | |
5564 | end if; | |
16212e89 | 5565 | |
67ce0d7e RD |
5566 | -- All done, evaluate call and deal with elaboration issues |
5567 | ||
c01a9391 | 5568 | Eval_Call (N); |
996ae0b0 | 5569 | Check_Elab_Call (N); |
76b84bf0 | 5570 | Warn_On_Overlapping_Actuals (Nam, N); |
996ae0b0 RK |
5571 | end Resolve_Call; |
5572 | ||
19d846a0 RD |
5573 | ----------------------------- |
5574 | -- Resolve_Case_Expression -- | |
5575 | ----------------------------- | |
5576 | ||
5577 | procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is | |
5578 | Alt : Node_Id; | |
5579 | ||
5580 | begin | |
5581 | Alt := First (Alternatives (N)); | |
5582 | while Present (Alt) loop | |
5583 | Resolve (Expression (Alt), Typ); | |
5584 | Next (Alt); | |
5585 | end loop; | |
5586 | ||
5587 | Set_Etype (N, Typ); | |
5588 | Eval_Case_Expression (N); | |
5589 | end Resolve_Case_Expression; | |
5590 | ||
996ae0b0 RK |
5591 | ------------------------------- |
5592 | -- Resolve_Character_Literal -- | |
5593 | ------------------------------- | |
5594 | ||
5595 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is | |
5596 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
5597 | C : Entity_Id; | |
5598 | ||
5599 | begin | |
5600 | -- Verify that the character does belong to the type of the context | |
5601 | ||
5602 | Set_Etype (N, B_Typ); | |
5603 | Eval_Character_Literal (N); | |
5604 | ||
82c80734 RD |
5605 | -- Wide_Wide_Character literals must always be defined, since the set |
5606 | -- of wide wide character literals is complete, i.e. if a character | |
5607 | -- literal is accepted by the parser, then it is OK for wide wide | |
5608 | -- character (out of range character literals are rejected). | |
996ae0b0 | 5609 | |
82c80734 | 5610 | if Root_Type (B_Typ) = Standard_Wide_Wide_Character then |
996ae0b0 RK |
5611 | return; |
5612 | ||
5613 | -- Always accept character literal for type Any_Character, which | |
5614 | -- occurs in error situations and in comparisons of literals, both | |
5615 | -- of which should accept all literals. | |
5616 | ||
5617 | elsif B_Typ = Any_Character then | |
5618 | return; | |
5619 | ||
5620 | -- For Standard.Character or a type derived from it, check that | |
5621 | -- the literal is in range | |
5622 | ||
5623 | elsif Root_Type (B_Typ) = Standard_Character then | |
82c80734 RD |
5624 | if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then |
5625 | return; | |
5626 | end if; | |
5627 | ||
5628 | -- For Standard.Wide_Character or a type derived from it, check | |
5629 | -- that the literal is in range | |
5630 | ||
5631 | elsif Root_Type (B_Typ) = Standard_Wide_Character then | |
5632 | if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then | |
996ae0b0 RK |
5633 | return; |
5634 | end if; | |
5635 | ||
82c80734 RD |
5636 | -- For Standard.Wide_Wide_Character or a type derived from it, we |
5637 | -- know the literal is in range, since the parser checked! | |
5638 | ||
5639 | elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then | |
5640 | return; | |
5641 | ||
d81b4bfe TQ |
5642 | -- If the entity is already set, this has already been resolved in a |
5643 | -- generic context, or comes from expansion. Nothing else to do. | |
996ae0b0 RK |
5644 | |
5645 | elsif Present (Entity (N)) then | |
5646 | return; | |
5647 | ||
d81b4bfe TQ |
5648 | -- Otherwise we have a user defined character type, and we can use the |
5649 | -- standard visibility mechanisms to locate the referenced entity. | |
996ae0b0 RK |
5650 | |
5651 | else | |
5652 | C := Current_Entity (N); | |
996ae0b0 RK |
5653 | while Present (C) loop |
5654 | if Etype (C) = B_Typ then | |
5655 | Set_Entity_With_Style_Check (N, C); | |
5656 | Generate_Reference (C, N); | |
5657 | return; | |
5658 | end if; | |
5659 | ||
5660 | C := Homonym (C); | |
5661 | end loop; | |
5662 | end if; | |
5663 | ||
5664 | -- If we fall through, then the literal does not match any of the | |
5665 | -- entries of the enumeration type. This isn't just a constraint | |
5666 | -- error situation, it is an illegality (see RM 4.2). | |
5667 | ||
5668 | Error_Msg_NE | |
5669 | ("character not defined for }", N, First_Subtype (B_Typ)); | |
996ae0b0 RK |
5670 | end Resolve_Character_Literal; |
5671 | ||
5672 | --------------------------- | |
5673 | -- Resolve_Comparison_Op -- | |
5674 | --------------------------- | |
5675 | ||
5676 | -- Context requires a boolean type, and plays no role in resolution. | |
fbf5a39b AC |
5677 | -- Processing identical to that for equality operators. The result |
5678 | -- type is the base type, which matters when pathological subtypes of | |
5679 | -- booleans with limited ranges are used. | |
996ae0b0 RK |
5680 | |
5681 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is | |
5682 | L : constant Node_Id := Left_Opnd (N); | |
5683 | R : constant Node_Id := Right_Opnd (N); | |
5684 | T : Entity_Id; | |
5685 | ||
5686 | begin | |
d81b4bfe TQ |
5687 | -- If this is an intrinsic operation which is not predefined, use the |
5688 | -- types of its declared arguments to resolve the possibly overloaded | |
5689 | -- operands. Otherwise the operands are unambiguous and specify the | |
5690 | -- expected type. | |
996ae0b0 RK |
5691 | |
5692 | if Scope (Entity (N)) /= Standard_Standard then | |
5693 | T := Etype (First_Entity (Entity (N))); | |
1420b484 | 5694 | |
996ae0b0 RK |
5695 | else |
5696 | T := Find_Unique_Type (L, R); | |
5697 | ||
5698 | if T = Any_Fixed then | |
5699 | T := Unique_Fixed_Point_Type (L); | |
5700 | end if; | |
5701 | end if; | |
5702 | ||
fbf5a39b | 5703 | Set_Etype (N, Base_Type (Typ)); |
996ae0b0 RK |
5704 | Generate_Reference (T, N, ' '); |
5705 | ||
bd29d519 | 5706 | -- Skip remaining processing if already set to Any_Type |
996ae0b0 | 5707 | |
bd29d519 AC |
5708 | if T = Any_Type then |
5709 | return; | |
5710 | end if; | |
5711 | ||
5712 | -- Deal with other error cases | |
996ae0b0 | 5713 | |
bd29d519 AC |
5714 | if T = Any_String or else |
5715 | T = Any_Composite or else | |
5716 | T = Any_Character | |
5717 | then | |
5718 | if T = Any_Character then | |
5719 | Ambiguous_Character (L); | |
996ae0b0 | 5720 | else |
bd29d519 | 5721 | Error_Msg_N ("ambiguous operands for comparison", N); |
996ae0b0 | 5722 | end if; |
bd29d519 AC |
5723 | |
5724 | Set_Etype (N, Any_Type); | |
5725 | return; | |
996ae0b0 | 5726 | end if; |
bd29d519 AC |
5727 | |
5728 | -- Resolve the operands if types OK | |
5729 | ||
5730 | Resolve (L, T); | |
5731 | Resolve (R, T); | |
5732 | Check_Unset_Reference (L); | |
5733 | Check_Unset_Reference (R); | |
5734 | Generate_Operator_Reference (N, T); | |
5735 | Check_Low_Bound_Tested (N); | |
5736 | ||
5737 | -- Check comparison on unordered enumeration | |
5738 | ||
5739 | if Comes_From_Source (N) | |
5740 | and then Bad_Unordered_Enumeration_Reference (N, Etype (L)) | |
5741 | then | |
5742 | Error_Msg_N ("comparison on unordered enumeration type?", N); | |
5743 | end if; | |
5744 | ||
5745 | -- Evaluate the relation (note we do this after the above check | |
5746 | -- since this Eval call may change N to True/False. | |
5747 | ||
5748 | Eval_Relational_Op (N); | |
996ae0b0 RK |
5749 | end Resolve_Comparison_Op; |
5750 | ||
5751 | ------------------------------------ | |
5752 | -- Resolve_Conditional_Expression -- | |
5753 | ------------------------------------ | |
5754 | ||
5755 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is | |
5756 | Condition : constant Node_Id := First (Expressions (N)); | |
5757 | Then_Expr : constant Node_Id := Next (Condition); | |
b46be8a2 RD |
5758 | Else_Expr : Node_Id := Next (Then_Expr); |
5759 | ||
996ae0b0 | 5760 | begin |
b46be8a2 | 5761 | Resolve (Condition, Any_Boolean); |
996ae0b0 | 5762 | Resolve (Then_Expr, Typ); |
b46be8a2 RD |
5763 | |
5764 | -- If ELSE expression present, just resolve using the determined type | |
5765 | ||
5766 | if Present (Else_Expr) then | |
5767 | Resolve (Else_Expr, Typ); | |
5768 | ||
5769 | -- If no ELSE expression is present, root type must be Standard.Boolean | |
5770 | -- and we provide a Standard.True result converted to the appropriate | |
5771 | -- Boolean type (in case it is a derived boolean type). | |
5772 | ||
5773 | elsif Root_Type (Typ) = Standard_Boolean then | |
5774 | Else_Expr := | |
5775 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N))); | |
5776 | Analyze_And_Resolve (Else_Expr, Typ); | |
5777 | Append_To (Expressions (N), Else_Expr); | |
5778 | ||
5779 | else | |
5780 | Error_Msg_N ("can only omit ELSE expression in Boolean case", N); | |
5781 | Append_To (Expressions (N), Error); | |
5782 | end if; | |
5783 | ||
996ae0b0 RK |
5784 | Set_Etype (N, Typ); |
5785 | Eval_Conditional_Expression (N); | |
5786 | end Resolve_Conditional_Expression; | |
5787 | ||
5788 | ----------------------------------------- | |
5789 | -- Resolve_Discrete_Subtype_Indication -- | |
5790 | ----------------------------------------- | |
5791 | ||
5792 | procedure Resolve_Discrete_Subtype_Indication | |
5793 | (N : Node_Id; | |
5794 | Typ : Entity_Id) | |
5795 | is | |
5796 | R : Node_Id; | |
5797 | S : Entity_Id; | |
5798 | ||
5799 | begin | |
5800 | Analyze (Subtype_Mark (N)); | |
5801 | S := Entity (Subtype_Mark (N)); | |
5802 | ||
5803 | if Nkind (Constraint (N)) /= N_Range_Constraint then | |
5804 | Error_Msg_N ("expect range constraint for discrete type", N); | |
5805 | Set_Etype (N, Any_Type); | |
5806 | ||
5807 | else | |
5808 | R := Range_Expression (Constraint (N)); | |
5c736541 RD |
5809 | |
5810 | if R = Error then | |
5811 | return; | |
5812 | end if; | |
5813 | ||
996ae0b0 RK |
5814 | Analyze (R); |
5815 | ||
5816 | if Base_Type (S) /= Base_Type (Typ) then | |
5817 | Error_Msg_NE | |
5818 | ("expect subtype of }", N, First_Subtype (Typ)); | |
5819 | ||
5820 | -- Rewrite the constraint as a range of Typ | |
5821 | -- to allow compilation to proceed further. | |
5822 | ||
5823 | Set_Etype (N, Typ); | |
5824 | Rewrite (Low_Bound (R), | |
5825 | Make_Attribute_Reference (Sloc (Low_Bound (R)), | |
5826 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), | |
5827 | Attribute_Name => Name_First)); | |
5828 | Rewrite (High_Bound (R), | |
5829 | Make_Attribute_Reference (Sloc (High_Bound (R)), | |
5830 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), | |
5831 | Attribute_Name => Name_First)); | |
5832 | ||
5833 | else | |
5834 | Resolve (R, Typ); | |
5835 | Set_Etype (N, Etype (R)); | |
5836 | ||
5837 | -- Additionally, we must check that the bounds are compatible | |
5838 | -- with the given subtype, which might be different from the | |
5839 | -- type of the context. | |
5840 | ||
5841 | Apply_Range_Check (R, S); | |
5842 | ||
5843 | -- ??? If the above check statically detects a Constraint_Error | |
5844 | -- it replaces the offending bound(s) of the range R with a | |
5845 | -- Constraint_Error node. When the itype which uses these bounds | |
5846 | -- is frozen the resulting call to Duplicate_Subexpr generates | |
5847 | -- a new temporary for the bounds. | |
5848 | ||
5849 | -- Unfortunately there are other itypes that are also made depend | |
5850 | -- on these bounds, so when Duplicate_Subexpr is called they get | |
5851 | -- a forward reference to the newly created temporaries and Gigi | |
5852 | -- aborts on such forward references. This is probably sign of a | |
5853 | -- more fundamental problem somewhere else in either the order of | |
5854 | -- itype freezing or the way certain itypes are constructed. | |
5855 | ||
5856 | -- To get around this problem we call Remove_Side_Effects right | |
5857 | -- away if either bounds of R are a Constraint_Error. | |
5858 | ||
5859 | declare | |
fbf5a39b AC |
5860 | L : constant Node_Id := Low_Bound (R); |
5861 | H : constant Node_Id := High_Bound (R); | |
996ae0b0 RK |
5862 | |
5863 | begin | |
5864 | if Nkind (L) = N_Raise_Constraint_Error then | |
5865 | Remove_Side_Effects (L); | |
5866 | end if; | |
5867 | ||
5868 | if Nkind (H) = N_Raise_Constraint_Error then | |
5869 | Remove_Side_Effects (H); | |
5870 | end if; | |
5871 | end; | |
5872 | ||
5873 | Check_Unset_Reference (Low_Bound (R)); | |
5874 | Check_Unset_Reference (High_Bound (R)); | |
5875 | end if; | |
5876 | end if; | |
5877 | end Resolve_Discrete_Subtype_Indication; | |
5878 | ||
5879 | ------------------------- | |
5880 | -- Resolve_Entity_Name -- | |
5881 | ------------------------- | |
5882 | ||
5883 | -- Used to resolve identifiers and expanded names | |
5884 | ||
5885 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is | |
5886 | E : constant Entity_Id := Entity (N); | |
5887 | ||
5888 | begin | |
07fc65c4 GB |
5889 | -- If garbage from errors, set to Any_Type and return |
5890 | ||
5891 | if No (E) and then Total_Errors_Detected /= 0 then | |
5892 | Set_Etype (N, Any_Type); | |
5893 | return; | |
5894 | end if; | |
5895 | ||
996ae0b0 RK |
5896 | -- Replace named numbers by corresponding literals. Note that this is |
5897 | -- the one case where Resolve_Entity_Name must reset the Etype, since | |
5898 | -- it is currently marked as universal. | |
5899 | ||
5900 | if Ekind (E) = E_Named_Integer then | |
5901 | Set_Etype (N, Typ); | |
5902 | Eval_Named_Integer (N); | |
5903 | ||
5904 | elsif Ekind (E) = E_Named_Real then | |
5905 | Set_Etype (N, Typ); | |
5906 | Eval_Named_Real (N); | |
5907 | ||
6989bc1f AC |
5908 | -- For enumeration literals, we need to make sure that a proper style |
5909 | -- check is done, since such literals are overloaded, and thus we did | |
5910 | -- not do a style check during the first phase of analysis. | |
5911 | ||
5912 | elsif Ekind (E) = E_Enumeration_Literal then | |
5913 | Set_Entity_With_Style_Check (N, E); | |
5914 | Eval_Entity_Name (N); | |
5915 | ||
996ae0b0 | 5916 | -- Allow use of subtype only if it is a concurrent type where we are |
d81b4bfe TQ |
5917 | -- currently inside the body. This will eventually be expanded into a |
5918 | -- call to Self (for tasks) or _object (for protected objects). Any | |
5919 | -- other use of a subtype is invalid. | |
996ae0b0 RK |
5920 | |
5921 | elsif Is_Type (E) then | |
5922 | if Is_Concurrent_Type (E) | |
5923 | and then In_Open_Scopes (E) | |
5924 | then | |
5925 | null; | |
5926 | else | |
5927 | Error_Msg_N | |
758c442c | 5928 | ("invalid use of subtype mark in expression or call", N); |
996ae0b0 RK |
5929 | end if; |
5930 | ||
5931 | -- Check discriminant use if entity is discriminant in current scope, | |
5932 | -- i.e. discriminant of record or concurrent type currently being | |
5933 | -- analyzed. Uses in corresponding body are unrestricted. | |
5934 | ||
5935 | elsif Ekind (E) = E_Discriminant | |
5936 | and then Scope (E) = Current_Scope | |
5937 | and then not Has_Completion (Current_Scope) | |
5938 | then | |
5939 | Check_Discriminant_Use (N); | |
5940 | ||
5941 | -- A parameterless generic function cannot appear in a context that | |
5942 | -- requires resolution. | |
5943 | ||
5944 | elsif Ekind (E) = E_Generic_Function then | |
5945 | Error_Msg_N ("illegal use of generic function", N); | |
5946 | ||
5947 | elsif Ekind (E) = E_Out_Parameter | |
0ab80019 | 5948 | and then Ada_Version = Ada_83 |
996ae0b0 RK |
5949 | and then (Nkind (Parent (N)) in N_Op |
5950 | or else (Nkind (Parent (N)) = N_Assignment_Statement | |
5951 | and then N = Expression (Parent (N))) | |
5952 | or else Nkind (Parent (N)) = N_Explicit_Dereference) | |
5953 | then | |
5954 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", N); | |
5955 | ||
5956 | -- In all other cases, just do the possible static evaluation | |
5957 | ||
5958 | else | |
d81b4bfe TQ |
5959 | -- A deferred constant that appears in an expression must have a |
5960 | -- completion, unless it has been removed by in-place expansion of | |
5961 | -- an aggregate. | |
996ae0b0 RK |
5962 | |
5963 | if Ekind (E) = E_Constant | |
5964 | and then Comes_From_Source (E) | |
5965 | and then No (Constant_Value (E)) | |
5966 | and then Is_Frozen (Etype (E)) | |
45fc7ddb | 5967 | and then not In_Spec_Expression |
996ae0b0 RK |
5968 | and then not Is_Imported (E) |
5969 | then | |
996ae0b0 RK |
5970 | if No_Initialization (Parent (E)) |
5971 | or else (Present (Full_View (E)) | |
5972 | and then No_Initialization (Parent (Full_View (E)))) | |
5973 | then | |
5974 | null; | |
5975 | else | |
5976 | Error_Msg_N ( | |
5977 | "deferred constant is frozen before completion", N); | |
5978 | end if; | |
5979 | end if; | |
5980 | ||
5981 | Eval_Entity_Name (N); | |
5982 | end if; | |
5983 | end Resolve_Entity_Name; | |
5984 | ||
5985 | ------------------- | |
5986 | -- Resolve_Entry -- | |
5987 | ------------------- | |
5988 | ||
5989 | procedure Resolve_Entry (Entry_Name : Node_Id) is | |
5990 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
5991 | Nam : Entity_Id; | |
5992 | New_N : Node_Id; | |
5993 | S : Entity_Id; | |
5994 | Tsk : Entity_Id; | |
5995 | E_Name : Node_Id; | |
5996 | Index : Node_Id; | |
5997 | ||
5998 | function Actual_Index_Type (E : Entity_Id) return Entity_Id; | |
5999 | -- If the bounds of the entry family being called depend on task | |
6000 | -- discriminants, build a new index subtype where a discriminant is | |
6001 | -- replaced with the value of the discriminant of the target task. | |
6002 | -- The target task is the prefix of the entry name in the call. | |
6003 | ||
6004 | ----------------------- | |
6005 | -- Actual_Index_Type -- | |
6006 | ----------------------- | |
6007 | ||
6008 | function Actual_Index_Type (E : Entity_Id) return Entity_Id is | |
fbf5a39b AC |
6009 | Typ : constant Entity_Id := Entry_Index_Type (E); |
6010 | Tsk : constant Entity_Id := Scope (E); | |
6011 | Lo : constant Node_Id := Type_Low_Bound (Typ); | |
6012 | Hi : constant Node_Id := Type_High_Bound (Typ); | |
996ae0b0 RK |
6013 | New_T : Entity_Id; |
6014 | ||
6015 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id; | |
6016 | -- If the bound is given by a discriminant, replace with a reference | |
d81b4bfe TQ |
6017 | -- to the discriminant of the same name in the target task. If the |
6018 | -- entry name is the target of a requeue statement and the entry is | |
6019 | -- in the current protected object, the bound to be used is the | |
008f6fd3 | 6020 | -- discriminal of the object (see Apply_Range_Checks for details of |
d81b4bfe | 6021 | -- the transformation). |
996ae0b0 RK |
6022 | |
6023 | ----------------------------- | |
6024 | -- Actual_Discriminant_Ref -- | |
6025 | ----------------------------- | |
6026 | ||
6027 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is | |
fbf5a39b | 6028 | Typ : constant Entity_Id := Etype (Bound); |
996ae0b0 RK |
6029 | Ref : Node_Id; |
6030 | ||
6031 | begin | |
6032 | Remove_Side_Effects (Bound); | |
6033 | ||
6034 | if not Is_Entity_Name (Bound) | |
6035 | or else Ekind (Entity (Bound)) /= E_Discriminant | |
6036 | then | |
6037 | return Bound; | |
6038 | ||
6039 | elsif Is_Protected_Type (Tsk) | |
6040 | and then In_Open_Scopes (Tsk) | |
6041 | and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement | |
6042 | then | |
6ca9ec9c AC |
6043 | -- Note: here Bound denotes a discriminant of the corresponding |
6044 | -- record type tskV, whose discriminal is a formal of the | |
6045 | -- init-proc tskVIP. What we want is the body discriminal, | |
6046 | -- which is associated to the discriminant of the original | |
6047 | -- concurrent type tsk. | |
6048 | ||
5a153b27 AC |
6049 | return New_Occurrence_Of |
6050 | (Find_Body_Discriminal (Entity (Bound)), Loc); | |
996ae0b0 RK |
6051 | |
6052 | else | |
6053 | Ref := | |
6054 | Make_Selected_Component (Loc, | |
6055 | Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))), | |
6056 | Selector_Name => New_Occurrence_Of (Entity (Bound), Loc)); | |
6057 | Analyze (Ref); | |
6058 | Resolve (Ref, Typ); | |
6059 | return Ref; | |
6060 | end if; | |
6061 | end Actual_Discriminant_Ref; | |
6062 | ||
6063 | -- Start of processing for Actual_Index_Type | |
6064 | ||
6065 | begin | |
6066 | if not Has_Discriminants (Tsk) | |
6067 | or else (not Is_Entity_Name (Lo) | |
d81b4bfe TQ |
6068 | and then |
6069 | not Is_Entity_Name (Hi)) | |
996ae0b0 RK |
6070 | then |
6071 | return Entry_Index_Type (E); | |
6072 | ||
6073 | else | |
6074 | New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name)); | |
6075 | Set_Etype (New_T, Base_Type (Typ)); | |
6076 | Set_Size_Info (New_T, Typ); | |
6077 | Set_RM_Size (New_T, RM_Size (Typ)); | |
6078 | Set_Scalar_Range (New_T, | |
6079 | Make_Range (Sloc (Entry_Name), | |
6080 | Low_Bound => Actual_Discriminant_Ref (Lo), | |
6081 | High_Bound => Actual_Discriminant_Ref (Hi))); | |
6082 | ||
6083 | return New_T; | |
6084 | end if; | |
6085 | end Actual_Index_Type; | |
6086 | ||
6087 | -- Start of processing of Resolve_Entry | |
6088 | ||
6089 | begin | |
6090 | -- Find name of entry being called, and resolve prefix of name | |
6091 | -- with its own type. The prefix can be overloaded, and the name | |
6092 | -- and signature of the entry must be taken into account. | |
6093 | ||
6094 | if Nkind (Entry_Name) = N_Indexed_Component then | |
6095 | ||
6096 | -- Case of dealing with entry family within the current tasks | |
6097 | ||
6098 | E_Name := Prefix (Entry_Name); | |
6099 | ||
6100 | else | |
6101 | E_Name := Entry_Name; | |
6102 | end if; | |
6103 | ||
6104 | if Is_Entity_Name (E_Name) then | |
996ae0b0 | 6105 | |
d81b4bfe TQ |
6106 | -- Entry call to an entry (or entry family) in the current task. This |
6107 | -- is legal even though the task will deadlock. Rewrite as call to | |
6108 | -- current task. | |
996ae0b0 | 6109 | |
d81b4bfe TQ |
6110 | -- This can also be a call to an entry in an enclosing task. If this |
6111 | -- is a single task, we have to retrieve its name, because the scope | |
6112 | -- of the entry is the task type, not the object. If the enclosing | |
6113 | -- task is a task type, the identity of the task is given by its own | |
6114 | -- self variable. | |
6115 | ||
6116 | -- Finally this can be a requeue on an entry of the same task or | |
6117 | -- protected object. | |
996ae0b0 RK |
6118 | |
6119 | S := Scope (Entity (E_Name)); | |
6120 | ||
6121 | for J in reverse 0 .. Scope_Stack.Last loop | |
996ae0b0 RK |
6122 | if Is_Task_Type (Scope_Stack.Table (J).Entity) |
6123 | and then not Comes_From_Source (S) | |
6124 | then | |
6125 | -- S is an enclosing task or protected object. The concurrent | |
6126 | -- declaration has been converted into a type declaration, and | |
6127 | -- the object itself has an object declaration that follows | |
6128 | -- the type in the same declarative part. | |
6129 | ||
6130 | Tsk := Next_Entity (S); | |
996ae0b0 RK |
6131 | while Etype (Tsk) /= S loop |
6132 | Next_Entity (Tsk); | |
6133 | end loop; | |
6134 | ||
6135 | S := Tsk; | |
6136 | exit; | |
6137 | ||
6138 | elsif S = Scope_Stack.Table (J).Entity then | |
6139 | ||
6140 | -- Call to current task. Will be transformed into call to Self | |
6141 | ||
6142 | exit; | |
6143 | ||
6144 | end if; | |
6145 | end loop; | |
6146 | ||
6147 | New_N := | |
6148 | Make_Selected_Component (Loc, | |
6149 | Prefix => New_Occurrence_Of (S, Loc), | |
6150 | Selector_Name => | |
6151 | New_Occurrence_Of (Entity (E_Name), Loc)); | |
6152 | Rewrite (E_Name, New_N); | |
6153 | Analyze (E_Name); | |
6154 | ||
6155 | elsif Nkind (Entry_Name) = N_Selected_Component | |
6156 | and then Is_Overloaded (Prefix (Entry_Name)) | |
6157 | then | |
d81b4bfe TQ |
6158 | -- Use the entry name (which must be unique at this point) to find |
6159 | -- the prefix that returns the corresponding task type or protected | |
6160 | -- type. | |
996ae0b0 RK |
6161 | |
6162 | declare | |
fbf5a39b AC |
6163 | Pref : constant Node_Id := Prefix (Entry_Name); |
6164 | Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name)); | |
996ae0b0 RK |
6165 | I : Interp_Index; |
6166 | It : Interp; | |
996ae0b0 RK |
6167 | |
6168 | begin | |
6169 | Get_First_Interp (Pref, I, It); | |
996ae0b0 | 6170 | while Present (It.Typ) loop |
996ae0b0 RK |
6171 | if Scope (Ent) = It.Typ then |
6172 | Set_Etype (Pref, It.Typ); | |
6173 | exit; | |
6174 | end if; | |
6175 | ||
6176 | Get_Next_Interp (I, It); | |
6177 | end loop; | |
6178 | end; | |
6179 | end if; | |
6180 | ||
6181 | if Nkind (Entry_Name) = N_Selected_Component then | |
fbf5a39b | 6182 | Resolve (Prefix (Entry_Name)); |
996ae0b0 RK |
6183 | |
6184 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6185 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
fbf5a39b | 6186 | Resolve (Prefix (Prefix (Entry_Name))); |
996ae0b0 RK |
6187 | Index := First (Expressions (Entry_Name)); |
6188 | Resolve (Index, Entry_Index_Type (Nam)); | |
6189 | ||
d81b4bfe TQ |
6190 | -- Up to this point the expression could have been the actual in a |
6191 | -- simple entry call, and be given by a named association. | |
996ae0b0 RK |
6192 | |
6193 | if Nkind (Index) = N_Parameter_Association then | |
6194 | Error_Msg_N ("expect expression for entry index", Index); | |
6195 | else | |
6196 | Apply_Range_Check (Index, Actual_Index_Type (Nam)); | |
6197 | end if; | |
6198 | end if; | |
996ae0b0 RK |
6199 | end Resolve_Entry; |
6200 | ||
6201 | ------------------------ | |
6202 | -- Resolve_Entry_Call -- | |
6203 | ------------------------ | |
6204 | ||
6205 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is | |
6206 | Entry_Name : constant Node_Id := Name (N); | |
6207 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
6208 | Actuals : List_Id; | |
6209 | First_Named : Node_Id; | |
6210 | Nam : Entity_Id; | |
6211 | Norm_OK : Boolean; | |
6212 | Obj : Node_Id; | |
6213 | Was_Over : Boolean; | |
6214 | ||
6215 | begin | |
d81b4bfe TQ |
6216 | -- We kill all checks here, because it does not seem worth the effort to |
6217 | -- do anything better, an entry call is a big operation. | |
fbf5a39b AC |
6218 | |
6219 | Kill_All_Checks; | |
6220 | ||
996ae0b0 RK |
6221 | -- Processing of the name is similar for entry calls and protected |
6222 | -- operation calls. Once the entity is determined, we can complete | |
6223 | -- the resolution of the actuals. | |
6224 | ||
6225 | -- The selector may be overloaded, in the case of a protected object | |
6226 | -- with overloaded functions. The type of the context is used for | |
6227 | -- resolution. | |
6228 | ||
6229 | if Nkind (Entry_Name) = N_Selected_Component | |
6230 | and then Is_Overloaded (Selector_Name (Entry_Name)) | |
6231 | and then Typ /= Standard_Void_Type | |
6232 | then | |
6233 | declare | |
6234 | I : Interp_Index; | |
6235 | It : Interp; | |
6236 | ||
6237 | begin | |
6238 | Get_First_Interp (Selector_Name (Entry_Name), I, It); | |
996ae0b0 | 6239 | while Present (It.Typ) loop |
996ae0b0 RK |
6240 | if Covers (Typ, It.Typ) then |
6241 | Set_Entity (Selector_Name (Entry_Name), It.Nam); | |
6242 | Set_Etype (Entry_Name, It.Typ); | |
6243 | ||
6244 | Generate_Reference (It.Typ, N, ' '); | |
6245 | end if; | |
6246 | ||
6247 | Get_Next_Interp (I, It); | |
6248 | end loop; | |
6249 | end; | |
6250 | end if; | |
6251 | ||
6252 | Resolve_Entry (Entry_Name); | |
6253 | ||
6254 | if Nkind (Entry_Name) = N_Selected_Component then | |
6255 | ||
a77842bd | 6256 | -- Simple entry call |
996ae0b0 RK |
6257 | |
6258 | Nam := Entity (Selector_Name (Entry_Name)); | |
6259 | Obj := Prefix (Entry_Name); | |
6260 | Was_Over := Is_Overloaded (Selector_Name (Entry_Name)); | |
6261 | ||
6262 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6263 | ||
a77842bd | 6264 | -- Call to member of entry family |
996ae0b0 RK |
6265 | |
6266 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
6267 | Obj := Prefix (Prefix (Entry_Name)); | |
6268 | Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name))); | |
6269 | end if; | |
6270 | ||
fbf5a39b AC |
6271 | -- We cannot in general check the maximum depth of protected entry |
6272 | -- calls at compile time. But we can tell that any protected entry | |
6273 | -- call at all violates a specified nesting depth of zero. | |
6274 | ||
6275 | if Is_Protected_Type (Scope (Nam)) then | |
9f4fd324 | 6276 | Check_Restriction (Max_Entry_Queue_Length, N); |
fbf5a39b AC |
6277 | end if; |
6278 | ||
996ae0b0 RK |
6279 | -- Use context type to disambiguate a protected function that can be |
6280 | -- called without actuals and that returns an array type, and where | |
6281 | -- the argument list may be an indexing of the returned value. | |
6282 | ||
6283 | if Ekind (Nam) = E_Function | |
6284 | and then Needs_No_Actuals (Nam) | |
6285 | and then Present (Parameter_Associations (N)) | |
6286 | and then | |
6287 | ((Is_Array_Type (Etype (Nam)) | |
6288 | and then Covers (Typ, Component_Type (Etype (Nam)))) | |
6289 | ||
6290 | or else (Is_Access_Type (Etype (Nam)) | |
6291 | and then Is_Array_Type (Designated_Type (Etype (Nam))) | |
6292 | and then Covers (Typ, | |
6293 | Component_Type (Designated_Type (Etype (Nam)))))) | |
6294 | then | |
6295 | declare | |
6296 | Index_Node : Node_Id; | |
6297 | ||
6298 | begin | |
6299 | Index_Node := | |
6300 | Make_Indexed_Component (Loc, | |
6301 | Prefix => | |
6302 | Make_Function_Call (Loc, | |
6303 | Name => Relocate_Node (Entry_Name)), | |
6304 | Expressions => Parameter_Associations (N)); | |
6305 | ||
6306 | -- Since we are correcting a node classification error made by | |
6307 | -- the parser, we call Replace rather than Rewrite. | |
6308 | ||
6309 | Replace (N, Index_Node); | |
6310 | Set_Etype (Prefix (N), Etype (Nam)); | |
6311 | Set_Etype (N, Typ); | |
6312 | Resolve_Indexed_Component (N, Typ); | |
6313 | return; | |
6314 | end; | |
6315 | end if; | |
6316 | ||
6317 | -- The operation name may have been overloaded. Order the actuals | |
fbf5a39b AC |
6318 | -- according to the formals of the resolved entity, and set the |
6319 | -- return type to that of the operation. | |
996ae0b0 RK |
6320 | |
6321 | if Was_Over then | |
6322 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
6323 | pragma Assert (Norm_OK); | |
fbf5a39b | 6324 | Set_Etype (N, Etype (Nam)); |
996ae0b0 RK |
6325 | end if; |
6326 | ||
6327 | Resolve_Actuals (N, Nam); | |
6328 | Generate_Reference (Nam, Entry_Name); | |
6329 | ||
8a95f4e8 | 6330 | if Ekind_In (Nam, E_Entry, E_Entry_Family) then |
996ae0b0 RK |
6331 | Check_Potentially_Blocking_Operation (N); |
6332 | end if; | |
6333 | ||
6334 | -- Verify that a procedure call cannot masquerade as an entry | |
6335 | -- call where an entry call is expected. | |
6336 | ||
6337 | if Ekind (Nam) = E_Procedure then | |
996ae0b0 RK |
6338 | if Nkind (Parent (N)) = N_Entry_Call_Alternative |
6339 | and then N = Entry_Call_Statement (Parent (N)) | |
6340 | then | |
6341 | Error_Msg_N ("entry call required in select statement", N); | |
6342 | ||
6343 | elsif Nkind (Parent (N)) = N_Triggering_Alternative | |
6344 | and then N = Triggering_Statement (Parent (N)) | |
6345 | then | |
6346 | Error_Msg_N ("triggering statement cannot be procedure call", N); | |
6347 | ||
6348 | elsif Ekind (Scope (Nam)) = E_Task_Type | |
6349 | and then not In_Open_Scopes (Scope (Nam)) | |
6350 | then | |
758c442c | 6351 | Error_Msg_N ("task has no entry with this name", Entry_Name); |
996ae0b0 RK |
6352 | end if; |
6353 | end if; | |
6354 | ||
d81b4bfe TQ |
6355 | -- After resolution, entry calls and protected procedure calls are |
6356 | -- changed into entry calls, for expansion. The structure of the node | |
6357 | -- does not change, so it can safely be done in place. Protected | |
6358 | -- function calls must keep their structure because they are | |
6359 | -- subexpressions. | |
996ae0b0 RK |
6360 | |
6361 | if Ekind (Nam) /= E_Function then | |
6362 | ||
6363 | -- A protected operation that is not a function may modify the | |
d81b4bfe TQ |
6364 | -- corresponding object, and cannot apply to a constant. If this |
6365 | -- is an internal call, the prefix is the type itself. | |
996ae0b0 RK |
6366 | |
6367 | if Is_Protected_Type (Scope (Nam)) | |
6368 | and then not Is_Variable (Obj) | |
6369 | and then (not Is_Entity_Name (Obj) | |
6370 | or else not Is_Type (Entity (Obj))) | |
6371 | then | |
6372 | Error_Msg_N | |
6373 | ("prefix of protected procedure or entry call must be variable", | |
6374 | Entry_Name); | |
6375 | end if; | |
6376 | ||
6377 | Actuals := Parameter_Associations (N); | |
6378 | First_Named := First_Named_Actual (N); | |
6379 | ||
6380 | Rewrite (N, | |
6381 | Make_Entry_Call_Statement (Loc, | |
6382 | Name => Entry_Name, | |
6383 | Parameter_Associations => Actuals)); | |
6384 | ||
6385 | Set_First_Named_Actual (N, First_Named); | |
6386 | Set_Analyzed (N, True); | |
6387 | ||
6388 | -- Protected functions can return on the secondary stack, in which | |
1420b484 | 6389 | -- case we must trigger the transient scope mechanism. |
996ae0b0 RK |
6390 | |
6391 | elsif Expander_Active | |
6392 | and then Requires_Transient_Scope (Etype (Nam)) | |
6393 | then | |
0669bebe | 6394 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 6395 | end if; |
996ae0b0 RK |
6396 | end Resolve_Entry_Call; |
6397 | ||
6398 | ------------------------- | |
6399 | -- Resolve_Equality_Op -- | |
6400 | ------------------------- | |
6401 | ||
d81b4bfe TQ |
6402 | -- Both arguments must have the same type, and the boolean context does |
6403 | -- not participate in the resolution. The first pass verifies that the | |
6404 | -- interpretation is not ambiguous, and the type of the left argument is | |
6405 | -- correctly set, or is Any_Type in case of ambiguity. If both arguments | |
6406 | -- are strings or aggregates, allocators, or Null, they are ambiguous even | |
6407 | -- though they carry a single (universal) type. Diagnose this case here. | |
996ae0b0 RK |
6408 | |
6409 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is | |
6410 | L : constant Node_Id := Left_Opnd (N); | |
6411 | R : constant Node_Id := Right_Opnd (N); | |
6412 | T : Entity_Id := Find_Unique_Type (L, R); | |
6413 | ||
a8930b80 AC |
6414 | procedure Check_Conditional_Expression (Cond : Node_Id); |
6415 | -- The resolution rule for conditional expressions requires that each | |
6416 | -- such must have a unique type. This means that if several dependent | |
6417 | -- expressions are of a non-null anonymous access type, and the context | |
6418 | -- does not impose an expected type (as can be the case in an equality | |
6419 | -- operation) the expression must be rejected. | |
6420 | ||
996ae0b0 RK |
6421 | function Find_Unique_Access_Type return Entity_Id; |
6422 | -- In the case of allocators, make a last-ditch attempt to find a single | |
6423 | -- access type with the right designated type. This is semantically | |
6424 | -- dubious, and of no interest to any real code, but c48008a makes it | |
6425 | -- all worthwhile. | |
6426 | ||
a8930b80 AC |
6427 | ---------------------------------- |
6428 | -- Check_Conditional_Expression -- | |
6429 | ---------------------------------- | |
6430 | ||
6431 | procedure Check_Conditional_Expression (Cond : Node_Id) is | |
6432 | Then_Expr : Node_Id; | |
6433 | Else_Expr : Node_Id; | |
6434 | ||
6435 | begin | |
6436 | if Nkind (Cond) = N_Conditional_Expression then | |
6437 | Then_Expr := Next (First (Expressions (Cond))); | |
6438 | Else_Expr := Next (Then_Expr); | |
6439 | ||
6440 | if Nkind (Then_Expr) /= N_Null | |
6441 | and then Nkind (Else_Expr) /= N_Null | |
6442 | then | |
6443 | Error_Msg_N | |
6444 | ("cannot determine type of conditional expression", Cond); | |
6445 | end if; | |
6446 | end if; | |
6447 | end Check_Conditional_Expression; | |
6448 | ||
996ae0b0 RK |
6449 | ----------------------------- |
6450 | -- Find_Unique_Access_Type -- | |
6451 | ----------------------------- | |
6452 | ||
6453 | function Find_Unique_Access_Type return Entity_Id is | |
6454 | Acc : Entity_Id; | |
6455 | E : Entity_Id; | |
1420b484 | 6456 | S : Entity_Id; |
996ae0b0 RK |
6457 | |
6458 | begin | |
6459 | if Ekind (Etype (R)) = E_Allocator_Type then | |
6460 | Acc := Designated_Type (Etype (R)); | |
996ae0b0 RK |
6461 | elsif Ekind (Etype (L)) = E_Allocator_Type then |
6462 | Acc := Designated_Type (Etype (L)); | |
996ae0b0 RK |
6463 | else |
6464 | return Empty; | |
6465 | end if; | |
6466 | ||
1420b484 | 6467 | S := Current_Scope; |
996ae0b0 RK |
6468 | while S /= Standard_Standard loop |
6469 | E := First_Entity (S); | |
996ae0b0 | 6470 | while Present (E) loop |
996ae0b0 RK |
6471 | if Is_Type (E) |
6472 | and then Is_Access_Type (E) | |
6473 | and then Ekind (E) /= E_Allocator_Type | |
6474 | and then Designated_Type (E) = Base_Type (Acc) | |
6475 | then | |
6476 | return E; | |
6477 | end if; | |
6478 | ||
6479 | Next_Entity (E); | |
6480 | end loop; | |
6481 | ||
6482 | S := Scope (S); | |
6483 | end loop; | |
6484 | ||
6485 | return Empty; | |
6486 | end Find_Unique_Access_Type; | |
6487 | ||
6488 | -- Start of processing for Resolve_Equality_Op | |
6489 | ||
6490 | begin | |
6491 | Set_Etype (N, Base_Type (Typ)); | |
6492 | Generate_Reference (T, N, ' '); | |
6493 | ||
6494 | if T = Any_Fixed then | |
6495 | T := Unique_Fixed_Point_Type (L); | |
6496 | end if; | |
6497 | ||
6498 | if T /= Any_Type then | |
996ae0b0 RK |
6499 | if T = Any_String |
6500 | or else T = Any_Composite | |
6501 | or else T = Any_Character | |
6502 | then | |
996ae0b0 RK |
6503 | if T = Any_Character then |
6504 | Ambiguous_Character (L); | |
6505 | else | |
6506 | Error_Msg_N ("ambiguous operands for equality", N); | |
6507 | end if; | |
6508 | ||
6509 | Set_Etype (N, Any_Type); | |
6510 | return; | |
6511 | ||
6512 | elsif T = Any_Access | |
964f13da | 6513 | or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type) |
996ae0b0 RK |
6514 | then |
6515 | T := Find_Unique_Access_Type; | |
6516 | ||
6517 | if No (T) then | |
6518 | Error_Msg_N ("ambiguous operands for equality", N); | |
6519 | Set_Etype (N, Any_Type); | |
6520 | return; | |
6521 | end if; | |
a8930b80 AC |
6522 | |
6523 | -- Conditional expressions must have a single type, and if the | |
6524 | -- context does not impose one the dependent expressions cannot | |
6525 | -- be anonymous access types. | |
6526 | ||
6527 | elsif Ada_Version >= Ada_2012 | |
ae2aa109 AC |
6528 | and then Ekind_In (Etype (L), E_Anonymous_Access_Type, |
6529 | E_Anonymous_Access_Subprogram_Type) | |
6530 | and then Ekind_In (Etype (R), E_Anonymous_Access_Type, | |
6531 | E_Anonymous_Access_Subprogram_Type) | |
a8930b80 AC |
6532 | then |
6533 | Check_Conditional_Expression (L); | |
6534 | Check_Conditional_Expression (R); | |
996ae0b0 RK |
6535 | end if; |
6536 | ||
996ae0b0 RK |
6537 | Resolve (L, T); |
6538 | Resolve (R, T); | |
fbf5a39b | 6539 | |
0669bebe GB |
6540 | -- If the unique type is a class-wide type then it will be expanded |
6541 | -- into a dispatching call to the predefined primitive. Therefore we | |
6542 | -- check here for potential violation of such restriction. | |
6543 | ||
6544 | if Is_Class_Wide_Type (T) then | |
6545 | Check_Restriction (No_Dispatching_Calls, N); | |
6546 | end if; | |
6547 | ||
fbf5a39b AC |
6548 | if Warn_On_Redundant_Constructs |
6549 | and then Comes_From_Source (N) | |
6550 | and then Is_Entity_Name (R) | |
6551 | and then Entity (R) = Standard_True | |
6552 | and then Comes_From_Source (R) | |
6553 | then | |
305caf42 AC |
6554 | Error_Msg_N -- CODEFIX |
6555 | ("?comparison with True is redundant!", R); | |
fbf5a39b AC |
6556 | end if; |
6557 | ||
996ae0b0 RK |
6558 | Check_Unset_Reference (L); |
6559 | Check_Unset_Reference (R); | |
fbf5a39b | 6560 | Generate_Operator_Reference (N, T); |
fad0600d | 6561 | Check_Low_Bound_Tested (N); |
996ae0b0 RK |
6562 | |
6563 | -- If this is an inequality, it may be the implicit inequality | |
6564 | -- created for a user-defined operation, in which case the corres- | |
6565 | -- ponding equality operation is not intrinsic, and the operation | |
6566 | -- cannot be constant-folded. Else fold. | |
6567 | ||
6568 | if Nkind (N) = N_Op_Eq | |
6569 | or else Comes_From_Source (Entity (N)) | |
6570 | or else Ekind (Entity (N)) = E_Operator | |
6571 | or else Is_Intrinsic_Subprogram | |
6572 | (Corresponding_Equality (Entity (N))) | |
6573 | then | |
6574 | Eval_Relational_Op (N); | |
45fc7ddb | 6575 | |
996ae0b0 | 6576 | elsif Nkind (N) = N_Op_Ne |
0669bebe | 6577 | and then Is_Abstract_Subprogram (Entity (N)) |
996ae0b0 RK |
6578 | then |
6579 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N)); | |
6580 | end if; | |
758c442c | 6581 | |
d81b4bfe TQ |
6582 | -- Ada 2005: If one operand is an anonymous access type, convert the |
6583 | -- other operand to it, to ensure that the underlying types match in | |
6584 | -- the back-end. Same for access_to_subprogram, and the conversion | |
6585 | -- verifies that the types are subtype conformant. | |
b7d1f17f | 6586 | |
d81b4bfe TQ |
6587 | -- We apply the same conversion in the case one of the operands is a |
6588 | -- private subtype of the type of the other. | |
c8ef728f | 6589 | |
b7d1f17f HK |
6590 | -- Why the Expander_Active test here ??? |
6591 | ||
4197ae1e | 6592 | if Expander_Active |
b7d1f17f | 6593 | and then |
964f13da RD |
6594 | (Ekind_In (T, E_Anonymous_Access_Type, |
6595 | E_Anonymous_Access_Subprogram_Type) | |
b7d1f17f | 6596 | or else Is_Private_Type (T)) |
c8ef728f ES |
6597 | then |
6598 | if Etype (L) /= T then | |
6599 | Rewrite (L, | |
6600 | Make_Unchecked_Type_Conversion (Sloc (L), | |
6601 | Subtype_Mark => New_Occurrence_Of (T, Sloc (L)), | |
6602 | Expression => Relocate_Node (L))); | |
6603 | Analyze_And_Resolve (L, T); | |
6604 | end if; | |
6605 | ||
6606 | if (Etype (R)) /= T then | |
6607 | Rewrite (R, | |
6608 | Make_Unchecked_Type_Conversion (Sloc (R), | |
6609 | Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)), | |
6610 | Expression => Relocate_Node (R))); | |
6611 | Analyze_And_Resolve (R, T); | |
6612 | end if; | |
6613 | end if; | |
996ae0b0 RK |
6614 | end if; |
6615 | end Resolve_Equality_Op; | |
6616 | ||
6617 | ---------------------------------- | |
6618 | -- Resolve_Explicit_Dereference -- | |
6619 | ---------------------------------- | |
6620 | ||
6621 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is | |
bc5f3720 RD |
6622 | Loc : constant Source_Ptr := Sloc (N); |
6623 | New_N : Node_Id; | |
6624 | P : constant Node_Id := Prefix (N); | |
6625 | I : Interp_Index; | |
6626 | It : Interp; | |
996ae0b0 RK |
6627 | |
6628 | begin | |
c8ef728f | 6629 | Check_Fully_Declared_Prefix (Typ, P); |
996ae0b0 RK |
6630 | |
6631 | if Is_Overloaded (P) then | |
6632 | ||
758c442c GD |
6633 | -- Use the context type to select the prefix that has the correct |
6634 | -- designated type. | |
996ae0b0 RK |
6635 | |
6636 | Get_First_Interp (P, I, It); | |
6637 | while Present (It.Typ) loop | |
6638 | exit when Is_Access_Type (It.Typ) | |
6639 | and then Covers (Typ, Designated_Type (It.Typ)); | |
996ae0b0 RK |
6640 | Get_Next_Interp (I, It); |
6641 | end loop; | |
6642 | ||
bc5f3720 RD |
6643 | if Present (It.Typ) then |
6644 | Resolve (P, It.Typ); | |
6645 | else | |
758c442c GD |
6646 | -- If no interpretation covers the designated type of the prefix, |
6647 | -- this is the pathological case where not all implementations of | |
6648 | -- the prefix allow the interpretation of the node as a call. Now | |
6649 | -- that the expected type is known, Remove other interpretations | |
6650 | -- from prefix, rewrite it as a call, and resolve again, so that | |
6651 | -- the proper call node is generated. | |
bc5f3720 RD |
6652 | |
6653 | Get_First_Interp (P, I, It); | |
6654 | while Present (It.Typ) loop | |
6655 | if Ekind (It.Typ) /= E_Access_Subprogram_Type then | |
6656 | Remove_Interp (I); | |
6657 | end if; | |
6658 | ||
6659 | Get_Next_Interp (I, It); | |
6660 | end loop; | |
6661 | ||
6662 | New_N := | |
6663 | Make_Function_Call (Loc, | |
6664 | Name => | |
6665 | Make_Explicit_Dereference (Loc, | |
6666 | Prefix => P), | |
6667 | Parameter_Associations => New_List); | |
6668 | ||
6669 | Save_Interps (N, New_N); | |
6670 | Rewrite (N, New_N); | |
6671 | Analyze_And_Resolve (N, Typ); | |
6672 | return; | |
6673 | end if; | |
6674 | ||
996ae0b0 RK |
6675 | Set_Etype (N, Designated_Type (It.Typ)); |
6676 | ||
6677 | else | |
fbf5a39b | 6678 | Resolve (P); |
996ae0b0 RK |
6679 | end if; |
6680 | ||
6681 | if Is_Access_Type (Etype (P)) then | |
6682 | Apply_Access_Check (N); | |
6683 | end if; | |
6684 | ||
758c442c GD |
6685 | -- If the designated type is a packed unconstrained array type, and the |
6686 | -- explicit dereference is not in the context of an attribute reference, | |
6687 | -- then we must compute and set the actual subtype, since it is needed | |
6688 | -- by Gigi. The reason we exclude the attribute case is that this is | |
6689 | -- handled fine by Gigi, and in fact we use such attributes to build the | |
6690 | -- actual subtype. We also exclude generated code (which builds actual | |
6691 | -- subtypes directly if they are needed). | |
996ae0b0 RK |
6692 | |
6693 | if Is_Array_Type (Etype (N)) | |
6694 | and then Is_Packed (Etype (N)) | |
6695 | and then not Is_Constrained (Etype (N)) | |
6696 | and then Nkind (Parent (N)) /= N_Attribute_Reference | |
6697 | and then Comes_From_Source (N) | |
6698 | then | |
6699 | Set_Etype (N, Get_Actual_Subtype (N)); | |
6700 | end if; | |
6701 | ||
09494c32 AC |
6702 | -- Note: No Eval processing is required for an explicit dereference, |
6703 | -- because such a name can never be static. | |
996ae0b0 RK |
6704 | |
6705 | end Resolve_Explicit_Dereference; | |
6706 | ||
955871d3 AC |
6707 | ------------------------------------- |
6708 | -- Resolve_Expression_With_Actions -- | |
6709 | ------------------------------------- | |
6710 | ||
6711 | procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is | |
6712 | begin | |
6713 | Set_Etype (N, Typ); | |
6714 | end Resolve_Expression_With_Actions; | |
6715 | ||
996ae0b0 RK |
6716 | ------------------------------- |
6717 | -- Resolve_Indexed_Component -- | |
6718 | ------------------------------- | |
6719 | ||
6720 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is | |
6721 | Name : constant Node_Id := Prefix (N); | |
6722 | Expr : Node_Id; | |
6723 | Array_Type : Entity_Id := Empty; -- to prevent junk warning | |
6724 | Index : Node_Id; | |
6725 | ||
6726 | begin | |
6727 | if Is_Overloaded (Name) then | |
6728 | ||
758c442c GD |
6729 | -- Use the context type to select the prefix that yields the correct |
6730 | -- component type. | |
996ae0b0 RK |
6731 | |
6732 | declare | |
6733 | I : Interp_Index; | |
6734 | It : Interp; | |
6735 | I1 : Interp_Index := 0; | |
6736 | P : constant Node_Id := Prefix (N); | |
6737 | Found : Boolean := False; | |
6738 | ||
6739 | begin | |
6740 | Get_First_Interp (P, I, It); | |
996ae0b0 | 6741 | while Present (It.Typ) loop |
996ae0b0 RK |
6742 | if (Is_Array_Type (It.Typ) |
6743 | and then Covers (Typ, Component_Type (It.Typ))) | |
6744 | or else (Is_Access_Type (It.Typ) | |
6745 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
6746 | and then Covers | |
6747 | (Typ, Component_Type (Designated_Type (It.Typ)))) | |
6748 | then | |
6749 | if Found then | |
6750 | It := Disambiguate (P, I1, I, Any_Type); | |
6751 | ||
6752 | if It = No_Interp then | |
6753 | Error_Msg_N ("ambiguous prefix for indexing", N); | |
6754 | Set_Etype (N, Typ); | |
6755 | return; | |
6756 | ||
6757 | else | |
6758 | Found := True; | |
6759 | Array_Type := It.Typ; | |
6760 | I1 := I; | |
6761 | end if; | |
6762 | ||
6763 | else | |
6764 | Found := True; | |
6765 | Array_Type := It.Typ; | |
6766 | I1 := I; | |
6767 | end if; | |
6768 | end if; | |
6769 | ||
6770 | Get_Next_Interp (I, It); | |
6771 | end loop; | |
6772 | end; | |
6773 | ||
6774 | else | |
6775 | Array_Type := Etype (Name); | |
6776 | end if; | |
6777 | ||
6778 | Resolve (Name, Array_Type); | |
6779 | Array_Type := Get_Actual_Subtype_If_Available (Name); | |
6780 | ||
6781 | -- If prefix is access type, dereference to get real array type. | |
6782 | -- Note: we do not apply an access check because the expander always | |
6783 | -- introduces an explicit dereference, and the check will happen there. | |
6784 | ||
6785 | if Is_Access_Type (Array_Type) then | |
6786 | Array_Type := Designated_Type (Array_Type); | |
6787 | end if; | |
6788 | ||
a77842bd | 6789 | -- If name was overloaded, set component type correctly now |
f3d57416 | 6790 | -- If a misplaced call to an entry family (which has no index types) |
b7d1f17f | 6791 | -- return. Error will be diagnosed from calling context. |
996ae0b0 | 6792 | |
b7d1f17f HK |
6793 | if Is_Array_Type (Array_Type) then |
6794 | Set_Etype (N, Component_Type (Array_Type)); | |
6795 | else | |
6796 | return; | |
6797 | end if; | |
996ae0b0 RK |
6798 | |
6799 | Index := First_Index (Array_Type); | |
6800 | Expr := First (Expressions (N)); | |
6801 | ||
758c442c GD |
6802 | -- The prefix may have resolved to a string literal, in which case its |
6803 | -- etype has a special representation. This is only possible currently | |
6804 | -- if the prefix is a static concatenation, written in functional | |
6805 | -- notation. | |
996ae0b0 RK |
6806 | |
6807 | if Ekind (Array_Type) = E_String_Literal_Subtype then | |
6808 | Resolve (Expr, Standard_Positive); | |
6809 | ||
6810 | else | |
6811 | while Present (Index) and Present (Expr) loop | |
6812 | Resolve (Expr, Etype (Index)); | |
6813 | Check_Unset_Reference (Expr); | |
6814 | ||
6815 | if Is_Scalar_Type (Etype (Expr)) then | |
6816 | Apply_Scalar_Range_Check (Expr, Etype (Index)); | |
6817 | else | |
6818 | Apply_Range_Check (Expr, Get_Actual_Subtype (Index)); | |
6819 | end if; | |
6820 | ||
6821 | Next_Index (Index); | |
6822 | Next (Expr); | |
6823 | end loop; | |
6824 | end if; | |
6825 | ||
0669bebe GB |
6826 | -- Do not generate the warning on suspicious index if we are analyzing |
6827 | -- package Ada.Tags; otherwise we will report the warning with the | |
6828 | -- Prims_Ptr field of the dispatch table. | |
6829 | ||
6830 | if Scope (Etype (Prefix (N))) = Standard_Standard | |
6831 | or else not | |
6832 | Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))), | |
6833 | Ada_Tags) | |
6834 | then | |
6835 | Warn_On_Suspicious_Index (Name, First (Expressions (N))); | |
6836 | Eval_Indexed_Component (N); | |
6837 | end if; | |
c28408b7 RD |
6838 | |
6839 | -- If the array type is atomic, and is packed, and we are in a left side | |
6840 | -- context, then this is worth a warning, since we have a situation | |
6841 | -- where the access to the component may cause extra read/writes of | |
6842 | -- the atomic array object, which could be considered unexpected. | |
6843 | ||
6844 | if Nkind (N) = N_Indexed_Component | |
6845 | and then (Is_Atomic (Array_Type) | |
6846 | or else (Is_Entity_Name (Prefix (N)) | |
6847 | and then Is_Atomic (Entity (Prefix (N))))) | |
6848 | and then Is_Bit_Packed_Array (Array_Type) | |
6849 | and then Is_LHS (N) | |
6850 | then | |
6851 | Error_Msg_N ("?assignment to component of packed atomic array", | |
6852 | Prefix (N)); | |
6853 | Error_Msg_N ("?\may cause unexpected accesses to atomic object", | |
6854 | Prefix (N)); | |
6855 | end if; | |
996ae0b0 RK |
6856 | end Resolve_Indexed_Component; |
6857 | ||
6858 | ----------------------------- | |
6859 | -- Resolve_Integer_Literal -- | |
6860 | ----------------------------- | |
6861 | ||
6862 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is | |
6863 | begin | |
6864 | Set_Etype (N, Typ); | |
6865 | Eval_Integer_Literal (N); | |
6866 | end Resolve_Integer_Literal; | |
6867 | ||
15ce9ca2 AC |
6868 | -------------------------------- |
6869 | -- Resolve_Intrinsic_Operator -- | |
6870 | -------------------------------- | |
996ae0b0 RK |
6871 | |
6872 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is | |
bb481772 AC |
6873 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); |
6874 | Op : Entity_Id; | |
6875 | Orig_Op : constant Entity_Id := Entity (N); | |
6876 | Arg1 : Node_Id; | |
6877 | Arg2 : Node_Id; | |
996ae0b0 RK |
6878 | |
6879 | begin | |
305caf42 AC |
6880 | -- We must preserve the original entity in a generic setting, so that |
6881 | -- the legality of the operation can be verified in an instance. | |
6882 | ||
6883 | if not Expander_Active then | |
6884 | return; | |
6885 | end if; | |
6886 | ||
996ae0b0 | 6887 | Op := Entity (N); |
996ae0b0 RK |
6888 | while Scope (Op) /= Standard_Standard loop |
6889 | Op := Homonym (Op); | |
6890 | pragma Assert (Present (Op)); | |
6891 | end loop; | |
6892 | ||
6893 | Set_Entity (N, Op); | |
af152989 | 6894 | Set_Is_Overloaded (N, False); |
996ae0b0 | 6895 | |
758c442c GD |
6896 | -- If the operand type is private, rewrite with suitable conversions on |
6897 | -- the operands and the result, to expose the proper underlying numeric | |
6898 | -- type. | |
996ae0b0 | 6899 | |
fbf5a39b AC |
6900 | if Is_Private_Type (Typ) then |
6901 | Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N)); | |
6902 | ||
6903 | if Nkind (N) = N_Op_Expon then | |
6904 | Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N)); | |
6905 | else | |
6906 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
6907 | end if; | |
6908 | ||
bb481772 AC |
6909 | if Nkind (Arg1) = N_Type_Conversion then |
6910 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
6911 | end if; | |
6912 | ||
6913 | if Nkind (Arg2) = N_Type_Conversion then | |
6914 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
6915 | end if; | |
996ae0b0 | 6916 | |
fbf5a39b AC |
6917 | Set_Left_Opnd (N, Arg1); |
6918 | Set_Right_Opnd (N, Arg2); | |
6919 | ||
6920 | Set_Etype (N, Btyp); | |
6921 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
6922 | Resolve (N, Typ); | |
6923 | ||
6924 | elsif Typ /= Etype (Left_Opnd (N)) | |
6925 | or else Typ /= Etype (Right_Opnd (N)) | |
6926 | then | |
d81b4bfe | 6927 | -- Add explicit conversion where needed, and save interpretations in |
bb481772 AC |
6928 | -- case operands are overloaded. If the context is a VMS operation, |
6929 | -- assert that the conversion is legal (the operands have the proper | |
6930 | -- types to select the VMS intrinsic). Note that in rare cases the | |
6931 | -- VMS operators may be visible, but the default System is being used | |
6932 | -- and Address is a private type. | |
fbf5a39b | 6933 | |
af152989 | 6934 | Arg1 := Convert_To (Typ, Left_Opnd (N)); |
fbf5a39b AC |
6935 | Arg2 := Convert_To (Typ, Right_Opnd (N)); |
6936 | ||
6937 | if Nkind (Arg1) = N_Type_Conversion then | |
6938 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
bb481772 AC |
6939 | |
6940 | if Is_VMS_Operator (Orig_Op) then | |
6941 | Set_Conversion_OK (Arg1); | |
6942 | end if; | |
af152989 AC |
6943 | else |
6944 | Save_Interps (Left_Opnd (N), Arg1); | |
fbf5a39b AC |
6945 | end if; |
6946 | ||
6947 | if Nkind (Arg2) = N_Type_Conversion then | |
6948 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
bb481772 AC |
6949 | |
6950 | if Is_VMS_Operator (Orig_Op) then | |
6951 | Set_Conversion_OK (Arg2); | |
6952 | end if; | |
af152989 | 6953 | else |
0ab80019 | 6954 | Save_Interps (Right_Opnd (N), Arg2); |
fbf5a39b AC |
6955 | end if; |
6956 | ||
6957 | Rewrite (Left_Opnd (N), Arg1); | |
6958 | Rewrite (Right_Opnd (N), Arg2); | |
6959 | Analyze (Arg1); | |
6960 | Analyze (Arg2); | |
6961 | Resolve_Arithmetic_Op (N, Typ); | |
6962 | ||
6963 | else | |
6964 | Resolve_Arithmetic_Op (N, Typ); | |
6965 | end if; | |
996ae0b0 RK |
6966 | end Resolve_Intrinsic_Operator; |
6967 | ||
fbf5a39b AC |
6968 | -------------------------------------- |
6969 | -- Resolve_Intrinsic_Unary_Operator -- | |
6970 | -------------------------------------- | |
6971 | ||
6972 | procedure Resolve_Intrinsic_Unary_Operator | |
6973 | (N : Node_Id; | |
6974 | Typ : Entity_Id) | |
6975 | is | |
6976 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); | |
6977 | Op : Entity_Id; | |
6978 | Arg2 : Node_Id; | |
6979 | ||
6980 | begin | |
6981 | Op := Entity (N); | |
fbf5a39b AC |
6982 | while Scope (Op) /= Standard_Standard loop |
6983 | Op := Homonym (Op); | |
6984 | pragma Assert (Present (Op)); | |
6985 | end loop; | |
6986 | ||
6987 | Set_Entity (N, Op); | |
6988 | ||
6989 | if Is_Private_Type (Typ) then | |
6990 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
6991 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
6992 | ||
6993 | Set_Right_Opnd (N, Arg2); | |
6994 | ||
6995 | Set_Etype (N, Btyp); | |
6996 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
6997 | Resolve (N, Typ); | |
6998 | ||
6999 | else | |
7000 | Resolve_Unary_Op (N, Typ); | |
7001 | end if; | |
7002 | end Resolve_Intrinsic_Unary_Operator; | |
7003 | ||
996ae0b0 RK |
7004 | ------------------------ |
7005 | -- Resolve_Logical_Op -- | |
7006 | ------------------------ | |
7007 | ||
7008 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is | |
7009 | B_Typ : Entity_Id; | |
7010 | ||
7011 | begin | |
f61580d4 AC |
7012 | Check_No_Direct_Boolean_Operators (N); |
7013 | ||
758c442c GD |
7014 | -- Predefined operations on scalar types yield the base type. On the |
7015 | -- other hand, logical operations on arrays yield the type of the | |
7016 | -- arguments (and the context). | |
996ae0b0 RK |
7017 | |
7018 | if Is_Array_Type (Typ) then | |
7019 | B_Typ := Typ; | |
7020 | else | |
7021 | B_Typ := Base_Type (Typ); | |
7022 | end if; | |
7023 | ||
001c7783 AC |
7024 | -- OK if this is a VMS-specific intrinsic operation |
7025 | ||
7026 | if Is_VMS_Operator (Entity (N)) then | |
7027 | null; | |
7028 | ||
996ae0b0 RK |
7029 | -- The following test is required because the operands of the operation |
7030 | -- may be literals, in which case the resulting type appears to be | |
7031 | -- compatible with a signed integer type, when in fact it is compatible | |
7032 | -- only with modular types. If the context itself is universal, the | |
7033 | -- operation is illegal. | |
7034 | ||
001c7783 | 7035 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
7036 | Error_Msg_N ("invalid context for logical operation", N); |
7037 | Set_Etype (N, Any_Type); | |
7038 | return; | |
7039 | ||
7040 | elsif Typ = Any_Modular then | |
7041 | Error_Msg_N | |
7042 | ("no modular type available in this context", N); | |
7043 | Set_Etype (N, Any_Type); | |
7044 | return; | |
07fc65c4 GB |
7045 | elsif Is_Modular_Integer_Type (Typ) |
7046 | and then Etype (Left_Opnd (N)) = Universal_Integer | |
7047 | and then Etype (Right_Opnd (N)) = Universal_Integer | |
7048 | then | |
7049 | Check_For_Visible_Operator (N, B_Typ); | |
996ae0b0 RK |
7050 | end if; |
7051 | ||
7052 | Resolve (Left_Opnd (N), B_Typ); | |
7053 | Resolve (Right_Opnd (N), B_Typ); | |
7054 | ||
7055 | Check_Unset_Reference (Left_Opnd (N)); | |
7056 | Check_Unset_Reference (Right_Opnd (N)); | |
7057 | ||
7058 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7059 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7060 | Eval_Logical_Op (N); |
7061 | end Resolve_Logical_Op; | |
7062 | ||
7063 | --------------------------- | |
7064 | -- Resolve_Membership_Op -- | |
7065 | --------------------------- | |
7066 | ||
7067 | -- The context can only be a boolean type, and does not determine | |
7068 | -- the arguments. Arguments should be unambiguous, but the preference | |
7069 | -- rule for universal types applies. | |
7070 | ||
7071 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7072 | pragma Warnings (Off, Typ); |
7073 | ||
197e4514 | 7074 | L : constant Node_Id := Left_Opnd (N); |
b1c11e0e | 7075 | R : constant Node_Id := Right_Opnd (N); |
996ae0b0 RK |
7076 | T : Entity_Id; |
7077 | ||
197e4514 AC |
7078 | procedure Resolve_Set_Membership; |
7079 | -- Analysis has determined a unique type for the left operand. | |
7080 | -- Use it to resolve the disjuncts. | |
7081 | ||
7082 | ---------------------------- | |
7083 | -- Resolve_Set_Membership -- | |
7084 | ---------------------------- | |
7085 | ||
7086 | procedure Resolve_Set_Membership is | |
7087 | Alt : Node_Id; | |
7088 | ||
7089 | begin | |
7090 | Resolve (L, Etype (L)); | |
7091 | ||
7092 | Alt := First (Alternatives (N)); | |
7093 | while Present (Alt) loop | |
7094 | ||
7095 | -- Alternative is an expression, a range | |
7096 | -- or a subtype mark. | |
7097 | ||
7098 | if not Is_Entity_Name (Alt) | |
7099 | or else not Is_Type (Entity (Alt)) | |
7100 | then | |
7101 | Resolve (Alt, Etype (L)); | |
7102 | end if; | |
7103 | ||
7104 | Next (Alt); | |
7105 | end loop; | |
7106 | end Resolve_Set_Membership; | |
7107 | ||
442c0581 | 7108 | -- Start of processing for Resolve_Membership_Op |
197e4514 | 7109 | |
996ae0b0 RK |
7110 | begin |
7111 | if L = Error or else R = Error then | |
7112 | return; | |
7113 | end if; | |
7114 | ||
197e4514 AC |
7115 | if Present (Alternatives (N)) then |
7116 | Resolve_Set_Membership; | |
7117 | return; | |
7118 | ||
7119 | elsif not Is_Overloaded (R) | |
996ae0b0 RK |
7120 | and then |
7121 | (Etype (R) = Universal_Integer or else | |
7122 | Etype (R) = Universal_Real) | |
7123 | and then Is_Overloaded (L) | |
7124 | then | |
7125 | T := Etype (R); | |
1420b484 | 7126 | |
d81b4bfe | 7127 | -- Ada 2005 (AI-251): Support the following case: |
1420b484 JM |
7128 | |
7129 | -- type I is interface; | |
7130 | -- type T is tagged ... | |
7131 | ||
c8ef728f | 7132 | -- function Test (O : I'Class) is |
1420b484 JM |
7133 | -- begin |
7134 | -- return O in T'Class. | |
7135 | -- end Test; | |
7136 | ||
d81b4bfe | 7137 | -- In this case we have nothing else to do. The membership test will be |
e7c0dd39 | 7138 | -- done at run time. |
1420b484 JM |
7139 | |
7140 | elsif Ada_Version >= Ada_05 | |
7141 | and then Is_Class_Wide_Type (Etype (L)) | |
7142 | and then Is_Interface (Etype (L)) | |
7143 | and then Is_Class_Wide_Type (Etype (R)) | |
7144 | and then not Is_Interface (Etype (R)) | |
7145 | then | |
7146 | return; | |
7147 | ||
996ae0b0 RK |
7148 | else |
7149 | T := Intersect_Types (L, R); | |
7150 | end if; | |
7151 | ||
9a0ddeee AC |
7152 | -- If mixed-mode operations are present and operands are all literal, |
7153 | -- the only interpretation involves Duration, which is probably not | |
7154 | -- the intention of the programmer. | |
7155 | ||
7156 | if T = Any_Fixed then | |
7157 | T := Unique_Fixed_Point_Type (N); | |
7158 | ||
7159 | if T = Any_Type then | |
7160 | return; | |
7161 | end if; | |
7162 | end if; | |
7163 | ||
996ae0b0 RK |
7164 | Resolve (L, T); |
7165 | Check_Unset_Reference (L); | |
7166 | ||
7167 | if Nkind (R) = N_Range | |
7168 | and then not Is_Scalar_Type (T) | |
7169 | then | |
7170 | Error_Msg_N ("scalar type required for range", R); | |
7171 | end if; | |
7172 | ||
7173 | if Is_Entity_Name (R) then | |
7174 | Freeze_Expression (R); | |
7175 | else | |
7176 | Resolve (R, T); | |
7177 | Check_Unset_Reference (R); | |
7178 | end if; | |
7179 | ||
7180 | Eval_Membership_Op (N); | |
7181 | end Resolve_Membership_Op; | |
7182 | ||
7183 | ------------------ | |
7184 | -- Resolve_Null -- | |
7185 | ------------------ | |
7186 | ||
7187 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is | |
b1c11e0e JM |
7188 | Loc : constant Source_Ptr := Sloc (N); |
7189 | ||
996ae0b0 | 7190 | begin |
758c442c | 7191 | -- Handle restriction against anonymous null access values This |
6ba6b1e3 | 7192 | -- restriction can be turned off using -gnatdj. |
996ae0b0 | 7193 | |
0ab80019 | 7194 | -- Ada 2005 (AI-231): Remove restriction |
2820d220 | 7195 | |
0ab80019 | 7196 | if Ada_Version < Ada_05 |
2820d220 | 7197 | and then not Debug_Flag_J |
996ae0b0 RK |
7198 | and then Ekind (Typ) = E_Anonymous_Access_Type |
7199 | and then Comes_From_Source (N) | |
7200 | then | |
d81b4bfe TQ |
7201 | -- In the common case of a call which uses an explicitly null value |
7202 | -- for an access parameter, give specialized error message. | |
996ae0b0 | 7203 | |
45fc7ddb HK |
7204 | if Nkind_In (Parent (N), N_Procedure_Call_Statement, |
7205 | N_Function_Call) | |
996ae0b0 RK |
7206 | then |
7207 | Error_Msg_N | |
7208 | ("null is not allowed as argument for an access parameter", N); | |
7209 | ||
7210 | -- Standard message for all other cases (are there any?) | |
7211 | ||
7212 | else | |
7213 | Error_Msg_N | |
7214 | ("null cannot be of an anonymous access type", N); | |
7215 | end if; | |
7216 | end if; | |
7217 | ||
b1c11e0e JM |
7218 | -- Ada 2005 (AI-231): Generate the null-excluding check in case of |
7219 | -- assignment to a null-excluding object | |
7220 | ||
7221 | if Ada_Version >= Ada_05 | |
7222 | and then Can_Never_Be_Null (Typ) | |
7223 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
7224 | then | |
7225 | if not Inside_Init_Proc then | |
7226 | Insert_Action | |
7227 | (Compile_Time_Constraint_Error (N, | |
7228 | "(Ada 2005) null not allowed in null-excluding objects?"), | |
7229 | Make_Raise_Constraint_Error (Loc, | |
7230 | Reason => CE_Access_Check_Failed)); | |
7231 | else | |
7232 | Insert_Action (N, | |
7233 | Make_Raise_Constraint_Error (Loc, | |
7234 | Reason => CE_Access_Check_Failed)); | |
7235 | end if; | |
7236 | end if; | |
7237 | ||
d81b4bfe TQ |
7238 | -- In a distributed context, null for a remote access to subprogram may |
7239 | -- need to be replaced with a special record aggregate. In this case, | |
7240 | -- return after having done the transformation. | |
996ae0b0 RK |
7241 | |
7242 | if (Ekind (Typ) = E_Record_Type | |
7243 | or else Is_Remote_Access_To_Subprogram_Type (Typ)) | |
7244 | and then Remote_AST_Null_Value (N, Typ) | |
7245 | then | |
7246 | return; | |
7247 | end if; | |
7248 | ||
a77842bd | 7249 | -- The null literal takes its type from the context |
996ae0b0 RK |
7250 | |
7251 | Set_Etype (N, Typ); | |
7252 | end Resolve_Null; | |
7253 | ||
7254 | ----------------------- | |
7255 | -- Resolve_Op_Concat -- | |
7256 | ----------------------- | |
7257 | ||
7258 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is | |
996ae0b0 | 7259 | |
10303118 BD |
7260 | -- We wish to avoid deep recursion, because concatenations are often |
7261 | -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left | |
7262 | -- operands nonrecursively until we find something that is not a simple | |
7263 | -- concatenation (A in this case). We resolve that, and then walk back | |
7264 | -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest | |
7265 | -- to do the rest of the work at each level. The Parent pointers allow | |
7266 | -- us to avoid recursion, and thus avoid running out of memory. See also | |
d81b4bfe | 7267 | -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used. |
996ae0b0 | 7268 | |
10303118 BD |
7269 | NN : Node_Id := N; |
7270 | Op1 : Node_Id; | |
996ae0b0 | 7271 | |
10303118 BD |
7272 | begin |
7273 | -- The following code is equivalent to: | |
996ae0b0 | 7274 | |
10303118 BD |
7275 | -- Resolve_Op_Concat_First (NN, Typ); |
7276 | -- Resolve_Op_Concat_Arg (N, ...); | |
7277 | -- Resolve_Op_Concat_Rest (N, Typ); | |
996ae0b0 | 7278 | |
10303118 BD |
7279 | -- where the Resolve_Op_Concat_Arg call recurses back here if the left |
7280 | -- operand is a concatenation. | |
996ae0b0 | 7281 | |
10303118 | 7282 | -- Walk down left operands |
996ae0b0 | 7283 | |
10303118 BD |
7284 | loop |
7285 | Resolve_Op_Concat_First (NN, Typ); | |
7286 | Op1 := Left_Opnd (NN); | |
7287 | exit when not (Nkind (Op1) = N_Op_Concat | |
7288 | and then not Is_Array_Type (Component_Type (Typ)) | |
7289 | and then Entity (Op1) = Entity (NN)); | |
7290 | NN := Op1; | |
7291 | end loop; | |
996ae0b0 | 7292 | |
10303118 | 7293 | -- Now (given the above example) NN is A&B and Op1 is A |
996ae0b0 | 7294 | |
10303118 | 7295 | -- First resolve Op1 ... |
9ebe3743 | 7296 | |
10303118 | 7297 | Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN)); |
9ebe3743 | 7298 | |
10303118 BD |
7299 | -- ... then walk NN back up until we reach N (where we started), calling |
7300 | -- Resolve_Op_Concat_Rest along the way. | |
9ebe3743 | 7301 | |
10303118 BD |
7302 | loop |
7303 | Resolve_Op_Concat_Rest (NN, Typ); | |
7304 | exit when NN = N; | |
7305 | NN := Parent (NN); | |
7306 | end loop; | |
7307 | end Resolve_Op_Concat; | |
9ebe3743 | 7308 | |
10303118 BD |
7309 | --------------------------- |
7310 | -- Resolve_Op_Concat_Arg -- | |
7311 | --------------------------- | |
996ae0b0 | 7312 | |
10303118 BD |
7313 | procedure Resolve_Op_Concat_Arg |
7314 | (N : Node_Id; | |
7315 | Arg : Node_Id; | |
7316 | Typ : Entity_Id; | |
7317 | Is_Comp : Boolean) | |
7318 | is | |
7319 | Btyp : constant Entity_Id := Base_Type (Typ); | |
996ae0b0 | 7320 | |
10303118 BD |
7321 | begin |
7322 | if In_Instance then | |
7323 | if Is_Comp | |
7324 | or else (not Is_Overloaded (Arg) | |
7325 | and then Etype (Arg) /= Any_Composite | |
7326 | and then Covers (Component_Type (Typ), Etype (Arg))) | |
7327 | then | |
7328 | Resolve (Arg, Component_Type (Typ)); | |
7329 | else | |
7330 | Resolve (Arg, Btyp); | |
7331 | end if; | |
fbf5a39b | 7332 | |
10303118 BD |
7333 | elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then |
7334 | if Nkind (Arg) = N_Aggregate | |
7335 | and then Is_Composite_Type (Component_Type (Typ)) | |
7336 | then | |
7337 | if Is_Private_Type (Component_Type (Typ)) then | |
7338 | Resolve (Arg, Btyp); | |
7339 | else | |
7340 | Error_Msg_N ("ambiguous aggregate must be qualified", Arg); | |
7341 | Set_Etype (Arg, Any_Type); | |
996ae0b0 RK |
7342 | end if; |
7343 | ||
7344 | else | |
10303118 BD |
7345 | if Is_Overloaded (Arg) |
7346 | and then Has_Compatible_Type (Arg, Typ) | |
7347 | and then Etype (Arg) /= Any_Type | |
7348 | then | |
7349 | declare | |
7350 | I : Interp_Index; | |
7351 | It : Interp; | |
7352 | Func : Entity_Id; | |
7353 | ||
7354 | begin | |
7355 | Get_First_Interp (Arg, I, It); | |
7356 | Func := It.Nam; | |
7357 | Get_Next_Interp (I, It); | |
7358 | ||
7359 | -- Special-case the error message when the overloading is | |
7360 | -- caused by a function that yields an array and can be | |
7361 | -- called without parameters. | |
7362 | ||
7363 | if It.Nam = Func then | |
7364 | Error_Msg_Sloc := Sloc (Func); | |
7365 | Error_Msg_N ("ambiguous call to function#", Arg); | |
7366 | Error_Msg_NE | |
7367 | ("\\interpretation as call yields&", Arg, Typ); | |
7368 | Error_Msg_NE | |
7369 | ("\\interpretation as indexing of call yields&", | |
7370 | Arg, Component_Type (Typ)); | |
7371 | ||
7372 | else | |
7373 | Error_Msg_N | |
7374 | ("ambiguous operand for concatenation!", Arg); | |
7375 | Get_First_Interp (Arg, I, It); | |
7376 | while Present (It.Nam) loop | |
7377 | Error_Msg_Sloc := Sloc (It.Nam); | |
7378 | ||
7379 | if Base_Type (It.Typ) = Base_Type (Typ) | |
7380 | or else Base_Type (It.Typ) = | |
7381 | Base_Type (Component_Type (Typ)) | |
7382 | then | |
4e7a4f6e AC |
7383 | Error_Msg_N -- CODEFIX |
7384 | ("\\possible interpretation#", Arg); | |
10303118 BD |
7385 | end if; |
7386 | ||
7387 | Get_Next_Interp (I, It); | |
7388 | end loop; | |
7389 | end if; | |
7390 | end; | |
7391 | end if; | |
7392 | ||
7393 | Resolve (Arg, Component_Type (Typ)); | |
7394 | ||
7395 | if Nkind (Arg) = N_String_Literal then | |
7396 | Set_Etype (Arg, Component_Type (Typ)); | |
7397 | end if; | |
7398 | ||
7399 | if Arg = Left_Opnd (N) then | |
7400 | Set_Is_Component_Left_Opnd (N); | |
7401 | else | |
7402 | Set_Is_Component_Right_Opnd (N); | |
7403 | end if; | |
996ae0b0 RK |
7404 | end if; |
7405 | ||
10303118 BD |
7406 | else |
7407 | Resolve (Arg, Btyp); | |
7408 | end if; | |
7409 | ||
7410 | Check_Unset_Reference (Arg); | |
7411 | end Resolve_Op_Concat_Arg; | |
996ae0b0 | 7412 | |
10303118 BD |
7413 | ----------------------------- |
7414 | -- Resolve_Op_Concat_First -- | |
7415 | ----------------------------- | |
7416 | ||
7417 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is | |
7418 | Btyp : constant Entity_Id := Base_Type (Typ); | |
7419 | Op1 : constant Node_Id := Left_Opnd (N); | |
7420 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 RK |
7421 | |
7422 | begin | |
dae2b8ea HK |
7423 | -- The parser folds an enormous sequence of concatenations of string |
7424 | -- literals into "" & "...", where the Is_Folded_In_Parser flag is set | |
4fc26524 | 7425 | -- in the right operand. If the expression resolves to a predefined "&" |
dae2b8ea HK |
7426 | -- operator, all is well. Otherwise, the parser's folding is wrong, so |
7427 | -- we give an error. See P_Simple_Expression in Par.Ch4. | |
7428 | ||
7429 | if Nkind (Op2) = N_String_Literal | |
7430 | and then Is_Folded_In_Parser (Op2) | |
7431 | and then Ekind (Entity (N)) = E_Function | |
7432 | then | |
7433 | pragma Assert (Nkind (Op1) = N_String_Literal -- should be "" | |
7434 | and then String_Length (Strval (Op1)) = 0); | |
7435 | Error_Msg_N ("too many user-defined concatenations", N); | |
7436 | return; | |
7437 | end if; | |
7438 | ||
996ae0b0 RK |
7439 | Set_Etype (N, Btyp); |
7440 | ||
7441 | if Is_Limited_Composite (Btyp) then | |
7442 | Error_Msg_N ("concatenation not available for limited array", N); | |
fbf5a39b | 7443 | Explain_Limited_Type (Btyp, N); |
996ae0b0 | 7444 | end if; |
10303118 | 7445 | end Resolve_Op_Concat_First; |
996ae0b0 | 7446 | |
10303118 BD |
7447 | ---------------------------- |
7448 | -- Resolve_Op_Concat_Rest -- | |
7449 | ---------------------------- | |
996ae0b0 | 7450 | |
10303118 BD |
7451 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is |
7452 | Op1 : constant Node_Id := Left_Opnd (N); | |
7453 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 | 7454 | |
10303118 BD |
7455 | begin |
7456 | Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N)); | |
996ae0b0 | 7457 | |
fbf5a39b | 7458 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
7459 | |
7460 | if Is_String_Type (Typ) then | |
7461 | Eval_Concatenation (N); | |
7462 | end if; | |
7463 | ||
d81b4bfe TQ |
7464 | -- If this is not a static concatenation, but the result is a string |
7465 | -- type (and not an array of strings) ensure that static string operands | |
7466 | -- have their subtypes properly constructed. | |
996ae0b0 RK |
7467 | |
7468 | if Nkind (N) /= N_String_Literal | |
7469 | and then Is_Character_Type (Component_Type (Typ)) | |
7470 | then | |
7471 | Set_String_Literal_Subtype (Op1, Typ); | |
7472 | Set_String_Literal_Subtype (Op2, Typ); | |
7473 | end if; | |
10303118 | 7474 | end Resolve_Op_Concat_Rest; |
996ae0b0 RK |
7475 | |
7476 | ---------------------- | |
7477 | -- Resolve_Op_Expon -- | |
7478 | ---------------------- | |
7479 | ||
7480 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is | |
7481 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
7482 | ||
7483 | begin | |
f3d57416 | 7484 | -- Catch attempts to do fixed-point exponentiation with universal |
758c442c GD |
7485 | -- operands, which is a case where the illegality is not caught during |
7486 | -- normal operator analysis. | |
996ae0b0 RK |
7487 | |
7488 | if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then | |
7489 | Error_Msg_N ("exponentiation not available for fixed point", N); | |
7490 | return; | |
7491 | end if; | |
7492 | ||
fbf5a39b AC |
7493 | if Comes_From_Source (N) |
7494 | and then Ekind (Entity (N)) = E_Function | |
7495 | and then Is_Imported (Entity (N)) | |
7496 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
7497 | then | |
7498 | Resolve_Intrinsic_Operator (N, Typ); | |
7499 | return; | |
7500 | end if; | |
7501 | ||
996ae0b0 RK |
7502 | if Etype (Left_Opnd (N)) = Universal_Integer |
7503 | or else Etype (Left_Opnd (N)) = Universal_Real | |
7504 | then | |
7505 | Check_For_Visible_Operator (N, B_Typ); | |
7506 | end if; | |
7507 | ||
7508 | -- We do the resolution using the base type, because intermediate values | |
7509 | -- in expressions always are of the base type, not a subtype of it. | |
7510 | ||
7511 | Resolve (Left_Opnd (N), B_Typ); | |
7512 | Resolve (Right_Opnd (N), Standard_Integer); | |
7513 | ||
7514 | Check_Unset_Reference (Left_Opnd (N)); | |
7515 | Check_Unset_Reference (Right_Opnd (N)); | |
7516 | ||
7517 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7518 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7519 | Eval_Op_Expon (N); |
7520 | ||
7521 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
7522 | -- and perhaps the Resolve routines should be separated for the various | |
7523 | -- arithmetic operations, since they will need different processing. ??? | |
7524 | ||
7525 | if Nkind (N) in N_Op then | |
7526 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 7527 | Enable_Overflow_Check (N); |
996ae0b0 RK |
7528 | end if; |
7529 | end if; | |
996ae0b0 RK |
7530 | end Resolve_Op_Expon; |
7531 | ||
7532 | -------------------- | |
7533 | -- Resolve_Op_Not -- | |
7534 | -------------------- | |
7535 | ||
7536 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is | |
7537 | B_Typ : Entity_Id; | |
7538 | ||
7539 | function Parent_Is_Boolean return Boolean; | |
7540 | -- This function determines if the parent node is a boolean operator | |
7541 | -- or operation (comparison op, membership test, or short circuit form) | |
7542 | -- and the not in question is the left operand of this operation. | |
7543 | -- Note that if the not is in parens, then false is returned. | |
7544 | ||
aa180613 RD |
7545 | ----------------------- |
7546 | -- Parent_Is_Boolean -- | |
7547 | ----------------------- | |
7548 | ||
996ae0b0 RK |
7549 | function Parent_Is_Boolean return Boolean is |
7550 | begin | |
7551 | if Paren_Count (N) /= 0 then | |
7552 | return False; | |
7553 | ||
7554 | else | |
7555 | case Nkind (Parent (N)) is | |
7556 | when N_Op_And | | |
7557 | N_Op_Eq | | |
7558 | N_Op_Ge | | |
7559 | N_Op_Gt | | |
7560 | N_Op_Le | | |
7561 | N_Op_Lt | | |
7562 | N_Op_Ne | | |
7563 | N_Op_Or | | |
7564 | N_Op_Xor | | |
7565 | N_In | | |
7566 | N_Not_In | | |
7567 | N_And_Then | | |
aa180613 | 7568 | N_Or_Else => |
996ae0b0 RK |
7569 | |
7570 | return Left_Opnd (Parent (N)) = N; | |
7571 | ||
7572 | when others => | |
7573 | return False; | |
7574 | end case; | |
7575 | end if; | |
7576 | end Parent_Is_Boolean; | |
7577 | ||
7578 | -- Start of processing for Resolve_Op_Not | |
7579 | ||
7580 | begin | |
758c442c GD |
7581 | -- Predefined operations on scalar types yield the base type. On the |
7582 | -- other hand, logical operations on arrays yield the type of the | |
7583 | -- arguments (and the context). | |
996ae0b0 RK |
7584 | |
7585 | if Is_Array_Type (Typ) then | |
7586 | B_Typ := Typ; | |
7587 | else | |
7588 | B_Typ := Base_Type (Typ); | |
7589 | end if; | |
7590 | ||
001c7783 AC |
7591 | if Is_VMS_Operator (Entity (N)) then |
7592 | null; | |
7593 | ||
f3d57416 | 7594 | -- Straightforward case of incorrect arguments |
aa180613 | 7595 | |
001c7783 | 7596 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
7597 | Error_Msg_N ("invalid operand type for operator&", N); |
7598 | Set_Etype (N, Any_Type); | |
7599 | return; | |
7600 | ||
aa180613 RD |
7601 | -- Special case of probable missing parens |
7602 | ||
fbf5a39b | 7603 | elsif Typ = Universal_Integer or else Typ = Any_Modular then |
996ae0b0 | 7604 | if Parent_Is_Boolean then |
ed2233dc | 7605 | Error_Msg_N |
996ae0b0 RK |
7606 | ("operand of not must be enclosed in parentheses", |
7607 | Right_Opnd (N)); | |
7608 | else | |
7609 | Error_Msg_N | |
7610 | ("no modular type available in this context", N); | |
7611 | end if; | |
7612 | ||
7613 | Set_Etype (N, Any_Type); | |
7614 | return; | |
7615 | ||
aa180613 RD |
7616 | -- OK resolution of not |
7617 | ||
996ae0b0 | 7618 | else |
aa180613 RD |
7619 | -- Warn if non-boolean types involved. This is a case like not a < b |
7620 | -- where a and b are modular, where we will get (not a) < b and most | |
7621 | -- likely not (a < b) was intended. | |
7622 | ||
7623 | if Warn_On_Questionable_Missing_Parens | |
7624 | and then not Is_Boolean_Type (Typ) | |
996ae0b0 RK |
7625 | and then Parent_Is_Boolean |
7626 | then | |
ed2233dc | 7627 | Error_Msg_N ("?not expression should be parenthesized here!", N); |
996ae0b0 RK |
7628 | end if; |
7629 | ||
09bc9ab6 RD |
7630 | -- Warn on double negation if checking redundant constructs |
7631 | ||
7632 | if Warn_On_Redundant_Constructs | |
7633 | and then Comes_From_Source (N) | |
7634 | and then Comes_From_Source (Right_Opnd (N)) | |
7635 | and then Root_Type (Typ) = Standard_Boolean | |
7636 | and then Nkind (Right_Opnd (N)) = N_Op_Not | |
7637 | then | |
ed2233dc | 7638 | Error_Msg_N ("redundant double negation?", N); |
09bc9ab6 RD |
7639 | end if; |
7640 | ||
7641 | -- Complete resolution and evaluation of NOT | |
7642 | ||
996ae0b0 RK |
7643 | Resolve (Right_Opnd (N), B_Typ); |
7644 | Check_Unset_Reference (Right_Opnd (N)); | |
7645 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7646 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7647 | Eval_Op_Not (N); |
7648 | end if; | |
7649 | end Resolve_Op_Not; | |
7650 | ||
7651 | ----------------------------- | |
7652 | -- Resolve_Operator_Symbol -- | |
7653 | ----------------------------- | |
7654 | ||
7655 | -- Nothing to be done, all resolved already | |
7656 | ||
7657 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7658 | pragma Warnings (Off, N); |
7659 | pragma Warnings (Off, Typ); | |
7660 | ||
996ae0b0 RK |
7661 | begin |
7662 | null; | |
7663 | end Resolve_Operator_Symbol; | |
7664 | ||
7665 | ---------------------------------- | |
7666 | -- Resolve_Qualified_Expression -- | |
7667 | ---------------------------------- | |
7668 | ||
7669 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7670 | pragma Warnings (Off, Typ); |
7671 | ||
996ae0b0 RK |
7672 | Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N)); |
7673 | Expr : constant Node_Id := Expression (N); | |
7674 | ||
7675 | begin | |
7676 | Resolve (Expr, Target_Typ); | |
7677 | ||
7678 | -- A qualified expression requires an exact match of the type, | |
1420b484 JM |
7679 | -- class-wide matching is not allowed. However, if the qualifying |
7680 | -- type is specific and the expression has a class-wide type, it | |
7681 | -- may still be okay, since it can be the result of the expansion | |
7682 | -- of a call to a dispatching function, so we also have to check | |
7683 | -- class-wideness of the type of the expression's original node. | |
7684 | ||
7685 | if (Is_Class_Wide_Type (Target_Typ) | |
7686 | or else | |
7687 | (Is_Class_Wide_Type (Etype (Expr)) | |
7688 | and then Is_Class_Wide_Type (Etype (Original_Node (Expr))))) | |
996ae0b0 RK |
7689 | and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ) |
7690 | then | |
7691 | Wrong_Type (Expr, Target_Typ); | |
7692 | end if; | |
7693 | ||
7694 | -- If the target type is unconstrained, then we reset the type of | |
7695 | -- the result from the type of the expression. For other cases, the | |
7696 | -- actual subtype of the expression is the target type. | |
7697 | ||
7698 | if Is_Composite_Type (Target_Typ) | |
7699 | and then not Is_Constrained (Target_Typ) | |
7700 | then | |
7701 | Set_Etype (N, Etype (Expr)); | |
7702 | end if; | |
7703 | ||
7704 | Eval_Qualified_Expression (N); | |
7705 | end Resolve_Qualified_Expression; | |
7706 | ||
7707 | ------------------- | |
7708 | -- Resolve_Range -- | |
7709 | ------------------- | |
7710 | ||
7711 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is | |
7712 | L : constant Node_Id := Low_Bound (N); | |
7713 | H : constant Node_Id := High_Bound (N); | |
7714 | ||
bd29d519 AC |
7715 | function First_Last_Ref return Boolean; |
7716 | -- Returns True if N is of the form X'First .. X'Last where X is the | |
7717 | -- same entity for both attributes. | |
7718 | ||
7719 | -------------------- | |
7720 | -- First_Last_Ref -- | |
7721 | -------------------- | |
7722 | ||
7723 | function First_Last_Ref return Boolean is | |
7724 | Lorig : constant Node_Id := Original_Node (L); | |
7725 | Horig : constant Node_Id := Original_Node (H); | |
7726 | ||
7727 | begin | |
7728 | if Nkind (Lorig) = N_Attribute_Reference | |
7729 | and then Nkind (Horig) = N_Attribute_Reference | |
7730 | and then Attribute_Name (Lorig) = Name_First | |
7731 | and then Attribute_Name (Horig) = Name_Last | |
7732 | then | |
7733 | declare | |
7734 | PL : constant Node_Id := Prefix (Lorig); | |
7735 | PH : constant Node_Id := Prefix (Horig); | |
7736 | begin | |
7737 | if Is_Entity_Name (PL) | |
7738 | and then Is_Entity_Name (PH) | |
7739 | and then Entity (PL) = Entity (PH) | |
7740 | then | |
7741 | return True; | |
7742 | end if; | |
7743 | end; | |
7744 | end if; | |
7745 | ||
7746 | return False; | |
7747 | end First_Last_Ref; | |
7748 | ||
7749 | -- Start of processing for Resolve_Range | |
7750 | ||
996ae0b0 RK |
7751 | begin |
7752 | Set_Etype (N, Typ); | |
7753 | Resolve (L, Typ); | |
7754 | Resolve (H, Typ); | |
7755 | ||
bd29d519 AC |
7756 | -- Check for inappropriate range on unordered enumeration type |
7757 | ||
7758 | if Bad_Unordered_Enumeration_Reference (N, Typ) | |
7759 | ||
7760 | -- Exclude X'First .. X'Last if X is the same entity for both | |
7761 | ||
7762 | and then not First_Last_Ref | |
7763 | then | |
7764 | Error_Msg ("subrange of unordered enumeration type?", Sloc (N)); | |
498d1b80 AC |
7765 | end if; |
7766 | ||
996ae0b0 RK |
7767 | Check_Unset_Reference (L); |
7768 | Check_Unset_Reference (H); | |
7769 | ||
7770 | -- We have to check the bounds for being within the base range as | |
758c442c GD |
7771 | -- required for a non-static context. Normally this is automatic and |
7772 | -- done as part of evaluating expressions, but the N_Range node is an | |
7773 | -- exception, since in GNAT we consider this node to be a subexpression, | |
7774 | -- even though in Ada it is not. The circuit in Sem_Eval could check for | |
7775 | -- this, but that would put the test on the main evaluation path for | |
7776 | -- expressions. | |
996ae0b0 RK |
7777 | |
7778 | Check_Non_Static_Context (L); | |
7779 | Check_Non_Static_Context (H); | |
7780 | ||
b7d1f17f HK |
7781 | -- Check for an ambiguous range over character literals. This will |
7782 | -- happen with a membership test involving only literals. | |
7783 | ||
7784 | if Typ = Any_Character then | |
7785 | Ambiguous_Character (L); | |
7786 | Set_Etype (N, Any_Type); | |
7787 | return; | |
7788 | end if; | |
7789 | ||
fbf5a39b AC |
7790 | -- If bounds are static, constant-fold them, so size computations |
7791 | -- are identical between front-end and back-end. Do not perform this | |
7792 | -- transformation while analyzing generic units, as type information | |
7793 | -- would then be lost when reanalyzing the constant node in the | |
7794 | -- instance. | |
7795 | ||
7796 | if Is_Discrete_Type (Typ) and then Expander_Active then | |
7797 | if Is_OK_Static_Expression (L) then | |
7798 | Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L)); | |
7799 | end if; | |
7800 | ||
7801 | if Is_OK_Static_Expression (H) then | |
7802 | Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H)); | |
7803 | end if; | |
7804 | end if; | |
996ae0b0 RK |
7805 | end Resolve_Range; |
7806 | ||
7807 | -------------------------- | |
7808 | -- Resolve_Real_Literal -- | |
7809 | -------------------------- | |
7810 | ||
7811 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is | |
7812 | Actual_Typ : constant Entity_Id := Etype (N); | |
7813 | ||
7814 | begin | |
7815 | -- Special processing for fixed-point literals to make sure that the | |
7816 | -- value is an exact multiple of small where this is required. We | |
7817 | -- skip this for the universal real case, and also for generic types. | |
7818 | ||
7819 | if Is_Fixed_Point_Type (Typ) | |
7820 | and then Typ /= Universal_Fixed | |
7821 | and then Typ /= Any_Fixed | |
7822 | and then not Is_Generic_Type (Typ) | |
7823 | then | |
7824 | declare | |
7825 | Val : constant Ureal := Realval (N); | |
7826 | Cintr : constant Ureal := Val / Small_Value (Typ); | |
7827 | Cint : constant Uint := UR_Trunc (Cintr); | |
7828 | Den : constant Uint := Norm_Den (Cintr); | |
7829 | Stat : Boolean; | |
7830 | ||
7831 | begin | |
7832 | -- Case of literal is not an exact multiple of the Small | |
7833 | ||
7834 | if Den /= 1 then | |
7835 | ||
7836 | -- For a source program literal for a decimal fixed-point | |
7837 | -- type, this is statically illegal (RM 4.9(36)). | |
7838 | ||
7839 | if Is_Decimal_Fixed_Point_Type (Typ) | |
7840 | and then Actual_Typ = Universal_Real | |
7841 | and then Comes_From_Source (N) | |
7842 | then | |
7843 | Error_Msg_N ("value has extraneous low order digits", N); | |
7844 | end if; | |
7845 | ||
bc5f3720 RD |
7846 | -- Generate a warning if literal from source |
7847 | ||
7848 | if Is_Static_Expression (N) | |
7849 | and then Warn_On_Bad_Fixed_Value | |
7850 | then | |
7851 | Error_Msg_N | |
aa5147f0 | 7852 | ("?static fixed-point value is not a multiple of Small!", |
bc5f3720 RD |
7853 | N); |
7854 | end if; | |
7855 | ||
996ae0b0 RK |
7856 | -- Replace literal by a value that is the exact representation |
7857 | -- of a value of the type, i.e. a multiple of the small value, | |
7858 | -- by truncation, since Machine_Rounds is false for all GNAT | |
7859 | -- fixed-point types (RM 4.9(38)). | |
7860 | ||
7861 | Stat := Is_Static_Expression (N); | |
7862 | Rewrite (N, | |
7863 | Make_Real_Literal (Sloc (N), | |
7864 | Realval => Small_Value (Typ) * Cint)); | |
7865 | ||
7866 | Set_Is_Static_Expression (N, Stat); | |
7867 | end if; | |
7868 | ||
7869 | -- In all cases, set the corresponding integer field | |
7870 | ||
7871 | Set_Corresponding_Integer_Value (N, Cint); | |
7872 | end; | |
7873 | end if; | |
7874 | ||
7875 | -- Now replace the actual type by the expected type as usual | |
7876 | ||
7877 | Set_Etype (N, Typ); | |
7878 | Eval_Real_Literal (N); | |
7879 | end Resolve_Real_Literal; | |
7880 | ||
7881 | ----------------------- | |
7882 | -- Resolve_Reference -- | |
7883 | ----------------------- | |
7884 | ||
7885 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is | |
7886 | P : constant Node_Id := Prefix (N); | |
7887 | ||
7888 | begin | |
7889 | -- Replace general access with specific type | |
7890 | ||
7891 | if Ekind (Etype (N)) = E_Allocator_Type then | |
7892 | Set_Etype (N, Base_Type (Typ)); | |
7893 | end if; | |
7894 | ||
7895 | Resolve (P, Designated_Type (Etype (N))); | |
7896 | ||
7897 | -- If we are taking the reference of a volatile entity, then treat | |
7898 | -- it as a potential modification of this entity. This is much too | |
638e383e | 7899 | -- conservative, but is necessary because remove side effects can |
996ae0b0 RK |
7900 | -- result in transformations of normal assignments into reference |
7901 | -- sequences that otherwise fail to notice the modification. | |
7902 | ||
fbf5a39b | 7903 | if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then |
45fc7ddb | 7904 | Note_Possible_Modification (P, Sure => False); |
996ae0b0 RK |
7905 | end if; |
7906 | end Resolve_Reference; | |
7907 | ||
7908 | -------------------------------- | |
7909 | -- Resolve_Selected_Component -- | |
7910 | -------------------------------- | |
7911 | ||
7912 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is | |
7913 | Comp : Entity_Id; | |
7914 | Comp1 : Entity_Id := Empty; -- prevent junk warning | |
7915 | P : constant Node_Id := Prefix (N); | |
7916 | S : constant Node_Id := Selector_Name (N); | |
7917 | T : Entity_Id := Etype (P); | |
7918 | I : Interp_Index; | |
7919 | I1 : Interp_Index := 0; -- prevent junk warning | |
7920 | It : Interp; | |
7921 | It1 : Interp; | |
7922 | Found : Boolean; | |
7923 | ||
6510f4c9 GB |
7924 | function Init_Component return Boolean; |
7925 | -- Check whether this is the initialization of a component within an | |
fbf5a39b | 7926 | -- init proc (by assignment or call to another init proc). If true, |
6510f4c9 GB |
7927 | -- there is no need for a discriminant check. |
7928 | ||
7929 | -------------------- | |
7930 | -- Init_Component -- | |
7931 | -------------------- | |
7932 | ||
7933 | function Init_Component return Boolean is | |
7934 | begin | |
7935 | return Inside_Init_Proc | |
7936 | and then Nkind (Prefix (N)) = N_Identifier | |
7937 | and then Chars (Prefix (N)) = Name_uInit | |
7938 | and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative; | |
7939 | end Init_Component; | |
7940 | ||
7941 | -- Start of processing for Resolve_Selected_Component | |
7942 | ||
996ae0b0 RK |
7943 | begin |
7944 | if Is_Overloaded (P) then | |
7945 | ||
7946 | -- Use the context type to select the prefix that has a selector | |
7947 | -- of the correct name and type. | |
7948 | ||
7949 | Found := False; | |
7950 | Get_First_Interp (P, I, It); | |
7951 | ||
7952 | Search : while Present (It.Typ) loop | |
7953 | if Is_Access_Type (It.Typ) then | |
7954 | T := Designated_Type (It.Typ); | |
7955 | else | |
7956 | T := It.Typ; | |
7957 | end if; | |
7958 | ||
7959 | if Is_Record_Type (T) then | |
36fcf362 RD |
7960 | |
7961 | -- The visible components of a class-wide type are those of | |
7962 | -- the root type. | |
7963 | ||
7964 | if Is_Class_Wide_Type (T) then | |
7965 | T := Etype (T); | |
7966 | end if; | |
7967 | ||
996ae0b0 | 7968 | Comp := First_Entity (T); |
996ae0b0 | 7969 | while Present (Comp) loop |
996ae0b0 RK |
7970 | if Chars (Comp) = Chars (S) |
7971 | and then Covers (Etype (Comp), Typ) | |
7972 | then | |
7973 | if not Found then | |
7974 | Found := True; | |
7975 | I1 := I; | |
7976 | It1 := It; | |
7977 | Comp1 := Comp; | |
7978 | ||
7979 | else | |
7980 | It := Disambiguate (P, I1, I, Any_Type); | |
7981 | ||
7982 | if It = No_Interp then | |
7983 | Error_Msg_N | |
7984 | ("ambiguous prefix for selected component", N); | |
7985 | Set_Etype (N, Typ); | |
7986 | return; | |
7987 | ||
7988 | else | |
7989 | It1 := It; | |
7990 | ||
c8ef728f ES |
7991 | -- There may be an implicit dereference. Retrieve |
7992 | -- designated record type. | |
7993 | ||
7994 | if Is_Access_Type (It1.Typ) then | |
7995 | T := Designated_Type (It1.Typ); | |
7996 | else | |
7997 | T := It1.Typ; | |
7998 | end if; | |
7999 | ||
8000 | if Scope (Comp1) /= T then | |
996ae0b0 RK |
8001 | |
8002 | -- Resolution chooses the new interpretation. | |
8003 | -- Find the component with the right name. | |
8004 | ||
c8ef728f | 8005 | Comp1 := First_Entity (T); |
996ae0b0 RK |
8006 | while Present (Comp1) |
8007 | and then Chars (Comp1) /= Chars (S) | |
8008 | loop | |
8009 | Comp1 := Next_Entity (Comp1); | |
8010 | end loop; | |
8011 | end if; | |
8012 | ||
8013 | exit Search; | |
8014 | end if; | |
8015 | end if; | |
8016 | end if; | |
8017 | ||
8018 | Comp := Next_Entity (Comp); | |
8019 | end loop; | |
996ae0b0 RK |
8020 | end if; |
8021 | ||
8022 | Get_Next_Interp (I, It); | |
996ae0b0 RK |
8023 | end loop Search; |
8024 | ||
8025 | Resolve (P, It1.Typ); | |
8026 | Set_Etype (N, Typ); | |
aa180613 | 8027 | Set_Entity_With_Style_Check (S, Comp1); |
996ae0b0 RK |
8028 | |
8029 | else | |
fbf5a39b | 8030 | -- Resolve prefix with its type |
996ae0b0 RK |
8031 | |
8032 | Resolve (P, T); | |
8033 | end if; | |
8034 | ||
aa180613 RD |
8035 | -- Generate cross-reference. We needed to wait until full overloading |
8036 | -- resolution was complete to do this, since otherwise we can't tell if | |
01e17342 | 8037 | -- we are an lvalue or not. |
aa180613 RD |
8038 | |
8039 | if May_Be_Lvalue (N) then | |
8040 | Generate_Reference (Entity (S), S, 'm'); | |
8041 | else | |
8042 | Generate_Reference (Entity (S), S, 'r'); | |
8043 | end if; | |
8044 | ||
c8ef728f ES |
8045 | -- If prefix is an access type, the node will be transformed into an |
8046 | -- explicit dereference during expansion. The type of the node is the | |
8047 | -- designated type of that of the prefix. | |
996ae0b0 RK |
8048 | |
8049 | if Is_Access_Type (Etype (P)) then | |
996ae0b0 | 8050 | T := Designated_Type (Etype (P)); |
c8ef728f | 8051 | Check_Fully_Declared_Prefix (T, P); |
996ae0b0 RK |
8052 | else |
8053 | T := Etype (P); | |
8054 | end if; | |
8055 | ||
8056 | if Has_Discriminants (T) | |
964f13da | 8057 | and then Ekind_In (Entity (S), E_Component, E_Discriminant) |
996ae0b0 RK |
8058 | and then Present (Original_Record_Component (Entity (S))) |
8059 | and then Ekind (Original_Record_Component (Entity (S))) = E_Component | |
8060 | and then Present (Discriminant_Checking_Func | |
8061 | (Original_Record_Component (Entity (S)))) | |
8062 | and then not Discriminant_Checks_Suppressed (T) | |
6510f4c9 | 8063 | and then not Init_Component |
996ae0b0 RK |
8064 | then |
8065 | Set_Do_Discriminant_Check (N); | |
8066 | end if; | |
8067 | ||
8068 | if Ekind (Entity (S)) = E_Void then | |
8069 | Error_Msg_N ("premature use of component", S); | |
8070 | end if; | |
8071 | ||
8072 | -- If the prefix is a record conversion, this may be a renamed | |
8073 | -- discriminant whose bounds differ from those of the original | |
8074 | -- one, so we must ensure that a range check is performed. | |
8075 | ||
8076 | if Nkind (P) = N_Type_Conversion | |
8077 | and then Ekind (Entity (S)) = E_Discriminant | |
fbf5a39b | 8078 | and then Is_Discrete_Type (Typ) |
996ae0b0 RK |
8079 | then |
8080 | Set_Etype (N, Base_Type (Typ)); | |
8081 | end if; | |
8082 | ||
8083 | -- Note: No Eval processing is required, because the prefix is of a | |
8084 | -- record type, or protected type, and neither can possibly be static. | |
8085 | ||
c28408b7 RD |
8086 | -- If the array type is atomic, and is packed, and we are in a left side |
8087 | -- context, then this is worth a warning, since we have a situation | |
8088 | -- where the access to the component may cause extra read/writes of | |
8089 | -- the atomic array object, which could be considered unexpected. | |
8090 | ||
8091 | if Nkind (N) = N_Selected_Component | |
8092 | and then (Is_Atomic (T) | |
8093 | or else (Is_Entity_Name (Prefix (N)) | |
8094 | and then Is_Atomic (Entity (Prefix (N))))) | |
8095 | and then Is_Packed (T) | |
8096 | and then Is_LHS (N) | |
8097 | then | |
8098 | Error_Msg_N ("?assignment to component of packed atomic record", | |
8099 | Prefix (N)); | |
8100 | Error_Msg_N ("?\may cause unexpected accesses to atomic object", | |
8101 | Prefix (N)); | |
8102 | end if; | |
996ae0b0 RK |
8103 | end Resolve_Selected_Component; |
8104 | ||
8105 | ------------------- | |
8106 | -- Resolve_Shift -- | |
8107 | ------------------- | |
8108 | ||
8109 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is | |
8110 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8111 | L : constant Node_Id := Left_Opnd (N); | |
8112 | R : constant Node_Id := Right_Opnd (N); | |
8113 | ||
8114 | begin | |
8115 | -- We do the resolution using the base type, because intermediate values | |
8116 | -- in expressions always are of the base type, not a subtype of it. | |
8117 | ||
8118 | Resolve (L, B_Typ); | |
8119 | Resolve (R, Standard_Natural); | |
8120 | ||
8121 | Check_Unset_Reference (L); | |
8122 | Check_Unset_Reference (R); | |
8123 | ||
8124 | Set_Etype (N, B_Typ); | |
fbf5a39b | 8125 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
8126 | Eval_Shift (N); |
8127 | end Resolve_Shift; | |
8128 | ||
8129 | --------------------------- | |
8130 | -- Resolve_Short_Circuit -- | |
8131 | --------------------------- | |
8132 | ||
8133 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is | |
8134 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8135 | L : constant Node_Id := Left_Opnd (N); | |
8136 | R : constant Node_Id := Right_Opnd (N); | |
8137 | ||
8138 | begin | |
87dc09cb | 8139 | -- Why are the calls to Check_Order_Dependence commented out ??? |
996ae0b0 | 8140 | Resolve (L, B_Typ); |
bb481772 | 8141 | -- Check_Order_Dependence; -- For AI05-0144 |
996ae0b0 | 8142 | Resolve (R, B_Typ); |
bb481772 | 8143 | -- Check_Order_Dependence; -- For AI05-0144 |
996ae0b0 | 8144 | |
45fc7ddb HK |
8145 | -- Check for issuing warning for always False assert/check, this happens |
8146 | -- when assertions are turned off, in which case the pragma Assert/Check | |
36fcf362 RD |
8147 | -- was transformed into: |
8148 | ||
8149 | -- if False and then <condition> then ... | |
8150 | ||
8151 | -- and we detect this pattern | |
8152 | ||
8153 | if Warn_On_Assertion_Failure | |
8154 | and then Is_Entity_Name (R) | |
8155 | and then Entity (R) = Standard_False | |
8156 | and then Nkind (Parent (N)) = N_If_Statement | |
8157 | and then Nkind (N) = N_And_Then | |
8158 | and then Is_Entity_Name (L) | |
8159 | and then Entity (L) = Standard_False | |
8160 | then | |
8161 | declare | |
8162 | Orig : constant Node_Id := Original_Node (Parent (N)); | |
45fc7ddb | 8163 | |
36fcf362 RD |
8164 | begin |
8165 | if Nkind (Orig) = N_Pragma | |
26570b21 | 8166 | and then Pragma_Name (Orig) = Name_Assert |
36fcf362 RD |
8167 | then |
8168 | -- Don't want to warn if original condition is explicit False | |
8169 | ||
8170 | declare | |
8171 | Expr : constant Node_Id := | |
8172 | Original_Node | |
8173 | (Expression | |
8174 | (First (Pragma_Argument_Associations (Orig)))); | |
8175 | begin | |
8176 | if Is_Entity_Name (Expr) | |
8177 | and then Entity (Expr) = Standard_False | |
8178 | then | |
8179 | null; | |
8180 | else | |
51bf9bdf AC |
8181 | -- Issue warning. We do not want the deletion of the |
8182 | -- IF/AND-THEN to take this message with it. We achieve | |
8183 | -- this by making sure that the expanded code points to | |
8184 | -- the Sloc of the expression, not the original pragma. | |
8185 | ||
8186 | Error_Msg_N | |
e7c0dd39 | 8187 | ("?assertion would fail at run time!", |
51bf9bdf AC |
8188 | Expression |
8189 | (First (Pragma_Argument_Associations (Orig)))); | |
36fcf362 RD |
8190 | end if; |
8191 | end; | |
45fc7ddb HK |
8192 | |
8193 | -- Similar processing for Check pragma | |
8194 | ||
8195 | elsif Nkind (Orig) = N_Pragma | |
8196 | and then Pragma_Name (Orig) = Name_Check | |
8197 | then | |
8198 | -- Don't want to warn if original condition is explicit False | |
8199 | ||
8200 | declare | |
8201 | Expr : constant Node_Id := | |
8202 | Original_Node | |
8203 | (Expression | |
8204 | (Next (First | |
8205 | (Pragma_Argument_Associations (Orig))))); | |
8206 | begin | |
8207 | if Is_Entity_Name (Expr) | |
8208 | and then Entity (Expr) = Standard_False | |
8209 | then | |
8210 | null; | |
8211 | else | |
51bf9bdf | 8212 | Error_Msg_N |
e7c0dd39 | 8213 | ("?check would fail at run time!", |
51bf9bdf AC |
8214 | Expression |
8215 | (Last (Pragma_Argument_Associations (Orig)))); | |
45fc7ddb HK |
8216 | end if; |
8217 | end; | |
36fcf362 RD |
8218 | end if; |
8219 | end; | |
8220 | end if; | |
8221 | ||
8222 | -- Continue with processing of short circuit | |
8223 | ||
996ae0b0 RK |
8224 | Check_Unset_Reference (L); |
8225 | Check_Unset_Reference (R); | |
8226 | ||
8227 | Set_Etype (N, B_Typ); | |
8228 | Eval_Short_Circuit (N); | |
8229 | end Resolve_Short_Circuit; | |
8230 | ||
8231 | ------------------- | |
8232 | -- Resolve_Slice -- | |
8233 | ------------------- | |
8234 | ||
8235 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is | |
8236 | Name : constant Node_Id := Prefix (N); | |
8237 | Drange : constant Node_Id := Discrete_Range (N); | |
8238 | Array_Type : Entity_Id := Empty; | |
8239 | Index : Node_Id; | |
8240 | ||
8241 | begin | |
8242 | if Is_Overloaded (Name) then | |
8243 | ||
d81b4bfe TQ |
8244 | -- Use the context type to select the prefix that yields the correct |
8245 | -- array type. | |
996ae0b0 RK |
8246 | |
8247 | declare | |
8248 | I : Interp_Index; | |
8249 | I1 : Interp_Index := 0; | |
8250 | It : Interp; | |
8251 | P : constant Node_Id := Prefix (N); | |
8252 | Found : Boolean := False; | |
8253 | ||
8254 | begin | |
8255 | Get_First_Interp (P, I, It); | |
996ae0b0 | 8256 | while Present (It.Typ) loop |
996ae0b0 RK |
8257 | if (Is_Array_Type (It.Typ) |
8258 | and then Covers (Typ, It.Typ)) | |
8259 | or else (Is_Access_Type (It.Typ) | |
8260 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
8261 | and then Covers (Typ, Designated_Type (It.Typ))) | |
8262 | then | |
8263 | if Found then | |
8264 | It := Disambiguate (P, I1, I, Any_Type); | |
8265 | ||
8266 | if It = No_Interp then | |
8267 | Error_Msg_N ("ambiguous prefix for slicing", N); | |
8268 | Set_Etype (N, Typ); | |
8269 | return; | |
8270 | else | |
8271 | Found := True; | |
8272 | Array_Type := It.Typ; | |
8273 | I1 := I; | |
8274 | end if; | |
8275 | else | |
8276 | Found := True; | |
8277 | Array_Type := It.Typ; | |
8278 | I1 := I; | |
8279 | end if; | |
8280 | end if; | |
8281 | ||
8282 | Get_Next_Interp (I, It); | |
8283 | end loop; | |
8284 | end; | |
8285 | ||
8286 | else | |
8287 | Array_Type := Etype (Name); | |
8288 | end if; | |
8289 | ||
8290 | Resolve (Name, Array_Type); | |
8291 | ||
8292 | if Is_Access_Type (Array_Type) then | |
8293 | Apply_Access_Check (N); | |
8294 | Array_Type := Designated_Type (Array_Type); | |
8295 | ||
c8ef728f ES |
8296 | -- If the prefix is an access to an unconstrained array, we must use |
8297 | -- the actual subtype of the object to perform the index checks. The | |
8298 | -- object denoted by the prefix is implicit in the node, so we build | |
8299 | -- an explicit representation for it in order to compute the actual | |
8300 | -- subtype. | |
82c80734 RD |
8301 | |
8302 | if not Is_Constrained (Array_Type) then | |
8303 | Remove_Side_Effects (Prefix (N)); | |
8304 | ||
8305 | declare | |
8306 | Obj : constant Node_Id := | |
8307 | Make_Explicit_Dereference (Sloc (N), | |
8308 | Prefix => New_Copy_Tree (Prefix (N))); | |
8309 | begin | |
8310 | Set_Etype (Obj, Array_Type); | |
8311 | Set_Parent (Obj, Parent (N)); | |
8312 | Array_Type := Get_Actual_Subtype (Obj); | |
8313 | end; | |
8314 | end if; | |
8315 | ||
996ae0b0 | 8316 | elsif Is_Entity_Name (Name) |
6c994759 | 8317 | or else Nkind (Name) = N_Explicit_Dereference |
996ae0b0 RK |
8318 | or else (Nkind (Name) = N_Function_Call |
8319 | and then not Is_Constrained (Etype (Name))) | |
8320 | then | |
8321 | Array_Type := Get_Actual_Subtype (Name); | |
aa5147f0 ES |
8322 | |
8323 | -- If the name is a selected component that depends on discriminants, | |
8324 | -- build an actual subtype for it. This can happen only when the name | |
8325 | -- itself is overloaded; otherwise the actual subtype is created when | |
8326 | -- the selected component is analyzed. | |
8327 | ||
8328 | elsif Nkind (Name) = N_Selected_Component | |
8329 | and then Full_Analysis | |
8330 | and then Depends_On_Discriminant (First_Index (Array_Type)) | |
8331 | then | |
8332 | declare | |
8333 | Act_Decl : constant Node_Id := | |
8334 | Build_Actual_Subtype_Of_Component (Array_Type, Name); | |
8335 | begin | |
8336 | Insert_Action (N, Act_Decl); | |
8337 | Array_Type := Defining_Identifier (Act_Decl); | |
8338 | end; | |
d79e621a GD |
8339 | |
8340 | -- Maybe this should just be "else", instead of checking for the | |
8341 | -- specific case of slice??? This is needed for the case where | |
8342 | -- the prefix is an Image attribute, which gets expanded to a | |
8343 | -- slice, and so has a constrained subtype which we want to use | |
8344 | -- for the slice range check applied below (the range check won't | |
8345 | -- get done if the unconstrained subtype of the 'Image is used). | |
8346 | ||
8347 | elsif Nkind (Name) = N_Slice then | |
8348 | Array_Type := Etype (Name); | |
996ae0b0 RK |
8349 | end if; |
8350 | ||
8351 | -- If name was overloaded, set slice type correctly now | |
8352 | ||
8353 | Set_Etype (N, Array_Type); | |
8354 | ||
c8ef728f ES |
8355 | -- If the range is specified by a subtype mark, no resolution is |
8356 | -- necessary. Else resolve the bounds, and apply needed checks. | |
996ae0b0 RK |
8357 | |
8358 | if not Is_Entity_Name (Drange) then | |
8359 | Index := First_Index (Array_Type); | |
8360 | Resolve (Drange, Base_Type (Etype (Index))); | |
8361 | ||
0669bebe GB |
8362 | if Nkind (Drange) = N_Range |
8363 | ||
8364 | -- Do not apply the range check to nodes associated with the | |
8365 | -- frontend expansion of the dispatch table. We first check | |
8366 | -- if Ada.Tags is already loaded to void the addition of an | |
8367 | -- undesired dependence on such run-time unit. | |
8368 | ||
b7d1f17f | 8369 | and then |
1f110335 AC |
8370 | (not Tagged_Type_Expansion |
8371 | or else not | |
8372 | (RTU_Loaded (Ada_Tags) | |
8373 | and then Nkind (Prefix (N)) = N_Selected_Component | |
8374 | and then Present (Entity (Selector_Name (Prefix (N)))) | |
8375 | and then Entity (Selector_Name (Prefix (N))) = | |
8376 | RTE_Record_Component (RE_Prims_Ptr))) | |
0669bebe | 8377 | then |
996ae0b0 RK |
8378 | Apply_Range_Check (Drange, Etype (Index)); |
8379 | end if; | |
8380 | end if; | |
8381 | ||
8382 | Set_Slice_Subtype (N); | |
aa180613 RD |
8383 | |
8384 | if Nkind (Drange) = N_Range then | |
8385 | Warn_On_Suspicious_Index (Name, Low_Bound (Drange)); | |
8386 | Warn_On_Suspicious_Index (Name, High_Bound (Drange)); | |
8387 | end if; | |
8388 | ||
996ae0b0 | 8389 | Eval_Slice (N); |
996ae0b0 RK |
8390 | end Resolve_Slice; |
8391 | ||
8392 | ---------------------------- | |
8393 | -- Resolve_String_Literal -- | |
8394 | ---------------------------- | |
8395 | ||
8396 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is | |
8397 | C_Typ : constant Entity_Id := Component_Type (Typ); | |
8398 | R_Typ : constant Entity_Id := Root_Type (C_Typ); | |
8399 | Loc : constant Source_Ptr := Sloc (N); | |
8400 | Str : constant String_Id := Strval (N); | |
8401 | Strlen : constant Nat := String_Length (Str); | |
8402 | Subtype_Id : Entity_Id; | |
8403 | Need_Check : Boolean; | |
8404 | ||
8405 | begin | |
8406 | -- For a string appearing in a concatenation, defer creation of the | |
8407 | -- string_literal_subtype until the end of the resolution of the | |
c8ef728f ES |
8408 | -- concatenation, because the literal may be constant-folded away. This |
8409 | -- is a useful optimization for long concatenation expressions. | |
996ae0b0 | 8410 | |
c8ef728f | 8411 | -- If the string is an aggregate built for a single character (which |
996ae0b0 | 8412 | -- happens in a non-static context) or a is null string to which special |
c8ef728f ES |
8413 | -- checks may apply, we build the subtype. Wide strings must also get a |
8414 | -- string subtype if they come from a one character aggregate. Strings | |
996ae0b0 RK |
8415 | -- generated by attributes might be static, but it is often hard to |
8416 | -- determine whether the enclosing context is static, so we generate | |
8417 | -- subtypes for them as well, thus losing some rarer optimizations ??? | |
8418 | -- Same for strings that come from a static conversion. | |
8419 | ||
8420 | Need_Check := | |
8421 | (Strlen = 0 and then Typ /= Standard_String) | |
8422 | or else Nkind (Parent (N)) /= N_Op_Concat | |
8423 | or else (N /= Left_Opnd (Parent (N)) | |
8424 | and then N /= Right_Opnd (Parent (N))) | |
82c80734 RD |
8425 | or else ((Typ = Standard_Wide_String |
8426 | or else Typ = Standard_Wide_Wide_String) | |
996ae0b0 RK |
8427 | and then Nkind (Original_Node (N)) /= N_String_Literal); |
8428 | ||
d81b4bfe TQ |
8429 | -- If the resolving type is itself a string literal subtype, we can just |
8430 | -- reuse it, since there is no point in creating another. | |
996ae0b0 RK |
8431 | |
8432 | if Ekind (Typ) = E_String_Literal_Subtype then | |
8433 | Subtype_Id := Typ; | |
8434 | ||
8435 | elsif Nkind (Parent (N)) = N_Op_Concat | |
8436 | and then not Need_Check | |
45fc7ddb HK |
8437 | and then not Nkind_In (Original_Node (N), N_Character_Literal, |
8438 | N_Attribute_Reference, | |
8439 | N_Qualified_Expression, | |
8440 | N_Type_Conversion) | |
996ae0b0 RK |
8441 | then |
8442 | Subtype_Id := Typ; | |
8443 | ||
8444 | -- Otherwise we must create a string literal subtype. Note that the | |
8445 | -- whole idea of string literal subtypes is simply to avoid the need | |
8446 | -- for building a full fledged array subtype for each literal. | |
45fc7ddb | 8447 | |
996ae0b0 RK |
8448 | else |
8449 | Set_String_Literal_Subtype (N, Typ); | |
8450 | Subtype_Id := Etype (N); | |
8451 | end if; | |
8452 | ||
8453 | if Nkind (Parent (N)) /= N_Op_Concat | |
8454 | or else Need_Check | |
8455 | then | |
8456 | Set_Etype (N, Subtype_Id); | |
8457 | Eval_String_Literal (N); | |
8458 | end if; | |
8459 | ||
8460 | if Is_Limited_Composite (Typ) | |
8461 | or else Is_Private_Composite (Typ) | |
8462 | then | |
8463 | Error_Msg_N ("string literal not available for private array", N); | |
8464 | Set_Etype (N, Any_Type); | |
8465 | return; | |
8466 | end if; | |
8467 | ||
d81b4bfe TQ |
8468 | -- The validity of a null string has been checked in the call to |
8469 | -- Eval_String_Literal. | |
996ae0b0 RK |
8470 | |
8471 | if Strlen = 0 then | |
8472 | return; | |
8473 | ||
c8ef728f ES |
8474 | -- Always accept string literal with component type Any_Character, which |
8475 | -- occurs in error situations and in comparisons of literals, both of | |
8476 | -- which should accept all literals. | |
996ae0b0 RK |
8477 | |
8478 | elsif R_Typ = Any_Character then | |
8479 | return; | |
8480 | ||
f3d57416 RW |
8481 | -- If the type is bit-packed, then we always transform the string |
8482 | -- literal into a full fledged aggregate. | |
996ae0b0 RK |
8483 | |
8484 | elsif Is_Bit_Packed_Array (Typ) then | |
8485 | null; | |
8486 | ||
82c80734 | 8487 | -- Deal with cases of Wide_Wide_String, Wide_String, and String |
996ae0b0 RK |
8488 | |
8489 | else | |
82c80734 RD |
8490 | -- For Standard.Wide_Wide_String, or any other type whose component |
8491 | -- type is Standard.Wide_Wide_Character, we know that all the | |
996ae0b0 RK |
8492 | -- characters in the string must be acceptable, since the parser |
8493 | -- accepted the characters as valid character literals. | |
8494 | ||
82c80734 | 8495 | if R_Typ = Standard_Wide_Wide_Character then |
996ae0b0 RK |
8496 | null; |
8497 | ||
c8ef728f ES |
8498 | -- For the case of Standard.String, or any other type whose component |
8499 | -- type is Standard.Character, we must make sure that there are no | |
8500 | -- wide characters in the string, i.e. that it is entirely composed | |
8501 | -- of characters in range of type Character. | |
996ae0b0 | 8502 | |
c8ef728f ES |
8503 | -- If the string literal is the result of a static concatenation, the |
8504 | -- test has already been performed on the components, and need not be | |
8505 | -- repeated. | |
996ae0b0 RK |
8506 | |
8507 | elsif R_Typ = Standard_Character | |
8508 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8509 | then | |
8510 | for J in 1 .. Strlen loop | |
8511 | if not In_Character_Range (Get_String_Char (Str, J)) then | |
8512 | ||
8513 | -- If we are out of range, post error. This is one of the | |
8514 | -- very few places that we place the flag in the middle of | |
d81b4bfe TQ |
8515 | -- a token, right under the offending wide character. Not |
8516 | -- quite clear if this is right wrt wide character encoding | |
8517 | -- sequences, but it's only an error message! | |
996ae0b0 RK |
8518 | |
8519 | Error_Msg | |
82c80734 RD |
8520 | ("literal out of range of type Standard.Character", |
8521 | Source_Ptr (Int (Loc) + J)); | |
8522 | return; | |
8523 | end if; | |
8524 | end loop; | |
8525 | ||
8526 | -- For the case of Standard.Wide_String, or any other type whose | |
8527 | -- component type is Standard.Wide_Character, we must make sure that | |
8528 | -- there are no wide characters in the string, i.e. that it is | |
8529 | -- entirely composed of characters in range of type Wide_Character. | |
8530 | ||
8531 | -- If the string literal is the result of a static concatenation, | |
8532 | -- the test has already been performed on the components, and need | |
8533 | -- not be repeated. | |
8534 | ||
8535 | elsif R_Typ = Standard_Wide_Character | |
8536 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8537 | then | |
8538 | for J in 1 .. Strlen loop | |
8539 | if not In_Wide_Character_Range (Get_String_Char (Str, J)) then | |
8540 | ||
8541 | -- If we are out of range, post error. This is one of the | |
8542 | -- very few places that we place the flag in the middle of | |
8543 | -- a token, right under the offending wide character. | |
8544 | ||
8545 | -- This is not quite right, because characters in general | |
8546 | -- will take more than one character position ??? | |
8547 | ||
8548 | Error_Msg | |
8549 | ("literal out of range of type Standard.Wide_Character", | |
996ae0b0 RK |
8550 | Source_Ptr (Int (Loc) + J)); |
8551 | return; | |
8552 | end if; | |
8553 | end loop; | |
8554 | ||
8555 | -- If the root type is not a standard character, then we will convert | |
8556 | -- the string into an aggregate and will let the aggregate code do | |
82c80734 | 8557 | -- the checking. Standard Wide_Wide_Character is also OK here. |
996ae0b0 RK |
8558 | |
8559 | else | |
8560 | null; | |
996ae0b0 RK |
8561 | end if; |
8562 | ||
c8ef728f ES |
8563 | -- See if the component type of the array corresponding to the string |
8564 | -- has compile time known bounds. If yes we can directly check | |
8565 | -- whether the evaluation of the string will raise constraint error. | |
8566 | -- Otherwise we need to transform the string literal into the | |
8567 | -- corresponding character aggregate and let the aggregate | |
996ae0b0 RK |
8568 | -- code do the checking. |
8569 | ||
45fc7ddb HK |
8570 | if Is_Standard_Character_Type (R_Typ) then |
8571 | ||
996ae0b0 RK |
8572 | -- Check for the case of full range, where we are definitely OK |
8573 | ||
8574 | if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then | |
8575 | return; | |
8576 | end if; | |
8577 | ||
8578 | -- Here the range is not the complete base type range, so check | |
8579 | ||
8580 | declare | |
8581 | Comp_Typ_Lo : constant Node_Id := | |
8582 | Type_Low_Bound (Component_Type (Typ)); | |
8583 | Comp_Typ_Hi : constant Node_Id := | |
8584 | Type_High_Bound (Component_Type (Typ)); | |
8585 | ||
8586 | Char_Val : Uint; | |
8587 | ||
8588 | begin | |
8589 | if Compile_Time_Known_Value (Comp_Typ_Lo) | |
8590 | and then Compile_Time_Known_Value (Comp_Typ_Hi) | |
8591 | then | |
8592 | for J in 1 .. Strlen loop | |
8593 | Char_Val := UI_From_Int (Int (Get_String_Char (Str, J))); | |
8594 | ||
8595 | if Char_Val < Expr_Value (Comp_Typ_Lo) | |
8596 | or else Char_Val > Expr_Value (Comp_Typ_Hi) | |
8597 | then | |
8598 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 8599 | (N, "character out of range?", CE_Range_Check_Failed, |
996ae0b0 RK |
8600 | Loc => Source_Ptr (Int (Loc) + J)); |
8601 | end if; | |
8602 | end loop; | |
8603 | ||
8604 | return; | |
8605 | end if; | |
8606 | end; | |
8607 | end if; | |
8608 | end if; | |
8609 | ||
8610 | -- If we got here we meed to transform the string literal into the | |
8611 | -- equivalent qualified positional array aggregate. This is rather | |
8612 | -- heavy artillery for this situation, but it is hard work to avoid. | |
8613 | ||
8614 | declare | |
fbf5a39b | 8615 | Lits : constant List_Id := New_List; |
996ae0b0 RK |
8616 | P : Source_Ptr := Loc + 1; |
8617 | C : Char_Code; | |
8618 | ||
8619 | begin | |
c8ef728f ES |
8620 | -- Build the character literals, we give them source locations that |
8621 | -- correspond to the string positions, which is a bit tricky given | |
8622 | -- the possible presence of wide character escape sequences. | |
996ae0b0 RK |
8623 | |
8624 | for J in 1 .. Strlen loop | |
8625 | C := Get_String_Char (Str, J); | |
8626 | Set_Character_Literal_Name (C); | |
8627 | ||
8628 | Append_To (Lits, | |
82c80734 RD |
8629 | Make_Character_Literal (P, |
8630 | Chars => Name_Find, | |
8631 | Char_Literal_Value => UI_From_CC (C))); | |
996ae0b0 RK |
8632 | |
8633 | if In_Character_Range (C) then | |
8634 | P := P + 1; | |
8635 | ||
8636 | -- Should we have a call to Skip_Wide here ??? | |
8637 | -- ??? else | |
8638 | -- Skip_Wide (P); | |
8639 | ||
8640 | end if; | |
8641 | end loop; | |
8642 | ||
8643 | Rewrite (N, | |
8644 | Make_Qualified_Expression (Loc, | |
8645 | Subtype_Mark => New_Reference_To (Typ, Loc), | |
8646 | Expression => | |
8647 | Make_Aggregate (Loc, Expressions => Lits))); | |
8648 | ||
8649 | Analyze_And_Resolve (N, Typ); | |
8650 | end; | |
8651 | end Resolve_String_Literal; | |
8652 | ||
8653 | ----------------------------- | |
8654 | -- Resolve_Subprogram_Info -- | |
8655 | ----------------------------- | |
8656 | ||
8657 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is | |
8658 | begin | |
8659 | Set_Etype (N, Typ); | |
8660 | end Resolve_Subprogram_Info; | |
8661 | ||
8662 | ----------------------------- | |
8663 | -- Resolve_Type_Conversion -- | |
8664 | ----------------------------- | |
8665 | ||
8666 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is | |
4b2d2c13 AC |
8667 | Conv_OK : constant Boolean := Conversion_OK (N); |
8668 | Operand : constant Node_Id := Expression (N); | |
b7d1f17f HK |
8669 | Operand_Typ : constant Entity_Id := Etype (Operand); |
8670 | Target_Typ : constant Entity_Id := Etype (N); | |
996ae0b0 | 8671 | Rop : Node_Id; |
fbf5a39b AC |
8672 | Orig_N : Node_Id; |
8673 | Orig_T : Node_Id; | |
996ae0b0 | 8674 | |
ae2aa109 AC |
8675 | Test_Redundant : Boolean := Warn_On_Redundant_Constructs; |
8676 | -- Set to False to suppress cases where we want to suppress the test | |
8677 | -- for redundancy to avoid possible false positives on this warning. | |
8678 | ||
996ae0b0 | 8679 | begin |
996ae0b0 | 8680 | if not Conv_OK |
b7d1f17f | 8681 | and then not Valid_Conversion (N, Target_Typ, Operand) |
996ae0b0 RK |
8682 | then |
8683 | return; | |
8684 | end if; | |
8685 | ||
ae2aa109 AC |
8686 | -- If the Operand Etype is Universal_Fixed, then the conversion is |
8687 | -- never redundant. We need this check because by the time we have | |
8688 | -- finished the rather complex transformation, the conversion looks | |
8689 | -- redundant when it is not. | |
8690 | ||
8691 | if Operand_Typ = Universal_Fixed then | |
8692 | Test_Redundant := False; | |
8693 | ||
8694 | -- If the operand is marked as Any_Fixed, then special processing is | |
8695 | -- required. This is also a case where we suppress the test for a | |
8696 | -- redundant conversion, since most certainly it is not redundant. | |
8697 | ||
8698 | elsif Operand_Typ = Any_Fixed then | |
8699 | Test_Redundant := False; | |
996ae0b0 RK |
8700 | |
8701 | -- Mixed-mode operation involving a literal. Context must be a fixed | |
8702 | -- type which is applied to the literal subsequently. | |
8703 | ||
8704 | if Is_Fixed_Point_Type (Typ) then | |
8705 | Set_Etype (Operand, Universal_Real); | |
8706 | ||
8707 | elsif Is_Numeric_Type (Typ) | |
45fc7ddb | 8708 | and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide) |
996ae0b0 | 8709 | and then (Etype (Right_Opnd (Operand)) = Universal_Real |
45fc7ddb HK |
8710 | or else |
8711 | Etype (Left_Opnd (Operand)) = Universal_Real) | |
996ae0b0 | 8712 | then |
a77842bd TQ |
8713 | -- Return if expression is ambiguous |
8714 | ||
996ae0b0 | 8715 | if Unique_Fixed_Point_Type (N) = Any_Type then |
a77842bd | 8716 | return; |
82c80734 | 8717 | |
a77842bd TQ |
8718 | -- If nothing else, the available fixed type is Duration |
8719 | ||
8720 | else | |
996ae0b0 RK |
8721 | Set_Etype (Operand, Standard_Duration); |
8722 | end if; | |
8723 | ||
bc5f3720 | 8724 | -- Resolve the real operand with largest available precision |
9ebe3743 | 8725 | |
996ae0b0 RK |
8726 | if Etype (Right_Opnd (Operand)) = Universal_Real then |
8727 | Rop := New_Copy_Tree (Right_Opnd (Operand)); | |
8728 | else | |
8729 | Rop := New_Copy_Tree (Left_Opnd (Operand)); | |
8730 | end if; | |
8731 | ||
9ebe3743 | 8732 | Resolve (Rop, Universal_Real); |
996ae0b0 | 8733 | |
82c80734 RD |
8734 | -- If the operand is a literal (it could be a non-static and |
8735 | -- illegal exponentiation) check whether the use of Duration | |
8736 | -- is potentially inaccurate. | |
8737 | ||
8738 | if Nkind (Rop) = N_Real_Literal | |
8739 | and then Realval (Rop) /= Ureal_0 | |
996ae0b0 RK |
8740 | and then abs (Realval (Rop)) < Delta_Value (Standard_Duration) |
8741 | then | |
aa180613 | 8742 | Error_Msg_N |
aa5147f0 ES |
8743 | ("?universal real operand can only " & |
8744 | "be interpreted as Duration!", | |
aa180613 RD |
8745 | Rop); |
8746 | Error_Msg_N | |
aa5147f0 | 8747 | ("\?precision will be lost in the conversion!", Rop); |
996ae0b0 RK |
8748 | end if; |
8749 | ||
891a6e79 AC |
8750 | elsif Is_Numeric_Type (Typ) |
8751 | and then Nkind (Operand) in N_Op | |
8752 | and then Unique_Fixed_Point_Type (N) /= Any_Type | |
8753 | then | |
8754 | Set_Etype (Operand, Standard_Duration); | |
8755 | ||
996ae0b0 RK |
8756 | else |
8757 | Error_Msg_N ("invalid context for mixed mode operation", N); | |
8758 | Set_Etype (Operand, Any_Type); | |
8759 | return; | |
8760 | end if; | |
8761 | end if; | |
8762 | ||
fbf5a39b | 8763 | Resolve (Operand); |
996ae0b0 RK |
8764 | |
8765 | -- Note: we do the Eval_Type_Conversion call before applying the | |
d81b4bfe TQ |
8766 | -- required checks for a subtype conversion. This is important, since |
8767 | -- both are prepared under certain circumstances to change the type | |
8768 | -- conversion to a constraint error node, but in the case of | |
8769 | -- Eval_Type_Conversion this may reflect an illegality in the static | |
8770 | -- case, and we would miss the illegality (getting only a warning | |
8771 | -- message), if we applied the type conversion checks first. | |
996ae0b0 RK |
8772 | |
8773 | Eval_Type_Conversion (N); | |
8774 | ||
d81b4bfe TQ |
8775 | -- Even when evaluation is not possible, we may be able to simplify the |
8776 | -- conversion or its expression. This needs to be done before applying | |
8777 | -- checks, since otherwise the checks may use the original expression | |
8778 | -- and defeat the simplifications. This is specifically the case for | |
8779 | -- elimination of the floating-point Truncation attribute in | |
8780 | -- float-to-int conversions. | |
0669bebe GB |
8781 | |
8782 | Simplify_Type_Conversion (N); | |
8783 | ||
d81b4bfe TQ |
8784 | -- If after evaluation we still have a type conversion, then we may need |
8785 | -- to apply checks required for a subtype conversion. | |
996ae0b0 RK |
8786 | |
8787 | -- Skip these type conversion checks if universal fixed operands | |
8788 | -- operands involved, since range checks are handled separately for | |
8789 | -- these cases (in the appropriate Expand routines in unit Exp_Fixd). | |
8790 | ||
8791 | if Nkind (N) = N_Type_Conversion | |
b7d1f17f HK |
8792 | and then not Is_Generic_Type (Root_Type (Target_Typ)) |
8793 | and then Target_Typ /= Universal_Fixed | |
8794 | and then Operand_Typ /= Universal_Fixed | |
996ae0b0 RK |
8795 | then |
8796 | Apply_Type_Conversion_Checks (N); | |
8797 | end if; | |
8798 | ||
d81b4bfe TQ |
8799 | -- Issue warning for conversion of simple object to its own type. We |
8800 | -- have to test the original nodes, since they may have been rewritten | |
8801 | -- by various optimizations. | |
fbf5a39b AC |
8802 | |
8803 | Orig_N := Original_Node (N); | |
996ae0b0 | 8804 | |
ae2aa109 AC |
8805 | -- Here we test for a redundant conversion if the warning mode is |
8806 | -- active (and was not locally reset), and we have a type conversion | |
8807 | -- from source not appearing in a generic instance. | |
8808 | ||
8809 | if Test_Redundant | |
fbf5a39b | 8810 | and then Nkind (Orig_N) = N_Type_Conversion |
ae2aa109 | 8811 | and then Comes_From_Source (Orig_N) |
5453d5bd | 8812 | and then not In_Instance |
996ae0b0 | 8813 | then |
fbf5a39b | 8814 | Orig_N := Original_Node (Expression (Orig_N)); |
b7d1f17f | 8815 | Orig_T := Target_Typ; |
fbf5a39b AC |
8816 | |
8817 | -- If the node is part of a larger expression, the Target_Type | |
8818 | -- may not be the original type of the node if the context is a | |
8819 | -- condition. Recover original type to see if conversion is needed. | |
8820 | ||
8821 | if Is_Boolean_Type (Orig_T) | |
8822 | and then Nkind (Parent (N)) in N_Op | |
8823 | then | |
8824 | Orig_T := Etype (Parent (N)); | |
8825 | end if; | |
8826 | ||
ae2aa109 AC |
8827 | -- if we have an entity name, then give the warning if the entity |
8828 | -- is the right type, or if it is a loop parameter covered by the | |
8829 | -- original type (that's needed because loop parameters have an | |
8830 | -- odd subtype coming from the bounds). | |
8831 | ||
8832 | if (Is_Entity_Name (Orig_N) | |
8833 | and then | |
8834 | (Etype (Entity (Orig_N)) = Orig_T | |
8835 | or else | |
8836 | (Ekind (Entity (Orig_N)) = E_Loop_Parameter | |
477bd732 | 8837 | and then Covers (Orig_T, Etype (Entity (Orig_N)))))) |
ae2aa109 | 8838 | |
477bd732 | 8839 | -- If not an entity, then type of expression must match |
ae2aa109 AC |
8840 | |
8841 | or else Etype (Orig_N) = Orig_T | |
fbf5a39b | 8842 | then |
4b2d2c13 AC |
8843 | -- One more check, do not give warning if the analyzed conversion |
8844 | -- has an expression with non-static bounds, and the bounds of the | |
8845 | -- target are static. This avoids junk warnings in cases where the | |
8846 | -- conversion is necessary to establish staticness, for example in | |
8847 | -- a case statement. | |
8848 | ||
8849 | if not Is_OK_Static_Subtype (Operand_Typ) | |
8850 | and then Is_OK_Static_Subtype (Target_Typ) | |
8851 | then | |
8852 | null; | |
8853 | ||
9db0b232 AC |
8854 | -- Finally, if this type conversion occurs in a context that |
8855 | -- requires a prefix, and the expression is a qualified | |
8856 | -- expression, then the type conversion is not redundant, | |
8857 | -- because a qualified expression is not a prefix, whereas a | |
8858 | -- type conversion is. For example, "X := T'(Funx(...)).Y;" is | |
8859 | -- illegal. because a selected component requires a prefix, but | |
8860 | -- a type conversion makes it legal: "X := T(T'(Funx(...))).Y;" | |
8861 | -- In Ada 2012, a qualified expression is a name, so this idiom is | |
8862 | -- no longer needed, but we still suppress the warning because it | |
8863 | -- seems unfriendly for warnings to pop up when you switch to the | |
8864 | -- newer language version. | |
be257e99 AC |
8865 | |
8866 | elsif Nkind (Orig_N) = N_Qualified_Expression | |
f5d96d00 AC |
8867 | and then Nkind_In (Parent (N), N_Attribute_Reference, |
8868 | N_Indexed_Component, | |
8869 | N_Selected_Component, | |
8870 | N_Slice, | |
8871 | N_Explicit_Dereference) | |
be257e99 AC |
8872 | then |
8873 | null; | |
8874 | ||
ae2aa109 AC |
8875 | -- Here we give the redundant conversion warning. If it is an |
8876 | -- entity, give the name of the entity in the message. If not, | |
8877 | -- just mention the expression. | |
4b2d2c13 AC |
8878 | |
8879 | else | |
ae2aa109 AC |
8880 | if Is_Entity_Name (Orig_N) then |
8881 | Error_Msg_Node_2 := Orig_T; | |
8882 | Error_Msg_NE -- CODEFIX | |
8883 | ("?redundant conversion, & is of type &!", | |
8884 | N, Entity (Orig_N)); | |
8885 | else | |
8886 | Error_Msg_NE | |
8887 | ("?redundant conversion, expression is of type&!", | |
8888 | N, Orig_T); | |
8889 | end if; | |
4b2d2c13 | 8890 | end if; |
fbf5a39b | 8891 | end if; |
996ae0b0 | 8892 | end if; |
758c442c | 8893 | |
b7d1f17f | 8894 | -- Ada 2005 (AI-251): Handle class-wide interface type conversions. |
0669bebe GB |
8895 | -- No need to perform any interface conversion if the type of the |
8896 | -- expression coincides with the target type. | |
758c442c | 8897 | |
0669bebe GB |
8898 | if Ada_Version >= Ada_05 |
8899 | and then Expander_Active | |
b7d1f17f | 8900 | and then Operand_Typ /= Target_Typ |
0669bebe | 8901 | then |
b7d1f17f HK |
8902 | declare |
8903 | Opnd : Entity_Id := Operand_Typ; | |
8904 | Target : Entity_Id := Target_Typ; | |
758c442c | 8905 | |
b7d1f17f HK |
8906 | begin |
8907 | if Is_Access_Type (Opnd) then | |
841dd0f5 | 8908 | Opnd := Designated_Type (Opnd); |
1420b484 JM |
8909 | end if; |
8910 | ||
b7d1f17f | 8911 | if Is_Access_Type (Target_Typ) then |
841dd0f5 | 8912 | Target := Designated_Type (Target); |
4197ae1e | 8913 | end if; |
c8ef728f | 8914 | |
b7d1f17f HK |
8915 | if Opnd = Target then |
8916 | null; | |
c8ef728f | 8917 | |
b7d1f17f | 8918 | -- Conversion from interface type |
ea985d95 | 8919 | |
b7d1f17f | 8920 | elsif Is_Interface (Opnd) then |
ea985d95 | 8921 | |
b7d1f17f | 8922 | -- Ada 2005 (AI-217): Handle entities from limited views |
aa180613 | 8923 | |
b7d1f17f HK |
8924 | if From_With_Type (Opnd) then |
8925 | Error_Msg_Qual_Level := 99; | |
305caf42 AC |
8926 | Error_Msg_NE -- CODEFIX |
8927 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
8928 | Cunit_Entity (Get_Source_Unit (Base_Type (Opnd)))); |
8929 | Error_Msg_N | |
8930 | ("type conversions require visibility of the full view", | |
8931 | N); | |
aa180613 | 8932 | |
aa5147f0 ES |
8933 | elsif From_With_Type (Target) |
8934 | and then not | |
8935 | (Is_Access_Type (Target_Typ) | |
8936 | and then Present (Non_Limited_View (Etype (Target)))) | |
8937 | then | |
b7d1f17f | 8938 | Error_Msg_Qual_Level := 99; |
305caf42 AC |
8939 | Error_Msg_NE -- CODEFIX |
8940 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
8941 | Cunit_Entity (Get_Source_Unit (Base_Type (Target)))); |
8942 | Error_Msg_N | |
8943 | ("type conversions require visibility of the full view", | |
8944 | N); | |
aa180613 | 8945 | |
b7d1f17f HK |
8946 | else |
8947 | Expand_Interface_Conversion (N, Is_Static => False); | |
8948 | end if; | |
8949 | ||
8950 | -- Conversion to interface type | |
8951 | ||
8952 | elsif Is_Interface (Target) then | |
8953 | ||
8954 | -- Handle subtypes | |
8955 | ||
8a95f4e8 | 8956 | if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then |
b7d1f17f HK |
8957 | Opnd := Etype (Opnd); |
8958 | end if; | |
8959 | ||
8960 | if not Interface_Present_In_Ancestor | |
8961 | (Typ => Opnd, | |
8962 | Iface => Target) | |
8963 | then | |
8964 | if Is_Class_Wide_Type (Opnd) then | |
8965 | ||
8966 | -- The static analysis is not enough to know if the | |
8967 | -- interface is implemented or not. Hence we must pass | |
8968 | -- the work to the expander to generate code to evaluate | |
e7c0dd39 | 8969 | -- the conversion at run time. |
b7d1f17f HK |
8970 | |
8971 | Expand_Interface_Conversion (N, Is_Static => False); | |
8972 | ||
8973 | else | |
8974 | Error_Msg_Name_1 := Chars (Etype (Target)); | |
8975 | Error_Msg_Name_2 := Chars (Opnd); | |
8976 | Error_Msg_N | |
8977 | ("wrong interface conversion (% is not a progenitor " & | |
8978 | "of %)", N); | |
8979 | end if; | |
8980 | ||
8981 | else | |
8982 | Expand_Interface_Conversion (N); | |
8983 | end if; | |
8984 | end if; | |
8985 | end; | |
758c442c | 8986 | end if; |
996ae0b0 RK |
8987 | end Resolve_Type_Conversion; |
8988 | ||
8989 | ---------------------- | |
8990 | -- Resolve_Unary_Op -- | |
8991 | ---------------------- | |
8992 | ||
8993 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
8994 | B_Typ : constant Entity_Id := Base_Type (Typ); |
8995 | R : constant Node_Id := Right_Opnd (N); | |
8996 | OK : Boolean; | |
8997 | Lo : Uint; | |
8998 | Hi : Uint; | |
996ae0b0 RK |
8999 | |
9000 | begin | |
b7d1f17f | 9001 | -- Deal with intrinsic unary operators |
996ae0b0 | 9002 | |
fbf5a39b AC |
9003 | if Comes_From_Source (N) |
9004 | and then Ekind (Entity (N)) = E_Function | |
9005 | and then Is_Imported (Entity (N)) | |
9006 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
9007 | then | |
9008 | Resolve_Intrinsic_Unary_Operator (N, Typ); | |
9009 | return; | |
9010 | end if; | |
9011 | ||
0669bebe GB |
9012 | -- Deal with universal cases |
9013 | ||
996ae0b0 | 9014 | if Etype (R) = Universal_Integer |
0669bebe GB |
9015 | or else |
9016 | Etype (R) = Universal_Real | |
996ae0b0 RK |
9017 | then |
9018 | Check_For_Visible_Operator (N, B_Typ); | |
9019 | end if; | |
9020 | ||
9021 | Set_Etype (N, B_Typ); | |
9022 | Resolve (R, B_Typ); | |
fbf5a39b | 9023 | |
9ebe3743 HK |
9024 | -- Generate warning for expressions like abs (x mod 2) |
9025 | ||
9026 | if Warn_On_Redundant_Constructs | |
9027 | and then Nkind (N) = N_Op_Abs | |
9028 | then | |
9029 | Determine_Range (Right_Opnd (N), OK, Lo, Hi); | |
9030 | ||
9031 | if OK and then Hi >= Lo and then Lo >= 0 then | |
305caf42 | 9032 | Error_Msg_N -- CODEFIX |
9ebe3743 HK |
9033 | ("?abs applied to known non-negative value has no effect", N); |
9034 | end if; | |
9035 | end if; | |
9036 | ||
0669bebe GB |
9037 | -- Deal with reference generation |
9038 | ||
996ae0b0 | 9039 | Check_Unset_Reference (R); |
fbf5a39b | 9040 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
9041 | Eval_Unary_Op (N); |
9042 | ||
9043 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
9044 | -- and perhaps the Resolve routines should be separated for the various | |
9045 | -- arithmetic operations, since they will need different processing ??? | |
9046 | ||
9047 | if Nkind (N) in N_Op then | |
9048 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 9049 | Enable_Overflow_Check (N); |
996ae0b0 RK |
9050 | end if; |
9051 | end if; | |
0669bebe | 9052 | |
d81b4bfe TQ |
9053 | -- Generate warning for expressions like -5 mod 3 for integers. No need |
9054 | -- to worry in the floating-point case, since parens do not affect the | |
9055 | -- result so there is no point in giving in a warning. | |
0669bebe GB |
9056 | |
9057 | declare | |
9058 | Norig : constant Node_Id := Original_Node (N); | |
9059 | Rorig : Node_Id; | |
9060 | Val : Uint; | |
9061 | HB : Uint; | |
9062 | LB : Uint; | |
9063 | Lval : Uint; | |
9064 | Opnd : Node_Id; | |
9065 | ||
9066 | begin | |
9067 | if Warn_On_Questionable_Missing_Parens | |
9068 | and then Comes_From_Source (Norig) | |
9069 | and then Is_Integer_Type (Typ) | |
9070 | and then Nkind (Norig) = N_Op_Minus | |
9071 | then | |
9072 | Rorig := Original_Node (Right_Opnd (Norig)); | |
9073 | ||
9074 | -- We are looking for cases where the right operand is not | |
f3d57416 | 9075 | -- parenthesized, and is a binary operator, multiply, divide, or |
0669bebe GB |
9076 | -- mod. These are the cases where the grouping can affect results. |
9077 | ||
9078 | if Paren_Count (Rorig) = 0 | |
45fc7ddb | 9079 | and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide) |
0669bebe GB |
9080 | then |
9081 | -- For mod, we always give the warning, since the value is | |
9082 | -- affected by the parenthesization (e.g. (-5) mod 315 /= | |
d81b4bfe | 9083 | -- -(5 mod 315)). But for the other cases, the only concern is |
0669bebe GB |
9084 | -- overflow, e.g. for the case of 8 big signed (-(2 * 64) |
9085 | -- overflows, but (-2) * 64 does not). So we try to give the | |
9086 | -- message only when overflow is possible. | |
9087 | ||
9088 | if Nkind (Rorig) /= N_Op_Mod | |
9089 | and then Compile_Time_Known_Value (R) | |
9090 | then | |
9091 | Val := Expr_Value (R); | |
9092 | ||
9093 | if Compile_Time_Known_Value (Type_High_Bound (Typ)) then | |
9094 | HB := Expr_Value (Type_High_Bound (Typ)); | |
9095 | else | |
9096 | HB := Expr_Value (Type_High_Bound (Base_Type (Typ))); | |
9097 | end if; | |
9098 | ||
9099 | if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then | |
9100 | LB := Expr_Value (Type_Low_Bound (Typ)); | |
9101 | else | |
9102 | LB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); | |
9103 | end if; | |
9104 | ||
d81b4bfe TQ |
9105 | -- Note that the test below is deliberately excluding the |
9106 | -- largest negative number, since that is a potentially | |
0669bebe GB |
9107 | -- troublesome case (e.g. -2 * x, where the result is the |
9108 | -- largest negative integer has an overflow with 2 * x). | |
9109 | ||
9110 | if Val > LB and then Val <= HB then | |
9111 | return; | |
9112 | end if; | |
9113 | end if; | |
9114 | ||
9115 | -- For the multiplication case, the only case we have to worry | |
9116 | -- about is when (-a)*b is exactly the largest negative number | |
9117 | -- so that -(a*b) can cause overflow. This can only happen if | |
9118 | -- a is a power of 2, and more generally if any operand is a | |
9119 | -- constant that is not a power of 2, then the parentheses | |
9120 | -- cannot affect whether overflow occurs. We only bother to | |
9121 | -- test the left most operand | |
9122 | ||
9123 | -- Loop looking at left operands for one that has known value | |
9124 | ||
9125 | Opnd := Rorig; | |
9126 | Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop | |
9127 | if Compile_Time_Known_Value (Left_Opnd (Opnd)) then | |
9128 | Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd))); | |
9129 | ||
9130 | -- Operand value of 0 or 1 skips warning | |
9131 | ||
9132 | if Lval <= 1 then | |
9133 | return; | |
9134 | ||
9135 | -- Otherwise check power of 2, if power of 2, warn, if | |
9136 | -- anything else, skip warning. | |
9137 | ||
9138 | else | |
9139 | while Lval /= 2 loop | |
9140 | if Lval mod 2 = 1 then | |
9141 | return; | |
9142 | else | |
9143 | Lval := Lval / 2; | |
9144 | end if; | |
9145 | end loop; | |
9146 | ||
9147 | exit Opnd_Loop; | |
9148 | end if; | |
9149 | end if; | |
9150 | ||
9151 | -- Keep looking at left operands | |
9152 | ||
9153 | Opnd := Left_Opnd (Opnd); | |
9154 | end loop Opnd_Loop; | |
9155 | ||
9156 | -- For rem or "/" we can only have a problematic situation | |
9157 | -- if the divisor has a value of minus one or one. Otherwise | |
9158 | -- overflow is impossible (divisor > 1) or we have a case of | |
9159 | -- division by zero in any case. | |
9160 | ||
45fc7ddb | 9161 | if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem) |
0669bebe GB |
9162 | and then Compile_Time_Known_Value (Right_Opnd (Rorig)) |
9163 | and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1 | |
9164 | then | |
9165 | return; | |
9166 | end if; | |
9167 | ||
9168 | -- If we fall through warning should be issued | |
9169 | ||
ed2233dc | 9170 | Error_Msg_N |
aa5147f0 | 9171 | ("?unary minus expression should be parenthesized here!", N); |
0669bebe GB |
9172 | end if; |
9173 | end if; | |
9174 | end; | |
996ae0b0 RK |
9175 | end Resolve_Unary_Op; |
9176 | ||
9177 | ---------------------------------- | |
9178 | -- Resolve_Unchecked_Expression -- | |
9179 | ---------------------------------- | |
9180 | ||
9181 | procedure Resolve_Unchecked_Expression | |
9182 | (N : Node_Id; | |
9183 | Typ : Entity_Id) | |
9184 | is | |
9185 | begin | |
9186 | Resolve (Expression (N), Typ, Suppress => All_Checks); | |
9187 | Set_Etype (N, Typ); | |
9188 | end Resolve_Unchecked_Expression; | |
9189 | ||
9190 | --------------------------------------- | |
9191 | -- Resolve_Unchecked_Type_Conversion -- | |
9192 | --------------------------------------- | |
9193 | ||
9194 | procedure Resolve_Unchecked_Type_Conversion | |
9195 | (N : Node_Id; | |
9196 | Typ : Entity_Id) | |
9197 | is | |
07fc65c4 GB |
9198 | pragma Warnings (Off, Typ); |
9199 | ||
996ae0b0 RK |
9200 | Operand : constant Node_Id := Expression (N); |
9201 | Opnd_Type : constant Entity_Id := Etype (Operand); | |
9202 | ||
9203 | begin | |
a77842bd | 9204 | -- Resolve operand using its own type |
996ae0b0 RK |
9205 | |
9206 | Resolve (Operand, Opnd_Type); | |
9207 | Eval_Unchecked_Conversion (N); | |
996ae0b0 RK |
9208 | end Resolve_Unchecked_Type_Conversion; |
9209 | ||
9210 | ------------------------------ | |
9211 | -- Rewrite_Operator_As_Call -- | |
9212 | ------------------------------ | |
9213 | ||
9214 | procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is | |
fbf5a39b AC |
9215 | Loc : constant Source_Ptr := Sloc (N); |
9216 | Actuals : constant List_Id := New_List; | |
996ae0b0 RK |
9217 | New_N : Node_Id; |
9218 | ||
9219 | begin | |
9220 | if Nkind (N) in N_Binary_Op then | |
9221 | Append (Left_Opnd (N), Actuals); | |
9222 | end if; | |
9223 | ||
9224 | Append (Right_Opnd (N), Actuals); | |
9225 | ||
9226 | New_N := | |
9227 | Make_Function_Call (Sloc => Loc, | |
9228 | Name => New_Occurrence_Of (Nam, Loc), | |
9229 | Parameter_Associations => Actuals); | |
9230 | ||
9231 | Preserve_Comes_From_Source (New_N, N); | |
9232 | Preserve_Comes_From_Source (Name (New_N), N); | |
9233 | Rewrite (N, New_N); | |
9234 | Set_Etype (N, Etype (Nam)); | |
9235 | end Rewrite_Operator_As_Call; | |
9236 | ||
9237 | ------------------------------ | |
9238 | -- Rewrite_Renamed_Operator -- | |
9239 | ------------------------------ | |
9240 | ||
0ab80019 AC |
9241 | procedure Rewrite_Renamed_Operator |
9242 | (N : Node_Id; | |
9243 | Op : Entity_Id; | |
9244 | Typ : Entity_Id) | |
9245 | is | |
996ae0b0 RK |
9246 | Nam : constant Name_Id := Chars (Op); |
9247 | Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
9248 | Op_Node : Node_Id; | |
9249 | ||
9250 | begin | |
d81b4bfe TQ |
9251 | -- Rewrite the operator node using the real operator, not its renaming. |
9252 | -- Exclude user-defined intrinsic operations of the same name, which are | |
9253 | -- treated separately and rewritten as calls. | |
996ae0b0 | 9254 | |
964f13da | 9255 | if Ekind (Op) /= E_Function or else Chars (N) /= Nam then |
996ae0b0 RK |
9256 | Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N)); |
9257 | Set_Chars (Op_Node, Nam); | |
9258 | Set_Etype (Op_Node, Etype (N)); | |
9259 | Set_Entity (Op_Node, Op); | |
9260 | Set_Right_Opnd (Op_Node, Right_Opnd (N)); | |
9261 | ||
b7d1f17f HK |
9262 | -- Indicate that both the original entity and its renaming are |
9263 | -- referenced at this point. | |
fbf5a39b AC |
9264 | |
9265 | Generate_Reference (Entity (N), N); | |
996ae0b0 RK |
9266 | Generate_Reference (Op, N); |
9267 | ||
9268 | if Is_Binary then | |
9269 | Set_Left_Opnd (Op_Node, Left_Opnd (N)); | |
9270 | end if; | |
9271 | ||
9272 | Rewrite (N, Op_Node); | |
0ab80019 | 9273 | |
1366997b AC |
9274 | -- If the context type is private, add the appropriate conversions so |
9275 | -- that the operator is applied to the full view. This is done in the | |
9276 | -- routines that resolve intrinsic operators. | |
0ab80019 AC |
9277 | |
9278 | if Is_Intrinsic_Subprogram (Op) | |
9279 | and then Is_Private_Type (Typ) | |
9280 | then | |
9281 | case Nkind (N) is | |
9282 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | | |
9283 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
9284 | Resolve_Intrinsic_Operator (N, Typ); | |
9285 | ||
d81b4bfe | 9286 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => |
0ab80019 AC |
9287 | Resolve_Intrinsic_Unary_Operator (N, Typ); |
9288 | ||
9289 | when others => | |
9290 | Resolve (N, Typ); | |
9291 | end case; | |
9292 | end if; | |
9293 | ||
964f13da RD |
9294 | elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then |
9295 | ||
1366997b AC |
9296 | -- Operator renames a user-defined operator of the same name. Use the |
9297 | -- original operator in the node, which is the one Gigi knows about. | |
0ab80019 AC |
9298 | |
9299 | Set_Entity (N, Op); | |
9300 | Set_Is_Overloaded (N, False); | |
996ae0b0 RK |
9301 | end if; |
9302 | end Rewrite_Renamed_Operator; | |
9303 | ||
9304 | ----------------------- | |
9305 | -- Set_Slice_Subtype -- | |
9306 | ----------------------- | |
9307 | ||
1366997b AC |
9308 | -- Build an implicit subtype declaration to represent the type delivered by |
9309 | -- the slice. This is an abbreviated version of an array subtype. We define | |
9310 | -- an index subtype for the slice, using either the subtype name or the | |
9311 | -- discrete range of the slice. To be consistent with index usage elsewhere | |
9312 | -- we create a list header to hold the single index. This list is not | |
9313 | -- otherwise attached to the syntax tree. | |
996ae0b0 RK |
9314 | |
9315 | procedure Set_Slice_Subtype (N : Node_Id) is | |
9316 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 9317 | Index_List : constant List_Id := New_List; |
996ae0b0 | 9318 | Index : Node_Id; |
996ae0b0 RK |
9319 | Index_Subtype : Entity_Id; |
9320 | Index_Type : Entity_Id; | |
9321 | Slice_Subtype : Entity_Id; | |
9322 | Drange : constant Node_Id := Discrete_Range (N); | |
9323 | ||
9324 | begin | |
9325 | if Is_Entity_Name (Drange) then | |
9326 | Index_Subtype := Entity (Drange); | |
9327 | ||
9328 | else | |
9329 | -- We force the evaluation of a range. This is definitely needed in | |
9330 | -- the renamed case, and seems safer to do unconditionally. Note in | |
9331 | -- any case that since we will create and insert an Itype referring | |
9332 | -- to this range, we must make sure any side effect removal actions | |
9333 | -- are inserted before the Itype definition. | |
9334 | ||
9335 | if Nkind (Drange) = N_Range then | |
9336 | Force_Evaluation (Low_Bound (Drange)); | |
9337 | Force_Evaluation (High_Bound (Drange)); | |
9338 | end if; | |
9339 | ||
9340 | Index_Type := Base_Type (Etype (Drange)); | |
9341 | ||
9342 | Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
9343 | ||
8a95f4e8 | 9344 | -- Take a new copy of Drange (where bounds have been rewritten to |
3c1ecd7e AC |
9345 | -- reference side-effect-free names). Using a separate tree ensures |
9346 | -- that further expansion (e.g. while rewriting a slice assignment | |
8a95f4e8 RD |
9347 | -- into a FOR loop) does not attempt to remove side effects on the |
9348 | -- bounds again (which would cause the bounds in the index subtype | |
9349 | -- definition to refer to temporaries before they are defined) (the | |
9350 | -- reason is that some names are considered side effect free here | |
9351 | -- for the subtype, but not in the context of a loop iteration | |
9352 | -- scheme). | |
9353 | ||
9354 | Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange)); | |
996ae0b0 RK |
9355 | Set_Etype (Index_Subtype, Index_Type); |
9356 | Set_Size_Info (Index_Subtype, Index_Type); | |
9357 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
9358 | end if; | |
9359 | ||
9360 | Slice_Subtype := Create_Itype (E_Array_Subtype, N); | |
9361 | ||
9362 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
9363 | Set_Etype (Index, Index_Subtype); | |
9364 | Append (Index, Index_List); | |
9365 | ||
996ae0b0 RK |
9366 | Set_First_Index (Slice_Subtype, Index); |
9367 | Set_Etype (Slice_Subtype, Base_Type (Etype (N))); | |
9368 | Set_Is_Constrained (Slice_Subtype, True); | |
996ae0b0 | 9369 | |
8a95f4e8 RD |
9370 | Check_Compile_Time_Size (Slice_Subtype); |
9371 | ||
b7d1f17f HK |
9372 | -- The Etype of the existing Slice node is reset to this slice subtype. |
9373 | -- Its bounds are obtained from its first index. | |
996ae0b0 RK |
9374 | |
9375 | Set_Etype (N, Slice_Subtype); | |
9376 | ||
cfab0c49 AC |
9377 | -- For packed slice subtypes, freeze immediately (except in the |
9378 | -- case of being in a "spec expression" where we never freeze | |
9379 | -- when we first see the expression). | |
8a95f4e8 RD |
9380 | |
9381 | if Is_Packed (Slice_Subtype) and not In_Spec_Expression then | |
9382 | Freeze_Itype (Slice_Subtype, N); | |
996ae0b0 | 9383 | |
cfab0c49 AC |
9384 | -- For all other cases insert an itype reference in the slice's actions |
9385 | -- so that the itype is frozen at the proper place in the tree (i.e. at | |
9386 | -- the point where actions for the slice are analyzed). Note that this | |
9387 | -- is different from freezing the itype immediately, which might be | |
9388 | -- premature (e.g. if the slice is within a transient scope). | |
9389 | ||
8a95f4e8 RD |
9390 | else |
9391 | Ensure_Defined (Typ => Slice_Subtype, N => N); | |
9392 | end if; | |
996ae0b0 RK |
9393 | end Set_Slice_Subtype; |
9394 | ||
9395 | -------------------------------- | |
9396 | -- Set_String_Literal_Subtype -- | |
9397 | -------------------------------- | |
9398 | ||
9399 | procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is | |
c8ef728f ES |
9400 | Loc : constant Source_Ptr := Sloc (N); |
9401 | Low_Bound : constant Node_Id := | |
d81b4bfe | 9402 | Type_Low_Bound (Etype (First_Index (Typ))); |
996ae0b0 RK |
9403 | Subtype_Id : Entity_Id; |
9404 | ||
9405 | begin | |
9406 | if Nkind (N) /= N_String_Literal then | |
9407 | return; | |
996ae0b0 RK |
9408 | end if; |
9409 | ||
c8ef728f | 9410 | Subtype_Id := Create_Itype (E_String_Literal_Subtype, N); |
91b1417d AC |
9411 | Set_String_Literal_Length (Subtype_Id, UI_From_Int |
9412 | (String_Length (Strval (N)))); | |
c8ef728f ES |
9413 | Set_Etype (Subtype_Id, Base_Type (Typ)); |
9414 | Set_Is_Constrained (Subtype_Id); | |
9415 | Set_Etype (N, Subtype_Id); | |
9416 | ||
9417 | if Is_OK_Static_Expression (Low_Bound) then | |
996ae0b0 | 9418 | |
1366997b AC |
9419 | -- The low bound is set from the low bound of the corresponding index |
9420 | -- type. Note that we do not store the high bound in the string literal | |
9421 | -- subtype, but it can be deduced if necessary from the length and the | |
9422 | -- low bound. | |
996ae0b0 | 9423 | |
c8ef728f | 9424 | Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound); |
996ae0b0 | 9425 | |
c8ef728f ES |
9426 | else |
9427 | Set_String_Literal_Low_Bound | |
9428 | (Subtype_Id, Make_Integer_Literal (Loc, 1)); | |
9429 | Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive); | |
9430 | ||
b7d1f17f HK |
9431 | -- Build bona fide subtype for the string, and wrap it in an |
9432 | -- unchecked conversion, because the backend expects the | |
c8ef728f ES |
9433 | -- String_Literal_Subtype to have a static lower bound. |
9434 | ||
9435 | declare | |
9436 | Index_List : constant List_Id := New_List; | |
9437 | Index_Type : constant Entity_Id := Etype (First_Index (Typ)); | |
9438 | High_Bound : constant Node_Id := | |
9439 | Make_Op_Add (Loc, | |
9440 | Left_Opnd => New_Copy_Tree (Low_Bound), | |
9441 | Right_Opnd => | |
9442 | Make_Integer_Literal (Loc, | |
9443 | String_Length (Strval (N)) - 1)); | |
9444 | Array_Subtype : Entity_Id; | |
9445 | Index_Subtype : Entity_Id; | |
9446 | Drange : Node_Id; | |
9447 | Index : Node_Id; | |
9448 | ||
9449 | begin | |
9450 | Index_Subtype := | |
9451 | Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
0669bebe | 9452 | Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound); |
c8ef728f ES |
9453 | Set_Scalar_Range (Index_Subtype, Drange); |
9454 | Set_Parent (Drange, N); | |
9455 | Analyze_And_Resolve (Drange, Index_Type); | |
9456 | ||
36fcf362 RD |
9457 | -- In the context, the Index_Type may already have a constraint, |
9458 | -- so use common base type on string subtype. The base type may | |
9459 | -- be used when generating attributes of the string, for example | |
9460 | -- in the context of a slice assignment. | |
9461 | ||
9462 | Set_Etype (Index_Subtype, Base_Type (Index_Type)); | |
c8ef728f ES |
9463 | Set_Size_Info (Index_Subtype, Index_Type); |
9464 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
9465 | ||
9466 | Array_Subtype := Create_Itype (E_Array_Subtype, N); | |
9467 | ||
9468 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
9469 | Set_Etype (Index, Index_Subtype); | |
9470 | Append (Index, Index_List); | |
9471 | ||
9472 | Set_First_Index (Array_Subtype, Index); | |
9473 | Set_Etype (Array_Subtype, Base_Type (Typ)); | |
9474 | Set_Is_Constrained (Array_Subtype, True); | |
c8ef728f ES |
9475 | |
9476 | Rewrite (N, | |
9477 | Make_Unchecked_Type_Conversion (Loc, | |
9478 | Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc), | |
9479 | Expression => Relocate_Node (N))); | |
9480 | Set_Etype (N, Array_Subtype); | |
9481 | end; | |
9482 | end if; | |
996ae0b0 RK |
9483 | end Set_String_Literal_Subtype; |
9484 | ||
0669bebe GB |
9485 | ------------------------------ |
9486 | -- Simplify_Type_Conversion -- | |
9487 | ------------------------------ | |
9488 | ||
9489 | procedure Simplify_Type_Conversion (N : Node_Id) is | |
9490 | begin | |
9491 | if Nkind (N) = N_Type_Conversion then | |
9492 | declare | |
9493 | Operand : constant Node_Id := Expression (N); | |
9494 | Target_Typ : constant Entity_Id := Etype (N); | |
9495 | Opnd_Typ : constant Entity_Id := Etype (Operand); | |
9496 | ||
9497 | begin | |
9498 | if Is_Floating_Point_Type (Opnd_Typ) | |
9499 | and then | |
9500 | (Is_Integer_Type (Target_Typ) | |
9501 | or else (Is_Fixed_Point_Type (Target_Typ) | |
9502 | and then Conversion_OK (N))) | |
9503 | and then Nkind (Operand) = N_Attribute_Reference | |
9504 | and then Attribute_Name (Operand) = Name_Truncation | |
9505 | ||
9506 | -- Special processing required if the conversion is the expression | |
9507 | -- of a Truncation attribute reference. In this case we replace: | |
9508 | ||
9509 | -- ityp (ftyp'Truncation (x)) | |
9510 | ||
9511 | -- by | |
9512 | ||
9513 | -- ityp (x) | |
9514 | ||
9515 | -- with the Float_Truncate flag set, which is more efficient | |
9516 | ||
9517 | then | |
9518 | Rewrite (Operand, | |
9519 | Relocate_Node (First (Expressions (Operand)))); | |
9520 | Set_Float_Truncate (N, True); | |
9521 | end if; | |
9522 | end; | |
9523 | end if; | |
9524 | end Simplify_Type_Conversion; | |
9525 | ||
996ae0b0 RK |
9526 | ----------------------------- |
9527 | -- Unique_Fixed_Point_Type -- | |
9528 | ----------------------------- | |
9529 | ||
9530 | function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is | |
9531 | T1 : Entity_Id := Empty; | |
9532 | T2 : Entity_Id; | |
9533 | Item : Node_Id; | |
9534 | Scop : Entity_Id; | |
9535 | ||
9536 | procedure Fixed_Point_Error; | |
d81b4bfe TQ |
9537 | -- Give error messages for true ambiguity. Messages are posted on node |
9538 | -- N, and entities T1, T2 are the possible interpretations. | |
a77842bd TQ |
9539 | |
9540 | ----------------------- | |
9541 | -- Fixed_Point_Error -- | |
9542 | ----------------------- | |
996ae0b0 RK |
9543 | |
9544 | procedure Fixed_Point_Error is | |
9545 | begin | |
ed2233dc AC |
9546 | Error_Msg_N ("ambiguous universal_fixed_expression", N); |
9547 | Error_Msg_NE ("\\possible interpretation as}", N, T1); | |
9548 | Error_Msg_NE ("\\possible interpretation as}", N, T2); | |
996ae0b0 RK |
9549 | end Fixed_Point_Error; |
9550 | ||
a77842bd TQ |
9551 | -- Start of processing for Unique_Fixed_Point_Type |
9552 | ||
996ae0b0 RK |
9553 | begin |
9554 | -- The operations on Duration are visible, so Duration is always a | |
9555 | -- possible interpretation. | |
9556 | ||
9557 | T1 := Standard_Duration; | |
9558 | ||
bc5f3720 | 9559 | -- Look for fixed-point types in enclosing scopes |
996ae0b0 | 9560 | |
fbf5a39b | 9561 | Scop := Current_Scope; |
996ae0b0 RK |
9562 | while Scop /= Standard_Standard loop |
9563 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9564 | while Present (T2) loop |
9565 | if Is_Fixed_Point_Type (T2) | |
9566 | and then Current_Entity (T2) = T2 | |
9567 | and then Scope (Base_Type (T2)) = Scop | |
9568 | then | |
9569 | if Present (T1) then | |
9570 | Fixed_Point_Error; | |
9571 | return Any_Type; | |
9572 | else | |
9573 | T1 := T2; | |
9574 | end if; | |
9575 | end if; | |
9576 | ||
9577 | Next_Entity (T2); | |
9578 | end loop; | |
9579 | ||
9580 | Scop := Scope (Scop); | |
9581 | end loop; | |
9582 | ||
a77842bd | 9583 | -- Look for visible fixed type declarations in the context |
996ae0b0 RK |
9584 | |
9585 | Item := First (Context_Items (Cunit (Current_Sem_Unit))); | |
996ae0b0 | 9586 | while Present (Item) loop |
996ae0b0 RK |
9587 | if Nkind (Item) = N_With_Clause then |
9588 | Scop := Entity (Name (Item)); | |
9589 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9590 | while Present (T2) loop |
9591 | if Is_Fixed_Point_Type (T2) | |
9592 | and then Scope (Base_Type (T2)) = Scop | |
9593 | and then (Is_Potentially_Use_Visible (T2) | |
9594 | or else In_Use (T2)) | |
9595 | then | |
9596 | if Present (T1) then | |
9597 | Fixed_Point_Error; | |
9598 | return Any_Type; | |
9599 | else | |
9600 | T1 := T2; | |
9601 | end if; | |
9602 | end if; | |
9603 | ||
9604 | Next_Entity (T2); | |
9605 | end loop; | |
9606 | end if; | |
9607 | ||
9608 | Next (Item); | |
9609 | end loop; | |
9610 | ||
9611 | if Nkind (N) = N_Real_Literal then | |
aa5147f0 | 9612 | Error_Msg_NE ("?real literal interpreted as }!", N, T1); |
996ae0b0 | 9613 | else |
aa5147f0 | 9614 | Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1); |
996ae0b0 RK |
9615 | end if; |
9616 | ||
9617 | return T1; | |
9618 | end Unique_Fixed_Point_Type; | |
9619 | ||
9620 | ---------------------- | |
9621 | -- Valid_Conversion -- | |
9622 | ---------------------- | |
9623 | ||
9624 | function Valid_Conversion | |
9625 | (N : Node_Id; | |
9626 | Target : Entity_Id; | |
0ab80019 | 9627 | Operand : Node_Id) return Boolean |
996ae0b0 | 9628 | is |
fbf5a39b | 9629 | Target_Type : constant Entity_Id := Base_Type (Target); |
996ae0b0 RK |
9630 | Opnd_Type : Entity_Id := Etype (Operand); |
9631 | ||
9632 | function Conversion_Check | |
9633 | (Valid : Boolean; | |
0ab80019 | 9634 | Msg : String) return Boolean; |
996ae0b0 RK |
9635 | -- Little routine to post Msg if Valid is False, returns Valid value |
9636 | ||
9637 | function Valid_Tagged_Conversion | |
9638 | (Target_Type : Entity_Id; | |
0ab80019 | 9639 | Opnd_Type : Entity_Id) return Boolean; |
996ae0b0 RK |
9640 | -- Specifically test for validity of tagged conversions |
9641 | ||
aa180613 RD |
9642 | function Valid_Array_Conversion return Boolean; |
9643 | -- Check index and component conformance, and accessibility levels | |
9644 | -- if the component types are anonymous access types (Ada 2005) | |
9645 | ||
996ae0b0 RK |
9646 | ---------------------- |
9647 | -- Conversion_Check -- | |
9648 | ---------------------- | |
9649 | ||
9650 | function Conversion_Check | |
9651 | (Valid : Boolean; | |
0ab80019 | 9652 | Msg : String) return Boolean |
996ae0b0 RK |
9653 | is |
9654 | begin | |
9655 | if not Valid then | |
9656 | Error_Msg_N (Msg, Operand); | |
9657 | end if; | |
9658 | ||
9659 | return Valid; | |
9660 | end Conversion_Check; | |
9661 | ||
aa180613 RD |
9662 | ---------------------------- |
9663 | -- Valid_Array_Conversion -- | |
9664 | ---------------------------- | |
9665 | ||
9666 | function Valid_Array_Conversion return Boolean | |
9667 | is | |
9668 | Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type); | |
9669 | Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type); | |
9670 | ||
9671 | Opnd_Index : Node_Id; | |
9672 | Opnd_Index_Type : Entity_Id; | |
9673 | ||
9674 | Target_Comp_Type : constant Entity_Id := | |
9675 | Component_Type (Target_Type); | |
9676 | Target_Comp_Base : constant Entity_Id := | |
9677 | Base_Type (Target_Comp_Type); | |
9678 | ||
9679 | Target_Index : Node_Id; | |
9680 | Target_Index_Type : Entity_Id; | |
9681 | ||
9682 | begin | |
9683 | -- Error if wrong number of dimensions | |
9684 | ||
9685 | if | |
9686 | Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type) | |
9687 | then | |
9688 | Error_Msg_N | |
9689 | ("incompatible number of dimensions for conversion", Operand); | |
9690 | return False; | |
9691 | ||
9692 | -- Number of dimensions matches | |
9693 | ||
9694 | else | |
9695 | -- Loop through indexes of the two arrays | |
9696 | ||
9697 | Target_Index := First_Index (Target_Type); | |
9698 | Opnd_Index := First_Index (Opnd_Type); | |
9699 | while Present (Target_Index) and then Present (Opnd_Index) loop | |
9700 | Target_Index_Type := Etype (Target_Index); | |
9701 | Opnd_Index_Type := Etype (Opnd_Index); | |
9702 | ||
9703 | -- Error if index types are incompatible | |
9704 | ||
9705 | if not (Is_Integer_Type (Target_Index_Type) | |
9706 | and then Is_Integer_Type (Opnd_Index_Type)) | |
9707 | and then (Root_Type (Target_Index_Type) | |
9708 | /= Root_Type (Opnd_Index_Type)) | |
9709 | then | |
9710 | Error_Msg_N | |
9711 | ("incompatible index types for array conversion", | |
9712 | Operand); | |
9713 | return False; | |
9714 | end if; | |
9715 | ||
9716 | Next_Index (Target_Index); | |
9717 | Next_Index (Opnd_Index); | |
9718 | end loop; | |
9719 | ||
9720 | -- If component types have same base type, all set | |
9721 | ||
9722 | if Target_Comp_Base = Opnd_Comp_Base then | |
9723 | null; | |
9724 | ||
9725 | -- Here if base types of components are not the same. The only | |
9726 | -- time this is allowed is if we have anonymous access types. | |
9727 | ||
9728 | -- The conversion of arrays of anonymous access types can lead | |
9729 | -- to dangling pointers. AI-392 formalizes the accessibility | |
9730 | -- checks that must be applied to such conversions to prevent | |
9731 | -- out-of-scope references. | |
9732 | ||
9733 | elsif | |
964f13da RD |
9734 | Ekind_In (Target_Comp_Base, E_Anonymous_Access_Type, |
9735 | E_Anonymous_Access_Subprogram_Type) | |
aa180613 RD |
9736 | and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base) |
9737 | and then | |
9738 | Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type) | |
9739 | then | |
9740 | if Type_Access_Level (Target_Type) < | |
9741 | Type_Access_Level (Opnd_Type) | |
9742 | then | |
9743 | if In_Instance_Body then | |
9744 | Error_Msg_N ("?source array type " & | |
9745 | "has deeper accessibility level than target", Operand); | |
9746 | Error_Msg_N ("\?Program_Error will be raised at run time", | |
9747 | Operand); | |
9748 | Rewrite (N, | |
9749 | Make_Raise_Program_Error (Sloc (N), | |
9750 | Reason => PE_Accessibility_Check_Failed)); | |
9751 | Set_Etype (N, Target_Type); | |
9752 | return False; | |
9753 | ||
9754 | -- Conversion not allowed because of accessibility levels | |
9755 | ||
9756 | else | |
9757 | Error_Msg_N ("source array type " & | |
9758 | "has deeper accessibility level than target", Operand); | |
9759 | return False; | |
9760 | end if; | |
9761 | else | |
9762 | null; | |
9763 | end if; | |
9764 | ||
9765 | -- All other cases where component base types do not match | |
9766 | ||
9767 | else | |
9768 | Error_Msg_N | |
9769 | ("incompatible component types for array conversion", | |
9770 | Operand); | |
9771 | return False; | |
9772 | end if; | |
9773 | ||
45fc7ddb HK |
9774 | -- Check that component subtypes statically match. For numeric |
9775 | -- types this means that both must be either constrained or | |
9776 | -- unconstrained. For enumeration types the bounds must match. | |
9777 | -- All of this is checked in Subtypes_Statically_Match. | |
aa180613 | 9778 | |
45fc7ddb | 9779 | if not Subtypes_Statically_Match |
aa180613 RD |
9780 | (Target_Comp_Type, Opnd_Comp_Type) |
9781 | then | |
9782 | Error_Msg_N | |
9783 | ("component subtypes must statically match", Operand); | |
9784 | return False; | |
9785 | end if; | |
9786 | end if; | |
9787 | ||
9788 | return True; | |
9789 | end Valid_Array_Conversion; | |
9790 | ||
996ae0b0 RK |
9791 | ----------------------------- |
9792 | -- Valid_Tagged_Conversion -- | |
9793 | ----------------------------- | |
9794 | ||
9795 | function Valid_Tagged_Conversion | |
9796 | (Target_Type : Entity_Id; | |
0ab80019 | 9797 | Opnd_Type : Entity_Id) return Boolean |
996ae0b0 RK |
9798 | is |
9799 | begin | |
a77842bd | 9800 | -- Upward conversions are allowed (RM 4.6(22)) |
996ae0b0 RK |
9801 | |
9802 | if Covers (Target_Type, Opnd_Type) | |
9803 | or else Is_Ancestor (Target_Type, Opnd_Type) | |
9804 | then | |
9805 | return True; | |
9806 | ||
a77842bd TQ |
9807 | -- Downward conversion are allowed if the operand is class-wide |
9808 | -- (RM 4.6(23)). | |
996ae0b0 RK |
9809 | |
9810 | elsif Is_Class_Wide_Type (Opnd_Type) | |
b7d1f17f | 9811 | and then Covers (Opnd_Type, Target_Type) |
996ae0b0 RK |
9812 | then |
9813 | return True; | |
9814 | ||
9815 | elsif Covers (Opnd_Type, Target_Type) | |
9816 | or else Is_Ancestor (Opnd_Type, Target_Type) | |
9817 | then | |
9818 | return | |
9819 | Conversion_Check (False, | |
9820 | "downward conversion of tagged objects not allowed"); | |
758c442c | 9821 | |
0669bebe GB |
9822 | -- Ada 2005 (AI-251): The conversion to/from interface types is |
9823 | -- always valid | |
758c442c | 9824 | |
0669bebe | 9825 | elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then |
758c442c GD |
9826 | return True; |
9827 | ||
b7d1f17f HK |
9828 | -- If the operand is a class-wide type obtained through a limited_ |
9829 | -- with clause, and the context includes the non-limited view, use | |
9830 | -- it to determine whether the conversion is legal. | |
9831 | ||
9832 | elsif Is_Class_Wide_Type (Opnd_Type) | |
9833 | and then From_With_Type (Opnd_Type) | |
9834 | and then Present (Non_Limited_View (Etype (Opnd_Type))) | |
9835 | and then Is_Interface (Non_Limited_View (Etype (Opnd_Type))) | |
9836 | then | |
9837 | return True; | |
9838 | ||
aa180613 RD |
9839 | elsif Is_Access_Type (Opnd_Type) |
9840 | and then Is_Interface (Directly_Designated_Type (Opnd_Type)) | |
9841 | then | |
9842 | return True; | |
9843 | ||
996ae0b0 RK |
9844 | else |
9845 | Error_Msg_NE | |
9846 | ("invalid tagged conversion, not compatible with}", | |
9847 | N, First_Subtype (Opnd_Type)); | |
9848 | return False; | |
9849 | end if; | |
9850 | end Valid_Tagged_Conversion; | |
9851 | ||
9852 | -- Start of processing for Valid_Conversion | |
9853 | ||
9854 | begin | |
9855 | Check_Parameterless_Call (Operand); | |
9856 | ||
9857 | if Is_Overloaded (Operand) then | |
9858 | declare | |
9859 | I : Interp_Index; | |
9860 | I1 : Interp_Index; | |
9861 | It : Interp; | |
9862 | It1 : Interp; | |
9863 | N1 : Entity_Id; | |
f0d10385 | 9864 | T1 : Entity_Id; |
996ae0b0 RK |
9865 | |
9866 | begin | |
d81b4bfe TQ |
9867 | -- Remove procedure calls, which syntactically cannot appear in |
9868 | -- this context, but which cannot be removed by type checking, | |
996ae0b0 RK |
9869 | -- because the context does not impose a type. |
9870 | ||
1420b484 JM |
9871 | -- When compiling for VMS, spurious ambiguities can be produced |
9872 | -- when arithmetic operations have a literal operand and return | |
9873 | -- System.Address or a descendant of it. These ambiguities are | |
9874 | -- otherwise resolved by the context, but for conversions there | |
9875 | -- is no context type and the removal of the spurious operations | |
9876 | -- must be done explicitly here. | |
9877 | ||
9ebe3743 HK |
9878 | -- The node may be labelled overloaded, but still contain only |
9879 | -- one interpretation because others were discarded in previous | |
9880 | -- filters. If this is the case, retain the single interpretation | |
9881 | -- if legal. | |
9882 | ||
996ae0b0 | 9883 | Get_First_Interp (Operand, I, It); |
9ebe3743 HK |
9884 | Opnd_Type := It.Typ; |
9885 | Get_Next_Interp (I, It); | |
996ae0b0 | 9886 | |
9ebe3743 HK |
9887 | if Present (It.Typ) |
9888 | and then Opnd_Type /= Standard_Void_Type | |
9889 | then | |
9890 | -- More than one candidate interpretation is available | |
996ae0b0 | 9891 | |
9ebe3743 HK |
9892 | Get_First_Interp (Operand, I, It); |
9893 | while Present (It.Typ) loop | |
9894 | if It.Typ = Standard_Void_Type then | |
9895 | Remove_Interp (I); | |
9896 | end if; | |
1420b484 | 9897 | |
9ebe3743 HK |
9898 | if Present (System_Aux_Id) |
9899 | and then Is_Descendent_Of_Address (It.Typ) | |
9900 | then | |
9901 | Remove_Interp (I); | |
9902 | end if; | |
9903 | ||
9904 | Get_Next_Interp (I, It); | |
9905 | end loop; | |
9906 | end if; | |
996ae0b0 RK |
9907 | |
9908 | Get_First_Interp (Operand, I, It); | |
9909 | I1 := I; | |
9910 | It1 := It; | |
9911 | ||
9912 | if No (It.Typ) then | |
9913 | Error_Msg_N ("illegal operand in conversion", Operand); | |
9914 | return False; | |
9915 | end if; | |
9916 | ||
9917 | Get_Next_Interp (I, It); | |
9918 | ||
9919 | if Present (It.Typ) then | |
9920 | N1 := It1.Nam; | |
f0d10385 | 9921 | T1 := It1.Typ; |
996ae0b0 RK |
9922 | It1 := Disambiguate (Operand, I1, I, Any_Type); |
9923 | ||
9924 | if It1 = No_Interp then | |
9925 | Error_Msg_N ("ambiguous operand in conversion", Operand); | |
9926 | ||
f0d10385 AC |
9927 | -- If the interpretation involves a standard operator, use |
9928 | -- the location of the type, which may be user-defined. | |
9929 | ||
9930 | if Sloc (It.Nam) = Standard_Location then | |
9931 | Error_Msg_Sloc := Sloc (It.Typ); | |
9932 | else | |
9933 | Error_Msg_Sloc := Sloc (It.Nam); | |
9934 | end if; | |
9935 | ||
4e7a4f6e AC |
9936 | Error_Msg_N -- CODEFIX |
9937 | ("\\possible interpretation#!", Operand); | |
996ae0b0 | 9938 | |
f0d10385 AC |
9939 | if Sloc (N1) = Standard_Location then |
9940 | Error_Msg_Sloc := Sloc (T1); | |
9941 | else | |
9942 | Error_Msg_Sloc := Sloc (N1); | |
9943 | end if; | |
9944 | ||
4e7a4f6e AC |
9945 | Error_Msg_N -- CODEFIX |
9946 | ("\\possible interpretation#!", Operand); | |
996ae0b0 RK |
9947 | |
9948 | return False; | |
9949 | end if; | |
9950 | end if; | |
9951 | ||
9952 | Set_Etype (Operand, It1.Typ); | |
9953 | Opnd_Type := It1.Typ; | |
9954 | end; | |
9955 | end if; | |
9956 | ||
aa180613 | 9957 | -- Numeric types |
996ae0b0 | 9958 | |
aa180613 | 9959 | if Is_Numeric_Type (Target_Type) then |
996ae0b0 | 9960 | |
aa180613 | 9961 | -- A universal fixed expression can be converted to any numeric type |
996ae0b0 | 9962 | |
996ae0b0 RK |
9963 | if Opnd_Type = Universal_Fixed then |
9964 | return True; | |
7324bf49 | 9965 | |
aa180613 RD |
9966 | -- Also no need to check when in an instance or inlined body, because |
9967 | -- the legality has been established when the template was analyzed. | |
9968 | -- Furthermore, numeric conversions may occur where only a private | |
f3d57416 | 9969 | -- view of the operand type is visible at the instantiation point. |
aa180613 RD |
9970 | -- This results in a spurious error if we check that the operand type |
9971 | -- is a numeric type. | |
9972 | ||
9973 | -- Note: in a previous version of this unit, the following tests were | |
9974 | -- applied only for generated code (Comes_From_Source set to False), | |
9975 | -- but in fact the test is required for source code as well, since | |
9976 | -- this situation can arise in source code. | |
9977 | ||
9978 | elsif In_Instance or else In_Inlined_Body then | |
9979 | return True; | |
9980 | ||
9981 | -- Otherwise we need the conversion check | |
7324bf49 | 9982 | |
996ae0b0 | 9983 | else |
aa180613 RD |
9984 | return Conversion_Check |
9985 | (Is_Numeric_Type (Opnd_Type), | |
9986 | "illegal operand for numeric conversion"); | |
996ae0b0 RK |
9987 | end if; |
9988 | ||
aa180613 RD |
9989 | -- Array types |
9990 | ||
996ae0b0 RK |
9991 | elsif Is_Array_Type (Target_Type) then |
9992 | if not Is_Array_Type (Opnd_Type) | |
9993 | or else Opnd_Type = Any_Composite | |
9994 | or else Opnd_Type = Any_String | |
9995 | then | |
9996 | Error_Msg_N | |
9997 | ("illegal operand for array conversion", Operand); | |
9998 | return False; | |
996ae0b0 | 9999 | else |
aa180613 | 10000 | return Valid_Array_Conversion; |
996ae0b0 RK |
10001 | end if; |
10002 | ||
e65f50ec ES |
10003 | -- Ada 2005 (AI-251): Anonymous access types where target references an |
10004 | -- interface type. | |
758c442c | 10005 | |
964f13da RD |
10006 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10007 | E_Anonymous_Access_Type) | |
758c442c GD |
10008 | and then Is_Interface (Directly_Designated_Type (Target_Type)) |
10009 | then | |
10010 | -- Check the static accessibility rule of 4.6(17). Note that the | |
d81b4bfe TQ |
10011 | -- check is not enforced when within an instance body, since the |
10012 | -- RM requires such cases to be caught at run time. | |
758c442c GD |
10013 | |
10014 | if Ekind (Target_Type) /= E_Anonymous_Access_Type then | |
10015 | if Type_Access_Level (Opnd_Type) > | |
10016 | Type_Access_Level (Target_Type) | |
10017 | then | |
10018 | -- In an instance, this is a run-time check, but one we know | |
10019 | -- will fail, so generate an appropriate warning. The raise | |
10020 | -- will be generated by Expand_N_Type_Conversion. | |
10021 | ||
10022 | if In_Instance_Body then | |
10023 | Error_Msg_N | |
10024 | ("?cannot convert local pointer to non-local access type", | |
10025 | Operand); | |
10026 | Error_Msg_N | |
c8ef728f | 10027 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
10028 | else |
10029 | Error_Msg_N | |
10030 | ("cannot convert local pointer to non-local access type", | |
10031 | Operand); | |
10032 | return False; | |
10033 | end if; | |
10034 | ||
10035 | -- Special accessibility checks are needed in the case of access | |
10036 | -- discriminants declared for a limited type. | |
10037 | ||
10038 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
10039 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
10040 | then | |
10041 | -- When the operand is a selected access discriminant the check | |
10042 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
10043 | -- the prefix of the selected name (Object_Access_Level handles |
10044 | -- checking the prefix of the operand for this case). | |
758c442c GD |
10045 | |
10046 | if Nkind (Operand) = N_Selected_Component | |
c8ef728f | 10047 | and then Object_Access_Level (Operand) > |
45fc7ddb | 10048 | Type_Access_Level (Target_Type) |
758c442c | 10049 | then |
d81b4bfe TQ |
10050 | -- In an instance, this is a run-time check, but one we know |
10051 | -- will fail, so generate an appropriate warning. The raise | |
10052 | -- will be generated by Expand_N_Type_Conversion. | |
758c442c GD |
10053 | |
10054 | if In_Instance_Body then | |
10055 | Error_Msg_N | |
10056 | ("?cannot convert access discriminant to non-local" & | |
10057 | " access type", Operand); | |
10058 | Error_Msg_N | |
c8ef728f | 10059 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
10060 | else |
10061 | Error_Msg_N | |
10062 | ("cannot convert access discriminant to non-local" & | |
10063 | " access type", Operand); | |
10064 | return False; | |
10065 | end if; | |
10066 | end if; | |
10067 | ||
10068 | -- The case of a reference to an access discriminant from | |
10069 | -- within a limited type declaration (which will appear as | |
10070 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 10071 | -- discriminant is considered to be deeper than any (nameable) |
758c442c GD |
10072 | -- access type. |
10073 | ||
10074 | if Is_Entity_Name (Operand) | |
10075 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
964f13da RD |
10076 | and then |
10077 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
758c442c GD |
10078 | and then Present (Discriminal_Link (Entity (Operand))) |
10079 | then | |
10080 | Error_Msg_N | |
10081 | ("discriminant has deeper accessibility level than target", | |
10082 | Operand); | |
10083 | return False; | |
10084 | end if; | |
10085 | end if; | |
10086 | end if; | |
10087 | ||
10088 | return True; | |
10089 | ||
aa180613 RD |
10090 | -- General and anonymous access types |
10091 | ||
964f13da RD |
10092 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10093 | E_Anonymous_Access_Type) | |
996ae0b0 RK |
10094 | and then |
10095 | Conversion_Check | |
10096 | (Is_Access_Type (Opnd_Type) | |
964f13da RD |
10097 | and then not |
10098 | Ekind_In (Opnd_Type, E_Access_Subprogram_Type, | |
10099 | E_Access_Protected_Subprogram_Type), | |
996ae0b0 RK |
10100 | "must be an access-to-object type") |
10101 | then | |
10102 | if Is_Access_Constant (Opnd_Type) | |
10103 | and then not Is_Access_Constant (Target_Type) | |
10104 | then | |
10105 | Error_Msg_N | |
10106 | ("access-to-constant operand type not allowed", Operand); | |
10107 | return False; | |
10108 | end if; | |
10109 | ||
758c442c GD |
10110 | -- Check the static accessibility rule of 4.6(17). Note that the |
10111 | -- check is not enforced when within an instance body, since the RM | |
10112 | -- requires such cases to be caught at run time. | |
996ae0b0 | 10113 | |
758c442c GD |
10114 | if Ekind (Target_Type) /= E_Anonymous_Access_Type |
10115 | or else Is_Local_Anonymous_Access (Target_Type) | |
10116 | then | |
996ae0b0 RK |
10117 | if Type_Access_Level (Opnd_Type) |
10118 | > Type_Access_Level (Target_Type) | |
10119 | then | |
d81b4bfe TQ |
10120 | -- In an instance, this is a run-time check, but one we know |
10121 | -- will fail, so generate an appropriate warning. The raise | |
10122 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
10123 | |
10124 | if In_Instance_Body then | |
10125 | Error_Msg_N | |
10126 | ("?cannot convert local pointer to non-local access type", | |
10127 | Operand); | |
10128 | Error_Msg_N | |
c8ef728f | 10129 | ("\?Program_Error will be raised at run time", Operand); |
996ae0b0 RK |
10130 | |
10131 | else | |
b90cfacd HK |
10132 | -- Avoid generation of spurious error message |
10133 | ||
10134 | if not Error_Posted (N) then | |
10135 | Error_Msg_N | |
10136 | ("cannot convert local pointer to non-local access type", | |
10137 | Operand); | |
10138 | end if; | |
10139 | ||
996ae0b0 RK |
10140 | return False; |
10141 | end if; | |
10142 | ||
758c442c GD |
10143 | -- Special accessibility checks are needed in the case of access |
10144 | -- discriminants declared for a limited type. | |
10145 | ||
10146 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
10147 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
10148 | then | |
758c442c GD |
10149 | -- When the operand is a selected access discriminant the check |
10150 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
10151 | -- the prefix of the selected name (Object_Access_Level handles |
10152 | -- checking the prefix of the operand for this case). | |
996ae0b0 RK |
10153 | |
10154 | if Nkind (Operand) = N_Selected_Component | |
45fc7ddb HK |
10155 | and then Object_Access_Level (Operand) > |
10156 | Type_Access_Level (Target_Type) | |
996ae0b0 | 10157 | then |
d81b4bfe TQ |
10158 | -- In an instance, this is a run-time check, but one we know |
10159 | -- will fail, so generate an appropriate warning. The raise | |
10160 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
10161 | |
10162 | if In_Instance_Body then | |
10163 | Error_Msg_N | |
10164 | ("?cannot convert access discriminant to non-local" & | |
10165 | " access type", Operand); | |
10166 | Error_Msg_N | |
c8ef728f ES |
10167 | ("\?Program_Error will be raised at run time", |
10168 | Operand); | |
996ae0b0 RK |
10169 | |
10170 | else | |
10171 | Error_Msg_N | |
10172 | ("cannot convert access discriminant to non-local" & | |
10173 | " access type", Operand); | |
10174 | return False; | |
10175 | end if; | |
10176 | end if; | |
10177 | ||
758c442c GD |
10178 | -- The case of a reference to an access discriminant from |
10179 | -- within a limited type declaration (which will appear as | |
10180 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 10181 | -- discriminant is considered to be deeper than any (nameable) |
758c442c | 10182 | -- access type. |
996ae0b0 RK |
10183 | |
10184 | if Is_Entity_Name (Operand) | |
964f13da RD |
10185 | and then |
10186 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
996ae0b0 RK |
10187 | and then Present (Discriminal_Link (Entity (Operand))) |
10188 | then | |
10189 | Error_Msg_N | |
10190 | ("discriminant has deeper accessibility level than target", | |
10191 | Operand); | |
10192 | return False; | |
10193 | end if; | |
10194 | end if; | |
10195 | end if; | |
10196 | ||
14e33999 AC |
10197 | -- In the presence of limited_with clauses we have to use non-limited |
10198 | -- views, if available. | |
d81b4bfe | 10199 | |
14e33999 | 10200 | Check_Limited : declare |
0669bebe GB |
10201 | function Full_Designated_Type (T : Entity_Id) return Entity_Id; |
10202 | -- Helper function to handle limited views | |
10203 | ||
10204 | -------------------------- | |
10205 | -- Full_Designated_Type -- | |
10206 | -------------------------- | |
10207 | ||
10208 | function Full_Designated_Type (T : Entity_Id) return Entity_Id is | |
950d217a | 10209 | Desig : constant Entity_Id := Designated_Type (T); |
c0985d4e | 10210 | |
0669bebe | 10211 | begin |
950d217a AC |
10212 | -- Handle the limited view of a type |
10213 | ||
c0985d4e HK |
10214 | if Is_Incomplete_Type (Desig) |
10215 | and then From_With_Type (Desig) | |
0669bebe GB |
10216 | and then Present (Non_Limited_View (Desig)) |
10217 | then | |
950d217a AC |
10218 | return Available_View (Desig); |
10219 | else | |
10220 | return Desig; | |
0669bebe GB |
10221 | end if; |
10222 | end Full_Designated_Type; | |
10223 | ||
d81b4bfe TQ |
10224 | -- Local Declarations |
10225 | ||
0669bebe GB |
10226 | Target : constant Entity_Id := Full_Designated_Type (Target_Type); |
10227 | Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type); | |
10228 | ||
10229 | Same_Base : constant Boolean := | |
10230 | Base_Type (Target) = Base_Type (Opnd); | |
996ae0b0 | 10231 | |
14e33999 | 10232 | -- Start of processing for Check_Limited |
d81b4bfe | 10233 | |
996ae0b0 RK |
10234 | begin |
10235 | if Is_Tagged_Type (Target) then | |
10236 | return Valid_Tagged_Conversion (Target, Opnd); | |
10237 | ||
10238 | else | |
0669bebe | 10239 | if not Same_Base then |
996ae0b0 RK |
10240 | Error_Msg_NE |
10241 | ("target designated type not compatible with }", | |
10242 | N, Base_Type (Opnd)); | |
10243 | return False; | |
10244 | ||
da709d08 AC |
10245 | -- Ada 2005 AI-384: legality rule is symmetric in both |
10246 | -- designated types. The conversion is legal (with possible | |
10247 | -- constraint check) if either designated type is | |
10248 | -- unconstrained. | |
10249 | ||
10250 | elsif Subtypes_Statically_Match (Target, Opnd) | |
10251 | or else | |
10252 | (Has_Discriminants (Target) | |
10253 | and then | |
10254 | (not Is_Constrained (Opnd) | |
10255 | or else not Is_Constrained (Target))) | |
996ae0b0 | 10256 | then |
9fa33291 RD |
10257 | -- Special case, if Value_Size has been used to make the |
10258 | -- sizes different, the conversion is not allowed even | |
10259 | -- though the subtypes statically match. | |
10260 | ||
10261 | if Known_Static_RM_Size (Target) | |
10262 | and then Known_Static_RM_Size (Opnd) | |
10263 | and then RM_Size (Target) /= RM_Size (Opnd) | |
10264 | then | |
10265 | Error_Msg_NE | |
10266 | ("target designated subtype not compatible with }", | |
10267 | N, Opnd); | |
10268 | Error_Msg_NE | |
10269 | ("\because sizes of the two designated subtypes differ", | |
10270 | N, Opnd); | |
10271 | return False; | |
10272 | ||
10273 | -- Normal case where conversion is allowed | |
10274 | ||
10275 | else | |
10276 | return True; | |
10277 | end if; | |
da709d08 AC |
10278 | |
10279 | else | |
996ae0b0 RK |
10280 | Error_Msg_NE |
10281 | ("target designated subtype not compatible with }", | |
10282 | N, Opnd); | |
10283 | return False; | |
996ae0b0 RK |
10284 | end if; |
10285 | end if; | |
14e33999 | 10286 | end Check_Limited; |
996ae0b0 | 10287 | |
cdbf04c0 | 10288 | -- Access to subprogram types. If the operand is an access parameter, |
c147ac26 | 10289 | -- the type has a deeper accessibility that any master, and cannot |
53cf4600 ES |
10290 | -- be assigned. We must make an exception if the conversion is part |
10291 | -- of an assignment and the target is the return object of an extended | |
10292 | -- return statement, because in that case the accessibility check | |
10293 | -- takes place after the return. | |
aa180613 | 10294 | |
dce86910 | 10295 | elsif Is_Access_Subprogram_Type (Target_Type) |
bc5f3720 | 10296 | and then No (Corresponding_Remote_Type (Opnd_Type)) |
996ae0b0 | 10297 | then |
cdbf04c0 AC |
10298 | if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type |
10299 | and then Is_Entity_Name (Operand) | |
10300 | and then Ekind (Entity (Operand)) = E_In_Parameter | |
53cf4600 ES |
10301 | and then |
10302 | (Nkind (Parent (N)) /= N_Assignment_Statement | |
10303 | or else not Is_Entity_Name (Name (Parent (N))) | |
10304 | or else not Is_Return_Object (Entity (Name (Parent (N))))) | |
0669bebe GB |
10305 | then |
10306 | Error_Msg_N | |
10307 | ("illegal attempt to store anonymous access to subprogram", | |
10308 | Operand); | |
10309 | Error_Msg_N | |
10310 | ("\value has deeper accessibility than any master " & | |
aa5147f0 | 10311 | "(RM 3.10.2 (13))", |
0669bebe GB |
10312 | Operand); |
10313 | ||
c147ac26 ES |
10314 | Error_Msg_NE |
10315 | ("\use named access type for& instead of access parameter", | |
10316 | Operand, Entity (Operand)); | |
0669bebe GB |
10317 | end if; |
10318 | ||
996ae0b0 RK |
10319 | -- Check that the designated types are subtype conformant |
10320 | ||
bc5f3720 RD |
10321 | Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type), |
10322 | Old_Id => Designated_Type (Opnd_Type), | |
10323 | Err_Loc => N); | |
996ae0b0 RK |
10324 | |
10325 | -- Check the static accessibility rule of 4.6(20) | |
10326 | ||
10327 | if Type_Access_Level (Opnd_Type) > | |
10328 | Type_Access_Level (Target_Type) | |
10329 | then | |
10330 | Error_Msg_N | |
10331 | ("operand type has deeper accessibility level than target", | |
10332 | Operand); | |
10333 | ||
10334 | -- Check that if the operand type is declared in a generic body, | |
10335 | -- then the target type must be declared within that same body | |
10336 | -- (enforces last sentence of 4.6(20)). | |
10337 | ||
10338 | elsif Present (Enclosing_Generic_Body (Opnd_Type)) then | |
10339 | declare | |
10340 | O_Gen : constant Node_Id := | |
10341 | Enclosing_Generic_Body (Opnd_Type); | |
10342 | ||
1420b484 | 10343 | T_Gen : Node_Id; |
996ae0b0 RK |
10344 | |
10345 | begin | |
1420b484 | 10346 | T_Gen := Enclosing_Generic_Body (Target_Type); |
996ae0b0 RK |
10347 | while Present (T_Gen) and then T_Gen /= O_Gen loop |
10348 | T_Gen := Enclosing_Generic_Body (T_Gen); | |
10349 | end loop; | |
10350 | ||
10351 | if T_Gen /= O_Gen then | |
10352 | Error_Msg_N | |
10353 | ("target type must be declared in same generic body" | |
10354 | & " as operand type", N); | |
10355 | end if; | |
10356 | end; | |
10357 | end if; | |
10358 | ||
10359 | return True; | |
10360 | ||
aa180613 RD |
10361 | -- Remote subprogram access types |
10362 | ||
996ae0b0 RK |
10363 | elsif Is_Remote_Access_To_Subprogram_Type (Target_Type) |
10364 | and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type) | |
10365 | then | |
10366 | -- It is valid to convert from one RAS type to another provided | |
10367 | -- that their specification statically match. | |
10368 | ||
10369 | Check_Subtype_Conformant | |
10370 | (New_Id => | |
10371 | Designated_Type (Corresponding_Remote_Type (Target_Type)), | |
10372 | Old_Id => | |
10373 | Designated_Type (Corresponding_Remote_Type (Opnd_Type)), | |
10374 | Err_Loc => | |
10375 | N); | |
10376 | return True; | |
aa180613 | 10377 | |
e65f50ec | 10378 | -- If both are tagged types, check legality of view conversions |
996ae0b0 | 10379 | |
e65f50ec ES |
10380 | elsif Is_Tagged_Type (Target_Type) |
10381 | and then Is_Tagged_Type (Opnd_Type) | |
10382 | then | |
996ae0b0 RK |
10383 | return Valid_Tagged_Conversion (Target_Type, Opnd_Type); |
10384 | ||
a77842bd | 10385 | -- Types derived from the same root type are convertible |
996ae0b0 RK |
10386 | |
10387 | elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then | |
10388 | return True; | |
10389 | ||
aa5147f0 ES |
10390 | -- In an instance or an inlined body, there may be inconsistent |
10391 | -- views of the same type, or of types derived from a common root. | |
996ae0b0 | 10392 | |
aa5147f0 ES |
10393 | elsif (In_Instance or In_Inlined_Body) |
10394 | and then | |
d81b4bfe TQ |
10395 | Root_Type (Underlying_Type (Target_Type)) = |
10396 | Root_Type (Underlying_Type (Opnd_Type)) | |
996ae0b0 RK |
10397 | then |
10398 | return True; | |
10399 | ||
10400 | -- Special check for common access type error case | |
10401 | ||
10402 | elsif Ekind (Target_Type) = E_Access_Type | |
10403 | and then Is_Access_Type (Opnd_Type) | |
10404 | then | |
10405 | Error_Msg_N ("target type must be general access type!", N); | |
305caf42 AC |
10406 | Error_Msg_NE -- CODEFIX |
10407 | ("add ALL to }!", N, Target_Type); | |
996ae0b0 RK |
10408 | return False; |
10409 | ||
10410 | else | |
10411 | Error_Msg_NE ("invalid conversion, not compatible with }", | |
10412 | N, Opnd_Type); | |
996ae0b0 RK |
10413 | return False; |
10414 | end if; | |
10415 | end Valid_Conversion; | |
10416 | ||
10417 | end Sem_Res; |