]>
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 | -- -- | |
67bdbf1e | 9 | -- Copyright (C) 1992-2012, 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; | |
31 | with Errout; use Errout; | |
32 | with Expander; use Expander; | |
758c442c | 33 | with Exp_Disp; use Exp_Disp; |
0669bebe | 34 | with Exp_Ch6; use Exp_Ch6; |
996ae0b0 | 35 | with Exp_Ch7; use Exp_Ch7; |
fbf5a39b | 36 | with Exp_Tss; use Exp_Tss; |
996ae0b0 | 37 | with Exp_Util; use Exp_Util; |
dae2b8ea | 38 | with Fname; use Fname; |
996ae0b0 RK |
39 | with Freeze; use Freeze; |
40 | with Itypes; use Itypes; | |
41 | with Lib; use Lib; | |
42 | with Lib.Xref; use Lib.Xref; | |
43 | with Namet; use Namet; | |
44 | with Nmake; use Nmake; | |
45 | with Nlists; use Nlists; | |
46 | with Opt; use Opt; | |
47 | with Output; use Output; | |
48 | with Restrict; use Restrict; | |
6e937c1c | 49 | with Rident; use Rident; |
996ae0b0 RK |
50 | with Rtsfind; use Rtsfind; |
51 | with Sem; use Sem; | |
a4100e55 | 52 | with Sem_Aux; use Sem_Aux; |
996ae0b0 RK |
53 | with Sem_Aggr; use Sem_Aggr; |
54 | with Sem_Attr; use Sem_Attr; | |
55 | with Sem_Cat; use Sem_Cat; | |
56 | with Sem_Ch4; use Sem_Ch4; | |
57 | with Sem_Ch6; use Sem_Ch6; | |
58 | with Sem_Ch8; use Sem_Ch8; | |
4b92fd3c | 59 | with Sem_Ch13; use Sem_Ch13; |
dec6faf1 | 60 | with Sem_Dim; use Sem_Dim; |
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; | |
ce72a9a3 | 68 | with Targparm; use Targparm; |
996ae0b0 RK |
69 | with Sem_Type; use Sem_Type; |
70 | with Sem_Warn; use Sem_Warn; | |
71 | with Sinfo; use Sinfo; | |
f4b049db | 72 | with Sinfo.CN; use Sinfo.CN; |
fbf5a39b | 73 | with Snames; use Snames; |
996ae0b0 RK |
74 | with Stand; use Stand; |
75 | with Stringt; use Stringt; | |
45fc7ddb | 76 | with Style; use Style; |
996ae0b0 RK |
77 | with Tbuild; use Tbuild; |
78 | with Uintp; use Uintp; | |
79 | with Urealp; use Urealp; | |
80 | ||
81 | package body Sem_Res is | |
82 | ||
83 | ----------------------- | |
84 | -- Local Subprograms -- | |
85 | ----------------------- | |
86 | ||
87 | -- Second pass (top-down) type checking and overload resolution procedures | |
5cc9353d RD |
88 | -- Typ is the type required by context. These procedures propagate the type |
89 | -- information recursively to the descendants of N. If the node is not | |
90 | -- overloaded, its Etype is established in the first pass. If overloaded, | |
91 | -- the Resolve routines set the correct type. For arith. operators, the | |
92 | -- Etype is the base type of the context. | |
996ae0b0 RK |
93 | |
94 | -- Note that Resolve_Attribute is separated off in Sem_Attr | |
95 | ||
bd29d519 AC |
96 | function Bad_Unordered_Enumeration_Reference |
97 | (N : Node_Id; | |
98 | T : Entity_Id) return Boolean; | |
99 | -- Node N contains a potentially dubious reference to type T, either an | |
100 | -- explicit comparison, or an explicit range. This function returns True | |
101 | -- if the type T is an enumeration type for which No pragma Order has been | |
102 | -- given, and the reference N is not in the same extended source unit as | |
103 | -- the declaration of T. | |
104 | ||
996ae0b0 RK |
105 | procedure Check_Discriminant_Use (N : Node_Id); |
106 | -- Enforce the restrictions on the use of discriminants when constraining | |
107 | -- a component of a discriminated type (record or concurrent type). | |
108 | ||
109 | procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id); | |
110 | -- Given a node for an operator associated with type T, check that | |
111 | -- the operator is visible. Operators all of whose operands are | |
112 | -- universal must be checked for visibility during resolution | |
113 | -- because their type is not determinable based on their operands. | |
114 | ||
c8ef728f ES |
115 | procedure Check_Fully_Declared_Prefix |
116 | (Typ : Entity_Id; | |
117 | Pref : Node_Id); | |
118 | -- Check that the type of the prefix of a dereference is not incomplete | |
119 | ||
996ae0b0 RK |
120 | function Check_Infinite_Recursion (N : Node_Id) return Boolean; |
121 | -- Given a call node, N, which is known to occur immediately within the | |
122 | -- subprogram being called, determines whether it is a detectable case of | |
123 | -- an infinite recursion, and if so, outputs appropriate messages. Returns | |
124 | -- True if an infinite recursion is detected, and False otherwise. | |
125 | ||
126 | procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id); | |
127 | -- If the type of the object being initialized uses the secondary stack | |
128 | -- directly or indirectly, create a transient scope for the call to the | |
fbf5a39b AC |
129 | -- init proc. This is because we do not create transient scopes for the |
130 | -- initialization of individual components within the init proc itself. | |
996ae0b0 RK |
131 | -- Could be optimized away perhaps? |
132 | ||
f61580d4 | 133 | procedure Check_No_Direct_Boolean_Operators (N : Node_Id); |
6fb4cdde AC |
134 | -- N is the node for a logical operator. If the operator is predefined, and |
135 | -- the root type of the operands is Standard.Boolean, then a check is made | |
a36c1c3e RD |
136 | -- for restriction No_Direct_Boolean_Operators. This procedure also handles |
137 | -- the style check for Style_Check_Boolean_And_Or. | |
f61580d4 | 138 | |
67ce0d7e | 139 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean; |
5cc9353d RD |
140 | -- Determine whether E is an access type declared by an access declaration, |
141 | -- and not an (anonymous) allocator type. | |
67ce0d7e | 142 | |
996ae0b0 | 143 | function Is_Predefined_Op (Nam : Entity_Id) return Boolean; |
6a497607 AC |
144 | -- Utility to check whether the entity for an operator is a predefined |
145 | -- operator, in which case the expression is left as an operator in the | |
146 | -- tree (else it is rewritten into a call). An instance of an intrinsic | |
147 | -- conversion operation may be given an operator name, but is not treated | |
148 | -- like an operator. Note that an operator that is an imported back-end | |
149 | -- builtin has convention Intrinsic, but is expected to be rewritten into | |
150 | -- a call, so such an operator is not treated as predefined by this | |
151 | -- predicate. | |
996ae0b0 RK |
152 | |
153 | procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id); | |
154 | -- If a default expression in entry call N depends on the discriminants | |
155 | -- of the task, it must be replaced with a reference to the discriminant | |
156 | -- of the task being called. | |
157 | ||
10303118 BD |
158 | procedure Resolve_Op_Concat_Arg |
159 | (N : Node_Id; | |
160 | Arg : Node_Id; | |
161 | Typ : Entity_Id; | |
162 | Is_Comp : Boolean); | |
163 | -- Internal procedure for Resolve_Op_Concat to resolve one operand of | |
164 | -- concatenation operator. The operand is either of the array type or of | |
165 | -- the component type. If the operand is an aggregate, and the component | |
166 | -- type is composite, this is ambiguous if component type has aggregates. | |
167 | ||
168 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id); | |
169 | -- Does the first part of the work of Resolve_Op_Concat | |
170 | ||
171 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id); | |
172 | -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand | |
173 | -- has been resolved. See Resolve_Op_Concat for details. | |
174 | ||
996ae0b0 RK |
175 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id); |
176 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id); | |
177 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id); | |
19d846a0 | 178 | procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id); |
996ae0b0 RK |
179 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id); |
180 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id); | |
181 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id); | |
955871d3 | 182 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id); |
996ae0b0 RK |
183 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id); |
184 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id); | |
955871d3 | 185 | procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id); |
996ae0b0 RK |
186 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id); |
187 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id); | |
188 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id); | |
189 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id); | |
190 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id); | |
191 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id); | |
192 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id); | |
193 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id); | |
194 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id); | |
195 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id); | |
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 | 276 | |
996ae0b0 RK |
277 | ------------------------- |
278 | -- Ambiguous_Character -- | |
279 | ------------------------- | |
280 | ||
281 | procedure Ambiguous_Character (C : Node_Id) is | |
282 | E : Entity_Id; | |
283 | ||
284 | begin | |
285 | if Nkind (C) = N_Character_Literal then | |
ed2233dc | 286 | Error_Msg_N ("ambiguous character literal", C); |
b7d1f17f HK |
287 | |
288 | -- First the ones in Standard | |
289 | ||
ed2233dc AC |
290 | Error_Msg_N ("\\possible interpretation: Character!", C); |
291 | Error_Msg_N ("\\possible interpretation: Wide_Character!", C); | |
b7d1f17f HK |
292 | |
293 | -- Include Wide_Wide_Character in Ada 2005 mode | |
294 | ||
0791fbe9 | 295 | if Ada_Version >= Ada_2005 then |
ed2233dc | 296 | Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C); |
b7d1f17f HK |
297 | end if; |
298 | ||
299 | -- Now any other types that match | |
996ae0b0 RK |
300 | |
301 | E := Current_Entity (C); | |
1420b484 | 302 | while Present (E) loop |
ed2233dc | 303 | Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E)); |
1420b484 JM |
304 | E := Homonym (E); |
305 | end loop; | |
996ae0b0 RK |
306 | end if; |
307 | end Ambiguous_Character; | |
308 | ||
309 | ------------------------- | |
310 | -- Analyze_And_Resolve -- | |
311 | ------------------------- | |
312 | ||
313 | procedure Analyze_And_Resolve (N : Node_Id) is | |
314 | begin | |
315 | Analyze (N); | |
fbf5a39b | 316 | Resolve (N); |
996ae0b0 RK |
317 | end Analyze_And_Resolve; |
318 | ||
319 | procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is | |
320 | begin | |
321 | Analyze (N); | |
322 | Resolve (N, Typ); | |
323 | end Analyze_And_Resolve; | |
324 | ||
325 | -- Version withs check(s) suppressed | |
326 | ||
327 | procedure Analyze_And_Resolve | |
328 | (N : Node_Id; | |
329 | Typ : Entity_Id; | |
330 | Suppress : Check_Id) | |
331 | is | |
fbf5a39b | 332 | Scop : constant Entity_Id := Current_Scope; |
996ae0b0 RK |
333 | |
334 | begin | |
335 | if Suppress = All_Checks then | |
336 | declare | |
fbf5a39b | 337 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
338 | begin |
339 | Scope_Suppress := (others => True); | |
340 | Analyze_And_Resolve (N, Typ); | |
341 | Scope_Suppress := Svg; | |
342 | end; | |
343 | ||
344 | else | |
345 | declare | |
fbf5a39b | 346 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 RK |
347 | |
348 | begin | |
fbf5a39b | 349 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 350 | Analyze_And_Resolve (N, Typ); |
fbf5a39b | 351 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
352 | end; |
353 | end if; | |
354 | ||
355 | if Current_Scope /= Scop | |
356 | and then Scope_Is_Transient | |
357 | then | |
5cc9353d RD |
358 | -- This can only happen if a transient scope was created for an inner |
359 | -- expression, which will be removed upon completion of the analysis | |
360 | -- of an enclosing construct. The transient scope must have the | |
361 | -- suppress status of the enclosing environment, not of this Analyze | |
362 | -- call. | |
996ae0b0 RK |
363 | |
364 | Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress := | |
365 | Scope_Suppress; | |
366 | end if; | |
367 | end Analyze_And_Resolve; | |
368 | ||
369 | procedure Analyze_And_Resolve | |
370 | (N : Node_Id; | |
371 | Suppress : Check_Id) | |
372 | is | |
fbf5a39b | 373 | Scop : constant Entity_Id := Current_Scope; |
996ae0b0 RK |
374 | |
375 | begin | |
376 | if Suppress = All_Checks then | |
377 | declare | |
fbf5a39b | 378 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
379 | begin |
380 | Scope_Suppress := (others => True); | |
381 | Analyze_And_Resolve (N); | |
382 | Scope_Suppress := Svg; | |
383 | end; | |
384 | ||
385 | else | |
386 | declare | |
fbf5a39b | 387 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 RK |
388 | |
389 | begin | |
fbf5a39b | 390 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 391 | Analyze_And_Resolve (N); |
fbf5a39b | 392 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
393 | end; |
394 | end if; | |
395 | ||
396 | if Current_Scope /= Scop | |
397 | and then Scope_Is_Transient | |
398 | then | |
399 | Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress := | |
400 | Scope_Suppress; | |
401 | end if; | |
402 | end Analyze_And_Resolve; | |
403 | ||
bd29d519 AC |
404 | ---------------------------------------- |
405 | -- Bad_Unordered_Enumeration_Reference -- | |
406 | ---------------------------------------- | |
407 | ||
408 | function Bad_Unordered_Enumeration_Reference | |
409 | (N : Node_Id; | |
410 | T : Entity_Id) return Boolean | |
411 | is | |
412 | begin | |
413 | return Is_Enumeration_Type (T) | |
414 | and then Comes_From_Source (N) | |
415 | and then Warn_On_Unordered_Enumeration_Type | |
416 | and then not Has_Pragma_Ordered (T) | |
417 | and then not In_Same_Extended_Unit (N, T); | |
418 | end Bad_Unordered_Enumeration_Reference; | |
419 | ||
996ae0b0 RK |
420 | ---------------------------- |
421 | -- Check_Discriminant_Use -- | |
422 | ---------------------------- | |
423 | ||
424 | procedure Check_Discriminant_Use (N : Node_Id) is | |
425 | PN : constant Node_Id := Parent (N); | |
426 | Disc : constant Entity_Id := Entity (N); | |
427 | P : Node_Id; | |
428 | D : Node_Id; | |
429 | ||
430 | begin | |
f3d0f304 | 431 | -- Any use in a spec-expression is legal |
996ae0b0 | 432 | |
45fc7ddb | 433 | if In_Spec_Expression then |
996ae0b0 RK |
434 | null; |
435 | ||
436 | elsif Nkind (PN) = N_Range then | |
437 | ||
a77842bd | 438 | -- Discriminant cannot be used to constrain a scalar type |
996ae0b0 RK |
439 | |
440 | P := Parent (PN); | |
441 | ||
442 | if Nkind (P) = N_Range_Constraint | |
443 | and then Nkind (Parent (P)) = N_Subtype_Indication | |
a397db96 | 444 | and then Nkind (Parent (Parent (P))) = N_Component_Definition |
996ae0b0 RK |
445 | then |
446 | Error_Msg_N ("discriminant cannot constrain scalar type", N); | |
447 | ||
448 | elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then | |
449 | ||
5cc9353d RD |
450 | -- The following check catches the unusual case where a |
451 | -- discriminant appears within an index constraint that is part of | |
452 | -- a larger expression within a constraint on a component, e.g. "C | |
453 | -- : Int range 1 .. F (new A(1 .. D))". For now we only check case | |
454 | -- of record components, and note that a similar check should also | |
455 | -- apply in the case of discriminant constraints below. ??? | |
996ae0b0 RK |
456 | |
457 | -- Note that the check for N_Subtype_Declaration below is to | |
458 | -- detect the valid use of discriminants in the constraints of a | |
459 | -- subtype declaration when this subtype declaration appears | |
460 | -- inside the scope of a record type (which is syntactically | |
461 | -- illegal, but which may be created as part of derived type | |
462 | -- processing for records). See Sem_Ch3.Build_Derived_Record_Type | |
463 | -- for more info. | |
464 | ||
465 | if Ekind (Current_Scope) = E_Record_Type | |
466 | and then Scope (Disc) = Current_Scope | |
467 | and then not | |
468 | (Nkind (Parent (P)) = N_Subtype_Indication | |
45fc7ddb HK |
469 | and then |
470 | Nkind_In (Parent (Parent (P)), N_Component_Definition, | |
471 | N_Subtype_Declaration) | |
996ae0b0 RK |
472 | and then Paren_Count (N) = 0) |
473 | then | |
474 | Error_Msg_N | |
475 | ("discriminant must appear alone in component constraint", N); | |
476 | return; | |
477 | end if; | |
478 | ||
a0ac3932 | 479 | -- Detect a common error: |
9bc43c53 | 480 | |
996ae0b0 | 481 | -- type R (D : Positive := 100) is record |
9bc43c53 | 482 | -- Name : String (1 .. D); |
996ae0b0 RK |
483 | -- end record; |
484 | ||
a0ac3932 RD |
485 | -- The default value causes an object of type R to be allocated |
486 | -- with room for Positive'Last characters. The RM does not mandate | |
487 | -- the allocation of the maximum size, but that is what GNAT does | |
488 | -- so we should warn the programmer that there is a problem. | |
996ae0b0 | 489 | |
a0ac3932 | 490 | Check_Large : declare |
996ae0b0 RK |
491 | SI : Node_Id; |
492 | T : Entity_Id; | |
493 | TB : Node_Id; | |
494 | CB : Entity_Id; | |
495 | ||
496 | function Large_Storage_Type (T : Entity_Id) return Boolean; | |
5cc9353d RD |
497 | -- Return True if type T has a large enough range that any |
498 | -- array whose index type covered the whole range of the type | |
499 | -- would likely raise Storage_Error. | |
996ae0b0 | 500 | |
fbf5a39b AC |
501 | ------------------------ |
502 | -- Large_Storage_Type -- | |
503 | ------------------------ | |
504 | ||
996ae0b0 RK |
505 | function Large_Storage_Type (T : Entity_Id) return Boolean is |
506 | begin | |
4b92fd3c ST |
507 | -- The type is considered large if its bounds are known at |
508 | -- compile time and if it requires at least as many bits as | |
509 | -- a Positive to store the possible values. | |
510 | ||
511 | return Compile_Time_Known_Value (Type_Low_Bound (T)) | |
512 | and then Compile_Time_Known_Value (Type_High_Bound (T)) | |
513 | and then | |
514 | Minimum_Size (T, Biased => True) >= | |
a0ac3932 | 515 | RM_Size (Standard_Positive); |
996ae0b0 RK |
516 | end Large_Storage_Type; |
517 | ||
a0ac3932 RD |
518 | -- Start of processing for Check_Large |
519 | ||
996ae0b0 RK |
520 | begin |
521 | -- Check that the Disc has a large range | |
522 | ||
523 | if not Large_Storage_Type (Etype (Disc)) then | |
524 | goto No_Danger; | |
525 | end if; | |
526 | ||
527 | -- If the enclosing type is limited, we allocate only the | |
528 | -- default value, not the maximum, and there is no need for | |
529 | -- a warning. | |
530 | ||
531 | if Is_Limited_Type (Scope (Disc)) then | |
532 | goto No_Danger; | |
533 | end if; | |
534 | ||
535 | -- Check that it is the high bound | |
536 | ||
537 | if N /= High_Bound (PN) | |
c8ef728f | 538 | or else No (Discriminant_Default_Value (Disc)) |
996ae0b0 RK |
539 | then |
540 | goto No_Danger; | |
541 | end if; | |
542 | ||
5cc9353d RD |
543 | -- Check the array allows a large range at this bound. First |
544 | -- find the array | |
996ae0b0 RK |
545 | |
546 | SI := Parent (P); | |
547 | ||
548 | if Nkind (SI) /= N_Subtype_Indication then | |
549 | goto No_Danger; | |
550 | end if; | |
551 | ||
552 | T := Entity (Subtype_Mark (SI)); | |
553 | ||
554 | if not Is_Array_Type (T) then | |
555 | goto No_Danger; | |
556 | end if; | |
557 | ||
558 | -- Next, find the dimension | |
559 | ||
560 | TB := First_Index (T); | |
561 | CB := First (Constraints (P)); | |
562 | while True | |
563 | and then Present (TB) | |
564 | and then Present (CB) | |
565 | and then CB /= PN | |
566 | loop | |
567 | Next_Index (TB); | |
568 | Next (CB); | |
569 | end loop; | |
570 | ||
571 | if CB /= PN then | |
572 | goto No_Danger; | |
573 | end if; | |
574 | ||
575 | -- Now, check the dimension has a large range | |
576 | ||
577 | if not Large_Storage_Type (Etype (TB)) then | |
578 | goto No_Danger; | |
579 | end if; | |
580 | ||
581 | -- Warn about the danger | |
582 | ||
583 | Error_Msg_N | |
aa5147f0 | 584 | ("?creation of & object may raise Storage_Error!", |
fbf5a39b | 585 | Scope (Disc)); |
996ae0b0 RK |
586 | |
587 | <<No_Danger>> | |
588 | null; | |
589 | ||
a0ac3932 | 590 | end Check_Large; |
996ae0b0 RK |
591 | end if; |
592 | ||
593 | -- Legal case is in index or discriminant constraint | |
594 | ||
45fc7ddb HK |
595 | elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint, |
596 | N_Discriminant_Association) | |
996ae0b0 RK |
597 | then |
598 | if Paren_Count (N) > 0 then | |
599 | Error_Msg_N | |
600 | ("discriminant in constraint must appear alone", N); | |
758c442c GD |
601 | |
602 | elsif Nkind (N) = N_Expanded_Name | |
603 | and then Comes_From_Source (N) | |
604 | then | |
605 | Error_Msg_N | |
606 | ("discriminant must appear alone as a direct name", N); | |
996ae0b0 RK |
607 | end if; |
608 | ||
609 | return; | |
610 | ||
5cc9353d RD |
611 | -- Otherwise, context is an expression. It should not be within (i.e. a |
612 | -- subexpression of) a constraint for a component. | |
996ae0b0 RK |
613 | |
614 | else | |
615 | D := PN; | |
616 | P := Parent (PN); | |
45fc7ddb HK |
617 | while not Nkind_In (P, N_Component_Declaration, |
618 | N_Subtype_Indication, | |
619 | N_Entry_Declaration) | |
996ae0b0 RK |
620 | loop |
621 | D := P; | |
622 | P := Parent (P); | |
623 | exit when No (P); | |
624 | end loop; | |
625 | ||
5cc9353d RD |
626 | -- If the discriminant is used in an expression that is a bound of a |
627 | -- scalar type, an Itype is created and the bounds are attached to | |
628 | -- its range, not to the original subtype indication. Such use is of | |
629 | -- course a double fault. | |
996ae0b0 RK |
630 | |
631 | if (Nkind (P) = N_Subtype_Indication | |
45fc7ddb HK |
632 | and then Nkind_In (Parent (P), N_Component_Definition, |
633 | N_Derived_Type_Definition) | |
996ae0b0 RK |
634 | and then D = Constraint (P)) |
635 | ||
19fb051c AC |
636 | -- The constraint itself may be given by a subtype indication, |
637 | -- rather than by a more common discrete range. | |
996ae0b0 RK |
638 | |
639 | or else (Nkind (P) = N_Subtype_Indication | |
fbf5a39b AC |
640 | and then |
641 | Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint) | |
996ae0b0 RK |
642 | or else Nkind (P) = N_Entry_Declaration |
643 | or else Nkind (D) = N_Defining_Identifier | |
644 | then | |
645 | Error_Msg_N | |
646 | ("discriminant in constraint must appear alone", N); | |
647 | end if; | |
648 | end if; | |
649 | end Check_Discriminant_Use; | |
650 | ||
651 | -------------------------------- | |
652 | -- Check_For_Visible_Operator -- | |
653 | -------------------------------- | |
654 | ||
655 | procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is | |
996ae0b0 | 656 | begin |
fbf5a39b | 657 | if Is_Invisible_Operator (N, T) then |
305caf42 | 658 | Error_Msg_NE -- CODEFIX |
996ae0b0 | 659 | ("operator for} is not directly visible!", N, First_Subtype (T)); |
305caf42 AC |
660 | Error_Msg_N -- CODEFIX |
661 | ("use clause would make operation legal!", N); | |
996ae0b0 RK |
662 | end if; |
663 | end Check_For_Visible_Operator; | |
664 | ||
c8ef728f ES |
665 | ---------------------------------- |
666 | -- Check_Fully_Declared_Prefix -- | |
667 | ---------------------------------- | |
668 | ||
669 | procedure Check_Fully_Declared_Prefix | |
670 | (Typ : Entity_Id; | |
671 | Pref : Node_Id) | |
672 | is | |
673 | begin | |
674 | -- Check that the designated type of the prefix of a dereference is | |
675 | -- not an incomplete type. This cannot be done unconditionally, because | |
676 | -- dereferences of private types are legal in default expressions. This | |
677 | -- case is taken care of in Check_Fully_Declared, called below. There | |
678 | -- are also 2005 cases where it is legal for the prefix to be unfrozen. | |
679 | ||
680 | -- This consideration also applies to similar checks for allocators, | |
681 | -- qualified expressions, and type conversions. | |
682 | ||
683 | -- An additional exception concerns other per-object expressions that | |
684 | -- are not directly related to component declarations, in particular | |
685 | -- representation pragmas for tasks. These will be per-object | |
686 | -- expressions if they depend on discriminants or some global entity. | |
687 | -- If the task has access discriminants, the designated type may be | |
688 | -- incomplete at the point the expression is resolved. This resolution | |
689 | -- takes place within the body of the initialization procedure, where | |
690 | -- the discriminant is replaced by its discriminal. | |
691 | ||
692 | if Is_Entity_Name (Pref) | |
693 | and then Ekind (Entity (Pref)) = E_In_Parameter | |
694 | then | |
695 | null; | |
696 | ||
697 | -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages | |
698 | -- are handled by Analyze_Access_Attribute, Analyze_Assignment, | |
699 | -- Analyze_Object_Renaming, and Freeze_Entity. | |
700 | ||
0791fbe9 | 701 | elsif Ada_Version >= Ada_2005 |
c8ef728f | 702 | and then Is_Entity_Name (Pref) |
811c6a85 | 703 | and then Is_Access_Type (Etype (Pref)) |
c8ef728f ES |
704 | and then Ekind (Directly_Designated_Type (Etype (Pref))) = |
705 | E_Incomplete_Type | |
706 | and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref))) | |
707 | then | |
708 | null; | |
709 | else | |
710 | Check_Fully_Declared (Typ, Parent (Pref)); | |
711 | end if; | |
712 | end Check_Fully_Declared_Prefix; | |
713 | ||
996ae0b0 RK |
714 | ------------------------------ |
715 | -- Check_Infinite_Recursion -- | |
716 | ------------------------------ | |
717 | ||
718 | function Check_Infinite_Recursion (N : Node_Id) return Boolean is | |
719 | P : Node_Id; | |
720 | C : Node_Id; | |
721 | ||
07fc65c4 | 722 | function Same_Argument_List return Boolean; |
5cc9353d RD |
723 | -- Check whether list of actuals is identical to list of formals of |
724 | -- called function (which is also the enclosing scope). | |
07fc65c4 GB |
725 | |
726 | ------------------------ | |
727 | -- Same_Argument_List -- | |
728 | ------------------------ | |
729 | ||
730 | function Same_Argument_List return Boolean is | |
731 | A : Node_Id; | |
732 | F : Entity_Id; | |
733 | Subp : Entity_Id; | |
734 | ||
735 | begin | |
736 | if not Is_Entity_Name (Name (N)) then | |
737 | return False; | |
738 | else | |
739 | Subp := Entity (Name (N)); | |
740 | end if; | |
741 | ||
742 | F := First_Formal (Subp); | |
743 | A := First_Actual (N); | |
07fc65c4 GB |
744 | while Present (F) and then Present (A) loop |
745 | if not Is_Entity_Name (A) | |
746 | or else Entity (A) /= F | |
747 | then | |
748 | return False; | |
749 | end if; | |
750 | ||
751 | Next_Actual (A); | |
752 | Next_Formal (F); | |
753 | end loop; | |
754 | ||
755 | return True; | |
756 | end Same_Argument_List; | |
757 | ||
758 | -- Start of processing for Check_Infinite_Recursion | |
759 | ||
996ae0b0 | 760 | begin |
26570b21 RD |
761 | -- Special case, if this is a procedure call and is a call to the |
762 | -- current procedure with the same argument list, then this is for | |
763 | -- sure an infinite recursion and we insert a call to raise SE. | |
764 | ||
765 | if Is_List_Member (N) | |
766 | and then List_Length (List_Containing (N)) = 1 | |
767 | and then Same_Argument_List | |
768 | then | |
769 | declare | |
770 | P : constant Node_Id := Parent (N); | |
771 | begin | |
772 | if Nkind (P) = N_Handled_Sequence_Of_Statements | |
773 | and then Nkind (Parent (P)) = N_Subprogram_Body | |
774 | and then Is_Empty_List (Declarations (Parent (P))) | |
775 | then | |
776 | Error_Msg_N ("!?infinite recursion", N); | |
777 | Error_Msg_N ("\!?Storage_Error will be raised at run time", N); | |
778 | Insert_Action (N, | |
779 | Make_Raise_Storage_Error (Sloc (N), | |
780 | Reason => SE_Infinite_Recursion)); | |
781 | return True; | |
782 | end if; | |
783 | end; | |
784 | end if; | |
785 | ||
786 | -- If not that special case, search up tree, quitting if we reach a | |
787 | -- construct (e.g. a conditional) that tells us that this is not a | |
788 | -- case for an infinite recursion warning. | |
996ae0b0 RK |
789 | |
790 | C := N; | |
791 | loop | |
792 | P := Parent (C); | |
9a7da240 RD |
793 | |
794 | -- If no parent, then we were not inside a subprogram, this can for | |
795 | -- example happen when processing certain pragmas in a spec. Just | |
796 | -- return False in this case. | |
797 | ||
798 | if No (P) then | |
799 | return False; | |
800 | end if; | |
801 | ||
802 | -- Done if we get to subprogram body, this is definitely an infinite | |
803 | -- recursion case if we did not find anything to stop us. | |
804 | ||
996ae0b0 | 805 | exit when Nkind (P) = N_Subprogram_Body; |
9a7da240 RD |
806 | |
807 | -- If appearing in conditional, result is false | |
808 | ||
45fc7ddb HK |
809 | if Nkind_In (P, N_Or_Else, |
810 | N_And_Then, | |
d347f572 AC |
811 | N_Case_Expression, |
812 | N_Case_Statement, | |
813 | N_Conditional_Expression, | |
814 | N_If_Statement) | |
996ae0b0 RK |
815 | then |
816 | return False; | |
817 | ||
818 | elsif Nkind (P) = N_Handled_Sequence_Of_Statements | |
819 | and then C /= First (Statements (P)) | |
820 | then | |
26570b21 RD |
821 | -- If the call is the expression of a return statement and the |
822 | -- actuals are identical to the formals, it's worth a warning. | |
823 | -- However, we skip this if there is an immediately preceding | |
824 | -- raise statement, since the call is never executed. | |
07fc65c4 GB |
825 | |
826 | -- Furthermore, this corresponds to a common idiom: | |
827 | ||
828 | -- function F (L : Thing) return Boolean is | |
829 | -- begin | |
830 | -- raise Program_Error; | |
831 | -- return F (L); | |
832 | -- end F; | |
833 | ||
834 | -- for generating a stub function | |
835 | ||
aa5147f0 | 836 | if Nkind (Parent (N)) = N_Simple_Return_Statement |
07fc65c4 GB |
837 | and then Same_Argument_List |
838 | then | |
9ebe3743 HK |
839 | exit when not Is_List_Member (Parent (N)); |
840 | ||
841 | -- OK, return statement is in a statement list, look for raise | |
842 | ||
843 | declare | |
844 | Nod : Node_Id; | |
845 | ||
846 | begin | |
847 | -- Skip past N_Freeze_Entity nodes generated by expansion | |
848 | ||
849 | Nod := Prev (Parent (N)); | |
850 | while Present (Nod) | |
851 | and then Nkind (Nod) = N_Freeze_Entity | |
852 | loop | |
853 | Prev (Nod); | |
854 | end loop; | |
855 | ||
3235dc87 AC |
856 | -- If no raise statement, give warning. We look at the |
857 | -- original node, because in the case of "raise ... with | |
858 | -- ...", the node has been transformed into a call. | |
9ebe3743 | 859 | |
3235dc87 | 860 | exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement |
9ebe3743 HK |
861 | and then |
862 | (Nkind (Nod) not in N_Raise_xxx_Error | |
19fb051c | 863 | or else Present (Condition (Nod))); |
9ebe3743 | 864 | end; |
07fc65c4 GB |
865 | end if; |
866 | ||
996ae0b0 RK |
867 | return False; |
868 | ||
869 | else | |
870 | C := P; | |
871 | end if; | |
872 | end loop; | |
873 | ||
aa5147f0 ES |
874 | Error_Msg_N ("!?possible infinite recursion", N); |
875 | Error_Msg_N ("\!?Storage_Error may be raised at run time", N); | |
996ae0b0 RK |
876 | |
877 | return True; | |
878 | end Check_Infinite_Recursion; | |
879 | ||
880 | ------------------------------- | |
881 | -- Check_Initialization_Call -- | |
882 | ------------------------------- | |
883 | ||
884 | procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is | |
fbf5a39b | 885 | Typ : constant Entity_Id := Etype (First_Formal (Nam)); |
996ae0b0 RK |
886 | |
887 | function Uses_SS (T : Entity_Id) return Boolean; | |
07fc65c4 GB |
888 | -- Check whether the creation of an object of the type will involve |
889 | -- use of the secondary stack. If T is a record type, this is true | |
f3d57416 | 890 | -- if the expression for some component uses the secondary stack, e.g. |
07fc65c4 GB |
891 | -- through a call to a function that returns an unconstrained value. |
892 | -- False if T is controlled, because cleanups occur elsewhere. | |
893 | ||
894 | ------------- | |
895 | -- Uses_SS -- | |
896 | ------------- | |
996ae0b0 RK |
897 | |
898 | function Uses_SS (T : Entity_Id) return Boolean is | |
aa5147f0 ES |
899 | Comp : Entity_Id; |
900 | Expr : Node_Id; | |
901 | Full_Type : Entity_Id := Underlying_Type (T); | |
996ae0b0 RK |
902 | |
903 | begin | |
aa5147f0 ES |
904 | -- Normally we want to use the underlying type, but if it's not set |
905 | -- then continue with T. | |
906 | ||
907 | if not Present (Full_Type) then | |
908 | Full_Type := T; | |
909 | end if; | |
910 | ||
911 | if Is_Controlled (Full_Type) then | |
996ae0b0 RK |
912 | return False; |
913 | ||
aa5147f0 ES |
914 | elsif Is_Array_Type (Full_Type) then |
915 | return Uses_SS (Component_Type (Full_Type)); | |
996ae0b0 | 916 | |
aa5147f0 ES |
917 | elsif Is_Record_Type (Full_Type) then |
918 | Comp := First_Component (Full_Type); | |
996ae0b0 | 919 | while Present (Comp) loop |
996ae0b0 RK |
920 | if Ekind (Comp) = E_Component |
921 | and then Nkind (Parent (Comp)) = N_Component_Declaration | |
922 | then | |
aa5147f0 ES |
923 | -- The expression for a dynamic component may be rewritten |
924 | -- as a dereference, so retrieve original node. | |
925 | ||
926 | Expr := Original_Node (Expression (Parent (Comp))); | |
996ae0b0 | 927 | |
aa5147f0 | 928 | -- Return True if the expression is a call to a function |
1d57c04f AC |
929 | -- (including an attribute function such as Image, or a |
930 | -- user-defined operator) with a result that requires a | |
931 | -- transient scope. | |
fbf5a39b | 932 | |
aa5147f0 | 933 | if (Nkind (Expr) = N_Function_Call |
1d57c04f | 934 | or else Nkind (Expr) in N_Op |
aa5147f0 ES |
935 | or else (Nkind (Expr) = N_Attribute_Reference |
936 | and then Present (Expressions (Expr)))) | |
996ae0b0 RK |
937 | and then Requires_Transient_Scope (Etype (Expr)) |
938 | then | |
939 | return True; | |
940 | ||
941 | elsif Uses_SS (Etype (Comp)) then | |
942 | return True; | |
943 | end if; | |
944 | end if; | |
945 | ||
946 | Next_Component (Comp); | |
947 | end loop; | |
948 | ||
949 | return False; | |
950 | ||
951 | else | |
952 | return False; | |
953 | end if; | |
954 | end Uses_SS; | |
955 | ||
07fc65c4 GB |
956 | -- Start of processing for Check_Initialization_Call |
957 | ||
996ae0b0 | 958 | begin |
0669bebe | 959 | -- Establish a transient scope if the type needs it |
07fc65c4 | 960 | |
0669bebe | 961 | if Uses_SS (Typ) then |
996ae0b0 RK |
962 | Establish_Transient_Scope (First_Actual (N), Sec_Stack => True); |
963 | end if; | |
964 | end Check_Initialization_Call; | |
965 | ||
f61580d4 AC |
966 | --------------------------------------- |
967 | -- Check_No_Direct_Boolean_Operators -- | |
968 | --------------------------------------- | |
969 | ||
970 | procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is | |
971 | begin | |
972 | if Scope (Entity (N)) = Standard_Standard | |
973 | and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean | |
974 | then | |
6fb4cdde | 975 | -- Restriction only applies to original source code |
f61580d4 | 976 | |
6fb4cdde | 977 | if Comes_From_Source (N) then |
f61580d4 AC |
978 | Check_Restriction (No_Direct_Boolean_Operators, N); |
979 | end if; | |
980 | end if; | |
a36c1c3e RD |
981 | |
982 | if Style_Check then | |
983 | Check_Boolean_Operator (N); | |
984 | end if; | |
f61580d4 AC |
985 | end Check_No_Direct_Boolean_Operators; |
986 | ||
996ae0b0 RK |
987 | ------------------------------ |
988 | -- Check_Parameterless_Call -- | |
989 | ------------------------------ | |
990 | ||
991 | procedure Check_Parameterless_Call (N : Node_Id) is | |
992 | Nam : Node_Id; | |
993 | ||
bc5f3720 RD |
994 | function Prefix_Is_Access_Subp return Boolean; |
995 | -- If the prefix is of an access_to_subprogram type, the node must be | |
996 | -- rewritten as a call. Ditto if the prefix is overloaded and all its | |
997 | -- interpretations are access to subprograms. | |
998 | ||
999 | --------------------------- | |
1000 | -- Prefix_Is_Access_Subp -- | |
1001 | --------------------------- | |
1002 | ||
1003 | function Prefix_Is_Access_Subp return Boolean is | |
1004 | I : Interp_Index; | |
1005 | It : Interp; | |
1006 | ||
1007 | begin | |
22b77f68 | 1008 | -- If the context is an attribute reference that can apply to |
b4a4936b | 1009 | -- functions, this is never a parameterless call (RM 4.1.4(6)). |
96d2756f AC |
1010 | |
1011 | if Nkind (Parent (N)) = N_Attribute_Reference | |
19fb051c AC |
1012 | and then (Attribute_Name (Parent (N)) = Name_Address or else |
1013 | Attribute_Name (Parent (N)) = Name_Code_Address or else | |
1014 | Attribute_Name (Parent (N)) = Name_Access) | |
96d2756f AC |
1015 | then |
1016 | return False; | |
1017 | end if; | |
1018 | ||
bc5f3720 RD |
1019 | if not Is_Overloaded (N) then |
1020 | return | |
1021 | Ekind (Etype (N)) = E_Subprogram_Type | |
1022 | and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type; | |
1023 | else | |
1024 | Get_First_Interp (N, I, It); | |
1025 | while Present (It.Typ) loop | |
1026 | if Ekind (It.Typ) /= E_Subprogram_Type | |
1027 | or else Base_Type (Etype (It.Typ)) = Standard_Void_Type | |
1028 | then | |
1029 | return False; | |
1030 | end if; | |
1031 | ||
1032 | Get_Next_Interp (I, It); | |
1033 | end loop; | |
1034 | ||
1035 | return True; | |
1036 | end if; | |
1037 | end Prefix_Is_Access_Subp; | |
1038 | ||
1039 | -- Start of processing for Check_Parameterless_Call | |
1040 | ||
996ae0b0 | 1041 | begin |
07fc65c4 GB |
1042 | -- Defend against junk stuff if errors already detected |
1043 | ||
1044 | if Total_Errors_Detected /= 0 then | |
1045 | if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then | |
1046 | return; | |
1047 | elsif Nkind (N) in N_Has_Chars | |
1048 | and then Chars (N) in Error_Name_Or_No_Name | |
1049 | then | |
1050 | return; | |
1051 | end if; | |
fbf5a39b AC |
1052 | |
1053 | Require_Entity (N); | |
996ae0b0 RK |
1054 | end if; |
1055 | ||
45fc7ddb HK |
1056 | -- If the context expects a value, and the name is a procedure, this is |
1057 | -- most likely a missing 'Access. Don't try to resolve the parameterless | |
1058 | -- call, error will be caught when the outer call is analyzed. | |
18c0ecbe AC |
1059 | |
1060 | if Is_Entity_Name (N) | |
1061 | and then Ekind (Entity (N)) = E_Procedure | |
1062 | and then not Is_Overloaded (N) | |
1063 | and then | |
45fc7ddb HK |
1064 | Nkind_In (Parent (N), N_Parameter_Association, |
1065 | N_Function_Call, | |
1066 | N_Procedure_Call_Statement) | |
18c0ecbe AC |
1067 | then |
1068 | return; | |
1069 | end if; | |
1070 | ||
45fc7ddb HK |
1071 | -- Rewrite as call if overloadable entity that is (or could be, in the |
1072 | -- overloaded case) a function call. If we know for sure that the entity | |
1073 | -- is an enumeration literal, we do not rewrite it. | |
f4b049db | 1074 | |
e1d9659d AC |
1075 | -- If the entity is the name of an operator, it cannot be a call because |
1076 | -- operators cannot have default parameters. In this case, this must be | |
1077 | -- a string whose contents coincide with an operator name. Set the kind | |
96d2756f | 1078 | -- of the node appropriately. |
996ae0b0 RK |
1079 | |
1080 | if (Is_Entity_Name (N) | |
e1d9659d | 1081 | and then Nkind (N) /= N_Operator_Symbol |
996ae0b0 RK |
1082 | and then Is_Overloadable (Entity (N)) |
1083 | and then (Ekind (Entity (N)) /= E_Enumeration_Literal | |
964f13da | 1084 | or else Is_Overloaded (N))) |
996ae0b0 | 1085 | |
09494c32 | 1086 | -- Rewrite as call if it is an explicit dereference of an expression of |
f3d57416 | 1087 | -- a subprogram access type, and the subprogram type is not that of a |
996ae0b0 RK |
1088 | -- procedure or entry. |
1089 | ||
1090 | or else | |
bc5f3720 | 1091 | (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp) |
996ae0b0 RK |
1092 | |
1093 | -- Rewrite as call if it is a selected component which is a function, | |
1094 | -- this is the case of a call to a protected function (which may be | |
1095 | -- overloaded with other protected operations). | |
1096 | ||
1097 | or else | |
1098 | (Nkind (N) = N_Selected_Component | |
1099 | and then (Ekind (Entity (Selector_Name (N))) = E_Function | |
964f13da RD |
1100 | or else |
1101 | (Ekind_In (Entity (Selector_Name (N)), E_Entry, | |
1102 | E_Procedure) | |
1103 | and then Is_Overloaded (Selector_Name (N))))) | |
996ae0b0 | 1104 | |
5cc9353d RD |
1105 | -- If one of the above three conditions is met, rewrite as call. Apply |
1106 | -- the rewriting only once. | |
996ae0b0 RK |
1107 | |
1108 | then | |
1109 | if Nkind (Parent (N)) /= N_Function_Call | |
1110 | or else N /= Name (Parent (N)) | |
1111 | then | |
747de90b AC |
1112 | |
1113 | -- This may be a prefixed call that was not fully analyzed, e.g. | |
1114 | -- an actual in an instance. | |
1115 | ||
1116 | if Ada_Version >= Ada_2005 | |
1117 | and then Nkind (N) = N_Selected_Component | |
1118 | and then Is_Dispatching_Operation (Entity (Selector_Name (N))) | |
1119 | then | |
1120 | Analyze_Selected_Component (N); | |
996c8821 | 1121 | |
747de90b AC |
1122 | if Nkind (N) /= N_Selected_Component then |
1123 | return; | |
1124 | end if; | |
1125 | end if; | |
1126 | ||
996ae0b0 RK |
1127 | Nam := New_Copy (N); |
1128 | ||
bc5f3720 | 1129 | -- If overloaded, overload set belongs to new copy |
996ae0b0 RK |
1130 | |
1131 | Save_Interps (N, Nam); | |
1132 | ||
1133 | -- Change node to parameterless function call (note that the | |
1134 | -- Parameter_Associations associations field is left set to Empty, | |
1135 | -- its normal default value since there are no parameters) | |
1136 | ||
1137 | Change_Node (N, N_Function_Call); | |
1138 | Set_Name (N, Nam); | |
1139 | Set_Sloc (N, Sloc (Nam)); | |
1140 | Analyze_Call (N); | |
1141 | end if; | |
1142 | ||
1143 | elsif Nkind (N) = N_Parameter_Association then | |
1144 | Check_Parameterless_Call (Explicit_Actual_Parameter (N)); | |
e1d9659d AC |
1145 | |
1146 | elsif Nkind (N) = N_Operator_Symbol then | |
1147 | Change_Operator_Symbol_To_String_Literal (N); | |
1148 | Set_Is_Overloaded (N, False); | |
1149 | Set_Etype (N, Any_String); | |
996ae0b0 RK |
1150 | end if; |
1151 | end Check_Parameterless_Call; | |
1152 | ||
67ce0d7e RD |
1153 | ----------------------------- |
1154 | -- Is_Definite_Access_Type -- | |
1155 | ----------------------------- | |
1156 | ||
1157 | function Is_Definite_Access_Type (E : Entity_Id) return Boolean is | |
1158 | Btyp : constant Entity_Id := Base_Type (E); | |
1159 | begin | |
1160 | return Ekind (Btyp) = E_Access_Type | |
1161 | or else (Ekind (Btyp) = E_Access_Subprogram_Type | |
72e9f2b9 | 1162 | and then Comes_From_Source (Btyp)); |
67ce0d7e RD |
1163 | end Is_Definite_Access_Type; |
1164 | ||
996ae0b0 RK |
1165 | ---------------------- |
1166 | -- Is_Predefined_Op -- | |
1167 | ---------------------- | |
1168 | ||
1169 | function Is_Predefined_Op (Nam : Entity_Id) return Boolean is | |
1170 | begin | |
6a497607 AC |
1171 | -- Predefined operators are intrinsic subprograms |
1172 | ||
1173 | if not Is_Intrinsic_Subprogram (Nam) then | |
1174 | return False; | |
1175 | end if; | |
1176 | ||
1177 | -- A call to a back-end builtin is never a predefined operator | |
1178 | ||
1179 | if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then | |
1180 | return False; | |
1181 | end if; | |
1182 | ||
1183 | return not Is_Generic_Instance (Nam) | |
996ae0b0 | 1184 | and then Chars (Nam) in Any_Operator_Name |
6a497607 | 1185 | and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam))); |
996ae0b0 RK |
1186 | end Is_Predefined_Op; |
1187 | ||
1188 | ----------------------------- | |
1189 | -- Make_Call_Into_Operator -- | |
1190 | ----------------------------- | |
1191 | ||
1192 | procedure Make_Call_Into_Operator | |
1193 | (N : Node_Id; | |
1194 | Typ : Entity_Id; | |
1195 | Op_Id : Entity_Id) | |
1196 | is | |
1197 | Op_Name : constant Name_Id := Chars (Op_Id); | |
1198 | Act1 : Node_Id := First_Actual (N); | |
1199 | Act2 : Node_Id := Next_Actual (Act1); | |
1200 | Error : Boolean := False; | |
2820d220 AC |
1201 | Func : constant Entity_Id := Entity (Name (N)); |
1202 | Is_Binary : constant Boolean := Present (Act2); | |
996ae0b0 RK |
1203 | Op_Node : Node_Id; |
1204 | Opnd_Type : Entity_Id; | |
1205 | Orig_Type : Entity_Id := Empty; | |
1206 | Pack : Entity_Id; | |
1207 | ||
1208 | type Kind_Test is access function (E : Entity_Id) return Boolean; | |
1209 | ||
996ae0b0 | 1210 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean; |
b4a4936b AC |
1211 | -- If the operand is not universal, and the operator is given by an |
1212 | -- expanded name, verify that the operand has an interpretation with a | |
1213 | -- type defined in the given scope of the operator. | |
996ae0b0 RK |
1214 | |
1215 | function Type_In_P (Test : Kind_Test) return Entity_Id; | |
b4a4936b AC |
1216 | -- Find a type of the given class in package Pack that contains the |
1217 | -- operator. | |
996ae0b0 | 1218 | |
996ae0b0 RK |
1219 | --------------------------- |
1220 | -- Operand_Type_In_Scope -- | |
1221 | --------------------------- | |
1222 | ||
1223 | function Operand_Type_In_Scope (S : Entity_Id) return Boolean is | |
1224 | Nod : constant Node_Id := Right_Opnd (Op_Node); | |
1225 | I : Interp_Index; | |
1226 | It : Interp; | |
1227 | ||
1228 | begin | |
1229 | if not Is_Overloaded (Nod) then | |
1230 | return Scope (Base_Type (Etype (Nod))) = S; | |
1231 | ||
1232 | else | |
1233 | Get_First_Interp (Nod, I, It); | |
996ae0b0 | 1234 | while Present (It.Typ) loop |
996ae0b0 RK |
1235 | if Scope (Base_Type (It.Typ)) = S then |
1236 | return True; | |
1237 | end if; | |
1238 | ||
1239 | Get_Next_Interp (I, It); | |
1240 | end loop; | |
1241 | ||
1242 | return False; | |
1243 | end if; | |
1244 | end Operand_Type_In_Scope; | |
1245 | ||
1246 | --------------- | |
1247 | -- Type_In_P -- | |
1248 | --------------- | |
1249 | ||
1250 | function Type_In_P (Test : Kind_Test) return Entity_Id is | |
1251 | E : Entity_Id; | |
1252 | ||
1253 | function In_Decl return Boolean; | |
1254 | -- Verify that node is not part of the type declaration for the | |
1255 | -- candidate type, which would otherwise be invisible. | |
1256 | ||
1257 | ------------- | |
1258 | -- In_Decl -- | |
1259 | ------------- | |
1260 | ||
1261 | function In_Decl return Boolean is | |
1262 | Decl_Node : constant Node_Id := Parent (E); | |
1263 | N2 : Node_Id; | |
1264 | ||
1265 | begin | |
1266 | N2 := N; | |
1267 | ||
1268 | if Etype (E) = Any_Type then | |
1269 | return True; | |
1270 | ||
1271 | elsif No (Decl_Node) then | |
1272 | return False; | |
1273 | ||
1274 | else | |
1275 | while Present (N2) | |
1276 | and then Nkind (N2) /= N_Compilation_Unit | |
1277 | loop | |
1278 | if N2 = Decl_Node then | |
1279 | return True; | |
1280 | else | |
1281 | N2 := Parent (N2); | |
1282 | end if; | |
1283 | end loop; | |
1284 | ||
1285 | return False; | |
1286 | end if; | |
1287 | end In_Decl; | |
1288 | ||
1289 | -- Start of processing for Type_In_P | |
1290 | ||
1291 | begin | |
b4a4936b AC |
1292 | -- If the context type is declared in the prefix package, this is the |
1293 | -- desired base type. | |
996ae0b0 | 1294 | |
b4a4936b | 1295 | if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then |
996ae0b0 RK |
1296 | return Base_Type (Typ); |
1297 | ||
1298 | else | |
1299 | E := First_Entity (Pack); | |
996ae0b0 | 1300 | while Present (E) loop |
996ae0b0 RK |
1301 | if Test (E) |
1302 | and then not In_Decl | |
1303 | then | |
1304 | return E; | |
1305 | end if; | |
1306 | ||
1307 | Next_Entity (E); | |
1308 | end loop; | |
1309 | ||
1310 | return Empty; | |
1311 | end if; | |
1312 | end Type_In_P; | |
1313 | ||
996ae0b0 RK |
1314 | -- Start of processing for Make_Call_Into_Operator |
1315 | ||
1316 | begin | |
1317 | Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N)); | |
1318 | ||
1319 | -- Binary operator | |
1320 | ||
1321 | if Is_Binary then | |
1322 | Set_Left_Opnd (Op_Node, Relocate_Node (Act1)); | |
1323 | Set_Right_Opnd (Op_Node, Relocate_Node (Act2)); | |
1324 | Save_Interps (Act1, Left_Opnd (Op_Node)); | |
1325 | Save_Interps (Act2, Right_Opnd (Op_Node)); | |
1326 | Act1 := Left_Opnd (Op_Node); | |
1327 | Act2 := Right_Opnd (Op_Node); | |
1328 | ||
1329 | -- Unary operator | |
1330 | ||
1331 | else | |
1332 | Set_Right_Opnd (Op_Node, Relocate_Node (Act1)); | |
1333 | Save_Interps (Act1, Right_Opnd (Op_Node)); | |
1334 | Act1 := Right_Opnd (Op_Node); | |
1335 | end if; | |
1336 | ||
1337 | -- If the operator is denoted by an expanded name, and the prefix is | |
1338 | -- not Standard, but the operator is a predefined one whose scope is | |
1339 | -- Standard, then this is an implicit_operator, inserted as an | |
1340 | -- interpretation by the procedure of the same name. This procedure | |
1341 | -- overestimates the presence of implicit operators, because it does | |
1342 | -- not examine the type of the operands. Verify now that the operand | |
1343 | -- type appears in the given scope. If right operand is universal, | |
1344 | -- check the other operand. In the case of concatenation, either | |
1345 | -- argument can be the component type, so check the type of the result. | |
1346 | -- If both arguments are literals, look for a type of the right kind | |
1347 | -- defined in the given scope. This elaborate nonsense is brought to | |
1348 | -- you courtesy of b33302a. The type itself must be frozen, so we must | |
1349 | -- find the type of the proper class in the given scope. | |
1350 | ||
06f2efd7 TQ |
1351 | -- A final wrinkle is the multiplication operator for fixed point types, |
1352 | -- which is defined in Standard only, and not in the scope of the | |
b4a4936b | 1353 | -- fixed point type itself. |
996ae0b0 RK |
1354 | |
1355 | if Nkind (Name (N)) = N_Expanded_Name then | |
1356 | Pack := Entity (Prefix (Name (N))); | |
1357 | ||
06f2efd7 TQ |
1358 | -- If the entity being called is defined in the given package, it is |
1359 | -- a renaming of a predefined operator, and known to be legal. | |
996ae0b0 RK |
1360 | |
1361 | if Scope (Entity (Name (N))) = Pack | |
1362 | and then Pack /= Standard_Standard | |
1363 | then | |
1364 | null; | |
1365 | ||
9ebe3743 HK |
1366 | -- Visibility does not need to be checked in an instance: if the |
1367 | -- operator was not visible in the generic it has been diagnosed | |
1368 | -- already, else there is an implicit copy of it in the instance. | |
1369 | ||
1370 | elsif In_Instance then | |
1371 | null; | |
1372 | ||
12577815 | 1373 | elsif (Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide) |
996ae0b0 RK |
1374 | and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node))) |
1375 | and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node))) | |
1376 | then | |
1377 | if Pack /= Standard_Standard then | |
1378 | Error := True; | |
1379 | end if; | |
1380 | ||
b4a4936b | 1381 | -- Ada 2005 AI-420: Predefined equality on Universal_Access is |
06f2efd7 | 1382 | -- available. |
c8ef728f | 1383 | |
0791fbe9 | 1384 | elsif Ada_Version >= Ada_2005 |
c8ef728f ES |
1385 | and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) |
1386 | and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type | |
1387 | then | |
1388 | null; | |
1389 | ||
996ae0b0 RK |
1390 | else |
1391 | Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node))); | |
1392 | ||
1393 | if Op_Name = Name_Op_Concat then | |
1394 | Opnd_Type := Base_Type (Typ); | |
1395 | ||
1396 | elsif (Scope (Opnd_Type) = Standard_Standard | |
1397 | and then Is_Binary) | |
1398 | or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference | |
1399 | and then Is_Binary | |
1400 | and then not Comes_From_Source (Opnd_Type)) | |
1401 | then | |
1402 | Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node))); | |
1403 | end if; | |
1404 | ||
1405 | if Scope (Opnd_Type) = Standard_Standard then | |
1406 | ||
1407 | -- Verify that the scope contains a type that corresponds to | |
1408 | -- the given literal. Optimize the case where Pack is Standard. | |
1409 | ||
1410 | if Pack /= Standard_Standard then | |
1411 | ||
1412 | if Opnd_Type = Universal_Integer then | |
06f2efd7 | 1413 | Orig_Type := Type_In_P (Is_Integer_Type'Access); |
996ae0b0 RK |
1414 | |
1415 | elsif Opnd_Type = Universal_Real then | |
1416 | Orig_Type := Type_In_P (Is_Real_Type'Access); | |
1417 | ||
1418 | elsif Opnd_Type = Any_String then | |
1419 | Orig_Type := Type_In_P (Is_String_Type'Access); | |
1420 | ||
1421 | elsif Opnd_Type = Any_Access then | |
06f2efd7 | 1422 | Orig_Type := Type_In_P (Is_Definite_Access_Type'Access); |
996ae0b0 RK |
1423 | |
1424 | elsif Opnd_Type = Any_Composite then | |
1425 | Orig_Type := Type_In_P (Is_Composite_Type'Access); | |
1426 | ||
1427 | if Present (Orig_Type) then | |
1428 | if Has_Private_Component (Orig_Type) then | |
1429 | Orig_Type := Empty; | |
1430 | else | |
1431 | Set_Etype (Act1, Orig_Type); | |
1432 | ||
1433 | if Is_Binary then | |
1434 | Set_Etype (Act2, Orig_Type); | |
1435 | end if; | |
1436 | end if; | |
1437 | end if; | |
1438 | ||
1439 | else | |
1440 | Orig_Type := Empty; | |
1441 | end if; | |
1442 | ||
1443 | Error := No (Orig_Type); | |
1444 | end if; | |
1445 | ||
1446 | elsif Ekind (Opnd_Type) = E_Allocator_Type | |
1447 | and then No (Type_In_P (Is_Definite_Access_Type'Access)) | |
1448 | then | |
1449 | Error := True; | |
1450 | ||
1451 | -- If the type is defined elsewhere, and the operator is not | |
1452 | -- defined in the given scope (by a renaming declaration, e.g.) | |
1453 | -- then this is an error as well. If an extension of System is | |
1454 | -- present, and the type may be defined there, Pack must be | |
1455 | -- System itself. | |
1456 | ||
1457 | elsif Scope (Opnd_Type) /= Pack | |
1458 | and then Scope (Op_Id) /= Pack | |
1459 | and then (No (System_Aux_Id) | |
1460 | or else Scope (Opnd_Type) /= System_Aux_Id | |
1461 | or else Pack /= Scope (System_Aux_Id)) | |
1462 | then | |
244e5a2c AC |
1463 | if not Is_Overloaded (Right_Opnd (Op_Node)) then |
1464 | Error := True; | |
1465 | else | |
1466 | Error := not Operand_Type_In_Scope (Pack); | |
1467 | end if; | |
996ae0b0 RK |
1468 | |
1469 | elsif Pack = Standard_Standard | |
1470 | and then not Operand_Type_In_Scope (Standard_Standard) | |
1471 | then | |
1472 | Error := True; | |
1473 | end if; | |
1474 | end if; | |
1475 | ||
1476 | if Error then | |
1477 | Error_Msg_Node_2 := Pack; | |
1478 | Error_Msg_NE | |
1479 | ("& not declared in&", N, Selector_Name (Name (N))); | |
1480 | Set_Etype (N, Any_Type); | |
1481 | return; | |
88b17d45 AC |
1482 | |
1483 | -- Detect a mismatch between the context type and the result type | |
1484 | -- in the named package, which is otherwise not detected if the | |
1485 | -- operands are universal. Check is only needed if source entity is | |
1486 | -- an operator, not a function that renames an operator. | |
1487 | ||
1488 | elsif Nkind (Parent (N)) /= N_Type_Conversion | |
1489 | and then Ekind (Entity (Name (N))) = E_Operator | |
1490 | and then Is_Numeric_Type (Typ) | |
1491 | and then not Is_Universal_Numeric_Type (Typ) | |
1492 | and then Scope (Base_Type (Typ)) /= Pack | |
1493 | and then not In_Instance | |
1494 | then | |
1495 | if Is_Fixed_Point_Type (Typ) | |
1496 | and then (Op_Name = Name_Op_Multiply | |
1497 | or else | |
1498 | Op_Name = Name_Op_Divide) | |
1499 | then | |
1500 | -- Already checked above | |
1501 | ||
1502 | null; | |
1503 | ||
e86a3a7e | 1504 | -- Operator may be defined in an extension of System |
80c3be7a AC |
1505 | |
1506 | elsif Present (System_Aux_Id) | |
1507 | and then Scope (Opnd_Type) = System_Aux_Id | |
1508 | then | |
1509 | null; | |
1510 | ||
88b17d45 | 1511 | else |
be5a1b93 TQ |
1512 | -- Could we use Wrong_Type here??? (this would require setting |
1513 | -- Etype (N) to the actual type found where Typ was expected). | |
1514 | ||
e86a3a7e | 1515 | Error_Msg_NE ("expect }", N, Typ); |
88b17d45 | 1516 | end if; |
996ae0b0 RK |
1517 | end if; |
1518 | end if; | |
1519 | ||
1520 | Set_Chars (Op_Node, Op_Name); | |
fbf5a39b AC |
1521 | |
1522 | if not Is_Private_Type (Etype (N)) then | |
1523 | Set_Etype (Op_Node, Base_Type (Etype (N))); | |
1524 | else | |
1525 | Set_Etype (Op_Node, Etype (N)); | |
1526 | end if; | |
1527 | ||
2820d220 AC |
1528 | -- If this is a call to a function that renames a predefined equality, |
1529 | -- the renaming declaration provides a type that must be used to | |
1530 | -- resolve the operands. This must be done now because resolution of | |
1531 | -- the equality node will not resolve any remaining ambiguity, and it | |
1532 | -- assumes that the first operand is not overloaded. | |
1533 | ||
1534 | if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne) | |
1535 | and then Ekind (Func) = E_Function | |
1536 | and then Is_Overloaded (Act1) | |
1537 | then | |
1538 | Resolve (Act1, Base_Type (Etype (First_Formal (Func)))); | |
1539 | Resolve (Act2, Base_Type (Etype (First_Formal (Func)))); | |
1540 | end if; | |
1541 | ||
996ae0b0 RK |
1542 | Set_Entity (Op_Node, Op_Id); |
1543 | Generate_Reference (Op_Id, N, ' '); | |
45fc7ddb HK |
1544 | |
1545 | -- Do rewrite setting Comes_From_Source on the result if the original | |
1546 | -- call came from source. Although it is not strictly the case that the | |
1547 | -- operator as such comes from the source, logically it corresponds | |
1548 | -- exactly to the function call in the source, so it should be marked | |
1549 | -- this way (e.g. to make sure that validity checks work fine). | |
1550 | ||
1551 | declare | |
1552 | CS : constant Boolean := Comes_From_Source (N); | |
1553 | begin | |
1554 | Rewrite (N, Op_Node); | |
1555 | Set_Comes_From_Source (N, CS); | |
1556 | end; | |
fbf5a39b AC |
1557 | |
1558 | -- If this is an arithmetic operator and the result type is private, | |
1559 | -- the operands and the result must be wrapped in conversion to | |
1560 | -- expose the underlying numeric type and expand the proper checks, | |
1561 | -- e.g. on division. | |
1562 | ||
1563 | if Is_Private_Type (Typ) then | |
1564 | case Nkind (N) is | |
5cc9353d RD |
1565 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | |
1566 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
fbf5a39b AC |
1567 | Resolve_Intrinsic_Operator (N, Typ); |
1568 | ||
5cc9353d | 1569 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => |
fbf5a39b AC |
1570 | Resolve_Intrinsic_Unary_Operator (N, Typ); |
1571 | ||
1572 | when others => | |
1573 | Resolve (N, Typ); | |
1574 | end case; | |
1575 | else | |
1576 | Resolve (N, Typ); | |
1577 | end if; | |
996ae0b0 RK |
1578 | end Make_Call_Into_Operator; |
1579 | ||
1580 | ------------------- | |
1581 | -- Operator_Kind -- | |
1582 | ------------------- | |
1583 | ||
1584 | function Operator_Kind | |
1585 | (Op_Name : Name_Id; | |
0ab80019 | 1586 | Is_Binary : Boolean) return Node_Kind |
996ae0b0 RK |
1587 | is |
1588 | Kind : Node_Kind; | |
1589 | ||
1590 | begin | |
b0186f71 AC |
1591 | -- Use CASE statement or array??? |
1592 | ||
996ae0b0 | 1593 | if Is_Binary then |
aa5147f0 ES |
1594 | if Op_Name = Name_Op_And then |
1595 | Kind := N_Op_And; | |
1596 | elsif Op_Name = Name_Op_Or then | |
1597 | Kind := N_Op_Or; | |
1598 | elsif Op_Name = Name_Op_Xor then | |
1599 | Kind := N_Op_Xor; | |
1600 | elsif Op_Name = Name_Op_Eq then | |
1601 | Kind := N_Op_Eq; | |
1602 | elsif Op_Name = Name_Op_Ne then | |
1603 | Kind := N_Op_Ne; | |
1604 | elsif Op_Name = Name_Op_Lt then | |
1605 | Kind := N_Op_Lt; | |
1606 | elsif Op_Name = Name_Op_Le then | |
1607 | Kind := N_Op_Le; | |
1608 | elsif Op_Name = Name_Op_Gt then | |
1609 | Kind := N_Op_Gt; | |
1610 | elsif Op_Name = Name_Op_Ge then | |
1611 | Kind := N_Op_Ge; | |
1612 | elsif Op_Name = Name_Op_Add then | |
1613 | Kind := N_Op_Add; | |
1614 | elsif Op_Name = Name_Op_Subtract then | |
1615 | Kind := N_Op_Subtract; | |
1616 | elsif Op_Name = Name_Op_Concat then | |
1617 | Kind := N_Op_Concat; | |
1618 | elsif Op_Name = Name_Op_Multiply then | |
1619 | Kind := N_Op_Multiply; | |
1620 | elsif Op_Name = Name_Op_Divide then | |
1621 | Kind := N_Op_Divide; | |
1622 | elsif Op_Name = Name_Op_Mod then | |
1623 | Kind := N_Op_Mod; | |
1624 | elsif Op_Name = Name_Op_Rem then | |
1625 | Kind := N_Op_Rem; | |
1626 | elsif Op_Name = Name_Op_Expon then | |
1627 | Kind := N_Op_Expon; | |
996ae0b0 RK |
1628 | else |
1629 | raise Program_Error; | |
1630 | end if; | |
1631 | ||
1632 | -- Unary operators | |
1633 | ||
1634 | else | |
aa5147f0 ES |
1635 | if Op_Name = Name_Op_Add then |
1636 | Kind := N_Op_Plus; | |
1637 | elsif Op_Name = Name_Op_Subtract then | |
1638 | Kind := N_Op_Minus; | |
1639 | elsif Op_Name = Name_Op_Abs then | |
1640 | Kind := N_Op_Abs; | |
1641 | elsif Op_Name = Name_Op_Not then | |
1642 | Kind := N_Op_Not; | |
996ae0b0 RK |
1643 | else |
1644 | raise Program_Error; | |
1645 | end if; | |
1646 | end if; | |
1647 | ||
1648 | return Kind; | |
1649 | end Operator_Kind; | |
1650 | ||
45fc7ddb HK |
1651 | ---------------------------- |
1652 | -- Preanalyze_And_Resolve -- | |
1653 | ---------------------------- | |
996ae0b0 | 1654 | |
45fc7ddb | 1655 | procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is |
996ae0b0 RK |
1656 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1657 | ||
1658 | begin | |
1659 | Full_Analysis := False; | |
1660 | Expander_Mode_Save_And_Set (False); | |
1661 | ||
1662 | -- We suppress all checks for this analysis, since the checks will | |
1663 | -- be applied properly, and in the right location, when the default | |
1664 | -- expression is reanalyzed and reexpanded later on. | |
1665 | ||
1666 | Analyze_And_Resolve (N, T, Suppress => All_Checks); | |
1667 | ||
1668 | Expander_Mode_Restore; | |
1669 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1670 | end Preanalyze_And_Resolve; |
996ae0b0 | 1671 | |
a77842bd | 1672 | -- Version without context type |
996ae0b0 | 1673 | |
45fc7ddb | 1674 | procedure Preanalyze_And_Resolve (N : Node_Id) is |
996ae0b0 RK |
1675 | Save_Full_Analysis : constant Boolean := Full_Analysis; |
1676 | ||
1677 | begin | |
1678 | Full_Analysis := False; | |
1679 | Expander_Mode_Save_And_Set (False); | |
1680 | ||
1681 | Analyze (N); | |
1682 | Resolve (N, Etype (N), Suppress => All_Checks); | |
1683 | ||
1684 | Expander_Mode_Restore; | |
1685 | Full_Analysis := Save_Full_Analysis; | |
45fc7ddb | 1686 | end Preanalyze_And_Resolve; |
996ae0b0 RK |
1687 | |
1688 | ---------------------------------- | |
1689 | -- Replace_Actual_Discriminants -- | |
1690 | ---------------------------------- | |
1691 | ||
1692 | procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is | |
1693 | Loc : constant Source_Ptr := Sloc (N); | |
1694 | Tsk : Node_Id := Empty; | |
1695 | ||
1696 | function Process_Discr (Nod : Node_Id) return Traverse_Result; | |
e0296583 | 1697 | -- Comment needed??? |
996ae0b0 RK |
1698 | |
1699 | ------------------- | |
1700 | -- Process_Discr -- | |
1701 | ------------------- | |
1702 | ||
1703 | function Process_Discr (Nod : Node_Id) return Traverse_Result is | |
1704 | Ent : Entity_Id; | |
1705 | ||
1706 | begin | |
1707 | if Nkind (Nod) = N_Identifier then | |
1708 | Ent := Entity (Nod); | |
1709 | ||
1710 | if Present (Ent) | |
1711 | and then Ekind (Ent) = E_Discriminant | |
1712 | then | |
1713 | Rewrite (Nod, | |
1714 | Make_Selected_Component (Loc, | |
1715 | Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc), | |
1716 | Selector_Name => Make_Identifier (Loc, Chars (Ent)))); | |
1717 | ||
1718 | Set_Etype (Nod, Etype (Ent)); | |
1719 | end if; | |
1720 | ||
1721 | end if; | |
1722 | ||
1723 | return OK; | |
1724 | end Process_Discr; | |
1725 | ||
1726 | procedure Replace_Discrs is new Traverse_Proc (Process_Discr); | |
1727 | ||
1728 | -- Start of processing for Replace_Actual_Discriminants | |
1729 | ||
1730 | begin | |
11fa950b | 1731 | if not Full_Expander_Active then |
996ae0b0 RK |
1732 | return; |
1733 | end if; | |
1734 | ||
1735 | if Nkind (Name (N)) = N_Selected_Component then | |
1736 | Tsk := Prefix (Name (N)); | |
1737 | ||
1738 | elsif Nkind (Name (N)) = N_Indexed_Component then | |
1739 | Tsk := Prefix (Prefix (Name (N))); | |
1740 | end if; | |
1741 | ||
1742 | if No (Tsk) then | |
1743 | return; | |
1744 | else | |
1745 | Replace_Discrs (Default); | |
1746 | end if; | |
1747 | end Replace_Actual_Discriminants; | |
1748 | ||
1749 | ------------- | |
1750 | -- Resolve -- | |
1751 | ------------- | |
1752 | ||
1753 | procedure Resolve (N : Node_Id; Typ : Entity_Id) is | |
dae2b8ea HK |
1754 | Ambiguous : Boolean := False; |
1755 | Ctx_Type : Entity_Id := Typ; | |
1756 | Expr_Type : Entity_Id := Empty; -- prevent junk warning | |
1757 | Err_Type : Entity_Id := Empty; | |
1758 | Found : Boolean := False; | |
1759 | From_Lib : Boolean; | |
996ae0b0 | 1760 | I : Interp_Index; |
dae2b8ea | 1761 | I1 : Interp_Index := 0; -- prevent junk warning |
996ae0b0 RK |
1762 | It : Interp; |
1763 | It1 : Interp; | |
996ae0b0 | 1764 | Seen : Entity_Id := Empty; -- prevent junk warning |
dae2b8ea HK |
1765 | |
1766 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean; | |
1767 | -- Determine whether a node comes from a predefined library unit or | |
1768 | -- Standard. | |
996ae0b0 RK |
1769 | |
1770 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id); | |
1771 | -- Try and fix up a literal so that it matches its expected type. New | |
1772 | -- literals are manufactured if necessary to avoid cascaded errors. | |
1773 | ||
804670f1 AC |
1774 | function Proper_Current_Scope return Entity_Id; |
1775 | -- Return the current scope. Skip loop scopes created for the purpose of | |
1776 | -- quantified expression analysis since those do not appear in the tree. | |
1777 | ||
7415029d AC |
1778 | procedure Report_Ambiguous_Argument; |
1779 | -- Additional diagnostics when an ambiguous call has an ambiguous | |
1780 | -- argument (typically a controlling actual). | |
1781 | ||
996ae0b0 RK |
1782 | procedure Resolution_Failed; |
1783 | -- Called when attempt at resolving current expression fails | |
1784 | ||
dae2b8ea HK |
1785 | ------------------------------------ |
1786 | -- Comes_From_Predefined_Lib_Unit -- | |
1787 | ------------------------------------- | |
1788 | ||
1789 | function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is | |
1790 | begin | |
1791 | return | |
1792 | Sloc (Nod) = Standard_Location | |
5cc9353d RD |
1793 | or else Is_Predefined_File_Name |
1794 | (Unit_File_Name (Get_Source_Unit (Sloc (Nod)))); | |
dae2b8ea HK |
1795 | end Comes_From_Predefined_Lib_Unit; |
1796 | ||
996ae0b0 RK |
1797 | -------------------- |
1798 | -- Patch_Up_Value -- | |
1799 | -------------------- | |
1800 | ||
1801 | procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is | |
1802 | begin | |
e0296583 | 1803 | if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then |
996ae0b0 RK |
1804 | Rewrite (N, |
1805 | Make_Real_Literal (Sloc (N), | |
1806 | Realval => UR_From_Uint (Intval (N)))); | |
1807 | Set_Etype (N, Universal_Real); | |
1808 | Set_Is_Static_Expression (N); | |
1809 | ||
e0296583 | 1810 | elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then |
996ae0b0 RK |
1811 | Rewrite (N, |
1812 | Make_Integer_Literal (Sloc (N), | |
1813 | Intval => UR_To_Uint (Realval (N)))); | |
1814 | Set_Etype (N, Universal_Integer); | |
1815 | Set_Is_Static_Expression (N); | |
45fc7ddb | 1816 | |
996ae0b0 | 1817 | elsif Nkind (N) = N_String_Literal |
e0296583 | 1818 | and then Is_Character_Type (Typ) |
996ae0b0 RK |
1819 | then |
1820 | Set_Character_Literal_Name (Char_Code (Character'Pos ('A'))); | |
1821 | Rewrite (N, | |
1822 | Make_Character_Literal (Sloc (N), | |
1823 | Chars => Name_Find, | |
82c80734 RD |
1824 | Char_Literal_Value => |
1825 | UI_From_Int (Character'Pos ('A')))); | |
996ae0b0 RK |
1826 | Set_Etype (N, Any_Character); |
1827 | Set_Is_Static_Expression (N); | |
1828 | ||
e0296583 | 1829 | elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then |
996ae0b0 RK |
1830 | Rewrite (N, |
1831 | Make_String_Literal (Sloc (N), | |
1832 | Strval => End_String)); | |
1833 | ||
1834 | elsif Nkind (N) = N_Range then | |
e0296583 | 1835 | Patch_Up_Value (Low_Bound (N), Typ); |
996ae0b0 RK |
1836 | Patch_Up_Value (High_Bound (N), Typ); |
1837 | end if; | |
1838 | end Patch_Up_Value; | |
1839 | ||
804670f1 AC |
1840 | -------------------------- |
1841 | -- Proper_Current_Scope -- | |
1842 | -------------------------- | |
1843 | ||
1844 | function Proper_Current_Scope return Entity_Id is | |
1845 | S : Entity_Id := Current_Scope; | |
1846 | ||
1847 | begin | |
1848 | while Present (S) loop | |
1849 | ||
1850 | -- Skip a loop scope created for quantified expression analysis | |
1851 | ||
1852 | if Ekind (S) = E_Loop | |
1853 | and then Nkind (Parent (S)) = N_Quantified_Expression | |
1854 | then | |
1855 | S := Scope (S); | |
1856 | else | |
1857 | exit; | |
1858 | end if; | |
1859 | end loop; | |
1860 | ||
1861 | return S; | |
1862 | end Proper_Current_Scope; | |
1863 | ||
7415029d AC |
1864 | ------------------------------- |
1865 | -- Report_Ambiguous_Argument -- | |
1866 | ------------------------------- | |
1867 | ||
1868 | procedure Report_Ambiguous_Argument is | |
1869 | Arg : constant Node_Id := First (Parameter_Associations (N)); | |
1870 | I : Interp_Index; | |
1871 | It : Interp; | |
1872 | ||
1873 | begin | |
1874 | if Nkind (Arg) = N_Function_Call | |
1875 | and then Is_Entity_Name (Name (Arg)) | |
1876 | and then Is_Overloaded (Name (Arg)) | |
1877 | then | |
ed2233dc | 1878 | Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg)); |
7415029d | 1879 | |
e0296583 | 1880 | -- Could use comments on what is going on here??? |
bfc07071 | 1881 | |
7415029d AC |
1882 | Get_First_Interp (Name (Arg), I, It); |
1883 | while Present (It.Nam) loop | |
1884 | Error_Msg_Sloc := Sloc (It.Nam); | |
1885 | ||
1886 | if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then | |
ed2233dc | 1887 | Error_Msg_N ("interpretation (inherited) #!", Arg); |
7415029d | 1888 | else |
ed2233dc | 1889 | Error_Msg_N ("interpretation #!", Arg); |
7415029d AC |
1890 | end if; |
1891 | ||
1892 | Get_Next_Interp (I, It); | |
1893 | end loop; | |
1894 | end if; | |
1895 | end Report_Ambiguous_Argument; | |
1896 | ||
996ae0b0 RK |
1897 | ----------------------- |
1898 | -- Resolution_Failed -- | |
1899 | ----------------------- | |
1900 | ||
1901 | procedure Resolution_Failed is | |
1902 | begin | |
1903 | Patch_Up_Value (N, Typ); | |
1904 | Set_Etype (N, Typ); | |
1905 | Debug_A_Exit ("resolving ", N, " (done, resolution failed)"); | |
1906 | Set_Is_Overloaded (N, False); | |
1907 | ||
1908 | -- The caller will return without calling the expander, so we need | |
1909 | -- to set the analyzed flag. Note that it is fine to set Analyzed | |
1910 | -- to True even if we are in the middle of a shallow analysis, | |
1911 | -- (see the spec of sem for more details) since this is an error | |
1912 | -- situation anyway, and there is no point in repeating the | |
1913 | -- analysis later (indeed it won't work to repeat it later, since | |
1914 | -- we haven't got a clear resolution of which entity is being | |
1915 | -- referenced.) | |
1916 | ||
1917 | Set_Analyzed (N, True); | |
1918 | return; | |
1919 | end Resolution_Failed; | |
1920 | ||
1921 | -- Start of processing for Resolve | |
1922 | ||
1923 | begin | |
5c736541 RD |
1924 | if N = Error then |
1925 | return; | |
1926 | end if; | |
1927 | ||
e0296583 AC |
1928 | -- Access attribute on remote subprogram cannot be used for a non-remote |
1929 | -- access-to-subprogram type. | |
996ae0b0 RK |
1930 | |
1931 | if Nkind (N) = N_Attribute_Reference | |
19fb051c AC |
1932 | and then (Attribute_Name (N) = Name_Access or else |
1933 | Attribute_Name (N) = Name_Unrestricted_Access or else | |
1934 | Attribute_Name (N) = Name_Unchecked_Access) | |
996ae0b0 RK |
1935 | and then Comes_From_Source (N) |
1936 | and then Is_Entity_Name (Prefix (N)) | |
1937 | and then Is_Subprogram (Entity (Prefix (N))) | |
1938 | and then Is_Remote_Call_Interface (Entity (Prefix (N))) | |
1939 | and then not Is_Remote_Access_To_Subprogram_Type (Typ) | |
1940 | then | |
1941 | Error_Msg_N | |
1942 | ("prefix must statically denote a non-remote subprogram", N); | |
1943 | end if; | |
1944 | ||
dae2b8ea HK |
1945 | From_Lib := Comes_From_Predefined_Lib_Unit (N); |
1946 | ||
996ae0b0 RK |
1947 | -- If the context is a Remote_Access_To_Subprogram, access attributes |
1948 | -- must be resolved with the corresponding fat pointer. There is no need | |
1949 | -- to check for the attribute name since the return type of an | |
1950 | -- attribute is never a remote type. | |
1951 | ||
1952 | if Nkind (N) = N_Attribute_Reference | |
1953 | and then Comes_From_Source (N) | |
19fb051c | 1954 | and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ)) |
996ae0b0 RK |
1955 | then |
1956 | declare | |
1957 | Attr : constant Attribute_Id := | |
1958 | Get_Attribute_Id (Attribute_Name (N)); | |
1959 | Pref : constant Node_Id := Prefix (N); | |
1960 | Decl : Node_Id; | |
1961 | Spec : Node_Id; | |
1962 | Is_Remote : Boolean := True; | |
1963 | ||
1964 | begin | |
a77842bd | 1965 | -- Check that Typ is a remote access-to-subprogram type |
996ae0b0 | 1966 | |
a77842bd | 1967 | if Is_Remote_Access_To_Subprogram_Type (Typ) then |
955871d3 | 1968 | |
996ae0b0 RK |
1969 | -- Prefix (N) must statically denote a remote subprogram |
1970 | -- declared in a package specification. | |
1971 | ||
799d0e05 AC |
1972 | if Attr = Attribute_Access or else |
1973 | Attr = Attribute_Unchecked_Access or else | |
1974 | Attr = Attribute_Unrestricted_Access | |
1975 | then | |
996ae0b0 RK |
1976 | Decl := Unit_Declaration_Node (Entity (Pref)); |
1977 | ||
1978 | if Nkind (Decl) = N_Subprogram_Body then | |
1979 | Spec := Corresponding_Spec (Decl); | |
1980 | ||
1981 | if not No (Spec) then | |
1982 | Decl := Unit_Declaration_Node (Spec); | |
1983 | end if; | |
1984 | end if; | |
1985 | ||
1986 | Spec := Parent (Decl); | |
1987 | ||
1988 | if not Is_Entity_Name (Prefix (N)) | |
1989 | or else Nkind (Spec) /= N_Package_Specification | |
1990 | or else | |
1991 | not Is_Remote_Call_Interface (Defining_Entity (Spec)) | |
1992 | then | |
1993 | Is_Remote := False; | |
1994 | Error_Msg_N | |
1995 | ("prefix must statically denote a remote subprogram ", | |
1996 | N); | |
1997 | end if; | |
996ae0b0 | 1998 | |
799d0e05 AC |
1999 | -- If we are generating code in distributed mode, perform |
2000 | -- semantic checks against corresponding remote entities. | |
fbf5a39b | 2001 | |
799d0e05 AC |
2002 | if Full_Expander_Active |
2003 | and then Get_PCS_Name /= Name_No_DSA | |
2004 | then | |
2005 | Check_Subtype_Conformant | |
2006 | (New_Id => Entity (Prefix (N)), | |
2007 | Old_Id => Designated_Type | |
2008 | (Corresponding_Remote_Type (Typ)), | |
2009 | Err_Loc => N); | |
2010 | ||
2011 | if Is_Remote then | |
2012 | Process_Remote_AST_Attribute (N, Typ); | |
2013 | end if; | |
996ae0b0 RK |
2014 | end if; |
2015 | end if; | |
2016 | end if; | |
2017 | end; | |
2018 | end if; | |
2019 | ||
2020 | Debug_A_Entry ("resolving ", N); | |
fe58fea7 | 2021 | |
ee1a7572 AC |
2022 | if Debug_Flag_V then |
2023 | Write_Overloads (N); | |
2024 | end if; | |
996ae0b0 | 2025 | |
07fc65c4 GB |
2026 | if Comes_From_Source (N) then |
2027 | if Is_Fixed_Point_Type (Typ) then | |
2028 | Check_Restriction (No_Fixed_Point, N); | |
996ae0b0 | 2029 | |
07fc65c4 GB |
2030 | elsif Is_Floating_Point_Type (Typ) |
2031 | and then Typ /= Universal_Real | |
2032 | and then Typ /= Any_Real | |
2033 | then | |
2034 | Check_Restriction (No_Floating_Point, N); | |
2035 | end if; | |
996ae0b0 RK |
2036 | end if; |
2037 | ||
2038 | -- Return if already analyzed | |
2039 | ||
2040 | if Analyzed (N) then | |
2041 | Debug_A_Exit ("resolving ", N, " (done, already analyzed)"); | |
dec6faf1 | 2042 | Analyze_Dimension (N); |
996ae0b0 RK |
2043 | return; |
2044 | ||
2045 | -- Return if type = Any_Type (previous error encountered) | |
2046 | ||
2047 | elsif Etype (N) = Any_Type then | |
2048 | Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)"); | |
2049 | return; | |
2050 | end if; | |
2051 | ||
2052 | Check_Parameterless_Call (N); | |
2053 | ||
2054 | -- If not overloaded, then we know the type, and all that needs doing | |
2055 | -- is to check that this type is compatible with the context. | |
2056 | ||
2057 | if not Is_Overloaded (N) then | |
2058 | Found := Covers (Typ, Etype (N)); | |
2059 | Expr_Type := Etype (N); | |
2060 | ||
2061 | -- In the overloaded case, we must select the interpretation that | |
2062 | -- is compatible with the context (i.e. the type passed to Resolve) | |
2063 | ||
2064 | else | |
996ae0b0 RK |
2065 | -- Loop through possible interpretations |
2066 | ||
1420b484 | 2067 | Get_First_Interp (N, I, It); |
996ae0b0 RK |
2068 | Interp_Loop : while Present (It.Typ) loop |
2069 | ||
ee1a7572 AC |
2070 | if Debug_Flag_V then |
2071 | Write_Str ("Interp: "); | |
2072 | Write_Interp (It); | |
2073 | end if; | |
2074 | ||
996ae0b0 | 2075 | -- We are only interested in interpretations that are compatible |
aa5147f0 | 2076 | -- with the expected type, any other interpretations are ignored. |
996ae0b0 | 2077 | |
fbf5a39b AC |
2078 | if not Covers (Typ, It.Typ) then |
2079 | if Debug_Flag_V then | |
2080 | Write_Str (" interpretation incompatible with context"); | |
2081 | Write_Eol; | |
2082 | end if; | |
996ae0b0 | 2083 | |
fbf5a39b | 2084 | else |
aa5147f0 ES |
2085 | -- Skip the current interpretation if it is disabled by an |
2086 | -- abstract operator. This action is performed only when the | |
2087 | -- type against which we are resolving is the same as the | |
2088 | -- type of the interpretation. | |
2089 | ||
0791fbe9 | 2090 | if Ada_Version >= Ada_2005 |
aa5147f0 ES |
2091 | and then It.Typ = Typ |
2092 | and then Typ /= Universal_Integer | |
2093 | and then Typ /= Universal_Real | |
2094 | and then Present (It.Abstract_Op) | |
2095 | then | |
ee1a7572 AC |
2096 | if Debug_Flag_V then |
2097 | Write_Line ("Skip."); | |
2098 | end if; | |
2099 | ||
aa5147f0 ES |
2100 | goto Continue; |
2101 | end if; | |
2102 | ||
996ae0b0 RK |
2103 | -- First matching interpretation |
2104 | ||
2105 | if not Found then | |
2106 | Found := True; | |
2107 | I1 := I; | |
2108 | Seen := It.Nam; | |
2109 | Expr_Type := It.Typ; | |
2110 | ||
fbf5a39b | 2111 | -- Matching interpretation that is not the first, maybe an |
996ae0b0 RK |
2112 | -- error, but there are some cases where preference rules are |
2113 | -- used to choose between the two possibilities. These and | |
2114 | -- some more obscure cases are handled in Disambiguate. | |
2115 | ||
2116 | else | |
dae2b8ea HK |
2117 | -- If the current statement is part of a predefined library |
2118 | -- unit, then all interpretations which come from user level | |
2119 | -- packages should not be considered. | |
2120 | ||
2121 | if From_Lib | |
2122 | and then not Comes_From_Predefined_Lib_Unit (It.Nam) | |
2123 | then | |
2124 | goto Continue; | |
2125 | end if; | |
2126 | ||
996ae0b0 RK |
2127 | Error_Msg_Sloc := Sloc (Seen); |
2128 | It1 := Disambiguate (N, I1, I, Typ); | |
2129 | ||
fbf5a39b AC |
2130 | -- Disambiguation has succeeded. Skip the remaining |
2131 | -- interpretations. | |
996ae0b0 | 2132 | |
fbf5a39b AC |
2133 | if It1 /= No_Interp then |
2134 | Seen := It1.Nam; | |
2135 | Expr_Type := It1.Typ; | |
2136 | ||
2137 | while Present (It.Typ) loop | |
2138 | Get_Next_Interp (I, It); | |
2139 | end loop; | |
2140 | ||
2141 | else | |
996ae0b0 RK |
2142 | -- Before we issue an ambiguity complaint, check for |
2143 | -- the case of a subprogram call where at least one | |
2144 | -- of the arguments is Any_Type, and if so, suppress | |
2145 | -- the message, since it is a cascaded error. | |
2146 | ||
d3b00ce3 | 2147 | if Nkind (N) in N_Subprogram_Call then |
996ae0b0 | 2148 | declare |
1420b484 | 2149 | A : Node_Id; |
996ae0b0 RK |
2150 | E : Node_Id; |
2151 | ||
2152 | begin | |
1420b484 | 2153 | A := First_Actual (N); |
996ae0b0 RK |
2154 | while Present (A) loop |
2155 | E := A; | |
2156 | ||
2157 | if Nkind (E) = N_Parameter_Association then | |
2158 | E := Explicit_Actual_Parameter (E); | |
2159 | end if; | |
2160 | ||
2161 | if Etype (E) = Any_Type then | |
2162 | if Debug_Flag_V then | |
2163 | Write_Str ("Any_Type in call"); | |
2164 | Write_Eol; | |
2165 | end if; | |
2166 | ||
2167 | exit Interp_Loop; | |
2168 | end if; | |
2169 | ||
2170 | Next_Actual (A); | |
2171 | end loop; | |
2172 | end; | |
2173 | ||
aa5147f0 | 2174 | elsif Nkind (N) in N_Binary_Op |
996ae0b0 RK |
2175 | and then (Etype (Left_Opnd (N)) = Any_Type |
2176 | or else Etype (Right_Opnd (N)) = Any_Type) | |
2177 | then | |
2178 | exit Interp_Loop; | |
2179 | ||
2180 | elsif Nkind (N) in N_Unary_Op | |
2181 | and then Etype (Right_Opnd (N)) = Any_Type | |
2182 | then | |
2183 | exit Interp_Loop; | |
2184 | end if; | |
2185 | ||
2186 | -- Not that special case, so issue message using the | |
2187 | -- flag Ambiguous to control printing of the header | |
2188 | -- message only at the start of an ambiguous set. | |
2189 | ||
2190 | if not Ambiguous then | |
aa180613 RD |
2191 | if Nkind (N) = N_Function_Call |
2192 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2193 | then | |
ed2233dc | 2194 | Error_Msg_N |
aa180613 RD |
2195 | ("ambiguous expression " |
2196 | & "(cannot resolve indirect call)!", N); | |
2197 | else | |
483c78cb | 2198 | Error_Msg_NE -- CODEFIX |
aa180613 RD |
2199 | ("ambiguous expression (cannot resolve&)!", |
2200 | N, It.Nam); | |
2201 | end if; | |
fbf5a39b | 2202 | |
996ae0b0 | 2203 | Ambiguous := True; |
0669bebe GB |
2204 | |
2205 | if Nkind (Parent (Seen)) = N_Full_Type_Declaration then | |
ed2233dc | 2206 | Error_Msg_N |
0669bebe GB |
2207 | ("\\possible interpretation (inherited)#!", N); |
2208 | else | |
4e7a4f6e AC |
2209 | Error_Msg_N -- CODEFIX |
2210 | ("\\possible interpretation#!", N); | |
0669bebe | 2211 | end if; |
7415029d | 2212 | |
d3b00ce3 | 2213 | if Nkind (N) in N_Subprogram_Call |
7415029d AC |
2214 | and then Present (Parameter_Associations (N)) |
2215 | then | |
2216 | Report_Ambiguous_Argument; | |
2217 | end if; | |
996ae0b0 RK |
2218 | end if; |
2219 | ||
2220 | Error_Msg_Sloc := Sloc (It.Nam); | |
996ae0b0 | 2221 | |
fbf5a39b | 2222 | -- By default, the error message refers to the candidate |
0669bebe GB |
2223 | -- interpretation. But if it is a predefined operator, it |
2224 | -- is implicitly declared at the declaration of the type | |
2225 | -- of the operand. Recover the sloc of that declaration | |
2226 | -- for the error message. | |
fbf5a39b AC |
2227 | |
2228 | if Nkind (N) in N_Op | |
2229 | and then Scope (It.Nam) = Standard_Standard | |
2230 | and then not Is_Overloaded (Right_Opnd (N)) | |
0669bebe GB |
2231 | and then Scope (Base_Type (Etype (Right_Opnd (N)))) /= |
2232 | Standard_Standard | |
fbf5a39b AC |
2233 | then |
2234 | Err_Type := First_Subtype (Etype (Right_Opnd (N))); | |
2235 | ||
2236 | if Comes_From_Source (Err_Type) | |
2237 | and then Present (Parent (Err_Type)) | |
2238 | then | |
2239 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2240 | end if; | |
2241 | ||
2242 | elsif Nkind (N) in N_Binary_Op | |
2243 | and then Scope (It.Nam) = Standard_Standard | |
2244 | and then not Is_Overloaded (Left_Opnd (N)) | |
0669bebe GB |
2245 | and then Scope (Base_Type (Etype (Left_Opnd (N)))) /= |
2246 | Standard_Standard | |
fbf5a39b AC |
2247 | then |
2248 | Err_Type := First_Subtype (Etype (Left_Opnd (N))); | |
2249 | ||
2250 | if Comes_From_Source (Err_Type) | |
2251 | and then Present (Parent (Err_Type)) | |
2252 | then | |
2253 | Error_Msg_Sloc := Sloc (Parent (Err_Type)); | |
2254 | end if; | |
aa180613 RD |
2255 | |
2256 | -- If this is an indirect call, use the subprogram_type | |
5cc9353d RD |
2257 | -- in the message, to have a meaningful location. Also |
2258 | -- indicate if this is an inherited operation, created | |
2259 | -- by a type declaration. | |
aa180613 RD |
2260 | |
2261 | elsif Nkind (N) = N_Function_Call | |
2262 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
2263 | and then Is_Type (It.Nam) | |
2264 | then | |
2265 | Err_Type := It.Nam; | |
2266 | Error_Msg_Sloc := | |
2267 | Sloc (Associated_Node_For_Itype (Err_Type)); | |
fbf5a39b AC |
2268 | else |
2269 | Err_Type := Empty; | |
2270 | end if; | |
2271 | ||
2272 | if Nkind (N) in N_Op | |
2273 | and then Scope (It.Nam) = Standard_Standard | |
2274 | and then Present (Err_Type) | |
2275 | then | |
aa5147f0 ES |
2276 | -- Special-case the message for universal_fixed |
2277 | -- operators, which are not declared with the type | |
2278 | -- of the operand, but appear forever in Standard. | |
2279 | ||
2280 | if It.Typ = Universal_Fixed | |
2281 | and then Scope (It.Nam) = Standard_Standard | |
2282 | then | |
ed2233dc | 2283 | Error_Msg_N |
aa5147f0 ES |
2284 | ("\\possible interpretation as " & |
2285 | "universal_fixed operation " & | |
2286 | "(RM 4.5.5 (19))", N); | |
2287 | else | |
ed2233dc | 2288 | Error_Msg_N |
aa5147f0 ES |
2289 | ("\\possible interpretation (predefined)#!", N); |
2290 | end if; | |
aa180613 RD |
2291 | |
2292 | elsif | |
2293 | Nkind (Parent (It.Nam)) = N_Full_Type_Declaration | |
2294 | then | |
ed2233dc | 2295 | Error_Msg_N |
aa180613 | 2296 | ("\\possible interpretation (inherited)#!", N); |
fbf5a39b | 2297 | else |
4e7a4f6e AC |
2298 | Error_Msg_N -- CODEFIX |
2299 | ("\\possible interpretation#!", N); | |
fbf5a39b | 2300 | end if; |
996ae0b0 | 2301 | |
996ae0b0 RK |
2302 | end if; |
2303 | end if; | |
2304 | ||
0669bebe GB |
2305 | -- We have a matching interpretation, Expr_Type is the type |
2306 | -- from this interpretation, and Seen is the entity. | |
996ae0b0 | 2307 | |
0669bebe GB |
2308 | -- For an operator, just set the entity name. The type will be |
2309 | -- set by the specific operator resolution routine. | |
996ae0b0 RK |
2310 | |
2311 | if Nkind (N) in N_Op then | |
2312 | Set_Entity (N, Seen); | |
2313 | Generate_Reference (Seen, N); | |
2314 | ||
19d846a0 RD |
2315 | elsif Nkind (N) = N_Case_Expression then |
2316 | Set_Etype (N, Expr_Type); | |
2317 | ||
996ae0b0 RK |
2318 | elsif Nkind (N) = N_Character_Literal then |
2319 | Set_Etype (N, Expr_Type); | |
2320 | ||
e0ba1bfd ES |
2321 | elsif Nkind (N) = N_Conditional_Expression then |
2322 | Set_Etype (N, Expr_Type); | |
2323 | ||
dedac3eb RD |
2324 | -- AI05-0139-2: Expression is overloaded because type has |
2325 | -- implicit dereference. If type matches context, no implicit | |
2326 | -- dereference is involved. | |
44a10091 AC |
2327 | |
2328 | elsif Has_Implicit_Dereference (Expr_Type) then | |
2329 | Set_Etype (N, Expr_Type); | |
2330 | Set_Is_Overloaded (N, False); | |
2331 | exit Interp_Loop; | |
2332 | ||
2333 | elsif Is_Overloaded (N) | |
2334 | and then Present (It.Nam) | |
2335 | and then Ekind (It.Nam) = E_Discriminant | |
2336 | and then Has_Implicit_Dereference (It.Nam) | |
2337 | then | |
2338 | Build_Explicit_Dereference (N, It.Nam); | |
2339 | ||
996ae0b0 | 2340 | -- For an explicit dereference, attribute reference, range, |
0669bebe GB |
2341 | -- short-circuit form (which is not an operator node), or call |
2342 | -- with a name that is an explicit dereference, there is | |
2343 | -- nothing to be done at this point. | |
996ae0b0 | 2344 | |
45fc7ddb HK |
2345 | elsif Nkind_In (N, N_Explicit_Dereference, |
2346 | N_Attribute_Reference, | |
2347 | N_And_Then, | |
2348 | N_Indexed_Component, | |
2349 | N_Or_Else, | |
2350 | N_Range, | |
2351 | N_Selected_Component, | |
2352 | N_Slice) | |
996ae0b0 RK |
2353 | or else Nkind (Name (N)) = N_Explicit_Dereference |
2354 | then | |
2355 | null; | |
2356 | ||
0669bebe | 2357 | -- For procedure or function calls, set the type of the name, |
4519314c | 2358 | -- and also the entity pointer for the prefix. |
996ae0b0 | 2359 | |
d3b00ce3 | 2360 | elsif Nkind (N) in N_Subprogram_Call |
a3f2babd | 2361 | and then Is_Entity_Name (Name (N)) |
996ae0b0 RK |
2362 | then |
2363 | Set_Etype (Name (N), Expr_Type); | |
2364 | Set_Entity (Name (N), Seen); | |
2365 | Generate_Reference (Seen, Name (N)); | |
2366 | ||
2367 | elsif Nkind (N) = N_Function_Call | |
2368 | and then Nkind (Name (N)) = N_Selected_Component | |
2369 | then | |
2370 | Set_Etype (Name (N), Expr_Type); | |
2371 | Set_Entity (Selector_Name (Name (N)), Seen); | |
2372 | Generate_Reference (Seen, Selector_Name (Name (N))); | |
2373 | ||
2374 | -- For all other cases, just set the type of the Name | |
2375 | ||
2376 | else | |
2377 | Set_Etype (Name (N), Expr_Type); | |
2378 | end if; | |
2379 | ||
996ae0b0 RK |
2380 | end if; |
2381 | ||
aa5147f0 ES |
2382 | <<Continue>> |
2383 | ||
996ae0b0 RK |
2384 | -- Move to next interpretation |
2385 | ||
c8ef728f | 2386 | exit Interp_Loop when No (It.Typ); |
996ae0b0 RK |
2387 | |
2388 | Get_Next_Interp (I, It); | |
2389 | end loop Interp_Loop; | |
2390 | end if; | |
2391 | ||
2392 | -- At this stage Found indicates whether or not an acceptable | |
4519314c AC |
2393 | -- interpretation exists. If not, then we have an error, except that if |
2394 | -- the context is Any_Type as a result of some other error, then we | |
2395 | -- suppress the error report. | |
996ae0b0 RK |
2396 | |
2397 | if not Found then | |
2398 | if Typ /= Any_Type then | |
2399 | ||
0669bebe GB |
2400 | -- If type we are looking for is Void, then this is the procedure |
2401 | -- call case, and the error is simply that what we gave is not a | |
2402 | -- procedure name (we think of procedure calls as expressions with | |
2403 | -- types internally, but the user doesn't think of them this way!) | |
996ae0b0 RK |
2404 | |
2405 | if Typ = Standard_Void_Type then | |
91b1417d AC |
2406 | |
2407 | -- Special case message if function used as a procedure | |
2408 | ||
2409 | if Nkind (N) = N_Procedure_Call_Statement | |
2410 | and then Is_Entity_Name (Name (N)) | |
2411 | and then Ekind (Entity (Name (N))) = E_Function | |
2412 | then | |
2413 | Error_Msg_NE | |
2414 | ("cannot use function & in a procedure call", | |
2415 | Name (N), Entity (Name (N))); | |
2416 | ||
0669bebe GB |
2417 | -- Otherwise give general message (not clear what cases this |
2418 | -- covers, but no harm in providing for them!) | |
91b1417d AC |
2419 | |
2420 | else | |
2421 | Error_Msg_N ("expect procedure name in procedure call", N); | |
2422 | end if; | |
2423 | ||
996ae0b0 RK |
2424 | Found := True; |
2425 | ||
2426 | -- Otherwise we do have a subexpression with the wrong type | |
2427 | ||
0669bebe GB |
2428 | -- Check for the case of an allocator which uses an access type |
2429 | -- instead of the designated type. This is a common error and we | |
2430 | -- specialize the message, posting an error on the operand of the | |
2431 | -- allocator, complaining that we expected the designated type of | |
2432 | -- the allocator. | |
996ae0b0 RK |
2433 | |
2434 | elsif Nkind (N) = N_Allocator | |
2435 | and then Ekind (Typ) in Access_Kind | |
2436 | and then Ekind (Etype (N)) in Access_Kind | |
2437 | and then Designated_Type (Etype (N)) = Typ | |
2438 | then | |
2439 | Wrong_Type (Expression (N), Designated_Type (Typ)); | |
2440 | Found := True; | |
2441 | ||
0669bebe GB |
2442 | -- Check for view mismatch on Null in instances, for which the |
2443 | -- view-swapping mechanism has no identifier. | |
17be0cdf ES |
2444 | |
2445 | elsif (In_Instance or else In_Inlined_Body) | |
2446 | and then (Nkind (N) = N_Null) | |
2447 | and then Is_Private_Type (Typ) | |
2448 | and then Is_Access_Type (Full_View (Typ)) | |
2449 | then | |
2450 | Resolve (N, Full_View (Typ)); | |
2451 | Set_Etype (N, Typ); | |
2452 | return; | |
2453 | ||
aa180613 RD |
2454 | -- Check for an aggregate. Sometimes we can get bogus aggregates |
2455 | -- from misuse of parentheses, and we are about to complain about | |
2456 | -- the aggregate without even looking inside it. | |
996ae0b0 | 2457 | |
aa180613 RD |
2458 | -- Instead, if we have an aggregate of type Any_Composite, then |
2459 | -- analyze and resolve the component fields, and then only issue | |
2460 | -- another message if we get no errors doing this (otherwise | |
2461 | -- assume that the errors in the aggregate caused the problem). | |
996ae0b0 RK |
2462 | |
2463 | elsif Nkind (N) = N_Aggregate | |
2464 | and then Etype (N) = Any_Composite | |
2465 | then | |
996ae0b0 RK |
2466 | -- Disable expansion in any case. If there is a type mismatch |
2467 | -- it may be fatal to try to expand the aggregate. The flag | |
2468 | -- would otherwise be set to false when the error is posted. | |
2469 | ||
2470 | Expander_Active := False; | |
2471 | ||
2472 | declare | |
2473 | procedure Check_Aggr (Aggr : Node_Id); | |
aa180613 RD |
2474 | -- Check one aggregate, and set Found to True if we have a |
2475 | -- definite error in any of its elements | |
996ae0b0 RK |
2476 | |
2477 | procedure Check_Elmt (Aelmt : Node_Id); | |
aa180613 RD |
2478 | -- Check one element of aggregate and set Found to True if |
2479 | -- we definitely have an error in the element. | |
2480 | ||
2481 | ---------------- | |
2482 | -- Check_Aggr -- | |
2483 | ---------------- | |
996ae0b0 RK |
2484 | |
2485 | procedure Check_Aggr (Aggr : Node_Id) is | |
2486 | Elmt : Node_Id; | |
2487 | ||
2488 | begin | |
2489 | if Present (Expressions (Aggr)) then | |
2490 | Elmt := First (Expressions (Aggr)); | |
2491 | while Present (Elmt) loop | |
2492 | Check_Elmt (Elmt); | |
2493 | Next (Elmt); | |
2494 | end loop; | |
2495 | end if; | |
2496 | ||
2497 | if Present (Component_Associations (Aggr)) then | |
2498 | Elmt := First (Component_Associations (Aggr)); | |
2499 | while Present (Elmt) loop | |
aa180613 | 2500 | |
0669bebe GB |
2501 | -- If this is a default-initialized component, then |
2502 | -- there is nothing to check. The box will be | |
2503 | -- replaced by the appropriate call during late | |
2504 | -- expansion. | |
aa180613 RD |
2505 | |
2506 | if not Box_Present (Elmt) then | |
2507 | Check_Elmt (Expression (Elmt)); | |
2508 | end if; | |
2509 | ||
996ae0b0 RK |
2510 | Next (Elmt); |
2511 | end loop; | |
2512 | end if; | |
2513 | end Check_Aggr; | |
2514 | ||
fbf5a39b AC |
2515 | ---------------- |
2516 | -- Check_Elmt -- | |
2517 | ---------------- | |
2518 | ||
996ae0b0 RK |
2519 | procedure Check_Elmt (Aelmt : Node_Id) is |
2520 | begin | |
2521 | -- If we have a nested aggregate, go inside it (to | |
5cc9353d RD |
2522 | -- attempt a naked analyze-resolve of the aggregate can |
2523 | -- cause undesirable cascaded errors). Do not resolve | |
2524 | -- expression if it needs a type from context, as for | |
2525 | -- integer * fixed expression. | |
996ae0b0 RK |
2526 | |
2527 | if Nkind (Aelmt) = N_Aggregate then | |
2528 | Check_Aggr (Aelmt); | |
2529 | ||
2530 | else | |
2531 | Analyze (Aelmt); | |
2532 | ||
2533 | if not Is_Overloaded (Aelmt) | |
2534 | and then Etype (Aelmt) /= Any_Fixed | |
2535 | then | |
fbf5a39b | 2536 | Resolve (Aelmt); |
996ae0b0 RK |
2537 | end if; |
2538 | ||
2539 | if Etype (Aelmt) = Any_Type then | |
2540 | Found := True; | |
2541 | end if; | |
2542 | end if; | |
2543 | end Check_Elmt; | |
2544 | ||
2545 | begin | |
2546 | Check_Aggr (N); | |
2547 | end; | |
2548 | end if; | |
2549 | ||
5cc9353d RD |
2550 | -- If an error message was issued already, Found got reset to |
2551 | -- True, so if it is still False, issue standard Wrong_Type msg. | |
996ae0b0 RK |
2552 | |
2553 | if not Found then | |
2554 | if Is_Overloaded (N) | |
2555 | and then Nkind (N) = N_Function_Call | |
2556 | then | |
65356e64 AC |
2557 | declare |
2558 | Subp_Name : Node_Id; | |
2559 | begin | |
2560 | if Is_Entity_Name (Name (N)) then | |
2561 | Subp_Name := Name (N); | |
2562 | ||
2563 | elsif Nkind (Name (N)) = N_Selected_Component then | |
2564 | ||
a77842bd | 2565 | -- Protected operation: retrieve operation name |
65356e64 AC |
2566 | |
2567 | Subp_Name := Selector_Name (Name (N)); | |
19fb051c | 2568 | |
65356e64 AC |
2569 | else |
2570 | raise Program_Error; | |
2571 | end if; | |
2572 | ||
2573 | Error_Msg_Node_2 := Typ; | |
2574 | Error_Msg_NE ("no visible interpretation of&" & | |
2575 | " matches expected type&", N, Subp_Name); | |
2576 | end; | |
996ae0b0 RK |
2577 | |
2578 | if All_Errors_Mode then | |
2579 | declare | |
2580 | Index : Interp_Index; | |
2581 | It : Interp; | |
2582 | ||
2583 | begin | |
aa180613 | 2584 | Error_Msg_N ("\\possible interpretations:", N); |
996ae0b0 | 2585 | |
1420b484 | 2586 | Get_First_Interp (Name (N), Index, It); |
996ae0b0 | 2587 | while Present (It.Nam) loop |
ea985d95 | 2588 | Error_Msg_Sloc := Sloc (It.Nam); |
aa5147f0 ES |
2589 | Error_Msg_Node_2 := It.Nam; |
2590 | Error_Msg_NE | |
2591 | ("\\ type& for & declared#", N, It.Typ); | |
996ae0b0 RK |
2592 | Get_Next_Interp (Index, It); |
2593 | end loop; | |
2594 | end; | |
aa5147f0 | 2595 | |
996ae0b0 RK |
2596 | else |
2597 | Error_Msg_N ("\use -gnatf for details", N); | |
2598 | end if; | |
19fb051c | 2599 | |
996ae0b0 RK |
2600 | else |
2601 | Wrong_Type (N, Typ); | |
2602 | end if; | |
2603 | end if; | |
2604 | end if; | |
2605 | ||
2606 | Resolution_Failed; | |
2607 | return; | |
2608 | ||
2609 | -- Test if we have more than one interpretation for the context | |
2610 | ||
2611 | elsif Ambiguous then | |
2612 | Resolution_Failed; | |
2613 | return; | |
2614 | ||
fe58fea7 AC |
2615 | -- Only one intepretation |
2616 | ||
996ae0b0 | 2617 | else |
ee1a7572 AC |
2618 | -- In Ada 2005, if we have something like "X : T := 2 + 2;", where |
2619 | -- the "+" on T is abstract, and the operands are of universal type, | |
2620 | -- the above code will have (incorrectly) resolved the "+" to the | |
fe58fea7 AC |
2621 | -- universal one in Standard. Therefore check for this case and give |
2622 | -- an error. We can't do this earlier, because it would cause legal | |
2623 | -- cases to get errors (when some other type has an abstract "+"). | |
ee1a7572 | 2624 | |
36504e5f AC |
2625 | if Ada_Version >= Ada_2005 |
2626 | and then Nkind (N) in N_Op | |
2627 | and then Is_Overloaded (N) | |
2628 | and then Is_Universal_Numeric_Type (Etype (Entity (N))) | |
ee1a7572 AC |
2629 | then |
2630 | Get_First_Interp (N, I, It); | |
2631 | while Present (It.Typ) loop | |
2632 | if Present (It.Abstract_Op) and then | |
2633 | Etype (It.Abstract_Op) = Typ | |
2634 | then | |
2635 | Error_Msg_NE | |
2636 | ("cannot call abstract subprogram &!", N, It.Abstract_Op); | |
2637 | return; | |
2638 | end if; | |
2639 | ||
2640 | Get_Next_Interp (I, It); | |
2641 | end loop; | |
2642 | end if; | |
2643 | ||
2644 | -- Here we have an acceptable interpretation for the context | |
2645 | ||
996ae0b0 RK |
2646 | -- Propagate type information and normalize tree for various |
2647 | -- predefined operations. If the context only imposes a class of | |
2648 | -- types, rather than a specific type, propagate the actual type | |
2649 | -- downward. | |
2650 | ||
19fb051c AC |
2651 | if Typ = Any_Integer or else |
2652 | Typ = Any_Boolean or else | |
2653 | Typ = Any_Modular or else | |
2654 | Typ = Any_Real or else | |
2655 | Typ = Any_Discrete | |
996ae0b0 RK |
2656 | then |
2657 | Ctx_Type := Expr_Type; | |
2658 | ||
5cc9353d RD |
2659 | -- Any_Fixed is legal in a real context only if a specific fixed- |
2660 | -- point type is imposed. If Norman Cohen can be confused by this, | |
2661 | -- it deserves a separate message. | |
996ae0b0 RK |
2662 | |
2663 | if Typ = Any_Real | |
2664 | and then Expr_Type = Any_Fixed | |
2665 | then | |
758c442c | 2666 | Error_Msg_N ("illegal context for mixed mode operation", N); |
996ae0b0 RK |
2667 | Set_Etype (N, Universal_Real); |
2668 | Ctx_Type := Universal_Real; | |
2669 | end if; | |
2670 | end if; | |
2671 | ||
f3d57416 | 2672 | -- A user-defined operator is transformed into a function call at |
0ab80019 AC |
2673 | -- this point, so that further processing knows that operators are |
2674 | -- really operators (i.e. are predefined operators). User-defined | |
2675 | -- operators that are intrinsic are just renamings of the predefined | |
2676 | -- ones, and need not be turned into calls either, but if they rename | |
2677 | -- a different operator, we must transform the node accordingly. | |
2678 | -- Instantiations of Unchecked_Conversion are intrinsic but are | |
2679 | -- treated as functions, even if given an operator designator. | |
2680 | ||
2681 | if Nkind (N) in N_Op | |
2682 | and then Present (Entity (N)) | |
2683 | and then Ekind (Entity (N)) /= E_Operator | |
2684 | then | |
2685 | ||
2686 | if not Is_Predefined_Op (Entity (N)) then | |
2687 | Rewrite_Operator_As_Call (N, Entity (N)); | |
2688 | ||
615cbd95 AC |
2689 | elsif Present (Alias (Entity (N))) |
2690 | and then | |
45fc7ddb HK |
2691 | Nkind (Parent (Parent (Entity (N)))) = |
2692 | N_Subprogram_Renaming_Declaration | |
615cbd95 | 2693 | then |
0ab80019 AC |
2694 | Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ); |
2695 | ||
2696 | -- If the node is rewritten, it will be fully resolved in | |
2697 | -- Rewrite_Renamed_Operator. | |
2698 | ||
2699 | if Analyzed (N) then | |
2700 | return; | |
2701 | end if; | |
2702 | end if; | |
2703 | end if; | |
2704 | ||
996ae0b0 RK |
2705 | case N_Subexpr'(Nkind (N)) is |
2706 | ||
2707 | when N_Aggregate => Resolve_Aggregate (N, Ctx_Type); | |
2708 | ||
2709 | when N_Allocator => Resolve_Allocator (N, Ctx_Type); | |
2710 | ||
514d0fc5 | 2711 | when N_Short_Circuit |
996ae0b0 RK |
2712 | => Resolve_Short_Circuit (N, Ctx_Type); |
2713 | ||
2714 | when N_Attribute_Reference | |
2715 | => Resolve_Attribute (N, Ctx_Type); | |
2716 | ||
19d846a0 RD |
2717 | when N_Case_Expression |
2718 | => Resolve_Case_Expression (N, Ctx_Type); | |
2719 | ||
996ae0b0 RK |
2720 | when N_Character_Literal |
2721 | => Resolve_Character_Literal (N, Ctx_Type); | |
2722 | ||
2723 | when N_Conditional_Expression | |
2724 | => Resolve_Conditional_Expression (N, Ctx_Type); | |
2725 | ||
2726 | when N_Expanded_Name | |
2727 | => Resolve_Entity_Name (N, Ctx_Type); | |
2728 | ||
996ae0b0 RK |
2729 | when N_Explicit_Dereference |
2730 | => Resolve_Explicit_Dereference (N, Ctx_Type); | |
2731 | ||
955871d3 AC |
2732 | when N_Expression_With_Actions |
2733 | => Resolve_Expression_With_Actions (N, Ctx_Type); | |
2734 | ||
2735 | when N_Extension_Aggregate | |
2736 | => Resolve_Extension_Aggregate (N, Ctx_Type); | |
2737 | ||
996ae0b0 RK |
2738 | when N_Function_Call |
2739 | => Resolve_Call (N, Ctx_Type); | |
2740 | ||
2741 | when N_Identifier | |
2742 | => Resolve_Entity_Name (N, Ctx_Type); | |
2743 | ||
996ae0b0 RK |
2744 | when N_Indexed_Component |
2745 | => Resolve_Indexed_Component (N, Ctx_Type); | |
2746 | ||
2747 | when N_Integer_Literal | |
2748 | => Resolve_Integer_Literal (N, Ctx_Type); | |
2749 | ||
0669bebe GB |
2750 | when N_Membership_Test |
2751 | => Resolve_Membership_Op (N, Ctx_Type); | |
2752 | ||
996ae0b0 RK |
2753 | when N_Null => Resolve_Null (N, Ctx_Type); |
2754 | ||
2755 | when N_Op_And | N_Op_Or | N_Op_Xor | |
2756 | => Resolve_Logical_Op (N, Ctx_Type); | |
2757 | ||
2758 | when N_Op_Eq | N_Op_Ne | |
2759 | => Resolve_Equality_Op (N, Ctx_Type); | |
2760 | ||
2761 | when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge | |
2762 | => Resolve_Comparison_Op (N, Ctx_Type); | |
2763 | ||
2764 | when N_Op_Not => Resolve_Op_Not (N, Ctx_Type); | |
2765 | ||
2766 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | | |
2767 | N_Op_Divide | N_Op_Mod | N_Op_Rem | |
2768 | ||
2769 | => Resolve_Arithmetic_Op (N, Ctx_Type); | |
2770 | ||
2771 | when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type); | |
2772 | ||
2773 | when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type); | |
2774 | ||
2775 | when N_Op_Plus | N_Op_Minus | N_Op_Abs | |
2776 | => Resolve_Unary_Op (N, Ctx_Type); | |
2777 | ||
2778 | when N_Op_Shift => Resolve_Shift (N, Ctx_Type); | |
2779 | ||
2780 | when N_Procedure_Call_Statement | |
2781 | => Resolve_Call (N, Ctx_Type); | |
2782 | ||
2783 | when N_Operator_Symbol | |
2784 | => Resolve_Operator_Symbol (N, Ctx_Type); | |
2785 | ||
2786 | when N_Qualified_Expression | |
2787 | => Resolve_Qualified_Expression (N, Ctx_Type); | |
2788 | ||
804670f1 | 2789 | when N_Quantified_Expression => null; |
a961aa79 | 2790 | |
996ae0b0 RK |
2791 | when N_Raise_xxx_Error |
2792 | => Set_Etype (N, Ctx_Type); | |
2793 | ||
2794 | when N_Range => Resolve_Range (N, Ctx_Type); | |
2795 | ||
2796 | when N_Real_Literal | |
2797 | => Resolve_Real_Literal (N, Ctx_Type); | |
2798 | ||
2799 | when N_Reference => Resolve_Reference (N, Ctx_Type); | |
2800 | ||
2801 | when N_Selected_Component | |
2802 | => Resolve_Selected_Component (N, Ctx_Type); | |
2803 | ||
2804 | when N_Slice => Resolve_Slice (N, Ctx_Type); | |
2805 | ||
2806 | when N_String_Literal | |
2807 | => Resolve_String_Literal (N, Ctx_Type); | |
2808 | ||
2809 | when N_Subprogram_Info | |
2810 | => Resolve_Subprogram_Info (N, Ctx_Type); | |
2811 | ||
2812 | when N_Type_Conversion | |
2813 | => Resolve_Type_Conversion (N, Ctx_Type); | |
2814 | ||
2815 | when N_Unchecked_Expression => | |
2816 | Resolve_Unchecked_Expression (N, Ctx_Type); | |
2817 | ||
2818 | when N_Unchecked_Type_Conversion => | |
2819 | Resolve_Unchecked_Type_Conversion (N, Ctx_Type); | |
996ae0b0 RK |
2820 | end case; |
2821 | ||
6cce2156 GD |
2822 | -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an |
2823 | -- expression of an anonymous access type that occurs in the context | |
2824 | -- of a named general access type, except when the expression is that | |
2825 | -- of a membership test. This ensures proper legality checking in | |
2826 | -- terms of allowed conversions (expressions that would be illegal to | |
2827 | -- convert implicitly are allowed in membership tests). | |
2828 | ||
2829 | if Ada_Version >= Ada_2012 | |
2830 | and then Ekind (Ctx_Type) = E_General_Access_Type | |
2831 | and then Ekind (Etype (N)) = E_Anonymous_Access_Type | |
2832 | and then Nkind (Parent (N)) not in N_Membership_Test | |
2833 | then | |
2834 | Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N))); | |
2835 | Analyze_And_Resolve (N, Ctx_Type); | |
2836 | end if; | |
2837 | ||
996ae0b0 RK |
2838 | -- If the subexpression was replaced by a non-subexpression, then |
2839 | -- all we do is to expand it. The only legitimate case we know of | |
2840 | -- is converting procedure call statement to entry call statements, | |
2841 | -- but there may be others, so we are making this test general. | |
2842 | ||
2843 | if Nkind (N) not in N_Subexpr then | |
2844 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2845 | Expand (N); | |
2846 | return; | |
2847 | end if; | |
2848 | ||
1e194575 AC |
2849 | -- AI05-144-2: Check dangerous order dependence within an expression |
2850 | -- that is not a subexpression. Exclude RHS of an assignment, because | |
2851 | -- both sides may have side-effects and the check must be performed | |
2852 | -- over the statement. | |
2853 | ||
2854 | if Nkind (Parent (N)) not in N_Subexpr | |
2855 | and then Nkind (Parent (N)) /= N_Assignment_Statement | |
2856 | and then Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
2857 | then | |
2858 | Check_Order_Dependence; | |
2859 | end if; | |
2860 | ||
996ae0b0 RK |
2861 | -- The expression is definitely NOT overloaded at this point, so |
2862 | -- we reset the Is_Overloaded flag to avoid any confusion when | |
2863 | -- reanalyzing the node. | |
2864 | ||
2865 | Set_Is_Overloaded (N, False); | |
2866 | ||
2867 | -- Freeze expression type, entity if it is a name, and designated | |
fbf5a39b | 2868 | -- type if it is an allocator (RM 13.14(10,11,13)). |
996ae0b0 | 2869 | |
5cc9353d RD |
2870 | -- Now that the resolution of the type of the node is complete, and |
2871 | -- we did not detect an error, we can expand this node. We skip the | |
2872 | -- expand call if we are in a default expression, see section | |
2873 | -- "Handling of Default Expressions" in Sem spec. | |
996ae0b0 RK |
2874 | |
2875 | Debug_A_Exit ("resolving ", N, " (done)"); | |
2876 | ||
2877 | -- We unconditionally freeze the expression, even if we are in | |
5cc9353d RD |
2878 | -- default expression mode (the Freeze_Expression routine tests this |
2879 | -- flag and only freezes static types if it is set). | |
996ae0b0 | 2880 | |
95160516 | 2881 | -- Ada 2012 (AI05-177): Expression functions do not freeze. Only |
08f8a983 AC |
2882 | -- their use (in an expanded call) freezes. |
2883 | ||
804670f1 AC |
2884 | if Ekind (Proper_Current_Scope) /= E_Function |
2885 | or else Nkind (Original_Node (Unit_Declaration_Node | |
2886 | (Proper_Current_Scope))) /= N_Expression_Function | |
08f8a983 AC |
2887 | then |
2888 | Freeze_Expression (N); | |
2889 | end if; | |
996ae0b0 RK |
2890 | |
2891 | -- Now we can do the expansion | |
2892 | ||
2893 | Expand (N); | |
2894 | end if; | |
996ae0b0 RK |
2895 | end Resolve; |
2896 | ||
fbf5a39b AC |
2897 | ------------- |
2898 | -- Resolve -- | |
2899 | ------------- | |
2900 | ||
996ae0b0 RK |
2901 | -- Version with check(s) suppressed |
2902 | ||
2903 | procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is | |
2904 | begin | |
2905 | if Suppress = All_Checks then | |
2906 | declare | |
fbf5a39b | 2907 | Svg : constant Suppress_Array := Scope_Suppress; |
996ae0b0 RK |
2908 | begin |
2909 | Scope_Suppress := (others => True); | |
2910 | Resolve (N, Typ); | |
2911 | Scope_Suppress := Svg; | |
2912 | end; | |
2913 | ||
2914 | else | |
2915 | declare | |
fbf5a39b | 2916 | Svg : constant Boolean := Scope_Suppress (Suppress); |
996ae0b0 | 2917 | begin |
fbf5a39b | 2918 | Scope_Suppress (Suppress) := True; |
996ae0b0 | 2919 | Resolve (N, Typ); |
fbf5a39b | 2920 | Scope_Suppress (Suppress) := Svg; |
996ae0b0 RK |
2921 | end; |
2922 | end if; | |
2923 | end Resolve; | |
2924 | ||
fbf5a39b AC |
2925 | ------------- |
2926 | -- Resolve -- | |
2927 | ------------- | |
2928 | ||
2929 | -- Version with implicit type | |
2930 | ||
2931 | procedure Resolve (N : Node_Id) is | |
2932 | begin | |
2933 | Resolve (N, Etype (N)); | |
2934 | end Resolve; | |
2935 | ||
996ae0b0 RK |
2936 | --------------------- |
2937 | -- Resolve_Actuals -- | |
2938 | --------------------- | |
2939 | ||
2940 | procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is | |
2941 | Loc : constant Source_Ptr := Sloc (N); | |
2942 | A : Node_Id; | |
2943 | F : Entity_Id; | |
2944 | A_Typ : Entity_Id; | |
2945 | F_Typ : Entity_Id; | |
2946 | Prev : Node_Id := Empty; | |
67ce0d7e | 2947 | Orig_A : Node_Id; |
996ae0b0 | 2948 | |
45fc7ddb HK |
2949 | procedure Check_Argument_Order; |
2950 | -- Performs a check for the case where the actuals are all simple | |
2951 | -- identifiers that correspond to the formal names, but in the wrong | |
2952 | -- order, which is considered suspicious and cause for a warning. | |
2953 | ||
b7d1f17f HK |
2954 | procedure Check_Prefixed_Call; |
2955 | -- If the original node is an overloaded call in prefix notation, | |
2956 | -- insert an 'Access or a dereference as needed over the first actual. | |
2957 | -- Try_Object_Operation has already verified that there is a valid | |
2958 | -- interpretation, but the form of the actual can only be determined | |
2959 | -- once the primitive operation is identified. | |
2960 | ||
996ae0b0 RK |
2961 | procedure Insert_Default; |
2962 | -- If the actual is missing in a call, insert in the actuals list | |
2963 | -- an instance of the default expression. The insertion is always | |
2964 | -- a named association. | |
2965 | ||
fbf5a39b AC |
2966 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean; |
2967 | -- Check whether T1 and T2, or their full views, are derived from a | |
2968 | -- common type. Used to enforce the restrictions on array conversions | |
2969 | -- of AI95-00246. | |
2970 | ||
a7a3cf5c AC |
2971 | function Static_Concatenation (N : Node_Id) return Boolean; |
2972 | -- Predicate to determine whether an actual that is a concatenation | |
2973 | -- will be evaluated statically and does not need a transient scope. | |
2974 | -- This must be determined before the actual is resolved and expanded | |
2975 | -- because if needed the transient scope must be introduced earlier. | |
2976 | ||
45fc7ddb HK |
2977 | -------------------------- |
2978 | -- Check_Argument_Order -- | |
2979 | -------------------------- | |
2980 | ||
2981 | procedure Check_Argument_Order is | |
2982 | begin | |
2983 | -- Nothing to do if no parameters, or original node is neither a | |
2984 | -- function call nor a procedure call statement (happens in the | |
2985 | -- operator-transformed-to-function call case), or the call does | |
2986 | -- not come from source, or this warning is off. | |
2987 | ||
2988 | if not Warn_On_Parameter_Order | |
19fb051c | 2989 | or else No (Parameter_Associations (N)) |
d3b00ce3 | 2990 | or else Nkind (Original_Node (N)) not in N_Subprogram_Call |
19fb051c | 2991 | or else not Comes_From_Source (N) |
45fc7ddb HK |
2992 | then |
2993 | return; | |
2994 | end if; | |
2995 | ||
2996 | declare | |
2997 | Nargs : constant Nat := List_Length (Parameter_Associations (N)); | |
2998 | ||
2999 | begin | |
3000 | -- Nothing to do if only one parameter | |
3001 | ||
3002 | if Nargs < 2 then | |
3003 | return; | |
3004 | end if; | |
3005 | ||
3006 | -- Here if at least two arguments | |
3007 | ||
3008 | declare | |
3009 | Actuals : array (1 .. Nargs) of Node_Id; | |
3010 | Actual : Node_Id; | |
3011 | Formal : Node_Id; | |
3012 | ||
3013 | Wrong_Order : Boolean := False; | |
3014 | -- Set True if an out of order case is found | |
3015 | ||
3016 | begin | |
3017 | -- Collect identifier names of actuals, fail if any actual is | |
3018 | -- not a simple identifier, and record max length of name. | |
3019 | ||
3020 | Actual := First (Parameter_Associations (N)); | |
3021 | for J in Actuals'Range loop | |
3022 | if Nkind (Actual) /= N_Identifier then | |
3023 | return; | |
3024 | else | |
3025 | Actuals (J) := Actual; | |
3026 | Next (Actual); | |
3027 | end if; | |
3028 | end loop; | |
3029 | ||
3030 | -- If we got this far, all actuals are identifiers and the list | |
3031 | -- of their names is stored in the Actuals array. | |
3032 | ||
3033 | Formal := First_Formal (Nam); | |
3034 | for J in Actuals'Range loop | |
3035 | ||
3036 | -- If we ran out of formals, that's odd, probably an error | |
3037 | -- which will be detected elsewhere, but abandon the search. | |
3038 | ||
3039 | if No (Formal) then | |
3040 | return; | |
3041 | end if; | |
3042 | ||
3043 | -- If name matches and is in order OK | |
3044 | ||
3045 | if Chars (Formal) = Chars (Actuals (J)) then | |
3046 | null; | |
3047 | ||
3048 | else | |
3049 | -- If no match, see if it is elsewhere in list and if so | |
3050 | -- flag potential wrong order if type is compatible. | |
3051 | ||
3052 | for K in Actuals'Range loop | |
3053 | if Chars (Formal) = Chars (Actuals (K)) | |
3054 | and then | |
3055 | Has_Compatible_Type (Actuals (K), Etype (Formal)) | |
3056 | then | |
3057 | Wrong_Order := True; | |
3058 | goto Continue; | |
3059 | end if; | |
3060 | end loop; | |
3061 | ||
3062 | -- No match | |
3063 | ||
3064 | return; | |
3065 | end if; | |
3066 | ||
3067 | <<Continue>> Next_Formal (Formal); | |
3068 | end loop; | |
3069 | ||
3070 | -- If Formals left over, also probably an error, skip warning | |
3071 | ||
3072 | if Present (Formal) then | |
3073 | return; | |
3074 | end if; | |
3075 | ||
3076 | -- Here we give the warning if something was out of order | |
3077 | ||
3078 | if Wrong_Order then | |
3079 | Error_Msg_N | |
3080 | ("actuals for this call may be in wrong order?", N); | |
3081 | end if; | |
3082 | end; | |
3083 | end; | |
3084 | end Check_Argument_Order; | |
3085 | ||
b7d1f17f HK |
3086 | ------------------------- |
3087 | -- Check_Prefixed_Call -- | |
3088 | ------------------------- | |
3089 | ||
3090 | procedure Check_Prefixed_Call is | |
3091 | Act : constant Node_Id := First_Actual (N); | |
3092 | A_Type : constant Entity_Id := Etype (Act); | |
3093 | F_Type : constant Entity_Id := Etype (First_Formal (Nam)); | |
3094 | Orig : constant Node_Id := Original_Node (N); | |
3095 | New_A : Node_Id; | |
3096 | ||
3097 | begin | |
3098 | -- Check whether the call is a prefixed call, with or without | |
3099 | -- additional actuals. | |
3100 | ||
3101 | if Nkind (Orig) = N_Selected_Component | |
3102 | or else | |
3103 | (Nkind (Orig) = N_Indexed_Component | |
3104 | and then Nkind (Prefix (Orig)) = N_Selected_Component | |
3105 | and then Is_Entity_Name (Prefix (Prefix (Orig))) | |
3106 | and then Is_Entity_Name (Act) | |
3107 | and then Chars (Act) = Chars (Prefix (Prefix (Orig)))) | |
3108 | then | |
3109 | if Is_Access_Type (A_Type) | |
3110 | and then not Is_Access_Type (F_Type) | |
3111 | then | |
3112 | -- Introduce dereference on object in prefix | |
3113 | ||
3114 | New_A := | |
3115 | Make_Explicit_Dereference (Sloc (Act), | |
3116 | Prefix => Relocate_Node (Act)); | |
3117 | Rewrite (Act, New_A); | |
3118 | Analyze (Act); | |
3119 | ||
3120 | elsif Is_Access_Type (F_Type) | |
3121 | and then not Is_Access_Type (A_Type) | |
3122 | then | |
3123 | -- Introduce an implicit 'Access in prefix | |
3124 | ||
3125 | if not Is_Aliased_View (Act) then | |
ed2233dc | 3126 | Error_Msg_NE |
b7d1f17f | 3127 | ("object in prefixed call to& must be aliased" |
aa5147f0 | 3128 | & " (RM-2005 4.3.1 (13))", |
b7d1f17f HK |
3129 | Prefix (Act), Nam); |
3130 | end if; | |
3131 | ||
3132 | Rewrite (Act, | |
3133 | Make_Attribute_Reference (Loc, | |
3134 | Attribute_Name => Name_Access, | |
3135 | Prefix => Relocate_Node (Act))); | |
3136 | end if; | |
3137 | ||
3138 | Analyze (Act); | |
3139 | end if; | |
3140 | end Check_Prefixed_Call; | |
3141 | ||
996ae0b0 RK |
3142 | -------------------- |
3143 | -- Insert_Default -- | |
3144 | -------------------- | |
3145 | ||
3146 | procedure Insert_Default is | |
3147 | Actval : Node_Id; | |
3148 | Assoc : Node_Id; | |
3149 | ||
3150 | begin | |
fbf5a39b | 3151 | -- Missing argument in call, nothing to insert |
996ae0b0 | 3152 | |
fbf5a39b AC |
3153 | if No (Default_Value (F)) then |
3154 | return; | |
3155 | ||
3156 | else | |
3157 | -- Note that we do a full New_Copy_Tree, so that any associated | |
3158 | -- Itypes are properly copied. This may not be needed any more, | |
3159 | -- but it does no harm as a safety measure! Defaults of a generic | |
3160 | -- formal may be out of bounds of the corresponding actual (see | |
3161 | -- cc1311b) and an additional check may be required. | |
996ae0b0 | 3162 | |
b7d1f17f HK |
3163 | Actval := |
3164 | New_Copy_Tree | |
3165 | (Default_Value (F), | |
3166 | New_Scope => Current_Scope, | |
3167 | New_Sloc => Loc); | |
996ae0b0 RK |
3168 | |
3169 | if Is_Concurrent_Type (Scope (Nam)) | |
3170 | and then Has_Discriminants (Scope (Nam)) | |
3171 | then | |
3172 | Replace_Actual_Discriminants (N, Actval); | |
3173 | end if; | |
3174 | ||
3175 | if Is_Overloadable (Nam) | |
3176 | and then Present (Alias (Nam)) | |
3177 | then | |
3178 | if Base_Type (Etype (F)) /= Base_Type (Etype (Actval)) | |
3179 | and then not Is_Tagged_Type (Etype (F)) | |
3180 | then | |
3181 | -- If default is a real literal, do not introduce a | |
3182 | -- conversion whose effect may depend on the run-time | |
3183 | -- size of universal real. | |
3184 | ||
3185 | if Nkind (Actval) = N_Real_Literal then | |
3186 | Set_Etype (Actval, Base_Type (Etype (F))); | |
3187 | else | |
3188 | Actval := Unchecked_Convert_To (Etype (F), Actval); | |
3189 | end if; | |
3190 | end if; | |
3191 | ||
3192 | if Is_Scalar_Type (Etype (F)) then | |
3193 | Enable_Range_Check (Actval); | |
3194 | end if; | |
3195 | ||
996ae0b0 RK |
3196 | Set_Parent (Actval, N); |
3197 | ||
3198 | -- Resolve aggregates with their base type, to avoid scope | |
f3d57416 | 3199 | -- anomalies: the subtype was first built in the subprogram |
996ae0b0 RK |
3200 | -- declaration, and the current call may be nested. |
3201 | ||
76b84bf0 AC |
3202 | if Nkind (Actval) = N_Aggregate then |
3203 | Analyze_And_Resolve (Actval, Etype (F)); | |
996ae0b0 RK |
3204 | else |
3205 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
3206 | end if; | |
fbf5a39b AC |
3207 | |
3208 | else | |
3209 | Set_Parent (Actval, N); | |
3210 | ||
a77842bd | 3211 | -- See note above concerning aggregates |
fbf5a39b AC |
3212 | |
3213 | if Nkind (Actval) = N_Aggregate | |
3214 | and then Has_Discriminants (Etype (Actval)) | |
3215 | then | |
3216 | Analyze_And_Resolve (Actval, Base_Type (Etype (Actval))); | |
3217 | ||
5cc9353d RD |
3218 | -- Resolve entities with their own type, which may differ from |
3219 | -- the type of a reference in a generic context (the view | |
3220 | -- swapping mechanism did not anticipate the re-analysis of | |
3221 | -- default values in calls). | |
fbf5a39b AC |
3222 | |
3223 | elsif Is_Entity_Name (Actval) then | |
3224 | Analyze_And_Resolve (Actval, Etype (Entity (Actval))); | |
3225 | ||
3226 | else | |
3227 | Analyze_And_Resolve (Actval, Etype (Actval)); | |
3228 | end if; | |
996ae0b0 RK |
3229 | end if; |
3230 | ||
5cc9353d RD |
3231 | -- If default is a tag indeterminate function call, propagate tag |
3232 | -- to obtain proper dispatching. | |
996ae0b0 RK |
3233 | |
3234 | if Is_Controlling_Formal (F) | |
3235 | and then Nkind (Default_Value (F)) = N_Function_Call | |
3236 | then | |
3237 | Set_Is_Controlling_Actual (Actval); | |
3238 | end if; | |
3239 | ||
996ae0b0 RK |
3240 | end if; |
3241 | ||
3242 | -- If the default expression raises constraint error, then just | |
5cc9353d RD |
3243 | -- silently replace it with an N_Raise_Constraint_Error node, since |
3244 | -- we already gave the warning on the subprogram spec. If node is | |
3245 | -- already a Raise_Constraint_Error leave as is, to prevent loops in | |
3246 | -- the warnings removal machinery. | |
996ae0b0 | 3247 | |
2604ec03 AC |
3248 | if Raises_Constraint_Error (Actval) |
3249 | and then Nkind (Actval) /= N_Raise_Constraint_Error | |
3250 | then | |
996ae0b0 | 3251 | Rewrite (Actval, |
07fc65c4 GB |
3252 | Make_Raise_Constraint_Error (Loc, |
3253 | Reason => CE_Range_Check_Failed)); | |
996ae0b0 RK |
3254 | Set_Raises_Constraint_Error (Actval); |
3255 | Set_Etype (Actval, Etype (F)); | |
3256 | end if; | |
3257 | ||
3258 | Assoc := | |
3259 | Make_Parameter_Association (Loc, | |
3260 | Explicit_Actual_Parameter => Actval, | |
3261 | Selector_Name => Make_Identifier (Loc, Chars (F))); | |
3262 | ||
3263 | -- Case of insertion is first named actual | |
3264 | ||
3265 | if No (Prev) or else | |
3266 | Nkind (Parent (Prev)) /= N_Parameter_Association | |
3267 | then | |
3268 | Set_Next_Named_Actual (Assoc, First_Named_Actual (N)); | |
3269 | Set_First_Named_Actual (N, Actval); | |
3270 | ||
3271 | if No (Prev) then | |
c8ef728f | 3272 | if No (Parameter_Associations (N)) then |
996ae0b0 RK |
3273 | Set_Parameter_Associations (N, New_List (Assoc)); |
3274 | else | |
3275 | Append (Assoc, Parameter_Associations (N)); | |
3276 | end if; | |
3277 | ||
3278 | else | |
3279 | Insert_After (Prev, Assoc); | |
3280 | end if; | |
3281 | ||
3282 | -- Case of insertion is not first named actual | |
3283 | ||
3284 | else | |
3285 | Set_Next_Named_Actual | |
3286 | (Assoc, Next_Named_Actual (Parent (Prev))); | |
3287 | Set_Next_Named_Actual (Parent (Prev), Actval); | |
3288 | Append (Assoc, Parameter_Associations (N)); | |
3289 | end if; | |
3290 | ||
3291 | Mark_Rewrite_Insertion (Assoc); | |
3292 | Mark_Rewrite_Insertion (Actval); | |
3293 | ||
3294 | Prev := Actval; | |
3295 | end Insert_Default; | |
3296 | ||
fbf5a39b AC |
3297 | ------------------- |
3298 | -- Same_Ancestor -- | |
3299 | ------------------- | |
3300 | ||
3301 | function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is | |
3302 | FT1 : Entity_Id := T1; | |
3303 | FT2 : Entity_Id := T2; | |
3304 | ||
3305 | begin | |
3306 | if Is_Private_Type (T1) | |
3307 | and then Present (Full_View (T1)) | |
3308 | then | |
3309 | FT1 := Full_View (T1); | |
3310 | end if; | |
3311 | ||
3312 | if Is_Private_Type (T2) | |
3313 | and then Present (Full_View (T2)) | |
3314 | then | |
3315 | FT2 := Full_View (T2); | |
3316 | end if; | |
3317 | ||
3318 | return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2)); | |
3319 | end Same_Ancestor; | |
3320 | ||
a7a3cf5c AC |
3321 | -------------------------- |
3322 | -- Static_Concatenation -- | |
3323 | -------------------------- | |
3324 | ||
3325 | function Static_Concatenation (N : Node_Id) return Boolean is | |
3326 | begin | |
c72a85f2 TQ |
3327 | case Nkind (N) is |
3328 | when N_String_Literal => | |
3329 | return True; | |
a7a3cf5c | 3330 | |
d81b4bfe TQ |
3331 | when N_Op_Concat => |
3332 | ||
5cc9353d RD |
3333 | -- Concatenation is static when both operands are static and |
3334 | -- the concatenation operator is a predefined one. | |
4342eda9 TQ |
3335 | |
3336 | return Scope (Entity (N)) = Standard_Standard | |
3337 | and then | |
3338 | Static_Concatenation (Left_Opnd (N)) | |
c72a85f2 TQ |
3339 | and then |
3340 | Static_Concatenation (Right_Opnd (N)); | |
3341 | ||
3342 | when others => | |
3343 | if Is_Entity_Name (N) then | |
3344 | declare | |
3345 | Ent : constant Entity_Id := Entity (N); | |
3346 | begin | |
3347 | return Ekind (Ent) = E_Constant | |
3348 | and then Present (Constant_Value (Ent)) | |
d81b4bfe TQ |
3349 | and then |
3350 | Is_Static_Expression (Constant_Value (Ent)); | |
c72a85f2 | 3351 | end; |
a7a3cf5c | 3352 | |
a7a3cf5c AC |
3353 | else |
3354 | return False; | |
3355 | end if; | |
c72a85f2 | 3356 | end case; |
a7a3cf5c AC |
3357 | end Static_Concatenation; |
3358 | ||
996ae0b0 RK |
3359 | -- Start of processing for Resolve_Actuals |
3360 | ||
3361 | begin | |
45fc7ddb HK |
3362 | Check_Argument_Order; |
3363 | ||
b7d1f17f HK |
3364 | if Present (First_Actual (N)) then |
3365 | Check_Prefixed_Call; | |
3366 | end if; | |
3367 | ||
996ae0b0 RK |
3368 | A := First_Actual (N); |
3369 | F := First_Formal (Nam); | |
996ae0b0 | 3370 | while Present (F) loop |
fbf5a39b AC |
3371 | if No (A) and then Needs_No_Actuals (Nam) then |
3372 | null; | |
996ae0b0 | 3373 | |
d81b4bfe TQ |
3374 | -- If we have an error in any actual or formal, indicated by a type |
3375 | -- of Any_Type, then abandon resolution attempt, and set result type | |
3376 | -- to Any_Type. | |
07fc65c4 | 3377 | |
fbf5a39b AC |
3378 | elsif (Present (A) and then Etype (A) = Any_Type) |
3379 | or else Etype (F) = Any_Type | |
07fc65c4 GB |
3380 | then |
3381 | Set_Etype (N, Any_Type); | |
3382 | return; | |
3383 | end if; | |
3384 | ||
e65f50ec ES |
3385 | -- Case where actual is present |
3386 | ||
45fc7ddb | 3387 | -- If the actual is an entity, generate a reference to it now. We |
36fcf362 RD |
3388 | -- do this before the actual is resolved, because a formal of some |
3389 | -- protected subprogram, or a task discriminant, will be rewritten | |
5cc9353d | 3390 | -- during expansion, and the source entity reference may be lost. |
36fcf362 RD |
3391 | |
3392 | if Present (A) | |
3393 | and then Is_Entity_Name (A) | |
3394 | and then Comes_From_Source (N) | |
3395 | then | |
3396 | Orig_A := Entity (A); | |
3397 | ||
3398 | if Present (Orig_A) then | |
3399 | if Is_Formal (Orig_A) | |
3400 | and then Ekind (F) /= E_In_Parameter | |
3401 | then | |
3402 | Generate_Reference (Orig_A, A, 'm'); | |
19fb051c | 3403 | |
36fcf362 RD |
3404 | elsif not Is_Overloaded (A) then |
3405 | Generate_Reference (Orig_A, A); | |
3406 | end if; | |
3407 | end if; | |
3408 | end if; | |
3409 | ||
996ae0b0 RK |
3410 | if Present (A) |
3411 | and then (Nkind (Parent (A)) /= N_Parameter_Association | |
19fb051c | 3412 | or else Chars (Selector_Name (Parent (A))) = Chars (F)) |
996ae0b0 | 3413 | then |
45fc7ddb HK |
3414 | -- If style checking mode on, check match of formal name |
3415 | ||
3416 | if Style_Check then | |
3417 | if Nkind (Parent (A)) = N_Parameter_Association then | |
3418 | Check_Identifier (Selector_Name (Parent (A)), F); | |
3419 | end if; | |
3420 | end if; | |
3421 | ||
996ae0b0 RK |
3422 | -- If the formal is Out or In_Out, do not resolve and expand the |
3423 | -- conversion, because it is subsequently expanded into explicit | |
3424 | -- temporaries and assignments. However, the object of the | |
ea985d95 RD |
3425 | -- conversion can be resolved. An exception is the case of tagged |
3426 | -- type conversion with a class-wide actual. In that case we want | |
3427 | -- the tag check to occur and no temporary will be needed (no | |
3428 | -- representation change can occur) and the parameter is passed by | |
3429 | -- reference, so we go ahead and resolve the type conversion. | |
c8ef728f | 3430 | -- Another exception is the case of reference to component or |
ea985d95 RD |
3431 | -- subcomponent of a bit-packed array, in which case we want to |
3432 | -- defer expansion to the point the in and out assignments are | |
3433 | -- performed. | |
996ae0b0 RK |
3434 | |
3435 | if Ekind (F) /= E_In_Parameter | |
3436 | and then Nkind (A) = N_Type_Conversion | |
3437 | and then not Is_Class_Wide_Type (Etype (Expression (A))) | |
3438 | then | |
07fc65c4 GB |
3439 | if Ekind (F) = E_In_Out_Parameter |
3440 | and then Is_Array_Type (Etype (F)) | |
07fc65c4 | 3441 | then |
038140ed AC |
3442 | -- In a view conversion, the conversion must be legal in |
3443 | -- both directions, and thus both component types must be | |
3444 | -- aliased, or neither (4.6 (8)). | |
758c442c | 3445 | |
038140ed AC |
3446 | -- The extra rule in 4.6 (24.9.2) seems unduly restrictive: |
3447 | -- the privacy requirement should not apply to generic | |
3448 | -- types, and should be checked in an instance. ARG query | |
3449 | -- is in order ??? | |
45fc7ddb | 3450 | |
038140ed AC |
3451 | if Has_Aliased_Components (Etype (Expression (A))) /= |
3452 | Has_Aliased_Components (Etype (F)) | |
3453 | then | |
45fc7ddb HK |
3454 | Error_Msg_N |
3455 | ("both component types in a view conversion must be" | |
3456 | & " aliased, or neither", A); | |
3457 | ||
038140ed AC |
3458 | -- Comment here??? what set of cases??? |
3459 | ||
45fc7ddb HK |
3460 | elsif |
3461 | not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3462 | then | |
038140ed AC |
3463 | -- Check view conv between unrelated by ref array types |
3464 | ||
45fc7ddb HK |
3465 | if Is_By_Reference_Type (Etype (F)) |
3466 | or else Is_By_Reference_Type (Etype (Expression (A))) | |
758c442c GD |
3467 | then |
3468 | Error_Msg_N | |
45fc7ddb HK |
3469 | ("view conversion between unrelated by reference " & |
3470 | "array types not allowed (\'A'I-00246)", A); | |
038140ed AC |
3471 | |
3472 | -- In Ada 2005 mode, check view conversion component | |
3473 | -- type cannot be private, tagged, or volatile. Note | |
3474 | -- that we only apply this to source conversions. The | |
3475 | -- generated code can contain conversions which are | |
3476 | -- not subject to this test, and we cannot extract the | |
3477 | -- component type in such cases since it is not present. | |
3478 | ||
3479 | elsif Comes_From_Source (A) | |
3480 | and then Ada_Version >= Ada_2005 | |
3481 | then | |
45fc7ddb HK |
3482 | declare |
3483 | Comp_Type : constant Entity_Id := | |
3484 | Component_Type | |
3485 | (Etype (Expression (A))); | |
3486 | begin | |
038140ed AC |
3487 | if (Is_Private_Type (Comp_Type) |
3488 | and then not Is_Generic_Type (Comp_Type)) | |
3489 | or else Is_Tagged_Type (Comp_Type) | |
3490 | or else Is_Volatile (Comp_Type) | |
45fc7ddb HK |
3491 | then |
3492 | Error_Msg_N | |
3493 | ("component type of a view conversion cannot" | |
3494 | & " be private, tagged, or volatile" | |
3495 | & " (RM 4.6 (24))", | |
3496 | Expression (A)); | |
3497 | end if; | |
3498 | end; | |
758c442c | 3499 | end if; |
fbf5a39b | 3500 | end if; |
07fc65c4 GB |
3501 | end if; |
3502 | ||
038140ed AC |
3503 | -- Resolve expression if conversion is all OK |
3504 | ||
16397eff | 3505 | if (Conversion_OK (A) |
038140ed | 3506 | or else Valid_Conversion (A, Etype (A), Expression (A))) |
16397eff | 3507 | and then not Is_Ref_To_Bit_Packed_Array (Expression (A)) |
996ae0b0 | 3508 | then |
fbf5a39b | 3509 | Resolve (Expression (A)); |
996ae0b0 RK |
3510 | end if; |
3511 | ||
b7d1f17f HK |
3512 | -- If the actual is a function call that returns a limited |
3513 | -- unconstrained object that needs finalization, create a | |
3514 | -- transient scope for it, so that it can receive the proper | |
3515 | -- finalization list. | |
3516 | ||
3517 | elsif Nkind (A) = N_Function_Call | |
3518 | and then Is_Limited_Record (Etype (F)) | |
3519 | and then not Is_Constrained (Etype (F)) | |
da94696d | 3520 | and then Full_Expander_Active |
19fb051c | 3521 | and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F))) |
b7d1f17f HK |
3522 | then |
3523 | Establish_Transient_Scope (A, False); | |
24a120ac | 3524 | Resolve (A, Etype (F)); |
b7d1f17f | 3525 | |
a52fefe6 AC |
3526 | -- A small optimization: if one of the actuals is a concatenation |
3527 | -- create a block around a procedure call to recover stack space. | |
3528 | -- This alleviates stack usage when several procedure calls in | |
76e776e5 AC |
3529 | -- the same statement list use concatenation. We do not perform |
3530 | -- this wrapping for code statements, where the argument is a | |
3531 | -- static string, and we want to preserve warnings involving | |
3532 | -- sequences of such statements. | |
a52fefe6 AC |
3533 | |
3534 | elsif Nkind (A) = N_Op_Concat | |
3535 | and then Nkind (N) = N_Procedure_Call_Statement | |
da94696d | 3536 | and then Full_Expander_Active |
76e776e5 AC |
3537 | and then |
3538 | not (Is_Intrinsic_Subprogram (Nam) | |
3539 | and then Chars (Nam) = Name_Asm) | |
a7a3cf5c | 3540 | and then not Static_Concatenation (A) |
a52fefe6 AC |
3541 | then |
3542 | Establish_Transient_Scope (A, False); | |
3543 | Resolve (A, Etype (F)); | |
3544 | ||
996ae0b0 | 3545 | else |
fbf5a39b AC |
3546 | if Nkind (A) = N_Type_Conversion |
3547 | and then Is_Array_Type (Etype (F)) | |
3548 | and then not Same_Ancestor (Etype (F), Etype (Expression (A))) | |
3549 | and then | |
3550 | (Is_Limited_Type (Etype (F)) | |
3551 | or else Is_Limited_Type (Etype (Expression (A)))) | |
3552 | then | |
3553 | Error_Msg_N | |
758c442c GD |
3554 | ("conversion between unrelated limited array types " & |
3555 | "not allowed (\A\I-00246)", A); | |
fbf5a39b | 3556 | |
758c442c GD |
3557 | if Is_Limited_Type (Etype (F)) then |
3558 | Explain_Limited_Type (Etype (F), A); | |
3559 | end if; | |
fbf5a39b | 3560 | |
758c442c GD |
3561 | if Is_Limited_Type (Etype (Expression (A))) then |
3562 | Explain_Limited_Type (Etype (Expression (A)), A); | |
3563 | end if; | |
fbf5a39b AC |
3564 | end if; |
3565 | ||
c8ef728f ES |
3566 | -- (Ada 2005: AI-251): If the actual is an allocator whose |
3567 | -- directly designated type is a class-wide interface, we build | |
3568 | -- an anonymous access type to use it as the type of the | |
3569 | -- allocator. Later, when the subprogram call is expanded, if | |
3570 | -- the interface has a secondary dispatch table the expander | |
3571 | -- will add a type conversion to force the correct displacement | |
3572 | -- of the pointer. | |
3573 | ||
3574 | if Nkind (A) = N_Allocator then | |
3575 | declare | |
3576 | DDT : constant Entity_Id := | |
3577 | Directly_Designated_Type (Base_Type (Etype (F))); | |
45fc7ddb | 3578 | |
c8ef728f | 3579 | New_Itype : Entity_Id; |
45fc7ddb | 3580 | |
c8ef728f ES |
3581 | begin |
3582 | if Is_Class_Wide_Type (DDT) | |
3583 | and then Is_Interface (DDT) | |
3584 | then | |
3585 | New_Itype := Create_Itype (E_Anonymous_Access_Type, A); | |
45fc7ddb | 3586 | Set_Etype (New_Itype, Etype (A)); |
c8ef728f ES |
3587 | Set_Directly_Designated_Type (New_Itype, |
3588 | Directly_Designated_Type (Etype (A))); | |
3589 | Set_Etype (A, New_Itype); | |
3590 | end if; | |
0669bebe GB |
3591 | |
3592 | -- Ada 2005, AI-162:If the actual is an allocator, the | |
3593 | -- innermost enclosing statement is the master of the | |
b7d1f17f HK |
3594 | -- created object. This needs to be done with expansion |
3595 | -- enabled only, otherwise the transient scope will not | |
3596 | -- be removed in the expansion of the wrapped construct. | |
0669bebe | 3597 | |
45fc7ddb | 3598 | if (Is_Controlled (DDT) or else Has_Task (DDT)) |
da94696d | 3599 | and then Full_Expander_Active |
0669bebe GB |
3600 | then |
3601 | Establish_Transient_Scope (A, False); | |
3602 | end if; | |
c8ef728f ES |
3603 | end; |
3604 | end if; | |
3605 | ||
b7d1f17f HK |
3606 | -- (Ada 2005): The call may be to a primitive operation of |
3607 | -- a tagged synchronized type, declared outside of the type. | |
3608 | -- In this case the controlling actual must be converted to | |
3609 | -- its corresponding record type, which is the formal type. | |
45fc7ddb HK |
3610 | -- The actual may be a subtype, either because of a constraint |
3611 | -- or because it is a generic actual, so use base type to | |
3612 | -- locate concurrent type. | |
b7d1f17f | 3613 | |
15e4986c JM |
3614 | F_Typ := Base_Type (Etype (F)); |
3615 | ||
cb7fa356 AC |
3616 | if Is_Tagged_Type (F_Typ) |
3617 | and then (Is_Concurrent_Type (F_Typ) | |
3618 | or else Is_Concurrent_Record_Type (F_Typ)) | |
3619 | then | |
3620 | -- If the actual is overloaded, look for an interpretation | |
3621 | -- that has a synchronized type. | |
3622 | ||
3623 | if not Is_Overloaded (A) then | |
3624 | A_Typ := Base_Type (Etype (A)); | |
15e4986c | 3625 | |
15e4986c | 3626 | else |
cb7fa356 AC |
3627 | declare |
3628 | Index : Interp_Index; | |
3629 | It : Interp; | |
218e6dee | 3630 | |
cb7fa356 AC |
3631 | begin |
3632 | Get_First_Interp (A, Index, It); | |
3633 | while Present (It.Typ) loop | |
3634 | if Is_Concurrent_Type (It.Typ) | |
3635 | or else Is_Concurrent_Record_Type (It.Typ) | |
3636 | then | |
3637 | A_Typ := Base_Type (It.Typ); | |
3638 | exit; | |
3639 | end if; | |
3640 | ||
3641 | Get_Next_Interp (Index, It); | |
3642 | end loop; | |
3643 | end; | |
15e4986c | 3644 | end if; |
b7d1f17f | 3645 | |
cb7fa356 AC |
3646 | declare |
3647 | Full_A_Typ : Entity_Id; | |
15e4986c | 3648 | |
cb7fa356 AC |
3649 | begin |
3650 | if Present (Full_View (A_Typ)) then | |
3651 | Full_A_Typ := Base_Type (Full_View (A_Typ)); | |
3652 | else | |
3653 | Full_A_Typ := A_Typ; | |
3654 | end if; | |
3655 | ||
3656 | -- Tagged synchronized type (case 1): the actual is a | |
3657 | -- concurrent type. | |
3658 | ||
3659 | if Is_Concurrent_Type (A_Typ) | |
3660 | and then Corresponding_Record_Type (A_Typ) = F_Typ | |
3661 | then | |
3662 | Rewrite (A, | |
3663 | Unchecked_Convert_To | |
3664 | (Corresponding_Record_Type (A_Typ), A)); | |
3665 | Resolve (A, Etype (F)); | |
15e4986c | 3666 | |
cb7fa356 AC |
3667 | -- Tagged synchronized type (case 2): the formal is a |
3668 | -- concurrent type. | |
15e4986c | 3669 | |
cb7fa356 AC |
3670 | elsif Ekind (Full_A_Typ) = E_Record_Type |
3671 | and then Present | |
15e4986c | 3672 | (Corresponding_Concurrent_Type (Full_A_Typ)) |
cb7fa356 AC |
3673 | and then Is_Concurrent_Type (F_Typ) |
3674 | and then Present (Corresponding_Record_Type (F_Typ)) | |
3675 | and then Full_A_Typ = Corresponding_Record_Type (F_Typ) | |
3676 | then | |
3677 | Resolve (A, Corresponding_Record_Type (F_Typ)); | |
15e4986c | 3678 | |
cb7fa356 | 3679 | -- Common case |
15e4986c | 3680 | |
cb7fa356 AC |
3681 | else |
3682 | Resolve (A, Etype (F)); | |
3683 | end if; | |
3684 | end; | |
3685 | else | |
3686 | ||
3687 | -- not a synchronized operation. | |
3688 | ||
3689 | Resolve (A, Etype (F)); | |
3690 | end if; | |
996ae0b0 RK |
3691 | end if; |
3692 | ||
3693 | A_Typ := Etype (A); | |
3694 | F_Typ := Etype (F); | |
3695 | ||
e24329cd | 3696 | if Comes_From_Source (Original_Node (N)) |
6320f5e1 AC |
3697 | and then Nkind_In (Original_Node (N), N_Function_Call, |
3698 | N_Procedure_Call_Statement) | |
b0186f71 | 3699 | then |
e24329cd YM |
3700 | -- In formal mode, check that actual parameters matching |
3701 | -- formals of tagged types are objects (or ancestor type | |
3702 | -- conversions of objects), not general expressions. | |
780d052e | 3703 | |
e24329cd YM |
3704 | if Is_Actual_Tagged_Parameter (A) then |
3705 | if Is_SPARK_Object_Reference (A) then | |
3706 | null; | |
3707 | ||
3708 | elsif Nkind (A) = N_Type_Conversion then | |
3709 | declare | |
3710 | Operand : constant Node_Id := Expression (A); | |
3711 | Operand_Typ : constant Entity_Id := Etype (Operand); | |
3712 | Target_Typ : constant Entity_Id := A_Typ; | |
3713 | ||
3714 | begin | |
3715 | if not Is_SPARK_Object_Reference (Operand) then | |
2ba431e5 | 3716 | Check_SPARK_Restriction |
e24329cd YM |
3717 | ("object required", Operand); |
3718 | ||
3719 | -- In formal mode, the only view conversions are those | |
3720 | -- involving ancestor conversion of an extended type. | |
3721 | ||
3722 | elsif not | |
3723 | (Is_Tagged_Type (Target_Typ) | |
780d052e RD |
3724 | and then not Is_Class_Wide_Type (Target_Typ) |
3725 | and then Is_Tagged_Type (Operand_Typ) | |
3726 | and then not Is_Class_Wide_Type (Operand_Typ) | |
3727 | and then Is_Ancestor (Target_Typ, Operand_Typ)) | |
e24329cd YM |
3728 | then |
3729 | if Ekind_In | |
3730 | (F, E_Out_Parameter, E_In_Out_Parameter) | |
3731 | then | |
2ba431e5 | 3732 | Check_SPARK_Restriction |
e24329cd YM |
3733 | ("ancestor conversion is the only permitted " |
3734 | & "view conversion", A); | |
3735 | else | |
2ba431e5 | 3736 | Check_SPARK_Restriction |
e24329cd YM |
3737 | ("ancestor conversion required", A); |
3738 | end if; | |
3739 | ||
3740 | else | |
3741 | null; | |
3742 | end if; | |
3743 | end; | |
3744 | ||
3745 | else | |
2ba431e5 | 3746 | Check_SPARK_Restriction ("object required", A); |
b0186f71 | 3747 | end if; |
e24329cd YM |
3748 | |
3749 | -- In formal mode, the only view conversions are those | |
3750 | -- involving ancestor conversion of an extended type. | |
3751 | ||
3752 | elsif Nkind (A) = N_Type_Conversion | |
3753 | and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) | |
3754 | then | |
2ba431e5 | 3755 | Check_SPARK_Restriction |
e24329cd YM |
3756 | ("ancestor conversion is the only permitted view " |
3757 | & "conversion", A); | |
3758 | end if; | |
b0186f71 AC |
3759 | end if; |
3760 | ||
1e194575 AC |
3761 | -- Save actual for subsequent check on order dependence, and |
3762 | -- indicate whether actual is modifiable. For AI05-0144-2. | |
bb481772 | 3763 | |
67bdbf1e AC |
3764 | -- If this is a call to a reference function that is the result |
3765 | -- of expansion, as in element iterator loops, this does not lead | |
3766 | -- to a dangerous order dependence: only subsequent use of the | |
3767 | -- denoted element might, in some enclosing call. | |
3768 | ||
3769 | if not Has_Implicit_Dereference (Etype (Nam)) | |
3770 | or else Comes_From_Source (N) | |
3771 | then | |
3772 | Save_Actual (A, Ekind (F) /= E_In_Parameter); | |
3773 | end if; | |
bb481772 | 3774 | |
26570b21 RD |
3775 | -- For mode IN, if actual is an entity, and the type of the formal |
3776 | -- has warnings suppressed, then we reset Never_Set_In_Source for | |
3777 | -- the calling entity. The reason for this is to catch cases like | |
3778 | -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram | |
3779 | -- uses trickery to modify an IN parameter. | |
3780 | ||
3781 | if Ekind (F) = E_In_Parameter | |
3782 | and then Is_Entity_Name (A) | |
3783 | and then Present (Entity (A)) | |
3784 | and then Ekind (Entity (A)) = E_Variable | |
3785 | and then Has_Warnings_Off (F_Typ) | |
3786 | then | |
3787 | Set_Never_Set_In_Source (Entity (A), False); | |
3788 | end if; | |
3789 | ||
fbf5a39b AC |
3790 | -- Perform error checks for IN and IN OUT parameters |
3791 | ||
3792 | if Ekind (F) /= E_Out_Parameter then | |
3793 | ||
3794 | -- Check unset reference. For scalar parameters, it is clearly | |
3795 | -- wrong to pass an uninitialized value as either an IN or | |
3796 | -- IN-OUT parameter. For composites, it is also clearly an | |
3797 | -- error to pass a completely uninitialized value as an IN | |
3798 | -- parameter, but the case of IN OUT is trickier. We prefer | |
3799 | -- not to give a warning here. For example, suppose there is | |
3800 | -- a routine that sets some component of a record to False. | |
3801 | -- It is perfectly reasonable to make this IN-OUT and allow | |
3802 | -- either initialized or uninitialized records to be passed | |
3803 | -- in this case. | |
3804 | ||
3805 | -- For partially initialized composite values, we also avoid | |
3806 | -- warnings, since it is quite likely that we are passing a | |
3807 | -- partially initialized value and only the initialized fields | |
3808 | -- will in fact be read in the subprogram. | |
3809 | ||
3810 | if Is_Scalar_Type (A_Typ) | |
3811 | or else (Ekind (F) = E_In_Parameter | |
19fb051c | 3812 | and then not Is_Partially_Initialized_Type (A_Typ)) |
996ae0b0 | 3813 | then |
fbf5a39b | 3814 | Check_Unset_Reference (A); |
996ae0b0 | 3815 | end if; |
996ae0b0 | 3816 | |
758c442c GD |
3817 | -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT |
3818 | -- actual to a nested call, since this is case of reading an | |
3819 | -- out parameter, which is not allowed. | |
996ae0b0 | 3820 | |
0ab80019 | 3821 | if Ada_Version = Ada_83 |
996ae0b0 RK |
3822 | and then Is_Entity_Name (A) |
3823 | and then Ekind (Entity (A)) = E_Out_Parameter | |
3824 | then | |
3825 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", A); | |
3826 | end if; | |
3827 | end if; | |
3828 | ||
67ce0d7e RD |
3829 | -- Case of OUT or IN OUT parameter |
3830 | ||
36fcf362 | 3831 | if Ekind (F) /= E_In_Parameter then |
67ce0d7e RD |
3832 | |
3833 | -- For an Out parameter, check for useless assignment. Note | |
45fc7ddb HK |
3834 | -- that we can't set Last_Assignment this early, because we may |
3835 | -- kill current values in Resolve_Call, and that call would | |
3836 | -- clobber the Last_Assignment field. | |
67ce0d7e | 3837 | |
45fc7ddb HK |
3838 | -- Note: call Warn_On_Useless_Assignment before doing the check |
3839 | -- below for Is_OK_Variable_For_Out_Formal so that the setting | |
3840 | -- of Referenced_As_LHS/Referenced_As_Out_Formal properly | |
3841 | -- reflects the last assignment, not this one! | |
36fcf362 | 3842 | |
67ce0d7e | 3843 | if Ekind (F) = E_Out_Parameter then |
36fcf362 | 3844 | if Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
3845 | and then Is_Entity_Name (A) |
3846 | and then Present (Entity (A)) | |
36fcf362 | 3847 | and then Comes_From_Source (N) |
67ce0d7e | 3848 | then |
36fcf362 | 3849 | Warn_On_Useless_Assignment (Entity (A), A); |
67ce0d7e RD |
3850 | end if; |
3851 | end if; | |
3852 | ||
36fcf362 RD |
3853 | -- Validate the form of the actual. Note that the call to |
3854 | -- Is_OK_Variable_For_Out_Formal generates the required | |
3855 | -- reference in this case. | |
3856 | ||
0180fd26 AC |
3857 | -- A call to an initialization procedure for an aggregate |
3858 | -- component may initialize a nested component of a constant | |
3859 | -- designated object. In this context the object is variable. | |
3860 | ||
3861 | if not Is_OK_Variable_For_Out_Formal (A) | |
3862 | and then not Is_Init_Proc (Nam) | |
3863 | then | |
36fcf362 RD |
3864 | Error_Msg_NE ("actual for& must be a variable", A, F); |
3865 | end if; | |
3866 | ||
67ce0d7e | 3867 | -- What's the following about??? |
fbf5a39b AC |
3868 | |
3869 | if Is_Entity_Name (A) then | |
3870 | Kill_Checks (Entity (A)); | |
3871 | else | |
3872 | Kill_All_Checks; | |
3873 | end if; | |
3874 | end if; | |
3875 | ||
3876 | if Etype (A) = Any_Type then | |
3877 | Set_Etype (N, Any_Type); | |
3878 | return; | |
3879 | end if; | |
3880 | ||
996ae0b0 RK |
3881 | -- Apply appropriate range checks for in, out, and in-out |
3882 | -- parameters. Out and in-out parameters also need a separate | |
3883 | -- check, if there is a type conversion, to make sure the return | |
3884 | -- value meets the constraints of the variable before the | |
3885 | -- conversion. | |
3886 | ||
3887 | -- Gigi looks at the check flag and uses the appropriate types. | |
3888 | -- For now since one flag is used there is an optimization which | |
3889 | -- might not be done in the In Out case since Gigi does not do | |
3890 | -- any analysis. More thought required about this ??? | |
3891 | ||
8a95f4e8 | 3892 | if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then |
48f91b44 RD |
3893 | |
3894 | -- Apply predicate checks, unless this is a call to the | |
3895 | -- predicate check function itself, which would cause an | |
3896 | -- infinite recursion. | |
3897 | ||
3898 | if not (Ekind (Nam) = E_Function | |
3899 | and then Has_Predicates (Nam)) | |
3900 | then | |
3901 | Apply_Predicate_Check (A, F_Typ); | |
3902 | end if; | |
3903 | ||
3904 | -- Apply required constraint checks | |
3905 | ||
996ae0b0 RK |
3906 | if Is_Scalar_Type (Etype (A)) then |
3907 | Apply_Scalar_Range_Check (A, F_Typ); | |
3908 | ||
3909 | elsif Is_Array_Type (Etype (A)) then | |
3910 | Apply_Length_Check (A, F_Typ); | |
3911 | ||
3912 | elsif Is_Record_Type (F_Typ) | |
3913 | and then Has_Discriminants (F_Typ) | |
3914 | and then Is_Constrained (F_Typ) | |
3915 | and then (not Is_Derived_Type (F_Typ) | |
19fb051c | 3916 | or else Comes_From_Source (Nam)) |
996ae0b0 RK |
3917 | then |
3918 | Apply_Discriminant_Check (A, F_Typ); | |
3919 | ||
3920 | elsif Is_Access_Type (F_Typ) | |
3921 | and then Is_Array_Type (Designated_Type (F_Typ)) | |
3922 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3923 | then | |
3924 | Apply_Length_Check (A, F_Typ); | |
3925 | ||
3926 | elsif Is_Access_Type (F_Typ) | |
3927 | and then Has_Discriminants (Designated_Type (F_Typ)) | |
3928 | and then Is_Constrained (Designated_Type (F_Typ)) | |
3929 | then | |
3930 | Apply_Discriminant_Check (A, F_Typ); | |
3931 | ||
3932 | else | |
3933 | Apply_Range_Check (A, F_Typ); | |
3934 | end if; | |
2820d220 | 3935 | |
0f1a6a0b AC |
3936 | -- Ada 2005 (AI-231): Note that the controlling parameter case |
3937 | -- already existed in Ada 95, which is partially checked | |
3938 | -- elsewhere (see Checks), and we don't want the warning | |
3939 | -- message to differ. | |
2820d220 | 3940 | |
0f1a6a0b | 3941 | if Is_Access_Type (F_Typ) |
1420b484 | 3942 | and then Can_Never_Be_Null (F_Typ) |
aa5147f0 | 3943 | and then Known_Null (A) |
2820d220 | 3944 | then |
0f1a6a0b AC |
3945 | if Is_Controlling_Formal (F) then |
3946 | Apply_Compile_Time_Constraint_Error | |
3947 | (N => A, | |
3948 | Msg => "null value not allowed here?", | |
3949 | Reason => CE_Access_Check_Failed); | |
3950 | ||
3951 | elsif Ada_Version >= Ada_2005 then | |
3952 | Apply_Compile_Time_Constraint_Error | |
3953 | (N => A, | |
3954 | Msg => "(Ada 2005) null not allowed in " | |
3955 | & "null-excluding formal?", | |
3956 | Reason => CE_Null_Not_Allowed); | |
3957 | end if; | |
2820d220 | 3958 | end if; |
996ae0b0 RK |
3959 | end if; |
3960 | ||
8a95f4e8 | 3961 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then |
996ae0b0 RK |
3962 | if Nkind (A) = N_Type_Conversion then |
3963 | if Is_Scalar_Type (A_Typ) then | |
3964 | Apply_Scalar_Range_Check | |
3965 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3966 | else | |
3967 | Apply_Range_Check | |
3968 | (Expression (A), Etype (Expression (A)), A_Typ); | |
3969 | end if; | |
3970 | ||
3971 | else | |
3972 | if Is_Scalar_Type (F_Typ) then | |
3973 | Apply_Scalar_Range_Check (A, A_Typ, F_Typ); | |
996ae0b0 RK |
3974 | elsif Is_Array_Type (F_Typ) |
3975 | and then Ekind (F) = E_Out_Parameter | |
3976 | then | |
3977 | Apply_Length_Check (A, F_Typ); | |
996ae0b0 RK |
3978 | else |
3979 | Apply_Range_Check (A, A_Typ, F_Typ); | |
3980 | end if; | |
3981 | end if; | |
3982 | end if; | |
3983 | ||
3984 | -- An actual associated with an access parameter is implicitly | |
45fc7ddb HK |
3985 | -- converted to the anonymous access type of the formal and must |
3986 | -- satisfy the legality checks for access conversions. | |
996ae0b0 RK |
3987 | |
3988 | if Ekind (F_Typ) = E_Anonymous_Access_Type then | |
3989 | if not Valid_Conversion (A, F_Typ, A) then | |
3990 | Error_Msg_N | |
3991 | ("invalid implicit conversion for access parameter", A); | |
3992 | end if; | |
de94a7e7 AC |
3993 | |
3994 | -- If the actual is an access selected component of a variable, | |
3995 | -- the call may modify its designated object. It is reasonable | |
3996 | -- to treat this as a potential modification of the enclosing | |
3997 | -- record, to prevent spurious warnings that it should be | |
3998 | -- declared as a constant, because intuitively programmers | |
3999 | -- regard the designated subcomponent as part of the record. | |
4000 | ||
4001 | if Nkind (A) = N_Selected_Component | |
4002 | and then Is_Entity_Name (Prefix (A)) | |
4003 | and then not Is_Constant_Object (Entity (Prefix (A))) | |
4004 | then | |
4005 | Note_Possible_Modification (A, Sure => False); | |
4006 | end if; | |
996ae0b0 RK |
4007 | end if; |
4008 | ||
4009 | -- Check bad case of atomic/volatile argument (RM C.6(12)) | |
4010 | ||
4011 | if Is_By_Reference_Type (Etype (F)) | |
4012 | and then Comes_From_Source (N) | |
4013 | then | |
4014 | if Is_Atomic_Object (A) | |
4015 | and then not Is_Atomic (Etype (F)) | |
4016 | then | |
b5bf3335 AC |
4017 | Error_Msg_NE |
4018 | ("cannot pass atomic argument to non-atomic formal&", | |
4019 | A, F); | |
996ae0b0 RK |
4020 | |
4021 | elsif Is_Volatile_Object (A) | |
4022 | and then not Is_Volatile (Etype (F)) | |
4023 | then | |
b5bf3335 AC |
4024 | Error_Msg_NE |
4025 | ("cannot pass volatile argument to non-volatile formal&", | |
4026 | A, F); | |
996ae0b0 RK |
4027 | end if; |
4028 | end if; | |
4029 | ||
4030 | -- Check that subprograms don't have improper controlling | |
d81b4bfe | 4031 | -- arguments (RM 3.9.2 (9)). |
996ae0b0 | 4032 | |
0669bebe GB |
4033 | -- A primitive operation may have an access parameter of an |
4034 | -- incomplete tagged type, but a dispatching call is illegal | |
4035 | -- if the type is still incomplete. | |
4036 | ||
996ae0b0 RK |
4037 | if Is_Controlling_Formal (F) then |
4038 | Set_Is_Controlling_Actual (A); | |
0669bebe GB |
4039 | |
4040 | if Ekind (Etype (F)) = E_Anonymous_Access_Type then | |
4041 | declare | |
4042 | Desig : constant Entity_Id := Designated_Type (Etype (F)); | |
4043 | begin | |
4044 | if Ekind (Desig) = E_Incomplete_Type | |
4045 | and then No (Full_View (Desig)) | |
4046 | and then No (Non_Limited_View (Desig)) | |
4047 | then | |
4048 | Error_Msg_NE | |
4049 | ("premature use of incomplete type& " & | |
4050 | "in dispatching call", A, Desig); | |
4051 | end if; | |
4052 | end; | |
4053 | end if; | |
4054 | ||
996ae0b0 RK |
4055 | elsif Nkind (A) = N_Explicit_Dereference then |
4056 | Validate_Remote_Access_To_Class_Wide_Type (A); | |
4057 | end if; | |
4058 | ||
4059 | if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A)) | |
4060 | and then not Is_Class_Wide_Type (F_Typ) | |
4061 | and then not Is_Controlling_Formal (F) | |
4062 | then | |
4063 | Error_Msg_N ("class-wide argument not allowed here!", A); | |
07fc65c4 GB |
4064 | |
4065 | if Is_Subprogram (Nam) | |
4066 | and then Comes_From_Source (Nam) | |
4067 | then | |
996ae0b0 RK |
4068 | Error_Msg_Node_2 := F_Typ; |
4069 | Error_Msg_NE | |
82c80734 | 4070 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
4071 | end if; |
4072 | ||
97216ca8 ES |
4073 | -- Apply the checks described in 3.10.2(27): if the context is a |
4074 | -- specific access-to-object, the actual cannot be class-wide. | |
4075 | -- Use base type to exclude access_to_subprogram cases. | |
4076 | ||
996ae0b0 RK |
4077 | elsif Is_Access_Type (A_Typ) |
4078 | and then Is_Access_Type (F_Typ) | |
97216ca8 | 4079 | and then not Is_Access_Subprogram_Type (Base_Type (F_Typ)) |
996ae0b0 | 4080 | and then (Is_Class_Wide_Type (Designated_Type (A_Typ)) |
07fc65c4 GB |
4081 | or else (Nkind (A) = N_Attribute_Reference |
4082 | and then | |
97216ca8 | 4083 | Is_Class_Wide_Type (Etype (Prefix (A))))) |
996ae0b0 RK |
4084 | and then not Is_Class_Wide_Type (Designated_Type (F_Typ)) |
4085 | and then not Is_Controlling_Formal (F) | |
ae65d635 | 4086 | |
46fe0142 | 4087 | -- Disable these checks for call to imported C++ subprograms |
ae65d635 | 4088 | |
46fe0142 AC |
4089 | and then not |
4090 | (Is_Entity_Name (Name (N)) | |
4091 | and then Is_Imported (Entity (Name (N))) | |
4092 | and then Convention (Entity (Name (N))) = Convention_CPP) | |
996ae0b0 RK |
4093 | then |
4094 | Error_Msg_N | |
4095 | ("access to class-wide argument not allowed here!", A); | |
07fc65c4 | 4096 | |
97216ca8 | 4097 | if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then |
996ae0b0 RK |
4098 | Error_Msg_Node_2 := Designated_Type (F_Typ); |
4099 | Error_Msg_NE | |
82c80734 | 4100 | ("& is not a dispatching operation of &!", A, Nam); |
996ae0b0 RK |
4101 | end if; |
4102 | end if; | |
4103 | ||
4104 | Eval_Actual (A); | |
4105 | ||
8e4dac80 | 4106 | -- If it is a named association, treat the selector_name as a |
1f9939b5 | 4107 | -- proper identifier, and mark the corresponding entity. Ignore |
56812278 | 4108 | -- this reference in Alfa mode, as it refers to an entity not in |
1f9939b5 AC |
4109 | -- scope at the point of reference, so the reference should be |
4110 | -- ignored for computing effects of subprograms. | |
996ae0b0 | 4111 | |
1f9939b5 | 4112 | if Nkind (Parent (A)) = N_Parameter_Association |
56812278 | 4113 | and then not Alfa_Mode |
1f9939b5 | 4114 | then |
996ae0b0 RK |
4115 | Set_Entity (Selector_Name (Parent (A)), F); |
4116 | Generate_Reference (F, Selector_Name (Parent (A))); | |
4117 | Set_Etype (Selector_Name (Parent (A)), F_Typ); | |
4118 | Generate_Reference (F_Typ, N, ' '); | |
4119 | end if; | |
4120 | ||
4121 | Prev := A; | |
fbf5a39b AC |
4122 | |
4123 | if Ekind (F) /= E_Out_Parameter then | |
4124 | Check_Unset_Reference (A); | |
4125 | end if; | |
4126 | ||
996ae0b0 RK |
4127 | Next_Actual (A); |
4128 | ||
fbf5a39b AC |
4129 | -- Case where actual is not present |
4130 | ||
996ae0b0 RK |
4131 | else |
4132 | Insert_Default; | |
4133 | end if; | |
4134 | ||
4135 | Next_Formal (F); | |
4136 | end loop; | |
996ae0b0 RK |
4137 | end Resolve_Actuals; |
4138 | ||
4139 | ----------------------- | |
4140 | -- Resolve_Allocator -- | |
4141 | ----------------------- | |
4142 | ||
4143 | procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is | |
949a18cc | 4144 | Desig_T : constant Entity_Id := Designated_Type (Typ); |
ee2e3f6b | 4145 | E : constant Node_Id := Expression (N); |
996ae0b0 RK |
4146 | Subtyp : Entity_Id; |
4147 | Discrim : Entity_Id; | |
4148 | Constr : Node_Id; | |
b7d1f17f HK |
4149 | Aggr : Node_Id; |
4150 | Assoc : Node_Id := Empty; | |
996ae0b0 RK |
4151 | Disc_Exp : Node_Id; |
4152 | ||
b7d1f17f HK |
4153 | procedure Check_Allocator_Discrim_Accessibility |
4154 | (Disc_Exp : Node_Id; | |
4155 | Alloc_Typ : Entity_Id); | |
4156 | -- Check that accessibility level associated with an access discriminant | |
4157 | -- initialized in an allocator by the expression Disc_Exp is not deeper | |
4158 | -- than the level of the allocator type Alloc_Typ. An error message is | |
4159 | -- issued if this condition is violated. Specialized checks are done for | |
4160 | -- the cases of a constraint expression which is an access attribute or | |
4161 | -- an access discriminant. | |
4162 | ||
07fc65c4 | 4163 | function In_Dispatching_Context return Boolean; |
b7d1f17f HK |
4164 | -- If the allocator is an actual in a call, it is allowed to be class- |
4165 | -- wide when the context is not because it is a controlling actual. | |
4166 | ||
b7d1f17f HK |
4167 | ------------------------------------------- |
4168 | -- Check_Allocator_Discrim_Accessibility -- | |
4169 | ------------------------------------------- | |
4170 | ||
4171 | procedure Check_Allocator_Discrim_Accessibility | |
4172 | (Disc_Exp : Node_Id; | |
4173 | Alloc_Typ : Entity_Id) | |
4174 | is | |
4175 | begin | |
4176 | if Type_Access_Level (Etype (Disc_Exp)) > | |
f460d8f3 | 4177 | Deepest_Type_Access_Level (Alloc_Typ) |
b7d1f17f HK |
4178 | then |
4179 | Error_Msg_N | |
4180 | ("operand type has deeper level than allocator type", Disc_Exp); | |
4181 | ||
4182 | -- When the expression is an Access attribute the level of the prefix | |
4183 | -- object must not be deeper than that of the allocator's type. | |
4184 | ||
4185 | elsif Nkind (Disc_Exp) = N_Attribute_Reference | |
83e5da69 AC |
4186 | and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) = |
4187 | Attribute_Access | |
4188 | and then Object_Access_Level (Prefix (Disc_Exp)) > | |
4189 | Deepest_Type_Access_Level (Alloc_Typ) | |
b7d1f17f HK |
4190 | then |
4191 | Error_Msg_N | |
4192 | ("prefix of attribute has deeper level than allocator type", | |
4193 | Disc_Exp); | |
4194 | ||
4195 | -- When the expression is an access discriminant the check is against | |
4196 | -- the level of the prefix object. | |
4197 | ||
4198 | elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type | |
4199 | and then Nkind (Disc_Exp) = N_Selected_Component | |
83e5da69 AC |
4200 | and then Object_Access_Level (Prefix (Disc_Exp)) > |
4201 | Deepest_Type_Access_Level (Alloc_Typ) | |
b7d1f17f HK |
4202 | then |
4203 | Error_Msg_N | |
4204 | ("access discriminant has deeper level than allocator type", | |
4205 | Disc_Exp); | |
4206 | ||
4207 | -- All other cases are legal | |
4208 | ||
4209 | else | |
4210 | null; | |
4211 | end if; | |
4212 | end Check_Allocator_Discrim_Accessibility; | |
07fc65c4 GB |
4213 | |
4214 | ---------------------------- | |
4215 | -- In_Dispatching_Context -- | |
4216 | ---------------------------- | |
4217 | ||
4218 | function In_Dispatching_Context return Boolean is | |
4219 | Par : constant Node_Id := Parent (N); | |
b7d1f17f HK |
4220 | |
4221 | begin | |
d3b00ce3 AC |
4222 | return Nkind (Par) in N_Subprogram_Call |
4223 | and then Is_Entity_Name (Name (Par)) | |
4224 | and then Is_Dispatching_Operation (Entity (Name (Par))); | |
df3e68b1 | 4225 | end In_Dispatching_Context; |
b7d1f17f | 4226 | |
07fc65c4 GB |
4227 | -- Start of processing for Resolve_Allocator |
4228 | ||
996ae0b0 RK |
4229 | begin |
4230 | -- Replace general access with specific type | |
4231 | ||
4232 | if Ekind (Etype (N)) = E_Allocator_Type then | |
4233 | Set_Etype (N, Base_Type (Typ)); | |
4234 | end if; | |
4235 | ||
0669bebe | 4236 | if Is_Abstract_Type (Typ) then |
996ae0b0 RK |
4237 | Error_Msg_N ("type of allocator cannot be abstract", N); |
4238 | end if; | |
4239 | ||
4240 | -- For qualified expression, resolve the expression using the | |
4241 | -- given subtype (nothing to do for type mark, subtype indication) | |
4242 | ||
4243 | if Nkind (E) = N_Qualified_Expression then | |
4244 | if Is_Class_Wide_Type (Etype (E)) | |
949a18cc | 4245 | and then not Is_Class_Wide_Type (Desig_T) |
07fc65c4 | 4246 | and then not In_Dispatching_Context |
996ae0b0 RK |
4247 | then |
4248 | Error_Msg_N | |
4249 | ("class-wide allocator not allowed for this access type", N); | |
4250 | end if; | |
4251 | ||
4252 | Resolve (Expression (E), Etype (E)); | |
4253 | Check_Unset_Reference (Expression (E)); | |
4254 | ||
fbf5a39b | 4255 | -- A qualified expression requires an exact match of the type, |
7b4db06c | 4256 | -- class-wide matching is not allowed. |
fbf5a39b | 4257 | |
7b4db06c | 4258 | if (Is_Class_Wide_Type (Etype (Expression (E))) |
19fb051c | 4259 | or else Is_Class_Wide_Type (Etype (E))) |
fbf5a39b AC |
4260 | and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E)) |
4261 | then | |
4262 | Wrong_Type (Expression (E), Etype (E)); | |
4263 | end if; | |
4264 | ||
a8551b5f AC |
4265 | -- Calls to build-in-place functions are not currently supported in |
4266 | -- allocators for access types associated with a simple storage pool. | |
4267 | -- Supporting such allocators may require passing additional implicit | |
4268 | -- parameters to build-in-place functions (or a significant revision | |
4269 | -- of the current b-i-p implementation to unify the handling for | |
4270 | -- multiple kinds of storage pools). ??? | |
4271 | ||
4272 | if Is_Immutably_Limited_Type (Desig_T) | |
4273 | and then Nkind (Expression (E)) = N_Function_Call | |
4274 | then | |
4275 | declare | |
260359e3 AC |
4276 | Pool : constant Entity_Id := |
4277 | Associated_Storage_Pool (Root_Type (Typ)); | |
a8551b5f AC |
4278 | begin |
4279 | if Present (Pool) | |
f6205414 AC |
4280 | and then |
4281 | Present (Get_Rep_Pragma | |
4282 | (Etype (Pool), Name_Simple_Storage_Pool_Type)) | |
a8551b5f AC |
4283 | then |
4284 | Error_Msg_N | |
4285 | ("limited function calls not yet supported in simple " & | |
4286 | "storage pool allocators", Expression (E)); | |
4287 | end if; | |
4288 | end; | |
4289 | end if; | |
4290 | ||
b7d1f17f HK |
4291 | -- A special accessibility check is needed for allocators that |
4292 | -- constrain access discriminants. The level of the type of the | |
4293 | -- expression used to constrain an access discriminant cannot be | |
f3d57416 | 4294 | -- deeper than the type of the allocator (in contrast to access |
b7d1f17f HK |
4295 | -- parameters, where the level of the actual can be arbitrary). |
4296 | ||
4297 | -- We can't use Valid_Conversion to perform this check because | |
4298 | -- in general the type of the allocator is unrelated to the type | |
4299 | -- of the access discriminant. | |
4300 | ||
4301 | if Ekind (Typ) /= E_Anonymous_Access_Type | |
4302 | or else Is_Local_Anonymous_Access (Typ) | |
4303 | then | |
4304 | Subtyp := Entity (Subtype_Mark (E)); | |
4305 | ||
4306 | Aggr := Original_Node (Expression (E)); | |
4307 | ||
4308 | if Has_Discriminants (Subtyp) | |
45fc7ddb | 4309 | and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate) |
b7d1f17f HK |
4310 | then |
4311 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4312 | ||
4313 | -- Get the first component expression of the aggregate | |
4314 | ||
4315 | if Present (Expressions (Aggr)) then | |
4316 | Disc_Exp := First (Expressions (Aggr)); | |
4317 | ||
4318 | elsif Present (Component_Associations (Aggr)) then | |
4319 | Assoc := First (Component_Associations (Aggr)); | |
4320 | ||
4321 | if Present (Assoc) then | |
4322 | Disc_Exp := Expression (Assoc); | |
4323 | else | |
4324 | Disc_Exp := Empty; | |
4325 | end if; | |
4326 | ||
4327 | else | |
4328 | Disc_Exp := Empty; | |
4329 | end if; | |
4330 | ||
4331 | while Present (Discrim) and then Present (Disc_Exp) loop | |
4332 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4333 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); | |
4334 | end if; | |
4335 | ||
4336 | Next_Discriminant (Discrim); | |
4337 | ||
4338 | if Present (Discrim) then | |
4339 | if Present (Assoc) then | |
4340 | Next (Assoc); | |
4341 | Disc_Exp := Expression (Assoc); | |
4342 | ||
4343 | elsif Present (Next (Disc_Exp)) then | |
4344 | Next (Disc_Exp); | |
4345 | ||
4346 | else | |
4347 | Assoc := First (Component_Associations (Aggr)); | |
4348 | ||
4349 | if Present (Assoc) then | |
4350 | Disc_Exp := Expression (Assoc); | |
4351 | else | |
4352 | Disc_Exp := Empty; | |
4353 | end if; | |
4354 | end if; | |
4355 | end if; | |
4356 | end loop; | |
4357 | end if; | |
4358 | end if; | |
4359 | ||
996ae0b0 RK |
4360 | -- For a subtype mark or subtype indication, freeze the subtype |
4361 | ||
4362 | else | |
4363 | Freeze_Expression (E); | |
4364 | ||
4365 | if Is_Access_Constant (Typ) and then not No_Initialization (N) then | |
4366 | Error_Msg_N | |
4367 | ("initialization required for access-to-constant allocator", N); | |
4368 | end if; | |
4369 | ||
4370 | -- A special accessibility check is needed for allocators that | |
4371 | -- constrain access discriminants. The level of the type of the | |
b7d1f17f | 4372 | -- expression used to constrain an access discriminant cannot be |
f3d57416 | 4373 | -- deeper than the type of the allocator (in contrast to access |
996ae0b0 RK |
4374 | -- parameters, where the level of the actual can be arbitrary). |
4375 | -- We can't use Valid_Conversion to perform this check because | |
4376 | -- in general the type of the allocator is unrelated to the type | |
b7d1f17f | 4377 | -- of the access discriminant. |
996ae0b0 RK |
4378 | |
4379 | if Nkind (Original_Node (E)) = N_Subtype_Indication | |
b7d1f17f HK |
4380 | and then (Ekind (Typ) /= E_Anonymous_Access_Type |
4381 | or else Is_Local_Anonymous_Access (Typ)) | |
996ae0b0 RK |
4382 | then |
4383 | Subtyp := Entity (Subtype_Mark (Original_Node (E))); | |
4384 | ||
4385 | if Has_Discriminants (Subtyp) then | |
4386 | Discrim := First_Discriminant (Base_Type (Subtyp)); | |
4387 | Constr := First (Constraints (Constraint (Original_Node (E)))); | |
996ae0b0 RK |
4388 | while Present (Discrim) and then Present (Constr) loop |
4389 | if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then | |
4390 | if Nkind (Constr) = N_Discriminant_Association then | |
4391 | Disc_Exp := Original_Node (Expression (Constr)); | |
4392 | else | |
4393 | Disc_Exp := Original_Node (Constr); | |
4394 | end if; | |
4395 | ||
b7d1f17f | 4396 | Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ); |
996ae0b0 | 4397 | end if; |
b7d1f17f | 4398 | |
996ae0b0 RK |
4399 | Next_Discriminant (Discrim); |
4400 | Next (Constr); | |
4401 | end loop; | |
4402 | end if; | |
4403 | end if; | |
4404 | end if; | |
4405 | ||
758c442c GD |
4406 | -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility |
4407 | -- check that the level of the type of the created object is not deeper | |
4408 | -- than the level of the allocator's access type, since extensions can | |
4409 | -- now occur at deeper levels than their ancestor types. This is a | |
4410 | -- static accessibility level check; a run-time check is also needed in | |
4411 | -- the case of an initialized allocator with a class-wide argument (see | |
4412 | -- Expand_Allocator_Expression). | |
4413 | ||
0791fbe9 | 4414 | if Ada_Version >= Ada_2005 |
949a18cc | 4415 | and then Is_Class_Wide_Type (Desig_T) |
758c442c GD |
4416 | then |
4417 | declare | |
b7d1f17f | 4418 | Exp_Typ : Entity_Id; |
758c442c GD |
4419 | |
4420 | begin | |
4421 | if Nkind (E) = N_Qualified_Expression then | |
4422 | Exp_Typ := Etype (E); | |
4423 | elsif Nkind (E) = N_Subtype_Indication then | |
4424 | Exp_Typ := Entity (Subtype_Mark (Original_Node (E))); | |
4425 | else | |
4426 | Exp_Typ := Entity (E); | |
4427 | end if; | |
4428 | ||
f460d8f3 | 4429 | if Type_Access_Level (Exp_Typ) > |
83e5da69 AC |
4430 | Deepest_Type_Access_Level (Typ) |
4431 | then | |
758c442c GD |
4432 | if In_Instance_Body then |
4433 | Error_Msg_N ("?type in allocator has deeper level than" & | |
4434 | " designated class-wide type", E); | |
c8ef728f ES |
4435 | Error_Msg_N ("\?Program_Error will be raised at run time", |
4436 | E); | |
758c442c GD |
4437 | Rewrite (N, |
4438 | Make_Raise_Program_Error (Sloc (N), | |
4439 | Reason => PE_Accessibility_Check_Failed)); | |
4440 | Set_Etype (N, Typ); | |
aa180613 RD |
4441 | |
4442 | -- Do not apply Ada 2005 accessibility checks on a class-wide | |
4443 | -- allocator if the type given in the allocator is a formal | |
4444 | -- type. A run-time check will be performed in the instance. | |
4445 | ||
4446 | elsif not Is_Generic_Type (Exp_Typ) then | |
758c442c GD |
4447 | Error_Msg_N ("type in allocator has deeper level than" & |
4448 | " designated class-wide type", E); | |
4449 | end if; | |
4450 | end if; | |
4451 | end; | |
4452 | end if; | |
4453 | ||
996ae0b0 RK |
4454 | -- Check for allocation from an empty storage pool |
4455 | ||
4456 | if No_Pool_Assigned (Typ) then | |
8da337c5 | 4457 | Error_Msg_N ("allocation from empty storage pool!", N); |
1420b484 | 4458 | |
5cc9353d RD |
4459 | -- If the context is an unchecked conversion, as may happen within an |
4460 | -- inlined subprogram, the allocator is being resolved with its own | |
4461 | -- anonymous type. In that case, if the target type has a specific | |
1420b484 JM |
4462 | -- storage pool, it must be inherited explicitly by the allocator type. |
4463 | ||
4464 | elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion | |
4465 | and then No (Associated_Storage_Pool (Typ)) | |
4466 | then | |
4467 | Set_Associated_Storage_Pool | |
4468 | (Typ, Associated_Storage_Pool (Etype (Parent (N)))); | |
996ae0b0 | 4469 | end if; |
b7d1f17f | 4470 | |
e57ab550 AC |
4471 | if Ekind (Etype (N)) = E_Anonymous_Access_Type then |
4472 | Check_Restriction (No_Anonymous_Allocators, N); | |
4473 | end if; | |
4474 | ||
6aaa0587 ES |
4475 | -- Check that an allocator with task parts isn't for a nested access |
4476 | -- type when restriction No_Task_Hierarchy applies. | |
4477 | ||
4478 | if not Is_Library_Level_Entity (Base_Type (Typ)) | |
949a18cc | 4479 | and then Has_Task (Base_Type (Desig_T)) |
6aaa0587 ES |
4480 | then |
4481 | Check_Restriction (No_Task_Hierarchy, N); | |
4482 | end if; | |
4483 | ||
b7d1f17f HK |
4484 | -- An erroneous allocator may be rewritten as a raise Program_Error |
4485 | -- statement. | |
4486 | ||
4487 | if Nkind (N) = N_Allocator then | |
4488 | ||
4489 | -- An anonymous access discriminant is the definition of a | |
aa5147f0 | 4490 | -- coextension. |
b7d1f17f HK |
4491 | |
4492 | if Ekind (Typ) = E_Anonymous_Access_Type | |
4493 | and then Nkind (Associated_Node_For_Itype (Typ)) = | |
4494 | N_Discriminant_Specification | |
4495 | then | |
949a18cc AC |
4496 | declare |
4497 | Discr : constant Entity_Id := | |
4498 | Defining_Identifier (Associated_Node_For_Itype (Typ)); | |
ee2e3f6b | 4499 | |
949a18cc | 4500 | begin |
5d59eef2 AC |
4501 | -- Ada 2012 AI05-0052: If the designated type of the allocator |
4502 | -- is limited, then the allocator shall not be used to define | |
4503 | -- the value of an access discriminant unless the discriminated | |
949a18cc AC |
4504 | -- type is immutably limited. |
4505 | ||
4506 | if Ada_Version >= Ada_2012 | |
4507 | and then Is_Limited_Type (Desig_T) | |
4508 | and then not Is_Immutably_Limited_Type (Scope (Discr)) | |
4509 | then | |
4510 | Error_Msg_N | |
5d59eef2 AC |
4511 | ("only immutably limited types can have anonymous " |
4512 | & "access discriminants designating a limited type", N); | |
949a18cc AC |
4513 | end if; |
4514 | end; | |
4515 | ||
b7d1f17f | 4516 | -- Avoid marking an allocator as a dynamic coextension if it is |
aa5147f0 | 4517 | -- within a static construct. |
b7d1f17f HK |
4518 | |
4519 | if not Is_Static_Coextension (N) then | |
aa5147f0 | 4520 | Set_Is_Dynamic_Coextension (N); |
b7d1f17f HK |
4521 | end if; |
4522 | ||
4523 | -- Cleanup for potential static coextensions | |
4524 | ||
4525 | else | |
aa5147f0 ES |
4526 | Set_Is_Dynamic_Coextension (N, False); |
4527 | Set_Is_Static_Coextension (N, False); | |
b7d1f17f | 4528 | end if; |
b7d1f17f | 4529 | end if; |
d9b056ea | 4530 | |
833eaa8a | 4531 | -- Report a simple error: if the designated object is a local task, |
14848f57 AC |
4532 | -- its body has not been seen yet, and its activation will fail an |
4533 | -- elaboration check. | |
d9b056ea | 4534 | |
949a18cc AC |
4535 | if Is_Task_Type (Desig_T) |
4536 | and then Scope (Base_Type (Desig_T)) = Current_Scope | |
d9b056ea AC |
4537 | and then Is_Compilation_Unit (Current_Scope) |
4538 | and then Ekind (Current_Scope) = E_Package | |
4539 | and then not In_Package_Body (Current_Scope) | |
4540 | then | |
7b2aafc9 HK |
4541 | Error_Msg_N ("?cannot activate task before body seen", N); |
4542 | Error_Msg_N ("\?Program_Error will be raised at run time", N); | |
d9b056ea | 4543 | end if; |
14848f57 | 4544 | |
7b2aafc9 HK |
4545 | -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a |
4546 | -- type with a task component on a subpool. This action must raise | |
4547 | -- Program_Error at runtime. | |
14848f57 AC |
4548 | |
4549 | if Ada_Version >= Ada_2012 | |
dfbcb149 | 4550 | and then Nkind (N) = N_Allocator |
14848f57 AC |
4551 | and then Present (Subpool_Handle_Name (N)) |
4552 | and then Has_Task (Desig_T) | |
4553 | then | |
7b2aafc9 HK |
4554 | Error_Msg_N ("?cannot allocate task on subpool", N); |
4555 | Error_Msg_N ("\?Program_Error will be raised at run time", N); | |
4556 | ||
4557 | Rewrite (N, | |
4558 | Make_Raise_Program_Error (Sloc (N), | |
4559 | Reason => PE_Explicit_Raise)); | |
4560 | Set_Etype (N, Typ); | |
14848f57 | 4561 | end if; |
996ae0b0 RK |
4562 | end Resolve_Allocator; |
4563 | ||
4564 | --------------------------- | |
4565 | -- Resolve_Arithmetic_Op -- | |
4566 | --------------------------- | |
4567 | ||
4568 | -- Used for resolving all arithmetic operators except exponentiation | |
4569 | ||
4570 | procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
4571 | L : constant Node_Id := Left_Opnd (N); |
4572 | R : constant Node_Id := Right_Opnd (N); | |
4573 | TL : constant Entity_Id := Base_Type (Etype (L)); | |
4574 | TR : constant Entity_Id := Base_Type (Etype (R)); | |
4575 | T : Entity_Id; | |
4576 | Rop : Node_Id; | |
996ae0b0 RK |
4577 | |
4578 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
4579 | -- We do the resolution using the base type, because intermediate values | |
4580 | -- in expressions always are of the base type, not a subtype of it. | |
4581 | ||
aa180613 RD |
4582 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean; |
4583 | -- Returns True if N is in a context that expects "any real type" | |
4584 | ||
996ae0b0 RK |
4585 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean; |
4586 | -- Return True iff given type is Integer or universal real/integer | |
4587 | ||
4588 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id); | |
4589 | -- Choose type of integer literal in fixed-point operation to conform | |
4590 | -- to available fixed-point type. T is the type of the other operand, | |
4591 | -- which is needed to determine the expected type of N. | |
4592 | ||
4593 | procedure Set_Operand_Type (N : Node_Id); | |
4594 | -- Set operand type to T if universal | |
4595 | ||
aa180613 RD |
4596 | ------------------------------- |
4597 | -- Expected_Type_Is_Any_Real -- | |
4598 | ------------------------------- | |
4599 | ||
4600 | function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is | |
4601 | begin | |
4602 | -- N is the expression after "delta" in a fixed_point_definition; | |
4603 | -- see RM-3.5.9(6): | |
4604 | ||
45fc7ddb HK |
4605 | return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition, |
4606 | N_Decimal_Fixed_Point_Definition, | |
aa180613 RD |
4607 | |
4608 | -- N is one of the bounds in a real_range_specification; | |
4609 | -- see RM-3.5.7(5): | |
4610 | ||
45fc7ddb | 4611 | N_Real_Range_Specification, |
aa180613 RD |
4612 | |
4613 | -- N is the expression of a delta_constraint; | |
4614 | -- see RM-J.3(3): | |
4615 | ||
45fc7ddb | 4616 | N_Delta_Constraint); |
aa180613 RD |
4617 | end Expected_Type_Is_Any_Real; |
4618 | ||
996ae0b0 RK |
4619 | ----------------------------- |
4620 | -- Is_Integer_Or_Universal -- | |
4621 | ----------------------------- | |
4622 | ||
4623 | function Is_Integer_Or_Universal (N : Node_Id) return Boolean is | |
4624 | T : Entity_Id; | |
4625 | Index : Interp_Index; | |
4626 | It : Interp; | |
4627 | ||
4628 | begin | |
4629 | if not Is_Overloaded (N) then | |
4630 | T := Etype (N); | |
4631 | return Base_Type (T) = Base_Type (Standard_Integer) | |
4632 | or else T = Universal_Integer | |
4633 | or else T = Universal_Real; | |
4634 | else | |
4635 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4636 | while Present (It.Typ) loop |
996ae0b0 RK |
4637 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) |
4638 | or else It.Typ = Universal_Integer | |
4639 | or else It.Typ = Universal_Real | |
4640 | then | |
4641 | return True; | |
4642 | end if; | |
4643 | ||
4644 | Get_Next_Interp (Index, It); | |
4645 | end loop; | |
4646 | end if; | |
4647 | ||
4648 | return False; | |
4649 | end Is_Integer_Or_Universal; | |
4650 | ||
4651 | ---------------------------- | |
4652 | -- Set_Mixed_Mode_Operand -- | |
4653 | ---------------------------- | |
4654 | ||
4655 | procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is | |
4656 | Index : Interp_Index; | |
4657 | It : Interp; | |
4658 | ||
4659 | begin | |
4660 | if Universal_Interpretation (N) = Universal_Integer then | |
4661 | ||
4662 | -- A universal integer literal is resolved as standard integer | |
758c442c GD |
4663 | -- except in the case of a fixed-point result, where we leave it |
4664 | -- as universal (to be handled by Exp_Fixd later on) | |
996ae0b0 RK |
4665 | |
4666 | if Is_Fixed_Point_Type (T) then | |
4667 | Resolve (N, Universal_Integer); | |
4668 | else | |
4669 | Resolve (N, Standard_Integer); | |
4670 | end if; | |
4671 | ||
4672 | elsif Universal_Interpretation (N) = Universal_Real | |
4673 | and then (T = Base_Type (Standard_Integer) | |
4674 | or else T = Universal_Integer | |
4675 | or else T = Universal_Real) | |
4676 | then | |
4677 | -- A universal real can appear in a fixed-type context. We resolve | |
4678 | -- the literal with that context, even though this might raise an | |
4679 | -- exception prematurely (the other operand may be zero). | |
4680 | ||
4681 | Resolve (N, B_Typ); | |
4682 | ||
4683 | elsif Etype (N) = Base_Type (Standard_Integer) | |
4684 | and then T = Universal_Real | |
4685 | and then Is_Overloaded (N) | |
4686 | then | |
4687 | -- Integer arg in mixed-mode operation. Resolve with universal | |
4688 | -- type, in case preference rule must be applied. | |
4689 | ||
4690 | Resolve (N, Universal_Integer); | |
4691 | ||
4692 | elsif Etype (N) = T | |
4693 | and then B_Typ /= Universal_Fixed | |
4694 | then | |
a77842bd | 4695 | -- Not a mixed-mode operation, resolve with context |
996ae0b0 RK |
4696 | |
4697 | Resolve (N, B_Typ); | |
4698 | ||
4699 | elsif Etype (N) = Any_Fixed then | |
4700 | ||
a77842bd | 4701 | -- N may itself be a mixed-mode operation, so use context type |
996ae0b0 RK |
4702 | |
4703 | Resolve (N, B_Typ); | |
4704 | ||
4705 | elsif Is_Fixed_Point_Type (T) | |
4706 | and then B_Typ = Universal_Fixed | |
4707 | and then Is_Overloaded (N) | |
4708 | then | |
4709 | -- Must be (fixed * fixed) operation, operand must have one | |
4710 | -- compatible interpretation. | |
4711 | ||
4712 | Resolve (N, Any_Fixed); | |
4713 | ||
4714 | elsif Is_Fixed_Point_Type (B_Typ) | |
4715 | and then (T = Universal_Real | |
4716 | or else Is_Fixed_Point_Type (T)) | |
4717 | and then Is_Overloaded (N) | |
4718 | then | |
4719 | -- C * F(X) in a fixed context, where C is a real literal or a | |
4720 | -- fixed-point expression. F must have either a fixed type | |
4721 | -- interpretation or an integer interpretation, but not both. | |
4722 | ||
4723 | Get_First_Interp (N, Index, It); | |
996ae0b0 | 4724 | while Present (It.Typ) loop |
996ae0b0 | 4725 | if Base_Type (It.Typ) = Base_Type (Standard_Integer) then |
996ae0b0 RK |
4726 | if Analyzed (N) then |
4727 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4728 | else | |
4729 | Resolve (N, Standard_Integer); | |
4730 | end if; | |
4731 | ||
4732 | elsif Is_Fixed_Point_Type (It.Typ) then | |
996ae0b0 RK |
4733 | if Analyzed (N) then |
4734 | Error_Msg_N ("ambiguous operand in fixed operation", N); | |
4735 | else | |
4736 | Resolve (N, It.Typ); | |
4737 | end if; | |
4738 | end if; | |
4739 | ||
4740 | Get_Next_Interp (Index, It); | |
4741 | end loop; | |
4742 | ||
758c442c GD |
4743 | -- Reanalyze the literal with the fixed type of the context. If |
4744 | -- context is Universal_Fixed, we are within a conversion, leave | |
4745 | -- the literal as a universal real because there is no usable | |
4746 | -- fixed type, and the target of the conversion plays no role in | |
4747 | -- the resolution. | |
996ae0b0 | 4748 | |
0ab80019 AC |
4749 | declare |
4750 | Op2 : Node_Id; | |
4751 | T2 : Entity_Id; | |
4752 | ||
4753 | begin | |
4754 | if N = L then | |
4755 | Op2 := R; | |
4756 | else | |
4757 | Op2 := L; | |
4758 | end if; | |
4759 | ||
4760 | if B_Typ = Universal_Fixed | |
4761 | and then Nkind (Op2) = N_Real_Literal | |
4762 | then | |
4763 | T2 := Universal_Real; | |
4764 | else | |
4765 | T2 := B_Typ; | |
4766 | end if; | |
4767 | ||
4768 | Set_Analyzed (Op2, False); | |
4769 | Resolve (Op2, T2); | |
4770 | end; | |
996ae0b0 RK |
4771 | |
4772 | else | |
fbf5a39b | 4773 | Resolve (N); |
996ae0b0 RK |
4774 | end if; |
4775 | end Set_Mixed_Mode_Operand; | |
4776 | ||
4777 | ---------------------- | |
4778 | -- Set_Operand_Type -- | |
4779 | ---------------------- | |
4780 | ||
4781 | procedure Set_Operand_Type (N : Node_Id) is | |
4782 | begin | |
4783 | if Etype (N) = Universal_Integer | |
4784 | or else Etype (N) = Universal_Real | |
4785 | then | |
4786 | Set_Etype (N, T); | |
4787 | end if; | |
4788 | end Set_Operand_Type; | |
4789 | ||
996ae0b0 RK |
4790 | -- Start of processing for Resolve_Arithmetic_Op |
4791 | ||
4792 | begin | |
4793 | if Comes_From_Source (N) | |
4794 | and then Ekind (Entity (N)) = E_Function | |
4795 | and then Is_Imported (Entity (N)) | |
fbf5a39b | 4796 | and then Is_Intrinsic_Subprogram (Entity (N)) |
996ae0b0 RK |
4797 | then |
4798 | Resolve_Intrinsic_Operator (N, Typ); | |
4799 | return; | |
4800 | ||
5cc9353d RD |
4801 | -- Special-case for mixed-mode universal expressions or fixed point type |
4802 | -- operation: each argument is resolved separately. The same treatment | |
4803 | -- is required if one of the operands of a fixed point operation is | |
4804 | -- universal real, since in this case we don't do a conversion to a | |
4805 | -- specific fixed-point type (instead the expander handles the case). | |
996ae0b0 | 4806 | |
ddf67a1d AC |
4807 | -- Set the type of the node to its universal interpretation because |
4808 | -- legality checks on an exponentiation operand need the context. | |
4809 | ||
45fc7ddb | 4810 | elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real) |
996ae0b0 RK |
4811 | and then Present (Universal_Interpretation (L)) |
4812 | and then Present (Universal_Interpretation (R)) | |
4813 | then | |
ddf67a1d | 4814 | Set_Etype (N, B_Typ); |
996ae0b0 RK |
4815 | Resolve (L, Universal_Interpretation (L)); |
4816 | Resolve (R, Universal_Interpretation (R)); | |
996ae0b0 RK |
4817 | |
4818 | elsif (B_Typ = Universal_Real | |
45fc7ddb HK |
4819 | or else Etype (N) = Universal_Fixed |
4820 | or else (Etype (N) = Any_Fixed | |
4821 | and then Is_Fixed_Point_Type (B_Typ)) | |
4822 | or else (Is_Fixed_Point_Type (B_Typ) | |
4823 | and then (Is_Integer_Or_Universal (L) | |
4824 | or else | |
4825 | Is_Integer_Or_Universal (R)))) | |
4826 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) | |
996ae0b0 RK |
4827 | then |
4828 | if TL = Universal_Integer or else TR = Universal_Integer then | |
4829 | Check_For_Visible_Operator (N, B_Typ); | |
4830 | end if; | |
4831 | ||
5cc9353d RD |
4832 | -- If context is a fixed type and one operand is integer, the other |
4833 | -- is resolved with the type of the context. | |
996ae0b0 RK |
4834 | |
4835 | if Is_Fixed_Point_Type (B_Typ) | |
4836 | and then (Base_Type (TL) = Base_Type (Standard_Integer) | |
4837 | or else TL = Universal_Integer) | |
4838 | then | |
4839 | Resolve (R, B_Typ); | |
4840 | Resolve (L, TL); | |
4841 | ||
4842 | elsif Is_Fixed_Point_Type (B_Typ) | |
4843 | and then (Base_Type (TR) = Base_Type (Standard_Integer) | |
4844 | or else TR = Universal_Integer) | |
4845 | then | |
4846 | Resolve (L, B_Typ); | |
4847 | Resolve (R, TR); | |
4848 | ||
4849 | else | |
4850 | Set_Mixed_Mode_Operand (L, TR); | |
4851 | Set_Mixed_Mode_Operand (R, TL); | |
4852 | end if; | |
4853 | ||
45fc7ddb HK |
4854 | -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed |
4855 | -- multiplying operators from being used when the expected type is | |
4856 | -- also universal_fixed. Note that B_Typ will be Universal_Fixed in | |
4857 | -- some cases where the expected type is actually Any_Real; | |
4858 | -- Expected_Type_Is_Any_Real takes care of that case. | |
aa180613 | 4859 | |
996ae0b0 RK |
4860 | if Etype (N) = Universal_Fixed |
4861 | or else Etype (N) = Any_Fixed | |
4862 | then | |
4863 | if B_Typ = Universal_Fixed | |
aa180613 | 4864 | and then not Expected_Type_Is_Any_Real (N) |
45fc7ddb HK |
4865 | and then not Nkind_In (Parent (N), N_Type_Conversion, |
4866 | N_Unchecked_Type_Conversion) | |
996ae0b0 | 4867 | then |
45fc7ddb HK |
4868 | Error_Msg_N ("type cannot be determined from context!", N); |
4869 | Error_Msg_N ("\explicit conversion to result type required", N); | |
996ae0b0 RK |
4870 | |
4871 | Set_Etype (L, Any_Type); | |
4872 | Set_Etype (R, Any_Type); | |
4873 | ||
4874 | else | |
0ab80019 | 4875 | if Ada_Version = Ada_83 |
45fc7ddb HK |
4876 | and then Etype (N) = Universal_Fixed |
4877 | and then not | |
4878 | Nkind_In (Parent (N), N_Type_Conversion, | |
4879 | N_Unchecked_Type_Conversion) | |
996ae0b0 RK |
4880 | then |
4881 | Error_Msg_N | |
45fc7ddb HK |
4882 | ("(Ada 83) fixed-point operation " |
4883 | & "needs explicit conversion", N); | |
996ae0b0 RK |
4884 | end if; |
4885 | ||
aa180613 | 4886 | -- The expected type is "any real type" in contexts like |
5cc9353d | 4887 | |
aa180613 | 4888 | -- type T is delta <universal_fixed-expression> ... |
5cc9353d | 4889 | |
aa180613 RD |
4890 | -- in which case we need to set the type to Universal_Real |
4891 | -- so that static expression evaluation will work properly. | |
4892 | ||
4893 | if Expected_Type_Is_Any_Real (N) then | |
4894 | Set_Etype (N, Universal_Real); | |
4895 | else | |
4896 | Set_Etype (N, B_Typ); | |
4897 | end if; | |
996ae0b0 RK |
4898 | end if; |
4899 | ||
4900 | elsif Is_Fixed_Point_Type (B_Typ) | |
4901 | and then (Is_Integer_Or_Universal (L) | |
4902 | or else Nkind (L) = N_Real_Literal | |
4903 | or else Nkind (R) = N_Real_Literal | |
45fc7ddb | 4904 | or else Is_Integer_Or_Universal (R)) |
996ae0b0 RK |
4905 | then |
4906 | Set_Etype (N, B_Typ); | |
4907 | ||
4908 | elsif Etype (N) = Any_Fixed then | |
4909 | ||
5cc9353d RD |
4910 | -- If no previous errors, this is only possible if one operand is |
4911 | -- overloaded and the context is universal. Resolve as such. | |
996ae0b0 RK |
4912 | |
4913 | Set_Etype (N, B_Typ); | |
4914 | end if; | |
4915 | ||
4916 | else | |
4917 | if (TL = Universal_Integer or else TL = Universal_Real) | |
45fc7ddb HK |
4918 | and then |
4919 | (TR = Universal_Integer or else TR = Universal_Real) | |
996ae0b0 RK |
4920 | then |
4921 | Check_For_Visible_Operator (N, B_Typ); | |
4922 | end if; | |
4923 | ||
4924 | -- If the context is Universal_Fixed and the operands are also | |
4925 | -- universal fixed, this is an error, unless there is only one | |
841dd0f5 | 4926 | -- applicable fixed_point type (usually Duration). |
996ae0b0 | 4927 | |
45fc7ddb | 4928 | if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then |
996ae0b0 RK |
4929 | T := Unique_Fixed_Point_Type (N); |
4930 | ||
4931 | if T = Any_Type then | |
4932 | Set_Etype (N, T); | |
4933 | return; | |
4934 | else | |
4935 | Resolve (L, T); | |
4936 | Resolve (R, T); | |
4937 | end if; | |
4938 | ||
4939 | else | |
4940 | Resolve (L, B_Typ); | |
4941 | Resolve (R, B_Typ); | |
4942 | end if; | |
4943 | ||
4944 | -- If one of the arguments was resolved to a non-universal type. | |
4945 | -- label the result of the operation itself with the same type. | |
4946 | -- Do the same for the universal argument, if any. | |
4947 | ||
4948 | T := Intersect_Types (L, R); | |
4949 | Set_Etype (N, Base_Type (T)); | |
4950 | Set_Operand_Type (L); | |
4951 | Set_Operand_Type (R); | |
4952 | end if; | |
4953 | ||
fbf5a39b | 4954 | Generate_Operator_Reference (N, Typ); |
dec6faf1 | 4955 | Analyze_Dimension (N); |
996ae0b0 RK |
4956 | Eval_Arithmetic_Op (N); |
4957 | ||
2ba431e5 YM |
4958 | -- In SPARK, a multiplication or division with operands of fixed point |
4959 | -- types shall be qualified or explicitly converted to identify the | |
4960 | -- result type. | |
b0186f71 | 4961 | |
fe5d3068 YM |
4962 | if (Is_Fixed_Point_Type (Etype (L)) |
4963 | or else Is_Fixed_Point_Type (Etype (R))) | |
b0186f71 AC |
4964 | and then Nkind_In (N, N_Op_Multiply, N_Op_Divide) |
4965 | and then | |
4966 | not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion) | |
4967 | then | |
2ba431e5 | 4968 | Check_SPARK_Restriction |
fe5d3068 | 4969 | ("operation should be qualified or explicitly converted", N); |
b0186f71 AC |
4970 | end if; |
4971 | ||
996ae0b0 RK |
4972 | -- Set overflow and division checking bit. Much cleverer code needed |
4973 | -- here eventually and perhaps the Resolve routines should be separated | |
4974 | -- for the various arithmetic operations, since they will need | |
4975 | -- different processing. ??? | |
4976 | ||
4977 | if Nkind (N) in N_Op then | |
4978 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 4979 | Enable_Overflow_Check (N); |
996ae0b0 RK |
4980 | end if; |
4981 | ||
fbf5a39b AC |
4982 | -- Give warning if explicit division by zero |
4983 | ||
45fc7ddb | 4984 | if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod) |
996ae0b0 RK |
4985 | and then not Division_Checks_Suppressed (Etype (N)) |
4986 | then | |
fbf5a39b AC |
4987 | Rop := Right_Opnd (N); |
4988 | ||
4989 | if Compile_Time_Known_Value (Rop) | |
4990 | and then ((Is_Integer_Type (Etype (Rop)) | |
780d052e RD |
4991 | and then Expr_Value (Rop) = Uint_0) |
4992 | or else | |
4993 | (Is_Real_Type (Etype (Rop)) | |
4994 | and then Expr_Value_R (Rop) = Ureal_0)) | |
fbf5a39b | 4995 | then |
ce72a9a3 AC |
4996 | -- Specialize the warning message according to the operation. |
4997 | -- The following warnings are for the case | |
aa180613 RD |
4998 | |
4999 | case Nkind (N) is | |
5000 | when N_Op_Divide => | |
ce72a9a3 AC |
5001 | |
5002 | -- For division, we have two cases, for float division | |
5003 | -- of an unconstrained float type, on a machine where | |
5004 | -- Machine_Overflows is false, we don't get an exception | |
5005 | -- at run-time, but rather an infinity or Nan. The Nan | |
5006 | -- case is pretty obscure, so just warn about infinities. | |
5007 | ||
5008 | if Is_Floating_Point_Type (Typ) | |
5009 | and then not Is_Constrained (Typ) | |
5010 | and then not Machine_Overflows_On_Target | |
5011 | then | |
5012 | Error_Msg_N | |
5013 | ("float division by zero, " & | |
5014 | "may generate '+'/'- infinity?", Right_Opnd (N)); | |
5015 | ||
5016 | -- For all other cases, we get a Constraint_Error | |
5017 | ||
5018 | else | |
5019 | Apply_Compile_Time_Constraint_Error | |
5020 | (N, "division by zero?", CE_Divide_By_Zero, | |
5021 | Loc => Sloc (Right_Opnd (N))); | |
5022 | end if; | |
aa180613 RD |
5023 | |
5024 | when N_Op_Rem => | |
5025 | Apply_Compile_Time_Constraint_Error | |
5026 | (N, "rem with zero divisor?", CE_Divide_By_Zero, | |
5027 | Loc => Sloc (Right_Opnd (N))); | |
5028 | ||
5029 | when N_Op_Mod => | |
5030 | Apply_Compile_Time_Constraint_Error | |
5031 | (N, "mod with zero divisor?", CE_Divide_By_Zero, | |
5032 | Loc => Sloc (Right_Opnd (N))); | |
5033 | ||
5034 | -- Division by zero can only happen with division, rem, | |
5035 | -- and mod operations. | |
5036 | ||
5037 | when others => | |
5038 | raise Program_Error; | |
5039 | end case; | |
fbf5a39b AC |
5040 | |
5041 | -- Otherwise just set the flag to check at run time | |
5042 | ||
5043 | else | |
b7d1f17f | 5044 | Activate_Division_Check (N); |
fbf5a39b | 5045 | end if; |
996ae0b0 | 5046 | end if; |
45fc7ddb HK |
5047 | |
5048 | -- If Restriction No_Implicit_Conditionals is active, then it is | |
5049 | -- violated if either operand can be negative for mod, or for rem | |
5050 | -- if both operands can be negative. | |
5051 | ||
7a963087 | 5052 | if Restriction_Check_Required (No_Implicit_Conditionals) |
45fc7ddb HK |
5053 | and then Nkind_In (N, N_Op_Rem, N_Op_Mod) |
5054 | then | |
5055 | declare | |
5056 | Lo : Uint; | |
5057 | Hi : Uint; | |
5058 | OK : Boolean; | |
5059 | ||
5060 | LNeg : Boolean; | |
5061 | RNeg : Boolean; | |
5062 | -- Set if corresponding operand might be negative | |
5063 | ||
5064 | begin | |
5d5e9775 AC |
5065 | Determine_Range |
5066 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
5067 | LNeg := (not OK) or else Lo < 0; |
5068 | ||
5d5e9775 AC |
5069 | Determine_Range |
5070 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
45fc7ddb HK |
5071 | RNeg := (not OK) or else Lo < 0; |
5072 | ||
5d5e9775 AC |
5073 | -- Check if we will be generating conditionals. There are two |
5074 | -- cases where that can happen, first for REM, the only case | |
5075 | -- is largest negative integer mod -1, where the division can | |
5076 | -- overflow, but we still have to give the right result. The | |
5077 | -- front end generates a test for this annoying case. Here we | |
5078 | -- just test if both operands can be negative (that's what the | |
5079 | -- expander does, so we match its logic here). | |
5080 | ||
5081 | -- The second case is mod where either operand can be negative. | |
308e6f3a | 5082 | -- In this case, the back end has to generate additional tests. |
5d5e9775 | 5083 | |
45fc7ddb HK |
5084 | if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg)) |
5085 | or else | |
5086 | (Nkind (N) = N_Op_Mod and then (LNeg or RNeg)) | |
5087 | then | |
5088 | Check_Restriction (No_Implicit_Conditionals, N); | |
5089 | end if; | |
5090 | end; | |
5091 | end if; | |
996ae0b0 RK |
5092 | end if; |
5093 | ||
5094 | Check_Unset_Reference (L); | |
5095 | Check_Unset_Reference (R); | |
996ae0b0 RK |
5096 | end Resolve_Arithmetic_Op; |
5097 | ||
5098 | ------------------ | |
5099 | -- Resolve_Call -- | |
5100 | ------------------ | |
5101 | ||
5102 | procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is | |
5103 | Loc : constant Source_Ptr := Sloc (N); | |
5104 | Subp : constant Node_Id := Name (N); | |
5105 | Nam : Entity_Id; | |
5106 | I : Interp_Index; | |
5107 | It : Interp; | |
5108 | Norm_OK : Boolean; | |
5109 | Scop : Entity_Id; | |
aa180613 | 5110 | Rtype : Entity_Id; |
996ae0b0 | 5111 | |
ee81cbe9 AC |
5112 | function Same_Or_Aliased_Subprograms |
5113 | (S : Entity_Id; | |
5114 | E : Entity_Id) return Boolean; | |
5115 | -- Returns True if the subprogram entity S is the same as E or else | |
5116 | -- S is an alias of E. | |
5117 | ||
001c7783 AC |
5118 | --------------------------------- |
5119 | -- Same_Or_Aliased_Subprograms -- | |
5120 | --------------------------------- | |
5121 | ||
ee81cbe9 AC |
5122 | function Same_Or_Aliased_Subprograms |
5123 | (S : Entity_Id; | |
5124 | E : Entity_Id) return Boolean | |
5125 | is | |
5126 | Subp_Alias : constant Entity_Id := Alias (S); | |
ee81cbe9 AC |
5127 | begin |
5128 | return S = E | |
5129 | or else (Present (Subp_Alias) and then Subp_Alias = E); | |
5130 | end Same_Or_Aliased_Subprograms; | |
5131 | ||
5132 | -- Start of processing for Resolve_Call | |
5133 | ||
996ae0b0 | 5134 | begin |
758c442c GD |
5135 | -- The context imposes a unique interpretation with type Typ on a |
5136 | -- procedure or function call. Find the entity of the subprogram that | |
5137 | -- yields the expected type, and propagate the corresponding formal | |
5138 | -- constraints on the actuals. The caller has established that an | |
5139 | -- interpretation exists, and emitted an error if not unique. | |
996ae0b0 RK |
5140 | |
5141 | -- First deal with the case of a call to an access-to-subprogram, | |
5142 | -- dereference made explicit in Analyze_Call. | |
5143 | ||
5144 | if Ekind (Etype (Subp)) = E_Subprogram_Type then | |
996ae0b0 RK |
5145 | if not Is_Overloaded (Subp) then |
5146 | Nam := Etype (Subp); | |
5147 | ||
5148 | else | |
758c442c GD |
5149 | -- Find the interpretation whose type (a subprogram type) has a |
5150 | -- return type that is compatible with the context. Analysis of | |
5151 | -- the node has established that one exists. | |
996ae0b0 | 5152 | |
996ae0b0 RK |
5153 | Nam := Empty; |
5154 | ||
1420b484 | 5155 | Get_First_Interp (Subp, I, It); |
996ae0b0 | 5156 | while Present (It.Typ) loop |
996ae0b0 RK |
5157 | if Covers (Typ, Etype (It.Typ)) then |
5158 | Nam := It.Typ; | |
5159 | exit; | |
5160 | end if; | |
5161 | ||
5162 | Get_Next_Interp (I, It); | |
5163 | end loop; | |
5164 | ||
5165 | if No (Nam) then | |
5166 | raise Program_Error; | |
5167 | end if; | |
5168 | end if; | |
5169 | ||
5170 | -- If the prefix is not an entity, then resolve it | |
5171 | ||
5172 | if not Is_Entity_Name (Subp) then | |
5173 | Resolve (Subp, Nam); | |
5174 | end if; | |
5175 | ||
758c442c GD |
5176 | -- For an indirect call, we always invalidate checks, since we do not |
5177 | -- know whether the subprogram is local or global. Yes we could do | |
5178 | -- better here, e.g. by knowing that there are no local subprograms, | |
aa180613 | 5179 | -- but it does not seem worth the effort. Similarly, we kill all |
758c442c | 5180 | -- knowledge of current constant values. |
fbf5a39b AC |
5181 | |
5182 | Kill_Current_Values; | |
5183 | ||
b7d1f17f HK |
5184 | -- If this is a procedure call which is really an entry call, do |
5185 | -- the conversion of the procedure call to an entry call. Protected | |
5186 | -- operations use the same circuitry because the name in the call | |
5187 | -- can be an arbitrary expression with special resolution rules. | |
996ae0b0 | 5188 | |
45fc7ddb | 5189 | elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component) |
996ae0b0 RK |
5190 | or else (Is_Entity_Name (Subp) |
5191 | and then Ekind (Entity (Subp)) = E_Entry) | |
5192 | then | |
5193 | Resolve_Entry_Call (N, Typ); | |
5194 | Check_Elab_Call (N); | |
fbf5a39b AC |
5195 | |
5196 | -- Kill checks and constant values, as above for indirect case | |
5197 | -- Who knows what happens when another task is activated? | |
5198 | ||
5199 | Kill_Current_Values; | |
996ae0b0 RK |
5200 | return; |
5201 | ||
5202 | -- Normal subprogram call with name established in Resolve | |
5203 | ||
5204 | elsif not (Is_Type (Entity (Subp))) then | |
5205 | Nam := Entity (Subp); | |
5206 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
5207 | |
5208 | -- Otherwise we must have the case of an overloaded call | |
5209 | ||
5210 | else | |
5211 | pragma Assert (Is_Overloaded (Subp)); | |
d81b4bfe TQ |
5212 | |
5213 | -- Initialize Nam to prevent warning (we know it will be assigned | |
5214 | -- in the loop below, but the compiler does not know that). | |
5215 | ||
5216 | Nam := Empty; | |
996ae0b0 RK |
5217 | |
5218 | Get_First_Interp (Subp, I, It); | |
996ae0b0 RK |
5219 | while Present (It.Typ) loop |
5220 | if Covers (Typ, It.Typ) then | |
5221 | Nam := It.Nam; | |
5222 | Set_Entity_With_Style_Check (Subp, Nam); | |
996ae0b0 RK |
5223 | exit; |
5224 | end if; | |
5225 | ||
5226 | Get_Next_Interp (I, It); | |
5227 | end loop; | |
5228 | end if; | |
5229 | ||
c9b99571 ES |
5230 | if Is_Access_Subprogram_Type (Base_Type (Etype (Nam))) |
5231 | and then not Is_Access_Subprogram_Type (Base_Type (Typ)) | |
53cf4600 ES |
5232 | and then Nkind (Subp) /= N_Explicit_Dereference |
5233 | and then Present (Parameter_Associations (N)) | |
5234 | then | |
66aa7643 TQ |
5235 | -- The prefix is a parameterless function call that returns an access |
5236 | -- to subprogram. If parameters are present in the current call, add | |
5237 | -- add an explicit dereference. We use the base type here because | |
5238 | -- within an instance these may be subtypes. | |
53cf4600 ES |
5239 | |
5240 | -- The dereference is added either in Analyze_Call or here. Should | |
5241 | -- be consolidated ??? | |
5242 | ||
5243 | Set_Is_Overloaded (Subp, False); | |
5244 | Set_Etype (Subp, Etype (Nam)); | |
5245 | Insert_Explicit_Dereference (Subp); | |
5246 | Nam := Designated_Type (Etype (Nam)); | |
5247 | Resolve (Subp, Nam); | |
5248 | end if; | |
5249 | ||
996ae0b0 RK |
5250 | -- Check that a call to Current_Task does not occur in an entry body |
5251 | ||
5252 | if Is_RTE (Nam, RE_Current_Task) then | |
5253 | declare | |
5254 | P : Node_Id; | |
5255 | ||
5256 | begin | |
5257 | P := N; | |
5258 | loop | |
5259 | P := Parent (P); | |
45fc7ddb HK |
5260 | |
5261 | -- Exclude calls that occur within the default of a formal | |
5262 | -- parameter of the entry, since those are evaluated outside | |
5263 | -- of the body. | |
5264 | ||
5265 | exit when No (P) or else Nkind (P) = N_Parameter_Specification; | |
996ae0b0 | 5266 | |
aa180613 RD |
5267 | if Nkind (P) = N_Entry_Body |
5268 | or else (Nkind (P) = N_Subprogram_Body | |
45fc7ddb | 5269 | and then Is_Entry_Barrier_Function (P)) |
aa180613 RD |
5270 | then |
5271 | Rtype := Etype (N); | |
996ae0b0 | 5272 | Error_Msg_NE |
aa5147f0 | 5273 | ("?& should not be used in entry body (RM C.7(17))", |
996ae0b0 | 5274 | N, Nam); |
aa180613 RD |
5275 | Error_Msg_NE |
5276 | ("\Program_Error will be raised at run time?", N, Nam); | |
5277 | Rewrite (N, | |
5278 | Make_Raise_Program_Error (Loc, | |
5279 | Reason => PE_Current_Task_In_Entry_Body)); | |
5280 | Set_Etype (N, Rtype); | |
e65f50ec | 5281 | return; |
996ae0b0 RK |
5282 | end if; |
5283 | end loop; | |
5284 | end; | |
5285 | end if; | |
5286 | ||
758c442c GD |
5287 | -- Check that a procedure call does not occur in the context of the |
5288 | -- entry call statement of a conditional or timed entry call. Note that | |
5289 | -- the case of a call to a subprogram renaming of an entry will also be | |
5290 | -- rejected. The test for N not being an N_Entry_Call_Statement is | |
5291 | -- defensive, covering the possibility that the processing of entry | |
5292 | -- calls might reach this point due to later modifications of the code | |
5293 | -- above. | |
996ae0b0 RK |
5294 | |
5295 | if Nkind (Parent (N)) = N_Entry_Call_Alternative | |
5296 | and then Nkind (N) /= N_Entry_Call_Statement | |
5297 | and then Entry_Call_Statement (Parent (N)) = N | |
5298 | then | |
0791fbe9 | 5299 | if Ada_Version < Ada_2005 then |
1420b484 JM |
5300 | Error_Msg_N ("entry call required in select statement", N); |
5301 | ||
5302 | -- Ada 2005 (AI-345): If a procedure_call_statement is used | |
66aa7643 TQ |
5303 | -- for a procedure_or_entry_call, the procedure_name or |
5304 | -- procedure_prefix of the procedure_call_statement shall denote | |
1420b484 JM |
5305 | -- an entry renamed by a procedure, or (a view of) a primitive |
5306 | -- subprogram of a limited interface whose first parameter is | |
5307 | -- a controlling parameter. | |
5308 | ||
5309 | elsif Nkind (N) = N_Procedure_Call_Statement | |
5310 | and then not Is_Renamed_Entry (Nam) | |
5311 | and then not Is_Controlling_Limited_Procedure (Nam) | |
5312 | then | |
5313 | Error_Msg_N | |
c8ef728f | 5314 | ("entry call or dispatching primitive of interface required", N); |
1420b484 | 5315 | end if; |
996ae0b0 RK |
5316 | end if; |
5317 | ||
66aa7643 TQ |
5318 | -- Check that this is not a call to a protected procedure or entry from |
5319 | -- within a protected function. | |
fbf5a39b AC |
5320 | |
5321 | if Ekind (Current_Scope) = E_Function | |
5322 | and then Ekind (Scope (Current_Scope)) = E_Protected_Type | |
5323 | and then Ekind (Nam) /= E_Function | |
5324 | and then Scope (Nam) = Scope (Current_Scope) | |
5325 | then | |
5326 | Error_Msg_N ("within protected function, protected " & | |
5327 | "object is constant", N); | |
5328 | Error_Msg_N ("\cannot call operation that may modify it", N); | |
5329 | end if; | |
5330 | ||
45fc7ddb | 5331 | -- Freeze the subprogram name if not in a spec-expression. Note that we |
758c442c GD |
5332 | -- freeze procedure calls as well as function calls. Procedure calls are |
5333 | -- not frozen according to the rules (RM 13.14(14)) because it is | |
5334 | -- impossible to have a procedure call to a non-frozen procedure in pure | |
5335 | -- Ada, but in the code that we generate in the expander, this rule | |
5336 | -- needs extending because we can generate procedure calls that need | |
5337 | -- freezing. | |
996ae0b0 | 5338 | |
a429e6b3 AC |
5339 | -- In Ada 2012, expression functions may be called within pre/post |
5340 | -- conditions of subsequent functions or expression functions. Such | |
5341 | -- calls do not freeze when they appear within generated bodies, which | |
5342 | -- would place the freeze node in the wrong scope. An expression | |
5343 | -- function is frozen in the usual fashion, by the appearance of a real | |
5344 | -- body, or at the end of a declarative part. | |
5345 | ||
5346 | if Is_Entity_Name (Subp) and then not In_Spec_Expression | |
5347 | and then | |
5348 | (not Is_Expression_Function (Entity (Subp)) | |
5349 | or else Scope (Entity (Subp)) = Current_Scope) | |
5350 | then | |
996ae0b0 RK |
5351 | Freeze_Expression (Subp); |
5352 | end if; | |
5353 | ||
758c442c GD |
5354 | -- For a predefined operator, the type of the result is the type imposed |
5355 | -- by context, except for a predefined operation on universal fixed. | |
5356 | -- Otherwise The type of the call is the type returned by the subprogram | |
5357 | -- being called. | |
996ae0b0 RK |
5358 | |
5359 | if Is_Predefined_Op (Nam) then | |
996ae0b0 RK |
5360 | if Etype (N) /= Universal_Fixed then |
5361 | Set_Etype (N, Typ); | |
5362 | end if; | |
5363 | ||
758c442c GD |
5364 | -- If the subprogram returns an array type, and the context requires the |
5365 | -- component type of that array type, the node is really an indexing of | |
5366 | -- the parameterless call. Resolve as such. A pathological case occurs | |
5367 | -- when the type of the component is an access to the array type. In | |
5368 | -- this case the call is truly ambiguous. | |
996ae0b0 | 5369 | |
0669bebe | 5370 | elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam)) |
996ae0b0 RK |
5371 | and then |
5372 | ((Is_Array_Type (Etype (Nam)) | |
19fb051c | 5373 | and then Covers (Typ, Component_Type (Etype (Nam)))) |
996ae0b0 | 5374 | or else (Is_Access_Type (Etype (Nam)) |
19fb051c AC |
5375 | and then Is_Array_Type (Designated_Type (Etype (Nam))) |
5376 | and then | |
5377 | Covers | |
5378 | (Typ, | |
5379 | Component_Type (Designated_Type (Etype (Nam)))))) | |
996ae0b0 RK |
5380 | then |
5381 | declare | |
5382 | Index_Node : Node_Id; | |
fbf5a39b AC |
5383 | New_Subp : Node_Id; |
5384 | Ret_Type : constant Entity_Id := Etype (Nam); | |
996ae0b0 RK |
5385 | |
5386 | begin | |
fbf5a39b AC |
5387 | if Is_Access_Type (Ret_Type) |
5388 | and then Ret_Type = Component_Type (Designated_Type (Ret_Type)) | |
5389 | then | |
5390 | Error_Msg_N | |
5391 | ("cannot disambiguate function call and indexing", N); | |
5392 | else | |
5393 | New_Subp := Relocate_Node (Subp); | |
5394 | Set_Entity (Subp, Nam); | |
5395 | ||
7205254b | 5396 | if (Is_Array_Type (Ret_Type) |
5d5e9775 | 5397 | and then Component_Type (Ret_Type) /= Any_Type) |
7205254b JM |
5398 | or else |
5399 | (Is_Access_Type (Ret_Type) | |
5d5e9775 AC |
5400 | and then |
5401 | Component_Type (Designated_Type (Ret_Type)) /= Any_Type) | |
7205254b | 5402 | then |
0669bebe GB |
5403 | if Needs_No_Actuals (Nam) then |
5404 | ||
5405 | -- Indexed call to a parameterless function | |
5406 | ||
5407 | Index_Node := | |
5408 | Make_Indexed_Component (Loc, | |
5409 | Prefix => | |
5410 | Make_Function_Call (Loc, | |
5411 | Name => New_Subp), | |
5412 | Expressions => Parameter_Associations (N)); | |
5413 | else | |
5414 | -- An Ada 2005 prefixed call to a primitive operation | |
5415 | -- whose first parameter is the prefix. This prefix was | |
5416 | -- prepended to the parameter list, which is actually a | |
3b42c566 | 5417 | -- list of indexes. Remove the prefix in order to build |
0669bebe GB |
5418 | -- the proper indexed component. |
5419 | ||
5420 | Index_Node := | |
5421 | Make_Indexed_Component (Loc, | |
5422 | Prefix => | |
5423 | Make_Function_Call (Loc, | |
5424 | Name => New_Subp, | |
5425 | Parameter_Associations => | |
5426 | New_List | |
5427 | (Remove_Head (Parameter_Associations (N)))), | |
5428 | Expressions => Parameter_Associations (N)); | |
5429 | end if; | |
fbf5a39b | 5430 | |
74e7891f RD |
5431 | -- Preserve the parenthesis count of the node |
5432 | ||
5433 | Set_Paren_Count (Index_Node, Paren_Count (N)); | |
5434 | ||
fbf5a39b AC |
5435 | -- Since we are correcting a node classification error made |
5436 | -- by the parser, we call Replace rather than Rewrite. | |
5437 | ||
5438 | Replace (N, Index_Node); | |
74e7891f | 5439 | |
fbf5a39b AC |
5440 | Set_Etype (Prefix (N), Ret_Type); |
5441 | Set_Etype (N, Typ); | |
5442 | Resolve_Indexed_Component (N, Typ); | |
5443 | Check_Elab_Call (Prefix (N)); | |
5444 | end if; | |
996ae0b0 RK |
5445 | end if; |
5446 | ||
5447 | return; | |
5448 | end; | |
5449 | ||
5450 | else | |
5451 | Set_Etype (N, Etype (Nam)); | |
5452 | end if; | |
5453 | ||
5454 | -- In the case where the call is to an overloaded subprogram, Analyze | |
5455 | -- calls Normalize_Actuals once per overloaded subprogram. Therefore in | |
5456 | -- such a case Normalize_Actuals needs to be called once more to order | |
5457 | -- the actuals correctly. Otherwise the call will have the ordering | |
5458 | -- given by the last overloaded subprogram whether this is the correct | |
5459 | -- one being called or not. | |
5460 | ||
5461 | if Is_Overloaded (Subp) then | |
5462 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
5463 | pragma Assert (Norm_OK); | |
5464 | end if; | |
5465 | ||
5466 | -- In any case, call is fully resolved now. Reset Overload flag, to | |
5467 | -- prevent subsequent overload resolution if node is analyzed again | |
5468 | ||
5469 | Set_Is_Overloaded (Subp, False); | |
5470 | Set_Is_Overloaded (N, False); | |
5471 | ||
758c442c GD |
5472 | -- If we are calling the current subprogram from immediately within its |
5473 | -- body, then that is the case where we can sometimes detect cases of | |
5474 | -- infinite recursion statically. Do not try this in case restriction | |
b7d1f17f | 5475 | -- No_Recursion is in effect anyway, and do it only for source calls. |
996ae0b0 | 5476 | |
b7d1f17f HK |
5477 | if Comes_From_Source (N) then |
5478 | Scop := Current_Scope; | |
996ae0b0 | 5479 | |
26570b21 RD |
5480 | -- Issue warning for possible infinite recursion in the absence |
5481 | -- of the No_Recursion restriction. | |
5482 | ||
ee81cbe9 | 5483 | if Same_Or_Aliased_Subprograms (Nam, Scop) |
b7d1f17f HK |
5484 | and then not Restriction_Active (No_Recursion) |
5485 | and then Check_Infinite_Recursion (N) | |
5486 | then | |
5487 | -- Here we detected and flagged an infinite recursion, so we do | |
da20aa43 RD |
5488 | -- not need to test the case below for further warnings. Also we |
5489 | -- are all done if we now have a raise SE node. | |
996ae0b0 | 5490 | |
26570b21 RD |
5491 | if Nkind (N) = N_Raise_Storage_Error then |
5492 | return; | |
5493 | end if; | |
996ae0b0 | 5494 | |
26570b21 RD |
5495 | -- If call is to immediately containing subprogram, then check for |
5496 | -- the case of a possible run-time detectable infinite recursion. | |
996ae0b0 | 5497 | |
b7d1f17f HK |
5498 | else |
5499 | Scope_Loop : while Scop /= Standard_Standard loop | |
ee81cbe9 | 5500 | if Same_Or_Aliased_Subprograms (Nam, Scop) then |
b7d1f17f HK |
5501 | |
5502 | -- Although in general case, recursion is not statically | |
5503 | -- checkable, the case of calling an immediately containing | |
5504 | -- subprogram is easy to catch. | |
5505 | ||
5506 | Check_Restriction (No_Recursion, N); | |
5507 | ||
5508 | -- If the recursive call is to a parameterless subprogram, | |
5509 | -- then even if we can't statically detect infinite | |
5510 | -- recursion, this is pretty suspicious, and we output a | |
5511 | -- warning. Furthermore, we will try later to detect some | |
5512 | -- cases here at run time by expanding checking code (see | |
5513 | -- Detect_Infinite_Recursion in package Exp_Ch6). | |
5514 | ||
5515 | -- If the recursive call is within a handler, do not emit a | |
5516 | -- warning, because this is a common idiom: loop until input | |
5517 | -- is correct, catch illegal input in handler and restart. | |
5518 | ||
5519 | if No (First_Formal (Nam)) | |
5520 | and then Etype (Nam) = Standard_Void_Type | |
5521 | and then not Error_Posted (N) | |
5522 | and then Nkind (Parent (N)) /= N_Exception_Handler | |
aa180613 | 5523 | then |
b7d1f17f HK |
5524 | -- For the case of a procedure call. We give the message |
5525 | -- only if the call is the first statement in a sequence | |
5526 | -- of statements, or if all previous statements are | |
5527 | -- simple assignments. This is simply a heuristic to | |
5528 | -- decrease false positives, without losing too many good | |
5529 | -- warnings. The idea is that these previous statements | |
5530 | -- may affect global variables the procedure depends on. | |
78efd712 AC |
5531 | -- We also exclude raise statements, that may arise from |
5532 | -- constraint checks and are probably unrelated to the | |
5533 | -- intended control flow. | |
b7d1f17f HK |
5534 | |
5535 | if Nkind (N) = N_Procedure_Call_Statement | |
5536 | and then Is_List_Member (N) | |
5537 | then | |
5538 | declare | |
5539 | P : Node_Id; | |
5540 | begin | |
5541 | P := Prev (N); | |
5542 | while Present (P) loop | |
78efd712 AC |
5543 | if not Nkind_In (P, |
5544 | N_Assignment_Statement, | |
5545 | N_Raise_Constraint_Error) | |
5546 | then | |
b7d1f17f HK |
5547 | exit Scope_Loop; |
5548 | end if; | |
5549 | ||
5550 | Prev (P); | |
5551 | end loop; | |
5552 | end; | |
5553 | end if; | |
5554 | ||
5555 | -- Do not give warning if we are in a conditional context | |
5556 | ||
aa180613 | 5557 | declare |
b7d1f17f | 5558 | K : constant Node_Kind := Nkind (Parent (N)); |
aa180613 | 5559 | begin |
b7d1f17f | 5560 | if (K = N_Loop_Statement |
b5c739f9 | 5561 | and then Present (Iteration_Scheme (Parent (N)))) |
b7d1f17f HK |
5562 | or else K = N_If_Statement |
5563 | or else K = N_Elsif_Part | |
5564 | or else K = N_Case_Statement_Alternative | |
5565 | then | |
5566 | exit Scope_Loop; | |
5567 | end if; | |
aa180613 | 5568 | end; |
aa180613 | 5569 | |
b7d1f17f | 5570 | -- Here warning is to be issued |
aa180613 | 5571 | |
b7d1f17f HK |
5572 | Set_Has_Recursive_Call (Nam); |
5573 | Error_Msg_N | |
aa5147f0 | 5574 | ("?possible infinite recursion!", N); |
b7d1f17f | 5575 | Error_Msg_N |
aa5147f0 | 5576 | ("\?Storage_Error may be raised at run time!", N); |
b7d1f17f | 5577 | end if; |
aa180613 | 5578 | |
b7d1f17f | 5579 | exit Scope_Loop; |
996ae0b0 RK |
5580 | end if; |
5581 | ||
b7d1f17f HK |
5582 | Scop := Scope (Scop); |
5583 | end loop Scope_Loop; | |
5584 | end if; | |
996ae0b0 RK |
5585 | end if; |
5586 | ||
b5c739f9 RD |
5587 | -- Check obsolescent reference to Ada.Characters.Handling subprogram |
5588 | ||
5589 | Check_Obsolescent_2005_Entity (Nam, Subp); | |
5590 | ||
996ae0b0 RK |
5591 | -- If subprogram name is a predefined operator, it was given in |
5592 | -- functional notation. Replace call node with operator node, so | |
5593 | -- that actuals can be resolved appropriately. | |
5594 | ||
5595 | if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then | |
5596 | Make_Call_Into_Operator (N, Typ, Entity (Name (N))); | |
5597 | return; | |
5598 | ||
5599 | elsif Present (Alias (Nam)) | |
5600 | and then Is_Predefined_Op (Alias (Nam)) | |
5601 | then | |
5602 | Resolve_Actuals (N, Nam); | |
5603 | Make_Call_Into_Operator (N, Typ, Alias (Nam)); | |
5604 | return; | |
5605 | end if; | |
5606 | ||
fbf5a39b AC |
5607 | -- Create a transient scope if the resulting type requires it |
5608 | ||
4017021b AC |
5609 | -- There are several notable exceptions: |
5610 | ||
4d2907fd | 5611 | -- a) In init procs, the transient scope overhead is not needed, and is |
4017021b AC |
5612 | -- even incorrect when the call is a nested initialization call for a |
5613 | -- component whose expansion may generate adjust calls. However, if the | |
5614 | -- call is some other procedure call within an initialization procedure | |
5615 | -- (for example a call to Create_Task in the init_proc of the task | |
5616 | -- run-time record) a transient scope must be created around this call. | |
5617 | ||
4d2907fd | 5618 | -- b) Enumeration literal pseudo-calls need no transient scope |
4017021b | 5619 | |
4d2907fd | 5620 | -- c) Intrinsic subprograms (Unchecked_Conversion and source info |
4017021b | 5621 | -- functions) do not use the secondary stack even though the return |
4d2907fd | 5622 | -- type may be unconstrained. |
4017021b | 5623 | |
4d2907fd | 5624 | -- d) Calls to a build-in-place function, since such functions may |
4017021b AC |
5625 | -- allocate their result directly in a target object, and cases where |
5626 | -- the result does get allocated in the secondary stack are checked for | |
5627 | -- within the specialized Exp_Ch6 procedures for expanding those | |
5628 | -- build-in-place calls. | |
5629 | ||
5630 | -- e) If the subprogram is marked Inline_Always, then even if it returns | |
c8ef728f | 5631 | -- an unconstrained type the call does not require use of the secondary |
45fc7ddb HK |
5632 | -- stack. However, inlining will only take place if the body to inline |
5633 | -- is already present. It may not be available if e.g. the subprogram is | |
5634 | -- declared in a child instance. | |
c8ef728f | 5635 | |
4017021b AC |
5636 | -- If this is an initialization call for a type whose construction |
5637 | -- uses the secondary stack, and it is not a nested call to initialize | |
5638 | -- a component, we do need to create a transient scope for it. We | |
5639 | -- check for this by traversing the type in Check_Initialization_Call. | |
5640 | ||
c8ef728f | 5641 | if Is_Inlined (Nam) |
45fc7ddb HK |
5642 | and then Has_Pragma_Inline_Always (Nam) |
5643 | and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration | |
5644 | and then Present (Body_To_Inline (Unit_Declaration_Node (Nam))) | |
84f4072a JM |
5645 | and then not Debug_Flag_Dot_K |
5646 | then | |
5647 | null; | |
5648 | ||
5649 | elsif Is_Inlined (Nam) | |
5650 | and then Has_Pragma_Inline (Nam) | |
5651 | and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration | |
5652 | and then Present (Body_To_Inline (Unit_Declaration_Node (Nam))) | |
5653 | and then Debug_Flag_Dot_K | |
c8ef728f ES |
5654 | then |
5655 | null; | |
5656 | ||
4017021b AC |
5657 | elsif Ekind (Nam) = E_Enumeration_Literal |
5658 | or else Is_Build_In_Place_Function (Nam) | |
5659 | or else Is_Intrinsic_Subprogram (Nam) | |
5660 | then | |
5661 | null; | |
5662 | ||
da94696d | 5663 | elsif Full_Expander_Active |
996ae0b0 RK |
5664 | and then Is_Type (Etype (Nam)) |
5665 | and then Requires_Transient_Scope (Etype (Nam)) | |
4017021b AC |
5666 | and then |
5667 | (not Within_Init_Proc | |
5668 | or else | |
5669 | (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function)) | |
996ae0b0 | 5670 | then |
0669bebe | 5671 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 5672 | |
a9f4e3d2 AC |
5673 | -- If the call appears within the bounds of a loop, it will |
5674 | -- be rewritten and reanalyzed, nothing left to do here. | |
5675 | ||
5676 | if Nkind (N) /= N_Function_Call then | |
5677 | return; | |
5678 | end if; | |
5679 | ||
fbf5a39b | 5680 | elsif Is_Init_Proc (Nam) |
996ae0b0 RK |
5681 | and then not Within_Init_Proc |
5682 | then | |
5683 | Check_Initialization_Call (N, Nam); | |
5684 | end if; | |
5685 | ||
5686 | -- A protected function cannot be called within the definition of the | |
5687 | -- enclosing protected type. | |
5688 | ||
5689 | if Is_Protected_Type (Scope (Nam)) | |
5690 | and then In_Open_Scopes (Scope (Nam)) | |
5691 | and then not Has_Completion (Scope (Nam)) | |
5692 | then | |
5693 | Error_Msg_NE | |
5694 | ("& cannot be called before end of protected definition", N, Nam); | |
5695 | end if; | |
5696 | ||
5697 | -- Propagate interpretation to actuals, and add default expressions | |
5698 | -- where needed. | |
5699 | ||
5700 | if Present (First_Formal (Nam)) then | |
5701 | Resolve_Actuals (N, Nam); | |
5702 | ||
d81b4bfe TQ |
5703 | -- Overloaded literals are rewritten as function calls, for purpose of |
5704 | -- resolution. After resolution, we can replace the call with the | |
5705 | -- literal itself. | |
996ae0b0 RK |
5706 | |
5707 | elsif Ekind (Nam) = E_Enumeration_Literal then | |
5708 | Copy_Node (Subp, N); | |
5709 | Resolve_Entity_Name (N, Typ); | |
5710 | ||
fbf5a39b | 5711 | -- Avoid validation, since it is a static function call |
996ae0b0 | 5712 | |
e65f50ec | 5713 | Generate_Reference (Nam, Subp); |
996ae0b0 RK |
5714 | return; |
5715 | end if; | |
5716 | ||
b7d1f17f HK |
5717 | -- If the subprogram is not global, then kill all saved values and |
5718 | -- checks. This is a bit conservative, since in many cases we could do | |
5719 | -- better, but it is not worth the effort. Similarly, we kill constant | |
5720 | -- values. However we do not need to do this for internal entities | |
5721 | -- (unless they are inherited user-defined subprograms), since they | |
5722 | -- are not in the business of molesting local values. | |
5723 | ||
5724 | -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also | |
5725 | -- kill all checks and values for calls to global subprograms. This | |
5726 | -- takes care of the case where an access to a local subprogram is | |
5727 | -- taken, and could be passed directly or indirectly and then called | |
5728 | -- from almost any context. | |
aa180613 RD |
5729 | |
5730 | -- Note: we do not do this step till after resolving the actuals. That | |
5731 | -- way we still take advantage of the current value information while | |
5732 | -- scanning the actuals. | |
5733 | ||
45fc7ddb HK |
5734 | -- We suppress killing values if we are processing the nodes associated |
5735 | -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged | |
5736 | -- type kills all the values as part of analyzing the code that | |
5737 | -- initializes the dispatch tables. | |
5738 | ||
5739 | if Inside_Freezing_Actions = 0 | |
5740 | and then (not Is_Library_Level_Entity (Nam) | |
24357840 RD |
5741 | or else Suppress_Value_Tracking_On_Call |
5742 | (Nearest_Dynamic_Scope (Current_Scope))) | |
aa180613 RD |
5743 | and then (Comes_From_Source (Nam) |
5744 | or else (Present (Alias (Nam)) | |
5745 | and then Comes_From_Source (Alias (Nam)))) | |
5746 | then | |
5747 | Kill_Current_Values; | |
5748 | end if; | |
5749 | ||
36fcf362 RD |
5750 | -- If we are warning about unread OUT parameters, this is the place to |
5751 | -- set Last_Assignment for OUT and IN OUT parameters. We have to do this | |
5752 | -- after the above call to Kill_Current_Values (since that call clears | |
5753 | -- the Last_Assignment field of all local variables). | |
67ce0d7e | 5754 | |
36fcf362 | 5755 | if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters) |
67ce0d7e RD |
5756 | and then Comes_From_Source (N) |
5757 | and then In_Extended_Main_Source_Unit (N) | |
5758 | then | |
5759 | declare | |
5760 | F : Entity_Id; | |
5761 | A : Node_Id; | |
5762 | ||
5763 | begin | |
5764 | F := First_Formal (Nam); | |
5765 | A := First_Actual (N); | |
5766 | while Present (F) and then Present (A) loop | |
964f13da | 5767 | if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) |
36fcf362 | 5768 | and then Warn_On_Modified_As_Out_Parameter (F) |
67ce0d7e RD |
5769 | and then Is_Entity_Name (A) |
5770 | and then Present (Entity (A)) | |
36fcf362 | 5771 | and then Comes_From_Source (N) |
67ce0d7e RD |
5772 | and then Safe_To_Capture_Value (N, Entity (A)) |
5773 | then | |
5774 | Set_Last_Assignment (Entity (A), A); | |
5775 | end if; | |
5776 | ||
5777 | Next_Formal (F); | |
5778 | Next_Actual (A); | |
5779 | end loop; | |
5780 | end; | |
5781 | end if; | |
5782 | ||
996ae0b0 RK |
5783 | -- If the subprogram is a primitive operation, check whether or not |
5784 | -- it is a correct dispatching call. | |
5785 | ||
5786 | if Is_Overloadable (Nam) | |
5787 | and then Is_Dispatching_Operation (Nam) | |
5788 | then | |
5789 | Check_Dispatching_Call (N); | |
5790 | ||
0669bebe GB |
5791 | elsif Ekind (Nam) /= E_Subprogram_Type |
5792 | and then Is_Abstract_Subprogram (Nam) | |
996ae0b0 RK |
5793 | and then not In_Instance |
5794 | then | |
5795 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam); | |
5796 | end if; | |
5797 | ||
e65f50ec ES |
5798 | -- If this is a dispatching call, generate the appropriate reference, |
5799 | -- for better source navigation in GPS. | |
5800 | ||
5801 | if Is_Overloadable (Nam) | |
5802 | and then Present (Controlling_Argument (N)) | |
5803 | then | |
5804 | Generate_Reference (Nam, Subp, 'R'); | |
c5d91669 | 5805 | |
5cc9353d | 5806 | -- Normal case, not a dispatching call: generate a call reference |
c5d91669 | 5807 | |
e65f50ec | 5808 | else |
9c870c90 | 5809 | Generate_Reference (Nam, Subp, 's'); |
e65f50ec ES |
5810 | end if; |
5811 | ||
996ae0b0 RK |
5812 | if Is_Intrinsic_Subprogram (Nam) then |
5813 | Check_Intrinsic_Call (N); | |
5814 | end if; | |
5815 | ||
5b2217f8 | 5816 | -- Check for violation of restriction No_Specific_Termination_Handlers |
dce86910 | 5817 | -- and warn on a potentially blocking call to Abort_Task. |
5b2217f8 | 5818 | |
273adcdf AC |
5819 | if Restriction_Check_Required (No_Specific_Termination_Handlers) |
5820 | and then (Is_RTE (Nam, RE_Set_Specific_Handler) | |
5821 | or else | |
5822 | Is_RTE (Nam, RE_Specific_Handler)) | |
5b2217f8 RD |
5823 | then |
5824 | Check_Restriction (No_Specific_Termination_Handlers, N); | |
dce86910 AC |
5825 | |
5826 | elsif Is_RTE (Nam, RE_Abort_Task) then | |
5827 | Check_Potentially_Blocking_Operation (N); | |
5b2217f8 RD |
5828 | end if; |
5829 | ||
806f6d37 AC |
5830 | -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative |
5831 | -- timing event violates restriction No_Relative_Delay (AI-0211). We | |
5832 | -- need to check the second argument to determine whether it is an | |
5833 | -- absolute or relative timing event. | |
afbcdf5e | 5834 | |
273adcdf AC |
5835 | if Restriction_Check_Required (No_Relative_Delay) |
5836 | and then Is_RTE (Nam, RE_Set_Handler) | |
806f6d37 AC |
5837 | and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span) |
5838 | then | |
afbcdf5e AC |
5839 | Check_Restriction (No_Relative_Delay, N); |
5840 | end if; | |
5841 | ||
21791d97 AC |
5842 | -- Issue an error for a call to an eliminated subprogram. This routine |
5843 | -- will not perform the check if the call appears within a default | |
5844 | -- expression. | |
16212e89 | 5845 | |
df378148 | 5846 | Check_For_Eliminated_Subprogram (Subp, Nam); |
16212e89 | 5847 | |
12f0c50c AC |
5848 | -- In formal mode, the primitive operations of a tagged type or type |
5849 | -- extension do not include functions that return the tagged type. | |
5850 | ||
5851 | -- Commented out as the call to Is_Inherited_Operation_For_Type may | |
5852 | -- cause an error because the type entity of the parent node of | |
ded8909b AC |
5853 | -- Entity (Name (N) may not be set. ??? |
5854 | -- So why not just add a guard ??? | |
12f0c50c AC |
5855 | |
5856 | -- if Nkind (N) = N_Function_Call | |
5857 | -- and then Is_Tagged_Type (Etype (N)) | |
5858 | -- and then Is_Entity_Name (Name (N)) | |
5859 | -- and then Is_Inherited_Operation_For_Type | |
ded8909b | 5860 | -- (Entity (Name (N)), Etype (N)) |
12f0c50c | 5861 | -- then |
c4d67e2d | 5862 | -- Check_SPARK_Restriction ("function not inherited", N); |
12f0c50c AC |
5863 | -- end if; |
5864 | ||
e8374e7a AC |
5865 | -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is |
5866 | -- class-wide and the call dispatches on result in a context that does | |
5867 | -- not provide a tag, the call raises Program_Error. | |
1f6439e3 AC |
5868 | |
5869 | if Nkind (N) = N_Function_Call | |
5870 | and then In_Instance | |
5871 | and then Is_Generic_Actual_Type (Typ) | |
5872 | and then Is_Class_Wide_Type (Typ) | |
5873 | and then Has_Controlling_Result (Nam) | |
5874 | and then Nkind (Parent (N)) = N_Object_Declaration | |
5875 | then | |
e8374e7a | 5876 | -- Verify that none of the formals are controlling |
1f6439e3 AC |
5877 | |
5878 | declare | |
e8374e7a | 5879 | Call_OK : Boolean := False; |
1f6439e3 AC |
5880 | F : Entity_Id; |
5881 | ||
5882 | begin | |
5883 | F := First_Formal (Nam); | |
5884 | while Present (F) loop | |
5885 | if Is_Controlling_Formal (F) then | |
5886 | Call_OK := True; | |
5887 | exit; | |
5888 | end if; | |
e8374e7a | 5889 | |
1f6439e3 AC |
5890 | Next_Formal (F); |
5891 | end loop; | |
5892 | ||
5893 | if not Call_OK then | |
5894 | Error_Msg_N ("!? cannot determine tag of result", N); | |
5895 | Error_Msg_N ("!? Program_Error will be raised", N); | |
5896 | Insert_Action (N, | |
5897 | Make_Raise_Program_Error (Sloc (N), | |
5898 | Reason => PE_Explicit_Raise)); | |
5899 | end if; | |
5900 | end; | |
5901 | end if; | |
5902 | ||
dec6faf1 AC |
5903 | Analyze_Dimension (N); |
5904 | ||
67ce0d7e RD |
5905 | -- All done, evaluate call and deal with elaboration issues |
5906 | ||
c01a9391 | 5907 | Eval_Call (N); |
996ae0b0 | 5908 | Check_Elab_Call (N); |
76b84bf0 | 5909 | Warn_On_Overlapping_Actuals (Nam, N); |
996ae0b0 RK |
5910 | end Resolve_Call; |
5911 | ||
19d846a0 RD |
5912 | ----------------------------- |
5913 | -- Resolve_Case_Expression -- | |
5914 | ----------------------------- | |
5915 | ||
5916 | procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is | |
5917 | Alt : Node_Id; | |
5918 | ||
5919 | begin | |
5920 | Alt := First (Alternatives (N)); | |
5921 | while Present (Alt) loop | |
5922 | Resolve (Expression (Alt), Typ); | |
5923 | Next (Alt); | |
5924 | end loop; | |
5925 | ||
5926 | Set_Etype (N, Typ); | |
5927 | Eval_Case_Expression (N); | |
5928 | end Resolve_Case_Expression; | |
5929 | ||
996ae0b0 RK |
5930 | ------------------------------- |
5931 | -- Resolve_Character_Literal -- | |
5932 | ------------------------------- | |
5933 | ||
5934 | procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is | |
5935 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
5936 | C : Entity_Id; | |
5937 | ||
5938 | begin | |
5939 | -- Verify that the character does belong to the type of the context | |
5940 | ||
5941 | Set_Etype (N, B_Typ); | |
5942 | Eval_Character_Literal (N); | |
5943 | ||
82c80734 RD |
5944 | -- Wide_Wide_Character literals must always be defined, since the set |
5945 | -- of wide wide character literals is complete, i.e. if a character | |
5946 | -- literal is accepted by the parser, then it is OK for wide wide | |
5947 | -- character (out of range character literals are rejected). | |
996ae0b0 | 5948 | |
82c80734 | 5949 | if Root_Type (B_Typ) = Standard_Wide_Wide_Character then |
996ae0b0 RK |
5950 | return; |
5951 | ||
5952 | -- Always accept character literal for type Any_Character, which | |
5953 | -- occurs in error situations and in comparisons of literals, both | |
5954 | -- of which should accept all literals. | |
5955 | ||
5956 | elsif B_Typ = Any_Character then | |
5957 | return; | |
5958 | ||
5cc9353d RD |
5959 | -- For Standard.Character or a type derived from it, check that the |
5960 | -- literal is in range. | |
996ae0b0 RK |
5961 | |
5962 | elsif Root_Type (B_Typ) = Standard_Character then | |
82c80734 RD |
5963 | if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then |
5964 | return; | |
5965 | end if; | |
5966 | ||
5cc9353d RD |
5967 | -- For Standard.Wide_Character or a type derived from it, check that the |
5968 | -- literal is in range. | |
82c80734 RD |
5969 | |
5970 | elsif Root_Type (B_Typ) = Standard_Wide_Character then | |
5971 | if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then | |
996ae0b0 RK |
5972 | return; |
5973 | end if; | |
5974 | ||
82c80734 RD |
5975 | -- For Standard.Wide_Wide_Character or a type derived from it, we |
5976 | -- know the literal is in range, since the parser checked! | |
5977 | ||
5978 | elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then | |
5979 | return; | |
5980 | ||
d81b4bfe TQ |
5981 | -- If the entity is already set, this has already been resolved in a |
5982 | -- generic context, or comes from expansion. Nothing else to do. | |
996ae0b0 RK |
5983 | |
5984 | elsif Present (Entity (N)) then | |
5985 | return; | |
5986 | ||
d81b4bfe TQ |
5987 | -- Otherwise we have a user defined character type, and we can use the |
5988 | -- standard visibility mechanisms to locate the referenced entity. | |
996ae0b0 RK |
5989 | |
5990 | else | |
5991 | C := Current_Entity (N); | |
996ae0b0 RK |
5992 | while Present (C) loop |
5993 | if Etype (C) = B_Typ then | |
5994 | Set_Entity_With_Style_Check (N, C); | |
5995 | Generate_Reference (C, N); | |
5996 | return; | |
5997 | end if; | |
5998 | ||
5999 | C := Homonym (C); | |
6000 | end loop; | |
6001 | end if; | |
6002 | ||
6003 | -- If we fall through, then the literal does not match any of the | |
5cc9353d RD |
6004 | -- entries of the enumeration type. This isn't just a constraint error |
6005 | -- situation, it is an illegality (see RM 4.2). | |
996ae0b0 RK |
6006 | |
6007 | Error_Msg_NE | |
6008 | ("character not defined for }", N, First_Subtype (B_Typ)); | |
996ae0b0 RK |
6009 | end Resolve_Character_Literal; |
6010 | ||
6011 | --------------------------- | |
6012 | -- Resolve_Comparison_Op -- | |
6013 | --------------------------- | |
6014 | ||
6015 | -- Context requires a boolean type, and plays no role in resolution. | |
5cc9353d RD |
6016 | -- Processing identical to that for equality operators. The result type is |
6017 | -- the base type, which matters when pathological subtypes of booleans with | |
6018 | -- limited ranges are used. | |
996ae0b0 RK |
6019 | |
6020 | procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is | |
6021 | L : constant Node_Id := Left_Opnd (N); | |
6022 | R : constant Node_Id := Right_Opnd (N); | |
6023 | T : Entity_Id; | |
6024 | ||
6025 | begin | |
d81b4bfe TQ |
6026 | -- If this is an intrinsic operation which is not predefined, use the |
6027 | -- types of its declared arguments to resolve the possibly overloaded | |
6028 | -- operands. Otherwise the operands are unambiguous and specify the | |
6029 | -- expected type. | |
996ae0b0 RK |
6030 | |
6031 | if Scope (Entity (N)) /= Standard_Standard then | |
6032 | T := Etype (First_Entity (Entity (N))); | |
1420b484 | 6033 | |
996ae0b0 RK |
6034 | else |
6035 | T := Find_Unique_Type (L, R); | |
6036 | ||
6037 | if T = Any_Fixed then | |
6038 | T := Unique_Fixed_Point_Type (L); | |
6039 | end if; | |
6040 | end if; | |
6041 | ||
fbf5a39b | 6042 | Set_Etype (N, Base_Type (Typ)); |
996ae0b0 RK |
6043 | Generate_Reference (T, N, ' '); |
6044 | ||
bd29d519 | 6045 | -- Skip remaining processing if already set to Any_Type |
996ae0b0 | 6046 | |
bd29d519 AC |
6047 | if T = Any_Type then |
6048 | return; | |
6049 | end if; | |
6050 | ||
6051 | -- Deal with other error cases | |
996ae0b0 | 6052 | |
bd29d519 AC |
6053 | if T = Any_String or else |
6054 | T = Any_Composite or else | |
6055 | T = Any_Character | |
6056 | then | |
6057 | if T = Any_Character then | |
6058 | Ambiguous_Character (L); | |
996ae0b0 | 6059 | else |
bd29d519 | 6060 | Error_Msg_N ("ambiguous operands for comparison", N); |
996ae0b0 | 6061 | end if; |
bd29d519 AC |
6062 | |
6063 | Set_Etype (N, Any_Type); | |
6064 | return; | |
996ae0b0 | 6065 | end if; |
bd29d519 AC |
6066 | |
6067 | -- Resolve the operands if types OK | |
6068 | ||
6069 | Resolve (L, T); | |
6070 | Resolve (R, T); | |
6071 | Check_Unset_Reference (L); | |
6072 | Check_Unset_Reference (R); | |
6073 | Generate_Operator_Reference (N, T); | |
6074 | Check_Low_Bound_Tested (N); | |
6075 | ||
2ba431e5 YM |
6076 | -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean |
6077 | -- types or array types except String. | |
b0186f71 | 6078 | |
fe5d3068 | 6079 | if Is_Boolean_Type (T) then |
2ba431e5 | 6080 | Check_SPARK_Restriction |
fe5d3068 | 6081 | ("comparison is not defined on Boolean type", N); |
975c6896 | 6082 | |
ad05f2e9 AC |
6083 | elsif Is_Array_Type (T) |
6084 | and then Base_Type (T) /= Standard_String | |
6085 | then | |
6086 | Check_SPARK_Restriction | |
6087 | ("comparison is not defined on array types other than String", N); | |
b0186f71 AC |
6088 | end if; |
6089 | ||
bd29d519 AC |
6090 | -- Check comparison on unordered enumeration |
6091 | ||
6092 | if Comes_From_Source (N) | |
6093 | and then Bad_Unordered_Enumeration_Reference (N, Etype (L)) | |
6094 | then | |
6095 | Error_Msg_N ("comparison on unordered enumeration type?", N); | |
6096 | end if; | |
6097 | ||
5cc9353d RD |
6098 | -- Evaluate the relation (note we do this after the above check since |
6099 | -- this Eval call may change N to True/False. | |
bd29d519 | 6100 | |
dec6faf1 | 6101 | Analyze_Dimension (N); |
bd29d519 | 6102 | Eval_Relational_Op (N); |
996ae0b0 RK |
6103 | end Resolve_Comparison_Op; |
6104 | ||
6105 | ------------------------------------ | |
6106 | -- Resolve_Conditional_Expression -- | |
6107 | ------------------------------------ | |
6108 | ||
6109 | procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is | |
6110 | Condition : constant Node_Id := First (Expressions (N)); | |
6111 | Then_Expr : constant Node_Id := Next (Condition); | |
19fb051c | 6112 | Else_Expr : Node_Id := Next (Then_Expr); |
36504e5f AC |
6113 | Else_Typ : Entity_Id; |
6114 | Then_Typ : Entity_Id; | |
b46be8a2 | 6115 | |
996ae0b0 | 6116 | begin |
b46be8a2 | 6117 | Resolve (Condition, Any_Boolean); |
996ae0b0 | 6118 | Resolve (Then_Expr, Typ); |
36504e5f AC |
6119 | Then_Typ := Etype (Then_Expr); |
6120 | ||
6121 | -- When the "then" and "else" expressions are of a scalar type, insert | |
6122 | -- a conversion to ensure the generation of a constraint check. | |
6123 | ||
6124 | if Is_Scalar_Type (Then_Typ) | |
6125 | and then Then_Typ /= Typ | |
6126 | then | |
6127 | Rewrite (Then_Expr, Convert_To (Typ, Then_Expr)); | |
6128 | Analyze_And_Resolve (Then_Expr, Typ); | |
6129 | end if; | |
b46be8a2 RD |
6130 | |
6131 | -- If ELSE expression present, just resolve using the determined type | |
6132 | ||
6133 | if Present (Else_Expr) then | |
6134 | Resolve (Else_Expr, Typ); | |
36504e5f AC |
6135 | Else_Typ := Etype (Else_Expr); |
6136 | ||
6137 | if Is_Scalar_Type (Else_Typ) | |
6138 | and then Else_Typ /= Typ | |
6139 | then | |
6140 | Rewrite (Else_Expr, Convert_To (Typ, Else_Expr)); | |
6141 | Analyze_And_Resolve (Else_Expr, Typ); | |
6142 | end if; | |
b46be8a2 RD |
6143 | |
6144 | -- If no ELSE expression is present, root type must be Standard.Boolean | |
6145 | -- and we provide a Standard.True result converted to the appropriate | |
6146 | -- Boolean type (in case it is a derived boolean type). | |
6147 | ||
6148 | elsif Root_Type (Typ) = Standard_Boolean then | |
6149 | Else_Expr := | |
6150 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N))); | |
6151 | Analyze_And_Resolve (Else_Expr, Typ); | |
6152 | Append_To (Expressions (N), Else_Expr); | |
6153 | ||
6154 | else | |
6155 | Error_Msg_N ("can only omit ELSE expression in Boolean case", N); | |
6156 | Append_To (Expressions (N), Error); | |
6157 | end if; | |
6158 | ||
996ae0b0 RK |
6159 | Set_Etype (N, Typ); |
6160 | Eval_Conditional_Expression (N); | |
6161 | end Resolve_Conditional_Expression; | |
6162 | ||
6163 | ----------------------------------------- | |
6164 | -- Resolve_Discrete_Subtype_Indication -- | |
6165 | ----------------------------------------- | |
6166 | ||
6167 | procedure Resolve_Discrete_Subtype_Indication | |
6168 | (N : Node_Id; | |
6169 | Typ : Entity_Id) | |
6170 | is | |
6171 | R : Node_Id; | |
6172 | S : Entity_Id; | |
6173 | ||
6174 | begin | |
6175 | Analyze (Subtype_Mark (N)); | |
6176 | S := Entity (Subtype_Mark (N)); | |
6177 | ||
6178 | if Nkind (Constraint (N)) /= N_Range_Constraint then | |
6179 | Error_Msg_N ("expect range constraint for discrete type", N); | |
6180 | Set_Etype (N, Any_Type); | |
6181 | ||
6182 | else | |
6183 | R := Range_Expression (Constraint (N)); | |
5c736541 RD |
6184 | |
6185 | if R = Error then | |
6186 | return; | |
6187 | end if; | |
6188 | ||
996ae0b0 RK |
6189 | Analyze (R); |
6190 | ||
6191 | if Base_Type (S) /= Base_Type (Typ) then | |
6192 | Error_Msg_NE | |
6193 | ("expect subtype of }", N, First_Subtype (Typ)); | |
6194 | ||
6195 | -- Rewrite the constraint as a range of Typ | |
6196 | -- to allow compilation to proceed further. | |
6197 | ||
6198 | Set_Etype (N, Typ); | |
6199 | Rewrite (Low_Bound (R), | |
6200 | Make_Attribute_Reference (Sloc (Low_Bound (R)), | |
5cc9353d | 6201 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), |
996ae0b0 RK |
6202 | Attribute_Name => Name_First)); |
6203 | Rewrite (High_Bound (R), | |
6204 | Make_Attribute_Reference (Sloc (High_Bound (R)), | |
5cc9353d | 6205 | Prefix => New_Occurrence_Of (Typ, Sloc (R)), |
996ae0b0 RK |
6206 | Attribute_Name => Name_First)); |
6207 | ||
6208 | else | |
6209 | Resolve (R, Typ); | |
6210 | Set_Etype (N, Etype (R)); | |
6211 | ||
6212 | -- Additionally, we must check that the bounds are compatible | |
6213 | -- with the given subtype, which might be different from the | |
6214 | -- type of the context. | |
6215 | ||
6216 | Apply_Range_Check (R, S); | |
6217 | ||
6218 | -- ??? If the above check statically detects a Constraint_Error | |
6219 | -- it replaces the offending bound(s) of the range R with a | |
6220 | -- Constraint_Error node. When the itype which uses these bounds | |
6221 | -- is frozen the resulting call to Duplicate_Subexpr generates | |
6222 | -- a new temporary for the bounds. | |
6223 | ||
6224 | -- Unfortunately there are other itypes that are also made depend | |
6225 | -- on these bounds, so when Duplicate_Subexpr is called they get | |
6226 | -- a forward reference to the newly created temporaries and Gigi | |
6227 | -- aborts on such forward references. This is probably sign of a | |
6228 | -- more fundamental problem somewhere else in either the order of | |
6229 | -- itype freezing or the way certain itypes are constructed. | |
6230 | ||
6231 | -- To get around this problem we call Remove_Side_Effects right | |
6232 | -- away if either bounds of R are a Constraint_Error. | |
6233 | ||
6234 | declare | |
fbf5a39b AC |
6235 | L : constant Node_Id := Low_Bound (R); |
6236 | H : constant Node_Id := High_Bound (R); | |
996ae0b0 RK |
6237 | |
6238 | begin | |
6239 | if Nkind (L) = N_Raise_Constraint_Error then | |
6240 | Remove_Side_Effects (L); | |
6241 | end if; | |
6242 | ||
6243 | if Nkind (H) = N_Raise_Constraint_Error then | |
6244 | Remove_Side_Effects (H); | |
6245 | end if; | |
6246 | end; | |
6247 | ||
6248 | Check_Unset_Reference (Low_Bound (R)); | |
6249 | Check_Unset_Reference (High_Bound (R)); | |
6250 | end if; | |
6251 | end if; | |
6252 | end Resolve_Discrete_Subtype_Indication; | |
6253 | ||
6254 | ------------------------- | |
6255 | -- Resolve_Entity_Name -- | |
6256 | ------------------------- | |
6257 | ||
6258 | -- Used to resolve identifiers and expanded names | |
6259 | ||
6260 | procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is | |
6261 | E : constant Entity_Id := Entity (N); | |
6262 | ||
6263 | begin | |
07fc65c4 GB |
6264 | -- If garbage from errors, set to Any_Type and return |
6265 | ||
6266 | if No (E) and then Total_Errors_Detected /= 0 then | |
6267 | Set_Etype (N, Any_Type); | |
6268 | return; | |
6269 | end if; | |
6270 | ||
996ae0b0 RK |
6271 | -- Replace named numbers by corresponding literals. Note that this is |
6272 | -- the one case where Resolve_Entity_Name must reset the Etype, since | |
6273 | -- it is currently marked as universal. | |
6274 | ||
6275 | if Ekind (E) = E_Named_Integer then | |
6276 | Set_Etype (N, Typ); | |
6277 | Eval_Named_Integer (N); | |
6278 | ||
6279 | elsif Ekind (E) = E_Named_Real then | |
6280 | Set_Etype (N, Typ); | |
6281 | Eval_Named_Real (N); | |
6282 | ||
6989bc1f AC |
6283 | -- For enumeration literals, we need to make sure that a proper style |
6284 | -- check is done, since such literals are overloaded, and thus we did | |
6285 | -- not do a style check during the first phase of analysis. | |
6286 | ||
6287 | elsif Ekind (E) = E_Enumeration_Literal then | |
6288 | Set_Entity_With_Style_Check (N, E); | |
6289 | Eval_Entity_Name (N); | |
6290 | ||
e606088a | 6291 | -- Case of subtype name appearing as an operand in expression |
996ae0b0 RK |
6292 | |
6293 | elsif Is_Type (E) then | |
e606088a AC |
6294 | |
6295 | -- Allow use of subtype if it is a concurrent type where we are | |
6296 | -- currently inside the body. This will eventually be expanded into a | |
6297 | -- call to Self (for tasks) or _object (for protected objects). Any | |
6298 | -- other use of a subtype is invalid. | |
6299 | ||
996ae0b0 RK |
6300 | if Is_Concurrent_Type (E) |
6301 | and then In_Open_Scopes (E) | |
6302 | then | |
6303 | null; | |
e606088a | 6304 | |
308e6f3a | 6305 | -- Any other use is an error |
e606088a | 6306 | |
996ae0b0 RK |
6307 | else |
6308 | Error_Msg_N | |
758c442c | 6309 | ("invalid use of subtype mark in expression or call", N); |
996ae0b0 RK |
6310 | end if; |
6311 | ||
6312 | -- Check discriminant use if entity is discriminant in current scope, | |
6313 | -- i.e. discriminant of record or concurrent type currently being | |
6314 | -- analyzed. Uses in corresponding body are unrestricted. | |
6315 | ||
6316 | elsif Ekind (E) = E_Discriminant | |
6317 | and then Scope (E) = Current_Scope | |
6318 | and then not Has_Completion (Current_Scope) | |
6319 | then | |
6320 | Check_Discriminant_Use (N); | |
6321 | ||
6322 | -- A parameterless generic function cannot appear in a context that | |
6323 | -- requires resolution. | |
6324 | ||
6325 | elsif Ekind (E) = E_Generic_Function then | |
6326 | Error_Msg_N ("illegal use of generic function", N); | |
6327 | ||
6328 | elsif Ekind (E) = E_Out_Parameter | |
0ab80019 | 6329 | and then Ada_Version = Ada_83 |
996ae0b0 | 6330 | and then (Nkind (Parent (N)) in N_Op |
19fb051c AC |
6331 | or else (Nkind (Parent (N)) = N_Assignment_Statement |
6332 | and then N = Expression (Parent (N))) | |
6333 | or else Nkind (Parent (N)) = N_Explicit_Dereference) | |
996ae0b0 RK |
6334 | then |
6335 | Error_Msg_N ("(Ada 83) illegal reading of out parameter", N); | |
6336 | ||
6337 | -- In all other cases, just do the possible static evaluation | |
6338 | ||
6339 | else | |
d81b4bfe TQ |
6340 | -- A deferred constant that appears in an expression must have a |
6341 | -- completion, unless it has been removed by in-place expansion of | |
6342 | -- an aggregate. | |
996ae0b0 RK |
6343 | |
6344 | if Ekind (E) = E_Constant | |
6345 | and then Comes_From_Source (E) | |
6346 | and then No (Constant_Value (E)) | |
6347 | and then Is_Frozen (Etype (E)) | |
45fc7ddb | 6348 | and then not In_Spec_Expression |
996ae0b0 RK |
6349 | and then not Is_Imported (E) |
6350 | then | |
996ae0b0 RK |
6351 | if No_Initialization (Parent (E)) |
6352 | or else (Present (Full_View (E)) | |
6353 | and then No_Initialization (Parent (Full_View (E)))) | |
6354 | then | |
6355 | null; | |
6356 | else | |
6357 | Error_Msg_N ( | |
6358 | "deferred constant is frozen before completion", N); | |
6359 | end if; | |
6360 | end if; | |
6361 | ||
6362 | Eval_Entity_Name (N); | |
6363 | end if; | |
6364 | end Resolve_Entity_Name; | |
6365 | ||
6366 | ------------------- | |
6367 | -- Resolve_Entry -- | |
6368 | ------------------- | |
6369 | ||
6370 | procedure Resolve_Entry (Entry_Name : Node_Id) is | |
6371 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
6372 | Nam : Entity_Id; | |
6373 | New_N : Node_Id; | |
6374 | S : Entity_Id; | |
6375 | Tsk : Entity_Id; | |
6376 | E_Name : Node_Id; | |
6377 | Index : Node_Id; | |
6378 | ||
6379 | function Actual_Index_Type (E : Entity_Id) return Entity_Id; | |
6380 | -- If the bounds of the entry family being called depend on task | |
6381 | -- discriminants, build a new index subtype where a discriminant is | |
6382 | -- replaced with the value of the discriminant of the target task. | |
6383 | -- The target task is the prefix of the entry name in the call. | |
6384 | ||
6385 | ----------------------- | |
6386 | -- Actual_Index_Type -- | |
6387 | ----------------------- | |
6388 | ||
6389 | function Actual_Index_Type (E : Entity_Id) return Entity_Id is | |
fbf5a39b AC |
6390 | Typ : constant Entity_Id := Entry_Index_Type (E); |
6391 | Tsk : constant Entity_Id := Scope (E); | |
6392 | Lo : constant Node_Id := Type_Low_Bound (Typ); | |
6393 | Hi : constant Node_Id := Type_High_Bound (Typ); | |
996ae0b0 RK |
6394 | New_T : Entity_Id; |
6395 | ||
6396 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id; | |
6397 | -- If the bound is given by a discriminant, replace with a reference | |
d81b4bfe TQ |
6398 | -- to the discriminant of the same name in the target task. If the |
6399 | -- entry name is the target of a requeue statement and the entry is | |
6400 | -- in the current protected object, the bound to be used is the | |
008f6fd3 | 6401 | -- discriminal of the object (see Apply_Range_Checks for details of |
d81b4bfe | 6402 | -- the transformation). |
996ae0b0 RK |
6403 | |
6404 | ----------------------------- | |
6405 | -- Actual_Discriminant_Ref -- | |
6406 | ----------------------------- | |
6407 | ||
6408 | function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is | |
fbf5a39b | 6409 | Typ : constant Entity_Id := Etype (Bound); |
996ae0b0 RK |
6410 | Ref : Node_Id; |
6411 | ||
6412 | begin | |
6413 | Remove_Side_Effects (Bound); | |
6414 | ||
6415 | if not Is_Entity_Name (Bound) | |
6416 | or else Ekind (Entity (Bound)) /= E_Discriminant | |
6417 | then | |
6418 | return Bound; | |
6419 | ||
6420 | elsif Is_Protected_Type (Tsk) | |
6421 | and then In_Open_Scopes (Tsk) | |
6422 | and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement | |
6423 | then | |
6ca9ec9c AC |
6424 | -- Note: here Bound denotes a discriminant of the corresponding |
6425 | -- record type tskV, whose discriminal is a formal of the | |
6426 | -- init-proc tskVIP. What we want is the body discriminal, | |
6427 | -- which is associated to the discriminant of the original | |
6428 | -- concurrent type tsk. | |
6429 | ||
5a153b27 AC |
6430 | return New_Occurrence_Of |
6431 | (Find_Body_Discriminal (Entity (Bound)), Loc); | |
996ae0b0 RK |
6432 | |
6433 | else | |
6434 | Ref := | |
6435 | Make_Selected_Component (Loc, | |
6436 | Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))), | |
6437 | Selector_Name => New_Occurrence_Of (Entity (Bound), Loc)); | |
6438 | Analyze (Ref); | |
6439 | Resolve (Ref, Typ); | |
6440 | return Ref; | |
6441 | end if; | |
6442 | end Actual_Discriminant_Ref; | |
6443 | ||
6444 | -- Start of processing for Actual_Index_Type | |
6445 | ||
6446 | begin | |
6447 | if not Has_Discriminants (Tsk) | |
19fb051c | 6448 | or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi)) |
996ae0b0 RK |
6449 | then |
6450 | return Entry_Index_Type (E); | |
6451 | ||
6452 | else | |
6453 | New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name)); | |
6454 | Set_Etype (New_T, Base_Type (Typ)); | |
6455 | Set_Size_Info (New_T, Typ); | |
6456 | Set_RM_Size (New_T, RM_Size (Typ)); | |
6457 | Set_Scalar_Range (New_T, | |
6458 | Make_Range (Sloc (Entry_Name), | |
6459 | Low_Bound => Actual_Discriminant_Ref (Lo), | |
6460 | High_Bound => Actual_Discriminant_Ref (Hi))); | |
6461 | ||
6462 | return New_T; | |
6463 | end if; | |
6464 | end Actual_Index_Type; | |
6465 | ||
6466 | -- Start of processing of Resolve_Entry | |
6467 | ||
6468 | begin | |
5cc9353d RD |
6469 | -- Find name of entry being called, and resolve prefix of name with its |
6470 | -- own type. The prefix can be overloaded, and the name and signature of | |
6471 | -- the entry must be taken into account. | |
996ae0b0 RK |
6472 | |
6473 | if Nkind (Entry_Name) = N_Indexed_Component then | |
6474 | ||
6475 | -- Case of dealing with entry family within the current tasks | |
6476 | ||
6477 | E_Name := Prefix (Entry_Name); | |
6478 | ||
6479 | else | |
6480 | E_Name := Entry_Name; | |
6481 | end if; | |
6482 | ||
6483 | if Is_Entity_Name (E_Name) then | |
996ae0b0 | 6484 | |
d81b4bfe TQ |
6485 | -- Entry call to an entry (or entry family) in the current task. This |
6486 | -- is legal even though the task will deadlock. Rewrite as call to | |
6487 | -- current task. | |
996ae0b0 | 6488 | |
d81b4bfe TQ |
6489 | -- This can also be a call to an entry in an enclosing task. If this |
6490 | -- is a single task, we have to retrieve its name, because the scope | |
6491 | -- of the entry is the task type, not the object. If the enclosing | |
6492 | -- task is a task type, the identity of the task is given by its own | |
6493 | -- self variable. | |
6494 | ||
6495 | -- Finally this can be a requeue on an entry of the same task or | |
6496 | -- protected object. | |
996ae0b0 RK |
6497 | |
6498 | S := Scope (Entity (E_Name)); | |
6499 | ||
6500 | for J in reverse 0 .. Scope_Stack.Last loop | |
996ae0b0 RK |
6501 | if Is_Task_Type (Scope_Stack.Table (J).Entity) |
6502 | and then not Comes_From_Source (S) | |
6503 | then | |
6504 | -- S is an enclosing task or protected object. The concurrent | |
6505 | -- declaration has been converted into a type declaration, and | |
6506 | -- the object itself has an object declaration that follows | |
6507 | -- the type in the same declarative part. | |
6508 | ||
6509 | Tsk := Next_Entity (S); | |
996ae0b0 RK |
6510 | while Etype (Tsk) /= S loop |
6511 | Next_Entity (Tsk); | |
6512 | end loop; | |
6513 | ||
6514 | S := Tsk; | |
6515 | exit; | |
6516 | ||
6517 | elsif S = Scope_Stack.Table (J).Entity then | |
6518 | ||
6519 | -- Call to current task. Will be transformed into call to Self | |
6520 | ||
6521 | exit; | |
6522 | ||
6523 | end if; | |
6524 | end loop; | |
6525 | ||
6526 | New_N := | |
6527 | Make_Selected_Component (Loc, | |
6528 | Prefix => New_Occurrence_Of (S, Loc), | |
6529 | Selector_Name => | |
6530 | New_Occurrence_Of (Entity (E_Name), Loc)); | |
6531 | Rewrite (E_Name, New_N); | |
6532 | Analyze (E_Name); | |
6533 | ||
6534 | elsif Nkind (Entry_Name) = N_Selected_Component | |
6535 | and then Is_Overloaded (Prefix (Entry_Name)) | |
6536 | then | |
d81b4bfe | 6537 | -- Use the entry name (which must be unique at this point) to find |
5cc9353d | 6538 | -- the prefix that returns the corresponding task/protected type. |
996ae0b0 RK |
6539 | |
6540 | declare | |
fbf5a39b AC |
6541 | Pref : constant Node_Id := Prefix (Entry_Name); |
6542 | Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name)); | |
996ae0b0 RK |
6543 | I : Interp_Index; |
6544 | It : Interp; | |
996ae0b0 RK |
6545 | |
6546 | begin | |
6547 | Get_First_Interp (Pref, I, It); | |
996ae0b0 | 6548 | while Present (It.Typ) loop |
996ae0b0 RK |
6549 | if Scope (Ent) = It.Typ then |
6550 | Set_Etype (Pref, It.Typ); | |
6551 | exit; | |
6552 | end if; | |
6553 | ||
6554 | Get_Next_Interp (I, It); | |
6555 | end loop; | |
6556 | end; | |
6557 | end if; | |
6558 | ||
6559 | if Nkind (Entry_Name) = N_Selected_Component then | |
fbf5a39b | 6560 | Resolve (Prefix (Entry_Name)); |
996ae0b0 RK |
6561 | |
6562 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6563 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
fbf5a39b | 6564 | Resolve (Prefix (Prefix (Entry_Name))); |
996ae0b0 RK |
6565 | Index := First (Expressions (Entry_Name)); |
6566 | Resolve (Index, Entry_Index_Type (Nam)); | |
6567 | ||
d81b4bfe TQ |
6568 | -- Up to this point the expression could have been the actual in a |
6569 | -- simple entry call, and be given by a named association. | |
996ae0b0 RK |
6570 | |
6571 | if Nkind (Index) = N_Parameter_Association then | |
6572 | Error_Msg_N ("expect expression for entry index", Index); | |
6573 | else | |
6574 | Apply_Range_Check (Index, Actual_Index_Type (Nam)); | |
6575 | end if; | |
6576 | end if; | |
996ae0b0 RK |
6577 | end Resolve_Entry; |
6578 | ||
6579 | ------------------------ | |
6580 | -- Resolve_Entry_Call -- | |
6581 | ------------------------ | |
6582 | ||
6583 | procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is | |
6584 | Entry_Name : constant Node_Id := Name (N); | |
6585 | Loc : constant Source_Ptr := Sloc (Entry_Name); | |
6586 | Actuals : List_Id; | |
6587 | First_Named : Node_Id; | |
6588 | Nam : Entity_Id; | |
6589 | Norm_OK : Boolean; | |
6590 | Obj : Node_Id; | |
6591 | Was_Over : Boolean; | |
6592 | ||
6593 | begin | |
d81b4bfe TQ |
6594 | -- We kill all checks here, because it does not seem worth the effort to |
6595 | -- do anything better, an entry call is a big operation. | |
fbf5a39b AC |
6596 | |
6597 | Kill_All_Checks; | |
6598 | ||
996ae0b0 RK |
6599 | -- Processing of the name is similar for entry calls and protected |
6600 | -- operation calls. Once the entity is determined, we can complete | |
6601 | -- the resolution of the actuals. | |
6602 | ||
6603 | -- The selector may be overloaded, in the case of a protected object | |
6604 | -- with overloaded functions. The type of the context is used for | |
6605 | -- resolution. | |
6606 | ||
6607 | if Nkind (Entry_Name) = N_Selected_Component | |
6608 | and then Is_Overloaded (Selector_Name (Entry_Name)) | |
6609 | and then Typ /= Standard_Void_Type | |
6610 | then | |
6611 | declare | |
6612 | I : Interp_Index; | |
6613 | It : Interp; | |
6614 | ||
6615 | begin | |
6616 | Get_First_Interp (Selector_Name (Entry_Name), I, It); | |
996ae0b0 | 6617 | while Present (It.Typ) loop |
996ae0b0 RK |
6618 | if Covers (Typ, It.Typ) then |
6619 | Set_Entity (Selector_Name (Entry_Name), It.Nam); | |
6620 | Set_Etype (Entry_Name, It.Typ); | |
6621 | ||
6622 | Generate_Reference (It.Typ, N, ' '); | |
6623 | end if; | |
6624 | ||
6625 | Get_Next_Interp (I, It); | |
6626 | end loop; | |
6627 | end; | |
6628 | end if; | |
6629 | ||
6630 | Resolve_Entry (Entry_Name); | |
6631 | ||
6632 | if Nkind (Entry_Name) = N_Selected_Component then | |
6633 | ||
a77842bd | 6634 | -- Simple entry call |
996ae0b0 RK |
6635 | |
6636 | Nam := Entity (Selector_Name (Entry_Name)); | |
6637 | Obj := Prefix (Entry_Name); | |
6638 | Was_Over := Is_Overloaded (Selector_Name (Entry_Name)); | |
6639 | ||
6640 | else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component); | |
6641 | ||
a77842bd | 6642 | -- Call to member of entry family |
996ae0b0 RK |
6643 | |
6644 | Nam := Entity (Selector_Name (Prefix (Entry_Name))); | |
6645 | Obj := Prefix (Prefix (Entry_Name)); | |
6646 | Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name))); | |
6647 | end if; | |
6648 | ||
5cc9353d RD |
6649 | -- We cannot in general check the maximum depth of protected entry calls |
6650 | -- at compile time. But we can tell that any protected entry call at all | |
6651 | -- violates a specified nesting depth of zero. | |
fbf5a39b AC |
6652 | |
6653 | if Is_Protected_Type (Scope (Nam)) then | |
9f4fd324 | 6654 | Check_Restriction (Max_Entry_Queue_Length, N); |
fbf5a39b AC |
6655 | end if; |
6656 | ||
996ae0b0 | 6657 | -- Use context type to disambiguate a protected function that can be |
5cc9353d RD |
6658 | -- called without actuals and that returns an array type, and where the |
6659 | -- argument list may be an indexing of the returned value. | |
996ae0b0 RK |
6660 | |
6661 | if Ekind (Nam) = E_Function | |
6662 | and then Needs_No_Actuals (Nam) | |
6663 | and then Present (Parameter_Associations (N)) | |
6664 | and then | |
6665 | ((Is_Array_Type (Etype (Nam)) | |
6666 | and then Covers (Typ, Component_Type (Etype (Nam)))) | |
6667 | ||
6668 | or else (Is_Access_Type (Etype (Nam)) | |
6669 | and then Is_Array_Type (Designated_Type (Etype (Nam))) | |
19fb051c AC |
6670 | and then |
6671 | Covers | |
6672 | (Typ, | |
6673 | Component_Type (Designated_Type (Etype (Nam)))))) | |
996ae0b0 RK |
6674 | then |
6675 | declare | |
6676 | Index_Node : Node_Id; | |
6677 | ||
6678 | begin | |
6679 | Index_Node := | |
6680 | Make_Indexed_Component (Loc, | |
6681 | Prefix => | |
19fb051c | 6682 | Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)), |
996ae0b0 RK |
6683 | Expressions => Parameter_Associations (N)); |
6684 | ||
5cc9353d RD |
6685 | -- Since we are correcting a node classification error made by the |
6686 | -- parser, we call Replace rather than Rewrite. | |
996ae0b0 RK |
6687 | |
6688 | Replace (N, Index_Node); | |
6689 | Set_Etype (Prefix (N), Etype (Nam)); | |
6690 | Set_Etype (N, Typ); | |
6691 | Resolve_Indexed_Component (N, Typ); | |
6692 | return; | |
6693 | end; | |
6694 | end if; | |
6695 | ||
b7f17b20 ES |
6696 | if Ekind_In (Nam, E_Entry, E_Entry_Family) |
6697 | and then Present (PPC_Wrapper (Nam)) | |
6698 | and then Current_Scope /= PPC_Wrapper (Nam) | |
6699 | then | |
468ee96a | 6700 | -- Rewrite as call to the precondition wrapper, adding the task |
5cc9353d RD |
6701 | -- object to the list of actuals. If the call is to a member of an |
6702 | -- entry family, include the index as well. | |
b7f17b20 ES |
6703 | |
6704 | declare | |
468ee96a | 6705 | New_Call : Node_Id; |
b7f17b20 | 6706 | New_Actuals : List_Id; |
19fb051c | 6707 | |
b7f17b20 ES |
6708 | begin |
6709 | New_Actuals := New_List (Obj); | |
3fd9f17c AC |
6710 | |
6711 | if Nkind (Entry_Name) = N_Indexed_Component then | |
6712 | Append_To (New_Actuals, | |
6713 | New_Copy_Tree (First (Expressions (Entry_Name)))); | |
6714 | end if; | |
6715 | ||
b7f17b20 | 6716 | Append_List (Parameter_Associations (N), New_Actuals); |
468ee96a AC |
6717 | New_Call := |
6718 | Make_Procedure_Call_Statement (Loc, | |
6719 | Name => | |
6720 | New_Occurrence_Of (PPC_Wrapper (Nam), Loc), | |
6721 | Parameter_Associations => New_Actuals); | |
b7f17b20 ES |
6722 | Rewrite (N, New_Call); |
6723 | Analyze_And_Resolve (N); | |
6724 | return; | |
6725 | end; | |
6726 | end if; | |
6727 | ||
996ae0b0 | 6728 | -- The operation name may have been overloaded. Order the actuals |
5cc9353d RD |
6729 | -- according to the formals of the resolved entity, and set the return |
6730 | -- type to that of the operation. | |
996ae0b0 RK |
6731 | |
6732 | if Was_Over then | |
6733 | Normalize_Actuals (N, Nam, False, Norm_OK); | |
6734 | pragma Assert (Norm_OK); | |
fbf5a39b | 6735 | Set_Etype (N, Etype (Nam)); |
996ae0b0 RK |
6736 | end if; |
6737 | ||
6738 | Resolve_Actuals (N, Nam); | |
ae6ede77 AC |
6739 | |
6740 | -- Create a call reference to the entry | |
6741 | ||
6742 | Generate_Reference (Nam, Entry_Name, 's'); | |
996ae0b0 | 6743 | |
8a95f4e8 | 6744 | if Ekind_In (Nam, E_Entry, E_Entry_Family) then |
996ae0b0 RK |
6745 | Check_Potentially_Blocking_Operation (N); |
6746 | end if; | |
6747 | ||
6748 | -- Verify that a procedure call cannot masquerade as an entry | |
6749 | -- call where an entry call is expected. | |
6750 | ||
6751 | if Ekind (Nam) = E_Procedure then | |
996ae0b0 RK |
6752 | if Nkind (Parent (N)) = N_Entry_Call_Alternative |
6753 | and then N = Entry_Call_Statement (Parent (N)) | |
6754 | then | |
6755 | Error_Msg_N ("entry call required in select statement", N); | |
6756 | ||
6757 | elsif Nkind (Parent (N)) = N_Triggering_Alternative | |
6758 | and then N = Triggering_Statement (Parent (N)) | |
6759 | then | |
6760 | Error_Msg_N ("triggering statement cannot be procedure call", N); | |
6761 | ||
6762 | elsif Ekind (Scope (Nam)) = E_Task_Type | |
6763 | and then not In_Open_Scopes (Scope (Nam)) | |
6764 | then | |
758c442c | 6765 | Error_Msg_N ("task has no entry with this name", Entry_Name); |
996ae0b0 RK |
6766 | end if; |
6767 | end if; | |
6768 | ||
d81b4bfe TQ |
6769 | -- After resolution, entry calls and protected procedure calls are |
6770 | -- changed into entry calls, for expansion. The structure of the node | |
6771 | -- does not change, so it can safely be done in place. Protected | |
6772 | -- function calls must keep their structure because they are | |
6773 | -- subexpressions. | |
996ae0b0 RK |
6774 | |
6775 | if Ekind (Nam) /= E_Function then | |
6776 | ||
6777 | -- A protected operation that is not a function may modify the | |
d81b4bfe TQ |
6778 | -- corresponding object, and cannot apply to a constant. If this |
6779 | -- is an internal call, the prefix is the type itself. | |
996ae0b0 RK |
6780 | |
6781 | if Is_Protected_Type (Scope (Nam)) | |
6782 | and then not Is_Variable (Obj) | |
6783 | and then (not Is_Entity_Name (Obj) | |
6784 | or else not Is_Type (Entity (Obj))) | |
6785 | then | |
6786 | Error_Msg_N | |
6787 | ("prefix of protected procedure or entry call must be variable", | |
6788 | Entry_Name); | |
6789 | end if; | |
6790 | ||
6791 | Actuals := Parameter_Associations (N); | |
6792 | First_Named := First_Named_Actual (N); | |
6793 | ||
6794 | Rewrite (N, | |
6795 | Make_Entry_Call_Statement (Loc, | |
6796 | Name => Entry_Name, | |
6797 | Parameter_Associations => Actuals)); | |
6798 | ||
6799 | Set_First_Named_Actual (N, First_Named); | |
6800 | Set_Analyzed (N, True); | |
6801 | ||
6802 | -- Protected functions can return on the secondary stack, in which | |
1420b484 | 6803 | -- case we must trigger the transient scope mechanism. |
996ae0b0 | 6804 | |
da94696d | 6805 | elsif Full_Expander_Active |
996ae0b0 RK |
6806 | and then Requires_Transient_Scope (Etype (Nam)) |
6807 | then | |
0669bebe | 6808 | Establish_Transient_Scope (N, Sec_Stack => True); |
996ae0b0 | 6809 | end if; |
996ae0b0 RK |
6810 | end Resolve_Entry_Call; |
6811 | ||
6812 | ------------------------- | |
6813 | -- Resolve_Equality_Op -- | |
6814 | ------------------------- | |
6815 | ||
d81b4bfe TQ |
6816 | -- Both arguments must have the same type, and the boolean context does |
6817 | -- not participate in the resolution. The first pass verifies that the | |
6818 | -- interpretation is not ambiguous, and the type of the left argument is | |
6819 | -- correctly set, or is Any_Type in case of ambiguity. If both arguments | |
6820 | -- are strings or aggregates, allocators, or Null, they are ambiguous even | |
6821 | -- though they carry a single (universal) type. Diagnose this case here. | |
996ae0b0 RK |
6822 | |
6823 | procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is | |
6824 | L : constant Node_Id := Left_Opnd (N); | |
6825 | R : constant Node_Id := Right_Opnd (N); | |
6826 | T : Entity_Id := Find_Unique_Type (L, R); | |
6827 | ||
a8930b80 AC |
6828 | procedure Check_Conditional_Expression (Cond : Node_Id); |
6829 | -- The resolution rule for conditional expressions requires that each | |
6830 | -- such must have a unique type. This means that if several dependent | |
6831 | -- expressions are of a non-null anonymous access type, and the context | |
6832 | -- does not impose an expected type (as can be the case in an equality | |
6833 | -- operation) the expression must be rejected. | |
6834 | ||
996ae0b0 RK |
6835 | function Find_Unique_Access_Type return Entity_Id; |
6836 | -- In the case of allocators, make a last-ditch attempt to find a single | |
6837 | -- access type with the right designated type. This is semantically | |
6838 | -- dubious, and of no interest to any real code, but c48008a makes it | |
6839 | -- all worthwhile. | |
6840 | ||
a8930b80 AC |
6841 | ---------------------------------- |
6842 | -- Check_Conditional_Expression -- | |
6843 | ---------------------------------- | |
6844 | ||
6845 | procedure Check_Conditional_Expression (Cond : Node_Id) is | |
6846 | Then_Expr : Node_Id; | |
6847 | Else_Expr : Node_Id; | |
6848 | ||
6849 | begin | |
6850 | if Nkind (Cond) = N_Conditional_Expression then | |
6851 | Then_Expr := Next (First (Expressions (Cond))); | |
6852 | Else_Expr := Next (Then_Expr); | |
6853 | ||
6854 | if Nkind (Then_Expr) /= N_Null | |
6855 | and then Nkind (Else_Expr) /= N_Null | |
6856 | then | |
6857 | Error_Msg_N | |
6858 | ("cannot determine type of conditional expression", Cond); | |
6859 | end if; | |
6860 | end if; | |
6861 | end Check_Conditional_Expression; | |
6862 | ||
996ae0b0 RK |
6863 | ----------------------------- |
6864 | -- Find_Unique_Access_Type -- | |
6865 | ----------------------------- | |
6866 | ||
6867 | function Find_Unique_Access_Type return Entity_Id is | |
6868 | Acc : Entity_Id; | |
6869 | E : Entity_Id; | |
1420b484 | 6870 | S : Entity_Id; |
996ae0b0 RK |
6871 | |
6872 | begin | |
6873 | if Ekind (Etype (R)) = E_Allocator_Type then | |
6874 | Acc := Designated_Type (Etype (R)); | |
996ae0b0 RK |
6875 | elsif Ekind (Etype (L)) = E_Allocator_Type then |
6876 | Acc := Designated_Type (Etype (L)); | |
996ae0b0 RK |
6877 | else |
6878 | return Empty; | |
6879 | end if; | |
6880 | ||
1420b484 | 6881 | S := Current_Scope; |
996ae0b0 RK |
6882 | while S /= Standard_Standard loop |
6883 | E := First_Entity (S); | |
996ae0b0 | 6884 | while Present (E) loop |
996ae0b0 RK |
6885 | if Is_Type (E) |
6886 | and then Is_Access_Type (E) | |
6887 | and then Ekind (E) /= E_Allocator_Type | |
6888 | and then Designated_Type (E) = Base_Type (Acc) | |
6889 | then | |
6890 | return E; | |
6891 | end if; | |
6892 | ||
6893 | Next_Entity (E); | |
6894 | end loop; | |
6895 | ||
6896 | S := Scope (S); | |
6897 | end loop; | |
6898 | ||
6899 | return Empty; | |
6900 | end Find_Unique_Access_Type; | |
6901 | ||
6902 | -- Start of processing for Resolve_Equality_Op | |
6903 | ||
6904 | begin | |
6905 | Set_Etype (N, Base_Type (Typ)); | |
6906 | Generate_Reference (T, N, ' '); | |
6907 | ||
6908 | if T = Any_Fixed then | |
6909 | T := Unique_Fixed_Point_Type (L); | |
6910 | end if; | |
6911 | ||
6912 | if T /= Any_Type then | |
19fb051c AC |
6913 | if T = Any_String or else |
6914 | T = Any_Composite or else | |
6915 | T = Any_Character | |
996ae0b0 | 6916 | then |
996ae0b0 RK |
6917 | if T = Any_Character then |
6918 | Ambiguous_Character (L); | |
6919 | else | |
6920 | Error_Msg_N ("ambiguous operands for equality", N); | |
6921 | end if; | |
6922 | ||
6923 | Set_Etype (N, Any_Type); | |
6924 | return; | |
6925 | ||
6926 | elsif T = Any_Access | |
964f13da | 6927 | or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type) |
996ae0b0 RK |
6928 | then |
6929 | T := Find_Unique_Access_Type; | |
6930 | ||
6931 | if No (T) then | |
6932 | Error_Msg_N ("ambiguous operands for equality", N); | |
6933 | Set_Etype (N, Any_Type); | |
6934 | return; | |
6935 | end if; | |
a8930b80 AC |
6936 | |
6937 | -- Conditional expressions must have a single type, and if the | |
6938 | -- context does not impose one the dependent expressions cannot | |
6939 | -- be anonymous access types. | |
6940 | ||
6941 | elsif Ada_Version >= Ada_2012 | |
ae2aa109 AC |
6942 | and then Ekind_In (Etype (L), E_Anonymous_Access_Type, |
6943 | E_Anonymous_Access_Subprogram_Type) | |
6944 | and then Ekind_In (Etype (R), E_Anonymous_Access_Type, | |
6945 | E_Anonymous_Access_Subprogram_Type) | |
a8930b80 AC |
6946 | then |
6947 | Check_Conditional_Expression (L); | |
6948 | Check_Conditional_Expression (R); | |
996ae0b0 RK |
6949 | end if; |
6950 | ||
996ae0b0 RK |
6951 | Resolve (L, T); |
6952 | Resolve (R, T); | |
fbf5a39b | 6953 | |
2ba431e5 YM |
6954 | -- In SPARK, equality operators = and /= for array types other than |
6955 | -- String are only defined when, for each index position, the | |
6956 | -- operands have equal static bounds. | |
b0186f71 | 6957 | |
975c6896 | 6958 | if Is_Array_Type (T) then |
7b98672f YM |
6959 | -- Protect call to Matching_Static_Array_Bounds to avoid costly |
6960 | -- operation if not needed. | |
6961 | ||
6962 | if Restriction_Check_Required (SPARK) | |
6963 | and then Base_Type (T) /= Standard_String | |
975c6896 YM |
6964 | and then Base_Type (Etype (L)) = Base_Type (Etype (R)) |
6965 | and then Etype (L) /= Any_Composite -- or else L in error | |
6966 | and then Etype (R) /= Any_Composite -- or else R in error | |
6967 | and then not Matching_Static_Array_Bounds (Etype (L), Etype (R)) | |
6968 | then | |
6969 | Check_SPARK_Restriction | |
6970 | ("array types should have matching static bounds", N); | |
6971 | end if; | |
b0186f71 AC |
6972 | end if; |
6973 | ||
0669bebe GB |
6974 | -- If the unique type is a class-wide type then it will be expanded |
6975 | -- into a dispatching call to the predefined primitive. Therefore we | |
6976 | -- check here for potential violation of such restriction. | |
6977 | ||
6978 | if Is_Class_Wide_Type (T) then | |
6979 | Check_Restriction (No_Dispatching_Calls, N); | |
6980 | end if; | |
6981 | ||
fbf5a39b AC |
6982 | if Warn_On_Redundant_Constructs |
6983 | and then Comes_From_Source (N) | |
6984 | and then Is_Entity_Name (R) | |
6985 | and then Entity (R) = Standard_True | |
6986 | and then Comes_From_Source (R) | |
6987 | then | |
305caf42 AC |
6988 | Error_Msg_N -- CODEFIX |
6989 | ("?comparison with True is redundant!", R); | |
fbf5a39b AC |
6990 | end if; |
6991 | ||
996ae0b0 RK |
6992 | Check_Unset_Reference (L); |
6993 | Check_Unset_Reference (R); | |
fbf5a39b | 6994 | Generate_Operator_Reference (N, T); |
fad0600d | 6995 | Check_Low_Bound_Tested (N); |
996ae0b0 RK |
6996 | |
6997 | -- If this is an inequality, it may be the implicit inequality | |
6998 | -- created for a user-defined operation, in which case the corres- | |
6999 | -- ponding equality operation is not intrinsic, and the operation | |
7000 | -- cannot be constant-folded. Else fold. | |
7001 | ||
7002 | if Nkind (N) = N_Op_Eq | |
7003 | or else Comes_From_Source (Entity (N)) | |
7004 | or else Ekind (Entity (N)) = E_Operator | |
7005 | or else Is_Intrinsic_Subprogram | |
19fb051c | 7006 | (Corresponding_Equality (Entity (N))) |
996ae0b0 | 7007 | then |
dec6faf1 | 7008 | Analyze_Dimension (N); |
996ae0b0 | 7009 | Eval_Relational_Op (N); |
45fc7ddb | 7010 | |
996ae0b0 | 7011 | elsif Nkind (N) = N_Op_Ne |
0669bebe | 7012 | and then Is_Abstract_Subprogram (Entity (N)) |
996ae0b0 RK |
7013 | then |
7014 | Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N)); | |
7015 | end if; | |
758c442c | 7016 | |
d81b4bfe TQ |
7017 | -- Ada 2005: If one operand is an anonymous access type, convert the |
7018 | -- other operand to it, to ensure that the underlying types match in | |
7019 | -- the back-end. Same for access_to_subprogram, and the conversion | |
7020 | -- verifies that the types are subtype conformant. | |
b7d1f17f | 7021 | |
d81b4bfe TQ |
7022 | -- We apply the same conversion in the case one of the operands is a |
7023 | -- private subtype of the type of the other. | |
c8ef728f | 7024 | |
b7d1f17f HK |
7025 | -- Why the Expander_Active test here ??? |
7026 | ||
11fa950b | 7027 | if Full_Expander_Active |
b7d1f17f | 7028 | and then |
964f13da RD |
7029 | (Ekind_In (T, E_Anonymous_Access_Type, |
7030 | E_Anonymous_Access_Subprogram_Type) | |
b7d1f17f | 7031 | or else Is_Private_Type (T)) |
c8ef728f ES |
7032 | then |
7033 | if Etype (L) /= T then | |
7034 | Rewrite (L, | |
7035 | Make_Unchecked_Type_Conversion (Sloc (L), | |
7036 | Subtype_Mark => New_Occurrence_Of (T, Sloc (L)), | |
7037 | Expression => Relocate_Node (L))); | |
7038 | Analyze_And_Resolve (L, T); | |
7039 | end if; | |
7040 | ||
7041 | if (Etype (R)) /= T then | |
7042 | Rewrite (R, | |
7043 | Make_Unchecked_Type_Conversion (Sloc (R), | |
7044 | Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)), | |
7045 | Expression => Relocate_Node (R))); | |
7046 | Analyze_And_Resolve (R, T); | |
7047 | end if; | |
7048 | end if; | |
996ae0b0 RK |
7049 | end if; |
7050 | end Resolve_Equality_Op; | |
7051 | ||
7052 | ---------------------------------- | |
7053 | -- Resolve_Explicit_Dereference -- | |
7054 | ---------------------------------- | |
7055 | ||
7056 | procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is | |
bc5f3720 RD |
7057 | Loc : constant Source_Ptr := Sloc (N); |
7058 | New_N : Node_Id; | |
7059 | P : constant Node_Id := Prefix (N); | |
7060 | I : Interp_Index; | |
7061 | It : Interp; | |
996ae0b0 RK |
7062 | |
7063 | begin | |
c8ef728f | 7064 | Check_Fully_Declared_Prefix (Typ, P); |
996ae0b0 RK |
7065 | |
7066 | if Is_Overloaded (P) then | |
7067 | ||
758c442c GD |
7068 | -- Use the context type to select the prefix that has the correct |
7069 | -- designated type. | |
996ae0b0 RK |
7070 | |
7071 | Get_First_Interp (P, I, It); | |
7072 | while Present (It.Typ) loop | |
7073 | exit when Is_Access_Type (It.Typ) | |
7074 | and then Covers (Typ, Designated_Type (It.Typ)); | |
996ae0b0 RK |
7075 | Get_Next_Interp (I, It); |
7076 | end loop; | |
7077 | ||
bc5f3720 RD |
7078 | if Present (It.Typ) then |
7079 | Resolve (P, It.Typ); | |
7080 | else | |
758c442c GD |
7081 | -- If no interpretation covers the designated type of the prefix, |
7082 | -- this is the pathological case where not all implementations of | |
7083 | -- the prefix allow the interpretation of the node as a call. Now | |
7084 | -- that the expected type is known, Remove other interpretations | |
7085 | -- from prefix, rewrite it as a call, and resolve again, so that | |
7086 | -- the proper call node is generated. | |
bc5f3720 RD |
7087 | |
7088 | Get_First_Interp (P, I, It); | |
7089 | while Present (It.Typ) loop | |
7090 | if Ekind (It.Typ) /= E_Access_Subprogram_Type then | |
7091 | Remove_Interp (I); | |
7092 | end if; | |
7093 | ||
7094 | Get_Next_Interp (I, It); | |
7095 | end loop; | |
7096 | ||
7097 | New_N := | |
7098 | Make_Function_Call (Loc, | |
7099 | Name => | |
7100 | Make_Explicit_Dereference (Loc, | |
7101 | Prefix => P), | |
7102 | Parameter_Associations => New_List); | |
7103 | ||
7104 | Save_Interps (N, New_N); | |
7105 | Rewrite (N, New_N); | |
7106 | Analyze_And_Resolve (N, Typ); | |
7107 | return; | |
7108 | end if; | |
7109 | ||
996ae0b0 RK |
7110 | Set_Etype (N, Designated_Type (It.Typ)); |
7111 | ||
7112 | else | |
fbf5a39b | 7113 | Resolve (P); |
996ae0b0 RK |
7114 | end if; |
7115 | ||
7116 | if Is_Access_Type (Etype (P)) then | |
7117 | Apply_Access_Check (N); | |
7118 | end if; | |
7119 | ||
758c442c GD |
7120 | -- If the designated type is a packed unconstrained array type, and the |
7121 | -- explicit dereference is not in the context of an attribute reference, | |
7122 | -- then we must compute and set the actual subtype, since it is needed | |
7123 | -- by Gigi. The reason we exclude the attribute case is that this is | |
7124 | -- handled fine by Gigi, and in fact we use such attributes to build the | |
7125 | -- actual subtype. We also exclude generated code (which builds actual | |
7126 | -- subtypes directly if they are needed). | |
996ae0b0 RK |
7127 | |
7128 | if Is_Array_Type (Etype (N)) | |
7129 | and then Is_Packed (Etype (N)) | |
7130 | and then not Is_Constrained (Etype (N)) | |
7131 | and then Nkind (Parent (N)) /= N_Attribute_Reference | |
7132 | and then Comes_From_Source (N) | |
7133 | then | |
7134 | Set_Etype (N, Get_Actual_Subtype (N)); | |
7135 | end if; | |
7136 | ||
09494c32 AC |
7137 | -- Note: No Eval processing is required for an explicit dereference, |
7138 | -- because such a name can never be static. | |
996ae0b0 RK |
7139 | |
7140 | end Resolve_Explicit_Dereference; | |
7141 | ||
955871d3 AC |
7142 | ------------------------------------- |
7143 | -- Resolve_Expression_With_Actions -- | |
7144 | ------------------------------------- | |
7145 | ||
7146 | procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is | |
7147 | begin | |
7148 | Set_Etype (N, Typ); | |
7149 | end Resolve_Expression_With_Actions; | |
7150 | ||
996ae0b0 RK |
7151 | ------------------------------- |
7152 | -- Resolve_Indexed_Component -- | |
7153 | ------------------------------- | |
7154 | ||
7155 | procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is | |
7156 | Name : constant Node_Id := Prefix (N); | |
7157 | Expr : Node_Id; | |
7158 | Array_Type : Entity_Id := Empty; -- to prevent junk warning | |
7159 | Index : Node_Id; | |
7160 | ||
7161 | begin | |
7162 | if Is_Overloaded (Name) then | |
7163 | ||
758c442c GD |
7164 | -- Use the context type to select the prefix that yields the correct |
7165 | -- component type. | |
996ae0b0 RK |
7166 | |
7167 | declare | |
7168 | I : Interp_Index; | |
7169 | It : Interp; | |
7170 | I1 : Interp_Index := 0; | |
7171 | P : constant Node_Id := Prefix (N); | |
7172 | Found : Boolean := False; | |
7173 | ||
7174 | begin | |
7175 | Get_First_Interp (P, I, It); | |
996ae0b0 | 7176 | while Present (It.Typ) loop |
996ae0b0 RK |
7177 | if (Is_Array_Type (It.Typ) |
7178 | and then Covers (Typ, Component_Type (It.Typ))) | |
7179 | or else (Is_Access_Type (It.Typ) | |
7180 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
19fb051c AC |
7181 | and then |
7182 | Covers | |
7183 | (Typ, | |
7184 | Component_Type (Designated_Type (It.Typ)))) | |
996ae0b0 RK |
7185 | then |
7186 | if Found then | |
7187 | It := Disambiguate (P, I1, I, Any_Type); | |
7188 | ||
7189 | if It = No_Interp then | |
7190 | Error_Msg_N ("ambiguous prefix for indexing", N); | |
7191 | Set_Etype (N, Typ); | |
7192 | return; | |
7193 | ||
7194 | else | |
7195 | Found := True; | |
7196 | Array_Type := It.Typ; | |
7197 | I1 := I; | |
7198 | end if; | |
7199 | ||
7200 | else | |
7201 | Found := True; | |
7202 | Array_Type := It.Typ; | |
7203 | I1 := I; | |
7204 | end if; | |
7205 | end if; | |
7206 | ||
7207 | Get_Next_Interp (I, It); | |
7208 | end loop; | |
7209 | end; | |
7210 | ||
7211 | else | |
7212 | Array_Type := Etype (Name); | |
7213 | end if; | |
7214 | ||
7215 | Resolve (Name, Array_Type); | |
7216 | Array_Type := Get_Actual_Subtype_If_Available (Name); | |
7217 | ||
7218 | -- If prefix is access type, dereference to get real array type. | |
7219 | -- Note: we do not apply an access check because the expander always | |
7220 | -- introduces an explicit dereference, and the check will happen there. | |
7221 | ||
7222 | if Is_Access_Type (Array_Type) then | |
7223 | Array_Type := Designated_Type (Array_Type); | |
7224 | end if; | |
7225 | ||
a77842bd | 7226 | -- If name was overloaded, set component type correctly now |
f3d57416 | 7227 | -- If a misplaced call to an entry family (which has no index types) |
b7d1f17f | 7228 | -- return. Error will be diagnosed from calling context. |
996ae0b0 | 7229 | |
b7d1f17f HK |
7230 | if Is_Array_Type (Array_Type) then |
7231 | Set_Etype (N, Component_Type (Array_Type)); | |
7232 | else | |
7233 | return; | |
7234 | end if; | |
996ae0b0 RK |
7235 | |
7236 | Index := First_Index (Array_Type); | |
7237 | Expr := First (Expressions (N)); | |
7238 | ||
758c442c GD |
7239 | -- The prefix may have resolved to a string literal, in which case its |
7240 | -- etype has a special representation. This is only possible currently | |
7241 | -- if the prefix is a static concatenation, written in functional | |
7242 | -- notation. | |
996ae0b0 RK |
7243 | |
7244 | if Ekind (Array_Type) = E_String_Literal_Subtype then | |
7245 | Resolve (Expr, Standard_Positive); | |
7246 | ||
7247 | else | |
7248 | while Present (Index) and Present (Expr) loop | |
7249 | Resolve (Expr, Etype (Index)); | |
7250 | Check_Unset_Reference (Expr); | |
7251 | ||
7252 | if Is_Scalar_Type (Etype (Expr)) then | |
7253 | Apply_Scalar_Range_Check (Expr, Etype (Index)); | |
7254 | else | |
7255 | Apply_Range_Check (Expr, Get_Actual_Subtype (Index)); | |
7256 | end if; | |
7257 | ||
7258 | Next_Index (Index); | |
7259 | Next (Expr); | |
7260 | end loop; | |
7261 | end if; | |
7262 | ||
dec6faf1 AC |
7263 | Analyze_Dimension (N); |
7264 | ||
0669bebe GB |
7265 | -- Do not generate the warning on suspicious index if we are analyzing |
7266 | -- package Ada.Tags; otherwise we will report the warning with the | |
7267 | -- Prims_Ptr field of the dispatch table. | |
7268 | ||
7269 | if Scope (Etype (Prefix (N))) = Standard_Standard | |
7270 | or else not | |
7271 | Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))), | |
7272 | Ada_Tags) | |
7273 | then | |
7274 | Warn_On_Suspicious_Index (Name, First (Expressions (N))); | |
7275 | Eval_Indexed_Component (N); | |
7276 | end if; | |
c28408b7 RD |
7277 | |
7278 | -- If the array type is atomic, and is packed, and we are in a left side | |
7279 | -- context, then this is worth a warning, since we have a situation | |
7280 | -- where the access to the component may cause extra read/writes of | |
7281 | -- the atomic array object, which could be considered unexpected. | |
7282 | ||
7283 | if Nkind (N) = N_Indexed_Component | |
7284 | and then (Is_Atomic (Array_Type) | |
7285 | or else (Is_Entity_Name (Prefix (N)) | |
7286 | and then Is_Atomic (Entity (Prefix (N))))) | |
7287 | and then Is_Bit_Packed_Array (Array_Type) | |
7288 | and then Is_LHS (N) | |
7289 | then | |
7290 | Error_Msg_N ("?assignment to component of packed atomic array", | |
7291 | Prefix (N)); | |
7292 | Error_Msg_N ("?\may cause unexpected accesses to atomic object", | |
7293 | Prefix (N)); | |
7294 | end if; | |
996ae0b0 RK |
7295 | end Resolve_Indexed_Component; |
7296 | ||
7297 | ----------------------------- | |
7298 | -- Resolve_Integer_Literal -- | |
7299 | ----------------------------- | |
7300 | ||
7301 | procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is | |
7302 | begin | |
7303 | Set_Etype (N, Typ); | |
7304 | Eval_Integer_Literal (N); | |
7305 | end Resolve_Integer_Literal; | |
7306 | ||
15ce9ca2 AC |
7307 | -------------------------------- |
7308 | -- Resolve_Intrinsic_Operator -- | |
7309 | -------------------------------- | |
996ae0b0 RK |
7310 | |
7311 | procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is | |
bb481772 AC |
7312 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); |
7313 | Op : Entity_Id; | |
7314 | Orig_Op : constant Entity_Id := Entity (N); | |
7315 | Arg1 : Node_Id; | |
7316 | Arg2 : Node_Id; | |
996ae0b0 | 7317 | |
78efd712 AC |
7318 | function Convert_Operand (Opnd : Node_Id) return Node_Id; |
7319 | -- If the operand is a literal, it cannot be the expression in a | |
7320 | -- conversion. Use a qualified expression instead. | |
7321 | ||
7322 | function Convert_Operand (Opnd : Node_Id) return Node_Id is | |
7323 | Loc : constant Source_Ptr := Sloc (Opnd); | |
7324 | Res : Node_Id; | |
7325 | begin | |
7326 | if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then | |
7327 | Res := | |
7328 | Make_Qualified_Expression (Loc, | |
7329 | Subtype_Mark => New_Occurrence_Of (Btyp, Loc), | |
7330 | Expression => Relocate_Node (Opnd)); | |
7331 | Analyze (Res); | |
7332 | ||
7333 | else | |
7334 | Res := Unchecked_Convert_To (Btyp, Opnd); | |
7335 | end if; | |
7336 | ||
7337 | return Res; | |
7338 | end Convert_Operand; | |
7339 | ||
d72e7628 | 7340 | -- Start of processing for Resolve_Intrinsic_Operator |
7109f4f5 | 7341 | |
996ae0b0 | 7342 | begin |
305caf42 AC |
7343 | -- We must preserve the original entity in a generic setting, so that |
7344 | -- the legality of the operation can be verified in an instance. | |
7345 | ||
11fa950b | 7346 | if not Full_Expander_Active then |
305caf42 AC |
7347 | return; |
7348 | end if; | |
7349 | ||
996ae0b0 | 7350 | Op := Entity (N); |
996ae0b0 RK |
7351 | while Scope (Op) /= Standard_Standard loop |
7352 | Op := Homonym (Op); | |
7353 | pragma Assert (Present (Op)); | |
7354 | end loop; | |
7355 | ||
7356 | Set_Entity (N, Op); | |
af152989 | 7357 | Set_Is_Overloaded (N, False); |
996ae0b0 | 7358 | |
7109f4f5 AC |
7359 | -- If the result or operand types are private, rewrite with unchecked |
7360 | -- conversions on the operands and the result, to expose the proper | |
7361 | -- underlying numeric type. | |
996ae0b0 | 7362 | |
7109f4f5 AC |
7363 | if Is_Private_Type (Typ) |
7364 | or else Is_Private_Type (Etype (Left_Opnd (N))) | |
7365 | or else Is_Private_Type (Etype (Right_Opnd (N))) | |
7366 | then | |
78efd712 AC |
7367 | Arg1 := Convert_Operand (Left_Opnd (N)); |
7368 | -- Unchecked_Convert_To (Btyp, Left_Opnd (N)); | |
aeae67ed | 7369 | -- What on earth is this commented out fragment of code??? |
fbf5a39b AC |
7370 | |
7371 | if Nkind (N) = N_Op_Expon then | |
7372 | Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N)); | |
7373 | else | |
78efd712 | 7374 | Arg2 := Convert_Operand (Right_Opnd (N)); |
fbf5a39b AC |
7375 | end if; |
7376 | ||
bb481772 AC |
7377 | if Nkind (Arg1) = N_Type_Conversion then |
7378 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
7379 | end if; | |
7380 | ||
7381 | if Nkind (Arg2) = N_Type_Conversion then | |
7382 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
7383 | end if; | |
996ae0b0 | 7384 | |
fbf5a39b AC |
7385 | Set_Left_Opnd (N, Arg1); |
7386 | Set_Right_Opnd (N, Arg2); | |
7387 | ||
7388 | Set_Etype (N, Btyp); | |
7389 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7390 | Resolve (N, Typ); | |
7391 | ||
7392 | elsif Typ /= Etype (Left_Opnd (N)) | |
7393 | or else Typ /= Etype (Right_Opnd (N)) | |
7394 | then | |
d81b4bfe | 7395 | -- Add explicit conversion where needed, and save interpretations in |
bb481772 AC |
7396 | -- case operands are overloaded. If the context is a VMS operation, |
7397 | -- assert that the conversion is legal (the operands have the proper | |
7398 | -- types to select the VMS intrinsic). Note that in rare cases the | |
7399 | -- VMS operators may be visible, but the default System is being used | |
7400 | -- and Address is a private type. | |
fbf5a39b | 7401 | |
af152989 | 7402 | Arg1 := Convert_To (Typ, Left_Opnd (N)); |
fbf5a39b AC |
7403 | Arg2 := Convert_To (Typ, Right_Opnd (N)); |
7404 | ||
7405 | if Nkind (Arg1) = N_Type_Conversion then | |
7406 | Save_Interps (Left_Opnd (N), Expression (Arg1)); | |
bb481772 AC |
7407 | |
7408 | if Is_VMS_Operator (Orig_Op) then | |
7409 | Set_Conversion_OK (Arg1); | |
7410 | end if; | |
af152989 AC |
7411 | else |
7412 | Save_Interps (Left_Opnd (N), Arg1); | |
fbf5a39b AC |
7413 | end if; |
7414 | ||
7415 | if Nkind (Arg2) = N_Type_Conversion then | |
7416 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
bb481772 AC |
7417 | |
7418 | if Is_VMS_Operator (Orig_Op) then | |
7419 | Set_Conversion_OK (Arg2); | |
7420 | end if; | |
af152989 | 7421 | else |
0ab80019 | 7422 | Save_Interps (Right_Opnd (N), Arg2); |
fbf5a39b AC |
7423 | end if; |
7424 | ||
7425 | Rewrite (Left_Opnd (N), Arg1); | |
7426 | Rewrite (Right_Opnd (N), Arg2); | |
7427 | Analyze (Arg1); | |
7428 | Analyze (Arg2); | |
7429 | Resolve_Arithmetic_Op (N, Typ); | |
7430 | ||
7431 | else | |
7432 | Resolve_Arithmetic_Op (N, Typ); | |
7433 | end if; | |
996ae0b0 RK |
7434 | end Resolve_Intrinsic_Operator; |
7435 | ||
fbf5a39b AC |
7436 | -------------------------------------- |
7437 | -- Resolve_Intrinsic_Unary_Operator -- | |
7438 | -------------------------------------- | |
7439 | ||
7440 | procedure Resolve_Intrinsic_Unary_Operator | |
7441 | (N : Node_Id; | |
7442 | Typ : Entity_Id) | |
7443 | is | |
7444 | Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ)); | |
7445 | Op : Entity_Id; | |
7446 | Arg2 : Node_Id; | |
7447 | ||
7448 | begin | |
7449 | Op := Entity (N); | |
fbf5a39b AC |
7450 | while Scope (Op) /= Standard_Standard loop |
7451 | Op := Homonym (Op); | |
7452 | pragma Assert (Present (Op)); | |
7453 | end loop; | |
7454 | ||
7455 | Set_Entity (N, Op); | |
7456 | ||
7457 | if Is_Private_Type (Typ) then | |
7458 | Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N)); | |
7459 | Save_Interps (Right_Opnd (N), Expression (Arg2)); | |
7460 | ||
7461 | Set_Right_Opnd (N, Arg2); | |
7462 | ||
7463 | Set_Etype (N, Btyp); | |
7464 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7465 | Resolve (N, Typ); | |
7466 | ||
7467 | else | |
7468 | Resolve_Unary_Op (N, Typ); | |
7469 | end if; | |
7470 | end Resolve_Intrinsic_Unary_Operator; | |
7471 | ||
996ae0b0 RK |
7472 | ------------------------ |
7473 | -- Resolve_Logical_Op -- | |
7474 | ------------------------ | |
7475 | ||
7476 | procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is | |
7477 | B_Typ : Entity_Id; | |
7478 | ||
7479 | begin | |
f61580d4 AC |
7480 | Check_No_Direct_Boolean_Operators (N); |
7481 | ||
758c442c GD |
7482 | -- Predefined operations on scalar types yield the base type. On the |
7483 | -- other hand, logical operations on arrays yield the type of the | |
7484 | -- arguments (and the context). | |
996ae0b0 RK |
7485 | |
7486 | if Is_Array_Type (Typ) then | |
7487 | B_Typ := Typ; | |
7488 | else | |
7489 | B_Typ := Base_Type (Typ); | |
7490 | end if; | |
7491 | ||
001c7783 AC |
7492 | -- OK if this is a VMS-specific intrinsic operation |
7493 | ||
7494 | if Is_VMS_Operator (Entity (N)) then | |
7495 | null; | |
7496 | ||
996ae0b0 RK |
7497 | -- The following test is required because the operands of the operation |
7498 | -- may be literals, in which case the resulting type appears to be | |
7499 | -- compatible with a signed integer type, when in fact it is compatible | |
7500 | -- only with modular types. If the context itself is universal, the | |
7501 | -- operation is illegal. | |
7502 | ||
001c7783 | 7503 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
7504 | Error_Msg_N ("invalid context for logical operation", N); |
7505 | Set_Etype (N, Any_Type); | |
7506 | return; | |
7507 | ||
7508 | elsif Typ = Any_Modular then | |
7509 | Error_Msg_N | |
7510 | ("no modular type available in this context", N); | |
7511 | Set_Etype (N, Any_Type); | |
7512 | return; | |
19fb051c | 7513 | |
07fc65c4 GB |
7514 | elsif Is_Modular_Integer_Type (Typ) |
7515 | and then Etype (Left_Opnd (N)) = Universal_Integer | |
7516 | and then Etype (Right_Opnd (N)) = Universal_Integer | |
7517 | then | |
7518 | Check_For_Visible_Operator (N, B_Typ); | |
996ae0b0 RK |
7519 | end if; |
7520 | ||
f2d10a02 AC |
7521 | -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or |
7522 | -- is active and the result type is standard Boolean (do not mess with | |
7523 | -- ops that return a nonstandard Boolean type, because something strange | |
7524 | -- is going on). | |
7525 | ||
7526 | -- Note: you might expect this replacement to be done during expansion, | |
7527 | -- but that doesn't work, because when the pragma Short_Circuit_And_Or | |
7528 | -- is used, no part of the right operand of an "and" or "or" operator | |
7529 | -- should be executed if the left operand would short-circuit the | |
7530 | -- evaluation of the corresponding "and then" or "or else". If we left | |
7531 | -- the replacement to expansion time, then run-time checks associated | |
7532 | -- with such operands would be evaluated unconditionally, due to being | |
af89615f | 7533 | -- before the condition prior to the rewriting as short-circuit forms |
f2d10a02 AC |
7534 | -- during expansion. |
7535 | ||
7536 | if Short_Circuit_And_Or | |
7537 | and then B_Typ = Standard_Boolean | |
7538 | and then Nkind_In (N, N_Op_And, N_Op_Or) | |
7539 | then | |
7540 | if Nkind (N) = N_Op_And then | |
7541 | Rewrite (N, | |
7542 | Make_And_Then (Sloc (N), | |
7543 | Left_Opnd => Relocate_Node (Left_Opnd (N)), | |
7544 | Right_Opnd => Relocate_Node (Right_Opnd (N)))); | |
7545 | Analyze_And_Resolve (N, B_Typ); | |
7546 | ||
7547 | -- Case of OR changed to OR ELSE | |
7548 | ||
7549 | else | |
7550 | Rewrite (N, | |
7551 | Make_Or_Else (Sloc (N), | |
7552 | Left_Opnd => Relocate_Node (Left_Opnd (N)), | |
7553 | Right_Opnd => Relocate_Node (Right_Opnd (N)))); | |
7554 | Analyze_And_Resolve (N, B_Typ); | |
7555 | end if; | |
7556 | ||
7557 | -- Return now, since analysis of the rewritten ops will take care of | |
7558 | -- other reference bookkeeping and expression folding. | |
7559 | ||
7560 | return; | |
7561 | end if; | |
7562 | ||
996ae0b0 RK |
7563 | Resolve (Left_Opnd (N), B_Typ); |
7564 | Resolve (Right_Opnd (N), B_Typ); | |
7565 | ||
7566 | Check_Unset_Reference (Left_Opnd (N)); | |
7567 | Check_Unset_Reference (Right_Opnd (N)); | |
7568 | ||
7569 | Set_Etype (N, B_Typ); | |
fbf5a39b | 7570 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 | 7571 | Eval_Logical_Op (N); |
9f90d123 | 7572 | |
2ba431e5 YM |
7573 | -- In SPARK, logical operations AND, OR and XOR for arrays are defined |
7574 | -- only when both operands have same static lower and higher bounds. Of | |
7575 | -- course the types have to match, so only check if operands are | |
7576 | -- compatible and the node itself has no errors. | |
9f90d123 | 7577 | |
f5afb270 AC |
7578 | if Is_Array_Type (B_Typ) |
7579 | and then Nkind (N) in N_Binary_Op | |
7580 | then | |
7581 | declare | |
7582 | Left_Typ : constant Node_Id := Etype (Left_Opnd (N)); | |
7583 | Right_Typ : constant Node_Id := Etype (Right_Opnd (N)); | |
2598ee6d | 7584 | |
f5afb270 | 7585 | begin |
7b98672f YM |
7586 | -- Protect call to Matching_Static_Array_Bounds to avoid costly |
7587 | -- operation if not needed. | |
7588 | ||
7589 | if Restriction_Check_Required (SPARK) | |
7590 | and then Base_Type (Left_Typ) = Base_Type (Right_Typ) | |
f5afb270 AC |
7591 | and then Left_Typ /= Any_Composite -- or Left_Opnd in error |
7592 | and then Right_Typ /= Any_Composite -- or Right_Opnd in error | |
7593 | and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ) | |
7594 | then | |
2ba431e5 | 7595 | Check_SPARK_Restriction |
f5afb270 AC |
7596 | ("array types should have matching static bounds", N); |
7597 | end if; | |
7598 | end; | |
7599 | end if; | |
996ae0b0 RK |
7600 | end Resolve_Logical_Op; |
7601 | ||
7602 | --------------------------- | |
7603 | -- Resolve_Membership_Op -- | |
7604 | --------------------------- | |
7605 | ||
5cc9353d RD |
7606 | -- The context can only be a boolean type, and does not determine the |
7607 | -- arguments. Arguments should be unambiguous, but the preference rule for | |
7608 | -- universal types applies. | |
996ae0b0 RK |
7609 | |
7610 | procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
7611 | pragma Warnings (Off, Typ); |
7612 | ||
197e4514 | 7613 | L : constant Node_Id := Left_Opnd (N); |
b1c11e0e | 7614 | R : constant Node_Id := Right_Opnd (N); |
996ae0b0 RK |
7615 | T : Entity_Id; |
7616 | ||
197e4514 | 7617 | procedure Resolve_Set_Membership; |
5cc9353d RD |
7618 | -- Analysis has determined a unique type for the left operand. Use it to |
7619 | -- resolve the disjuncts. | |
197e4514 AC |
7620 | |
7621 | ---------------------------- | |
7622 | -- Resolve_Set_Membership -- | |
7623 | ---------------------------- | |
7624 | ||
7625 | procedure Resolve_Set_Membership is | |
7626 | Alt : Node_Id; | |
7627 | ||
7628 | begin | |
7629 | Resolve (L, Etype (L)); | |
7630 | ||
7631 | Alt := First (Alternatives (N)); | |
7632 | while Present (Alt) loop | |
7633 | ||
7634 | -- Alternative is an expression, a range | |
7635 | -- or a subtype mark. | |
7636 | ||
7637 | if not Is_Entity_Name (Alt) | |
7638 | or else not Is_Type (Entity (Alt)) | |
7639 | then | |
7640 | Resolve (Alt, Etype (L)); | |
7641 | end if; | |
7642 | ||
7643 | Next (Alt); | |
7644 | end loop; | |
7645 | end Resolve_Set_Membership; | |
7646 | ||
442c0581 | 7647 | -- Start of processing for Resolve_Membership_Op |
197e4514 | 7648 | |
996ae0b0 RK |
7649 | begin |
7650 | if L = Error or else R = Error then | |
7651 | return; | |
7652 | end if; | |
7653 | ||
197e4514 AC |
7654 | if Present (Alternatives (N)) then |
7655 | Resolve_Set_Membership; | |
7656 | return; | |
7657 | ||
7658 | elsif not Is_Overloaded (R) | |
996ae0b0 | 7659 | and then |
19fb051c AC |
7660 | (Etype (R) = Universal_Integer |
7661 | or else | |
996ae0b0 RK |
7662 | Etype (R) = Universal_Real) |
7663 | and then Is_Overloaded (L) | |
7664 | then | |
7665 | T := Etype (R); | |
1420b484 | 7666 | |
d81b4bfe | 7667 | -- Ada 2005 (AI-251): Support the following case: |
1420b484 JM |
7668 | |
7669 | -- type I is interface; | |
7670 | -- type T is tagged ... | |
7671 | ||
c8ef728f | 7672 | -- function Test (O : I'Class) is |
1420b484 JM |
7673 | -- begin |
7674 | -- return O in T'Class. | |
7675 | -- end Test; | |
7676 | ||
d81b4bfe | 7677 | -- In this case we have nothing else to do. The membership test will be |
e7c0dd39 | 7678 | -- done at run time. |
1420b484 | 7679 | |
0791fbe9 | 7680 | elsif Ada_Version >= Ada_2005 |
1420b484 JM |
7681 | and then Is_Class_Wide_Type (Etype (L)) |
7682 | and then Is_Interface (Etype (L)) | |
7683 | and then Is_Class_Wide_Type (Etype (R)) | |
7684 | and then not Is_Interface (Etype (R)) | |
7685 | then | |
7686 | return; | |
996ae0b0 RK |
7687 | else |
7688 | T := Intersect_Types (L, R); | |
7689 | end if; | |
7690 | ||
9a0ddeee AC |
7691 | -- If mixed-mode operations are present and operands are all literal, |
7692 | -- the only interpretation involves Duration, which is probably not | |
7693 | -- the intention of the programmer. | |
7694 | ||
7695 | if T = Any_Fixed then | |
7696 | T := Unique_Fixed_Point_Type (N); | |
7697 | ||
7698 | if T = Any_Type then | |
7699 | return; | |
7700 | end if; | |
7701 | end if; | |
7702 | ||
996ae0b0 RK |
7703 | Resolve (L, T); |
7704 | Check_Unset_Reference (L); | |
7705 | ||
7706 | if Nkind (R) = N_Range | |
7707 | and then not Is_Scalar_Type (T) | |
7708 | then | |
7709 | Error_Msg_N ("scalar type required for range", R); | |
7710 | end if; | |
7711 | ||
7712 | if Is_Entity_Name (R) then | |
7713 | Freeze_Expression (R); | |
7714 | else | |
7715 | Resolve (R, T); | |
7716 | Check_Unset_Reference (R); | |
7717 | end if; | |
7718 | ||
7719 | Eval_Membership_Op (N); | |
7720 | end Resolve_Membership_Op; | |
7721 | ||
7722 | ------------------ | |
7723 | -- Resolve_Null -- | |
7724 | ------------------ | |
7725 | ||
7726 | procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is | |
b1c11e0e JM |
7727 | Loc : constant Source_Ptr := Sloc (N); |
7728 | ||
996ae0b0 | 7729 | begin |
758c442c | 7730 | -- Handle restriction against anonymous null access values This |
6ba6b1e3 | 7731 | -- restriction can be turned off using -gnatdj. |
996ae0b0 | 7732 | |
0ab80019 | 7733 | -- Ada 2005 (AI-231): Remove restriction |
2820d220 | 7734 | |
0791fbe9 | 7735 | if Ada_Version < Ada_2005 |
2820d220 | 7736 | and then not Debug_Flag_J |
996ae0b0 RK |
7737 | and then Ekind (Typ) = E_Anonymous_Access_Type |
7738 | and then Comes_From_Source (N) | |
7739 | then | |
d81b4bfe TQ |
7740 | -- In the common case of a call which uses an explicitly null value |
7741 | -- for an access parameter, give specialized error message. | |
996ae0b0 | 7742 | |
d3b00ce3 | 7743 | if Nkind (Parent (N)) in N_Subprogram_Call then |
996ae0b0 RK |
7744 | Error_Msg_N |
7745 | ("null is not allowed as argument for an access parameter", N); | |
7746 | ||
7747 | -- Standard message for all other cases (are there any?) | |
7748 | ||
7749 | else | |
7750 | Error_Msg_N | |
7751 | ("null cannot be of an anonymous access type", N); | |
7752 | end if; | |
7753 | end if; | |
7754 | ||
b1c11e0e JM |
7755 | -- Ada 2005 (AI-231): Generate the null-excluding check in case of |
7756 | -- assignment to a null-excluding object | |
7757 | ||
0791fbe9 | 7758 | if Ada_Version >= Ada_2005 |
b1c11e0e JM |
7759 | and then Can_Never_Be_Null (Typ) |
7760 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
7761 | then | |
7762 | if not Inside_Init_Proc then | |
7763 | Insert_Action | |
7764 | (Compile_Time_Constraint_Error (N, | |
7765 | "(Ada 2005) null not allowed in null-excluding objects?"), | |
7766 | Make_Raise_Constraint_Error (Loc, | |
7767 | Reason => CE_Access_Check_Failed)); | |
7768 | else | |
7769 | Insert_Action (N, | |
7770 | Make_Raise_Constraint_Error (Loc, | |
7771 | Reason => CE_Access_Check_Failed)); | |
7772 | end if; | |
7773 | end if; | |
7774 | ||
d81b4bfe TQ |
7775 | -- In a distributed context, null for a remote access to subprogram may |
7776 | -- need to be replaced with a special record aggregate. In this case, | |
7777 | -- return after having done the transformation. | |
996ae0b0 RK |
7778 | |
7779 | if (Ekind (Typ) = E_Record_Type | |
7780 | or else Is_Remote_Access_To_Subprogram_Type (Typ)) | |
7781 | and then Remote_AST_Null_Value (N, Typ) | |
7782 | then | |
7783 | return; | |
7784 | end if; | |
7785 | ||
a77842bd | 7786 | -- The null literal takes its type from the context |
996ae0b0 RK |
7787 | |
7788 | Set_Etype (N, Typ); | |
7789 | end Resolve_Null; | |
7790 | ||
7791 | ----------------------- | |
7792 | -- Resolve_Op_Concat -- | |
7793 | ----------------------- | |
7794 | ||
7795 | procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is | |
996ae0b0 | 7796 | |
10303118 BD |
7797 | -- We wish to avoid deep recursion, because concatenations are often |
7798 | -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left | |
7799 | -- operands nonrecursively until we find something that is not a simple | |
7800 | -- concatenation (A in this case). We resolve that, and then walk back | |
7801 | -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest | |
7802 | -- to do the rest of the work at each level. The Parent pointers allow | |
7803 | -- us to avoid recursion, and thus avoid running out of memory. See also | |
d81b4bfe | 7804 | -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used. |
996ae0b0 | 7805 | |
10303118 BD |
7806 | NN : Node_Id := N; |
7807 | Op1 : Node_Id; | |
996ae0b0 | 7808 | |
10303118 BD |
7809 | begin |
7810 | -- The following code is equivalent to: | |
996ae0b0 | 7811 | |
10303118 BD |
7812 | -- Resolve_Op_Concat_First (NN, Typ); |
7813 | -- Resolve_Op_Concat_Arg (N, ...); | |
7814 | -- Resolve_Op_Concat_Rest (N, Typ); | |
996ae0b0 | 7815 | |
10303118 BD |
7816 | -- where the Resolve_Op_Concat_Arg call recurses back here if the left |
7817 | -- operand is a concatenation. | |
996ae0b0 | 7818 | |
10303118 | 7819 | -- Walk down left operands |
996ae0b0 | 7820 | |
10303118 BD |
7821 | loop |
7822 | Resolve_Op_Concat_First (NN, Typ); | |
7823 | Op1 := Left_Opnd (NN); | |
7824 | exit when not (Nkind (Op1) = N_Op_Concat | |
7825 | and then not Is_Array_Type (Component_Type (Typ)) | |
7826 | and then Entity (Op1) = Entity (NN)); | |
7827 | NN := Op1; | |
7828 | end loop; | |
996ae0b0 | 7829 | |
10303118 | 7830 | -- Now (given the above example) NN is A&B and Op1 is A |
996ae0b0 | 7831 | |
10303118 | 7832 | -- First resolve Op1 ... |
9ebe3743 | 7833 | |
10303118 | 7834 | Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN)); |
9ebe3743 | 7835 | |
10303118 BD |
7836 | -- ... then walk NN back up until we reach N (where we started), calling |
7837 | -- Resolve_Op_Concat_Rest along the way. | |
9ebe3743 | 7838 | |
10303118 BD |
7839 | loop |
7840 | Resolve_Op_Concat_Rest (NN, Typ); | |
7841 | exit when NN = N; | |
7842 | NN := Parent (NN); | |
7843 | end loop; | |
2933b16c | 7844 | |
fe5d3068 | 7845 | if Base_Type (Etype (N)) /= Standard_String then |
2ba431e5 | 7846 | Check_SPARK_Restriction |
fe5d3068 | 7847 | ("result of concatenation should have type String", N); |
2933b16c | 7848 | end if; |
10303118 | 7849 | end Resolve_Op_Concat; |
9ebe3743 | 7850 | |
10303118 BD |
7851 | --------------------------- |
7852 | -- Resolve_Op_Concat_Arg -- | |
7853 | --------------------------- | |
996ae0b0 | 7854 | |
10303118 BD |
7855 | procedure Resolve_Op_Concat_Arg |
7856 | (N : Node_Id; | |
7857 | Arg : Node_Id; | |
7858 | Typ : Entity_Id; | |
7859 | Is_Comp : Boolean) | |
7860 | is | |
7861 | Btyp : constant Entity_Id := Base_Type (Typ); | |
668a19bc | 7862 | Ctyp : constant Entity_Id := Component_Type (Typ); |
996ae0b0 | 7863 | |
10303118 BD |
7864 | begin |
7865 | if In_Instance then | |
7866 | if Is_Comp | |
7867 | or else (not Is_Overloaded (Arg) | |
7868 | and then Etype (Arg) /= Any_Composite | |
668a19bc | 7869 | and then Covers (Ctyp, Etype (Arg))) |
10303118 | 7870 | then |
668a19bc | 7871 | Resolve (Arg, Ctyp); |
10303118 BD |
7872 | else |
7873 | Resolve (Arg, Btyp); | |
7874 | end if; | |
fbf5a39b | 7875 | |
668a19bc ES |
7876 | -- If both Array & Array and Array & Component are visible, there is a |
7877 | -- potential ambiguity that must be reported. | |
7878 | ||
7879 | elsif Has_Compatible_Type (Arg, Ctyp) then | |
10303118 | 7880 | if Nkind (Arg) = N_Aggregate |
668a19bc | 7881 | and then Is_Composite_Type (Ctyp) |
10303118 | 7882 | then |
668a19bc | 7883 | if Is_Private_Type (Ctyp) then |
10303118 | 7884 | Resolve (Arg, Btyp); |
668a19bc ES |
7885 | |
7886 | -- If the operation is user-defined and not overloaded use its | |
7887 | -- profile. The operation may be a renaming, in which case it has | |
7888 | -- been rewritten, and we want the original profile. | |
7889 | ||
7890 | elsif not Is_Overloaded (N) | |
7891 | and then Comes_From_Source (Entity (Original_Node (N))) | |
7892 | and then Ekind (Entity (Original_Node (N))) = E_Function | |
7893 | then | |
7894 | Resolve (Arg, | |
7895 | Etype | |
7896 | (Next_Formal (First_Formal (Entity (Original_Node (N)))))); | |
7897 | return; | |
7898 | ||
7899 | -- Otherwise an aggregate may match both the array type and the | |
7900 | -- component type. | |
7901 | ||
10303118 BD |
7902 | else |
7903 | Error_Msg_N ("ambiguous aggregate must be qualified", Arg); | |
7904 | Set_Etype (Arg, Any_Type); | |
996ae0b0 RK |
7905 | end if; |
7906 | ||
7907 | else | |
10303118 BD |
7908 | if Is_Overloaded (Arg) |
7909 | and then Has_Compatible_Type (Arg, Typ) | |
7910 | and then Etype (Arg) /= Any_Type | |
7911 | then | |
7912 | declare | |
7913 | I : Interp_Index; | |
7914 | It : Interp; | |
7915 | Func : Entity_Id; | |
7916 | ||
7917 | begin | |
7918 | Get_First_Interp (Arg, I, It); | |
7919 | Func := It.Nam; | |
7920 | Get_Next_Interp (I, It); | |
7921 | ||
7922 | -- Special-case the error message when the overloading is | |
7923 | -- caused by a function that yields an array and can be | |
7924 | -- called without parameters. | |
7925 | ||
7926 | if It.Nam = Func then | |
7927 | Error_Msg_Sloc := Sloc (Func); | |
7928 | Error_Msg_N ("ambiguous call to function#", Arg); | |
7929 | Error_Msg_NE | |
7930 | ("\\interpretation as call yields&", Arg, Typ); | |
7931 | Error_Msg_NE | |
7932 | ("\\interpretation as indexing of call yields&", | |
7933 | Arg, Component_Type (Typ)); | |
7934 | ||
7935 | else | |
668a19bc | 7936 | Error_Msg_N ("ambiguous operand for concatenation!", Arg); |
19fb051c | 7937 | |
10303118 BD |
7938 | Get_First_Interp (Arg, I, It); |
7939 | while Present (It.Nam) loop | |
7940 | Error_Msg_Sloc := Sloc (It.Nam); | |
7941 | ||
668a19bc ES |
7942 | if Base_Type (It.Typ) = Btyp |
7943 | or else | |
7944 | Base_Type (It.Typ) = Base_Type (Ctyp) | |
10303118 | 7945 | then |
4e7a4f6e AC |
7946 | Error_Msg_N -- CODEFIX |
7947 | ("\\possible interpretation#", Arg); | |
10303118 BD |
7948 | end if; |
7949 | ||
7950 | Get_Next_Interp (I, It); | |
7951 | end loop; | |
7952 | end if; | |
7953 | end; | |
7954 | end if; | |
7955 | ||
7956 | Resolve (Arg, Component_Type (Typ)); | |
7957 | ||
7958 | if Nkind (Arg) = N_String_Literal then | |
7959 | Set_Etype (Arg, Component_Type (Typ)); | |
7960 | end if; | |
7961 | ||
7962 | if Arg = Left_Opnd (N) then | |
7963 | Set_Is_Component_Left_Opnd (N); | |
7964 | else | |
7965 | Set_Is_Component_Right_Opnd (N); | |
7966 | end if; | |
996ae0b0 RK |
7967 | end if; |
7968 | ||
10303118 BD |
7969 | else |
7970 | Resolve (Arg, Btyp); | |
7971 | end if; | |
7972 | ||
2ba431e5 | 7973 | -- Concatenation is restricted in SPARK: each operand must be either a |
92e77027 AC |
7974 | -- string literal, the name of a string constant, a static character or |
7975 | -- string expression, or another concatenation. Arg cannot be a | |
7976 | -- concatenation here as callers of Resolve_Op_Concat_Arg call it | |
7977 | -- separately on each final operand, past concatenation operations. | |
2933b16c | 7978 | |
fe5d3068 YM |
7979 | if Is_Character_Type (Etype (Arg)) then |
7980 | if not Is_Static_Expression (Arg) then | |
2ba431e5 | 7981 | Check_SPARK_Restriction |
5b5588dd | 7982 | ("character operand for concatenation should be static", Arg); |
fe5d3068 | 7983 | end if; |
2933b16c | 7984 | |
fe5d3068 | 7985 | elsif Is_String_Type (Etype (Arg)) then |
92e77027 AC |
7986 | if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name) |
7987 | and then Is_Constant_Object (Entity (Arg))) | |
7988 | and then not Is_Static_Expression (Arg) | |
7989 | then | |
2ba431e5 | 7990 | Check_SPARK_Restriction |
5b5588dd | 7991 | ("string operand for concatenation should be static", Arg); |
fe5d3068 | 7992 | end if; |
2933b16c | 7993 | |
b9e48541 AC |
7994 | -- Do not issue error on an operand that is neither a character nor a |
7995 | -- string, as the error is issued in Resolve_Op_Concat. | |
2933b16c | 7996 | |
fe5d3068 YM |
7997 | else |
7998 | null; | |
2933b16c RD |
7999 | end if; |
8000 | ||
10303118 BD |
8001 | Check_Unset_Reference (Arg); |
8002 | end Resolve_Op_Concat_Arg; | |
996ae0b0 | 8003 | |
10303118 BD |
8004 | ----------------------------- |
8005 | -- Resolve_Op_Concat_First -- | |
8006 | ----------------------------- | |
8007 | ||
8008 | procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is | |
8009 | Btyp : constant Entity_Id := Base_Type (Typ); | |
8010 | Op1 : constant Node_Id := Left_Opnd (N); | |
8011 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 RK |
8012 | |
8013 | begin | |
dae2b8ea HK |
8014 | -- The parser folds an enormous sequence of concatenations of string |
8015 | -- literals into "" & "...", where the Is_Folded_In_Parser flag is set | |
4fc26524 | 8016 | -- in the right operand. If the expression resolves to a predefined "&" |
dae2b8ea HK |
8017 | -- operator, all is well. Otherwise, the parser's folding is wrong, so |
8018 | -- we give an error. See P_Simple_Expression in Par.Ch4. | |
8019 | ||
8020 | if Nkind (Op2) = N_String_Literal | |
8021 | and then Is_Folded_In_Parser (Op2) | |
8022 | and then Ekind (Entity (N)) = E_Function | |
8023 | then | |
8024 | pragma Assert (Nkind (Op1) = N_String_Literal -- should be "" | |
8025 | and then String_Length (Strval (Op1)) = 0); | |
8026 | Error_Msg_N ("too many user-defined concatenations", N); | |
8027 | return; | |
8028 | end if; | |
8029 | ||
996ae0b0 RK |
8030 | Set_Etype (N, Btyp); |
8031 | ||
8032 | if Is_Limited_Composite (Btyp) then | |
8033 | Error_Msg_N ("concatenation not available for limited array", N); | |
fbf5a39b | 8034 | Explain_Limited_Type (Btyp, N); |
996ae0b0 | 8035 | end if; |
10303118 | 8036 | end Resolve_Op_Concat_First; |
996ae0b0 | 8037 | |
10303118 BD |
8038 | ---------------------------- |
8039 | -- Resolve_Op_Concat_Rest -- | |
8040 | ---------------------------- | |
996ae0b0 | 8041 | |
10303118 BD |
8042 | procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is |
8043 | Op1 : constant Node_Id := Left_Opnd (N); | |
8044 | Op2 : constant Node_Id := Right_Opnd (N); | |
996ae0b0 | 8045 | |
10303118 BD |
8046 | begin |
8047 | Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N)); | |
996ae0b0 | 8048 | |
fbf5a39b | 8049 | Generate_Operator_Reference (N, Typ); |
996ae0b0 RK |
8050 | |
8051 | if Is_String_Type (Typ) then | |
8052 | Eval_Concatenation (N); | |
8053 | end if; | |
8054 | ||
d81b4bfe TQ |
8055 | -- If this is not a static concatenation, but the result is a string |
8056 | -- type (and not an array of strings) ensure that static string operands | |
8057 | -- have their subtypes properly constructed. | |
996ae0b0 RK |
8058 | |
8059 | if Nkind (N) /= N_String_Literal | |
8060 | and then Is_Character_Type (Component_Type (Typ)) | |
8061 | then | |
8062 | Set_String_Literal_Subtype (Op1, Typ); | |
8063 | Set_String_Literal_Subtype (Op2, Typ); | |
8064 | end if; | |
10303118 | 8065 | end Resolve_Op_Concat_Rest; |
996ae0b0 RK |
8066 | |
8067 | ---------------------- | |
8068 | -- Resolve_Op_Expon -- | |
8069 | ---------------------- | |
8070 | ||
8071 | procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is | |
8072 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8073 | ||
8074 | begin | |
f3d57416 | 8075 | -- Catch attempts to do fixed-point exponentiation with universal |
758c442c GD |
8076 | -- operands, which is a case where the illegality is not caught during |
8077 | -- normal operator analysis. | |
996ae0b0 RK |
8078 | |
8079 | if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then | |
8080 | Error_Msg_N ("exponentiation not available for fixed point", N); | |
4d792549 AC |
8081 | return; |
8082 | ||
8083 | elsif Nkind (Parent (N)) in N_Op | |
8084 | and then Is_Fixed_Point_Type (Etype (Parent (N))) | |
8085 | and then Etype (N) = Universal_Real | |
8086 | and then Comes_From_Source (N) | |
8087 | then | |
8088 | Error_Msg_N ("exponentiation not available for fixed point", N); | |
996ae0b0 RK |
8089 | return; |
8090 | end if; | |
8091 | ||
fbf5a39b AC |
8092 | if Comes_From_Source (N) |
8093 | and then Ekind (Entity (N)) = E_Function | |
8094 | and then Is_Imported (Entity (N)) | |
8095 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
8096 | then | |
8097 | Resolve_Intrinsic_Operator (N, Typ); | |
8098 | return; | |
8099 | end if; | |
8100 | ||
996ae0b0 RK |
8101 | if Etype (Left_Opnd (N)) = Universal_Integer |
8102 | or else Etype (Left_Opnd (N)) = Universal_Real | |
8103 | then | |
8104 | Check_For_Visible_Operator (N, B_Typ); | |
8105 | end if; | |
8106 | ||
8107 | -- We do the resolution using the base type, because intermediate values | |
8108 | -- in expressions always are of the base type, not a subtype of it. | |
8109 | ||
8110 | Resolve (Left_Opnd (N), B_Typ); | |
8111 | Resolve (Right_Opnd (N), Standard_Integer); | |
8112 | ||
8113 | Check_Unset_Reference (Left_Opnd (N)); | |
8114 | Check_Unset_Reference (Right_Opnd (N)); | |
8115 | ||
8116 | Set_Etype (N, B_Typ); | |
fbf5a39b | 8117 | Generate_Operator_Reference (N, B_Typ); |
dec6faf1 AC |
8118 | |
8119 | Analyze_Dimension (N); | |
8120 | ||
15954beb | 8121 | if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then |
6c57023b | 8122 | -- Evaluate the exponentiation operator for dimensioned type |
dec6faf1 | 8123 | |
6c57023b AC |
8124 | Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ); |
8125 | else | |
8126 | Eval_Op_Expon (N); | |
dec6faf1 AC |
8127 | end if; |
8128 | ||
996ae0b0 RK |
8129 | -- Set overflow checking bit. Much cleverer code needed here eventually |
8130 | -- and perhaps the Resolve routines should be separated for the various | |
8131 | -- arithmetic operations, since they will need different processing. ??? | |
8132 | ||
8133 | if Nkind (N) in N_Op then | |
8134 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 8135 | Enable_Overflow_Check (N); |
996ae0b0 RK |
8136 | end if; |
8137 | end if; | |
996ae0b0 RK |
8138 | end Resolve_Op_Expon; |
8139 | ||
8140 | -------------------- | |
8141 | -- Resolve_Op_Not -- | |
8142 | -------------------- | |
8143 | ||
8144 | procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is | |
8145 | B_Typ : Entity_Id; | |
8146 | ||
8147 | function Parent_Is_Boolean return Boolean; | |
5cc9353d RD |
8148 | -- This function determines if the parent node is a boolean operator or |
8149 | -- operation (comparison op, membership test, or short circuit form) and | |
8150 | -- the not in question is the left operand of this operation. Note that | |
8151 | -- if the not is in parens, then false is returned. | |
996ae0b0 | 8152 | |
aa180613 RD |
8153 | ----------------------- |
8154 | -- Parent_Is_Boolean -- | |
8155 | ----------------------- | |
8156 | ||
996ae0b0 RK |
8157 | function Parent_Is_Boolean return Boolean is |
8158 | begin | |
8159 | if Paren_Count (N) /= 0 then | |
8160 | return False; | |
8161 | ||
8162 | else | |
8163 | case Nkind (Parent (N)) is | |
8164 | when N_Op_And | | |
8165 | N_Op_Eq | | |
8166 | N_Op_Ge | | |
8167 | N_Op_Gt | | |
8168 | N_Op_Le | | |
8169 | N_Op_Lt | | |
8170 | N_Op_Ne | | |
8171 | N_Op_Or | | |
8172 | N_Op_Xor | | |
8173 | N_In | | |
8174 | N_Not_In | | |
8175 | N_And_Then | | |
aa180613 | 8176 | N_Or_Else => |
996ae0b0 RK |
8177 | |
8178 | return Left_Opnd (Parent (N)) = N; | |
8179 | ||
8180 | when others => | |
8181 | return False; | |
8182 | end case; | |
8183 | end if; | |
8184 | end Parent_Is_Boolean; | |
8185 | ||
8186 | -- Start of processing for Resolve_Op_Not | |
8187 | ||
8188 | begin | |
758c442c GD |
8189 | -- Predefined operations on scalar types yield the base type. On the |
8190 | -- other hand, logical operations on arrays yield the type of the | |
8191 | -- arguments (and the context). | |
996ae0b0 RK |
8192 | |
8193 | if Is_Array_Type (Typ) then | |
8194 | B_Typ := Typ; | |
8195 | else | |
8196 | B_Typ := Base_Type (Typ); | |
8197 | end if; | |
8198 | ||
001c7783 AC |
8199 | if Is_VMS_Operator (Entity (N)) then |
8200 | null; | |
8201 | ||
f3d57416 | 8202 | -- Straightforward case of incorrect arguments |
aa180613 | 8203 | |
001c7783 | 8204 | elsif not Valid_Boolean_Arg (Typ) then |
996ae0b0 RK |
8205 | Error_Msg_N ("invalid operand type for operator&", N); |
8206 | Set_Etype (N, Any_Type); | |
8207 | return; | |
8208 | ||
aa180613 RD |
8209 | -- Special case of probable missing parens |
8210 | ||
fbf5a39b | 8211 | elsif Typ = Universal_Integer or else Typ = Any_Modular then |
996ae0b0 | 8212 | if Parent_Is_Boolean then |
ed2233dc | 8213 | Error_Msg_N |
996ae0b0 RK |
8214 | ("operand of not must be enclosed in parentheses", |
8215 | Right_Opnd (N)); | |
8216 | else | |
8217 | Error_Msg_N | |
8218 | ("no modular type available in this context", N); | |
8219 | end if; | |
8220 | ||
8221 | Set_Etype (N, Any_Type); | |
8222 | return; | |
8223 | ||
5cc9353d | 8224 | -- OK resolution of NOT |
aa180613 | 8225 | |
996ae0b0 | 8226 | else |
aa180613 RD |
8227 | -- Warn if non-boolean types involved. This is a case like not a < b |
8228 | -- where a and b are modular, where we will get (not a) < b and most | |
8229 | -- likely not (a < b) was intended. | |
8230 | ||
8231 | if Warn_On_Questionable_Missing_Parens | |
8232 | and then not Is_Boolean_Type (Typ) | |
996ae0b0 RK |
8233 | and then Parent_Is_Boolean |
8234 | then | |
ed2233dc | 8235 | Error_Msg_N ("?not expression should be parenthesized here!", N); |
996ae0b0 RK |
8236 | end if; |
8237 | ||
09bc9ab6 RD |
8238 | -- Warn on double negation if checking redundant constructs |
8239 | ||
8240 | if Warn_On_Redundant_Constructs | |
8241 | and then Comes_From_Source (N) | |
8242 | and then Comes_From_Source (Right_Opnd (N)) | |
8243 | and then Root_Type (Typ) = Standard_Boolean | |
8244 | and then Nkind (Right_Opnd (N)) = N_Op_Not | |
8245 | then | |
ed2233dc | 8246 | Error_Msg_N ("redundant double negation?", N); |
09bc9ab6 RD |
8247 | end if; |
8248 | ||
8249 | -- Complete resolution and evaluation of NOT | |
8250 | ||
996ae0b0 RK |
8251 | Resolve (Right_Opnd (N), B_Typ); |
8252 | Check_Unset_Reference (Right_Opnd (N)); | |
8253 | Set_Etype (N, B_Typ); | |
fbf5a39b | 8254 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
8255 | Eval_Op_Not (N); |
8256 | end if; | |
8257 | end Resolve_Op_Not; | |
8258 | ||
8259 | ----------------------------- | |
8260 | -- Resolve_Operator_Symbol -- | |
8261 | ----------------------------- | |
8262 | ||
8263 | -- Nothing to be done, all resolved already | |
8264 | ||
8265 | procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
8266 | pragma Warnings (Off, N); |
8267 | pragma Warnings (Off, Typ); | |
8268 | ||
996ae0b0 RK |
8269 | begin |
8270 | null; | |
8271 | end Resolve_Operator_Symbol; | |
8272 | ||
8273 | ---------------------------------- | |
8274 | -- Resolve_Qualified_Expression -- | |
8275 | ---------------------------------- | |
8276 | ||
8277 | procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is | |
07fc65c4 GB |
8278 | pragma Warnings (Off, Typ); |
8279 | ||
996ae0b0 RK |
8280 | Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N)); |
8281 | Expr : constant Node_Id := Expression (N); | |
8282 | ||
8283 | begin | |
8284 | Resolve (Expr, Target_Typ); | |
8285 | ||
7b98672f YM |
8286 | -- Protect call to Matching_Static_Array_Bounds to avoid costly |
8287 | -- operation if not needed. | |
8288 | ||
8289 | if Restriction_Check_Required (SPARK) | |
8290 | and then Is_Array_Type (Target_Typ) | |
b0186f71 | 8291 | and then Is_Array_Type (Etype (Expr)) |
db72f10a | 8292 | and then Etype (Expr) /= Any_Composite -- or else Expr in error |
b0186f71 AC |
8293 | and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr)) |
8294 | then | |
2ba431e5 | 8295 | Check_SPARK_Restriction |
fe5d3068 | 8296 | ("array types should have matching static bounds", N); |
b0186f71 AC |
8297 | end if; |
8298 | ||
5cc9353d RD |
8299 | -- A qualified expression requires an exact match of the type, class- |
8300 | -- wide matching is not allowed. However, if the qualifying type is | |
8301 | -- specific and the expression has a class-wide type, it may still be | |
8302 | -- okay, since it can be the result of the expansion of a call to a | |
8303 | -- dispatching function, so we also have to check class-wideness of the | |
8304 | -- type of the expression's original node. | |
1420b484 JM |
8305 | |
8306 | if (Is_Class_Wide_Type (Target_Typ) | |
8307 | or else | |
8308 | (Is_Class_Wide_Type (Etype (Expr)) | |
8309 | and then Is_Class_Wide_Type (Etype (Original_Node (Expr))))) | |
996ae0b0 RK |
8310 | and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ) |
8311 | then | |
8312 | Wrong_Type (Expr, Target_Typ); | |
8313 | end if; | |
8314 | ||
90c63b09 AC |
8315 | -- If the target type is unconstrained, then we reset the type of the |
8316 | -- result from the type of the expression. For other cases, the actual | |
8317 | -- subtype of the expression is the target type. | |
996ae0b0 RK |
8318 | |
8319 | if Is_Composite_Type (Target_Typ) | |
8320 | and then not Is_Constrained (Target_Typ) | |
8321 | then | |
8322 | Set_Etype (N, Etype (Expr)); | |
8323 | end if; | |
8324 | ||
dec6faf1 | 8325 | Analyze_Dimension (N); |
996ae0b0 RK |
8326 | Eval_Qualified_Expression (N); |
8327 | end Resolve_Qualified_Expression; | |
8328 | ||
8329 | ------------------- | |
8330 | -- Resolve_Range -- | |
8331 | ------------------- | |
8332 | ||
8333 | procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is | |
8334 | L : constant Node_Id := Low_Bound (N); | |
8335 | H : constant Node_Id := High_Bound (N); | |
8336 | ||
bd29d519 AC |
8337 | function First_Last_Ref return Boolean; |
8338 | -- Returns True if N is of the form X'First .. X'Last where X is the | |
8339 | -- same entity for both attributes. | |
8340 | ||
8341 | -------------------- | |
8342 | -- First_Last_Ref -- | |
8343 | -------------------- | |
8344 | ||
8345 | function First_Last_Ref return Boolean is | |
8346 | Lorig : constant Node_Id := Original_Node (L); | |
8347 | Horig : constant Node_Id := Original_Node (H); | |
8348 | ||
8349 | begin | |
8350 | if Nkind (Lorig) = N_Attribute_Reference | |
8351 | and then Nkind (Horig) = N_Attribute_Reference | |
8352 | and then Attribute_Name (Lorig) = Name_First | |
8353 | and then Attribute_Name (Horig) = Name_Last | |
8354 | then | |
8355 | declare | |
8356 | PL : constant Node_Id := Prefix (Lorig); | |
8357 | PH : constant Node_Id := Prefix (Horig); | |
8358 | begin | |
8359 | if Is_Entity_Name (PL) | |
8360 | and then Is_Entity_Name (PH) | |
8361 | and then Entity (PL) = Entity (PH) | |
8362 | then | |
8363 | return True; | |
8364 | end if; | |
8365 | end; | |
8366 | end if; | |
8367 | ||
8368 | return False; | |
8369 | end First_Last_Ref; | |
8370 | ||
8371 | -- Start of processing for Resolve_Range | |
8372 | ||
996ae0b0 RK |
8373 | begin |
8374 | Set_Etype (N, Typ); | |
8375 | Resolve (L, Typ); | |
8376 | Resolve (H, Typ); | |
8377 | ||
bd29d519 AC |
8378 | -- Check for inappropriate range on unordered enumeration type |
8379 | ||
8380 | if Bad_Unordered_Enumeration_Reference (N, Typ) | |
8381 | ||
8382 | -- Exclude X'First .. X'Last if X is the same entity for both | |
8383 | ||
8384 | and then not First_Last_Ref | |
8385 | then | |
8386 | Error_Msg ("subrange of unordered enumeration type?", Sloc (N)); | |
498d1b80 AC |
8387 | end if; |
8388 | ||
996ae0b0 RK |
8389 | Check_Unset_Reference (L); |
8390 | Check_Unset_Reference (H); | |
8391 | ||
8392 | -- We have to check the bounds for being within the base range as | |
758c442c GD |
8393 | -- required for a non-static context. Normally this is automatic and |
8394 | -- done as part of evaluating expressions, but the N_Range node is an | |
8395 | -- exception, since in GNAT we consider this node to be a subexpression, | |
8396 | -- even though in Ada it is not. The circuit in Sem_Eval could check for | |
8397 | -- this, but that would put the test on the main evaluation path for | |
8398 | -- expressions. | |
996ae0b0 RK |
8399 | |
8400 | Check_Non_Static_Context (L); | |
8401 | Check_Non_Static_Context (H); | |
8402 | ||
b7d1f17f HK |
8403 | -- Check for an ambiguous range over character literals. This will |
8404 | -- happen with a membership test involving only literals. | |
8405 | ||
8406 | if Typ = Any_Character then | |
8407 | Ambiguous_Character (L); | |
8408 | Set_Etype (N, Any_Type); | |
8409 | return; | |
8410 | end if; | |
8411 | ||
5cc9353d RD |
8412 | -- If bounds are static, constant-fold them, so size computations are |
8413 | -- identical between front-end and back-end. Do not perform this | |
fbf5a39b | 8414 | -- transformation while analyzing generic units, as type information |
5cc9353d | 8415 | -- would be lost when reanalyzing the constant node in the instance. |
fbf5a39b | 8416 | |
11fa950b | 8417 | if Is_Discrete_Type (Typ) and then Full_Expander_Active then |
fbf5a39b AC |
8418 | if Is_OK_Static_Expression (L) then |
8419 | Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L)); | |
8420 | end if; | |
8421 | ||
8422 | if Is_OK_Static_Expression (H) then | |
8423 | Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H)); | |
8424 | end if; | |
8425 | end if; | |
996ae0b0 RK |
8426 | end Resolve_Range; |
8427 | ||
8428 | -------------------------- | |
8429 | -- Resolve_Real_Literal -- | |
8430 | -------------------------- | |
8431 | ||
8432 | procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is | |
8433 | Actual_Typ : constant Entity_Id := Etype (N); | |
8434 | ||
8435 | begin | |
8436 | -- Special processing for fixed-point literals to make sure that the | |
5cc9353d RD |
8437 | -- value is an exact multiple of small where this is required. We skip |
8438 | -- this for the universal real case, and also for generic types. | |
996ae0b0 RK |
8439 | |
8440 | if Is_Fixed_Point_Type (Typ) | |
8441 | and then Typ /= Universal_Fixed | |
8442 | and then Typ /= Any_Fixed | |
8443 | and then not Is_Generic_Type (Typ) | |
8444 | then | |
8445 | declare | |
8446 | Val : constant Ureal := Realval (N); | |
8447 | Cintr : constant Ureal := Val / Small_Value (Typ); | |
8448 | Cint : constant Uint := UR_Trunc (Cintr); | |
8449 | Den : constant Uint := Norm_Den (Cintr); | |
8450 | Stat : Boolean; | |
8451 | ||
8452 | begin | |
8453 | -- Case of literal is not an exact multiple of the Small | |
8454 | ||
8455 | if Den /= 1 then | |
8456 | ||
5cc9353d RD |
8457 | -- For a source program literal for a decimal fixed-point type, |
8458 | -- this is statically illegal (RM 4.9(36)). | |
996ae0b0 RK |
8459 | |
8460 | if Is_Decimal_Fixed_Point_Type (Typ) | |
8461 | and then Actual_Typ = Universal_Real | |
8462 | and then Comes_From_Source (N) | |
8463 | then | |
8464 | Error_Msg_N ("value has extraneous low order digits", N); | |
8465 | end if; | |
8466 | ||
bc5f3720 RD |
8467 | -- Generate a warning if literal from source |
8468 | ||
8469 | if Is_Static_Expression (N) | |
8470 | and then Warn_On_Bad_Fixed_Value | |
8471 | then | |
8472 | Error_Msg_N | |
aa5147f0 | 8473 | ("?static fixed-point value is not a multiple of Small!", |
bc5f3720 RD |
8474 | N); |
8475 | end if; | |
8476 | ||
996ae0b0 RK |
8477 | -- Replace literal by a value that is the exact representation |
8478 | -- of a value of the type, i.e. a multiple of the small value, | |
8479 | -- by truncation, since Machine_Rounds is false for all GNAT | |
8480 | -- fixed-point types (RM 4.9(38)). | |
8481 | ||
8482 | Stat := Is_Static_Expression (N); | |
8483 | Rewrite (N, | |
8484 | Make_Real_Literal (Sloc (N), | |
8485 | Realval => Small_Value (Typ) * Cint)); | |
8486 | ||
8487 | Set_Is_Static_Expression (N, Stat); | |
8488 | end if; | |
8489 | ||
8490 | -- In all cases, set the corresponding integer field | |
8491 | ||
8492 | Set_Corresponding_Integer_Value (N, Cint); | |
8493 | end; | |
8494 | end if; | |
8495 | ||
8496 | -- Now replace the actual type by the expected type as usual | |
8497 | ||
8498 | Set_Etype (N, Typ); | |
8499 | Eval_Real_Literal (N); | |
8500 | end Resolve_Real_Literal; | |
8501 | ||
8502 | ----------------------- | |
8503 | -- Resolve_Reference -- | |
8504 | ----------------------- | |
8505 | ||
8506 | procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is | |
8507 | P : constant Node_Id := Prefix (N); | |
8508 | ||
8509 | begin | |
8510 | -- Replace general access with specific type | |
8511 | ||
8512 | if Ekind (Etype (N)) = E_Allocator_Type then | |
8513 | Set_Etype (N, Base_Type (Typ)); | |
8514 | end if; | |
8515 | ||
8516 | Resolve (P, Designated_Type (Etype (N))); | |
8517 | ||
5cc9353d RD |
8518 | -- If we are taking the reference of a volatile entity, then treat it as |
8519 | -- a potential modification of this entity. This is too conservative, | |
8520 | -- but necessary because remove side effects can cause transformations | |
8521 | -- of normal assignments into reference sequences that otherwise fail to | |
8522 | -- notice the modification. | |
996ae0b0 | 8523 | |
fbf5a39b | 8524 | if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then |
45fc7ddb | 8525 | Note_Possible_Modification (P, Sure => False); |
996ae0b0 RK |
8526 | end if; |
8527 | end Resolve_Reference; | |
8528 | ||
8529 | -------------------------------- | |
8530 | -- Resolve_Selected_Component -- | |
8531 | -------------------------------- | |
8532 | ||
8533 | procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is | |
8534 | Comp : Entity_Id; | |
8535 | Comp1 : Entity_Id := Empty; -- prevent junk warning | |
8536 | P : constant Node_Id := Prefix (N); | |
8537 | S : constant Node_Id := Selector_Name (N); | |
8538 | T : Entity_Id := Etype (P); | |
8539 | I : Interp_Index; | |
8540 | I1 : Interp_Index := 0; -- prevent junk warning | |
8541 | It : Interp; | |
8542 | It1 : Interp; | |
8543 | Found : Boolean; | |
8544 | ||
6510f4c9 GB |
8545 | function Init_Component return Boolean; |
8546 | -- Check whether this is the initialization of a component within an | |
fbf5a39b | 8547 | -- init proc (by assignment or call to another init proc). If true, |
6510f4c9 GB |
8548 | -- there is no need for a discriminant check. |
8549 | ||
8550 | -------------------- | |
8551 | -- Init_Component -- | |
8552 | -------------------- | |
8553 | ||
8554 | function Init_Component return Boolean is | |
8555 | begin | |
8556 | return Inside_Init_Proc | |
8557 | and then Nkind (Prefix (N)) = N_Identifier | |
8558 | and then Chars (Prefix (N)) = Name_uInit | |
8559 | and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative; | |
8560 | end Init_Component; | |
8561 | ||
8562 | -- Start of processing for Resolve_Selected_Component | |
8563 | ||
996ae0b0 RK |
8564 | begin |
8565 | if Is_Overloaded (P) then | |
8566 | ||
8567 | -- Use the context type to select the prefix that has a selector | |
8568 | -- of the correct name and type. | |
8569 | ||
8570 | Found := False; | |
8571 | Get_First_Interp (P, I, It); | |
8572 | ||
8573 | Search : while Present (It.Typ) loop | |
8574 | if Is_Access_Type (It.Typ) then | |
8575 | T := Designated_Type (It.Typ); | |
8576 | else | |
8577 | T := It.Typ; | |
8578 | end if; | |
8579 | ||
95eb8b69 AC |
8580 | -- Locate selected component. For a private prefix the selector |
8581 | -- can denote a discriminant. | |
8582 | ||
8583 | if Is_Record_Type (T) or else Is_Private_Type (T) then | |
36fcf362 RD |
8584 | |
8585 | -- The visible components of a class-wide type are those of | |
8586 | -- the root type. | |
8587 | ||
8588 | if Is_Class_Wide_Type (T) then | |
8589 | T := Etype (T); | |
8590 | end if; | |
8591 | ||
996ae0b0 | 8592 | Comp := First_Entity (T); |
996ae0b0 | 8593 | while Present (Comp) loop |
996ae0b0 RK |
8594 | if Chars (Comp) = Chars (S) |
8595 | and then Covers (Etype (Comp), Typ) | |
8596 | then | |
8597 | if not Found then | |
8598 | Found := True; | |
8599 | I1 := I; | |
8600 | It1 := It; | |
8601 | Comp1 := Comp; | |
8602 | ||
8603 | else | |
8604 | It := Disambiguate (P, I1, I, Any_Type); | |
8605 | ||
8606 | if It = No_Interp then | |
8607 | Error_Msg_N | |
8608 | ("ambiguous prefix for selected component", N); | |
8609 | Set_Etype (N, Typ); | |
8610 | return; | |
8611 | ||
8612 | else | |
8613 | It1 := It; | |
8614 | ||
c8ef728f ES |
8615 | -- There may be an implicit dereference. Retrieve |
8616 | -- designated record type. | |
8617 | ||
8618 | if Is_Access_Type (It1.Typ) then | |
8619 | T := Designated_Type (It1.Typ); | |
8620 | else | |
8621 | T := It1.Typ; | |
8622 | end if; | |
8623 | ||
8624 | if Scope (Comp1) /= T then | |
996ae0b0 RK |
8625 | |
8626 | -- Resolution chooses the new interpretation. | |
8627 | -- Find the component with the right name. | |
8628 | ||
c8ef728f | 8629 | Comp1 := First_Entity (T); |
996ae0b0 RK |
8630 | while Present (Comp1) |
8631 | and then Chars (Comp1) /= Chars (S) | |
8632 | loop | |
8633 | Comp1 := Next_Entity (Comp1); | |
8634 | end loop; | |
8635 | end if; | |
8636 | ||
8637 | exit Search; | |
8638 | end if; | |
8639 | end if; | |
8640 | end if; | |
8641 | ||
8642 | Comp := Next_Entity (Comp); | |
8643 | end loop; | |
996ae0b0 RK |
8644 | end if; |
8645 | ||
8646 | Get_Next_Interp (I, It); | |
996ae0b0 RK |
8647 | end loop Search; |
8648 | ||
8649 | Resolve (P, It1.Typ); | |
8650 | Set_Etype (N, Typ); | |
aa180613 | 8651 | Set_Entity_With_Style_Check (S, Comp1); |
996ae0b0 RK |
8652 | |
8653 | else | |
fbf5a39b | 8654 | -- Resolve prefix with its type |
996ae0b0 RK |
8655 | |
8656 | Resolve (P, T); | |
8657 | end if; | |
8658 | ||
aa180613 RD |
8659 | -- Generate cross-reference. We needed to wait until full overloading |
8660 | -- resolution was complete to do this, since otherwise we can't tell if | |
01e17342 | 8661 | -- we are an lvalue or not. |
aa180613 RD |
8662 | |
8663 | if May_Be_Lvalue (N) then | |
8664 | Generate_Reference (Entity (S), S, 'm'); | |
8665 | else | |
8666 | Generate_Reference (Entity (S), S, 'r'); | |
8667 | end if; | |
8668 | ||
c8ef728f ES |
8669 | -- If prefix is an access type, the node will be transformed into an |
8670 | -- explicit dereference during expansion. The type of the node is the | |
8671 | -- designated type of that of the prefix. | |
996ae0b0 RK |
8672 | |
8673 | if Is_Access_Type (Etype (P)) then | |
996ae0b0 | 8674 | T := Designated_Type (Etype (P)); |
c8ef728f | 8675 | Check_Fully_Declared_Prefix (T, P); |
996ae0b0 RK |
8676 | else |
8677 | T := Etype (P); | |
8678 | end if; | |
8679 | ||
8680 | if Has_Discriminants (T) | |
964f13da | 8681 | and then Ekind_In (Entity (S), E_Component, E_Discriminant) |
996ae0b0 RK |
8682 | and then Present (Original_Record_Component (Entity (S))) |
8683 | and then Ekind (Original_Record_Component (Entity (S))) = E_Component | |
8684 | and then Present (Discriminant_Checking_Func | |
8685 | (Original_Record_Component (Entity (S)))) | |
8686 | and then not Discriminant_Checks_Suppressed (T) | |
6510f4c9 | 8687 | and then not Init_Component |
996ae0b0 RK |
8688 | then |
8689 | Set_Do_Discriminant_Check (N); | |
8690 | end if; | |
8691 | ||
8692 | if Ekind (Entity (S)) = E_Void then | |
8693 | Error_Msg_N ("premature use of component", S); | |
8694 | end if; | |
8695 | ||
8696 | -- If the prefix is a record conversion, this may be a renamed | |
8697 | -- discriminant whose bounds differ from those of the original | |
8698 | -- one, so we must ensure that a range check is performed. | |
8699 | ||
8700 | if Nkind (P) = N_Type_Conversion | |
8701 | and then Ekind (Entity (S)) = E_Discriminant | |
fbf5a39b | 8702 | and then Is_Discrete_Type (Typ) |
996ae0b0 RK |
8703 | then |
8704 | Set_Etype (N, Base_Type (Typ)); | |
8705 | end if; | |
8706 | ||
8707 | -- Note: No Eval processing is required, because the prefix is of a | |
8708 | -- record type, or protected type, and neither can possibly be static. | |
8709 | ||
c28408b7 RD |
8710 | -- If the array type is atomic, and is packed, and we are in a left side |
8711 | -- context, then this is worth a warning, since we have a situation | |
5cc9353d RD |
8712 | -- where the access to the component may cause extra read/writes of the |
8713 | -- atomic array object, which could be considered unexpected. | |
c28408b7 RD |
8714 | |
8715 | if Nkind (N) = N_Selected_Component | |
8716 | and then (Is_Atomic (T) | |
8717 | or else (Is_Entity_Name (Prefix (N)) | |
8718 | and then Is_Atomic (Entity (Prefix (N))))) | |
8719 | and then Is_Packed (T) | |
8720 | and then Is_LHS (N) | |
8721 | then | |
54c04d6c AC |
8722 | Error_Msg_N |
8723 | ("?assignment to component of packed atomic record", Prefix (N)); | |
8724 | Error_Msg_N | |
8725 | ("?\may cause unexpected accesses to atomic object", Prefix (N)); | |
c28408b7 | 8726 | end if; |
54c04d6c | 8727 | |
dec6faf1 | 8728 | Analyze_Dimension (N); |
996ae0b0 RK |
8729 | end Resolve_Selected_Component; |
8730 | ||
8731 | ------------------- | |
8732 | -- Resolve_Shift -- | |
8733 | ------------------- | |
8734 | ||
8735 | procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is | |
8736 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8737 | L : constant Node_Id := Left_Opnd (N); | |
8738 | R : constant Node_Id := Right_Opnd (N); | |
8739 | ||
8740 | begin | |
8741 | -- We do the resolution using the base type, because intermediate values | |
8742 | -- in expressions always are of the base type, not a subtype of it. | |
8743 | ||
8744 | Resolve (L, B_Typ); | |
8745 | Resolve (R, Standard_Natural); | |
8746 | ||
8747 | Check_Unset_Reference (L); | |
8748 | Check_Unset_Reference (R); | |
8749 | ||
8750 | Set_Etype (N, B_Typ); | |
fbf5a39b | 8751 | Generate_Operator_Reference (N, B_Typ); |
996ae0b0 RK |
8752 | Eval_Shift (N); |
8753 | end Resolve_Shift; | |
8754 | ||
8755 | --------------------------- | |
8756 | -- Resolve_Short_Circuit -- | |
8757 | --------------------------- | |
8758 | ||
8759 | procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is | |
8760 | B_Typ : constant Entity_Id := Base_Type (Typ); | |
8761 | L : constant Node_Id := Left_Opnd (N); | |
8762 | R : constant Node_Id := Right_Opnd (N); | |
8763 | ||
8764 | begin | |
8765 | Resolve (L, B_Typ); | |
8766 | Resolve (R, B_Typ); | |
8767 | ||
45fc7ddb HK |
8768 | -- Check for issuing warning for always False assert/check, this happens |
8769 | -- when assertions are turned off, in which case the pragma Assert/Check | |
36fcf362 RD |
8770 | -- was transformed into: |
8771 | ||
8772 | -- if False and then <condition> then ... | |
8773 | ||
8774 | -- and we detect this pattern | |
8775 | ||
8776 | if Warn_On_Assertion_Failure | |
8777 | and then Is_Entity_Name (R) | |
8778 | and then Entity (R) = Standard_False | |
8779 | and then Nkind (Parent (N)) = N_If_Statement | |
8780 | and then Nkind (N) = N_And_Then | |
8781 | and then Is_Entity_Name (L) | |
8782 | and then Entity (L) = Standard_False | |
8783 | then | |
8784 | declare | |
8785 | Orig : constant Node_Id := Original_Node (Parent (N)); | |
45fc7ddb | 8786 | |
36fcf362 RD |
8787 | begin |
8788 | if Nkind (Orig) = N_Pragma | |
26570b21 | 8789 | and then Pragma_Name (Orig) = Name_Assert |
36fcf362 RD |
8790 | then |
8791 | -- Don't want to warn if original condition is explicit False | |
8792 | ||
8793 | declare | |
8794 | Expr : constant Node_Id := | |
8795 | Original_Node | |
8796 | (Expression | |
8797 | (First (Pragma_Argument_Associations (Orig)))); | |
8798 | begin | |
8799 | if Is_Entity_Name (Expr) | |
8800 | and then Entity (Expr) = Standard_False | |
8801 | then | |
8802 | null; | |
8803 | else | |
51bf9bdf AC |
8804 | -- Issue warning. We do not want the deletion of the |
8805 | -- IF/AND-THEN to take this message with it. We achieve | |
8806 | -- this by making sure that the expanded code points to | |
8807 | -- the Sloc of the expression, not the original pragma. | |
8808 | ||
8a06151a RD |
8809 | -- Note: Use Error_Msg_F here rather than Error_Msg_N. |
8810 | -- The source location of the expression is not usually | |
8811 | -- the best choice here. For example, it gets located on | |
8812 | -- the last AND keyword in a chain of boolean expressiond | |
8813 | -- AND'ed together. It is best to put the message on the | |
8814 | -- first character of the assertion, which is the effect | |
8815 | -- of the First_Node call here. | |
8816 | ||
ca20a08e | 8817 | Error_Msg_F |
e7c0dd39 | 8818 | ("?assertion would fail at run time!", |
51bf9bdf AC |
8819 | Expression |
8820 | (First (Pragma_Argument_Associations (Orig)))); | |
36fcf362 RD |
8821 | end if; |
8822 | end; | |
45fc7ddb HK |
8823 | |
8824 | -- Similar processing for Check pragma | |
8825 | ||
8826 | elsif Nkind (Orig) = N_Pragma | |
8827 | and then Pragma_Name (Orig) = Name_Check | |
8828 | then | |
8829 | -- Don't want to warn if original condition is explicit False | |
8830 | ||
8831 | declare | |
8832 | Expr : constant Node_Id := | |
8833 | Original_Node | |
8834 | (Expression | |
8835 | (Next (First | |
8836 | (Pragma_Argument_Associations (Orig))))); | |
8837 | begin | |
8838 | if Is_Entity_Name (Expr) | |
8839 | and then Entity (Expr) = Standard_False | |
8840 | then | |
8841 | null; | |
8a06151a RD |
8842 | |
8843 | -- Post warning | |
8844 | ||
45fc7ddb | 8845 | else |
8a06151a RD |
8846 | -- Again use Error_Msg_F rather than Error_Msg_N, see |
8847 | -- comment above for an explanation of why we do this. | |
8848 | ||
ca20a08e | 8849 | Error_Msg_F |
e7c0dd39 | 8850 | ("?check would fail at run time!", |
51bf9bdf AC |
8851 | Expression |
8852 | (Last (Pragma_Argument_Associations (Orig)))); | |
45fc7ddb HK |
8853 | end if; |
8854 | end; | |
36fcf362 RD |
8855 | end if; |
8856 | end; | |
8857 | end if; | |
8858 | ||
8859 | -- Continue with processing of short circuit | |
8860 | ||
996ae0b0 RK |
8861 | Check_Unset_Reference (L); |
8862 | Check_Unset_Reference (R); | |
8863 | ||
8864 | Set_Etype (N, B_Typ); | |
8865 | Eval_Short_Circuit (N); | |
8866 | end Resolve_Short_Circuit; | |
8867 | ||
8868 | ------------------- | |
8869 | -- Resolve_Slice -- | |
8870 | ------------------- | |
8871 | ||
8872 | procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is | |
996ae0b0 | 8873 | Drange : constant Node_Id := Discrete_Range (N); |
5f44f0d4 | 8874 | Name : constant Node_Id := Prefix (N); |
996ae0b0 | 8875 | Array_Type : Entity_Id := Empty; |
5f44f0d4 | 8876 | Index_Type : Entity_Id; |
996ae0b0 RK |
8877 | |
8878 | begin | |
8879 | if Is_Overloaded (Name) then | |
8880 | ||
d81b4bfe TQ |
8881 | -- Use the context type to select the prefix that yields the correct |
8882 | -- array type. | |
996ae0b0 RK |
8883 | |
8884 | declare | |
8885 | I : Interp_Index; | |
8886 | I1 : Interp_Index := 0; | |
8887 | It : Interp; | |
8888 | P : constant Node_Id := Prefix (N); | |
8889 | Found : Boolean := False; | |
8890 | ||
8891 | begin | |
8892 | Get_First_Interp (P, I, It); | |
996ae0b0 | 8893 | while Present (It.Typ) loop |
996ae0b0 RK |
8894 | if (Is_Array_Type (It.Typ) |
8895 | and then Covers (Typ, It.Typ)) | |
8896 | or else (Is_Access_Type (It.Typ) | |
8897 | and then Is_Array_Type (Designated_Type (It.Typ)) | |
8898 | and then Covers (Typ, Designated_Type (It.Typ))) | |
8899 | then | |
8900 | if Found then | |
8901 | It := Disambiguate (P, I1, I, Any_Type); | |
8902 | ||
8903 | if It = No_Interp then | |
8904 | Error_Msg_N ("ambiguous prefix for slicing", N); | |
8905 | Set_Etype (N, Typ); | |
8906 | return; | |
8907 | else | |
8908 | Found := True; | |
8909 | Array_Type := It.Typ; | |
8910 | I1 := I; | |
8911 | end if; | |
8912 | else | |
8913 | Found := True; | |
8914 | Array_Type := It.Typ; | |
8915 | I1 := I; | |
8916 | end if; | |
8917 | end if; | |
8918 | ||
8919 | Get_Next_Interp (I, It); | |
8920 | end loop; | |
8921 | end; | |
8922 | ||
8923 | else | |
8924 | Array_Type := Etype (Name); | |
8925 | end if; | |
8926 | ||
8927 | Resolve (Name, Array_Type); | |
8928 | ||
8929 | if Is_Access_Type (Array_Type) then | |
8930 | Apply_Access_Check (N); | |
8931 | Array_Type := Designated_Type (Array_Type); | |
8932 | ||
c8ef728f ES |
8933 | -- If the prefix is an access to an unconstrained array, we must use |
8934 | -- the actual subtype of the object to perform the index checks. The | |
8935 | -- object denoted by the prefix is implicit in the node, so we build | |
8936 | -- an explicit representation for it in order to compute the actual | |
8937 | -- subtype. | |
82c80734 RD |
8938 | |
8939 | if not Is_Constrained (Array_Type) then | |
8940 | Remove_Side_Effects (Prefix (N)); | |
8941 | ||
8942 | declare | |
8943 | Obj : constant Node_Id := | |
8944 | Make_Explicit_Dereference (Sloc (N), | |
8945 | Prefix => New_Copy_Tree (Prefix (N))); | |
8946 | begin | |
8947 | Set_Etype (Obj, Array_Type); | |
8948 | Set_Parent (Obj, Parent (N)); | |
8949 | Array_Type := Get_Actual_Subtype (Obj); | |
8950 | end; | |
8951 | end if; | |
8952 | ||
996ae0b0 | 8953 | elsif Is_Entity_Name (Name) |
6c994759 | 8954 | or else Nkind (Name) = N_Explicit_Dereference |
996ae0b0 RK |
8955 | or else (Nkind (Name) = N_Function_Call |
8956 | and then not Is_Constrained (Etype (Name))) | |
8957 | then | |
8958 | Array_Type := Get_Actual_Subtype (Name); | |
aa5147f0 ES |
8959 | |
8960 | -- If the name is a selected component that depends on discriminants, | |
8961 | -- build an actual subtype for it. This can happen only when the name | |
8962 | -- itself is overloaded; otherwise the actual subtype is created when | |
8963 | -- the selected component is analyzed. | |
8964 | ||
8965 | elsif Nkind (Name) = N_Selected_Component | |
8966 | and then Full_Analysis | |
8967 | and then Depends_On_Discriminant (First_Index (Array_Type)) | |
8968 | then | |
8969 | declare | |
8970 | Act_Decl : constant Node_Id := | |
8971 | Build_Actual_Subtype_Of_Component (Array_Type, Name); | |
8972 | begin | |
8973 | Insert_Action (N, Act_Decl); | |
8974 | Array_Type := Defining_Identifier (Act_Decl); | |
8975 | end; | |
d79e621a GD |
8976 | |
8977 | -- Maybe this should just be "else", instead of checking for the | |
5cc9353d RD |
8978 | -- specific case of slice??? This is needed for the case where the |
8979 | -- prefix is an Image attribute, which gets expanded to a slice, and so | |
8980 | -- has a constrained subtype which we want to use for the slice range | |
8981 | -- check applied below (the range check won't get done if the | |
8982 | -- unconstrained subtype of the 'Image is used). | |
d79e621a GD |
8983 | |
8984 | elsif Nkind (Name) = N_Slice then | |
8985 | Array_Type := Etype (Name); | |
996ae0b0 RK |
8986 | end if; |
8987 | ||
8988 | -- If name was overloaded, set slice type correctly now | |
8989 | ||
8990 | Set_Etype (N, Array_Type); | |
8991 | ||
c8ef728f ES |
8992 | -- If the range is specified by a subtype mark, no resolution is |
8993 | -- necessary. Else resolve the bounds, and apply needed checks. | |
996ae0b0 RK |
8994 | |
8995 | if not Is_Entity_Name (Drange) then | |
5f44f0d4 AC |
8996 | if Ekind (Array_Type) = E_String_Literal_Subtype then |
8997 | Index_Type := Etype (String_Literal_Low_Bound (Array_Type)); | |
8998 | else | |
8999 | Index_Type := Etype (First_Index (Array_Type)); | |
9000 | end if; | |
9001 | ||
9002 | Resolve (Drange, Base_Type (Index_Type)); | |
996ae0b0 | 9003 | |
dbe945f1 AC |
9004 | if Nkind (Drange) = N_Range then |
9005 | ||
9006 | -- Ensure that side effects in the bounds are properly handled | |
9007 | ||
cae81f17 JM |
9008 | Force_Evaluation (Low_Bound (Drange)); |
9009 | Force_Evaluation (High_Bound (Drange)); | |
0669bebe GB |
9010 | |
9011 | -- Do not apply the range check to nodes associated with the | |
9012 | -- frontend expansion of the dispatch table. We first check | |
dbe945f1 | 9013 | -- if Ada.Tags is already loaded to avoid the addition of an |
0669bebe GB |
9014 | -- undesired dependence on such run-time unit. |
9015 | ||
dbe945f1 AC |
9016 | if not Tagged_Type_Expansion |
9017 | or else not | |
9018 | (RTU_Loaded (Ada_Tags) | |
cead616d AC |
9019 | and then Nkind (Prefix (N)) = N_Selected_Component |
9020 | and then Present (Entity (Selector_Name (Prefix (N)))) | |
9021 | and then Entity (Selector_Name (Prefix (N))) = | |
dbe945f1 AC |
9022 | RTE_Record_Component (RE_Prims_Ptr)) |
9023 | then | |
5f44f0d4 | 9024 | Apply_Range_Check (Drange, Index_Type); |
dbe945f1 | 9025 | end if; |
996ae0b0 RK |
9026 | end if; |
9027 | end if; | |
9028 | ||
9029 | Set_Slice_Subtype (N); | |
aa180613 | 9030 | |
ea034236 AC |
9031 | -- Check bad use of type with predicates |
9032 | ||
9033 | if Has_Predicates (Etype (Drange)) then | |
ed00f472 | 9034 | Bad_Predicated_Subtype_Use |
ea034236 AC |
9035 | ("subtype& has predicate, not allowed in slice", |
9036 | Drange, Etype (Drange)); | |
9037 | ||
9038 | -- Otherwise here is where we check suspicious indexes | |
9039 | ||
9040 | elsif Nkind (Drange) = N_Range then | |
aa180613 RD |
9041 | Warn_On_Suspicious_Index (Name, Low_Bound (Drange)); |
9042 | Warn_On_Suspicious_Index (Name, High_Bound (Drange)); | |
9043 | end if; | |
9044 | ||
dec6faf1 | 9045 | Analyze_Dimension (N); |
996ae0b0 | 9046 | Eval_Slice (N); |
996ae0b0 RK |
9047 | end Resolve_Slice; |
9048 | ||
9049 | ---------------------------- | |
9050 | -- Resolve_String_Literal -- | |
9051 | ---------------------------- | |
9052 | ||
9053 | procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is | |
9054 | C_Typ : constant Entity_Id := Component_Type (Typ); | |
9055 | R_Typ : constant Entity_Id := Root_Type (C_Typ); | |
9056 | Loc : constant Source_Ptr := Sloc (N); | |
9057 | Str : constant String_Id := Strval (N); | |
9058 | Strlen : constant Nat := String_Length (Str); | |
9059 | Subtype_Id : Entity_Id; | |
9060 | Need_Check : Boolean; | |
9061 | ||
9062 | begin | |
9063 | -- For a string appearing in a concatenation, defer creation of the | |
9064 | -- string_literal_subtype until the end of the resolution of the | |
c8ef728f ES |
9065 | -- concatenation, because the literal may be constant-folded away. This |
9066 | -- is a useful optimization for long concatenation expressions. | |
996ae0b0 | 9067 | |
c8ef728f | 9068 | -- If the string is an aggregate built for a single character (which |
996ae0b0 | 9069 | -- happens in a non-static context) or a is null string to which special |
c8ef728f ES |
9070 | -- checks may apply, we build the subtype. Wide strings must also get a |
9071 | -- string subtype if they come from a one character aggregate. Strings | |
996ae0b0 RK |
9072 | -- generated by attributes might be static, but it is often hard to |
9073 | -- determine whether the enclosing context is static, so we generate | |
9074 | -- subtypes for them as well, thus losing some rarer optimizations ??? | |
9075 | -- Same for strings that come from a static conversion. | |
9076 | ||
9077 | Need_Check := | |
9078 | (Strlen = 0 and then Typ /= Standard_String) | |
9079 | or else Nkind (Parent (N)) /= N_Op_Concat | |
9080 | or else (N /= Left_Opnd (Parent (N)) | |
9081 | and then N /= Right_Opnd (Parent (N))) | |
82c80734 RD |
9082 | or else ((Typ = Standard_Wide_String |
9083 | or else Typ = Standard_Wide_Wide_String) | |
996ae0b0 RK |
9084 | and then Nkind (Original_Node (N)) /= N_String_Literal); |
9085 | ||
d81b4bfe TQ |
9086 | -- If the resolving type is itself a string literal subtype, we can just |
9087 | -- reuse it, since there is no point in creating another. | |
996ae0b0 RK |
9088 | |
9089 | if Ekind (Typ) = E_String_Literal_Subtype then | |
9090 | Subtype_Id := Typ; | |
9091 | ||
9092 | elsif Nkind (Parent (N)) = N_Op_Concat | |
9093 | and then not Need_Check | |
45fc7ddb HK |
9094 | and then not Nkind_In (Original_Node (N), N_Character_Literal, |
9095 | N_Attribute_Reference, | |
9096 | N_Qualified_Expression, | |
9097 | N_Type_Conversion) | |
996ae0b0 RK |
9098 | then |
9099 | Subtype_Id := Typ; | |
9100 | ||
9101 | -- Otherwise we must create a string literal subtype. Note that the | |
9102 | -- whole idea of string literal subtypes is simply to avoid the need | |
9103 | -- for building a full fledged array subtype for each literal. | |
45fc7ddb | 9104 | |
996ae0b0 RK |
9105 | else |
9106 | Set_String_Literal_Subtype (N, Typ); | |
9107 | Subtype_Id := Etype (N); | |
9108 | end if; | |
9109 | ||
9110 | if Nkind (Parent (N)) /= N_Op_Concat | |
9111 | or else Need_Check | |
9112 | then | |
9113 | Set_Etype (N, Subtype_Id); | |
9114 | Eval_String_Literal (N); | |
9115 | end if; | |
9116 | ||
9117 | if Is_Limited_Composite (Typ) | |
9118 | or else Is_Private_Composite (Typ) | |
9119 | then | |
9120 | Error_Msg_N ("string literal not available for private array", N); | |
9121 | Set_Etype (N, Any_Type); | |
9122 | return; | |
9123 | end if; | |
9124 | ||
d81b4bfe TQ |
9125 | -- The validity of a null string has been checked in the call to |
9126 | -- Eval_String_Literal. | |
996ae0b0 RK |
9127 | |
9128 | if Strlen = 0 then | |
9129 | return; | |
9130 | ||
c8ef728f ES |
9131 | -- Always accept string literal with component type Any_Character, which |
9132 | -- occurs in error situations and in comparisons of literals, both of | |
9133 | -- which should accept all literals. | |
996ae0b0 RK |
9134 | |
9135 | elsif R_Typ = Any_Character then | |
9136 | return; | |
9137 | ||
f3d57416 RW |
9138 | -- If the type is bit-packed, then we always transform the string |
9139 | -- literal into a full fledged aggregate. | |
996ae0b0 RK |
9140 | |
9141 | elsif Is_Bit_Packed_Array (Typ) then | |
9142 | null; | |
9143 | ||
82c80734 | 9144 | -- Deal with cases of Wide_Wide_String, Wide_String, and String |
996ae0b0 RK |
9145 | |
9146 | else | |
82c80734 RD |
9147 | -- For Standard.Wide_Wide_String, or any other type whose component |
9148 | -- type is Standard.Wide_Wide_Character, we know that all the | |
996ae0b0 RK |
9149 | -- characters in the string must be acceptable, since the parser |
9150 | -- accepted the characters as valid character literals. | |
9151 | ||
82c80734 | 9152 | if R_Typ = Standard_Wide_Wide_Character then |
996ae0b0 RK |
9153 | null; |
9154 | ||
c8ef728f ES |
9155 | -- For the case of Standard.String, or any other type whose component |
9156 | -- type is Standard.Character, we must make sure that there are no | |
9157 | -- wide characters in the string, i.e. that it is entirely composed | |
9158 | -- of characters in range of type Character. | |
996ae0b0 | 9159 | |
c8ef728f ES |
9160 | -- If the string literal is the result of a static concatenation, the |
9161 | -- test has already been performed on the components, and need not be | |
9162 | -- repeated. | |
996ae0b0 RK |
9163 | |
9164 | elsif R_Typ = Standard_Character | |
9165 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
9166 | then | |
9167 | for J in 1 .. Strlen loop | |
9168 | if not In_Character_Range (Get_String_Char (Str, J)) then | |
9169 | ||
9170 | -- If we are out of range, post error. This is one of the | |
9171 | -- very few places that we place the flag in the middle of | |
d81b4bfe TQ |
9172 | -- a token, right under the offending wide character. Not |
9173 | -- quite clear if this is right wrt wide character encoding | |
9174 | -- sequences, but it's only an error message! | |
996ae0b0 RK |
9175 | |
9176 | Error_Msg | |
82c80734 RD |
9177 | ("literal out of range of type Standard.Character", |
9178 | Source_Ptr (Int (Loc) + J)); | |
9179 | return; | |
9180 | end if; | |
9181 | end loop; | |
9182 | ||
9183 | -- For the case of Standard.Wide_String, or any other type whose | |
9184 | -- component type is Standard.Wide_Character, we must make sure that | |
9185 | -- there are no wide characters in the string, i.e. that it is | |
9186 | -- entirely composed of characters in range of type Wide_Character. | |
9187 | ||
9188 | -- If the string literal is the result of a static concatenation, | |
9189 | -- the test has already been performed on the components, and need | |
9190 | -- not be repeated. | |
9191 | ||
9192 | elsif R_Typ = Standard_Wide_Character | |
9193 | and then Nkind (Original_Node (N)) /= N_Op_Concat | |
9194 | then | |
9195 | for J in 1 .. Strlen loop | |
9196 | if not In_Wide_Character_Range (Get_String_Char (Str, J)) then | |
9197 | ||
9198 | -- If we are out of range, post error. This is one of the | |
9199 | -- very few places that we place the flag in the middle of | |
9200 | -- a token, right under the offending wide character. | |
9201 | ||
9202 | -- This is not quite right, because characters in general | |
9203 | -- will take more than one character position ??? | |
9204 | ||
9205 | Error_Msg | |
9206 | ("literal out of range of type Standard.Wide_Character", | |
996ae0b0 RK |
9207 | Source_Ptr (Int (Loc) + J)); |
9208 | return; | |
9209 | end if; | |
9210 | end loop; | |
9211 | ||
9212 | -- If the root type is not a standard character, then we will convert | |
9213 | -- the string into an aggregate and will let the aggregate code do | |
82c80734 | 9214 | -- the checking. Standard Wide_Wide_Character is also OK here. |
996ae0b0 RK |
9215 | |
9216 | else | |
9217 | null; | |
996ae0b0 RK |
9218 | end if; |
9219 | ||
c8ef728f ES |
9220 | -- See if the component type of the array corresponding to the string |
9221 | -- has compile time known bounds. If yes we can directly check | |
9222 | -- whether the evaluation of the string will raise constraint error. | |
9223 | -- Otherwise we need to transform the string literal into the | |
5cc9353d RD |
9224 | -- corresponding character aggregate and let the aggregate code do |
9225 | -- the checking. | |
996ae0b0 | 9226 | |
45fc7ddb HK |
9227 | if Is_Standard_Character_Type (R_Typ) then |
9228 | ||
996ae0b0 RK |
9229 | -- Check for the case of full range, where we are definitely OK |
9230 | ||
9231 | if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then | |
9232 | return; | |
9233 | end if; | |
9234 | ||
9235 | -- Here the range is not the complete base type range, so check | |
9236 | ||
9237 | declare | |
9238 | Comp_Typ_Lo : constant Node_Id := | |
9239 | Type_Low_Bound (Component_Type (Typ)); | |
9240 | Comp_Typ_Hi : constant Node_Id := | |
9241 | Type_High_Bound (Component_Type (Typ)); | |
9242 | ||
9243 | Char_Val : Uint; | |
9244 | ||
9245 | begin | |
9246 | if Compile_Time_Known_Value (Comp_Typ_Lo) | |
9247 | and then Compile_Time_Known_Value (Comp_Typ_Hi) | |
9248 | then | |
9249 | for J in 1 .. Strlen loop | |
9250 | Char_Val := UI_From_Int (Int (Get_String_Char (Str, J))); | |
9251 | ||
9252 | if Char_Val < Expr_Value (Comp_Typ_Lo) | |
9253 | or else Char_Val > Expr_Value (Comp_Typ_Hi) | |
9254 | then | |
9255 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 9256 | (N, "character out of range?", CE_Range_Check_Failed, |
996ae0b0 RK |
9257 | Loc => Source_Ptr (Int (Loc) + J)); |
9258 | end if; | |
9259 | end loop; | |
9260 | ||
9261 | return; | |
9262 | end if; | |
9263 | end; | |
9264 | end if; | |
9265 | end if; | |
9266 | ||
9267 | -- If we got here we meed to transform the string literal into the | |
9268 | -- equivalent qualified positional array aggregate. This is rather | |
9269 | -- heavy artillery for this situation, but it is hard work to avoid. | |
9270 | ||
9271 | declare | |
fbf5a39b | 9272 | Lits : constant List_Id := New_List; |
996ae0b0 RK |
9273 | P : Source_Ptr := Loc + 1; |
9274 | C : Char_Code; | |
9275 | ||
9276 | begin | |
c8ef728f ES |
9277 | -- Build the character literals, we give them source locations that |
9278 | -- correspond to the string positions, which is a bit tricky given | |
9279 | -- the possible presence of wide character escape sequences. | |
996ae0b0 RK |
9280 | |
9281 | for J in 1 .. Strlen loop | |
9282 | C := Get_String_Char (Str, J); | |
9283 | Set_Character_Literal_Name (C); | |
9284 | ||
9285 | Append_To (Lits, | |
82c80734 RD |
9286 | Make_Character_Literal (P, |
9287 | Chars => Name_Find, | |
9288 | Char_Literal_Value => UI_From_CC (C))); | |
996ae0b0 RK |
9289 | |
9290 | if In_Character_Range (C) then | |
9291 | P := P + 1; | |
9292 | ||
9293 | -- Should we have a call to Skip_Wide here ??? | |
5cc9353d | 9294 | |
996ae0b0 RK |
9295 | -- ??? else |
9296 | -- Skip_Wide (P); | |
9297 | ||
9298 | end if; | |
9299 | end loop; | |
9300 | ||
9301 | Rewrite (N, | |
9302 | Make_Qualified_Expression (Loc, | |
9303 | Subtype_Mark => New_Reference_To (Typ, Loc), | |
9304 | Expression => | |
9305 | Make_Aggregate (Loc, Expressions => Lits))); | |
9306 | ||
9307 | Analyze_And_Resolve (N, Typ); | |
9308 | end; | |
9309 | end Resolve_String_Literal; | |
9310 | ||
9311 | ----------------------------- | |
9312 | -- Resolve_Subprogram_Info -- | |
9313 | ----------------------------- | |
9314 | ||
9315 | procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is | |
9316 | begin | |
9317 | Set_Etype (N, Typ); | |
9318 | end Resolve_Subprogram_Info; | |
9319 | ||
9320 | ----------------------------- | |
9321 | -- Resolve_Type_Conversion -- | |
9322 | ----------------------------- | |
9323 | ||
9324 | procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is | |
4b2d2c13 AC |
9325 | Conv_OK : constant Boolean := Conversion_OK (N); |
9326 | Operand : constant Node_Id := Expression (N); | |
b7d1f17f HK |
9327 | Operand_Typ : constant Entity_Id := Etype (Operand); |
9328 | Target_Typ : constant Entity_Id := Etype (N); | |
996ae0b0 | 9329 | Rop : Node_Id; |
fbf5a39b AC |
9330 | Orig_N : Node_Id; |
9331 | Orig_T : Node_Id; | |
996ae0b0 | 9332 | |
ae2aa109 AC |
9333 | Test_Redundant : Boolean := Warn_On_Redundant_Constructs; |
9334 | -- Set to False to suppress cases where we want to suppress the test | |
9335 | -- for redundancy to avoid possible false positives on this warning. | |
9336 | ||
996ae0b0 | 9337 | begin |
996ae0b0 | 9338 | if not Conv_OK |
b7d1f17f | 9339 | and then not Valid_Conversion (N, Target_Typ, Operand) |
996ae0b0 RK |
9340 | then |
9341 | return; | |
9342 | end if; | |
9343 | ||
ae2aa109 AC |
9344 | -- If the Operand Etype is Universal_Fixed, then the conversion is |
9345 | -- never redundant. We need this check because by the time we have | |
9346 | -- finished the rather complex transformation, the conversion looks | |
9347 | -- redundant when it is not. | |
9348 | ||
9349 | if Operand_Typ = Universal_Fixed then | |
9350 | Test_Redundant := False; | |
9351 | ||
9352 | -- If the operand is marked as Any_Fixed, then special processing is | |
9353 | -- required. This is also a case where we suppress the test for a | |
9354 | -- redundant conversion, since most certainly it is not redundant. | |
9355 | ||
9356 | elsif Operand_Typ = Any_Fixed then | |
9357 | Test_Redundant := False; | |
996ae0b0 RK |
9358 | |
9359 | -- Mixed-mode operation involving a literal. Context must be a fixed | |
9360 | -- type which is applied to the literal subsequently. | |
9361 | ||
9362 | if Is_Fixed_Point_Type (Typ) then | |
9363 | Set_Etype (Operand, Universal_Real); | |
9364 | ||
9365 | elsif Is_Numeric_Type (Typ) | |
45fc7ddb | 9366 | and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide) |
996ae0b0 | 9367 | and then (Etype (Right_Opnd (Operand)) = Universal_Real |
45fc7ddb HK |
9368 | or else |
9369 | Etype (Left_Opnd (Operand)) = Universal_Real) | |
996ae0b0 | 9370 | then |
a77842bd TQ |
9371 | -- Return if expression is ambiguous |
9372 | ||
996ae0b0 | 9373 | if Unique_Fixed_Point_Type (N) = Any_Type then |
a77842bd | 9374 | return; |
82c80734 | 9375 | |
a77842bd TQ |
9376 | -- If nothing else, the available fixed type is Duration |
9377 | ||
9378 | else | |
996ae0b0 RK |
9379 | Set_Etype (Operand, Standard_Duration); |
9380 | end if; | |
9381 | ||
bc5f3720 | 9382 | -- Resolve the real operand with largest available precision |
9ebe3743 | 9383 | |
996ae0b0 RK |
9384 | if Etype (Right_Opnd (Operand)) = Universal_Real then |
9385 | Rop := New_Copy_Tree (Right_Opnd (Operand)); | |
9386 | else | |
9387 | Rop := New_Copy_Tree (Left_Opnd (Operand)); | |
9388 | end if; | |
9389 | ||
9ebe3743 | 9390 | Resolve (Rop, Universal_Real); |
996ae0b0 | 9391 | |
82c80734 RD |
9392 | -- If the operand is a literal (it could be a non-static and |
9393 | -- illegal exponentiation) check whether the use of Duration | |
9394 | -- is potentially inaccurate. | |
9395 | ||
9396 | if Nkind (Rop) = N_Real_Literal | |
9397 | and then Realval (Rop) /= Ureal_0 | |
996ae0b0 RK |
9398 | and then abs (Realval (Rop)) < Delta_Value (Standard_Duration) |
9399 | then | |
aa180613 | 9400 | Error_Msg_N |
aa5147f0 ES |
9401 | ("?universal real operand can only " & |
9402 | "be interpreted as Duration!", | |
aa180613 RD |
9403 | Rop); |
9404 | Error_Msg_N | |
aa5147f0 | 9405 | ("\?precision will be lost in the conversion!", Rop); |
996ae0b0 RK |
9406 | end if; |
9407 | ||
891a6e79 AC |
9408 | elsif Is_Numeric_Type (Typ) |
9409 | and then Nkind (Operand) in N_Op | |
9410 | and then Unique_Fixed_Point_Type (N) /= Any_Type | |
9411 | then | |
9412 | Set_Etype (Operand, Standard_Duration); | |
9413 | ||
996ae0b0 RK |
9414 | else |
9415 | Error_Msg_N ("invalid context for mixed mode operation", N); | |
9416 | Set_Etype (Operand, Any_Type); | |
9417 | return; | |
9418 | end if; | |
9419 | end if; | |
9420 | ||
fbf5a39b | 9421 | Resolve (Operand); |
996ae0b0 | 9422 | |
2ba431e5 YM |
9423 | -- In SPARK, a type conversion between array types should be restricted |
9424 | -- to types which have matching static bounds. | |
b0186f71 | 9425 | |
7b98672f YM |
9426 | -- Protect call to Matching_Static_Array_Bounds to avoid costly |
9427 | -- operation if not needed. | |
9428 | ||
9429 | if Restriction_Check_Required (SPARK) | |
9430 | and then Is_Array_Type (Target_Typ) | |
b0186f71 | 9431 | and then Is_Array_Type (Operand_Typ) |
db72f10a | 9432 | and then Operand_Typ /= Any_Composite -- or else Operand in error |
b0186f71 AC |
9433 | and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ) |
9434 | then | |
2ba431e5 | 9435 | Check_SPARK_Restriction |
fe5d3068 | 9436 | ("array types should have matching static bounds", N); |
b0186f71 AC |
9437 | end if; |
9438 | ||
e24329cd YM |
9439 | -- In formal mode, the operand of an ancestor type conversion must be an |
9440 | -- object (not an expression). | |
9441 | ||
9442 | if Is_Tagged_Type (Target_Typ) | |
9443 | and then not Is_Class_Wide_Type (Target_Typ) | |
9444 | and then Is_Tagged_Type (Operand_Typ) | |
9445 | and then not Is_Class_Wide_Type (Operand_Typ) | |
9446 | and then Is_Ancestor (Target_Typ, Operand_Typ) | |
9447 | and then not Is_SPARK_Object_Reference (Operand) | |
9448 | then | |
2ba431e5 | 9449 | Check_SPARK_Restriction ("object required", Operand); |
e24329cd YM |
9450 | end if; |
9451 | ||
dec6faf1 AC |
9452 | Analyze_Dimension (N); |
9453 | ||
996ae0b0 | 9454 | -- Note: we do the Eval_Type_Conversion call before applying the |
d81b4bfe TQ |
9455 | -- required checks for a subtype conversion. This is important, since |
9456 | -- both are prepared under certain circumstances to change the type | |
9457 | -- conversion to a constraint error node, but in the case of | |
9458 | -- Eval_Type_Conversion this may reflect an illegality in the static | |
9459 | -- case, and we would miss the illegality (getting only a warning | |
9460 | -- message), if we applied the type conversion checks first. | |
996ae0b0 RK |
9461 | |
9462 | Eval_Type_Conversion (N); | |
9463 | ||
d81b4bfe TQ |
9464 | -- Even when evaluation is not possible, we may be able to simplify the |
9465 | -- conversion or its expression. This needs to be done before applying | |
9466 | -- checks, since otherwise the checks may use the original expression | |
9467 | -- and defeat the simplifications. This is specifically the case for | |
9468 | -- elimination of the floating-point Truncation attribute in | |
9469 | -- float-to-int conversions. | |
0669bebe GB |
9470 | |
9471 | Simplify_Type_Conversion (N); | |
9472 | ||
d81b4bfe TQ |
9473 | -- If after evaluation we still have a type conversion, then we may need |
9474 | -- to apply checks required for a subtype conversion. | |
996ae0b0 RK |
9475 | |
9476 | -- Skip these type conversion checks if universal fixed operands | |
9477 | -- operands involved, since range checks are handled separately for | |
9478 | -- these cases (in the appropriate Expand routines in unit Exp_Fixd). | |
9479 | ||
9480 | if Nkind (N) = N_Type_Conversion | |
b7d1f17f HK |
9481 | and then not Is_Generic_Type (Root_Type (Target_Typ)) |
9482 | and then Target_Typ /= Universal_Fixed | |
9483 | and then Operand_Typ /= Universal_Fixed | |
996ae0b0 RK |
9484 | then |
9485 | Apply_Type_Conversion_Checks (N); | |
9486 | end if; | |
9487 | ||
d81b4bfe TQ |
9488 | -- Issue warning for conversion of simple object to its own type. We |
9489 | -- have to test the original nodes, since they may have been rewritten | |
9490 | -- by various optimizations. | |
fbf5a39b AC |
9491 | |
9492 | Orig_N := Original_Node (N); | |
996ae0b0 | 9493 | |
ae2aa109 AC |
9494 | -- Here we test for a redundant conversion if the warning mode is |
9495 | -- active (and was not locally reset), and we have a type conversion | |
9496 | -- from source not appearing in a generic instance. | |
9497 | ||
9498 | if Test_Redundant | |
fbf5a39b | 9499 | and then Nkind (Orig_N) = N_Type_Conversion |
ae2aa109 | 9500 | and then Comes_From_Source (Orig_N) |
5453d5bd | 9501 | and then not In_Instance |
996ae0b0 | 9502 | then |
fbf5a39b | 9503 | Orig_N := Original_Node (Expression (Orig_N)); |
b7d1f17f | 9504 | Orig_T := Target_Typ; |
fbf5a39b AC |
9505 | |
9506 | -- If the node is part of a larger expression, the Target_Type | |
9507 | -- may not be the original type of the node if the context is a | |
9508 | -- condition. Recover original type to see if conversion is needed. | |
9509 | ||
9510 | if Is_Boolean_Type (Orig_T) | |
9511 | and then Nkind (Parent (N)) in N_Op | |
9512 | then | |
9513 | Orig_T := Etype (Parent (N)); | |
9514 | end if; | |
9515 | ||
4adf3c50 | 9516 | -- If we have an entity name, then give the warning if the entity |
ae2aa109 AC |
9517 | -- is the right type, or if it is a loop parameter covered by the |
9518 | -- original type (that's needed because loop parameters have an | |
9519 | -- odd subtype coming from the bounds). | |
9520 | ||
9521 | if (Is_Entity_Name (Orig_N) | |
9522 | and then | |
9523 | (Etype (Entity (Orig_N)) = Orig_T | |
9524 | or else | |
9525 | (Ekind (Entity (Orig_N)) = E_Loop_Parameter | |
477bd732 | 9526 | and then Covers (Orig_T, Etype (Entity (Orig_N)))))) |
ae2aa109 | 9527 | |
477bd732 | 9528 | -- If not an entity, then type of expression must match |
ae2aa109 AC |
9529 | |
9530 | or else Etype (Orig_N) = Orig_T | |
fbf5a39b | 9531 | then |
4b2d2c13 AC |
9532 | -- One more check, do not give warning if the analyzed conversion |
9533 | -- has an expression with non-static bounds, and the bounds of the | |
9534 | -- target are static. This avoids junk warnings in cases where the | |
9535 | -- conversion is necessary to establish staticness, for example in | |
9536 | -- a case statement. | |
9537 | ||
9538 | if not Is_OK_Static_Subtype (Operand_Typ) | |
9539 | and then Is_OK_Static_Subtype (Target_Typ) | |
9540 | then | |
9541 | null; | |
9542 | ||
5cc9353d RD |
9543 | -- Finally, if this type conversion occurs in a context requiring |
9544 | -- a prefix, and the expression is a qualified expression then the | |
9545 | -- type conversion is not redundant, since a qualified expression | |
9546 | -- is not a prefix, whereas a type conversion is. For example, "X | |
9547 | -- := T'(Funx(...)).Y;" is illegal because a selected component | |
9548 | -- requires a prefix, but a type conversion makes it legal: "X := | |
9549 | -- T(T'(Funx(...))).Y;" | |
4adf3c50 | 9550 | |
9db0b232 AC |
9551 | -- In Ada 2012, a qualified expression is a name, so this idiom is |
9552 | -- no longer needed, but we still suppress the warning because it | |
9553 | -- seems unfriendly for warnings to pop up when you switch to the | |
9554 | -- newer language version. | |
be257e99 AC |
9555 | |
9556 | elsif Nkind (Orig_N) = N_Qualified_Expression | |
f5d96d00 AC |
9557 | and then Nkind_In (Parent (N), N_Attribute_Reference, |
9558 | N_Indexed_Component, | |
9559 | N_Selected_Component, | |
9560 | N_Slice, | |
9561 | N_Explicit_Dereference) | |
be257e99 AC |
9562 | then |
9563 | null; | |
9564 | ||
ae2aa109 AC |
9565 | -- Here we give the redundant conversion warning. If it is an |
9566 | -- entity, give the name of the entity in the message. If not, | |
9567 | -- just mention the expression. | |
4b2d2c13 AC |
9568 | |
9569 | else | |
ae2aa109 AC |
9570 | if Is_Entity_Name (Orig_N) then |
9571 | Error_Msg_Node_2 := Orig_T; | |
9572 | Error_Msg_NE -- CODEFIX | |
9573 | ("?redundant conversion, & is of type &!", | |
9574 | N, Entity (Orig_N)); | |
9575 | else | |
9576 | Error_Msg_NE | |
9577 | ("?redundant conversion, expression is of type&!", | |
9578 | N, Orig_T); | |
9579 | end if; | |
4b2d2c13 | 9580 | end if; |
fbf5a39b | 9581 | end if; |
996ae0b0 | 9582 | end if; |
758c442c | 9583 | |
b7d1f17f | 9584 | -- Ada 2005 (AI-251): Handle class-wide interface type conversions. |
0669bebe GB |
9585 | -- No need to perform any interface conversion if the type of the |
9586 | -- expression coincides with the target type. | |
758c442c | 9587 | |
0791fbe9 | 9588 | if Ada_Version >= Ada_2005 |
11fa950b | 9589 | and then Full_Expander_Active |
b7d1f17f | 9590 | and then Operand_Typ /= Target_Typ |
0669bebe | 9591 | then |
b7d1f17f HK |
9592 | declare |
9593 | Opnd : Entity_Id := Operand_Typ; | |
9594 | Target : Entity_Id := Target_Typ; | |
758c442c | 9595 | |
b7d1f17f HK |
9596 | begin |
9597 | if Is_Access_Type (Opnd) then | |
841dd0f5 | 9598 | Opnd := Designated_Type (Opnd); |
1420b484 JM |
9599 | end if; |
9600 | ||
b7d1f17f | 9601 | if Is_Access_Type (Target_Typ) then |
841dd0f5 | 9602 | Target := Designated_Type (Target); |
4197ae1e | 9603 | end if; |
c8ef728f | 9604 | |
b7d1f17f HK |
9605 | if Opnd = Target then |
9606 | null; | |
c8ef728f | 9607 | |
b7d1f17f | 9608 | -- Conversion from interface type |
ea985d95 | 9609 | |
b7d1f17f | 9610 | elsif Is_Interface (Opnd) then |
ea985d95 | 9611 | |
b7d1f17f | 9612 | -- Ada 2005 (AI-217): Handle entities from limited views |
aa180613 | 9613 | |
b7d1f17f HK |
9614 | if From_With_Type (Opnd) then |
9615 | Error_Msg_Qual_Level := 99; | |
305caf42 AC |
9616 | Error_Msg_NE -- CODEFIX |
9617 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
9618 | Cunit_Entity (Get_Source_Unit (Base_Type (Opnd)))); |
9619 | Error_Msg_N | |
9620 | ("type conversions require visibility of the full view", | |
9621 | N); | |
aa180613 | 9622 | |
aa5147f0 ES |
9623 | elsif From_With_Type (Target) |
9624 | and then not | |
9625 | (Is_Access_Type (Target_Typ) | |
9626 | and then Present (Non_Limited_View (Etype (Target)))) | |
9627 | then | |
b7d1f17f | 9628 | Error_Msg_Qual_Level := 99; |
305caf42 AC |
9629 | Error_Msg_NE -- CODEFIX |
9630 | ("missing WITH clause on package &", N, | |
b7d1f17f HK |
9631 | Cunit_Entity (Get_Source_Unit (Base_Type (Target)))); |
9632 | Error_Msg_N | |
9633 | ("type conversions require visibility of the full view", | |
9634 | N); | |
aa180613 | 9635 | |
b7d1f17f HK |
9636 | else |
9637 | Expand_Interface_Conversion (N, Is_Static => False); | |
9638 | end if; | |
9639 | ||
9640 | -- Conversion to interface type | |
9641 | ||
9642 | elsif Is_Interface (Target) then | |
9643 | ||
9644 | -- Handle subtypes | |
9645 | ||
8a95f4e8 | 9646 | if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then |
b7d1f17f HK |
9647 | Opnd := Etype (Opnd); |
9648 | end if; | |
9649 | ||
9650 | if not Interface_Present_In_Ancestor | |
9651 | (Typ => Opnd, | |
9652 | Iface => Target) | |
9653 | then | |
9654 | if Is_Class_Wide_Type (Opnd) then | |
9655 | ||
9656 | -- The static analysis is not enough to know if the | |
9657 | -- interface is implemented or not. Hence we must pass | |
9658 | -- the work to the expander to generate code to evaluate | |
e7c0dd39 | 9659 | -- the conversion at run time. |
b7d1f17f HK |
9660 | |
9661 | Expand_Interface_Conversion (N, Is_Static => False); | |
9662 | ||
9663 | else | |
9664 | Error_Msg_Name_1 := Chars (Etype (Target)); | |
9665 | Error_Msg_Name_2 := Chars (Opnd); | |
9666 | Error_Msg_N | |
9667 | ("wrong interface conversion (% is not a progenitor " & | |
9668 | "of %)", N); | |
9669 | end if; | |
9670 | ||
9671 | else | |
9672 | Expand_Interface_Conversion (N); | |
9673 | end if; | |
9674 | end if; | |
9675 | end; | |
758c442c | 9676 | end if; |
996ae0b0 RK |
9677 | end Resolve_Type_Conversion; |
9678 | ||
9679 | ---------------------- | |
9680 | -- Resolve_Unary_Op -- | |
9681 | ---------------------- | |
9682 | ||
9683 | procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
9684 | B_Typ : constant Entity_Id := Base_Type (Typ); |
9685 | R : constant Node_Id := Right_Opnd (N); | |
9686 | OK : Boolean; | |
9687 | Lo : Uint; | |
9688 | Hi : Uint; | |
996ae0b0 RK |
9689 | |
9690 | begin | |
7a489a2b AC |
9691 | if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then |
9692 | Error_Msg_Name_1 := Chars (Typ); | |
2ba431e5 | 9693 | Check_SPARK_Restriction |
7a489a2b AC |
9694 | ("unary operator not defined for modular type%", N); |
9695 | end if; | |
9696 | ||
b7d1f17f | 9697 | -- Deal with intrinsic unary operators |
996ae0b0 | 9698 | |
fbf5a39b AC |
9699 | if Comes_From_Source (N) |
9700 | and then Ekind (Entity (N)) = E_Function | |
9701 | and then Is_Imported (Entity (N)) | |
9702 | and then Is_Intrinsic_Subprogram (Entity (N)) | |
9703 | then | |
9704 | Resolve_Intrinsic_Unary_Operator (N, Typ); | |
9705 | return; | |
9706 | end if; | |
9707 | ||
0669bebe GB |
9708 | -- Deal with universal cases |
9709 | ||
996ae0b0 | 9710 | if Etype (R) = Universal_Integer |
0669bebe GB |
9711 | or else |
9712 | Etype (R) = Universal_Real | |
996ae0b0 RK |
9713 | then |
9714 | Check_For_Visible_Operator (N, B_Typ); | |
9715 | end if; | |
9716 | ||
9717 | Set_Etype (N, B_Typ); | |
9718 | Resolve (R, B_Typ); | |
fbf5a39b | 9719 | |
9ebe3743 HK |
9720 | -- Generate warning for expressions like abs (x mod 2) |
9721 | ||
9722 | if Warn_On_Redundant_Constructs | |
9723 | and then Nkind (N) = N_Op_Abs | |
9724 | then | |
9725 | Determine_Range (Right_Opnd (N), OK, Lo, Hi); | |
9726 | ||
9727 | if OK and then Hi >= Lo and then Lo >= 0 then | |
305caf42 | 9728 | Error_Msg_N -- CODEFIX |
9ebe3743 HK |
9729 | ("?abs applied to known non-negative value has no effect", N); |
9730 | end if; | |
9731 | end if; | |
9732 | ||
0669bebe GB |
9733 | -- Deal with reference generation |
9734 | ||
996ae0b0 | 9735 | Check_Unset_Reference (R); |
fbf5a39b | 9736 | Generate_Operator_Reference (N, B_Typ); |
dec6faf1 | 9737 | Analyze_Dimension (N); |
996ae0b0 RK |
9738 | Eval_Unary_Op (N); |
9739 | ||
9740 | -- Set overflow checking bit. Much cleverer code needed here eventually | |
9741 | -- and perhaps the Resolve routines should be separated for the various | |
9742 | -- arithmetic operations, since they will need different processing ??? | |
9743 | ||
9744 | if Nkind (N) in N_Op then | |
9745 | if not Overflow_Checks_Suppressed (Etype (N)) then | |
fbf5a39b | 9746 | Enable_Overflow_Check (N); |
996ae0b0 RK |
9747 | end if; |
9748 | end if; | |
0669bebe | 9749 | |
d81b4bfe TQ |
9750 | -- Generate warning for expressions like -5 mod 3 for integers. No need |
9751 | -- to worry in the floating-point case, since parens do not affect the | |
9752 | -- result so there is no point in giving in a warning. | |
0669bebe GB |
9753 | |
9754 | declare | |
9755 | Norig : constant Node_Id := Original_Node (N); | |
9756 | Rorig : Node_Id; | |
9757 | Val : Uint; | |
9758 | HB : Uint; | |
9759 | LB : Uint; | |
9760 | Lval : Uint; | |
9761 | Opnd : Node_Id; | |
9762 | ||
9763 | begin | |
9764 | if Warn_On_Questionable_Missing_Parens | |
9765 | and then Comes_From_Source (Norig) | |
9766 | and then Is_Integer_Type (Typ) | |
9767 | and then Nkind (Norig) = N_Op_Minus | |
9768 | then | |
9769 | Rorig := Original_Node (Right_Opnd (Norig)); | |
9770 | ||
9771 | -- We are looking for cases where the right operand is not | |
f3d57416 | 9772 | -- parenthesized, and is a binary operator, multiply, divide, or |
0669bebe GB |
9773 | -- mod. These are the cases where the grouping can affect results. |
9774 | ||
9775 | if Paren_Count (Rorig) = 0 | |
45fc7ddb | 9776 | and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide) |
0669bebe GB |
9777 | then |
9778 | -- For mod, we always give the warning, since the value is | |
9779 | -- affected by the parenthesization (e.g. (-5) mod 315 /= | |
d81b4bfe | 9780 | -- -(5 mod 315)). But for the other cases, the only concern is |
0669bebe GB |
9781 | -- overflow, e.g. for the case of 8 big signed (-(2 * 64) |
9782 | -- overflows, but (-2) * 64 does not). So we try to give the | |
9783 | -- message only when overflow is possible. | |
9784 | ||
9785 | if Nkind (Rorig) /= N_Op_Mod | |
9786 | and then Compile_Time_Known_Value (R) | |
9787 | then | |
9788 | Val := Expr_Value (R); | |
9789 | ||
9790 | if Compile_Time_Known_Value (Type_High_Bound (Typ)) then | |
9791 | HB := Expr_Value (Type_High_Bound (Typ)); | |
9792 | else | |
9793 | HB := Expr_Value (Type_High_Bound (Base_Type (Typ))); | |
9794 | end if; | |
9795 | ||
9796 | if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then | |
9797 | LB := Expr_Value (Type_Low_Bound (Typ)); | |
9798 | else | |
9799 | LB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); | |
9800 | end if; | |
9801 | ||
d81b4bfe TQ |
9802 | -- Note that the test below is deliberately excluding the |
9803 | -- largest negative number, since that is a potentially | |
0669bebe GB |
9804 | -- troublesome case (e.g. -2 * x, where the result is the |
9805 | -- largest negative integer has an overflow with 2 * x). | |
9806 | ||
9807 | if Val > LB and then Val <= HB then | |
9808 | return; | |
9809 | end if; | |
9810 | end if; | |
9811 | ||
9812 | -- For the multiplication case, the only case we have to worry | |
9813 | -- about is when (-a)*b is exactly the largest negative number | |
9814 | -- so that -(a*b) can cause overflow. This can only happen if | |
9815 | -- a is a power of 2, and more generally if any operand is a | |
9816 | -- constant that is not a power of 2, then the parentheses | |
9817 | -- cannot affect whether overflow occurs. We only bother to | |
9818 | -- test the left most operand | |
9819 | ||
9820 | -- Loop looking at left operands for one that has known value | |
9821 | ||
9822 | Opnd := Rorig; | |
9823 | Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop | |
9824 | if Compile_Time_Known_Value (Left_Opnd (Opnd)) then | |
9825 | Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd))); | |
9826 | ||
9827 | -- Operand value of 0 or 1 skips warning | |
9828 | ||
9829 | if Lval <= 1 then | |
9830 | return; | |
9831 | ||
9832 | -- Otherwise check power of 2, if power of 2, warn, if | |
9833 | -- anything else, skip warning. | |
9834 | ||
9835 | else | |
9836 | while Lval /= 2 loop | |
9837 | if Lval mod 2 = 1 then | |
9838 | return; | |
9839 | else | |
9840 | Lval := Lval / 2; | |
9841 | end if; | |
9842 | end loop; | |
9843 | ||
9844 | exit Opnd_Loop; | |
9845 | end if; | |
9846 | end if; | |
9847 | ||
9848 | -- Keep looking at left operands | |
9849 | ||
9850 | Opnd := Left_Opnd (Opnd); | |
9851 | end loop Opnd_Loop; | |
9852 | ||
9853 | -- For rem or "/" we can only have a problematic situation | |
9854 | -- if the divisor has a value of minus one or one. Otherwise | |
9855 | -- overflow is impossible (divisor > 1) or we have a case of | |
9856 | -- division by zero in any case. | |
9857 | ||
45fc7ddb | 9858 | if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem) |
0669bebe GB |
9859 | and then Compile_Time_Known_Value (Right_Opnd (Rorig)) |
9860 | and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1 | |
9861 | then | |
9862 | return; | |
9863 | end if; | |
9864 | ||
9865 | -- If we fall through warning should be issued | |
9866 | ||
ed2233dc | 9867 | Error_Msg_N |
aa5147f0 | 9868 | ("?unary minus expression should be parenthesized here!", N); |
0669bebe GB |
9869 | end if; |
9870 | end if; | |
9871 | end; | |
996ae0b0 RK |
9872 | end Resolve_Unary_Op; |
9873 | ||
9874 | ---------------------------------- | |
9875 | -- Resolve_Unchecked_Expression -- | |
9876 | ---------------------------------- | |
9877 | ||
9878 | procedure Resolve_Unchecked_Expression | |
9879 | (N : Node_Id; | |
9880 | Typ : Entity_Id) | |
9881 | is | |
9882 | begin | |
9883 | Resolve (Expression (N), Typ, Suppress => All_Checks); | |
9884 | Set_Etype (N, Typ); | |
9885 | end Resolve_Unchecked_Expression; | |
9886 | ||
9887 | --------------------------------------- | |
9888 | -- Resolve_Unchecked_Type_Conversion -- | |
9889 | --------------------------------------- | |
9890 | ||
9891 | procedure Resolve_Unchecked_Type_Conversion | |
9892 | (N : Node_Id; | |
9893 | Typ : Entity_Id) | |
9894 | is | |
07fc65c4 GB |
9895 | pragma Warnings (Off, Typ); |
9896 | ||
996ae0b0 RK |
9897 | Operand : constant Node_Id := Expression (N); |
9898 | Opnd_Type : constant Entity_Id := Etype (Operand); | |
9899 | ||
9900 | begin | |
a77842bd | 9901 | -- Resolve operand using its own type |
996ae0b0 RK |
9902 | |
9903 | Resolve (Operand, Opnd_Type); | |
dec6faf1 | 9904 | Analyze_Dimension (N); |
996ae0b0 | 9905 | Eval_Unchecked_Conversion (N); |
996ae0b0 RK |
9906 | end Resolve_Unchecked_Type_Conversion; |
9907 | ||
9908 | ------------------------------ | |
9909 | -- Rewrite_Operator_As_Call -- | |
9910 | ------------------------------ | |
9911 | ||
9912 | procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is | |
fbf5a39b AC |
9913 | Loc : constant Source_Ptr := Sloc (N); |
9914 | Actuals : constant List_Id := New_List; | |
996ae0b0 RK |
9915 | New_N : Node_Id; |
9916 | ||
9917 | begin | |
9918 | if Nkind (N) in N_Binary_Op then | |
9919 | Append (Left_Opnd (N), Actuals); | |
9920 | end if; | |
9921 | ||
9922 | Append (Right_Opnd (N), Actuals); | |
9923 | ||
9924 | New_N := | |
9925 | Make_Function_Call (Sloc => Loc, | |
9926 | Name => New_Occurrence_Of (Nam, Loc), | |
9927 | Parameter_Associations => Actuals); | |
9928 | ||
9929 | Preserve_Comes_From_Source (New_N, N); | |
9930 | Preserve_Comes_From_Source (Name (New_N), N); | |
9931 | Rewrite (N, New_N); | |
9932 | Set_Etype (N, Etype (Nam)); | |
9933 | end Rewrite_Operator_As_Call; | |
9934 | ||
9935 | ------------------------------ | |
9936 | -- Rewrite_Renamed_Operator -- | |
9937 | ------------------------------ | |
9938 | ||
0ab80019 AC |
9939 | procedure Rewrite_Renamed_Operator |
9940 | (N : Node_Id; | |
9941 | Op : Entity_Id; | |
9942 | Typ : Entity_Id) | |
9943 | is | |
996ae0b0 RK |
9944 | Nam : constant Name_Id := Chars (Op); |
9945 | Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
9946 | Op_Node : Node_Id; | |
9947 | ||
9948 | begin | |
d81b4bfe TQ |
9949 | -- Rewrite the operator node using the real operator, not its renaming. |
9950 | -- Exclude user-defined intrinsic operations of the same name, which are | |
9951 | -- treated separately and rewritten as calls. | |
996ae0b0 | 9952 | |
964f13da | 9953 | if Ekind (Op) /= E_Function or else Chars (N) /= Nam then |
996ae0b0 RK |
9954 | Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N)); |
9955 | Set_Chars (Op_Node, Nam); | |
9956 | Set_Etype (Op_Node, Etype (N)); | |
9957 | Set_Entity (Op_Node, Op); | |
9958 | Set_Right_Opnd (Op_Node, Right_Opnd (N)); | |
9959 | ||
b7d1f17f HK |
9960 | -- Indicate that both the original entity and its renaming are |
9961 | -- referenced at this point. | |
fbf5a39b AC |
9962 | |
9963 | Generate_Reference (Entity (N), N); | |
996ae0b0 RK |
9964 | Generate_Reference (Op, N); |
9965 | ||
9966 | if Is_Binary then | |
9967 | Set_Left_Opnd (Op_Node, Left_Opnd (N)); | |
9968 | end if; | |
9969 | ||
9970 | Rewrite (N, Op_Node); | |
0ab80019 | 9971 | |
1366997b AC |
9972 | -- If the context type is private, add the appropriate conversions so |
9973 | -- that the operator is applied to the full view. This is done in the | |
9974 | -- routines that resolve intrinsic operators. | |
0ab80019 AC |
9975 | |
9976 | if Is_Intrinsic_Subprogram (Op) | |
9977 | and then Is_Private_Type (Typ) | |
9978 | then | |
9979 | case Nkind (N) is | |
9980 | when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide | | |
9981 | N_Op_Expon | N_Op_Mod | N_Op_Rem => | |
9982 | Resolve_Intrinsic_Operator (N, Typ); | |
9983 | ||
d81b4bfe | 9984 | when N_Op_Plus | N_Op_Minus | N_Op_Abs => |
0ab80019 AC |
9985 | Resolve_Intrinsic_Unary_Operator (N, Typ); |
9986 | ||
9987 | when others => | |
9988 | Resolve (N, Typ); | |
9989 | end case; | |
9990 | end if; | |
9991 | ||
964f13da RD |
9992 | elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then |
9993 | ||
1366997b AC |
9994 | -- Operator renames a user-defined operator of the same name. Use the |
9995 | -- original operator in the node, which is the one Gigi knows about. | |
0ab80019 AC |
9996 | |
9997 | Set_Entity (N, Op); | |
9998 | Set_Is_Overloaded (N, False); | |
996ae0b0 RK |
9999 | end if; |
10000 | end Rewrite_Renamed_Operator; | |
10001 | ||
10002 | ----------------------- | |
10003 | -- Set_Slice_Subtype -- | |
10004 | ----------------------- | |
10005 | ||
1366997b AC |
10006 | -- Build an implicit subtype declaration to represent the type delivered by |
10007 | -- the slice. This is an abbreviated version of an array subtype. We define | |
10008 | -- an index subtype for the slice, using either the subtype name or the | |
10009 | -- discrete range of the slice. To be consistent with index usage elsewhere | |
10010 | -- we create a list header to hold the single index. This list is not | |
10011 | -- otherwise attached to the syntax tree. | |
996ae0b0 RK |
10012 | |
10013 | procedure Set_Slice_Subtype (N : Node_Id) is | |
10014 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 10015 | Index_List : constant List_Id := New_List; |
996ae0b0 | 10016 | Index : Node_Id; |
996ae0b0 RK |
10017 | Index_Subtype : Entity_Id; |
10018 | Index_Type : Entity_Id; | |
10019 | Slice_Subtype : Entity_Id; | |
10020 | Drange : constant Node_Id := Discrete_Range (N); | |
10021 | ||
10022 | begin | |
10023 | if Is_Entity_Name (Drange) then | |
10024 | Index_Subtype := Entity (Drange); | |
10025 | ||
10026 | else | |
10027 | -- We force the evaluation of a range. This is definitely needed in | |
10028 | -- the renamed case, and seems safer to do unconditionally. Note in | |
10029 | -- any case that since we will create and insert an Itype referring | |
10030 | -- to this range, we must make sure any side effect removal actions | |
10031 | -- are inserted before the Itype definition. | |
10032 | ||
10033 | if Nkind (Drange) = N_Range then | |
10034 | Force_Evaluation (Low_Bound (Drange)); | |
10035 | Force_Evaluation (High_Bound (Drange)); | |
10036 | end if; | |
10037 | ||
10038 | Index_Type := Base_Type (Etype (Drange)); | |
10039 | ||
10040 | Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
10041 | ||
8a95f4e8 | 10042 | -- Take a new copy of Drange (where bounds have been rewritten to |
3c1ecd7e AC |
10043 | -- reference side-effect-free names). Using a separate tree ensures |
10044 | -- that further expansion (e.g. while rewriting a slice assignment | |
8a95f4e8 RD |
10045 | -- into a FOR loop) does not attempt to remove side effects on the |
10046 | -- bounds again (which would cause the bounds in the index subtype | |
10047 | -- definition to refer to temporaries before they are defined) (the | |
10048 | -- reason is that some names are considered side effect free here | |
10049 | -- for the subtype, but not in the context of a loop iteration | |
10050 | -- scheme). | |
10051 | ||
10052 | Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange)); | |
4230bdb7 | 10053 | Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype); |
996ae0b0 RK |
10054 | Set_Etype (Index_Subtype, Index_Type); |
10055 | Set_Size_Info (Index_Subtype, Index_Type); | |
10056 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
10057 | end if; | |
10058 | ||
10059 | Slice_Subtype := Create_Itype (E_Array_Subtype, N); | |
10060 | ||
10061 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
10062 | Set_Etype (Index, Index_Subtype); | |
10063 | Append (Index, Index_List); | |
10064 | ||
996ae0b0 RK |
10065 | Set_First_Index (Slice_Subtype, Index); |
10066 | Set_Etype (Slice_Subtype, Base_Type (Etype (N))); | |
10067 | Set_Is_Constrained (Slice_Subtype, True); | |
996ae0b0 | 10068 | |
8a95f4e8 RD |
10069 | Check_Compile_Time_Size (Slice_Subtype); |
10070 | ||
b7d1f17f HK |
10071 | -- The Etype of the existing Slice node is reset to this slice subtype. |
10072 | -- Its bounds are obtained from its first index. | |
996ae0b0 RK |
10073 | |
10074 | Set_Etype (N, Slice_Subtype); | |
10075 | ||
5cc9353d RD |
10076 | -- For packed slice subtypes, freeze immediately (except in the case of |
10077 | -- being in a "spec expression" where we never freeze when we first see | |
10078 | -- the expression). | |
8a95f4e8 RD |
10079 | |
10080 | if Is_Packed (Slice_Subtype) and not In_Spec_Expression then | |
10081 | Freeze_Itype (Slice_Subtype, N); | |
996ae0b0 | 10082 | |
cfab0c49 AC |
10083 | -- For all other cases insert an itype reference in the slice's actions |
10084 | -- so that the itype is frozen at the proper place in the tree (i.e. at | |
10085 | -- the point where actions for the slice are analyzed). Note that this | |
10086 | -- is different from freezing the itype immediately, which might be | |
6ff6152d ES |
10087 | -- premature (e.g. if the slice is within a transient scope). This needs |
10088 | -- to be done only if expansion is enabled. | |
cfab0c49 | 10089 | |
11fa950b | 10090 | elsif Full_Expander_Active then |
8a95f4e8 RD |
10091 | Ensure_Defined (Typ => Slice_Subtype, N => N); |
10092 | end if; | |
996ae0b0 RK |
10093 | end Set_Slice_Subtype; |
10094 | ||
10095 | -------------------------------- | |
10096 | -- Set_String_Literal_Subtype -- | |
10097 | -------------------------------- | |
10098 | ||
10099 | procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is | |
c8ef728f ES |
10100 | Loc : constant Source_Ptr := Sloc (N); |
10101 | Low_Bound : constant Node_Id := | |
d81b4bfe | 10102 | Type_Low_Bound (Etype (First_Index (Typ))); |
996ae0b0 RK |
10103 | Subtype_Id : Entity_Id; |
10104 | ||
10105 | begin | |
10106 | if Nkind (N) /= N_String_Literal then | |
10107 | return; | |
996ae0b0 RK |
10108 | end if; |
10109 | ||
c8ef728f | 10110 | Subtype_Id := Create_Itype (E_String_Literal_Subtype, N); |
91b1417d AC |
10111 | Set_String_Literal_Length (Subtype_Id, UI_From_Int |
10112 | (String_Length (Strval (N)))); | |
c8ef728f ES |
10113 | Set_Etype (Subtype_Id, Base_Type (Typ)); |
10114 | Set_Is_Constrained (Subtype_Id); | |
10115 | Set_Etype (N, Subtype_Id); | |
10116 | ||
1366997b AC |
10117 | -- The low bound is set from the low bound of the corresponding index |
10118 | -- type. Note that we do not store the high bound in the string literal | |
10119 | -- subtype, but it can be deduced if necessary from the length and the | |
10120 | -- low bound. | |
996ae0b0 | 10121 | |
5f44f0d4 | 10122 | if Is_OK_Static_Expression (Low_Bound) then |
c8ef728f | 10123 | Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound); |
996ae0b0 | 10124 | |
5f44f0d4 AC |
10125 | -- If the lower bound is not static we create a range for the string |
10126 | -- literal, using the index type and the known length of the literal. | |
10127 | -- The index type is not necessarily Positive, so the upper bound is | |
10128 | -- computed as T'Val (T'Pos (Low_Bound) + L - 1). | |
c8ef728f | 10129 | |
5f44f0d4 | 10130 | else |
c8ef728f | 10131 | declare |
5f44f0d4 AC |
10132 | Index_List : constant List_Id := New_List; |
10133 | Index_Type : constant Entity_Id := Etype (First_Index (Typ)); | |
10134 | High_Bound : constant Node_Id := | |
53f29d4f AC |
10135 | Make_Attribute_Reference (Loc, |
10136 | Attribute_Name => Name_Val, | |
10137 | Prefix => | |
10138 | New_Occurrence_Of (Index_Type, Loc), | |
10139 | Expressions => New_List ( | |
10140 | Make_Op_Add (Loc, | |
10141 | Left_Opnd => | |
10142 | Make_Attribute_Reference (Loc, | |
10143 | Attribute_Name => Name_Pos, | |
10144 | Prefix => | |
10145 | New_Occurrence_Of (Index_Type, Loc), | |
10146 | Expressions => | |
10147 | New_List (New_Copy_Tree (Low_Bound))), | |
10148 | Right_Opnd => | |
10149 | Make_Integer_Literal (Loc, | |
10150 | String_Length (Strval (N)) - 1)))); | |
c0b11850 | 10151 | |
c8ef728f | 10152 | Array_Subtype : Entity_Id; |
c8ef728f ES |
10153 | Drange : Node_Id; |
10154 | Index : Node_Id; | |
5f44f0d4 | 10155 | Index_Subtype : Entity_Id; |
c8ef728f ES |
10156 | |
10157 | begin | |
56e94186 AC |
10158 | if Is_Integer_Type (Index_Type) then |
10159 | Set_String_Literal_Low_Bound | |
10160 | (Subtype_Id, Make_Integer_Literal (Loc, 1)); | |
10161 | ||
10162 | else | |
10163 | -- If the index type is an enumeration type, build bounds | |
10164 | -- expression with attributes. | |
10165 | ||
10166 | Set_String_Literal_Low_Bound | |
10167 | (Subtype_Id, | |
10168 | Make_Attribute_Reference (Loc, | |
10169 | Attribute_Name => Name_First, | |
10170 | Prefix => | |
10171 | New_Occurrence_Of (Base_Type (Index_Type), Loc))); | |
10172 | Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type); | |
10173 | end if; | |
10174 | ||
c0b11850 AC |
10175 | Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id)); |
10176 | ||
10177 | -- Build bona fide subtype for the string, and wrap it in an | |
10178 | -- unchecked conversion, because the backend expects the | |
10179 | -- String_Literal_Subtype to have a static lower bound. | |
10180 | ||
c8ef728f ES |
10181 | Index_Subtype := |
10182 | Create_Itype (Subtype_Kind (Ekind (Index_Type)), N); | |
0669bebe | 10183 | Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound); |
c8ef728f ES |
10184 | Set_Scalar_Range (Index_Subtype, Drange); |
10185 | Set_Parent (Drange, N); | |
10186 | Analyze_And_Resolve (Drange, Index_Type); | |
10187 | ||
36fcf362 RD |
10188 | -- In the context, the Index_Type may already have a constraint, |
10189 | -- so use common base type on string subtype. The base type may | |
10190 | -- be used when generating attributes of the string, for example | |
10191 | -- in the context of a slice assignment. | |
10192 | ||
4adf3c50 AC |
10193 | Set_Etype (Index_Subtype, Base_Type (Index_Type)); |
10194 | Set_Size_Info (Index_Subtype, Index_Type); | |
10195 | Set_RM_Size (Index_Subtype, RM_Size (Index_Type)); | |
c8ef728f ES |
10196 | |
10197 | Array_Subtype := Create_Itype (E_Array_Subtype, N); | |
10198 | ||
10199 | Index := New_Occurrence_Of (Index_Subtype, Loc); | |
10200 | Set_Etype (Index, Index_Subtype); | |
10201 | Append (Index, Index_List); | |
10202 | ||
10203 | Set_First_Index (Array_Subtype, Index); | |
10204 | Set_Etype (Array_Subtype, Base_Type (Typ)); | |
10205 | Set_Is_Constrained (Array_Subtype, True); | |
c8ef728f ES |
10206 | |
10207 | Rewrite (N, | |
10208 | Make_Unchecked_Type_Conversion (Loc, | |
10209 | Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc), | |
5f44f0d4 | 10210 | Expression => Relocate_Node (N))); |
c8ef728f ES |
10211 | Set_Etype (N, Array_Subtype); |
10212 | end; | |
10213 | end if; | |
996ae0b0 RK |
10214 | end Set_String_Literal_Subtype; |
10215 | ||
0669bebe GB |
10216 | ------------------------------ |
10217 | -- Simplify_Type_Conversion -- | |
10218 | ------------------------------ | |
10219 | ||
10220 | procedure Simplify_Type_Conversion (N : Node_Id) is | |
10221 | begin | |
10222 | if Nkind (N) = N_Type_Conversion then | |
10223 | declare | |
10224 | Operand : constant Node_Id := Expression (N); | |
10225 | Target_Typ : constant Entity_Id := Etype (N); | |
10226 | Opnd_Typ : constant Entity_Id := Etype (Operand); | |
10227 | ||
10228 | begin | |
10229 | if Is_Floating_Point_Type (Opnd_Typ) | |
10230 | and then | |
10231 | (Is_Integer_Type (Target_Typ) | |
10232 | or else (Is_Fixed_Point_Type (Target_Typ) | |
10233 | and then Conversion_OK (N))) | |
10234 | and then Nkind (Operand) = N_Attribute_Reference | |
10235 | and then Attribute_Name (Operand) = Name_Truncation | |
10236 | ||
10237 | -- Special processing required if the conversion is the expression | |
10238 | -- of a Truncation attribute reference. In this case we replace: | |
10239 | ||
10240 | -- ityp (ftyp'Truncation (x)) | |
10241 | ||
10242 | -- by | |
10243 | ||
10244 | -- ityp (x) | |
10245 | ||
4adf3c50 | 10246 | -- with the Float_Truncate flag set, which is more efficient. |
0669bebe GB |
10247 | |
10248 | then | |
10249 | Rewrite (Operand, | |
10250 | Relocate_Node (First (Expressions (Operand)))); | |
10251 | Set_Float_Truncate (N, True); | |
10252 | end if; | |
10253 | end; | |
10254 | end if; | |
10255 | end Simplify_Type_Conversion; | |
10256 | ||
996ae0b0 RK |
10257 | ----------------------------- |
10258 | -- Unique_Fixed_Point_Type -- | |
10259 | ----------------------------- | |
10260 | ||
10261 | function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is | |
10262 | T1 : Entity_Id := Empty; | |
10263 | T2 : Entity_Id; | |
10264 | Item : Node_Id; | |
10265 | Scop : Entity_Id; | |
10266 | ||
10267 | procedure Fixed_Point_Error; | |
d81b4bfe TQ |
10268 | -- Give error messages for true ambiguity. Messages are posted on node |
10269 | -- N, and entities T1, T2 are the possible interpretations. | |
a77842bd TQ |
10270 | |
10271 | ----------------------- | |
10272 | -- Fixed_Point_Error -- | |
10273 | ----------------------- | |
996ae0b0 RK |
10274 | |
10275 | procedure Fixed_Point_Error is | |
10276 | begin | |
ed2233dc AC |
10277 | Error_Msg_N ("ambiguous universal_fixed_expression", N); |
10278 | Error_Msg_NE ("\\possible interpretation as}", N, T1); | |
10279 | Error_Msg_NE ("\\possible interpretation as}", N, T2); | |
996ae0b0 RK |
10280 | end Fixed_Point_Error; |
10281 | ||
a77842bd TQ |
10282 | -- Start of processing for Unique_Fixed_Point_Type |
10283 | ||
996ae0b0 RK |
10284 | begin |
10285 | -- The operations on Duration are visible, so Duration is always a | |
10286 | -- possible interpretation. | |
10287 | ||
10288 | T1 := Standard_Duration; | |
10289 | ||
bc5f3720 | 10290 | -- Look for fixed-point types in enclosing scopes |
996ae0b0 | 10291 | |
fbf5a39b | 10292 | Scop := Current_Scope; |
996ae0b0 RK |
10293 | while Scop /= Standard_Standard loop |
10294 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
10295 | while Present (T2) loop |
10296 | if Is_Fixed_Point_Type (T2) | |
10297 | and then Current_Entity (T2) = T2 | |
10298 | and then Scope (Base_Type (T2)) = Scop | |
10299 | then | |
10300 | if Present (T1) then | |
10301 | Fixed_Point_Error; | |
10302 | return Any_Type; | |
10303 | else | |
10304 | T1 := T2; | |
10305 | end if; | |
10306 | end if; | |
10307 | ||
10308 | Next_Entity (T2); | |
10309 | end loop; | |
10310 | ||
10311 | Scop := Scope (Scop); | |
10312 | end loop; | |
10313 | ||
a77842bd | 10314 | -- Look for visible fixed type declarations in the context |
996ae0b0 RK |
10315 | |
10316 | Item := First (Context_Items (Cunit (Current_Sem_Unit))); | |
996ae0b0 | 10317 | while Present (Item) loop |
996ae0b0 RK |
10318 | if Nkind (Item) = N_With_Clause then |
10319 | Scop := Entity (Name (Item)); | |
10320 | T2 := First_Entity (Scop); | |
996ae0b0 RK |
10321 | while Present (T2) loop |
10322 | if Is_Fixed_Point_Type (T2) | |
10323 | and then Scope (Base_Type (T2)) = Scop | |
19fb051c | 10324 | and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2)) |
996ae0b0 RK |
10325 | then |
10326 | if Present (T1) then | |
10327 | Fixed_Point_Error; | |
10328 | return Any_Type; | |
10329 | else | |
10330 | T1 := T2; | |
10331 | end if; | |
10332 | end if; | |
10333 | ||
10334 | Next_Entity (T2); | |
10335 | end loop; | |
10336 | end if; | |
10337 | ||
10338 | Next (Item); | |
10339 | end loop; | |
10340 | ||
10341 | if Nkind (N) = N_Real_Literal then | |
aa5147f0 | 10342 | Error_Msg_NE ("?real literal interpreted as }!", N, T1); |
996ae0b0 | 10343 | else |
aa5147f0 | 10344 | Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1); |
996ae0b0 RK |
10345 | end if; |
10346 | ||
10347 | return T1; | |
10348 | end Unique_Fixed_Point_Type; | |
10349 | ||
10350 | ---------------------- | |
10351 | -- Valid_Conversion -- | |
10352 | ---------------------- | |
10353 | ||
10354 | function Valid_Conversion | |
6cce2156 GD |
10355 | (N : Node_Id; |
10356 | Target : Entity_Id; | |
10357 | Operand : Node_Id; | |
10358 | Report_Errs : Boolean := True) return Boolean | |
996ae0b0 | 10359 | is |
fbf5a39b | 10360 | Target_Type : constant Entity_Id := Base_Type (Target); |
996c8821 | 10361 | Opnd_Type : Entity_Id := Etype (Operand); |
996ae0b0 RK |
10362 | |
10363 | function Conversion_Check | |
10364 | (Valid : Boolean; | |
0ab80019 | 10365 | Msg : String) return Boolean; |
996ae0b0 RK |
10366 | -- Little routine to post Msg if Valid is False, returns Valid value |
10367 | ||
996c8821 RD |
10368 | -- The following are badly named, this kind of overloading is actively |
10369 | -- confusing in reading code, please rename to something like | |
10370 | -- Error_Msg_N_If_Reporting ??? | |
10371 | ||
6cce2156 GD |
10372 | procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id); |
10373 | -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments | |
10374 | ||
10375 | procedure Error_Msg_NE | |
10376 | (Msg : String; | |
10377 | N : Node_Or_Entity_Id; | |
10378 | E : Node_Or_Entity_Id); | |
10379 | -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments | |
10380 | ||
996ae0b0 RK |
10381 | function Valid_Tagged_Conversion |
10382 | (Target_Type : Entity_Id; | |
0ab80019 | 10383 | Opnd_Type : Entity_Id) return Boolean; |
996ae0b0 RK |
10384 | -- Specifically test for validity of tagged conversions |
10385 | ||
aa180613 | 10386 | function Valid_Array_Conversion return Boolean; |
4adf3c50 AC |
10387 | -- Check index and component conformance, and accessibility levels if |
10388 | -- the component types are anonymous access types (Ada 2005). | |
aa180613 | 10389 | |
996ae0b0 RK |
10390 | ---------------------- |
10391 | -- Conversion_Check -- | |
10392 | ---------------------- | |
10393 | ||
10394 | function Conversion_Check | |
10395 | (Valid : Boolean; | |
0ab80019 | 10396 | Msg : String) return Boolean |
996ae0b0 RK |
10397 | is |
10398 | begin | |
0a190dfd AC |
10399 | if not Valid |
10400 | ||
10401 | -- A generic unit has already been analyzed and we have verified | |
10402 | -- that a particular conversion is OK in that context. Since the | |
10403 | -- instance is reanalyzed without relying on the relationships | |
10404 | -- established during the analysis of the generic, it is possible | |
10405 | -- to end up with inconsistent views of private types. Do not emit | |
10406 | -- the error message in such cases. The rest of the machinery in | |
10407 | -- Valid_Conversion still ensures the proper compatibility of | |
10408 | -- target and operand types. | |
10409 | ||
10410 | and then not In_Instance | |
10411 | then | |
996ae0b0 RK |
10412 | Error_Msg_N (Msg, Operand); |
10413 | end if; | |
10414 | ||
10415 | return Valid; | |
10416 | end Conversion_Check; | |
10417 | ||
6cce2156 GD |
10418 | ----------------- |
10419 | -- Error_Msg_N -- | |
10420 | ----------------- | |
10421 | ||
10422 | procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is | |
10423 | begin | |
10424 | if Report_Errs then | |
10425 | Errout.Error_Msg_N (Msg, N); | |
10426 | end if; | |
10427 | end Error_Msg_N; | |
10428 | ||
10429 | ------------------ | |
10430 | -- Error_Msg_NE -- | |
10431 | ------------------ | |
10432 | ||
10433 | procedure Error_Msg_NE | |
10434 | (Msg : String; | |
10435 | N : Node_Or_Entity_Id; | |
10436 | E : Node_Or_Entity_Id) | |
10437 | is | |
10438 | begin | |
10439 | if Report_Errs then | |
10440 | Errout.Error_Msg_NE (Msg, N, E); | |
10441 | end if; | |
10442 | end Error_Msg_NE; | |
10443 | ||
aa180613 RD |
10444 | ---------------------------- |
10445 | -- Valid_Array_Conversion -- | |
10446 | ---------------------------- | |
10447 | ||
10448 | function Valid_Array_Conversion return Boolean | |
10449 | is | |
10450 | Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type); | |
10451 | Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type); | |
10452 | ||
10453 | Opnd_Index : Node_Id; | |
10454 | Opnd_Index_Type : Entity_Id; | |
10455 | ||
10456 | Target_Comp_Type : constant Entity_Id := | |
10457 | Component_Type (Target_Type); | |
10458 | Target_Comp_Base : constant Entity_Id := | |
10459 | Base_Type (Target_Comp_Type); | |
10460 | ||
10461 | Target_Index : Node_Id; | |
10462 | Target_Index_Type : Entity_Id; | |
10463 | ||
10464 | begin | |
10465 | -- Error if wrong number of dimensions | |
10466 | ||
10467 | if | |
10468 | Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type) | |
10469 | then | |
10470 | Error_Msg_N | |
10471 | ("incompatible number of dimensions for conversion", Operand); | |
10472 | return False; | |
10473 | ||
10474 | -- Number of dimensions matches | |
10475 | ||
10476 | else | |
10477 | -- Loop through indexes of the two arrays | |
10478 | ||
10479 | Target_Index := First_Index (Target_Type); | |
10480 | Opnd_Index := First_Index (Opnd_Type); | |
10481 | while Present (Target_Index) and then Present (Opnd_Index) loop | |
10482 | Target_Index_Type := Etype (Target_Index); | |
10483 | Opnd_Index_Type := Etype (Opnd_Index); | |
10484 | ||
10485 | -- Error if index types are incompatible | |
10486 | ||
10487 | if not (Is_Integer_Type (Target_Index_Type) | |
10488 | and then Is_Integer_Type (Opnd_Index_Type)) | |
10489 | and then (Root_Type (Target_Index_Type) | |
10490 | /= Root_Type (Opnd_Index_Type)) | |
10491 | then | |
10492 | Error_Msg_N | |
10493 | ("incompatible index types for array conversion", | |
10494 | Operand); | |
10495 | return False; | |
10496 | end if; | |
10497 | ||
10498 | Next_Index (Target_Index); | |
10499 | Next_Index (Opnd_Index); | |
10500 | end loop; | |
10501 | ||
10502 | -- If component types have same base type, all set | |
10503 | ||
10504 | if Target_Comp_Base = Opnd_Comp_Base then | |
10505 | null; | |
10506 | ||
10507 | -- Here if base types of components are not the same. The only | |
10508 | -- time this is allowed is if we have anonymous access types. | |
10509 | ||
10510 | -- The conversion of arrays of anonymous access types can lead | |
10511 | -- to dangling pointers. AI-392 formalizes the accessibility | |
10512 | -- checks that must be applied to such conversions to prevent | |
10513 | -- out-of-scope references. | |
10514 | ||
19fb051c AC |
10515 | elsif Ekind_In |
10516 | (Target_Comp_Base, E_Anonymous_Access_Type, | |
10517 | E_Anonymous_Access_Subprogram_Type) | |
aa180613 RD |
10518 | and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base) |
10519 | and then | |
10520 | Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type) | |
10521 | then | |
10522 | if Type_Access_Level (Target_Type) < | |
83e5da69 | 10523 | Deepest_Type_Access_Level (Opnd_Type) |
aa180613 RD |
10524 | then |
10525 | if In_Instance_Body then | |
83e5da69 AC |
10526 | Error_Msg_N |
10527 | ("?source array type has " & | |
10528 | "deeper accessibility level than target", Operand); | |
10529 | Error_Msg_N | |
10530 | ("\?Program_Error will be raised at run time", | |
10531 | Operand); | |
aa180613 RD |
10532 | Rewrite (N, |
10533 | Make_Raise_Program_Error (Sloc (N), | |
10534 | Reason => PE_Accessibility_Check_Failed)); | |
10535 | Set_Etype (N, Target_Type); | |
10536 | return False; | |
10537 | ||
10538 | -- Conversion not allowed because of accessibility levels | |
10539 | ||
10540 | else | |
83e5da69 AC |
10541 | Error_Msg_N |
10542 | ("source array type has " & | |
10543 | "deeper accessibility level than target", Operand); | |
aa180613 RD |
10544 | return False; |
10545 | end if; | |
19fb051c | 10546 | |
aa180613 RD |
10547 | else |
10548 | null; | |
10549 | end if; | |
10550 | ||
10551 | -- All other cases where component base types do not match | |
10552 | ||
10553 | else | |
10554 | Error_Msg_N | |
10555 | ("incompatible component types for array conversion", | |
10556 | Operand); | |
10557 | return False; | |
10558 | end if; | |
10559 | ||
45fc7ddb HK |
10560 | -- Check that component subtypes statically match. For numeric |
10561 | -- types this means that both must be either constrained or | |
10562 | -- unconstrained. For enumeration types the bounds must match. | |
10563 | -- All of this is checked in Subtypes_Statically_Match. | |
aa180613 | 10564 | |
45fc7ddb | 10565 | if not Subtypes_Statically_Match |
83e5da69 | 10566 | (Target_Comp_Type, Opnd_Comp_Type) |
aa180613 RD |
10567 | then |
10568 | Error_Msg_N | |
10569 | ("component subtypes must statically match", Operand); | |
10570 | return False; | |
10571 | end if; | |
10572 | end if; | |
10573 | ||
10574 | return True; | |
10575 | end Valid_Array_Conversion; | |
10576 | ||
996ae0b0 RK |
10577 | ----------------------------- |
10578 | -- Valid_Tagged_Conversion -- | |
10579 | ----------------------------- | |
10580 | ||
10581 | function Valid_Tagged_Conversion | |
10582 | (Target_Type : Entity_Id; | |
0ab80019 | 10583 | Opnd_Type : Entity_Id) return Boolean |
996ae0b0 RK |
10584 | is |
10585 | begin | |
a77842bd | 10586 | -- Upward conversions are allowed (RM 4.6(22)) |
996ae0b0 RK |
10587 | |
10588 | if Covers (Target_Type, Opnd_Type) | |
10589 | or else Is_Ancestor (Target_Type, Opnd_Type) | |
10590 | then | |
10591 | return True; | |
10592 | ||
a77842bd TQ |
10593 | -- Downward conversion are allowed if the operand is class-wide |
10594 | -- (RM 4.6(23)). | |
996ae0b0 RK |
10595 | |
10596 | elsif Is_Class_Wide_Type (Opnd_Type) | |
b7d1f17f | 10597 | and then Covers (Opnd_Type, Target_Type) |
996ae0b0 RK |
10598 | then |
10599 | return True; | |
10600 | ||
10601 | elsif Covers (Opnd_Type, Target_Type) | |
10602 | or else Is_Ancestor (Opnd_Type, Target_Type) | |
10603 | then | |
10604 | return | |
10605 | Conversion_Check (False, | |
10606 | "downward conversion of tagged objects not allowed"); | |
758c442c | 10607 | |
0669bebe GB |
10608 | -- Ada 2005 (AI-251): The conversion to/from interface types is |
10609 | -- always valid | |
758c442c | 10610 | |
0669bebe | 10611 | elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then |
758c442c GD |
10612 | return True; |
10613 | ||
b7d1f17f HK |
10614 | -- If the operand is a class-wide type obtained through a limited_ |
10615 | -- with clause, and the context includes the non-limited view, use | |
10616 | -- it to determine whether the conversion is legal. | |
10617 | ||
10618 | elsif Is_Class_Wide_Type (Opnd_Type) | |
10619 | and then From_With_Type (Opnd_Type) | |
10620 | and then Present (Non_Limited_View (Etype (Opnd_Type))) | |
10621 | and then Is_Interface (Non_Limited_View (Etype (Opnd_Type))) | |
10622 | then | |
10623 | return True; | |
10624 | ||
aa180613 RD |
10625 | elsif Is_Access_Type (Opnd_Type) |
10626 | and then Is_Interface (Directly_Designated_Type (Opnd_Type)) | |
10627 | then | |
10628 | return True; | |
10629 | ||
996ae0b0 RK |
10630 | else |
10631 | Error_Msg_NE | |
10632 | ("invalid tagged conversion, not compatible with}", | |
10633 | N, First_Subtype (Opnd_Type)); | |
10634 | return False; | |
10635 | end if; | |
10636 | end Valid_Tagged_Conversion; | |
10637 | ||
10638 | -- Start of processing for Valid_Conversion | |
10639 | ||
10640 | begin | |
10641 | Check_Parameterless_Call (Operand); | |
10642 | ||
10643 | if Is_Overloaded (Operand) then | |
10644 | declare | |
10645 | I : Interp_Index; | |
10646 | I1 : Interp_Index; | |
10647 | It : Interp; | |
10648 | It1 : Interp; | |
10649 | N1 : Entity_Id; | |
f0d10385 | 10650 | T1 : Entity_Id; |
996ae0b0 RK |
10651 | |
10652 | begin | |
d81b4bfe TQ |
10653 | -- Remove procedure calls, which syntactically cannot appear in |
10654 | -- this context, but which cannot be removed by type checking, | |
996ae0b0 RK |
10655 | -- because the context does not impose a type. |
10656 | ||
1420b484 JM |
10657 | -- When compiling for VMS, spurious ambiguities can be produced |
10658 | -- when arithmetic operations have a literal operand and return | |
10659 | -- System.Address or a descendant of it. These ambiguities are | |
10660 | -- otherwise resolved by the context, but for conversions there | |
10661 | -- is no context type and the removal of the spurious operations | |
10662 | -- must be done explicitly here. | |
10663 | ||
4adf3c50 AC |
10664 | -- The node may be labelled overloaded, but still contain only one |
10665 | -- interpretation because others were discarded earlier. If this | |
10666 | -- is the case, retain the single interpretation if legal. | |
9ebe3743 | 10667 | |
996ae0b0 | 10668 | Get_First_Interp (Operand, I, It); |
9ebe3743 HK |
10669 | Opnd_Type := It.Typ; |
10670 | Get_Next_Interp (I, It); | |
996ae0b0 | 10671 | |
9ebe3743 HK |
10672 | if Present (It.Typ) |
10673 | and then Opnd_Type /= Standard_Void_Type | |
10674 | then | |
10675 | -- More than one candidate interpretation is available | |
996ae0b0 | 10676 | |
9ebe3743 HK |
10677 | Get_First_Interp (Operand, I, It); |
10678 | while Present (It.Typ) loop | |
10679 | if It.Typ = Standard_Void_Type then | |
10680 | Remove_Interp (I); | |
10681 | end if; | |
1420b484 | 10682 | |
9ebe3743 HK |
10683 | if Present (System_Aux_Id) |
10684 | and then Is_Descendent_Of_Address (It.Typ) | |
10685 | then | |
10686 | Remove_Interp (I); | |
10687 | end if; | |
10688 | ||
10689 | Get_Next_Interp (I, It); | |
10690 | end loop; | |
10691 | end if; | |
996ae0b0 RK |
10692 | |
10693 | Get_First_Interp (Operand, I, It); | |
10694 | I1 := I; | |
10695 | It1 := It; | |
10696 | ||
10697 | if No (It.Typ) then | |
10698 | Error_Msg_N ("illegal operand in conversion", Operand); | |
10699 | return False; | |
10700 | end if; | |
10701 | ||
10702 | Get_Next_Interp (I, It); | |
10703 | ||
10704 | if Present (It.Typ) then | |
10705 | N1 := It1.Nam; | |
f0d10385 | 10706 | T1 := It1.Typ; |
996ae0b0 RK |
10707 | It1 := Disambiguate (Operand, I1, I, Any_Type); |
10708 | ||
10709 | if It1 = No_Interp then | |
10710 | Error_Msg_N ("ambiguous operand in conversion", Operand); | |
10711 | ||
f0d10385 AC |
10712 | -- If the interpretation involves a standard operator, use |
10713 | -- the location of the type, which may be user-defined. | |
10714 | ||
10715 | if Sloc (It.Nam) = Standard_Location then | |
10716 | Error_Msg_Sloc := Sloc (It.Typ); | |
10717 | else | |
10718 | Error_Msg_Sloc := Sloc (It.Nam); | |
10719 | end if; | |
10720 | ||
4e7a4f6e AC |
10721 | Error_Msg_N -- CODEFIX |
10722 | ("\\possible interpretation#!", Operand); | |
996ae0b0 | 10723 | |
f0d10385 AC |
10724 | if Sloc (N1) = Standard_Location then |
10725 | Error_Msg_Sloc := Sloc (T1); | |
10726 | else | |
10727 | Error_Msg_Sloc := Sloc (N1); | |
10728 | end if; | |
10729 | ||
4e7a4f6e AC |
10730 | Error_Msg_N -- CODEFIX |
10731 | ("\\possible interpretation#!", Operand); | |
996ae0b0 RK |
10732 | |
10733 | return False; | |
10734 | end if; | |
10735 | end if; | |
10736 | ||
10737 | Set_Etype (Operand, It1.Typ); | |
10738 | Opnd_Type := It1.Typ; | |
10739 | end; | |
10740 | end if; | |
10741 | ||
aa180613 | 10742 | -- Numeric types |
996ae0b0 | 10743 | |
aa180613 | 10744 | if Is_Numeric_Type (Target_Type) then |
996ae0b0 | 10745 | |
aa180613 | 10746 | -- A universal fixed expression can be converted to any numeric type |
996ae0b0 | 10747 | |
996ae0b0 RK |
10748 | if Opnd_Type = Universal_Fixed then |
10749 | return True; | |
7324bf49 | 10750 | |
aa180613 RD |
10751 | -- Also no need to check when in an instance or inlined body, because |
10752 | -- the legality has been established when the template was analyzed. | |
10753 | -- Furthermore, numeric conversions may occur where only a private | |
f3d57416 | 10754 | -- view of the operand type is visible at the instantiation point. |
aa180613 RD |
10755 | -- This results in a spurious error if we check that the operand type |
10756 | -- is a numeric type. | |
10757 | ||
10758 | -- Note: in a previous version of this unit, the following tests were | |
10759 | -- applied only for generated code (Comes_From_Source set to False), | |
10760 | -- but in fact the test is required for source code as well, since | |
10761 | -- this situation can arise in source code. | |
10762 | ||
10763 | elsif In_Instance or else In_Inlined_Body then | |
d347f572 | 10764 | return True; |
aa180613 RD |
10765 | |
10766 | -- Otherwise we need the conversion check | |
7324bf49 | 10767 | |
996ae0b0 | 10768 | else |
aa180613 RD |
10769 | return Conversion_Check |
10770 | (Is_Numeric_Type (Opnd_Type), | |
10771 | "illegal operand for numeric conversion"); | |
996ae0b0 RK |
10772 | end if; |
10773 | ||
aa180613 RD |
10774 | -- Array types |
10775 | ||
996ae0b0 RK |
10776 | elsif Is_Array_Type (Target_Type) then |
10777 | if not Is_Array_Type (Opnd_Type) | |
10778 | or else Opnd_Type = Any_Composite | |
10779 | or else Opnd_Type = Any_String | |
10780 | then | |
4adf3c50 | 10781 | Error_Msg_N ("illegal operand for array conversion", Operand); |
996ae0b0 | 10782 | return False; |
996ae0b0 | 10783 | else |
aa180613 | 10784 | return Valid_Array_Conversion; |
996ae0b0 RK |
10785 | end if; |
10786 | ||
e65f50ec ES |
10787 | -- Ada 2005 (AI-251): Anonymous access types where target references an |
10788 | -- interface type. | |
758c442c | 10789 | |
964f13da RD |
10790 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10791 | E_Anonymous_Access_Type) | |
758c442c GD |
10792 | and then Is_Interface (Directly_Designated_Type (Target_Type)) |
10793 | then | |
10794 | -- Check the static accessibility rule of 4.6(17). Note that the | |
d81b4bfe TQ |
10795 | -- check is not enforced when within an instance body, since the |
10796 | -- RM requires such cases to be caught at run time. | |
758c442c | 10797 | |
4172a8e3 AC |
10798 | -- If the operand is a rewriting of an allocator no check is needed |
10799 | -- because there are no accessibility issues. | |
10800 | ||
10801 | if Nkind (Original_Node (N)) = N_Allocator then | |
10802 | null; | |
10803 | ||
10804 | elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then | |
758c442c | 10805 | if Type_Access_Level (Opnd_Type) > |
996c8821 | 10806 | Deepest_Type_Access_Level (Target_Type) |
758c442c GD |
10807 | then |
10808 | -- In an instance, this is a run-time check, but one we know | |
10809 | -- will fail, so generate an appropriate warning. The raise | |
10810 | -- will be generated by Expand_N_Type_Conversion. | |
10811 | ||
10812 | if In_Instance_Body then | |
10813 | Error_Msg_N | |
10814 | ("?cannot convert local pointer to non-local access type", | |
10815 | Operand); | |
10816 | Error_Msg_N | |
c8ef728f | 10817 | ("\?Program_Error will be raised at run time", Operand); |
996c8821 | 10818 | |
758c442c GD |
10819 | else |
10820 | Error_Msg_N | |
10821 | ("cannot convert local pointer to non-local access type", | |
10822 | Operand); | |
10823 | return False; | |
10824 | end if; | |
10825 | ||
10826 | -- Special accessibility checks are needed in the case of access | |
10827 | -- discriminants declared for a limited type. | |
10828 | ||
10829 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
10830 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
10831 | then | |
10832 | -- When the operand is a selected access discriminant the check | |
10833 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
10834 | -- the prefix of the selected name (Object_Access_Level handles |
10835 | -- checking the prefix of the operand for this case). | |
758c442c GD |
10836 | |
10837 | if Nkind (Operand) = N_Selected_Component | |
c8ef728f | 10838 | and then Object_Access_Level (Operand) > |
d15f9422 | 10839 | Deepest_Type_Access_Level (Target_Type) |
758c442c | 10840 | then |
d81b4bfe TQ |
10841 | -- In an instance, this is a run-time check, but one we know |
10842 | -- will fail, so generate an appropriate warning. The raise | |
10843 | -- will be generated by Expand_N_Type_Conversion. | |
758c442c GD |
10844 | |
10845 | if In_Instance_Body then | |
10846 | Error_Msg_N | |
10847 | ("?cannot convert access discriminant to non-local" & | |
10848 | " access type", Operand); | |
10849 | Error_Msg_N | |
c8ef728f | 10850 | ("\?Program_Error will be raised at run time", Operand); |
758c442c GD |
10851 | else |
10852 | Error_Msg_N | |
10853 | ("cannot convert access discriminant to non-local" & | |
10854 | " access type", Operand); | |
10855 | return False; | |
10856 | end if; | |
10857 | end if; | |
10858 | ||
10859 | -- The case of a reference to an access discriminant from | |
10860 | -- within a limited type declaration (which will appear as | |
10861 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 10862 | -- discriminant is considered to be deeper than any (nameable) |
758c442c GD |
10863 | -- access type. |
10864 | ||
10865 | if Is_Entity_Name (Operand) | |
10866 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
964f13da RD |
10867 | and then |
10868 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
758c442c GD |
10869 | and then Present (Discriminal_Link (Entity (Operand))) |
10870 | then | |
10871 | Error_Msg_N | |
10872 | ("discriminant has deeper accessibility level than target", | |
10873 | Operand); | |
10874 | return False; | |
10875 | end if; | |
10876 | end if; | |
10877 | end if; | |
10878 | ||
10879 | return True; | |
10880 | ||
aa180613 RD |
10881 | -- General and anonymous access types |
10882 | ||
964f13da RD |
10883 | elsif Ekind_In (Target_Type, E_General_Access_Type, |
10884 | E_Anonymous_Access_Type) | |
996ae0b0 RK |
10885 | and then |
10886 | Conversion_Check | |
10887 | (Is_Access_Type (Opnd_Type) | |
964f13da RD |
10888 | and then not |
10889 | Ekind_In (Opnd_Type, E_Access_Subprogram_Type, | |
10890 | E_Access_Protected_Subprogram_Type), | |
996ae0b0 RK |
10891 | "must be an access-to-object type") |
10892 | then | |
10893 | if Is_Access_Constant (Opnd_Type) | |
10894 | and then not Is_Access_Constant (Target_Type) | |
10895 | then | |
10896 | Error_Msg_N | |
10897 | ("access-to-constant operand type not allowed", Operand); | |
10898 | return False; | |
10899 | end if; | |
10900 | ||
758c442c GD |
10901 | -- Check the static accessibility rule of 4.6(17). Note that the |
10902 | -- check is not enforced when within an instance body, since the RM | |
10903 | -- requires such cases to be caught at run time. | |
996ae0b0 | 10904 | |
758c442c GD |
10905 | if Ekind (Target_Type) /= E_Anonymous_Access_Type |
10906 | or else Is_Local_Anonymous_Access (Target_Type) | |
d15f9422 | 10907 | or else Nkind (Associated_Node_For_Itype (Target_Type)) = |
996c8821 | 10908 | N_Object_Declaration |
758c442c | 10909 | then |
6cce2156 GD |
10910 | -- Ada 2012 (AI05-0149): Perform legality checking on implicit |
10911 | -- conversions from an anonymous access type to a named general | |
10912 | -- access type. Such conversions are not allowed in the case of | |
10913 | -- access parameters and stand-alone objects of an anonymous | |
c199ccf7 AC |
10914 | -- access type. The implicit conversion case is recognized by |
10915 | -- testing that Comes_From_Source is False and that it's been | |
10916 | -- rewritten. The Comes_From_Source test isn't sufficient because | |
10917 | -- nodes in inlined calls to predefined library routines can have | |
10918 | -- Comes_From_Source set to False. (Is there a better way to test | |
10919 | -- for implicit conversions???) | |
6cce2156 GD |
10920 | |
10921 | if Ada_Version >= Ada_2012 | |
10922 | and then not Comes_From_Source (N) | |
c199ccf7 | 10923 | and then N /= Original_Node (N) |
6cce2156 GD |
10924 | and then Ekind (Target_Type) = E_General_Access_Type |
10925 | and then Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
996ae0b0 | 10926 | then |
6cce2156 GD |
10927 | if Is_Itype (Opnd_Type) then |
10928 | ||
10929 | -- Implicit conversions aren't allowed for objects of an | |
10930 | -- anonymous access type, since such objects have nonstatic | |
10931 | -- levels in Ada 2012. | |
10932 | ||
10933 | if Nkind (Associated_Node_For_Itype (Opnd_Type)) = | |
10934 | N_Object_Declaration | |
10935 | then | |
10936 | Error_Msg_N | |
10937 | ("implicit conversion of stand-alone anonymous " & | |
10938 | "access object not allowed", Operand); | |
10939 | return False; | |
10940 | ||
10941 | -- Implicit conversions aren't allowed for anonymous access | |
10942 | -- parameters. The "not Is_Local_Anonymous_Access_Type" test | |
10943 | -- is done to exclude anonymous access results. | |
10944 | ||
10945 | elsif not Is_Local_Anonymous_Access (Opnd_Type) | |
10946 | and then Nkind_In (Associated_Node_For_Itype (Opnd_Type), | |
10947 | N_Function_Specification, | |
10948 | N_Procedure_Specification) | |
10949 | then | |
10950 | Error_Msg_N | |
10951 | ("implicit conversion of anonymous access formal " & | |
10952 | "not allowed", Operand); | |
10953 | return False; | |
10954 | ||
10955 | -- This is a case where there's an enclosing object whose | |
10956 | -- to which the "statically deeper than" relationship does | |
10957 | -- not apply (such as an access discriminant selected from | |
10958 | -- a dereference of an access parameter). | |
10959 | ||
10960 | elsif Object_Access_Level (Operand) | |
10961 | = Scope_Depth (Standard_Standard) | |
10962 | then | |
10963 | Error_Msg_N | |
10964 | ("implicit conversion of anonymous access value " & | |
10965 | "not allowed", Operand); | |
10966 | return False; | |
10967 | ||
10968 | -- In other cases, the level of the operand's type must be | |
10969 | -- statically less deep than that of the target type, else | |
10970 | -- implicit conversion is disallowed (by RM12-8.6(27.1/3)). | |
10971 | ||
d15f9422 | 10972 | elsif Type_Access_Level (Opnd_Type) > |
996c8821 | 10973 | Deepest_Type_Access_Level (Target_Type) |
6cce2156 GD |
10974 | then |
10975 | Error_Msg_N | |
10976 | ("implicit conversion of anonymous access value " & | |
10977 | "violates accessibility", Operand); | |
10978 | return False; | |
10979 | end if; | |
10980 | end if; | |
10981 | ||
d15f9422 | 10982 | elsif Type_Access_Level (Opnd_Type) > |
996c8821 | 10983 | Deepest_Type_Access_Level (Target_Type) |
6cce2156 | 10984 | then |
d81b4bfe TQ |
10985 | -- In an instance, this is a run-time check, but one we know |
10986 | -- will fail, so generate an appropriate warning. The raise | |
10987 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
10988 | |
10989 | if In_Instance_Body then | |
10990 | Error_Msg_N | |
10991 | ("?cannot convert local pointer to non-local access type", | |
10992 | Operand); | |
10993 | Error_Msg_N | |
c8ef728f | 10994 | ("\?Program_Error will be raised at run time", Operand); |
996ae0b0 RK |
10995 | |
10996 | else | |
b90cfacd HK |
10997 | -- Avoid generation of spurious error message |
10998 | ||
10999 | if not Error_Posted (N) then | |
11000 | Error_Msg_N | |
11001 | ("cannot convert local pointer to non-local access type", | |
11002 | Operand); | |
11003 | end if; | |
11004 | ||
996ae0b0 RK |
11005 | return False; |
11006 | end if; | |
11007 | ||
758c442c GD |
11008 | -- Special accessibility checks are needed in the case of access |
11009 | -- discriminants declared for a limited type. | |
11010 | ||
11011 | elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type | |
11012 | and then not Is_Local_Anonymous_Access (Opnd_Type) | |
11013 | then | |
758c442c GD |
11014 | -- When the operand is a selected access discriminant the check |
11015 | -- needs to be made against the level of the object denoted by | |
d81b4bfe TQ |
11016 | -- the prefix of the selected name (Object_Access_Level handles |
11017 | -- checking the prefix of the operand for this case). | |
996ae0b0 RK |
11018 | |
11019 | if Nkind (Operand) = N_Selected_Component | |
45fc7ddb | 11020 | and then Object_Access_Level (Operand) > |
996c8821 | 11021 | Deepest_Type_Access_Level (Target_Type) |
996ae0b0 | 11022 | then |
d81b4bfe TQ |
11023 | -- In an instance, this is a run-time check, but one we know |
11024 | -- will fail, so generate an appropriate warning. The raise | |
11025 | -- will be generated by Expand_N_Type_Conversion. | |
996ae0b0 RK |
11026 | |
11027 | if In_Instance_Body then | |
11028 | Error_Msg_N | |
11029 | ("?cannot convert access discriminant to non-local" & | |
11030 | " access type", Operand); | |
11031 | Error_Msg_N | |
c8ef728f ES |
11032 | ("\?Program_Error will be raised at run time", |
11033 | Operand); | |
996ae0b0 RK |
11034 | |
11035 | else | |
11036 | Error_Msg_N | |
11037 | ("cannot convert access discriminant to non-local" & | |
11038 | " access type", Operand); | |
11039 | return False; | |
11040 | end if; | |
11041 | end if; | |
11042 | ||
758c442c GD |
11043 | -- The case of a reference to an access discriminant from |
11044 | -- within a limited type declaration (which will appear as | |
11045 | -- a discriminal) is always illegal because the level of the | |
f3d57416 | 11046 | -- discriminant is considered to be deeper than any (nameable) |
758c442c | 11047 | -- access type. |
996ae0b0 RK |
11048 | |
11049 | if Is_Entity_Name (Operand) | |
964f13da RD |
11050 | and then |
11051 | Ekind_In (Entity (Operand), E_In_Parameter, E_Constant) | |
996ae0b0 RK |
11052 | and then Present (Discriminal_Link (Entity (Operand))) |
11053 | then | |
11054 | Error_Msg_N | |
11055 | ("discriminant has deeper accessibility level than target", | |
11056 | Operand); | |
11057 | return False; | |
11058 | end if; | |
11059 | end if; | |
11060 | end if; | |
11061 | ||
14e33999 AC |
11062 | -- In the presence of limited_with clauses we have to use non-limited |
11063 | -- views, if available. | |
d81b4bfe | 11064 | |
14e33999 | 11065 | Check_Limited : declare |
0669bebe GB |
11066 | function Full_Designated_Type (T : Entity_Id) return Entity_Id; |
11067 | -- Helper function to handle limited views | |
11068 | ||
11069 | -------------------------- | |
11070 | -- Full_Designated_Type -- | |
11071 | -------------------------- | |
11072 | ||
11073 | function Full_Designated_Type (T : Entity_Id) return Entity_Id is | |
950d217a | 11074 | Desig : constant Entity_Id := Designated_Type (T); |
c0985d4e | 11075 | |
0669bebe | 11076 | begin |
950d217a AC |
11077 | -- Handle the limited view of a type |
11078 | ||
c0985d4e HK |
11079 | if Is_Incomplete_Type (Desig) |
11080 | and then From_With_Type (Desig) | |
0669bebe GB |
11081 | and then Present (Non_Limited_View (Desig)) |
11082 | then | |
950d217a AC |
11083 | return Available_View (Desig); |
11084 | else | |
11085 | return Desig; | |
0669bebe GB |
11086 | end if; |
11087 | end Full_Designated_Type; | |
11088 | ||
d81b4bfe TQ |
11089 | -- Local Declarations |
11090 | ||
0669bebe GB |
11091 | Target : constant Entity_Id := Full_Designated_Type (Target_Type); |
11092 | Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type); | |
11093 | ||
11094 | Same_Base : constant Boolean := | |
11095 | Base_Type (Target) = Base_Type (Opnd); | |
996ae0b0 | 11096 | |
14e33999 | 11097 | -- Start of processing for Check_Limited |
d81b4bfe | 11098 | |
996ae0b0 RK |
11099 | begin |
11100 | if Is_Tagged_Type (Target) then | |
11101 | return Valid_Tagged_Conversion (Target, Opnd); | |
11102 | ||
11103 | else | |
0669bebe | 11104 | if not Same_Base then |
996ae0b0 RK |
11105 | Error_Msg_NE |
11106 | ("target designated type not compatible with }", | |
11107 | N, Base_Type (Opnd)); | |
11108 | return False; | |
11109 | ||
da709d08 AC |
11110 | -- Ada 2005 AI-384: legality rule is symmetric in both |
11111 | -- designated types. The conversion is legal (with possible | |
11112 | -- constraint check) if either designated type is | |
11113 | -- unconstrained. | |
11114 | ||
11115 | elsif Subtypes_Statically_Match (Target, Opnd) | |
11116 | or else | |
11117 | (Has_Discriminants (Target) | |
11118 | and then | |
11119 | (not Is_Constrained (Opnd) | |
11120 | or else not Is_Constrained (Target))) | |
996ae0b0 | 11121 | then |
9fa33291 RD |
11122 | -- Special case, if Value_Size has been used to make the |
11123 | -- sizes different, the conversion is not allowed even | |
11124 | -- though the subtypes statically match. | |
11125 | ||
11126 | if Known_Static_RM_Size (Target) | |
11127 | and then Known_Static_RM_Size (Opnd) | |
11128 | and then RM_Size (Target) /= RM_Size (Opnd) | |
11129 | then | |
11130 | Error_Msg_NE | |
11131 | ("target designated subtype not compatible with }", | |
11132 | N, Opnd); | |
11133 | Error_Msg_NE | |
11134 | ("\because sizes of the two designated subtypes differ", | |
11135 | N, Opnd); | |
11136 | return False; | |
11137 | ||
11138 | -- Normal case where conversion is allowed | |
11139 | ||
11140 | else | |
11141 | return True; | |
11142 | end if; | |
da709d08 AC |
11143 | |
11144 | else | |
996ae0b0 RK |
11145 | Error_Msg_NE |
11146 | ("target designated subtype not compatible with }", | |
11147 | N, Opnd); | |
11148 | return False; | |
996ae0b0 RK |
11149 | end if; |
11150 | end if; | |
14e33999 | 11151 | end Check_Limited; |
996ae0b0 | 11152 | |
cdbf04c0 | 11153 | -- Access to subprogram types. If the operand is an access parameter, |
4adf3c50 AC |
11154 | -- the type has a deeper accessibility that any master, and cannot be |
11155 | -- assigned. We must make an exception if the conversion is part of an | |
11156 | -- assignment and the target is the return object of an extended return | |
11157 | -- statement, because in that case the accessibility check takes place | |
11158 | -- after the return. | |
aa180613 | 11159 | |
dce86910 | 11160 | elsif Is_Access_Subprogram_Type (Target_Type) |
bc5f3720 | 11161 | and then No (Corresponding_Remote_Type (Opnd_Type)) |
996ae0b0 | 11162 | then |
cdbf04c0 AC |
11163 | if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type |
11164 | and then Is_Entity_Name (Operand) | |
11165 | and then Ekind (Entity (Operand)) = E_In_Parameter | |
53cf4600 ES |
11166 | and then |
11167 | (Nkind (Parent (N)) /= N_Assignment_Statement | |
11168 | or else not Is_Entity_Name (Name (Parent (N))) | |
11169 | or else not Is_Return_Object (Entity (Name (Parent (N))))) | |
0669bebe GB |
11170 | then |
11171 | Error_Msg_N | |
11172 | ("illegal attempt to store anonymous access to subprogram", | |
11173 | Operand); | |
11174 | Error_Msg_N | |
11175 | ("\value has deeper accessibility than any master " & | |
aa5147f0 | 11176 | "(RM 3.10.2 (13))", |
0669bebe GB |
11177 | Operand); |
11178 | ||
c147ac26 ES |
11179 | Error_Msg_NE |
11180 | ("\use named access type for& instead of access parameter", | |
11181 | Operand, Entity (Operand)); | |
0669bebe GB |
11182 | end if; |
11183 | ||
996ae0b0 RK |
11184 | -- Check that the designated types are subtype conformant |
11185 | ||
bc5f3720 RD |
11186 | Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type), |
11187 | Old_Id => Designated_Type (Opnd_Type), | |
11188 | Err_Loc => N); | |
996ae0b0 RK |
11189 | |
11190 | -- Check the static accessibility rule of 4.6(20) | |
11191 | ||
11192 | if Type_Access_Level (Opnd_Type) > | |
996c8821 | 11193 | Deepest_Type_Access_Level (Target_Type) |
996ae0b0 RK |
11194 | then |
11195 | Error_Msg_N | |
11196 | ("operand type has deeper accessibility level than target", | |
11197 | Operand); | |
11198 | ||
11199 | -- Check that if the operand type is declared in a generic body, | |
11200 | -- then the target type must be declared within that same body | |
11201 | -- (enforces last sentence of 4.6(20)). | |
11202 | ||
11203 | elsif Present (Enclosing_Generic_Body (Opnd_Type)) then | |
11204 | declare | |
11205 | O_Gen : constant Node_Id := | |
11206 | Enclosing_Generic_Body (Opnd_Type); | |
11207 | ||
1420b484 | 11208 | T_Gen : Node_Id; |
996ae0b0 RK |
11209 | |
11210 | begin | |
1420b484 | 11211 | T_Gen := Enclosing_Generic_Body (Target_Type); |
996ae0b0 RK |
11212 | while Present (T_Gen) and then T_Gen /= O_Gen loop |
11213 | T_Gen := Enclosing_Generic_Body (T_Gen); | |
11214 | end loop; | |
11215 | ||
11216 | if T_Gen /= O_Gen then | |
11217 | Error_Msg_N | |
11218 | ("target type must be declared in same generic body" | |
11219 | & " as operand type", N); | |
11220 | end if; | |
11221 | end; | |
11222 | end if; | |
11223 | ||
11224 | return True; | |
11225 | ||
aa180613 RD |
11226 | -- Remote subprogram access types |
11227 | ||
996ae0b0 RK |
11228 | elsif Is_Remote_Access_To_Subprogram_Type (Target_Type) |
11229 | and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type) | |
11230 | then | |
11231 | -- It is valid to convert from one RAS type to another provided | |
11232 | -- that their specification statically match. | |
11233 | ||
11234 | Check_Subtype_Conformant | |
11235 | (New_Id => | |
11236 | Designated_Type (Corresponding_Remote_Type (Target_Type)), | |
11237 | Old_Id => | |
11238 | Designated_Type (Corresponding_Remote_Type (Opnd_Type)), | |
11239 | Err_Loc => | |
11240 | N); | |
11241 | return True; | |
aa180613 | 11242 | |
be482a8c AC |
11243 | -- If it was legal in the generic, it's legal in the instance |
11244 | ||
11245 | elsif In_Instance_Body then | |
11246 | return True; | |
11247 | ||
e65f50ec | 11248 | -- If both are tagged types, check legality of view conversions |
996ae0b0 | 11249 | |
e65f50ec | 11250 | elsif Is_Tagged_Type (Target_Type) |
4adf3c50 AC |
11251 | and then |
11252 | Is_Tagged_Type (Opnd_Type) | |
e65f50ec | 11253 | then |
996ae0b0 RK |
11254 | return Valid_Tagged_Conversion (Target_Type, Opnd_Type); |
11255 | ||
a77842bd | 11256 | -- Types derived from the same root type are convertible |
996ae0b0 RK |
11257 | |
11258 | elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then | |
11259 | return True; | |
11260 | ||
4adf3c50 AC |
11261 | -- In an instance or an inlined body, there may be inconsistent views of |
11262 | -- the same type, or of types derived from a common root. | |
996ae0b0 | 11263 | |
aa5147f0 ES |
11264 | elsif (In_Instance or In_Inlined_Body) |
11265 | and then | |
d81b4bfe TQ |
11266 | Root_Type (Underlying_Type (Target_Type)) = |
11267 | Root_Type (Underlying_Type (Opnd_Type)) | |
996ae0b0 RK |
11268 | then |
11269 | return True; | |
11270 | ||
11271 | -- Special check for common access type error case | |
11272 | ||
11273 | elsif Ekind (Target_Type) = E_Access_Type | |
11274 | and then Is_Access_Type (Opnd_Type) | |
11275 | then | |
11276 | Error_Msg_N ("target type must be general access type!", N); | |
305caf42 AC |
11277 | Error_Msg_NE -- CODEFIX |
11278 | ("add ALL to }!", N, Target_Type); | |
996ae0b0 RK |
11279 | return False; |
11280 | ||
11281 | else | |
11282 | Error_Msg_NE ("invalid conversion, not compatible with }", | |
11283 | N, Opnd_Type); | |
996ae0b0 RK |
11284 | return False; |
11285 | end if; | |
11286 | end Valid_Conversion; | |
11287 | ||
11288 | end Sem_Res; |