]>
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; | |
218e6dee | 3522 | |
cb7fa356 AC |
3523 | begin |
3524 | Get_First_Interp (A, Index, It); | |
3525 | while Present (It.Typ) loop | |
3526 | if Is_Concurrent_Type (It.Typ) | |
3527 | or else Is_Concurrent_Record_Type (It.Typ) | |
3528 | then | |
3529 | A_Typ := Base_Type (It.Typ); | |
3530 | exit; | |
3531 | end if; | |
3532 | ||
3533 | Get_Next_Interp (Index, It); | |
3534 | end loop; | |
3535 | end; | |
15e4986c | 3536 | end if; |
b7d1f17f | 3537 | |
cb7fa356 AC |
3538 | declare |
3539 | Full_A_Typ : Entity_Id; | |
15e4986c | 3540 | |
cb7fa356 AC |
3541 | begin |
3542 | if Present (Full_View (A_Typ)) then | |
3543 | Full_A_Typ := Base_Type (Full_View (A_Typ)); | |
3544 | else | |
3545 | Full_A_Typ := A_Typ; | |
3546 | end if; | |
3547 | ||
3548 | -- Tagged synchronized type (case 1): the actual is a | |
3549 | -- concurrent type. | |
3550 | ||
3551 | if Is_Concurrent_Type (A_Typ) | |
3552 | and then Corresponding_Record_Type (A_Typ) = F_Typ | |
3553 | then | |
3554 | Rewrite (A, | |
3555 | Unchecked_Convert_To | |
3556 | (Corresponding_Record_Type (A_Typ), A)); | |
3557 | Resolve (A, Etype (F)); | |
15e4986c | 3558 | |
cb7fa356 AC |
3559 | -- Tagged synchronized type (case 2): the formal is a |
3560 | -- concurrent type. | |
15e4986c | 3561 | |
cb7fa356 AC |
3562 | elsif Ekind (Full_A_Typ) = E_Record_Type |
3563 | and then Present | |
15e4986c | 3564 | (Corresponding_Concurrent_Type (Full_A_Typ)) |
cb7fa356 AC |
3565 | and then Is_Concurrent_Type (F_Typ) |
3566 | and then Present (Corresponding_Record_Type (F_Typ)) | |
3567 | and then Full_A_Typ = Corresponding_Record_Type (F_Typ) | |
3568 | then | |
3569 | Resolve (A, Corresponding_Record_Type (F_Typ)); | |
15e4986c | 3570 | |
cb7fa356 | 3571 | -- Common case |
15e4986c | 3572 | |
cb7fa356 AC |
3573 | else |
3574 | Resolve (A, Etype (F)); | |
3575 | end if; | |
3576 | end; | |
3577 | else | |
3578 | ||
3579 | -- not a synchronized operation. | |
3580 | ||
3581 | Resolve (A, Etype (F)); | |
3582 | end if; | |
996ae0b0 RK |
3583 | end if; |
3584 | ||
3585 | A_Typ := Etype (A); | |
3586 | F_Typ := Etype (F); | |
3587 | ||
e24329cd YM |
3588 | if Comes_From_Source (Original_Node (N)) |
3589 | and then Nkind_In (Original_Node (N), | |
3590 | N_Function_Call, | |
3591 | N_Procedure_Call_Statement) | |
b0186f71 | 3592 | then |
e24329cd YM |
3593 | -- In formal mode, check that actual parameters matching |
3594 | -- formals of tagged types are objects (or ancestor type | |
3595 | -- conversions of objects), not general expressions. | |
780d052e | 3596 | |
e24329cd YM |
3597 | if Is_Actual_Tagged_Parameter (A) then |
3598 | if Is_SPARK_Object_Reference (A) then | |
3599 | null; | |
3600 | ||
3601 | elsif Nkind (A) = N_Type_Conversion then | |
3602 | declare | |
3603 | Operand : constant Node_Id := Expression (A); | |
3604 | Operand_Typ : constant Entity_Id := Etype (Operand); | |
3605 | Target_Typ : constant Entity_Id := A_Typ; | |
3606 | ||
3607 | begin | |
3608 | if not Is_SPARK_Object_Reference (Operand) then | |
3609 | Check_Formal_Restriction | |
3610 | ("object required", Operand); | |
3611 | ||
3612 | -- In formal mode, the only view conversions are those | |
3613 | -- involving ancestor conversion of an extended type. | |
3614 | ||
3615 | elsif not | |
3616 | (Is_Tagged_Type (Target_Typ) | |
780d052e RD |
3617 | and then not Is_Class_Wide_Type (Target_Typ) |
3618 | and then Is_Tagged_Type (Operand_Typ) | |
3619 | and then not Is_Class_Wide_Type (Operand_Typ) | |
3620 | and then Is_Ancestor (Target_Typ, Operand_Typ)) | |
e24329cd YM |
3621 | then |
3622 | if Ekind_In | |
3623 | (F, E_Out_Parameter, E_In_Out_Parameter) | |
3624 | then | |
3625 | Check_Formal_Restriction | |
3626 | ("ancestor conversion is the only permitted " | |
3627 | & "view conversion", A); | |
3628 | else | |
3629 | Check_Formal_Restriction | |
3630 | ("ancestor conversion required", A); | |
3631 | end if; | |
3632 | ||
3633 | else | |
3634 | null; | |
3635 | end if; | |
3636 | end; | |
3637 | ||
3638 | else | |
3639 | Check_Formal_Restriction ("object required", A); | |
b0186f71 | 3640 | end if; |
e24329cd YM |
3641 | |
3642 | -- In formal mode, the only view conversions are those | |
3643 | -- involving ancestor conversion of an extended type. | |
3644 | ||
3645 | elsif Nkind (A) = N_Type_Conversion | |
3646 | and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) | |
3647 | then | |
3648 | Check_Formal_Restriction | |
3649 | ("ancestor conversion is the only permitted view " | |
3650 | & "conversion", A); | |
3651 | end if; | |
b0186f71 AC |
3652 | end if; |
3653 | ||
1e194575 AC |
3654 | -- Save actual for subsequent check on order dependence, and |
3655 | -- indicate whether actual is modifiable. For AI05-0144-2. | |
bb481772 | 3656 | |
1e194575 | 3657 | Save_Actual (A, Ekind (F) /= E_In_Parameter); |
bb481772 | 3658 | |
26570b21 RD |
3659 | -- For mode IN, if actual is an entity, and the type of the formal |
3660 | -- has warnings suppressed, then we reset Never_Set_In_Source for | |
3661 | -- the calling entity. The reason for this is to catch cases like | |
3662 | -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram | |
3663 | -- uses trickery to modify an IN parameter. | |
3664 | ||
3665 | if Ekind (F) = E_In_Parameter | |
3666 | and then Is_Entity_Name (A) | |
3667 | and then Present (Entity (A)) | |
3668 | and then Ekind (Entity (A)) = E_Variable | |
3669 | and then Has_Warnings_Off (F_Typ) | |
3670 | then | |
3671 | Set_Never_Set_In_Source (Entity (A), False); | |
3672 | end if; | |
3673 | ||
fbf5a39b AC |
3674 | -- Perform error checks for IN and IN OUT parameters |
3675 | ||
3676 | if Ekind (F) /= E_Out_Parameter then | |
3677 | ||
3678 | -- Check unset reference. For scalar parameters, it is clearly | |
3679 | -- wrong to pass an uninitialized value as either an IN or | |
3680 | -- IN-OUT parameter. For composites, it is also clearly an | |
3681 | -- error to pass a completely uninitialized value as an IN | |
3682 | -- parameter, but the case of IN OUT is trickier. We prefer | |
3683 | -- not to give a warning here. For example, suppose there is | |
3684 | -- a routine that sets some component of a record to False. | |
3685 | -- It is perfectly reasonable to make this IN-OUT and allow | |
3686 | -- either initialized or uninitialized records to be passed | |
3687 | -- in this case. | |
3688 | ||
3689 | -- For partially initialized composite values, we also avoid | |
3690 | -- warnings, since it is quite likely that we are passing a | |
3691 | -- partially initialized value and only the initialized fields | |
3692 | -- will in fact be read in the subprogram. | |
3693 | ||
3694 | if Is_Scalar_Type (A_Typ) | |
3695 | or else (Ekind (F) = E_In_Parameter | |
19fb051c | 3696 | and then not Is_Partially_Initialized_Type (A_Typ)) |
996ae0b0 | 3697 | then |
fbf5a39b | 3698 | Check_Unset_Reference (A); |
996ae0b0 | 3699 | end if; |
996ae0b0 | 3700 | |
758c442c GD |
3701 | -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT |
3702 | -- actual to a nested call, since this is case of reading an | |
3703 | -- out parameter, which is not allowed. | |
996ae0b0 | 3704 | |
0ab80019 | 3705 | if Ada_Version = Ada_83 |
996ae0b0 RK |
3706 | and then Is_Entity_Name (A) |
3707 | and then Ekind (Entity (A)) = E_Out_Parameter | |
3708 | then | |
3709 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", A); | |
3710 | end if; | |
3711 | end if; | |
3712 | ||
67ce0d7e RD |
3713 | -- Case of OUT or IN OUT parameter |
3714 | ||
36fcf362 | 3715 | if Ekind (F) /= E_In_Parameter then |
67ce0d7e RD |
3716 | |
3717 | -- For an Out parameter, check for useless assignment. Note | |
45fc7ddb HK |
3718 | -- that we can't set Last_Assignment this early, because we may |
3719 | -- kill current values in Resolve_Call, and that call would | |
3720 | -- clobber the Last_Assignment field. | |
67ce0d7e | 3721 | |
45fc7ddb HK |
3722 | -- Note: call Warn_On_Useless_Assignment before doing the check |
3723 | -- below for Is_OK_Variable_For_Out_Formal so that the setting | |
3724 | -- of Referenced_As_LHS/Referenced_As_Out_Formal properly | |
3725 | -- reflects the last assignment, not this one! | |
36fcf362 | 3726 | |
67ce0d7e | 3727 | if Ekind (F) = E_Out_Parameter then |
36fcf362 | 3728 | if Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
3729 | and then Is_Entity_Name (A) |
3730 | and then Present (Entity (A)) | |
36fcf362 | 3731 | and then Comes_From_Source (N) |
67ce0d7e | 3732 | then |
36fcf362 | 3733 | Warn_On_Useless_Assignment (Entity (A), A); |
67ce0d7e RD |
3734 | end if; |
3735 | end if; | |
3736 | ||
36fcf362 RD |
3737 | -- Validate the form of the actual. Note that the call to |
3738 | -- Is_OK_Variable_For_Out_Formal generates the required | |
3739 | -- reference in this case. | |
3740 | ||
3741 | if not Is_OK_Variable_For_Out_Formal (A) then | |
3742 | Error_Msg_NE ("actual for& must be a variable", A, F); | |
3743 | end if; | |
3744 | ||
67ce0d7e | 3745 | -- What's the following about??? |
fbf5a39b AC |
3746 | |
3747 | if Is_Entity_Name (A) then | |
3748 | Kill_Checks (Entity (A)); | |
3749 | else | |
3750 | Kill_All_Checks; | |
3751 | end if; | |
3752 | end if; | |
3753 | ||
3754 | if Etype (A) = Any_Type then | |
3755 | Set_Etype (N, Any_Type); | |
3756 | return; | |
3757 | end if; | |
3758 | ||
996ae0b0 RK |
3759 | -- Apply appropriate range checks for in, out, and in-out |
3760 | -- parameters. Out and in-out parameters also need a separate | |
3761 | -- check, if there is a type conversion, to make sure the return | |
3762 | -- value meets the constraints of the variable before the | |
3763 | -- conversion. | |
3764 | ||
3765 | -- Gigi looks at the check flag and uses the appropriate types. | |
3766 | -- For now since one flag is used there is an optimization which | |
3767 | -- might not be done in the In Out case since Gigi does not do | |
3768 | -- any analysis. More thought required about this ??? | |
3769 | ||
8a95f4e8 | 3770 | if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then |
48f91b44 RD |
3771 | |
3772 | -- Apply predicate checks, unless this is a call to the | |
3773 | -- predicate check function itself, which would cause an | |
3774 | -- infinite recursion. | |
3775 | ||
3776 | if not (Ekind (Nam) = E_Function | |
3777 | and then Has_Predicates (Nam)) | |
3778 | then | |
3779 | Apply_Predicate_Check (A, F_Typ); | |
3780 | end if; | |
3781 | ||
3782 | -- Apply required constraint checks | |
3783 | ||
996ae0b0 RK |
3784 | if Is_Scalar_Type (Etype (A)) then |
3785 | Apply_Scalar_Range_Check (A, F_Typ); | |
3786 | ||
3787 | elsif Is_Array_Type (Etype (A)) then | |
3788 | Apply_Length_Check (A, F_Typ); | |
3789 | ||
3790 | elsif Is_Record_Type (F_Typ) | |
3791 | and then Has_Discriminants (F_Typ) | |
3792 | and then Is_Constrained (F_Typ) | |
3793 | and then (not Is_Derived_Type (F_Typ) | |
19fb051c | 3794 | or else Comes_From_Source (Nam)) |
996ae0b0 RK |
3795 | then |
3796 | Apply_Discriminant_Check (A, F_Typ); | |
3797 | ||
3798 | elsif Is_Access_Type (F_Typ) | |
3799 | and then Is_Array_Type (Designated_Type (F_Typ)) | |
3800 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3801 | then | |
3802 | Apply_Length_Check (A, F_Typ); | |
3803 | ||
3804 | elsif Is_Access_Type (F_Typ) | |
3805 | and then Has_Discriminants (Designated_Type (F_Typ)) | |
3806 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3807 | then | |
3808 | Apply_Discriminant_Check (A, F_Typ); | |
3809 | ||
3810 | else | |
3811 | Apply_Range_Check (A, F_Typ); | |
3812 | end if; | |
2820d220 | 3813 | |
0f1a6a0b AC |
3814 | -- Ada 2005 (AI-231): Note that the controlling parameter case |
3815 | -- already existed in Ada 95, which is partially checked | |
3816 | -- elsewhere (see Checks), and we don't want the warning | |
3817 | -- message to differ. | |
2820d220 | 3818 | |
0f1a6a0b | 3819 | if Is_Access_Type (F_Typ) |
1420b484 | 3820 | and then Can_Never_Be_Null (F_Typ) |
aa5147f0 | 3821 | and then Known_Null (A) |
2820d220 | 3822 | then |
0f1a6a0b AC |
3823 | if Is_Controlling_Formal (F) then |
3824 | Apply_Compile_Time_Constraint_Error | |
3825 | (N => A, | |
3826 | Msg => "null value not allowed here?", | |
3827 | Reason => CE_Access_Check_Failed); | |
3828 | ||
3829 | elsif Ada_Version >= Ada_2005 then | |
3830 | Apply_Compile_Time_Constraint_Error | |
3831 | (N => A, | |
3832 | Msg => "(Ada 2005) null not allowed in " | |
3833 | & "null-excluding formal?", | |
3834 | Reason => CE_Null_Not_Allowed); | |
3835 | end if; | |
2820d220 | 3836 | end if; |
996ae0b0 RK |
3837 | end if; |
3838 | ||
8a95f4e8 | 3839 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then |
996ae0b0 RK |
3840 | if Nkind (A) = N_Type_Conversion then |
3841 | if Is_Scalar_Type (A_Typ) then | |
3842 | Apply_Scalar_Range_Check | |
3843 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3844 | else | |
3845 | Apply_Range_Check | |
3846 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3847 | end if; | |
3848 | ||
3849 | else | |
3850 | if Is_Scalar_Type (F_Typ) then | |
3851 | Apply_Scalar_Range_Check (A, A_Typ, F_Typ); | |
996ae0b0 RK |
3852 | elsif Is_Array_Type (F_Typ) |
3853 | and then Ekind (F) = E_Out_Parameter | |
3854 | then | |
3855 | Apply_Length_Check (A, F_Typ); | |
996ae0b0 RK |
3856 | else |
3857 | Apply_Range_Check (A, A_Typ, F_Typ); | |
3858 | end if; | |
3859 | end if; | |
3860 | end if; | |
3861 | ||
3862 | -- An actual associated with an access parameter is implicitly | |
45fc7ddb HK |
3863 | -- converted to the anonymous access type of the formal and must |
3864 | -- satisfy the legality checks for access conversions. | |
996ae0b0 RK |
3865 | |
3866 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
3867 | if not Valid_Conversion (A, F_Typ, A) then | |
3868 | Error_Msg_N | |
3869 | ("invalid implicit conversion for access parameter", A); | |
3870 | end if; | |
3871 | end if; | |
3872 | ||
3873 | -- Check bad case of atomic/volatile argument (RM C.6(12)) | |
3874 | ||
3875 | if Is_By_Reference_Type (Etype (F)) | |
3876 | and then Comes_From_Source (N) | |
3877 | then | |
3878 | if Is_Atomic_Object (A) | |
3879 | and then not Is_Atomic (Etype (F)) | |
3880 | then | |
3881 | Error_Msg_N | |
3882 | ("cannot pass atomic argument to non-atomic formal", | |
3883 | N); | |
3884 | ||
3885 | elsif Is_Volatile_Object (A) | |
3886 | and then not Is_Volatile (Etype (F)) | |
3887 | then | |
3888 | Error_Msg_N | |
3889 | ("cannot pass volatile argument to non-volatile formal", | |
3890 | N); | |
3891 | end if; | |
3892 | end if; | |
3893 | ||
3894 | -- Check that subprograms don't have improper controlling | |
d81b4bfe | 3895 | -- arguments (RM 3.9.2 (9)). |
996ae0b0 | 3896 | |
0669bebe GB |
3897 | -- A primitive operation may have an access parameter of an |
3898 | -- incomplete tagged type, but a dispatching call is illegal | |
3899 | -- if the type is still incomplete. | |
3900 | ||
996ae0b0 RK |
3901 | if Is_Controlling_Formal (F) then |
3902 | Set_Is_Controlling_Actual (A); | |
0669bebe GB |
3903 | |
3904 | if Ekind (Etype (F)) = E_Anonymous_Access_Type then | |
3905 | declare | |
3906 | Desig : constant Entity_Id := Designated_Type (Etype (F)); | |
3907 | begin | |
3908 | if Ekind (Desig) = E_Incomplete_Type | |
3909 | and then No (Full_View (Desig)) | |
3910 | and then No (Non_Limited_View (Desig)) | |
3911 | then | |
3912 | Error_Msg_NE | |
3913 | ("premature use of incomplete type& " & | |
3914 | "in dispatching call", A, Desig); | |
3915 | end if; | |
3916 | end; | |
3917 | end if; | |
3918 | ||
996ae0b0 RK |
3919 | elsif Nkind (A) = N_Explicit_Dereference then |
3920 | Validate_Remote_Access_To_Class_Wide_Type (A); | |
3921 | end if; | |
3922 | ||
3923 | if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A)) | |
3924 | and then not Is_Class_Wide_Type (F_Typ) | |
3925 | and then not Is_Controlling_Formal (F) | |
3926 | then | |
3927 | Error_Msg_N ("class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3928 | |
3929 | if Is_Subprogram (Nam) | |
3930 | and then Comes_From_Source (Nam) | |
3931 | then | |
996ae0b0 RK |
3932 | Error_Msg_Node_2 := F_Typ; |
3933 | Error_Msg_NE | |
82c80734 | 3934 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3935 | end if; |
3936 | ||
3937 | elsif Is_Access_Type (A_Typ) | |
3938 | and then Is_Access_Type (F_Typ) | |
3939 | and then Ekind (F_Typ) /= E_Access_Subprogram_Type | |
aa5147f0 | 3940 | and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type |
996ae0b0 | 3941 | and then (Is_Class_Wide_Type (Designated_Type (A_Typ)) |
07fc65c4 GB |
3942 | or else (Nkind (A) = N_Attribute_Reference |
3943 | and then | |
46fe0142 | 3944 | Is_Class_Wide_Type (Etype (Prefix (A))))) |
996ae0b0 RK |
3945 | and then not Is_Class_Wide_Type (Designated_Type (F_Typ)) |
3946 | and then not Is_Controlling_Formal (F) | |
ae65d635 | 3947 | |
46fe0142 | 3948 | -- Disable these checks for call to imported C++ subprograms |
ae65d635 | 3949 | |
46fe0142 AC |
3950 | and then not |
3951 | (Is_Entity_Name (Name (N)) | |
3952 | and then Is_Imported (Entity (Name (N))) | |
3953 | and then Convention (Entity (Name (N))) = Convention_CPP) | |
996ae0b0 RK |
3954 | then |
3955 | Error_Msg_N | |
3956 | ("access to class-wide argument not allowed here!", A); | |
07fc65c4 GB |
3957 | |
3958 | if Is_Subprogram (Nam) | |
3959 | and then Comes_From_Source (Nam) | |
3960 | then | |
996ae0b0 RK |
3961 | Error_Msg_Node_2 := Designated_Type (F_Typ); |
3962 | Error_Msg_NE | |
82c80734 | 3963 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
3964 | end if; |
3965 | end if; | |
3966 | ||
3967 | Eval_Actual (A); | |
3968 | ||
8e4dac80 TQ |
3969 | -- If it is a named association, treat the selector_name as a |
3970 | -- proper identifier, and mark the corresponding entity. | |
996ae0b0 RK |
3971 | |
3972 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3973 | Set_Entity (Selector_Name (Parent (A)), F); | |
3974 | Generate_Reference (F, Selector_Name (Parent (A))); | |
3975 | Set_Etype (Selector_Name (Parent (A)), F_Typ); | |
3976 | Generate_Reference (F_Typ, N, ' '); | |
3977 | end if; | |
3978 | ||
3979 | Prev := A; | |
fbf5a39b AC |
3980 | |
3981 | if Ekind (F) /= E_Out_Parameter then | |
3982 | Check_Unset_Reference (A); | |
3983 | end if; | |
3984 | ||
996ae0b0 RK |
3985 | Next_Actual (A); |
3986 | ||
fbf5a39b AC |
3987 | -- Case where actual is not present |
3988 | ||
996ae0b0 RK |
3989 | else |
3990 | Insert_Default; | |
3991 | end if; | |
3992 | ||
3993 | Next_Formal (F); | |
3994 | end loop; | |
996ae0b0 RK |
3995 | end Resolve_Actuals; |
3996 | ||
3997 | ----------------------- | |
3998 | -- Resolve_Allocator -- | |
3999 | ----------------------- | |
4000 | ||
4001 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is | |
4002 | E : constant Node_Id := Expression (N); | |
4003 | Subtyp : Entity_Id; | |
4004 | Discrim : Entity_Id; | |
4005 | Constr : Node_Id; | |
b7d1f17f HK |
4006 | Aggr : Node_Id; |
4007 | Assoc : Node_Id := Empty; | |
996ae0b0 RK |
4008 | Disc_Exp : Node_Id; |
4009 | ||
b7d1f17f HK |
4010 | procedure Check_Allocator_Discrim_Accessibility |
4011 | (Disc_Exp : Node_Id; | |
4012 | Alloc_Typ : Entity_Id); | |
4013 | -- Check that accessibility level associated with an access discriminant | |
4014 | -- initialized in an allocator by the expression Disc_Exp is not deeper | |
4015 | -- than the level of the allocator type Alloc_Typ. An error message is | |
4016 | -- issued if this condition is violated. Specialized checks are done for | |
4017 | -- the cases of a constraint expression which is an access attribute or | |
4018 | -- an access discriminant. | |
4019 | ||
07fc65c4 | 4020 | function In_Dispatching_Context return Boolean; |
b7d1f17f HK |
4021 | -- If the allocator is an actual in a call, it is allowed to be class- |
4022 | -- wide when the context is not because it is a controlling actual. | |
4023 | ||
4024 | procedure Propagate_Coextensions (Root : Node_Id); | |
4025 | -- Propagate all nested coextensions which are located one nesting | |
4026 | -- level down the tree to the node Root. Example: | |
4027 | -- | |
4028 | -- Top_Record | |
4029 | -- Level_1_Coextension | |
4030 | -- Level_2_Coextension | |
4031 | -- | |
4032 | -- The algorithm is paired with delay actions done by the Expander. In | |
4033 | -- the above example, assume all coextensions are controlled types. | |
4034 | -- The cycle of analysis, resolution and expansion will yield: | |
4035 | -- | |
4036 | -- 1) Analyze Top_Record | |
4037 | -- 2) Analyze Level_1_Coextension | |
4038 | -- 3) Analyze Level_2_Coextension | |
f3d57416 | 4039 | -- 4) Resolve Level_2_Coextension. The allocator is marked as a |
b7d1f17f HK |
4040 | -- coextension. |
4041 | -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is | |
4042 | -- generated to capture the allocated object. Temp_1 is attached | |
4043 | -- to the coextension chain of Level_2_Coextension. | |
4044 | -- 6) Resolve Level_1_Coextension. The allocator is marked as a | |
4045 | -- coextension. A forward tree traversal is performed which finds | |
4046 | -- Level_2_Coextension's list and copies its contents into its | |
4047 | -- own list. | |
4048 | -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is | |
4049 | -- generated to capture the allocated object. Temp_2 is attached | |
4050 | -- to the coextension chain of Level_1_Coextension. Currently, the | |
4051 | -- contents of the list are [Temp_2, Temp_1]. | |
4052 | -- 8) Resolve Top_Record. A forward tree traversal is performed which | |
4053 | -- finds Level_1_Coextension's list and copies its contents into | |
4054 | -- its own list. | |
4055 | -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and | |
4056 | -- Temp_2 and attach them to Top_Record's finalization list. | |
4057 | ||
4058 | ------------------------------------------- | |
4059 | -- Check_Allocator_Discrim_Accessibility -- | |
4060 | ------------------------------------------- | |
4061 | ||
4062 | procedure Check_Allocator_Discrim_Accessibility | |
4063 | (Disc_Exp : Node_Id; | |
4064 | Alloc_Typ : Entity_Id) | |
4065 | is | |
4066 | begin | |
4067 | if Type_Access_Level (Etype (Disc_Exp)) > | |
4068 | Type_Access_Level (Alloc_Typ) | |
4069 | then | |
4070 | Error_Msg_N | |
4071 | ("operand type has deeper level than allocator type", Disc_Exp); | |
4072 | ||
4073 | -- When the expression is an Access attribute the level of the prefix | |
4074 | -- object must not be deeper than that of the allocator's type. | |
4075 | ||
4076 | elsif Nkind (Disc_Exp) = N_Attribute_Reference | |
4077 | and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) | |
4078 | = Attribute_Access | |
4079 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
4080 | > Type_Access_Level (Alloc_Typ) | |
4081 | then | |
4082 | Error_Msg_N | |
4083 | ("prefix of attribute has deeper level than allocator type", | |
4084 | Disc_Exp); | |
4085 | ||
4086 | -- When the expression is an access discriminant the check is against | |
4087 | -- the level of the prefix object. | |
4088 | ||
4089 | elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type | |
4090 | and then Nkind (Disc_Exp) = N_Selected_Component | |
4091 | and then Object_Access_Level (Prefix (Disc_Exp)) | |
4092 | > Type_Access_Level (Alloc_Typ) | |
4093 | then | |
4094 | Error_Msg_N | |
4095 | ("access discriminant has deeper level than allocator type", | |
4096 | Disc_Exp); | |
4097 | ||
4098 | -- All other cases are legal | |
4099 | ||
4100 | else | |
4101 | null; | |
4102 | end if; | |
4103 | end Check_Allocator_Discrim_Accessibility; | |
07fc65c4 GB |
4104 | |
4105 | ---------------------------- | |
4106 | -- In_Dispatching_Context -- | |
4107 | ---------------------------- | |
4108 | ||
4109 | function In_Dispatching_Context return Boolean is | |
4110 | Par : constant Node_Id := Parent (N); | |
07fc65c4 | 4111 | begin |
45fc7ddb | 4112 | return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement) |
07fc65c4 GB |
4113 | and then Is_Entity_Name (Name (Par)) |
4114 | and then Is_Dispatching_Operation (Entity (Name (Par))); | |
4115 | end In_Dispatching_Context; | |
4116 | ||
b7d1f17f HK |
4117 | ---------------------------- |
4118 | -- Propagate_Coextensions -- | |
4119 | ---------------------------- | |
4120 | ||
4121 | procedure Propagate_Coextensions (Root : Node_Id) is | |
4122 | ||
4123 | procedure Copy_List (From : Elist_Id; To : Elist_Id); | |
4124 | -- Copy the contents of list From into list To, preserving the | |
4125 | -- order of elements. | |
4126 | ||
4127 | function Process_Allocator (Nod : Node_Id) return Traverse_Result; | |
4128 | -- Recognize an allocator or a rewritten allocator node and add it | |
f3d57416 | 4129 | -- along with its nested coextensions to the list of Root. |
b7d1f17f HK |
4130 | |
4131 | --------------- | |
4132 | -- Copy_List -- | |
4133 | --------------- | |
4134 | ||
4135 | procedure Copy_List (From : Elist_Id; To : Elist_Id) is | |
4136 | From_Elmt : Elmt_Id; | |
4137 | begin | |
4138 | From_Elmt := First_Elmt (From); | |
4139 | while Present (From_Elmt) loop | |
4140 | Append_Elmt (Node (From_Elmt), To); | |
4141 | Next_Elmt (From_Elmt); | |
4142 | end loop; | |
4143 | end Copy_List; | |
4144 | ||
4145 | ----------------------- | |
4146 | -- Process_Allocator -- | |
4147 | ----------------------- | |
4148 | ||
4149 | function Process_Allocator (Nod : Node_Id) return Traverse_Result is | |
4150 | Orig_Nod : Node_Id := Nod; | |
4151 | ||
4152 | begin | |
4153 | -- This is a possible rewritten subtype indication allocator. Any | |
4154 | -- nested coextensions will appear as discriminant constraints. | |
4155 | ||
4156 | if Nkind (Nod) = N_Identifier | |
4157 | and then Present (Original_Node (Nod)) | |
4158 | and then Nkind (Original_Node (Nod)) = N_Subtype_Indication | |
4159 | then | |
4160 | declare | |
4161 | Discr : Node_Id; | |
4162 | Discr_Elmt : Elmt_Id; | |
4163 | ||
4164 | begin | |
4165 | if Is_Record_Type (Entity (Nod)) then | |
4166 | Discr_Elmt := | |
4167 | First_Elmt (Discriminant_Constraint (Entity (Nod))); | |
4168 | while Present (Discr_Elmt) loop | |
4169 | Discr := Node (Discr_Elmt); | |
4170 | ||
4171 | if Nkind (Discr) = N_Identifier | |
4172 | and then Present (Original_Node (Discr)) | |
4173 | and then Nkind (Original_Node (Discr)) = N_Allocator | |
4174 | and then Present (Coextensions ( | |
4175 | Original_Node (Discr))) | |
4176 | then | |
4177 | if No (Coextensions (Root)) then | |
4178 | Set_Coextensions (Root, New_Elmt_List); | |
4179 | end if; | |
4180 | ||
4181 | Copy_List | |
4182 | (From => Coextensions (Original_Node (Discr)), | |
4183 | To => Coextensions (Root)); | |
4184 | end if; | |
4185 | ||
4186 | Next_Elmt (Discr_Elmt); | |
4187 | end loop; | |
4188 | ||
4189 | -- There is no need to continue the traversal of this | |
4190 | -- subtree since all the information has already been | |
4191 | -- propagated. | |
4192 | ||
4193 | return Skip; | |
4194 | end if; | |
4195 | end; | |
4196 | ||
4197 | -- Case of either a stand alone allocator or a rewritten allocator | |
4198 | -- with an aggregate. | |
4199 | ||
4200 | else | |
4201 | if Present (Original_Node (Nod)) then | |
4202 | Orig_Nod := Original_Node (Nod); | |
4203 | end if; | |
4204 | ||
4205 | if Nkind (Orig_Nod) = N_Allocator then | |
4206 | ||
4207 | -- Propagate the list of nested coextensions to the Root | |
4208 | -- allocator. This is done through list copy since a single | |
4209 | -- allocator may have multiple coextensions. Do not touch | |
4210 | -- coextensions roots. | |
4211 | ||
4212 | if not Is_Coextension_Root (Orig_Nod) | |
4213 | and then Present (Coextensions (Orig_Nod)) | |
4214 | then | |
4215 | if No (Coextensions (Root)) then | |
4216 | Set_Coextensions (Root, New_Elmt_List); | |
4217 | end if; | |
4218 | ||
4219 | Copy_List | |
4220 | (From => Coextensions (Orig_Nod), | |
4221 | To => Coextensions (Root)); | |
4222 | end if; | |
4223 | ||
4224 | -- There is no need to continue the traversal of this | |
4225 | -- subtree since all the information has already been | |
4226 | -- propagated. | |
4227 | ||
4228 | return Skip; | |
4229 | end if; | |
4230 | end if; | |
4231 | ||
4232 | -- Keep on traversing, looking for the next allocator | |
4233 | ||
4234 | return OK; | |
4235 | end Process_Allocator; | |
4236 | ||
4237 | procedure Process_Allocators is | |
4238 | new Traverse_Proc (Process_Allocator); | |
4239 | ||
4240 | -- Start of processing for Propagate_Coextensions | |
4241 | ||
4242 | begin | |
4243 | Process_Allocators (Expression (Root)); | |
4244 | end Propagate_Coextensions; | |
4245 | ||
07fc65c4 GB |
4246 | -- Start of processing for Resolve_Allocator |
4247 | ||
996ae0b0 RK |
4248 | begin |
4249 | -- Replace general access with specific type | |
4250 | ||
4251 | if Ekind (Etype (N)) = E_Allocator_Type then | |
4252 | Set_Etype (N, Base_Type (Typ)); | |
4253 | end if; | |
4254 | ||
0669bebe | 4255 | if Is_Abstract_Type (Typ) then |
996ae0b0 RK |
4256 | Error_Msg_N ("type of allocator cannot be abstract", N); |
4257 | end if; | |
4258 | ||
4259 | -- For qualified expression, resolve the expression using the | |
4260 | -- given subtype (nothing to do for type mark, subtype indication) | |
4261 | ||
4262 | if Nkind (E) = N_Qualified_Expression then | |
4263 | if Is_Class_Wide_Type (Etype (E)) | |
4264 | and then not Is_Class_Wide_Type (Designated_Type (Typ)) | |
07fc65c4 | 4265 | and then not In_Dispatching_Context |
996ae0b0 RK |
4266 | then |
4267 | Error_Msg_N | |
4268 | ("class-wide allocator not allowed for this access type", N); | |
4269 | end if; | |
4270 | ||
4271 | Resolve (Expression (E), Etype (E)); | |
4272 | Check_Unset_Reference (Expression (E)); | |
4273 | ||
fbf5a39b | 4274 | -- A qualified expression requires an exact match of the type, |
7b4db06c | 4275 | -- class-wide matching is not allowed. |
fbf5a39b | 4276 | |
7b4db06c | 4277 | if (Is_Class_Wide_Type (Etype (Expression (E))) |
19fb051c | 4278 | or else Is_Class_Wide_Type (Etype (E))) |
fbf5a39b AC |
4279 | and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E)) |
4280 | then | |
4281 | Wrong_Type (Expression (E), Etype (E)); | |
4282 | end if; | |
4283 | ||
b7d1f17f HK |
4284 | -- A special accessibility check is needed for allocators that |
4285 | -- constrain access discriminants. The level of the type of the | |
4286 | -- expression used to constrain an access discriminant cannot be | |
f3d57416 | 4287 | -- deeper than the type of the allocator (in contrast to access |
b7d1f17f HK |
4288 | -- parameters, where the level of the actual can be arbitrary). |
4289 | ||
4290 | -- We can't use Valid_Conversion to perform this check because | |
4291 | -- in general the type of the allocator is unrelated to the type | |
4292 | -- of the access discriminant. | |
4293 | ||
4294 | if Ekind (Typ) /= E_Anonymous_Access_Type | |
4295 | or else Is_Local_Anonymous_Access (Typ) | |
4296 | then | |
4297 | Subtyp := Entity (Subtype_Mark (E)); | |
4298 | ||
4299 | Aggr := Original_Node (Expression (E)); | |
4300 | ||
4301 | if Has_Discriminants (Subtyp) | |
45fc7ddb | 4302 | and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate) |
b7d1f17f HK |
4303 | then |
4304 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4305 | ||
4306 | -- Get the first component expression of the aggregate | |
4307 | ||
4308 | if Present (Expressions (Aggr)) then | |
4309 | Disc_Exp := First (Expressions (Aggr)); | |
4310 | ||
4311 | elsif Present (Component_Associations (Aggr)) then | |
4312 | Assoc := First (Component_Associations (Aggr)); | |
4313 | ||
4314 | if Present (Assoc) then | |
4315 | Disc_Exp := Expression (Assoc); | |
4316 | else | |
4317 | Disc_Exp := Empty; | |
4318 | end if; | |
4319 | ||
4320 | else | |
4321 | Disc_Exp := Empty; | |
4322 | end if; | |
4323 | ||
4324 | while Present (Discrim) and then Present (Disc_Exp) loop | |
4325 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4326 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); | |
4327 | end if; | |
4328 | ||
4329 | Next_Discriminant (Discrim); | |
4330 | ||
4331 | if Present (Discrim) then | |
4332 | if Present (Assoc) then | |
4333 | Next (Assoc); | |
4334 | Disc_Exp := Expression (Assoc); | |
4335 | ||
4336 | elsif Present (Next (Disc_Exp)) then | |
4337 | Next (Disc_Exp); | |
4338 | ||
4339 | else | |
4340 | Assoc := First (Component_Associations (Aggr)); | |
4341 | ||
4342 | if Present (Assoc) then | |
4343 | Disc_Exp := Expression (Assoc); | |
4344 | else | |
4345 | Disc_Exp := Empty; | |
4346 | end if; | |
4347 | end if; | |
4348 | end if; | |
4349 | end loop; | |
4350 | end if; | |
4351 | end if; | |
4352 | ||
996ae0b0 RK |
4353 | -- For a subtype mark or subtype indication, freeze the subtype |
4354 | ||
4355 | else | |
4356 | Freeze_Expression (E); | |
4357 | ||
4358 | if Is_Access_Constant (Typ) and then not No_Initialization (N) then | |
4359 | Error_Msg_N | |
4360 | ("initialization required for access-to-constant allocator", N); | |
4361 | end if; | |
4362 | ||
4363 | -- A special accessibility check is needed for allocators that | |
4364 | -- constrain access discriminants. The level of the type of the | |
b7d1f17f | 4365 | -- expression used to constrain an access discriminant cannot be |
f3d57416 | 4366 | -- deeper than the type of the allocator (in contrast to access |
996ae0b0 RK |
4367 | -- parameters, where the level of the actual can be arbitrary). |
4368 | -- We can't use Valid_Conversion to perform this check because | |
4369 | -- in general the type of the allocator is unrelated to the type | |
b7d1f17f | 4370 | -- of the access discriminant. |
996ae0b0 RK |
4371 | |
4372 | if Nkind (Original_Node (E)) = N_Subtype_Indication | |
b7d1f17f HK |
4373 | and then (Ekind (Typ) /= E_Anonymous_Access_Type |
4374 | or else Is_Local_Anonymous_Access (Typ)) | |
996ae0b0 RK |
4375 | then |
4376 | Subtyp := Entity (Subtype_Mark (Original_Node (E))); | |
4377 | ||
4378 | if Has_Discriminants (Subtyp) then | |
4379 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4380 | Constr := First (Constraints (Constraint (Original_Node (E)))); | |
996ae0b0 RK |
4381 | while Present (Discrim) and then Present (Constr) loop |
4382 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4383 | if Nkind (Constr) = N_Discriminant_Association then | |
4384 | Disc_Exp := Original_Node (Expression (Constr)); | |
4385 | else | |
4386 | Disc_Exp := Original_Node (Constr); | |
4387 | end if; | |
4388 | ||
b7d1f17f | 4389 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); |
996ae0b0 | 4390 | end if; |
b7d1f17f | 4391 | |
996ae0b0 RK |
4392 | Next_Discriminant (Discrim); |
4393 | Next (Constr); | |
4394 | end loop; | |
4395 | end if; | |
4396 | end if; | |
4397 | end if; | |
4398 | ||
758c442c GD |
4399 | -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility |
4400 | -- check that the level of the type of the created object is not deeper | |
4401 | -- than the level of the allocator's access type, since extensions can | |
4402 | -- now occur at deeper levels than their ancestor types. This is a | |
4403 | -- static accessibility level check; a run-time check is also needed in | |
4404 | -- the case of an initialized allocator with a class-wide argument (see | |
4405 | -- Expand_Allocator_Expression). | |
4406 | ||
0791fbe9 | 4407 | if Ada_Version >= Ada_2005 |
758c442c GD |
4408 | and then Is_Class_Wide_Type (Designated_Type (Typ)) |
4409 | then | |
4410 | declare | |
b7d1f17f | 4411 | Exp_Typ : Entity_Id; |
758c442c GD |
4412 | |
4413 | begin | |
4414 | if Nkind (E) = N_Qualified_Expression then | |
4415 | Exp_Typ := Etype (E); | |
4416 | elsif Nkind (E) = N_Subtype_Indication then | |
4417 | Exp_Typ := Entity (Subtype_Mark (Original_Node (E))); | |
4418 | else | |
4419 | Exp_Typ := Entity (E); | |
4420 | end if; | |
4421 | ||
4422 | if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then | |
4423 | if In_Instance_Body then | |
4424 | Error_Msg_N ("?type in allocator has deeper level than" & | |
4425 | " designated class-wide type", E); | |
c8ef728f ES |
4426 | Error_Msg_N ("\?Program_Error will be raised at run time", |
4427 | E); | |
758c442c GD |
4428 | Rewrite (N, |
4429 | Make_Raise_Program_Error (Sloc (N), | |
4430 | Reason => PE_Accessibility_Check_Failed)); | |
4431 | Set_Etype (N, Typ); | |
aa180613 RD |
4432 | |
4433 | -- Do not apply Ada 2005 accessibility checks on a class-wide | |
4434 | -- allocator if the type given in the allocator is a formal | |
4435 | -- type. A run-time check will be performed in the instance. | |
4436 | ||
4437 | elsif not Is_Generic_Type (Exp_Typ) then | |
758c442c GD |
4438 | Error_Msg_N ("type in allocator has deeper level than" & |
4439 | " designated class-wide type", E); | |
4440 | end if; | |
4441 | end if; | |
4442 | end; | |
4443 | end if; | |
4444 | ||
996ae0b0 RK |
4445 | -- Check for allocation from an empty storage pool |
4446 | ||
4447 | if No_Pool_Assigned (Typ) then | |
8da337c5 | 4448 | Error_Msg_N ("allocation from empty storage pool!", N); |
1420b484 | 4449 | |
5cc9353d RD |
4450 | -- If the context is an unchecked conversion, as may happen within an |
4451 | -- inlined subprogram, the allocator is being resolved with its own | |
4452 | -- anonymous type. In that case, if the target type has a specific | |
1420b484 JM |
4453 | -- storage pool, it must be inherited explicitly by the allocator type. |
4454 | ||
4455 | elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion | |
4456 | and then No (Associated_Storage_Pool (Typ)) | |
4457 | then | |
4458 | Set_Associated_Storage_Pool | |
4459 | (Typ, Associated_Storage_Pool (Etype (Parent (N)))); | |
996ae0b0 | 4460 | end if; |
b7d1f17f | 4461 | |
e57ab550 AC |
4462 | if Ekind (Etype (N)) = E_Anonymous_Access_Type then |
4463 | Check_Restriction (No_Anonymous_Allocators, N); | |
4464 | end if; | |
4465 | ||
b7d1f17f HK |
4466 | -- An erroneous allocator may be rewritten as a raise Program_Error |
4467 | -- statement. | |
4468 | ||
4469 | if Nkind (N) = N_Allocator then | |
4470 | ||
4471 | -- An anonymous access discriminant is the definition of a | |
aa5147f0 | 4472 | -- coextension. |
b7d1f17f HK |
4473 | |
4474 | if Ekind (Typ) = E_Anonymous_Access_Type | |
4475 | and then Nkind (Associated_Node_For_Itype (Typ)) = | |
4476 | N_Discriminant_Specification | |
4477 | then | |
4478 | -- Avoid marking an allocator as a dynamic coextension if it is | |
aa5147f0 | 4479 | -- within a static construct. |
b7d1f17f HK |
4480 | |
4481 | if not Is_Static_Coextension (N) then | |
aa5147f0 | 4482 | Set_Is_Dynamic_Coextension (N); |
b7d1f17f HK |
4483 | end if; |
4484 | ||
4485 | -- Cleanup for potential static coextensions | |
4486 | ||
4487 | else | |
aa5147f0 ES |
4488 | Set_Is_Dynamic_Coextension (N, False); |
4489 | Set_Is_Static_Coextension (N, False); | |
b7d1f17f HK |
4490 | end if; |
4491 | ||
aa5147f0 ES |
4492 | -- There is no need to propagate any nested coextensions if they |
4493 | -- are marked as static since they will be rewritten on the spot. | |
4494 | ||
4495 | if not Is_Static_Coextension (N) then | |
4496 | Propagate_Coextensions (N); | |
4497 | end if; | |
b7d1f17f | 4498 | end if; |
996ae0b0 RK |
4499 | end Resolve_Allocator; |
4500 | ||
4501 | --------------------------- | |
4502 | -- Resolve_Arithmetic_Op -- | |
4503 | --------------------------- | |
4504 | ||
4505 | -- Used for resolving all arithmetic operators except exponentiation | |
4506 | ||
4507 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
4508 | L : constant Node_Id := Left_Opnd (N); |
4509 | R : constant Node_Id := Right_Opnd (N); | |
4510 | TL : constant Entity_Id := Base_Type (Etype (L)); | |
4511 | TR : constant Entity_Id := Base_Type (Etype (R)); | |
4512 | T : Entity_Id; | |
4513 | Rop : Node_Id; | |
996ae0b0 RK |
4514 | |
4515 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
4516 | -- We do the resolution using the base type, because intermediate values | |
4517 | -- in expressions always are of the base type, not a subtype of it. | |
4518 | ||
aa180613 RD |
4519 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean; |
4520 | -- Returns True if N is in a context that expects "any real type" | |
4521 | ||
996ae0b0 RK |
4522 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean; |
4523 | -- Return True iff given type is Integer or universal real/integer | |
4524 | ||
4525 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id); | |
4526 | -- Choose type of integer literal in fixed-point operation to conform | |
4527 | -- to available fixed-point type. T is the type of the other operand, | |
4528 | -- which is needed to determine the expected type of N. | |
4529 | ||
4530 | procedure Set_Operand_Type (N : Node_Id); | |
4531 | -- Set operand type to T if universal | |
4532 | ||
aa180613 RD |
4533 | ------------------------------- |
4534 | -- Expected_Type_Is_Any_Real -- | |
4535 | ------------------------------- | |
4536 | ||
4537 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is | |
4538 | begin | |
4539 | -- N is the expression after "delta" in a fixed_point_definition; | |
4540 | -- see RM-3.5.9(6): | |
4541 | ||
45fc7ddb HK |
4542 | return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition, |
4543 | N_Decimal_Fixed_Point_Definition, | |
aa180613 RD |
4544 | |
4545 | -- N is one of the bounds in a real_range_specification; | |
4546 | -- see RM-3.5.7(5): | |
4547 | ||
45fc7ddb | 4548 | N_Real_Range_Specification, |
aa180613 RD |
4549 | |
4550 | -- N is the expression of a delta_constraint; | |
4551 | -- see RM-J.3(3): | |
4552 | ||
45fc7ddb | 4553 | N_Delta_Constraint); |
aa180613 RD |
4554 | end Expected_Type_Is_Any_Real; |
4555 | ||
996ae0b0 RK |
4556 | ----------------------------- |
4557 | -- Is_Integer_Or_Universal -- | |
4558 | ----------------------------- | |
4559 | ||
4560 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean is | |
4561 | T : Entity_Id; | |
4562 | Index : Interp_Index; | |
4563 | It : Interp; | |
4564 | ||
4565 | begin | |
4566 | if not Is_Overloaded (N) then | |
4567 | T := Etype (N); | |
4568 | return Base_Type (T) = Base_Type (Standard_Integer) | |
4569 | or else T = Universal_Integer | |
4570 | or else T = Universal_Real; | |
4571 | else | |
4572 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4573 | while Present (It.Typ) loop |
996ae0b0 RK |
4574 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) |
4575 | or else It.Typ = Universal_Integer | |
4576 | or else It.Typ = Universal_Real | |
4577 | then | |
4578 | return True; | |
4579 | end if; | |
4580 | ||
4581 | Get_Next_Interp (Index, It); | |
4582 | end loop; | |
4583 | end if; | |
4584 | ||
4585 | return False; | |
4586 | end Is_Integer_Or_Universal; | |
4587 | ||
4588 | ---------------------------- | |
4589 | -- Set_Mixed_Mode_Operand -- | |
4590 | ---------------------------- | |
4591 | ||
4592 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is | |
4593 | Index : Interp_Index; | |
4594 | It : Interp; | |
4595 | ||
4596 | begin | |
4597 | if Universal_Interpretation (N) = Universal_Integer then | |
4598 | ||
4599 | -- A universal integer literal is resolved as standard integer | |
758c442c GD |
4600 | -- except in the case of a fixed-point result, where we leave it |
4601 | -- as universal (to be handled by Exp_Fixd later on) | |
996ae0b0 RK |
4602 | |
4603 | if Is_Fixed_Point_Type (T) then | |
4604 | Resolve (N, Universal_Integer); | |
4605 | else | |
4606 | Resolve (N, Standard_Integer); | |
4607 | end if; | |
4608 | ||
4609 | elsif Universal_Interpretation (N) = Universal_Real | |
4610 | and then (T = Base_Type (Standard_Integer) | |
4611 | or else T = Universal_Integer | |
4612 | or else T = Universal_Real) | |
4613 | then | |
4614 | -- A universal real can appear in a fixed-type context. We resolve | |
4615 | -- the literal with that context, even though this might raise an | |
4616 | -- exception prematurely (the other operand may be zero). | |
4617 | ||
4618 | Resolve (N, B_Typ); | |
4619 | ||
4620 | elsif Etype (N) = Base_Type (Standard_Integer) | |
4621 | and then T = Universal_Real | |
4622 | and then Is_Overloaded (N) | |
4623 | then | |
4624 | -- Integer arg in mixed-mode operation. Resolve with universal | |
4625 | -- type, in case preference rule must be applied. | |
4626 | ||
4627 | Resolve (N, Universal_Integer); | |
4628 | ||
4629 | elsif Etype (N) = T | |
4630 | and then B_Typ /= Universal_Fixed | |
4631 | then | |
a77842bd | 4632 | -- Not a mixed-mode operation, resolve with context |
996ae0b0 RK |
4633 | |
4634 | Resolve (N, B_Typ); | |
4635 | ||
4636 | elsif Etype (N) = Any_Fixed then | |
4637 | ||
a77842bd | 4638 | -- N may itself be a mixed-mode operation, so use context type |
996ae0b0 RK |
4639 | |
4640 | Resolve (N, B_Typ); | |
4641 | ||
4642 | elsif Is_Fixed_Point_Type (T) | |
4643 | and then B_Typ = Universal_Fixed | |
4644 | and then Is_Overloaded (N) | |
4645 | then | |
4646 | -- Must be (fixed * fixed) operation, operand must have one | |
4647 | -- compatible interpretation. | |
4648 | ||
4649 | Resolve (N, Any_Fixed); | |
4650 | ||
4651 | elsif Is_Fixed_Point_Type (B_Typ) | |
4652 | and then (T = Universal_Real | |
4653 | or else Is_Fixed_Point_Type (T)) | |
4654 | and then Is_Overloaded (N) | |
4655 | then | |
4656 | -- C * F(X) in a fixed context, where C is a real literal or a | |
4657 | -- fixed-point expression. F must have either a fixed type | |
4658 | -- interpretation or an integer interpretation, but not both. | |
4659 | ||
4660 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4661 | while Present (It.Typ) loop |
996ae0b0 | 4662 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) then |
996ae0b0 RK |
4663 | if Analyzed (N) then |
4664 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4665 | else | |
4666 | Resolve (N, Standard_Integer); | |
4667 | end if; | |
4668 | ||
4669 | elsif Is_Fixed_Point_Type (It.Typ) then | |
996ae0b0 RK |
4670 | if Analyzed (N) then |
4671 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4672 | else | |
4673 | Resolve (N, It.Typ); | |
4674 | end if; | |
4675 | end if; | |
4676 | ||
4677 | Get_Next_Interp (Index, It); | |
4678 | end loop; | |
4679 | ||
758c442c GD |
4680 | -- Reanalyze the literal with the fixed type of the context. If |
4681 | -- context is Universal_Fixed, we are within a conversion, leave | |
4682 | -- the literal as a universal real because there is no usable | |
4683 | -- fixed type, and the target of the conversion plays no role in | |
4684 | -- the resolution. | |
996ae0b0 | 4685 | |
0ab80019 AC |
4686 | declare |
4687 | Op2 : Node_Id; | |
4688 | T2 : Entity_Id; | |
4689 | ||
4690 | begin | |
4691 | if N = L then | |
4692 | Op2 := R; | |
4693 | else | |
4694 | Op2 := L; | |
4695 | end if; | |
4696 | ||
4697 | if B_Typ = Universal_Fixed | |
4698 | and then Nkind (Op2) = N_Real_Literal | |
4699 | then | |
4700 | T2 := Universal_Real; | |
4701 | else | |
4702 | T2 := B_Typ; | |
4703 | end if; | |
4704 | ||
4705 | Set_Analyzed (Op2, False); | |
4706 | Resolve (Op2, T2); | |
4707 | end; | |
996ae0b0 RK |
4708 | |
4709 | else | |
fbf5a39b | 4710 | Resolve (N); |
996ae0b0 RK |
4711 | end if; |
4712 | end Set_Mixed_Mode_Operand; | |
4713 | ||
4714 | ---------------------- | |
4715 | -- Set_Operand_Type -- | |
4716 | ---------------------- | |
4717 | ||
4718 | procedure Set_Operand_Type (N : Node_Id) is | |
4719 | begin | |
4720 | if Etype (N) = Universal_Integer | |
4721 | or else Etype (N) = Universal_Real | |
4722 | then | |
4723 | Set_Etype (N, T); | |
4724 | end if; | |
4725 | end Set_Operand_Type; | |
4726 | ||
996ae0b0 RK |
4727 | -- Start of processing for Resolve_Arithmetic_Op |
4728 | ||
4729 | begin | |
4730 | if Comes_From_Source (N) | |
4731 | and then Ekind (Entity (N)) = E_Function | |
4732 | and then Is_Imported (Entity (N)) | |
fbf5a39b | 4733 | and then Is_Intrinsic_Subprogram (Entity (N)) |
996ae0b0 RK |
4734 | then |
4735 | Resolve_Intrinsic_Operator (N, Typ); | |
4736 | return; | |
4737 | ||
5cc9353d RD |
4738 | -- Special-case for mixed-mode universal expressions or fixed point type |
4739 | -- operation: each argument is resolved separately. The same treatment | |
4740 | -- is required if one of the operands of a fixed point operation is | |
4741 | -- universal real, since in this case we don't do a conversion to a | |
4742 | -- specific fixed-point type (instead the expander handles the case). | |
996ae0b0 | 4743 | |
45fc7ddb | 4744 | elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real) |
996ae0b0 RK |
4745 | and then Present (Universal_Interpretation (L)) |
4746 | and then Present (Universal_Interpretation (R)) | |
4747 | then | |
4748 | Resolve (L, Universal_Interpretation (L)); | |
4749 | Resolve (R, Universal_Interpretation (R)); | |
4750 | Set_Etype (N, B_Typ); | |
4751 | ||
4752 | elsif (B_Typ = Universal_Real | |
45fc7ddb HK |
4753 | or else Etype (N) = Universal_Fixed |
4754 | or else (Etype (N) = Any_Fixed | |
4755 | and then Is_Fixed_Point_Type (B_Typ)) | |
4756 | or else (Is_Fixed_Point_Type (B_Typ) | |
4757 | and then (Is_Integer_Or_Universal (L) | |
4758 | or else | |
4759 | Is_Integer_Or_Universal (R)))) | |
4760 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) | |
996ae0b0 RK |
4761 | then |
4762 | if TL = Universal_Integer or else TR = Universal_Integer then | |
4763 | Check_For_Visible_Operator (N, B_Typ); | |
4764 | end if; | |
4765 | ||
5cc9353d RD |
4766 | -- If context is a fixed type and one operand is integer, the other |
4767 | -- is resolved with the type of the context. | |
996ae0b0 RK |
4768 | |
4769 | if Is_Fixed_Point_Type (B_Typ) | |
4770 | and then (Base_Type (TL) = Base_Type (Standard_Integer) | |
4771 | or else TL = Universal_Integer) | |
4772 | then | |
4773 | Resolve (R, B_Typ); | |
4774 | Resolve (L, TL); | |
4775 | ||
4776 | elsif Is_Fixed_Point_Type (B_Typ) | |
4777 | and then (Base_Type (TR) = Base_Type (Standard_Integer) | |
4778 | or else TR = Universal_Integer) | |
4779 | then | |
4780 | Resolve (L, B_Typ); | |
4781 | Resolve (R, TR); | |
4782 | ||
4783 | else | |
4784 | Set_Mixed_Mode_Operand (L, TR); | |
4785 | Set_Mixed_Mode_Operand (R, TL); | |
4786 | end if; | |
4787 | ||
45fc7ddb HK |
4788 | -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed |
4789 | -- multiplying operators from being used when the expected type is | |
4790 | -- also universal_fixed. Note that B_Typ will be Universal_Fixed in | |
4791 | -- some cases where the expected type is actually Any_Real; | |
4792 | -- Expected_Type_Is_Any_Real takes care of that case. | |
aa180613 | 4793 | |
996ae0b0 RK |
4794 | if Etype (N) = Universal_Fixed |
4795 | or else Etype (N) = Any_Fixed | |
4796 | then | |
4797 | if B_Typ = Universal_Fixed | |
aa180613 | 4798 | and then not Expected_Type_Is_Any_Real (N) |
45fc7ddb HK |
4799 | and then not Nkind_In (Parent (N), N_Type_Conversion, |
4800 | N_Unchecked_Type_Conversion) | |
996ae0b0 | 4801 | then |
45fc7ddb HK |
4802 | Error_Msg_N ("type cannot be determined from context!", N); |
4803 | Error_Msg_N ("\explicit conversion to result type required", N); | |
996ae0b0 RK |
4804 | |
4805 | Set_Etype (L, Any_Type); | |
4806 | Set_Etype (R, Any_Type); | |
4807 | ||
4808 | else | |
0ab80019 | 4809 | if Ada_Version = Ada_83 |
45fc7ddb HK |
4810 | and then Etype (N) = Universal_Fixed |
4811 | and then not | |
4812 | Nkind_In (Parent (N), N_Type_Conversion, | |
4813 | N_Unchecked_Type_Conversion) | |
996ae0b0 RK |
4814 | then |
4815 | Error_Msg_N | |
45fc7ddb HK |
4816 | ("(Ada 83) fixed-point operation " |
4817 | & "needs explicit conversion", N); | |
996ae0b0 RK |
4818 | end if; |
4819 | ||
aa180613 | 4820 | -- The expected type is "any real type" in contexts like |
5cc9353d | 4821 | |
aa180613 | 4822 | -- type T is delta <universal_fixed-expression> ... |
5cc9353d | 4823 | |
aa180613 RD |
4824 | -- in which case we need to set the type to Universal_Real |
4825 | -- so that static expression evaluation will work properly. | |
4826 | ||
4827 | if Expected_Type_Is_Any_Real (N) then | |
4828 | Set_Etype (N, Universal_Real); | |
4829 | else | |
4830 | Set_Etype (N, B_Typ); | |
4831 | end if; | |
996ae0b0 RK |
4832 | end if; |
4833 | ||
4834 | elsif Is_Fixed_Point_Type (B_Typ) | |
4835 | and then (Is_Integer_Or_Universal (L) | |
4836 | or else Nkind (L) = N_Real_Literal | |
4837 | or else Nkind (R) = N_Real_Literal | |
45fc7ddb | 4838 | or else Is_Integer_Or_Universal (R)) |
996ae0b0 RK |
4839 | then |
4840 | Set_Etype (N, B_Typ); | |
4841 | ||
4842 | elsif Etype (N) = Any_Fixed then | |
4843 | ||
5cc9353d RD |
4844 | -- If no previous errors, this is only possible if one operand is |
4845 | -- overloaded and the context is universal. Resolve as such. | |
996ae0b0 RK |
4846 | |
4847 | Set_Etype (N, B_Typ); | |
4848 | end if; | |
4849 | ||
4850 | else | |
4851 | if (TL = Universal_Integer or else TL = Universal_Real) | |
45fc7ddb HK |
4852 | and then |
4853 | (TR = Universal_Integer or else TR = Universal_Real) | |
996ae0b0 RK |
4854 | then |
4855 | Check_For_Visible_Operator (N, B_Typ); | |
4856 | end if; | |
4857 | ||
4858 | -- If the context is Universal_Fixed and the operands are also | |
4859 | -- universal fixed, this is an error, unless there is only one | |
841dd0f5 | 4860 | -- applicable fixed_point type (usually Duration). |
996ae0b0 | 4861 | |
45fc7ddb | 4862 | if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then |
996ae0b0 RK |
4863 | T := Unique_Fixed_Point_Type (N); |
4864 | ||
4865 | if T = Any_Type then | |
4866 | Set_Etype (N, T); | |
4867 | return; | |
4868 | else | |
4869 | Resolve (L, T); | |
4870 | Resolve (R, T); | |
4871 | end if; | |
4872 | ||
4873 | else | |
4874 | Resolve (L, B_Typ); | |
4875 | Resolve (R, B_Typ); | |
4876 | end if; | |
4877 | ||
4878 | -- If one of the arguments was resolved to a non-universal type. | |
4879 | -- label the result of the operation itself with the same type. | |
4880 | -- Do the same for the universal argument, if any. | |
4881 | ||
4882 | T := Intersect_Types (L, R); | |
4883 | Set_Etype (N, Base_Type (T)); | |
4884 | Set_Operand_Type (L); | |
4885 | Set_Operand_Type (R); | |
4886 | end if; | |
4887 | ||
fbf5a39b | 4888 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
4889 | Eval_Arithmetic_Op (N); |
4890 | ||
b0186f71 AC |
4891 | -- In SPARK and ALFA, a multiplication or division with operands of |
4892 | -- fixed point types shall be qualified or explicitly converted to | |
4893 | -- identify the result type. | |
4894 | ||
fe5d3068 YM |
4895 | if (Is_Fixed_Point_Type (Etype (L)) |
4896 | or else Is_Fixed_Point_Type (Etype (R))) | |
b0186f71 AC |
4897 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) |
4898 | and then | |
4899 | not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion) | |
4900 | then | |
fe5d3068 YM |
4901 | Check_Formal_Restriction |
4902 | ("operation should be qualified or explicitly converted", N); | |
b0186f71 AC |
4903 | end if; |
4904 | ||
996ae0b0 RK |
4905 | -- Set overflow and division checking bit. Much cleverer code needed |
4906 | -- here eventually and perhaps the Resolve routines should be separated | |
4907 | -- for the various arithmetic operations, since they will need | |
4908 | -- different processing. ??? | |
4909 | ||
4910 | if Nkind (N) in N_Op then | |
4911 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 4912 | Enable_Overflow_Check (N); |
996ae0b0 RK |
4913 | end if; |
4914 | ||
fbf5a39b AC |
4915 | -- Give warning if explicit division by zero |
4916 | ||
45fc7ddb | 4917 | if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod) |
996ae0b0 RK |
4918 | and then not Division_Checks_Suppressed (Etype (N)) |
4919 | then | |
fbf5a39b AC |
4920 | Rop := Right_Opnd (N); |
4921 | ||
4922 | if Compile_Time_Known_Value (Rop) | |
4923 | and then ((Is_Integer_Type (Etype (Rop)) | |
780d052e RD |
4924 | and then Expr_Value (Rop) = Uint_0) |
4925 | or else | |
4926 | (Is_Real_Type (Etype (Rop)) | |
4927 | and then Expr_Value_R (Rop) = Ureal_0)) | |
fbf5a39b | 4928 | then |
aa180613 RD |
4929 | -- Specialize the warning message according to the operation |
4930 | ||
4931 | case Nkind (N) is | |
4932 | when N_Op_Divide => | |
4933 | Apply_Compile_Time_Constraint_Error | |
4934 | (N, "division by zero?", CE_Divide_By_Zero, | |
4935 | Loc => Sloc (Right_Opnd (N))); | |
4936 | ||
4937 | when N_Op_Rem => | |
4938 | Apply_Compile_Time_Constraint_Error | |
4939 | (N, "rem with zero divisor?", CE_Divide_By_Zero, | |
4940 | Loc => Sloc (Right_Opnd (N))); | |
4941 | ||
4942 | when N_Op_Mod => | |
4943 | Apply_Compile_Time_Constraint_Error | |
4944 | (N, "mod with zero divisor?", CE_Divide_By_Zero, | |
4945 | Loc => Sloc (Right_Opnd (N))); | |
4946 | ||
4947 | -- Division by zero can only happen with division, rem, | |
4948 | -- and mod operations. | |
4949 | ||
4950 | when others => | |
4951 | raise Program_Error; | |
4952 | end case; | |
fbf5a39b AC |
4953 | |
4954 | -- Otherwise just set the flag to check at run time | |
4955 | ||
4956 | else | |
b7d1f17f | 4957 | Activate_Division_Check (N); |
fbf5a39b | 4958 | end if; |
996ae0b0 | 4959 | end if; |
45fc7ddb HK |
4960 | |
4961 | -- If Restriction No_Implicit_Conditionals is active, then it is | |
4962 | -- violated if either operand can be negative for mod, or for rem | |
4963 | -- if both operands can be negative. | |
4964 | ||
7a963087 | 4965 | if Restriction_Check_Required (No_Implicit_Conditionals) |
45fc7ddb HK |
4966 | and then Nkind_In (N, N_Op_Rem, N_Op_Mod) |
4967 | then | |
4968 | declare | |
4969 | Lo : Uint; | |
4970 | Hi : Uint; | |
4971 | OK : Boolean; | |
4972 | ||
4973 | LNeg : Boolean; | |
4974 | RNeg : Boolean; | |
4975 | -- Set if corresponding operand might be negative | |
4976 | ||
4977 | begin | |
5d5e9775 AC |
4978 | Determine_Range |
4979 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
4980 | LNeg := (not OK) or else Lo < 0; |
4981 | ||
5d5e9775 AC |
4982 | Determine_Range |
4983 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
4984 | RNeg := (not OK) or else Lo < 0; |
4985 | ||
5d5e9775 AC |
4986 | -- Check if we will be generating conditionals. There are two |
4987 | -- cases where that can happen, first for REM, the only case | |
4988 | -- is largest negative integer mod -1, where the division can | |
4989 | -- overflow, but we still have to give the right result. The | |
4990 | -- front end generates a test for this annoying case. Here we | |
4991 | -- just test if both operands can be negative (that's what the | |
4992 | -- expander does, so we match its logic here). | |
4993 | ||
4994 | -- The second case is mod where either operand can be negative. | |
308e6f3a | 4995 | -- In this case, the back end has to generate additional tests. |
5d5e9775 | 4996 | |
45fc7ddb HK |
4997 | if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg)) |
4998 | or else | |
4999 | (Nkind (N) = N_Op_Mod and then (LNeg or RNeg)) | |
5000 | then | |
5001 | Check_Restriction (No_Implicit_Conditionals, N); | |
5002 | end if; | |
5003 | end; | |
5004 | end if; | |
996ae0b0 RK |
5005 | end if; |
5006 | ||
5007 | Check_Unset_Reference (L); | |
5008 | Check_Unset_Reference (R); | |
996ae0b0 RK |
5009 | end Resolve_Arithmetic_Op; |
5010 | ||
5011 | ------------------ | |
5012 | -- Resolve_Call -- | |
5013 | ------------------ | |
5014 | ||
5015 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is | |
5016 | Loc : constant Source_Ptr := Sloc (N); | |
5017 | Subp : constant Node_Id := Name (N); | |
5018 | Nam : Entity_Id; | |
5019 | I : Interp_Index; | |
5020 | It : Interp; | |
5021 | Norm_OK : Boolean; | |
5022 | Scop : Entity_Id; | |
aa180613 | 5023 | Rtype : Entity_Id; |
996ae0b0 | 5024 | |
ee81cbe9 AC |
5025 | function Same_Or_Aliased_Subprograms |
5026 | (S : Entity_Id; | |
5027 | E : Entity_Id) return Boolean; | |
5028 | -- Returns True if the subprogram entity S is the same as E or else | |
5029 | -- S is an alias of E. | |
5030 | ||
001c7783 AC |
5031 | --------------------------------- |
5032 | -- Same_Or_Aliased_Subprograms -- | |
5033 | --------------------------------- | |
5034 | ||
ee81cbe9 AC |
5035 | function Same_Or_Aliased_Subprograms |
5036 | (S : Entity_Id; | |
5037 | E : Entity_Id) return Boolean | |
5038 | is | |
5039 | Subp_Alias : constant Entity_Id := Alias (S); | |
ee81cbe9 AC |
5040 | begin |
5041 | return S = E | |
5042 | or else (Present (Subp_Alias) and then Subp_Alias = E); | |
5043 | end Same_Or_Aliased_Subprograms; | |
5044 | ||
5045 | -- Start of processing for Resolve_Call | |
5046 | ||
996ae0b0 | 5047 | begin |
758c442c GD |
5048 | -- The context imposes a unique interpretation with type Typ on a |
5049 | -- procedure or function call. Find the entity of the subprogram that | |
5050 | -- yields the expected type, and propagate the corresponding formal | |
5051 | -- constraints on the actuals. The caller has established that an | |
5052 | -- interpretation exists, and emitted an error if not unique. | |
996ae0b0 RK |
5053 | |
5054 | -- First deal with the case of a call to an access-to-subprogram, | |
5055 | -- dereference made explicit in Analyze_Call. | |
5056 | ||
5057 | if Ekind (Etype (Subp)) = E_Subprogram_Type then | |
996ae0b0 RK |
5058 | if not Is_Overloaded (Subp) then |
5059 | Nam := Etype (Subp); | |
5060 | ||
5061 | else | |
758c442c GD |
5062 | -- Find the interpretation whose type (a subprogram type) has a |
5063 | -- return type that is compatible with the context. Analysis of | |
5064 | -- the node has established that one exists. | |
996ae0b0 | 5065 | |
996ae0b0 RK |
5066 | Nam := Empty; |
5067 | ||
1420b484 | 5068 | Get_First_Interp (Subp, I, It); |
996ae0b0 | 5069 | while Present (It.Typ) loop |
996ae0b0 RK |
5070 | if Covers (Typ, Etype (It.Typ)) then |
5071 | Nam := It.Typ; | |
5072 | exit; | |
5073 | end if; | |
5074 | ||
5075 | Get_Next_Interp (I, It); | |
5076 | end loop; | |
5077 | ||
5078 | if No (Nam) then | |
5079 | raise Program_Error; | |
5080 | end if; | |
5081 | end if; | |
5082 | ||
5083 | -- If the prefix is not an entity, then resolve it | |
5084 | ||
5085 | if not Is_Entity_Name (Subp) then | |
5086 | Resolve (Subp, Nam); | |
5087 | end if; | |
5088 | ||
758c442c GD |
5089 | -- For an indirect call, we always invalidate checks, since we do not |
5090 | -- know whether the subprogram is local or global. Yes we could do | |
5091 | -- better here, e.g. by knowing that there are no local subprograms, | |
aa180613 | 5092 | -- but it does not seem worth the effort. Similarly, we kill all |
758c442c | 5093 | -- knowledge of current constant values. |
fbf5a39b AC |
5094 | |
5095 | Kill_Current_Values; | |
5096 | ||
b7d1f17f HK |
5097 | -- If this is a procedure call which is really an entry call, do |
5098 | -- the conversion of the procedure call to an entry call. Protected | |
5099 | -- operations use the same circuitry because the name in the call | |
5100 | -- can be an arbitrary expression with special resolution rules. | |
996ae0b0 | 5101 | |
45fc7ddb | 5102 | elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component) |
996ae0b0 RK |
5103 | or else (Is_Entity_Name (Subp) |
5104 | and then Ekind (Entity (Subp)) = E_Entry) | |
5105 | then | |
5106 | Resolve_Entry_Call (N, Typ); | |
5107 | Check_Elab_Call (N); | |
fbf5a39b AC |
5108 | |
5109 | -- Kill checks and constant values, as above for indirect case | |
5110 | -- Who knows what happens when another task is activated? | |
5111 | ||
5112 | Kill_Current_Values; | |
996ae0b0 RK |
5113 | return; |
5114 | ||
5115 | -- Normal subprogram call with name established in Resolve | |
5116 | ||
5117 | elsif not (Is_Type (Entity (Subp))) then | |
5118 | Nam := Entity (Subp); | |
5119 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
5120 | |
5121 | -- Otherwise we must have the case of an overloaded call | |
5122 | ||
5123 | else | |
5124 | pragma Assert (Is_Overloaded (Subp)); | |
d81b4bfe TQ |
5125 | |
5126 | -- Initialize Nam to prevent warning (we know it will be assigned | |
5127 | -- in the loop below, but the compiler does not know that). | |
5128 | ||
5129 | Nam := Empty; | |
996ae0b0 RK |
5130 | |
5131 | Get_First_Interp (Subp, I, It); | |
996ae0b0 RK |
5132 | while Present (It.Typ) loop |
5133 | if Covers (Typ, It.Typ) then | |
5134 | Nam := It.Nam; | |
5135 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
5136 | exit; |
5137 | end if; | |
5138 | ||
5139 | Get_Next_Interp (I, It); | |
5140 | end loop; | |
5141 | end if; | |
5142 | ||
c9b99571 ES |
5143 | if Is_Access_Subprogram_Type (Base_Type (Etype (Nam))) |
5144 | and then not Is_Access_Subprogram_Type (Base_Type (Typ)) | |
53cf4600 ES |
5145 | and then Nkind (Subp) /= N_Explicit_Dereference |
5146 | and then Present (Parameter_Associations (N)) | |
5147 | then | |
66aa7643 TQ |
5148 | -- The prefix is a parameterless function call that returns an access |
5149 | -- to subprogram. If parameters are present in the current call, add | |
5150 | -- add an explicit dereference. We use the base type here because | |
5151 | -- within an instance these may be subtypes. | |
53cf4600 ES |
5152 | |
5153 | -- The dereference is added either in Analyze_Call or here. Should | |
5154 | -- be consolidated ??? | |
5155 | ||
5156 | Set_Is_Overloaded (Subp, False); | |
5157 | Set_Etype (Subp, Etype (Nam)); | |
5158 | Insert_Explicit_Dereference (Subp); | |
5159 | Nam := Designated_Type (Etype (Nam)); | |
5160 | Resolve (Subp, Nam); | |
5161 | end if; | |
5162 | ||
996ae0b0 RK |
5163 | -- Check that a call to Current_Task does not occur in an entry body |
5164 | ||
5165 | if Is_RTE (Nam, RE_Current_Task) then | |
5166 | declare | |
5167 | P : Node_Id; | |
5168 | ||
5169 | begin | |
5170 | P := N; | |
5171 | loop | |
5172 | P := Parent (P); | |
45fc7ddb HK |
5173 | |
5174 | -- Exclude calls that occur within the default of a formal | |
5175 | -- parameter of the entry, since those are evaluated outside | |
5176 | -- of the body. | |
5177 | ||
5178 | exit when No (P) or else Nkind (P) = N_Parameter_Specification; | |
996ae0b0 | 5179 | |
aa180613 RD |
5180 | if Nkind (P) = N_Entry_Body |
5181 | or else (Nkind (P) = N_Subprogram_Body | |
45fc7ddb | 5182 | and then Is_Entry_Barrier_Function (P)) |
aa180613 RD |
5183 | then |
5184 | Rtype := Etype (N); | |
996ae0b0 | 5185 | Error_Msg_NE |
aa5147f0 | 5186 | ("?& should not be used in entry body (RM C.7(17))", |
996ae0b0 | 5187 | N, Nam); |
aa180613 RD |
5188 | Error_Msg_NE |
5189 | ("\Program_Error will be raised at run time?", N, Nam); | |
5190 | Rewrite (N, | |
5191 | Make_Raise_Program_Error (Loc, | |
5192 | Reason => PE_Current_Task_In_Entry_Body)); | |
5193 | Set_Etype (N, Rtype); | |
e65f50ec | 5194 | return; |
996ae0b0 RK |
5195 | end if; |
5196 | end loop; | |
5197 | end; | |
5198 | end if; | |
5199 | ||
758c442c GD |
5200 | -- Check that a procedure call does not occur in the context of the |
5201 | -- entry call statement of a conditional or timed entry call. Note that | |
5202 | -- the case of a call to a subprogram renaming of an entry will also be | |
5203 | -- rejected. The test for N not being an N_Entry_Call_Statement is | |
5204 | -- defensive, covering the possibility that the processing of entry | |
5205 | -- calls might reach this point due to later modifications of the code | |
5206 | -- above. | |
996ae0b0 RK |
5207 | |
5208 | if Nkind (Parent (N)) = N_Entry_Call_Alternative | |
5209 | and then Nkind (N) /= N_Entry_Call_Statement | |
5210 | and then Entry_Call_Statement (Parent (N)) = N | |
5211 | then | |
0791fbe9 | 5212 | if Ada_Version < Ada_2005 then |
1420b484 JM |
5213 | Error_Msg_N ("entry call required in select statement", N); |
5214 | ||
5215 | -- Ada 2005 (AI-345): If a procedure_call_statement is used | |
66aa7643 TQ |
5216 | -- for a procedure_or_entry_call, the procedure_name or |
5217 | -- procedure_prefix of the procedure_call_statement shall denote | |
1420b484 JM |
5218 | -- an entry renamed by a procedure, or (a view of) a primitive |
5219 | -- subprogram of a limited interface whose first parameter is | |
5220 | -- a controlling parameter. | |
5221 | ||
5222 | elsif Nkind (N) = N_Procedure_Call_Statement | |
5223 | and then not Is_Renamed_Entry (Nam) | |
5224 | and then not Is_Controlling_Limited_Procedure (Nam) | |
5225 | then | |
5226 | Error_Msg_N | |
c8ef728f | 5227 | ("entry call or dispatching primitive of interface required", N); |
1420b484 | 5228 | end if; |
996ae0b0 RK |
5229 | end if; |
5230 | ||
66aa7643 TQ |
5231 | -- Check that this is not a call to a protected procedure or entry from |
5232 | -- within a protected function. | |
fbf5a39b AC |
5233 | |
5234 | if Ekind (Current_Scope) = E_Function | |
5235 | and then Ekind (Scope (Current_Scope)) = E_Protected_Type | |
5236 | and then Ekind (Nam) /= E_Function | |
5237 | and then Scope (Nam) = Scope (Current_Scope) | |
5238 | then | |
5239 | Error_Msg_N ("within protected function, protected " & | |
5240 | "object is constant", N); | |
5241 | Error_Msg_N ("\cannot call operation that may modify it", N); | |
5242 | end if; | |
5243 | ||
45fc7ddb | 5244 | -- Freeze the subprogram name if not in a spec-expression. Note that we |
758c442c GD |
5245 | -- freeze procedure calls as well as function calls. Procedure calls are |
5246 | -- not frozen according to the rules (RM 13.14(14)) because it is | |
5247 | -- impossible to have a procedure call to a non-frozen procedure in pure | |
5248 | -- Ada, but in the code that we generate in the expander, this rule | |
5249 | -- needs extending because we can generate procedure calls that need | |
5250 | -- freezing. | |
996ae0b0 | 5251 | |
45fc7ddb | 5252 | if Is_Entity_Name (Subp) and then not In_Spec_Expression then |
996ae0b0 RK |
5253 | Freeze_Expression (Subp); |
5254 | end if; | |
5255 | ||
758c442c GD |
5256 | -- For a predefined operator, the type of the result is the type imposed |
5257 | -- by context, except for a predefined operation on universal fixed. | |
5258 | -- Otherwise The type of the call is the type returned by the subprogram | |
5259 | -- being called. | |
996ae0b0 RK |
5260 | |
5261 | if Is_Predefined_Op (Nam) then | |
996ae0b0 RK |
5262 | if Etype (N) /= Universal_Fixed then |
5263 | Set_Etype (N, Typ); | |
5264 | end if; | |
5265 | ||
758c442c GD |
5266 | -- If the subprogram returns an array type, and the context requires the |
5267 | -- component type of that array type, the node is really an indexing of | |
5268 | -- the parameterless call. Resolve as such. A pathological case occurs | |
5269 | -- when the type of the component is an access to the array type. In | |
5270 | -- this case the call is truly ambiguous. | |
996ae0b0 | 5271 | |
0669bebe | 5272 | elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam)) |
996ae0b0 RK |
5273 | and then |
5274 | ((Is_Array_Type (Etype (Nam)) | |
19fb051c | 5275 | and then Covers (Typ, Component_Type (Etype (Nam)))) |
996ae0b0 | 5276 | or else (Is_Access_Type (Etype (Nam)) |
19fb051c AC |
5277 | and then Is_Array_Type (Designated_Type (Etype (Nam))) |
5278 | and then | |
5279 | Covers | |
5280 | (Typ, | |
5281 | Component_Type (Designated_Type (Etype (Nam)))))) | |
996ae0b0 RK |
5282 | then |
5283 | declare | |
5284 | Index_Node : Node_Id; | |
fbf5a39b AC |
5285 | New_Subp : Node_Id; |
5286 | Ret_Type : constant Entity_Id := Etype (Nam); | |
996ae0b0 RK |
5287 | |
5288 | begin | |
fbf5a39b AC |
5289 | if Is_Access_Type (Ret_Type) |
5290 | and then Ret_Type = Component_Type (Designated_Type (Ret_Type)) | |
5291 | then | |
5292 | Error_Msg_N | |
5293 | ("cannot disambiguate function call and indexing", N); | |
5294 | else | |
5295 | New_Subp := Relocate_Node (Subp); | |
5296 | Set_Entity (Subp, Nam); | |
5297 | ||
7205254b | 5298 | if (Is_Array_Type (Ret_Type) |
5d5e9775 | 5299 | and then Component_Type (Ret_Type) /= Any_Type) |
7205254b JM |
5300 | or else |
5301 | (Is_Access_Type (Ret_Type) | |
5d5e9775 AC |
5302 | and then |
5303 | Component_Type (Designated_Type (Ret_Type)) /= Any_Type) | |
7205254b | 5304 | then |
0669bebe GB |
5305 | if Needs_No_Actuals (Nam) then |
5306 | ||
5307 | -- Indexed call to a parameterless function | |
5308 | ||
5309 | Index_Node := | |
5310 | Make_Indexed_Component (Loc, | |
5311 | Prefix => | |
5312 | Make_Function_Call (Loc, | |
5313 | Name => New_Subp), | |
5314 | Expressions => Parameter_Associations (N)); | |
5315 | else | |
5316 | -- An Ada 2005 prefixed call to a primitive operation | |
5317 | -- whose first parameter is the prefix. This prefix was | |
5318 | -- prepended to the parameter list, which is actually a | |
3b42c566 | 5319 | -- list of indexes. Remove the prefix in order to build |
0669bebe GB |
5320 | -- the proper indexed component. |
5321 | ||
5322 | Index_Node := | |
5323 | Make_Indexed_Component (Loc, | |
5324 | Prefix => | |
5325 | Make_Function_Call (Loc, | |
5326 | Name => New_Subp, | |
5327 | Parameter_Associations => | |
5328 | New_List | |
5329 | (Remove_Head (Parameter_Associations (N)))), | |
5330 | Expressions => Parameter_Associations (N)); | |
5331 | end if; | |
fbf5a39b | 5332 | |
74e7891f RD |
5333 | -- Preserve the parenthesis count of the node |
5334 | ||
5335 | Set_Paren_Count (Index_Node, Paren_Count (N)); | |
5336 | ||
fbf5a39b AC |
5337 | -- Since we are correcting a node classification error made |
5338 | -- by the parser, we call Replace rather than Rewrite. | |
5339 | ||
5340 | Replace (N, Index_Node); | |
74e7891f | 5341 | |
fbf5a39b AC |
5342 | Set_Etype (Prefix (N), Ret_Type); |
5343 | Set_Etype (N, Typ); | |
5344 | Resolve_Indexed_Component (N, Typ); | |
5345 | Check_Elab_Call (Prefix (N)); | |
5346 | end if; | |
996ae0b0 RK |
5347 | end if; |
5348 | ||
5349 | return; | |
5350 | end; | |
5351 | ||
5352 | else | |
5353 | Set_Etype (N, Etype (Nam)); | |
5354 | end if; | |
5355 | ||
5356 | -- In the case where the call is to an overloaded subprogram, Analyze | |
5357 | -- calls Normalize_Actuals once per overloaded subprogram. Therefore in | |
5358 | -- such a case Normalize_Actuals needs to be called once more to order | |
5359 | -- the actuals correctly. Otherwise the call will have the ordering | |
5360 | -- given by the last overloaded subprogram whether this is the correct | |
5361 | -- one being called or not. | |
5362 | ||
5363 | if Is_Overloaded (Subp) then | |
5364 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
5365 | pragma Assert (Norm_OK); | |
5366 | end if; | |
5367 | ||
5368 | -- In any case, call is fully resolved now. Reset Overload flag, to | |
5369 | -- prevent subsequent overload resolution if node is analyzed again | |
5370 | ||
5371 | Set_Is_Overloaded (Subp, False); | |
5372 | Set_Is_Overloaded (N, False); | |
5373 | ||
758c442c GD |
5374 | -- If we are calling the current subprogram from immediately within its |
5375 | -- body, then that is the case where we can sometimes detect cases of | |
5376 | -- infinite recursion statically. Do not try this in case restriction | |
b7d1f17f | 5377 | -- No_Recursion is in effect anyway, and do it only for source calls. |
996ae0b0 | 5378 | |
b7d1f17f HK |
5379 | if Comes_From_Source (N) then |
5380 | Scop := Current_Scope; | |
996ae0b0 | 5381 | |
26570b21 RD |
5382 | -- Issue warning for possible infinite recursion in the absence |
5383 | -- of the No_Recursion restriction. | |
5384 | ||
ee81cbe9 | 5385 | if Same_Or_Aliased_Subprograms (Nam, Scop) |
b7d1f17f HK |
5386 | and then not Restriction_Active (No_Recursion) |
5387 | and then Check_Infinite_Recursion (N) | |
5388 | then | |
5389 | -- Here we detected and flagged an infinite recursion, so we do | |
da20aa43 RD |
5390 | -- not need to test the case below for further warnings. Also we |
5391 | -- are all done if we now have a raise SE node. | |
996ae0b0 | 5392 | |
26570b21 RD |
5393 | if Nkind (N) = N_Raise_Storage_Error then |
5394 | return; | |
5395 | end if; | |
996ae0b0 | 5396 | |
26570b21 RD |
5397 | -- If call is to immediately containing subprogram, then check for |
5398 | -- the case of a possible run-time detectable infinite recursion. | |
996ae0b0 | 5399 | |
b7d1f17f HK |
5400 | else |
5401 | Scope_Loop : while Scop /= Standard_Standard loop | |
ee81cbe9 | 5402 | if Same_Or_Aliased_Subprograms (Nam, Scop) then |
b7d1f17f HK |
5403 | |
5404 | -- Although in general case, recursion is not statically | |
5405 | -- checkable, the case of calling an immediately containing | |
5406 | -- subprogram is easy to catch. | |
5407 | ||
5408 | Check_Restriction (No_Recursion, N); | |
5409 | ||
5410 | -- If the recursive call is to a parameterless subprogram, | |
5411 | -- then even if we can't statically detect infinite | |
5412 | -- recursion, this is pretty suspicious, and we output a | |
5413 | -- warning. Furthermore, we will try later to detect some | |
5414 | -- cases here at run time by expanding checking code (see | |
5415 | -- Detect_Infinite_Recursion in package Exp_Ch6). | |
5416 | ||
5417 | -- If the recursive call is within a handler, do not emit a | |
5418 | -- warning, because this is a common idiom: loop until input | |
5419 | -- is correct, catch illegal input in handler and restart. | |
5420 | ||
5421 | if No (First_Formal (Nam)) | |
5422 | and then Etype (Nam) = Standard_Void_Type | |
5423 | and then not Error_Posted (N) | |
5424 | and then Nkind (Parent (N)) /= N_Exception_Handler | |
aa180613 | 5425 | then |
b7d1f17f HK |
5426 | -- For the case of a procedure call. We give the message |
5427 | -- only if the call is the first statement in a sequence | |
5428 | -- of statements, or if all previous statements are | |
5429 | -- simple assignments. This is simply a heuristic to | |
5430 | -- decrease false positives, without losing too many good | |
5431 | -- warnings. The idea is that these previous statements | |
5432 | -- may affect global variables the procedure depends on. | |
5433 | ||
5434 | if Nkind (N) = N_Procedure_Call_Statement | |
5435 | and then Is_List_Member (N) | |
5436 | then | |
5437 | declare | |
5438 | P : Node_Id; | |
5439 | begin | |
5440 | P := Prev (N); | |
5441 | while Present (P) loop | |
5442 | if Nkind (P) /= N_Assignment_Statement then | |
5443 | exit Scope_Loop; | |
5444 | end if; | |
5445 | ||
5446 | Prev (P); | |
5447 | end loop; | |
5448 | end; | |
5449 | end if; | |
5450 | ||
5451 | -- Do not give warning if we are in a conditional context | |
5452 | ||
aa180613 | 5453 | declare |
b7d1f17f | 5454 | K : constant Node_Kind := Nkind (Parent (N)); |
aa180613 | 5455 | begin |
b7d1f17f | 5456 | if (K = N_Loop_Statement |
b5c739f9 | 5457 | and then Present (Iteration_Scheme (Parent (N)))) |
b7d1f17f HK |
5458 | or else K = N_If_Statement |
5459 | or else K = N_Elsif_Part | |
5460 | or else K = N_Case_Statement_Alternative | |
5461 | then | |
5462 | exit Scope_Loop; | |
5463 | end if; | |
aa180613 | 5464 | end; |
aa180613 | 5465 | |
b7d1f17f | 5466 | -- Here warning is to be issued |
aa180613 | 5467 | |
b7d1f17f HK |
5468 | Set_Has_Recursive_Call (Nam); |
5469 | Error_Msg_N | |
aa5147f0 | 5470 | ("?possible infinite recursion!", N); |
b7d1f17f | 5471 | Error_Msg_N |
aa5147f0 | 5472 | ("\?Storage_Error may be raised at run time!", N); |
b7d1f17f | 5473 | end if; |
aa180613 | 5474 | |
b7d1f17f | 5475 | exit Scope_Loop; |
996ae0b0 RK |
5476 | end if; |
5477 | ||
b7d1f17f HK |
5478 | Scop := Scope (Scop); |
5479 | end loop Scope_Loop; | |
5480 | end if; | |
996ae0b0 RK |
5481 | end if; |
5482 | ||
b5c739f9 RD |
5483 | -- Check obsolescent reference to Ada.Characters.Handling subprogram |
5484 | ||
5485 | Check_Obsolescent_2005_Entity (Nam, Subp); | |
5486 | ||
996ae0b0 RK |
5487 | -- If subprogram name is a predefined operator, it was given in |
5488 | -- functional notation. Replace call node with operator node, so | |
5489 | -- that actuals can be resolved appropriately. | |
5490 | ||
5491 | if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then | |
5492 | Make_Call_Into_Operator (N, Typ, Entity (Name (N))); | |
5493 | return; | |
5494 | ||
5495 | elsif Present (Alias (Nam)) | |
5496 | and then Is_Predefined_Op (Alias (Nam)) | |
5497 | then | |
5498 | Resolve_Actuals (N, Nam); | |
5499 | Make_Call_Into_Operator (N, Typ, Alias (Nam)); | |
5500 | return; | |
5501 | end if; | |
5502 | ||
fbf5a39b AC |
5503 | -- Create a transient scope if the resulting type requires it |
5504 | ||
4017021b AC |
5505 | -- There are several notable exceptions: |
5506 | ||
4d2907fd | 5507 | -- a) In init procs, the transient scope overhead is not needed, and is |
4017021b AC |
5508 | -- even incorrect when the call is a nested initialization call for a |
5509 | -- component whose expansion may generate adjust calls. However, if the | |
5510 | -- call is some other procedure call within an initialization procedure | |
5511 | -- (for example a call to Create_Task in the init_proc of the task | |
5512 | -- run-time record) a transient scope must be created around this call. | |
5513 | ||
4d2907fd | 5514 | -- b) Enumeration literal pseudo-calls need no transient scope |
4017021b | 5515 | |
4d2907fd | 5516 | -- c) Intrinsic subprograms (Unchecked_Conversion and source info |
4017021b | 5517 | -- functions) do not use the secondary stack even though the return |
4d2907fd | 5518 | -- type may be unconstrained. |
4017021b | 5519 | |
4d2907fd | 5520 | -- d) Calls to a build-in-place function, since such functions may |
4017021b AC |
5521 | -- allocate their result directly in a target object, and cases where |
5522 | -- the result does get allocated in the secondary stack are checked for | |
5523 | -- within the specialized Exp_Ch6 procedures for expanding those | |
5524 | -- build-in-place calls. | |
5525 | ||
5526 | -- e) If the subprogram is marked Inline_Always, then even if it returns | |
c8ef728f | 5527 | -- an unconstrained type the call does not require use of the secondary |
45fc7ddb HK |
5528 | -- stack. However, inlining will only take place if the body to inline |
5529 | -- is already present. It may not be available if e.g. the subprogram is | |
5530 | -- declared in a child instance. | |
c8ef728f | 5531 | |
4017021b AC |
5532 | -- If this is an initialization call for a type whose construction |
5533 | -- uses the secondary stack, and it is not a nested call to initialize | |
5534 | -- a component, we do need to create a transient scope for it. We | |
5535 | -- check for this by traversing the type in Check_Initialization_Call. | |
5536 | ||
c8ef728f | 5537 | if Is_Inlined (Nam) |
45fc7ddb HK |
5538 | and then Has_Pragma_Inline_Always (Nam) |
5539 | and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration | |
5540 | and then Present (Body_To_Inline (Unit_Declaration_Node (Nam))) | |
c8ef728f ES |
5541 | then |
5542 | null; | |
5543 | ||
4017021b AC |
5544 | elsif Ekind (Nam) = E_Enumeration_Literal |
5545 | or else Is_Build_In_Place_Function (Nam) | |
5546 | or else Is_Intrinsic_Subprogram (Nam) | |
5547 | then | |
5548 | null; | |
5549 | ||
c8ef728f | 5550 | elsif Expander_Active |
996ae0b0 RK |
5551 | and then Is_Type (Etype (Nam)) |
5552 | and then Requires_Transient_Scope (Etype (Nam)) | |
4017021b AC |
5553 | and then |
5554 | (not Within_Init_Proc | |
5555 | or else | |
5556 | (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function)) | |
996ae0b0 | 5557 | then |
0669bebe | 5558 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 5559 | |
a9f4e3d2 AC |
5560 | -- If the call appears within the bounds of a loop, it will |
5561 | -- be rewritten and reanalyzed, nothing left to do here. | |
5562 | ||
5563 | if Nkind (N) /= N_Function_Call then | |
5564 | return; | |
5565 | end if; | |
5566 | ||
fbf5a39b | 5567 | elsif Is_Init_Proc (Nam) |
996ae0b0 RK |
5568 | and then not Within_Init_Proc |
5569 | then | |
5570 | Check_Initialization_Call (N, Nam); | |
5571 | end if; | |
5572 | ||
5573 | -- A protected function cannot be called within the definition of the | |
5574 | -- enclosing protected type. | |
5575 | ||
5576 | if Is_Protected_Type (Scope (Nam)) | |
5577 | and then In_Open_Scopes (Scope (Nam)) | |
5578 | and then not Has_Completion (Scope (Nam)) | |
5579 | then | |
5580 | Error_Msg_NE | |
5581 | ("& cannot be called before end of protected definition", N, Nam); | |
5582 | end if; | |
5583 | ||
5584 | -- Propagate interpretation to actuals, and add default expressions | |
5585 | -- where needed. | |
5586 | ||
5587 | if Present (First_Formal (Nam)) then | |
5588 | Resolve_Actuals (N, Nam); | |
5589 | ||
d81b4bfe TQ |
5590 | -- Overloaded literals are rewritten as function calls, for purpose of |
5591 | -- resolution. After resolution, we can replace the call with the | |
5592 | -- literal itself. | |
996ae0b0 RK |
5593 | |
5594 | elsif Ekind (Nam) = E_Enumeration_Literal then | |
5595 | Copy_Node (Subp, N); | |
5596 | Resolve_Entity_Name (N, Typ); | |
5597 | ||
fbf5a39b | 5598 | -- Avoid validation, since it is a static function call |
996ae0b0 | 5599 | |
e65f50ec | 5600 | Generate_Reference (Nam, Subp); |
996ae0b0 RK |
5601 | return; |
5602 | end if; | |
5603 | ||
b7d1f17f HK |
5604 | -- If the subprogram is not global, then kill all saved values and |
5605 | -- checks. This is a bit conservative, since in many cases we could do | |
5606 | -- better, but it is not worth the effort. Similarly, we kill constant | |
5607 | -- values. However we do not need to do this for internal entities | |
5608 | -- (unless they are inherited user-defined subprograms), since they | |
5609 | -- are not in the business of molesting local values. | |
5610 | ||
5611 | -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also | |
5612 | -- kill all checks and values for calls to global subprograms. This | |
5613 | -- takes care of the case where an access to a local subprogram is | |
5614 | -- taken, and could be passed directly or indirectly and then called | |
5615 | -- from almost any context. | |
aa180613 RD |
5616 | |
5617 | -- Note: we do not do this step till after resolving the actuals. That | |
5618 | -- way we still take advantage of the current value information while | |
5619 | -- scanning the actuals. | |
5620 | ||
45fc7ddb HK |
5621 | -- We suppress killing values if we are processing the nodes associated |
5622 | -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged | |
5623 | -- type kills all the values as part of analyzing the code that | |
5624 | -- initializes the dispatch tables. | |
5625 | ||
5626 | if Inside_Freezing_Actions = 0 | |
5627 | and then (not Is_Library_Level_Entity (Nam) | |
24357840 RD |
5628 | or else Suppress_Value_Tracking_On_Call |
5629 | (Nearest_Dynamic_Scope (Current_Scope))) | |
aa180613 RD |
5630 | and then (Comes_From_Source (Nam) |
5631 | or else (Present (Alias (Nam)) | |
5632 | and then Comes_From_Source (Alias (Nam)))) | |
5633 | then | |
5634 | Kill_Current_Values; | |
5635 | end if; | |
5636 | ||
36fcf362 RD |
5637 | -- If we are warning about unread OUT parameters, this is the place to |
5638 | -- set Last_Assignment for OUT and IN OUT parameters. We have to do this | |
5639 | -- after the above call to Kill_Current_Values (since that call clears | |
5640 | -- the Last_Assignment field of all local variables). | |
67ce0d7e | 5641 | |
36fcf362 | 5642 | if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters) |
67ce0d7e RD |
5643 | and then Comes_From_Source (N) |
5644 | and then In_Extended_Main_Source_Unit (N) | |
5645 | then | |
5646 | declare | |
5647 | F : Entity_Id; | |
5648 | A : Node_Id; | |
5649 | ||
5650 | begin | |
5651 | F := First_Formal (Nam); | |
5652 | A := First_Actual (N); | |
5653 | while Present (F) and then Present (A) loop | |
964f13da | 5654 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) |
36fcf362 | 5655 | and then Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
5656 | and then Is_Entity_Name (A) |
5657 | and then Present (Entity (A)) | |
36fcf362 | 5658 | and then Comes_From_Source (N) |
67ce0d7e RD |
5659 | and then Safe_To_Capture_Value (N, Entity (A)) |
5660 | then | |
5661 | Set_Last_Assignment (Entity (A), A); | |
5662 | end if; | |
5663 | ||
5664 | Next_Formal (F); | |
5665 | Next_Actual (A); | |
5666 | end loop; | |
5667 | end; | |
5668 | end if; | |
5669 | ||
996ae0b0 RK |
5670 | -- If the subprogram is a primitive operation, check whether or not |
5671 | -- it is a correct dispatching call. | |
5672 | ||
5673 | if Is_Overloadable (Nam) | |
5674 | and then Is_Dispatching_Operation (Nam) | |
5675 | then | |
5676 | Check_Dispatching_Call (N); | |
5677 | ||
0669bebe GB |
5678 | elsif Ekind (Nam) /= E_Subprogram_Type |
5679 | and then Is_Abstract_Subprogram (Nam) | |
996ae0b0 RK |
5680 | and then not In_Instance |
5681 | then | |
5682 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam); | |
5683 | end if; | |
5684 | ||
e65f50ec ES |
5685 | -- If this is a dispatching call, generate the appropriate reference, |
5686 | -- for better source navigation in GPS. | |
5687 | ||
5688 | if Is_Overloadable (Nam) | |
5689 | and then Present (Controlling_Argument (N)) | |
5690 | then | |
5691 | Generate_Reference (Nam, Subp, 'R'); | |
c5d91669 | 5692 | |
5cc9353d | 5693 | -- Normal case, not a dispatching call: generate a call reference |
c5d91669 | 5694 | |
e65f50ec | 5695 | else |
9c870c90 | 5696 | Generate_Reference (Nam, Subp, 's'); |
e65f50ec ES |
5697 | end if; |
5698 | ||
996ae0b0 RK |
5699 | if Is_Intrinsic_Subprogram (Nam) then |
5700 | Check_Intrinsic_Call (N); | |
5701 | end if; | |
5702 | ||
5b2217f8 | 5703 | -- Check for violation of restriction No_Specific_Termination_Handlers |
dce86910 | 5704 | -- and warn on a potentially blocking call to Abort_Task. |
5b2217f8 RD |
5705 | |
5706 | if Is_RTE (Nam, RE_Set_Specific_Handler) | |
5707 | or else | |
5708 | Is_RTE (Nam, RE_Specific_Handler) | |
5709 | then | |
5710 | Check_Restriction (No_Specific_Termination_Handlers, N); | |
dce86910 AC |
5711 | |
5712 | elsif Is_RTE (Nam, RE_Abort_Task) then | |
5713 | Check_Potentially_Blocking_Operation (N); | |
5b2217f8 RD |
5714 | end if; |
5715 | ||
806f6d37 AC |
5716 | -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative |
5717 | -- timing event violates restriction No_Relative_Delay (AI-0211). We | |
5718 | -- need to check the second argument to determine whether it is an | |
5719 | -- absolute or relative timing event. | |
afbcdf5e | 5720 | |
806f6d37 AC |
5721 | if Is_RTE (Nam, RE_Set_Handler) |
5722 | and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span) | |
5723 | then | |
afbcdf5e AC |
5724 | Check_Restriction (No_Relative_Delay, N); |
5725 | end if; | |
5726 | ||
9cbfc269 AC |
5727 | -- Issue an error for a call to an eliminated subprogram. We skip this |
5728 | -- in a spec expression, e.g. a call in a default parameter value, since | |
5729 | -- we are not really doing a call at this time. That's important because | |
5730 | -- the spec expression may itself belong to an eliminated subprogram. | |
16212e89 | 5731 | |
9cbfc269 AC |
5732 | if not In_Spec_Expression then |
5733 | Check_For_Eliminated_Subprogram (Subp, Nam); | |
5734 | end if; | |
16212e89 | 5735 | |
67ce0d7e RD |
5736 | -- All done, evaluate call and deal with elaboration issues |
5737 | ||
c01a9391 | 5738 | Eval_Call (N); |
996ae0b0 | 5739 | Check_Elab_Call (N); |
76b84bf0 | 5740 | Warn_On_Overlapping_Actuals (Nam, N); |
996ae0b0 RK |
5741 | end Resolve_Call; |
5742 | ||
19d846a0 RD |
5743 | ----------------------------- |
5744 | -- Resolve_Case_Expression -- | |
5745 | ----------------------------- | |
5746 | ||
5747 | procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is | |
5748 | Alt : Node_Id; | |
5749 | ||
5750 | begin | |
5751 | Alt := First (Alternatives (N)); | |
5752 | while Present (Alt) loop | |
5753 | Resolve (Expression (Alt), Typ); | |
5754 | Next (Alt); | |
5755 | end loop; | |
5756 | ||
5757 | Set_Etype (N, Typ); | |
5758 | Eval_Case_Expression (N); | |
5759 | end Resolve_Case_Expression; | |
5760 | ||
996ae0b0 RK |
5761 | ------------------------------- |
5762 | -- Resolve_Character_Literal -- | |
5763 | ------------------------------- | |
5764 | ||
5765 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is | |
5766 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
5767 | C : Entity_Id; | |
5768 | ||
5769 | begin | |
5770 | -- Verify that the character does belong to the type of the context | |
5771 | ||
5772 | Set_Etype (N, B_Typ); | |
5773 | Eval_Character_Literal (N); | |
5774 | ||
82c80734 RD |
5775 | -- Wide_Wide_Character literals must always be defined, since the set |
5776 | -- of wide wide character literals is complete, i.e. if a character | |
5777 | -- literal is accepted by the parser, then it is OK for wide wide | |
5778 | -- character (out of range character literals are rejected). | |
996ae0b0 | 5779 | |
82c80734 | 5780 | if Root_Type (B_Typ) = Standard_Wide_Wide_Character then |
996ae0b0 RK |
5781 | return; |
5782 | ||
5783 | -- Always accept character literal for type Any_Character, which | |
5784 | -- occurs in error situations and in comparisons of literals, both | |
5785 | -- of which should accept all literals. | |
5786 | ||
5787 | elsif B_Typ = Any_Character then | |
5788 | return; | |
5789 | ||
5cc9353d RD |
5790 | -- For Standard.Character or a type derived from it, check that the |
5791 | -- literal is in range. | |
996ae0b0 RK |
5792 | |
5793 | elsif Root_Type (B_Typ) = Standard_Character then | |
82c80734 RD |
5794 | if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then |
5795 | return; | |
5796 | end if; | |
5797 | ||
5cc9353d RD |
5798 | -- For Standard.Wide_Character or a type derived from it, check that the |
5799 | -- literal is in range. | |
82c80734 RD |
5800 | |
5801 | elsif Root_Type (B_Typ) = Standard_Wide_Character then | |
5802 | if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then | |
996ae0b0 RK |
5803 | return; |
5804 | end if; | |
5805 | ||
82c80734 RD |
5806 | -- For Standard.Wide_Wide_Character or a type derived from it, we |
5807 | -- know the literal is in range, since the parser checked! | |
5808 | ||
5809 | elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then | |
5810 | return; | |
5811 | ||
d81b4bfe TQ |
5812 | -- If the entity is already set, this has already been resolved in a |
5813 | -- generic context, or comes from expansion. Nothing else to do. | |
996ae0b0 RK |
5814 | |
5815 | elsif Present (Entity (N)) then | |
5816 | return; | |
5817 | ||
d81b4bfe TQ |
5818 | -- Otherwise we have a user defined character type, and we can use the |
5819 | -- standard visibility mechanisms to locate the referenced entity. | |
996ae0b0 RK |
5820 | |
5821 | else | |
5822 | C := Current_Entity (N); | |
996ae0b0 RK |
5823 | while Present (C) loop |
5824 | if Etype (C) = B_Typ then | |
5825 | Set_Entity_With_Style_Check (N, C); | |
5826 | Generate_Reference (C, N); | |
5827 | return; | |
5828 | end if; | |
5829 | ||
5830 | C := Homonym (C); | |
5831 | end loop; | |
5832 | end if; | |
5833 | ||
5834 | -- If we fall through, then the literal does not match any of the | |
5cc9353d RD |
5835 | -- entries of the enumeration type. This isn't just a constraint error |
5836 | -- situation, it is an illegality (see RM 4.2). | |
996ae0b0 RK |
5837 | |
5838 | Error_Msg_NE | |
5839 | ("character not defined for }", N, First_Subtype (B_Typ)); | |
996ae0b0 RK |
5840 | end Resolve_Character_Literal; |
5841 | ||
5842 | --------------------------- | |
5843 | -- Resolve_Comparison_Op -- | |
5844 | --------------------------- | |
5845 | ||
5846 | -- Context requires a boolean type, and plays no role in resolution. | |
5cc9353d RD |
5847 | -- Processing identical to that for equality operators. The result type is |
5848 | -- the base type, which matters when pathological subtypes of booleans with | |
5849 | -- limited ranges are used. | |
996ae0b0 RK |
5850 | |
5851 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is | |
5852 | L : constant Node_Id := Left_Opnd (N); | |
5853 | R : constant Node_Id := Right_Opnd (N); | |
5854 | T : Entity_Id; | |
5855 | ||
5856 | begin | |
d81b4bfe TQ |
5857 | -- If this is an intrinsic operation which is not predefined, use the |
5858 | -- types of its declared arguments to resolve the possibly overloaded | |
5859 | -- operands. Otherwise the operands are unambiguous and specify the | |
5860 | -- expected type. | |
996ae0b0 RK |
5861 | |
5862 | if Scope (Entity (N)) /= Standard_Standard then | |
5863 | T := Etype (First_Entity (Entity (N))); | |
1420b484 | 5864 | |
996ae0b0 RK |
5865 | else |
5866 | T := Find_Unique_Type (L, R); | |
5867 | ||
5868 | if T = Any_Fixed then | |
5869 | T := Unique_Fixed_Point_Type (L); | |
5870 | end if; | |
5871 | end if; | |
5872 | ||
fbf5a39b | 5873 | Set_Etype (N, Base_Type (Typ)); |
996ae0b0 RK |
5874 | Generate_Reference (T, N, ' '); |
5875 | ||
bd29d519 | 5876 | -- Skip remaining processing if already set to Any_Type |
996ae0b0 | 5877 | |
bd29d519 AC |
5878 | if T = Any_Type then |
5879 | return; | |
5880 | end if; | |
5881 | ||
5882 | -- Deal with other error cases | |
996ae0b0 | 5883 | |
bd29d519 AC |
5884 | if T = Any_String or else |
5885 | T = Any_Composite or else | |
5886 | T = Any_Character | |
5887 | then | |
5888 | if T = Any_Character then | |
5889 | Ambiguous_Character (L); | |
996ae0b0 | 5890 | else |
bd29d519 | 5891 | Error_Msg_N ("ambiguous operands for comparison", N); |
996ae0b0 | 5892 | end if; |
bd29d519 AC |
5893 | |
5894 | Set_Etype (N, Any_Type); | |
5895 | return; | |
996ae0b0 | 5896 | end if; |
bd29d519 AC |
5897 | |
5898 | -- Resolve the operands if types OK | |
5899 | ||
5900 | Resolve (L, T); | |
5901 | Resolve (R, T); | |
5902 | Check_Unset_Reference (L); | |
5903 | Check_Unset_Reference (R); | |
5904 | Generate_Operator_Reference (N, T); | |
5905 | Check_Low_Bound_Tested (N); | |
5906 | ||
b0186f71 AC |
5907 | -- In SPARK or ALFA, ordering operators <, <=, >, >= are not defined |
5908 | -- for Boolean types or array types except String. | |
5909 | ||
fe5d3068 YM |
5910 | if Is_Boolean_Type (T) then |
5911 | Check_Formal_Restriction | |
5912 | ("comparison is not defined on Boolean type", N); | |
5913 | elsif Is_Array_Type (T) | |
5914 | and then Base_Type (T) /= Standard_String | |
b0186f71 | 5915 | then |
fe5d3068 YM |
5916 | Check_Formal_Restriction |
5917 | ("comparison is not defined on array types other than String", N); | |
5918 | else | |
5919 | null; | |
b0186f71 AC |
5920 | end if; |
5921 | ||
bd29d519 AC |
5922 | -- Check comparison on unordered enumeration |
5923 | ||
5924 | if Comes_From_Source (N) | |
5925 | and then Bad_Unordered_Enumeration_Reference (N, Etype (L)) | |
5926 | then | |
5927 | Error_Msg_N ("comparison on unordered enumeration type?", N); | |
5928 | end if; | |
5929 | ||
5cc9353d RD |
5930 | -- Evaluate the relation (note we do this after the above check since |
5931 | -- this Eval call may change N to True/False. | |
bd29d519 AC |
5932 | |
5933 | Eval_Relational_Op (N); | |
996ae0b0 RK |
5934 | end Resolve_Comparison_Op; |
5935 | ||
5936 | ------------------------------------ | |
5937 | -- Resolve_Conditional_Expression -- | |
5938 | ------------------------------------ | |
5939 | ||
5940 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is | |
5941 | Condition : constant Node_Id := First (Expressions (N)); | |
5942 | Then_Expr : constant Node_Id := Next (Condition); | |
19fb051c | 5943 | Else_Expr : Node_Id := Next (Then_Expr); |
b46be8a2 | 5944 | |
996ae0b0 | 5945 | begin |
b46be8a2 | 5946 | Resolve (Condition, Any_Boolean); |
996ae0b0 | 5947 | Resolve (Then_Expr, Typ); |
b46be8a2 RD |
5948 | |
5949 | -- If ELSE expression present, just resolve using the determined type | |
5950 | ||
5951 | if Present (Else_Expr) then | |
5952 | Resolve (Else_Expr, Typ); | |
5953 | ||
5954 | -- If no ELSE expression is present, root type must be Standard.Boolean | |
5955 | -- and we provide a Standard.True result converted to the appropriate | |
5956 | -- Boolean type (in case it is a derived boolean type). | |
5957 | ||
5958 | elsif Root_Type (Typ) = Standard_Boolean then | |
5959 | Else_Expr := | |
5960 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N))); | |
5961 | Analyze_And_Resolve (Else_Expr, Typ); | |
5962 | Append_To (Expressions (N), Else_Expr); | |
5963 | ||
5964 | else | |
5965 | Error_Msg_N ("can only omit ELSE expression in Boolean case", N); | |
5966 | Append_To (Expressions (N), Error); | |
5967 | end if; | |
5968 | ||
996ae0b0 RK |
5969 | Set_Etype (N, Typ); |
5970 | Eval_Conditional_Expression (N); | |
5971 | end Resolve_Conditional_Expression; | |
5972 | ||
5973 | ----------------------------------------- | |
5974 | -- Resolve_Discrete_Subtype_Indication -- | |
5975 | ----------------------------------------- | |
5976 | ||
5977 | procedure Resolve_Discrete_Subtype_Indication | |
5978 | (N : Node_Id; | |
5979 | Typ : Entity_Id) | |
5980 | is | |
5981 | R : Node_Id; | |
5982 | S : Entity_Id; | |
5983 | ||
5984 | begin | |
5985 | Analyze (Subtype_Mark (N)); | |
5986 | S := Entity (Subtype_Mark (N)); | |
5987 | ||
5988 | if Nkind (Constraint (N)) /= N_Range_Constraint then | |
5989 | Error_Msg_N ("expect range constraint for discrete type", N); | |
5990 | Set_Etype (N, Any_Type); | |
5991 | ||
5992 | else | |
5993 | R := Range_Expression (Constraint (N)); | |
5c736541 RD |
5994 | |
5995 | if R = Error then | |
5996 | return; | |
5997 | end if; | |
5998 | ||
996ae0b0 RK |
5999 | Analyze (R); |
6000 | ||
6001 | if Base_Type (S) /= Base_Type (Typ) then | |
6002 | Error_Msg_NE | |
6003 | ("expect subtype of }", N, First_Subtype (Typ)); | |
6004 | ||
6005 | -- Rewrite the constraint as a range of Typ | |
6006 | -- to allow compilation to proceed further. | |
6007 | ||
6008 | Set_Etype (N, Typ); | |
6009 | Rewrite (Low_Bound (R), | |
6010 | Make_Attribute_Reference (Sloc (Low_Bound (R)), | |
5cc9353d | 6011 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), |
996ae0b0 RK |
6012 | Attribute_Name => Name_First)); |
6013 | Rewrite (High_Bound (R), | |
6014 | Make_Attribute_Reference (Sloc (High_Bound (R)), | |
5cc9353d | 6015 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), |
996ae0b0 RK |
6016 | Attribute_Name => Name_First)); |
6017 | ||
6018 | else | |
6019 | Resolve (R, Typ); | |
6020 | Set_Etype (N, Etype (R)); | |
6021 | ||
6022 | -- Additionally, we must check that the bounds are compatible | |
6023 | -- with the given subtype, which might be different from the | |
6024 | -- type of the context. | |
6025 | ||
6026 | Apply_Range_Check (R, S); | |
6027 | ||
6028 | -- ??? If the above check statically detects a Constraint_Error | |
6029 | -- it replaces the offending bound(s) of the range R with a | |
6030 | -- Constraint_Error node. When the itype which uses these bounds | |
6031 | -- is frozen the resulting call to Duplicate_Subexpr generates | |
6032 | -- a new temporary for the bounds. | |
6033 | ||
6034 | -- Unfortunately there are other itypes that are also made depend | |
6035 | -- on these bounds, so when Duplicate_Subexpr is called they get | |
6036 | -- a forward reference to the newly created temporaries and Gigi | |
6037 | -- aborts on such forward references. This is probably sign of a | |
6038 | -- more fundamental problem somewhere else in either the order of | |
6039 | -- itype freezing or the way certain itypes are constructed. | |
6040 | ||
6041 | -- To get around this problem we call Remove_Side_Effects right | |
6042 | -- away if either bounds of R are a Constraint_Error. | |
6043 | ||
6044 | declare | |
fbf5a39b AC |
6045 | L : constant Node_Id := Low_Bound (R); |
6046 | H : constant Node_Id := High_Bound (R); | |
996ae0b0 RK |
6047 | |
6048 | begin | |
6049 | if Nkind (L) = N_Raise_Constraint_Error then | |
6050 | Remove_Side_Effects (L); | |
6051 | end if; | |
6052 | ||
6053 | if Nkind (H) = N_Raise_Constraint_Error then | |
6054 | Remove_Side_Effects (H); | |
6055 | end if; | |
6056 | end; | |
6057 | ||
6058 | Check_Unset_Reference (Low_Bound (R)); | |
6059 | Check_Unset_Reference (High_Bound (R)); | |
6060 | end if; | |
6061 | end if; | |
6062 | end Resolve_Discrete_Subtype_Indication; | |
6063 | ||
6064 | ------------------------- | |
6065 | -- Resolve_Entity_Name -- | |
6066 | ------------------------- | |
6067 | ||
6068 | -- Used to resolve identifiers and expanded names | |
6069 | ||
6070 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is | |
6071 | E : constant Entity_Id := Entity (N); | |
6072 | ||
6073 | begin | |
07fc65c4 GB |
6074 | -- If garbage from errors, set to Any_Type and return |
6075 | ||
6076 | if No (E) and then Total_Errors_Detected /= 0 then | |
6077 | Set_Etype (N, Any_Type); | |
6078 | return; | |
6079 | end if; | |
6080 | ||
996ae0b0 RK |
6081 | -- Replace named numbers by corresponding literals. Note that this is |
6082 | -- the one case where Resolve_Entity_Name must reset the Etype, since | |
6083 | -- it is currently marked as universal. | |
6084 | ||
6085 | if Ekind (E) = E_Named_Integer then | |
6086 | Set_Etype (N, Typ); | |
6087 | Eval_Named_Integer (N); | |
6088 | ||
6089 | elsif Ekind (E) = E_Named_Real then | |
6090 | Set_Etype (N, Typ); | |
6091 | Eval_Named_Real (N); | |
6092 | ||
6989bc1f AC |
6093 | -- For enumeration literals, we need to make sure that a proper style |
6094 | -- check is done, since such literals are overloaded, and thus we did | |
6095 | -- not do a style check during the first phase of analysis. | |
6096 | ||
6097 | elsif Ekind (E) = E_Enumeration_Literal then | |
6098 | Set_Entity_With_Style_Check (N, E); | |
6099 | Eval_Entity_Name (N); | |
6100 | ||
e606088a | 6101 | -- Case of subtype name appearing as an operand in expression |
996ae0b0 RK |
6102 | |
6103 | elsif Is_Type (E) then | |
e606088a AC |
6104 | |
6105 | -- Allow use of subtype if it is a concurrent type where we are | |
6106 | -- currently inside the body. This will eventually be expanded into a | |
6107 | -- call to Self (for tasks) or _object (for protected objects). Any | |
6108 | -- other use of a subtype is invalid. | |
6109 | ||
996ae0b0 RK |
6110 | if Is_Concurrent_Type (E) |
6111 | and then In_Open_Scopes (E) | |
6112 | then | |
6113 | null; | |
e606088a | 6114 | |
308e6f3a | 6115 | -- Any other use is an error |
e606088a | 6116 | |
996ae0b0 RK |
6117 | else |
6118 | Error_Msg_N | |
758c442c | 6119 | ("invalid use of subtype mark in expression or call", N); |
996ae0b0 RK |
6120 | end if; |
6121 | ||
6122 | -- Check discriminant use if entity is discriminant in current scope, | |
6123 | -- i.e. discriminant of record or concurrent type currently being | |
6124 | -- analyzed. Uses in corresponding body are unrestricted. | |
6125 | ||
6126 | elsif Ekind (E) = E_Discriminant | |
6127 | and then Scope (E) = Current_Scope | |
6128 | and then not Has_Completion (Current_Scope) | |
6129 | then | |
6130 | Check_Discriminant_Use (N); | |
6131 | ||
6132 | -- A parameterless generic function cannot appear in a context that | |
6133 | -- requires resolution. | |
6134 | ||
6135 | elsif Ekind (E) = E_Generic_Function then | |
6136 | Error_Msg_N ("illegal use of generic function", N); | |
6137 | ||
6138 | elsif Ekind (E) = E_Out_Parameter | |
0ab80019 | 6139 | and then Ada_Version = Ada_83 |
996ae0b0 | 6140 | and then (Nkind (Parent (N)) in N_Op |
19fb051c AC |
6141 | or else (Nkind (Parent (N)) = N_Assignment_Statement |
6142 | and then N = Expression (Parent (N))) | |
6143 | or else Nkind (Parent (N)) = N_Explicit_Dereference) | |
996ae0b0 RK |
6144 | then |
6145 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", N); | |
6146 | ||
6147 | -- In all other cases, just do the possible static evaluation | |
6148 | ||
6149 | else | |
d81b4bfe TQ |
6150 | -- A deferred constant that appears in an expression must have a |
6151 | -- completion, unless it has been removed by in-place expansion of | |
6152 | -- an aggregate. | |
996ae0b0 RK |
6153 | |
6154 | if Ekind (E) = E_Constant | |
6155 | and then Comes_From_Source (E) | |
6156 | and then No (Constant_Value (E)) | |
6157 | and then Is_Frozen (Etype (E)) | |
45fc7ddb | 6158 | and then not In_Spec_Expression |
996ae0b0 RK |
6159 | and then not Is_Imported (E) |
6160 | then | |
996ae0b0 RK |
6161 | if No_Initialization (Parent (E)) |
6162 | or else (Present (Full_View (E)) | |
6163 | and then No_Initialization (Parent (Full_View (E)))) | |
6164 | then | |
6165 | null; | |
6166 | else | |
6167 | Error_Msg_N ( | |
6168 | "deferred constant is frozen before completion", N); | |
6169 | end if; | |
6170 | end if; | |
6171 | ||
6172 | Eval_Entity_Name (N); | |
6173 | end if; | |
6174 | end Resolve_Entity_Name; | |
6175 | ||
6176 | ------------------- | |
6177 | -- Resolve_Entry -- | |
6178 | ------------------- | |
6179 | ||
6180 | procedure Resolve_Entry (Entry_Name : Node_Id) is | |
6181 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
6182 | Nam : Entity_Id; | |
6183 | New_N : Node_Id; | |
6184 | S : Entity_Id; | |
6185 | Tsk : Entity_Id; | |
6186 | E_Name : Node_Id; | |
6187 | Index : Node_Id; | |
6188 | ||
6189 | function Actual_Index_Type (E : Entity_Id) return Entity_Id; | |
6190 | -- If the bounds of the entry family being called depend on task | |
6191 | -- discriminants, build a new index subtype where a discriminant is | |
6192 | -- replaced with the value of the discriminant of the target task. | |
6193 | -- The target task is the prefix of the entry name in the call. | |
6194 | ||
6195 | ----------------------- | |
6196 | -- Actual_Index_Type -- | |
6197 | ----------------------- | |
6198 | ||
6199 | function Actual_Index_Type (E : Entity_Id) return Entity_Id is | |
fbf5a39b AC |
6200 | Typ : constant Entity_Id := Entry_Index_Type (E); |
6201 | Tsk : constant Entity_Id := Scope (E); | |
6202 | Lo : constant Node_Id := Type_Low_Bound (Typ); | |
6203 | Hi : constant Node_Id := Type_High_Bound (Typ); | |
996ae0b0 RK |
6204 | New_T : Entity_Id; |
6205 | ||
6206 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id; | |
6207 | -- If the bound is given by a discriminant, replace with a reference | |
d81b4bfe TQ |
6208 | -- to the discriminant of the same name in the target task. If the |
6209 | -- entry name is the target of a requeue statement and the entry is | |
6210 | -- in the current protected object, the bound to be used is the | |
008f6fd3 | 6211 | -- discriminal of the object (see Apply_Range_Checks for details of |
d81b4bfe | 6212 | -- the transformation). |
996ae0b0 RK |
6213 | |
6214 | ----------------------------- | |
6215 | -- Actual_Discriminant_Ref -- | |
6216 | ----------------------------- | |
6217 | ||
6218 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is | |
fbf5a39b | 6219 | Typ : constant Entity_Id := Etype (Bound); |
996ae0b0 RK |
6220 | Ref : Node_Id; |
6221 | ||
6222 | begin | |
6223 | Remove_Side_Effects (Bound); | |
6224 | ||
6225 | if not Is_Entity_Name (Bound) | |
6226 | or else Ekind (Entity (Bound)) /= E_Discriminant | |
6227 | then | |
6228 | return Bound; | |
6229 | ||
6230 | elsif Is_Protected_Type (Tsk) | |
6231 | and then In_Open_Scopes (Tsk) | |
6232 | and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement | |
6233 | then | |
6ca9ec9c AC |
6234 | -- Note: here Bound denotes a discriminant of the corresponding |
6235 | -- record type tskV, whose discriminal is a formal of the | |
6236 | -- init-proc tskVIP. What we want is the body discriminal, | |
6237 | -- which is associated to the discriminant of the original | |
6238 | -- concurrent type tsk. | |
6239 | ||
5a153b27 AC |
6240 | return New_Occurrence_Of |
6241 | (Find_Body_Discriminal (Entity (Bound)), Loc); | |
996ae0b0 RK |
6242 | |
6243 | else | |
6244 | Ref := | |
6245 | Make_Selected_Component (Loc, | |
6246 | Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))), | |
6247 | Selector_Name => New_Occurrence_Of (Entity (Bound), Loc)); | |
6248 | Analyze (Ref); | |
6249 | Resolve (Ref, Typ); | |
6250 | return Ref; | |
6251 | end if; | |
6252 | end Actual_Discriminant_Ref; | |
6253 | ||
6254 | -- Start of processing for Actual_Index_Type | |
6255 | ||
6256 | begin | |
6257 | if not Has_Discriminants (Tsk) | |
19fb051c | 6258 | or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi)) |
996ae0b0 RK |
6259 | then |
6260 | return Entry_Index_Type (E); | |
6261 | ||
6262 | else | |
6263 | New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name)); | |
6264 | Set_Etype (New_T, Base_Type (Typ)); | |
6265 | Set_Size_Info (New_T, Typ); | |
6266 | Set_RM_Size (New_T, RM_Size (Typ)); | |
6267 | Set_Scalar_Range (New_T, | |
6268 | Make_Range (Sloc (Entry_Name), | |
6269 | Low_Bound => Actual_Discriminant_Ref (Lo), | |
6270 | High_Bound => Actual_Discriminant_Ref (Hi))); | |
6271 | ||
6272 | return New_T; | |
6273 | end if; | |
6274 | end Actual_Index_Type; | |
6275 | ||
6276 | -- Start of processing of Resolve_Entry | |
6277 | ||
6278 | begin | |
5cc9353d RD |
6279 | -- Find name of entry being called, and resolve prefix of name with its |
6280 | -- own type. The prefix can be overloaded, and the name and signature of | |
6281 | -- the entry must be taken into account. | |
996ae0b0 RK |
6282 | |
6283 | if Nkind (Entry_Name) = N_Indexed_Component then | |
6284 | ||
6285 | -- Case of dealing with entry family within the current tasks | |
6286 | ||
6287 | E_Name := Prefix (Entry_Name); | |
6288 | ||
6289 | else | |
6290 | E_Name := Entry_Name; | |
6291 | end if; | |
6292 | ||
6293 | if Is_Entity_Name (E_Name) then | |
996ae0b0 | 6294 | |
d81b4bfe TQ |
6295 | -- Entry call to an entry (or entry family) in the current task. This |
6296 | -- is legal even though the task will deadlock. Rewrite as call to | |
6297 | -- current task. | |
996ae0b0 | 6298 | |
d81b4bfe TQ |
6299 | -- This can also be a call to an entry in an enclosing task. If this |
6300 | -- is a single task, we have to retrieve its name, because the scope | |
6301 | -- of the entry is the task type, not the object. If the enclosing | |
6302 | -- task is a task type, the identity of the task is given by its own | |
6303 | -- self variable. | |
6304 | ||
6305 | -- Finally this can be a requeue on an entry of the same task or | |
6306 | -- protected object. | |
996ae0b0 RK |
6307 | |
6308 | S := Scope (Entity (E_Name)); | |
6309 | ||
6310 | for J in reverse 0 .. Scope_Stack.Last loop | |
996ae0b0 RK |
6311 | if Is_Task_Type (Scope_Stack.Table (J).Entity) |
6312 | and then not Comes_From_Source (S) | |
6313 | then | |
6314 | -- S is an enclosing task or protected object. The concurrent | |
6315 | -- declaration has been converted into a type declaration, and | |
6316 | -- the object itself has an object declaration that follows | |
6317 | -- the type in the same declarative part. | |
6318 | ||
6319 | Tsk := Next_Entity (S); | |
996ae0b0 RK |
6320 | while Etype (Tsk) /= S loop |
6321 | Next_Entity (Tsk); | |
6322 | end loop; | |
6323 | ||
6324 | S := Tsk; | |
6325 | exit; | |
6326 | ||
6327 | elsif S = Scope_Stack.Table (J).Entity then | |
6328 | ||
6329 | -- Call to current task. Will be transformed into call to Self | |
6330 | ||
6331 | exit; | |
6332 | ||
6333 | end if; | |
6334 | end loop; | |
6335 | ||
6336 | New_N := | |
6337 | Make_Selected_Component (Loc, | |
6338 | Prefix => New_Occurrence_Of (S, Loc), | |
6339 | Selector_Name => | |
6340 | New_Occurrence_Of (Entity (E_Name), Loc)); | |
6341 | Rewrite (E_Name, New_N); | |
6342 | Analyze (E_Name); | |
6343 | ||
6344 | elsif Nkind (Entry_Name) = N_Selected_Component | |
6345 | and then Is_Overloaded (Prefix (Entry_Name)) | |
6346 | then | |
d81b4bfe | 6347 | -- Use the entry name (which must be unique at this point) to find |
5cc9353d | 6348 | -- the prefix that returns the corresponding task/protected type. |
996ae0b0 RK |
6349 | |
6350 | declare | |
fbf5a39b AC |
6351 | Pref : constant Node_Id := Prefix (Entry_Name); |
6352 | Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name)); | |
996ae0b0 RK |
6353 | I : Interp_Index; |
6354 | It : Interp; | |
996ae0b0 RK |
6355 | |
6356 | begin | |
6357 | Get_First_Interp (Pref, I, It); | |
996ae0b0 | 6358 | while Present (It.Typ) loop |
996ae0b0 RK |
6359 | if Scope (Ent) = It.Typ then |
6360 | Set_Etype (Pref, It.Typ); | |
6361 | exit; | |
6362 | end if; | |
6363 | ||
6364 | Get_Next_Interp (I, It); | |
6365 | end loop; | |
6366 | end; | |
6367 | end if; | |
6368 | ||
6369 | if Nkind (Entry_Name) = N_Selected_Component then | |
fbf5a39b | 6370 | Resolve (Prefix (Entry_Name)); |
996ae0b0 RK |
6371 | |
6372 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6373 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
fbf5a39b | 6374 | Resolve (Prefix (Prefix (Entry_Name))); |
996ae0b0 RK |
6375 | Index := First (Expressions (Entry_Name)); |
6376 | Resolve (Index, Entry_Index_Type (Nam)); | |
6377 | ||
d81b4bfe TQ |
6378 | -- Up to this point the expression could have been the actual in a |
6379 | -- simple entry call, and be given by a named association. | |
996ae0b0 RK |
6380 | |
6381 | if Nkind (Index) = N_Parameter_Association then | |
6382 | Error_Msg_N ("expect expression for entry index", Index); | |
6383 | else | |
6384 | Apply_Range_Check (Index, Actual_Index_Type (Nam)); | |
6385 | end if; | |
6386 | end if; | |
996ae0b0 RK |
6387 | end Resolve_Entry; |
6388 | ||
6389 | ------------------------ | |
6390 | -- Resolve_Entry_Call -- | |
6391 | ------------------------ | |
6392 | ||
6393 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is | |
6394 | Entry_Name : constant Node_Id := Name (N); | |
6395 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
6396 | Actuals : List_Id; | |
6397 | First_Named : Node_Id; | |
6398 | Nam : Entity_Id; | |
6399 | Norm_OK : Boolean; | |
6400 | Obj : Node_Id; | |
6401 | Was_Over : Boolean; | |
6402 | ||
6403 | begin | |
d81b4bfe TQ |
6404 | -- We kill all checks here, because it does not seem worth the effort to |
6405 | -- do anything better, an entry call is a big operation. | |
fbf5a39b AC |
6406 | |
6407 | Kill_All_Checks; | |
6408 | ||
996ae0b0 RK |
6409 | -- Processing of the name is similar for entry calls and protected |
6410 | -- operation calls. Once the entity is determined, we can complete | |
6411 | -- the resolution of the actuals. | |
6412 | ||
6413 | -- The selector may be overloaded, in the case of a protected object | |
6414 | -- with overloaded functions. The type of the context is used for | |
6415 | -- resolution. | |
6416 | ||
6417 | if Nkind (Entry_Name) = N_Selected_Component | |
6418 | and then Is_Overloaded (Selector_Name (Entry_Name)) | |
6419 | and then Typ /= Standard_Void_Type | |
6420 | then | |
6421 | declare | |
6422 | I : Interp_Index; | |
6423 | It : Interp; | |
6424 | ||
6425 | begin | |
6426 | Get_First_Interp (Selector_Name (Entry_Name), I, It); | |
996ae0b0 | 6427 | while Present (It.Typ) loop |
996ae0b0 RK |
6428 | if Covers (Typ, It.Typ) then |
6429 | Set_Entity (Selector_Name (Entry_Name), It.Nam); | |
6430 | Set_Etype (Entry_Name, It.Typ); | |
6431 | ||
6432 | Generate_Reference (It.Typ, N, ' '); | |
6433 | end if; | |
6434 | ||
6435 | Get_Next_Interp (I, It); | |
6436 | end loop; | |
6437 | end; | |
6438 | end if; | |
6439 | ||
6440 | Resolve_Entry (Entry_Name); | |
6441 | ||
6442 | if Nkind (Entry_Name) = N_Selected_Component then | |
6443 | ||
a77842bd | 6444 | -- Simple entry call |
996ae0b0 RK |
6445 | |
6446 | Nam := Entity (Selector_Name (Entry_Name)); | |
6447 | Obj := Prefix (Entry_Name); | |
6448 | Was_Over := Is_Overloaded (Selector_Name (Entry_Name)); | |
6449 | ||
6450 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6451 | ||
a77842bd | 6452 | -- Call to member of entry family |
996ae0b0 RK |
6453 | |
6454 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
6455 | Obj := Prefix (Prefix (Entry_Name)); | |
6456 | Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name))); | |
6457 | end if; | |
6458 | ||
5cc9353d RD |
6459 | -- We cannot in general check the maximum depth of protected entry calls |
6460 | -- at compile time. But we can tell that any protected entry call at all | |
6461 | -- violates a specified nesting depth of zero. | |
fbf5a39b AC |
6462 | |
6463 | if Is_Protected_Type (Scope (Nam)) then | |
9f4fd324 | 6464 | Check_Restriction (Max_Entry_Queue_Length, N); |
fbf5a39b AC |
6465 | end if; |
6466 | ||
996ae0b0 | 6467 | -- Use context type to disambiguate a protected function that can be |
5cc9353d RD |
6468 | -- called without actuals and that returns an array type, and where the |
6469 | -- argument list may be an indexing of the returned value. | |
996ae0b0 RK |
6470 | |
6471 | if Ekind (Nam) = E_Function | |
6472 | and then Needs_No_Actuals (Nam) | |
6473 | and then Present (Parameter_Associations (N)) | |
6474 | and then | |
6475 | ((Is_Array_Type (Etype (Nam)) | |
6476 | and then Covers (Typ, Component_Type (Etype (Nam)))) | |
6477 | ||
6478 | or else (Is_Access_Type (Etype (Nam)) | |
6479 | and then Is_Array_Type (Designated_Type (Etype (Nam))) | |
19fb051c AC |
6480 | and then |
6481 | Covers | |
6482 | (Typ, | |
6483 | Component_Type (Designated_Type (Etype (Nam)))))) | |
996ae0b0 RK |
6484 | then |
6485 | declare | |
6486 | Index_Node : Node_Id; | |
6487 | ||
6488 | begin | |
6489 | Index_Node := | |
6490 | Make_Indexed_Component (Loc, | |
6491 | Prefix => | |
19fb051c | 6492 | Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)), |
996ae0b0 RK |
6493 | Expressions => Parameter_Associations (N)); |
6494 | ||
5cc9353d RD |
6495 | -- Since we are correcting a node classification error made by the |
6496 | -- parser, we call Replace rather than Rewrite. | |
996ae0b0 RK |
6497 | |
6498 | Replace (N, Index_Node); | |
6499 | Set_Etype (Prefix (N), Etype (Nam)); | |
6500 | Set_Etype (N, Typ); | |
6501 | Resolve_Indexed_Component (N, Typ); | |
6502 | return; | |
6503 | end; | |
6504 | end if; | |
6505 | ||
b7f17b20 ES |
6506 | if Ekind_In (Nam, E_Entry, E_Entry_Family) |
6507 | and then Present (PPC_Wrapper (Nam)) | |
6508 | and then Current_Scope /= PPC_Wrapper (Nam) | |
6509 | then | |
468ee96a | 6510 | -- Rewrite as call to the precondition wrapper, adding the task |
5cc9353d RD |
6511 | -- object to the list of actuals. If the call is to a member of an |
6512 | -- entry family, include the index as well. | |
b7f17b20 ES |
6513 | |
6514 | declare | |
468ee96a | 6515 | New_Call : Node_Id; |
b7f17b20 | 6516 | New_Actuals : List_Id; |
19fb051c | 6517 | |
b7f17b20 ES |
6518 | begin |
6519 | New_Actuals := New_List (Obj); | |
3fd9f17c AC |
6520 | |
6521 | if Nkind (Entry_Name) = N_Indexed_Component then | |
6522 | Append_To (New_Actuals, | |
6523 | New_Copy_Tree (First (Expressions (Entry_Name)))); | |
6524 | end if; | |
6525 | ||
b7f17b20 | 6526 | Append_List (Parameter_Associations (N), New_Actuals); |
468ee96a AC |
6527 | New_Call := |
6528 | Make_Procedure_Call_Statement (Loc, | |
6529 | Name => | |
6530 | New_Occurrence_Of (PPC_Wrapper (Nam), Loc), | |
6531 | Parameter_Associations => New_Actuals); | |
b7f17b20 ES |
6532 | Rewrite (N, New_Call); |
6533 | Analyze_And_Resolve (N); | |
6534 | return; | |
6535 | end; | |
6536 | end if; | |
6537 | ||
996ae0b0 | 6538 | -- The operation name may have been overloaded. Order the actuals |
5cc9353d RD |
6539 | -- according to the formals of the resolved entity, and set the return |
6540 | -- type to that of the operation. | |
996ae0b0 RK |
6541 | |
6542 | if Was_Over then | |
6543 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
6544 | pragma Assert (Norm_OK); | |
fbf5a39b | 6545 | Set_Etype (N, Etype (Nam)); |
996ae0b0 RK |
6546 | end if; |
6547 | ||
6548 | Resolve_Actuals (N, Nam); | |
ae6ede77 AC |
6549 | |
6550 | -- Create a call reference to the entry | |
6551 | ||
6552 | Generate_Reference (Nam, Entry_Name, 's'); | |
996ae0b0 | 6553 | |
8a95f4e8 | 6554 | if Ekind_In (Nam, E_Entry, E_Entry_Family) then |
996ae0b0 RK |
6555 | Check_Potentially_Blocking_Operation (N); |
6556 | end if; | |
6557 | ||
6558 | -- Verify that a procedure call cannot masquerade as an entry | |
6559 | -- call where an entry call is expected. | |
6560 | ||
6561 | if Ekind (Nam) = E_Procedure then | |
996ae0b0 RK |
6562 | if Nkind (Parent (N)) = N_Entry_Call_Alternative |
6563 | and then N = Entry_Call_Statement (Parent (N)) | |
6564 | then | |
6565 | Error_Msg_N ("entry call required in select statement", N); | |
6566 | ||
6567 | elsif Nkind (Parent (N)) = N_Triggering_Alternative | |
6568 | and then N = Triggering_Statement (Parent (N)) | |
6569 | then | |
6570 | Error_Msg_N ("triggering statement cannot be procedure call", N); | |
6571 | ||
6572 | elsif Ekind (Scope (Nam)) = E_Task_Type | |
6573 | and then not In_Open_Scopes (Scope (Nam)) | |
6574 | then | |
758c442c | 6575 | Error_Msg_N ("task has no entry with this name", Entry_Name); |
996ae0b0 RK |
6576 | end if; |
6577 | end if; | |
6578 | ||
d81b4bfe TQ |
6579 | -- After resolution, entry calls and protected procedure calls are |
6580 | -- changed into entry calls, for expansion. The structure of the node | |
6581 | -- does not change, so it can safely be done in place. Protected | |
6582 | -- function calls must keep their structure because they are | |
6583 | -- subexpressions. | |
996ae0b0 RK |
6584 | |
6585 | if Ekind (Nam) /= E_Function then | |
6586 | ||
6587 | -- A protected operation that is not a function may modify the | |
d81b4bfe TQ |
6588 | -- corresponding object, and cannot apply to a constant. If this |
6589 | -- is an internal call, the prefix is the type itself. | |
996ae0b0 RK |
6590 | |
6591 | if Is_Protected_Type (Scope (Nam)) | |
6592 | and then not Is_Variable (Obj) | |
6593 | and then (not Is_Entity_Name (Obj) | |
6594 | or else not Is_Type (Entity (Obj))) | |
6595 | then | |
6596 | Error_Msg_N | |
6597 | ("prefix of protected procedure or entry call must be variable", | |
6598 | Entry_Name); | |
6599 | end if; | |
6600 | ||
6601 | Actuals := Parameter_Associations (N); | |
6602 | First_Named := First_Named_Actual (N); | |
6603 | ||
6604 | Rewrite (N, | |
6605 | Make_Entry_Call_Statement (Loc, | |
6606 | Name => Entry_Name, | |
6607 | Parameter_Associations => Actuals)); | |
6608 | ||
6609 | Set_First_Named_Actual (N, First_Named); | |
6610 | Set_Analyzed (N, True); | |
6611 | ||
6612 | -- Protected functions can return on the secondary stack, in which | |
1420b484 | 6613 | -- case we must trigger the transient scope mechanism. |
996ae0b0 RK |
6614 | |
6615 | elsif Expander_Active | |
6616 | and then Requires_Transient_Scope (Etype (Nam)) | |
6617 | then | |
0669bebe | 6618 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 6619 | end if; |
996ae0b0 RK |
6620 | end Resolve_Entry_Call; |
6621 | ||
6622 | ------------------------- | |
6623 | -- Resolve_Equality_Op -- | |
6624 | ------------------------- | |
6625 | ||
d81b4bfe TQ |
6626 | -- Both arguments must have the same type, and the boolean context does |
6627 | -- not participate in the resolution. The first pass verifies that the | |
6628 | -- interpretation is not ambiguous, and the type of the left argument is | |
6629 | -- correctly set, or is Any_Type in case of ambiguity. If both arguments | |
6630 | -- are strings or aggregates, allocators, or Null, they are ambiguous even | |
6631 | -- though they carry a single (universal) type. Diagnose this case here. | |
996ae0b0 RK |
6632 | |
6633 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is | |
6634 | L : constant Node_Id := Left_Opnd (N); | |
6635 | R : constant Node_Id := Right_Opnd (N); | |
6636 | T : Entity_Id := Find_Unique_Type (L, R); | |
6637 | ||
a8930b80 AC |
6638 | procedure Check_Conditional_Expression (Cond : Node_Id); |
6639 | -- The resolution rule for conditional expressions requires that each | |
6640 | -- such must have a unique type. This means that if several dependent | |
6641 | -- expressions are of a non-null anonymous access type, and the context | |
6642 | -- does not impose an expected type (as can be the case in an equality | |
6643 | -- operation) the expression must be rejected. | |
6644 | ||
996ae0b0 RK |
6645 | function Find_Unique_Access_Type return Entity_Id; |
6646 | -- In the case of allocators, make a last-ditch attempt to find a single | |
6647 | -- access type with the right designated type. This is semantically | |
6648 | -- dubious, and of no interest to any real code, but c48008a makes it | |
6649 | -- all worthwhile. | |
6650 | ||
a8930b80 AC |
6651 | ---------------------------------- |
6652 | -- Check_Conditional_Expression -- | |
6653 | ---------------------------------- | |
6654 | ||
6655 | procedure Check_Conditional_Expression (Cond : Node_Id) is | |
6656 | Then_Expr : Node_Id; | |
6657 | Else_Expr : Node_Id; | |
6658 | ||
6659 | begin | |
6660 | if Nkind (Cond) = N_Conditional_Expression then | |
6661 | Then_Expr := Next (First (Expressions (Cond))); | |
6662 | Else_Expr := Next (Then_Expr); | |
6663 | ||
6664 | if Nkind (Then_Expr) /= N_Null | |
6665 | and then Nkind (Else_Expr) /= N_Null | |
6666 | then | |
6667 | Error_Msg_N | |
6668 | ("cannot determine type of conditional expression", Cond); | |
6669 | end if; | |
6670 | end if; | |
6671 | end Check_Conditional_Expression; | |
6672 | ||
996ae0b0 RK |
6673 | ----------------------------- |
6674 | -- Find_Unique_Access_Type -- | |
6675 | ----------------------------- | |
6676 | ||
6677 | function Find_Unique_Access_Type return Entity_Id is | |
6678 | Acc : Entity_Id; | |
6679 | E : Entity_Id; | |
1420b484 | 6680 | S : Entity_Id; |
996ae0b0 RK |
6681 | |
6682 | begin | |
6683 | if Ekind (Etype (R)) = E_Allocator_Type then | |
6684 | Acc := Designated_Type (Etype (R)); | |
996ae0b0 RK |
6685 | elsif Ekind (Etype (L)) = E_Allocator_Type then |
6686 | Acc := Designated_Type (Etype (L)); | |
996ae0b0 RK |
6687 | else |
6688 | return Empty; | |
6689 | end if; | |
6690 | ||
1420b484 | 6691 | S := Current_Scope; |
996ae0b0 RK |
6692 | while S /= Standard_Standard loop |
6693 | E := First_Entity (S); | |
996ae0b0 | 6694 | while Present (E) loop |
996ae0b0 RK |
6695 | if Is_Type (E) |
6696 | and then Is_Access_Type (E) | |
6697 | and then Ekind (E) /= E_Allocator_Type | |
6698 | and then Designated_Type (E) = Base_Type (Acc) | |
6699 | then | |
6700 | return E; | |
6701 | end if; | |
6702 | ||
6703 | Next_Entity (E); | |
6704 | end loop; | |
6705 | ||
6706 | S := Scope (S); | |
6707 | end loop; | |
6708 | ||
6709 | return Empty; | |
6710 | end Find_Unique_Access_Type; | |
6711 | ||
6712 | -- Start of processing for Resolve_Equality_Op | |
6713 | ||
6714 | begin | |
6715 | Set_Etype (N, Base_Type (Typ)); | |
6716 | Generate_Reference (T, N, ' '); | |
6717 | ||
6718 | if T = Any_Fixed then | |
6719 | T := Unique_Fixed_Point_Type (L); | |
6720 | end if; | |
6721 | ||
6722 | if T /= Any_Type then | |
19fb051c AC |
6723 | if T = Any_String or else |
6724 | T = Any_Composite or else | |
6725 | T = Any_Character | |
996ae0b0 | 6726 | then |
996ae0b0 RK |
6727 | if T = Any_Character then |
6728 | Ambiguous_Character (L); | |
6729 | else | |
6730 | Error_Msg_N ("ambiguous operands for equality", N); | |
6731 | end if; | |
6732 | ||
6733 | Set_Etype (N, Any_Type); | |
6734 | return; | |
6735 | ||
6736 | elsif T = Any_Access | |
964f13da | 6737 | or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type) |
996ae0b0 RK |
6738 | then |
6739 | T := Find_Unique_Access_Type; | |
6740 | ||
6741 | if No (T) then | |
6742 | Error_Msg_N ("ambiguous operands for equality", N); | |
6743 | Set_Etype (N, Any_Type); | |
6744 | return; | |
6745 | end if; | |
a8930b80 AC |
6746 | |
6747 | -- Conditional expressions must have a single type, and if the | |
6748 | -- context does not impose one the dependent expressions cannot | |
6749 | -- be anonymous access types. | |
6750 | ||
6751 | elsif Ada_Version >= Ada_2012 | |
ae2aa109 AC |
6752 | and then Ekind_In (Etype (L), E_Anonymous_Access_Type, |
6753 | E_Anonymous_Access_Subprogram_Type) | |
6754 | and then Ekind_In (Etype (R), E_Anonymous_Access_Type, | |
6755 | E_Anonymous_Access_Subprogram_Type) | |
a8930b80 AC |
6756 | then |
6757 | Check_Conditional_Expression (L); | |
6758 | Check_Conditional_Expression (R); | |
996ae0b0 RK |
6759 | end if; |
6760 | ||
996ae0b0 RK |
6761 | Resolve (L, T); |
6762 | Resolve (R, T); | |
fbf5a39b | 6763 | |
b0186f71 AC |
6764 | -- In SPARK or ALFA, equality operators = and /= for array types |
6765 | -- other than String are only defined when, for each index position, | |
6766 | -- the operands have equal static bounds. | |
6767 | ||
fe5d3068 | 6768 | if Is_Array_Type (T) |
b0186f71 | 6769 | and then Base_Type (T) /= Standard_String |
19fb051c | 6770 | and then Base_Type (Etype (L)) = Base_Type (Etype (R)) |
b0186f71 AC |
6771 | and then not Matching_Static_Array_Bounds (Etype (L), Etype (R)) |
6772 | then | |
fe5d3068 YM |
6773 | Check_Formal_Restriction |
6774 | ("array types should have matching static bounds", N); | |
b0186f71 AC |
6775 | end if; |
6776 | ||
0669bebe GB |
6777 | -- If the unique type is a class-wide type then it will be expanded |
6778 | -- into a dispatching call to the predefined primitive. Therefore we | |
6779 | -- check here for potential violation of such restriction. | |
6780 | ||
6781 | if Is_Class_Wide_Type (T) then | |
6782 | Check_Restriction (No_Dispatching_Calls, N); | |
6783 | end if; | |
6784 | ||
fbf5a39b AC |
6785 | if Warn_On_Redundant_Constructs |
6786 | and then Comes_From_Source (N) | |
6787 | and then Is_Entity_Name (R) | |
6788 | and then Entity (R) = Standard_True | |
6789 | and then Comes_From_Source (R) | |
6790 | then | |
305caf42 AC |
6791 | Error_Msg_N -- CODEFIX |
6792 | ("?comparison with True is redundant!", R); | |
fbf5a39b AC |
6793 | end if; |
6794 | ||
996ae0b0 RK |
6795 | Check_Unset_Reference (L); |
6796 | Check_Unset_Reference (R); | |
fbf5a39b | 6797 | Generate_Operator_Reference (N, T); |
fad0600d | 6798 | Check_Low_Bound_Tested (N); |
996ae0b0 RK |
6799 | |
6800 | -- If this is an inequality, it may be the implicit inequality | |
6801 | -- created for a user-defined operation, in which case the corres- | |
6802 | -- ponding equality operation is not intrinsic, and the operation | |
6803 | -- cannot be constant-folded. Else fold. | |
6804 | ||
6805 | if Nkind (N) = N_Op_Eq | |
6806 | or else Comes_From_Source (Entity (N)) | |
6807 | or else Ekind (Entity (N)) = E_Operator | |
6808 | or else Is_Intrinsic_Subprogram | |
19fb051c | 6809 | (Corresponding_Equality (Entity (N))) |
996ae0b0 RK |
6810 | then |
6811 | Eval_Relational_Op (N); | |
45fc7ddb | 6812 | |
996ae0b0 | 6813 | elsif Nkind (N) = N_Op_Ne |
0669bebe | 6814 | and then Is_Abstract_Subprogram (Entity (N)) |
996ae0b0 RK |
6815 | then |
6816 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N)); | |
6817 | end if; | |
758c442c | 6818 | |
d81b4bfe TQ |
6819 | -- Ada 2005: If one operand is an anonymous access type, convert the |
6820 | -- other operand to it, to ensure that the underlying types match in | |
6821 | -- the back-end. Same for access_to_subprogram, and the conversion | |
6822 | -- verifies that the types are subtype conformant. | |
b7d1f17f | 6823 | |
d81b4bfe TQ |
6824 | -- We apply the same conversion in the case one of the operands is a |
6825 | -- private subtype of the type of the other. | |
c8ef728f | 6826 | |
b7d1f17f HK |
6827 | -- Why the Expander_Active test here ??? |
6828 | ||
4197ae1e | 6829 | if Expander_Active |
b7d1f17f | 6830 | and then |
964f13da RD |
6831 | (Ekind_In (T, E_Anonymous_Access_Type, |
6832 | E_Anonymous_Access_Subprogram_Type) | |
b7d1f17f | 6833 | or else Is_Private_Type (T)) |
c8ef728f ES |
6834 | then |
6835 | if Etype (L) /= T then | |
6836 | Rewrite (L, | |
6837 | Make_Unchecked_Type_Conversion (Sloc (L), | |
6838 | Subtype_Mark => New_Occurrence_Of (T, Sloc (L)), | |
6839 | Expression => Relocate_Node (L))); | |
6840 | Analyze_And_Resolve (L, T); | |
6841 | end if; | |
6842 | ||
6843 | if (Etype (R)) /= T then | |
6844 | Rewrite (R, | |
6845 | Make_Unchecked_Type_Conversion (Sloc (R), | |
6846 | Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)), | |
6847 | Expression => Relocate_Node (R))); | |
6848 | Analyze_And_Resolve (R, T); | |
6849 | end if; | |
6850 | end if; | |
996ae0b0 RK |
6851 | end if; |
6852 | end Resolve_Equality_Op; | |
6853 | ||
6854 | ---------------------------------- | |
6855 | -- Resolve_Explicit_Dereference -- | |
6856 | ---------------------------------- | |
6857 | ||
6858 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is | |
bc5f3720 RD |
6859 | Loc : constant Source_Ptr := Sloc (N); |
6860 | New_N : Node_Id; | |
6861 | P : constant Node_Id := Prefix (N); | |
6862 | I : Interp_Index; | |
6863 | It : Interp; | |
996ae0b0 RK |
6864 | |
6865 | begin | |
c8ef728f | 6866 | Check_Fully_Declared_Prefix (Typ, P); |
996ae0b0 RK |
6867 | |
6868 | if Is_Overloaded (P) then | |
6869 | ||
758c442c GD |
6870 | -- Use the context type to select the prefix that has the correct |
6871 | -- designated type. | |
996ae0b0 RK |
6872 | |
6873 | Get_First_Interp (P, I, It); | |
6874 | while Present (It.Typ) loop | |
6875 | exit when Is_Access_Type (It.Typ) | |
6876 | and then Covers (Typ, Designated_Type (It.Typ)); | |
996ae0b0 RK |
6877 | Get_Next_Interp (I, It); |
6878 | end loop; | |
6879 | ||
bc5f3720 RD |
6880 | if Present (It.Typ) then |
6881 | Resolve (P, It.Typ); | |
6882 | else | |
758c442c GD |
6883 | -- If no interpretation covers the designated type of the prefix, |
6884 | -- this is the pathological case where not all implementations of | |
6885 | -- the prefix allow the interpretation of the node as a call. Now | |
6886 | -- that the expected type is known, Remove other interpretations | |
6887 | -- from prefix, rewrite it as a call, and resolve again, so that | |
6888 | -- the proper call node is generated. | |
bc5f3720 RD |
6889 | |
6890 | Get_First_Interp (P, I, It); | |
6891 | while Present (It.Typ) loop | |
6892 | if Ekind (It.Typ) /= E_Access_Subprogram_Type then | |
6893 | Remove_Interp (I); | |
6894 | end if; | |
6895 | ||
6896 | Get_Next_Interp (I, It); | |
6897 | end loop; | |
6898 | ||
6899 | New_N := | |
6900 | Make_Function_Call (Loc, | |
6901 | Name => | |
6902 | Make_Explicit_Dereference (Loc, | |
6903 | Prefix => P), | |
6904 | Parameter_Associations => New_List); | |
6905 | ||
6906 | Save_Interps (N, New_N); | |
6907 | Rewrite (N, New_N); | |
6908 | Analyze_And_Resolve (N, Typ); | |
6909 | return; | |
6910 | end if; | |
6911 | ||
996ae0b0 RK |
6912 | Set_Etype (N, Designated_Type (It.Typ)); |
6913 | ||
6914 | else | |
fbf5a39b | 6915 | Resolve (P); |
996ae0b0 RK |
6916 | end if; |
6917 | ||
6918 | if Is_Access_Type (Etype (P)) then | |
6919 | Apply_Access_Check (N); | |
6920 | end if; | |
6921 | ||
758c442c GD |
6922 | -- If the designated type is a packed unconstrained array type, and the |
6923 | -- explicit dereference is not in the context of an attribute reference, | |
6924 | -- then we must compute and set the actual subtype, since it is needed | |
6925 | -- by Gigi. The reason we exclude the attribute case is that this is | |
6926 | -- handled fine by Gigi, and in fact we use such attributes to build the | |
6927 | -- actual subtype. We also exclude generated code (which builds actual | |
6928 | -- subtypes directly if they are needed). | |
996ae0b0 RK |
6929 | |
6930 | if Is_Array_Type (Etype (N)) | |
6931 | and then Is_Packed (Etype (N)) | |
6932 | and then not Is_Constrained (Etype (N)) | |
6933 | and then Nkind (Parent (N)) /= N_Attribute_Reference | |
6934 | and then Comes_From_Source (N) | |
6935 | then | |
6936 | Set_Etype (N, Get_Actual_Subtype (N)); | |
6937 | end if; | |
6938 | ||
09494c32 AC |
6939 | -- Note: No Eval processing is required for an explicit dereference, |
6940 | -- because such a name can never be static. | |
996ae0b0 RK |
6941 | |
6942 | end Resolve_Explicit_Dereference; | |
6943 | ||
955871d3 AC |
6944 | ------------------------------------- |
6945 | -- Resolve_Expression_With_Actions -- | |
6946 | ------------------------------------- | |
6947 | ||
6948 | procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is | |
6949 | begin | |
6950 | Set_Etype (N, Typ); | |
6951 | end Resolve_Expression_With_Actions; | |
6952 | ||
996ae0b0 RK |
6953 | ------------------------------- |
6954 | -- Resolve_Indexed_Component -- | |
6955 | ------------------------------- | |
6956 | ||
6957 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is | |
6958 | Name : constant Node_Id := Prefix (N); | |
6959 | Expr : Node_Id; | |
6960 | Array_Type : Entity_Id := Empty; -- to prevent junk warning | |
6961 | Index : Node_Id; | |
6962 | ||
6963 | begin | |
6964 | if Is_Overloaded (Name) then | |
6965 | ||
758c442c GD |
6966 | -- Use the context type to select the prefix that yields the correct |
6967 | -- component type. | |
996ae0b0 RK |
6968 | |
6969 | declare | |
6970 | I : Interp_Index; | |
6971 | It : Interp; | |
6972 | I1 : Interp_Index := 0; | |
6973 | P : constant Node_Id := Prefix (N); | |
6974 | Found : Boolean := False; | |
6975 | ||
6976 | begin | |
6977 | Get_First_Interp (P, I, It); | |
996ae0b0 | 6978 | while Present (It.Typ) loop |
996ae0b0 RK |
6979 | if (Is_Array_Type (It.Typ) |
6980 | and then Covers (Typ, Component_Type (It.Typ))) | |
6981 | or else (Is_Access_Type (It.Typ) | |
6982 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
19fb051c AC |
6983 | and then |
6984 | Covers | |
6985 | (Typ, | |
6986 | Component_Type (Designated_Type (It.Typ)))) | |
996ae0b0 RK |
6987 | then |
6988 | if Found then | |
6989 | It := Disambiguate (P, I1, I, Any_Type); | |
6990 | ||
6991 | if It = No_Interp then | |
6992 | Error_Msg_N ("ambiguous prefix for indexing", N); | |
6993 | Set_Etype (N, Typ); | |
6994 | return; | |
6995 | ||
6996 | else | |
6997 | Found := True; | |
6998 | Array_Type := It.Typ; | |
6999 | I1 := I; | |
7000 | end if; | |
7001 | ||
7002 | else | |
7003 | Found := True; | |
7004 | Array_Type := It.Typ; | |
7005 | I1 := I; | |
7006 | end if; | |
7007 | end if; | |
7008 | ||
7009 | Get_Next_Interp (I, It); | |
7010 | end loop; | |
7011 | end; | |
7012 | ||
7013 | else | |
7014 | Array_Type := Etype (Name); | |
7015 | end if; | |
7016 | ||
7017 | Resolve (Name, Array_Type); | |
7018 | Array_Type := Get_Actual_Subtype_If_Available (Name); | |
7019 | ||
7020 | -- If prefix is access type, dereference to get real array type. | |
7021 | -- Note: we do not apply an access check because the expander always | |
7022 | -- introduces an explicit dereference, and the check will happen there. | |
7023 | ||
7024 | if Is_Access_Type (Array_Type) then | |
7025 | Array_Type := Designated_Type (Array_Type); | |
7026 | end if; | |
7027 | ||
a77842bd | 7028 | -- If name was overloaded, set component type correctly now |
f3d57416 | 7029 | -- If a misplaced call to an entry family (which has no index types) |
b7d1f17f | 7030 | -- return. Error will be diagnosed from calling context. |
996ae0b0 | 7031 | |
b7d1f17f HK |
7032 | if Is_Array_Type (Array_Type) then |
7033 | Set_Etype (N, Component_Type (Array_Type)); | |
7034 | else | |
7035 | return; | |
7036 | end if; | |
996ae0b0 RK |
7037 | |
7038 | Index := First_Index (Array_Type); | |
7039 | Expr := First (Expressions (N)); | |
7040 | ||
758c442c GD |
7041 | -- The prefix may have resolved to a string literal, in which case its |
7042 | -- etype has a special representation. This is only possible currently | |
7043 | -- if the prefix is a static concatenation, written in functional | |
7044 | -- notation. | |
996ae0b0 RK |
7045 | |
7046 | if Ekind (Array_Type) = E_String_Literal_Subtype then | |
7047 | Resolve (Expr, Standard_Positive); | |
7048 | ||
7049 | else | |
7050 | while Present (Index) and Present (Expr) loop | |
7051 | Resolve (Expr, Etype (Index)); | |
7052 | Check_Unset_Reference (Expr); | |
7053 | ||
7054 | if Is_Scalar_Type (Etype (Expr)) then | |
7055 | Apply_Scalar_Range_Check (Expr, Etype (Index)); | |
7056 | else | |
7057 | Apply_Range_Check (Expr, Get_Actual_Subtype (Index)); | |
7058 | end if; | |
7059 | ||
7060 | Next_Index (Index); | |
7061 | Next (Expr); | |
7062 | end loop; | |
7063 | end if; | |
7064 | ||
0669bebe GB |
7065 | -- Do not generate the warning on suspicious index if we are analyzing |
7066 | -- package Ada.Tags; otherwise we will report the warning with the | |
7067 | -- Prims_Ptr field of the dispatch table. | |
7068 | ||
7069 | if Scope (Etype (Prefix (N))) = Standard_Standard | |
7070 | or else not | |
7071 | Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))), | |
7072 | Ada_Tags) | |
7073 | then | |
7074 | Warn_On_Suspicious_Index (Name, First (Expressions (N))); | |
7075 | Eval_Indexed_Component (N); | |
7076 | end if; | |
c28408b7 RD |
7077 | |
7078 | -- If the array type is atomic, and is packed, and we are in a left side | |
7079 | -- context, then this is worth a warning, since we have a situation | |
7080 | -- where the access to the component may cause extra read/writes of | |
7081 | -- the atomic array object, which could be considered unexpected. | |
7082 | ||
7083 | if Nkind (N) = N_Indexed_Component | |
7084 | and then (Is_Atomic (Array_Type) | |
7085 | or else (Is_Entity_Name (Prefix (N)) | |
7086 | and then Is_Atomic (Entity (Prefix (N))))) | |
7087 | and then Is_Bit_Packed_Array (Array_Type) | |
7088 | and then Is_LHS (N) | |
7089 | then | |
7090 | Error_Msg_N ("?assignment to component of packed atomic array", | |
7091 | Prefix (N)); | |
7092 | Error_Msg_N ("?\may cause unexpected accesses to atomic object", | |
7093 | Prefix (N)); | |
7094 | end if; | |
996ae0b0 RK |
7095 | end Resolve_Indexed_Component; |
7096 | ||
7097 | ----------------------------- | |
7098 | -- Resolve_Integer_Literal -- | |
7099 | ----------------------------- | |
7100 | ||
7101 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is | |
7102 | begin | |
7103 | Set_Etype (N, Typ); | |
7104 | Eval_Integer_Literal (N); | |
7105 | end Resolve_Integer_Literal; | |
7106 | ||
15ce9ca2 AC |
7107 | -------------------------------- |
7108 | -- Resolve_Intrinsic_Operator -- | |
7109 | -------------------------------- | |
996ae0b0 RK |
7110 | |
7111 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is | |
bb481772 AC |
7112 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); |
7113 | Op : Entity_Id; | |
7114 | Orig_Op : constant Entity_Id := Entity (N); | |
7115 | Arg1 : Node_Id; | |
7116 | Arg2 : Node_Id; | |
996ae0b0 RK |
7117 | |
7118 | begin | |
305caf42 AC |
7119 | -- We must preserve the original entity in a generic setting, so that |
7120 | -- the legality of the operation can be verified in an instance. | |
7121 | ||
7122 | if not Expander_Active then | |
7123 | return; | |
7124 | end if; | |
7125 | ||
996ae0b0 | 7126 | Op := Entity (N); |
996ae0b0 RK |
7127 | while Scope (Op) /= Standard_Standard loop |
7128 | Op := Homonym (Op); | |
7129 | pragma Assert (Present (Op)); | |
7130 | end loop; | |
7131 | ||
7132 | Set_Entity (N, Op); | |
af152989 | 7133 | Set_Is_Overloaded (N, False); |
996ae0b0 | 7134 | |
758c442c GD |
7135 | -- If the operand type is private, rewrite with suitable conversions on |
7136 | -- the operands and the result, to expose the proper underlying numeric | |
7137 | -- type. | |
996ae0b0 | 7138 | |
fbf5a39b AC |
7139 | if Is_Private_Type (Typ) then |
7140 | Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N)); | |
7141 | ||
7142 | if Nkind (N) = N_Op_Expon then | |
7143 | Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N)); | |
7144 | else | |
7145 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
7146 | end if; | |
7147 | ||
bb481772 AC |
7148 | if Nkind (Arg1) = N_Type_Conversion then |
7149 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
7150 | end if; | |
7151 | ||
7152 | if Nkind (Arg2) = N_Type_Conversion then | |
7153 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
7154 | end if; | |
996ae0b0 | 7155 | |
fbf5a39b AC |
7156 | Set_Left_Opnd (N, Arg1); |
7157 | Set_Right_Opnd (N, Arg2); | |
7158 | ||
7159 | Set_Etype (N, Btyp); | |
7160 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7161 | Resolve (N, Typ); | |
7162 | ||
7163 | elsif Typ /= Etype (Left_Opnd (N)) | |
7164 | or else Typ /= Etype (Right_Opnd (N)) | |
7165 | then | |
d81b4bfe | 7166 | -- Add explicit conversion where needed, and save interpretations in |
bb481772 AC |
7167 | -- case operands are overloaded. If the context is a VMS operation, |
7168 | -- assert that the conversion is legal (the operands have the proper | |
7169 | -- types to select the VMS intrinsic). Note that in rare cases the | |
7170 | -- VMS operators may be visible, but the default System is being used | |
7171 | -- and Address is a private type. | |
fbf5a39b | 7172 | |
af152989 | 7173 | Arg1 := Convert_To (Typ, Left_Opnd (N)); |
fbf5a39b AC |
7174 | Arg2 := Convert_To (Typ, Right_Opnd (N)); |
7175 | ||
7176 | if Nkind (Arg1) = N_Type_Conversion then | |
7177 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
bb481772 AC |
7178 | |
7179 | if Is_VMS_Operator (Orig_Op) then | |
7180 | Set_Conversion_OK (Arg1); | |
7181 | end if; | |
af152989 AC |
7182 | else |
7183 | Save_Interps (Left_Opnd (N), Arg1); | |
fbf5a39b AC |
7184 | end if; |
7185 | ||
7186 | if Nkind (Arg2) = N_Type_Conversion then | |
7187 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
bb481772 AC |
7188 | |
7189 | if Is_VMS_Operator (Orig_Op) then | |
7190 | Set_Conversion_OK (Arg2); | |
7191 | end if; | |
af152989 | 7192 | else |
0ab80019 | 7193 | Save_Interps (Right_Opnd (N), Arg2); |
fbf5a39b AC |
7194 | end if; |
7195 | ||
7196 | Rewrite (Left_Opnd (N), Arg1); | |
7197 | Rewrite (Right_Opnd (N), Arg2); | |
7198 | Analyze (Arg1); | |
7199 | Analyze (Arg2); | |
7200 | Resolve_Arithmetic_Op (N, Typ); | |
7201 | ||
7202 | else | |
7203 | Resolve_Arithmetic_Op (N, Typ); | |
7204 | end if; | |
996ae0b0 RK |
7205 | end Resolve_Intrinsic_Operator; |
7206 | ||
fbf5a39b AC |
7207 | -------------------------------------- |
7208 | -- Resolve_Intrinsic_Unary_Operator -- | |
7209 | -------------------------------------- | |
7210 | ||
7211 | procedure Resolve_Intrinsic_Unary_Operator | |
7212 | (N : Node_Id; | |
7213 | Typ : Entity_Id) | |
7214 | is | |
7215 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); | |
7216 | Op : Entity_Id; | |
7217 | Arg2 : Node_Id; | |
7218 | ||
7219 | begin | |
7220 | Op := Entity (N); | |
fbf5a39b AC |
7221 | while Scope (Op) /= Standard_Standard loop |
7222 | Op := Homonym (Op); | |
7223 | pragma Assert (Present (Op)); | |
7224 | end loop; | |
7225 | ||
7226 | Set_Entity (N, Op); | |
7227 | ||
7228 | if Is_Private_Type (Typ) then | |
7229 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
7230 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
7231 | ||
7232 | Set_Right_Opnd (N, Arg2); | |
7233 | ||
7234 | Set_Etype (N, Btyp); | |
7235 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7236 | Resolve (N, Typ); | |
7237 | ||
7238 | else | |
7239 | Resolve_Unary_Op (N, Typ); | |
7240 | end if; | |
7241 | end Resolve_Intrinsic_Unary_Operator; | |
7242 | ||
996ae0b0 RK |
7243 | ------------------------ |
7244 | -- Resolve_Logical_Op -- | |
7245 | ------------------------ | |
7246 | ||
7247 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is | |
7248 | B_Typ : Entity_Id; | |
7249 | ||
7250 | begin | |
f61580d4 AC |
7251 | Check_No_Direct_Boolean_Operators (N); |
7252 | ||
758c442c GD |
7253 | -- Predefined operations on scalar types yield the base type. On the |
7254 | -- other hand, logical operations on arrays yield the type of the | |
7255 | -- arguments (and the context). | |
996ae0b0 RK |
7256 | |
7257 | if Is_Array_Type (Typ) then | |
7258 | B_Typ := Typ; | |
7259 | else | |
7260 | B_Typ := Base_Type (Typ); | |
7261 | end if; | |
7262 | ||
001c7783 AC |
7263 | -- OK if this is a VMS-specific intrinsic operation |
7264 | ||
7265 | if Is_VMS_Operator (Entity (N)) then | |
7266 | null; | |
7267 | ||
996ae0b0 RK |
7268 | -- The following test is required because the operands of the operation |
7269 | -- may be literals, in which case the resulting type appears to be | |
7270 | -- compatible with a signed integer type, when in fact it is compatible | |
7271 | -- only with modular types. If the context itself is universal, the | |
7272 | -- operation is illegal. | |
7273 | ||
001c7783 | 7274 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
7275 | Error_Msg_N ("invalid context for logical operation", N); |
7276 | Set_Etype (N, Any_Type); | |
7277 | return; | |
7278 | ||
7279 | elsif Typ = Any_Modular then | |
7280 | Error_Msg_N | |
7281 | ("no modular type available in this context", N); | |
7282 | Set_Etype (N, Any_Type); | |
7283 | return; | |
19fb051c | 7284 | |
07fc65c4 GB |
7285 | elsif Is_Modular_Integer_Type (Typ) |
7286 | and then Etype (Left_Opnd (N)) = Universal_Integer | |
7287 | and then Etype (Right_Opnd (N)) = Universal_Integer | |
7288 | then | |
7289 | Check_For_Visible_Operator (N, B_Typ); | |
996ae0b0 RK |
7290 | end if; |
7291 | ||
7292 | Resolve (Left_Opnd (N), B_Typ); | |
7293 | Resolve (Right_Opnd (N), B_Typ); | |
7294 | ||
7295 | Check_Unset_Reference (Left_Opnd (N)); | |
7296 | Check_Unset_Reference (Right_Opnd (N)); | |
7297 | ||
7298 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7299 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 | 7300 | Eval_Logical_Op (N); |
9f90d123 AC |
7301 | |
7302 | -- In SPARK or ALFA, logical operations AND, OR and XOR for arrays are | |
7303 | -- defined only when both operands have same static lower and higher | |
19fb051c AC |
7304 | -- bounds. Of course the types have to match, so only check if operands |
7305 | -- are compatible and the node itself has no errors. | |
9f90d123 | 7306 | |
fe5d3068 | 7307 | if Is_Array_Type (B_Typ) |
19fb051c AC |
7308 | and then Nkind (N) in N_Binary_Op |
7309 | and then | |
7310 | Base_Type (Etype (Left_Opnd (N))) | |
7311 | = Base_Type (Etype (Right_Opnd (N))) | |
b0186f71 AC |
7312 | and then not Matching_Static_Array_Bounds (Etype (Left_Opnd (N)), |
7313 | Etype (Right_Opnd (N))) | |
9f90d123 | 7314 | then |
fe5d3068 YM |
7315 | Check_Formal_Restriction |
7316 | ("array types should have matching static bounds", N); | |
9f90d123 AC |
7317 | end if; |
7318 | ||
996ae0b0 RK |
7319 | end Resolve_Logical_Op; |
7320 | ||
7321 | --------------------------- | |
7322 | -- Resolve_Membership_Op -- | |
7323 | --------------------------- | |
7324 | ||
5cc9353d RD |
7325 | -- The context can only be a boolean type, and does not determine the |
7326 | -- arguments. Arguments should be unambiguous, but the preference rule for | |
7327 | -- universal types applies. | |
996ae0b0 RK |
7328 | |
7329 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7330 | pragma Warnings (Off, Typ); |
7331 | ||
197e4514 | 7332 | L : constant Node_Id := Left_Opnd (N); |
b1c11e0e | 7333 | R : constant Node_Id := Right_Opnd (N); |
996ae0b0 RK |
7334 | T : Entity_Id; |
7335 | ||
197e4514 | 7336 | procedure Resolve_Set_Membership; |
5cc9353d RD |
7337 | -- Analysis has determined a unique type for the left operand. Use it to |
7338 | -- resolve the disjuncts. | |
197e4514 AC |
7339 | |
7340 | ---------------------------- | |
7341 | -- Resolve_Set_Membership -- | |
7342 | ---------------------------- | |
7343 | ||
7344 | procedure Resolve_Set_Membership is | |
7345 | Alt : Node_Id; | |
7346 | ||
7347 | begin | |
7348 | Resolve (L, Etype (L)); | |
7349 | ||
7350 | Alt := First (Alternatives (N)); | |
7351 | while Present (Alt) loop | |
7352 | ||
7353 | -- Alternative is an expression, a range | |
7354 | -- or a subtype mark. | |
7355 | ||
7356 | if not Is_Entity_Name (Alt) | |
7357 | or else not Is_Type (Entity (Alt)) | |
7358 | then | |
7359 | Resolve (Alt, Etype (L)); | |
7360 | end if; | |
7361 | ||
7362 | Next (Alt); | |
7363 | end loop; | |
7364 | end Resolve_Set_Membership; | |
7365 | ||
442c0581 | 7366 | -- Start of processing for Resolve_Membership_Op |
197e4514 | 7367 | |
996ae0b0 RK |
7368 | begin |
7369 | if L = Error or else R = Error then | |
7370 | return; | |
7371 | end if; | |
7372 | ||
197e4514 AC |
7373 | if Present (Alternatives (N)) then |
7374 | Resolve_Set_Membership; | |
7375 | return; | |
7376 | ||
7377 | elsif not Is_Overloaded (R) | |
996ae0b0 | 7378 | and then |
19fb051c AC |
7379 | (Etype (R) = Universal_Integer |
7380 | or else | |
996ae0b0 RK |
7381 | Etype (R) = Universal_Real) |
7382 | and then Is_Overloaded (L) | |
7383 | then | |
7384 | T := Etype (R); | |
1420b484 | 7385 | |
d81b4bfe | 7386 | -- Ada 2005 (AI-251): Support the following case: |
1420b484 JM |
7387 | |
7388 | -- type I is interface; | |
7389 | -- type T is tagged ... | |
7390 | ||
c8ef728f | 7391 | -- function Test (O : I'Class) is |
1420b484 JM |
7392 | -- begin |
7393 | -- return O in T'Class. | |
7394 | -- end Test; | |
7395 | ||
d81b4bfe | 7396 | -- In this case we have nothing else to do. The membership test will be |
e7c0dd39 | 7397 | -- done at run time. |
1420b484 | 7398 | |
0791fbe9 | 7399 | elsif Ada_Version >= Ada_2005 |
1420b484 JM |
7400 | and then Is_Class_Wide_Type (Etype (L)) |
7401 | and then Is_Interface (Etype (L)) | |
7402 | and then Is_Class_Wide_Type (Etype (R)) | |
7403 | and then not Is_Interface (Etype (R)) | |
7404 | then | |
7405 | return; | |
996ae0b0 RK |
7406 | else |
7407 | T := Intersect_Types (L, R); | |
7408 | end if; | |
7409 | ||
9a0ddeee AC |
7410 | -- If mixed-mode operations are present and operands are all literal, |
7411 | -- the only interpretation involves Duration, which is probably not | |
7412 | -- the intention of the programmer. | |
7413 | ||
7414 | if T = Any_Fixed then | |
7415 | T := Unique_Fixed_Point_Type (N); | |
7416 | ||
7417 | if T = Any_Type then | |
7418 | return; | |
7419 | end if; | |
7420 | end if; | |
7421 | ||
996ae0b0 RK |
7422 | Resolve (L, T); |
7423 | Check_Unset_Reference (L); | |
7424 | ||
7425 | if Nkind (R) = N_Range | |
7426 | and then not Is_Scalar_Type (T) | |
7427 | then | |
7428 | Error_Msg_N ("scalar type required for range", R); | |
7429 | end if; | |
7430 | ||
7431 | if Is_Entity_Name (R) then | |
7432 | Freeze_Expression (R); | |
7433 | else | |
7434 | Resolve (R, T); | |
7435 | Check_Unset_Reference (R); | |
7436 | end if; | |
7437 | ||
7438 | Eval_Membership_Op (N); | |
7439 | end Resolve_Membership_Op; | |
7440 | ||
7441 | ------------------ | |
7442 | -- Resolve_Null -- | |
7443 | ------------------ | |
7444 | ||
7445 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is | |
b1c11e0e JM |
7446 | Loc : constant Source_Ptr := Sloc (N); |
7447 | ||
996ae0b0 | 7448 | begin |
758c442c | 7449 | -- Handle restriction against anonymous null access values This |
6ba6b1e3 | 7450 | -- restriction can be turned off using -gnatdj. |
996ae0b0 | 7451 | |
0ab80019 | 7452 | -- Ada 2005 (AI-231): Remove restriction |
2820d220 | 7453 | |
0791fbe9 | 7454 | if Ada_Version < Ada_2005 |
2820d220 | 7455 | and then not Debug_Flag_J |
996ae0b0 RK |
7456 | and then Ekind (Typ) = E_Anonymous_Access_Type |
7457 | and then Comes_From_Source (N) | |
7458 | then | |
d81b4bfe TQ |
7459 | -- In the common case of a call which uses an explicitly null value |
7460 | -- for an access parameter, give specialized error message. | |
996ae0b0 | 7461 | |
45fc7ddb HK |
7462 | if Nkind_In (Parent (N), N_Procedure_Call_Statement, |
7463 | N_Function_Call) | |
996ae0b0 RK |
7464 | then |
7465 | Error_Msg_N | |
7466 | ("null is not allowed as argument for an access parameter", N); | |
7467 | ||
7468 | -- Standard message for all other cases (are there any?) | |
7469 | ||
7470 | else | |
7471 | Error_Msg_N | |
7472 | ("null cannot be of an anonymous access type", N); | |
7473 | end if; | |
7474 | end if; | |
7475 | ||
b1c11e0e JM |
7476 | -- Ada 2005 (AI-231): Generate the null-excluding check in case of |
7477 | -- assignment to a null-excluding object | |
7478 | ||
0791fbe9 | 7479 | if Ada_Version >= Ada_2005 |
b1c11e0e JM |
7480 | and then Can_Never_Be_Null (Typ) |
7481 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
7482 | then | |
7483 | if not Inside_Init_Proc then | |
7484 | Insert_Action | |
7485 | (Compile_Time_Constraint_Error (N, | |
7486 | "(Ada 2005) null not allowed in null-excluding objects?"), | |
7487 | Make_Raise_Constraint_Error (Loc, | |
7488 | Reason => CE_Access_Check_Failed)); | |
7489 | else | |
7490 | Insert_Action (N, | |
7491 | Make_Raise_Constraint_Error (Loc, | |
7492 | Reason => CE_Access_Check_Failed)); | |
7493 | end if; | |
7494 | end if; | |
7495 | ||
d81b4bfe TQ |
7496 | -- In a distributed context, null for a remote access to subprogram may |
7497 | -- need to be replaced with a special record aggregate. In this case, | |
7498 | -- return after having done the transformation. | |
996ae0b0 RK |
7499 | |
7500 | if (Ekind (Typ) = E_Record_Type | |
7501 | or else Is_Remote_Access_To_Subprogram_Type (Typ)) | |
7502 | and then Remote_AST_Null_Value (N, Typ) | |
7503 | then | |
7504 | return; | |
7505 | end if; | |
7506 | ||
a77842bd | 7507 | -- The null literal takes its type from the context |
996ae0b0 RK |
7508 | |
7509 | Set_Etype (N, Typ); | |
7510 | end Resolve_Null; | |
7511 | ||
7512 | ----------------------- | |
7513 | -- Resolve_Op_Concat -- | |
7514 | ----------------------- | |
7515 | ||
7516 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is | |
996ae0b0 | 7517 | |
10303118 BD |
7518 | -- We wish to avoid deep recursion, because concatenations are often |
7519 | -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left | |
7520 | -- operands nonrecursively until we find something that is not a simple | |
7521 | -- concatenation (A in this case). We resolve that, and then walk back | |
7522 | -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest | |
7523 | -- to do the rest of the work at each level. The Parent pointers allow | |
7524 | -- us to avoid recursion, and thus avoid running out of memory. See also | |
d81b4bfe | 7525 | -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used. |
996ae0b0 | 7526 | |
10303118 BD |
7527 | NN : Node_Id := N; |
7528 | Op1 : Node_Id; | |
996ae0b0 | 7529 | |
10303118 BD |
7530 | begin |
7531 | -- The following code is equivalent to: | |
996ae0b0 | 7532 | |
10303118 BD |
7533 | -- Resolve_Op_Concat_First (NN, Typ); |
7534 | -- Resolve_Op_Concat_Arg (N, ...); | |
7535 | -- Resolve_Op_Concat_Rest (N, Typ); | |
996ae0b0 | 7536 | |
10303118 BD |
7537 | -- where the Resolve_Op_Concat_Arg call recurses back here if the left |
7538 | -- operand is a concatenation. | |
996ae0b0 | 7539 | |
10303118 | 7540 | -- Walk down left operands |
996ae0b0 | 7541 | |
10303118 BD |
7542 | loop |
7543 | Resolve_Op_Concat_First (NN, Typ); | |
7544 | Op1 := Left_Opnd (NN); | |
7545 | exit when not (Nkind (Op1) = N_Op_Concat | |
7546 | and then not Is_Array_Type (Component_Type (Typ)) | |
7547 | and then Entity (Op1) = Entity (NN)); | |
7548 | NN := Op1; | |
7549 | end loop; | |
996ae0b0 | 7550 | |
10303118 | 7551 | -- Now (given the above example) NN is A&B and Op1 is A |
996ae0b0 | 7552 | |
10303118 | 7553 | -- First resolve Op1 ... |
9ebe3743 | 7554 | |
10303118 | 7555 | Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN)); |
9ebe3743 | 7556 | |
10303118 BD |
7557 | -- ... then walk NN back up until we reach N (where we started), calling |
7558 | -- Resolve_Op_Concat_Rest along the way. | |
9ebe3743 | 7559 | |
10303118 BD |
7560 | loop |
7561 | Resolve_Op_Concat_Rest (NN, Typ); | |
7562 | exit when NN = N; | |
7563 | NN := Parent (NN); | |
7564 | end loop; | |
2933b16c | 7565 | |
fe5d3068 YM |
7566 | if Base_Type (Etype (N)) /= Standard_String then |
7567 | Check_Formal_Restriction | |
7568 | ("result of concatenation should have type String", N); | |
2933b16c | 7569 | end if; |
10303118 | 7570 | end Resolve_Op_Concat; |
9ebe3743 | 7571 | |
10303118 BD |
7572 | --------------------------- |
7573 | -- Resolve_Op_Concat_Arg -- | |
7574 | --------------------------- | |
996ae0b0 | 7575 | |
10303118 BD |
7576 | procedure Resolve_Op_Concat_Arg |
7577 | (N : Node_Id; | |
7578 | Arg : Node_Id; | |
7579 | Typ : Entity_Id; | |
7580 | Is_Comp : Boolean) | |
7581 | is | |
7582 | Btyp : constant Entity_Id := Base_Type (Typ); | |
996ae0b0 | 7583 | |
10303118 BD |
7584 | begin |
7585 | if In_Instance then | |
7586 | if Is_Comp | |
7587 | or else (not Is_Overloaded (Arg) | |
7588 | and then Etype (Arg) /= Any_Composite | |
7589 | and then Covers (Component_Type (Typ), Etype (Arg))) | |
7590 | then | |
7591 | Resolve (Arg, Component_Type (Typ)); | |
7592 | else | |
7593 | Resolve (Arg, Btyp); | |
7594 | end if; | |
fbf5a39b | 7595 | |
10303118 BD |
7596 | elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then |
7597 | if Nkind (Arg) = N_Aggregate | |
7598 | and then Is_Composite_Type (Component_Type (Typ)) | |
7599 | then | |
7600 | if Is_Private_Type (Component_Type (Typ)) then | |
7601 | Resolve (Arg, Btyp); | |
7602 | else | |
7603 | Error_Msg_N ("ambiguous aggregate must be qualified", Arg); | |
7604 | Set_Etype (Arg, Any_Type); | |
996ae0b0 RK |
7605 | end if; |
7606 | ||
7607 | else | |
10303118 BD |
7608 | if Is_Overloaded (Arg) |
7609 | and then Has_Compatible_Type (Arg, Typ) | |
7610 | and then Etype (Arg) /= Any_Type | |
7611 | then | |
7612 | declare | |
7613 | I : Interp_Index; | |
7614 | It : Interp; | |
7615 | Func : Entity_Id; | |
7616 | ||
7617 | begin | |
7618 | Get_First_Interp (Arg, I, It); | |
7619 | Func := It.Nam; | |
7620 | Get_Next_Interp (I, It); | |
7621 | ||
7622 | -- Special-case the error message when the overloading is | |
7623 | -- caused by a function that yields an array and can be | |
7624 | -- called without parameters. | |
7625 | ||
7626 | if It.Nam = Func then | |
7627 | Error_Msg_Sloc := Sloc (Func); | |
7628 | Error_Msg_N ("ambiguous call to function#", Arg); | |
7629 | Error_Msg_NE | |
7630 | ("\\interpretation as call yields&", Arg, Typ); | |
7631 | Error_Msg_NE | |
7632 | ("\\interpretation as indexing of call yields&", | |
7633 | Arg, Component_Type (Typ)); | |
7634 | ||
7635 | else | |
7636 | Error_Msg_N | |
7637 | ("ambiguous operand for concatenation!", Arg); | |
19fb051c | 7638 | |
10303118 BD |
7639 | Get_First_Interp (Arg, I, It); |
7640 | while Present (It.Nam) loop | |
7641 | Error_Msg_Sloc := Sloc (It.Nam); | |
7642 | ||
7643 | if Base_Type (It.Typ) = Base_Type (Typ) | |
7644 | or else Base_Type (It.Typ) = | |
19fb051c | 7645 | Base_Type (Component_Type (Typ)) |
10303118 | 7646 | then |
4e7a4f6e AC |
7647 | Error_Msg_N -- CODEFIX |
7648 | ("\\possible interpretation#", Arg); | |
10303118 BD |
7649 | end if; |
7650 | ||
7651 | Get_Next_Interp (I, It); | |
7652 | end loop; | |
7653 | end if; | |
7654 | end; | |
7655 | end if; | |
7656 | ||
7657 | Resolve (Arg, Component_Type (Typ)); | |
7658 | ||
7659 | if Nkind (Arg) = N_String_Literal then | |
7660 | Set_Etype (Arg, Component_Type (Typ)); | |
7661 | end if; | |
7662 | ||
7663 | if Arg = Left_Opnd (N) then | |
7664 | Set_Is_Component_Left_Opnd (N); | |
7665 | else | |
7666 | Set_Is_Component_Right_Opnd (N); | |
7667 | end if; | |
996ae0b0 RK |
7668 | end if; |
7669 | ||
10303118 BD |
7670 | else |
7671 | Resolve (Arg, Btyp); | |
7672 | end if; | |
7673 | ||
2933b16c RD |
7674 | -- Concatenation is restricted in SPARK or ALFA: each operand must be |
7675 | -- either a string literal, a static character expression, or another | |
7676 | -- concatenation. Arg cannot be a concatenation here as callers of | |
7677 | -- Resolve_Op_Concat_Arg call it separately on each final operand, past | |
7678 | -- concatenation operations. | |
7679 | ||
fe5d3068 YM |
7680 | if Is_Character_Type (Etype (Arg)) then |
7681 | if not Is_Static_Expression (Arg) then | |
7682 | Check_Formal_Restriction | |
7683 | ("character operand for concatenation should be static", N); | |
7684 | end if; | |
2933b16c | 7685 | |
fe5d3068 YM |
7686 | elsif Is_String_Type (Etype (Arg)) then |
7687 | if Nkind (Arg) /= N_String_Literal then | |
7688 | Check_Formal_Restriction | |
7689 | ("string operand for concatenation should be a literal", N); | |
7690 | end if; | |
2933b16c | 7691 | |
b9e48541 AC |
7692 | -- Do not issue error on an operand that is neither a character nor a |
7693 | -- string, as the error is issued in Resolve_Op_Concat. | |
2933b16c | 7694 | |
fe5d3068 YM |
7695 | else |
7696 | null; | |
2933b16c RD |
7697 | end if; |
7698 | ||
10303118 BD |
7699 | Check_Unset_Reference (Arg); |
7700 | end Resolve_Op_Concat_Arg; | |
996ae0b0 | 7701 | |
10303118 BD |
7702 | ----------------------------- |
7703 | -- Resolve_Op_Concat_First -- | |
7704 | ----------------------------- | |
7705 | ||
7706 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is | |
7707 | Btyp : constant Entity_Id := Base_Type (Typ); | |
7708 | Op1 : constant Node_Id := Left_Opnd (N); | |
7709 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 RK |
7710 | |
7711 | begin | |
dae2b8ea HK |
7712 | -- The parser folds an enormous sequence of concatenations of string |
7713 | -- literals into "" & "...", where the Is_Folded_In_Parser flag is set | |
4fc26524 | 7714 | -- in the right operand. If the expression resolves to a predefined "&" |
dae2b8ea HK |
7715 | -- operator, all is well. Otherwise, the parser's folding is wrong, so |
7716 | -- we give an error. See P_Simple_Expression in Par.Ch4. | |
7717 | ||
7718 | if Nkind (Op2) = N_String_Literal | |
7719 | and then Is_Folded_In_Parser (Op2) | |
7720 | and then Ekind (Entity (N)) = E_Function | |
7721 | then | |
7722 | pragma Assert (Nkind (Op1) = N_String_Literal -- should be "" | |
7723 | and then String_Length (Strval (Op1)) = 0); | |
7724 | Error_Msg_N ("too many user-defined concatenations", N); | |
7725 | return; | |
7726 | end if; | |
7727 | ||
996ae0b0 RK |
7728 | Set_Etype (N, Btyp); |
7729 | ||
7730 | if Is_Limited_Composite (Btyp) then | |
7731 | Error_Msg_N ("concatenation not available for limited array", N); | |
fbf5a39b | 7732 | Explain_Limited_Type (Btyp, N); |
996ae0b0 | 7733 | end if; |
10303118 | 7734 | end Resolve_Op_Concat_First; |
996ae0b0 | 7735 | |
10303118 BD |
7736 | ---------------------------- |
7737 | -- Resolve_Op_Concat_Rest -- | |
7738 | ---------------------------- | |
996ae0b0 | 7739 | |
10303118 BD |
7740 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is |
7741 | Op1 : constant Node_Id := Left_Opnd (N); | |
7742 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 | 7743 | |
10303118 BD |
7744 | begin |
7745 | Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N)); | |
996ae0b0 | 7746 | |
fbf5a39b | 7747 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
7748 | |
7749 | if Is_String_Type (Typ) then | |
7750 | Eval_Concatenation (N); | |
7751 | end if; | |
7752 | ||
d81b4bfe TQ |
7753 | -- If this is not a static concatenation, but the result is a string |
7754 | -- type (and not an array of strings) ensure that static string operands | |
7755 | -- have their subtypes properly constructed. | |
996ae0b0 RK |
7756 | |
7757 | if Nkind (N) /= N_String_Literal | |
7758 | and then Is_Character_Type (Component_Type (Typ)) | |
7759 | then | |
7760 | Set_String_Literal_Subtype (Op1, Typ); | |
7761 | Set_String_Literal_Subtype (Op2, Typ); | |
7762 | end if; | |
10303118 | 7763 | end Resolve_Op_Concat_Rest; |
996ae0b0 RK |
7764 | |
7765 | ---------------------- | |
7766 | -- Resolve_Op_Expon -- | |
7767 | ---------------------- | |
7768 | ||
7769 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is | |
7770 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
7771 | ||
7772 | begin | |
f3d57416 | 7773 | -- Catch attempts to do fixed-point exponentiation with universal |
758c442c GD |
7774 | -- operands, which is a case where the illegality is not caught during |
7775 | -- normal operator analysis. | |
996ae0b0 RK |
7776 | |
7777 | if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then | |
7778 | Error_Msg_N ("exponentiation not available for fixed point", N); | |
7779 | return; | |
7780 | end if; | |
7781 | ||
fbf5a39b AC |
7782 | if Comes_From_Source (N) |
7783 | and then Ekind (Entity (N)) = E_Function | |
7784 | and then Is_Imported (Entity (N)) | |
7785 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
7786 | then | |
7787 | Resolve_Intrinsic_Operator (N, Typ); | |
7788 | return; | |
7789 | end if; | |
7790 | ||
996ae0b0 RK |
7791 | if Etype (Left_Opnd (N)) = Universal_Integer |
7792 | or else Etype (Left_Opnd (N)) = Universal_Real | |
7793 | then | |
7794 | Check_For_Visible_Operator (N, B_Typ); | |
7795 | end if; | |
7796 | ||
7797 | -- We do the resolution using the base type, because intermediate values | |
7798 | -- in expressions always are of the base type, not a subtype of it. | |
7799 | ||
7800 | Resolve (Left_Opnd (N), B_Typ); | |
7801 | Resolve (Right_Opnd (N), Standard_Integer); | |
7802 | ||
7803 | Check_Unset_Reference (Left_Opnd (N)); | |
7804 | Check_Unset_Reference (Right_Opnd (N)); | |
7805 | ||
7806 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7807 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7808 | Eval_Op_Expon (N); |
7809 | ||
7810 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
7811 | -- and perhaps the Resolve routines should be separated for the various | |
7812 | -- arithmetic operations, since they will need different processing. ??? | |
7813 | ||
7814 | if Nkind (N) in N_Op then | |
7815 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 7816 | Enable_Overflow_Check (N); |
996ae0b0 RK |
7817 | end if; |
7818 | end if; | |
996ae0b0 RK |
7819 | end Resolve_Op_Expon; |
7820 | ||
7821 | -------------------- | |
7822 | -- Resolve_Op_Not -- | |
7823 | -------------------- | |
7824 | ||
7825 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is | |
7826 | B_Typ : Entity_Id; | |
7827 | ||
7828 | function Parent_Is_Boolean return Boolean; | |
5cc9353d RD |
7829 | -- This function determines if the parent node is a boolean operator or |
7830 | -- operation (comparison op, membership test, or short circuit form) and | |
7831 | -- the not in question is the left operand of this operation. Note that | |
7832 | -- if the not is in parens, then false is returned. | |
996ae0b0 | 7833 | |
aa180613 RD |
7834 | ----------------------- |
7835 | -- Parent_Is_Boolean -- | |
7836 | ----------------------- | |
7837 | ||
996ae0b0 RK |
7838 | function Parent_Is_Boolean return Boolean is |
7839 | begin | |
7840 | if Paren_Count (N) /= 0 then | |
7841 | return False; | |
7842 | ||
7843 | else | |
7844 | case Nkind (Parent (N)) is | |
7845 | when N_Op_And | | |
7846 | N_Op_Eq | | |
7847 | N_Op_Ge | | |
7848 | N_Op_Gt | | |
7849 | N_Op_Le | | |
7850 | N_Op_Lt | | |
7851 | N_Op_Ne | | |
7852 | N_Op_Or | | |
7853 | N_Op_Xor | | |
7854 | N_In | | |
7855 | N_Not_In | | |
7856 | N_And_Then | | |
aa180613 | 7857 | N_Or_Else => |
996ae0b0 RK |
7858 | |
7859 | return Left_Opnd (Parent (N)) = N; | |
7860 | ||
7861 | when others => | |
7862 | return False; | |
7863 | end case; | |
7864 | end if; | |
7865 | end Parent_Is_Boolean; | |
7866 | ||
7867 | -- Start of processing for Resolve_Op_Not | |
7868 | ||
7869 | begin | |
758c442c GD |
7870 | -- Predefined operations on scalar types yield the base type. On the |
7871 | -- other hand, logical operations on arrays yield the type of the | |
7872 | -- arguments (and the context). | |
996ae0b0 RK |
7873 | |
7874 | if Is_Array_Type (Typ) then | |
7875 | B_Typ := Typ; | |
7876 | else | |
7877 | B_Typ := Base_Type (Typ); | |
7878 | end if; | |
7879 | ||
001c7783 AC |
7880 | if Is_VMS_Operator (Entity (N)) then |
7881 | null; | |
7882 | ||
f3d57416 | 7883 | -- Straightforward case of incorrect arguments |
aa180613 | 7884 | |
001c7783 | 7885 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
7886 | Error_Msg_N ("invalid operand type for operator&", N); |
7887 | Set_Etype (N, Any_Type); | |
7888 | return; | |
7889 | ||
aa180613 RD |
7890 | -- Special case of probable missing parens |
7891 | ||
fbf5a39b | 7892 | elsif Typ = Universal_Integer or else Typ = Any_Modular then |
996ae0b0 | 7893 | if Parent_Is_Boolean then |
ed2233dc | 7894 | Error_Msg_N |
996ae0b0 RK |
7895 | ("operand of not must be enclosed in parentheses", |
7896 | Right_Opnd (N)); | |
7897 | else | |
7898 | Error_Msg_N | |
7899 | ("no modular type available in this context", N); | |
7900 | end if; | |
7901 | ||
7902 | Set_Etype (N, Any_Type); | |
7903 | return; | |
7904 | ||
5cc9353d | 7905 | -- OK resolution of NOT |
aa180613 | 7906 | |
996ae0b0 | 7907 | else |
aa180613 RD |
7908 | -- Warn if non-boolean types involved. This is a case like not a < b |
7909 | -- where a and b are modular, where we will get (not a) < b and most | |
7910 | -- likely not (a < b) was intended. | |
7911 | ||
7912 | if Warn_On_Questionable_Missing_Parens | |
7913 | and then not Is_Boolean_Type (Typ) | |
996ae0b0 RK |
7914 | and then Parent_Is_Boolean |
7915 | then | |
ed2233dc | 7916 | Error_Msg_N ("?not expression should be parenthesized here!", N); |
996ae0b0 RK |
7917 | end if; |
7918 | ||
09bc9ab6 RD |
7919 | -- Warn on double negation if checking redundant constructs |
7920 | ||
7921 | if Warn_On_Redundant_Constructs | |
7922 | and then Comes_From_Source (N) | |
7923 | and then Comes_From_Source (Right_Opnd (N)) | |
7924 | and then Root_Type (Typ) = Standard_Boolean | |
7925 | and then Nkind (Right_Opnd (N)) = N_Op_Not | |
7926 | then | |
ed2233dc | 7927 | Error_Msg_N ("redundant double negation?", N); |
09bc9ab6 RD |
7928 | end if; |
7929 | ||
7930 | -- Complete resolution and evaluation of NOT | |
7931 | ||
996ae0b0 RK |
7932 | Resolve (Right_Opnd (N), B_Typ); |
7933 | Check_Unset_Reference (Right_Opnd (N)); | |
7934 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7935 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
7936 | Eval_Op_Not (N); |
7937 | end if; | |
7938 | end Resolve_Op_Not; | |
7939 | ||
7940 | ----------------------------- | |
7941 | -- Resolve_Operator_Symbol -- | |
7942 | ----------------------------- | |
7943 | ||
7944 | -- Nothing to be done, all resolved already | |
7945 | ||
7946 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7947 | pragma Warnings (Off, N); |
7948 | pragma Warnings (Off, Typ); | |
7949 | ||
996ae0b0 RK |
7950 | begin |
7951 | null; | |
7952 | end Resolve_Operator_Symbol; | |
7953 | ||
7954 | ---------------------------------- | |
7955 | -- Resolve_Qualified_Expression -- | |
7956 | ---------------------------------- | |
7957 | ||
7958 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7959 | pragma Warnings (Off, Typ); |
7960 | ||
996ae0b0 RK |
7961 | Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N)); |
7962 | Expr : constant Node_Id := Expression (N); | |
7963 | ||
7964 | begin | |
7965 | Resolve (Expr, Target_Typ); | |
7966 | ||
fe5d3068 | 7967 | if Is_Array_Type (Target_Typ) |
b0186f71 AC |
7968 | and then Is_Array_Type (Etype (Expr)) |
7969 | and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr)) | |
7970 | then | |
fe5d3068 YM |
7971 | Check_Formal_Restriction |
7972 | ("array types should have matching static bounds", N); | |
b0186f71 AC |
7973 | end if; |
7974 | ||
5cc9353d RD |
7975 | -- A qualified expression requires an exact match of the type, class- |
7976 | -- wide matching is not allowed. However, if the qualifying type is | |
7977 | -- specific and the expression has a class-wide type, it may still be | |
7978 | -- okay, since it can be the result of the expansion of a call to a | |
7979 | -- dispatching function, so we also have to check class-wideness of the | |
7980 | -- type of the expression's original node. | |
1420b484 JM |
7981 | |
7982 | if (Is_Class_Wide_Type (Target_Typ) | |
7983 | or else | |
7984 | (Is_Class_Wide_Type (Etype (Expr)) | |
7985 | and then Is_Class_Wide_Type (Etype (Original_Node (Expr))))) | |
996ae0b0 RK |
7986 | and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ) |
7987 | then | |
7988 | Wrong_Type (Expr, Target_Typ); | |
7989 | end if; | |
7990 | ||
90c63b09 AC |
7991 | -- If the target type is unconstrained, then we reset the type of the |
7992 | -- result from the type of the expression. For other cases, the actual | |
7993 | -- subtype of the expression is the target type. | |
996ae0b0 RK |
7994 | |
7995 | if Is_Composite_Type (Target_Typ) | |
7996 | and then not Is_Constrained (Target_Typ) | |
7997 | then | |
7998 | Set_Etype (N, Etype (Expr)); | |
7999 | end if; | |
8000 | ||
8001 | Eval_Qualified_Expression (N); | |
8002 | end Resolve_Qualified_Expression; | |
8003 | ||
a961aa79 AC |
8004 | ----------------------------------- |
8005 | -- Resolve_Quantified_Expression -- | |
8006 | ----------------------------------- | |
8007 | ||
8008 | procedure Resolve_Quantified_Expression (N : Node_Id; Typ : Entity_Id) is | |
8009 | begin | |
8010 | -- The loop structure is already resolved during its analysis, only the | |
0592046e AC |
8011 | -- resolution of the condition needs to be done. Expansion is disabled |
8012 | -- so that checks and other generated code are inserted in the tree | |
8013 | -- after expression has been rewritten as a loop. | |
a961aa79 | 8014 | |
0592046e | 8015 | Expander_Mode_Save_And_Set (False); |
a961aa79 | 8016 | Resolve (Condition (N), Typ); |
0592046e | 8017 | Expander_Mode_Restore; |
a961aa79 AC |
8018 | end Resolve_Quantified_Expression; |
8019 | ||
996ae0b0 RK |
8020 | ------------------- |
8021 | -- Resolve_Range -- | |
8022 | ------------------- | |
8023 | ||
8024 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is | |
8025 | L : constant Node_Id := Low_Bound (N); | |
8026 | H : constant Node_Id := High_Bound (N); | |
8027 | ||
bd29d519 AC |
8028 | function First_Last_Ref return Boolean; |
8029 | -- Returns True if N is of the form X'First .. X'Last where X is the | |
8030 | -- same entity for both attributes. | |
8031 | ||
8032 | -------------------- | |
8033 | -- First_Last_Ref -- | |
8034 | -------------------- | |
8035 | ||
8036 | function First_Last_Ref return Boolean is | |
8037 | Lorig : constant Node_Id := Original_Node (L); | |
8038 | Horig : constant Node_Id := Original_Node (H); | |
8039 | ||
8040 | begin | |
8041 | if Nkind (Lorig) = N_Attribute_Reference | |
8042 | and then Nkind (Horig) = N_Attribute_Reference | |
8043 | and then Attribute_Name (Lorig) = Name_First | |
8044 | and then Attribute_Name (Horig) = Name_Last | |
8045 | then | |
8046 | declare | |
8047 | PL : constant Node_Id := Prefix (Lorig); | |
8048 | PH : constant Node_Id := Prefix (Horig); | |
8049 | begin | |
8050 | if Is_Entity_Name (PL) | |
8051 | and then Is_Entity_Name (PH) | |
8052 | and then Entity (PL) = Entity (PH) | |
8053 | then | |
8054 | return True; | |
8055 | end if; | |
8056 | end; | |
8057 | end if; | |
8058 | ||
8059 | return False; | |
8060 | end First_Last_Ref; | |
8061 | ||
8062 | -- Start of processing for Resolve_Range | |
8063 | ||
996ae0b0 RK |
8064 | begin |
8065 | Set_Etype (N, Typ); | |
8066 | Resolve (L, Typ); | |
8067 | Resolve (H, Typ); | |
8068 | ||
bd29d519 AC |
8069 | -- Check for inappropriate range on unordered enumeration type |
8070 | ||
8071 | if Bad_Unordered_Enumeration_Reference (N, Typ) | |
8072 | ||
8073 | -- Exclude X'First .. X'Last if X is the same entity for both | |
8074 | ||
8075 | and then not First_Last_Ref | |
8076 | then | |
8077 | Error_Msg ("subrange of unordered enumeration type?", Sloc (N)); | |
498d1b80 AC |
8078 | end if; |
8079 | ||
996ae0b0 RK |
8080 | Check_Unset_Reference (L); |
8081 | Check_Unset_Reference (H); | |
8082 | ||
8083 | -- We have to check the bounds for being within the base range as | |
758c442c GD |
8084 | -- required for a non-static context. Normally this is automatic and |
8085 | -- done as part of evaluating expressions, but the N_Range node is an | |
8086 | -- exception, since in GNAT we consider this node to be a subexpression, | |
8087 | -- even though in Ada it is not. The circuit in Sem_Eval could check for | |
8088 | -- this, but that would put the test on the main evaluation path for | |
8089 | -- expressions. | |
996ae0b0 RK |
8090 | |
8091 | Check_Non_Static_Context (L); | |
8092 | Check_Non_Static_Context (H); | |
8093 | ||
b7d1f17f HK |
8094 | -- Check for an ambiguous range over character literals. This will |
8095 | -- happen with a membership test involving only literals. | |
8096 | ||
8097 | if Typ = Any_Character then | |
8098 | Ambiguous_Character (L); | |
8099 | Set_Etype (N, Any_Type); | |
8100 | return; | |
8101 | end if; | |
8102 | ||
5cc9353d RD |
8103 | -- If bounds are static, constant-fold them, so size computations are |
8104 | -- identical between front-end and back-end. Do not perform this | |
fbf5a39b | 8105 | -- transformation while analyzing generic units, as type information |
5cc9353d | 8106 | -- would be lost when reanalyzing the constant node in the instance. |
fbf5a39b AC |
8107 | |
8108 | if Is_Discrete_Type (Typ) and then Expander_Active then | |
8109 | if Is_OK_Static_Expression (L) then | |
8110 | Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L)); | |
8111 | end if; | |
8112 | ||
8113 | if Is_OK_Static_Expression (H) then | |
8114 | Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H)); | |
8115 | end if; | |
8116 | end if; | |
996ae0b0 RK |
8117 | end Resolve_Range; |
8118 | ||
8119 | -------------------------- | |
8120 | -- Resolve_Real_Literal -- | |
8121 | -------------------------- | |
8122 | ||
8123 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is | |
8124 | Actual_Typ : constant Entity_Id := Etype (N); | |
8125 | ||
8126 | begin | |
8127 | -- Special processing for fixed-point literals to make sure that the | |
5cc9353d RD |
8128 | -- value is an exact multiple of small where this is required. We skip |
8129 | -- this for the universal real case, and also for generic types. | |
996ae0b0 RK |
8130 | |
8131 | if Is_Fixed_Point_Type (Typ) | |
8132 | and then Typ /= Universal_Fixed | |
8133 | and then Typ /= Any_Fixed | |
8134 | and then not Is_Generic_Type (Typ) | |
8135 | then | |
8136 | declare | |
8137 | Val : constant Ureal := Realval (N); | |
8138 | Cintr : constant Ureal := Val / Small_Value (Typ); | |
8139 | Cint : constant Uint := UR_Trunc (Cintr); | |
8140 | Den : constant Uint := Norm_Den (Cintr); | |
8141 | Stat : Boolean; | |
8142 | ||
8143 | begin | |
8144 | -- Case of literal is not an exact multiple of the Small | |
8145 | ||
8146 | if Den /= 1 then | |
8147 | ||
5cc9353d RD |
8148 | -- For a source program literal for a decimal fixed-point type, |
8149 | -- this is statically illegal (RM 4.9(36)). | |
996ae0b0 RK |
8150 | |
8151 | if Is_Decimal_Fixed_Point_Type (Typ) | |
8152 | and then Actual_Typ = Universal_Real | |
8153 | and then Comes_From_Source (N) | |
8154 | then | |
8155 | Error_Msg_N ("value has extraneous low order digits", N); | |
8156 | end if; | |
8157 | ||
bc5f3720 RD |
8158 | -- Generate a warning if literal from source |
8159 | ||
8160 | if Is_Static_Expression (N) | |
8161 | and then Warn_On_Bad_Fixed_Value | |
8162 | then | |
8163 | Error_Msg_N | |
aa5147f0 | 8164 | ("?static fixed-point value is not a multiple of Small!", |
bc5f3720 RD |
8165 | N); |
8166 | end if; | |
8167 | ||
996ae0b0 RK |
8168 | -- Replace literal by a value that is the exact representation |
8169 | -- of a value of the type, i.e. a multiple of the small value, | |
8170 | -- by truncation, since Machine_Rounds is false for all GNAT | |
8171 | -- fixed-point types (RM 4.9(38)). | |
8172 | ||
8173 | Stat := Is_Static_Expression (N); | |
8174 | Rewrite (N, | |
8175 | Make_Real_Literal (Sloc (N), | |
8176 | Realval => Small_Value (Typ) * Cint)); | |
8177 | ||
8178 | Set_Is_Static_Expression (N, Stat); | |
8179 | end if; | |
8180 | ||
8181 | -- In all cases, set the corresponding integer field | |
8182 | ||
8183 | Set_Corresponding_Integer_Value (N, Cint); | |
8184 | end; | |
8185 | end if; | |
8186 | ||
8187 | -- Now replace the actual type by the expected type as usual | |
8188 | ||
8189 | Set_Etype (N, Typ); | |
8190 | Eval_Real_Literal (N); | |
8191 | end Resolve_Real_Literal; | |
8192 | ||
8193 | ----------------------- | |
8194 | -- Resolve_Reference -- | |
8195 | ----------------------- | |
8196 | ||
8197 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is | |
8198 | P : constant Node_Id := Prefix (N); | |
8199 | ||
8200 | begin | |
8201 | -- Replace general access with specific type | |
8202 | ||
8203 | if Ekind (Etype (N)) = E_Allocator_Type then | |
8204 | Set_Etype (N, Base_Type (Typ)); | |
8205 | end if; | |
8206 | ||
8207 | Resolve (P, Designated_Type (Etype (N))); | |
8208 | ||
5cc9353d RD |
8209 | -- If we are taking the reference of a volatile entity, then treat it as |
8210 | -- a potential modification of this entity. This is too conservative, | |
8211 | -- but necessary because remove side effects can cause transformations | |
8212 | -- of normal assignments into reference sequences that otherwise fail to | |
8213 | -- notice the modification. | |
996ae0b0 | 8214 | |
fbf5a39b | 8215 | if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then |
45fc7ddb | 8216 | Note_Possible_Modification (P, Sure => False); |
996ae0b0 RK |
8217 | end if; |
8218 | end Resolve_Reference; | |
8219 | ||
8220 | -------------------------------- | |
8221 | -- Resolve_Selected_Component -- | |
8222 | -------------------------------- | |
8223 | ||
8224 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is | |
8225 | Comp : Entity_Id; | |
8226 | Comp1 : Entity_Id := Empty; -- prevent junk warning | |
8227 | P : constant Node_Id := Prefix (N); | |
8228 | S : constant Node_Id := Selector_Name (N); | |
8229 | T : Entity_Id := Etype (P); | |
8230 | I : Interp_Index; | |
8231 | I1 : Interp_Index := 0; -- prevent junk warning | |
8232 | It : Interp; | |
8233 | It1 : Interp; | |
8234 | Found : Boolean; | |
8235 | ||
6510f4c9 GB |
8236 | function Init_Component return Boolean; |
8237 | -- Check whether this is the initialization of a component within an | |
fbf5a39b | 8238 | -- init proc (by assignment or call to another init proc). If true, |
6510f4c9 GB |
8239 | -- there is no need for a discriminant check. |
8240 | ||
8241 | -------------------- | |
8242 | -- Init_Component -- | |
8243 | -------------------- | |
8244 | ||
8245 | function Init_Component return Boolean is | |
8246 | begin | |
8247 | return Inside_Init_Proc | |
8248 | and then Nkind (Prefix (N)) = N_Identifier | |
8249 | and then Chars (Prefix (N)) = Name_uInit | |
8250 | and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative; | |
8251 | end Init_Component; | |
8252 | ||
8253 | -- Start of processing for Resolve_Selected_Component | |
8254 | ||
996ae0b0 RK |
8255 | begin |
8256 | if Is_Overloaded (P) then | |
8257 | ||
8258 | -- Use the context type to select the prefix that has a selector | |
8259 | -- of the correct name and type. | |
8260 | ||
8261 | Found := False; | |
8262 | Get_First_Interp (P, I, It); | |
8263 | ||
8264 | Search : while Present (It.Typ) loop | |
8265 | if Is_Access_Type (It.Typ) then | |
8266 | T := Designated_Type (It.Typ); | |
8267 | else | |
8268 | T := It.Typ; | |
8269 | end if; | |
8270 | ||
8271 | if Is_Record_Type (T) then | |
36fcf362 RD |
8272 | |
8273 | -- The visible components of a class-wide type are those of | |
8274 | -- the root type. | |
8275 | ||
8276 | if Is_Class_Wide_Type (T) then | |
8277 | T := Etype (T); | |
8278 | end if; | |
8279 | ||
996ae0b0 | 8280 | Comp := First_Entity (T); |
996ae0b0 | 8281 | while Present (Comp) loop |
996ae0b0 RK |
8282 | if Chars (Comp) = Chars (S) |
8283 | and then Covers (Etype (Comp), Typ) | |
8284 | then | |
8285 | if not Found then | |
8286 | Found := True; | |
8287 | I1 := I; | |
8288 | It1 := It; | |
8289 | Comp1 := Comp; | |
8290 | ||
8291 | else | |
8292 | It := Disambiguate (P, I1, I, Any_Type); | |
8293 | ||
8294 | if It = No_Interp then | |
8295 | Error_Msg_N | |
8296 | ("ambiguous prefix for selected component", N); | |
8297 | Set_Etype (N, Typ); | |
8298 | return; | |
8299 | ||
8300 | else | |
8301 | It1 := It; | |
8302 | ||
c8ef728f ES |
8303 | -- There may be an implicit dereference. Retrieve |
8304 | -- designated record type. | |
8305 | ||
8306 | if Is_Access_Type (It1.Typ) then | |
8307 | T := Designated_Type (It1.Typ); | |
8308 | else | |
8309 | T := It1.Typ; | |
8310 | end if; | |
8311 | ||
8312 | if Scope (Comp1) /= T then | |
996ae0b0 RK |
8313 | |
8314 | -- Resolution chooses the new interpretation. | |
8315 | -- Find the component with the right name. | |
8316 | ||
c8ef728f | 8317 | Comp1 := First_Entity (T); |
996ae0b0 RK |
8318 | while Present (Comp1) |
8319 | and then Chars (Comp1) /= Chars (S) | |
8320 | loop | |
8321 | Comp1 := Next_Entity (Comp1); | |
8322 | end loop; | |
8323 | end if; | |
8324 | ||
8325 | exit Search; | |
8326 | end if; | |
8327 | end if; | |
8328 | end if; | |
8329 | ||
8330 | Comp := Next_Entity (Comp); | |
8331 | end loop; | |
996ae0b0 RK |
8332 | end if; |
8333 | ||
8334 | Get_Next_Interp (I, It); | |
996ae0b0 RK |
8335 | end loop Search; |
8336 | ||
8337 | Resolve (P, It1.Typ); | |
8338 | Set_Etype (N, Typ); | |
aa180613 | 8339 | Set_Entity_With_Style_Check (S, Comp1); |
996ae0b0 RK |
8340 | |
8341 | else | |
fbf5a39b | 8342 | -- Resolve prefix with its type |
996ae0b0 RK |
8343 | |
8344 | Resolve (P, T); | |
8345 | end if; | |
8346 | ||
aa180613 RD |
8347 | -- Generate cross-reference. We needed to wait until full overloading |
8348 | -- resolution was complete to do this, since otherwise we can't tell if | |
01e17342 | 8349 | -- we are an lvalue or not. |
aa180613 RD |
8350 | |
8351 | if May_Be_Lvalue (N) then | |
8352 | Generate_Reference (Entity (S), S, 'm'); | |
8353 | else | |
8354 | Generate_Reference (Entity (S), S, 'r'); | |
8355 | end if; | |
8356 | ||
c8ef728f ES |
8357 | -- If prefix is an access type, the node will be transformed into an |
8358 | -- explicit dereference during expansion. The type of the node is the | |
8359 | -- designated type of that of the prefix. | |
996ae0b0 RK |
8360 | |
8361 | if Is_Access_Type (Etype (P)) then | |
996ae0b0 | 8362 | T := Designated_Type (Etype (P)); |
c8ef728f | 8363 | Check_Fully_Declared_Prefix (T, P); |
996ae0b0 RK |
8364 | else |
8365 | T := Etype (P); | |
8366 | end if; | |
8367 | ||
8368 | if Has_Discriminants (T) | |
964f13da | 8369 | and then Ekind_In (Entity (S), E_Component, E_Discriminant) |
996ae0b0 RK |
8370 | and then Present (Original_Record_Component (Entity (S))) |
8371 | and then Ekind (Original_Record_Component (Entity (S))) = E_Component | |
8372 | and then Present (Discriminant_Checking_Func | |
8373 | (Original_Record_Component (Entity (S)))) | |
8374 | and then not Discriminant_Checks_Suppressed (T) | |
6510f4c9 | 8375 | and then not Init_Component |
996ae0b0 RK |
8376 | then |
8377 | Set_Do_Discriminant_Check (N); | |
8378 | end if; | |
8379 | ||
8380 | if Ekind (Entity (S)) = E_Void then | |
8381 | Error_Msg_N ("premature use of component", S); | |
8382 | end if; | |
8383 | ||
8384 | -- If the prefix is a record conversion, this may be a renamed | |
8385 | -- discriminant whose bounds differ from those of the original | |
8386 | -- one, so we must ensure that a range check is performed. | |
8387 | ||
8388 | if Nkind (P) = N_Type_Conversion | |
8389 | and then Ekind (Entity (S)) = E_Discriminant | |
fbf5a39b | 8390 | and then Is_Discrete_Type (Typ) |
996ae0b0 RK |
8391 | then |
8392 | Set_Etype (N, Base_Type (Typ)); | |
8393 | end if; | |
8394 | ||
8395 | -- Note: No Eval processing is required, because the prefix is of a | |
8396 | -- record type, or protected type, and neither can possibly be static. | |
8397 | ||
c28408b7 RD |
8398 | -- If the array type is atomic, and is packed, and we are in a left side |
8399 | -- context, then this is worth a warning, since we have a situation | |
5cc9353d RD |
8400 | -- where the access to the component may cause extra read/writes of the |
8401 | -- atomic array object, which could be considered unexpected. | |
c28408b7 RD |
8402 | |
8403 | if Nkind (N) = N_Selected_Component | |
8404 | and then (Is_Atomic (T) | |
8405 | or else (Is_Entity_Name (Prefix (N)) | |
8406 | and then Is_Atomic (Entity (Prefix (N))))) | |
8407 | and then Is_Packed (T) | |
8408 | and then Is_LHS (N) | |
8409 | then | |
8410 | Error_Msg_N ("?assignment to component of packed atomic record", | |
8411 | Prefix (N)); | |
8412 | Error_Msg_N ("?\may cause unexpected accesses to atomic object", | |
8413 | Prefix (N)); | |
8414 | end if; | |
996ae0b0 RK |
8415 | end Resolve_Selected_Component; |
8416 | ||
8417 | ------------------- | |
8418 | -- Resolve_Shift -- | |
8419 | ------------------- | |
8420 | ||
8421 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is | |
8422 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8423 | L : constant Node_Id := Left_Opnd (N); | |
8424 | R : constant Node_Id := Right_Opnd (N); | |
8425 | ||
8426 | begin | |
8427 | -- We do the resolution using the base type, because intermediate values | |
8428 | -- in expressions always are of the base type, not a subtype of it. | |
8429 | ||
8430 | Resolve (L, B_Typ); | |
8431 | Resolve (R, Standard_Natural); | |
8432 | ||
8433 | Check_Unset_Reference (L); | |
8434 | Check_Unset_Reference (R); | |
8435 | ||
8436 | Set_Etype (N, B_Typ); | |
fbf5a39b | 8437 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
8438 | Eval_Shift (N); |
8439 | end Resolve_Shift; | |
8440 | ||
8441 | --------------------------- | |
8442 | -- Resolve_Short_Circuit -- | |
8443 | --------------------------- | |
8444 | ||
8445 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is | |
8446 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8447 | L : constant Node_Id := Left_Opnd (N); | |
8448 | R : constant Node_Id := Right_Opnd (N); | |
8449 | ||
8450 | begin | |
8451 | Resolve (L, B_Typ); | |
8452 | Resolve (R, B_Typ); | |
8453 | ||
45fc7ddb HK |
8454 | -- Check for issuing warning for always False assert/check, this happens |
8455 | -- when assertions are turned off, in which case the pragma Assert/Check | |
36fcf362 RD |
8456 | -- was transformed into: |
8457 | ||
8458 | -- if False and then <condition> then ... | |
8459 | ||
8460 | -- and we detect this pattern | |
8461 | ||
8462 | if Warn_On_Assertion_Failure | |
8463 | and then Is_Entity_Name (R) | |
8464 | and then Entity (R) = Standard_False | |
8465 | and then Nkind (Parent (N)) = N_If_Statement | |
8466 | and then Nkind (N) = N_And_Then | |
8467 | and then Is_Entity_Name (L) | |
8468 | and then Entity (L) = Standard_False | |
8469 | then | |
8470 | declare | |
8471 | Orig : constant Node_Id := Original_Node (Parent (N)); | |
45fc7ddb | 8472 | |
36fcf362 RD |
8473 | begin |
8474 | if Nkind (Orig) = N_Pragma | |
26570b21 | 8475 | and then Pragma_Name (Orig) = Name_Assert |
36fcf362 RD |
8476 | then |
8477 | -- Don't want to warn if original condition is explicit False | |
8478 | ||
8479 | declare | |
8480 | Expr : constant Node_Id := | |
8481 | Original_Node | |
8482 | (Expression | |
8483 | (First (Pragma_Argument_Associations (Orig)))); | |
8484 | begin | |
8485 | if Is_Entity_Name (Expr) | |
8486 | and then Entity (Expr) = Standard_False | |
8487 | then | |
8488 | null; | |
8489 | else | |
51bf9bdf AC |
8490 | -- Issue warning. We do not want the deletion of the |
8491 | -- IF/AND-THEN to take this message with it. We achieve | |
8492 | -- this by making sure that the expanded code points to | |
8493 | -- the Sloc of the expression, not the original pragma. | |
8494 | ||
8495 | Error_Msg_N | |
e7c0dd39 | 8496 | ("?assertion would fail at run time!", |
51bf9bdf AC |
8497 | Expression |
8498 | (First (Pragma_Argument_Associations (Orig)))); | |
36fcf362 RD |
8499 | end if; |
8500 | end; | |
45fc7ddb HK |
8501 | |
8502 | -- Similar processing for Check pragma | |
8503 | ||
8504 | elsif Nkind (Orig) = N_Pragma | |
8505 | and then Pragma_Name (Orig) = Name_Check | |
8506 | then | |
8507 | -- Don't want to warn if original condition is explicit False | |
8508 | ||
8509 | declare | |
8510 | Expr : constant Node_Id := | |
8511 | Original_Node | |
8512 | (Expression | |
8513 | (Next (First | |
8514 | (Pragma_Argument_Associations (Orig))))); | |
8515 | begin | |
8516 | if Is_Entity_Name (Expr) | |
8517 | and then Entity (Expr) = Standard_False | |
8518 | then | |
8519 | null; | |
8520 | else | |
51bf9bdf | 8521 | Error_Msg_N |
e7c0dd39 | 8522 | ("?check would fail at run time!", |
51bf9bdf AC |
8523 | Expression |
8524 | (Last (Pragma_Argument_Associations (Orig)))); | |
45fc7ddb HK |
8525 | end if; |
8526 | end; | |
36fcf362 RD |
8527 | end if; |
8528 | end; | |
8529 | end if; | |
8530 | ||
8531 | -- Continue with processing of short circuit | |
8532 | ||
996ae0b0 RK |
8533 | Check_Unset_Reference (L); |
8534 | Check_Unset_Reference (R); | |
8535 | ||
8536 | Set_Etype (N, B_Typ); | |
8537 | Eval_Short_Circuit (N); | |
8538 | end Resolve_Short_Circuit; | |
8539 | ||
8540 | ------------------- | |
8541 | -- Resolve_Slice -- | |
8542 | ------------------- | |
8543 | ||
8544 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is | |
8545 | Name : constant Node_Id := Prefix (N); | |
8546 | Drange : constant Node_Id := Discrete_Range (N); | |
8547 | Array_Type : Entity_Id := Empty; | |
8548 | Index : Node_Id; | |
8549 | ||
8550 | begin | |
8551 | if Is_Overloaded (Name) then | |
8552 | ||
d81b4bfe TQ |
8553 | -- Use the context type to select the prefix that yields the correct |
8554 | -- array type. | |
996ae0b0 RK |
8555 | |
8556 | declare | |
8557 | I : Interp_Index; | |
8558 | I1 : Interp_Index := 0; | |
8559 | It : Interp; | |
8560 | P : constant Node_Id := Prefix (N); | |
8561 | Found : Boolean := False; | |
8562 | ||
8563 | begin | |
8564 | Get_First_Interp (P, I, It); | |
996ae0b0 | 8565 | while Present (It.Typ) loop |
996ae0b0 RK |
8566 | if (Is_Array_Type (It.Typ) |
8567 | and then Covers (Typ, It.Typ)) | |
8568 | or else (Is_Access_Type (It.Typ) | |
8569 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
8570 | and then Covers (Typ, Designated_Type (It.Typ))) | |
8571 | then | |
8572 | if Found then | |
8573 | It := Disambiguate (P, I1, I, Any_Type); | |
8574 | ||
8575 | if It = No_Interp then | |
8576 | Error_Msg_N ("ambiguous prefix for slicing", N); | |
8577 | Set_Etype (N, Typ); | |
8578 | return; | |
8579 | else | |
8580 | Found := True; | |
8581 | Array_Type := It.Typ; | |
8582 | I1 := I; | |
8583 | end if; | |
8584 | else | |
8585 | Found := True; | |
8586 | Array_Type := It.Typ; | |
8587 | I1 := I; | |
8588 | end if; | |
8589 | end if; | |
8590 | ||
8591 | Get_Next_Interp (I, It); | |
8592 | end loop; | |
8593 | end; | |
8594 | ||
8595 | else | |
8596 | Array_Type := Etype (Name); | |
8597 | end if; | |
8598 | ||
8599 | Resolve (Name, Array_Type); | |
8600 | ||
8601 | if Is_Access_Type (Array_Type) then | |
8602 | Apply_Access_Check (N); | |
8603 | Array_Type := Designated_Type (Array_Type); | |
8604 | ||
c8ef728f ES |
8605 | -- If the prefix is an access to an unconstrained array, we must use |
8606 | -- the actual subtype of the object to perform the index checks. The | |
8607 | -- object denoted by the prefix is implicit in the node, so we build | |
8608 | -- an explicit representation for it in order to compute the actual | |
8609 | -- subtype. | |
82c80734 RD |
8610 | |
8611 | if not Is_Constrained (Array_Type) then | |
8612 | Remove_Side_Effects (Prefix (N)); | |
8613 | ||
8614 | declare | |
8615 | Obj : constant Node_Id := | |
8616 | Make_Explicit_Dereference (Sloc (N), | |
8617 | Prefix => New_Copy_Tree (Prefix (N))); | |
8618 | begin | |
8619 | Set_Etype (Obj, Array_Type); | |
8620 | Set_Parent (Obj, Parent (N)); | |
8621 | Array_Type := Get_Actual_Subtype (Obj); | |
8622 | end; | |
8623 | end if; | |
8624 | ||
996ae0b0 | 8625 | elsif Is_Entity_Name (Name) |
6c994759 | 8626 | or else Nkind (Name) = N_Explicit_Dereference |
996ae0b0 RK |
8627 | or else (Nkind (Name) = N_Function_Call |
8628 | and then not Is_Constrained (Etype (Name))) | |
8629 | then | |
8630 | Array_Type := Get_Actual_Subtype (Name); | |
aa5147f0 ES |
8631 | |
8632 | -- If the name is a selected component that depends on discriminants, | |
8633 | -- build an actual subtype for it. This can happen only when the name | |
8634 | -- itself is overloaded; otherwise the actual subtype is created when | |
8635 | -- the selected component is analyzed. | |
8636 | ||
8637 | elsif Nkind (Name) = N_Selected_Component | |
8638 | and then Full_Analysis | |
8639 | and then Depends_On_Discriminant (First_Index (Array_Type)) | |
8640 | then | |
8641 | declare | |
8642 | Act_Decl : constant Node_Id := | |
8643 | Build_Actual_Subtype_Of_Component (Array_Type, Name); | |
8644 | begin | |
8645 | Insert_Action (N, Act_Decl); | |
8646 | Array_Type := Defining_Identifier (Act_Decl); | |
8647 | end; | |
d79e621a GD |
8648 | |
8649 | -- Maybe this should just be "else", instead of checking for the | |
5cc9353d RD |
8650 | -- specific case of slice??? This is needed for the case where the |
8651 | -- prefix is an Image attribute, which gets expanded to a slice, and so | |
8652 | -- has a constrained subtype which we want to use for the slice range | |
8653 | -- check applied below (the range check won't get done if the | |
8654 | -- unconstrained subtype of the 'Image is used). | |
d79e621a GD |
8655 | |
8656 | elsif Nkind (Name) = N_Slice then | |
8657 | Array_Type := Etype (Name); | |
996ae0b0 RK |
8658 | end if; |
8659 | ||
8660 | -- If name was overloaded, set slice type correctly now | |
8661 | ||
8662 | Set_Etype (N, Array_Type); | |
8663 | ||
c8ef728f ES |
8664 | -- If the range is specified by a subtype mark, no resolution is |
8665 | -- necessary. Else resolve the bounds, and apply needed checks. | |
996ae0b0 RK |
8666 | |
8667 | if not Is_Entity_Name (Drange) then | |
8668 | Index := First_Index (Array_Type); | |
8669 | Resolve (Drange, Base_Type (Etype (Index))); | |
8670 | ||
dbe945f1 AC |
8671 | if Nkind (Drange) = N_Range then |
8672 | ||
8673 | -- Ensure that side effects in the bounds are properly handled | |
8674 | ||
cae81f17 JM |
8675 | Force_Evaluation (Low_Bound (Drange)); |
8676 | Force_Evaluation (High_Bound (Drange)); | |
0669bebe GB |
8677 | |
8678 | -- Do not apply the range check to nodes associated with the | |
8679 | -- frontend expansion of the dispatch table. We first check | |
dbe945f1 | 8680 | -- if Ada.Tags is already loaded to avoid the addition of an |
0669bebe GB |
8681 | -- undesired dependence on such run-time unit. |
8682 | ||
dbe945f1 AC |
8683 | if not Tagged_Type_Expansion |
8684 | or else not | |
8685 | (RTU_Loaded (Ada_Tags) | |
cead616d AC |
8686 | and then Nkind (Prefix (N)) = N_Selected_Component |
8687 | and then Present (Entity (Selector_Name (Prefix (N)))) | |
8688 | and then Entity (Selector_Name (Prefix (N))) = | |
dbe945f1 AC |
8689 | RTE_Record_Component (RE_Prims_Ptr)) |
8690 | then | |
8691 | Apply_Range_Check (Drange, Etype (Index)); | |
8692 | end if; | |
996ae0b0 RK |
8693 | end if; |
8694 | end if; | |
8695 | ||
8696 | Set_Slice_Subtype (N); | |
aa180613 | 8697 | |
ea034236 AC |
8698 | -- Check bad use of type with predicates |
8699 | ||
8700 | if Has_Predicates (Etype (Drange)) then | |
ed00f472 | 8701 | Bad_Predicated_Subtype_Use |
ea034236 AC |
8702 | ("subtype& has predicate, not allowed in slice", |
8703 | Drange, Etype (Drange)); | |
8704 | ||
8705 | -- Otherwise here is where we check suspicious indexes | |
8706 | ||
8707 | elsif Nkind (Drange) = N_Range then | |
aa180613 RD |
8708 | Warn_On_Suspicious_Index (Name, Low_Bound (Drange)); |
8709 | Warn_On_Suspicious_Index (Name, High_Bound (Drange)); | |
8710 | end if; | |
8711 | ||
996ae0b0 | 8712 | Eval_Slice (N); |
996ae0b0 RK |
8713 | end Resolve_Slice; |
8714 | ||
8715 | ---------------------------- | |
8716 | -- Resolve_String_Literal -- | |
8717 | ---------------------------- | |
8718 | ||
8719 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is | |
8720 | C_Typ : constant Entity_Id := Component_Type (Typ); | |
8721 | R_Typ : constant Entity_Id := Root_Type (C_Typ); | |
8722 | Loc : constant Source_Ptr := Sloc (N); | |
8723 | Str : constant String_Id := Strval (N); | |
8724 | Strlen : constant Nat := String_Length (Str); | |
8725 | Subtype_Id : Entity_Id; | |
8726 | Need_Check : Boolean; | |
8727 | ||
8728 | begin | |
8729 | -- For a string appearing in a concatenation, defer creation of the | |
8730 | -- string_literal_subtype until the end of the resolution of the | |
c8ef728f ES |
8731 | -- concatenation, because the literal may be constant-folded away. This |
8732 | -- is a useful optimization for long concatenation expressions. | |
996ae0b0 | 8733 | |
c8ef728f | 8734 | -- If the string is an aggregate built for a single character (which |
996ae0b0 | 8735 | -- happens in a non-static context) or a is null string to which special |
c8ef728f ES |
8736 | -- checks may apply, we build the subtype. Wide strings must also get a |
8737 | -- string subtype if they come from a one character aggregate. Strings | |
996ae0b0 RK |
8738 | -- generated by attributes might be static, but it is often hard to |
8739 | -- determine whether the enclosing context is static, so we generate | |
8740 | -- subtypes for them as well, thus losing some rarer optimizations ??? | |
8741 | -- Same for strings that come from a static conversion. | |
8742 | ||
8743 | Need_Check := | |
8744 | (Strlen = 0 and then Typ /= Standard_String) | |
8745 | or else Nkind (Parent (N)) /= N_Op_Concat | |
8746 | or else (N /= Left_Opnd (Parent (N)) | |
8747 | and then N /= Right_Opnd (Parent (N))) | |
82c80734 RD |
8748 | or else ((Typ = Standard_Wide_String |
8749 | or else Typ = Standard_Wide_Wide_String) | |
996ae0b0 RK |
8750 | and then Nkind (Original_Node (N)) /= N_String_Literal); |
8751 | ||
d81b4bfe TQ |
8752 | -- If the resolving type is itself a string literal subtype, we can just |
8753 | -- reuse it, since there is no point in creating another. | |
996ae0b0 RK |
8754 | |
8755 | if Ekind (Typ) = E_String_Literal_Subtype then | |
8756 | Subtype_Id := Typ; | |
8757 | ||
8758 | elsif Nkind (Parent (N)) = N_Op_Concat | |
8759 | and then not Need_Check | |
45fc7ddb HK |
8760 | and then not Nkind_In (Original_Node (N), N_Character_Literal, |
8761 | N_Attribute_Reference, | |
8762 | N_Qualified_Expression, | |
8763 | N_Type_Conversion) | |
996ae0b0 RK |
8764 | then |
8765 | Subtype_Id := Typ; | |
8766 | ||
8767 | -- Otherwise we must create a string literal subtype. Note that the | |
8768 | -- whole idea of string literal subtypes is simply to avoid the need | |
8769 | -- for building a full fledged array subtype for each literal. | |
45fc7ddb | 8770 | |
996ae0b0 RK |
8771 | else |
8772 | Set_String_Literal_Subtype (N, Typ); | |
8773 | Subtype_Id := Etype (N); | |
8774 | end if; | |
8775 | ||
8776 | if Nkind (Parent (N)) /= N_Op_Concat | |
8777 | or else Need_Check | |
8778 | then | |
8779 | Set_Etype (N, Subtype_Id); | |
8780 | Eval_String_Literal (N); | |
8781 | end if; | |
8782 | ||
8783 | if Is_Limited_Composite (Typ) | |
8784 | or else Is_Private_Composite (Typ) | |
8785 | then | |
8786 | Error_Msg_N ("string literal not available for private array", N); | |
8787 | Set_Etype (N, Any_Type); | |
8788 | return; | |
8789 | end if; | |
8790 | ||
d81b4bfe TQ |
8791 | -- The validity of a null string has been checked in the call to |
8792 | -- Eval_String_Literal. | |
996ae0b0 RK |
8793 | |
8794 | if Strlen = 0 then | |
8795 | return; | |
8796 | ||
c8ef728f ES |
8797 | -- Always accept string literal with component type Any_Character, which |
8798 | -- occurs in error situations and in comparisons of literals, both of | |
8799 | -- which should accept all literals. | |
996ae0b0 RK |
8800 | |
8801 | elsif R_Typ = Any_Character then | |
8802 | return; | |
8803 | ||
f3d57416 RW |
8804 | -- If the type is bit-packed, then we always transform the string |
8805 | -- literal into a full fledged aggregate. | |
996ae0b0 RK |
8806 | |
8807 | elsif Is_Bit_Packed_Array (Typ) then | |
8808 | null; | |
8809 | ||
82c80734 | 8810 | -- Deal with cases of Wide_Wide_String, Wide_String, and String |
996ae0b0 RK |
8811 | |
8812 | else | |
82c80734 RD |
8813 | -- For Standard.Wide_Wide_String, or any other type whose component |
8814 | -- type is Standard.Wide_Wide_Character, we know that all the | |
996ae0b0 RK |
8815 | -- characters in the string must be acceptable, since the parser |
8816 | -- accepted the characters as valid character literals. | |
8817 | ||
82c80734 | 8818 | if R_Typ = Standard_Wide_Wide_Character then |
996ae0b0 RK |
8819 | null; |
8820 | ||
c8ef728f ES |
8821 | -- For the case of Standard.String, or any other type whose component |
8822 | -- type is Standard.Character, we must make sure that there are no | |
8823 | -- wide characters in the string, i.e. that it is entirely composed | |
8824 | -- of characters in range of type Character. | |
996ae0b0 | 8825 | |
c8ef728f ES |
8826 | -- If the string literal is the result of a static concatenation, the |
8827 | -- test has already been performed on the components, and need not be | |
8828 | -- repeated. | |
996ae0b0 RK |
8829 | |
8830 | elsif R_Typ = Standard_Character | |
8831 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8832 | then | |
8833 | for J in 1 .. Strlen loop | |
8834 | if not In_Character_Range (Get_String_Char (Str, J)) then | |
8835 | ||
8836 | -- If we are out of range, post error. This is one of the | |
8837 | -- very few places that we place the flag in the middle of | |
d81b4bfe TQ |
8838 | -- a token, right under the offending wide character. Not |
8839 | -- quite clear if this is right wrt wide character encoding | |
8840 | -- sequences, but it's only an error message! | |
996ae0b0 RK |
8841 | |
8842 | Error_Msg | |
82c80734 RD |
8843 | ("literal out of range of type Standard.Character", |
8844 | Source_Ptr (Int (Loc) + J)); | |
8845 | return; | |
8846 | end if; | |
8847 | end loop; | |
8848 | ||
8849 | -- For the case of Standard.Wide_String, or any other type whose | |
8850 | -- component type is Standard.Wide_Character, we must make sure that | |
8851 | -- there are no wide characters in the string, i.e. that it is | |
8852 | -- entirely composed of characters in range of type Wide_Character. | |
8853 | ||
8854 | -- If the string literal is the result of a static concatenation, | |
8855 | -- the test has already been performed on the components, and need | |
8856 | -- not be repeated. | |
8857 | ||
8858 | elsif R_Typ = Standard_Wide_Character | |
8859 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
8860 | then | |
8861 | for J in 1 .. Strlen loop | |
8862 | if not In_Wide_Character_Range (Get_String_Char (Str, J)) then | |
8863 | ||
8864 | -- If we are out of range, post error. This is one of the | |
8865 | -- very few places that we place the flag in the middle of | |
8866 | -- a token, right under the offending wide character. | |
8867 | ||
8868 | -- This is not quite right, because characters in general | |
8869 | -- will take more than one character position ??? | |
8870 | ||
8871 | Error_Msg | |
8872 | ("literal out of range of type Standard.Wide_Character", | |
996ae0b0 RK |
8873 | Source_Ptr (Int (Loc) + J)); |
8874 | return; | |
8875 | end if; | |
8876 | end loop; | |
8877 | ||
8878 | -- If the root type is not a standard character, then we will convert | |
8879 | -- the string into an aggregate and will let the aggregate code do | |
82c80734 | 8880 | -- the checking. Standard Wide_Wide_Character is also OK here. |
996ae0b0 RK |
8881 | |
8882 | else | |
8883 | null; | |
996ae0b0 RK |
8884 | end if; |
8885 | ||
c8ef728f ES |
8886 | -- See if the component type of the array corresponding to the string |
8887 | -- has compile time known bounds. If yes we can directly check | |
8888 | -- whether the evaluation of the string will raise constraint error. | |
8889 | -- Otherwise we need to transform the string literal into the | |
5cc9353d RD |
8890 | -- corresponding character aggregate and let the aggregate code do |
8891 | -- the checking. | |
996ae0b0 | 8892 | |
45fc7ddb HK |
8893 | if Is_Standard_Character_Type (R_Typ) then |
8894 | ||
996ae0b0 RK |
8895 | -- Check for the case of full range, where we are definitely OK |
8896 | ||
8897 | if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then | |
8898 | return; | |
8899 | end if; | |
8900 | ||
8901 | -- Here the range is not the complete base type range, so check | |
8902 | ||
8903 | declare | |
8904 | Comp_Typ_Lo : constant Node_Id := | |
8905 | Type_Low_Bound (Component_Type (Typ)); | |
8906 | Comp_Typ_Hi : constant Node_Id := | |
8907 | Type_High_Bound (Component_Type (Typ)); | |
8908 | ||
8909 | Char_Val : Uint; | |
8910 | ||
8911 | begin | |
8912 | if Compile_Time_Known_Value (Comp_Typ_Lo) | |
8913 | and then Compile_Time_Known_Value (Comp_Typ_Hi) | |
8914 | then | |
8915 | for J in 1 .. Strlen loop | |
8916 | Char_Val := UI_From_Int (Int (Get_String_Char (Str, J))); | |
8917 | ||
8918 | if Char_Val < Expr_Value (Comp_Typ_Lo) | |
8919 | or else Char_Val > Expr_Value (Comp_Typ_Hi) | |
8920 | then | |
8921 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 8922 | (N, "character out of range?", CE_Range_Check_Failed, |
996ae0b0 RK |
8923 | Loc => Source_Ptr (Int (Loc) + J)); |
8924 | end if; | |
8925 | end loop; | |
8926 | ||
8927 | return; | |
8928 | end if; | |
8929 | end; | |
8930 | end if; | |
8931 | end if; | |
8932 | ||
8933 | -- If we got here we meed to transform the string literal into the | |
8934 | -- equivalent qualified positional array aggregate. This is rather | |
8935 | -- heavy artillery for this situation, but it is hard work to avoid. | |
8936 | ||
8937 | declare | |
fbf5a39b | 8938 | Lits : constant List_Id := New_List; |
996ae0b0 RK |
8939 | P : Source_Ptr := Loc + 1; |
8940 | C : Char_Code; | |
8941 | ||
8942 | begin | |
c8ef728f ES |
8943 | -- Build the character literals, we give them source locations that |
8944 | -- correspond to the string positions, which is a bit tricky given | |
8945 | -- the possible presence of wide character escape sequences. | |
996ae0b0 RK |
8946 | |
8947 | for J in 1 .. Strlen loop | |
8948 | C := Get_String_Char (Str, J); | |
8949 | Set_Character_Literal_Name (C); | |
8950 | ||
8951 | Append_To (Lits, | |
82c80734 RD |
8952 | Make_Character_Literal (P, |
8953 | Chars => Name_Find, | |
8954 | Char_Literal_Value => UI_From_CC (C))); | |
996ae0b0 RK |
8955 | |
8956 | if In_Character_Range (C) then | |
8957 | P := P + 1; | |
8958 | ||
8959 | -- Should we have a call to Skip_Wide here ??? | |
5cc9353d | 8960 | |
996ae0b0 RK |
8961 | -- ??? else |
8962 | -- Skip_Wide (P); | |
8963 | ||
8964 | end if; | |
8965 | end loop; | |
8966 | ||
8967 | Rewrite (N, | |
8968 | Make_Qualified_Expression (Loc, | |
8969 | Subtype_Mark => New_Reference_To (Typ, Loc), | |
8970 | Expression => | |
8971 | Make_Aggregate (Loc, Expressions => Lits))); | |
8972 | ||
8973 | Analyze_And_Resolve (N, Typ); | |
8974 | end; | |
8975 | end Resolve_String_Literal; | |
8976 | ||
8977 | ----------------------------- | |
8978 | -- Resolve_Subprogram_Info -- | |
8979 | ----------------------------- | |
8980 | ||
8981 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is | |
8982 | begin | |
8983 | Set_Etype (N, Typ); | |
8984 | end Resolve_Subprogram_Info; | |
8985 | ||
8986 | ----------------------------- | |
8987 | -- Resolve_Type_Conversion -- | |
8988 | ----------------------------- | |
8989 | ||
8990 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is | |
4b2d2c13 AC |
8991 | Conv_OK : constant Boolean := Conversion_OK (N); |
8992 | Operand : constant Node_Id := Expression (N); | |
b7d1f17f HK |
8993 | Operand_Typ : constant Entity_Id := Etype (Operand); |
8994 | Target_Typ : constant Entity_Id := Etype (N); | |
996ae0b0 | 8995 | Rop : Node_Id; |
fbf5a39b AC |
8996 | Orig_N : Node_Id; |
8997 | Orig_T : Node_Id; | |
996ae0b0 | 8998 | |
ae2aa109 AC |
8999 | Test_Redundant : Boolean := Warn_On_Redundant_Constructs; |
9000 | -- Set to False to suppress cases where we want to suppress the test | |
9001 | -- for redundancy to avoid possible false positives on this warning. | |
9002 | ||
996ae0b0 | 9003 | begin |
996ae0b0 | 9004 | if not Conv_OK |
b7d1f17f | 9005 | and then not Valid_Conversion (N, Target_Typ, Operand) |
996ae0b0 RK |
9006 | then |
9007 | return; | |
9008 | end if; | |
9009 | ||
ae2aa109 AC |
9010 | -- If the Operand Etype is Universal_Fixed, then the conversion is |
9011 | -- never redundant. We need this check because by the time we have | |
9012 | -- finished the rather complex transformation, the conversion looks | |
9013 | -- redundant when it is not. | |
9014 | ||
9015 | if Operand_Typ = Universal_Fixed then | |
9016 | Test_Redundant := False; | |
9017 | ||
9018 | -- If the operand is marked as Any_Fixed, then special processing is | |
9019 | -- required. This is also a case where we suppress the test for a | |
9020 | -- redundant conversion, since most certainly it is not redundant. | |
9021 | ||
9022 | elsif Operand_Typ = Any_Fixed then | |
9023 | Test_Redundant := False; | |
996ae0b0 RK |
9024 | |
9025 | -- Mixed-mode operation involving a literal. Context must be a fixed | |
9026 | -- type which is applied to the literal subsequently. | |
9027 | ||
9028 | if Is_Fixed_Point_Type (Typ) then | |
9029 | Set_Etype (Operand, Universal_Real); | |
9030 | ||
9031 | elsif Is_Numeric_Type (Typ) | |
45fc7ddb | 9032 | and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide) |
996ae0b0 | 9033 | and then (Etype (Right_Opnd (Operand)) = Universal_Real |
45fc7ddb HK |
9034 | or else |
9035 | Etype (Left_Opnd (Operand)) = Universal_Real) | |
996ae0b0 | 9036 | then |
a77842bd TQ |
9037 | -- Return if expression is ambiguous |
9038 | ||
996ae0b0 | 9039 | if Unique_Fixed_Point_Type (N) = Any_Type then |
a77842bd | 9040 | return; |
82c80734 | 9041 | |
a77842bd TQ |
9042 | -- If nothing else, the available fixed type is Duration |
9043 | ||
9044 | else | |
996ae0b0 RK |
9045 | Set_Etype (Operand, Standard_Duration); |
9046 | end if; | |
9047 | ||
bc5f3720 | 9048 | -- Resolve the real operand with largest available precision |
9ebe3743 | 9049 | |
996ae0b0 RK |
9050 | if Etype (Right_Opnd (Operand)) = Universal_Real then |
9051 | Rop := New_Copy_Tree (Right_Opnd (Operand)); | |
9052 | else | |
9053 | Rop := New_Copy_Tree (Left_Opnd (Operand)); | |
9054 | end if; | |
9055 | ||
9ebe3743 | 9056 | Resolve (Rop, Universal_Real); |
996ae0b0 | 9057 | |
82c80734 RD |
9058 | -- If the operand is a literal (it could be a non-static and |
9059 | -- illegal exponentiation) check whether the use of Duration | |
9060 | -- is potentially inaccurate. | |
9061 | ||
9062 | if Nkind (Rop) = N_Real_Literal | |
9063 | and then Realval (Rop) /= Ureal_0 | |
996ae0b0 RK |
9064 | and then abs (Realval (Rop)) < Delta_Value (Standard_Duration) |
9065 | then | |
aa180613 | 9066 | Error_Msg_N |
aa5147f0 ES |
9067 | ("?universal real operand can only " & |
9068 | "be interpreted as Duration!", | |
aa180613 RD |
9069 | Rop); |
9070 | Error_Msg_N | |
aa5147f0 | 9071 | ("\?precision will be lost in the conversion!", Rop); |
996ae0b0 RK |
9072 | end if; |
9073 | ||
891a6e79 AC |
9074 | elsif Is_Numeric_Type (Typ) |
9075 | and then Nkind (Operand) in N_Op | |
9076 | and then Unique_Fixed_Point_Type (N) /= Any_Type | |
9077 | then | |
9078 | Set_Etype (Operand, Standard_Duration); | |
9079 | ||
996ae0b0 RK |
9080 | else |
9081 | Error_Msg_N ("invalid context for mixed mode operation", N); | |
9082 | Set_Etype (Operand, Any_Type); | |
9083 | return; | |
9084 | end if; | |
9085 | end if; | |
9086 | ||
fbf5a39b | 9087 | Resolve (Operand); |
996ae0b0 | 9088 | |
b0186f71 AC |
9089 | -- In SPARK or ALFA, a type conversion between array types should be |
9090 | -- restricted to types which have matching static bounds. | |
9091 | ||
fe5d3068 | 9092 | if Is_Array_Type (Target_Typ) |
b0186f71 AC |
9093 | and then Is_Array_Type (Operand_Typ) |
9094 | and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ) | |
9095 | then | |
fe5d3068 YM |
9096 | Check_Formal_Restriction |
9097 | ("array types should have matching static bounds", N); | |
b0186f71 AC |
9098 | end if; |
9099 | ||
e24329cd YM |
9100 | -- In formal mode, the operand of an ancestor type conversion must be an |
9101 | -- object (not an expression). | |
9102 | ||
9103 | if Is_Tagged_Type (Target_Typ) | |
9104 | and then not Is_Class_Wide_Type (Target_Typ) | |
9105 | and then Is_Tagged_Type (Operand_Typ) | |
9106 | and then not Is_Class_Wide_Type (Operand_Typ) | |
9107 | and then Is_Ancestor (Target_Typ, Operand_Typ) | |
9108 | and then not Is_SPARK_Object_Reference (Operand) | |
9109 | then | |
9110 | Check_Formal_Restriction ("object required", Operand); | |
9111 | end if; | |
9112 | ||
996ae0b0 | 9113 | -- Note: we do the Eval_Type_Conversion call before applying the |
d81b4bfe TQ |
9114 | -- required checks for a subtype conversion. This is important, since |
9115 | -- both are prepared under certain circumstances to change the type | |
9116 | -- conversion to a constraint error node, but in the case of | |
9117 | -- Eval_Type_Conversion this may reflect an illegality in the static | |
9118 | -- case, and we would miss the illegality (getting only a warning | |
9119 | -- message), if we applied the type conversion checks first. | |
996ae0b0 RK |
9120 | |
9121 | Eval_Type_Conversion (N); | |
9122 | ||
d81b4bfe TQ |
9123 | -- Even when evaluation is not possible, we may be able to simplify the |
9124 | -- conversion or its expression. This needs to be done before applying | |
9125 | -- checks, since otherwise the checks may use the original expression | |
9126 | -- and defeat the simplifications. This is specifically the case for | |
9127 | -- elimination of the floating-point Truncation attribute in | |
9128 | -- float-to-int conversions. | |
0669bebe GB |
9129 | |
9130 | Simplify_Type_Conversion (N); | |
9131 | ||
d81b4bfe TQ |
9132 | -- If after evaluation we still have a type conversion, then we may need |
9133 | -- to apply checks required for a subtype conversion. | |
996ae0b0 RK |
9134 | |
9135 | -- Skip these type conversion checks if universal fixed operands | |
9136 | -- operands involved, since range checks are handled separately for | |
9137 | -- these cases (in the appropriate Expand routines in unit Exp_Fixd). | |
9138 | ||
9139 | if Nkind (N) = N_Type_Conversion | |
b7d1f17f HK |
9140 | and then not Is_Generic_Type (Root_Type (Target_Typ)) |
9141 | and then Target_Typ /= Universal_Fixed | |
9142 | and then Operand_Typ /= Universal_Fixed | |
996ae0b0 RK |
9143 | then |
9144 | Apply_Type_Conversion_Checks (N); | |
9145 | end if; | |
9146 | ||
d81b4bfe TQ |
9147 | -- Issue warning for conversion of simple object to its own type. We |
9148 | -- have to test the original nodes, since they may have been rewritten | |
9149 | -- by various optimizations. | |
fbf5a39b AC |
9150 | |
9151 | Orig_N := Original_Node (N); | |
996ae0b0 | 9152 | |
ae2aa109 AC |
9153 | -- Here we test for a redundant conversion if the warning mode is |
9154 | -- active (and was not locally reset), and we have a type conversion | |
9155 | -- from source not appearing in a generic instance. | |
9156 | ||
9157 | if Test_Redundant | |
fbf5a39b | 9158 | and then Nkind (Orig_N) = N_Type_Conversion |
ae2aa109 | 9159 | and then Comes_From_Source (Orig_N) |
5453d5bd | 9160 | and then not In_Instance |
996ae0b0 | 9161 | then |
fbf5a39b | 9162 | Orig_N := Original_Node (Expression (Orig_N)); |
b7d1f17f | 9163 | Orig_T := Target_Typ; |
fbf5a39b AC |
9164 | |
9165 | -- If the node is part of a larger expression, the Target_Type | |
9166 | -- may not be the original type of the node if the context is a | |
9167 | -- condition. Recover original type to see if conversion is needed. | |
9168 | ||
9169 | if Is_Boolean_Type (Orig_T) | |
9170 | and then Nkind (Parent (N)) in N_Op | |
9171 | then | |
9172 | Orig_T := Etype (Parent (N)); | |
9173 | end if; | |
9174 | ||
4adf3c50 | 9175 | -- If we have an entity name, then give the warning if the entity |
ae2aa109 AC |
9176 | -- is the right type, or if it is a loop parameter covered by the |
9177 | -- original type (that's needed because loop parameters have an | |
9178 | -- odd subtype coming from the bounds). | |
9179 | ||
9180 | if (Is_Entity_Name (Orig_N) | |
9181 | and then | |
9182 | (Etype (Entity (Orig_N)) = Orig_T | |
9183 | or else | |
9184 | (Ekind (Entity (Orig_N)) = E_Loop_Parameter | |
477bd732 | 9185 | and then Covers (Orig_T, Etype (Entity (Orig_N)))))) |
ae2aa109 | 9186 | |
477bd732 | 9187 | -- If not an entity, then type of expression must match |
ae2aa109 AC |
9188 | |
9189 | or else Etype (Orig_N) = Orig_T | |
fbf5a39b | 9190 | then |
4b2d2c13 AC |
9191 | -- One more check, do not give warning if the analyzed conversion |
9192 | -- has an expression with non-static bounds, and the bounds of the | |
9193 | -- target are static. This avoids junk warnings in cases where the | |
9194 | -- conversion is necessary to establish staticness, for example in | |
9195 | -- a case statement. | |
9196 | ||
9197 | if not Is_OK_Static_Subtype (Operand_Typ) | |
9198 | and then Is_OK_Static_Subtype (Target_Typ) | |
9199 | then | |
9200 | null; | |
9201 | ||
5cc9353d RD |
9202 | -- Finally, if this type conversion occurs in a context requiring |
9203 | -- a prefix, and the expression is a qualified expression then the | |
9204 | -- type conversion is not redundant, since a qualified expression | |
9205 | -- is not a prefix, whereas a type conversion is. For example, "X | |
9206 | -- := T'(Funx(...)).Y;" is illegal because a selected component | |
9207 | -- requires a prefix, but a type conversion makes it legal: "X := | |
9208 | -- T(T'(Funx(...))).Y;" | |
4adf3c50 | 9209 | |
9db0b232 AC |
9210 | -- In Ada 2012, a qualified expression is a name, so this idiom is |
9211 | -- no longer needed, but we still suppress the warning because it | |
9212 | -- seems unfriendly for warnings to pop up when you switch to the | |
9213 | -- newer language version. | |
be257e99 AC |
9214 | |
9215 | elsif Nkind (Orig_N) = N_Qualified_Expression | |
f5d96d00 AC |
9216 | and then Nkind_In (Parent (N), N_Attribute_Reference, |
9217 | N_Indexed_Component, | |
9218 | N_Selected_Component, | |
9219 | N_Slice, | |
9220 | N_Explicit_Dereference) | |
be257e99 AC |
9221 | then |
9222 | null; | |
9223 | ||
ae2aa109 AC |
9224 | -- Here we give the redundant conversion warning. If it is an |
9225 | -- entity, give the name of the entity in the message. If not, | |
9226 | -- just mention the expression. | |
4b2d2c13 AC |
9227 | |
9228 | else | |
ae2aa109 AC |
9229 | if Is_Entity_Name (Orig_N) then |
9230 | Error_Msg_Node_2 := Orig_T; | |
9231 | Error_Msg_NE -- CODEFIX | |
9232 | ("?redundant conversion, & is of type &!", | |
9233 | N, Entity (Orig_N)); | |
9234 | else | |
9235 | Error_Msg_NE | |
9236 | ("?redundant conversion, expression is of type&!", | |
9237 | N, Orig_T); | |
9238 | end if; | |
4b2d2c13 | 9239 | end if; |
fbf5a39b | 9240 | end if; |
996ae0b0 | 9241 | end if; |
758c442c | 9242 | |
b7d1f17f | 9243 | -- Ada 2005 (AI-251): Handle class-wide interface type conversions. |
0669bebe GB |
9244 | -- No need to perform any interface conversion if the type of the |
9245 | -- expression coincides with the target type. | |
758c442c | 9246 | |
0791fbe9 | 9247 | if Ada_Version >= Ada_2005 |
0669bebe | 9248 | and then Expander_Active |
b7d1f17f | 9249 | and then Operand_Typ /= Target_Typ |
0669bebe | 9250 | then |
b7d1f17f HK |
9251 | declare |
9252 | Opnd : Entity_Id := Operand_Typ; | |
9253 | Target : Entity_Id := Target_Typ; | |
758c442c | 9254 | |
b7d1f17f HK |
9255 | begin |
9256 | if Is_Access_Type (Opnd) then | |
841dd0f5 | 9257 | Opnd := Designated_Type (Opnd); |
1420b484 JM |
9258 | end if; |
9259 | ||
b7d1f17f | 9260 | if Is_Access_Type (Target_Typ) then |
841dd0f5 | 9261 | Target := Designated_Type (Target); |
4197ae1e | 9262 | end if; |
c8ef728f | 9263 | |
b7d1f17f HK |
9264 | if Opnd = Target then |
9265 | null; | |
c8ef728f | 9266 | |
b7d1f17f | 9267 | -- Conversion from interface type |
ea985d95 | 9268 | |
b7d1f17f | 9269 | elsif Is_Interface (Opnd) then |
ea985d95 | 9270 | |
b7d1f17f | 9271 | -- Ada 2005 (AI-217): Handle entities from limited views |
aa180613 | 9272 | |
b7d1f17f HK |
9273 | if From_With_Type (Opnd) then |
9274 | Error_Msg_Qual_Level := 99; | |
305caf42 AC |
9275 | Error_Msg_NE -- CODEFIX |
9276 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
9277 | Cunit_Entity (Get_Source_Unit (Base_Type (Opnd)))); |
9278 | Error_Msg_N | |
9279 | ("type conversions require visibility of the full view", | |
9280 | N); | |
aa180613 | 9281 | |
aa5147f0 ES |
9282 | elsif From_With_Type (Target) |
9283 | and then not | |
9284 | (Is_Access_Type (Target_Typ) | |
9285 | and then Present (Non_Limited_View (Etype (Target)))) | |
9286 | then | |
b7d1f17f | 9287 | Error_Msg_Qual_Level := 99; |
305caf42 AC |
9288 | Error_Msg_NE -- CODEFIX |
9289 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
9290 | Cunit_Entity (Get_Source_Unit (Base_Type (Target)))); |
9291 | Error_Msg_N | |
9292 | ("type conversions require visibility of the full view", | |
9293 | N); | |
aa180613 | 9294 | |
b7d1f17f HK |
9295 | else |
9296 | Expand_Interface_Conversion (N, Is_Static => False); | |
9297 | end if; | |
9298 | ||
9299 | -- Conversion to interface type | |
9300 | ||
9301 | elsif Is_Interface (Target) then | |
9302 | ||
9303 | -- Handle subtypes | |
9304 | ||
8a95f4e8 | 9305 | if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then |
b7d1f17f HK |
9306 | Opnd := Etype (Opnd); |
9307 | end if; | |
9308 | ||
9309 | if not Interface_Present_In_Ancestor | |
9310 | (Typ => Opnd, | |
9311 | Iface => Target) | |
9312 | then | |
9313 | if Is_Class_Wide_Type (Opnd) then | |
9314 | ||
9315 | -- The static analysis is not enough to know if the | |
9316 | -- interface is implemented or not. Hence we must pass | |
9317 | -- the work to the expander to generate code to evaluate | |
e7c0dd39 | 9318 | -- the conversion at run time. |
b7d1f17f HK |
9319 | |
9320 | Expand_Interface_Conversion (N, Is_Static => False); | |
9321 | ||
9322 | else | |
9323 | Error_Msg_Name_1 := Chars (Etype (Target)); | |
9324 | Error_Msg_Name_2 := Chars (Opnd); | |
9325 | Error_Msg_N | |
9326 | ("wrong interface conversion (% is not a progenitor " & | |
9327 | "of %)", N); | |
9328 | end if; | |
9329 | ||
9330 | else | |
9331 | Expand_Interface_Conversion (N); | |
9332 | end if; | |
9333 | end if; | |
9334 | end; | |
758c442c | 9335 | end if; |
996ae0b0 RK |
9336 | end Resolve_Type_Conversion; |
9337 | ||
9338 | ---------------------- | |
9339 | -- Resolve_Unary_Op -- | |
9340 | ---------------------- | |
9341 | ||
9342 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
9343 | B_Typ : constant Entity_Id := Base_Type (Typ); |
9344 | R : constant Node_Id := Right_Opnd (N); | |
9345 | OK : Boolean; | |
9346 | Lo : Uint; | |
9347 | Hi : Uint; | |
996ae0b0 RK |
9348 | |
9349 | begin | |
7a489a2b AC |
9350 | if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then |
9351 | Error_Msg_Name_1 := Chars (Typ); | |
9352 | Check_Formal_Restriction | |
9353 | ("unary operator not defined for modular type%", N); | |
9354 | end if; | |
9355 | ||
b7d1f17f | 9356 | -- Deal with intrinsic unary operators |
996ae0b0 | 9357 | |
fbf5a39b AC |
9358 | if Comes_From_Source (N) |
9359 | and then Ekind (Entity (N)) = E_Function | |
9360 | and then Is_Imported (Entity (N)) | |
9361 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
9362 | then | |
9363 | Resolve_Intrinsic_Unary_Operator (N, Typ); | |
9364 | return; | |
9365 | end if; | |
9366 | ||
0669bebe GB |
9367 | -- Deal with universal cases |
9368 | ||
996ae0b0 | 9369 | if Etype (R) = Universal_Integer |
0669bebe GB |
9370 | or else |
9371 | Etype (R) = Universal_Real | |
996ae0b0 RK |
9372 | then |
9373 | Check_For_Visible_Operator (N, B_Typ); | |
9374 | end if; | |
9375 | ||
9376 | Set_Etype (N, B_Typ); | |
9377 | Resolve (R, B_Typ); | |
fbf5a39b | 9378 | |
9ebe3743 HK |
9379 | -- Generate warning for expressions like abs (x mod 2) |
9380 | ||
9381 | if Warn_On_Redundant_Constructs | |
9382 | and then Nkind (N) = N_Op_Abs | |
9383 | then | |
9384 | Determine_Range (Right_Opnd (N), OK, Lo, Hi); | |
9385 | ||
9386 | if OK and then Hi >= Lo and then Lo >= 0 then | |
305caf42 | 9387 | Error_Msg_N -- CODEFIX |
9ebe3743 HK |
9388 | ("?abs applied to known non-negative value has no effect", N); |
9389 | end if; | |
9390 | end if; | |
9391 | ||
0669bebe GB |
9392 | -- Deal with reference generation |
9393 | ||
996ae0b0 | 9394 | Check_Unset_Reference (R); |
fbf5a39b | 9395 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
9396 | Eval_Unary_Op (N); |
9397 | ||
9398 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
9399 | -- and perhaps the Resolve routines should be separated for the various | |
9400 | -- arithmetic operations, since they will need different processing ??? | |
9401 | ||
9402 | if Nkind (N) in N_Op then | |
9403 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 9404 | Enable_Overflow_Check (N); |
996ae0b0 RK |
9405 | end if; |
9406 | end if; | |
0669bebe | 9407 | |
d81b4bfe TQ |
9408 | -- Generate warning for expressions like -5 mod 3 for integers. No need |
9409 | -- to worry in the floating-point case, since parens do not affect the | |
9410 | -- result so there is no point in giving in a warning. | |
0669bebe GB |
9411 | |
9412 | declare | |
9413 | Norig : constant Node_Id := Original_Node (N); | |
9414 | Rorig : Node_Id; | |
9415 | Val : Uint; | |
9416 | HB : Uint; | |
9417 | LB : Uint; | |
9418 | Lval : Uint; | |
9419 | Opnd : Node_Id; | |
9420 | ||
9421 | begin | |
9422 | if Warn_On_Questionable_Missing_Parens | |
9423 | and then Comes_From_Source (Norig) | |
9424 | and then Is_Integer_Type (Typ) | |
9425 | and then Nkind (Norig) = N_Op_Minus | |
9426 | then | |
9427 | Rorig := Original_Node (Right_Opnd (Norig)); | |
9428 | ||
9429 | -- We are looking for cases where the right operand is not | |
f3d57416 | 9430 | -- parenthesized, and is a binary operator, multiply, divide, or |
0669bebe GB |
9431 | -- mod. These are the cases where the grouping can affect results. |
9432 | ||
9433 | if Paren_Count (Rorig) = 0 | |
45fc7ddb | 9434 | and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide) |
0669bebe GB |
9435 | then |
9436 | -- For mod, we always give the warning, since the value is | |
9437 | -- affected by the parenthesization (e.g. (-5) mod 315 /= | |
d81b4bfe | 9438 | -- -(5 mod 315)). But for the other cases, the only concern is |
0669bebe GB |
9439 | -- overflow, e.g. for the case of 8 big signed (-(2 * 64) |
9440 | -- overflows, but (-2) * 64 does not). So we try to give the | |
9441 | -- message only when overflow is possible. | |
9442 | ||
9443 | if Nkind (Rorig) /= N_Op_Mod | |
9444 | and then Compile_Time_Known_Value (R) | |
9445 | then | |
9446 | Val := Expr_Value (R); | |
9447 | ||
9448 | if Compile_Time_Known_Value (Type_High_Bound (Typ)) then | |
9449 | HB := Expr_Value (Type_High_Bound (Typ)); | |
9450 | else | |
9451 | HB := Expr_Value (Type_High_Bound (Base_Type (Typ))); | |
9452 | end if; | |
9453 | ||
9454 | if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then | |
9455 | LB := Expr_Value (Type_Low_Bound (Typ)); | |
9456 | else | |
9457 | LB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); | |
9458 | end if; | |
9459 | ||
d81b4bfe TQ |
9460 | -- Note that the test below is deliberately excluding the |
9461 | -- largest negative number, since that is a potentially | |
0669bebe GB |
9462 | -- troublesome case (e.g. -2 * x, where the result is the |
9463 | -- largest negative integer has an overflow with 2 * x). | |
9464 | ||
9465 | if Val > LB and then Val <= HB then | |
9466 | return; | |
9467 | end if; | |
9468 | end if; | |
9469 | ||
9470 | -- For the multiplication case, the only case we have to worry | |
9471 | -- about is when (-a)*b is exactly the largest negative number | |
9472 | -- so that -(a*b) can cause overflow. This can only happen if | |
9473 | -- a is a power of 2, and more generally if any operand is a | |
9474 | -- constant that is not a power of 2, then the parentheses | |
9475 | -- cannot affect whether overflow occurs. We only bother to | |
9476 | -- test the left most operand | |
9477 | ||
9478 | -- Loop looking at left operands for one that has known value | |
9479 | ||
9480 | Opnd := Rorig; | |
9481 | Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop | |
9482 | if Compile_Time_Known_Value (Left_Opnd (Opnd)) then | |
9483 | Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd))); | |
9484 | ||
9485 | -- Operand value of 0 or 1 skips warning | |
9486 | ||
9487 | if Lval <= 1 then | |
9488 | return; | |
9489 | ||
9490 | -- Otherwise check power of 2, if power of 2, warn, if | |
9491 | -- anything else, skip warning. | |
9492 | ||
9493 | else | |
9494 | while Lval /= 2 loop | |
9495 | if Lval mod 2 = 1 then | |
9496 | return; | |
9497 | else | |
9498 | Lval := Lval / 2; | |
9499 | end if; | |
9500 | end loop; | |
9501 | ||
9502 | exit Opnd_Loop; | |
9503 | end if; | |
9504 | end if; | |
9505 | ||
9506 | -- Keep looking at left operands | |
9507 | ||
9508 | Opnd := Left_Opnd (Opnd); | |
9509 | end loop Opnd_Loop; | |
9510 | ||
9511 | -- For rem or "/" we can only have a problematic situation | |
9512 | -- if the divisor has a value of minus one or one. Otherwise | |
9513 | -- overflow is impossible (divisor > 1) or we have a case of | |
9514 | -- division by zero in any case. | |
9515 | ||
45fc7ddb | 9516 | if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem) |
0669bebe GB |
9517 | and then Compile_Time_Known_Value (Right_Opnd (Rorig)) |
9518 | and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1 | |
9519 | then | |
9520 | return; | |
9521 | end if; | |
9522 | ||
9523 | -- If we fall through warning should be issued | |
9524 | ||
ed2233dc | 9525 | Error_Msg_N |
aa5147f0 | 9526 | ("?unary minus expression should be parenthesized here!", N); |
0669bebe GB |
9527 | end if; |
9528 | end if; | |
9529 | end; | |
996ae0b0 RK |
9530 | end Resolve_Unary_Op; |
9531 | ||
9532 | ---------------------------------- | |
9533 | -- Resolve_Unchecked_Expression -- | |
9534 | ---------------------------------- | |
9535 | ||
9536 | procedure Resolve_Unchecked_Expression | |
9537 | (N : Node_Id; | |
9538 | Typ : Entity_Id) | |
9539 | is | |
9540 | begin | |
9541 | Resolve (Expression (N), Typ, Suppress => All_Checks); | |
9542 | Set_Etype (N, Typ); | |
9543 | end Resolve_Unchecked_Expression; | |
9544 | ||
9545 | --------------------------------------- | |
9546 | -- Resolve_Unchecked_Type_Conversion -- | |
9547 | --------------------------------------- | |
9548 | ||
9549 | procedure Resolve_Unchecked_Type_Conversion | |
9550 | (N : Node_Id; | |
9551 | Typ : Entity_Id) | |
9552 | is | |
07fc65c4 GB |
9553 | pragma Warnings (Off, Typ); |
9554 | ||
996ae0b0 RK |
9555 | Operand : constant Node_Id := Expression (N); |
9556 | Opnd_Type : constant Entity_Id := Etype (Operand); | |
9557 | ||
9558 | begin | |
a77842bd | 9559 | -- Resolve operand using its own type |
996ae0b0 RK |
9560 | |
9561 | Resolve (Operand, Opnd_Type); | |
9562 | Eval_Unchecked_Conversion (N); | |
996ae0b0 RK |
9563 | end Resolve_Unchecked_Type_Conversion; |
9564 | ||
9565 | ------------------------------ | |
9566 | -- Rewrite_Operator_As_Call -- | |
9567 | ------------------------------ | |
9568 | ||
9569 | procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is | |
fbf5a39b AC |
9570 | Loc : constant Source_Ptr := Sloc (N); |
9571 | Actuals : constant List_Id := New_List; | |
996ae0b0 RK |
9572 | New_N : Node_Id; |
9573 | ||
9574 | begin | |
9575 | if Nkind (N) in N_Binary_Op then | |
9576 | Append (Left_Opnd (N), Actuals); | |
9577 | end if; | |
9578 | ||
9579 | Append (Right_Opnd (N), Actuals); | |
9580 | ||
9581 | New_N := | |
9582 | Make_Function_Call (Sloc => Loc, | |
9583 | Name => New_Occurrence_Of (Nam, Loc), | |
9584 | Parameter_Associations => Actuals); | |
9585 | ||
9586 | Preserve_Comes_From_Source (New_N, N); | |
9587 | Preserve_Comes_From_Source (Name (New_N), N); | |
9588 | Rewrite (N, New_N); | |
9589 | Set_Etype (N, Etype (Nam)); | |
9590 | end Rewrite_Operator_As_Call; | |
9591 | ||
9592 | ------------------------------ | |
9593 | -- Rewrite_Renamed_Operator -- | |
9594 | ------------------------------ | |
9595 | ||
0ab80019 AC |
9596 | procedure Rewrite_Renamed_Operator |
9597 | (N : Node_Id; | |
9598 | Op : Entity_Id; | |
9599 | Typ : Entity_Id) | |
9600 | is | |
996ae0b0 RK |
9601 | Nam : constant Name_Id := Chars (Op); |
9602 | Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
9603 | Op_Node : Node_Id; | |
9604 | ||
9605 | begin | |
d81b4bfe TQ |
9606 | -- Rewrite the operator node using the real operator, not its renaming. |
9607 | -- Exclude user-defined intrinsic operations of the same name, which are | |
9608 | -- treated separately and rewritten as calls. | |
996ae0b0 | 9609 | |
964f13da | 9610 | if Ekind (Op) /= E_Function or else Chars (N) /= Nam then |
996ae0b0 RK |
9611 | Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N)); |
9612 | Set_Chars (Op_Node, Nam); | |
9613 | Set_Etype (Op_Node, Etype (N)); | |
9614 | Set_Entity (Op_Node, Op); | |
9615 | Set_Right_Opnd (Op_Node, Right_Opnd (N)); | |
9616 | ||
b7d1f17f HK |
9617 | -- Indicate that both the original entity and its renaming are |
9618 | -- referenced at this point. | |
fbf5a39b AC |
9619 | |
9620 | Generate_Reference (Entity (N), N); | |
996ae0b0 RK |
9621 | Generate_Reference (Op, N); |
9622 | ||
9623 | if Is_Binary then | |
9624 | Set_Left_Opnd (Op_Node, Left_Opnd (N)); | |
9625 | end if; | |
9626 | ||
9627 | Rewrite (N, Op_Node); | |
0ab80019 | 9628 | |
1366997b AC |
9629 | -- If the context type is private, add the appropriate conversions so |
9630 | -- that the operator is applied to the full view. This is done in the | |
9631 | -- routines that resolve intrinsic operators. | |
0ab80019 AC |
9632 | |
9633 | if Is_Intrinsic_Subprogram (Op) | |
9634 | and then Is_Private_Type (Typ) | |
9635 | then | |
9636 | case Nkind (N) is | |
9637 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | | |
9638 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
9639 | Resolve_Intrinsic_Operator (N, Typ); | |
9640 | ||
d81b4bfe | 9641 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => |
0ab80019 AC |
9642 | Resolve_Intrinsic_Unary_Operator (N, Typ); |
9643 | ||
9644 | when others => | |
9645 | Resolve (N, Typ); | |
9646 | end case; | |
9647 | end if; | |
9648 | ||
964f13da RD |
9649 | elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then |
9650 | ||
1366997b AC |
9651 | -- Operator renames a user-defined operator of the same name. Use the |
9652 | -- original operator in the node, which is the one Gigi knows about. | |
0ab80019 AC |
9653 | |
9654 | Set_Entity (N, Op); | |
9655 | Set_Is_Overloaded (N, False); | |
996ae0b0 RK |
9656 | end if; |
9657 | end Rewrite_Renamed_Operator; | |
9658 | ||
9659 | ----------------------- | |
9660 | -- Set_Slice_Subtype -- | |
9661 | ----------------------- | |
9662 | ||
1366997b AC |
9663 | -- Build an implicit subtype declaration to represent the type delivered by |
9664 | -- the slice. This is an abbreviated version of an array subtype. We define | |
9665 | -- an index subtype for the slice, using either the subtype name or the | |
9666 | -- discrete range of the slice. To be consistent with index usage elsewhere | |
9667 | -- we create a list header to hold the single index. This list is not | |
9668 | -- otherwise attached to the syntax tree. | |
996ae0b0 RK |
9669 | |
9670 | procedure Set_Slice_Subtype (N : Node_Id) is | |
9671 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 9672 | Index_List : constant List_Id := New_List; |
996ae0b0 | 9673 | Index : Node_Id; |
996ae0b0 RK |
9674 | Index_Subtype : Entity_Id; |
9675 | Index_Type : Entity_Id; | |
9676 | Slice_Subtype : Entity_Id; | |
9677 | Drange : constant Node_Id := Discrete_Range (N); | |
9678 | ||
9679 | begin | |
9680 | if Is_Entity_Name (Drange) then | |
9681 | Index_Subtype := Entity (Drange); | |
9682 | ||
9683 | else | |
9684 | -- We force the evaluation of a range. This is definitely needed in | |
9685 | -- the renamed case, and seems safer to do unconditionally. Note in | |
9686 | -- any case that since we will create and insert an Itype referring | |
9687 | -- to this range, we must make sure any side effect removal actions | |
9688 | -- are inserted before the Itype definition. | |
9689 | ||
9690 | if Nkind (Drange) = N_Range then | |
9691 | Force_Evaluation (Low_Bound (Drange)); | |
9692 | Force_Evaluation (High_Bound (Drange)); | |
9693 | end if; | |
9694 | ||
9695 | Index_Type := Base_Type (Etype (Drange)); | |
9696 | ||
9697 | Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
9698 | ||
8a95f4e8 | 9699 | -- Take a new copy of Drange (where bounds have been rewritten to |
3c1ecd7e AC |
9700 | -- reference side-effect-free names). Using a separate tree ensures |
9701 | -- that further expansion (e.g. while rewriting a slice assignment | |
8a95f4e8 RD |
9702 | -- into a FOR loop) does not attempt to remove side effects on the |
9703 | -- bounds again (which would cause the bounds in the index subtype | |
9704 | -- definition to refer to temporaries before they are defined) (the | |
9705 | -- reason is that some names are considered side effect free here | |
9706 | -- for the subtype, but not in the context of a loop iteration | |
9707 | -- scheme). | |
9708 | ||
9709 | Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange)); | |
4230bdb7 | 9710 | Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype); |
996ae0b0 RK |
9711 | Set_Etype (Index_Subtype, Index_Type); |
9712 | Set_Size_Info (Index_Subtype, Index_Type); | |
9713 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
9714 | end if; | |
9715 | ||
9716 | Slice_Subtype := Create_Itype (E_Array_Subtype, N); | |
9717 | ||
9718 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
9719 | Set_Etype (Index, Index_Subtype); | |
9720 | Append (Index, Index_List); | |
9721 | ||
996ae0b0 RK |
9722 | Set_First_Index (Slice_Subtype, Index); |
9723 | Set_Etype (Slice_Subtype, Base_Type (Etype (N))); | |
9724 | Set_Is_Constrained (Slice_Subtype, True); | |
996ae0b0 | 9725 | |
8a95f4e8 RD |
9726 | Check_Compile_Time_Size (Slice_Subtype); |
9727 | ||
b7d1f17f HK |
9728 | -- The Etype of the existing Slice node is reset to this slice subtype. |
9729 | -- Its bounds are obtained from its first index. | |
996ae0b0 RK |
9730 | |
9731 | Set_Etype (N, Slice_Subtype); | |
9732 | ||
5cc9353d RD |
9733 | -- For packed slice subtypes, freeze immediately (except in the case of |
9734 | -- being in a "spec expression" where we never freeze when we first see | |
9735 | -- the expression). | |
8a95f4e8 RD |
9736 | |
9737 | if Is_Packed (Slice_Subtype) and not In_Spec_Expression then | |
9738 | Freeze_Itype (Slice_Subtype, N); | |
996ae0b0 | 9739 | |
cfab0c49 AC |
9740 | -- For all other cases insert an itype reference in the slice's actions |
9741 | -- so that the itype is frozen at the proper place in the tree (i.e. at | |
9742 | -- the point where actions for the slice are analyzed). Note that this | |
9743 | -- is different from freezing the itype immediately, which might be | |
9744 | -- premature (e.g. if the slice is within a transient scope). | |
9745 | ||
8a95f4e8 RD |
9746 | else |
9747 | Ensure_Defined (Typ => Slice_Subtype, N => N); | |
9748 | end if; | |
996ae0b0 RK |
9749 | end Set_Slice_Subtype; |
9750 | ||
9751 | -------------------------------- | |
9752 | -- Set_String_Literal_Subtype -- | |
9753 | -------------------------------- | |
9754 | ||
9755 | procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is | |
c8ef728f ES |
9756 | Loc : constant Source_Ptr := Sloc (N); |
9757 | Low_Bound : constant Node_Id := | |
d81b4bfe | 9758 | Type_Low_Bound (Etype (First_Index (Typ))); |
996ae0b0 RK |
9759 | Subtype_Id : Entity_Id; |
9760 | ||
9761 | begin | |
9762 | if Nkind (N) /= N_String_Literal then | |
9763 | return; | |
996ae0b0 RK |
9764 | end if; |
9765 | ||
c8ef728f | 9766 | Subtype_Id := Create_Itype (E_String_Literal_Subtype, N); |
91b1417d AC |
9767 | Set_String_Literal_Length (Subtype_Id, UI_From_Int |
9768 | (String_Length (Strval (N)))); | |
c8ef728f ES |
9769 | Set_Etype (Subtype_Id, Base_Type (Typ)); |
9770 | Set_Is_Constrained (Subtype_Id); | |
9771 | Set_Etype (N, Subtype_Id); | |
9772 | ||
9773 | if Is_OK_Static_Expression (Low_Bound) then | |
996ae0b0 | 9774 | |
1366997b AC |
9775 | -- The low bound is set from the low bound of the corresponding index |
9776 | -- type. Note that we do not store the high bound in the string literal | |
9777 | -- subtype, but it can be deduced if necessary from the length and the | |
9778 | -- low bound. | |
996ae0b0 | 9779 | |
c8ef728f | 9780 | Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound); |
996ae0b0 | 9781 | |
c8ef728f ES |
9782 | else |
9783 | Set_String_Literal_Low_Bound | |
9784 | (Subtype_Id, Make_Integer_Literal (Loc, 1)); | |
9785 | Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive); | |
9786 | ||
b7d1f17f HK |
9787 | -- Build bona fide subtype for the string, and wrap it in an |
9788 | -- unchecked conversion, because the backend expects the | |
c8ef728f ES |
9789 | -- String_Literal_Subtype to have a static lower bound. |
9790 | ||
9791 | declare | |
9792 | Index_List : constant List_Id := New_List; | |
9793 | Index_Type : constant Entity_Id := Etype (First_Index (Typ)); | |
9794 | High_Bound : constant Node_Id := | |
9795 | Make_Op_Add (Loc, | |
9796 | Left_Opnd => New_Copy_Tree (Low_Bound), | |
9797 | Right_Opnd => | |
9798 | Make_Integer_Literal (Loc, | |
9799 | String_Length (Strval (N)) - 1)); | |
9800 | Array_Subtype : Entity_Id; | |
9801 | Index_Subtype : Entity_Id; | |
9802 | Drange : Node_Id; | |
9803 | Index : Node_Id; | |
9804 | ||
9805 | begin | |
9806 | Index_Subtype := | |
9807 | Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
0669bebe | 9808 | Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound); |
c8ef728f ES |
9809 | Set_Scalar_Range (Index_Subtype, Drange); |
9810 | Set_Parent (Drange, N); | |
9811 | Analyze_And_Resolve (Drange, Index_Type); | |
9812 | ||
36fcf362 RD |
9813 | -- In the context, the Index_Type may already have a constraint, |
9814 | -- so use common base type on string subtype. The base type may | |
9815 | -- be used when generating attributes of the string, for example | |
9816 | -- in the context of a slice assignment. | |
9817 | ||
4adf3c50 AC |
9818 | Set_Etype (Index_Subtype, Base_Type (Index_Type)); |
9819 | Set_Size_Info (Index_Subtype, Index_Type); | |
9820 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
c8ef728f ES |
9821 | |
9822 | Array_Subtype := Create_Itype (E_Array_Subtype, N); | |
9823 | ||
9824 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
9825 | Set_Etype (Index, Index_Subtype); | |
9826 | Append (Index, Index_List); | |
9827 | ||
9828 | Set_First_Index (Array_Subtype, Index); | |
9829 | Set_Etype (Array_Subtype, Base_Type (Typ)); | |
9830 | Set_Is_Constrained (Array_Subtype, True); | |
c8ef728f ES |
9831 | |
9832 | Rewrite (N, | |
9833 | Make_Unchecked_Type_Conversion (Loc, | |
9834 | Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc), | |
9835 | Expression => Relocate_Node (N))); | |
9836 | Set_Etype (N, Array_Subtype); | |
9837 | end; | |
9838 | end if; | |
996ae0b0 RK |
9839 | end Set_String_Literal_Subtype; |
9840 | ||
0669bebe GB |
9841 | ------------------------------ |
9842 | -- Simplify_Type_Conversion -- | |
9843 | ------------------------------ | |
9844 | ||
9845 | procedure Simplify_Type_Conversion (N : Node_Id) is | |
9846 | begin | |
9847 | if Nkind (N) = N_Type_Conversion then | |
9848 | declare | |
9849 | Operand : constant Node_Id := Expression (N); | |
9850 | Target_Typ : constant Entity_Id := Etype (N); | |
9851 | Opnd_Typ : constant Entity_Id := Etype (Operand); | |
9852 | ||
9853 | begin | |
9854 | if Is_Floating_Point_Type (Opnd_Typ) | |
9855 | and then | |
9856 | (Is_Integer_Type (Target_Typ) | |
9857 | or else (Is_Fixed_Point_Type (Target_Typ) | |
9858 | and then Conversion_OK (N))) | |
9859 | and then Nkind (Operand) = N_Attribute_Reference | |
9860 | and then Attribute_Name (Operand) = Name_Truncation | |
9861 | ||
9862 | -- Special processing required if the conversion is the expression | |
9863 | -- of a Truncation attribute reference. In this case we replace: | |
9864 | ||
9865 | -- ityp (ftyp'Truncation (x)) | |
9866 | ||
9867 | -- by | |
9868 | ||
9869 | -- ityp (x) | |
9870 | ||
4adf3c50 | 9871 | -- with the Float_Truncate flag set, which is more efficient. |
0669bebe GB |
9872 | |
9873 | then | |
9874 | Rewrite (Operand, | |
9875 | Relocate_Node (First (Expressions (Operand)))); | |
9876 | Set_Float_Truncate (N, True); | |
9877 | end if; | |
9878 | end; | |
9879 | end if; | |
9880 | end Simplify_Type_Conversion; | |
9881 | ||
996ae0b0 RK |
9882 | ----------------------------- |
9883 | -- Unique_Fixed_Point_Type -- | |
9884 | ----------------------------- | |
9885 | ||
9886 | function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is | |
9887 | T1 : Entity_Id := Empty; | |
9888 | T2 : Entity_Id; | |
9889 | Item : Node_Id; | |
9890 | Scop : Entity_Id; | |
9891 | ||
9892 | procedure Fixed_Point_Error; | |
d81b4bfe TQ |
9893 | -- Give error messages for true ambiguity. Messages are posted on node |
9894 | -- N, and entities T1, T2 are the possible interpretations. | |
a77842bd TQ |
9895 | |
9896 | ----------------------- | |
9897 | -- Fixed_Point_Error -- | |
9898 | ----------------------- | |
996ae0b0 RK |
9899 | |
9900 | procedure Fixed_Point_Error is | |
9901 | begin | |
ed2233dc AC |
9902 | Error_Msg_N ("ambiguous universal_fixed_expression", N); |
9903 | Error_Msg_NE ("\\possible interpretation as}", N, T1); | |
9904 | Error_Msg_NE ("\\possible interpretation as}", N, T2); | |
996ae0b0 RK |
9905 | end Fixed_Point_Error; |
9906 | ||
a77842bd TQ |
9907 | -- Start of processing for Unique_Fixed_Point_Type |
9908 | ||
996ae0b0 RK |
9909 | begin |
9910 | -- The operations on Duration are visible, so Duration is always a | |
9911 | -- possible interpretation. | |
9912 | ||
9913 | T1 := Standard_Duration; | |
9914 | ||
bc5f3720 | 9915 | -- Look for fixed-point types in enclosing scopes |
996ae0b0 | 9916 | |
fbf5a39b | 9917 | Scop := Current_Scope; |
996ae0b0 RK |
9918 | while Scop /= Standard_Standard loop |
9919 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9920 | while Present (T2) loop |
9921 | if Is_Fixed_Point_Type (T2) | |
9922 | and then Current_Entity (T2) = T2 | |
9923 | and then Scope (Base_Type (T2)) = Scop | |
9924 | then | |
9925 | if Present (T1) then | |
9926 | Fixed_Point_Error; | |
9927 | return Any_Type; | |
9928 | else | |
9929 | T1 := T2; | |
9930 | end if; | |
9931 | end if; | |
9932 | ||
9933 | Next_Entity (T2); | |
9934 | end loop; | |
9935 | ||
9936 | Scop := Scope (Scop); | |
9937 | end loop; | |
9938 | ||
a77842bd | 9939 | -- Look for visible fixed type declarations in the context |
996ae0b0 RK |
9940 | |
9941 | Item := First (Context_Items (Cunit (Current_Sem_Unit))); | |
996ae0b0 | 9942 | while Present (Item) loop |
996ae0b0 RK |
9943 | if Nkind (Item) = N_With_Clause then |
9944 | Scop := Entity (Name (Item)); | |
9945 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
9946 | while Present (T2) loop |
9947 | if Is_Fixed_Point_Type (T2) | |
9948 | and then Scope (Base_Type (T2)) = Scop | |
19fb051c | 9949 | and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2)) |
996ae0b0 RK |
9950 | then |
9951 | if Present (T1) then | |
9952 | Fixed_Point_Error; | |
9953 | return Any_Type; | |
9954 | else | |
9955 | T1 := T2; | |
9956 | end if; | |
9957 | end if; | |
9958 | ||
9959 | Next_Entity (T2); | |
9960 | end loop; | |
9961 | end if; | |
9962 | ||
9963 | Next (Item); | |
9964 | end loop; | |
9965 | ||
9966 | if Nkind (N) = N_Real_Literal then | |
aa5147f0 | 9967 | Error_Msg_NE ("?real literal interpreted as }!", N, T1); |
996ae0b0 | 9968 | else |
aa5147f0 | 9969 | Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1); |
996ae0b0 RK |
9970 | end if; |
9971 | ||
9972 | return T1; | |
9973 | end Unique_Fixed_Point_Type; | |
9974 | ||
9975 | ---------------------- | |
9976 | -- Valid_Conversion -- | |
9977 | ---------------------- | |
9978 | ||
9979 | function Valid_Conversion | |
9980 | (N : Node_Id; | |
9981 | Target : Entity_Id; | |
0ab80019 | 9982 | Operand : Node_Id) return Boolean |
996ae0b0 | 9983 | is |
fbf5a39b | 9984 | Target_Type : constant Entity_Id := Base_Type (Target); |
996ae0b0 RK |
9985 | Opnd_Type : Entity_Id := Etype (Operand); |
9986 | ||
9987 | function Conversion_Check | |
9988 | (Valid : Boolean; | |
0ab80019 | 9989 | Msg : String) return Boolean; |
996ae0b0 RK |
9990 | -- Little routine to post Msg if Valid is False, returns Valid value |
9991 | ||
9992 | function Valid_Tagged_Conversion | |
9993 | (Target_Type : Entity_Id; | |
0ab80019 | 9994 | Opnd_Type : Entity_Id) return Boolean; |
996ae0b0 RK |
9995 | -- Specifically test for validity of tagged conversions |
9996 | ||
aa180613 | 9997 | function Valid_Array_Conversion return Boolean; |
4adf3c50 AC |
9998 | -- Check index and component conformance, and accessibility levels if |
9999 | -- the component types are anonymous access types (Ada 2005). | |
aa180613 | 10000 | |
996ae0b0 RK |
10001 | ---------------------- |
10002 | -- Conversion_Check -- | |
10003 | ---------------------- | |
10004 | ||
10005 | function Conversion_Check | |
10006 | (Valid : Boolean; | |
0ab80019 | 10007 | Msg : String) return Boolean |
996ae0b0 RK |
10008 | is |
10009 | begin | |
10010 | if not Valid then | |
10011 | Error_Msg_N (Msg, Operand); | |
10012 | end if; | |
10013 | ||
10014 | return Valid; | |
10015 | end Conversion_Check; | |
10016 | ||
aa180613 RD |
10017 | ---------------------------- |
10018 | -- Valid_Array_Conversion -- | |
10019 | ---------------------------- | |
10020 | ||
10021 | function Valid_Array_Conversion return Boolean | |
10022 | is | |
10023 | Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type); | |
10024 | Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type); | |
10025 | ||
10026 | Opnd_Index : Node_Id; | |
10027 | Opnd_Index_Type : Entity_Id; | |
10028 | ||
10029 | Target_Comp_Type : constant Entity_Id := | |
10030 | Component_Type (Target_Type); | |
10031 | Target_Comp_Base : constant Entity_Id := | |
10032 | Base_Type (Target_Comp_Type); | |
10033 | ||
10034 | Target_Index : Node_Id; | |
10035 | Target_Index_Type : Entity_Id; | |
10036 | ||
10037 | begin | |
10038 | -- Error if wrong number of dimensions | |
10039 | ||
10040 | if | |
10041 | Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type) | |
10042 | then | |
10043 | Error_Msg_N | |
10044 | ("incompatible number of dimensions for conversion", Operand); | |
10045 | return False; | |
10046 | ||
10047 | -- Number of dimensions matches | |
10048 | ||
10049 | else | |
10050 | -- Loop through indexes of the two arrays | |
10051 | ||
10052 | Target_Index := First_Index (Target_Type); | |
10053 | Opnd_Index := First_Index (Opnd_Type); | |
10054 | while Present (Target_Index) and then Present (Opnd_Index) loop | |
10055 | Target_Index_Type := Etype (Target_Index); | |
10056 | Opnd_Index_Type := Etype (Opnd_Index); | |
10057 | ||
10058 | -- Error if index types are incompatible | |
10059 | ||
10060 | if not (Is_Integer_Type (Target_Index_Type) | |
10061 | and then Is_Integer_Type (Opnd_Index_Type)) | |
10062 | and then (Root_Type (Target_Index_Type) | |
10063 | /= Root_Type (Opnd_Index_Type)) | |
10064 | then | |
10065 | Error_Msg_N | |
10066 | ("incompatible index types for array conversion", | |
10067 | Operand); | |
10068 | return False; | |
10069 | end if; | |
10070 | ||
10071 | Next_Index (Target_Index); | |
10072 | Next_Index (Opnd_Index); | |
10073 | end loop; | |
10074 | ||
10075 | -- If component types have same base type, all set | |
10076 | ||
10077 | if Target_Comp_Base = Opnd_Comp_Base then | |
10078 | null; | |
10079 | ||
10080 | -- Here if base types of components are not the same. The only | |
10081 | -- time this is allowed is if we have anonymous access types. | |
10082 | ||
10083 | -- The conversion of arrays of anonymous access types can lead | |
10084 | -- to dangling pointers. AI-392 formalizes the accessibility | |
10085 | -- checks that must be applied to such conversions to prevent | |
10086 | -- out-of-scope references. | |
10087 | ||
19fb051c AC |
10088 | elsif Ekind_In |
10089 | (Target_Comp_Base, E_Anonymous_Access_Type, | |
10090 | E_Anonymous_Access_Subprogram_Type) | |
aa180613 RD |
10091 | and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base) |
10092 | and then | |
10093 | Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type) | |
10094 | then | |
10095 | if Type_Access_Level (Target_Type) < | |
10096 | Type_Access_Level (Opnd_Type) | |
10097 | then | |
10098 | if In_Instance_Body then | |
10099 | Error_Msg_N ("?source array type " & | |
10100 | "has deeper accessibility level than target", Operand); | |
10101 | Error_Msg_N ("\?Program_Error will be raised at run time", | |
10102 | Operand); | |
10103 | Rewrite (N, | |
10104 | Make_Raise_Program_Error (Sloc (N), | |
10105 | Reason => PE_Accessibility_Check_Failed)); | |
10106 | Set_Etype (N, Target_Type); | |
10107 | return False; | |
10108 | ||
10109 | -- Conversion not allowed because of accessibility levels | |
10110 | ||
10111 | else | |
10112 | Error_Msg_N ("source array type " & | |
10113 | "has deeper accessibility level than target", Operand); | |
10114 | return False; | |
10115 | end if; | |
19fb051c | 10116 | |
aa180613 RD |
10117 | else |
10118 | null; | |
10119 | end if; | |
10120 | ||
10121 | -- All other cases where component base types do not match | |
10122 | ||
10123 | else | |
10124 | Error_Msg_N | |
10125 | ("incompatible component types for array conversion", | |
10126 | Operand); | |
10127 | return False; | |
10128 | end if; | |
10129 | ||
45fc7ddb HK |
10130 | -- Check that component subtypes statically match. For numeric |
10131 | -- types this means that both must be either constrained or | |
10132 | -- unconstrained. For enumeration types the bounds must match. | |
10133 | -- All of this is checked in Subtypes_Statically_Match. | |
aa180613 | 10134 | |
45fc7ddb | 10135 | if not Subtypes_Statically_Match |
aa180613 RD |
10136 | (Target_Comp_Type, Opnd_Comp_Type) |
10137 | then | |
10138 | Error_Msg_N | |
10139 | ("component subtypes must statically match", Operand); | |
10140 | return False; | |
10141 | end if; | |
10142 | end if; | |
10143 | ||
10144 | return True; | |
10145 | end Valid_Array_Conversion; | |
10146 | ||
996ae0b0 RK |
10147 | ----------------------------- |
10148 | -- Valid_Tagged_Conversion -- | |
10149 | ----------------------------- | |
10150 | ||
10151 | function Valid_Tagged_Conversion | |
10152 | (Target_Type : Entity_Id; | |
0ab80019 | 10153 | Opnd_Type : Entity_Id) return Boolean |
996ae0b0 RK |
10154 | is |
10155 | begin | |
a77842bd | 10156 | -- Upward conversions are allowed (RM 4.6(22)) |
996ae0b0 RK |
10157 | |
10158 | if Covers (Target_Type, Opnd_Type) | |
10159 | or else Is_Ancestor (Target_Type, Opnd_Type) | |
10160 | then | |
10161 | return True; | |
10162 | ||
a77842bd TQ |
10163 | -- Downward conversion are allowed if the operand is class-wide |
10164 | -- (RM 4.6(23)). | |
996ae0b0 RK |
10165 | |
10166 | elsif Is_Class_Wide_Type (Opnd_Type) | |
b7d1f17f | 10167 | and then Covers (Opnd_Type, Target_Type) |
996ae0b0 RK |
10168 | then |
10169 | return True; | |
10170 | ||
10171 | elsif Covers (Opnd_Type, Target_Type) | |
10172 | or else Is_Ancestor (Opnd_Type, Target_Type) | |
10173 | then | |
10174 | return | |
10175 | Conversion_Check (False, | |
10176 | "downward conversion of tagged objects not allowed"); | |
758c442c | 10177 | |
0669bebe GB |
10178 | -- Ada 2005 (AI-251): The conversion to/from interface types is |
10179 | -- always valid | |
758c442c | 10180 | |
0669bebe | 10181 | elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then |
758c442c GD |
10182 | return True; |
10183 | ||
b7d1f17f HK |
10184 | -- If the operand is a class-wide type obtained through a limited_ |
10185 | -- with clause, and the context includes the non-limited view, use | |
10186 | -- it to determine whether the conversion is legal. | |
10187 | ||
10188 | elsif Is_Class_Wide_Type (Opnd_Type) | |
10189 | and then From_With_Type (Opnd_Type) | |
10190 | and then Present (Non_Limited_View (Etype (Opnd_Type))) | |
10191 | and then Is_Interface (Non_Limited_View (Etype (Opnd_Type))) | |
10192 | then | |
10193 | return True; | |
10194 | ||
aa180613 RD |
10195 | elsif Is_Access_Type (Opnd_Type) |
10196 | and then Is_Interface (Directly_Designated_Type (Opnd_Type)) | |
10197 | then | |
10198 | return True; | |
10199 | ||
996ae0b0 RK |
10200 | else |
10201 | Error_Msg_NE | |
10202 | ("invalid tagged conversion, not compatible with}", | |
10203 | N, First_Subtype (Opnd_Type)); | |
10204 | return False; | |
10205 | end if; | |
10206 | end Valid_Tagged_Conversion; | |
10207 | ||
10208 | -- Start of processing for Valid_Conversion | |
10209 | ||
10210 | begin | |
10211 | Check_Parameterless_Call (Operand); | |
10212 | ||
10213 | if Is_Overloaded (Operand) then | |
10214 | declare | |
10215 | I : Interp_Index; | |
10216 | I1 : Interp_Index; | |
10217 | It : Interp; | |
10218 | It1 : Interp; | |
10219 | N1 : Entity_Id; | |
f0d10385 | 10220 | T1 : Entity_Id; |
996ae0b0 RK |
10221 | |
10222 | begin | |
d81b4bfe TQ |
10223 | -- Remove procedure calls, which syntactically cannot appear in |
10224 | -- this context, but which cannot be removed by type checking, | |
996ae0b0 RK |
10225 | -- because the context does not impose a type. |
10226 | ||
1420b484 JM |
10227 | -- When compiling for VMS, spurious ambiguities can be produced |
10228 | -- when arithmetic operations have a literal operand and return | |
10229 | -- System.Address or a descendant of it. These ambiguities are | |
10230 | -- otherwise resolved by the context, but for conversions there | |
10231 | -- is no context type and the removal of the spurious operations | |
10232 | -- must be done explicitly here. | |
10233 | ||
4adf3c50 AC |
10234 | -- The node may be labelled overloaded, but still contain only one |
10235 | -- interpretation because others were discarded earlier. If this | |
10236 | -- is the case, retain the single interpretation if legal. | |
9ebe3743 | 10237 | |
996ae0b0 | 10238 | Get_First_Interp (Operand, I, It); |
9ebe3743 HK |
10239 | Opnd_Type := It.Typ; |
10240 | Get_Next_Interp (I, It); | |
996ae0b0 | 10241 | |
9ebe3743 HK |
10242 | if Present (It.Typ) |
10243 | and then Opnd_Type /= Standard_Void_Type | |
10244 | then | |
10245 | -- More than one candidate interpretation is available | |
996ae0b0 | 10246 | |
9ebe3743 HK |
10247 | Get_First_Interp (Operand, I, It); |
10248 | while Present (It.Typ) loop | |
10249 | if It.Typ = Standard_Void_Type then | |
10250 | Remove_Interp (I); | |
10251 | end if; | |
1420b484 | 10252 | |
9ebe3743 HK |
10253 | if Present (System_Aux_Id) |
10254 | and then Is_Descendent_Of_Address (It.Typ) | |
10255 | then | |
10256 | Remove_Interp (I); | |
10257 | end if; | |
10258 | ||
10259 | Get_Next_Interp (I, It); | |
10260 | end loop; | |
10261 | end if; | |
996ae0b0 RK |
10262 | |
10263 | Get_First_Interp (Operand, I, It); | |
10264 | I1 := I; | |
10265 | It1 := It; | |
10266 | ||
10267 | if No (It.Typ) then | |
10268 | Error_Msg_N ("illegal operand in conversion", Operand); | |
10269 | return False; | |
10270 | end if; | |
10271 | ||
10272 | Get_Next_Interp (I, It); | |
10273 | ||
10274 | if Present (It.Typ) then | |
10275 | N1 := It1.Nam; | |
f0d10385 | 10276 | T1 := It1.Typ; |
996ae0b0 RK |
10277 | It1 := Disambiguate (Operand, I1, I, Any_Type); |
10278 | ||
10279 | if It1 = No_Interp then | |
10280 | Error_Msg_N ("ambiguous operand in conversion", Operand); | |
10281 | ||
f0d10385 AC |
10282 | -- If the interpretation involves a standard operator, use |
10283 | -- the location of the type, which may be user-defined. | |
10284 | ||
10285 | if Sloc (It.Nam) = Standard_Location then | |
10286 | Error_Msg_Sloc := Sloc (It.Typ); | |
10287 | else | |
10288 | Error_Msg_Sloc := Sloc (It.Nam); | |
10289 | end if; | |
10290 | ||
4e7a4f6e AC |
10291 | Error_Msg_N -- CODEFIX |
10292 | ("\\possible interpretation#!", Operand); | |
996ae0b0 | 10293 | |
f0d10385 AC |
10294 | if Sloc (N1) = Standard_Location then |
10295 | Error_Msg_Sloc := Sloc (T1); | |
10296 | else | |
10297 | Error_Msg_Sloc := Sloc (N1); | |
10298 | end if; | |
10299 | ||
4e7a4f6e AC |
10300 | Error_Msg_N -- CODEFIX |
10301 | ("\\possible interpretation#!", Operand); | |
996ae0b0 RK |
10302 | |
10303 | return False; | |
10304 | end if; | |
10305 | end if; | |
10306 | ||
10307 | Set_Etype (Operand, It1.Typ); | |
10308 | Opnd_Type := It1.Typ; | |
10309 | end; | |
10310 | end if; | |
10311 | ||
aa180613 | 10312 | -- Numeric types |
996ae0b0 | 10313 | |
aa180613 | 10314 | if Is_Numeric_Type (Target_Type) then |
996ae0b0 | 10315 | |
aa180613 | 10316 | -- A universal fixed expression can be converted to any numeric type |
996ae0b0 | 10317 | |
996ae0b0 RK |
10318 | if Opnd_Type = Universal_Fixed then |
10319 | return True; | |
7324bf49 | 10320 | |
aa180613 RD |
10321 | -- Also no need to check when in an instance or inlined body, because |
10322 | -- the legality has been established when the template was analyzed. | |
10323 | -- Furthermore, numeric conversions may occur where only a private | |
f3d57416 | 10324 | -- view of the operand type is visible at the instantiation point. |
aa180613 RD |
10325 | -- This results in a spurious error if we check that the operand type |
10326 | -- is a numeric type. | |
10327 | ||
10328 | -- Note: in a previous version of this unit, the following tests were | |
10329 | -- applied only for generated code (Comes_From_Source set to False), | |
10330 | -- but in fact the test is required for source code as well, since | |
10331 | -- this situation can arise in source code. | |
10332 | ||
10333 | elsif In_Instance or else In_Inlined_Body then | |
d347f572 | 10334 | return True; |
aa180613 RD |
10335 | |
10336 | -- Otherwise we need the conversion check | |
7324bf49 | 10337 | |
996ae0b0 | 10338 | else |
aa180613 RD |
10339 | return Conversion_Check |
10340 | (Is_Numeric_Type (Opnd_Type), | |
10341 | "illegal operand for numeric conversion"); | |
996ae0b0 RK |
10342 | end if; |
10343 | ||
aa180613 RD |
10344 | -- Array types |
10345 | ||
996ae0b0 RK |
10346 | elsif Is_Array_Type (Target_Type) then |
10347 | if not Is_Array_Type (Opnd_Type) | |
10348 | or else Opnd_Type = Any_Composite | |
10349 | or else Opnd_Type = Any_String | |
10350 | then | |
4adf3c50 | 10351 | Error_Msg_N ("illegal operand for array conversion", Operand); |
996ae0b0 | 10352 | return False; |
996ae0b0 | 10353 | else |
aa180613 | 10354 | return Valid_Array_Conversion; |
996ae0b0 RK |
10355 | end if; |
10356 | ||
e65f50ec ES |
10357 | -- Ada 2005 (AI-251): Anonymous access types where target references an |
10358 | -- interface type. | |
758c442c | 10359 | |
964f13da RD |
10360 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10361 | E_Anonymous_Access_Type) | |
758c442c GD |
10362 | and then Is_Interface (Directly_Designated_Type (Target_Type)) |
10363 | then | |
10364 | -- Check the static accessibility rule of 4.6(17). Note that the | |
d81b4bfe TQ |
10365 | -- check is not enforced when within an instance body, since the |
10366 | -- RM requires such cases to be caught at run time. | |
758c442c GD |
10367 | |
10368 | if Ekind (Target_Type) /= E_Anonymous_Access_Type then | |
10369 | if Type_Access_Level (Opnd_Type) > | |
10370 | Type_Access_Level (Target_Type) | |
10371 | then | |
10372 | -- In an instance, this is a run-time check, but one we know | |
10373 | -- will fail, so generate an appropriate warning. The raise | |
10374 | -- will be generated by Expand_N_Type_Conversion. | |
10375 | ||
10376 | if In_Instance_Body then | |
10377 | Error_Msg_N | |
10378 | ("?cannot convert local pointer to non-local access type", | |
10379 | Operand); | |
10380 | Error_Msg_N | |
c8ef728f | 10381 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
10382 | else |
10383 | Error_Msg_N | |
10384 | ("cannot convert local pointer to non-local access type", | |
10385 | Operand); | |
10386 | return False; | |
10387 | end if; | |
10388 | ||
10389 | -- Special accessibility checks are needed in the case of access | |
10390 | -- discriminants declared for a limited type. | |
10391 | ||
10392 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
10393 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
10394 | then | |
10395 | -- When the operand is a selected access discriminant the check | |
10396 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
10397 | -- the prefix of the selected name (Object_Access_Level handles |
10398 | -- checking the prefix of the operand for this case). | |
758c442c GD |
10399 | |
10400 | if Nkind (Operand) = N_Selected_Component | |
c8ef728f | 10401 | and then Object_Access_Level (Operand) > |
45fc7ddb | 10402 | Type_Access_Level (Target_Type) |
758c442c | 10403 | then |
d81b4bfe TQ |
10404 | -- In an instance, this is a run-time check, but one we know |
10405 | -- will fail, so generate an appropriate warning. The raise | |
10406 | -- will be generated by Expand_N_Type_Conversion. | |
758c442c GD |
10407 | |
10408 | if In_Instance_Body then | |
10409 | Error_Msg_N | |
10410 | ("?cannot convert access discriminant to non-local" & | |
10411 | " access type", Operand); | |
10412 | Error_Msg_N | |
c8ef728f | 10413 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
10414 | else |
10415 | Error_Msg_N | |
10416 | ("cannot convert access discriminant to non-local" & | |
10417 | " access type", Operand); | |
10418 | return False; | |
10419 | end if; | |
10420 | end if; | |
10421 | ||
10422 | -- The case of a reference to an access discriminant from | |
10423 | -- within a limited type declaration (which will appear as | |
10424 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 10425 | -- discriminant is considered to be deeper than any (nameable) |
758c442c GD |
10426 | -- access type. |
10427 | ||
10428 | if Is_Entity_Name (Operand) | |
10429 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
964f13da RD |
10430 | and then |
10431 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
758c442c GD |
10432 | and then Present (Discriminal_Link (Entity (Operand))) |
10433 | then | |
10434 | Error_Msg_N | |
10435 | ("discriminant has deeper accessibility level than target", | |
10436 | Operand); | |
10437 | return False; | |
10438 | end if; | |
10439 | end if; | |
10440 | end if; | |
10441 | ||
10442 | return True; | |
10443 | ||
aa180613 RD |
10444 | -- General and anonymous access types |
10445 | ||
964f13da RD |
10446 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10447 | E_Anonymous_Access_Type) | |
996ae0b0 RK |
10448 | and then |
10449 | Conversion_Check | |
10450 | (Is_Access_Type (Opnd_Type) | |
964f13da RD |
10451 | and then not |
10452 | Ekind_In (Opnd_Type, E_Access_Subprogram_Type, | |
10453 | E_Access_Protected_Subprogram_Type), | |
996ae0b0 RK |
10454 | "must be an access-to-object type") |
10455 | then | |
10456 | if Is_Access_Constant (Opnd_Type) | |
10457 | and then not Is_Access_Constant (Target_Type) | |
10458 | then | |
10459 | Error_Msg_N | |
10460 | ("access-to-constant operand type not allowed", Operand); | |
10461 | return False; | |
10462 | end if; | |
10463 | ||
758c442c GD |
10464 | -- Check the static accessibility rule of 4.6(17). Note that the |
10465 | -- check is not enforced when within an instance body, since the RM | |
10466 | -- requires such cases to be caught at run time. | |
996ae0b0 | 10467 | |
758c442c GD |
10468 | if Ekind (Target_Type) /= E_Anonymous_Access_Type |
10469 | or else Is_Local_Anonymous_Access (Target_Type) | |
10470 | then | |
996ae0b0 RK |
10471 | if Type_Access_Level (Opnd_Type) |
10472 | > Type_Access_Level (Target_Type) | |
10473 | then | |
d81b4bfe TQ |
10474 | -- In an instance, this is a run-time check, but one we know |
10475 | -- will fail, so generate an appropriate warning. The raise | |
10476 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
10477 | |
10478 | if In_Instance_Body then | |
10479 | Error_Msg_N | |
10480 | ("?cannot convert local pointer to non-local access type", | |
10481 | Operand); | |
10482 | Error_Msg_N | |
c8ef728f | 10483 | ("\?Program_Error will be raised at run time", Operand); |
996ae0b0 RK |
10484 | |
10485 | else | |
b90cfacd HK |
10486 | -- Avoid generation of spurious error message |
10487 | ||
10488 | if not Error_Posted (N) then | |
10489 | Error_Msg_N | |
10490 | ("cannot convert local pointer to non-local access type", | |
10491 | Operand); | |
10492 | end if; | |
10493 | ||
996ae0b0 RK |
10494 | return False; |
10495 | end if; | |
10496 | ||
758c442c GD |
10497 | -- Special accessibility checks are needed in the case of access |
10498 | -- discriminants declared for a limited type. | |
10499 | ||
10500 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
10501 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
10502 | then | |
758c442c GD |
10503 | -- When the operand is a selected access discriminant the check |
10504 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
10505 | -- the prefix of the selected name (Object_Access_Level handles |
10506 | -- checking the prefix of the operand for this case). | |
996ae0b0 RK |
10507 | |
10508 | if Nkind (Operand) = N_Selected_Component | |
45fc7ddb HK |
10509 | and then Object_Access_Level (Operand) > |
10510 | Type_Access_Level (Target_Type) | |
996ae0b0 | 10511 | then |
d81b4bfe TQ |
10512 | -- In an instance, this is a run-time check, but one we know |
10513 | -- will fail, so generate an appropriate warning. The raise | |
10514 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
10515 | |
10516 | if In_Instance_Body then | |
10517 | Error_Msg_N | |
10518 | ("?cannot convert access discriminant to non-local" & | |
10519 | " access type", Operand); | |
10520 | Error_Msg_N | |
c8ef728f ES |
10521 | ("\?Program_Error will be raised at run time", |
10522 | Operand); | |
996ae0b0 RK |
10523 | |
10524 | else | |
10525 | Error_Msg_N | |
10526 | ("cannot convert access discriminant to non-local" & | |
10527 | " access type", Operand); | |
10528 | return False; | |
10529 | end if; | |
10530 | end if; | |
10531 | ||
758c442c GD |
10532 | -- The case of a reference to an access discriminant from |
10533 | -- within a limited type declaration (which will appear as | |
10534 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 10535 | -- discriminant is considered to be deeper than any (nameable) |
758c442c | 10536 | -- access type. |
996ae0b0 RK |
10537 | |
10538 | if Is_Entity_Name (Operand) | |
964f13da RD |
10539 | and then |
10540 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
996ae0b0 RK |
10541 | and then Present (Discriminal_Link (Entity (Operand))) |
10542 | then | |
10543 | Error_Msg_N | |
10544 | ("discriminant has deeper accessibility level than target", | |
10545 | Operand); | |
10546 | return False; | |
10547 | end if; | |
10548 | end if; | |
10549 | end if; | |
10550 | ||
14e33999 AC |
10551 | -- In the presence of limited_with clauses we have to use non-limited |
10552 | -- views, if available. | |
d81b4bfe | 10553 | |
14e33999 | 10554 | Check_Limited : declare |
0669bebe GB |
10555 | function Full_Designated_Type (T : Entity_Id) return Entity_Id; |
10556 | -- Helper function to handle limited views | |
10557 | ||
10558 | -------------------------- | |
10559 | -- Full_Designated_Type -- | |
10560 | -------------------------- | |
10561 | ||
10562 | function Full_Designated_Type (T : Entity_Id) return Entity_Id is | |
950d217a | 10563 | Desig : constant Entity_Id := Designated_Type (T); |
c0985d4e | 10564 | |
0669bebe | 10565 | begin |
950d217a AC |
10566 | -- Handle the limited view of a type |
10567 | ||
c0985d4e HK |
10568 | if Is_Incomplete_Type (Desig) |
10569 | and then From_With_Type (Desig) | |
0669bebe GB |
10570 | and then Present (Non_Limited_View (Desig)) |
10571 | then | |
950d217a AC |
10572 | return Available_View (Desig); |
10573 | else | |
10574 | return Desig; | |
0669bebe GB |
10575 | end if; |
10576 | end Full_Designated_Type; | |
10577 | ||
d81b4bfe TQ |
10578 | -- Local Declarations |
10579 | ||
0669bebe GB |
10580 | Target : constant Entity_Id := Full_Designated_Type (Target_Type); |
10581 | Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type); | |
10582 | ||
10583 | Same_Base : constant Boolean := | |
10584 | Base_Type (Target) = Base_Type (Opnd); | |
996ae0b0 | 10585 | |
14e33999 | 10586 | -- Start of processing for Check_Limited |
d81b4bfe | 10587 | |
996ae0b0 RK |
10588 | begin |
10589 | if Is_Tagged_Type (Target) then | |
10590 | return Valid_Tagged_Conversion (Target, Opnd); | |
10591 | ||
10592 | else | |
0669bebe | 10593 | if not Same_Base then |
996ae0b0 RK |
10594 | Error_Msg_NE |
10595 | ("target designated type not compatible with }", | |
10596 | N, Base_Type (Opnd)); | |
10597 | return False; | |
10598 | ||
da709d08 AC |
10599 | -- Ada 2005 AI-384: legality rule is symmetric in both |
10600 | -- designated types. The conversion is legal (with possible | |
10601 | -- constraint check) if either designated type is | |
10602 | -- unconstrained. | |
10603 | ||
10604 | elsif Subtypes_Statically_Match (Target, Opnd) | |
10605 | or else | |
10606 | (Has_Discriminants (Target) | |
10607 | and then | |
10608 | (not Is_Constrained (Opnd) | |
10609 | or else not Is_Constrained (Target))) | |
996ae0b0 | 10610 | then |
9fa33291 RD |
10611 | -- Special case, if Value_Size has been used to make the |
10612 | -- sizes different, the conversion is not allowed even | |
10613 | -- though the subtypes statically match. | |
10614 | ||
10615 | if Known_Static_RM_Size (Target) | |
10616 | and then Known_Static_RM_Size (Opnd) | |
10617 | and then RM_Size (Target) /= RM_Size (Opnd) | |
10618 | then | |
10619 | Error_Msg_NE | |
10620 | ("target designated subtype not compatible with }", | |
10621 | N, Opnd); | |
10622 | Error_Msg_NE | |
10623 | ("\because sizes of the two designated subtypes differ", | |
10624 | N, Opnd); | |
10625 | return False; | |
10626 | ||
10627 | -- Normal case where conversion is allowed | |
10628 | ||
10629 | else | |
10630 | return True; | |
10631 | end if; | |
da709d08 AC |
10632 | |
10633 | else | |
996ae0b0 RK |
10634 | Error_Msg_NE |
10635 | ("target designated subtype not compatible with }", | |
10636 | N, Opnd); | |
10637 | return False; | |
996ae0b0 RK |
10638 | end if; |
10639 | end if; | |
14e33999 | 10640 | end Check_Limited; |
996ae0b0 | 10641 | |
cdbf04c0 | 10642 | -- Access to subprogram types. If the operand is an access parameter, |
4adf3c50 AC |
10643 | -- the type has a deeper accessibility that any master, and cannot be |
10644 | -- assigned. We must make an exception if the conversion is part of an | |
10645 | -- assignment and the target is the return object of an extended return | |
10646 | -- statement, because in that case the accessibility check takes place | |
10647 | -- after the return. | |
aa180613 | 10648 | |
dce86910 | 10649 | elsif Is_Access_Subprogram_Type (Target_Type) |
bc5f3720 | 10650 | and then No (Corresponding_Remote_Type (Opnd_Type)) |
996ae0b0 | 10651 | then |
cdbf04c0 AC |
10652 | if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type |
10653 | and then Is_Entity_Name (Operand) | |
10654 | and then Ekind (Entity (Operand)) = E_In_Parameter | |
53cf4600 ES |
10655 | and then |
10656 | (Nkind (Parent (N)) /= N_Assignment_Statement | |
10657 | or else not Is_Entity_Name (Name (Parent (N))) | |
10658 | or else not Is_Return_Object (Entity (Name (Parent (N))))) | |
0669bebe GB |
10659 | then |
10660 | Error_Msg_N | |
10661 | ("illegal attempt to store anonymous access to subprogram", | |
10662 | Operand); | |
10663 | Error_Msg_N | |
10664 | ("\value has deeper accessibility than any master " & | |
aa5147f0 | 10665 | "(RM 3.10.2 (13))", |
0669bebe GB |
10666 | Operand); |
10667 | ||
c147ac26 ES |
10668 | Error_Msg_NE |
10669 | ("\use named access type for& instead of access parameter", | |
10670 | Operand, Entity (Operand)); | |
0669bebe GB |
10671 | end if; |
10672 | ||
996ae0b0 RK |
10673 | -- Check that the designated types are subtype conformant |
10674 | ||
bc5f3720 RD |
10675 | Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type), |
10676 | Old_Id => Designated_Type (Opnd_Type), | |
10677 | Err_Loc => N); | |
996ae0b0 RK |
10678 | |
10679 | -- Check the static accessibility rule of 4.6(20) | |
10680 | ||
10681 | if Type_Access_Level (Opnd_Type) > | |
10682 | Type_Access_Level (Target_Type) | |
10683 | then | |
10684 | Error_Msg_N | |
10685 | ("operand type has deeper accessibility level than target", | |
10686 | Operand); | |
10687 | ||
10688 | -- Check that if the operand type is declared in a generic body, | |
10689 | -- then the target type must be declared within that same body | |
10690 | -- (enforces last sentence of 4.6(20)). | |
10691 | ||
10692 | elsif Present (Enclosing_Generic_Body (Opnd_Type)) then | |
10693 | declare | |
10694 | O_Gen : constant Node_Id := | |
10695 | Enclosing_Generic_Body (Opnd_Type); | |
10696 | ||
1420b484 | 10697 | T_Gen : Node_Id; |
996ae0b0 RK |
10698 | |
10699 | begin | |
1420b484 | 10700 | T_Gen := Enclosing_Generic_Body (Target_Type); |
996ae0b0 RK |
10701 | while Present (T_Gen) and then T_Gen /= O_Gen loop |
10702 | T_Gen := Enclosing_Generic_Body (T_Gen); | |
10703 | end loop; | |
10704 | ||
10705 | if T_Gen /= O_Gen then | |
10706 | Error_Msg_N | |
10707 | ("target type must be declared in same generic body" | |
10708 | & " as operand type", N); | |
10709 | end if; | |
10710 | end; | |
10711 | end if; | |
10712 | ||
10713 | return True; | |
10714 | ||
aa180613 RD |
10715 | -- Remote subprogram access types |
10716 | ||
996ae0b0 RK |
10717 | elsif Is_Remote_Access_To_Subprogram_Type (Target_Type) |
10718 | and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type) | |
10719 | then | |
10720 | -- It is valid to convert from one RAS type to another provided | |
10721 | -- that their specification statically match. | |
10722 | ||
10723 | Check_Subtype_Conformant | |
10724 | (New_Id => | |
10725 | Designated_Type (Corresponding_Remote_Type (Target_Type)), | |
10726 | Old_Id => | |
10727 | Designated_Type (Corresponding_Remote_Type (Opnd_Type)), | |
10728 | Err_Loc => | |
10729 | N); | |
10730 | return True; | |
aa180613 | 10731 | |
e65f50ec | 10732 | -- If both are tagged types, check legality of view conversions |
996ae0b0 | 10733 | |
e65f50ec | 10734 | elsif Is_Tagged_Type (Target_Type) |
4adf3c50 AC |
10735 | and then |
10736 | Is_Tagged_Type (Opnd_Type) | |
e65f50ec | 10737 | then |
996ae0b0 RK |
10738 | return Valid_Tagged_Conversion (Target_Type, Opnd_Type); |
10739 | ||
a77842bd | 10740 | -- Types derived from the same root type are convertible |
996ae0b0 RK |
10741 | |
10742 | elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then | |
10743 | return True; | |
10744 | ||
4adf3c50 AC |
10745 | -- In an instance or an inlined body, there may be inconsistent views of |
10746 | -- the same type, or of types derived from a common root. | |
996ae0b0 | 10747 | |
aa5147f0 ES |
10748 | elsif (In_Instance or In_Inlined_Body) |
10749 | and then | |
d81b4bfe TQ |
10750 | Root_Type (Underlying_Type (Target_Type)) = |
10751 | Root_Type (Underlying_Type (Opnd_Type)) | |
996ae0b0 RK |
10752 | then |
10753 | return True; | |
10754 | ||
10755 | -- Special check for common access type error case | |
10756 | ||
10757 | elsif Ekind (Target_Type) = E_Access_Type | |
10758 | and then Is_Access_Type (Opnd_Type) | |
10759 | then | |
10760 | Error_Msg_N ("target type must be general access type!", N); | |
305caf42 AC |
10761 | Error_Msg_NE -- CODEFIX |
10762 | ("add ALL to }!", N, Target_Type); | |
996ae0b0 RK |
10763 | return False; |
10764 | ||
10765 | else | |
10766 | Error_Msg_NE ("invalid conversion, not compatible with }", | |
10767 | N, Opnd_Type); | |
996ae0b0 RK |
10768 | return False; |
10769 | end if; | |
10770 | end Valid_Conversion; | |
10771 | ||
10772 | end Sem_Res; |