]>
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 | |
5cc9353d RD |
87 | -- Typ is the type required by context. These procedures propagate the type |
88 | -- information recursively to the descendants of N. If the node is not | |
89 | -- overloaded, its Etype is established in the first pass. If overloaded, | |
90 | -- the Resolve routines set the correct type. For arith. operators, the | |
91 | -- Etype is the base type of the context. | |
996ae0b0 RK |
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 | 138 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean; |
5cc9353d RD |
139 | -- Determine whether E is an access type declared by an access declaration, |
140 | -- and not an (anonymous) allocator type. | |
67ce0d7e | 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); | |
a961aa79 | 195 | procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id); |
996ae0b0 RK |
196 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id); |
197 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id); | |
198 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id); | |
199 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id); | |
200 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id); | |
201 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id); | |
202 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id); | |
203 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id); | |
204 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id); | |
205 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id); | |
206 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id); | |
207 | procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id); | |
208 | procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id); | |
209 | ||
210 | function Operator_Kind | |
211 | (Op_Name : Name_Id; | |
0ab80019 | 212 | Is_Binary : Boolean) return Node_Kind; |
996ae0b0 RK |
213 | -- Utility to map the name of an operator into the corresponding Node. Used |
214 | -- by other node rewriting procedures. | |
215 | ||
216 | procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id); | |
bc5f3720 RD |
217 | -- Resolve actuals of call, and add default expressions for missing ones. |
218 | -- N is the Node_Id for the subprogram call, and Nam is the entity of the | |
219 | -- called subprogram. | |
996ae0b0 RK |
220 | |
221 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id); | |
222 | -- Called from Resolve_Call, when the prefix denotes an entry or element | |
223 | -- of entry family. Actuals are resolved as for subprograms, and the node | |
224 | -- is rebuilt as an entry call. Also called for protected operations. Typ | |
225 | -- is the context type, which is used when the operation is a protected | |
226 | -- function with no arguments, and the return value is indexed. | |
227 | ||
228 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id); | |
5cc9353d RD |
229 | -- A call to a user-defined intrinsic operator is rewritten as a call to |
230 | -- the corresponding predefined operator, with suitable conversions. Note | |
231 | -- that this applies only for intrinsic operators that denote predefined | |
232 | -- operators, not ones that are intrinsic imports of 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; |
5cc9353d RD |
272 | -- A universal_fixed expression in an universal context is unambiguous if |
273 | -- there is only one applicable fixed point type. Determining whether there | |
274 | -- is only one requires a search over all visible entities, and happens | |
275 | -- only in very pathological cases (see 6115-006). | |
996ae0b0 RK |
276 | |
277 | function Valid_Conversion | |
278 | (N : Node_Id; | |
279 | Target : Entity_Id; | |
0ab80019 | 280 | Operand : Node_Id) return Boolean; |
5cc9353d RD |
281 | -- Verify legality rules given in 4.6 (8-23). Target is the target type |
282 | -- of the conversion, which may be an implicit conversion of an actual | |
283 | -- parameter to an anonymous access type (in which case N denotes the | |
284 | -- actual parameter and N = Operand). | |
996ae0b0 RK |
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 | ||
0791fbe9 | 304 | if Ada_Version >= Ada_2005 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 | |
5cc9353d RD |
367 | -- This can only happen if a transient scope was created for an inner |
368 | -- expression, which will be removed upon completion of the analysis | |
369 | -- of an enclosing construct. The transient scope must have the | |
370 | -- suppress status of the enclosing environment, not of this Analyze | |
371 | -- call. | |
996ae0b0 RK |
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 | ||
5cc9353d RD |
459 | -- The following check catches the unusual case where a |
460 | -- discriminant appears within an index constraint that is part of | |
461 | -- a larger expression within a constraint on a component, e.g. "C | |
462 | -- : Int range 1 .. F (new A(1 .. D))". For now we only check case | |
463 | -- of record components, and note that a similar check should also | |
464 | -- apply in the case of discriminant constraints below. ??? | |
996ae0b0 RK |
465 | |
466 | -- Note that the check for N_Subtype_Declaration below is to | |
467 | -- detect the valid use of discriminants in the constraints of a | |
468 | -- subtype declaration when this subtype declaration appears | |
469 | -- inside the scope of a record type (which is syntactically | |
470 | -- illegal, but which may be created as part of derived type | |
471 | -- processing for records). See Sem_Ch3.Build_Derived_Record_Type | |
472 | -- for more info. | |
473 | ||
474 | if Ekind (Current_Scope) = E_Record_Type | |
475 | and then Scope (Disc) = Current_Scope | |
476 | and then not | |
477 | (Nkind (Parent (P)) = N_Subtype_Indication | |
45fc7ddb HK |
478 | and then |
479 | Nkind_In (Parent (Parent (P)), N_Component_Definition, | |
480 | N_Subtype_Declaration) | |
996ae0b0 RK |
481 | and then Paren_Count (N) = 0) |
482 | then | |
483 | Error_Msg_N | |
484 | ("discriminant must appear alone in component constraint", N); | |
485 | return; | |
486 | end if; | |
487 | ||
a0ac3932 | 488 | -- Detect a common error: |
9bc43c53 | 489 | |
996ae0b0 | 490 | -- type R (D : Positive := 100) is record |
9bc43c53 | 491 | -- Name : String (1 .. D); |
996ae0b0 RK |
492 | -- end record; |
493 | ||
a0ac3932 RD |
494 | -- The default value causes an object of type R to be allocated |
495 | -- with room for Positive'Last characters. The RM does not mandate | |
496 | -- the allocation of the maximum size, but that is what GNAT does | |
497 | -- so we should warn the programmer that there is a problem. | |
996ae0b0 | 498 | |
a0ac3932 | 499 | Check_Large : declare |
996ae0b0 RK |
500 | SI : Node_Id; |
501 | T : Entity_Id; | |
502 | TB : Node_Id; | |
503 | CB : Entity_Id; | |
504 | ||
505 | function Large_Storage_Type (T : Entity_Id) return Boolean; | |
5cc9353d RD |
506 | -- Return True if type T has a large enough range that any |
507 | -- array whose index type covered the whole range of the type | |
508 | -- would likely raise Storage_Error. | |
996ae0b0 | 509 | |
fbf5a39b AC |
510 | ------------------------ |
511 | -- Large_Storage_Type -- | |
512 | ------------------------ | |
513 | ||
996ae0b0 RK |
514 | function Large_Storage_Type (T : Entity_Id) return Boolean is |
515 | begin | |
4b92fd3c ST |
516 | -- The type is considered large if its bounds are known at |
517 | -- compile time and if it requires at least as many bits as | |
518 | -- a Positive to store the possible values. | |
519 | ||
520 | return Compile_Time_Known_Value (Type_Low_Bound (T)) | |
521 | and then Compile_Time_Known_Value (Type_High_Bound (T)) | |
522 | and then | |
523 | Minimum_Size (T, Biased => True) >= | |
a0ac3932 | 524 | RM_Size (Standard_Positive); |
996ae0b0 RK |
525 | end Large_Storage_Type; |
526 | ||
a0ac3932 RD |
527 | -- Start of processing for Check_Large |
528 | ||
996ae0b0 RK |
529 | begin |
530 | -- Check that the Disc has a large range | |
531 | ||
532 | if not Large_Storage_Type (Etype (Disc)) then | |
533 | goto No_Danger; | |
534 | end if; | |
535 | ||
536 | -- If the enclosing type is limited, we allocate only the | |
537 | -- default value, not the maximum, and there is no need for | |
538 | -- a warning. | |
539 | ||
540 | if Is_Limited_Type (Scope (Disc)) then | |
541 | goto No_Danger; | |
542 | end if; | |
543 | ||
544 | -- Check that it is the high bound | |
545 | ||
546 | if N /= High_Bound (PN) | |
c8ef728f | 547 | or else No (Discriminant_Default_Value (Disc)) |
996ae0b0 RK |
548 | then |
549 | goto No_Danger; | |
550 | end if; | |
551 | ||
5cc9353d RD |
552 | -- Check the array allows a large range at this bound. First |
553 | -- find the array | |
996ae0b0 RK |
554 | |
555 | SI := Parent (P); | |
556 | ||
557 | if Nkind (SI) /= N_Subtype_Indication then | |
558 | goto No_Danger; | |
559 | end if; | |
560 | ||
561 | T := Entity (Subtype_Mark (SI)); | |
562 | ||
563 | if not Is_Array_Type (T) then | |
564 | goto No_Danger; | |
565 | end if; | |
566 | ||
567 | -- Next, find the dimension | |
568 | ||
569 | TB := First_Index (T); | |
570 | CB := First (Constraints (P)); | |
571 | while True | |
572 | and then Present (TB) | |
573 | and then Present (CB) | |
574 | and then CB /= PN | |
575 | loop | |
576 | Next_Index (TB); | |
577 | Next (CB); | |
578 | end loop; | |
579 | ||
580 | if CB /= PN then | |
581 | goto No_Danger; | |
582 | end if; | |
583 | ||
584 | -- Now, check the dimension has a large range | |
585 | ||
586 | if not Large_Storage_Type (Etype (TB)) then | |
587 | goto No_Danger; | |
588 | end if; | |
589 | ||
590 | -- Warn about the danger | |
591 | ||
592 | Error_Msg_N | |
aa5147f0 | 593 | ("?creation of & object may raise Storage_Error!", |
fbf5a39b | 594 | Scope (Disc)); |
996ae0b0 RK |
595 | |
596 | <<No_Danger>> | |
597 | null; | |
598 | ||
a0ac3932 | 599 | end Check_Large; |
996ae0b0 RK |
600 | end if; |
601 | ||
602 | -- Legal case is in index or discriminant constraint | |
603 | ||
45fc7ddb HK |
604 | elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint, |
605 | N_Discriminant_Association) | |
996ae0b0 RK |
606 | then |
607 | if Paren_Count (N) > 0 then | |
608 | Error_Msg_N | |
609 | ("discriminant in constraint must appear alone", N); | |
758c442c GD |
610 | |
611 | elsif Nkind (N) = N_Expanded_Name | |
612 | and then Comes_From_Source (N) | |
613 | then | |
614 | Error_Msg_N | |
615 | ("discriminant must appear alone as a direct name", N); | |
996ae0b0 RK |
616 | end if; |
617 | ||
618 | return; | |
619 | ||
5cc9353d RD |
620 | -- Otherwise, context is an expression. It should not be within (i.e. a |
621 | -- subexpression of) a constraint for a component. | |
996ae0b0 RK |
622 | |
623 | else | |
624 | D := PN; | |
625 | P := Parent (PN); | |
45fc7ddb HK |
626 | while not Nkind_In (P, N_Component_Declaration, |
627 | N_Subtype_Indication, | |
628 | N_Entry_Declaration) | |
996ae0b0 RK |
629 | loop |
630 | D := P; | |
631 | P := Parent (P); | |
632 | exit when No (P); | |
633 | end loop; | |
634 | ||
5cc9353d RD |
635 | -- If the discriminant is used in an expression that is a bound of a |
636 | -- scalar type, an Itype is created and the bounds are attached to | |
637 | -- its range, not to the original subtype indication. Such use is of | |
638 | -- course a double fault. | |
996ae0b0 RK |
639 | |
640 | if (Nkind (P) = N_Subtype_Indication | |
45fc7ddb HK |
641 | and then Nkind_In (Parent (P), N_Component_Definition, |
642 | N_Derived_Type_Definition) | |
996ae0b0 RK |
643 | and then D = Constraint (P)) |
644 | ||
19fb051c AC |
645 | -- The constraint itself may be given by a subtype indication, |
646 | -- rather than by a more common discrete range. | |
996ae0b0 RK |
647 | |
648 | or else (Nkind (P) = N_Subtype_Indication | |
fbf5a39b AC |
649 | and then |
650 | Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint) | |
996ae0b0 RK |
651 | or else Nkind (P) = N_Entry_Declaration |
652 | or else Nkind (D) = N_Defining_Identifier | |
653 | then | |
654 | Error_Msg_N | |
655 | ("discriminant in constraint must appear alone", N); | |
656 | end if; | |
657 | end if; | |
658 | end Check_Discriminant_Use; | |
659 | ||
660 | -------------------------------- | |
661 | -- Check_For_Visible_Operator -- | |
662 | -------------------------------- | |
663 | ||
664 | procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is | |
996ae0b0 | 665 | begin |
fbf5a39b | 666 | if Is_Invisible_Operator (N, T) then |
305caf42 | 667 | Error_Msg_NE -- CODEFIX |
996ae0b0 | 668 | ("operator for} is not directly visible!", N, First_Subtype (T)); |
305caf42 AC |
669 | Error_Msg_N -- CODEFIX |
670 | ("use clause would make operation legal!", N); | |
996ae0b0 RK |
671 | end if; |
672 | end Check_For_Visible_Operator; | |
673 | ||
c8ef728f ES |
674 | ---------------------------------- |
675 | -- Check_Fully_Declared_Prefix -- | |
676 | ---------------------------------- | |
677 | ||
678 | procedure Check_Fully_Declared_Prefix | |
679 | (Typ : Entity_Id; | |
680 | Pref : Node_Id) | |
681 | is | |
682 | begin | |
683 | -- Check that the designated type of the prefix of a dereference is | |
684 | -- not an incomplete type. This cannot be done unconditionally, because | |
685 | -- dereferences of private types are legal in default expressions. This | |
686 | -- case is taken care of in Check_Fully_Declared, called below. There | |
687 | -- are also 2005 cases where it is legal for the prefix to be unfrozen. | |
688 | ||
689 | -- This consideration also applies to similar checks for allocators, | |
690 | -- qualified expressions, and type conversions. | |
691 | ||
692 | -- An additional exception concerns other per-object expressions that | |
693 | -- are not directly related to component declarations, in particular | |
694 | -- representation pragmas for tasks. These will be per-object | |
695 | -- expressions if they depend on discriminants or some global entity. | |
696 | -- If the task has access discriminants, the designated type may be | |
697 | -- incomplete at the point the expression is resolved. This resolution | |
698 | -- takes place within the body of the initialization procedure, where | |
699 | -- the discriminant is replaced by its discriminal. | |
700 | ||
701 | if Is_Entity_Name (Pref) | |
702 | and then Ekind (Entity (Pref)) = E_In_Parameter | |
703 | then | |
704 | null; | |
705 | ||
706 | -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages | |
707 | -- are handled by Analyze_Access_Attribute, Analyze_Assignment, | |
708 | -- Analyze_Object_Renaming, and Freeze_Entity. | |
709 | ||
0791fbe9 | 710 | elsif Ada_Version >= Ada_2005 |
c8ef728f | 711 | and then Is_Entity_Name (Pref) |
811c6a85 | 712 | and then Is_Access_Type (Etype (Pref)) |
c8ef728f ES |
713 | and then Ekind (Directly_Designated_Type (Etype (Pref))) = |
714 | E_Incomplete_Type | |
715 | and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref))) | |
716 | then | |
717 | null; | |
718 | else | |
719 | Check_Fully_Declared (Typ, Parent (Pref)); | |
720 | end if; | |
721 | end Check_Fully_Declared_Prefix; | |
722 | ||
996ae0b0 RK |
723 | ------------------------------ |
724 | -- Check_Infinite_Recursion -- | |
725 | ------------------------------ | |
726 | ||
727 | function Check_Infinite_Recursion (N : Node_Id) return Boolean is | |
728 | P : Node_Id; | |
729 | C : Node_Id; | |
730 | ||
07fc65c4 | 731 | function Same_Argument_List return Boolean; |
5cc9353d RD |
732 | -- Check whether list of actuals is identical to list of formals of |
733 | -- called function (which is also the enclosing scope). | |
07fc65c4 GB |
734 | |
735 | ------------------------ | |
736 | -- Same_Argument_List -- | |
737 | ------------------------ | |
738 | ||
739 | function Same_Argument_List return Boolean is | |
740 | A : Node_Id; | |
741 | F : Entity_Id; | |
742 | Subp : Entity_Id; | |
743 | ||
744 | begin | |
745 | if not Is_Entity_Name (Name (N)) then | |
746 | return False; | |
747 | else | |
748 | Subp := Entity (Name (N)); | |
749 | end if; | |
750 | ||
751 | F := First_Formal (Subp); | |
752 | A := First_Actual (N); | |
07fc65c4 GB |
753 | while Present (F) and then Present (A) loop |
754 | if not Is_Entity_Name (A) | |
755 | or else Entity (A) /= F | |
756 | then | |
757 | return False; | |
758 | end if; | |
759 | ||
760 | Next_Actual (A); | |
761 | Next_Formal (F); | |
762 | end loop; | |
763 | ||
764 | return True; | |
765 | end Same_Argument_List; | |
766 | ||
767 | -- Start of processing for Check_Infinite_Recursion | |
768 | ||
996ae0b0 | 769 | begin |
26570b21 RD |
770 | -- Special case, if this is a procedure call and is a call to the |
771 | -- current procedure with the same argument list, then this is for | |
772 | -- sure an infinite recursion and we insert a call to raise SE. | |
773 | ||
774 | if Is_List_Member (N) | |
775 | and then List_Length (List_Containing (N)) = 1 | |
776 | and then Same_Argument_List | |
777 | then | |
778 | declare | |
779 | P : constant Node_Id := Parent (N); | |
780 | begin | |
781 | if Nkind (P) = N_Handled_Sequence_Of_Statements | |
782 | and then Nkind (Parent (P)) = N_Subprogram_Body | |
783 | and then Is_Empty_List (Declarations (Parent (P))) | |
784 | then | |
785 | Error_Msg_N ("!?infinite recursion", N); | |
786 | Error_Msg_N ("\!?Storage_Error will be raised at run time", N); | |
787 | Insert_Action (N, | |
788 | Make_Raise_Storage_Error (Sloc (N), | |
789 | Reason => SE_Infinite_Recursion)); | |
790 | return True; | |
791 | end if; | |
792 | end; | |
793 | end if; | |
794 | ||
795 | -- If not that special case, search up tree, quitting if we reach a | |
796 | -- construct (e.g. a conditional) that tells us that this is not a | |
797 | -- case for an infinite recursion warning. | |
996ae0b0 RK |
798 | |
799 | C := N; | |
800 | loop | |
801 | P := Parent (C); | |
9a7da240 RD |
802 | |
803 | -- If no parent, then we were not inside a subprogram, this can for | |
804 | -- example happen when processing certain pragmas in a spec. Just | |
805 | -- return False in this case. | |
806 | ||
807 | if No (P) then | |
808 | return False; | |
809 | end if; | |
810 | ||
811 | -- Done if we get to subprogram body, this is definitely an infinite | |
812 | -- recursion case if we did not find anything to stop us. | |
813 | ||
996ae0b0 | 814 | exit when Nkind (P) = N_Subprogram_Body; |
9a7da240 RD |
815 | |
816 | -- If appearing in conditional, result is false | |
817 | ||
45fc7ddb HK |
818 | if Nkind_In (P, N_Or_Else, |
819 | N_And_Then, | |
d347f572 AC |
820 | N_Case_Expression, |
821 | N_Case_Statement, | |
822 | N_Conditional_Expression, | |
823 | N_If_Statement) | |
996ae0b0 RK |
824 | then |
825 | return False; | |
826 | ||
827 | elsif Nkind (P) = N_Handled_Sequence_Of_Statements | |
828 | and then C /= First (Statements (P)) | |
829 | then | |
26570b21 RD |
830 | -- If the call is the expression of a return statement and the |
831 | -- actuals are identical to the formals, it's worth a warning. | |
832 | -- However, we skip this if there is an immediately preceding | |
833 | -- raise statement, since the call is never executed. | |
07fc65c4 GB |
834 | |
835 | -- Furthermore, this corresponds to a common idiom: | |
836 | ||
837 | -- function F (L : Thing) return Boolean is | |
838 | -- begin | |
839 | -- raise Program_Error; | |
840 | -- return F (L); | |
841 | -- end F; | |
842 | ||
843 | -- for generating a stub function | |
844 | ||
aa5147f0 | 845 | if Nkind (Parent (N)) = N_Simple_Return_Statement |
07fc65c4 GB |
846 | and then Same_Argument_List |
847 | then | |
9ebe3743 HK |
848 | exit when not Is_List_Member (Parent (N)); |
849 | ||
850 | -- OK, return statement is in a statement list, look for raise | |
851 | ||
852 | declare | |
853 | Nod : Node_Id; | |
854 | ||
855 | begin | |
856 | -- Skip past N_Freeze_Entity nodes generated by expansion | |
857 | ||
858 | Nod := Prev (Parent (N)); | |
859 | while Present (Nod) | |
860 | and then Nkind (Nod) = N_Freeze_Entity | |
861 | loop | |
862 | Prev (Nod); | |
863 | end loop; | |
864 | ||
865 | -- If no raise statement, give warning | |
866 | ||
867 | exit when Nkind (Nod) /= N_Raise_Statement | |
868 | and then | |
869 | (Nkind (Nod) not in N_Raise_xxx_Error | |
19fb051c | 870 | or else Present (Condition (Nod))); |
9ebe3743 | 871 | end; |
07fc65c4 GB |
872 | end if; |
873 | ||
996ae0b0 RK |
874 | return False; |
875 | ||
876 | else | |
877 | C := P; | |
878 | end if; | |
879 | end loop; | |
880 | ||
aa5147f0 ES |
881 | Error_Msg_N ("!?possible infinite recursion", N); |
882 | Error_Msg_N ("\!?Storage_Error may be raised at run time", N); | |
996ae0b0 RK |
883 | |
884 | return True; | |
885 | end Check_Infinite_Recursion; | |
886 | ||
887 | ------------------------------- | |
888 | -- Check_Initialization_Call -- | |
889 | ------------------------------- | |
890 | ||
891 | procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is | |
fbf5a39b | 892 | Typ : constant Entity_Id := Etype (First_Formal (Nam)); |
996ae0b0 RK |
893 | |
894 | function Uses_SS (T : Entity_Id) return Boolean; | |
07fc65c4 GB |
895 | -- Check whether the creation of an object of the type will involve |
896 | -- use of the secondary stack. If T is a record type, this is true | |
f3d57416 | 897 | -- if the expression for some component uses the secondary stack, e.g. |
07fc65c4 GB |
898 | -- through a call to a function that returns an unconstrained value. |
899 | -- False if T is controlled, because cleanups occur elsewhere. | |
900 | ||
901 | ------------- | |
902 | -- Uses_SS -- | |
903 | ------------- | |
996ae0b0 RK |
904 | |
905 | function Uses_SS (T : Entity_Id) return Boolean is | |
aa5147f0 ES |
906 | Comp : Entity_Id; |
907 | Expr : Node_Id; | |
908 | Full_Type : Entity_Id := Underlying_Type (T); | |
996ae0b0 RK |
909 | |
910 | begin | |
aa5147f0 ES |
911 | -- Normally we want to use the underlying type, but if it's not set |
912 | -- then continue with T. | |
913 | ||
914 | if not Present (Full_Type) then | |
915 | Full_Type := T; | |
916 | end if; | |
917 | ||
918 | if Is_Controlled (Full_Type) then | |
996ae0b0 RK |
919 | return False; |
920 | ||
aa5147f0 ES |
921 | elsif Is_Array_Type (Full_Type) then |
922 | return Uses_SS (Component_Type (Full_Type)); | |
996ae0b0 | 923 | |
aa5147f0 ES |
924 | elsif Is_Record_Type (Full_Type) then |
925 | Comp := First_Component (Full_Type); | |
996ae0b0 | 926 | while Present (Comp) loop |
996ae0b0 RK |
927 | if Ekind (Comp) = E_Component |
928 | and then Nkind (Parent (Comp)) = N_Component_Declaration | |
929 | then | |
aa5147f0 ES |
930 | -- The expression for a dynamic component may be rewritten |
931 | -- as a dereference, so retrieve original node. | |
932 | ||
933 | Expr := Original_Node (Expression (Parent (Comp))); | |
996ae0b0 | 934 | |
aa5147f0 | 935 | -- Return True if the expression is a call to a function |
1d57c04f AC |
936 | -- (including an attribute function such as Image, or a |
937 | -- user-defined operator) with a result that requires a | |
938 | -- transient scope. | |
fbf5a39b | 939 | |
aa5147f0 | 940 | if (Nkind (Expr) = N_Function_Call |
1d57c04f | 941 | or else Nkind (Expr) in N_Op |
aa5147f0 ES |
942 | or else (Nkind (Expr) = N_Attribute_Reference |
943 | and then Present (Expressions (Expr)))) | |
996ae0b0 RK |
944 | and then Requires_Transient_Scope (Etype (Expr)) |
945 | then | |
946 | return True; | |
947 | ||
948 | elsif Uses_SS (Etype (Comp)) then | |
949 | return True; | |
950 | end if; | |
951 | end if; | |
952 | ||
953 | Next_Component (Comp); | |
954 | end loop; | |
955 | ||
956 | return False; | |
957 | ||
958 | else | |
959 | return False; | |
960 | end if; | |
961 | end Uses_SS; | |
962 | ||
07fc65c4 GB |
963 | -- Start of processing for Check_Initialization_Call |
964 | ||
996ae0b0 | 965 | begin |
0669bebe | 966 | -- Establish a transient scope if the type needs it |
07fc65c4 | 967 | |
0669bebe | 968 | if Uses_SS (Typ) then |
996ae0b0 RK |
969 | Establish_Transient_Scope (First_Actual (N), Sec_Stack => True); |
970 | end if; | |
971 | end Check_Initialization_Call; | |
972 | ||
f61580d4 AC |
973 | --------------------------------------- |
974 | -- Check_No_Direct_Boolean_Operators -- | |
975 | --------------------------------------- | |
976 | ||
977 | procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is | |
978 | begin | |
979 | if Scope (Entity (N)) = Standard_Standard | |
980 | and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean | |
981 | then | |
6fb4cdde | 982 | -- Restriction only applies to original source code |
f61580d4 | 983 | |
6fb4cdde | 984 | if Comes_From_Source (N) then |
f61580d4 AC |
985 | Check_Restriction (No_Direct_Boolean_Operators, N); |
986 | end if; | |
987 | end if; | |
a36c1c3e RD |
988 | |
989 | if Style_Check then | |
990 | Check_Boolean_Operator (N); | |
991 | end if; | |
f61580d4 AC |
992 | end Check_No_Direct_Boolean_Operators; |
993 | ||
996ae0b0 RK |
994 | ------------------------------ |
995 | -- Check_Parameterless_Call -- | |
996 | ------------------------------ | |
997 | ||
998 | procedure Check_Parameterless_Call (N : Node_Id) is | |
999 | Nam : Node_Id; | |
1000 | ||
bc5f3720 RD |
1001 | function Prefix_Is_Access_Subp return Boolean; |
1002 | -- If the prefix is of an access_to_subprogram type, the node must be | |
1003 | -- rewritten as a call. Ditto if the prefix is overloaded and all its | |
1004 | -- interpretations are access to subprograms. | |
1005 | ||
1006 | --------------------------- | |
1007 | -- Prefix_Is_Access_Subp -- | |
1008 | --------------------------- | |
1009 | ||
1010 | function Prefix_Is_Access_Subp return Boolean is | |
1011 | I : Interp_Index; | |
1012 | It : Interp; | |
1013 | ||
1014 | begin | |
22b77f68 | 1015 | -- If the context is an attribute reference that can apply to |
b4a4936b | 1016 | -- functions, this is never a parameterless call (RM 4.1.4(6)). |
96d2756f AC |
1017 | |
1018 | if Nkind (Parent (N)) = N_Attribute_Reference | |
19fb051c AC |
1019 | and then (Attribute_Name (Parent (N)) = Name_Address or else |
1020 | Attribute_Name (Parent (N)) = Name_Code_Address or else | |
1021 | Attribute_Name (Parent (N)) = Name_Access) | |
96d2756f AC |
1022 | then |
1023 | return False; | |
1024 | end if; | |
1025 | ||
bc5f3720 RD |
1026 | if not Is_Overloaded (N) then |
1027 | return | |
1028 | Ekind (Etype (N)) = E_Subprogram_Type | |
1029 | and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type; | |
1030 | else | |
1031 | Get_First_Interp (N, I, It); | |
1032 | while Present (It.Typ) loop | |
1033 | if Ekind (It.Typ) /= E_Subprogram_Type | |
1034 | or else Base_Type (Etype (It.Typ)) = Standard_Void_Type | |
1035 | then | |
1036 | return False; | |
1037 | end if; | |
1038 | ||
1039 | Get_Next_Interp (I, It); | |
1040 | end loop; | |
1041 | ||
1042 | return True; | |
1043 | end if; | |
1044 | end Prefix_Is_Access_Subp; | |
1045 | ||
1046 | -- Start of processing for Check_Parameterless_Call | |
1047 | ||
996ae0b0 | 1048 | begin |
07fc65c4 GB |
1049 | -- Defend against junk stuff if errors already detected |
1050 | ||
1051 | if Total_Errors_Detected /= 0 then | |
1052 | if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then | |
1053 | return; | |
1054 | elsif Nkind (N) in N_Has_Chars | |
1055 | and then Chars (N) in Error_Name_Or_No_Name | |
1056 | then | |
1057 | return; | |
1058 | end if; | |
fbf5a39b AC |
1059 | |
1060 | Require_Entity (N); | |
996ae0b0 RK |
1061 | end if; |
1062 | ||
45fc7ddb HK |
1063 | -- If the context expects a value, and the name is a procedure, this is |
1064 | -- most likely a missing 'Access. Don't try to resolve the parameterless | |
1065 | -- call, error will be caught when the outer call is analyzed. | |
18c0ecbe AC |
1066 | |
1067 | if Is_Entity_Name (N) | |
1068 | and then Ekind (Entity (N)) = E_Procedure | |
1069 | and then not Is_Overloaded (N) | |
1070 | and then | |
45fc7ddb HK |
1071 | Nkind_In (Parent (N), N_Parameter_Association, |
1072 | N_Function_Call, | |
1073 | N_Procedure_Call_Statement) | |
18c0ecbe AC |
1074 | then |
1075 | return; | |
1076 | end if; | |
1077 | ||
45fc7ddb HK |
1078 | -- Rewrite as call if overloadable entity that is (or could be, in the |
1079 | -- overloaded case) a function call. If we know for sure that the entity | |
1080 | -- is an enumeration literal, we do not rewrite it. | |
f4b049db | 1081 | |
e1d9659d AC |
1082 | -- If the entity is the name of an operator, it cannot be a call because |
1083 | -- operators cannot have default parameters. In this case, this must be | |
1084 | -- a string whose contents coincide with an operator name. Set the kind | |
96d2756f | 1085 | -- of the node appropriately. |
996ae0b0 RK |
1086 | |
1087 | if (Is_Entity_Name (N) | |
e1d9659d | 1088 | and then Nkind (N) /= N_Operator_Symbol |
996ae0b0 RK |
1089 | and then Is_Overloadable (Entity (N)) |
1090 | and then (Ekind (Entity (N)) /= E_Enumeration_Literal | |
964f13da | 1091 | or else Is_Overloaded (N))) |
996ae0b0 | 1092 | |
09494c32 | 1093 | -- Rewrite as call if it is an explicit dereference of an expression of |
f3d57416 | 1094 | -- a subprogram access type, and the subprogram type is not that of a |
996ae0b0 RK |
1095 | -- procedure or entry. |
1096 | ||
1097 | or else | |
bc5f3720 | 1098 | (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp) |
996ae0b0 RK |
1099 | |
1100 | -- Rewrite as call if it is a selected component which is a function, | |
1101 | -- this is the case of a call to a protected function (which may be | |
1102 | -- overloaded with other protected operations). | |
1103 | ||
1104 | or else | |
1105 | (Nkind (N) = N_Selected_Component | |
1106 | and then (Ekind (Entity (Selector_Name (N))) = E_Function | |
964f13da RD |
1107 | or else |
1108 | (Ekind_In (Entity (Selector_Name (N)), E_Entry, | |
1109 | E_Procedure) | |
1110 | and then Is_Overloaded (Selector_Name (N))))) | |
996ae0b0 | 1111 | |
5cc9353d RD |
1112 | -- If one of the above three conditions is met, rewrite as call. Apply |
1113 | -- the rewriting only once. | |
996ae0b0 RK |
1114 | |
1115 | then | |
1116 | if Nkind (Parent (N)) /= N_Function_Call | |
1117 | or else N /= Name (Parent (N)) | |
1118 | then | |
1119 | Nam := New_Copy (N); | |
1120 | ||
bc5f3720 | 1121 | -- If overloaded, overload set belongs to new copy |
996ae0b0 RK |
1122 | |
1123 | Save_Interps (N, Nam); | |
1124 | ||
1125 | -- Change node to parameterless function call (note that the | |
1126 | -- Parameter_Associations associations field is left set to Empty, | |
1127 | -- its normal default value since there are no parameters) | |
1128 | ||
1129 | Change_Node (N, N_Function_Call); | |
1130 | Set_Name (N, Nam); | |
1131 | Set_Sloc (N, Sloc (Nam)); | |
1132 | Analyze_Call (N); | |
1133 | end if; | |
1134 | ||
1135 | elsif Nkind (N) = N_Parameter_Association then | |
1136 | Check_Parameterless_Call (Explicit_Actual_Parameter (N)); | |
e1d9659d AC |
1137 | |
1138 | elsif Nkind (N) = N_Operator_Symbol then | |
1139 | Change_Operator_Symbol_To_String_Literal (N); | |
1140 | Set_Is_Overloaded (N, False); | |
1141 | Set_Etype (N, Any_String); | |
996ae0b0 RK |
1142 | end if; |
1143 | end Check_Parameterless_Call; | |
1144 | ||
67ce0d7e RD |
1145 | ----------------------------- |
1146 | -- Is_Definite_Access_Type -- | |
1147 | ----------------------------- | |
1148 | ||
1149 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean is | |
1150 | Btyp : constant Entity_Id := Base_Type (E); | |
1151 | begin | |
1152 | return Ekind (Btyp) = E_Access_Type | |
1153 | or else (Ekind (Btyp) = E_Access_Subprogram_Type | |
72e9f2b9 | 1154 | and then Comes_From_Source (Btyp)); |
67ce0d7e RD |
1155 | end Is_Definite_Access_Type; |
1156 | ||
996ae0b0 RK |
1157 | ---------------------- |
1158 | -- Is_Predefined_Op -- | |
1159 | ---------------------- | |
1160 | ||
1161 | function Is_Predefined_Op (Nam : Entity_Id) return Boolean is | |
1162 | begin | |
6a497607 AC |
1163 | -- Predefined operators are intrinsic subprograms |
1164 | ||
1165 | if not Is_Intrinsic_Subprogram (Nam) then | |
1166 | return False; | |
1167 | end if; | |
1168 | ||
1169 | -- A call to a back-end builtin is never a predefined operator | |
1170 | ||
1171 | if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then | |
1172 | return False; | |
1173 | end if; | |
1174 | ||
1175 | return not Is_Generic_Instance (Nam) | |
996ae0b0 | 1176 | and then Chars (Nam) in Any_Operator_Name |
6a497607 | 1177 | and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam))); |
996ae0b0 RK |
1178 | end Is_Predefined_Op; |
1179 | ||
1180 | ----------------------------- | |
1181 | -- Make_Call_Into_Operator -- | |
1182 | ----------------------------- | |
1183 | ||
1184 | procedure Make_Call_Into_Operator | |
1185 | (N : Node_Id; | |
1186 | Typ : Entity_Id; | |
1187 | Op_Id : Entity_Id) | |
1188 | is | |
1189 | Op_Name : constant Name_Id := Chars (Op_Id); | |
1190 | Act1 : Node_Id := First_Actual (N); | |
1191 | Act2 : Node_Id := Next_Actual (Act1); | |
1192 | Error : Boolean := False; | |
2820d220 AC |
1193 | Func : constant Entity_Id := Entity (Name (N)); |
1194 | Is_Binary : constant Boolean := Present (Act2); | |
996ae0b0 RK |
1195 | Op_Node : Node_Id; |
1196 | Opnd_Type : Entity_Id; | |
1197 | Orig_Type : Entity_Id := Empty; | |
1198 | Pack : Entity_Id; | |
1199 | ||
1200 | type Kind_Test is access function (E : Entity_Id) return Boolean; | |
1201 | ||
996ae0b0 | 1202 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean; |
b4a4936b AC |
1203 | -- If the operand is not universal, and the operator is given by an |
1204 | -- expanded name, verify that the operand has an interpretation with a | |
1205 | -- type defined in the given scope of the operator. | |
996ae0b0 RK |
1206 | |
1207 | function Type_In_P (Test : Kind_Test) return Entity_Id; | |
b4a4936b AC |
1208 | -- Find a type of the given class in package Pack that contains the |
1209 | -- operator. | |
996ae0b0 | 1210 | |
996ae0b0 RK |
1211 | --------------------------- |
1212 | -- Operand_Type_In_Scope -- | |
1213 | --------------------------- | |
1214 | ||
1215 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean is | |
1216 | Nod : constant Node_Id := Right_Opnd (Op_Node); | |
1217 | I : Interp_Index; | |
1218 | It : Interp; | |
1219 | ||
1220 | begin | |
1221 | if not Is_Overloaded (Nod) then | |
1222 | return Scope (Base_Type (Etype (Nod))) = S; | |
1223 | ||
1224 | else | |
1225 | Get_First_Interp (Nod, I, It); | |
996ae0b0 | 1226 | while Present (It.Typ) loop |
996ae0b0 RK |
1227 | if Scope (Base_Type (It.Typ)) = S then |
1228 | return True; | |
1229 | end if; | |
1230 | ||
1231 | Get_Next_Interp (I, It); | |
1232 | end loop; | |
1233 | ||
1234 | return False; | |
1235 | end if; | |
1236 | end Operand_Type_In_Scope; | |
1237 | ||
1238 | --------------- | |
1239 | -- Type_In_P -- | |
1240 | --------------- | |
1241 | ||
1242 | function Type_In_P (Test : Kind_Test) return Entity_Id is | |
1243 | E : Entity_Id; | |
1244 | ||
1245 | function In_Decl return Boolean; | |
1246 | -- Verify that node is not part of the type declaration for the | |
1247 | -- candidate type, which would otherwise be invisible. | |
1248 | ||
1249 | ------------- | |
1250 | -- In_Decl -- | |
1251 | ------------- | |
1252 | ||
1253 | function In_Decl return Boolean is | |
1254 | Decl_Node : constant Node_Id := Parent (E); | |
1255 | N2 : Node_Id; | |
1256 | ||
1257 | begin | |
1258 | N2 := N; | |
1259 | ||
1260 | if Etype (E) = Any_Type then | |
1261 | return True; | |
1262 | ||
1263 | elsif No (Decl_Node) then | |
1264 | return False; | |
1265 | ||
1266 | else | |
1267 | while Present (N2) | |
1268 | and then Nkind (N2) /= N_Compilation_Unit | |
1269 | loop | |
1270 | if N2 = Decl_Node then | |
1271 | return True; | |
1272 | else | |
1273 | N2 := Parent (N2); | |
1274 | end if; | |
1275 | end loop; | |
1276 | ||
1277 | return False; | |
1278 | end if; | |
1279 | end In_Decl; | |
1280 | ||
1281 | -- Start of processing for Type_In_P | |
1282 | ||
1283 | begin | |
b4a4936b AC |
1284 | -- If the context type is declared in the prefix package, this is the |
1285 | -- desired base type. | |
996ae0b0 | 1286 | |
b4a4936b | 1287 | if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then |
996ae0b0 RK |
1288 | return Base_Type (Typ); |
1289 | ||
1290 | else | |
1291 | E := First_Entity (Pack); | |
996ae0b0 | 1292 | while Present (E) loop |
996ae0b0 RK |
1293 | if Test (E) |
1294 | and then not In_Decl | |
1295 | then | |
1296 | return E; | |
1297 | end if; | |
1298 | ||
1299 | Next_Entity (E); | |
1300 | end loop; | |
1301 | ||
1302 | return Empty; | |
1303 | end if; | |
1304 | end Type_In_P; | |
1305 | ||
996ae0b0 RK |
1306 | -- Start of processing for Make_Call_Into_Operator |
1307 | ||
1308 | begin | |
1309 | Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N)); | |
1310 | ||
1311 | -- Binary operator | |
1312 | ||
1313 | if Is_Binary then | |
1314 | Set_Left_Opnd (Op_Node, Relocate_Node (Act1)); | |
1315 | Set_Right_Opnd (Op_Node, Relocate_Node (Act2)); | |
1316 | Save_Interps (Act1, Left_Opnd (Op_Node)); | |
1317 | Save_Interps (Act2, Right_Opnd (Op_Node)); | |
1318 | Act1 := Left_Opnd (Op_Node); | |
1319 | Act2 := Right_Opnd (Op_Node); | |
1320 | ||
1321 | -- Unary operator | |
1322 | ||
1323 | else | |
1324 | Set_Right_Opnd (Op_Node, Relocate_Node (Act1)); | |
1325 | Save_Interps (Act1, Right_Opnd (Op_Node)); | |
1326 | Act1 := Right_Opnd (Op_Node); | |
1327 | end if; | |
1328 | ||
1329 | -- If the operator is denoted by an expanded name, and the prefix is | |
1330 | -- not Standard, but the operator is a predefined one whose scope is | |
1331 | -- Standard, then this is an implicit_operator, inserted as an | |
1332 | -- interpretation by the procedure of the same name. This procedure | |
1333 | -- overestimates the presence of implicit operators, because it does | |
1334 | -- not examine the type of the operands. Verify now that the operand | |
1335 | -- type appears in the given scope. If right operand is universal, | |
1336 | -- check the other operand. In the case of concatenation, either | |
1337 | -- argument can be the component type, so check the type of the result. | |
1338 | -- If both arguments are literals, look for a type of the right kind | |
1339 | -- defined in the given scope. This elaborate nonsense is brought to | |
1340 | -- you courtesy of b33302a. The type itself must be frozen, so we must | |
1341 | -- find the type of the proper class in the given scope. | |
1342 | ||
06f2efd7 TQ |
1343 | -- A final wrinkle is the multiplication operator for fixed point types, |
1344 | -- which is defined in Standard only, and not in the scope of the | |
b4a4936b | 1345 | -- fixed point type itself. |
996ae0b0 RK |
1346 | |
1347 | if Nkind (Name (N)) = N_Expanded_Name then | |
1348 | Pack := Entity (Prefix (Name (N))); | |
1349 | ||
06f2efd7 TQ |
1350 | -- If the entity being called is defined in the given package, it is |
1351 | -- a renaming of a predefined operator, and known to be legal. | |
996ae0b0 RK |
1352 | |
1353 | if Scope (Entity (Name (N))) = Pack | |
1354 | and then Pack /= Standard_Standard | |
1355 | then | |
1356 | null; | |
1357 | ||
9ebe3743 HK |
1358 | -- Visibility does not need to be checked in an instance: if the |
1359 | -- operator was not visible in the generic it has been diagnosed | |
1360 | -- already, else there is an implicit copy of it in the instance. | |
1361 | ||
1362 | elsif In_Instance then | |
1363 | null; | |
1364 | ||
12577815 | 1365 | elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide) |
996ae0b0 RK |
1366 | and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node))) |
1367 | and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node))) | |
1368 | then | |
1369 | if Pack /= Standard_Standard then | |
1370 | Error := True; | |
1371 | end if; | |
1372 | ||
b4a4936b | 1373 | -- Ada 2005 AI-420: Predefined equality on Universal_Access is |
06f2efd7 | 1374 | -- available. |
c8ef728f | 1375 | |
0791fbe9 | 1376 | elsif Ada_Version >= Ada_2005 |
c8ef728f ES |
1377 | and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) |
1378 | and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type | |
1379 | then | |
1380 | null; | |
1381 | ||
996ae0b0 RK |
1382 | else |
1383 | Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node))); | |
1384 | ||
1385 | if Op_Name = Name_Op_Concat then | |
1386 | Opnd_Type := Base_Type (Typ); | |
1387 | ||
1388 | elsif (Scope (Opnd_Type) = Standard_Standard | |
1389 | and then Is_Binary) | |
1390 | or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference | |
1391 | and then Is_Binary | |
1392 | and then not Comes_From_Source (Opnd_Type)) | |
1393 | then | |
1394 | Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node))); | |
1395 | end if; | |
1396 | ||
1397 | if Scope (Opnd_Type) = Standard_Standard then | |
1398 | ||
1399 | -- Verify that the scope contains a type that corresponds to | |
1400 | -- the given literal. Optimize the case where Pack is Standard. | |
1401 | ||
1402 | if Pack /= Standard_Standard then | |
1403 | ||
1404 | if Opnd_Type = Universal_Integer then | |
06f2efd7 | 1405 | Orig_Type := Type_In_P (Is_Integer_Type'Access); |
996ae0b0 RK |
1406 | |
1407 | elsif Opnd_Type = Universal_Real then | |
1408 | Orig_Type := Type_In_P (Is_Real_Type'Access); | |
1409 | ||
1410 | elsif Opnd_Type = Any_String then | |
1411 | Orig_Type := Type_In_P (Is_String_Type'Access); | |
1412 | ||
1413 | elsif Opnd_Type = Any_Access then | |
06f2efd7 | 1414 | Orig_Type := Type_In_P (Is_Definite_Access_Type'Access); |
996ae0b0 RK |
1415 | |
1416 | elsif Opnd_Type = Any_Composite then | |
1417 | Orig_Type := Type_In_P (Is_Composite_Type'Access); | |
1418 | ||
1419 | if Present (Orig_Type) then | |
1420 | if Has_Private_Component (Orig_Type) then | |
1421 | Orig_Type := Empty; | |
1422 | else | |
1423 | Set_Etype (Act1, Orig_Type); | |
1424 | ||
1425 | if Is_Binary then | |
1426 | Set_Etype (Act2, Orig_Type); | |
1427 | end if; | |
1428 | end if; | |
1429 | end if; | |
1430 | ||
1431 | else | |
1432 | Orig_Type := Empty; | |
1433 | end if; | |
1434 | ||
1435 | Error := No (Orig_Type); | |
1436 | end if; | |
1437 | ||
1438 | elsif Ekind (Opnd_Type) = E_Allocator_Type | |
1439 | and then No (Type_In_P (Is_Definite_Access_Type'Access)) | |
1440 | then | |
1441 | Error := True; | |
1442 | ||
1443 | -- If the type is defined elsewhere, and the operator is not | |
1444 | -- defined in the given scope (by a renaming declaration, e.g.) | |
1445 | -- then this is an error as well. If an extension of System is | |
1446 | -- present, and the type may be defined there, Pack must be | |
1447 | -- System itself. | |
1448 | ||
1449 | elsif Scope (Opnd_Type) /= Pack | |
1450 | and then Scope (Op_Id) /= Pack | |
1451 | and then (No (System_Aux_Id) | |
1452 | or else Scope (Opnd_Type) /= System_Aux_Id | |
1453 | or else Pack /= Scope (System_Aux_Id)) | |
1454 | then | |
244e5a2c AC |
1455 | if not Is_Overloaded (Right_Opnd (Op_Node)) then |
1456 | Error := True; | |
1457 | else | |
1458 | Error := not Operand_Type_In_Scope (Pack); | |
1459 | end if; | |
996ae0b0 RK |
1460 | |
1461 | elsif Pack = Standard_Standard | |
1462 | and then not Operand_Type_In_Scope (Standard_Standard) | |
1463 | then | |
1464 | Error := True; | |
1465 | end if; | |
1466 | end if; | |
1467 | ||
1468 | if Error then | |
1469 | Error_Msg_Node_2 := Pack; | |
1470 | Error_Msg_NE | |
1471 | ("& not declared in&", N, Selector_Name (Name (N))); | |
1472 | Set_Etype (N, Any_Type); | |
1473 | return; | |
88b17d45 AC |
1474 | |
1475 | -- Detect a mismatch between the context type and the result type | |
1476 | -- in the named package, which is otherwise not detected if the | |
1477 | -- operands are universal. Check is only needed if source entity is | |
1478 | -- an operator, not a function that renames an operator. | |
1479 | ||
1480 | elsif Nkind (Parent (N)) /= N_Type_Conversion | |
1481 | and then Ekind (Entity (Name (N))) = E_Operator | |
1482 | and then Is_Numeric_Type (Typ) | |
1483 | and then not Is_Universal_Numeric_Type (Typ) | |
1484 | and then Scope (Base_Type (Typ)) /= Pack | |
1485 | and then not In_Instance | |
1486 | then | |
1487 | if Is_Fixed_Point_Type (Typ) | |
1488 | and then (Op_Name = Name_Op_Multiply | |
1489 | or else | |
1490 | Op_Name = Name_Op_Divide) | |
1491 | then | |
1492 | -- Already checked above | |
1493 | ||
1494 | null; | |
1495 | ||
e86a3a7e | 1496 | -- Operator may be defined in an extension of System |
80c3be7a AC |
1497 | |
1498 | elsif Present (System_Aux_Id) | |
1499 | and then Scope (Opnd_Type) = System_Aux_Id | |
1500 | then | |
1501 | null; | |
1502 | ||
88b17d45 | 1503 | else |
be5a1b93 TQ |
1504 | -- Could we use Wrong_Type here??? (this would require setting |
1505 | -- Etype (N) to the actual type found where Typ was expected). | |
1506 | ||
e86a3a7e | 1507 | Error_Msg_NE ("expect }", N, Typ); |
88b17d45 | 1508 | end if; |
996ae0b0 RK |
1509 | end if; |
1510 | end if; | |
1511 | ||
1512 | Set_Chars (Op_Node, Op_Name); | |
fbf5a39b AC |
1513 | |
1514 | if not Is_Private_Type (Etype (N)) then | |
1515 | Set_Etype (Op_Node, Base_Type (Etype (N))); | |
1516 | else | |
1517 | Set_Etype (Op_Node, Etype (N)); | |
1518 | end if; | |
1519 | ||
2820d220 AC |
1520 | -- If this is a call to a function that renames a predefined equality, |
1521 | -- the renaming declaration provides a type that must be used to | |
1522 | -- resolve the operands. This must be done now because resolution of | |
1523 | -- the equality node will not resolve any remaining ambiguity, and it | |
1524 | -- assumes that the first operand is not overloaded. | |
1525 | ||
1526 | if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) | |
1527 | and then Ekind (Func) = E_Function | |
1528 | and then Is_Overloaded (Act1) | |
1529 | then | |
1530 | Resolve (Act1, Base_Type (Etype (First_Formal (Func)))); | |
1531 | Resolve (Act2, Base_Type (Etype (First_Formal (Func)))); | |
1532 | end if; | |
1533 | ||
996ae0b0 RK |
1534 | Set_Entity (Op_Node, Op_Id); |
1535 | Generate_Reference (Op_Id, N, ' '); | |
45fc7ddb HK |
1536 | |
1537 | -- Do rewrite setting Comes_From_Source on the result if the original | |
1538 | -- call came from source. Although it is not strictly the case that the | |
1539 | -- operator as such comes from the source, logically it corresponds | |
1540 | -- exactly to the function call in the source, so it should be marked | |
1541 | -- this way (e.g. to make sure that validity checks work fine). | |
1542 | ||
1543 | declare | |
1544 | CS : constant Boolean := Comes_From_Source (N); | |
1545 | begin | |
1546 | Rewrite (N, Op_Node); | |
1547 | Set_Comes_From_Source (N, CS); | |
1548 | end; | |
fbf5a39b AC |
1549 | |
1550 | -- If this is an arithmetic operator and the result type is private, | |
1551 | -- the operands and the result must be wrapped in conversion to | |
1552 | -- expose the underlying numeric type and expand the proper checks, | |
1553 | -- e.g. on division. | |
1554 | ||
1555 | if Is_Private_Type (Typ) then | |
1556 | case Nkind (N) is | |
5cc9353d RD |
1557 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | |
1558 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
fbf5a39b AC |
1559 | Resolve_Intrinsic_Operator (N, Typ); |
1560 | ||
5cc9353d | 1561 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => |
fbf5a39b AC |
1562 | Resolve_Intrinsic_Unary_Operator (N, Typ); |
1563 | ||
1564 | when others => | |
1565 | Resolve (N, Typ); | |
1566 | end case; | |
1567 | else | |
1568 | Resolve (N, Typ); | |
1569 | end if; | |
996ae0b0 RK |
1570 | end Make_Call_Into_Operator; |
1571 | ||
1572 | ------------------- | |
1573 | -- Operator_Kind -- | |
1574 | ------------------- | |
1575 | ||
1576 | function Operator_Kind | |
1577 | (Op_Name : Name_Id; | |
0ab80019 | 1578 | Is_Binary : Boolean) return Node_Kind |
996ae0b0 RK |
1579 | is |
1580 | Kind : Node_Kind; | |
1581 | ||
1582 | begin | |
b0186f71 AC |
1583 | -- Use CASE statement or array??? |
1584 | ||
996ae0b0 | 1585 | if Is_Binary then |
aa5147f0 ES |
1586 | if Op_Name = Name_Op_And then |
1587 | Kind := N_Op_And; | |
1588 | elsif Op_Name = Name_Op_Or then | |
1589 | Kind := N_Op_Or; | |
1590 | elsif Op_Name = Name_Op_Xor then | |
1591 | Kind := N_Op_Xor; | |
1592 | elsif Op_Name = Name_Op_Eq then | |
1593 | Kind := N_Op_Eq; | |
1594 | elsif Op_Name = Name_Op_Ne then | |
1595 | Kind := N_Op_Ne; | |
1596 | elsif Op_Name = Name_Op_Lt then | |
1597 | Kind := N_Op_Lt; | |
1598 | elsif Op_Name = Name_Op_Le then | |
1599 | Kind := N_Op_Le; | |
1600 | elsif Op_Name = Name_Op_Gt then | |
1601 | Kind := N_Op_Gt; | |
1602 | elsif Op_Name = Name_Op_Ge then | |
1603 | Kind := N_Op_Ge; | |
1604 | elsif Op_Name = Name_Op_Add then | |
1605 | Kind := N_Op_Add; | |
1606 | elsif Op_Name = Name_Op_Subtract then | |
1607 | Kind := N_Op_Subtract; | |
1608 | elsif Op_Name = Name_Op_Concat then | |
1609 | Kind := N_Op_Concat; | |
1610 | elsif Op_Name = Name_Op_Multiply then | |
1611 | Kind := N_Op_Multiply; | |
1612 | elsif Op_Name = Name_Op_Divide then | |
1613 | Kind := N_Op_Divide; | |
1614 | elsif Op_Name = Name_Op_Mod then | |
1615 | Kind := N_Op_Mod; | |
1616 | elsif Op_Name = Name_Op_Rem then | |
1617 | Kind := N_Op_Rem; | |
1618 | elsif Op_Name = Name_Op_Expon then | |
1619 | Kind := N_Op_Expon; | |
996ae0b0 RK |
1620 | else |
1621 | raise Program_Error; | |
1622 | end if; | |
1623 | ||
1624 | -- Unary operators | |
1625 | ||
1626 | else | |
aa5147f0 ES |
1627 | if Op_Name = Name_Op_Add then |
1628 | Kind := N_Op_Plus; | |
1629 | elsif Op_Name = Name_Op_Subtract then | |
1630 | Kind := N_Op_Minus; | |
1631 | elsif Op_Name = Name_Op_Abs then | |
1632 | Kind := N_Op_Abs; | |
1633 | elsif Op_Name = Name_Op_Not then | |
1634 | Kind := N_Op_Not; | |
996ae0b0 RK |
1635 | else |
1636 | raise Program_Error; | |
1637 | end if; | |
1638 | end if; | |
1639 | ||
1640 | return Kind; | |
1641 | end Operator_Kind; | |
1642 | ||
45fc7ddb HK |
1643 | ---------------------------- |
1644 | -- Preanalyze_And_Resolve -- | |
1645 | ---------------------------- | |
996ae0b0 | 1646 | |
45fc7ddb | 1647 | procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is |
996ae0b0 RK |
1648 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1649 | ||
1650 | begin | |
1651 | Full_Analysis := False; | |
1652 | Expander_Mode_Save_And_Set (False); | |
1653 | ||
1654 | -- We suppress all checks for this analysis, since the checks will | |
1655 | -- be applied properly, and in the right location, when the default | |
1656 | -- expression is reanalyzed and reexpanded later on. | |
1657 | ||
1658 | Analyze_And_Resolve (N, T, Suppress => All_Checks); | |
1659 | ||
1660 | Expander_Mode_Restore; | |
1661 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1662 | end Preanalyze_And_Resolve; |
996ae0b0 | 1663 | |
a77842bd | 1664 | -- Version without context type |
996ae0b0 | 1665 | |
45fc7ddb | 1666 | procedure Preanalyze_And_Resolve (N : Node_Id) is |
996ae0b0 RK |
1667 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1668 | ||
1669 | begin | |
1670 | Full_Analysis := False; | |
1671 | Expander_Mode_Save_And_Set (False); | |
1672 | ||
1673 | Analyze (N); | |
1674 | Resolve (N, Etype (N), Suppress => All_Checks); | |
1675 | ||
1676 | Expander_Mode_Restore; | |
1677 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1678 | end Preanalyze_And_Resolve; |
996ae0b0 RK |
1679 | |
1680 | ---------------------------------- | |
1681 | -- Replace_Actual_Discriminants -- | |
1682 | ---------------------------------- | |
1683 | ||
1684 | procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is | |
1685 | Loc : constant Source_Ptr := Sloc (N); | |
1686 | Tsk : Node_Id := Empty; | |
1687 | ||
1688 | function Process_Discr (Nod : Node_Id) return Traverse_Result; | |
1689 | ||
1690 | ------------------- | |
1691 | -- Process_Discr -- | |
1692 | ------------------- | |
1693 | ||
1694 | function Process_Discr (Nod : Node_Id) return Traverse_Result is | |
1695 | Ent : Entity_Id; | |
1696 | ||
1697 | begin | |
1698 | if Nkind (Nod) = N_Identifier then | |
1699 | Ent := Entity (Nod); | |
1700 | ||
1701 | if Present (Ent) | |
1702 | and then Ekind (Ent) = E_Discriminant | |
1703 | then | |
1704 | Rewrite (Nod, | |
1705 | Make_Selected_Component (Loc, | |
1706 | Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc), | |
1707 | Selector_Name => Make_Identifier (Loc, Chars (Ent)))); | |
1708 | ||
1709 | Set_Etype (Nod, Etype (Ent)); | |
1710 | end if; | |
1711 | ||
1712 | end if; | |
1713 | ||
1714 | return OK; | |
1715 | end Process_Discr; | |
1716 | ||
1717 | procedure Replace_Discrs is new Traverse_Proc (Process_Discr); | |
1718 | ||
1719 | -- Start of processing for Replace_Actual_Discriminants | |
1720 | ||
1721 | begin | |
1722 | if not Expander_Active then | |
1723 | return; | |
1724 | end if; | |
1725 | ||
1726 | if Nkind (Name (N)) = N_Selected_Component then | |
1727 | Tsk := Prefix (Name (N)); | |
1728 | ||
1729 | elsif Nkind (Name (N)) = N_Indexed_Component then | |
1730 | Tsk := Prefix (Prefix (Name (N))); | |
1731 | end if; | |
1732 | ||
1733 | if No (Tsk) then | |
1734 | return; | |
1735 | else | |
1736 | Replace_Discrs (Default); | |
1737 | end if; | |
1738 | end Replace_Actual_Discriminants; | |
1739 | ||
1740 | ------------- | |
1741 | -- Resolve -- | |
1742 | ------------- | |
1743 | ||
1744 | procedure Resolve (N : Node_Id; Typ : Entity_Id) is | |
dae2b8ea HK |
1745 | Ambiguous : Boolean := False; |
1746 | Ctx_Type : Entity_Id := Typ; | |
1747 | Expr_Type : Entity_Id := Empty; -- prevent junk warning | |
1748 | Err_Type : Entity_Id := Empty; | |
1749 | Found : Boolean := False; | |
1750 | From_Lib : Boolean; | |
996ae0b0 | 1751 | I : Interp_Index; |
dae2b8ea | 1752 | I1 : Interp_Index := 0; -- prevent junk warning |
996ae0b0 RK |
1753 | It : Interp; |
1754 | It1 : Interp; | |
996ae0b0 | 1755 | Seen : Entity_Id := Empty; -- prevent junk warning |
dae2b8ea HK |
1756 | |
1757 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean; | |
1758 | -- Determine whether a node comes from a predefined library unit or | |
1759 | -- Standard. | |
996ae0b0 RK |
1760 | |
1761 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id); | |
1762 | -- Try and fix up a literal so that it matches its expected type. New | |
1763 | -- literals are manufactured if necessary to avoid cascaded errors. | |
1764 | ||
7415029d AC |
1765 | procedure Report_Ambiguous_Argument; |
1766 | -- Additional diagnostics when an ambiguous call has an ambiguous | |
1767 | -- argument (typically a controlling actual). | |
1768 | ||
996ae0b0 RK |
1769 | procedure Resolution_Failed; |
1770 | -- Called when attempt at resolving current expression fails | |
1771 | ||
dae2b8ea HK |
1772 | ------------------------------------ |
1773 | -- Comes_From_Predefined_Lib_Unit -- | |
1774 | ------------------------------------- | |
1775 | ||
1776 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is | |
1777 | begin | |
1778 | return | |
1779 | Sloc (Nod) = Standard_Location | |
5cc9353d RD |
1780 | or else Is_Predefined_File_Name |
1781 | (Unit_File_Name (Get_Source_Unit (Sloc (Nod)))); | |
dae2b8ea HK |
1782 | end Comes_From_Predefined_Lib_Unit; |
1783 | ||
996ae0b0 RK |
1784 | -------------------- |
1785 | -- Patch_Up_Value -- | |
1786 | -------------------- | |
1787 | ||
1788 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is | |
1789 | begin | |
1790 | if Nkind (N) = N_Integer_Literal | |
1791 | and then Is_Real_Type (Typ) | |
1792 | then | |
1793 | Rewrite (N, | |
1794 | Make_Real_Literal (Sloc (N), | |
1795 | Realval => UR_From_Uint (Intval (N)))); | |
1796 | Set_Etype (N, Universal_Real); | |
1797 | Set_Is_Static_Expression (N); | |
1798 | ||
1799 | elsif Nkind (N) = N_Real_Literal | |
1800 | and then Is_Integer_Type (Typ) | |
1801 | then | |
1802 | Rewrite (N, | |
1803 | Make_Integer_Literal (Sloc (N), | |
1804 | Intval => UR_To_Uint (Realval (N)))); | |
1805 | Set_Etype (N, Universal_Integer); | |
1806 | Set_Is_Static_Expression (N); | |
45fc7ddb | 1807 | |
996ae0b0 RK |
1808 | elsif Nkind (N) = N_String_Literal |
1809 | and then Is_Character_Type (Typ) | |
1810 | then | |
1811 | Set_Character_Literal_Name (Char_Code (Character'Pos ('A'))); | |
1812 | Rewrite (N, | |
1813 | Make_Character_Literal (Sloc (N), | |
1814 | Chars => Name_Find, | |
82c80734 RD |
1815 | Char_Literal_Value => |
1816 | UI_From_Int (Character'Pos ('A')))); | |
996ae0b0 RK |
1817 | Set_Etype (N, Any_Character); |
1818 | Set_Is_Static_Expression (N); | |
1819 | ||
1820 | elsif Nkind (N) /= N_String_Literal | |
1821 | and then Is_String_Type (Typ) | |
1822 | then | |
1823 | Rewrite (N, | |
1824 | Make_String_Literal (Sloc (N), | |
1825 | Strval => End_String)); | |
1826 | ||
1827 | elsif Nkind (N) = N_Range then | |
1828 | Patch_Up_Value (Low_Bound (N), Typ); | |
1829 | Patch_Up_Value (High_Bound (N), Typ); | |
1830 | end if; | |
1831 | end Patch_Up_Value; | |
1832 | ||
7415029d AC |
1833 | ------------------------------- |
1834 | -- Report_Ambiguous_Argument -- | |
1835 | ------------------------------- | |
1836 | ||
1837 | procedure Report_Ambiguous_Argument is | |
1838 | Arg : constant Node_Id := First (Parameter_Associations (N)); | |
1839 | I : Interp_Index; | |
1840 | It : Interp; | |
1841 | ||
1842 | begin | |
1843 | if Nkind (Arg) = N_Function_Call | |
1844 | and then Is_Entity_Name (Name (Arg)) | |
1845 | and then Is_Overloaded (Name (Arg)) | |
1846 | then | |
ed2233dc | 1847 | Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg)); |
7415029d | 1848 | |
bfc07071 AC |
1849 | -- Could use comments on what is going on here ??? |
1850 | ||
7415029d AC |
1851 | Get_First_Interp (Name (Arg), I, It); |
1852 | while Present (It.Nam) loop | |
1853 | Error_Msg_Sloc := Sloc (It.Nam); | |
1854 | ||
1855 | if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then | |
ed2233dc | 1856 | Error_Msg_N ("interpretation (inherited) #!", Arg); |
7415029d | 1857 | else |
ed2233dc | 1858 | Error_Msg_N ("interpretation #!", Arg); |
7415029d AC |
1859 | end if; |
1860 | ||
1861 | Get_Next_Interp (I, It); | |
1862 | end loop; | |
1863 | end if; | |
1864 | end Report_Ambiguous_Argument; | |
1865 | ||
996ae0b0 RK |
1866 | ----------------------- |
1867 | -- Resolution_Failed -- | |
1868 | ----------------------- | |
1869 | ||
1870 | procedure Resolution_Failed is | |
1871 | begin | |
1872 | Patch_Up_Value (N, Typ); | |
1873 | Set_Etype (N, Typ); | |
1874 | Debug_A_Exit ("resolving ", N, " (done, resolution failed)"); | |
1875 | Set_Is_Overloaded (N, False); | |
1876 | ||
1877 | -- The caller will return without calling the expander, so we need | |
1878 | -- to set the analyzed flag. Note that it is fine to set Analyzed | |
1879 | -- to True even if we are in the middle of a shallow analysis, | |
1880 | -- (see the spec of sem for more details) since this is an error | |
1881 | -- situation anyway, and there is no point in repeating the | |
1882 | -- analysis later (indeed it won't work to repeat it later, since | |
1883 | -- we haven't got a clear resolution of which entity is being | |
1884 | -- referenced.) | |
1885 | ||
1886 | Set_Analyzed (N, True); | |
1887 | return; | |
1888 | end Resolution_Failed; | |
1889 | ||
1890 | -- Start of processing for Resolve | |
1891 | ||
1892 | begin | |
5c736541 RD |
1893 | if N = Error then |
1894 | return; | |
1895 | end if; | |
1896 | ||
996ae0b0 RK |
1897 | -- Access attribute on remote subprogram cannot be used for |
1898 | -- a non-remote access-to-subprogram type. | |
1899 | ||
1900 | if Nkind (N) = N_Attribute_Reference | |
19fb051c AC |
1901 | and then (Attribute_Name (N) = Name_Access or else |
1902 | Attribute_Name (N) = Name_Unrestricted_Access or else | |
1903 | Attribute_Name (N) = Name_Unchecked_Access) | |
996ae0b0 RK |
1904 | and then Comes_From_Source (N) |
1905 | and then Is_Entity_Name (Prefix (N)) | |
1906 | and then Is_Subprogram (Entity (Prefix (N))) | |
1907 | and then Is_Remote_Call_Interface (Entity (Prefix (N))) | |
1908 | and then not Is_Remote_Access_To_Subprogram_Type (Typ) | |
1909 | then | |
1910 | Error_Msg_N | |
1911 | ("prefix must statically denote a non-remote subprogram", N); | |
1912 | end if; | |
1913 | ||
dae2b8ea HK |
1914 | From_Lib := Comes_From_Predefined_Lib_Unit (N); |
1915 | ||
996ae0b0 RK |
1916 | -- If the context is a Remote_Access_To_Subprogram, access attributes |
1917 | -- must be resolved with the corresponding fat pointer. There is no need | |
1918 | -- to check for the attribute name since the return type of an | |
1919 | -- attribute is never a remote type. | |
1920 | ||
1921 | if Nkind (N) = N_Attribute_Reference | |
1922 | and then Comes_From_Source (N) | |
19fb051c | 1923 | and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ)) |
996ae0b0 RK |
1924 | then |
1925 | declare | |
1926 | Attr : constant Attribute_Id := | |
1927 | Get_Attribute_Id (Attribute_Name (N)); | |
1928 | Pref : constant Node_Id := Prefix (N); | |
1929 | Decl : Node_Id; | |
1930 | Spec : Node_Id; | |
1931 | Is_Remote : Boolean := True; | |
1932 | ||
1933 | begin | |
a77842bd | 1934 | -- Check that Typ is a remote access-to-subprogram type |
996ae0b0 | 1935 | |
a77842bd | 1936 | if Is_Remote_Access_To_Subprogram_Type (Typ) then |
955871d3 | 1937 | |
996ae0b0 RK |
1938 | -- Prefix (N) must statically denote a remote subprogram |
1939 | -- declared in a package specification. | |
1940 | ||
1941 | if Attr = Attribute_Access then | |
1942 | Decl := Unit_Declaration_Node (Entity (Pref)); | |
1943 | ||
1944 | if Nkind (Decl) = N_Subprogram_Body then | |
1945 | Spec := Corresponding_Spec (Decl); | |
1946 | ||
1947 | if not No (Spec) then | |
1948 | Decl := Unit_Declaration_Node (Spec); | |
1949 | end if; | |
1950 | end if; | |
1951 | ||
1952 | Spec := Parent (Decl); | |
1953 | ||
1954 | if not Is_Entity_Name (Prefix (N)) | |
1955 | or else Nkind (Spec) /= N_Package_Specification | |
1956 | or else | |
1957 | not Is_Remote_Call_Interface (Defining_Entity (Spec)) | |
1958 | then | |
1959 | Is_Remote := False; | |
1960 | Error_Msg_N | |
1961 | ("prefix must statically denote a remote subprogram ", | |
1962 | N); | |
1963 | end if; | |
1964 | end if; | |
1965 | ||
fbf5a39b AC |
1966 | -- If we are generating code for a distributed program. |
1967 | -- perform semantic checks against the corresponding | |
1968 | -- remote entities. | |
1969 | ||
19fb051c AC |
1970 | if (Attr = Attribute_Access or else |
1971 | Attr = Attribute_Unchecked_Access or else | |
1972 | Attr = Attribute_Unrestricted_Access) | |
fbf5a39b | 1973 | and then Expander_Active |
a77842bd | 1974 | and then Get_PCS_Name /= Name_No_DSA |
996ae0b0 RK |
1975 | then |
1976 | Check_Subtype_Conformant | |
1977 | (New_Id => Entity (Prefix (N)), | |
1978 | Old_Id => Designated_Type | |
19fb051c | 1979 | (Corresponding_Remote_Type (Typ)), |
996ae0b0 | 1980 | Err_Loc => N); |
b7d1f17f | 1981 | |
996ae0b0 RK |
1982 | if Is_Remote then |
1983 | Process_Remote_AST_Attribute (N, Typ); | |
1984 | end if; | |
1985 | end if; | |
1986 | end if; | |
1987 | end; | |
1988 | end if; | |
1989 | ||
1990 | Debug_A_Entry ("resolving ", N); | |
1991 | ||
07fc65c4 GB |
1992 | if Comes_From_Source (N) then |
1993 | if Is_Fixed_Point_Type (Typ) then | |
1994 | Check_Restriction (No_Fixed_Point, N); | |
996ae0b0 | 1995 | |
07fc65c4 GB |
1996 | elsif Is_Floating_Point_Type (Typ) |
1997 | and then Typ /= Universal_Real | |
1998 | and then Typ /= Any_Real | |
1999 | then | |
2000 | Check_Restriction (No_Floating_Point, N); | |
2001 | end if; | |
996ae0b0 RK |
2002 | end if; |
2003 | ||
2004 | -- Return if already analyzed | |
2005 | ||
2006 | if Analyzed (N) then | |
2007 | Debug_A_Exit ("resolving ", N, " (done, already analyzed)"); | |
2008 | return; | |
2009 | ||
2010 | -- Return if type = Any_Type (previous error encountered) | |
2011 | ||
2012 | elsif Etype (N) = Any_Type then | |
2013 | Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)"); | |
2014 | return; | |
2015 | end if; | |
2016 | ||
2017 | Check_Parameterless_Call (N); | |
2018 | ||
2019 | -- If not overloaded, then we know the type, and all that needs doing | |
2020 | -- is to check that this type is compatible with the context. | |
2021 | ||
2022 | if not Is_Overloaded (N) then | |
2023 | Found := Covers (Typ, Etype (N)); | |
2024 | Expr_Type := Etype (N); | |
2025 | ||
2026 | -- In the overloaded case, we must select the interpretation that | |
2027 | -- is compatible with the context (i.e. the type passed to Resolve) | |
2028 | ||
2029 | else | |
996ae0b0 RK |
2030 | -- Loop through possible interpretations |
2031 | ||
1420b484 | 2032 | Get_First_Interp (N, I, It); |
996ae0b0 RK |
2033 | Interp_Loop : while Present (It.Typ) loop |
2034 | ||
2035 | -- We are only interested in interpretations that are compatible | |
aa5147f0 | 2036 | -- with the expected type, any other interpretations are ignored. |
996ae0b0 | 2037 | |
fbf5a39b AC |
2038 | if not Covers (Typ, It.Typ) then |
2039 | if Debug_Flag_V then | |
2040 | Write_Str (" interpretation incompatible with context"); | |
2041 | Write_Eol; | |
2042 | end if; | |
996ae0b0 | 2043 | |
fbf5a39b | 2044 | else |
aa5147f0 ES |
2045 | -- Skip the current interpretation if it is disabled by an |
2046 | -- abstract operator. This action is performed only when the | |
2047 | -- type against which we are resolving is the same as the | |
2048 | -- type of the interpretation. | |
2049 | ||
0791fbe9 | 2050 | if Ada_Version >= Ada_2005 |
aa5147f0 ES |
2051 | and then It.Typ = Typ |
2052 | and then Typ /= Universal_Integer | |
2053 | and then Typ /= Universal_Real | |
2054 | and then Present (It.Abstract_Op) | |
2055 | then | |
2056 | goto Continue; | |
2057 | end if; | |
2058 | ||
996ae0b0 RK |
2059 | -- First matching interpretation |
2060 | ||
2061 | if not Found then | |
2062 | Found := True; | |
2063 | I1 := I; | |
2064 | Seen := It.Nam; | |
2065 | Expr_Type := It.Typ; | |
2066 | ||
fbf5a39b | 2067 | -- Matching interpretation that is not the first, maybe an |
996ae0b0 RK |
2068 | -- error, but there are some cases where preference rules are |
2069 | -- used to choose between the two possibilities. These and | |
2070 | -- some more obscure cases are handled in Disambiguate. | |
2071 | ||
2072 | else | |
dae2b8ea HK |
2073 | -- If the current statement is part of a predefined library |
2074 | -- unit, then all interpretations which come from user level | |
2075 | -- packages should not be considered. | |
2076 | ||
2077 | if From_Lib | |
2078 | and then not Comes_From_Predefined_Lib_Unit (It.Nam) | |
2079 | then | |
2080 | goto Continue; | |
2081 | end if; | |
2082 | ||
996ae0b0 RK |
2083 | Error_Msg_Sloc := Sloc (Seen); |
2084 | It1 := Disambiguate (N, I1, I, Typ); | |
2085 | ||
fbf5a39b AC |
2086 | -- Disambiguation has succeeded. Skip the remaining |
2087 | -- interpretations. | |
996ae0b0 | 2088 | |
fbf5a39b AC |
2089 | if It1 /= No_Interp then |
2090 | Seen := It1.Nam; | |
2091 | Expr_Type := It1.Typ; | |
2092 | ||
2093 | while Present (It.Typ) loop | |
2094 | Get_Next_Interp (I, It); | |
2095 | end loop; | |
2096 | ||
2097 | else | |
996ae0b0 RK |
2098 | -- Before we issue an ambiguity complaint, check for |
2099 | -- the case of a subprogram call where at least one | |
2100 | -- of the arguments is Any_Type, and if so, suppress | |
2101 | -- the message, since it is a cascaded error. | |
2102 | ||
45fc7ddb HK |
2103 | if Nkind_In (N, N_Function_Call, |
2104 | N_Procedure_Call_Statement) | |
996ae0b0 RK |
2105 | then |
2106 | declare | |
1420b484 | 2107 | A : Node_Id; |
996ae0b0 RK |
2108 | E : Node_Id; |
2109 | ||
2110 | begin | |
1420b484 | 2111 | A := First_Actual (N); |
996ae0b0 RK |
2112 | while Present (A) loop |
2113 | E := A; | |
2114 | ||
2115 | if Nkind (E) = N_Parameter_Association then | |
2116 | E := Explicit_Actual_Parameter (E); | |
2117 | end if; | |
2118 | ||
2119 | if Etype (E) = Any_Type then | |
2120 | if Debug_Flag_V then | |
2121 | Write_Str ("Any_Type in call"); | |
2122 | Write_Eol; | |
2123 | end if; | |
2124 | ||
2125 | exit Interp_Loop; | |
2126 | end if; | |
2127 | ||
2128 | Next_Actual (A); | |
2129 | end loop; | |
2130 | end; | |
2131 | ||
aa5147f0 | 2132 | elsif Nkind (N) in N_Binary_Op |
996ae0b0 RK |
2133 | and then (Etype (Left_Opnd (N)) = Any_Type |
2134 | or else Etype (Right_Opnd (N)) = Any_Type) | |
2135 | then | |
2136 | exit Interp_Loop; | |
2137 | ||
2138 | elsif Nkind (N) in N_Unary_Op | |
2139 | and then Etype (Right_Opnd (N)) = Any_Type | |
2140 | then | |
2141 | exit Interp_Loop; | |
2142 | end if; | |
2143 | ||
2144 | -- Not that special case, so issue message using the | |
2145 | -- flag Ambiguous to control printing of the header | |
2146 | -- message only at the start of an ambiguous set. | |
2147 | ||
2148 | if not Ambiguous then | |
aa180613 RD |
2149 | if Nkind (N) = N_Function_Call |
2150 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2151 | then | |
ed2233dc | 2152 | Error_Msg_N |
aa180613 RD |
2153 | ("ambiguous expression " |
2154 | & "(cannot resolve indirect call)!", N); | |
2155 | else | |
483c78cb | 2156 | Error_Msg_NE -- CODEFIX |
aa180613 RD |
2157 | ("ambiguous expression (cannot resolve&)!", |
2158 | N, It.Nam); | |
2159 | end if; | |
fbf5a39b | 2160 | |
996ae0b0 | 2161 | Ambiguous := True; |
0669bebe GB |
2162 | |
2163 | if Nkind (Parent (Seen)) = N_Full_Type_Declaration then | |
ed2233dc | 2164 | Error_Msg_N |
0669bebe GB |
2165 | ("\\possible interpretation (inherited)#!", N); |
2166 | else | |
4e7a4f6e AC |
2167 | Error_Msg_N -- CODEFIX |
2168 | ("\\possible interpretation#!", N); | |
0669bebe | 2169 | end if; |
7415029d AC |
2170 | |
2171 | if Nkind_In | |
4519314c | 2172 | (N, N_Procedure_Call_Statement, N_Function_Call) |
7415029d AC |
2173 | and then Present (Parameter_Associations (N)) |
2174 | then | |
2175 | Report_Ambiguous_Argument; | |
2176 | end if; | |
996ae0b0 RK |
2177 | end if; |
2178 | ||
2179 | Error_Msg_Sloc := Sloc (It.Nam); | |
996ae0b0 | 2180 | |
fbf5a39b | 2181 | -- By default, the error message refers to the candidate |
0669bebe GB |
2182 | -- interpretation. But if it is a predefined operator, it |
2183 | -- is implicitly declared at the declaration of the type | |
2184 | -- of the operand. Recover the sloc of that declaration | |
2185 | -- for the error message. | |
fbf5a39b AC |
2186 | |
2187 | if Nkind (N) in N_Op | |
2188 | and then Scope (It.Nam) = Standard_Standard | |
2189 | and then not Is_Overloaded (Right_Opnd (N)) | |
0669bebe GB |
2190 | and then Scope (Base_Type (Etype (Right_Opnd (N)))) /= |
2191 | Standard_Standard | |
fbf5a39b AC |
2192 | then |
2193 | Err_Type := First_Subtype (Etype (Right_Opnd (N))); | |
2194 | ||
2195 | if Comes_From_Source (Err_Type) | |
2196 | and then Present (Parent (Err_Type)) | |
2197 | then | |
2198 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2199 | end if; | |
2200 | ||
2201 | elsif Nkind (N) in N_Binary_Op | |
2202 | and then Scope (It.Nam) = Standard_Standard | |
2203 | and then not Is_Overloaded (Left_Opnd (N)) | |
0669bebe GB |
2204 | and then Scope (Base_Type (Etype (Left_Opnd (N)))) /= |
2205 | Standard_Standard | |
fbf5a39b AC |
2206 | then |
2207 | Err_Type := First_Subtype (Etype (Left_Opnd (N))); | |
2208 | ||
2209 | if Comes_From_Source (Err_Type) | |
2210 | and then Present (Parent (Err_Type)) | |
2211 | then | |
2212 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2213 | end if; | |
aa180613 RD |
2214 | |
2215 | -- If this is an indirect call, use the subprogram_type | |
5cc9353d RD |
2216 | -- in the message, to have a meaningful location. Also |
2217 | -- indicate if this is an inherited operation, created | |
2218 | -- by a type declaration. | |
aa180613 RD |
2219 | |
2220 | elsif Nkind (N) = N_Function_Call | |
2221 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2222 | and then Is_Type (It.Nam) | |
2223 | then | |
2224 | Err_Type := It.Nam; | |
2225 | Error_Msg_Sloc := | |
2226 | Sloc (Associated_Node_For_Itype (Err_Type)); | |
fbf5a39b AC |
2227 | else |
2228 | Err_Type := Empty; | |
2229 | end if; | |
2230 | ||
2231 | if Nkind (N) in N_Op | |
2232 | and then Scope (It.Nam) = Standard_Standard | |
2233 | and then Present (Err_Type) | |
2234 | then | |
aa5147f0 ES |
2235 | -- Special-case the message for universal_fixed |
2236 | -- operators, which are not declared with the type | |
2237 | -- of the operand, but appear forever in Standard. | |
2238 | ||
2239 | if It.Typ = Universal_Fixed | |
2240 | and then Scope (It.Nam) = Standard_Standard | |
2241 | then | |
ed2233dc | 2242 | Error_Msg_N |
aa5147f0 ES |
2243 | ("\\possible interpretation as " & |
2244 | "universal_fixed operation " & | |
2245 | "(RM 4.5.5 (19))", N); | |
2246 | else | |
ed2233dc | 2247 | Error_Msg_N |
aa5147f0 ES |
2248 | ("\\possible interpretation (predefined)#!", N); |
2249 | end if; | |
aa180613 RD |
2250 | |
2251 | elsif | |
2252 | Nkind (Parent (It.Nam)) = N_Full_Type_Declaration | |
2253 | then | |
ed2233dc | 2254 | Error_Msg_N |
aa180613 | 2255 | ("\\possible interpretation (inherited)#!", N); |
fbf5a39b | 2256 | else |
4e7a4f6e AC |
2257 | Error_Msg_N -- CODEFIX |
2258 | ("\\possible interpretation#!", N); | |
fbf5a39b | 2259 | end if; |
996ae0b0 | 2260 | |
996ae0b0 RK |
2261 | end if; |
2262 | end if; | |
2263 | ||
0669bebe GB |
2264 | -- We have a matching interpretation, Expr_Type is the type |
2265 | -- from this interpretation, and Seen is the entity. | |
996ae0b0 | 2266 | |
0669bebe GB |
2267 | -- For an operator, just set the entity name. The type will be |
2268 | -- set by the specific operator resolution routine. | |
996ae0b0 RK |
2269 | |
2270 | if Nkind (N) in N_Op then | |
2271 | Set_Entity (N, Seen); | |
2272 | Generate_Reference (Seen, N); | |
2273 | ||
19d846a0 RD |
2274 | elsif Nkind (N) = N_Case_Expression then |
2275 | Set_Etype (N, Expr_Type); | |
2276 | ||
996ae0b0 RK |
2277 | elsif Nkind (N) = N_Character_Literal then |
2278 | Set_Etype (N, Expr_Type); | |
2279 | ||
e0ba1bfd ES |
2280 | elsif Nkind (N) = N_Conditional_Expression then |
2281 | Set_Etype (N, Expr_Type); | |
2282 | ||
996ae0b0 | 2283 | -- For an explicit dereference, attribute reference, range, |
0669bebe GB |
2284 | -- short-circuit form (which is not an operator node), or call |
2285 | -- with a name that is an explicit dereference, there is | |
2286 | -- nothing to be done at this point. | |
996ae0b0 | 2287 | |
45fc7ddb HK |
2288 | elsif Nkind_In (N, N_Explicit_Dereference, |
2289 | N_Attribute_Reference, | |
2290 | N_And_Then, | |
2291 | N_Indexed_Component, | |
2292 | N_Or_Else, | |
2293 | N_Range, | |
2294 | N_Selected_Component, | |
2295 | N_Slice) | |
996ae0b0 RK |
2296 | or else Nkind (Name (N)) = N_Explicit_Dereference |
2297 | then | |
2298 | null; | |
2299 | ||
0669bebe | 2300 | -- For procedure or function calls, set the type of the name, |
4519314c | 2301 | -- and also the entity pointer for the prefix. |
996ae0b0 | 2302 | |
45fc7ddb | 2303 | elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call) |
a3f2babd | 2304 | and then Is_Entity_Name (Name (N)) |
996ae0b0 RK |
2305 | then |
2306 | Set_Etype (Name (N), Expr_Type); | |
2307 | Set_Entity (Name (N), Seen); | |
2308 | Generate_Reference (Seen, Name (N)); | |
2309 | ||
2310 | elsif Nkind (N) = N_Function_Call | |
2311 | and then Nkind (Name (N)) = N_Selected_Component | |
2312 | then | |
2313 | Set_Etype (Name (N), Expr_Type); | |
2314 | Set_Entity (Selector_Name (Name (N)), Seen); | |
2315 | Generate_Reference (Seen, Selector_Name (Name (N))); | |
2316 | ||
2317 | -- For all other cases, just set the type of the Name | |
2318 | ||
2319 | else | |
2320 | Set_Etype (Name (N), Expr_Type); | |
2321 | end if; | |
2322 | ||
996ae0b0 RK |
2323 | end if; |
2324 | ||
aa5147f0 ES |
2325 | <<Continue>> |
2326 | ||
996ae0b0 RK |
2327 | -- Move to next interpretation |
2328 | ||
c8ef728f | 2329 | exit Interp_Loop when No (It.Typ); |
996ae0b0 RK |
2330 | |
2331 | Get_Next_Interp (I, It); | |
2332 | end loop Interp_Loop; | |
2333 | end if; | |
2334 | ||
2335 | -- At this stage Found indicates whether or not an acceptable | |
4519314c AC |
2336 | -- interpretation exists. If not, then we have an error, except that if |
2337 | -- the context is Any_Type as a result of some other error, then we | |
2338 | -- suppress the error report. | |
996ae0b0 RK |
2339 | |
2340 | if not Found then | |
2341 | if Typ /= Any_Type then | |
2342 | ||
0669bebe GB |
2343 | -- If type we are looking for is Void, then this is the procedure |
2344 | -- call case, and the error is simply that what we gave is not a | |
2345 | -- procedure name (we think of procedure calls as expressions with | |
2346 | -- types internally, but the user doesn't think of them this way!) | |
996ae0b0 RK |
2347 | |
2348 | if Typ = Standard_Void_Type then | |
91b1417d AC |
2349 | |
2350 | -- Special case message if function used as a procedure | |
2351 | ||
2352 | if Nkind (N) = N_Procedure_Call_Statement | |
2353 | and then Is_Entity_Name (Name (N)) | |
2354 | and then Ekind (Entity (Name (N))) = E_Function | |
2355 | then | |
2356 | Error_Msg_NE | |
2357 | ("cannot use function & in a procedure call", | |
2358 | Name (N), Entity (Name (N))); | |
2359 | ||
0669bebe GB |
2360 | -- Otherwise give general message (not clear what cases this |
2361 | -- covers, but no harm in providing for them!) | |
91b1417d AC |
2362 | |
2363 | else | |
2364 | Error_Msg_N ("expect procedure name in procedure call", N); | |
2365 | end if; | |
2366 | ||
996ae0b0 RK |
2367 | Found := True; |
2368 | ||
2369 | -- Otherwise we do have a subexpression with the wrong type | |
2370 | ||
0669bebe GB |
2371 | -- Check for the case of an allocator which uses an access type |
2372 | -- instead of the designated type. This is a common error and we | |
2373 | -- specialize the message, posting an error on the operand of the | |
2374 | -- allocator, complaining that we expected the designated type of | |
2375 | -- the allocator. | |
996ae0b0 RK |
2376 | |
2377 | elsif Nkind (N) = N_Allocator | |
2378 | and then Ekind (Typ) in Access_Kind | |
2379 | and then Ekind (Etype (N)) in Access_Kind | |
2380 | and then Designated_Type (Etype (N)) = Typ | |
2381 | then | |
2382 | Wrong_Type (Expression (N), Designated_Type (Typ)); | |
2383 | Found := True; | |
2384 | ||
0669bebe GB |
2385 | -- Check for view mismatch on Null in instances, for which the |
2386 | -- view-swapping mechanism has no identifier. | |
17be0cdf ES |
2387 | |
2388 | elsif (In_Instance or else In_Inlined_Body) | |
2389 | and then (Nkind (N) = N_Null) | |
2390 | and then Is_Private_Type (Typ) | |
2391 | and then Is_Access_Type (Full_View (Typ)) | |
2392 | then | |
2393 | Resolve (N, Full_View (Typ)); | |
2394 | Set_Etype (N, Typ); | |
2395 | return; | |
2396 | ||
aa180613 RD |
2397 | -- Check for an aggregate. Sometimes we can get bogus aggregates |
2398 | -- from misuse of parentheses, and we are about to complain about | |
2399 | -- the aggregate without even looking inside it. | |
996ae0b0 | 2400 | |
aa180613 RD |
2401 | -- Instead, if we have an aggregate of type Any_Composite, then |
2402 | -- analyze and resolve the component fields, and then only issue | |
2403 | -- another message if we get no errors doing this (otherwise | |
2404 | -- assume that the errors in the aggregate caused the problem). | |
996ae0b0 RK |
2405 | |
2406 | elsif Nkind (N) = N_Aggregate | |
2407 | and then Etype (N) = Any_Composite | |
2408 | then | |
996ae0b0 RK |
2409 | -- Disable expansion in any case. If there is a type mismatch |
2410 | -- it may be fatal to try to expand the aggregate. The flag | |
2411 | -- would otherwise be set to false when the error is posted. | |
2412 | ||
2413 | Expander_Active := False; | |
2414 | ||
2415 | declare | |
2416 | procedure Check_Aggr (Aggr : Node_Id); | |
aa180613 RD |
2417 | -- Check one aggregate, and set Found to True if we have a |
2418 | -- definite error in any of its elements | |
996ae0b0 RK |
2419 | |
2420 | procedure Check_Elmt (Aelmt : Node_Id); | |
aa180613 RD |
2421 | -- Check one element of aggregate and set Found to True if |
2422 | -- we definitely have an error in the element. | |
2423 | ||
2424 | ---------------- | |
2425 | -- Check_Aggr -- | |
2426 | ---------------- | |
996ae0b0 RK |
2427 | |
2428 | procedure Check_Aggr (Aggr : Node_Id) is | |
2429 | Elmt : Node_Id; | |
2430 | ||
2431 | begin | |
2432 | if Present (Expressions (Aggr)) then | |
2433 | Elmt := First (Expressions (Aggr)); | |
2434 | while Present (Elmt) loop | |
2435 | Check_Elmt (Elmt); | |
2436 | Next (Elmt); | |
2437 | end loop; | |
2438 | end if; | |
2439 | ||
2440 | if Present (Component_Associations (Aggr)) then | |
2441 | Elmt := First (Component_Associations (Aggr)); | |
2442 | while Present (Elmt) loop | |
aa180613 | 2443 | |
0669bebe GB |
2444 | -- If this is a default-initialized component, then |
2445 | -- there is nothing to check. The box will be | |
2446 | -- replaced by the appropriate call during late | |
2447 | -- expansion. | |
aa180613 RD |
2448 | |
2449 | if not Box_Present (Elmt) then | |
2450 | Check_Elmt (Expression (Elmt)); | |
2451 | end if; | |
2452 | ||
996ae0b0 RK |
2453 | Next (Elmt); |
2454 | end loop; | |
2455 | end if; | |
2456 | end Check_Aggr; | |
2457 | ||
fbf5a39b AC |
2458 | ---------------- |
2459 | -- Check_Elmt -- | |
2460 | ---------------- | |
2461 | ||
996ae0b0 RK |
2462 | procedure Check_Elmt (Aelmt : Node_Id) is |
2463 | begin | |
2464 | -- If we have a nested aggregate, go inside it (to | |
5cc9353d RD |
2465 | -- attempt a naked analyze-resolve of the aggregate can |
2466 | -- cause undesirable cascaded errors). Do not resolve | |
2467 | -- expression if it needs a type from context, as for | |
2468 | -- integer * fixed expression. | |
996ae0b0 RK |
2469 | |
2470 | if Nkind (Aelmt) = N_Aggregate then | |
2471 | Check_Aggr (Aelmt); | |
2472 | ||
2473 | else | |
2474 | Analyze (Aelmt); | |
2475 | ||
2476 | if not Is_Overloaded (Aelmt) | |
2477 | and then Etype (Aelmt) /= Any_Fixed | |
2478 | then | |
fbf5a39b | 2479 | Resolve (Aelmt); |
996ae0b0 RK |
2480 | end if; |
2481 | ||
2482 | if Etype (Aelmt) = Any_Type then | |
2483 | Found := True; | |
2484 | end if; | |
2485 | end if; | |
2486 | end Check_Elmt; | |
2487 | ||
2488 | begin | |
2489 | Check_Aggr (N); | |
2490 | end; | |
2491 | end if; | |
2492 | ||
5cc9353d RD |
2493 | -- If an error message was issued already, Found got reset to |
2494 | -- True, so if it is still False, issue standard Wrong_Type msg. | |
996ae0b0 RK |
2495 | |
2496 | if not Found then | |
2497 | if Is_Overloaded (N) | |
2498 | and then Nkind (N) = N_Function_Call | |
2499 | then | |
65356e64 AC |
2500 | declare |
2501 | Subp_Name : Node_Id; | |
2502 | begin | |
2503 | if Is_Entity_Name (Name (N)) then | |
2504 | Subp_Name := Name (N); | |
2505 | ||
2506 | elsif Nkind (Name (N)) = N_Selected_Component then | |
2507 | ||
a77842bd | 2508 | -- Protected operation: retrieve operation name |
65356e64 AC |
2509 | |
2510 | Subp_Name := Selector_Name (Name (N)); | |
19fb051c | 2511 | |
65356e64 AC |
2512 | else |
2513 | raise Program_Error; | |
2514 | end if; | |
2515 | ||
2516 | Error_Msg_Node_2 := Typ; | |
2517 | Error_Msg_NE ("no visible interpretation of&" & | |
2518 | " matches expected type&", N, Subp_Name); | |
2519 | end; | |
996ae0b0 RK |
2520 | |
2521 | if All_Errors_Mode then | |
2522 | declare | |
2523 | Index : Interp_Index; | |
2524 | It : Interp; | |
2525 | ||
2526 | begin | |
aa180613 | 2527 | Error_Msg_N ("\\possible interpretations:", N); |
996ae0b0 | 2528 | |
1420b484 | 2529 | Get_First_Interp (Name (N), Index, It); |
996ae0b0 | 2530 | while Present (It.Nam) loop |
ea985d95 | 2531 | Error_Msg_Sloc := Sloc (It.Nam); |
aa5147f0 ES |
2532 | Error_Msg_Node_2 := It.Nam; |
2533 | Error_Msg_NE | |
2534 | ("\\ type& for & declared#", N, It.Typ); | |
996ae0b0 RK |
2535 | Get_Next_Interp (Index, It); |
2536 | end loop; | |
2537 | end; | |
aa5147f0 | 2538 | |
996ae0b0 RK |
2539 | else |
2540 | Error_Msg_N ("\use -gnatf for details", N); | |
2541 | end if; | |
19fb051c | 2542 | |
996ae0b0 RK |
2543 | else |
2544 | Wrong_Type (N, Typ); | |
2545 | end if; | |
2546 | end if; | |
2547 | end if; | |
2548 | ||
2549 | Resolution_Failed; | |
2550 | return; | |
2551 | ||
2552 | -- Test if we have more than one interpretation for the context | |
2553 | ||
2554 | elsif Ambiguous then | |
2555 | Resolution_Failed; | |
2556 | return; | |
2557 | ||
2558 | -- Here we have an acceptable interpretation for the context | |
2559 | ||
2560 | else | |
996ae0b0 RK |
2561 | -- Propagate type information and normalize tree for various |
2562 | -- predefined operations. If the context only imposes a class of | |
2563 | -- types, rather than a specific type, propagate the actual type | |
2564 | -- downward. | |
2565 | ||
19fb051c AC |
2566 | if Typ = Any_Integer or else |
2567 | Typ = Any_Boolean or else | |
2568 | Typ = Any_Modular or else | |
2569 | Typ = Any_Real or else | |
2570 | Typ = Any_Discrete | |
996ae0b0 RK |
2571 | then |
2572 | Ctx_Type := Expr_Type; | |
2573 | ||
5cc9353d RD |
2574 | -- Any_Fixed is legal in a real context only if a specific fixed- |
2575 | -- point type is imposed. If Norman Cohen can be confused by this, | |
2576 | -- it deserves a separate message. | |
996ae0b0 RK |
2577 | |
2578 | if Typ = Any_Real | |
2579 | and then Expr_Type = Any_Fixed | |
2580 | then | |
758c442c | 2581 | Error_Msg_N ("illegal context for mixed mode operation", N); |
996ae0b0 RK |
2582 | Set_Etype (N, Universal_Real); |
2583 | Ctx_Type := Universal_Real; | |
2584 | end if; | |
2585 | end if; | |
2586 | ||
f3d57416 | 2587 | -- A user-defined operator is transformed into a function call at |
0ab80019 AC |
2588 | -- this point, so that further processing knows that operators are |
2589 | -- really operators (i.e. are predefined operators). User-defined | |
2590 | -- operators that are intrinsic are just renamings of the predefined | |
2591 | -- ones, and need not be turned into calls either, but if they rename | |
2592 | -- a different operator, we must transform the node accordingly. | |
2593 | -- Instantiations of Unchecked_Conversion are intrinsic but are | |
2594 | -- treated as functions, even if given an operator designator. | |
2595 | ||
2596 | if Nkind (N) in N_Op | |
2597 | and then Present (Entity (N)) | |
2598 | and then Ekind (Entity (N)) /= E_Operator | |
2599 | then | |
2600 | ||
2601 | if not Is_Predefined_Op (Entity (N)) then | |
2602 | Rewrite_Operator_As_Call (N, Entity (N)); | |
2603 | ||
615cbd95 AC |
2604 | elsif Present (Alias (Entity (N))) |
2605 | and then | |
45fc7ddb HK |
2606 | Nkind (Parent (Parent (Entity (N)))) = |
2607 | N_Subprogram_Renaming_Declaration | |
615cbd95 | 2608 | then |
0ab80019 AC |
2609 | Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ); |
2610 | ||
2611 | -- If the node is rewritten, it will be fully resolved in | |
2612 | -- Rewrite_Renamed_Operator. | |
2613 | ||
2614 | if Analyzed (N) then | |
2615 | return; | |
2616 | end if; | |
2617 | end if; | |
2618 | end if; | |
2619 | ||
996ae0b0 RK |
2620 | case N_Subexpr'(Nkind (N)) is |
2621 | ||
2622 | when N_Aggregate => Resolve_Aggregate (N, Ctx_Type); | |
2623 | ||
2624 | when N_Allocator => Resolve_Allocator (N, Ctx_Type); | |
2625 | ||
514d0fc5 | 2626 | when N_Short_Circuit |
996ae0b0 RK |
2627 | => Resolve_Short_Circuit (N, Ctx_Type); |
2628 | ||
2629 | when N_Attribute_Reference | |
2630 | => Resolve_Attribute (N, Ctx_Type); | |
2631 | ||
19d846a0 RD |
2632 | when N_Case_Expression |
2633 | => Resolve_Case_Expression (N, Ctx_Type); | |
2634 | ||
996ae0b0 RK |
2635 | when N_Character_Literal |
2636 | => Resolve_Character_Literal (N, Ctx_Type); | |
2637 | ||
2638 | when N_Conditional_Expression | |
2639 | => Resolve_Conditional_Expression (N, Ctx_Type); | |
2640 | ||
2641 | when N_Expanded_Name | |
2642 | => Resolve_Entity_Name (N, Ctx_Type); | |
2643 | ||
996ae0b0 RK |
2644 | when N_Explicit_Dereference |
2645 | => Resolve_Explicit_Dereference (N, Ctx_Type); | |
2646 | ||
955871d3 AC |
2647 | when N_Expression_With_Actions |
2648 | => Resolve_Expression_With_Actions (N, Ctx_Type); | |
2649 | ||
2650 | when N_Extension_Aggregate | |
2651 | => Resolve_Extension_Aggregate (N, Ctx_Type); | |
2652 | ||
996ae0b0 RK |
2653 | when N_Function_Call |
2654 | => Resolve_Call (N, Ctx_Type); | |
2655 | ||
2656 | when N_Identifier | |
2657 | => Resolve_Entity_Name (N, Ctx_Type); | |
2658 | ||
996ae0b0 RK |
2659 | when N_Indexed_Component |
2660 | => Resolve_Indexed_Component (N, Ctx_Type); | |
2661 | ||
2662 | when N_Integer_Literal | |
2663 | => Resolve_Integer_Literal (N, Ctx_Type); | |
2664 | ||
0669bebe GB |
2665 | when N_Membership_Test |
2666 | => Resolve_Membership_Op (N, Ctx_Type); | |
2667 | ||
996ae0b0 RK |
2668 | when N_Null => Resolve_Null (N, Ctx_Type); |
2669 | ||
2670 | when N_Op_And | N_Op_Or | N_Op_Xor | |
2671 | => Resolve_Logical_Op (N, Ctx_Type); | |
2672 | ||
2673 | when N_Op_Eq | N_Op_Ne | |
2674 | => Resolve_Equality_Op (N, Ctx_Type); | |
2675 | ||
2676 | when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge | |
2677 | => Resolve_Comparison_Op (N, Ctx_Type); | |
2678 | ||
2679 | when N_Op_Not => Resolve_Op_Not (N, Ctx_Type); | |
2680 | ||
2681 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | | |
2682 | N_Op_Divide | N_Op_Mod | N_Op_Rem | |
2683 | ||
2684 | => Resolve_Arithmetic_Op (N, Ctx_Type); | |
2685 | ||
2686 | when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type); | |
2687 | ||
2688 | when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type); | |
2689 | ||
2690 | when N_Op_Plus | N_Op_Minus | N_Op_Abs | |
2691 | => Resolve_Unary_Op (N, Ctx_Type); | |
2692 | ||
2693 | when N_Op_Shift => Resolve_Shift (N, Ctx_Type); | |
2694 | ||
2695 | when N_Procedure_Call_Statement | |
2696 | => Resolve_Call (N, Ctx_Type); | |
2697 | ||
2698 | when N_Operator_Symbol | |
2699 | => Resolve_Operator_Symbol (N, Ctx_Type); | |
2700 | ||
2701 | when N_Qualified_Expression | |
2702 | => Resolve_Qualified_Expression (N, Ctx_Type); | |
2703 | ||
a961aa79 AC |
2704 | when N_Quantified_Expression |
2705 | => Resolve_Quantified_Expression (N, Ctx_Type); | |
2706 | ||
996ae0b0 RK |
2707 | when N_Raise_xxx_Error |
2708 | => Set_Etype (N, Ctx_Type); | |
2709 | ||
2710 | when N_Range => Resolve_Range (N, Ctx_Type); | |
2711 | ||
2712 | when N_Real_Literal | |
2713 | => Resolve_Real_Literal (N, Ctx_Type); | |
2714 | ||
2715 | when N_Reference => Resolve_Reference (N, Ctx_Type); | |
2716 | ||
2717 | when N_Selected_Component | |
2718 | => Resolve_Selected_Component (N, Ctx_Type); | |
2719 | ||
2720 | when N_Slice => Resolve_Slice (N, Ctx_Type); | |
2721 | ||
2722 | when N_String_Literal | |
2723 | => Resolve_String_Literal (N, Ctx_Type); | |
2724 | ||
2725 | when N_Subprogram_Info | |
2726 | => Resolve_Subprogram_Info (N, Ctx_Type); | |
2727 | ||
2728 | when N_Type_Conversion | |
2729 | => Resolve_Type_Conversion (N, Ctx_Type); | |
2730 | ||
2731 | when N_Unchecked_Expression => | |
2732 | Resolve_Unchecked_Expression (N, Ctx_Type); | |
2733 | ||
2734 | when N_Unchecked_Type_Conversion => | |
2735 | Resolve_Unchecked_Type_Conversion (N, Ctx_Type); | |
996ae0b0 RK |
2736 | end case; |
2737 | ||
2738 | -- If the subexpression was replaced by a non-subexpression, then | |
2739 | -- all we do is to expand it. The only legitimate case we know of | |
2740 | -- is converting procedure call statement to entry call statements, | |
2741 | -- but there may be others, so we are making this test general. | |
2742 | ||
2743 | if Nkind (N) not in N_Subexpr then | |
2744 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2745 | Expand (N); | |
2746 | return; | |
2747 | end if; | |
2748 | ||
1e194575 AC |
2749 | -- AI05-144-2: Check dangerous order dependence within an expression |
2750 | -- that is not a subexpression. Exclude RHS of an assignment, because | |
2751 | -- both sides may have side-effects and the check must be performed | |
2752 | -- over the statement. | |
2753 | ||
2754 | if Nkind (Parent (N)) not in N_Subexpr | |
2755 | and then Nkind (Parent (N)) /= N_Assignment_Statement | |
2756 | and then Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
2757 | then | |
2758 | Check_Order_Dependence; | |
2759 | end if; | |
2760 | ||
996ae0b0 RK |
2761 | -- The expression is definitely NOT overloaded at this point, so |
2762 | -- we reset the Is_Overloaded flag to avoid any confusion when | |
2763 | -- reanalyzing the node. | |
2764 | ||
2765 | Set_Is_Overloaded (N, False); | |
2766 | ||
2767 | -- Freeze expression type, entity if it is a name, and designated | |
fbf5a39b | 2768 | -- type if it is an allocator (RM 13.14(10,11,13)). |
996ae0b0 | 2769 | |
5cc9353d RD |
2770 | -- Now that the resolution of the type of the node is complete, and |
2771 | -- we did not detect an error, we can expand this node. We skip the | |
2772 | -- expand call if we are in a default expression, see section | |
2773 | -- "Handling of Default Expressions" in Sem spec. | |
996ae0b0 RK |
2774 | |
2775 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2776 | ||
2777 | -- We unconditionally freeze the expression, even if we are in | |
5cc9353d RD |
2778 | -- default expression mode (the Freeze_Expression routine tests this |
2779 | -- flag and only freezes static types if it is set). | |
996ae0b0 RK |
2780 | |
2781 | Freeze_Expression (N); | |
2782 | ||
2783 | -- Now we can do the expansion | |
2784 | ||
2785 | Expand (N); | |
2786 | end if; | |
996ae0b0 RK |
2787 | end Resolve; |
2788 | ||
fbf5a39b AC |
2789 | ------------- |
2790 | -- Resolve -- | |
2791 | ------------- | |
2792 | ||
996ae0b0 RK |
2793 | -- Version with check(s) suppressed |
2794 | ||
2795 | procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is | |
2796 | begin | |
2797 | if Suppress = All_Checks then | |
2798 | declare | |
fbf5a39b | 2799 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
2800 | begin |
2801 | Scope_Suppress := (others => True); | |
2802 | Resolve (N, Typ); | |
2803 | Scope_Suppress := Svg; | |
2804 | end; | |
2805 | ||
2806 | else | |
2807 | declare | |
fbf5a39b | 2808 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 | 2809 | begin |
fbf5a39b | 2810 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 2811 | Resolve (N, Typ); |
fbf5a39b | 2812 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
2813 | end; |
2814 | end if; | |
2815 | end Resolve; | |
2816 | ||
fbf5a39b AC |
2817 | ------------- |
2818 | -- Resolve -- | |
2819 | ------------- | |
2820 | ||
2821 | -- Version with implicit type | |
2822 | ||
2823 | procedure Resolve (N : Node_Id) is | |
2824 | begin | |
2825 | Resolve (N, Etype (N)); | |
2826 | end Resolve; | |
2827 | ||
996ae0b0 RK |
2828 | --------------------- |
2829 | -- Resolve_Actuals -- | |
2830 | --------------------- | |
2831 | ||
2832 | procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is | |
2833 | Loc : constant Source_Ptr := Sloc (N); | |
2834 | A : Node_Id; | |
2835 | F : Entity_Id; | |
2836 | A_Typ : Entity_Id; | |
2837 | F_Typ : Entity_Id; | |
2838 | Prev : Node_Id := Empty; | |
67ce0d7e | 2839 | Orig_A : Node_Id; |
996ae0b0 | 2840 | |
45fc7ddb HK |
2841 | procedure Check_Argument_Order; |
2842 | -- Performs a check for the case where the actuals are all simple | |
2843 | -- identifiers that correspond to the formal names, but in the wrong | |
2844 | -- order, which is considered suspicious and cause for a warning. | |
2845 | ||
b7d1f17f HK |
2846 | procedure Check_Prefixed_Call; |
2847 | -- If the original node is an overloaded call in prefix notation, | |
2848 | -- insert an 'Access or a dereference as needed over the first actual. | |
2849 | -- Try_Object_Operation has already verified that there is a valid | |
2850 | -- interpretation, but the form of the actual can only be determined | |
2851 | -- once the primitive operation is identified. | |
2852 | ||
996ae0b0 RK |
2853 | procedure Insert_Default; |
2854 | -- If the actual is missing in a call, insert in the actuals list | |
2855 | -- an instance of the default expression. The insertion is always | |
2856 | -- a named association. | |
2857 | ||
fbf5a39b AC |
2858 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean; |
2859 | -- Check whether T1 and T2, or their full views, are derived from a | |
2860 | -- common type. Used to enforce the restrictions on array conversions | |
2861 | -- of AI95-00246. | |
2862 | ||
a7a3cf5c AC |
2863 | function Static_Concatenation (N : Node_Id) return Boolean; |
2864 | -- Predicate to determine whether an actual that is a concatenation | |
2865 | -- will be evaluated statically and does not need a transient scope. | |
2866 | -- This must be determined before the actual is resolved and expanded | |
2867 | -- because if needed the transient scope must be introduced earlier. | |
2868 | ||
45fc7ddb HK |
2869 | -------------------------- |
2870 | -- Check_Argument_Order -- | |
2871 | -------------------------- | |
2872 | ||
2873 | procedure Check_Argument_Order is | |
2874 | begin | |
2875 | -- Nothing to do if no parameters, or original node is neither a | |
2876 | -- function call nor a procedure call statement (happens in the | |
2877 | -- operator-transformed-to-function call case), or the call does | |
2878 | -- not come from source, or this warning is off. | |
2879 | ||
2880 | if not Warn_On_Parameter_Order | |
19fb051c AC |
2881 | or else No (Parameter_Associations (N)) |
2882 | or else not Nkind_In (Original_Node (N), N_Procedure_Call_Statement, | |
2883 | N_Function_Call) | |
2884 | or else not Comes_From_Source (N) | |
45fc7ddb HK |
2885 | then |
2886 | return; | |
2887 | end if; | |
2888 | ||
2889 | declare | |
2890 | Nargs : constant Nat := List_Length (Parameter_Associations (N)); | |
2891 | ||
2892 | begin | |
2893 | -- Nothing to do if only one parameter | |
2894 | ||
2895 | if Nargs < 2 then | |
2896 | return; | |
2897 | end if; | |
2898 | ||
2899 | -- Here if at least two arguments | |
2900 | ||
2901 | declare | |
2902 | Actuals : array (1 .. Nargs) of Node_Id; | |
2903 | Actual : Node_Id; | |
2904 | Formal : Node_Id; | |
2905 | ||
2906 | Wrong_Order : Boolean := False; | |
2907 | -- Set True if an out of order case is found | |
2908 | ||
2909 | begin | |
2910 | -- Collect identifier names of actuals, fail if any actual is | |
2911 | -- not a simple identifier, and record max length of name. | |
2912 | ||
2913 | Actual := First (Parameter_Associations (N)); | |
2914 | for J in Actuals'Range loop | |
2915 | if Nkind (Actual) /= N_Identifier then | |
2916 | return; | |
2917 | else | |
2918 | Actuals (J) := Actual; | |
2919 | Next (Actual); | |
2920 | end if; | |
2921 | end loop; | |
2922 | ||
2923 | -- If we got this far, all actuals are identifiers and the list | |
2924 | -- of their names is stored in the Actuals array. | |
2925 | ||
2926 | Formal := First_Formal (Nam); | |
2927 | for J in Actuals'Range loop | |
2928 | ||
2929 | -- If we ran out of formals, that's odd, probably an error | |
2930 | -- which will be detected elsewhere, but abandon the search. | |
2931 | ||
2932 | if No (Formal) then | |
2933 | return; | |
2934 | end if; | |
2935 | ||
2936 | -- If name matches and is in order OK | |
2937 | ||
2938 | if Chars (Formal) = Chars (Actuals (J)) then | |
2939 | null; | |
2940 | ||
2941 | else | |
2942 | -- If no match, see if it is elsewhere in list and if so | |
2943 | -- flag potential wrong order if type is compatible. | |
2944 | ||
2945 | for K in Actuals'Range loop | |
2946 | if Chars (Formal) = Chars (Actuals (K)) | |
2947 | and then | |
2948 | Has_Compatible_Type (Actuals (K), Etype (Formal)) | |
2949 | then | |
2950 | Wrong_Order := True; | |
2951 | goto Continue; | |
2952 | end if; | |
2953 | end loop; | |
2954 | ||
2955 | -- No match | |
2956 | ||
2957 | return; | |
2958 | end if; | |
2959 | ||
2960 | <<Continue>> Next_Formal (Formal); | |
2961 | end loop; | |
2962 | ||
2963 | -- If Formals left over, also probably an error, skip warning | |
2964 | ||
2965 | if Present (Formal) then | |
2966 | return; | |
2967 | end if; | |
2968 | ||
2969 | -- Here we give the warning if something was out of order | |
2970 | ||
2971 | if Wrong_Order then | |
2972 | Error_Msg_N | |
2973 | ("actuals for this call may be in wrong order?", N); | |
2974 | end if; | |
2975 | end; | |
2976 | end; | |
2977 | end Check_Argument_Order; | |
2978 | ||
b7d1f17f HK |
2979 | ------------------------- |
2980 | -- Check_Prefixed_Call -- | |
2981 | ------------------------- | |
2982 | ||
2983 | procedure Check_Prefixed_Call is | |
2984 | Act : constant Node_Id := First_Actual (N); | |
2985 | A_Type : constant Entity_Id := Etype (Act); | |
2986 | F_Type : constant Entity_Id := Etype (First_Formal (Nam)); | |
2987 | Orig : constant Node_Id := Original_Node (N); | |
2988 | New_A : Node_Id; | |
2989 | ||
2990 | begin | |
2991 | -- Check whether the call is a prefixed call, with or without | |
2992 | -- additional actuals. | |
2993 | ||
2994 | if Nkind (Orig) = N_Selected_Component | |
2995 | or else | |
2996 | (Nkind (Orig) = N_Indexed_Component | |
2997 | and then Nkind (Prefix (Orig)) = N_Selected_Component | |
2998 | and then Is_Entity_Name (Prefix (Prefix (Orig))) | |
2999 | and then Is_Entity_Name (Act) | |
3000 | and then Chars (Act) = Chars (Prefix (Prefix (Orig)))) | |
3001 | then | |
3002 | if Is_Access_Type (A_Type) | |
3003 | and then not Is_Access_Type (F_Type) | |
3004 | then | |
3005 | -- Introduce dereference on object in prefix | |
3006 | ||
3007 | New_A := | |
3008 | Make_Explicit_Dereference (Sloc (Act), | |
3009 | Prefix => Relocate_Node (Act)); | |
3010 | Rewrite (Act, New_A); | |
3011 | Analyze (Act); | |
3012 | ||
3013 | elsif Is_Access_Type (F_Type) | |
3014 | and then not Is_Access_Type (A_Type) | |
3015 | then | |
3016 | -- Introduce an implicit 'Access in prefix | |
3017 | ||
3018 | if not Is_Aliased_View (Act) then | |
ed2233dc | 3019 | Error_Msg_NE |
b7d1f17f | 3020 | ("object in prefixed call to& must be aliased" |
aa5147f0 | 3021 | & " (RM-2005 4.3.1 (13))", |
b7d1f17f HK |
3022 | Prefix (Act), Nam); |
3023 | end if; | |
3024 | ||
3025 | Rewrite (Act, | |
3026 | Make_Attribute_Reference (Loc, | |
3027 | Attribute_Name => Name_Access, | |
3028 | Prefix => Relocate_Node (Act))); | |
3029 | end if; | |
3030 | ||
3031 | Analyze (Act); | |
3032 | end if; | |
3033 | end Check_Prefixed_Call; | |
3034 | ||
996ae0b0 RK |
3035 | -------------------- |
3036 | -- Insert_Default -- | |
3037 | -------------------- | |
3038 | ||
3039 | procedure Insert_Default is | |
3040 | Actval : Node_Id; | |
3041 | Assoc : Node_Id; | |
3042 | ||
3043 | begin | |
fbf5a39b | 3044 | -- Missing argument in call, nothing to insert |
996ae0b0 | 3045 | |
fbf5a39b AC |
3046 | if No (Default_Value (F)) then |
3047 | return; | |
3048 | ||
3049 | else | |
3050 | -- Note that we do a full New_Copy_Tree, so that any associated | |
3051 | -- Itypes are properly copied. This may not be needed any more, | |
3052 | -- but it does no harm as a safety measure! Defaults of a generic | |
3053 | -- formal may be out of bounds of the corresponding actual (see | |
3054 | -- cc1311b) and an additional check may be required. | |
996ae0b0 | 3055 | |
b7d1f17f HK |
3056 | Actval := |
3057 | New_Copy_Tree | |
3058 | (Default_Value (F), | |
3059 | New_Scope => Current_Scope, | |
3060 | New_Sloc => Loc); | |
996ae0b0 RK |
3061 | |
3062 | if Is_Concurrent_Type (Scope (Nam)) | |
3063 | and then Has_Discriminants (Scope (Nam)) | |
3064 | then | |
3065 | Replace_Actual_Discriminants (N, Actval); | |
3066 | end if; | |
3067 | ||
3068 | if Is_Overloadable (Nam) | |
3069 | and then Present (Alias (Nam)) | |
3070 | then | |
3071 | if Base_Type (Etype (F)) /= Base_Type (Etype (Actval)) | |
3072 | and then not Is_Tagged_Type (Etype (F)) | |
3073 | then | |
3074 | -- If default is a real literal, do not introduce a | |
3075 | -- conversion whose effect may depend on the run-time | |
3076 | -- size of universal real. | |
3077 | ||
3078 | if Nkind (Actval) = N_Real_Literal then | |
3079 | Set_Etype (Actval, Base_Type (Etype (F))); | |
3080 | else | |
3081 | Actval := Unchecked_Convert_To (Etype (F), Actval); | |
3082 | end if; | |
3083 | end if; | |
3084 | ||
3085 | if Is_Scalar_Type (Etype (F)) then | |
3086 | Enable_Range_Check (Actval); | |
3087 | end if; | |
3088 | ||
996ae0b0 RK |
3089 | Set_Parent (Actval, N); |
3090 | ||
3091 | -- Resolve aggregates with their base type, to avoid scope | |
f3d57416 | 3092 | -- anomalies: the subtype was first built in the subprogram |
996ae0b0 RK |
3093 | -- declaration, and the current call may be nested. |
3094 | ||
76b84bf0 AC |
3095 | if Nkind (Actval) = N_Aggregate then |
3096 | Analyze_And_Resolve (Actval, Etype (F)); | |
996ae0b0 RK |
3097 | else |
3098 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
3099 | end if; | |
fbf5a39b AC |
3100 | |
3101 | else | |
3102 | Set_Parent (Actval, N); | |
3103 | ||
a77842bd | 3104 | -- See note above concerning aggregates |
fbf5a39b AC |
3105 | |
3106 | if Nkind (Actval) = N_Aggregate | |
3107 | and then Has_Discriminants (Etype (Actval)) | |
3108 | then | |
3109 | Analyze_And_Resolve (Actval, Base_Type (Etype (Actval))); | |
3110 | ||
5cc9353d RD |
3111 | -- Resolve entities with their own type, which may differ from |
3112 | -- the type of a reference in a generic context (the view | |
3113 | -- swapping mechanism did not anticipate the re-analysis of | |
3114 | -- default values in calls). | |
fbf5a39b AC |
3115 | |
3116 | elsif Is_Entity_Name (Actval) then | |
3117 | Analyze_And_Resolve (Actval, Etype (Entity (Actval))); | |
3118 | ||
3119 | else | |
3120 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
3121 | end if; | |
996ae0b0 RK |
3122 | end if; |
3123 | ||
5cc9353d RD |
3124 | -- If default is a tag indeterminate function call, propagate tag |
3125 | -- to obtain proper dispatching. | |
996ae0b0 RK |
3126 | |
3127 | if Is_Controlling_Formal (F) | |
3128 | and then Nkind (Default_Value (F)) = N_Function_Call | |
3129 | then | |
3130 | Set_Is_Controlling_Actual (Actval); | |
3131 | end if; | |
3132 | ||
996ae0b0 RK |
3133 | end if; |
3134 | ||
3135 | -- If the default expression raises constraint error, then just | |
5cc9353d RD |
3136 | -- silently replace it with an N_Raise_Constraint_Error node, since |
3137 | -- we already gave the warning on the subprogram spec. If node is | |
3138 | -- already a Raise_Constraint_Error leave as is, to prevent loops in | |
3139 | -- the warnings removal machinery. | |
996ae0b0 | 3140 | |
2604ec03 AC |
3141 | if Raises_Constraint_Error (Actval) |
3142 | and then Nkind (Actval) /= N_Raise_Constraint_Error | |
3143 | then | |
996ae0b0 | 3144 | Rewrite (Actval, |
07fc65c4 GB |
3145 | Make_Raise_Constraint_Error (Loc, |
3146 | Reason => CE_Range_Check_Failed)); | |
996ae0b0 RK |
3147 | Set_Raises_Constraint_Error (Actval); |
3148 | Set_Etype (Actval, Etype (F)); | |
3149 | end if; | |
3150 | ||
3151 | Assoc := | |
3152 | Make_Parameter_Association (Loc, | |
3153 | Explicit_Actual_Parameter => Actval, | |
3154 | Selector_Name => Make_Identifier (Loc, Chars (F))); | |
3155 | ||
3156 | -- Case of insertion is first named actual | |
3157 | ||
3158 | if No (Prev) or else | |
3159 | Nkind (Parent (Prev)) /= N_Parameter_Association | |
3160 | then | |
3161 | Set_Next_Named_Actual (Assoc, First_Named_Actual (N)); | |
3162 | Set_First_Named_Actual (N, Actval); | |
3163 | ||
3164 | if No (Prev) then | |
c8ef728f | 3165 | if No (Parameter_Associations (N)) then |
996ae0b0 RK |
3166 | Set_Parameter_Associations (N, New_List (Assoc)); |
3167 | else | |
3168 | Append (Assoc, Parameter_Associations (N)); | |
3169 | end if; | |
3170 | ||
3171 | else | |
3172 | Insert_After (Prev, Assoc); | |
3173 | end if; | |
3174 | ||
3175 | -- Case of insertion is not first named actual | |
3176 | ||
3177 | else | |
3178 | Set_Next_Named_Actual | |
3179 | (Assoc, Next_Named_Actual (Parent (Prev))); | |
3180 | Set_Next_Named_Actual (Parent (Prev), Actval); | |
3181 | Append (Assoc, Parameter_Associations (N)); | |
3182 | end if; | |
3183 | ||
3184 | Mark_Rewrite_Insertion (Assoc); | |
3185 | Mark_Rewrite_Insertion (Actval); | |
3186 | ||
3187 | Prev := Actval; | |
3188 | end Insert_Default; | |
3189 | ||
fbf5a39b AC |
3190 | ------------------- |
3191 | -- Same_Ancestor -- | |
3192 | ------------------- | |
3193 | ||
3194 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is | |
3195 | FT1 : Entity_Id := T1; | |
3196 | FT2 : Entity_Id := T2; | |
3197 | ||
3198 | begin | |
3199 | if Is_Private_Type (T1) | |
3200 | and then Present (Full_View (T1)) | |
3201 | then | |
3202 | FT1 := Full_View (T1); | |
3203 | end if; | |
3204 | ||
3205 | if Is_Private_Type (T2) | |
3206 | and then Present (Full_View (T2)) | |
3207 | then | |
3208 | FT2 := Full_View (T2); | |
3209 | end if; | |
3210 | ||
3211 | return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2)); | |
3212 | end Same_Ancestor; | |
3213 | ||
a7a3cf5c AC |
3214 | -------------------------- |
3215 | -- Static_Concatenation -- | |
3216 | -------------------------- | |
3217 | ||
3218 | function Static_Concatenation (N : Node_Id) return Boolean is | |
3219 | begin | |
c72a85f2 TQ |
3220 | case Nkind (N) is |
3221 | when N_String_Literal => | |
3222 | return True; | |
a7a3cf5c | 3223 | |
d81b4bfe TQ |
3224 | when N_Op_Concat => |
3225 | ||
5cc9353d RD |
3226 | -- Concatenation is static when both operands are static and |
3227 | -- the concatenation operator is a predefined one. | |
4342eda9 TQ |
3228 | |
3229 | return Scope (Entity (N)) = Standard_Standard | |
3230 | and then | |
3231 | Static_Concatenation (Left_Opnd (N)) | |
c72a85f2 TQ |
3232 | and then |
3233 | Static_Concatenation (Right_Opnd (N)); | |
3234 | ||
3235 | when others => | |
3236 | if Is_Entity_Name (N) then | |
3237 | declare | |
3238 | Ent : constant Entity_Id := Entity (N); | |
3239 | begin | |
3240 | return Ekind (Ent) = E_Constant | |
3241 | and then Present (Constant_Value (Ent)) | |
d81b4bfe TQ |
3242 | and then |
3243 | Is_Static_Expression (Constant_Value (Ent)); | |
c72a85f2 | 3244 | end; |
a7a3cf5c | 3245 | |
a7a3cf5c AC |
3246 | else |
3247 | return False; | |
3248 | end if; | |
c72a85f2 | 3249 | end case; |
a7a3cf5c AC |
3250 | end Static_Concatenation; |
3251 | ||
996ae0b0 RK |
3252 | -- Start of processing for Resolve_Actuals |
3253 | ||
3254 | begin | |
45fc7ddb HK |
3255 | Check_Argument_Order; |
3256 | ||
b7d1f17f HK |
3257 | if Present (First_Actual (N)) then |
3258 | Check_Prefixed_Call; | |
3259 | end if; | |
3260 | ||
996ae0b0 RK |
3261 | A := First_Actual (N); |
3262 | F := First_Formal (Nam); | |
996ae0b0 | 3263 | while Present (F) loop |
fbf5a39b AC |
3264 | if No (A) and then Needs_No_Actuals (Nam) then |
3265 | null; | |
996ae0b0 | 3266 | |
d81b4bfe TQ |
3267 | -- If we have an error in any actual or formal, indicated by a type |
3268 | -- of Any_Type, then abandon resolution attempt, and set result type | |
3269 | -- to Any_Type. | |
07fc65c4 | 3270 | |
fbf5a39b AC |
3271 | elsif (Present (A) and then Etype (A) = Any_Type) |
3272 | or else Etype (F) = Any_Type | |
07fc65c4 GB |
3273 | then |
3274 | Set_Etype (N, Any_Type); | |
3275 | return; | |
3276 | end if; | |
3277 | ||
e65f50ec ES |
3278 | -- Case where actual is present |
3279 | ||
45fc7ddb | 3280 | -- If the actual is an entity, generate a reference to it now. We |
36fcf362 RD |
3281 | -- do this before the actual is resolved, because a formal of some |
3282 | -- protected subprogram, or a task discriminant, will be rewritten | |
5cc9353d | 3283 | -- during expansion, and the source entity reference may be lost. |
36fcf362 RD |
3284 | |
3285 | if Present (A) | |
3286 | and then Is_Entity_Name (A) | |
3287 | and then Comes_From_Source (N) | |
3288 | then | |
3289 | Orig_A := Entity (A); | |
3290 | ||
3291 | if Present (Orig_A) then | |
3292 | if Is_Formal (Orig_A) | |
3293 | and then Ekind (F) /= E_In_Parameter | |
3294 | then | |
3295 | Generate_Reference (Orig_A, A, 'm'); | |
19fb051c | 3296 | |
36fcf362 RD |
3297 | elsif not Is_Overloaded (A) then |
3298 | Generate_Reference (Orig_A, A); | |
3299 | end if; | |
3300 | end if; | |
3301 | end if; | |
3302 | ||
996ae0b0 RK |
3303 | if Present (A) |
3304 | and then (Nkind (Parent (A)) /= N_Parameter_Association | |
19fb051c | 3305 | or else Chars (Selector_Name (Parent (A))) = Chars (F)) |
996ae0b0 | 3306 | then |
45fc7ddb HK |
3307 | -- If style checking mode on, check match of formal name |
3308 | ||
3309 | if Style_Check then | |
3310 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3311 | Check_Identifier (Selector_Name (Parent (A)), F); | |
3312 | end if; | |
3313 | end if; | |
3314 | ||
996ae0b0 RK |
3315 | -- If the formal is Out or In_Out, do not resolve and expand the |
3316 | -- conversion, because it is subsequently expanded into explicit | |
3317 | -- temporaries and assignments. However, the object of the | |
ea985d95 RD |
3318 | -- conversion can be resolved. An exception is the case of tagged |
3319 | -- type conversion with a class-wide actual. In that case we want | |
3320 | -- the tag check to occur and no temporary will be needed (no | |
3321 | -- representation change can occur) and the parameter is passed by | |
3322 | -- reference, so we go ahead and resolve the type conversion. | |
c8ef728f | 3323 | -- Another exception is the case of reference to component or |
ea985d95 RD |
3324 | -- subcomponent of a bit-packed array, in which case we want to |
3325 | -- defer expansion to the point the in and out assignments are | |
3326 | -- performed. | |
996ae0b0 RK |
3327 | |
3328 | if Ekind (F) /= E_In_Parameter | |
3329 | and then Nkind (A) = N_Type_Conversion | |
3330 | and then not Is_Class_Wide_Type (Etype (Expression (A))) | |
3331 | then | |
07fc65c4 GB |
3332 | if Ekind (F) = E_In_Out_Parameter |
3333 | and then Is_Array_Type (Etype (F)) | |
07fc65c4 | 3334 | then |
038140ed AC |
3335 | -- In a view conversion, the conversion must be legal in |
3336 | -- both directions, and thus both component types must be | |
3337 | -- aliased, or neither (4.6 (8)). | |
758c442c | 3338 | |
038140ed AC |
3339 | -- The extra rule in 4.6 (24.9.2) seems unduly restrictive: |
3340 | -- the privacy requirement should not apply to generic | |
3341 | -- types, and should be checked in an instance. ARG query | |
3342 | -- is in order ??? | |
45fc7ddb | 3343 | |
038140ed AC |
3344 | if Has_Aliased_Components (Etype (Expression (A))) /= |
3345 | Has_Aliased_Components (Etype (F)) | |
3346 | then | |
45fc7ddb HK |
3347 | Error_Msg_N |
3348 | ("both component types in a view conversion must be" | |
3349 | & " aliased, or neither", A); | |
3350 | ||
038140ed AC |
3351 | -- Comment here??? what set of cases??? |
3352 | ||
45fc7ddb HK |
3353 | elsif |
3354 | not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3355 | then | |
038140ed AC |
3356 | -- Check view conv between unrelated by ref array types |
3357 | ||
45fc7ddb HK |
3358 | if Is_By_Reference_Type (Etype (F)) |
3359 | or else Is_By_Reference_Type (Etype (Expression (A))) | |
758c442c GD |
3360 | then |
3361 | Error_Msg_N | |
45fc7ddb HK |
3362 | ("view conversion between unrelated by reference " & |
3363 | "array types not allowed (\'A'I-00246)", A); | |
038140ed AC |
3364 | |
3365 | -- In Ada 2005 mode, check view conversion component | |
3366 | -- type cannot be private, tagged, or volatile. Note | |
3367 | -- that we only apply this to source conversions. The | |
3368 | -- generated code can contain conversions which are | |
3369 | -- not subject to this test, and we cannot extract the | |
3370 | -- component type in such cases since it is not present. | |
3371 | ||
3372 | elsif Comes_From_Source (A) | |
3373 | and then Ada_Version >= Ada_2005 | |
3374 | then | |
45fc7ddb HK |
3375 | declare |
3376 | Comp_Type : constant Entity_Id := | |
3377 | Component_Type | |
3378 | (Etype (Expression (A))); | |
3379 | begin | |
038140ed AC |
3380 | if (Is_Private_Type (Comp_Type) |
3381 | and then not Is_Generic_Type (Comp_Type)) | |
3382 | or else Is_Tagged_Type (Comp_Type) | |
3383 | or else Is_Volatile (Comp_Type) | |
45fc7ddb HK |
3384 | then |
3385 | Error_Msg_N | |
3386 | ("component type of a view conversion cannot" | |
3387 | & " be private, tagged, or volatile" | |
3388 | & " (RM 4.6 (24))", | |
3389 | Expression (A)); | |
3390 | end if; | |
3391 | end; | |
758c442c | 3392 | end if; |
fbf5a39b | 3393 | end if; |
07fc65c4 GB |
3394 | end if; |
3395 | ||
038140ed AC |
3396 | -- Resolve expression if conversion is all OK |
3397 | ||
16397eff | 3398 | if (Conversion_OK (A) |
038140ed | 3399 | or else Valid_Conversion (A, Etype (A), Expression (A))) |
16397eff | 3400 | and then not Is_Ref_To_Bit_Packed_Array (Expression (A)) |
996ae0b0 | 3401 | then |
fbf5a39b | 3402 | Resolve (Expression (A)); |
996ae0b0 RK |
3403 | end if; |
3404 | ||
b7d1f17f HK |
3405 | -- If the actual is a function call that returns a limited |
3406 | -- unconstrained object that needs finalization, create a | |
3407 | -- transient scope for it, so that it can receive the proper | |
3408 | -- finalization list. | |
3409 | ||
3410 | elsif Nkind (A) = N_Function_Call | |
3411 | and then Is_Limited_Record (Etype (F)) | |
3412 | and then not Is_Constrained (Etype (F)) | |
3413 | and then Expander_Active | |
19fb051c | 3414 | and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F))) |
b7d1f17f HK |
3415 | then |
3416 | Establish_Transient_Scope (A, False); | |
3417 | ||
a52fefe6 AC |
3418 | -- A small optimization: if one of the actuals is a concatenation |
3419 | -- create a block around a procedure call to recover stack space. | |
3420 | -- This alleviates stack usage when several procedure calls in | |
76e776e5 AC |
3421 | -- the same statement list use concatenation. We do not perform |
3422 | -- this wrapping for code statements, where the argument is a | |
3423 | -- static string, and we want to preserve warnings involving | |
3424 | -- sequences of such statements. | |
a52fefe6 AC |
3425 | |
3426 | elsif Nkind (A) = N_Op_Concat | |
3427 | and then Nkind (N) = N_Procedure_Call_Statement | |
3428 | and then Expander_Active | |
76e776e5 AC |
3429 | and then |
3430 | not (Is_Intrinsic_Subprogram (Nam) | |
3431 | and then Chars (Nam) = Name_Asm) | |
a7a3cf5c | 3432 | and then not Static_Concatenation (A) |
a52fefe6 AC |
3433 | then |
3434 | Establish_Transient_Scope (A, False); | |
3435 | Resolve (A, Etype (F)); | |
3436 | ||
996ae0b0 | 3437 | else |
fbf5a39b AC |
3438 | if Nkind (A) = N_Type_Conversion |
3439 | and then Is_Array_Type (Etype (F)) | |
3440 | and then not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3441 | and then | |
3442 | (Is_Limited_Type (Etype (F)) | |
3443 | or else Is_Limited_Type (Etype (Expression (A)))) | |
3444 | then | |
3445 | Error_Msg_N | |
758c442c GD |
3446 | ("conversion between unrelated limited array types " & |
3447 | "not allowed (\A\I-00246)", A); | |
fbf5a39b | 3448 | |
758c442c GD |
3449 | if Is_Limited_Type (Etype (F)) then |
3450 | Explain_Limited_Type (Etype (F), A); | |
3451 | end if; | |
fbf5a39b | 3452 | |
758c442c GD |
3453 | if Is_Limited_Type (Etype (Expression (A))) then |
3454 | Explain_Limited_Type (Etype (Expression (A)), A); | |
3455 | end if; | |
fbf5a39b AC |
3456 | end if; |
3457 | ||
c8ef728f ES |
3458 | -- (Ada 2005: AI-251): If the actual is an allocator whose |
3459 | -- directly designated type is a class-wide interface, we build | |
3460 | -- an anonymous access type to use it as the type of the | |
3461 | -- allocator. Later, when the subprogram call is expanded, if | |
3462 | -- the interface has a secondary dispatch table the expander | |
3463 | -- will add a type conversion to force the correct displacement | |
3464 | -- of the pointer. | |
3465 | ||
3466 | if Nkind (A) = N_Allocator then | |
3467 | declare | |
3468 | DDT : constant Entity_Id := | |
3469 | Directly_Designated_Type (Base_Type (Etype (F))); | |
45fc7ddb | 3470 | |
c8ef728f | 3471 | New_Itype : Entity_Id; |
45fc7ddb | 3472 | |
c8ef728f ES |
3473 | begin |
3474 | if Is_Class_Wide_Type (DDT) | |
3475 | and then Is_Interface (DDT) | |
3476 | then | |
3477 | New_Itype := Create_Itype (E_Anonymous_Access_Type, A); | |
45fc7ddb | 3478 | Set_Etype (New_Itype, Etype (A)); |
c8ef728f ES |
3479 | Set_Directly_Designated_Type (New_Itype, |
3480 | Directly_Designated_Type (Etype (A))); | |
3481 | Set_Etype (A, New_Itype); | |
3482 | end if; | |
0669bebe GB |
3483 | |
3484 | -- Ada 2005, AI-162:If the actual is an allocator, the | |
3485 | -- innermost enclosing statement is the master of the | |
b7d1f17f HK |
3486 | -- created object. This needs to be done with expansion |
3487 | -- enabled only, otherwise the transient scope will not | |
3488 | -- be removed in the expansion of the wrapped construct. | |
0669bebe | 3489 | |
45fc7ddb | 3490 | if (Is_Controlled (DDT) or else Has_Task (DDT)) |
b7d1f17f | 3491 | and then Expander_Active |
0669bebe GB |
3492 | then |
3493 | Establish_Transient_Scope (A, False); | |
3494 | end if; | |
c8ef728f ES |
3495 | end; |
3496 | end if; | |
3497 | ||
b7d1f17f HK |
3498 | -- (Ada 2005): The call may be to a primitive operation of |
3499 | -- a tagged synchronized type, declared outside of the type. | |
3500 | -- In this case the controlling actual must be converted to | |
3501 | -- its corresponding record type, which is the formal type. | |
45fc7ddb HK |
3502 | -- The actual may be a subtype, either because of a constraint |
3503 | -- or because it is a generic actual, so use base type to | |
3504 | -- locate concurrent type. | |
b7d1f17f | 3505 | |
15e4986c JM |
3506 | F_Typ := Base_Type (Etype (F)); |
3507 | ||
cb7fa356 AC |
3508 | if Is_Tagged_Type (F_Typ) |
3509 | and then (Is_Concurrent_Type (F_Typ) | |
3510 | or else Is_Concurrent_Record_Type (F_Typ)) | |
3511 | then | |
3512 | -- If the actual is overloaded, look for an interpretation | |
3513 | -- that has a synchronized type. | |
3514 | ||
3515 | if not Is_Overloaded (A) then | |
3516 | A_Typ := Base_Type (Etype (A)); | |
15e4986c | 3517 | |
15e4986c | 3518 | else |
cb7fa356 AC |
3519 | declare |
3520 | Index : Interp_Index; | |
3521 | It : Interp; | |
3522 | begin | |
3523 | Get_First_Interp (A, Index, It); | |
3524 | while Present (It.Typ) loop | |
3525 | if Is_Concurrent_Type (It.Typ) | |
3526 | or else Is_Concurrent_Record_Type (It.Typ) | |
3527 | then | |
3528 | A_Typ := Base_Type (It.Typ); | |
3529 | exit; | |
3530 | end if; | |
3531 | ||
3532 | Get_Next_Interp (Index, It); | |
3533 | end loop; | |
3534 | end; | |
15e4986c | 3535 | end if; |
b7d1f17f | 3536 | |
cb7fa356 AC |
3537 | declare |
3538 | Full_A_Typ : Entity_Id; | |
15e4986c | 3539 | |
cb7fa356 AC |
3540 | begin |
3541 | if Present (Full_View (A_Typ)) then | |
3542 | Full_A_Typ := Base_Type (Full_View (A_Typ)); | |
3543 | else | |
3544 | Full_A_Typ := A_Typ; | |
3545 | end if; | |
3546 | ||
3547 | -- Tagged synchronized type (case 1): the actual is a | |
3548 | -- concurrent type. | |
3549 | ||
3550 | if Is_Concurrent_Type (A_Typ) | |
3551 | and then Corresponding_Record_Type (A_Typ) = F_Typ | |
3552 | then | |
3553 | Rewrite (A, | |
3554 | Unchecked_Convert_To | |
3555 | (Corresponding_Record_Type (A_Typ), A)); | |
3556 | Resolve (A, Etype (F)); | |
15e4986c | 3557 | |
cb7fa356 AC |
3558 | -- Tagged synchronized type (case 2): the formal is a |
3559 | -- concurrent type. | |
15e4986c | 3560 | |
cb7fa356 AC |
3561 | elsif Ekind (Full_A_Typ) = E_Record_Type |
3562 | and then Present | |
15e4986c | 3563 | (Corresponding_Concurrent_Type (Full_A_Typ)) |
cb7fa356 AC |
3564 | and then Is_Concurrent_Type (F_Typ) |
3565 | and then Present (Corresponding_Record_Type (F_Typ)) | |
3566 | and then Full_A_Typ = Corresponding_Record_Type (F_Typ) | |
3567 | then | |
3568 | Resolve (A, Corresponding_Record_Type (F_Typ)); | |
15e4986c | 3569 | |
cb7fa356 | 3570 | -- Common case |
15e4986c | 3571 | |
cb7fa356 AC |
3572 | else |
3573 | Resolve (A, Etype (F)); | |
3574 | end if; | |
3575 | end; | |
3576 | else | |
3577 | ||
3578 | -- not a synchronized operation. | |
3579 | ||
3580 | Resolve (A, Etype (F)); | |
3581 | end if; | |
996ae0b0 RK |
3582 | end if; |
3583 | ||
3584 | A_Typ := Etype (A); | |
3585 | F_Typ := Etype (F); | |
3586 | ||
b0186f71 AC |
3587 | -- In SPARK or ALFA, the only view conversions are those involving |
3588 | -- ancestor conversion of an extended type. | |
3589 | ||
fe5d3068 | 3590 | if Nkind (A) = N_Type_Conversion |
b0186f71 AC |
3591 | and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) |
3592 | then | |
3593 | declare | |
3594 | Operand : constant Node_Id := Expression (A); | |
3595 | Operand_Typ : constant Entity_Id := Etype (Operand); | |
3596 | Target_Typ : constant Entity_Id := A_Typ; | |
780d052e | 3597 | |
b0186f71 AC |
3598 | begin |
3599 | if not (Is_Tagged_Type (Target_Typ) | |
780d052e RD |
3600 | and then not Is_Class_Wide_Type (Target_Typ) |
3601 | and then Is_Tagged_Type (Operand_Typ) | |
3602 | and then not Is_Class_Wide_Type (Operand_Typ) | |
3603 | and then Is_Ancestor (Target_Typ, Operand_Typ)) | |
b0186f71 | 3604 | then |
fe5d3068 YM |
3605 | Check_Formal_Restriction |
3606 | ("ancestor conversion is the only permitted view " | |
3607 | & "conversion", A); | |
b0186f71 AC |
3608 | end if; |
3609 | end; | |
3610 | end if; | |
3611 | ||
1e194575 AC |
3612 | -- Save actual for subsequent check on order dependence, and |
3613 | -- indicate whether actual is modifiable. For AI05-0144-2. | |
bb481772 | 3614 | |
1e194575 | 3615 | Save_Actual (A, Ekind (F) /= E_In_Parameter); |
bb481772 | 3616 | |
26570b21 RD |
3617 | -- For mode IN, if actual is an entity, and the type of the formal |
3618 | -- has warnings suppressed, then we reset Never_Set_In_Source for | |
3619 | -- the calling entity. The reason for this is to catch cases like | |
3620 | -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram | |
3621 | -- uses trickery to modify an IN parameter. | |
3622 | ||
3623 | if Ekind (F) = E_In_Parameter | |
3624 | and then Is_Entity_Name (A) | |
3625 | and then Present (Entity (A)) | |
3626 | and then Ekind (Entity (A)) = E_Variable | |
3627 | and then Has_Warnings_Off (F_Typ) | |
3628 | then | |
3629 | Set_Never_Set_In_Source (Entity (A), False); | |
3630 | end if; | |
3631 | ||
fbf5a39b AC |
3632 | -- Perform error checks for IN and IN OUT parameters |
3633 | ||
3634 | if Ekind (F) /= E_Out_Parameter then | |
3635 | ||
3636 | -- Check unset reference. For scalar parameters, it is clearly | |
3637 | -- wrong to pass an uninitialized value as either an IN or | |
3638 | -- IN-OUT parameter. For composites, it is also clearly an | |
3639 | -- error to pass a completely uninitialized value as an IN | |
3640 | -- parameter, but the case of IN OUT is trickier. We prefer | |
3641 | -- not to give a warning here. For example, suppose there is | |
3642 | -- a routine that sets some component of a record to False. | |
3643 | -- It is perfectly reasonable to make this IN-OUT and allow | |
3644 | -- either initialized or uninitialized records to be passed | |
3645 | -- in this case. | |
3646 | ||
3647 | -- For partially initialized composite values, we also avoid | |
3648 | -- warnings, since it is quite likely that we are passing a | |
3649 | -- partially initialized value and only the initialized fields | |
3650 | -- will in fact be read in the subprogram. | |
3651 | ||
3652 | if Is_Scalar_Type (A_Typ) | |
3653 | or else (Ekind (F) = E_In_Parameter | |
19fb051c | 3654 | and then not Is_Partially_Initialized_Type (A_Typ)) |
996ae0b0 | 3655 | then |
fbf5a39b | 3656 | Check_Unset_Reference (A); |
996ae0b0 | 3657 | end if; |
996ae0b0 | 3658 | |
758c442c GD |
3659 | -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT |
3660 | -- actual to a nested call, since this is case of reading an | |
3661 | -- out parameter, which is not allowed. | |
996ae0b0 | 3662 | |
0ab80019 | 3663 | if Ada_Version = Ada_83 |
996ae0b0 RK |
3664 | and then Is_Entity_Name (A) |
3665 | and then Ekind (Entity (A)) = E_Out_Parameter | |
3666 | then | |
3667 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", A); | |
3668 | end if; | |
3669 | end if; | |
3670 | ||
67ce0d7e RD |
3671 | -- Case of OUT or IN OUT parameter |
3672 | ||
36fcf362 | 3673 | if Ekind (F) /= E_In_Parameter then |
67ce0d7e RD |
3674 | |
3675 | -- For an Out parameter, check for useless assignment. Note | |
45fc7ddb HK |
3676 | -- that we can't set Last_Assignment this early, because we may |
3677 | -- kill current values in Resolve_Call, and that call would | |
3678 | -- clobber the Last_Assignment field. | |
67ce0d7e | 3679 | |
45fc7ddb HK |
3680 | -- Note: call Warn_On_Useless_Assignment before doing the check |
3681 | -- below for Is_OK_Variable_For_Out_Formal so that the setting | |
3682 | -- of Referenced_As_LHS/Referenced_As_Out_Formal properly | |
3683 | -- reflects the last assignment, not this one! | |
36fcf362 | 3684 | |
67ce0d7e | 3685 | if Ekind (F) = E_Out_Parameter then |
36fcf362 | 3686 | if Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
3687 | and then Is_Entity_Name (A) |
3688 | and then Present (Entity (A)) | |
36fcf362 | 3689 | and then Comes_From_Source (N) |
67ce0d7e | 3690 | then |
36fcf362 | 3691 | Warn_On_Useless_Assignment (Entity (A), A); |
67ce0d7e RD |
3692 | end if; |
3693 | end if; | |
3694 | ||
36fcf362 RD |
3695 | -- Validate the form of the actual. Note that the call to |
3696 | -- Is_OK_Variable_For_Out_Formal generates the required | |
3697 | -- reference in this case. | |
3698 | ||
3699 | if not Is_OK_Variable_For_Out_Formal (A) then | |
3700 | Error_Msg_NE ("actual for& must be a variable", A, F); | |
3701 | end if; | |
3702 | ||
67ce0d7e | 3703 | -- What's the following about??? |
fbf5a39b AC |
3704 | |
3705 | if Is_Entity_Name (A) then | |
3706 | Kill_Checks (Entity (A)); | |
3707 | else | |
3708 | Kill_All_Checks; | |
3709 | end if; | |
3710 | end if; | |
3711 | ||
3712 | if Etype (A) = Any_Type then | |
3713 | Set_Etype (N, Any_Type); | |
3714 | return; | |
3715 | end if; | |
3716 | ||
996ae0b0 RK |
3717 | -- Apply appropriate range checks for in, out, and in-out |
3718 | -- parameters. Out and in-out parameters also need a separate | |
3719 | -- check, if there is a type conversion, to make sure the return | |
3720 | -- value meets the constraints of the variable before the | |
3721 | -- conversion. | |
3722 | ||
3723 | -- Gigi looks at the check flag and uses the appropriate types. | |
3724 | -- For now since one flag is used there is an optimization which | |
3725 | -- might not be done in the In Out case since Gigi does not do | |
3726 | -- any analysis. More thought required about this ??? | |
3727 | ||
8a95f4e8 | 3728 | if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then |
48f91b44 RD |
3729 | |
3730 | -- Apply predicate checks, unless this is a call to the | |
3731 | -- predicate check function itself, which would cause an | |
3732 | -- infinite recursion. | |
3733 | ||
3734 | if not (Ekind (Nam) = E_Function | |
3735 | and then Has_Predicates (Nam)) | |
3736 | then | |
3737 | Apply_Predicate_Check (A, F_Typ); | |
3738 | end if; | |
3739 | ||
3740 | -- Apply required constraint checks | |
3741 | ||
996ae0b0 RK |
3742 | if Is_Scalar_Type (Etype (A)) then |
3743 | Apply_Scalar_Range_Check (A, F_Typ); | |
3744 | ||
3745 | elsif Is_Array_Type (Etype (A)) then | |
3746 | Apply_Length_Check (A, F_Typ); | |
3747 | ||
3748 | elsif Is_Record_Type (F_Typ) | |
3749 | and then Has_Discriminants (F_Typ) | |
3750 | and then Is_Constrained (F_Typ) | |
3751 | and then (not Is_Derived_Type (F_Typ) | |
19fb051c | 3752 | or else Comes_From_Source (Nam)) |
996ae0b0 RK |
3753 | then |
3754 | Apply_Discriminant_Check (A, F_Typ); | |
3755 | ||
3756 | elsif Is_Access_Type (F_Typ) | |
3757 | and then Is_Array_Type (Designated_Type (F_Typ)) | |
3758 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3759 | then | |
3760 | Apply_Length_Check (A, F_Typ); | |
3761 | ||
3762 | elsif Is_Access_Type (F_Typ) | |
3763 | and then Has_Discriminants (Designated_Type (F_Typ)) | |
3764 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3765 | then | |
3766 | Apply_Discriminant_Check (A, F_Typ); | |
3767 | ||
3768 | else | |
3769 | Apply_Range_Check (A, F_Typ); | |
3770 | end if; | |
2820d220 | 3771 | |
0f1a6a0b AC |
3772 | -- Ada 2005 (AI-231): Note that the controlling parameter case |
3773 | -- already existed in Ada 95, which is partially checked | |
3774 | -- elsewhere (see Checks), and we don't want the warning | |
3775 | -- message to differ. | |
2820d220 | 3776 | |
0f1a6a0b | 3777 | if Is_Access_Type (F_Typ) |
1420b484 | 3778 | and then Can_Never_Be_Null (F_Typ) |
aa5147f0 | 3779 | and then Known_Null (A) |
2820d220 | 3780 | then |
0f1a6a0b AC |
3781 | if Is_Controlling_Formal (F) then |
3782 | Apply_Compile_Time_Constraint_Error | |
3783 | (N => A, | |
3784 | Msg => "null value not allowed here?", | |
3785 | Reason => CE_Access_Check_Failed); | |
3786 | ||
3787 | elsif Ada_Version >= Ada_2005 then | |
3788 | Apply_Compile_Time_Constraint_Error | |
3789 | (N => A, | |
3790 | Msg => "(Ada 2005) null not allowed in " | |
3791 | & "null-excluding formal?", | |
3792 | Reason => CE_Null_Not_Allowed); | |
3793 | end if; | |
2820d220 | 3794 | end if; |
996ae0b0 RK |
3795 | end if; |
3796 | ||
8a95f4e8 | 3797 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then |
996ae0b0 RK |
3798 | if Nkind (A) = N_Type_Conversion then |
3799 | if Is_Scalar_Type (A_Typ) then | |
3800 | Apply_Scalar_Range_Check | |
3801 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3802 | else | |
3803 | Apply_Range_Check | |
3804 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3805 | end if; | |
3806 | ||
3807 | else | |
3808 | if Is_Scalar_Type (F_Typ) then | |
3809 | Apply_Scalar_Range_Check (A, A_Typ, F_Typ); | |
996ae0b0 RK |
3810 | elsif Is_Array_Type (F_Typ) |
3811 | and then Ekind (F) = E_Out_Parameter | |
3812 | then | |
3813 | Apply_Length_Check (A, F_Typ); | |
996ae0b0 RK |
3814 | else |
3815 | Apply_Range_Check (A, A_Typ, F_Typ); | |
3816 | end if; | |
3817 | end if; | |
3818 | end if; | |
3819 | ||
3820 | -- An actual associated with an access parameter is implicitly | |
45fc7ddb HK |
3821 | -- converted to the anonymous access type of the formal and must |
3822 | -- satisfy the legality checks for access conversions. | |
996ae0b0 RK |
3823 | |
3824 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
3825 | if not Valid_Conversion (A, F_Typ, A) then | |
3826 | Error_Msg_N | |
3827 | ("invalid implicit conversion for access parameter", A); | |
3828 | end if; | |
3829 | end if; | |
3830 | ||
3831 | -- Check bad case of atomic/volatile argument (RM C.6(12)) | |
3832 | ||
3833 | if Is_By_Reference_Type (Etype (F)) | |
3834 | and then Comes_From_Source (N) | |
3835 | then | |
3836 | if Is_Atomic_Object (A) | |
3837 | and then not Is_Atomic (Etype (F)) | |
3838 | then | |
3839 | Error_Msg_N | |
3840 | ("cannot pass atomic argument to non-atomic formal", | |
3841 | N); | |
3842 | ||
3843 | elsif Is_Volatile_Object (A) | |
3844 | and then not Is_Volatile (Etype (F)) | |
3845 | then | |
3846 | Error_Msg_N | |
3847 | ("cannot pass volatile argument to non-volatile formal", | |
3848 | N); | |
3849 | end if; | |
3850 | end if; | |
3851 | ||
3852 | -- Check that subprograms don't have improper controlling | |
d81b4bfe | 3853 | -- arguments (RM 3.9.2 (9)). |
996ae0b0 | 3854 | |
0669bebe GB |
3855 | -- A primitive operation may have an access parameter of an |
3856 | -- incomplete tagged type, but a dispatching call is illegal | |
3857 | -- if the type is still incomplete. | |
3858 | ||
996ae0b0 RK |
3859 | if Is_Controlling_Formal (F) then |
3860 | Set_Is_Controlling_Actual (A); | |
0669bebe GB |
3861 | |
3862 | if Ekind (Etype (F)) = E_Anonymous_Access_Type then | |
3863 | declare | |
3864 | Desig : constant Entity_Id := Designated_Type (Etype (F)); | |
3865 | begin | |
3866 | if Ekind (Desig) = E_Incomplete_Type | |
3867 | and then No (Full_View (Desig)) | |
3868 | and then No (Non_Limited_View (Desig)) | |
3869 | then | |
3870 | Error_Msg_NE | |
3871 | ("premature use of incomplete type& " & | |
3872 | "in dispatching call", A, Desig); | |
3873 | end if; | |
3874 | end; | |
3875 | end if; | |
3876 | ||
996ae0b0 RK |
3877 | elsif Nkind (A) = N_Explicit_Dereference then |
3878 | Validate_Remote_Access_To_Class_Wide_Type (A); | |
3879 | end if; | |
3880 | ||
3881 | if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A)) | |
3882 | and then not Is_Class_Wide_Type (F_Typ) | |
3883 | and then not Is_Controlling_Formal (F) | |
3884 | then | |
3885 | Error_Msg_N ("class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3886 | |
3887 | if Is_Subprogram (Nam) | |
3888 | and then Comes_From_Source (Nam) | |
3889 | then | |
996ae0b0 RK |
3890 | Error_Msg_Node_2 := F_Typ; |
3891 | Error_Msg_NE | |
82c80734 | 3892 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3893 | end if; |
3894 | ||
3895 | elsif Is_Access_Type (A_Typ) | |
3896 | and then Is_Access_Type (F_Typ) | |
3897 | and then Ekind (F_Typ) /= E_Access_Subprogram_Type | |
aa5147f0 | 3898 | and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type |
996ae0b0 | 3899 | and then (Is_Class_Wide_Type (Designated_Type (A_Typ)) |
07fc65c4 GB |
3900 | or else (Nkind (A) = N_Attribute_Reference |
3901 | and then | |
46fe0142 | 3902 | Is_Class_Wide_Type (Etype (Prefix (A))))) |
996ae0b0 RK |
3903 | and then not Is_Class_Wide_Type (Designated_Type (F_Typ)) |
3904 | and then not Is_Controlling_Formal (F) | |
ae65d635 | 3905 | |
46fe0142 | 3906 | -- Disable these checks for call to imported C++ subprograms |
ae65d635 | 3907 | |
46fe0142 AC |
3908 | and then not |
3909 | (Is_Entity_Name (Name (N)) | |
3910 | and then Is_Imported (Entity (Name (N))) | |
3911 | and then Convention (Entity (Name (N))) = Convention_CPP) | |
996ae0b0 RK |
3912 | then |
3913 | Error_Msg_N | |
3914 | ("access to class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3915 | |
3916 | if Is_Subprogram (Nam) | |
3917 | and then Comes_From_Source (Nam) | |
3918 | then | |
996ae0b0 RK |
3919 | Error_Msg_Node_2 := Designated_Type (F_Typ); |
3920 | Error_Msg_NE | |
82c80734 | 3921 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3922 | end if; |
3923 | end if; | |
3924 | ||
3925 | Eval_Actual (A); | |
3926 | ||
8e4dac80 TQ |
3927 | -- If it is a named association, treat the selector_name as a |
3928 | -- proper identifier, and mark the corresponding entity. | |
996ae0b0 RK |
3929 | |
3930 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3931 | Set_Entity (Selector_Name (Parent (A)), F); | |
3932 | Generate_Reference (F, Selector_Name (Parent (A))); | |
3933 | Set_Etype (Selector_Name (Parent (A)), F_Typ); | |
3934 | Generate_Reference (F_Typ, N, ' '); | |
3935 | end if; | |
3936 | ||
3937 | Prev := A; | |
fbf5a39b AC |
3938 | |
3939 | if Ekind (F) /= E_Out_Parameter then | |
3940 | Check_Unset_Reference (A); | |
3941 | end if; | |
3942 | ||
996ae0b0 RK |
3943 | Next_Actual (A); |
3944 | ||
fbf5a39b AC |
3945 | -- Case where actual is not present |
3946 | ||
996ae0b0 RK |
3947 | else |
3948 | Insert_Default; | |
3949 | end if; | |
3950 | ||
3951 | Next_Formal (F); | |
3952 | end loop; | |
996ae0b0 RK |
3953 | end Resolve_Actuals; |
3954 | ||
3955 | ----------------------- | |
3956 | -- Resolve_Allocator -- | |
3957 | ----------------------- | |
3958 | ||
3959 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is | |
3960 | E : constant Node_Id := Expression (N); | |
3961 | Subtyp : Entity_Id; | |
3962 | Discrim : Entity_Id; | |
3963 | Constr : Node_Id; | |
b7d1f17f HK |
3964 | Aggr : Node_Id; |
3965 | Assoc : Node_Id := Empty; | |
996ae0b0 RK |
3966 | Disc_Exp : Node_Id; |
3967 | ||
b7d1f17f HK |
3968 | procedure Check_Allocator_Discrim_Accessibility |
3969 | (Disc_Exp : Node_Id; | |
3970 | Alloc_Typ : Entity_Id); | |
3971 | -- Check that accessibility level associated with an access discriminant | |
3972 | -- initialized in an allocator by the expression Disc_Exp is not deeper | |
3973 | -- than the level of the allocator type Alloc_Typ. An error message is | |
3974 | -- issued if this condition is violated. Specialized checks are done for | |
3975 | -- the cases of a constraint expression which is an access attribute or | |
3976 | -- an access discriminant. | |
3977 | ||
07fc65c4 | 3978 | function In_Dispatching_Context return Boolean; |
b7d1f17f HK |
3979 | -- If the allocator is an actual in a call, it is allowed to be class- |
3980 | -- wide when the context is not because it is a controlling actual. | |
3981 | ||
3982 | procedure Propagate_Coextensions (Root : Node_Id); | |
3983 | -- Propagate all nested coextensions which are located one nesting | |
3984 | -- level down the tree to the node Root. Example: | |
3985 | -- | |
3986 | -- Top_Record | |
3987 | -- Level_1_Coextension | |
3988 | -- Level_2_Coextension | |
3989 | -- | |
3990 | -- The algorithm is paired with delay actions done by the Expander. In | |
3991 | -- the above example, assume all coextensions are controlled types. | |
3992 | -- The cycle of analysis, resolution and expansion will yield: | |
3993 | -- | |
3994 | -- 1) Analyze Top_Record | |
3995 | -- 2) Analyze Level_1_Coextension | |
3996 | -- 3) Analyze Level_2_Coextension | |
f3d57416 | 3997 | -- 4) Resolve Level_2_Coextension. The allocator is marked as a |
b7d1f17f HK |
3998 | -- coextension. |
3999 | -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is | |
4000 | -- generated to capture the allocated object. Temp_1 is attached | |
4001 | -- to the coextension chain of Level_2_Coextension. | |
4002 | -- 6) Resolve Level_1_Coextension. The allocator is marked as a | |
4003 | -- coextension. A forward tree traversal is performed which finds | |
4004 | -- Level_2_Coextension's list and copies its contents into its | |
4005 | -- own list. | |
4006 | -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is | |
4007 | -- generated to capture the allocated object. Temp_2 is attached | |
4008 | -- to the coextension chain of Level_1_Coextension. Currently, the | |
4009 | -- contents of the list are [Temp_2, Temp_1]. | |
4010 | -- 8) Resolve Top_Record. A forward tree traversal is performed which | |
4011 | -- finds Level_1_Coextension's list and copies its contents into | |
4012 | -- its own list. | |
4013 | -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and | |
4014 | -- Temp_2 and attach them to Top_Record's finalization list. | |
4015 | ||
4016 | ------------------------------------------- | |
4017 | -- Check_Allocator_Discrim_Accessibility -- | |
4018 | ------------------------------------------- | |
4019 | ||
4020 | procedure Check_Allocator_Discrim_Accessibility | |
4021 | (Disc_Exp : Node_Id; | |
4022 | Alloc_Typ : Entity_Id) | |
4023 | is | |
4024 | begin | |
4025 | if Type_Access_Level (Etype (Disc_Exp)) > | |
4026 | Type_Access_Level (Alloc_Typ) | |
4027 | then | |
4028 | Error_Msg_N | |
4029 | ("operand type has deeper level than allocator type", Disc_Exp); | |
4030 | ||
4031 | -- When the expression is an Access attribute the level of the prefix | |
4032 | -- object must not be deeper than that of the allocator's type. | |
4033 | ||
4034 | elsif Nkind (Disc_Exp) = N_Attribute_Reference | |
4035 | and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) | |
4036 | = Attribute_Access | |
4037 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
4038 | > Type_Access_Level (Alloc_Typ) | |
4039 | then | |
4040 | Error_Msg_N | |
4041 | ("prefix of attribute has deeper level than allocator type", | |
4042 | Disc_Exp); | |
4043 | ||
4044 | -- When the expression is an access discriminant the check is against | |
4045 | -- the level of the prefix object. | |
4046 | ||
4047 | elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type | |
4048 | and then Nkind (Disc_Exp) = N_Selected_Component | |
4049 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
4050 | > Type_Access_Level (Alloc_Typ) | |
4051 | then | |
4052 | Error_Msg_N | |
4053 | ("access discriminant has deeper level than allocator type", | |
4054 | Disc_Exp); | |
4055 | ||
4056 | -- All other cases are legal | |
4057 | ||
4058 | else | |
4059 | null; | |
4060 | end if; | |
4061 | end Check_Allocator_Discrim_Accessibility; | |
07fc65c4 GB |
4062 | |
4063 | ---------------------------- | |
4064 | -- In_Dispatching_Context -- | |
4065 | ---------------------------- | |
4066 | ||
4067 | function In_Dispatching_Context return Boolean is | |
4068 | Par : constant Node_Id := Parent (N); | |
07fc65c4 | 4069 | begin |
45fc7ddb | 4070 | return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement) |
07fc65c4 GB |
4071 | and then Is_Entity_Name (Name (Par)) |
4072 | and then Is_Dispatching_Operation (Entity (Name (Par))); | |
4073 | end In_Dispatching_Context; | |
4074 | ||
b7d1f17f HK |
4075 | ---------------------------- |
4076 | -- Propagate_Coextensions -- | |
4077 | ---------------------------- | |
4078 | ||
4079 | procedure Propagate_Coextensions (Root : Node_Id) is | |
4080 | ||
4081 | procedure Copy_List (From : Elist_Id; To : Elist_Id); | |
4082 | -- Copy the contents of list From into list To, preserving the | |
4083 | -- order of elements. | |
4084 | ||
4085 | function Process_Allocator (Nod : Node_Id) return Traverse_Result; | |
4086 | -- Recognize an allocator or a rewritten allocator node and add it | |
f3d57416 | 4087 | -- along with its nested coextensions to the list of Root. |
b7d1f17f HK |
4088 | |
4089 | --------------- | |
4090 | -- Copy_List -- | |
4091 | --------------- | |
4092 | ||
4093 | procedure Copy_List (From : Elist_Id; To : Elist_Id) is | |
4094 | From_Elmt : Elmt_Id; | |
4095 | begin | |
4096 | From_Elmt := First_Elmt (From); | |
4097 | while Present (From_Elmt) loop | |
4098 | Append_Elmt (Node (From_Elmt), To); | |
4099 | Next_Elmt (From_Elmt); | |
4100 | end loop; | |
4101 | end Copy_List; | |
4102 | ||
4103 | ----------------------- | |
4104 | -- Process_Allocator -- | |
4105 | ----------------------- | |
4106 | ||
4107 | function Process_Allocator (Nod : Node_Id) return Traverse_Result is | |
4108 | Orig_Nod : Node_Id := Nod; | |
4109 | ||
4110 | begin | |
4111 | -- This is a possible rewritten subtype indication allocator. Any | |
4112 | -- nested coextensions will appear as discriminant constraints. | |
4113 | ||
4114 | if Nkind (Nod) = N_Identifier | |
4115 | and then Present (Original_Node (Nod)) | |
4116 | and then Nkind (Original_Node (Nod)) = N_Subtype_Indication | |
4117 | then | |
4118 | declare | |
4119 | Discr : Node_Id; | |
4120 | Discr_Elmt : Elmt_Id; | |
4121 | ||
4122 | begin | |
4123 | if Is_Record_Type (Entity (Nod)) then | |
4124 | Discr_Elmt := | |
4125 | First_Elmt (Discriminant_Constraint (Entity (Nod))); | |
4126 | while Present (Discr_Elmt) loop | |
4127 | Discr := Node (Discr_Elmt); | |
4128 | ||
4129 | if Nkind (Discr) = N_Identifier | |
4130 | and then Present (Original_Node (Discr)) | |
4131 | and then Nkind (Original_Node (Discr)) = N_Allocator | |
4132 | and then Present (Coextensions ( | |
4133 | Original_Node (Discr))) | |
4134 | then | |
4135 | if No (Coextensions (Root)) then | |
4136 | Set_Coextensions (Root, New_Elmt_List); | |
4137 | end if; | |
4138 | ||
4139 | Copy_List | |
4140 | (From => Coextensions (Original_Node (Discr)), | |
4141 | To => Coextensions (Root)); | |
4142 | end if; | |
4143 | ||
4144 | Next_Elmt (Discr_Elmt); | |
4145 | end loop; | |
4146 | ||
4147 | -- There is no need to continue the traversal of this | |
4148 | -- subtree since all the information has already been | |
4149 | -- propagated. | |
4150 | ||
4151 | return Skip; | |
4152 | end if; | |
4153 | end; | |
4154 | ||
4155 | -- Case of either a stand alone allocator or a rewritten allocator | |
4156 | -- with an aggregate. | |
4157 | ||
4158 | else | |
4159 | if Present (Original_Node (Nod)) then | |
4160 | Orig_Nod := Original_Node (Nod); | |
4161 | end if; | |
4162 | ||
4163 | if Nkind (Orig_Nod) = N_Allocator then | |
4164 | ||
4165 | -- Propagate the list of nested coextensions to the Root | |
4166 | -- allocator. This is done through list copy since a single | |
4167 | -- allocator may have multiple coextensions. Do not touch | |
4168 | -- coextensions roots. | |
4169 | ||
4170 | if not Is_Coextension_Root (Orig_Nod) | |
4171 | and then Present (Coextensions (Orig_Nod)) | |
4172 | then | |
4173 | if No (Coextensions (Root)) then | |
4174 | Set_Coextensions (Root, New_Elmt_List); | |
4175 | end if; | |
4176 | ||
4177 | Copy_List | |
4178 | (From => Coextensions (Orig_Nod), | |
4179 | To => Coextensions (Root)); | |
4180 | end if; | |
4181 | ||
4182 | -- There is no need to continue the traversal of this | |
4183 | -- subtree since all the information has already been | |
4184 | -- propagated. | |
4185 | ||
4186 | return Skip; | |
4187 | end if; | |
4188 | end if; | |
4189 | ||
4190 | -- Keep on traversing, looking for the next allocator | |
4191 | ||
4192 | return OK; | |
4193 | end Process_Allocator; | |
4194 | ||
4195 | procedure Process_Allocators is | |
4196 | new Traverse_Proc (Process_Allocator); | |
4197 | ||
4198 | -- Start of processing for Propagate_Coextensions | |
4199 | ||
4200 | begin | |
4201 | Process_Allocators (Expression (Root)); | |
4202 | end Propagate_Coextensions; | |
4203 | ||
07fc65c4 GB |
4204 | -- Start of processing for Resolve_Allocator |
4205 | ||
996ae0b0 RK |
4206 | begin |
4207 | -- Replace general access with specific type | |
4208 | ||
4209 | if Ekind (Etype (N)) = E_Allocator_Type then | |
4210 | Set_Etype (N, Base_Type (Typ)); | |
4211 | end if; | |
4212 | ||
0669bebe | 4213 | if Is_Abstract_Type (Typ) then |
996ae0b0 RK |
4214 | Error_Msg_N ("type of allocator cannot be abstract", N); |
4215 | end if; | |
4216 | ||
4217 | -- For qualified expression, resolve the expression using the | |
4218 | -- given subtype (nothing to do for type mark, subtype indication) | |
4219 | ||
4220 | if Nkind (E) = N_Qualified_Expression then | |
4221 | if Is_Class_Wide_Type (Etype (E)) | |
4222 | and then not Is_Class_Wide_Type (Designated_Type (Typ)) | |
07fc65c4 | 4223 | and then not In_Dispatching_Context |
996ae0b0 RK |
4224 | then |
4225 | Error_Msg_N | |
4226 | ("class-wide allocator not allowed for this access type", N); | |
4227 | end if; | |
4228 | ||
4229 | Resolve (Expression (E), Etype (E)); | |
4230 | Check_Unset_Reference (Expression (E)); | |
4231 | ||
fbf5a39b | 4232 | -- A qualified expression requires an exact match of the type, |
7b4db06c | 4233 | -- class-wide matching is not allowed. |
fbf5a39b | 4234 | |
7b4db06c | 4235 | if (Is_Class_Wide_Type (Etype (Expression (E))) |
19fb051c | 4236 | or else Is_Class_Wide_Type (Etype (E))) |
fbf5a39b AC |
4237 | and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E)) |
4238 | then | |
4239 | Wrong_Type (Expression (E), Etype (E)); | |
4240 | end if; | |
4241 | ||
b7d1f17f HK |
4242 | -- A special accessibility check is needed for allocators that |
4243 | -- constrain access discriminants. The level of the type of the | |
4244 | -- expression used to constrain an access discriminant cannot be | |
f3d57416 | 4245 | -- deeper than the type of the allocator (in contrast to access |
b7d1f17f HK |
4246 | -- parameters, where the level of the actual can be arbitrary). |
4247 | ||
4248 | -- We can't use Valid_Conversion to perform this check because | |
4249 | -- in general the type of the allocator is unrelated to the type | |
4250 | -- of the access discriminant. | |
4251 | ||
4252 | if Ekind (Typ) /= E_Anonymous_Access_Type | |
4253 | or else Is_Local_Anonymous_Access (Typ) | |
4254 | then | |
4255 | Subtyp := Entity (Subtype_Mark (E)); | |
4256 | ||
4257 | Aggr := Original_Node (Expression (E)); | |
4258 | ||
4259 | if Has_Discriminants (Subtyp) | |
45fc7ddb | 4260 | and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate) |
b7d1f17f HK |
4261 | then |
4262 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4263 | ||
4264 | -- Get the first component expression of the aggregate | |
4265 | ||
4266 | if Present (Expressions (Aggr)) then | |
4267 | Disc_Exp := First (Expressions (Aggr)); | |
4268 | ||
4269 | elsif Present (Component_Associations (Aggr)) then | |
4270 | Assoc := First (Component_Associations (Aggr)); | |
4271 | ||
4272 | if Present (Assoc) then | |
4273 | Disc_Exp := Expression (Assoc); | |
4274 | else | |
4275 | Disc_Exp := Empty; | |
4276 | end if; | |
4277 | ||
4278 | else | |
4279 | Disc_Exp := Empty; | |
4280 | end if; | |
4281 | ||
4282 | while Present (Discrim) and then Present (Disc_Exp) loop | |
4283 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4284 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); | |
4285 | end if; | |
4286 | ||
4287 | Next_Discriminant (Discrim); | |
4288 | ||
4289 | if Present (Discrim) then | |
4290 | if Present (Assoc) then | |
4291 | Next (Assoc); | |
4292 | Disc_Exp := Expression (Assoc); | |
4293 | ||
4294 | elsif Present (Next (Disc_Exp)) then | |
4295 | Next (Disc_Exp); | |
4296 | ||
4297 | else | |
4298 | Assoc := First (Component_Associations (Aggr)); | |
4299 | ||
4300 | if Present (Assoc) then | |
4301 | Disc_Exp := Expression (Assoc); | |
4302 | else | |
4303 | Disc_Exp := Empty; | |
4304 | end if; | |
4305 | end if; | |
4306 | end if; | |
4307 | end loop; | |
4308 | end if; | |
4309 | end if; | |
4310 | ||
996ae0b0 RK |
4311 | -- For a subtype mark or subtype indication, freeze the subtype |
4312 | ||
4313 | else | |
4314 | Freeze_Expression (E); | |
4315 | ||
4316 | if Is_Access_Constant (Typ) and then not No_Initialization (N) then | |
4317 | Error_Msg_N | |
4318 | ("initialization required for access-to-constant allocator", N); | |
4319 | end if; | |
4320 | ||
4321 | -- A special accessibility check is needed for allocators that | |
4322 | -- constrain access discriminants. The level of the type of the | |
b7d1f17f | 4323 | -- expression used to constrain an access discriminant cannot be |
f3d57416 | 4324 | -- deeper than the type of the allocator (in contrast to access |
996ae0b0 RK |
4325 | -- parameters, where the level of the actual can be arbitrary). |
4326 | -- We can't use Valid_Conversion to perform this check because | |
4327 | -- in general the type of the allocator is unrelated to the type | |
b7d1f17f | 4328 | -- of the access discriminant. |
996ae0b0 RK |
4329 | |
4330 | if Nkind (Original_Node (E)) = N_Subtype_Indication | |
b7d1f17f HK |
4331 | and then (Ekind (Typ) /= E_Anonymous_Access_Type |
4332 | or else Is_Local_Anonymous_Access (Typ)) | |
996ae0b0 RK |
4333 | then |
4334 | Subtyp := Entity (Subtype_Mark (Original_Node (E))); | |
4335 | ||
4336 | if Has_Discriminants (Subtyp) then | |
4337 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4338 | Constr := First (Constraints (Constraint (Original_Node (E)))); | |
996ae0b0 RK |
4339 | while Present (Discrim) and then Present (Constr) loop |
4340 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4341 | if Nkind (Constr) = N_Discriminant_Association then | |
4342 | Disc_Exp := Original_Node (Expression (Constr)); | |
4343 | else | |
4344 | Disc_Exp := Original_Node (Constr); | |
4345 | end if; | |
4346 | ||
b7d1f17f | 4347 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); |
996ae0b0 | 4348 | end if; |
b7d1f17f | 4349 | |
996ae0b0 RK |
4350 | Next_Discriminant (Discrim); |
4351 | Next (Constr); | |
4352 | end loop; | |
4353 | end if; | |
4354 | end if; | |
4355 | end if; | |
4356 | ||
758c442c GD |
4357 | -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility |
4358 | -- check that the level of the type of the created object is not deeper | |
4359 | -- than the level of the allocator's access type, since extensions can | |
4360 | -- now occur at deeper levels than their ancestor types. This is a | |
4361 | -- static accessibility level check; a run-time check is also needed in | |
4362 | -- the case of an initialized allocator with a class-wide argument (see | |
4363 | -- Expand_Allocator_Expression). | |
4364 | ||
0791fbe9 | 4365 | if Ada_Version >= Ada_2005 |
758c442c GD |
4366 | and then Is_Class_Wide_Type (Designated_Type (Typ)) |
4367 | then | |
4368 | declare | |
b7d1f17f | 4369 | Exp_Typ : Entity_Id; |
758c442c GD |
4370 | |
4371 | begin | |
4372 | if Nkind (E) = N_Qualified_Expression then | |
4373 | Exp_Typ := Etype (E); | |
4374 | elsif Nkind (E) = N_Subtype_Indication then | |
4375 | Exp_Typ := Entity (Subtype_Mark (Original_Node (E))); | |
4376 | else | |
4377 | Exp_Typ := Entity (E); | |
4378 | end if; | |
4379 | ||
4380 | if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then | |
4381 | if In_Instance_Body then | |
4382 | Error_Msg_N ("?type in allocator has deeper level than" & | |
4383 | " designated class-wide type", E); | |
c8ef728f ES |
4384 | Error_Msg_N ("\?Program_Error will be raised at run time", |
4385 | E); | |
758c442c GD |
4386 | Rewrite (N, |
4387 | Make_Raise_Program_Error (Sloc (N), | |
4388 | Reason => PE_Accessibility_Check_Failed)); | |
4389 | Set_Etype (N, Typ); | |
aa180613 RD |
4390 | |
4391 | -- Do not apply Ada 2005 accessibility checks on a class-wide | |
4392 | -- allocator if the type given in the allocator is a formal | |
4393 | -- type. A run-time check will be performed in the instance. | |
4394 | ||
4395 | elsif not Is_Generic_Type (Exp_Typ) then | |
758c442c GD |
4396 | Error_Msg_N ("type in allocator has deeper level than" & |
4397 | " designated class-wide type", E); | |
4398 | end if; | |
4399 | end if; | |
4400 | end; | |
4401 | end if; | |
4402 | ||
996ae0b0 RK |
4403 | -- Check for allocation from an empty storage pool |
4404 | ||
4405 | if No_Pool_Assigned (Typ) then | |
8da337c5 | 4406 | Error_Msg_N ("allocation from empty storage pool!", N); |
1420b484 | 4407 | |
5cc9353d RD |
4408 | -- If the context is an unchecked conversion, as may happen within an |
4409 | -- inlined subprogram, the allocator is being resolved with its own | |
4410 | -- anonymous type. In that case, if the target type has a specific | |
1420b484 JM |
4411 | -- storage pool, it must be inherited explicitly by the allocator type. |
4412 | ||
4413 | elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion | |
4414 | and then No (Associated_Storage_Pool (Typ)) | |
4415 | then | |
4416 | Set_Associated_Storage_Pool | |
4417 | (Typ, Associated_Storage_Pool (Etype (Parent (N)))); | |
996ae0b0 | 4418 | end if; |
b7d1f17f | 4419 | |
e57ab550 AC |
4420 | if Ekind (Etype (N)) = E_Anonymous_Access_Type then |
4421 | Check_Restriction (No_Anonymous_Allocators, N); | |
4422 | end if; | |
4423 | ||
b7d1f17f HK |
4424 | -- An erroneous allocator may be rewritten as a raise Program_Error |
4425 | -- statement. | |
4426 | ||
4427 | if Nkind (N) = N_Allocator then | |
4428 | ||
4429 | -- An anonymous access discriminant is the definition of a | |
aa5147f0 | 4430 | -- coextension. |
b7d1f17f HK |
4431 | |
4432 | if Ekind (Typ) = E_Anonymous_Access_Type | |
4433 | and then Nkind (Associated_Node_For_Itype (Typ)) = | |
4434 | N_Discriminant_Specification | |
4435 | then | |
4436 | -- Avoid marking an allocator as a dynamic coextension if it is | |
aa5147f0 | 4437 | -- within a static construct. |
b7d1f17f HK |
4438 | |
4439 | if not Is_Static_Coextension (N) then | |
aa5147f0 | 4440 | Set_Is_Dynamic_Coextension (N); |
b7d1f17f HK |
4441 | end if; |
4442 | ||
4443 | -- Cleanup for potential static coextensions | |
4444 | ||
4445 | else | |
aa5147f0 ES |
4446 | Set_Is_Dynamic_Coextension (N, False); |
4447 | Set_Is_Static_Coextension (N, False); | |
b7d1f17f HK |
4448 | end if; |
4449 | ||
aa5147f0 ES |
4450 | -- There is no need to propagate any nested coextensions if they |
4451 | -- are marked as static since they will be rewritten on the spot. | |
4452 | ||
4453 | if not Is_Static_Coextension (N) then | |
4454 | Propagate_Coextensions (N); | |
4455 | end if; | |
b7d1f17f | 4456 | end if; |
996ae0b0 RK |
4457 | end Resolve_Allocator; |
4458 | ||
4459 | --------------------------- | |
4460 | -- Resolve_Arithmetic_Op -- | |
4461 | --------------------------- | |
4462 | ||
4463 | -- Used for resolving all arithmetic operators except exponentiation | |
4464 | ||
4465 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
4466 | L : constant Node_Id := Left_Opnd (N); |
4467 | R : constant Node_Id := Right_Opnd (N); | |
4468 | TL : constant Entity_Id := Base_Type (Etype (L)); | |
4469 | TR : constant Entity_Id := Base_Type (Etype (R)); | |
4470 | T : Entity_Id; | |
4471 | Rop : Node_Id; | |
996ae0b0 RK |
4472 | |
4473 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
4474 | -- We do the resolution using the base type, because intermediate values | |
4475 | -- in expressions always are of the base type, not a subtype of it. | |
4476 | ||
aa180613 RD |
4477 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean; |
4478 | -- Returns True if N is in a context that expects "any real type" | |
4479 | ||
996ae0b0 RK |
4480 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean; |
4481 | -- Return True iff given type is Integer or universal real/integer | |
4482 | ||
4483 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id); | |
4484 | -- Choose type of integer literal in fixed-point operation to conform | |
4485 | -- to available fixed-point type. T is the type of the other operand, | |
4486 | -- which is needed to determine the expected type of N. | |
4487 | ||
4488 | procedure Set_Operand_Type (N : Node_Id); | |
4489 | -- Set operand type to T if universal | |
4490 | ||
aa180613 RD |
4491 | ------------------------------- |
4492 | -- Expected_Type_Is_Any_Real -- | |
4493 | ------------------------------- | |
4494 | ||
4495 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is | |
4496 | begin | |
4497 | -- N is the expression after "delta" in a fixed_point_definition; | |
4498 | -- see RM-3.5.9(6): | |
4499 | ||
45fc7ddb HK |
4500 | return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition, |
4501 | N_Decimal_Fixed_Point_Definition, | |
aa180613 RD |
4502 | |
4503 | -- N is one of the bounds in a real_range_specification; | |
4504 | -- see RM-3.5.7(5): | |
4505 | ||
45fc7ddb | 4506 | N_Real_Range_Specification, |
aa180613 RD |
4507 | |
4508 | -- N is the expression of a delta_constraint; | |
4509 | -- see RM-J.3(3): | |
4510 | ||
45fc7ddb | 4511 | N_Delta_Constraint); |
aa180613 RD |
4512 | end Expected_Type_Is_Any_Real; |
4513 | ||
996ae0b0 RK |
4514 | ----------------------------- |
4515 | -- Is_Integer_Or_Universal -- | |
4516 | ----------------------------- | |
4517 | ||
4518 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean is | |
4519 | T : Entity_Id; | |
4520 | Index : Interp_Index; | |
4521 | It : Interp; | |
4522 | ||
4523 | begin | |
4524 | if not Is_Overloaded (N) then | |
4525 | T := Etype (N); | |
4526 | return Base_Type (T) = Base_Type (Standard_Integer) | |
4527 | or else T = Universal_Integer | |
4528 | or else T = Universal_Real; | |
4529 | else | |
4530 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4531 | while Present (It.Typ) loop |
996ae0b0 RK |
4532 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) |
4533 | or else It.Typ = Universal_Integer | |
4534 | or else It.Typ = Universal_Real | |
4535 | then | |
4536 | return True; | |
4537 | end if; | |
4538 | ||
4539 | Get_Next_Interp (Index, It); | |
4540 | end loop; | |
4541 | end if; | |
4542 | ||
4543 | return False; | |
4544 | end Is_Integer_Or_Universal; | |
4545 | ||
4546 | ---------------------------- | |
4547 | -- Set_Mixed_Mode_Operand -- | |
4548 | ---------------------------- | |
4549 | ||
4550 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is | |
4551 | Index : Interp_Index; | |
4552 | It : Interp; | |
4553 | ||
4554 | begin | |
4555 | if Universal_Interpretation (N) = Universal_Integer then | |
4556 | ||
4557 | -- A universal integer literal is resolved as standard integer | |
758c442c GD |
4558 | -- except in the case of a fixed-point result, where we leave it |
4559 | -- as universal (to be handled by Exp_Fixd later on) | |
996ae0b0 RK |
4560 | |
4561 | if Is_Fixed_Point_Type (T) then | |
4562 | Resolve (N, Universal_Integer); | |
4563 | else | |
4564 | Resolve (N, Standard_Integer); | |
4565 | end if; | |
4566 | ||
4567 | elsif Universal_Interpretation (N) = Universal_Real | |
4568 | and then (T = Base_Type (Standard_Integer) | |
4569 | or else T = Universal_Integer | |
4570 | or else T = Universal_Real) | |
4571 | then | |
4572 | -- A universal real can appear in a fixed-type context. We resolve | |
4573 | -- the literal with that context, even though this might raise an | |
4574 | -- exception prematurely (the other operand may be zero). | |
4575 | ||
4576 | Resolve (N, B_Typ); | |
4577 | ||
4578 | elsif Etype (N) = Base_Type (Standard_Integer) | |
4579 | and then T = Universal_Real | |
4580 | and then Is_Overloaded (N) | |
4581 | then | |
4582 | -- Integer arg in mixed-mode operation. Resolve with universal | |
4583 | -- type, in case preference rule must be applied. | |
4584 | ||
4585 | Resolve (N, Universal_Integer); | |
4586 | ||
4587 | elsif Etype (N) = T | |
4588 | and then B_Typ /= Universal_Fixed | |
4589 | then | |
a77842bd | 4590 | -- Not a mixed-mode operation, resolve with context |
996ae0b0 RK |
4591 | |
4592 | Resolve (N, B_Typ); | |
4593 | ||
4594 | elsif Etype (N) = Any_Fixed then | |
4595 | ||
a77842bd | 4596 | -- N may itself be a mixed-mode operation, so use context type |
996ae0b0 RK |
4597 | |
4598 | Resolve (N, B_Typ); | |
4599 | ||
4600 | elsif Is_Fixed_Point_Type (T) | |
4601 | and then B_Typ = Universal_Fixed | |
4602 | and then Is_Overloaded (N) | |
4603 | then | |
4604 | -- Must be (fixed * fixed) operation, operand must have one | |
4605 | -- compatible interpretation. | |
4606 | ||
4607 | Resolve (N, Any_Fixed); | |
4608 | ||
4609 | elsif Is_Fixed_Point_Type (B_Typ) | |
4610 | and then (T = Universal_Real | |
4611 | or else Is_Fixed_Point_Type (T)) | |
4612 | and then Is_Overloaded (N) | |
4613 | then | |
4614 | -- C * F(X) in a fixed context, where C is a real literal or a | |
4615 | -- fixed-point expression. F must have either a fixed type | |
4616 | -- interpretation or an integer interpretation, but not both. | |
4617 | ||
4618 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4619 | while Present (It.Typ) loop |
996ae0b0 | 4620 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) then |
996ae0b0 RK |
4621 | if Analyzed (N) then |
4622 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4623 | else | |
4624 | Resolve (N, Standard_Integer); | |
4625 | end if; | |
4626 | ||
4627 | elsif Is_Fixed_Point_Type (It.Typ) then | |
996ae0b0 RK |
4628 | if Analyzed (N) then |
4629 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4630 | else | |
4631 | Resolve (N, It.Typ); | |
4632 | end if; | |
4633 | end if; | |
4634 | ||
4635 | Get_Next_Interp (Index, It); | |
4636 | end loop; | |
4637 | ||
758c442c GD |
4638 | -- Reanalyze the literal with the fixed type of the context. If |
4639 | -- context is Universal_Fixed, we are within a conversion, leave | |
4640 | -- the literal as a universal real because there is no usable | |
4641 | -- fixed type, and the target of the conversion plays no role in | |
4642 | -- the resolution. | |
996ae0b0 | 4643 | |
0ab80019 AC |
4644 | declare |
4645 | Op2 : Node_Id; | |
4646 | T2 : Entity_Id; | |
4647 | ||
4648 | begin | |
4649 | if N = L then | |
4650 | Op2 := R; | |
4651 | else | |
4652 | Op2 := L; | |
4653 | end if; | |
4654 | ||
4655 | if B_Typ = Universal_Fixed | |
4656 | and then Nkind (Op2) = N_Real_Literal | |
4657 | then | |
4658 | T2 := Universal_Real; | |
4659 | else | |
4660 | T2 := B_Typ; | |
4661 | end if; | |
4662 | ||
4663 | Set_Analyzed (Op2, False); | |
4664 | Resolve (Op2, T2); | |
4665 | end; | |
996ae0b0 RK |
4666 | |
4667 | else | |
fbf5a39b | 4668 | Resolve (N); |
996ae0b0 RK |
4669 | end if; |
4670 | end Set_Mixed_Mode_Operand; | |
4671 | ||
4672 | ---------------------- | |
4673 | -- Set_Operand_Type -- | |
4674 | ---------------------- | |
4675 | ||
4676 | procedure Set_Operand_Type (N : Node_Id) is | |
4677 | begin | |
4678 | if Etype (N) = Universal_Integer | |
4679 | or else Etype (N) = Universal_Real | |
4680 | then | |
4681 | Set_Etype (N, T); | |
4682 | end if; | |
4683 | end Set_Operand_Type; | |
4684 | ||
996ae0b0 RK |
4685 | -- Start of processing for Resolve_Arithmetic_Op |
4686 | ||
4687 | begin | |
4688 | if Comes_From_Source (N) | |
4689 | and then Ekind (Entity (N)) = E_Function | |
4690 | and then Is_Imported (Entity (N)) | |
fbf5a39b | 4691 | and then Is_Intrinsic_Subprogram (Entity (N)) |
996ae0b0 RK |
4692 | then |
4693 | Resolve_Intrinsic_Operator (N, Typ); | |
4694 | return; | |
4695 | ||
5cc9353d RD |
4696 | -- Special-case for mixed-mode universal expressions or fixed point type |
4697 | -- operation: each argument is resolved separately. The same treatment | |
4698 | -- is required if one of the operands of a fixed point operation is | |
4699 | -- universal real, since in this case we don't do a conversion to a | |
4700 | -- specific fixed-point type (instead the expander handles the case). | |
996ae0b0 | 4701 | |
45fc7ddb | 4702 | elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real) |
996ae0b0 RK |
4703 | and then Present (Universal_Interpretation (L)) |
4704 | and then Present (Universal_Interpretation (R)) | |
4705 | then | |
4706 | Resolve (L, Universal_Interpretation (L)); | |
4707 | Resolve (R, Universal_Interpretation (R)); | |
4708 | Set_Etype (N, B_Typ); | |
4709 | ||
4710 | elsif (B_Typ = Universal_Real | |
45fc7ddb HK |
4711 | or else Etype (N) = Universal_Fixed |
4712 | or else (Etype (N) = Any_Fixed | |
4713 | and then Is_Fixed_Point_Type (B_Typ)) | |
4714 | or else (Is_Fixed_Point_Type (B_Typ) | |
4715 | and then (Is_Integer_Or_Universal (L) | |
4716 | or else | |
4717 | Is_Integer_Or_Universal (R)))) | |
4718 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) | |
996ae0b0 RK |
4719 | then |
4720 | if TL = Universal_Integer or else TR = Universal_Integer then | |
4721 | Check_For_Visible_Operator (N, B_Typ); | |
4722 | end if; | |
4723 | ||
5cc9353d RD |
4724 | -- If context is a fixed type and one operand is integer, the other |
4725 | -- is resolved with the type of the context. | |
996ae0b0 RK |
4726 | |
4727 | if Is_Fixed_Point_Type (B_Typ) | |
4728 | and then (Base_Type (TL) = Base_Type (Standard_Integer) | |
4729 | or else TL = Universal_Integer) | |
4730 | then | |
4731 | Resolve (R, B_Typ); | |
4732 | Resolve (L, TL); | |
4733 | ||
4734 | elsif Is_Fixed_Point_Type (B_Typ) | |
4735 | and then (Base_Type (TR) = Base_Type (Standard_Integer) | |
4736 | or else TR = Universal_Integer) | |
4737 | then | |
4738 | Resolve (L, B_Typ); | |
4739 | Resolve (R, TR); | |
4740 | ||
4741 | else | |
4742 | Set_Mixed_Mode_Operand (L, TR); | |
4743 | Set_Mixed_Mode_Operand (R, TL); | |
4744 | end if; | |
4745 | ||
45fc7ddb HK |
4746 | -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed |
4747 | -- multiplying operators from being used when the expected type is | |
4748 | -- also universal_fixed. Note that B_Typ will be Universal_Fixed in | |
4749 | -- some cases where the expected type is actually Any_Real; | |
4750 | -- Expected_Type_Is_Any_Real takes care of that case. | |
aa180613 | 4751 | |
996ae0b0 RK |
4752 | if Etype (N) = Universal_Fixed |
4753 | or else Etype (N) = Any_Fixed | |
4754 | then | |
4755 | if B_Typ = Universal_Fixed | |
aa180613 | 4756 | and then not Expected_Type_Is_Any_Real (N) |
45fc7ddb HK |
4757 | and then not Nkind_In (Parent (N), N_Type_Conversion, |
4758 | N_Unchecked_Type_Conversion) | |
996ae0b0 | 4759 | then |
45fc7ddb HK |
4760 | Error_Msg_N ("type cannot be determined from context!", N); |
4761 | Error_Msg_N ("\explicit conversion to result type required", N); | |
996ae0b0 RK |
4762 | |
4763 | Set_Etype (L, Any_Type); | |
4764 | Set_Etype (R, Any_Type); | |
4765 | ||
4766 | else | |
0ab80019 | 4767 | if Ada_Version = Ada_83 |
45fc7ddb HK |
4768 | and then Etype (N) = Universal_Fixed |
4769 | and then not | |
4770 | Nkind_In (Parent (N), N_Type_Conversion, | |
4771 | N_Unchecked_Type_Conversion) | |
996ae0b0 RK |
4772 | then |
4773 | Error_Msg_N | |
45fc7ddb HK |
4774 | ("(Ada 83) fixed-point operation " |
4775 | & "needs explicit conversion", N); | |
996ae0b0 RK |
4776 | end if; |
4777 | ||
aa180613 | 4778 | -- The expected type is "any real type" in contexts like |
5cc9353d | 4779 | |
aa180613 | 4780 | -- type T is delta <universal_fixed-expression> ... |
5cc9353d | 4781 | |
aa180613 RD |
4782 | -- in which case we need to set the type to Universal_Real |
4783 | -- so that static expression evaluation will work properly. | |
4784 | ||
4785 | if Expected_Type_Is_Any_Real (N) then | |
4786 | Set_Etype (N, Universal_Real); | |
4787 | else | |
4788 | Set_Etype (N, B_Typ); | |
4789 | end if; | |
996ae0b0 RK |
4790 | end if; |
4791 | ||
4792 | elsif Is_Fixed_Point_Type (B_Typ) | |
4793 | and then (Is_Integer_Or_Universal (L) | |
4794 | or else Nkind (L) = N_Real_Literal | |
4795 | or else Nkind (R) = N_Real_Literal | |
45fc7ddb | 4796 | or else Is_Integer_Or_Universal (R)) |
996ae0b0 RK |
4797 | then |
4798 | Set_Etype (N, B_Typ); | |
4799 | ||
4800 | elsif Etype (N) = Any_Fixed then | |
4801 | ||
5cc9353d RD |
4802 | -- If no previous errors, this is only possible if one operand is |
4803 | -- overloaded and the context is universal. Resolve as such. | |
996ae0b0 RK |
4804 | |
4805 | Set_Etype (N, B_Typ); | |
4806 | end if; | |
4807 | ||
4808 | else | |
4809 | if (TL = Universal_Integer or else TL = Universal_Real) | |
45fc7ddb HK |
4810 | and then |
4811 | (TR = Universal_Integer or else TR = Universal_Real) | |
996ae0b0 RK |
4812 | then |
4813 | Check_For_Visible_Operator (N, B_Typ); | |
4814 | end if; | |
4815 | ||
4816 | -- If the context is Universal_Fixed and the operands are also | |
4817 | -- universal fixed, this is an error, unless there is only one | |
841dd0f5 | 4818 | -- applicable fixed_point type (usually Duration). |
996ae0b0 | 4819 | |
45fc7ddb | 4820 | if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then |
996ae0b0 RK |
4821 | T := Unique_Fixed_Point_Type (N); |
4822 | ||
4823 | if T = Any_Type then | |
4824 | Set_Etype (N, T); | |
4825 | return; | |
4826 | else | |
4827 | Resolve (L, T); | |
4828 | Resolve (R, T); | |
4829 | end if; | |
4830 | ||
4831 | else | |
4832 | Resolve (L, B_Typ); | |
4833 | Resolve (R, B_Typ); | |
4834 | end if; | |
4835 | ||
4836 | -- If one of the arguments was resolved to a non-universal type. | |
4837 | -- label the result of the operation itself with the same type. | |
4838 | -- Do the same for the universal argument, if any. | |
4839 | ||
4840 | T := Intersect_Types (L, R); | |
4841 | Set_Etype (N, Base_Type (T)); | |
4842 | Set_Operand_Type (L); | |
4843 | Set_Operand_Type (R); | |
4844 | end if; | |
4845 | ||
fbf5a39b | 4846 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
4847 | Eval_Arithmetic_Op (N); |
4848 | ||
b0186f71 AC |
4849 | -- In SPARK and ALFA, a multiplication or division with operands of |
4850 | -- fixed point types shall be qualified or explicitly converted to | |
4851 | -- identify the result type. | |
4852 | ||
fe5d3068 YM |
4853 | if (Is_Fixed_Point_Type (Etype (L)) |
4854 | or else Is_Fixed_Point_Type (Etype (R))) | |
b0186f71 AC |
4855 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) |
4856 | and then | |
4857 | not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion) | |
4858 | then | |
fe5d3068 YM |
4859 | Check_Formal_Restriction |
4860 | ("operation should be qualified or explicitly converted", N); | |
b0186f71 AC |
4861 | end if; |
4862 | ||
996ae0b0 RK |
4863 | -- Set overflow and division checking bit. Much cleverer code needed |
4864 | -- here eventually and perhaps the Resolve routines should be separated | |
4865 | -- for the various arithmetic operations, since they will need | |
4866 | -- different processing. ??? | |
4867 | ||
4868 | if Nkind (N) in N_Op then | |
4869 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 4870 | Enable_Overflow_Check (N); |
996ae0b0 RK |
4871 | end if; |
4872 | ||
fbf5a39b AC |
4873 | -- Give warning if explicit division by zero |
4874 | ||
45fc7ddb | 4875 | if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod) |
996ae0b0 RK |
4876 | and then not Division_Checks_Suppressed (Etype (N)) |
4877 | then | |
fbf5a39b AC |
4878 | Rop := Right_Opnd (N); |
4879 | ||
4880 | if Compile_Time_Known_Value (Rop) | |
4881 | and then ((Is_Integer_Type (Etype (Rop)) | |
780d052e RD |
4882 | and then Expr_Value (Rop) = Uint_0) |
4883 | or else | |
4884 | (Is_Real_Type (Etype (Rop)) | |
4885 | and then Expr_Value_R (Rop) = Ureal_0)) | |
fbf5a39b | 4886 | then |
aa180613 RD |
4887 | -- Specialize the warning message according to the operation |
4888 | ||
4889 | case Nkind (N) is | |
4890 | when N_Op_Divide => | |
4891 | Apply_Compile_Time_Constraint_Error | |
4892 | (N, "division by zero?", CE_Divide_By_Zero, | |
4893 | Loc => Sloc (Right_Opnd (N))); | |
4894 | ||
4895 | when N_Op_Rem => | |
4896 | Apply_Compile_Time_Constraint_Error | |
4897 | (N, "rem with zero divisor?", CE_Divide_By_Zero, | |
4898 | Loc => Sloc (Right_Opnd (N))); | |
4899 | ||
4900 | when N_Op_Mod => | |
4901 | Apply_Compile_Time_Constraint_Error | |
4902 | (N, "mod with zero divisor?", CE_Divide_By_Zero, | |
4903 | Loc => Sloc (Right_Opnd (N))); | |
4904 | ||
4905 | -- Division by zero can only happen with division, rem, | |
4906 | -- and mod operations. | |
4907 | ||
4908 | when others => | |
4909 | raise Program_Error; | |
4910 | end case; | |
fbf5a39b AC |
4911 | |
4912 | -- Otherwise just set the flag to check at run time | |
4913 | ||
4914 | else | |
b7d1f17f | 4915 | Activate_Division_Check (N); |
fbf5a39b | 4916 | end if; |
996ae0b0 | 4917 | end if; |
45fc7ddb HK |
4918 | |
4919 | -- If Restriction No_Implicit_Conditionals is active, then it is | |
4920 | -- violated if either operand can be negative for mod, or for rem | |
4921 | -- if both operands can be negative. | |
4922 | ||
7a963087 | 4923 | if Restriction_Check_Required (No_Implicit_Conditionals) |
45fc7ddb HK |
4924 | and then Nkind_In (N, N_Op_Rem, N_Op_Mod) |
4925 | then | |
4926 | declare | |
4927 | Lo : Uint; | |
4928 | Hi : Uint; | |
4929 | OK : Boolean; | |
4930 | ||
4931 | LNeg : Boolean; | |
4932 | RNeg : Boolean; | |
4933 | -- Set if corresponding operand might be negative | |
4934 | ||
4935 | begin | |
5d5e9775 AC |
4936 | Determine_Range |
4937 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
4938 | LNeg := (not OK) or else Lo < 0; |
4939 | ||
5d5e9775 AC |
4940 | Determine_Range |
4941 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
4942 | RNeg := (not OK) or else Lo < 0; |
4943 | ||
5d5e9775 AC |
4944 | -- Check if we will be generating conditionals. There are two |
4945 | -- cases where that can happen, first for REM, the only case | |
4946 | -- is largest negative integer mod -1, where the division can | |
4947 | -- overflow, but we still have to give the right result. The | |
4948 | -- front end generates a test for this annoying case. Here we | |
4949 | -- just test if both operands can be negative (that's what the | |
4950 | -- expander does, so we match its logic here). | |
4951 | ||
4952 | -- The second case is mod where either operand can be negative. | |
308e6f3a | 4953 | -- In this case, the back end has to generate additional tests. |
5d5e9775 | 4954 | |
45fc7ddb HK |
4955 | if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg)) |
4956 | or else | |
4957 | (Nkind (N) = N_Op_Mod and then (LNeg or RNeg)) | |
4958 | then | |
4959 | Check_Restriction (No_Implicit_Conditionals, N); | |
4960 | end if; | |
4961 | end; | |
4962 | end if; | |
996ae0b0 RK |
4963 | end if; |
4964 | ||
4965 | Check_Unset_Reference (L); | |
4966 | Check_Unset_Reference (R); | |
996ae0b0 RK |
4967 | end Resolve_Arithmetic_Op; |
4968 | ||
4969 | ------------------ | |
4970 | -- Resolve_Call -- | |
4971 | ------------------ | |
4972 | ||
4973 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is | |
4974 | Loc : constant Source_Ptr := Sloc (N); | |
4975 | Subp : constant Node_Id := Name (N); | |
4976 | Nam : Entity_Id; | |
4977 | I : Interp_Index; | |
4978 | It : Interp; | |
4979 | Norm_OK : Boolean; | |
4980 | Scop : Entity_Id; | |
aa180613 | 4981 | Rtype : Entity_Id; |
996ae0b0 | 4982 | |
ee81cbe9 AC |
4983 | function Same_Or_Aliased_Subprograms |
4984 | (S : Entity_Id; | |
4985 | E : Entity_Id) return Boolean; | |
4986 | -- Returns True if the subprogram entity S is the same as E or else | |
4987 | -- S is an alias of E. | |
4988 | ||
001c7783 AC |
4989 | --------------------------------- |
4990 | -- Same_Or_Aliased_Subprograms -- | |
4991 | --------------------------------- | |
4992 | ||
ee81cbe9 AC |
4993 | function Same_Or_Aliased_Subprograms |
4994 | (S : Entity_Id; | |
4995 | E : Entity_Id) return Boolean | |
4996 | is | |
4997 | Subp_Alias : constant Entity_Id := Alias (S); | |
ee81cbe9 AC |
4998 | begin |
4999 | return S = E | |
5000 | or else (Present (Subp_Alias) and then Subp_Alias = E); | |
5001 | end Same_Or_Aliased_Subprograms; | |
5002 | ||
5003 | -- Start of processing for Resolve_Call | |
5004 | ||
996ae0b0 | 5005 | begin |
758c442c GD |
5006 | -- The context imposes a unique interpretation with type Typ on a |
5007 | -- procedure or function call. Find the entity of the subprogram that | |
5008 | -- yields the expected type, and propagate the corresponding formal | |
5009 | -- constraints on the actuals. The caller has established that an | |
5010 | -- interpretation exists, and emitted an error if not unique. | |
996ae0b0 RK |
5011 | |
5012 | -- First deal with the case of a call to an access-to-subprogram, | |
5013 | -- dereference made explicit in Analyze_Call. | |
5014 | ||
5015 | if Ekind (Etype (Subp)) = E_Subprogram_Type then | |
996ae0b0 RK |
5016 | if not Is_Overloaded (Subp) then |
5017 | Nam := Etype (Subp); | |
5018 | ||
5019 | else | |
758c442c GD |
5020 | -- Find the interpretation whose type (a subprogram type) has a |
5021 | -- return type that is compatible with the context. Analysis of | |
5022 | -- the node has established that one exists. | |
996ae0b0 | 5023 | |
996ae0b0 RK |
5024 | Nam := Empty; |
5025 | ||
1420b484 | 5026 | Get_First_Interp (Subp, I, It); |
996ae0b0 | 5027 | while Present (It.Typ) loop |
996ae0b0 RK |
5028 | if Covers (Typ, Etype (It.Typ)) then |
5029 | Nam := It.Typ; | |
5030 | exit; | |
5031 | end if; | |
5032 | ||
5033 | Get_Next_Interp (I, It); | |
5034 | end loop; | |
5035 | ||
5036 | if No (Nam) then | |
5037 | raise Program_Error; | |
5038 | end if; | |
5039 | end if; | |
5040 | ||
5041 | -- If the prefix is not an entity, then resolve it | |
5042 | ||
5043 | if not Is_Entity_Name (Subp) then | |
5044 | Resolve (Subp, Nam); | |
5045 | end if; | |
5046 | ||
758c442c GD |
5047 | -- For an indirect call, we always invalidate checks, since we do not |
5048 | -- know whether the subprogram is local or global. Yes we could do | |
5049 | -- better here, e.g. by knowing that there are no local subprograms, | |
aa180613 | 5050 | -- but it does not seem worth the effort. Similarly, we kill all |
758c442c | 5051 | -- knowledge of current constant values. |
fbf5a39b AC |
5052 | |
5053 | Kill_Current_Values; | |
5054 | ||
b7d1f17f HK |
5055 | -- If this is a procedure call which is really an entry call, do |
5056 | -- the conversion of the procedure call to an entry call. Protected | |
5057 | -- operations use the same circuitry because the name in the call | |
5058 | -- can be an arbitrary expression with special resolution rules. | |
996ae0b0 | 5059 | |
45fc7ddb | 5060 | elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component) |
996ae0b0 RK |
5061 | or else (Is_Entity_Name (Subp) |
5062 | and then Ekind (Entity (Subp)) = E_Entry) | |
5063 | then | |
5064 | Resolve_Entry_Call (N, Typ); | |
5065 | Check_Elab_Call (N); | |
fbf5a39b AC |
5066 | |
5067 | -- Kill checks and constant values, as above for indirect case | |
5068 | -- Who knows what happens when another task is activated? | |
5069 | ||
5070 | Kill_Current_Values; | |
996ae0b0 RK |
5071 | return; |
5072 | ||
5073 | -- Normal subprogram call with name established in Resolve | |
5074 | ||
5075 | elsif not (Is_Type (Entity (Subp))) then | |
5076 | Nam := Entity (Subp); | |
5077 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
5078 | |
5079 | -- Otherwise we must have the case of an overloaded call | |
5080 | ||
5081 | else | |
5082 | pragma Assert (Is_Overloaded (Subp)); | |
d81b4bfe TQ |
5083 | |
5084 | -- Initialize Nam to prevent warning (we know it will be assigned | |
5085 | -- in the loop below, but the compiler does not know that). | |
5086 | ||
5087 | Nam := Empty; | |
996ae0b0 RK |
5088 | |
5089 | Get_First_Interp (Subp, I, It); | |
996ae0b0 RK |
5090 | while Present (It.Typ) loop |
5091 | if Covers (Typ, It.Typ) then | |
5092 | Nam := It.Nam; | |
5093 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
5094 | exit; |
5095 | end if; | |
5096 | ||
5097 | Get_Next_Interp (I, It); | |
5098 | end loop; | |
5099 | end if; | |
5100 | ||
c9b99571 ES |
5101 | if Is_Access_Subprogram_Type (Base_Type (Etype (Nam))) |
5102 | and then not Is_Access_Subprogram_Type (Base_Type (Typ)) | |
53cf4600 ES |
5103 | and then Nkind (Subp) /= N_Explicit_Dereference |
5104 | and then Present (Parameter_Associations (N)) | |
5105 | then | |
66aa7643 TQ |
5106 | -- The prefix is a parameterless function call that returns an access |
5107 | -- to subprogram. If parameters are present in the current call, add | |
5108 | -- add an explicit dereference. We use the base type here because | |
5109 | -- within an instance these may be subtypes. | |
53cf4600 ES |
5110 | |
5111 | -- The dereference is added either in Analyze_Call or here. Should | |
5112 | -- be consolidated ??? | |
5113 | ||
5114 | Set_Is_Overloaded (Subp, False); | |
5115 | Set_Etype (Subp, Etype (Nam)); | |
5116 | Insert_Explicit_Dereference (Subp); | |
5117 | Nam := Designated_Type (Etype (Nam)); | |
5118 | Resolve (Subp, Nam); | |
5119 | end if; | |
5120 | ||
996ae0b0 RK |
5121 | -- Check that a call to Current_Task does not occur in an entry body |
5122 | ||
5123 | if Is_RTE (Nam, RE_Current_Task) then | |
5124 | declare | |
5125 | P : Node_Id; | |
5126 | ||
5127 | begin | |
5128 | P := N; | |
5129 | loop | |
5130 | P := Parent (P); | |
45fc7ddb HK |
5131 | |
5132 | -- Exclude calls that occur within the default of a formal | |
5133 | -- parameter of the entry, since those are evaluated outside | |
5134 | -- of the body. | |
5135 | ||
5136 | exit when No (P) or else Nkind (P) = N_Parameter_Specification; | |
996ae0b0 | 5137 | |
aa180613 RD |
5138 | if Nkind (P) = N_Entry_Body |
5139 | or else (Nkind (P) = N_Subprogram_Body | |
45fc7ddb | 5140 | and then Is_Entry_Barrier_Function (P)) |
aa180613 RD |
5141 | then |
5142 | Rtype := Etype (N); | |
996ae0b0 | 5143 | Error_Msg_NE |
aa5147f0 | 5144 | ("?& should not be used in entry body (RM C.7(17))", |
996ae0b0 | 5145 | N, Nam); |
aa180613 RD |
5146 | Error_Msg_NE |
5147 | ("\Program_Error will be raised at run time?", N, Nam); | |
5148 | Rewrite (N, | |
5149 | Make_Raise_Program_Error (Loc, | |
5150 | Reason => PE_Current_Task_In_Entry_Body)); | |
5151 | Set_Etype (N, Rtype); | |
e65f50ec | 5152 | return; |
996ae0b0 RK |
5153 | end if; |
5154 | end loop; | |
5155 | end; | |
5156 | end if; | |
5157 | ||
758c442c GD |
5158 | -- Check that a procedure call does not occur in the context of the |
5159 | -- entry call statement of a conditional or timed entry call. Note that | |
5160 | -- the case of a call to a subprogram renaming of an entry will also be | |
5161 | -- rejected. The test for N not being an N_Entry_Call_Statement is | |
5162 | -- defensive, covering the possibility that the processing of entry | |
5163 | -- calls might reach this point due to later modifications of the code | |
5164 | -- above. | |
996ae0b0 RK |
5165 | |
5166 | if Nkind (Parent (N)) = N_Entry_Call_Alternative | |
5167 | and then Nkind (N) /= N_Entry_Call_Statement | |
5168 | and then Entry_Call_Statement (Parent (N)) = N | |
5169 | then | |
0791fbe9 | 5170 | if Ada_Version < Ada_2005 then |
1420b484 JM |
5171 | Error_Msg_N ("entry call required in select statement", N); |
5172 | ||
5173 | -- Ada 2005 (AI-345): If a procedure_call_statement is used | |
66aa7643 TQ |
5174 | -- for a procedure_or_entry_call, the procedure_name or |
5175 | -- procedure_prefix of the procedure_call_statement shall denote | |
1420b484 JM |
5176 | -- an entry renamed by a procedure, or (a view of) a primitive |
5177 | -- subprogram of a limited interface whose first parameter is | |
5178 | -- a controlling parameter. | |
5179 | ||
5180 | elsif Nkind (N) = N_Procedure_Call_Statement | |
5181 | and then not Is_Renamed_Entry (Nam) | |
5182 | and then not Is_Controlling_Limited_Procedure (Nam) | |
5183 | then | |
5184 | Error_Msg_N | |
c8ef728f | 5185 | ("entry call or dispatching primitive of interface required", N); |
1420b484 | 5186 | end if; |
996ae0b0 RK |
5187 | end if; |
5188 | ||
66aa7643 TQ |
5189 | -- Check that this is not a call to a protected procedure or entry from |
5190 | -- within a protected function. | |
fbf5a39b AC |
5191 | |
5192 | if Ekind (Current_Scope) = E_Function | |
5193 | and then Ekind (Scope (Current_Scope)) = E_Protected_Type | |
5194 | and then Ekind (Nam) /= E_Function | |
5195 | and then Scope (Nam) = Scope (Current_Scope) | |
5196 | then | |
5197 | Error_Msg_N ("within protected function, protected " & | |
5198 | "object is constant", N); | |
5199 | Error_Msg_N ("\cannot call operation that may modify it", N); | |
5200 | end if; | |
5201 | ||
45fc7ddb | 5202 | -- Freeze the subprogram name if not in a spec-expression. Note that we |
758c442c GD |
5203 | -- freeze procedure calls as well as function calls. Procedure calls are |
5204 | -- not frozen according to the rules (RM 13.14(14)) because it is | |
5205 | -- impossible to have a procedure call to a non-frozen procedure in pure | |
5206 | -- Ada, but in the code that we generate in the expander, this rule | |
5207 | -- needs extending because we can generate procedure calls that need | |
5208 | -- freezing. | |
996ae0b0 | 5209 | |
45fc7ddb | 5210 | if Is_Entity_Name (Subp) and then not In_Spec_Expression then |
996ae0b0 RK |
5211 | Freeze_Expression (Subp); |
5212 | end if; | |
5213 | ||
758c442c GD |
5214 | -- For a predefined operator, the type of the result is the type imposed |
5215 | -- by context, except for a predefined operation on universal fixed. | |
5216 | -- Otherwise The type of the call is the type returned by the subprogram | |
5217 | -- being called. | |
996ae0b0 RK |
5218 | |
5219 | if Is_Predefined_Op (Nam) then | |
996ae0b0 RK |
5220 | if Etype (N) /= Universal_Fixed then |
5221 | Set_Etype (N, Typ); | |
5222 | end if; | |
5223 | ||
758c442c GD |
5224 | -- If the subprogram returns an array type, and the context requires the |
5225 | -- component type of that array type, the node is really an indexing of | |
5226 | -- the parameterless call. Resolve as such. A pathological case occurs | |
5227 | -- when the type of the component is an access to the array type. In | |
5228 | -- this case the call is truly ambiguous. | |
996ae0b0 | 5229 | |
0669bebe | 5230 | elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam)) |
996ae0b0 RK |
5231 | and then |
5232 | ((Is_Array_Type (Etype (Nam)) | |
19fb051c | 5233 | and then Covers (Typ, Component_Type (Etype (Nam)))) |
996ae0b0 | 5234 | or else (Is_Access_Type (Etype (Nam)) |
19fb051c AC |
5235 | and then Is_Array_Type (Designated_Type (Etype (Nam))) |
5236 | and then | |
5237 | Covers | |
5238 | (Typ, | |
5239 | Component_Type (Designated_Type (Etype (Nam)))))) | |
996ae0b0 RK |
5240 | then |
5241 | declare | |
5242 | Index_Node : Node_Id; | |
fbf5a39b AC |
5243 | New_Subp : Node_Id; |
5244 | Ret_Type : constant Entity_Id := Etype (Nam); | |
996ae0b0 RK |
5245 | |
5246 | begin | |
fbf5a39b AC |
5247 | if Is_Access_Type (Ret_Type) |
5248 | and then Ret_Type = Component_Type (Designated_Type (Ret_Type)) | |
5249 | then | |
5250 | Error_Msg_N | |
5251 | ("cannot disambiguate function call and indexing", N); | |
5252 | else | |
5253 | New_Subp := Relocate_Node (Subp); | |
5254 | Set_Entity (Subp, Nam); | |
5255 | ||
7205254b | 5256 | if (Is_Array_Type (Ret_Type) |
5d5e9775 | 5257 | and then Component_Type (Ret_Type) /= Any_Type) |
7205254b JM |
5258 | or else |
5259 | (Is_Access_Type (Ret_Type) | |
5d5e9775 AC |
5260 | and then |
5261 | Component_Type (Designated_Type (Ret_Type)) /= Any_Type) | |
7205254b | 5262 | then |
0669bebe GB |
5263 | if Needs_No_Actuals (Nam) then |
5264 | ||
5265 | -- Indexed call to a parameterless function | |
5266 | ||
5267 | Index_Node := | |
5268 | Make_Indexed_Component (Loc, | |
5269 | Prefix => | |
5270 | Make_Function_Call (Loc, | |
5271 | Name => New_Subp), | |
5272 | Expressions => Parameter_Associations (N)); | |
5273 | else | |
5274 | -- An Ada 2005 prefixed call to a primitive operation | |
5275 | -- whose first parameter is the prefix. This prefix was | |
5276 | -- prepended to the parameter list, which is actually a | |
3b42c566 | 5277 | -- list of indexes. Remove the prefix in order to build |
0669bebe GB |
5278 | -- the proper indexed component. |
5279 | ||
5280 | Index_Node := | |
5281 | Make_Indexed_Component (Loc, | |
5282 | Prefix => | |
5283 | Make_Function_Call (Loc, | |
5284 | Name => New_Subp, | |
5285 | Parameter_Associations => | |
5286 | New_List | |
5287 | (Remove_Head (Parameter_Associations (N)))), | |
5288 | Expressions => Parameter_Associations (N)); | |
5289 | end if; | |
fbf5a39b | 5290 | |
74e7891f RD |
5291 | -- Preserve the parenthesis count of the node |
5292 | ||
5293 | Set_Paren_Count (Index_Node, Paren_Count (N)); | |
5294 | ||
fbf5a39b AC |
5295 | -- Since we are correcting a node classification error made |
5296 | -- by the parser, we call Replace rather than Rewrite. | |
5297 | ||
5298 | Replace (N, Index_Node); | |
74e7891f | 5299 | |
fbf5a39b AC |
5300 | Set_Etype (Prefix (N), Ret_Type); |
5301 | Set_Etype (N, Typ); | |
5302 | Resolve_Indexed_Component (N, Typ); | |
5303 | Check_Elab_Call (Prefix (N)); | |
5304 | end if; | |
996ae0b0 RK |
5305 | end if; |
5306 | ||
5307 | return; | |
5308 | end; | |
5309 | ||
5310 | else | |
5311 | Set_Etype (N, Etype (Nam)); | |
5312 | end if; | |
5313 | ||
5314 | -- In the case where the call is to an overloaded subprogram, Analyze | |
5315 | -- calls Normalize_Actuals once per overloaded subprogram. Therefore in | |
5316 | -- such a case Normalize_Actuals needs to be called once more to order | |
5317 | -- the actuals correctly. Otherwise the call will have the ordering | |
5318 | -- given by the last overloaded subprogram whether this is the correct | |
5319 | -- one being called or not. | |
5320 | ||
5321 | if Is_Overloaded (Subp) then | |
5322 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
5323 | pragma Assert (Norm_OK); | |
5324 | end if; | |
5325 | ||
5326 | -- In any case, call is fully resolved now. Reset Overload flag, to | |
5327 | -- prevent subsequent overload resolution if node is analyzed again | |
5328 | ||
5329 | Set_Is_Overloaded (Subp, False); | |
5330 | Set_Is_Overloaded (N, False); | |
5331 | ||
758c442c GD |
5332 | -- If we are calling the current subprogram from immediately within its |
5333 | -- body, then that is the case where we can sometimes detect cases of | |
5334 | -- infinite recursion statically. Do not try this in case restriction | |
b7d1f17f | 5335 | -- No_Recursion is in effect anyway, and do it only for source calls. |
996ae0b0 | 5336 | |
b7d1f17f HK |
5337 | if Comes_From_Source (N) then |
5338 | Scop := Current_Scope; | |
996ae0b0 | 5339 | |
26570b21 RD |
5340 | -- Issue warning for possible infinite recursion in the absence |
5341 | -- of the No_Recursion restriction. | |
5342 | ||
ee81cbe9 | 5343 | if Same_Or_Aliased_Subprograms (Nam, Scop) |
b7d1f17f HK |
5344 | and then not Restriction_Active (No_Recursion) |
5345 | and then Check_Infinite_Recursion (N) | |
5346 | then | |
5347 | -- Here we detected and flagged an infinite recursion, so we do | |
da20aa43 RD |
5348 | -- not need to test the case below for further warnings. Also we |
5349 | -- are all done if we now have a raise SE node. | |
996ae0b0 | 5350 | |
26570b21 RD |
5351 | if Nkind (N) = N_Raise_Storage_Error then |
5352 | return; | |
5353 | end if; | |
996ae0b0 | 5354 | |
26570b21 RD |
5355 | -- If call is to immediately containing subprogram, then check for |
5356 | -- the case of a possible run-time detectable infinite recursion. | |
996ae0b0 | 5357 | |
b7d1f17f HK |
5358 | else |
5359 | Scope_Loop : while Scop /= Standard_Standard loop | |
ee81cbe9 | 5360 | if Same_Or_Aliased_Subprograms (Nam, Scop) then |
b7d1f17f HK |
5361 | |
5362 | -- Although in general case, recursion is not statically | |
5363 | -- checkable, the case of calling an immediately containing | |
5364 | -- subprogram is easy to catch. | |
5365 | ||
5366 | Check_Restriction (No_Recursion, N); | |
5367 | ||
5368 | -- If the recursive call is to a parameterless subprogram, | |
5369 | -- then even if we can't statically detect infinite | |
5370 | -- recursion, this is pretty suspicious, and we output a | |
5371 | -- warning. Furthermore, we will try later to detect some | |
5372 | -- cases here at run time by expanding checking code (see | |
5373 | -- Detect_Infinite_Recursion in package Exp_Ch6). | |
5374 | ||
5375 | -- If the recursive call is within a handler, do not emit a | |
5376 | -- warning, because this is a common idiom: loop until input | |
5377 | -- is correct, catch illegal input in handler and restart. | |
5378 | ||
5379 | if No (First_Formal (Nam)) | |
5380 | and then Etype (Nam) = Standard_Void_Type | |
5381 | and then not Error_Posted (N) | |
5382 | and then Nkind (Parent (N)) /= N_Exception_Handler | |
aa180613 | 5383 | then |
b7d1f17f HK |
5384 | -- For the case of a procedure call. We give the message |
5385 | -- only if the call is the first statement in a sequence | |
5386 | -- of statements, or if all previous statements are | |
5387 | -- simple assignments. This is simply a heuristic to | |
5388 | -- decrease false positives, without losing too many good | |
5389 | -- warnings. The idea is that these previous statements | |
5390 | -- may affect global variables the procedure depends on. | |
5391 | ||
5392 | if Nkind (N) = N_Procedure_Call_Statement | |
5393 | and then Is_List_Member (N) | |
5394 | then | |
5395 | declare | |
5396 | P : Node_Id; | |
5397 | begin | |
5398 | P := Prev (N); | |
5399 | while Present (P) loop | |
5400 | if Nkind (P) /= N_Assignment_Statement then | |
5401 | exit Scope_Loop; | |
5402 | end if; | |
5403 | ||
5404 | Prev (P); | |
5405 | end loop; | |
5406 | end; | |
5407 | end if; | |
5408 | ||
5409 | -- Do not give warning if we are in a conditional context | |
5410 | ||
aa180613 | 5411 | declare |
b7d1f17f | 5412 | K : constant Node_Kind := Nkind (Parent (N)); |
aa180613 | 5413 | begin |
b7d1f17f | 5414 | if (K = N_Loop_Statement |
b5c739f9 | 5415 | and then Present (Iteration_Scheme (Parent (N)))) |
b7d1f17f HK |
5416 | or else K = N_If_Statement |
5417 | or else K = N_Elsif_Part | |
5418 | or else K = N_Case_Statement_Alternative | |
5419 | then | |
5420 | exit Scope_Loop; | |
5421 | end if; | |
aa180613 | 5422 | end; |
aa180613 | 5423 | |
b7d1f17f | 5424 | -- Here warning is to be issued |
aa180613 | 5425 | |
b7d1f17f HK |
5426 | Set_Has_Recursive_Call (Nam); |
5427 | Error_Msg_N | |
aa5147f0 | 5428 | ("?possible infinite recursion!", N); |
b7d1f17f | 5429 | Error_Msg_N |
aa5147f0 | 5430 | ("\?Storage_Error may be raised at run time!", N); |
b7d1f17f | 5431 | end if; |
aa180613 | 5432 | |
b7d1f17f | 5433 | exit Scope_Loop; |
996ae0b0 RK |
5434 | end if; |
5435 | ||
b7d1f17f HK |
5436 | Scop := Scope (Scop); |
5437 | end loop Scope_Loop; | |
5438 | end if; | |
996ae0b0 RK |
5439 | end if; |
5440 | ||
b5c739f9 RD |
5441 | -- Check obsolescent reference to Ada.Characters.Handling subprogram |
5442 | ||
5443 | Check_Obsolescent_2005_Entity (Nam, Subp); | |
5444 | ||
996ae0b0 RK |
5445 | -- If subprogram name is a predefined operator, it was given in |
5446 | -- functional notation. Replace call node with operator node, so | |
5447 | -- that actuals can be resolved appropriately. | |
5448 | ||
5449 | if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then | |
5450 | Make_Call_Into_Operator (N, Typ, Entity (Name (N))); | |
5451 | return; | |
5452 | ||
5453 | elsif Present (Alias (Nam)) | |
5454 | and then Is_Predefined_Op (Alias (Nam)) | |
5455 | then | |
5456 | Resolve_Actuals (N, Nam); | |
5457 | Make_Call_Into_Operator (N, Typ, Alias (Nam)); | |
5458 | return; | |
5459 | end if; | |
5460 | ||
fbf5a39b AC |
5461 | -- Create a transient scope if the resulting type requires it |
5462 | ||
4017021b AC |
5463 | -- There are several notable exceptions: |
5464 | ||
4d2907fd | 5465 | -- a) In init procs, the transient scope overhead is not needed, and is |
4017021b AC |
5466 | -- even incorrect when the call is a nested initialization call for a |
5467 | -- component whose expansion may generate adjust calls. However, if the | |
5468 | -- call is some other procedure call within an initialization procedure | |
5469 | -- (for example a call to Create_Task in the init_proc of the task | |
5470 | -- run-time record) a transient scope must be created around this call. | |
5471 | ||
4d2907fd | 5472 | -- b) Enumeration literal pseudo-calls need no transient scope |
4017021b | 5473 | |
4d2907fd | 5474 | -- c) Intrinsic subprograms (Unchecked_Conversion and source info |
4017021b | 5475 | -- functions) do not use the secondary stack even though the return |
4d2907fd | 5476 | -- type may be unconstrained. |
4017021b | 5477 | |
4d2907fd | 5478 | -- d) Calls to a build-in-place function, since such functions may |
4017021b AC |
5479 | -- allocate their result directly in a target object, and cases where |
5480 | -- the result does get allocated in the secondary stack are checked for | |
5481 | -- within the specialized Exp_Ch6 procedures for expanding those | |
5482 | -- build-in-place calls. | |
5483 | ||
5484 | -- e) If the subprogram is marked Inline_Always, then even if it returns | |
c8ef728f | 5485 | -- an unconstrained type the call does not require use of the secondary |
45fc7ddb HK |
5486 | -- stack. However, inlining will only take place if the body to inline |
5487 | -- is already present. It may not be available if e.g. the subprogram is | |
5488 | -- declared in a child instance. | |
c8ef728f | 5489 | |
4017021b AC |
5490 | -- If this is an initialization call for a type whose construction |
5491 | -- uses the secondary stack, and it is not a nested call to initialize | |
5492 | -- a component, we do need to create a transient scope for it. We | |
5493 | -- check for this by traversing the type in Check_Initialization_Call. | |
5494 | ||
c8ef728f | 5495 | if Is_Inlined (Nam) |
45fc7ddb HK |
5496 | and then Has_Pragma_Inline_Always (Nam) |
5497 | and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration | |
5498 | and then Present (Body_To_Inline (Unit_Declaration_Node (Nam))) | |
c8ef728f ES |
5499 | then |
5500 | null; | |
5501 | ||
4017021b AC |
5502 | elsif Ekind (Nam) = E_Enumeration_Literal |
5503 | or else Is_Build_In_Place_Function (Nam) | |
5504 | or else Is_Intrinsic_Subprogram (Nam) | |
5505 | then | |
5506 | null; | |
5507 | ||
c8ef728f | 5508 | elsif Expander_Active |
996ae0b0 RK |
5509 | and then Is_Type (Etype (Nam)) |
5510 | and then Requires_Transient_Scope (Etype (Nam)) | |
4017021b AC |
5511 | and then |
5512 | (not Within_Init_Proc | |
5513 | or else | |
5514 | (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function)) | |
996ae0b0 | 5515 | then |
0669bebe | 5516 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 5517 | |
a9f4e3d2 AC |
5518 | -- If the call appears within the bounds of a loop, it will |
5519 | -- be rewritten and reanalyzed, nothing left to do here. | |
5520 | ||
5521 | if Nkind (N) /= N_Function_Call then | |
5522 | return; | |
5523 | end if; | |
5524 | ||
fbf5a39b | 5525 | elsif Is_Init_Proc (Nam) |
996ae0b0 RK |
5526 | and then not Within_Init_Proc |
5527 | then | |
5528 | Check_Initialization_Call (N, Nam); | |
5529 | end if; | |
5530 | ||
5531 | -- A protected function cannot be called within the definition of the | |
5532 | -- enclosing protected type. | |
5533 | ||
5534 | if Is_Protected_Type (Scope (Nam)) | |
5535 | and then In_Open_Scopes (Scope (Nam)) | |
5536 | and then not Has_Completion (Scope (Nam)) | |
5537 | then | |
5538 | Error_Msg_NE | |
5539 | ("& cannot be called before end of protected definition", N, Nam); | |
5540 | end if; | |
5541 | ||
5542 | -- Propagate interpretation to actuals, and add default expressions | |
5543 | -- where needed. | |
5544 | ||
5545 | if Present (First_Formal (Nam)) then | |
5546 | Resolve_Actuals (N, Nam); | |
5547 | ||
d81b4bfe TQ |
5548 | -- Overloaded literals are rewritten as function calls, for purpose of |
5549 | -- resolution. After resolution, we can replace the call with the | |
5550 | -- literal itself. | |
996ae0b0 RK |
5551 | |
5552 | elsif Ekind (Nam) = E_Enumeration_Literal then | |
5553 | Copy_Node (Subp, N); | |
5554 | Resolve_Entity_Name (N, Typ); | |
5555 | ||
fbf5a39b | 5556 | -- Avoid validation, since it is a static function call |
996ae0b0 | 5557 | |
e65f50ec | 5558 | Generate_Reference (Nam, Subp); |
996ae0b0 RK |
5559 | return; |
5560 | end if; | |
5561 | ||
b7d1f17f HK |
5562 | -- If the subprogram is not global, then kill all saved values and |
5563 | -- checks. This is a bit conservative, since in many cases we could do | |
5564 | -- better, but it is not worth the effort. Similarly, we kill constant | |
5565 | -- values. However we do not need to do this for internal entities | |
5566 | -- (unless they are inherited user-defined subprograms), since they | |
5567 | -- are not in the business of molesting local values. | |
5568 | ||
5569 | -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also | |
5570 | -- kill all checks and values for calls to global subprograms. This | |
5571 | -- takes care of the case where an access to a local subprogram is | |
5572 | -- taken, and could be passed directly or indirectly and then called | |
5573 | -- from almost any context. | |
aa180613 RD |
5574 | |
5575 | -- Note: we do not do this step till after resolving the actuals. That | |
5576 | -- way we still take advantage of the current value information while | |
5577 | -- scanning the actuals. | |
5578 | ||
45fc7ddb HK |
5579 | -- We suppress killing values if we are processing the nodes associated |
5580 | -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged | |
5581 | -- type kills all the values as part of analyzing the code that | |
5582 | -- initializes the dispatch tables. | |
5583 | ||
5584 | if Inside_Freezing_Actions = 0 | |
5585 | and then (not Is_Library_Level_Entity (Nam) | |
24357840 RD |
5586 | or else Suppress_Value_Tracking_On_Call |
5587 | (Nearest_Dynamic_Scope (Current_Scope))) | |
aa180613 RD |
5588 | and then (Comes_From_Source (Nam) |
5589 | or else (Present (Alias (Nam)) | |
5590 | and then Comes_From_Source (Alias (Nam)))) | |
5591 | then | |
5592 | Kill_Current_Values; | |
5593 | end if; | |
5594 | ||
36fcf362 RD |
5595 | -- If we are warning about unread OUT parameters, this is the place to |
5596 | -- set Last_Assignment for OUT and IN OUT parameters. We have to do this | |
5597 | -- after the above call to Kill_Current_Values (since that call clears | |
5598 | -- the Last_Assignment field of all local variables). | |
67ce0d7e | 5599 | |
36fcf362 | 5600 | if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters) |
67ce0d7e RD |
5601 | and then Comes_From_Source (N) |
5602 | and then In_Extended_Main_Source_Unit (N) | |
5603 | then | |
5604 | declare | |
5605 | F : Entity_Id; | |
5606 | A : Node_Id; | |
5607 | ||
5608 | begin | |
5609 | F := First_Formal (Nam); | |
5610 | A := First_Actual (N); | |
5611 | while Present (F) and then Present (A) loop | |
964f13da | 5612 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) |
36fcf362 | 5613 | and then Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
5614 | and then Is_Entity_Name (A) |
5615 | and then Present (Entity (A)) | |
36fcf362 | 5616 | and then Comes_From_Source (N) |
67ce0d7e RD |
5617 | and then Safe_To_Capture_Value (N, Entity (A)) |
5618 | then | |
5619 | Set_Last_Assignment (Entity (A), A); | |
5620 | end if; | |
5621 | ||
5622 | Next_Formal (F); | |
5623 | Next_Actual (A); | |
5624 | end loop; | |
5625 | end; | |
5626 | end if; | |
5627 | ||
996ae0b0 RK |
5628 | -- If the subprogram is a primitive operation, check whether or not |
5629 | -- it is a correct dispatching call. | |
5630 | ||
5631 | if Is_Overloadable (Nam) | |
5632 | and then Is_Dispatching_Operation (Nam) | |
5633 | then | |
5634 | Check_Dispatching_Call (N); | |
5635 | ||
0669bebe GB |
5636 | elsif Ekind (Nam) /= E_Subprogram_Type |
5637 | and then Is_Abstract_Subprogram (Nam) | |
996ae0b0 RK |
5638 | and then not In_Instance |
5639 | then | |
5640 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam); | |
5641 | end if; | |
5642 | ||
e65f50ec ES |
5643 | -- If this is a dispatching call, generate the appropriate reference, |
5644 | -- for better source navigation in GPS. | |
5645 | ||
5646 | if Is_Overloadable (Nam) | |
5647 | and then Present (Controlling_Argument (N)) | |
5648 | then | |
5649 | Generate_Reference (Nam, Subp, 'R'); | |
c5d91669 | 5650 | |
5cc9353d | 5651 | -- Normal case, not a dispatching call: generate a call reference |
c5d91669 | 5652 | |
e65f50ec | 5653 | else |
9c870c90 | 5654 | Generate_Reference (Nam, Subp, 's'); |
e65f50ec ES |
5655 | end if; |
5656 | ||
996ae0b0 RK |
5657 | if Is_Intrinsic_Subprogram (Nam) then |
5658 | Check_Intrinsic_Call (N); | |
5659 | end if; | |
5660 | ||
5b2217f8 | 5661 | -- Check for violation of restriction No_Specific_Termination_Handlers |
dce86910 | 5662 | -- and warn on a potentially blocking call to Abort_Task. |
5b2217f8 RD |
5663 | |
5664 | if Is_RTE (Nam, RE_Set_Specific_Handler) | |
5665 | or else | |
5666 | Is_RTE (Nam, RE_Specific_Handler) | |
5667 | then | |
5668 | Check_Restriction (No_Specific_Termination_Handlers, N); | |
dce86910 AC |
5669 | |
5670 | elsif Is_RTE (Nam, RE_Abort_Task) then | |
5671 | Check_Potentially_Blocking_Operation (N); | |
5b2217f8 RD |
5672 | end if; |
5673 | ||
806f6d37 AC |
5674 | -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative |
5675 | -- timing event violates restriction No_Relative_Delay (AI-0211). We | |
5676 | -- need to check the second argument to determine whether it is an | |
5677 | -- absolute or relative timing event. | |
afbcdf5e | 5678 | |
806f6d37 AC |
5679 | if Is_RTE (Nam, RE_Set_Handler) |
5680 | and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span) | |
5681 | then | |
afbcdf5e AC |
5682 | Check_Restriction (No_Relative_Delay, N); |
5683 | end if; | |
5684 | ||
9cbfc269 AC |
5685 | -- Issue an error for a call to an eliminated subprogram. We skip this |
5686 | -- in a spec expression, e.g. a call in a default parameter value, since | |
5687 | -- we are not really doing a call at this time. That's important because | |
5688 | -- the spec expression may itself belong to an eliminated subprogram. | |
16212e89 | 5689 | |
9cbfc269 AC |
5690 | if not In_Spec_Expression then |
5691 | Check_For_Eliminated_Subprogram (Subp, Nam); | |
5692 | end if; | |
16212e89 | 5693 | |
67ce0d7e RD |
5694 | -- All done, evaluate call and deal with elaboration issues |
5695 | ||
c01a9391 | 5696 | Eval_Call (N); |
996ae0b0 | 5697 | Check_Elab_Call (N); |
76b84bf0 | 5698 | Warn_On_Overlapping_Actuals (Nam, N); |
996ae0b0 RK |
5699 | end Resolve_Call; |
5700 | ||
19d846a0 RD |
5701 | ----------------------------- |
5702 | -- Resolve_Case_Expression -- | |
5703 | ----------------------------- | |
5704 | ||
5705 | procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is | |
5706 | Alt : Node_Id; | |
5707 | ||
5708 | begin | |
5709 | Alt := First (Alternatives (N)); | |
5710 | while Present (Alt) loop | |
5711 | Resolve (Expression (Alt), Typ); | |
5712 | Next (Alt); | |
5713 | end loop; | |
5714 | ||
5715 | Set_Etype (N, Typ); | |
5716 | Eval_Case_Expression (N); | |
5717 | end Resolve_Case_Expression; | |
5718 | ||
996ae0b0 RK |
5719 | ------------------------------- |
5720 | -- Resolve_Character_Literal -- | |
5721 | ------------------------------- | |
5722 | ||
5723 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is | |
5724 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
5725 | C : Entity_Id; | |
5726 | ||
5727 | begin | |
5728 | -- Verify that the character does belong to the type of the context | |
5729 | ||
5730 | Set_Etype (N, B_Typ); | |
5731 | Eval_Character_Literal (N); | |
5732 | ||
82c80734 RD |
5733 | -- Wide_Wide_Character literals must always be defined, since the set |
5734 | -- of wide wide character literals is complete, i.e. if a character | |
5735 | -- literal is accepted by the parser, then it is OK for wide wide | |
5736 | -- character (out of range character literals are rejected). | |
996ae0b0 | 5737 | |
82c80734 | 5738 | if Root_Type (B_Typ) = Standard_Wide_Wide_Character then |
996ae0b0 RK |
5739 | return; |
5740 | ||
5741 | -- Always accept character literal for type Any_Character, which | |
5742 | -- occurs in error situations and in comparisons of literals, both | |
5743 | -- of which should accept all literals. | |
5744 | ||
5745 | elsif B_Typ = Any_Character then | |
5746 | return; | |
5747 | ||
5cc9353d RD |
5748 | -- For Standard.Character or a type derived from it, check that the |
5749 | -- literal is in range. | |
996ae0b0 RK |
5750 | |
5751 | elsif Root_Type (B_Typ) = Standard_Character then | |
82c80734 RD |
5752 | if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then |
5753 | return; | |
5754 | end if; | |
5755 | ||
5cc9353d RD |
5756 | -- For Standard.Wide_Character or a type derived from it, check that the |
5757 | -- literal is in range. | |
82c80734 RD |
5758 | |
5759 | elsif Root_Type (B_Typ) = Standard_Wide_Character then | |
5760 | if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then | |
996ae0b0 RK |
5761 | return; |
5762 | end if; | |
5763 | ||
82c80734 RD |
5764 | -- For Standard.Wide_Wide_Character or a type derived from it, we |
5765 | -- know the literal is in range, since the parser checked! | |
5766 | ||
5767 | elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then | |
5768 | return; | |
5769 | ||
d81b4bfe TQ |
5770 | -- If the entity is already set, this has already been resolved in a |
5771 | -- generic context, or comes from expansion. Nothing else to do. | |
996ae0b0 RK |
5772 | |
5773 | elsif Present (Entity (N)) then | |
5774 | return; | |
5775 | ||
d81b4bfe TQ |
5776 | -- Otherwise we have a user defined character type, and we can use the |
5777 | -- standard visibility mechanisms to locate the referenced entity. | |
996ae0b0 RK |
5778 | |
5779 | else | |
5780 | C := Current_Entity (N); | |
996ae0b0 RK |
5781 | while Present (C) loop |
5782 | if Etype (C) = B_Typ then | |
5783 | Set_Entity_With_Style_Check (N, C); | |
5784 | Generate_Reference (C, N); | |
5785 | return; | |
5786 | end if; | |
5787 | ||
5788 | C := Homonym (C); | |
5789 | end loop; | |
5790 | end if; | |
5791 | ||
5792 | -- If we fall through, then the literal does not match any of the | |
5cc9353d RD |
5793 | -- entries of the enumeration type. This isn't just a constraint error |
5794 | -- situation, it is an illegality (see RM 4.2). | |
996ae0b0 RK |
5795 | |
5796 | Error_Msg_NE | |
5797 | ("character not defined for }", N, First_Subtype (B_Typ)); | |
996ae0b0 RK |
5798 | end Resolve_Character_Literal; |
5799 | ||
5800 | --------------------------- | |
5801 | -- Resolve_Comparison_Op -- | |
5802 | --------------------------- | |
5803 | ||
5804 | -- Context requires a boolean type, and plays no role in resolution. | |
5cc9353d RD |
5805 | -- Processing identical to that for equality operators. The result type is |
5806 | -- the base type, which matters when pathological subtypes of booleans with | |
5807 | -- limited ranges are used. | |
996ae0b0 RK |
5808 | |
5809 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is | |
5810 | L : constant Node_Id := Left_Opnd (N); | |
5811 | R : constant Node_Id := Right_Opnd (N); | |
5812 | T : Entity_Id; | |
5813 | ||
5814 | begin | |
d81b4bfe TQ |
5815 | -- If this is an intrinsic operation which is not predefined, use the |
5816 | -- types of its declared arguments to resolve the possibly overloaded | |
5817 | -- operands. Otherwise the operands are unambiguous and specify the | |
5818 | -- expected type. | |
996ae0b0 RK |
5819 | |
5820 | if Scope (Entity (N)) /= Standard_Standard then | |
5821 | T := Etype (First_Entity (Entity (N))); | |
1420b484 | 5822 | |
996ae0b0 RK |
5823 | else |
5824 | T := Find_Unique_Type (L, R); | |
5825 | ||
5826 | if T = Any_Fixed then | |
5827 | T := Unique_Fixed_Point_Type (L); | |
5828 | end if; | |
5829 | end if; | |
5830 | ||
fbf5a39b | 5831 | Set_Etype (N, Base_Type (Typ)); |
996ae0b0 RK |
5832 | Generate_Reference (T, N, ' '); |
5833 | ||
bd29d519 | 5834 | -- Skip remaining processing if already set to Any_Type |
996ae0b0 | 5835 | |
bd29d519 AC |
5836 | if T = Any_Type then |
5837 | return; | |
5838 | end if; | |
5839 | ||
5840 | -- Deal with other error cases | |
996ae0b0 | 5841 | |
bd29d519 AC |
5842 | if T = Any_String or else |
5843 | T = Any_Composite or else | |
5844 | T = Any_Character | |
5845 | then | |
5846 | if T = Any_Character then | |
5847 | Ambiguous_Character (L); | |
996ae0b0 | 5848 | else |
bd29d519 | 5849 | Error_Msg_N ("ambiguous operands for comparison", N); |
996ae0b0 | 5850 | end if; |
bd29d519 AC |
5851 | |
5852 | Set_Etype (N, Any_Type); | |
5853 | return; | |
996ae0b0 | 5854 | end if; |
bd29d519 AC |
5855 | |
5856 | -- Resolve the operands if types OK | |
5857 | ||
5858 | Resolve (L, T); | |
5859 | Resolve (R, T); | |
5860 | Check_Unset_Reference (L); | |
5861 | Check_Unset_Reference (R); | |
5862 | Generate_Operator_Reference (N, T); | |
5863 | Check_Low_Bound_Tested (N); | |
5864 | ||
b0186f71 AC |
5865 | -- In SPARK or ALFA, ordering operators <, <=, >, >= are not defined |
5866 | -- for Boolean types or array types except String. | |
5867 | ||
fe5d3068 YM |
5868 | if Is_Boolean_Type (T) then |
5869 | Check_Formal_Restriction | |
5870 | ("comparison is not defined on Boolean type", N); | |
5871 | elsif Is_Array_Type (T) | |
5872 | and then Base_Type (T) /= Standard_String | |
b0186f71 | 5873 | then |
fe5d3068 YM |
5874 | Check_Formal_Restriction |
5875 | ("comparison is not defined on array types other than String", N); | |
5876 | else | |
5877 | null; | |
b0186f71 AC |
5878 | end if; |
5879 | ||
bd29d519 AC |
5880 | -- Check comparison on unordered enumeration |
5881 | ||
5882 | if Comes_From_Source (N) | |
5883 | and then Bad_Unordered_Enumeration_Reference (N, Etype (L)) | |
5884 | then | |
5885 | Error_Msg_N ("comparison on unordered enumeration type?", N); | |
5886 | end if; | |
5887 | ||
5cc9353d RD |
5888 | -- Evaluate the relation (note we do this after the above check since |
5889 | -- this Eval call may change N to True/False. | |
bd29d519 AC |
5890 | |
5891 | Eval_Relational_Op (N); | |
996ae0b0 RK |
5892 | end Resolve_Comparison_Op; |
5893 | ||
5894 | ------------------------------------ | |
5895 | -- Resolve_Conditional_Expression -- | |
5896 | ------------------------------------ | |
5897 | ||
5898 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is | |
5899 | Condition : constant Node_Id := First (Expressions (N)); | |
5900 | Then_Expr : constant Node_Id := Next (Condition); | |
19fb051c | 5901 | Else_Expr : Node_Id := Next (Then_Expr); |
b46be8a2 | 5902 | |
996ae0b0 | 5903 | begin |
b46be8a2 | 5904 | Resolve (Condition, Any_Boolean); |
996ae0b0 | 5905 | Resolve (Then_Expr, Typ); |
b46be8a2 RD |
5906 | |
5907 | -- If ELSE expression present, just resolve using the determined type | |
5908 | ||
5909 | if Present (Else_Expr) then | |
5910 | Resolve (Else_Expr, Typ); | |
5911 | ||
5912 | -- If no ELSE expression is present, root type must be Standard.Boolean | |
5913 | -- and we provide a Standard.True result converted to the appropriate | |
5914 | -- Boolean type (in case it is a derived boolean type). | |
5915 | ||
5916 | elsif Root_Type (Typ) = Standard_Boolean then | |
5917 | Else_Expr := | |
5918 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N))); | |
5919 | Analyze_And_Resolve (Else_Expr, Typ); | |
5920 | Append_To (Expressions (N), Else_Expr); | |
5921 | ||
5922 | else | |
5923 | Error_Msg_N ("can only omit ELSE expression in Boolean case", N); | |
5924 | Append_To (Expressions (N), Error); | |
5925 | end if; | |
5926 | ||
996ae0b0 RK |
5927 | Set_Etype (N, Typ); |
5928 | Eval_Conditional_Expression (N); | |
5929 | end Resolve_Conditional_Expression; | |
5930 | ||
5931 | ----------------------------------------- | |
5932 | -- Resolve_Discrete_Subtype_Indication -- | |
5933 | ----------------------------------------- | |
5934 | ||
5935 | procedure Resolve_Discrete_Subtype_Indication | |
5936 | (N : Node_Id; | |
5937 | Typ : Entity_Id) | |
5938 | is | |
5939 | R : Node_Id; | |
5940 | S : Entity_Id; | |
5941 | ||
5942 | begin | |
5943 | Analyze (Subtype_Mark (N)); | |
5944 | S := Entity (Subtype_Mark (N)); | |
5945 | ||
5946 | if Nkind (Constraint (N)) /= N_Range_Constraint then | |
5947 | Error_Msg_N ("expect range constraint for discrete type", N); | |
5948 | Set_Etype (N, Any_Type); | |
5949 | ||
5950 | else | |
5951 | R := Range_Expression (Constraint (N)); | |
5c736541 RD |
5952 | |
5953 | if R = Error then | |
5954 | return; | |
5955 | end if; | |
5956 | ||
996ae0b0 RK |
5957 | Analyze (R); |
5958 | ||
5959 | if Base_Type (S) /= Base_Type (Typ) then | |
5960 | Error_Msg_NE | |
5961 | ("expect subtype of }", N, First_Subtype (Typ)); | |
5962 | ||
5963 | -- Rewrite the constraint as a range of Typ | |
5964 | -- to allow compilation to proceed further. | |
5965 | ||
5966 | Set_Etype (N, Typ); | |
5967 | Rewrite (Low_Bound (R), | |
5968 | Make_Attribute_Reference (Sloc (Low_Bound (R)), | |
5cc9353d | 5969 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), |
996ae0b0 RK |
5970 | Attribute_Name => Name_First)); |
5971 | Rewrite (High_Bound (R), | |
5972 | Make_Attribute_Reference (Sloc (High_Bound (R)), | |
5cc9353d | 5973 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), |
996ae0b0 RK |
5974 | Attribute_Name => Name_First)); |
5975 | ||
5976 | else | |
5977 | Resolve (R, Typ); | |
5978 | Set_Etype (N, Etype (R)); | |
5979 | ||
5980 | -- Additionally, we must check that the bounds are compatible | |
5981 | -- with the given subtype, which might be different from the | |
5982 | -- type of the context. | |
5983 | ||
5984 | Apply_Range_Check (R, S); | |
5985 | ||
5986 | -- ??? If the above check statically detects a Constraint_Error | |
5987 | -- it replaces the offending bound(s) of the range R with a | |
5988 | -- Constraint_Error node. When the itype which uses these bounds | |
5989 | -- is frozen the resulting call to Duplicate_Subexpr generates | |
5990 | -- a new temporary for the bounds. | |
5991 | ||
5992 | -- Unfortunately there are other itypes that are also made depend | |
5993 | -- on these bounds, so when Duplicate_Subexpr is called they get | |
5994 | -- a forward reference to the newly created temporaries and Gigi | |
5995 | -- aborts on such forward references. This is probably sign of a | |
5996 | -- more fundamental problem somewhere else in either the order of | |
5997 | -- itype freezing or the way certain itypes are constructed. | |
5998 | ||
5999 | -- To get around this problem we call Remove_Side_Effects right | |
6000 | -- away if either bounds of R are a Constraint_Error. | |
6001 | ||
6002 | declare | |
fbf5a39b AC |
6003 | L : constant Node_Id := Low_Bound (R); |
6004 | H : constant Node_Id := High_Bound (R); | |
996ae0b0 RK |
6005 | |
6006 | begin | |
6007 | if Nkind (L) = N_Raise_Constraint_Error then | |
6008 | Remove_Side_Effects (L); | |
6009 | end if; | |
6010 | ||
6011 | if Nkind (H) = N_Raise_Constraint_Error then | |
6012 | Remove_Side_Effects (H); | |
6013 | end if; | |
6014 | end; | |
6015 | ||
6016 | Check_Unset_Reference (Low_Bound (R)); | |
6017 | Check_Unset_Reference (High_Bound (R)); | |
6018 | end if; | |
6019 | end if; | |
6020 | end Resolve_Discrete_Subtype_Indication; | |
6021 | ||
6022 | ------------------------- | |
6023 | -- Resolve_Entity_Name -- | |
6024 | ------------------------- | |
6025 | ||
6026 | -- Used to resolve identifiers and expanded names | |
6027 | ||
6028 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is | |
6029 | E : constant Entity_Id := Entity (N); | |
6030 | ||
6031 | begin | |
07fc65c4 GB |
6032 | -- If garbage from errors, set to Any_Type and return |
6033 | ||
6034 | if No (E) and then Total_Errors_Detected /= 0 then | |
6035 | Set_Etype (N, Any_Type); | |
6036 | return; | |
6037 | end if; | |
6038 | ||
996ae0b0 RK |
6039 | -- Replace named numbers by corresponding literals. Note that this is |
6040 | -- the one case where Resolve_Entity_Name must reset the Etype, since | |
6041 | -- it is currently marked as universal. | |
6042 | ||
6043 | if Ekind (E) = E_Named_Integer then | |
6044 | Set_Etype (N, Typ); | |
6045 | Eval_Named_Integer (N); | |
6046 | ||
6047 | elsif Ekind (E) = E_Named_Real then | |
6048 | Set_Etype (N, Typ); | |
6049 | Eval_Named_Real (N); | |
6050 | ||
6989bc1f AC |
6051 | -- For enumeration literals, we need to make sure that a proper style |
6052 | -- check is done, since such literals are overloaded, and thus we did | |
6053 | -- not do a style check during the first phase of analysis. | |
6054 | ||
6055 | elsif Ekind (E) = E_Enumeration_Literal then | |
6056 | Set_Entity_With_Style_Check (N, E); | |
6057 | Eval_Entity_Name (N); | |
6058 | ||
e606088a | 6059 | -- Case of subtype name appearing as an operand in expression |
996ae0b0 RK |
6060 | |
6061 | elsif Is_Type (E) then | |
e606088a AC |
6062 | |
6063 | -- Allow use of subtype if it is a concurrent type where we are | |
6064 | -- currently inside the body. This will eventually be expanded into a | |
6065 | -- call to Self (for tasks) or _object (for protected objects). Any | |
6066 | -- other use of a subtype is invalid. | |
6067 | ||
996ae0b0 RK |
6068 | if Is_Concurrent_Type (E) |
6069 | and then In_Open_Scopes (E) | |
6070 | then | |
6071 | null; | |
e606088a | 6072 | |
308e6f3a | 6073 | -- Any other use is an error |
e606088a | 6074 | |
996ae0b0 RK |
6075 | else |
6076 | Error_Msg_N | |
758c442c | 6077 | ("invalid use of subtype mark in expression or call", N); |
996ae0b0 RK |
6078 | end if; |
6079 | ||
6080 | -- Check discriminant use if entity is discriminant in current scope, | |
6081 | -- i.e. discriminant of record or concurrent type currently being | |
6082 | -- analyzed. Uses in corresponding body are unrestricted. | |
6083 | ||
6084 | elsif Ekind (E) = E_Discriminant | |
6085 | and then Scope (E) = Current_Scope | |
6086 | and then not Has_Completion (Current_Scope) | |
6087 | then | |
6088 | Check_Discriminant_Use (N); | |
6089 | ||
6090 | -- A parameterless generic function cannot appear in a context that | |
6091 | -- requires resolution. | |
6092 | ||
6093 | elsif Ekind (E) = E_Generic_Function then | |
6094 | Error_Msg_N ("illegal use of generic function", N); | |
6095 | ||
6096 | elsif Ekind (E) = E_Out_Parameter | |
0ab80019 | 6097 | and then Ada_Version = Ada_83 |
996ae0b0 | 6098 | and then (Nkind (Parent (N)) in N_Op |
19fb051c AC |
6099 | or else (Nkind (Parent (N)) = N_Assignment_Statement |
6100 | and then N = Expression (Parent (N))) | |
6101 | or else Nkind (Parent (N)) = N_Explicit_Dereference) | |
996ae0b0 RK |
6102 | then |
6103 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", N); | |
6104 | ||
6105 | -- In all other cases, just do the possible static evaluation | |
6106 | ||
6107 | else | |
d81b4bfe TQ |
6108 | -- A deferred constant that appears in an expression must have a |
6109 | -- completion, unless it has been removed by in-place expansion of | |
6110 | -- an aggregate. | |
996ae0b0 RK |
6111 | |
6112 | if Ekind (E) = E_Constant | |
6113 | and then Comes_From_Source (E) | |
6114 | and then No (Constant_Value (E)) | |
6115 | and then Is_Frozen (Etype (E)) | |
45fc7ddb | 6116 | and then not In_Spec_Expression |
996ae0b0 RK |
6117 | and then not Is_Imported (E) |
6118 | then | |
996ae0b0 RK |
6119 | if No_Initialization (Parent (E)) |
6120 | or else (Present (Full_View (E)) | |
6121 | and then No_Initialization (Parent (Full_View (E)))) | |
6122 | then | |
6123 | null; | |
6124 | else | |
6125 | Error_Msg_N ( | |
6126 | "deferred constant is frozen before completion", N); | |
6127 | end if; | |
6128 | end if; | |
6129 | ||
6130 | Eval_Entity_Name (N); | |
6131 | end if; | |
6132 | end Resolve_Entity_Name; | |
6133 | ||
6134 | ------------------- | |
6135 | -- Resolve_Entry -- | |
6136 | ------------------- | |
6137 | ||
6138 | procedure Resolve_Entry (Entry_Name : Node_Id) is | |
6139 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
6140 | Nam : Entity_Id; | |
6141 | New_N : Node_Id; | |
6142 | S : Entity_Id; | |
6143 | Tsk : Entity_Id; | |
6144 | E_Name : Node_Id; | |
6145 | Index : Node_Id; | |
6146 | ||
6147 | function Actual_Index_Type (E : Entity_Id) return Entity_Id; | |
6148 | -- If the bounds of the entry family being called depend on task | |
6149 | -- discriminants, build a new index subtype where a discriminant is | |
6150 | -- replaced with the value of the discriminant of the target task. | |
6151 | -- The target task is the prefix of the entry name in the call. | |
6152 | ||
6153 | ----------------------- | |
6154 | -- Actual_Index_Type -- | |
6155 | ----------------------- | |
6156 | ||
6157 | function Actual_Index_Type (E : Entity_Id) return Entity_Id is | |
fbf5a39b AC |
6158 | Typ : constant Entity_Id := Entry_Index_Type (E); |
6159 | Tsk : constant Entity_Id := Scope (E); | |
6160 | Lo : constant Node_Id := Type_Low_Bound (Typ); | |
6161 | Hi : constant Node_Id := Type_High_Bound (Typ); | |
996ae0b0 RK |
6162 | New_T : Entity_Id; |
6163 | ||
6164 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id; | |
6165 | -- If the bound is given by a discriminant, replace with a reference | |
d81b4bfe TQ |
6166 | -- to the discriminant of the same name in the target task. If the |
6167 | -- entry name is the target of a requeue statement and the entry is | |
6168 | -- in the current protected object, the bound to be used is the | |
008f6fd3 | 6169 | -- discriminal of the object (see Apply_Range_Checks for details of |
d81b4bfe | 6170 | -- the transformation). |
996ae0b0 RK |
6171 | |
6172 | ----------------------------- | |
6173 | -- Actual_Discriminant_Ref -- | |
6174 | ----------------------------- | |
6175 | ||
6176 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is | |
fbf5a39b | 6177 | Typ : constant Entity_Id := Etype (Bound); |
996ae0b0 RK |
6178 | Ref : Node_Id; |
6179 | ||
6180 | begin | |
6181 | Remove_Side_Effects (Bound); | |
6182 | ||
6183 | if not Is_Entity_Name (Bound) | |
6184 | or else Ekind (Entity (Bound)) /= E_Discriminant | |
6185 | then | |
6186 | return Bound; | |
6187 | ||
6188 | elsif Is_Protected_Type (Tsk) | |
6189 | and then In_Open_Scopes (Tsk) | |
6190 | and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement | |
6191 | then | |
6ca9ec9c AC |
6192 | -- Note: here Bound denotes a discriminant of the corresponding |
6193 | -- record type tskV, whose discriminal is a formal of the | |
6194 | -- init-proc tskVIP. What we want is the body discriminal, | |
6195 | -- which is associated to the discriminant of the original | |
6196 | -- concurrent type tsk. | |
6197 | ||
5a153b27 AC |
6198 | return New_Occurrence_Of |
6199 | (Find_Body_Discriminal (Entity (Bound)), Loc); | |
996ae0b0 RK |
6200 | |
6201 | else | |
6202 | Ref := | |
6203 | Make_Selected_Component (Loc, | |
6204 | Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))), | |
6205 | Selector_Name => New_Occurrence_Of (Entity (Bound), Loc)); | |
6206 | Analyze (Ref); | |
6207 | Resolve (Ref, Typ); | |
6208 | return Ref; | |
6209 | end if; | |
6210 | end Actual_Discriminant_Ref; | |
6211 | ||
6212 | -- Start of processing for Actual_Index_Type | |
6213 | ||
6214 | begin | |
6215 | if not Has_Discriminants (Tsk) | |
19fb051c | 6216 | or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi)) |
996ae0b0 RK |
6217 | then |
6218 | return Entry_Index_Type (E); | |
6219 | ||
6220 | else | |
6221 | New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name)); | |
6222 | Set_Etype (New_T, Base_Type (Typ)); | |
6223 | Set_Size_Info (New_T, Typ); | |
6224 | Set_RM_Size (New_T, RM_Size (Typ)); | |
6225 | Set_Scalar_Range (New_T, | |
6226 | Make_Range (Sloc (Entry_Name), | |
6227 | Low_Bound => Actual_Discriminant_Ref (Lo), | |
6228 | High_Bound => Actual_Discriminant_Ref (Hi))); | |
6229 | ||
6230 | return New_T; | |
6231 | end if; | |
6232 | end Actual_Index_Type; | |
6233 | ||
6234 | -- Start of processing of Resolve_Entry | |
6235 | ||
6236 | begin | |
5cc9353d RD |
6237 | -- Find name of entry being called, and resolve prefix of name with its |
6238 | -- own type. The prefix can be overloaded, and the name and signature of | |
6239 | -- the entry must be taken into account. | |
996ae0b0 RK |
6240 | |
6241 | if Nkind (Entry_Name) = N_Indexed_Component then | |
6242 | ||
6243 | -- Case of dealing with entry family within the current tasks | |
6244 | ||
6245 | E_Name := Prefix (Entry_Name); | |
6246 | ||
6247 | else | |
6248 | E_Name := Entry_Name; | |
6249 | end if; | |
6250 | ||
6251 | if Is_Entity_Name (E_Name) then | |
996ae0b0 | 6252 | |
d81b4bfe TQ |
6253 | -- Entry call to an entry (or entry family) in the current task. This |
6254 | -- is legal even though the task will deadlock. Rewrite as call to | |
6255 | -- current task. | |
996ae0b0 | 6256 | |
d81b4bfe TQ |
6257 | -- This can also be a call to an entry in an enclosing task. If this |
6258 | -- is a single task, we have to retrieve its name, because the scope | |
6259 | -- of the entry is the task type, not the object. If the enclosing | |
6260 | -- task is a task type, the identity of the task is given by its own | |
6261 | -- self variable. | |
6262 | ||
6263 | -- Finally this can be a requeue on an entry of the same task or | |
6264 | -- protected object. | |
996ae0b0 RK |
6265 | |
6266 | S := Scope (Entity (E_Name)); | |
6267 | ||
6268 | for J in reverse 0 .. Scope_Stack.Last loop | |
996ae0b0 RK |
6269 | if Is_Task_Type (Scope_Stack.Table (J).Entity) |
6270 | and then not Comes_From_Source (S) | |
6271 | then | |
6272 | -- S is an enclosing task or protected object. The concurrent | |
6273 | -- declaration has been converted into a type declaration, and | |
6274 | -- the object itself has an object declaration that follows | |
6275 | -- the type in the same declarative part. | |
6276 | ||
6277 | Tsk := Next_Entity (S); | |
996ae0b0 RK |
6278 | while Etype (Tsk) /= S loop |
6279 | Next_Entity (Tsk); | |
6280 | end loop; | |
6281 | ||
6282 | S := Tsk; | |
6283 | exit; | |
6284 | ||
6285 | elsif S = Scope_Stack.Table (J).Entity then | |
6286 | ||
6287 | -- Call to current task. Will be transformed into call to Self | |
6288 | ||
6289 | exit; | |
6290 | ||
6291 | end if; | |
6292 | end loop; | |
6293 | ||
6294 | New_N := | |
6295 | Make_Selected_Component (Loc, | |
6296 | Prefix => New_Occurrence_Of (S, Loc), | |
6297 | Selector_Name => | |
6298 | New_Occurrence_Of (Entity (E_Name), Loc)); | |
6299 | Rewrite (E_Name, New_N); | |
6300 | Analyze (E_Name); | |
6301 | ||
6302 | elsif Nkind (Entry_Name) = N_Selected_Component | |
6303 | and then Is_Overloaded (Prefix (Entry_Name)) | |
6304 | then | |
d81b4bfe | 6305 | -- Use the entry name (which must be unique at this point) to find |
5cc9353d | 6306 | -- the prefix that returns the corresponding task/protected type. |
996ae0b0 RK |
6307 | |
6308 | declare | |
fbf5a39b AC |
6309 | Pref : constant Node_Id := Prefix (Entry_Name); |
6310 | Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name)); | |
996ae0b0 RK |
6311 | I : Interp_Index; |
6312 | It : Interp; | |
996ae0b0 RK |
6313 | |
6314 | begin | |
6315 | Get_First_Interp (Pref, I, It); | |
996ae0b0 | 6316 | while Present (It.Typ) loop |
996ae0b0 RK |
6317 | if Scope (Ent) = It.Typ then |
6318 | Set_Etype (Pref, It.Typ); | |
6319 | exit; | |
6320 | end if; | |
6321 | ||
6322 | Get_Next_Interp (I, It); | |
6323 | end loop; | |
6324 | end; | |
6325 | end if; | |
6326 | ||
6327 | if Nkind (Entry_Name) = N_Selected_Component then | |
fbf5a39b | 6328 | Resolve (Prefix (Entry_Name)); |
996ae0b0 RK |
6329 | |
6330 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6331 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
fbf5a39b | 6332 | Resolve (Prefix (Prefix (Entry_Name))); |
996ae0b0 RK |
6333 | Index := First (Expressions (Entry_Name)); |
6334 | Resolve (Index, Entry_Index_Type (Nam)); | |
6335 | ||
d81b4bfe TQ |
6336 | -- Up to this point the expression could have been the actual in a |
6337 | -- simple entry call, and be given by a named association. | |
996ae0b0 RK |
6338 | |
6339 | if Nkind (Index) = N_Parameter_Association then | |
6340 | Error_Msg_N ("expect expression for entry index", Index); | |
6341 | else | |
6342 | Apply_Range_Check (Index, Actual_Index_Type (Nam)); | |
6343 | end if; | |
6344 | end if; | |
996ae0b0 RK |
6345 | end Resolve_Entry; |
6346 | ||
6347 | ------------------------ | |
6348 | -- Resolve_Entry_Call -- | |
6349 | ------------------------ | |
6350 | ||
6351 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is | |
6352 | Entry_Name : constant Node_Id := Name (N); | |
6353 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
6354 | Actuals : List_Id; | |
6355 | First_Named : Node_Id; | |
6356 | Nam : Entity_Id; | |
6357 | Norm_OK : Boolean; | |
6358 | Obj : Node_Id; | |
6359 | Was_Over : Boolean; | |
6360 | ||
6361 | begin | |
d81b4bfe TQ |
6362 | -- We kill all checks here, because it does not seem worth the effort to |
6363 | -- do anything better, an entry call is a big operation. | |
fbf5a39b AC |
6364 | |
6365 | Kill_All_Checks; | |
6366 | ||
996ae0b0 RK |
6367 | -- Processing of the name is similar for entry calls and protected |
6368 | -- operation calls. Once the entity is determined, we can complete | |
6369 | -- the resolution of the actuals. | |
6370 | ||
6371 | -- The selector may be overloaded, in the case of a protected object | |
6372 | -- with overloaded functions. The type of the context is used for | |
6373 | -- resolution. | |
6374 | ||
6375 | if Nkind (Entry_Name) = N_Selected_Component | |
6376 | and then Is_Overloaded (Selector_Name (Entry_Name)) | |
6377 | and then Typ /= Standard_Void_Type | |
6378 | then | |
6379 | declare | |
6380 | I : Interp_Index; | |
6381 | It : Interp; | |
6382 | ||
6383 | begin | |
6384 | Get_First_Interp (Selector_Name (Entry_Name), I, It); | |
996ae0b0 | 6385 | while Present (It.Typ) loop |
996ae0b0 RK |
6386 | if Covers (Typ, It.Typ) then |
6387 | Set_Entity (Selector_Name (Entry_Name), It.Nam); | |
6388 | Set_Etype (Entry_Name, It.Typ); | |
6389 | ||
6390 | Generate_Reference (It.Typ, N, ' '); | |
6391 | end if; | |
6392 | ||
6393 | Get_Next_Interp (I, It); | |
6394 | end loop; | |
6395 | end; | |
6396 | end if; | |
6397 | ||
6398 | Resolve_Entry (Entry_Name); | |
6399 | ||
6400 | if Nkind (Entry_Name) = N_Selected_Component then | |
6401 | ||
a77842bd | 6402 | -- Simple entry call |
996ae0b0 RK |
6403 | |
6404 | Nam := Entity (Selector_Name (Entry_Name)); | |
6405 | Obj := Prefix (Entry_Name); | |
6406 | Was_Over := Is_Overloaded (Selector_Name (Entry_Name)); | |
6407 | ||
6408 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6409 | ||
a77842bd | 6410 | -- Call to member of entry family |
996ae0b0 RK |
6411 | |
6412 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
6413 | Obj := Prefix (Prefix (Entry_Name)); | |
6414 | Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name))); | |
6415 | end if; | |
6416 | ||
5cc9353d RD |
6417 | -- We cannot in general check the maximum depth of protected entry calls |
6418 | -- at compile time. But we can tell that any protected entry call at all | |
6419 | -- violates a specified nesting depth of zero. | |
fbf5a39b AC |
6420 | |
6421 | if Is_Protected_Type (Scope (Nam)) then | |
9f4fd324 | 6422 | Check_Restriction (Max_Entry_Queue_Length, N); |
fbf5a39b AC |
6423 | end if; |
6424 | ||
996ae0b0 | 6425 | -- Use context type to disambiguate a protected function that can be |
5cc9353d RD |
6426 | -- called without actuals and that returns an array type, and where the |
6427 | -- argument list may be an indexing of the returned value. | |
996ae0b0 RK |
6428 | |
6429 | if Ekind (Nam) = E_Function | |
6430 | and then Needs_No_Actuals (Nam) | |
6431 | and then Present (Parameter_Associations (N)) | |
6432 | and then | |
6433 | ((Is_Array_Type (Etype (Nam)) | |
6434 | and then Covers (Typ, Component_Type (Etype (Nam)))) | |
6435 | ||
6436 | or else (Is_Access_Type (Etype (Nam)) | |
6437 | and then Is_Array_Type (Designated_Type (Etype (Nam))) | |
19fb051c AC |
6438 | and then |
6439 | Covers | |
6440 | (Typ, | |
6441 | Component_Type (Designated_Type (Etype (Nam)))))) | |
996ae0b0 RK |
6442 | then |
6443 | declare | |
6444 | Index_Node : Node_Id; | |
6445 | ||
6446 | begin | |
6447 | Index_Node := | |
6448 | Make_Indexed_Component (Loc, | |
6449 | Prefix => | |
19fb051c | 6450 | Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)), |
996ae0b0 RK |
6451 | Expressions => Parameter_Associations (N)); |
6452 | ||
5cc9353d RD |
6453 | -- Since we are correcting a node classification error made by the |
6454 | -- parser, we call Replace rather than Rewrite. | |
996ae0b0 RK |
6455 | |
6456 | Replace (N, Index_Node); | |
6457 | Set_Etype (Prefix (N), Etype (Nam)); | |
6458 | Set_Etype (N, Typ); | |
6459 | Resolve_Indexed_Component (N, Typ); | |
6460 | return; | |
6461 | end; | |
6462 | end if; | |
6463 | ||
b7f17b20 ES |
6464 | if Ekind_In (Nam, E_Entry, E_Entry_Family) |
6465 | and then Present (PPC_Wrapper (Nam)) | |
6466 | and then Current_Scope /= PPC_Wrapper (Nam) | |
6467 | then | |
468ee96a | 6468 | -- Rewrite as call to the precondition wrapper, adding the task |
5cc9353d RD |
6469 | -- object to the list of actuals. If the call is to a member of an |
6470 | -- entry family, include the index as well. | |
b7f17b20 ES |
6471 | |
6472 | declare | |
468ee96a | 6473 | New_Call : Node_Id; |
b7f17b20 | 6474 | New_Actuals : List_Id; |
19fb051c | 6475 | |
b7f17b20 ES |
6476 | begin |
6477 | New_Actuals := New_List (Obj); | |
3fd9f17c AC |
6478 | |
6479 | if Nkind (Entry_Name) = N_Indexed_Component then | |
6480 | Append_To (New_Actuals, | |
6481 | New_Copy_Tree (First (Expressions (Entry_Name)))); | |
6482 | end if; | |
6483 | ||
b7f17b20 | 6484 | Append_List (Parameter_Associations (N), New_Actuals); |
468ee96a AC |
6485 | New_Call := |
6486 | Make_Procedure_Call_Statement (Loc, | |
6487 | Name => | |
6488 | New_Occurrence_Of (PPC_Wrapper (Nam), Loc), | |
6489 | Parameter_Associations => New_Actuals); | |
b7f17b20 ES |
6490 | Rewrite (N, New_Call); |
6491 | Analyze_And_Resolve (N); | |
6492 | return; | |
6493 | end; | |
6494 | end if; | |
6495 | ||
996ae0b0 | 6496 | -- The operation name may have been overloaded. Order the actuals |
5cc9353d RD |
6497 | -- according to the formals of the resolved entity, and set the return |
6498 | -- type to that of the operation. | |
996ae0b0 RK |
6499 | |
6500 | if Was_Over then | |
6501 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
6502 | pragma Assert (Norm_OK); | |
fbf5a39b | 6503 | Set_Etype (N, Etype (Nam)); |
996ae0b0 RK |
6504 | end if; |
6505 | ||
6506 | Resolve_Actuals (N, Nam); | |
ae6ede77 AC |
6507 | |
6508 | -- Create a call reference to the entry | |
6509 | ||
6510 | Generate_Reference (Nam, Entry_Name, 's'); | |
996ae0b0 | 6511 | |
8a95f4e8 | 6512 | if Ekind_In (Nam, E_Entry, E_Entry_Family) then |
996ae0b0 RK |
6513 | Check_Potentially_Blocking_Operation (N); |
6514 | end if; | |
6515 | ||
6516 | -- Verify that a procedure call cannot masquerade as an entry | |
6517 | -- call where an entry call is expected. | |
6518 | ||
6519 | if Ekind (Nam) = E_Procedure then | |
996ae0b0 RK |
6520 | if Nkind (Parent (N)) = N_Entry_Call_Alternative |
6521 | and then N = Entry_Call_Statement (Parent (N)) | |
6522 | then | |
6523 | Error_Msg_N ("entry call required in select statement", N); | |
6524 | ||
6525 | elsif Nkind (Parent (N)) = N_Triggering_Alternative | |
6526 | and then N = Triggering_Statement (Parent (N)) | |
6527 | then | |
6528 | Error_Msg_N ("triggering statement cannot be procedure call", N); | |
6529 | ||
6530 | elsif Ekind (Scope (Nam)) = E_Task_Type | |
6531 | and then not In_Open_Scopes (Scope (Nam)) | |
6532 | then | |
758c442c | 6533 | Error_Msg_N ("task has no entry with this name", Entry_Name); |
996ae0b0 RK |
6534 | end if; |
6535 | end if; | |
6536 | ||
d81b4bfe TQ |
6537 | -- After resolution, entry calls and protected procedure calls are |
6538 | -- changed into entry calls, for expansion. The structure of the node | |
6539 | -- does not change, so it can safely be done in place. Protected | |
6540 | -- function calls must keep their structure because they are | |
6541 | -- subexpressions. | |
996ae0b0 RK |
6542 | |
6543 | if Ekind (Nam) /= E_Function then | |
6544 | ||
6545 | -- A protected operation that is not a function may modify the | |
d81b4bfe TQ |
6546 | -- corresponding object, and cannot apply to a constant. If this |
6547 | -- is an internal call, the prefix is the type itself. | |
996ae0b0 RK |
6548 | |
6549 | if Is_Protected_Type (Scope (Nam)) | |
6550 | and then not Is_Variable (Obj) | |
6551 | and then (not Is_Entity_Name (Obj) | |
6552 | or else not Is_Type (Entity (Obj))) | |
6553 | then | |
6554 | Error_Msg_N | |
6555 | ("prefix of protected procedure or entry call must be variable", | |
6556 | Entry_Name); | |
6557 | end if; | |
6558 | ||
6559 | Actuals := Parameter_Associations (N); | |
6560 | First_Named := First_Named_Actual (N); | |
6561 | ||
6562 | Rewrite (N, | |
6563 | Make_Entry_Call_Statement (Loc, | |
6564 | Name => Entry_Name, | |
6565 | Parameter_Associations => Actuals)); | |
6566 | ||
6567 | Set_First_Named_Actual (N, First_Named); | |
6568 | Set_Analyzed (N, True); | |
6569 | ||
6570 | -- Protected functions can return on the secondary stack, in which | |
1420b484 | 6571 | -- case we must trigger the transient scope mechanism. |
996ae0b0 RK |
6572 | |
6573 | elsif Expander_Active | |
6574 | and then Requires_Transient_Scope (Etype (Nam)) | |
6575 | then | |
0669bebe | 6576 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 6577 | end if; |
996ae0b0 RK |
6578 | end Resolve_Entry_Call; |
6579 | ||
6580 | ------------------------- | |
6581 | -- Resolve_Equality_Op -- | |
6582 | ------------------------- | |
6583 | ||
d81b4bfe TQ |
6584 | -- Both arguments must have the same type, and the boolean context does |
6585 | -- not participate in the resolution. The first pass verifies that the | |
6586 | -- interpretation is not ambiguous, and the type of the left argument is | |
6587 | -- correctly set, or is Any_Type in case of ambiguity. If both arguments | |
6588 | -- are strings or aggregates, allocators, or Null, they are ambiguous even | |
6589 | -- though they carry a single (universal) type. Diagnose this case here. | |
996ae0b0 RK |
6590 | |
6591 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is | |
6592 | L : constant Node_Id := Left_Opnd (N); | |
6593 | R : constant Node_Id := Right_Opnd (N); | |
6594 | T : Entity_Id := Find_Unique_Type (L, R); | |
6595 | ||
a8930b80 AC |
6596 | procedure Check_Conditional_Expression (Cond : Node_Id); |
6597 | -- The resolution rule for conditional expressions requires that each | |
6598 | -- such must have a unique type. This means that if several dependent | |
6599 | -- expressions are of a non-null anonymous access type, and the context | |
6600 | -- does not impose an expected type (as can be the case in an equality | |
6601 | -- operation) the expression must be rejected. | |
6602 | ||
996ae0b0 RK |
6603 | function Find_Unique_Access_Type return Entity_Id; |
6604 | -- In the case of allocators, make a last-ditch attempt to find a single | |
6605 | -- access type with the right designated type. This is semantically | |
6606 | -- dubious, and of no interest to any real code, but c48008a makes it | |
6607 | -- all worthwhile. | |
6608 | ||
a8930b80 AC |
6609 | ---------------------------------- |
6610 | -- Check_Conditional_Expression -- | |
6611 | ---------------------------------- | |
6612 | ||
6613 | procedure Check_Conditional_Expression (Cond : Node_Id) is | |
6614 | Then_Expr : Node_Id; | |
6615 | Else_Expr : Node_Id; | |
6616 | ||
6617 | begin | |
6618 | if Nkind (Cond) = N_Conditional_Expression then | |
6619 | Then_Expr := Next (First (Expressions (Cond))); | |
6620 | Else_Expr := Next (Then_Expr); | |
6621 | ||
6622 | if Nkind (Then_Expr) /= N_Null | |
6623 | and then Nkind (Else_Expr) /= N_Null | |
6624 | then | |
6625 | Error_Msg_N | |
6626 | ("cannot determine type of conditional expression", Cond); | |
6627 | end if; | |
6628 | end if; | |
6629 | end Check_Conditional_Expression; | |
6630 | ||
996ae0b0 RK |
6631 | ----------------------------- |
6632 | -- Find_Unique_Access_Type -- | |
6633 | ----------------------------- | |
6634 | ||
6635 | function Find_Unique_Access_Type return Entity_Id is | |
6636 | Acc : Entity_Id; | |
6637 | E : Entity_Id; | |
1420b484 | 6638 | S : Entity_Id; |
996ae0b0 RK |
6639 | |
6640 | begin | |
6641 | if Ekind (Etype (R)) = E_Allocator_Type then | |
6642 | Acc := Designated_Type (Etype (R)); | |
996ae0b0 RK |
6643 | elsif Ekind (Etype (L)) = E_Allocator_Type then |
6644 | Acc := Designated_Type (Etype (L)); | |
996ae0b0 RK |
6645 | else |
6646 | return Empty; | |
6647 | end if; | |
6648 | ||
1420b484 | 6649 | S := Current_Scope; |
996ae0b0 RK |
6650 | while S /= Standard_Standard loop |
6651 | E := First_Entity (S); | |
996ae0b0 | 6652 | while Present (E) loop |
996ae0b0 RK |
6653 | if Is_Type (E) |
6654 | and then Is_Access_Type (E) | |
6655 | and then Ekind (E) /= E_Allocator_Type | |
6656 | and then Designated_Type (E) = Base_Type (Acc) | |
6657 | then | |
6658 | return E; | |
6659 | end if; | |
6660 | ||
6661 | Next_Entity (E); | |
6662 | end loop; | |
6663 | ||
6664 | S := Scope (S); | |
6665 | end loop; | |
6666 | ||
6667 | return Empty; | |
6668 | end Find_Unique_Access_Type; | |
6669 | ||
6670 | -- Start of processing for Resolve_Equality_Op | |
6671 | ||
6672 | begin | |
6673 | Set_Etype (N, Base_Type (Typ)); | |
6674 | Generate_Reference (T, N, ' '); | |
6675 | ||
6676 | if T = Any_Fixed then | |
6677 | T := Unique_Fixed_Point_Type (L); | |
6678 | end if; | |
6679 | ||
6680 | if T /= Any_Type then | |
19fb051c AC |
6681 | if T = Any_String or else |
6682 | T = Any_Composite or else | |
6683 | T = Any_Character | |
996ae0b0 | 6684 | then |
996ae0b0 RK |
6685 | if T = Any_Character then |
6686 | Ambiguous_Character (L); | |
6687 | else | |
6688 | Error_Msg_N ("ambiguous operands for equality", N); | |
6689 | end if; | |
6690 | ||
6691 | Set_Etype (N, Any_Type); | |
6692 | return; | |
6693 | ||
6694 | elsif T = Any_Access | |
964f13da | 6695 | or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type) |
996ae0b0 RK |
6696 | then |
6697 | T := Find_Unique_Access_Type; | |
6698 | ||
6699 | if No (T) then | |
6700 | Error_Msg_N ("ambiguous operands for equality", N); | |
6701 | Set_Etype (N, Any_Type); | |
6702 | return; | |
6703 | end if; | |
a8930b80 AC |
6704 | |
6705 | -- Conditional expressions must have a single type, and if the | |
6706 | -- context does not impose one the dependent expressions cannot | |
6707 | -- be anonymous access types. | |
6708 | ||
6709 | elsif Ada_Version >= Ada_2012 | |
ae2aa109 AC |
6710 | and then Ekind_In (Etype (L), E_Anonymous_Access_Type, |
6711 | E_Anonymous_Access_Subprogram_Type) | |
6712 | and then Ekind_In (Etype (R), E_Anonymous_Access_Type, | |
6713 | E_Anonymous_Access_Subprogram_Type) | |
a8930b80 AC |
6714 | then |
6715 | Check_Conditional_Expression (L); | |
6716 | Check_Conditional_Expression (R); | |
996ae0b0 RK |
6717 | end if; |
6718 | ||
996ae0b0 RK |
6719 | Resolve (L, T); |
6720 | Resolve (R, T); | |
fbf5a39b | 6721 | |
b0186f71 AC |
6722 | -- In SPARK or ALFA, equality operators = and /= for array types |
6723 | -- other than String are only defined when, for each index position, | |
6724 | -- the operands have equal static bounds. | |
6725 | ||
fe5d3068 | 6726 | if Is_Array_Type (T) |
b0186f71 | 6727 | and then Base_Type (T) /= Standard_String |
19fb051c | 6728 | and then Base_Type (Etype (L)) = Base_Type (Etype (R)) |
b0186f71 AC |
6729 | and then not Matching_Static_Array_Bounds (Etype (L), Etype (R)) |
6730 | then | |
fe5d3068 YM |
6731 | Check_Formal_Restriction |
6732 | ("array types should have matching static bounds", N); | |
b0186f71 AC |
6733 | end if; |
6734 | ||
0669bebe GB |
6735 | -- If the unique type is a class-wide type then it will be expanded |
6736 | -- into a dispatching call to the predefined primitive. Therefore we | |
6737 | -- check here for potential violation of such restriction. | |
6738 | ||
6739 | if Is_Class_Wide_Type (T) then | |
6740 | Check_Restriction (No_Dispatching_Calls, N); | |
6741 | end if; | |
6742 | ||
fbf5a39b AC |
6743 | if Warn_On_Redundant_Constructs |
6744 | and then Comes_From_Source (N) | |
6745 | and then Is_Entity_Name (R) | |
6746 | and then Entity (R) = Standard_True | |
6747 | and then Comes_From_Source (R) | |
6748 | then | |
305caf42 AC |
6749 | Error_Msg_N -- CODEFIX |
6750 | ("?comparison with True is redundant!", R); | |
fbf5a39b AC |
6751 | end if; |
6752 | ||
996ae0b0 RK |
6753 | Check_Unset_Reference (L); |
6754 | Check_Unset_Reference (R); | |
fbf5a39b | 6755 | Generate_Operator_Reference (N, T); |
fad0600d | 6756 | Check_Low_Bound_Tested (N); |
996ae0b0 RK |
6757 | |
6758 | -- If this is an inequality, it may be the implicit inequality | |
6759 | -- created for a user-defined operation, in which case the corres- | |
6760 | -- ponding equality operation is not intrinsic, and the operation | |
6761 | -- cannot be constant-folded. Else fold. | |
6762 | ||
6763 | if Nkind (N) = N_Op_Eq | |
6764 | or else Comes_From_Source (Entity (N)) | |
6765 | or else Ekind (Entity (N)) = E_Operator | |
6766 | or else Is_Intrinsic_Subprogram | |
19fb051c | 6767 | (Corresponding_Equality (Entity (N))) |
996ae0b0 RK |
6768 | then |
6769 | Eval_Relational_Op (N); | |
45fc7ddb | 6770 | |
996ae0b0 | 6771 | elsif Nkind (N) = N_Op_Ne |
0669bebe | 6772 | and then Is_Abstract_Subprogram (Entity (N)) |
996ae0b0 RK |
6773 | then |
6774 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N)); | |
6775 | end if; | |
758c442c | 6776 | |
d81b4bfe TQ |
6777 | -- Ada 2005: If one operand is an anonymous access type, convert the |
6778 | -- other operand to it, to ensure that the underlying types match in | |
6779 | -- the back-end. Same for access_to_subprogram, and the conversion | |
6780 | -- verifies that the types are subtype conformant. | |
b7d1f17f | 6781 | |
d81b4bfe TQ |
6782 | -- We apply the same conversion in the case one of the operands is a |
6783 | -- private subtype of the type of the other. | |
c8ef728f | 6784 | |
b7d1f17f HK |
6785 | -- Why the Expander_Active test here ??? |
6786 | ||
4197ae1e | 6787 | if Expander_Active |
b7d1f17f | 6788 | and then |
964f13da RD |
6789 | (Ekind_In (T, E_Anonymous_Access_Type, |
6790 | E_Anonymous_Access_Subprogram_Type) | |
b7d1f17f | 6791 | or else Is_Private_Type (T)) |
c8ef728f ES |
6792 | then |
6793 | if Etype (L) /= T then | |
6794 | Rewrite (L, | |
6795 | Make_Unchecked_Type_Conversion (Sloc (L), | |
6796 | Subtype_Mark => New_Occurrence_Of (T, Sloc (L)), | |
6797 | Expression => Relocate_Node (L))); | |
6798 | Analyze_And_Resolve (L, T); | |
6799 | end if; | |
6800 | ||
6801 | if (Etype (R)) /= T then | |
6802 | Rewrite (R, | |
6803 | Make_Unchecked_Type_Conversion (Sloc (R), | |
6804 | Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)), | |
6805 | Expression => Relocate_Node (R))); | |
6806 | Analyze_And_Resolve (R, T); | |
6807 | end if; | |
6808 | end if; | |
996ae0b0 RK |
6809 | end if; |
6810 | end Resolve_Equality_Op; | |
6811 | ||
6812 | ---------------------------------- | |
6813 | -- Resolve_Explicit_Dereference -- | |
6814 | ---------------------------------- | |
6815 | ||
6816 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is | |
bc5f3720 RD |
6817 | Loc : constant Source_Ptr := Sloc (N); |
6818 | New_N : Node_Id; | |
6819 | P : constant Node_Id := Prefix (N); | |
6820 | I : Interp_Index; | |
6821 | It : Interp; | |
996ae0b0 RK |
6822 | |
6823 | begin | |
c8ef728f | 6824 | Check_Fully_Declared_Prefix (Typ, P); |
996ae0b0 RK |
6825 | |
6826 | if Is_Overloaded (P) then | |
6827 | ||
758c442c GD |
6828 | -- Use the context type to select the prefix that has the correct |
6829 | -- designated type. | |
996ae0b0 RK |
6830 | |
6831 | Get_First_Interp (P, I, It); | |
6832 | while Present (It.Typ) loop | |
6833 | exit when Is_Access_Type (It.Typ) | |
6834 | and then Covers (Typ, Designated_Type (It.Typ)); | |
996ae0b0 RK |
6835 | Get_Next_Interp (I, It); |
6836 | end loop; | |
6837 | ||
bc5f3720 RD |
6838 | if Present (It.Typ) then |
6839 | Resolve (P, It.Typ); | |
6840 | else | |
758c442c GD |
6841 | -- If no interpretation covers the designated type of the prefix, |
6842 | -- this is the pathological case where not all implementations of | |
6843 | -- the prefix allow the interpretation of the node as a call. Now | |
6844 | -- that the expected type is known, Remove other interpretations | |
6845 | -- from prefix, rewrite it as a call, and resolve again, so that | |
6846 | -- the proper call node is generated. | |
bc5f3720 RD |
6847 | |
6848 | Get_First_Interp (P, I, It); | |
6849 | while Present (It.Typ) loop | |
6850 | if Ekind (It.Typ) /= E_Access_Subprogram_Type then | |
6851 | Remove_Interp (I); | |
6852 | end if; | |
6853 | ||
6854 | Get_Next_Interp (I, It); | |
6855 | end loop; | |
6856 | ||
6857 | New_N := | |
6858 | Make_Function_Call (Loc, | |
6859 | Name => | |
6860 | Make_Explicit_Dereference (Loc, | |
6861 | Prefix => P), | |
6862 | Parameter_Associations => New_List); | |
6863 | ||
6864 | Save_Interps (N, New_N); | |
6865 | Rewrite (N, New_N); | |
6866 | Analyze_And_Resolve (N, Typ); | |
6867 | return; | |
6868 | end if; | |
6869 | ||
996ae0b0 RK |
6870 | Set_Etype (N, Designated_Type (It.Typ)); |
6871 | ||
6872 | else | |
fbf5a39b | 6873 | Resolve (P); |
996ae0b0 RK |
6874 | end if; |
6875 | ||
6876 | if Is_Access_Type (Etype (P)) then | |
6877 | Apply_Access_Check (N); | |
6878 | end if; | |
6879 | ||
758c442c GD |
6880 | -- If the designated type is a packed unconstrained array type, and the |
6881 | -- explicit dereference is not in the context of an attribute reference, | |
6882 | -- then we must compute and set the actual subtype, since it is needed | |
6883 | -- by Gigi. The reason we exclude the attribute case is that this is | |
6884 | -- handled fine by Gigi, and in fact we use such attributes to build the | |
6885 | -- actual subtype. We also exclude generated code (which builds actual | |
6886 | -- subtypes directly if they are needed). | |
996ae0b0 RK |
6887 | |
6888 | if Is_Array_Type (Etype (N)) | |
6889 | and then Is_Packed (Etype (N)) | |
6890 | and then not Is_Constrained (Etype (N)) | |
6891 | and then Nkind (Parent (N)) /= N_Attribute_Reference | |
6892 | and then Comes_From_Source (N) | |
6893 | then | |
6894 | Set_Etype (N, Get_Actual_Subtype (N)); | |
6895 | end if; | |
6896 | ||
09494c32 AC |
6897 | -- Note: No Eval processing is required for an explicit dereference, |
6898 | -- because such a name can never be static. | |
996ae0b0 RK |
6899 | |
6900 | end Resolve_Explicit_Dereference; | |
6901 | ||
955871d3 AC |
6902 | ------------------------------------- |
6903 | -- Resolve_Expression_With_Actions -- | |
6904 | ------------------------------------- | |
6905 | ||
6906 | procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is | |
6907 | begin | |
6908 | Set_Etype (N, Typ); | |
6909 | end Resolve_Expression_With_Actions; | |
6910 | ||
996ae0b0 RK |
6911 | ------------------------------- |
6912 | -- Resolve_Indexed_Component -- | |
6913 | ------------------------------- | |
6914 | ||
6915 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is | |
6916 | Name : constant Node_Id := Prefix (N); | |
6917 | Expr : Node_Id; | |
6918 | Array_Type : Entity_Id := Empty; -- to prevent junk warning | |
6919 | Index : Node_Id; | |
6920 | ||
6921 | begin | |
6922 | if Is_Overloaded (Name) then | |
6923 | ||
758c442c GD |
6924 | -- Use the context type to select the prefix that yields the correct |
6925 | -- component type. | |
996ae0b0 RK |
6926 | |
6927 | declare | |
6928 | I : Interp_Index; | |
6929 | It : Interp; | |
6930 | I1 : Interp_Index := 0; | |
6931 | P : constant Node_Id := Prefix (N); | |
6932 | Found : Boolean := False; | |
6933 | ||
6934 | begin | |
6935 | Get_First_Interp (P, I, It); | |
996ae0b0 | 6936 | while Present (It.Typ) loop |
996ae0b0 RK |
6937 | if (Is_Array_Type (It.Typ) |
6938 | and then Covers (Typ, Component_Type (It.Typ))) | |
6939 | or else (Is_Access_Type (It.Typ) | |
6940 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
19fb051c AC |
6941 | and then |
6942 | Covers | |
6943 | (Typ, | |
6944 | Component_Type (Designated_Type (It.Typ)))) | |
996ae0b0 RK |
6945 | then |
6946 | if Found then | |
6947 | It := Disambiguate (P, I1, I, Any_Type); | |
6948 | ||
6949 | if It = No_Interp then | |
6950 | Error_Msg_N ("ambiguous prefix for indexing", N); | |
6951 | Set_Etype (N, Typ); | |
6952 | return; | |
6953 | ||
6954 | else | |
6955 | Found := True; | |
6956 | Array_Type := It.Typ; | |
6957 | I1 := I; | |
6958 | end if; | |
6959 | ||
6960 | else | |
6961 | Found := True; | |
6962 | Array_Type := It.Typ; | |
6963 | I1 := I; | |
6964 | end if; | |
6965 | end if; | |
6966 | ||
6967 | Get_Next_Interp (I, It); | |
6968 | end loop; | |
6969 | end; | |
6970 | ||
6971 | else | |
6972 | Array_Type := Etype (Name); | |
6973 | end if; | |
6974 | ||
6975 | Resolve (Name, Array_Type); | |
6976 | Array_Type := Get_Actual_Subtype_If_Available (Name); | |
6977 | ||
6978 | -- If prefix is access type, dereference to get real array type. | |
6979 | -- Note: we do not apply an access check because the expander always | |
6980 | -- introduces an explicit dereference, and the check will happen there. | |
6981 | ||
6982 | if Is_Access_Type (Array_Type) then | |
6983 | Array_Type := Designated_Type (Array_Type); | |
6984 | end if; | |
6985 | ||
a77842bd | 6986 | -- If name was overloaded, set component type correctly now |
f3d57416 | 6987 | -- If a misplaced call to an entry family (which has no index types) |
b7d1f17f | 6988 | -- return. Error will be diagnosed from calling context. |
996ae0b0 | 6989 | |
b7d1f17f HK |
6990 | if Is_Array_Type (Array_Type) then |
6991 | Set_Etype (N, Component_Type (Array_Type)); | |
6992 | else | |
6993 | return; | |
6994 | end if; | |
996ae0b0 RK |
6995 | |
6996 | Index := First_Index (Array_Type); | |
6997 | Expr := First (Expressions (N)); | |
6998 | ||
758c442c GD |
6999 | -- The prefix may have resolved to a string literal, in which case its |
7000 | -- etype has a special representation. This is only possible currently | |
7001 | -- if the prefix is a static concatenation, written in functional | |
7002 | -- notation. | |
996ae0b0 RK |
7003 | |
7004 | if Ekind (Array_Type) = E_String_Literal_Subtype then | |
7005 | Resolve (Expr, Standard_Positive); | |
7006 | ||
7007 | else | |
7008 | while Present (Index) and Present (Expr) loop | |
7009 | Resolve (Expr, Etype (Index)); | |
7010 | Check_Unset_Reference (Expr); | |
7011 | ||
7012 | if Is_Scalar_Type (Etype (Expr)) then | |
7013 | Apply_Scalar_Range_Check (Expr, Etype (Index)); | |
7014 | else | |
7015 | Apply_Range_Check (Expr, Get_Actual_Subtype (Index)); | |
7016 | end if; | |
7017 | ||
7018 | Next_Index (Index); | |
7019 | Next (Expr); | |
7020 | end loop; | |
7021 | end if; | |
7022 | ||
0669bebe GB |
7023 | -- Do not generate the warning on suspicious index if we are analyzing |
7024 | -- package Ada.Tags; otherwise we will report the warning with the | |
7025 | -- Prims_Ptr field of the dispatch table. | |
7026 | ||
7027 | if Scope (Etype (Prefix (N))) = Standard_Standard | |
7028 | or else not | |
7029 | Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))), | |
7030 | Ada_Tags) | |
7031 | then | |
7032 | Warn_On_Suspicious_Index (Name, First (Expressions (N))); | |
7033 | Eval_Indexed_Component (N); | |
7034 | end if; | |
c28408b7 RD |
7035 | |
7036 | -- If the array type is atomic, and is packed, and we are in a left side | |
7037 | -- context, then this is worth a warning, since we have a situation | |
7038 | -- where the access to the component may cause extra read/writes of | |
7039 | -- the atomic array object, which could be considered unexpected. | |
7040 | ||
7041 | if Nkind (N) = N_Indexed_Component | |
7042 | and then (Is_Atomic (Array_Type) | |
7043 | or else (Is_Entity_Name (Prefix (N)) | |
7044 | and then Is_Atomic (Entity (Prefix (N))))) | |
7045 | and then Is_Bit_Packed_Array (Array_Type) | |
7046 | and then Is_LHS (N) | |
7047 | then | |
7048 | Error_Msg_N ("?assignment to component of packed atomic array", | |
7049 | Prefix (N)); | |
7050 | Error_Msg_N ("?\may cause unexpected accesses to atomic object", | |
7051 | Prefix (N)); | |
7052 | end if; | |
996ae0b0 RK |
7053 | end Resolve_Indexed_Component; |
7054 | ||
7055 | ----------------------------- | |
7056 | -- Resolve_Integer_Literal -- | |
7057 | ----------------------------- | |
7058 | ||
7059 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is | |
7060 | begin | |
7061 | Set_Etype (N, Typ); | |
7062 | Eval_Integer_Literal (N); | |
7063 | end Resolve_Integer_Literal; | |
7064 | ||
15ce9ca2 AC |
7065 | -------------------------------- |
7066 | -- Resolve_Intrinsic_Operator -- | |
7067 | -------------------------------- | |
996ae0b0 RK |
7068 | |
7069 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is | |
bb481772 AC |
7070 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); |
7071 | Op : Entity_Id; | |
7072 | Orig_Op : constant Entity_Id := Entity (N); | |
7073 | Arg1 : Node_Id; | |
7074 | Arg2 : Node_Id; | |
996ae0b0 RK |
7075 | |
7076 | begin | |
305caf42 AC |
7077 | -- We must preserve the original entity in a generic setting, so that |
7078 | -- the legality of the operation can be verified in an instance. | |
7079 | ||
7080 | if not Expander_Active then | |
7081 | return; | |
7082 | end if; | |
7083 | ||
996ae0b0 | 7084 | Op := Entity (N); |
996ae0b0 RK |
7085 | while Scope (Op) /= Standard_Standard loop |
7086 | Op := Homonym (Op); | |
7087 | pragma Assert (Present (Op)); | |
7088 | end loop; | |
7089 | ||
7090 | Set_Entity (N, Op); | |
af152989 | 7091 | Set_Is_Overloaded (N, False); |
996ae0b0 | 7092 | |
758c442c GD |
7093 | -- If the operand type is private, rewrite with suitable conversions on |
7094 | -- the operands and the result, to expose the proper underlying numeric | |
7095 | -- type. | |
996ae0b0 | 7096 | |
fbf5a39b AC |
7097 | if Is_Private_Type (Typ) then |
7098 | Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N)); | |
7099 | ||
7100 | if Nkind (N) = N_Op_Expon then | |
7101 | Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N)); | |
7102 | else | |
7103 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
7104 | end if; | |
7105 | ||
bb481772 AC |
7106 | if Nkind (Arg1) = N_Type_Conversion then |
7107 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
7108 | end if; | |
7109 | ||
7110 | if Nkind (Arg2) = N_Type_Conversion then | |
7111 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
7112 | end if; | |
996ae0b0 | 7113 | |
fbf5a39b AC |
7114 | Set_Left_Opnd (N, Arg1); |
7115 | Set_Right_Opnd (N, Arg2); | |
7116 | ||
7117 | Set_Etype (N, Btyp); | |
7118 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7119 | Resolve (N, Typ); | |
7120 | ||
7121 | elsif Typ /= Etype (Left_Opnd (N)) | |
7122 | or else Typ /= Etype (Right_Opnd (N)) | |
7123 | then | |
d81b4bfe | 7124 | -- Add explicit conversion where needed, and save interpretations in |
bb481772 AC |
7125 | -- case operands are overloaded. If the context is a VMS operation, |
7126 | -- assert that the conversion is legal (the operands have the proper | |
7127 | -- types to select the VMS intrinsic). Note that in rare cases the | |
7128 | -- VMS operators may be visible, but the default System is being used | |
7129 | -- and Address is a private type. | |
fbf5a39b | 7130 | |
af152989 | 7131 | Arg1 := Convert_To (Typ, Left_Opnd (N)); |
fbf5a39b AC |
7132 | Arg2 := Convert_To (Typ, Right_Opnd (N)); |
7133 | ||
7134 | if Nkind (Arg1) = N_Type_Conversion then | |
7135 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
bb481772 AC |
7136 | |
7137 | if Is_VMS_Operator (Orig_Op) then | |
7138 | Set_Conversion_OK (Arg1); | |
7139 | end if; | |
af152989 AC |
7140 | else |
7141 | Save_Interps (Left_Opnd (N), Arg1); | |
fbf5a39b AC |
7142 | end if; |
7143 | ||
7144 | if Nkind (Arg2) = N_Type_Conversion then | |
7145 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
bb481772 AC |
7146 | |
7147 | if Is_VMS_Operator (Orig_Op) then | |
7148 | Set_Conversion_OK (Arg2); | |
7149 | end if; | |
af152989 | 7150 | else |
0ab80019 | 7151 | Save_Interps (Right_Opnd (N), Arg2); |
fbf5a39b AC |
7152 | end if; |
7153 | ||
7154 | Rewrite (Left_Opnd (N), Arg1); | |
7155 | Rewrite (Right_Opnd (N), Arg2); | |
7156 | Analyze (Arg1); | |
7157 | Analyze (Arg2); | |
7158 | Resolve_Arithmetic_Op (N, Typ); | |
7159 | ||
7160 | else | |
7161 | Resolve_Arithmetic_Op (N, Typ); | |
7162 | end if; | |
996ae0b0 RK |
7163 | end Resolve_Intrinsic_Operator; |
7164 | ||
fbf5a39b AC |
7165 | -------------------------------------- |
7166 | -- Resolve_Intrinsic_Unary_Operator -- | |
7167 | -------------------------------------- | |
7168 | ||
7169 | procedure Resolve_Intrinsic_Unary_Operator | |
7170 | (N : Node_Id; | |
7171 | Typ : Entity_Id) | |
7172 | is | |
7173 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); | |
7174 | Op : Entity_Id; | |
7175 | Arg2 : Node_Id; | |
7176 | ||
7177 | begin | |
7178 | Op := Entity (N); | |
fbf5a39b AC |
7179 | while Scope (Op) /= Standard_Standard loop |
7180 | Op := Homonym (Op); | |
7181 | pragma Assert (Present (Op)); | |
7182 | end loop; | |
7183 | ||
7184 | Set_Entity (N, Op); | |
7185 | ||
7186 | if Is_Private_Type (Typ) then | |
7187 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
7188 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
7189 | ||
7190 | Set_Right_Opnd (N, Arg2); | |
7191 | ||
7192 | Set_Etype (N, Btyp); | |
7193 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7194 | Resolve (N, Typ); | |
7195 | ||
7196 | else | |
7197 | Resolve_Unary_Op (N, Typ); | |
7198 | end if; | |
7199 | end Resolve_Intrinsic_Unary_Operator; | |
7200 | ||
996ae0b0 RK |
7201 | ------------------------ |
7202 | -- Resolve_Logical_Op -- | |
7203 | ------------------------ | |
7204 | ||
7205 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is | |
7206 | B_Typ : Entity_Id; | |
7207 | ||
7208 | begin | |
f61580d4 AC |
7209 | Check_No_Direct_Boolean_Operators (N); |
7210 | ||
758c442c GD |
7211 | -- Predefined operations on scalar types yield the base type. On the |
7212 | -- other hand, logical operations on arrays yield the type of the | |
7213 | -- arguments (and the context). | |
996ae0b0 RK |
7214 | |
7215 | if Is_Array_Type (Typ) then | |
7216 | B_Typ := Typ; | |
7217 | else | |
7218 | B_Typ := Base_Type (Typ); | |
7219 | end if; | |
7220 | ||
001c7783 AC |
7221 | -- OK if this is a VMS-specific intrinsic operation |
7222 | ||
7223 | if Is_VMS_Operator (Entity (N)) then | |
7224 | null; | |
7225 | ||
996ae0b0 RK |
7226 | -- The following test is required because the operands of the operation |
7227 | -- may be literals, in which case the resulting type appears to be | |
7228 | -- compatible with a signed integer type, when in fact it is compatible | |
7229 | -- only with modular types. If the context itself is universal, the | |
7230 | -- operation is illegal. | |
7231 | ||
001c7783 | 7232 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
7233 | Error_Msg_N ("invalid context for logical operation", N); |
7234 | Set_Etype (N, Any_Type); | |
7235 | return; | |
7236 | ||
7237 | elsif Typ = Any_Modular then | |
7238 | Error_Msg_N | |
7239 | ("no modular type available in this context", N); | |
7240 | Set_Etype (N, Any_Type); | |
7241 | return; | |
19fb051c | 7242 | |
07fc65c4 GB |
7243 | elsif Is_Modular_Integer_Type (Typ) |
7244 | and then Etype (Left_Opnd (N)) = Universal_Integer | |
7245 | and then Etype (Right_Opnd (N)) = Universal_Integer | |
7246 | then | |
7247 | Check_For_Visible_Operator (N, B_Typ); | |
996ae0b0 RK |
7248 | end if; |
7249 | ||
7250 | Resolve (Left_Opnd (N), B_Typ); | |
7251 | Resolve (Right_Opnd (N), B_Typ); | |
7252 | ||
7253 | Check_Unset_Reference (Left_Opnd (N)); | |
7254 | Check_Unset_Reference (Right_Opnd (N)); | |
7255 | ||
7256 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7257 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 | 7258 | Eval_Logical_Op (N); |
9f90d123 AC |
7259 | |
7260 | -- In SPARK or ALFA, logical operations AND, OR and XOR for arrays are | |
7261 | -- defined only when both operands have same static lower and higher | |
19fb051c AC |
7262 | -- bounds. Of course the types have to match, so only check if operands |
7263 | -- are compatible and the node itself has no errors. | |
9f90d123 | 7264 | |
fe5d3068 | 7265 | if Is_Array_Type (B_Typ) |
19fb051c AC |
7266 | and then Nkind (N) in N_Binary_Op |
7267 | and then | |
7268 | Base_Type (Etype (Left_Opnd (N))) | |
7269 | = Base_Type (Etype (Right_Opnd (N))) | |
b0186f71 AC |
7270 | and then not Matching_Static_Array_Bounds (Etype (Left_Opnd (N)), |
7271 | Etype (Right_Opnd (N))) | |
9f90d123 | 7272 | then |
fe5d3068 YM |
7273 | Check_Formal_Restriction |
7274 | ("array types should have matching static bounds", N); | |
9f90d123 AC |
7275 | end if; |
7276 | ||
996ae0b0 RK |
7277 | end Resolve_Logical_Op; |
7278 | ||
7279 | --------------------------- | |
7280 | -- Resolve_Membership_Op -- | |
7281 | --------------------------- | |
7282 | ||
5cc9353d RD |
7283 | -- The context can only be a boolean type, and does not determine the |
7284 | -- arguments. Arguments should be unambiguous, but the preference rule for | |
7285 | -- universal types applies. | |
996ae0b0 RK |
7286 | |
7287 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7288 | pragma Warnings (Off, Typ); |
7289 | ||
197e4514 | 7290 | L : constant Node_Id := Left_Opnd (N); |
b1c11e0e | 7291 | R : constant Node_Id := Right_Opnd (N); |
996ae0b0 RK |
7292 | T : Entity_Id; |
7293 | ||
197e4514 | 7294 | procedure Resolve_Set_Membership; |
5cc9353d RD |
7295 | -- Analysis has determined a unique type for the left operand. Use it to |
7296 | -- resolve the disjuncts. | |
197e4514 AC |
7297 | |
7298 | ---------------------------- | |
7299 | -- Resolve_Set_Membership -- | |
7300 | ---------------------------- | |
7301 | ||
7302 | procedure Resolve_Set_Membership is | |
7303 | Alt : Node_Id; | |
7304 | ||
7305 | begin | |
7306 | Resolve (L, Etype (L)); | |
7307 | ||
7308 | Alt := First (Alternatives (N)); | |
7309 | while Present (Alt) loop | |
7310 | ||
7311 | -- Alternative is an expression, a range | |
7312 | -- or a subtype mark. | |
7313 | ||
7314 | if not Is_Entity_Name (Alt) | |
7315 | or else not Is_Type (Entity (Alt)) | |
7316 | then | |
7317 | Resolve (Alt, Etype (L)); | |
7318 | end if; | |
7319 | ||
7320 | Next (Alt); | |
7321 | end loop; | |
7322 | end Resolve_Set_Membership; | |
7323 | ||
442c0581 | 7324 | -- Start of processing for Resolve_Membership_Op |
197e4514 | 7325 | |
996ae0b0 RK |
7326 | begin |
7327 | if L = Error or else R = Error then | |
7328 | return; | |
7329 | end if; | |
7330 | ||
197e4514 AC |
7331 | if Present (Alternatives (N)) then |
7332 | Resolve_Set_Membership; | |
7333 | return; | |
7334 | ||
7335 | elsif not Is_Overloaded (R) | |
996ae0b0 | 7336 | and then |
19fb051c AC |
7337 | (Etype (R) = Universal_Integer |
7338 | or else | |
996ae0b0 RK |
7339 | Etype (R) = Universal_Real) |
7340 | and then Is_Overloaded (L) | |
7341 | then | |
7342 | T := Etype (R); | |
1420b484 | 7343 | |
d81b4bfe | 7344 | -- Ada 2005 (AI-251): Support the following case: |
1420b484 JM |
7345 | |
7346 | -- type I is interface; | |
7347 | -- type T is tagged ... | |
7348 | ||
c8ef728f | 7349 | -- function Test (O : I'Class) is |
1420b484 JM |
7350 | -- begin |
7351 | -- return O in T'Class. | |
7352 | -- end Test; | |
7353 | ||
d81b4bfe | 7354 | -- In this case we have nothing else to do. The membership test will be |
e7c0dd39 | 7355 | -- done at run time. |
1420b484 | 7356 | |
0791fbe9 | 7357 | elsif Ada_Version >= Ada_2005 |
1420b484 JM |
7358 | and then Is_Class_Wide_Type (Etype (L)) |
7359 | and then Is_Interface (Etype (L)) | |
7360 | and then Is_Class_Wide_Type (Etype (R)) | |
7361 | and then not Is_Interface (Etype (R)) | |
7362 | then | |
7363 | return; | |
996ae0b0 RK |
7364 | else |
7365 | T := Intersect_Types (L, R); | |
7366 | end if; | |
7367 | ||
9a0ddeee AC |
7368 | -- If mixed-mode operations are present and operands are all literal, |
7369 | -- the only interpretation involves Duration, which is probably not | |
7370 | -- the intention of the programmer. | |
7371 | ||
7372 | if T = Any_Fixed then | |
7373 | T := Unique_Fixed_Point_Type (N); | |
7374 | ||
7375 | if T = Any_Type then | |
7376 | return; | |
7377 | end if; | |
7378 | end if; | |
7379 | ||
996ae0b0 RK |
7380 | Resolve (L, T); |
7381 | Check_Unset_Reference (L); | |
7382 | ||
7383 | if Nkind (R) = N_Range | |
7384 | and then not Is_Scalar_Type (T) | |
7385 | then | |
7386 | Error_Msg_N ("scalar type required for range", R); | |
7387 | end if; | |
7388 | ||
7389 | if Is_Entity_Name (R) then | |
7390 | Freeze_Expression (R); | |
7391 | else | |
7392 | Resolve (R, T); | |
7393 | Check_Unset_Reference (R); | |
7394 | end if; | |
7395 | ||
7396 | Eval_Membership_Op (N); | |
7397 | end Resolve_Membership_Op; | |
7398 | ||
7399 | ------------------ | |
7400 | -- Resolve_Null -- | |
7401 | ------------------ | |
7402 | ||
7403 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is | |
b1c11e0e JM |
7404 | Loc : constant Source_Ptr := Sloc (N); |
7405 | ||
996ae0b0 | 7406 | begin |
758c442c | 7407 | -- Handle restriction against anonymous null access values This |
6ba6b1e3 | 7408 | -- restriction can be turned off using -gnatdj. |
996ae0b0 | 7409 | |
0ab80019 | 7410 | -- Ada 2005 (AI-231): Remove restriction |
2820d220 | 7411 | |
0791fbe9 | 7412 | if Ada_Version < Ada_2005 |
2820d220 | 7413 | and then not Debug_Flag_J |
996ae0b0 RK |
7414 | and then Ekind (Typ) = E_Anonymous_Access_Type |
7415 | and then Comes_From_Source (N) | |
7416 | then | |
d81b4bfe TQ |
7417 | -- In the common case of a call which uses an explicitly null value |
7418 | -- for an access parameter, give specialized error message. | |
996ae0b0 | 7419 | |
45fc7ddb HK |
7420 | if Nkind_In (Parent (N), N_Procedure_Call_Statement, |
7421 | N_Function_Call) | |
996ae0b0 RK |
7422 | then |
7423 | Error_Msg_N | |
7424 | ("null is not allowed as argument for an access parameter", N); | |
7425 | ||
7426 | -- Standard message for all other cases (are there any?) | |
7427 | ||
7428 | else | |
7429 | Error_Msg_N | |
7430 | ("null cannot be of an anonymous access type", N); | |
7431 | end if; | |
7432 | end if; | |
7433 | ||
b1c11e0e JM |
7434 | -- Ada 2005 (AI-231): Generate the null-excluding check in case of |
7435 | -- assignment to a null-excluding object | |
7436 | ||
0791fbe9 | 7437 | if Ada_Version >= Ada_2005 |
b1c11e0e JM |
7438 | and then Can_Never_Be_Null (Typ) |
7439 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
7440 | then | |
7441 | if not Inside_Init_Proc then | |
7442 | Insert_Action | |
7443 | (Compile_Time_Constraint_Error (N, | |
7444 | "(Ada 2005) null not allowed in null-excluding objects?"), | |
7445 | Make_Raise_Constraint_Error (Loc, | |
7446 | Reason => CE_Access_Check_Failed)); | |
7447 | else | |
7448 | Insert_Action (N, | |
7449 | Make_Raise_Constraint_Error (Loc, | |
7450 | Reason => CE_Access_Check_Failed)); | |
7451 | end if; | |
7452 | end if; | |
7453 | ||
d81b4bfe TQ |
7454 | -- In a distributed context, null for a remote access to subprogram may |
7455 | -- need to be replaced with a special record aggregate. In this case, | |
7456 | -- return after having done the transformation. | |
996ae0b0 RK |
7457 | |
7458 | if (Ekind (Typ) = E_Record_Type | |
7459 | or else Is_Remote_Access_To_Subprogram_Type (Typ)) | |
7460 | and then Remote_AST_Null_Value (N, Typ) | |
7461 | then | |
7462 | return; | |
7463 | end if; | |
7464 | ||
a77842bd | 7465 | -- The null literal takes its type from the context |
996ae0b0 RK |
7466 | |
7467 | Set_Etype (N, Typ); | |
7468 | end Resolve_Null; | |
7469 | ||
7470 | ----------------------- | |
7471 | -- Resolve_Op_Concat -- | |
7472 | ----------------------- | |
7473 | ||
7474 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is | |
996ae0b0 | 7475 | |
10303118 BD |
7476 | -- We wish to avoid deep recursion, because concatenations are often |
7477 | -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left | |
7478 | -- operands nonrecursively until we find something that is not a simple | |
7479 | -- concatenation (A in this case). We resolve that, and then walk back | |
7480 | -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest | |
7481 | -- to do the rest of the work at each level. The Parent pointers allow | |
7482 | -- us to avoid recursion, and thus avoid running out of memory. See also | |
d81b4bfe | 7483 | -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used. |
996ae0b0 | 7484 | |
10303118 BD |
7485 | NN : Node_Id := N; |
7486 | Op1 : Node_Id; | |
996ae0b0 | 7487 | |
10303118 BD |
7488 | begin |
7489 | -- The following code is equivalent to: | |
996ae0b0 | 7490 | |
10303118 BD |
7491 | -- Resolve_Op_Concat_First (NN, Typ); |
7492 | -- Resolve_Op_Concat_Arg (N, ...); | |
7493 | -- Resolve_Op_Concat_Rest (N, Typ); | |
996ae0b0 | 7494 | |
10303118 BD |
7495 | -- where the Resolve_Op_Concat_Arg call recurses back here if the left |
7496 | -- operand is a concatenation. | |
996ae0b0 | 7497 | |
10303118 | 7498 | -- Walk down left operands |
996ae0b0 | 7499 | |
10303118 BD |
7500 | loop |
7501 | Resolve_Op_Concat_First (NN, Typ); | |
7502 | Op1 := Left_Opnd (NN); | |
7503 | exit when not (Nkind (Op1) = N_Op_Concat | |
7504 | and then not Is_Array_Type (Component_Type (Typ)) | |
7505 | and then Entity (Op1) = Entity (NN)); | |
7506 | NN := Op1; | |
7507 | end loop; | |
996ae0b0 | 7508 | |
10303118 | 7509 | -- Now (given the above example) NN is A&B and Op1 is A |
996ae0b0 | 7510 | |
10303118 | 7511 | -- First resolve Op1 ... |
9ebe3743 | 7512 | |
10303118 | 7513 | Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN)); |
9ebe3743 | 7514 | |
10303118 BD |
7515 | -- ... then walk NN back up until we reach N (where we started), calling |
7516 | -- Resolve_Op_Concat_Rest along the way. | |
9ebe3743 | 7517 | |
10303118 BD |
7518 | loop |
7519 | Resolve_Op_Concat_Rest (NN, Typ); | |
7520 | exit when NN = N; | |
7521 | NN := Parent (NN); | |
7522 | end loop; | |
2933b16c | 7523 | |
fe5d3068 YM |
7524 | if Base_Type (Etype (N)) /= Standard_String then |
7525 | Check_Formal_Restriction | |
7526 | ("result of concatenation should have type String", N); | |
2933b16c | 7527 | end if; |
10303118 | 7528 | end Resolve_Op_Concat; |
9ebe3743 | 7529 | |
10303118 BD |
7530 | --------------------------- |
7531 | -- Resolve_Op_Concat_Arg -- | |
7532 | --------------------------- | |
996ae0b0 | 7533 | |
10303118 BD |
7534 | procedure Resolve_Op_Concat_Arg |
7535 | (N : Node_Id; | |
7536 | Arg : Node_Id; | |
7537 | Typ : Entity_Id; | |
7538 | Is_Comp : Boolean) | |
7539 | is | |
7540 | Btyp : constant Entity_Id := Base_Type (Typ); | |
996ae0b0 | 7541 | |
10303118 BD |
7542 | begin |
7543 | if In_Instance then | |
7544 | if Is_Comp | |
7545 | or else (not Is_Overloaded (Arg) | |
7546 | and then Etype (Arg) /= Any_Composite | |
7547 | and then Covers (Component_Type (Typ), Etype (Arg))) | |
7548 | then | |
7549 | Resolve (Arg, Component_Type (Typ)); | |
7550 | else | |
7551 | Resolve (Arg, Btyp); | |
7552 | end if; | |
fbf5a39b | 7553 | |
10303118 BD |
7554 | elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then |
7555 | if Nkind (Arg) = N_Aggregate | |
7556 | and then Is_Composite_Type (Component_Type (Typ)) | |
7557 | then | |
7558 | if Is_Private_Type (Component_Type (Typ)) then | |
7559 | Resolve (Arg, Btyp); | |
7560 | else | |
7561 | Error_Msg_N ("ambiguous aggregate must be qualified", Arg); | |
7562 | Set_Etype (Arg, Any_Type); | |
996ae0b0 RK |
7563 | end if; |
7564 | ||
7565 | else | |
10303118 BD |
7566 | if Is_Overloaded (Arg) |
7567 | and then Has_Compatible_Type (Arg, Typ) | |
7568 | and then Etype (Arg) /= Any_Type | |
7569 | then | |
7570 | declare | |
7571 | I : Interp_Index; | |
7572 | It : Interp; | |
7573 | Func : Entity_Id; | |
7574 | ||
7575 | begin | |
7576 | Get_First_Interp (Arg, I, It); | |
7577 | Func := It.Nam; | |
7578 | Get_Next_Interp (I, It); | |
7579 | ||
7580 | -- Special-case the error message when the overloading is | |
7581 | -- caused by a function that yields an array and can be | |
7582 | -- called without parameters. | |
7583 | ||
7584 | if It.Nam = Func then | |
7585 | Error_Msg_Sloc := Sloc (Func); | |
7586 | Error_Msg_N ("ambiguous call to function#", Arg); | |
7587 | Error_Msg_NE | |
7588 | ("\\interpretation as call yields&", Arg, Typ); | |
7589 | Error_Msg_NE | |
7590 | ("\\interpretation as indexing of call yields&", | |
7591 | Arg, Component_Type (Typ)); | |
7592 | ||
7593 | else | |
7594 | Error_Msg_N | |
7595 | ("ambiguous operand for concatenation!", Arg); | |
19fb051c | 7596 | |
10303118 BD |
7597 | Get_First_Interp (Arg, I, It); |
7598 | while Present (It.Nam) loop | |
7599 | Error_Msg_Sloc := Sloc (It.Nam); | |
7600 | ||
7601 | if Base_Type (It.Typ) = Base_Type (Typ) | |
7602 | or else Base_Type (It.Typ) = | |
19fb051c | 7603 | Base_Type (Component_Type (Typ)) |
10303118 | 7604 | then |
4e7a4f6e AC |
7605 | Error_Msg_N -- CODEFIX |
7606 | ("\\possible interpretation#", Arg); | |
10303118 BD |
7607 | end if; |
7608 | ||
7609 | Get_Next_Interp (I, It); | |
7610 | end loop; | |
7611 | end if; | |
7612 | end; | |
7613 | end if; | |
7614 | ||
7615 | Resolve (Arg, Component_Type (Typ)); | |
7616 | ||
7617 | if Nkind (Arg) = N_String_Literal then | |
7618 | Set_Etype (Arg, Component_Type (Typ)); | |
7619 | end if; | |
7620 | ||
7621 | if Arg = Left_Opnd (N) then | |
7622 | Set_Is_Component_Left_Opnd (N); | |
7623 | else | |
7624 | Set_Is_Component_Right_Opnd (N); | |
7625 | end if; | |
996ae0b0 RK |
7626 | end if; |
7627 | ||
10303118 BD |
7628 | else |
7629 | Resolve (Arg, Btyp); | |
7630 | end if; | |
7631 | ||
2933b16c RD |
7632 | -- Concatenation is restricted in SPARK or ALFA: each operand must be |
7633 | -- either a string literal, a static character expression, or another | |
7634 | -- concatenation. Arg cannot be a concatenation here as callers of | |
7635 | -- Resolve_Op_Concat_Arg call it separately on each final operand, past | |
7636 | -- concatenation operations. | |
7637 | ||
fe5d3068 YM |
7638 | if Is_Character_Type (Etype (Arg)) then |
7639 | if not Is_Static_Expression (Arg) then | |
7640 | Check_Formal_Restriction | |
7641 | ("character operand for concatenation should be static", N); | |
7642 | end if; | |
2933b16c | 7643 | |
fe5d3068 YM |
7644 | elsif Is_String_Type (Etype (Arg)) then |
7645 | if Nkind (Arg) /= N_String_Literal then | |
7646 | Check_Formal_Restriction | |
7647 | ("string operand for concatenation should be a literal", N); | |
7648 | end if; | |
2933b16c | 7649 | |
b9e48541 AC |
7650 | -- Do not issue error on an operand that is neither a character nor a |
7651 | -- string, as the error is issued in Resolve_Op_Concat. | |
2933b16c | 7652 | |
fe5d3068 YM |
7653 | else |
7654 | null; | |
2933b16c RD |
7655 | end if; |
7656 | ||
10303118 BD |
7657 | Check_Unset_Reference (Arg); |
7658 | end Resolve_Op_Concat_Arg; | |
996ae0b0 | 7659 | |
10303118 BD |
7660 | ----------------------------- |
7661 | -- Resolve_Op_Concat_First -- | |
7662 | ----------------------------- | |
7663 | ||
7664 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is | |
7665 | Btyp : constant Entity_Id := Base_Type (Typ); | |
7666 | Op1 : constant Node_Id := Left_Opnd (N); | |
7667 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 RK |
7668 | |
7669 | begin | |
dae2b8ea HK |
7670 | -- The parser folds an enormous sequence of concatenations of string |
7671 | -- literals into "" & "...", where the Is_Folded_In_Parser flag is set | |
4fc26524 | 7672 | -- in the right operand. If the expression resolves to a predefined "&" |
dae2b8ea HK |
7673 | -- operator, all is well. Otherwise, the parser's folding is wrong, so |
7674 | -- we give an error. See P_Simple_Expression in Par.Ch4. | |
7675 | ||
7676 | if Nkind (Op2) = N_String_Literal | |
7677 | and then Is_Folded_In_Parser (Op2) | |
7678 | and then Ekind (Entity (N)) = E_Function | |
7679 | then | |
7680 | pragma Assert (Nkind (Op1) = N_String_Literal -- should be "" | |
7681 | and then String_Length (Strval (Op1)) = 0); | |
7682 | Error_Msg_N ("too many user-defined concatenations", N); | |
7683 | return; | |
7684 | end if; | |
7685 | ||
996ae0b0 RK |
7686 | Set_Etype (N, Btyp); |
7687 | ||
7688 | if Is_Limited_Composite (Btyp) then | |
7689 | Error_Msg_N ("concatenation not available for limited array", N); | |
fbf5a39b | 7690 | Explain_Limited_Type (Btyp, N); |
996ae0b0 | 7691 | end if; |
10303118 | 7692 | end Resolve_Op_Concat_First; |
996ae0b0 | 7693 | |
10303118 BD |
7694 | ---------------------------- |
7695 | -- Resolve_Op_Concat_Rest -- | |
7696 | ---------------------------- | |
996ae0b0 | 7697 | |
10303118 BD |
7698 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is |
7699 | Op1 : constant Node_Id := Left_Opnd (N); | |
7700 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 | 7701 | |
10303118 BD |
7702 | begin |
7703 | Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N)); | |
996ae0b0 | 7704 | |
fbf5a39b | 7705 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
7706 | |
7707 | if Is_String_Type (Typ) then | |
7708 | Eval_Concatenation (N); | |
7709 | end if; | |
7710 | ||
d81b4bfe TQ |
7711 | -- If this is not a static concatenation, but the result is a string |
7712 | -- type (and not an array of strings) ensure that static string operands | |
7713 | -- have their subtypes properly constructed. | |
996ae0b0 RK |
7714 | |
7715 | if Nkind (N) /= N_String_Literal | |
7716 | and then Is_Character_Type (Component_Type (Typ)) | |
7717 | then | |
7718 | Set_String_Literal_Subtype (Op1, Typ); | |
7719 | Set_String_Literal_Subtype (Op2, Typ); | |
7720 | end if; | |
10303118 | 7721 | end Resolve_Op_Concat_Rest; |
996ae0b0 RK |
7722 | |
7723 | ---------------------- | |
7724 | -- Resolve_Op_Expon -- | |
7725 | ---------------------- | |
7726 | ||
7727 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is | |
7728 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
7729 | ||
7730 | begin | |
f3d57416 | 7731 | -- Catch attempts to do fixed-point exponentiation with universal |
758c442c GD |
7732 | -- operands, which is a case where the illegality is not caught during |
7733 | -- normal operator analysis. | |
996ae0b0 RK |
7734 | |
7735 | if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then | |
7736 | Error_Msg_N ("exponentiation not available for fixed point", N); | |
7737 | return; | |
7738 | end if; | |
7739 | ||
fbf5a39b AC |
7740 | if Comes_From_Source (N) |
7741 | and then Ekind (Entity (N)) = E_Function | |
7742 | and then Is_Imported (Entity (N)) | |
7743 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
7744 | then | |
7745 | Resolve_Intrinsic_Operator (N, Typ); | |
7746 | return; | |
7747 | end if; | |
7748 | ||
996ae0b0 RK |
7749 | if Etype (Left_Opnd (N)) = Universal_Integer |
7750 | or else Etype (Left_Opnd (N)) = Universal_Real | |
7751 | then | |
7752 | Check_For_Visible_Operator (N, B_Typ); | |
7753 | end if; | |
7754 | ||
7755 | -- We do the resolution using the base type, because intermediate values | |
7756 | -- in expressions always are of the base type, not a subtype of it. | |
7757 | ||
7758 | Resolve (Left_Opnd (N), B_Typ); | |
7759 | Resolve (Right_Opnd (N), Standard_Integer); | |
7760 | ||
7761 | Check_Unset_Reference (Left_Opnd (N)); | |
7762 | Check_Unset_Reference (Right_Opnd (N)); | |
7763 | ||
7764 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7765 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7766 | Eval_Op_Expon (N); |
7767 | ||
7768 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
7769 | -- and perhaps the Resolve routines should be separated for the various | |
7770 | -- arithmetic operations, since they will need different processing. ??? | |
7771 | ||
7772 | if Nkind (N) in N_Op then | |
7773 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 7774 | Enable_Overflow_Check (N); |
996ae0b0 RK |
7775 | end if; |
7776 | end if; | |
996ae0b0 RK |
7777 | end Resolve_Op_Expon; |
7778 | ||
7779 | -------------------- | |
7780 | -- Resolve_Op_Not -- | |
7781 | -------------------- | |
7782 | ||
7783 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is | |
7784 | B_Typ : Entity_Id; | |
7785 | ||
7786 | function Parent_Is_Boolean return Boolean; | |
5cc9353d RD |
7787 | -- This function determines if the parent node is a boolean operator or |
7788 | -- operation (comparison op, membership test, or short circuit form) and | |
7789 | -- the not in question is the left operand of this operation. Note that | |
7790 | -- if the not is in parens, then false is returned. | |
996ae0b0 | 7791 | |
aa180613 RD |
7792 | ----------------------- |
7793 | -- Parent_Is_Boolean -- | |
7794 | ----------------------- | |
7795 | ||
996ae0b0 RK |
7796 | function Parent_Is_Boolean return Boolean is |
7797 | begin | |
7798 | if Paren_Count (N) /= 0 then | |
7799 | return False; | |
7800 | ||
7801 | else | |
7802 | case Nkind (Parent (N)) is | |
7803 | when N_Op_And | | |
7804 | N_Op_Eq | | |
7805 | N_Op_Ge | | |
7806 | N_Op_Gt | | |
7807 | N_Op_Le | | |
7808 | N_Op_Lt | | |
7809 | N_Op_Ne | | |
7810 | N_Op_Or | | |
7811 | N_Op_Xor | | |
7812 | N_In | | |
7813 | N_Not_In | | |
7814 | N_And_Then | | |
aa180613 | 7815 | N_Or_Else => |
996ae0b0 RK |
7816 | |
7817 | return Left_Opnd (Parent (N)) = N; | |
7818 | ||
7819 | when others => | |
7820 | return False; | |
7821 | end case; | |
7822 | end if; | |
7823 | end Parent_Is_Boolean; | |
7824 | ||
7825 | -- Start of processing for Resolve_Op_Not | |
7826 | ||
7827 | begin | |
758c442c GD |
7828 | -- Predefined operations on scalar types yield the base type. On the |
7829 | -- other hand, logical operations on arrays yield the type of the | |
7830 | -- arguments (and the context). | |
996ae0b0 RK |
7831 | |
7832 | if Is_Array_Type (Typ) then | |
7833 | B_Typ := Typ; | |
7834 | else | |
7835 | B_Typ := Base_Type (Typ); | |
7836 | end if; | |
7837 | ||
001c7783 AC |
7838 | if Is_VMS_Operator (Entity (N)) then |
7839 | null; | |
7840 | ||
f3d57416 | 7841 | -- Straightforward case of incorrect arguments |
aa180613 | 7842 | |
001c7783 | 7843 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
7844 | Error_Msg_N ("invalid operand type for operator&", N); |
7845 | Set_Etype (N, Any_Type); | |
7846 | return; | |
7847 | ||
aa180613 RD |
7848 | -- Special case of probable missing parens |
7849 | ||
fbf5a39b | 7850 | elsif Typ = Universal_Integer or else Typ = Any_Modular then |
996ae0b0 | 7851 | if Parent_Is_Boolean then |
ed2233dc | 7852 | Error_Msg_N |
996ae0b0 RK |
7853 | ("operand of not must be enclosed in parentheses", |
7854 | Right_Opnd (N)); | |
7855 | else | |
7856 | Error_Msg_N | |
7857 | ("no modular type available in this context", N); | |
7858 | end if; | |
7859 | ||
7860 | Set_Etype (N, Any_Type); | |
7861 | return; | |
7862 | ||
5cc9353d | 7863 | -- OK resolution of NOT |
aa180613 | 7864 | |
996ae0b0 | 7865 | else |
aa180613 RD |
7866 | -- Warn if non-boolean types involved. This is a case like not a < b |
7867 | -- where a and b are modular, where we will get (not a) < b and most | |
7868 | -- likely not (a < b) was intended. | |
7869 | ||
7870 | if Warn_On_Questionable_Missing_Parens | |
7871 | and then not Is_Boolean_Type (Typ) | |
996ae0b0 RK |
7872 | and then Parent_Is_Boolean |
7873 | then | |
ed2233dc | 7874 | Error_Msg_N ("?not expression should be parenthesized here!", N); |
996ae0b0 RK |
7875 | end if; |
7876 | ||
09bc9ab6 RD |
7877 | -- Warn on double negation if checking redundant constructs |
7878 | ||
7879 | if Warn_On_Redundant_Constructs | |
7880 | and then Comes_From_Source (N) | |
7881 | and then Comes_From_Source (Right_Opnd (N)) | |
7882 | and then Root_Type (Typ) = Standard_Boolean | |
7883 | and then Nkind (Right_Opnd (N)) = N_Op_Not | |
7884 | then | |
ed2233dc | 7885 | Error_Msg_N ("redundant double negation?", N); |
09bc9ab6 RD |
7886 | end if; |
7887 | ||
7888 | -- Complete resolution and evaluation of NOT | |
7889 | ||
996ae0b0 RK |
7890 | Resolve (Right_Opnd (N), B_Typ); |
7891 | Check_Unset_Reference (Right_Opnd (N)); | |
7892 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7893 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7894 | Eval_Op_Not (N); |
7895 | end if; | |
7896 | end Resolve_Op_Not; | |
7897 | ||
7898 | ----------------------------- | |
7899 | -- Resolve_Operator_Symbol -- | |
7900 | ----------------------------- | |
7901 | ||
7902 | -- Nothing to be done, all resolved already | |
7903 | ||
7904 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7905 | pragma Warnings (Off, N); |
7906 | pragma Warnings (Off, Typ); | |
7907 | ||
996ae0b0 RK |
7908 | begin |
7909 | null; | |
7910 | end Resolve_Operator_Symbol; | |
7911 | ||
7912 | ---------------------------------- | |
7913 | -- Resolve_Qualified_Expression -- | |
7914 | ---------------------------------- | |
7915 | ||
7916 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7917 | pragma Warnings (Off, Typ); |
7918 | ||
996ae0b0 RK |
7919 | Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N)); |
7920 | Expr : constant Node_Id := Expression (N); | |
7921 | ||
7922 | begin | |
7923 | Resolve (Expr, Target_Typ); | |
7924 | ||
fe5d3068 | 7925 | if Is_Array_Type (Target_Typ) |
b0186f71 AC |
7926 | and then Is_Array_Type (Etype (Expr)) |
7927 | and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr)) | |
7928 | then | |
fe5d3068 YM |
7929 | Check_Formal_Restriction |
7930 | ("array types should have matching static bounds", N); | |
b0186f71 AC |
7931 | end if; |
7932 | ||
5cc9353d RD |
7933 | -- A qualified expression requires an exact match of the type, class- |
7934 | -- wide matching is not allowed. However, if the qualifying type is | |
7935 | -- specific and the expression has a class-wide type, it may still be | |
7936 | -- okay, since it can be the result of the expansion of a call to a | |
7937 | -- dispatching function, so we also have to check class-wideness of the | |
7938 | -- type of the expression's original node. | |
1420b484 JM |
7939 | |
7940 | if (Is_Class_Wide_Type (Target_Typ) | |
7941 | or else | |
7942 | (Is_Class_Wide_Type (Etype (Expr)) | |
7943 | and then Is_Class_Wide_Type (Etype (Original_Node (Expr))))) | |
996ae0b0 RK |
7944 | and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ) |
7945 | then | |
7946 | Wrong_Type (Expr, Target_Typ); | |
7947 | end if; | |
7948 | ||
90c63b09 AC |
7949 | -- If the target type is unconstrained, then we reset the type of the |
7950 | -- result from the type of the expression. For other cases, the actual | |
7951 | -- subtype of the expression is the target type. | |
996ae0b0 RK |
7952 | |
7953 | if Is_Composite_Type (Target_Typ) | |
7954 | and then not Is_Constrained (Target_Typ) | |
7955 | then | |
7956 | Set_Etype (N, Etype (Expr)); | |
7957 | end if; | |
7958 | ||
7959 | Eval_Qualified_Expression (N); | |
7960 | end Resolve_Qualified_Expression; | |
7961 | ||
a961aa79 AC |
7962 | ----------------------------------- |
7963 | -- Resolve_Quantified_Expression -- | |
7964 | ----------------------------------- | |
7965 | ||
7966 | procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is | |
7967 | begin | |
7968 | -- The loop structure is already resolved during its analysis, only the | |
0592046e AC |
7969 | -- resolution of the condition needs to be done. Expansion is disabled |
7970 | -- so that checks and other generated code are inserted in the tree | |
7971 | -- after expression has been rewritten as a loop. | |
a961aa79 | 7972 | |
0592046e | 7973 | Expander_Mode_Save_And_Set (False); |
a961aa79 | 7974 | Resolve (Condition (N), Typ); |
0592046e | 7975 | Expander_Mode_Restore; |
a961aa79 AC |
7976 | end Resolve_Quantified_Expression; |
7977 | ||
996ae0b0 RK |
7978 | ------------------- |
7979 | -- Resolve_Range -- | |
7980 | ------------------- | |
7981 | ||
7982 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is | |
7983 | L : constant Node_Id := Low_Bound (N); | |
7984 | H : constant Node_Id := High_Bound (N); | |
7985 | ||
bd29d519 AC |
7986 | function First_Last_Ref return Boolean; |
7987 | -- Returns True if N is of the form X'First .. X'Last where X is the | |
7988 | -- same entity for both attributes. | |
7989 | ||
7990 | -------------------- | |
7991 | -- First_Last_Ref -- | |
7992 | -------------------- | |
7993 | ||
7994 | function First_Last_Ref return Boolean is | |
7995 | Lorig : constant Node_Id := Original_Node (L); | |
7996 | Horig : constant Node_Id := Original_Node (H); | |
7997 | ||
7998 | begin | |
7999 | if Nkind (Lorig) = N_Attribute_Reference | |
8000 | and then Nkind (Horig) = N_Attribute_Reference | |
8001 | and then Attribute_Name (Lorig) = Name_First | |
8002 | and then Attribute_Name (Horig) = Name_Last | |
8003 | then | |
8004 | declare | |
8005 | PL : constant Node_Id := Prefix (Lorig); | |
8006 | PH : constant Node_Id := Prefix (Horig); | |
8007 | begin | |
8008 | if Is_Entity_Name (PL) | |
8009 | and then Is_Entity_Name (PH) | |
8010 | and then Entity (PL) = Entity (PH) | |
8011 | then | |
8012 | return True; | |
8013 | end if; | |
8014 | end; | |
8015 | end if; | |
8016 | ||
8017 | return False; | |
8018 | end First_Last_Ref; | |
8019 | ||
8020 | -- Start of processing for Resolve_Range | |
8021 | ||
996ae0b0 RK |
8022 | begin |
8023 | Set_Etype (N, Typ); | |
8024 | Resolve (L, Typ); | |
8025 | Resolve (H, Typ); | |
8026 | ||
bd29d519 AC |
8027 | -- Check for inappropriate range on unordered enumeration type |
8028 | ||
8029 | if Bad_Unordered_Enumeration_Reference (N, Typ) | |
8030 | ||
8031 | -- Exclude X'First .. X'Last if X is the same entity for both | |
8032 | ||
8033 | and then not First_Last_Ref | |
8034 | then | |
8035 | Error_Msg ("subrange of unordered enumeration type?", Sloc (N)); | |
498d1b80 AC |
8036 | end if; |
8037 | ||
996ae0b0 RK |
8038 | Check_Unset_Reference (L); |
8039 | Check_Unset_Reference (H); | |
8040 | ||
8041 | -- We have to check the bounds for being within the base range as | |
758c442c GD |
8042 | -- required for a non-static context. Normally this is automatic and |
8043 | -- done as part of evaluating expressions, but the N_Range node is an | |
8044 | -- exception, since in GNAT we consider this node to be a subexpression, | |
8045 | -- even though in Ada it is not. The circuit in Sem_Eval could check for | |
8046 | -- this, but that would put the test on the main evaluation path for | |
8047 | -- expressions. | |
996ae0b0 RK |
8048 | |
8049 | Check_Non_Static_Context (L); | |
8050 | Check_Non_Static_Context (H); | |
8051 | ||
b7d1f17f HK |
8052 | -- Check for an ambiguous range over character literals. This will |
8053 | -- happen with a membership test involving only literals. | |
8054 | ||
8055 | if Typ = Any_Character then | |
8056 | Ambiguous_Character (L); | |
8057 | Set_Etype (N, Any_Type); | |
8058 | return; | |
8059 | end if; | |
8060 | ||
5cc9353d RD |
8061 | -- If bounds are static, constant-fold them, so size computations are |
8062 | -- identical between front-end and back-end. Do not perform this | |
fbf5a39b | 8063 | -- transformation while analyzing generic units, as type information |
5cc9353d | 8064 | -- would be lost when reanalyzing the constant node in the instance. |
fbf5a39b AC |
8065 | |
8066 | if Is_Discrete_Type (Typ) and then Expander_Active then | |
8067 | if Is_OK_Static_Expression (L) then | |
8068 | Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L)); | |
8069 | end if; | |
8070 | ||
8071 | if Is_OK_Static_Expression (H) then | |
8072 | Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H)); | |
8073 | end if; | |
8074 | end if; | |
996ae0b0 RK |
8075 | end Resolve_Range; |
8076 | ||
8077 | -------------------------- | |
8078 | -- Resolve_Real_Literal -- | |
8079 | -------------------------- | |
8080 | ||
8081 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is | |
8082 | Actual_Typ : constant Entity_Id := Etype (N); | |
8083 | ||
8084 | begin | |
8085 | -- Special processing for fixed-point literals to make sure that the | |
5cc9353d RD |
8086 | -- value is an exact multiple of small where this is required. We skip |
8087 | -- this for the universal real case, and also for generic types. | |
996ae0b0 RK |
8088 | |
8089 | if Is_Fixed_Point_Type (Typ) | |
8090 | and then Typ /= Universal_Fixed | |
8091 | and then Typ /= Any_Fixed | |
8092 | and then not Is_Generic_Type (Typ) | |
8093 | then | |
8094 | declare | |
8095 | Val : constant Ureal := Realval (N); | |
8096 | Cintr : constant Ureal := Val / Small_Value (Typ); | |
8097 | Cint : constant Uint := UR_Trunc (Cintr); | |
8098 | Den : constant Uint := Norm_Den (Cintr); | |
8099 | Stat : Boolean; | |
8100 | ||
8101 | begin | |
8102 | -- Case of literal is not an exact multiple of the Small | |
8103 | ||
8104 | if Den /= 1 then | |
8105 | ||
5cc9353d RD |
8106 | -- For a source program literal for a decimal fixed-point type, |
8107 | -- this is statically illegal (RM 4.9(36)). | |
996ae0b0 RK |
8108 | |
8109 | if Is_Decimal_Fixed_Point_Type (Typ) | |
8110 | and then Actual_Typ = Universal_Real | |
8111 | and then Comes_From_Source (N) | |
8112 | then | |
8113 | Error_Msg_N ("value has extraneous low order digits", N); | |
8114 | end if; | |
8115 | ||
bc5f3720 RD |
8116 | -- Generate a warning if literal from source |
8117 | ||
8118 | if Is_Static_Expression (N) | |
8119 | and then Warn_On_Bad_Fixed_Value | |
8120 | then | |
8121 | Error_Msg_N | |
aa5147f0 | 8122 | ("?static fixed-point value is not a multiple of Small!", |
bc5f3720 RD |
8123 | N); |
8124 | end if; | |
8125 | ||
996ae0b0 RK |
8126 | -- Replace literal by a value that is the exact representation |
8127 | -- of a value of the type, i.e. a multiple of the small value, | |
8128 | -- by truncation, since Machine_Rounds is false for all GNAT | |
8129 | -- fixed-point types (RM 4.9(38)). | |
8130 | ||
8131 | Stat := Is_Static_Expression (N); | |
8132 | Rewrite (N, | |
8133 | Make_Real_Literal (Sloc (N), | |
8134 | Realval => Small_Value (Typ) * Cint)); | |
8135 | ||
8136 | Set_Is_Static_Expression (N, Stat); | |
8137 | end if; | |
8138 | ||
8139 | -- In all cases, set the corresponding integer field | |
8140 | ||
8141 | Set_Corresponding_Integer_Value (N, Cint); | |
8142 | end; | |
8143 | end if; | |
8144 | ||
8145 | -- Now replace the actual type by the expected type as usual | |
8146 | ||
8147 | Set_Etype (N, Typ); | |
8148 | Eval_Real_Literal (N); | |
8149 | end Resolve_Real_Literal; | |
8150 | ||
8151 | ----------------------- | |
8152 | -- Resolve_Reference -- | |
8153 | ----------------------- | |
8154 | ||
8155 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is | |
8156 | P : constant Node_Id := Prefix (N); | |
8157 | ||
8158 | begin | |
8159 | -- Replace general access with specific type | |
8160 | ||
8161 | if Ekind (Etype (N)) = E_Allocator_Type then | |
8162 | Set_Etype (N, Base_Type (Typ)); | |
8163 | end if; | |
8164 | ||
8165 | Resolve (P, Designated_Type (Etype (N))); | |
8166 | ||
5cc9353d RD |
8167 | -- If we are taking the reference of a volatile entity, then treat it as |
8168 | -- a potential modification of this entity. This is too conservative, | |
8169 | -- but necessary because remove side effects can cause transformations | |
8170 | -- of normal assignments into reference sequences that otherwise fail to | |
8171 | -- notice the modification. | |
996ae0b0 | 8172 | |
fbf5a39b | 8173 | if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then |
45fc7ddb | 8174 | Note_Possible_Modification (P, Sure => False); |
996ae0b0 RK |
8175 | end if; |
8176 | end Resolve_Reference; | |
8177 | ||
8178 | -------------------------------- | |
8179 | -- Resolve_Selected_Component -- | |
8180 | -------------------------------- | |
8181 | ||
8182 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is | |
8183 | Comp : Entity_Id; | |
8184 | Comp1 : Entity_Id := Empty; -- prevent junk warning | |
8185 | P : constant Node_Id := Prefix (N); | |
8186 | S : constant Node_Id := Selector_Name (N); | |
8187 | T : Entity_Id := Etype (P); | |
8188 | I : Interp_Index; | |
8189 | I1 : Interp_Index := 0; -- prevent junk warning | |
8190 | It : Interp; | |
8191 | It1 : Interp; | |
8192 | Found : Boolean; | |
8193 | ||
6510f4c9 GB |
8194 | function Init_Component return Boolean; |
8195 | -- Check whether this is the initialization of a component within an | |
fbf5a39b | 8196 | -- init proc (by assignment or call to another init proc). If true, |
6510f4c9 GB |
8197 | -- there is no need for a discriminant check. |
8198 | ||
8199 | -------------------- | |
8200 | -- Init_Component -- | |
8201 | -------------------- | |
8202 | ||
8203 | function Init_Component return Boolean is | |
8204 | begin | |
8205 | return Inside_Init_Proc | |
8206 | and then Nkind (Prefix (N)) = N_Identifier | |
8207 | and then Chars (Prefix (N)) = Name_uInit | |
8208 | and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative; | |
8209 | end Init_Component; | |
8210 | ||
8211 | -- Start of processing for Resolve_Selected_Component | |
8212 | ||
996ae0b0 RK |
8213 | begin |
8214 | if Is_Overloaded (P) then | |
8215 | ||
8216 | -- Use the context type to select the prefix that has a selector | |
8217 | -- of the correct name and type. | |
8218 | ||
8219 | Found := False; | |
8220 | Get_First_Interp (P, I, It); | |
8221 | ||
8222 | Search : while Present (It.Typ) loop | |
8223 | if Is_Access_Type (It.Typ) then | |
8224 | T := Designated_Type (It.Typ); | |
8225 | else | |
8226 | T := It.Typ; | |
8227 | end if; | |
8228 | ||
8229 | if Is_Record_Type (T) then | |
36fcf362 RD |
8230 | |
8231 | -- The visible components of a class-wide type are those of | |
8232 | -- the root type. | |
8233 | ||
8234 | if Is_Class_Wide_Type (T) then | |
8235 | T := Etype (T); | |
8236 | end if; | |
8237 | ||
996ae0b0 | 8238 | Comp := First_Entity (T); |
996ae0b0 | 8239 | while Present (Comp) loop |
996ae0b0 RK |
8240 | if Chars (Comp) = Chars (S) |
8241 | and then Covers (Etype (Comp), Typ) | |
8242 | then | |
8243 | if not Found then | |
8244 | Found := True; | |
8245 | I1 := I; | |
8246 | It1 := It; | |
8247 | Comp1 := Comp; | |
8248 | ||
8249 | else | |
8250 | It := Disambiguate (P, I1, I, Any_Type); | |
8251 | ||
8252 | if It = No_Interp then | |
8253 | Error_Msg_N | |
8254 | ("ambiguous prefix for selected component", N); | |
8255 | Set_Etype (N, Typ); | |
8256 | return; | |
8257 | ||
8258 | else | |
8259 | It1 := It; | |
8260 | ||
c8ef728f ES |
8261 | -- There may be an implicit dereference. Retrieve |
8262 | -- designated record type. | |
8263 | ||
8264 | if Is_Access_Type (It1.Typ) then | |
8265 | T := Designated_Type (It1.Typ); | |
8266 | else | |
8267 | T := It1.Typ; | |
8268 | end if; | |
8269 | ||
8270 | if Scope (Comp1) /= T then | |
996ae0b0 RK |
8271 | |
8272 | -- Resolution chooses the new interpretation. | |
8273 | -- Find the component with the right name. | |
8274 | ||
c8ef728f | 8275 | Comp1 := First_Entity (T); |
996ae0b0 RK |
8276 | while Present (Comp1) |
8277 | and then Chars (Comp1) /= Chars (S) | |
8278 | loop | |
8279 | Comp1 := Next_Entity (Comp1); | |
8280 | end loop; | |
8281 | end if; | |
8282 | ||
8283 | exit Search; | |
8284 | end if; | |
8285 | end if; | |
8286 | end if; | |
8287 | ||
8288 | Comp := Next_Entity (Comp); | |
8289 | end loop; | |
996ae0b0 RK |
8290 | end if; |
8291 | ||
8292 | Get_Next_Interp (I, It); | |
996ae0b0 RK |
8293 | end loop Search; |
8294 | ||
8295 | Resolve (P, It1.Typ); | |
8296 | Set_Etype (N, Typ); | |
aa180613 | 8297 | Set_Entity_With_Style_Check (S, Comp1); |
996ae0b0 RK |
8298 | |
8299 | else | |
fbf5a39b | 8300 | -- Resolve prefix with its type |
996ae0b0 RK |
8301 | |
8302 | Resolve (P, T); | |
8303 | end if; | |
8304 | ||
aa180613 RD |
8305 | -- Generate cross-reference. We needed to wait until full overloading |
8306 | -- resolution was complete to do this, since otherwise we can't tell if | |
01e17342 | 8307 | -- we are an lvalue or not. |
aa180613 RD |
8308 | |
8309 | if May_Be_Lvalue (N) then | |
8310 | Generate_Reference (Entity (S), S, 'm'); | |
8311 | else | |
8312 | Generate_Reference (Entity (S), S, 'r'); | |
8313 | end if; | |
8314 | ||
c8ef728f ES |
8315 | -- If prefix is an access type, the node will be transformed into an |
8316 | -- explicit dereference during expansion. The type of the node is the | |
8317 | -- designated type of that of the prefix. | |
996ae0b0 RK |
8318 | |
8319 | if Is_Access_Type (Etype (P)) then | |
996ae0b0 | 8320 | T := Designated_Type (Etype (P)); |
c8ef728f | 8321 | Check_Fully_Declared_Prefix (T, P); |
996ae0b0 RK |
8322 | else |
8323 | T := Etype (P); | |
8324 | end if; | |
8325 | ||
8326 | if Has_Discriminants (T) | |
964f13da | 8327 | and then Ekind_In (Entity (S), E_Component, E_Discriminant) |
996ae0b0 RK |
8328 | and then Present (Original_Record_Component (Entity (S))) |
8329 | and then Ekind (Original_Record_Component (Entity (S))) = E_Component | |
8330 | and then Present (Discriminant_Checking_Func | |
8331 | (Original_Record_Component (Entity (S)))) | |
8332 | and then not Discriminant_Checks_Suppressed (T) | |
6510f4c9 | 8333 | and then not Init_Component |
996ae0b0 RK |
8334 | then |
8335 | Set_Do_Discriminant_Check (N); | |
8336 | end if; | |
8337 | ||
8338 | if Ekind (Entity (S)) = E_Void then | |
8339 | Error_Msg_N ("premature use of component", S); | |
8340 | end if; | |
8341 | ||
8342 | -- If the prefix is a record conversion, this may be a renamed | |
8343 | -- discriminant whose bounds differ from those of the original | |
8344 | -- one, so we must ensure that a range check is performed. | |
8345 | ||
8346 | if Nkind (P) = N_Type_Conversion | |
8347 | and then Ekind (Entity (S)) = E_Discriminant | |
fbf5a39b | 8348 | and then Is_Discrete_Type (Typ) |
996ae0b0 RK |
8349 | then |
8350 | Set_Etype (N, Base_Type (Typ)); | |
8351 | end if; | |
8352 | ||
8353 | -- Note: No Eval processing is required, because the prefix is of a | |
8354 | -- record type, or protected type, and neither can possibly be static. | |
8355 | ||
c28408b7 RD |
8356 | -- If the array type is atomic, and is packed, and we are in a left side |
8357 | -- context, then this is worth a warning, since we have a situation | |
5cc9353d RD |
8358 | -- where the access to the component may cause extra read/writes of the |
8359 | -- atomic array object, which could be considered unexpected. | |
c28408b7 RD |
8360 | |
8361 | if Nkind (N) = N_Selected_Component | |
8362 | and then (Is_Atomic (T) | |
8363 | or else (Is_Entity_Name (Prefix (N)) | |
8364 | and then Is_Atomic (Entity (Prefix (N))))) | |
8365 | and then Is_Packed (T) | |
8366 | and then Is_LHS (N) | |
8367 | then | |
8368 | Error_Msg_N ("?assignment to component of packed atomic record", | |
8369 | Prefix (N)); | |
8370 | Error_Msg_N ("?\may cause unexpected accesses to atomic object", | |
8371 | Prefix (N)); | |
8372 | end if; | |
996ae0b0 RK |
8373 | end Resolve_Selected_Component; |
8374 | ||
8375 | ------------------- | |
8376 | -- Resolve_Shift -- | |
8377 | ------------------- | |
8378 | ||
8379 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is | |
8380 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8381 | L : constant Node_Id := Left_Opnd (N); | |
8382 | R : constant Node_Id := Right_Opnd (N); | |
8383 | ||
8384 | begin | |
8385 | -- We do the resolution using the base type, because intermediate values | |
8386 | -- in expressions always are of the base type, not a subtype of it. | |
8387 | ||
8388 | Resolve (L, B_Typ); | |
8389 | Resolve (R, Standard_Natural); | |
8390 | ||
8391 | Check_Unset_Reference (L); | |
8392 | Check_Unset_Reference (R); | |
8393 | ||
8394 | Set_Etype (N, B_Typ); | |
fbf5a39b | 8395 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
8396 | Eval_Shift (N); |
8397 | end Resolve_Shift; | |
8398 | ||
8399 | --------------------------- | |
8400 | -- Resolve_Short_Circuit -- | |
8401 | --------------------------- | |
8402 | ||
8403 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is | |
8404 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8405 | L : constant Node_Id := Left_Opnd (N); | |
8406 | R : constant Node_Id := Right_Opnd (N); | |
8407 | ||
8408 | begin | |
8409 | Resolve (L, B_Typ); | |
8410 | Resolve (R, B_Typ); | |
8411 | ||
45fc7ddb HK |
8412 | -- Check for issuing warning for always False assert/check, this happens |
8413 | -- when assertions are turned off, in which case the pragma Assert/Check | |
36fcf362 RD |
8414 | -- was transformed into: |
8415 | ||
8416 | -- if False and then <condition> then ... | |
8417 | ||
8418 | -- and we detect this pattern | |
8419 | ||
8420 | if Warn_On_Assertion_Failure | |
8421 | and then Is_Entity_Name (R) | |
8422 | and then Entity (R) = Standard_False | |
8423 | and then Nkind (Parent (N)) = N_If_Statement | |
8424 | and then Nkind (N) = N_And_Then | |
8425 | and then Is_Entity_Name (L) | |
8426 | and then Entity (L) = Standard_False | |
8427 | then | |
8428 | declare | |
8429 | Orig : constant Node_Id := Original_Node (Parent (N)); | |
45fc7ddb | 8430 | |
36fcf362 RD |
8431 | begin |
8432 | if Nkind (Orig) = N_Pragma | |
26570b21 | 8433 | and then Pragma_Name (Orig) = Name_Assert |
36fcf362 RD |
8434 | then |
8435 | -- Don't want to warn if original condition is explicit False | |
8436 | ||
8437 | declare | |
8438 | Expr : constant Node_Id := | |
8439 | Original_Node | |
8440 | (Expression | |
8441 | (First (Pragma_Argument_Associations (Orig)))); | |
8442 | begin | |
8443 | if Is_Entity_Name (Expr) | |
8444 | and then Entity (Expr) = Standard_False | |
8445 | then | |
8446 | null; | |
8447 | else | |
51bf9bdf AC |
8448 | -- Issue warning. We do not want the deletion of the |
8449 | -- IF/AND-THEN to take this message with it. We achieve | |
8450 | -- this by making sure that the expanded code points to | |
8451 | -- the Sloc of the expression, not the original pragma. | |
8452 | ||
8453 | Error_Msg_N | |
e7c0dd39 | 8454 | ("?assertion would fail at run time!", |
51bf9bdf AC |
8455 | Expression |
8456 | (First (Pragma_Argument_Associations (Orig)))); | |
36fcf362 RD |
8457 | end if; |
8458 | end; | |
45fc7ddb HK |
8459 | |
8460 | -- Similar processing for Check pragma | |
8461 | ||
8462 | elsif Nkind (Orig) = N_Pragma | |
8463 | and then Pragma_Name (Orig) = Name_Check | |
8464 | then | |
8465 | -- Don't want to warn if original condition is explicit False | |
8466 | ||
8467 | declare | |
8468 | Expr : constant Node_Id := | |
8469 | Original_Node | |
8470 | (Expression | |
8471 | (Next (First | |
8472 | (Pragma_Argument_Associations (Orig))))); | |
8473 | begin | |
8474 | if Is_Entity_Name (Expr) | |
8475 | and then Entity (Expr) = Standard_False | |
8476 | then | |
8477 | null; | |
8478 | else | |
51bf9bdf | 8479 | Error_Msg_N |
e7c0dd39 | 8480 | ("?check would fail at run time!", |
51bf9bdf AC |
8481 | Expression |
8482 | (Last (Pragma_Argument_Associations (Orig)))); | |
45fc7ddb HK |
8483 | end if; |
8484 | end; | |
36fcf362 RD |
8485 | end if; |
8486 | end; | |
8487 | end if; | |
8488 | ||
8489 | -- Continue with processing of short circuit | |
8490 | ||
996ae0b0 RK |
8491 | Check_Unset_Reference (L); |
8492 | Check_Unset_Reference (R); | |
8493 | ||
8494 | Set_Etype (N, B_Typ); | |
8495 | Eval_Short_Circuit (N); | |
8496 | end Resolve_Short_Circuit; | |
8497 | ||
8498 | ------------------- | |
8499 | -- Resolve_Slice -- | |
8500 | ------------------- | |
8501 | ||
8502 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is | |
8503 | Name : constant Node_Id := Prefix (N); | |
8504 | Drange : constant Node_Id := Discrete_Range (N); | |
8505 | Array_Type : Entity_Id := Empty; | |
8506 | Index : Node_Id; | |
8507 | ||
8508 | begin | |
8509 | if Is_Overloaded (Name) then | |
8510 | ||
d81b4bfe TQ |
8511 | -- Use the context type to select the prefix that yields the correct |
8512 | -- array type. | |
996ae0b0 RK |
8513 | |
8514 | declare | |
8515 | I : Interp_Index; | |
8516 | I1 : Interp_Index := 0; | |
8517 | It : Interp; | |
8518 | P : constant Node_Id := Prefix (N); | |
8519 | Found : Boolean := False; | |
8520 | ||
8521 | begin | |
8522 | Get_First_Interp (P, I, It); | |
996ae0b0 | 8523 | while Present (It.Typ) loop |
996ae0b0 RK |
8524 | if (Is_Array_Type (It.Typ) |
8525 | and then Covers (Typ, It.Typ)) | |
8526 | or else (Is_Access_Type (It.Typ) | |
8527 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
8528 | and then Covers (Typ, Designated_Type (It.Typ))) | |
8529 | then | |
8530 | if Found then | |
8531 | It := Disambiguate (P, I1, I, Any_Type); | |
8532 | ||
8533 | if It = No_Interp then | |
8534 | Error_Msg_N ("ambiguous prefix for slicing", N); | |
8535 | Set_Etype (N, Typ); | |
8536 | return; | |
8537 | else | |
8538 | Found := True; | |
8539 | Array_Type := It.Typ; | |
8540 | I1 := I; | |
8541 | end if; | |
8542 | else | |
8543 | Found := True; | |
8544 | Array_Type := It.Typ; | |
8545 | I1 := I; | |
8546 | end if; | |
8547 | end if; | |
8548 | ||
8549 | Get_Next_Interp (I, It); | |
8550 | end loop; | |
8551 | end; | |
8552 | ||
8553 | else | |
8554 | Array_Type := Etype (Name); | |
8555 | end if; | |
8556 | ||
8557 | Resolve (Name, Array_Type); | |
8558 | ||
8559 | if Is_Access_Type (Array_Type) then | |
8560 | Apply_Access_Check (N); | |
8561 | Array_Type := Designated_Type (Array_Type); | |
8562 | ||
c8ef728f ES |
8563 | -- If the prefix is an access to an unconstrained array, we must use |
8564 | -- the actual subtype of the object to perform the index checks. The | |
8565 | -- object denoted by the prefix is implicit in the node, so we build | |
8566 | -- an explicit representation for it in order to compute the actual | |
8567 | -- subtype. | |
82c80734 RD |
8568 | |
8569 | if not Is_Constrained (Array_Type) then | |
8570 | Remove_Side_Effects (Prefix (N)); | |
8571 | ||
8572 | declare | |
8573 | Obj : constant Node_Id := | |
8574 | Make_Explicit_Dereference (Sloc (N), | |
8575 | Prefix => New_Copy_Tree (Prefix (N))); | |
8576 | begin | |
8577 | Set_Etype (Obj, Array_Type); | |
8578 | Set_Parent (Obj, Parent (N)); | |
8579 | Array_Type := Get_Actual_Subtype (Obj); | |
8580 | end; | |
8581 | end if; | |
8582 | ||
996ae0b0 | 8583 | elsif Is_Entity_Name (Name) |
6c994759 | 8584 | or else Nkind (Name) = N_Explicit_Dereference |
996ae0b0 RK |
8585 | or else (Nkind (Name) = N_Function_Call |
8586 | and then not Is_Constrained (Etype (Name))) | |
8587 | then | |
8588 | Array_Type := Get_Actual_Subtype (Name); | |
aa5147f0 ES |
8589 | |
8590 | -- If the name is a selected component that depends on discriminants, | |
8591 | -- build an actual subtype for it. This can happen only when the name | |
8592 | -- itself is overloaded; otherwise the actual subtype is created when | |
8593 | -- the selected component is analyzed. | |
8594 | ||
8595 | elsif Nkind (Name) = N_Selected_Component | |
8596 | and then Full_Analysis | |
8597 | and then Depends_On_Discriminant (First_Index (Array_Type)) | |
8598 | then | |
8599 | declare | |
8600 | Act_Decl : constant Node_Id := | |
8601 | Build_Actual_Subtype_Of_Component (Array_Type, Name); | |
8602 | begin | |
8603 | Insert_Action (N, Act_Decl); | |
8604 | Array_Type := Defining_Identifier (Act_Decl); | |
8605 | end; | |
d79e621a GD |
8606 | |
8607 | -- Maybe this should just be "else", instead of checking for the | |
5cc9353d RD |
8608 | -- specific case of slice??? This is needed for the case where the |
8609 | -- prefix is an Image attribute, which gets expanded to a slice, and so | |
8610 | -- has a constrained subtype which we want to use for the slice range | |
8611 | -- check applied below (the range check won't get done if the | |
8612 | -- unconstrained subtype of the 'Image is used). | |
d79e621a GD |
8613 | |
8614 | elsif Nkind (Name) = N_Slice then | |
8615 | Array_Type := Etype (Name); | |
996ae0b0 RK |
8616 | end if; |
8617 | ||
8618 | -- If name was overloaded, set slice type correctly now | |
8619 | ||
8620 | Set_Etype (N, Array_Type); | |
8621 | ||
c8ef728f ES |
8622 | -- If the range is specified by a subtype mark, no resolution is |
8623 | -- necessary. Else resolve the bounds, and apply needed checks. | |
996ae0b0 RK |
8624 | |
8625 | if not Is_Entity_Name (Drange) then | |
8626 | Index := First_Index (Array_Type); | |
8627 | Resolve (Drange, Base_Type (Etype (Index))); | |
8628 | ||
dbe945f1 AC |
8629 | if Nkind (Drange) = N_Range then |
8630 | ||
8631 | -- Ensure that side effects in the bounds are properly handled | |
8632 | ||
8633 | Remove_Side_Effects (Low_Bound (Drange), Variable_Ref => True); | |
8634 | Remove_Side_Effects (High_Bound (Drange), Variable_Ref => True); | |
0669bebe GB |
8635 | |
8636 | -- Do not apply the range check to nodes associated with the | |
8637 | -- frontend expansion of the dispatch table. We first check | |
dbe945f1 | 8638 | -- if Ada.Tags is already loaded to avoid the addition of an |
0669bebe GB |
8639 | -- undesired dependence on such run-time unit. |
8640 | ||
dbe945f1 AC |
8641 | if not Tagged_Type_Expansion |
8642 | or else not | |
8643 | (RTU_Loaded (Ada_Tags) | |
cead616d AC |
8644 | and then Nkind (Prefix (N)) = N_Selected_Component |
8645 | and then Present (Entity (Selector_Name (Prefix (N)))) | |
8646 | and then Entity (Selector_Name (Prefix (N))) = | |
dbe945f1 AC |
8647 | RTE_Record_Component (RE_Prims_Ptr)) |
8648 | then | |
8649 | Apply_Range_Check (Drange, Etype (Index)); | |
8650 | end if; | |
996ae0b0 RK |
8651 | end if; |
8652 | end if; | |
8653 | ||
8654 | Set_Slice_Subtype (N); | |
aa180613 | 8655 | |
ea034236 AC |
8656 | -- Check bad use of type with predicates |
8657 | ||
8658 | if Has_Predicates (Etype (Drange)) then | |
ed00f472 | 8659 | Bad_Predicated_Subtype_Use |
ea034236 AC |
8660 | ("subtype& has predicate, not allowed in slice", |
8661 | Drange, Etype (Drange)); | |
8662 | ||
8663 | -- Otherwise here is where we check suspicious indexes | |
8664 | ||
8665 | elsif Nkind (Drange) = N_Range then | |
aa180613 RD |
8666 | Warn_On_Suspicious_Index (Name, Low_Bound (Drange)); |
8667 | Warn_On_Suspicious_Index (Name, High_Bound (Drange)); | |
8668 | end if; | |
8669 | ||
996ae0b0 | 8670 | Eval_Slice (N); |
996ae0b0 RK |
8671 | end Resolve_Slice; |
8672 | ||
8673 | ---------------------------- | |
8674 | -- Resolve_String_Literal -- | |
8675 | ---------------------------- | |
8676 | ||
8677 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is | |
8678 | C_Typ : constant Entity_Id := Component_Type (Typ); | |
8679 | R_Typ : constant Entity_Id := Root_Type (C_Typ); | |
8680 | Loc : constant Source_Ptr := Sloc (N); | |
8681 | Str : constant String_Id := Strval (N); | |
8682 | Strlen : constant Nat := String_Length (Str); | |
8683 | Subtype_Id : Entity_Id; | |
8684 | Need_Check : Boolean; | |
8685 | ||
8686 | begin | |
8687 | -- For a string appearing in a concatenation, defer creation of the | |
8688 | -- string_literal_subtype until the end of the resolution of the | |
c8ef728f ES |
8689 | -- concatenation, because the literal may be constant-folded away. This |
8690 | -- is a useful optimization for long concatenation expressions. | |
996ae0b0 | 8691 | |
c8ef728f | 8692 | -- If the string is an aggregate built for a single character (which |
996ae0b0 | 8693 | -- happens in a non-static context) or a is null string to which special |
c8ef728f ES |
8694 | -- checks may apply, we build the subtype. Wide strings must also get a |
8695 | -- string subtype if they come from a one character aggregate. Strings | |
996ae0b0 RK |
8696 | -- generated by attributes might be static, but it is often hard to |
8697 | -- determine whether the enclosing context is static, so we generate | |
8698 | -- subtypes for them as well, thus losing some rarer optimizations ??? | |
8699 | -- Same for strings that come from a static conversion. | |
8700 | ||
8701 | Need_Check := | |
8702 | (Strlen = 0 and then Typ /= Standard_String) | |
8703 | or else Nkind (Parent (N)) /= N_Op_Concat | |
8704 | or else (N /= Left_Opnd (Parent (N)) | |
8705 | and then N /= Right_Opnd (Parent (N))) | |
82c80734 RD |
8706 | or else ((Typ = Standard_Wide_String |
8707 | or else Typ = Standard_Wide_Wide_String) | |
996ae0b0 RK |
8708 | and then Nkind (Original_Node (N)) /= N_String_Literal); |
8709 | ||
d81b4bfe TQ |
8710 | -- If the resolving type is itself a string literal subtype, we can just |
8711 | -- reuse it, since there is no point in creating another. | |
996ae0b0 RK |
8712 | |
8713 | if Ekind (Typ) = E_String_Literal_Subtype then | |
8714 | Subtype_Id := Typ; | |
8715 | ||
8716 | elsif Nkind (Parent (N)) = N_Op_Concat | |
8717 | and then not Need_Check | |
45fc7ddb HK |
8718 | and then not Nkind_In (Original_Node (N), N_Character_Literal, |
8719 | N_Attribute_Reference, | |
8720 | N_Qualified_Expression, | |
8721 | N_Type_Conversion) | |
996ae0b0 RK |
8722 | then |
8723 | Subtype_Id := Typ; | |
8724 | ||
8725 | -- Otherwise we must create a string literal subtype. Note that the | |
8726 | -- whole idea of string literal subtypes is simply to avoid the need | |
8727 | -- for building a full fledged array subtype for each literal. | |
45fc7ddb | 8728 | |
996ae0b0 RK |
8729 | else |
8730 | Set_String_Literal_Subtype (N, Typ); | |
8731 | Subtype_Id := Etype (N); | |
8732 | end if; | |
8733 | ||
8734 | if Nkind (Parent (N)) /= N_Op_Concat | |
8735 | or else Need_Check | |
8736 | then | |
8737 | Set_Etype (N, Subtype_Id); | |
8738 | Eval_String_Literal (N); | |
8739 | end if; | |
8740 | ||
8741 | if Is_Limited_Composite (Typ) | |
8742 | or else Is_Private_Composite (Typ) | |
8743 | then | |
8744 | Error_Msg_N ("string literal not available for private array", N); | |
8745 | Set_Etype (N, Any_Type); | |
8746 | return; | |
8747 | end if; | |
8748 | ||
d81b4bfe TQ |
8749 | -- The validity of a null string has been checked in the call to |
8750 | -- Eval_String_Literal. | |
996ae0b0 RK |
8751 | |
8752 | if Strlen = 0 then | |
8753 | return; | |
8754 | ||
c8ef728f ES |
8755 | -- Always accept string literal with component type Any_Character, which |
8756 | -- occurs in error situations and in comparisons of literals, both of | |
8757 | -- which should accept all literals. | |
996ae0b0 RK |
8758 | |
8759 | elsif R_Typ = Any_Character then | |
8760 | return; | |
8761 | ||
f3d57416 RW |
8762 | -- If the type is bit-packed, then we always transform the string |
8763 | -- literal into a full fledged aggregate. | |
996ae0b0 RK |
8764 | |
8765 | elsif Is_Bit_Packed_Array (Typ) then | |
8766 | null; | |
8767 | ||
82c80734 | 8768 | -- Deal with cases of Wide_Wide_String, Wide_String, and String |
996ae0b0 RK |
8769 | |
8770 | else | |
82c80734 RD |
8771 | -- For Standard.Wide_Wide_String, or any other type whose component |
8772 | -- type is Standard.Wide_Wide_Character, we know that all the | |
996ae0b0 RK |
8773 | -- characters in the string must be acceptable, since the parser |
8774 | -- accepted the characters as valid character literals. | |
8775 | ||
82c80734 | 8776 | if R_Typ = Standard_Wide_Wide_Character then |
996ae0b0 RK |
8777 | null; |
8778 | ||
c8ef728f ES |
8779 | -- For the case of Standard.String, or any other type whose component |
8780 | -- type is Standard.Character, we must make sure that there are no | |
8781 | -- wide characters in the string, i.e. that it is entirely composed | |
8782 | -- of characters in range of type Character. | |
996ae0b0 | 8783 | |
c8ef728f ES |
8784 | -- If the string literal is the result of a static concatenation, the |
8785 | -- test has already been performed on the components, and need not be | |
8786 | -- repeated. | |
996ae0b0 RK |
8787 | |
8788 | elsif R_Typ = Standard_Character | |
8789 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8790 | then | |
8791 | for J in 1 .. Strlen loop | |
8792 | if not In_Character_Range (Get_String_Char (Str, J)) then | |
8793 | ||
8794 | -- If we are out of range, post error. This is one of the | |
8795 | -- very few places that we place the flag in the middle of | |
d81b4bfe TQ |
8796 | -- a token, right under the offending wide character. Not |
8797 | -- quite clear if this is right wrt wide character encoding | |
8798 | -- sequences, but it's only an error message! | |
996ae0b0 RK |
8799 | |
8800 | Error_Msg | |
82c80734 RD |
8801 | ("literal out of range of type Standard.Character", |
8802 | Source_Ptr (Int (Loc) + J)); | |
8803 | return; | |
8804 | end if; | |
8805 | end loop; | |
8806 | ||
8807 | -- For the case of Standard.Wide_String, or any other type whose | |
8808 | -- component type is Standard.Wide_Character, we must make sure that | |
8809 | -- there are no wide characters in the string, i.e. that it is | |
8810 | -- entirely composed of characters in range of type Wide_Character. | |
8811 | ||
8812 | -- If the string literal is the result of a static concatenation, | |
8813 | -- the test has already been performed on the components, and need | |
8814 | -- not be repeated. | |
8815 | ||
8816 | elsif R_Typ = Standard_Wide_Character | |
8817 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8818 | then | |
8819 | for J in 1 .. Strlen loop | |
8820 | if not In_Wide_Character_Range (Get_String_Char (Str, J)) then | |
8821 | ||
8822 | -- If we are out of range, post error. This is one of the | |
8823 | -- very few places that we place the flag in the middle of | |
8824 | -- a token, right under the offending wide character. | |
8825 | ||
8826 | -- This is not quite right, because characters in general | |
8827 | -- will take more than one character position ??? | |
8828 | ||
8829 | Error_Msg | |
8830 | ("literal out of range of type Standard.Wide_Character", | |
996ae0b0 RK |
8831 | Source_Ptr (Int (Loc) + J)); |
8832 | return; | |
8833 | end if; | |
8834 | end loop; | |
8835 | ||
8836 | -- If the root type is not a standard character, then we will convert | |
8837 | -- the string into an aggregate and will let the aggregate code do | |
82c80734 | 8838 | -- the checking. Standard Wide_Wide_Character is also OK here. |
996ae0b0 RK |
8839 | |
8840 | else | |
8841 | null; | |
996ae0b0 RK |
8842 | end if; |
8843 | ||
c8ef728f ES |
8844 | -- See if the component type of the array corresponding to the string |
8845 | -- has compile time known bounds. If yes we can directly check | |
8846 | -- whether the evaluation of the string will raise constraint error. | |
8847 | -- Otherwise we need to transform the string literal into the | |
5cc9353d RD |
8848 | -- corresponding character aggregate and let the aggregate code do |
8849 | -- the checking. | |
996ae0b0 | 8850 | |
45fc7ddb HK |
8851 | if Is_Standard_Character_Type (R_Typ) then |
8852 | ||
996ae0b0 RK |
8853 | -- Check for the case of full range, where we are definitely OK |
8854 | ||
8855 | if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then | |
8856 | return; | |
8857 | end if; | |
8858 | ||
8859 | -- Here the range is not the complete base type range, so check | |
8860 | ||
8861 | declare | |
8862 | Comp_Typ_Lo : constant Node_Id := | |
8863 | Type_Low_Bound (Component_Type (Typ)); | |
8864 | Comp_Typ_Hi : constant Node_Id := | |
8865 | Type_High_Bound (Component_Type (Typ)); | |
8866 | ||
8867 | Char_Val : Uint; | |
8868 | ||
8869 | begin | |
8870 | if Compile_Time_Known_Value (Comp_Typ_Lo) | |
8871 | and then Compile_Time_Known_Value (Comp_Typ_Hi) | |
8872 | then | |
8873 | for J in 1 .. Strlen loop | |
8874 | Char_Val := UI_From_Int (Int (Get_String_Char (Str, J))); | |
8875 | ||
8876 | if Char_Val < Expr_Value (Comp_Typ_Lo) | |
8877 | or else Char_Val > Expr_Value (Comp_Typ_Hi) | |
8878 | then | |
8879 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 8880 | (N, "character out of range?", CE_Range_Check_Failed, |
996ae0b0 RK |
8881 | Loc => Source_Ptr (Int (Loc) + J)); |
8882 | end if; | |
8883 | end loop; | |
8884 | ||
8885 | return; | |
8886 | end if; | |
8887 | end; | |
8888 | end if; | |
8889 | end if; | |
8890 | ||
8891 | -- If we got here we meed to transform the string literal into the | |
8892 | -- equivalent qualified positional array aggregate. This is rather | |
8893 | -- heavy artillery for this situation, but it is hard work to avoid. | |
8894 | ||
8895 | declare | |
fbf5a39b | 8896 | Lits : constant List_Id := New_List; |
996ae0b0 RK |
8897 | P : Source_Ptr := Loc + 1; |
8898 | C : Char_Code; | |
8899 | ||
8900 | begin | |
c8ef728f ES |
8901 | -- Build the character literals, we give them source locations that |
8902 | -- correspond to the string positions, which is a bit tricky given | |
8903 | -- the possible presence of wide character escape sequences. | |
996ae0b0 RK |
8904 | |
8905 | for J in 1 .. Strlen loop | |
8906 | C := Get_String_Char (Str, J); | |
8907 | Set_Character_Literal_Name (C); | |
8908 | ||
8909 | Append_To (Lits, | |
82c80734 RD |
8910 | Make_Character_Literal (P, |
8911 | Chars => Name_Find, | |
8912 | Char_Literal_Value => UI_From_CC (C))); | |
996ae0b0 RK |
8913 | |
8914 | if In_Character_Range (C) then | |
8915 | P := P + 1; | |
8916 | ||
8917 | -- Should we have a call to Skip_Wide here ??? | |
5cc9353d | 8918 | |
996ae0b0 RK |
8919 | -- ??? else |
8920 | -- Skip_Wide (P); | |
8921 | ||
8922 | end if; | |
8923 | end loop; | |
8924 | ||
8925 | Rewrite (N, | |
8926 | Make_Qualified_Expression (Loc, | |
8927 | Subtype_Mark => New_Reference_To (Typ, Loc), | |
8928 | Expression => | |
8929 | Make_Aggregate (Loc, Expressions => Lits))); | |
8930 | ||
8931 | Analyze_And_Resolve (N, Typ); | |
8932 | end; | |
8933 | end Resolve_String_Literal; | |
8934 | ||
8935 | ----------------------------- | |
8936 | -- Resolve_Subprogram_Info -- | |
8937 | ----------------------------- | |
8938 | ||
8939 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is | |
8940 | begin | |
8941 | Set_Etype (N, Typ); | |
8942 | end Resolve_Subprogram_Info; | |
8943 | ||
8944 | ----------------------------- | |
8945 | -- Resolve_Type_Conversion -- | |
8946 | ----------------------------- | |
8947 | ||
8948 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is | |
4b2d2c13 AC |
8949 | Conv_OK : constant Boolean := Conversion_OK (N); |
8950 | Operand : constant Node_Id := Expression (N); | |
b7d1f17f HK |
8951 | Operand_Typ : constant Entity_Id := Etype (Operand); |
8952 | Target_Typ : constant Entity_Id := Etype (N); | |
996ae0b0 | 8953 | Rop : Node_Id; |
fbf5a39b AC |
8954 | Orig_N : Node_Id; |
8955 | Orig_T : Node_Id; | |
996ae0b0 | 8956 | |
ae2aa109 AC |
8957 | Test_Redundant : Boolean := Warn_On_Redundant_Constructs; |
8958 | -- Set to False to suppress cases where we want to suppress the test | |
8959 | -- for redundancy to avoid possible false positives on this warning. | |
8960 | ||
996ae0b0 | 8961 | begin |
996ae0b0 | 8962 | if not Conv_OK |
b7d1f17f | 8963 | and then not Valid_Conversion (N, Target_Typ, Operand) |
996ae0b0 RK |
8964 | then |
8965 | return; | |
8966 | end if; | |
8967 | ||
ae2aa109 AC |
8968 | -- If the Operand Etype is Universal_Fixed, then the conversion is |
8969 | -- never redundant. We need this check because by the time we have | |
8970 | -- finished the rather complex transformation, the conversion looks | |
8971 | -- redundant when it is not. | |
8972 | ||
8973 | if Operand_Typ = Universal_Fixed then | |
8974 | Test_Redundant := False; | |
8975 | ||
8976 | -- If the operand is marked as Any_Fixed, then special processing is | |
8977 | -- required. This is also a case where we suppress the test for a | |
8978 | -- redundant conversion, since most certainly it is not redundant. | |
8979 | ||
8980 | elsif Operand_Typ = Any_Fixed then | |
8981 | Test_Redundant := False; | |
996ae0b0 RK |
8982 | |
8983 | -- Mixed-mode operation involving a literal. Context must be a fixed | |
8984 | -- type which is applied to the literal subsequently. | |
8985 | ||
8986 | if Is_Fixed_Point_Type (Typ) then | |
8987 | Set_Etype (Operand, Universal_Real); | |
8988 | ||
8989 | elsif Is_Numeric_Type (Typ) | |
45fc7ddb | 8990 | and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide) |
996ae0b0 | 8991 | and then (Etype (Right_Opnd (Operand)) = Universal_Real |
45fc7ddb HK |
8992 | or else |
8993 | Etype (Left_Opnd (Operand)) = Universal_Real) | |
996ae0b0 | 8994 | then |
a77842bd TQ |
8995 | -- Return if expression is ambiguous |
8996 | ||
996ae0b0 | 8997 | if Unique_Fixed_Point_Type (N) = Any_Type then |
a77842bd | 8998 | return; |
82c80734 | 8999 | |
a77842bd TQ |
9000 | -- If nothing else, the available fixed type is Duration |
9001 | ||
9002 | else | |
996ae0b0 RK |
9003 | Set_Etype (Operand, Standard_Duration); |
9004 | end if; | |
9005 | ||
bc5f3720 | 9006 | -- Resolve the real operand with largest available precision |
9ebe3743 | 9007 | |
996ae0b0 RK |
9008 | if Etype (Right_Opnd (Operand)) = Universal_Real then |
9009 | Rop := New_Copy_Tree (Right_Opnd (Operand)); | |
9010 | else | |
9011 | Rop := New_Copy_Tree (Left_Opnd (Operand)); | |
9012 | end if; | |
9013 | ||
9ebe3743 | 9014 | Resolve (Rop, Universal_Real); |
996ae0b0 | 9015 | |
82c80734 RD |
9016 | -- If the operand is a literal (it could be a non-static and |
9017 | -- illegal exponentiation) check whether the use of Duration | |
9018 | -- is potentially inaccurate. | |
9019 | ||
9020 | if Nkind (Rop) = N_Real_Literal | |
9021 | and then Realval (Rop) /= Ureal_0 | |
996ae0b0 RK |
9022 | and then abs (Realval (Rop)) < Delta_Value (Standard_Duration) |
9023 | then | |
aa180613 | 9024 | Error_Msg_N |
aa5147f0 ES |
9025 | ("?universal real operand can only " & |
9026 | "be interpreted as Duration!", | |
aa180613 RD |
9027 | Rop); |
9028 | Error_Msg_N | |
aa5147f0 | 9029 | ("\?precision will be lost in the conversion!", Rop); |
996ae0b0 RK |
9030 | end if; |
9031 | ||
891a6e79 AC |
9032 | elsif Is_Numeric_Type (Typ) |
9033 | and then Nkind (Operand) in N_Op | |
9034 | and then Unique_Fixed_Point_Type (N) /= Any_Type | |
9035 | then | |
9036 | Set_Etype (Operand, Standard_Duration); | |
9037 | ||
996ae0b0 RK |
9038 | else |
9039 | Error_Msg_N ("invalid context for mixed mode operation", N); | |
9040 | Set_Etype (Operand, Any_Type); | |
9041 | return; | |
9042 | end if; | |
9043 | end if; | |
9044 | ||
fbf5a39b | 9045 | Resolve (Operand); |
996ae0b0 | 9046 | |
b0186f71 AC |
9047 | -- In SPARK or ALFA, a type conversion between array types should be |
9048 | -- restricted to types which have matching static bounds. | |
9049 | ||
fe5d3068 | 9050 | if Is_Array_Type (Target_Typ) |
b0186f71 AC |
9051 | and then Is_Array_Type (Operand_Typ) |
9052 | and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ) | |
9053 | then | |
fe5d3068 YM |
9054 | Check_Formal_Restriction |
9055 | ("array types should have matching static bounds", N); | |
b0186f71 AC |
9056 | end if; |
9057 | ||
996ae0b0 | 9058 | -- Note: we do the Eval_Type_Conversion call before applying the |
d81b4bfe TQ |
9059 | -- required checks for a subtype conversion. This is important, since |
9060 | -- both are prepared under certain circumstances to change the type | |
9061 | -- conversion to a constraint error node, but in the case of | |
9062 | -- Eval_Type_Conversion this may reflect an illegality in the static | |
9063 | -- case, and we would miss the illegality (getting only a warning | |
9064 | -- message), if we applied the type conversion checks first. | |
996ae0b0 RK |
9065 | |
9066 | Eval_Type_Conversion (N); | |
9067 | ||
d81b4bfe TQ |
9068 | -- Even when evaluation is not possible, we may be able to simplify the |
9069 | -- conversion or its expression. This needs to be done before applying | |
9070 | -- checks, since otherwise the checks may use the original expression | |
9071 | -- and defeat the simplifications. This is specifically the case for | |
9072 | -- elimination of the floating-point Truncation attribute in | |
9073 | -- float-to-int conversions. | |
0669bebe GB |
9074 | |
9075 | Simplify_Type_Conversion (N); | |
9076 | ||
d81b4bfe TQ |
9077 | -- If after evaluation we still have a type conversion, then we may need |
9078 | -- to apply checks required for a subtype conversion. | |
996ae0b0 RK |
9079 | |
9080 | -- Skip these type conversion checks if universal fixed operands | |
9081 | -- operands involved, since range checks are handled separately for | |
9082 | -- these cases (in the appropriate Expand routines in unit Exp_Fixd). | |
9083 | ||
9084 | if Nkind (N) = N_Type_Conversion | |
b7d1f17f HK |
9085 | and then not Is_Generic_Type (Root_Type (Target_Typ)) |
9086 | and then Target_Typ /= Universal_Fixed | |
9087 | and then Operand_Typ /= Universal_Fixed | |
996ae0b0 RK |
9088 | then |
9089 | Apply_Type_Conversion_Checks (N); | |
9090 | end if; | |
9091 | ||
d81b4bfe TQ |
9092 | -- Issue warning for conversion of simple object to its own type. We |
9093 | -- have to test the original nodes, since they may have been rewritten | |
9094 | -- by various optimizations. | |
fbf5a39b AC |
9095 | |
9096 | Orig_N := Original_Node (N); | |
996ae0b0 | 9097 | |
ae2aa109 AC |
9098 | -- Here we test for a redundant conversion if the warning mode is |
9099 | -- active (and was not locally reset), and we have a type conversion | |
9100 | -- from source not appearing in a generic instance. | |
9101 | ||
9102 | if Test_Redundant | |
fbf5a39b | 9103 | and then Nkind (Orig_N) = N_Type_Conversion |
ae2aa109 | 9104 | and then Comes_From_Source (Orig_N) |
5453d5bd | 9105 | and then not In_Instance |
996ae0b0 | 9106 | then |
fbf5a39b | 9107 | Orig_N := Original_Node (Expression (Orig_N)); |
b7d1f17f | 9108 | Orig_T := Target_Typ; |
fbf5a39b AC |
9109 | |
9110 | -- If the node is part of a larger expression, the Target_Type | |
9111 | -- may not be the original type of the node if the context is a | |
9112 | -- condition. Recover original type to see if conversion is needed. | |
9113 | ||
9114 | if Is_Boolean_Type (Orig_T) | |
9115 | and then Nkind (Parent (N)) in N_Op | |
9116 | then | |
9117 | Orig_T := Etype (Parent (N)); | |
9118 | end if; | |
9119 | ||
4adf3c50 | 9120 | -- If we have an entity name, then give the warning if the entity |
ae2aa109 AC |
9121 | -- is the right type, or if it is a loop parameter covered by the |
9122 | -- original type (that's needed because loop parameters have an | |
9123 | -- odd subtype coming from the bounds). | |
9124 | ||
9125 | if (Is_Entity_Name (Orig_N) | |
9126 | and then | |
9127 | (Etype (Entity (Orig_N)) = Orig_T | |
9128 | or else | |
9129 | (Ekind (Entity (Orig_N)) = E_Loop_Parameter | |
477bd732 | 9130 | and then Covers (Orig_T, Etype (Entity (Orig_N)))))) |
ae2aa109 | 9131 | |
477bd732 | 9132 | -- If not an entity, then type of expression must match |
ae2aa109 AC |
9133 | |
9134 | or else Etype (Orig_N) = Orig_T | |
fbf5a39b | 9135 | then |
4b2d2c13 AC |
9136 | -- One more check, do not give warning if the analyzed conversion |
9137 | -- has an expression with non-static bounds, and the bounds of the | |
9138 | -- target are static. This avoids junk warnings in cases where the | |
9139 | -- conversion is necessary to establish staticness, for example in | |
9140 | -- a case statement. | |
9141 | ||
9142 | if not Is_OK_Static_Subtype (Operand_Typ) | |
9143 | and then Is_OK_Static_Subtype (Target_Typ) | |
9144 | then | |
9145 | null; | |
9146 | ||
5cc9353d RD |
9147 | -- Finally, if this type conversion occurs in a context requiring |
9148 | -- a prefix, and the expression is a qualified expression then the | |
9149 | -- type conversion is not redundant, since a qualified expression | |
9150 | -- is not a prefix, whereas a type conversion is. For example, "X | |
9151 | -- := T'(Funx(...)).Y;" is illegal because a selected component | |
9152 | -- requires a prefix, but a type conversion makes it legal: "X := | |
9153 | -- T(T'(Funx(...))).Y;" | |
4adf3c50 | 9154 | |
9db0b232 AC |
9155 | -- In Ada 2012, a qualified expression is a name, so this idiom is |
9156 | -- no longer needed, but we still suppress the warning because it | |
9157 | -- seems unfriendly for warnings to pop up when you switch to the | |
9158 | -- newer language version. | |
be257e99 AC |
9159 | |
9160 | elsif Nkind (Orig_N) = N_Qualified_Expression | |
f5d96d00 AC |
9161 | and then Nkind_In (Parent (N), N_Attribute_Reference, |
9162 | N_Indexed_Component, | |
9163 | N_Selected_Component, | |
9164 | N_Slice, | |
9165 | N_Explicit_Dereference) | |
be257e99 AC |
9166 | then |
9167 | null; | |
9168 | ||
ae2aa109 AC |
9169 | -- Here we give the redundant conversion warning. If it is an |
9170 | -- entity, give the name of the entity in the message. If not, | |
9171 | -- just mention the expression. | |
4b2d2c13 AC |
9172 | |
9173 | else | |
ae2aa109 AC |
9174 | if Is_Entity_Name (Orig_N) then |
9175 | Error_Msg_Node_2 := Orig_T; | |
9176 | Error_Msg_NE -- CODEFIX | |
9177 | ("?redundant conversion, & is of type &!", | |
9178 | N, Entity (Orig_N)); | |
9179 | else | |
9180 | Error_Msg_NE | |
9181 | ("?redundant conversion, expression is of type&!", | |
9182 | N, Orig_T); | |
9183 | end if; | |
4b2d2c13 | 9184 | end if; |
fbf5a39b | 9185 | end if; |
996ae0b0 | 9186 | end if; |
758c442c | 9187 | |
b7d1f17f | 9188 | -- Ada 2005 (AI-251): Handle class-wide interface type conversions. |
0669bebe GB |
9189 | -- No need to perform any interface conversion if the type of the |
9190 | -- expression coincides with the target type. | |
758c442c | 9191 | |
0791fbe9 | 9192 | if Ada_Version >= Ada_2005 |
0669bebe | 9193 | and then Expander_Active |
b7d1f17f | 9194 | and then Operand_Typ /= Target_Typ |
0669bebe | 9195 | then |
b7d1f17f HK |
9196 | declare |
9197 | Opnd : Entity_Id := Operand_Typ; | |
9198 | Target : Entity_Id := Target_Typ; | |
758c442c | 9199 | |
b7d1f17f HK |
9200 | begin |
9201 | if Is_Access_Type (Opnd) then | |
841dd0f5 | 9202 | Opnd := Designated_Type (Opnd); |
1420b484 JM |
9203 | end if; |
9204 | ||
b7d1f17f | 9205 | if Is_Access_Type (Target_Typ) then |
841dd0f5 | 9206 | Target := Designated_Type (Target); |
4197ae1e | 9207 | end if; |
c8ef728f | 9208 | |
b7d1f17f HK |
9209 | if Opnd = Target then |
9210 | null; | |
c8ef728f | 9211 | |
b7d1f17f | 9212 | -- Conversion from interface type |
ea985d95 | 9213 | |
b7d1f17f | 9214 | elsif Is_Interface (Opnd) then |
ea985d95 | 9215 | |
b7d1f17f | 9216 | -- Ada 2005 (AI-217): Handle entities from limited views |
aa180613 | 9217 | |
b7d1f17f HK |
9218 | if From_With_Type (Opnd) then |
9219 | Error_Msg_Qual_Level := 99; | |
305caf42 AC |
9220 | Error_Msg_NE -- CODEFIX |
9221 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
9222 | Cunit_Entity (Get_Source_Unit (Base_Type (Opnd)))); |
9223 | Error_Msg_N | |
9224 | ("type conversions require visibility of the full view", | |
9225 | N); | |
aa180613 | 9226 | |
aa5147f0 ES |
9227 | elsif From_With_Type (Target) |
9228 | and then not | |
9229 | (Is_Access_Type (Target_Typ) | |
9230 | and then Present (Non_Limited_View (Etype (Target)))) | |
9231 | then | |
b7d1f17f | 9232 | Error_Msg_Qual_Level := 99; |
305caf42 AC |
9233 | Error_Msg_NE -- CODEFIX |
9234 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
9235 | Cunit_Entity (Get_Source_Unit (Base_Type (Target)))); |
9236 | Error_Msg_N | |
9237 | ("type conversions require visibility of the full view", | |
9238 | N); | |
aa180613 | 9239 | |
b7d1f17f HK |
9240 | else |
9241 | Expand_Interface_Conversion (N, Is_Static => False); | |
9242 | end if; | |
9243 | ||
9244 | -- Conversion to interface type | |
9245 | ||
9246 | elsif Is_Interface (Target) then | |
9247 | ||
9248 | -- Handle subtypes | |
9249 | ||
8a95f4e8 | 9250 | if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then |
b7d1f17f HK |
9251 | Opnd := Etype (Opnd); |
9252 | end if; | |
9253 | ||
9254 | if not Interface_Present_In_Ancestor | |
9255 | (Typ => Opnd, | |
9256 | Iface => Target) | |
9257 | then | |
9258 | if Is_Class_Wide_Type (Opnd) then | |
9259 | ||
9260 | -- The static analysis is not enough to know if the | |
9261 | -- interface is implemented or not. Hence we must pass | |
9262 | -- the work to the expander to generate code to evaluate | |
e7c0dd39 | 9263 | -- the conversion at run time. |
b7d1f17f HK |
9264 | |
9265 | Expand_Interface_Conversion (N, Is_Static => False); | |
9266 | ||
9267 | else | |
9268 | Error_Msg_Name_1 := Chars (Etype (Target)); | |
9269 | Error_Msg_Name_2 := Chars (Opnd); | |
9270 | Error_Msg_N | |
9271 | ("wrong interface conversion (% is not a progenitor " & | |
9272 | "of %)", N); | |
9273 | end if; | |
9274 | ||
9275 | else | |
9276 | Expand_Interface_Conversion (N); | |
9277 | end if; | |
9278 | end if; | |
9279 | end; | |
758c442c | 9280 | end if; |
996ae0b0 RK |
9281 | end Resolve_Type_Conversion; |
9282 | ||
9283 | ---------------------- | |
9284 | -- Resolve_Unary_Op -- | |
9285 | ---------------------- | |
9286 | ||
9287 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
9288 | B_Typ : constant Entity_Id := Base_Type (Typ); |
9289 | R : constant Node_Id := Right_Opnd (N); | |
9290 | OK : Boolean; | |
9291 | Lo : Uint; | |
9292 | Hi : Uint; | |
996ae0b0 RK |
9293 | |
9294 | begin | |
7a489a2b AC |
9295 | if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then |
9296 | Error_Msg_Name_1 := Chars (Typ); | |
9297 | Check_Formal_Restriction | |
9298 | ("unary operator not defined for modular type%", N); | |
9299 | end if; | |
9300 | ||
b7d1f17f | 9301 | -- Deal with intrinsic unary operators |
996ae0b0 | 9302 | |
fbf5a39b AC |
9303 | if Comes_From_Source (N) |
9304 | and then Ekind (Entity (N)) = E_Function | |
9305 | and then Is_Imported (Entity (N)) | |
9306 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
9307 | then | |
9308 | Resolve_Intrinsic_Unary_Operator (N, Typ); | |
9309 | return; | |
9310 | end if; | |
9311 | ||
0669bebe GB |
9312 | -- Deal with universal cases |
9313 | ||
996ae0b0 | 9314 | if Etype (R) = Universal_Integer |
0669bebe GB |
9315 | or else |
9316 | Etype (R) = Universal_Real | |
996ae0b0 RK |
9317 | then |
9318 | Check_For_Visible_Operator (N, B_Typ); | |
9319 | end if; | |
9320 | ||
9321 | Set_Etype (N, B_Typ); | |
9322 | Resolve (R, B_Typ); | |
fbf5a39b | 9323 | |
9ebe3743 HK |
9324 | -- Generate warning for expressions like abs (x mod 2) |
9325 | ||
9326 | if Warn_On_Redundant_Constructs | |
9327 | and then Nkind (N) = N_Op_Abs | |
9328 | then | |
9329 | Determine_Range (Right_Opnd (N), OK, Lo, Hi); | |
9330 | ||
9331 | if OK and then Hi >= Lo and then Lo >= 0 then | |
305caf42 | 9332 | Error_Msg_N -- CODEFIX |
9ebe3743 HK |
9333 | ("?abs applied to known non-negative value has no effect", N); |
9334 | end if; | |
9335 | end if; | |
9336 | ||
0669bebe GB |
9337 | -- Deal with reference generation |
9338 | ||
996ae0b0 | 9339 | Check_Unset_Reference (R); |
fbf5a39b | 9340 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
9341 | Eval_Unary_Op (N); |
9342 | ||
9343 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
9344 | -- and perhaps the Resolve routines should be separated for the various | |
9345 | -- arithmetic operations, since they will need different processing ??? | |
9346 | ||
9347 | if Nkind (N) in N_Op then | |
9348 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 9349 | Enable_Overflow_Check (N); |
996ae0b0 RK |
9350 | end if; |
9351 | end if; | |
0669bebe | 9352 | |
d81b4bfe TQ |
9353 | -- Generate warning for expressions like -5 mod 3 for integers. No need |
9354 | -- to worry in the floating-point case, since parens do not affect the | |
9355 | -- result so there is no point in giving in a warning. | |
0669bebe GB |
9356 | |
9357 | declare | |
9358 | Norig : constant Node_Id := Original_Node (N); | |
9359 | Rorig : Node_Id; | |
9360 | Val : Uint; | |
9361 | HB : Uint; | |
9362 | LB : Uint; | |
9363 | Lval : Uint; | |
9364 | Opnd : Node_Id; | |
9365 | ||
9366 | begin | |
9367 | if Warn_On_Questionable_Missing_Parens | |
9368 | and then Comes_From_Source (Norig) | |
9369 | and then Is_Integer_Type (Typ) | |
9370 | and then Nkind (Norig) = N_Op_Minus | |
9371 | then | |
9372 | Rorig := Original_Node (Right_Opnd (Norig)); | |
9373 | ||
9374 | -- We are looking for cases where the right operand is not | |
f3d57416 | 9375 | -- parenthesized, and is a binary operator, multiply, divide, or |
0669bebe GB |
9376 | -- mod. These are the cases where the grouping can affect results. |
9377 | ||
9378 | if Paren_Count (Rorig) = 0 | |
45fc7ddb | 9379 | and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide) |
0669bebe GB |
9380 | then |
9381 | -- For mod, we always give the warning, since the value is | |
9382 | -- affected by the parenthesization (e.g. (-5) mod 315 /= | |
d81b4bfe | 9383 | -- -(5 mod 315)). But for the other cases, the only concern is |
0669bebe GB |
9384 | -- overflow, e.g. for the case of 8 big signed (-(2 * 64) |
9385 | -- overflows, but (-2) * 64 does not). So we try to give the | |
9386 | -- message only when overflow is possible. | |
9387 | ||
9388 | if Nkind (Rorig) /= N_Op_Mod | |
9389 | and then Compile_Time_Known_Value (R) | |
9390 | then | |
9391 | Val := Expr_Value (R); | |
9392 | ||
9393 | if Compile_Time_Known_Value (Type_High_Bound (Typ)) then | |
9394 | HB := Expr_Value (Type_High_Bound (Typ)); | |
9395 | else | |
9396 | HB := Expr_Value (Type_High_Bound (Base_Type (Typ))); | |
9397 | end if; | |
9398 | ||
9399 | if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then | |
9400 | LB := Expr_Value (Type_Low_Bound (Typ)); | |
9401 | else | |
9402 | LB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); | |
9403 | end if; | |
9404 | ||
d81b4bfe TQ |
9405 | -- Note that the test below is deliberately excluding the |
9406 | -- largest negative number, since that is a potentially | |
0669bebe GB |
9407 | -- troublesome case (e.g. -2 * x, where the result is the |
9408 | -- largest negative integer has an overflow with 2 * x). | |
9409 | ||
9410 | if Val > LB and then Val <= HB then | |
9411 | return; | |
9412 | end if; | |
9413 | end if; | |
9414 | ||
9415 | -- For the multiplication case, the only case we have to worry | |
9416 | -- about is when (-a)*b is exactly the largest negative number | |
9417 | -- so that -(a*b) can cause overflow. This can only happen if | |
9418 | -- a is a power of 2, and more generally if any operand is a | |
9419 | -- constant that is not a power of 2, then the parentheses | |
9420 | -- cannot affect whether overflow occurs. We only bother to | |
9421 | -- test the left most operand | |
9422 | ||
9423 | -- Loop looking at left operands for one that has known value | |
9424 | ||
9425 | Opnd := Rorig; | |
9426 | Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop | |
9427 | if Compile_Time_Known_Value (Left_Opnd (Opnd)) then | |
9428 | Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd))); | |
9429 | ||
9430 | -- Operand value of 0 or 1 skips warning | |
9431 | ||
9432 | if Lval <= 1 then | |
9433 | return; | |
9434 | ||
9435 | -- Otherwise check power of 2, if power of 2, warn, if | |
9436 | -- anything else, skip warning. | |
9437 | ||
9438 | else | |
9439 | while Lval /= 2 loop | |
9440 | if Lval mod 2 = 1 then | |
9441 | return; | |
9442 | else | |
9443 | Lval := Lval / 2; | |
9444 | end if; | |
9445 | end loop; | |
9446 | ||
9447 | exit Opnd_Loop; | |
9448 | end if; | |
9449 | end if; | |
9450 | ||
9451 | -- Keep looking at left operands | |
9452 | ||
9453 | Opnd := Left_Opnd (Opnd); | |
9454 | end loop Opnd_Loop; | |
9455 | ||
9456 | -- For rem or "/" we can only have a problematic situation | |
9457 | -- if the divisor has a value of minus one or one. Otherwise | |
9458 | -- overflow is impossible (divisor > 1) or we have a case of | |
9459 | -- division by zero in any case. | |
9460 | ||
45fc7ddb | 9461 | if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem) |
0669bebe GB |
9462 | and then Compile_Time_Known_Value (Right_Opnd (Rorig)) |
9463 | and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1 | |
9464 | then | |
9465 | return; | |
9466 | end if; | |
9467 | ||
9468 | -- If we fall through warning should be issued | |
9469 | ||
ed2233dc | 9470 | Error_Msg_N |
aa5147f0 | 9471 | ("?unary minus expression should be parenthesized here!", N); |
0669bebe GB |
9472 | end if; |
9473 | end if; | |
9474 | end; | |
996ae0b0 RK |
9475 | end Resolve_Unary_Op; |
9476 | ||
9477 | ---------------------------------- | |
9478 | -- Resolve_Unchecked_Expression -- | |
9479 | ---------------------------------- | |
9480 | ||
9481 | procedure Resolve_Unchecked_Expression | |
9482 | (N : Node_Id; | |
9483 | Typ : Entity_Id) | |
9484 | is | |
9485 | begin | |
9486 | Resolve (Expression (N), Typ, Suppress => All_Checks); | |
9487 | Set_Etype (N, Typ); | |
9488 | end Resolve_Unchecked_Expression; | |
9489 | ||
9490 | --------------------------------------- | |
9491 | -- Resolve_Unchecked_Type_Conversion -- | |
9492 | --------------------------------------- | |
9493 | ||
9494 | procedure Resolve_Unchecked_Type_Conversion | |
9495 | (N : Node_Id; | |
9496 | Typ : Entity_Id) | |
9497 | is | |
07fc65c4 GB |
9498 | pragma Warnings (Off, Typ); |
9499 | ||
996ae0b0 RK |
9500 | Operand : constant Node_Id := Expression (N); |
9501 | Opnd_Type : constant Entity_Id := Etype (Operand); | |
9502 | ||
9503 | begin | |
a77842bd | 9504 | -- Resolve operand using its own type |
996ae0b0 RK |
9505 | |
9506 | Resolve (Operand, Opnd_Type); | |
9507 | Eval_Unchecked_Conversion (N); | |
996ae0b0 RK |
9508 | end Resolve_Unchecked_Type_Conversion; |
9509 | ||
9510 | ------------------------------ | |
9511 | -- Rewrite_Operator_As_Call -- | |
9512 | ------------------------------ | |
9513 | ||
9514 | procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is | |
fbf5a39b AC |
9515 | Loc : constant Source_Ptr := Sloc (N); |
9516 | Actuals : constant List_Id := New_List; | |
996ae0b0 RK |
9517 | New_N : Node_Id; |
9518 | ||
9519 | begin | |
9520 | if Nkind (N) in N_Binary_Op then | |
9521 | Append (Left_Opnd (N), Actuals); | |
9522 | end if; | |
9523 | ||
9524 | Append (Right_Opnd (N), Actuals); | |
9525 | ||
9526 | New_N := | |
9527 | Make_Function_Call (Sloc => Loc, | |
9528 | Name => New_Occurrence_Of (Nam, Loc), | |
9529 | Parameter_Associations => Actuals); | |
9530 | ||
9531 | Preserve_Comes_From_Source (New_N, N); | |
9532 | Preserve_Comes_From_Source (Name (New_N), N); | |
9533 | Rewrite (N, New_N); | |
9534 | Set_Etype (N, Etype (Nam)); | |
9535 | end Rewrite_Operator_As_Call; | |
9536 | ||
9537 | ------------------------------ | |
9538 | -- Rewrite_Renamed_Operator -- | |
9539 | ------------------------------ | |
9540 | ||
0ab80019 AC |
9541 | procedure Rewrite_Renamed_Operator |
9542 | (N : Node_Id; | |
9543 | Op : Entity_Id; | |
9544 | Typ : Entity_Id) | |
9545 | is | |
996ae0b0 RK |
9546 | Nam : constant Name_Id := Chars (Op); |
9547 | Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
9548 | Op_Node : Node_Id; | |
9549 | ||
9550 | begin | |
d81b4bfe TQ |
9551 | -- Rewrite the operator node using the real operator, not its renaming. |
9552 | -- Exclude user-defined intrinsic operations of the same name, which are | |
9553 | -- treated separately and rewritten as calls. | |
996ae0b0 | 9554 | |
964f13da | 9555 | if Ekind (Op) /= E_Function or else Chars (N) /= Nam then |
996ae0b0 RK |
9556 | Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N)); |
9557 | Set_Chars (Op_Node, Nam); | |
9558 | Set_Etype (Op_Node, Etype (N)); | |
9559 | Set_Entity (Op_Node, Op); | |
9560 | Set_Right_Opnd (Op_Node, Right_Opnd (N)); | |
9561 | ||
b7d1f17f HK |
9562 | -- Indicate that both the original entity and its renaming are |
9563 | -- referenced at this point. | |
fbf5a39b AC |
9564 | |
9565 | Generate_Reference (Entity (N), N); | |
996ae0b0 RK |
9566 | Generate_Reference (Op, N); |
9567 | ||
9568 | if Is_Binary then | |
9569 | Set_Left_Opnd (Op_Node, Left_Opnd (N)); | |
9570 | end if; | |
9571 | ||
9572 | Rewrite (N, Op_Node); | |
0ab80019 | 9573 | |
1366997b AC |
9574 | -- If the context type is private, add the appropriate conversions so |
9575 | -- that the operator is applied to the full view. This is done in the | |
9576 | -- routines that resolve intrinsic operators. | |
0ab80019 AC |
9577 | |
9578 | if Is_Intrinsic_Subprogram (Op) | |
9579 | and then Is_Private_Type (Typ) | |
9580 | then | |
9581 | case Nkind (N) is | |
9582 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | | |
9583 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
9584 | Resolve_Intrinsic_Operator (N, Typ); | |
9585 | ||
d81b4bfe | 9586 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => |
0ab80019 AC |
9587 | Resolve_Intrinsic_Unary_Operator (N, Typ); |
9588 | ||
9589 | when others => | |
9590 | Resolve (N, Typ); | |
9591 | end case; | |
9592 | end if; | |
9593 | ||
964f13da RD |
9594 | elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then |
9595 | ||
1366997b AC |
9596 | -- Operator renames a user-defined operator of the same name. Use the |
9597 | -- original operator in the node, which is the one Gigi knows about. | |
0ab80019 AC |
9598 | |
9599 | Set_Entity (N, Op); | |
9600 | Set_Is_Overloaded (N, False); | |
996ae0b0 RK |
9601 | end if; |
9602 | end Rewrite_Renamed_Operator; | |
9603 | ||
9604 | ----------------------- | |
9605 | -- Set_Slice_Subtype -- | |
9606 | ----------------------- | |
9607 | ||
1366997b AC |
9608 | -- Build an implicit subtype declaration to represent the type delivered by |
9609 | -- the slice. This is an abbreviated version of an array subtype. We define | |
9610 | -- an index subtype for the slice, using either the subtype name or the | |
9611 | -- discrete range of the slice. To be consistent with index usage elsewhere | |
9612 | -- we create a list header to hold the single index. This list is not | |
9613 | -- otherwise attached to the syntax tree. | |
996ae0b0 RK |
9614 | |
9615 | procedure Set_Slice_Subtype (N : Node_Id) is | |
9616 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 9617 | Index_List : constant List_Id := New_List; |
996ae0b0 | 9618 | Index : Node_Id; |
996ae0b0 RK |
9619 | Index_Subtype : Entity_Id; |
9620 | Index_Type : Entity_Id; | |
9621 | Slice_Subtype : Entity_Id; | |
9622 | Drange : constant Node_Id := Discrete_Range (N); | |
9623 | ||
9624 | begin | |
9625 | if Is_Entity_Name (Drange) then | |
9626 | Index_Subtype := Entity (Drange); | |
9627 | ||
9628 | else | |
9629 | -- We force the evaluation of a range. This is definitely needed in | |
9630 | -- the renamed case, and seems safer to do unconditionally. Note in | |
9631 | -- any case that since we will create and insert an Itype referring | |
9632 | -- to this range, we must make sure any side effect removal actions | |
9633 | -- are inserted before the Itype definition. | |
9634 | ||
9635 | if Nkind (Drange) = N_Range then | |
9636 | Force_Evaluation (Low_Bound (Drange)); | |
9637 | Force_Evaluation (High_Bound (Drange)); | |
9638 | end if; | |
9639 | ||
9640 | Index_Type := Base_Type (Etype (Drange)); | |
9641 | ||
9642 | Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
9643 | ||
8a95f4e8 | 9644 | -- Take a new copy of Drange (where bounds have been rewritten to |
3c1ecd7e AC |
9645 | -- reference side-effect-free names). Using a separate tree ensures |
9646 | -- that further expansion (e.g. while rewriting a slice assignment | |
8a95f4e8 RD |
9647 | -- into a FOR loop) does not attempt to remove side effects on the |
9648 | -- bounds again (which would cause the bounds in the index subtype | |
9649 | -- definition to refer to temporaries before they are defined) (the | |
9650 | -- reason is that some names are considered side effect free here | |
9651 | -- for the subtype, but not in the context of a loop iteration | |
9652 | -- scheme). | |
9653 | ||
9654 | Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange)); | |
4230bdb7 | 9655 | Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype); |
996ae0b0 RK |
9656 | Set_Etype (Index_Subtype, Index_Type); |
9657 | Set_Size_Info (Index_Subtype, Index_Type); | |
9658 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
9659 | end if; | |
9660 | ||
9661 | Slice_Subtype := Create_Itype (E_Array_Subtype, N); | |
9662 | ||
9663 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
9664 | Set_Etype (Index, Index_Subtype); | |
9665 | Append (Index, Index_List); | |
9666 | ||
996ae0b0 RK |
9667 | Set_First_Index (Slice_Subtype, Index); |
9668 | Set_Etype (Slice_Subtype, Base_Type (Etype (N))); | |
9669 | Set_Is_Constrained (Slice_Subtype, True); | |
996ae0b0 | 9670 | |
8a95f4e8 RD |
9671 | Check_Compile_Time_Size (Slice_Subtype); |
9672 | ||
b7d1f17f HK |
9673 | -- The Etype of the existing Slice node is reset to this slice subtype. |
9674 | -- Its bounds are obtained from its first index. | |
996ae0b0 RK |
9675 | |
9676 | Set_Etype (N, Slice_Subtype); | |
9677 | ||
5cc9353d RD |
9678 | -- For packed slice subtypes, freeze immediately (except in the case of |
9679 | -- being in a "spec expression" where we never freeze when we first see | |
9680 | -- the expression). | |
8a95f4e8 RD |
9681 | |
9682 | if Is_Packed (Slice_Subtype) and not In_Spec_Expression then | |
9683 | Freeze_Itype (Slice_Subtype, N); | |
996ae0b0 | 9684 | |
cfab0c49 AC |
9685 | -- For all other cases insert an itype reference in the slice's actions |
9686 | -- so that the itype is frozen at the proper place in the tree (i.e. at | |
9687 | -- the point where actions for the slice are analyzed). Note that this | |
9688 | -- is different from freezing the itype immediately, which might be | |
9689 | -- premature (e.g. if the slice is within a transient scope). | |
9690 | ||
8a95f4e8 RD |
9691 | else |
9692 | Ensure_Defined (Typ => Slice_Subtype, N => N); | |
9693 | end if; | |
996ae0b0 RK |
9694 | end Set_Slice_Subtype; |
9695 | ||
9696 | -------------------------------- | |
9697 | -- Set_String_Literal_Subtype -- | |
9698 | -------------------------------- | |
9699 | ||
9700 | procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is | |
c8ef728f ES |
9701 | Loc : constant Source_Ptr := Sloc (N); |
9702 | Low_Bound : constant Node_Id := | |
d81b4bfe | 9703 | Type_Low_Bound (Etype (First_Index (Typ))); |
996ae0b0 RK |
9704 | Subtype_Id : Entity_Id; |
9705 | ||
9706 | begin | |
9707 | if Nkind (N) /= N_String_Literal then | |
9708 | return; | |
996ae0b0 RK |
9709 | end if; |
9710 | ||
c8ef728f | 9711 | Subtype_Id := Create_Itype (E_String_Literal_Subtype, N); |
91b1417d AC |
9712 | Set_String_Literal_Length (Subtype_Id, UI_From_Int |
9713 | (String_Length (Strval (N)))); | |
c8ef728f ES |
9714 | Set_Etype (Subtype_Id, Base_Type (Typ)); |
9715 | Set_Is_Constrained (Subtype_Id); | |
9716 | Set_Etype (N, Subtype_Id); | |
9717 | ||
9718 | if Is_OK_Static_Expression (Low_Bound) then | |
996ae0b0 | 9719 | |
1366997b AC |
9720 | -- The low bound is set from the low bound of the corresponding index |
9721 | -- type. Note that we do not store the high bound in the string literal | |
9722 | -- subtype, but it can be deduced if necessary from the length and the | |
9723 | -- low bound. | |
996ae0b0 | 9724 | |
c8ef728f | 9725 | Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound); |
996ae0b0 | 9726 | |
c8ef728f ES |
9727 | else |
9728 | Set_String_Literal_Low_Bound | |
9729 | (Subtype_Id, Make_Integer_Literal (Loc, 1)); | |
9730 | Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive); | |
9731 | ||
b7d1f17f HK |
9732 | -- Build bona fide subtype for the string, and wrap it in an |
9733 | -- unchecked conversion, because the backend expects the | |
c8ef728f ES |
9734 | -- String_Literal_Subtype to have a static lower bound. |
9735 | ||
9736 | declare | |
9737 | Index_List : constant List_Id := New_List; | |
9738 | Index_Type : constant Entity_Id := Etype (First_Index (Typ)); | |
9739 | High_Bound : constant Node_Id := | |
9740 | Make_Op_Add (Loc, | |
9741 | Left_Opnd => New_Copy_Tree (Low_Bound), | |
9742 | Right_Opnd => | |
9743 | Make_Integer_Literal (Loc, | |
9744 | String_Length (Strval (N)) - 1)); | |
9745 | Array_Subtype : Entity_Id; | |
9746 | Index_Subtype : Entity_Id; | |
9747 | Drange : Node_Id; | |
9748 | Index : Node_Id; | |
9749 | ||
9750 | begin | |
9751 | Index_Subtype := | |
9752 | Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
0669bebe | 9753 | Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound); |
c8ef728f ES |
9754 | Set_Scalar_Range (Index_Subtype, Drange); |
9755 | Set_Parent (Drange, N); | |
9756 | Analyze_And_Resolve (Drange, Index_Type); | |
9757 | ||
36fcf362 RD |
9758 | -- In the context, the Index_Type may already have a constraint, |
9759 | -- so use common base type on string subtype. The base type may | |
9760 | -- be used when generating attributes of the string, for example | |
9761 | -- in the context of a slice assignment. | |
9762 | ||
4adf3c50 AC |
9763 | Set_Etype (Index_Subtype, Base_Type (Index_Type)); |
9764 | Set_Size_Info (Index_Subtype, Index_Type); | |
9765 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
c8ef728f ES |
9766 | |
9767 | Array_Subtype := Create_Itype (E_Array_Subtype, N); | |
9768 | ||
9769 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
9770 | Set_Etype (Index, Index_Subtype); | |
9771 | Append (Index, Index_List); | |
9772 | ||
9773 | Set_First_Index (Array_Subtype, Index); | |
9774 | Set_Etype (Array_Subtype, Base_Type (Typ)); | |
9775 | Set_Is_Constrained (Array_Subtype, True); | |
c8ef728f ES |
9776 | |
9777 | Rewrite (N, | |
9778 | Make_Unchecked_Type_Conversion (Loc, | |
9779 | Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc), | |
9780 | Expression => Relocate_Node (N))); | |
9781 | Set_Etype (N, Array_Subtype); | |
9782 | end; | |
9783 | end if; | |
996ae0b0 RK |
9784 | end Set_String_Literal_Subtype; |
9785 | ||
0669bebe GB |
9786 | ------------------------------ |
9787 | -- Simplify_Type_Conversion -- | |
9788 | ------------------------------ | |
9789 | ||
9790 | procedure Simplify_Type_Conversion (N : Node_Id) is | |
9791 | begin | |
9792 | if Nkind (N) = N_Type_Conversion then | |
9793 | declare | |
9794 | Operand : constant Node_Id := Expression (N); | |
9795 | Target_Typ : constant Entity_Id := Etype (N); | |
9796 | Opnd_Typ : constant Entity_Id := Etype (Operand); | |
9797 | ||
9798 | begin | |
9799 | if Is_Floating_Point_Type (Opnd_Typ) | |
9800 | and then | |
9801 | (Is_Integer_Type (Target_Typ) | |
9802 | or else (Is_Fixed_Point_Type (Target_Typ) | |
9803 | and then Conversion_OK (N))) | |
9804 | and then Nkind (Operand) = N_Attribute_Reference | |
9805 | and then Attribute_Name (Operand) = Name_Truncation | |
9806 | ||
9807 | -- Special processing required if the conversion is the expression | |
9808 | -- of a Truncation attribute reference. In this case we replace: | |
9809 | ||
9810 | -- ityp (ftyp'Truncation (x)) | |
9811 | ||
9812 | -- by | |
9813 | ||
9814 | -- ityp (x) | |
9815 | ||
4adf3c50 | 9816 | -- with the Float_Truncate flag set, which is more efficient. |
0669bebe GB |
9817 | |
9818 | then | |
9819 | Rewrite (Operand, | |
9820 | Relocate_Node (First (Expressions (Operand)))); | |
9821 | Set_Float_Truncate (N, True); | |
9822 | end if; | |
9823 | end; | |
9824 | end if; | |
9825 | end Simplify_Type_Conversion; | |
9826 | ||
996ae0b0 RK |
9827 | ----------------------------- |
9828 | -- Unique_Fixed_Point_Type -- | |
9829 | ----------------------------- | |
9830 | ||
9831 | function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is | |
9832 | T1 : Entity_Id := Empty; | |
9833 | T2 : Entity_Id; | |
9834 | Item : Node_Id; | |
9835 | Scop : Entity_Id; | |
9836 | ||
9837 | procedure Fixed_Point_Error; | |
d81b4bfe TQ |
9838 | -- Give error messages for true ambiguity. Messages are posted on node |
9839 | -- N, and entities T1, T2 are the possible interpretations. | |
a77842bd TQ |
9840 | |
9841 | ----------------------- | |
9842 | -- Fixed_Point_Error -- | |
9843 | ----------------------- | |
996ae0b0 RK |
9844 | |
9845 | procedure Fixed_Point_Error is | |
9846 | begin | |
ed2233dc AC |
9847 | Error_Msg_N ("ambiguous universal_fixed_expression", N); |
9848 | Error_Msg_NE ("\\possible interpretation as}", N, T1); | |
9849 | Error_Msg_NE ("\\possible interpretation as}", N, T2); | |
996ae0b0 RK |
9850 | end Fixed_Point_Error; |
9851 | ||
a77842bd TQ |
9852 | -- Start of processing for Unique_Fixed_Point_Type |
9853 | ||
996ae0b0 RK |
9854 | begin |
9855 | -- The operations on Duration are visible, so Duration is always a | |
9856 | -- possible interpretation. | |
9857 | ||
9858 | T1 := Standard_Duration; | |
9859 | ||
bc5f3720 | 9860 | -- Look for fixed-point types in enclosing scopes |
996ae0b0 | 9861 | |
fbf5a39b | 9862 | Scop := Current_Scope; |
996ae0b0 RK |
9863 | while Scop /= Standard_Standard loop |
9864 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9865 | while Present (T2) loop |
9866 | if Is_Fixed_Point_Type (T2) | |
9867 | and then Current_Entity (T2) = T2 | |
9868 | and then Scope (Base_Type (T2)) = Scop | |
9869 | then | |
9870 | if Present (T1) then | |
9871 | Fixed_Point_Error; | |
9872 | return Any_Type; | |
9873 | else | |
9874 | T1 := T2; | |
9875 | end if; | |
9876 | end if; | |
9877 | ||
9878 | Next_Entity (T2); | |
9879 | end loop; | |
9880 | ||
9881 | Scop := Scope (Scop); | |
9882 | end loop; | |
9883 | ||
a77842bd | 9884 | -- Look for visible fixed type declarations in the context |
996ae0b0 RK |
9885 | |
9886 | Item := First (Context_Items (Cunit (Current_Sem_Unit))); | |
996ae0b0 | 9887 | while Present (Item) loop |
996ae0b0 RK |
9888 | if Nkind (Item) = N_With_Clause then |
9889 | Scop := Entity (Name (Item)); | |
9890 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9891 | while Present (T2) loop |
9892 | if Is_Fixed_Point_Type (T2) | |
9893 | and then Scope (Base_Type (T2)) = Scop | |
19fb051c | 9894 | and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2)) |
996ae0b0 RK |
9895 | then |
9896 | if Present (T1) then | |
9897 | Fixed_Point_Error; | |
9898 | return Any_Type; | |
9899 | else | |
9900 | T1 := T2; | |
9901 | end if; | |
9902 | end if; | |
9903 | ||
9904 | Next_Entity (T2); | |
9905 | end loop; | |
9906 | end if; | |
9907 | ||
9908 | Next (Item); | |
9909 | end loop; | |
9910 | ||
9911 | if Nkind (N) = N_Real_Literal then | |
aa5147f0 | 9912 | Error_Msg_NE ("?real literal interpreted as }!", N, T1); |
996ae0b0 | 9913 | else |
aa5147f0 | 9914 | Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1); |
996ae0b0 RK |
9915 | end if; |
9916 | ||
9917 | return T1; | |
9918 | end Unique_Fixed_Point_Type; | |
9919 | ||
9920 | ---------------------- | |
9921 | -- Valid_Conversion -- | |
9922 | ---------------------- | |
9923 | ||
9924 | function Valid_Conversion | |
9925 | (N : Node_Id; | |
9926 | Target : Entity_Id; | |
0ab80019 | 9927 | Operand : Node_Id) return Boolean |
996ae0b0 | 9928 | is |
fbf5a39b | 9929 | Target_Type : constant Entity_Id := Base_Type (Target); |
996ae0b0 RK |
9930 | Opnd_Type : Entity_Id := Etype (Operand); |
9931 | ||
9932 | function Conversion_Check | |
9933 | (Valid : Boolean; | |
0ab80019 | 9934 | Msg : String) return Boolean; |
996ae0b0 RK |
9935 | -- Little routine to post Msg if Valid is False, returns Valid value |
9936 | ||
9937 | function Valid_Tagged_Conversion | |
9938 | (Target_Type : Entity_Id; | |
0ab80019 | 9939 | Opnd_Type : Entity_Id) return Boolean; |
996ae0b0 RK |
9940 | -- Specifically test for validity of tagged conversions |
9941 | ||
aa180613 | 9942 | function Valid_Array_Conversion return Boolean; |
4adf3c50 AC |
9943 | -- Check index and component conformance, and accessibility levels if |
9944 | -- the component types are anonymous access types (Ada 2005). | |
aa180613 | 9945 | |
996ae0b0 RK |
9946 | ---------------------- |
9947 | -- Conversion_Check -- | |
9948 | ---------------------- | |
9949 | ||
9950 | function Conversion_Check | |
9951 | (Valid : Boolean; | |
0ab80019 | 9952 | Msg : String) return Boolean |
996ae0b0 RK |
9953 | is |
9954 | begin | |
9955 | if not Valid then | |
9956 | Error_Msg_N (Msg, Operand); | |
9957 | end if; | |
9958 | ||
9959 | return Valid; | |
9960 | end Conversion_Check; | |
9961 | ||
aa180613 RD |
9962 | ---------------------------- |
9963 | -- Valid_Array_Conversion -- | |
9964 | ---------------------------- | |
9965 | ||
9966 | function Valid_Array_Conversion return Boolean | |
9967 | is | |
9968 | Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type); | |
9969 | Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type); | |
9970 | ||
9971 | Opnd_Index : Node_Id; | |
9972 | Opnd_Index_Type : Entity_Id; | |
9973 | ||
9974 | Target_Comp_Type : constant Entity_Id := | |
9975 | Component_Type (Target_Type); | |
9976 | Target_Comp_Base : constant Entity_Id := | |
9977 | Base_Type (Target_Comp_Type); | |
9978 | ||
9979 | Target_Index : Node_Id; | |
9980 | Target_Index_Type : Entity_Id; | |
9981 | ||
9982 | begin | |
9983 | -- Error if wrong number of dimensions | |
9984 | ||
9985 | if | |
9986 | Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type) | |
9987 | then | |
9988 | Error_Msg_N | |
9989 | ("incompatible number of dimensions for conversion", Operand); | |
9990 | return False; | |
9991 | ||
9992 | -- Number of dimensions matches | |
9993 | ||
9994 | else | |
9995 | -- Loop through indexes of the two arrays | |
9996 | ||
9997 | Target_Index := First_Index (Target_Type); | |
9998 | Opnd_Index := First_Index (Opnd_Type); | |
9999 | while Present (Target_Index) and then Present (Opnd_Index) loop | |
10000 | Target_Index_Type := Etype (Target_Index); | |
10001 | Opnd_Index_Type := Etype (Opnd_Index); | |
10002 | ||
10003 | -- Error if index types are incompatible | |
10004 | ||
10005 | if not (Is_Integer_Type (Target_Index_Type) | |
10006 | and then Is_Integer_Type (Opnd_Index_Type)) | |
10007 | and then (Root_Type (Target_Index_Type) | |
10008 | /= Root_Type (Opnd_Index_Type)) | |
10009 | then | |
10010 | Error_Msg_N | |
10011 | ("incompatible index types for array conversion", | |
10012 | Operand); | |
10013 | return False; | |
10014 | end if; | |
10015 | ||
10016 | Next_Index (Target_Index); | |
10017 | Next_Index (Opnd_Index); | |
10018 | end loop; | |
10019 | ||
10020 | -- If component types have same base type, all set | |
10021 | ||
10022 | if Target_Comp_Base = Opnd_Comp_Base then | |
10023 | null; | |
10024 | ||
10025 | -- Here if base types of components are not the same. The only | |
10026 | -- time this is allowed is if we have anonymous access types. | |
10027 | ||
10028 | -- The conversion of arrays of anonymous access types can lead | |
10029 | -- to dangling pointers. AI-392 formalizes the accessibility | |
10030 | -- checks that must be applied to such conversions to prevent | |
10031 | -- out-of-scope references. | |
10032 | ||
19fb051c AC |
10033 | elsif Ekind_In |
10034 | (Target_Comp_Base, E_Anonymous_Access_Type, | |
10035 | E_Anonymous_Access_Subprogram_Type) | |
aa180613 RD |
10036 | and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base) |
10037 | and then | |
10038 | Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type) | |
10039 | then | |
10040 | if Type_Access_Level (Target_Type) < | |
10041 | Type_Access_Level (Opnd_Type) | |
10042 | then | |
10043 | if In_Instance_Body then | |
10044 | Error_Msg_N ("?source array type " & | |
10045 | "has deeper accessibility level than target", Operand); | |
10046 | Error_Msg_N ("\?Program_Error will be raised at run time", | |
10047 | Operand); | |
10048 | Rewrite (N, | |
10049 | Make_Raise_Program_Error (Sloc (N), | |
10050 | Reason => PE_Accessibility_Check_Failed)); | |
10051 | Set_Etype (N, Target_Type); | |
10052 | return False; | |
10053 | ||
10054 | -- Conversion not allowed because of accessibility levels | |
10055 | ||
10056 | else | |
10057 | Error_Msg_N ("source array type " & | |
10058 | "has deeper accessibility level than target", Operand); | |
10059 | return False; | |
10060 | end if; | |
19fb051c | 10061 | |
aa180613 RD |
10062 | else |
10063 | null; | |
10064 | end if; | |
10065 | ||
10066 | -- All other cases where component base types do not match | |
10067 | ||
10068 | else | |
10069 | Error_Msg_N | |
10070 | ("incompatible component types for array conversion", | |
10071 | Operand); | |
10072 | return False; | |
10073 | end if; | |
10074 | ||
45fc7ddb HK |
10075 | -- Check that component subtypes statically match. For numeric |
10076 | -- types this means that both must be either constrained or | |
10077 | -- unconstrained. For enumeration types the bounds must match. | |
10078 | -- All of this is checked in Subtypes_Statically_Match. | |
aa180613 | 10079 | |
45fc7ddb | 10080 | if not Subtypes_Statically_Match |
aa180613 RD |
10081 | (Target_Comp_Type, Opnd_Comp_Type) |
10082 | then | |
10083 | Error_Msg_N | |
10084 | ("component subtypes must statically match", Operand); | |
10085 | return False; | |
10086 | end if; | |
10087 | end if; | |
10088 | ||
10089 | return True; | |
10090 | end Valid_Array_Conversion; | |
10091 | ||
996ae0b0 RK |
10092 | ----------------------------- |
10093 | -- Valid_Tagged_Conversion -- | |
10094 | ----------------------------- | |
10095 | ||
10096 | function Valid_Tagged_Conversion | |
10097 | (Target_Type : Entity_Id; | |
0ab80019 | 10098 | Opnd_Type : Entity_Id) return Boolean |
996ae0b0 RK |
10099 | is |
10100 | begin | |
a77842bd | 10101 | -- Upward conversions are allowed (RM 4.6(22)) |
996ae0b0 RK |
10102 | |
10103 | if Covers (Target_Type, Opnd_Type) | |
10104 | or else Is_Ancestor (Target_Type, Opnd_Type) | |
10105 | then | |
10106 | return True; | |
10107 | ||
a77842bd TQ |
10108 | -- Downward conversion are allowed if the operand is class-wide |
10109 | -- (RM 4.6(23)). | |
996ae0b0 RK |
10110 | |
10111 | elsif Is_Class_Wide_Type (Opnd_Type) | |
b7d1f17f | 10112 | and then Covers (Opnd_Type, Target_Type) |
996ae0b0 RK |
10113 | then |
10114 | return True; | |
10115 | ||
10116 | elsif Covers (Opnd_Type, Target_Type) | |
10117 | or else Is_Ancestor (Opnd_Type, Target_Type) | |
10118 | then | |
10119 | return | |
10120 | Conversion_Check (False, | |
10121 | "downward conversion of tagged objects not allowed"); | |
758c442c | 10122 | |
0669bebe GB |
10123 | -- Ada 2005 (AI-251): The conversion to/from interface types is |
10124 | -- always valid | |
758c442c | 10125 | |
0669bebe | 10126 | elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then |
758c442c GD |
10127 | return True; |
10128 | ||
b7d1f17f HK |
10129 | -- If the operand is a class-wide type obtained through a limited_ |
10130 | -- with clause, and the context includes the non-limited view, use | |
10131 | -- it to determine whether the conversion is legal. | |
10132 | ||
10133 | elsif Is_Class_Wide_Type (Opnd_Type) | |
10134 | and then From_With_Type (Opnd_Type) | |
10135 | and then Present (Non_Limited_View (Etype (Opnd_Type))) | |
10136 | and then Is_Interface (Non_Limited_View (Etype (Opnd_Type))) | |
10137 | then | |
10138 | return True; | |
10139 | ||
aa180613 RD |
10140 | elsif Is_Access_Type (Opnd_Type) |
10141 | and then Is_Interface (Directly_Designated_Type (Opnd_Type)) | |
10142 | then | |
10143 | return True; | |
10144 | ||
996ae0b0 RK |
10145 | else |
10146 | Error_Msg_NE | |
10147 | ("invalid tagged conversion, not compatible with}", | |
10148 | N, First_Subtype (Opnd_Type)); | |
10149 | return False; | |
10150 | end if; | |
10151 | end Valid_Tagged_Conversion; | |
10152 | ||
10153 | -- Start of processing for Valid_Conversion | |
10154 | ||
10155 | begin | |
10156 | Check_Parameterless_Call (Operand); | |
10157 | ||
10158 | if Is_Overloaded (Operand) then | |
10159 | declare | |
10160 | I : Interp_Index; | |
10161 | I1 : Interp_Index; | |
10162 | It : Interp; | |
10163 | It1 : Interp; | |
10164 | N1 : Entity_Id; | |
f0d10385 | 10165 | T1 : Entity_Id; |
996ae0b0 RK |
10166 | |
10167 | begin | |
d81b4bfe TQ |
10168 | -- Remove procedure calls, which syntactically cannot appear in |
10169 | -- this context, but which cannot be removed by type checking, | |
996ae0b0 RK |
10170 | -- because the context does not impose a type. |
10171 | ||
1420b484 JM |
10172 | -- When compiling for VMS, spurious ambiguities can be produced |
10173 | -- when arithmetic operations have a literal operand and return | |
10174 | -- System.Address or a descendant of it. These ambiguities are | |
10175 | -- otherwise resolved by the context, but for conversions there | |
10176 | -- is no context type and the removal of the spurious operations | |
10177 | -- must be done explicitly here. | |
10178 | ||
4adf3c50 AC |
10179 | -- The node may be labelled overloaded, but still contain only one |
10180 | -- interpretation because others were discarded earlier. If this | |
10181 | -- is the case, retain the single interpretation if legal. | |
9ebe3743 | 10182 | |
996ae0b0 | 10183 | Get_First_Interp (Operand, I, It); |
9ebe3743 HK |
10184 | Opnd_Type := It.Typ; |
10185 | Get_Next_Interp (I, It); | |
996ae0b0 | 10186 | |
9ebe3743 HK |
10187 | if Present (It.Typ) |
10188 | and then Opnd_Type /= Standard_Void_Type | |
10189 | then | |
10190 | -- More than one candidate interpretation is available | |
996ae0b0 | 10191 | |
9ebe3743 HK |
10192 | Get_First_Interp (Operand, I, It); |
10193 | while Present (It.Typ) loop | |
10194 | if It.Typ = Standard_Void_Type then | |
10195 | Remove_Interp (I); | |
10196 | end if; | |
1420b484 | 10197 | |
9ebe3743 HK |
10198 | if Present (System_Aux_Id) |
10199 | and then Is_Descendent_Of_Address (It.Typ) | |
10200 | then | |
10201 | Remove_Interp (I); | |
10202 | end if; | |
10203 | ||
10204 | Get_Next_Interp (I, It); | |
10205 | end loop; | |
10206 | end if; | |
996ae0b0 RK |
10207 | |
10208 | Get_First_Interp (Operand, I, It); | |
10209 | I1 := I; | |
10210 | It1 := It; | |
10211 | ||
10212 | if No (It.Typ) then | |
10213 | Error_Msg_N ("illegal operand in conversion", Operand); | |
10214 | return False; | |
10215 | end if; | |
10216 | ||
10217 | Get_Next_Interp (I, It); | |
10218 | ||
10219 | if Present (It.Typ) then | |
10220 | N1 := It1.Nam; | |
f0d10385 | 10221 | T1 := It1.Typ; |
996ae0b0 RK |
10222 | It1 := Disambiguate (Operand, I1, I, Any_Type); |
10223 | ||
10224 | if It1 = No_Interp then | |
10225 | Error_Msg_N ("ambiguous operand in conversion", Operand); | |
10226 | ||
f0d10385 AC |
10227 | -- If the interpretation involves a standard operator, use |
10228 | -- the location of the type, which may be user-defined. | |
10229 | ||
10230 | if Sloc (It.Nam) = Standard_Location then | |
10231 | Error_Msg_Sloc := Sloc (It.Typ); | |
10232 | else | |
10233 | Error_Msg_Sloc := Sloc (It.Nam); | |
10234 | end if; | |
10235 | ||
4e7a4f6e AC |
10236 | Error_Msg_N -- CODEFIX |
10237 | ("\\possible interpretation#!", Operand); | |
996ae0b0 | 10238 | |
f0d10385 AC |
10239 | if Sloc (N1) = Standard_Location then |
10240 | Error_Msg_Sloc := Sloc (T1); | |
10241 | else | |
10242 | Error_Msg_Sloc := Sloc (N1); | |
10243 | end if; | |
10244 | ||
4e7a4f6e AC |
10245 | Error_Msg_N -- CODEFIX |
10246 | ("\\possible interpretation#!", Operand); | |
996ae0b0 RK |
10247 | |
10248 | return False; | |
10249 | end if; | |
10250 | end if; | |
10251 | ||
10252 | Set_Etype (Operand, It1.Typ); | |
10253 | Opnd_Type := It1.Typ; | |
10254 | end; | |
10255 | end if; | |
10256 | ||
aa180613 | 10257 | -- Numeric types |
996ae0b0 | 10258 | |
aa180613 | 10259 | if Is_Numeric_Type (Target_Type) then |
996ae0b0 | 10260 | |
aa180613 | 10261 | -- A universal fixed expression can be converted to any numeric type |
996ae0b0 | 10262 | |
996ae0b0 RK |
10263 | if Opnd_Type = Universal_Fixed then |
10264 | return True; | |
7324bf49 | 10265 | |
aa180613 RD |
10266 | -- Also no need to check when in an instance or inlined body, because |
10267 | -- the legality has been established when the template was analyzed. | |
10268 | -- Furthermore, numeric conversions may occur where only a private | |
f3d57416 | 10269 | -- view of the operand type is visible at the instantiation point. |
aa180613 RD |
10270 | -- This results in a spurious error if we check that the operand type |
10271 | -- is a numeric type. | |
10272 | ||
10273 | -- Note: in a previous version of this unit, the following tests were | |
10274 | -- applied only for generated code (Comes_From_Source set to False), | |
10275 | -- but in fact the test is required for source code as well, since | |
10276 | -- this situation can arise in source code. | |
10277 | ||
10278 | elsif In_Instance or else In_Inlined_Body then | |
d347f572 | 10279 | return True; |
aa180613 RD |
10280 | |
10281 | -- Otherwise we need the conversion check | |
7324bf49 | 10282 | |
996ae0b0 | 10283 | else |
aa180613 RD |
10284 | return Conversion_Check |
10285 | (Is_Numeric_Type (Opnd_Type), | |
10286 | "illegal operand for numeric conversion"); | |
996ae0b0 RK |
10287 | end if; |
10288 | ||
aa180613 RD |
10289 | -- Array types |
10290 | ||
996ae0b0 RK |
10291 | elsif Is_Array_Type (Target_Type) then |
10292 | if not Is_Array_Type (Opnd_Type) | |
10293 | or else Opnd_Type = Any_Composite | |
10294 | or else Opnd_Type = Any_String | |
10295 | then | |
4adf3c50 | 10296 | Error_Msg_N ("illegal operand for array conversion", Operand); |
996ae0b0 | 10297 | return False; |
996ae0b0 | 10298 | else |
aa180613 | 10299 | return Valid_Array_Conversion; |
996ae0b0 RK |
10300 | end if; |
10301 | ||
e65f50ec ES |
10302 | -- Ada 2005 (AI-251): Anonymous access types where target references an |
10303 | -- interface type. | |
758c442c | 10304 | |
964f13da RD |
10305 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10306 | E_Anonymous_Access_Type) | |
758c442c GD |
10307 | and then Is_Interface (Directly_Designated_Type (Target_Type)) |
10308 | then | |
10309 | -- Check the static accessibility rule of 4.6(17). Note that the | |
d81b4bfe TQ |
10310 | -- check is not enforced when within an instance body, since the |
10311 | -- RM requires such cases to be caught at run time. | |
758c442c GD |
10312 | |
10313 | if Ekind (Target_Type) /= E_Anonymous_Access_Type then | |
10314 | if Type_Access_Level (Opnd_Type) > | |
10315 | Type_Access_Level (Target_Type) | |
10316 | then | |
10317 | -- In an instance, this is a run-time check, but one we know | |
10318 | -- will fail, so generate an appropriate warning. The raise | |
10319 | -- will be generated by Expand_N_Type_Conversion. | |
10320 | ||
10321 | if In_Instance_Body then | |
10322 | Error_Msg_N | |
10323 | ("?cannot convert local pointer to non-local access type", | |
10324 | Operand); | |
10325 | Error_Msg_N | |
c8ef728f | 10326 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
10327 | else |
10328 | Error_Msg_N | |
10329 | ("cannot convert local pointer to non-local access type", | |
10330 | Operand); | |
10331 | return False; | |
10332 | end if; | |
10333 | ||
10334 | -- Special accessibility checks are needed in the case of access | |
10335 | -- discriminants declared for a limited type. | |
10336 | ||
10337 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
10338 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
10339 | then | |
10340 | -- When the operand is a selected access discriminant the check | |
10341 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
10342 | -- the prefix of the selected name (Object_Access_Level handles |
10343 | -- checking the prefix of the operand for this case). | |
758c442c GD |
10344 | |
10345 | if Nkind (Operand) = N_Selected_Component | |
c8ef728f | 10346 | and then Object_Access_Level (Operand) > |
45fc7ddb | 10347 | Type_Access_Level (Target_Type) |
758c442c | 10348 | then |
d81b4bfe TQ |
10349 | -- In an instance, this is a run-time check, but one we know |
10350 | -- will fail, so generate an appropriate warning. The raise | |
10351 | -- will be generated by Expand_N_Type_Conversion. | |
758c442c GD |
10352 | |
10353 | if In_Instance_Body then | |
10354 | Error_Msg_N | |
10355 | ("?cannot convert access discriminant to non-local" & | |
10356 | " access type", Operand); | |
10357 | Error_Msg_N | |
c8ef728f | 10358 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
10359 | else |
10360 | Error_Msg_N | |
10361 | ("cannot convert access discriminant to non-local" & | |
10362 | " access type", Operand); | |
10363 | return False; | |
10364 | end if; | |
10365 | end if; | |
10366 | ||
10367 | -- The case of a reference to an access discriminant from | |
10368 | -- within a limited type declaration (which will appear as | |
10369 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 10370 | -- discriminant is considered to be deeper than any (nameable) |
758c442c GD |
10371 | -- access type. |
10372 | ||
10373 | if Is_Entity_Name (Operand) | |
10374 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
964f13da RD |
10375 | and then |
10376 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
758c442c GD |
10377 | and then Present (Discriminal_Link (Entity (Operand))) |
10378 | then | |
10379 | Error_Msg_N | |
10380 | ("discriminant has deeper accessibility level than target", | |
10381 | Operand); | |
10382 | return False; | |
10383 | end if; | |
10384 | end if; | |
10385 | end if; | |
10386 | ||
10387 | return True; | |
10388 | ||
aa180613 RD |
10389 | -- General and anonymous access types |
10390 | ||
964f13da RD |
10391 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10392 | E_Anonymous_Access_Type) | |
996ae0b0 RK |
10393 | and then |
10394 | Conversion_Check | |
10395 | (Is_Access_Type (Opnd_Type) | |
964f13da RD |
10396 | and then not |
10397 | Ekind_In (Opnd_Type, E_Access_Subprogram_Type, | |
10398 | E_Access_Protected_Subprogram_Type), | |
996ae0b0 RK |
10399 | "must be an access-to-object type") |
10400 | then | |
10401 | if Is_Access_Constant (Opnd_Type) | |
10402 | and then not Is_Access_Constant (Target_Type) | |
10403 | then | |
10404 | Error_Msg_N | |
10405 | ("access-to-constant operand type not allowed", Operand); | |
10406 | return False; | |
10407 | end if; | |
10408 | ||
758c442c GD |
10409 | -- Check the static accessibility rule of 4.6(17). Note that the |
10410 | -- check is not enforced when within an instance body, since the RM | |
10411 | -- requires such cases to be caught at run time. | |
996ae0b0 | 10412 | |
758c442c GD |
10413 | if Ekind (Target_Type) /= E_Anonymous_Access_Type |
10414 | or else Is_Local_Anonymous_Access (Target_Type) | |
10415 | then | |
996ae0b0 RK |
10416 | if Type_Access_Level (Opnd_Type) |
10417 | > Type_Access_Level (Target_Type) | |
10418 | then | |
d81b4bfe TQ |
10419 | -- In an instance, this is a run-time check, but one we know |
10420 | -- will fail, so generate an appropriate warning. The raise | |
10421 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
10422 | |
10423 | if In_Instance_Body then | |
10424 | Error_Msg_N | |
10425 | ("?cannot convert local pointer to non-local access type", | |
10426 | Operand); | |
10427 | Error_Msg_N | |
c8ef728f | 10428 | ("\?Program_Error will be raised at run time", Operand); |
996ae0b0 RK |
10429 | |
10430 | else | |
b90cfacd HK |
10431 | -- Avoid generation of spurious error message |
10432 | ||
10433 | if not Error_Posted (N) then | |
10434 | Error_Msg_N | |
10435 | ("cannot convert local pointer to non-local access type", | |
10436 | Operand); | |
10437 | end if; | |
10438 | ||
996ae0b0 RK |
10439 | return False; |
10440 | end if; | |
10441 | ||
758c442c GD |
10442 | -- Special accessibility checks are needed in the case of access |
10443 | -- discriminants declared for a limited type. | |
10444 | ||
10445 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
10446 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
10447 | then | |
758c442c GD |
10448 | -- When the operand is a selected access discriminant the check |
10449 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
10450 | -- the prefix of the selected name (Object_Access_Level handles |
10451 | -- checking the prefix of the operand for this case). | |
996ae0b0 RK |
10452 | |
10453 | if Nkind (Operand) = N_Selected_Component | |
45fc7ddb HK |
10454 | and then Object_Access_Level (Operand) > |
10455 | Type_Access_Level (Target_Type) | |
996ae0b0 | 10456 | then |
d81b4bfe TQ |
10457 | -- In an instance, this is a run-time check, but one we know |
10458 | -- will fail, so generate an appropriate warning. The raise | |
10459 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
10460 | |
10461 | if In_Instance_Body then | |
10462 | Error_Msg_N | |
10463 | ("?cannot convert access discriminant to non-local" & | |
10464 | " access type", Operand); | |
10465 | Error_Msg_N | |
c8ef728f ES |
10466 | ("\?Program_Error will be raised at run time", |
10467 | Operand); | |
996ae0b0 RK |
10468 | |
10469 | else | |
10470 | Error_Msg_N | |
10471 | ("cannot convert access discriminant to non-local" & | |
10472 | " access type", Operand); | |
10473 | return False; | |
10474 | end if; | |
10475 | end if; | |
10476 | ||
758c442c GD |
10477 | -- The case of a reference to an access discriminant from |
10478 | -- within a limited type declaration (which will appear as | |
10479 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 10480 | -- discriminant is considered to be deeper than any (nameable) |
758c442c | 10481 | -- access type. |
996ae0b0 RK |
10482 | |
10483 | if Is_Entity_Name (Operand) | |
964f13da RD |
10484 | and then |
10485 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
996ae0b0 RK |
10486 | and then Present (Discriminal_Link (Entity (Operand))) |
10487 | then | |
10488 | Error_Msg_N | |
10489 | ("discriminant has deeper accessibility level than target", | |
10490 | Operand); | |
10491 | return False; | |
10492 | end if; | |
10493 | end if; | |
10494 | end if; | |
10495 | ||
14e33999 AC |
10496 | -- In the presence of limited_with clauses we have to use non-limited |
10497 | -- views, if available. | |
d81b4bfe | 10498 | |
14e33999 | 10499 | Check_Limited : declare |
0669bebe GB |
10500 | function Full_Designated_Type (T : Entity_Id) return Entity_Id; |
10501 | -- Helper function to handle limited views | |
10502 | ||
10503 | -------------------------- | |
10504 | -- Full_Designated_Type -- | |
10505 | -------------------------- | |
10506 | ||
10507 | function Full_Designated_Type (T : Entity_Id) return Entity_Id is | |
950d217a | 10508 | Desig : constant Entity_Id := Designated_Type (T); |
c0985d4e | 10509 | |
0669bebe | 10510 | begin |
950d217a AC |
10511 | -- Handle the limited view of a type |
10512 | ||
c0985d4e HK |
10513 | if Is_Incomplete_Type (Desig) |
10514 | and then From_With_Type (Desig) | |
0669bebe GB |
10515 | and then Present (Non_Limited_View (Desig)) |
10516 | then | |
950d217a AC |
10517 | return Available_View (Desig); |
10518 | else | |
10519 | return Desig; | |
0669bebe GB |
10520 | end if; |
10521 | end Full_Designated_Type; | |
10522 | ||
d81b4bfe TQ |
10523 | -- Local Declarations |
10524 | ||
0669bebe GB |
10525 | Target : constant Entity_Id := Full_Designated_Type (Target_Type); |
10526 | Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type); | |
10527 | ||
10528 | Same_Base : constant Boolean := | |
10529 | Base_Type (Target) = Base_Type (Opnd); | |
996ae0b0 | 10530 | |
14e33999 | 10531 | -- Start of processing for Check_Limited |
d81b4bfe | 10532 | |
996ae0b0 RK |
10533 | begin |
10534 | if Is_Tagged_Type (Target) then | |
10535 | return Valid_Tagged_Conversion (Target, Opnd); | |
10536 | ||
10537 | else | |
0669bebe | 10538 | if not Same_Base then |
996ae0b0 RK |
10539 | Error_Msg_NE |
10540 | ("target designated type not compatible with }", | |
10541 | N, Base_Type (Opnd)); | |
10542 | return False; | |
10543 | ||
da709d08 AC |
10544 | -- Ada 2005 AI-384: legality rule is symmetric in both |
10545 | -- designated types. The conversion is legal (with possible | |
10546 | -- constraint check) if either designated type is | |
10547 | -- unconstrained. | |
10548 | ||
10549 | elsif Subtypes_Statically_Match (Target, Opnd) | |
10550 | or else | |
10551 | (Has_Discriminants (Target) | |
10552 | and then | |
10553 | (not Is_Constrained (Opnd) | |
10554 | or else not Is_Constrained (Target))) | |
996ae0b0 | 10555 | then |
9fa33291 RD |
10556 | -- Special case, if Value_Size has been used to make the |
10557 | -- sizes different, the conversion is not allowed even | |
10558 | -- though the subtypes statically match. | |
10559 | ||
10560 | if Known_Static_RM_Size (Target) | |
10561 | and then Known_Static_RM_Size (Opnd) | |
10562 | and then RM_Size (Target) /= RM_Size (Opnd) | |
10563 | then | |
10564 | Error_Msg_NE | |
10565 | ("target designated subtype not compatible with }", | |
10566 | N, Opnd); | |
10567 | Error_Msg_NE | |
10568 | ("\because sizes of the two designated subtypes differ", | |
10569 | N, Opnd); | |
10570 | return False; | |
10571 | ||
10572 | -- Normal case where conversion is allowed | |
10573 | ||
10574 | else | |
10575 | return True; | |
10576 | end if; | |
da709d08 AC |
10577 | |
10578 | else | |
996ae0b0 RK |
10579 | Error_Msg_NE |
10580 | ("target designated subtype not compatible with }", | |
10581 | N, Opnd); | |
10582 | return False; | |
996ae0b0 RK |
10583 | end if; |
10584 | end if; | |
14e33999 | 10585 | end Check_Limited; |
996ae0b0 | 10586 | |
cdbf04c0 | 10587 | -- Access to subprogram types. If the operand is an access parameter, |
4adf3c50 AC |
10588 | -- the type has a deeper accessibility that any master, and cannot be |
10589 | -- assigned. We must make an exception if the conversion is part of an | |
10590 | -- assignment and the target is the return object of an extended return | |
10591 | -- statement, because in that case the accessibility check takes place | |
10592 | -- after the return. | |
aa180613 | 10593 | |
dce86910 | 10594 | elsif Is_Access_Subprogram_Type (Target_Type) |
bc5f3720 | 10595 | and then No (Corresponding_Remote_Type (Opnd_Type)) |
996ae0b0 | 10596 | then |
cdbf04c0 AC |
10597 | if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type |
10598 | and then Is_Entity_Name (Operand) | |
10599 | and then Ekind (Entity (Operand)) = E_In_Parameter | |
53cf4600 ES |
10600 | and then |
10601 | (Nkind (Parent (N)) /= N_Assignment_Statement | |
10602 | or else not Is_Entity_Name (Name (Parent (N))) | |
10603 | or else not Is_Return_Object (Entity (Name (Parent (N))))) | |
0669bebe GB |
10604 | then |
10605 | Error_Msg_N | |
10606 | ("illegal attempt to store anonymous access to subprogram", | |
10607 | Operand); | |
10608 | Error_Msg_N | |
10609 | ("\value has deeper accessibility than any master " & | |
aa5147f0 | 10610 | "(RM 3.10.2 (13))", |
0669bebe GB |
10611 | Operand); |
10612 | ||
c147ac26 ES |
10613 | Error_Msg_NE |
10614 | ("\use named access type for& instead of access parameter", | |
10615 | Operand, Entity (Operand)); | |
0669bebe GB |
10616 | end if; |
10617 | ||
996ae0b0 RK |
10618 | -- Check that the designated types are subtype conformant |
10619 | ||
bc5f3720 RD |
10620 | Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type), |
10621 | Old_Id => Designated_Type (Opnd_Type), | |
10622 | Err_Loc => N); | |
996ae0b0 RK |
10623 | |
10624 | -- Check the static accessibility rule of 4.6(20) | |
10625 | ||
10626 | if Type_Access_Level (Opnd_Type) > | |
10627 | Type_Access_Level (Target_Type) | |
10628 | then | |
10629 | Error_Msg_N | |
10630 | ("operand type has deeper accessibility level than target", | |
10631 | Operand); | |
10632 | ||
10633 | -- Check that if the operand type is declared in a generic body, | |
10634 | -- then the target type must be declared within that same body | |
10635 | -- (enforces last sentence of 4.6(20)). | |
10636 | ||
10637 | elsif Present (Enclosing_Generic_Body (Opnd_Type)) then | |
10638 | declare | |
10639 | O_Gen : constant Node_Id := | |
10640 | Enclosing_Generic_Body (Opnd_Type); | |
10641 | ||
1420b484 | 10642 | T_Gen : Node_Id; |
996ae0b0 RK |
10643 | |
10644 | begin | |
1420b484 | 10645 | T_Gen := Enclosing_Generic_Body (Target_Type); |
996ae0b0 RK |
10646 | while Present (T_Gen) and then T_Gen /= O_Gen loop |
10647 | T_Gen := Enclosing_Generic_Body (T_Gen); | |
10648 | end loop; | |
10649 | ||
10650 | if T_Gen /= O_Gen then | |
10651 | Error_Msg_N | |
10652 | ("target type must be declared in same generic body" | |
10653 | & " as operand type", N); | |
10654 | end if; | |
10655 | end; | |
10656 | end if; | |
10657 | ||
10658 | return True; | |
10659 | ||
aa180613 RD |
10660 | -- Remote subprogram access types |
10661 | ||
996ae0b0 RK |
10662 | elsif Is_Remote_Access_To_Subprogram_Type (Target_Type) |
10663 | and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type) | |
10664 | then | |
10665 | -- It is valid to convert from one RAS type to another provided | |
10666 | -- that their specification statically match. | |
10667 | ||
10668 | Check_Subtype_Conformant | |
10669 | (New_Id => | |
10670 | Designated_Type (Corresponding_Remote_Type (Target_Type)), | |
10671 | Old_Id => | |
10672 | Designated_Type (Corresponding_Remote_Type (Opnd_Type)), | |
10673 | Err_Loc => | |
10674 | N); | |
10675 | return True; | |
aa180613 | 10676 | |
e65f50ec | 10677 | -- If both are tagged types, check legality of view conversions |
996ae0b0 | 10678 | |
e65f50ec | 10679 | elsif Is_Tagged_Type (Target_Type) |
4adf3c50 AC |
10680 | and then |
10681 | Is_Tagged_Type (Opnd_Type) | |
e65f50ec | 10682 | then |
996ae0b0 RK |
10683 | return Valid_Tagged_Conversion (Target_Type, Opnd_Type); |
10684 | ||
a77842bd | 10685 | -- Types derived from the same root type are convertible |
996ae0b0 RK |
10686 | |
10687 | elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then | |
10688 | return True; | |
10689 | ||
4adf3c50 AC |
10690 | -- In an instance or an inlined body, there may be inconsistent views of |
10691 | -- the same type, or of types derived from a common root. | |
996ae0b0 | 10692 | |
aa5147f0 ES |
10693 | elsif (In_Instance or In_Inlined_Body) |
10694 | and then | |
d81b4bfe TQ |
10695 | Root_Type (Underlying_Type (Target_Type)) = |
10696 | Root_Type (Underlying_Type (Opnd_Type)) | |
996ae0b0 RK |
10697 | then |
10698 | return True; | |
10699 | ||
10700 | -- Special check for common access type error case | |
10701 | ||
10702 | elsif Ekind (Target_Type) = E_Access_Type | |
10703 | and then Is_Access_Type (Opnd_Type) | |
10704 | then | |
10705 | Error_Msg_N ("target type must be general access type!", N); | |
305caf42 AC |
10706 | Error_Msg_NE -- CODEFIX |
10707 | ("add ALL to }!", N, Target_Type); | |
996ae0b0 RK |
10708 | return False; |
10709 | ||
10710 | else | |
10711 | Error_Msg_NE ("invalid conversion, not compatible with }", | |
10712 | N, Opnd_Type); | |
996ae0b0 RK |
10713 | return False; |
10714 | end if; | |
10715 | end Valid_Conversion; | |
10716 | ||
10717 | end Sem_Res; |