]>
Commit | Line | Data |
---|---|---|
fbf5a39b | 1 | ------------------------------------------------------------------------------ |
996ae0b0 RK |
2 | -- -- |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- S E M _ E V A L -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
22cb89b5 | 9 | -- Copyright (C) 1992-2010, Free Software Foundation, Inc. -- |
996ae0b0 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
b5c84c3c | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
996ae0b0 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
b5c84c3c RD |
18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
996ae0b0 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
996ae0b0 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
27 | with Checks; use Checks; | |
28 | with Debug; use Debug; | |
29 | with Einfo; use Einfo; | |
30 | with Elists; use Elists; | |
31 | with Errout; use Errout; | |
32 | with Eval_Fat; use Eval_Fat; | |
8cbb664e | 33 | with Exp_Util; use Exp_Util; |
d7567964 | 34 | with Freeze; use Freeze; |
0356699b | 35 | with Lib; use Lib; |
13f34a3f | 36 | with Namet; use Namet; |
996ae0b0 RK |
37 | with Nmake; use Nmake; |
38 | with Nlists; use Nlists; | |
39 | with Opt; use Opt; | |
40 | with Sem; use Sem; | |
a4100e55 | 41 | with Sem_Aux; use Sem_Aux; |
996ae0b0 | 42 | with Sem_Cat; use Sem_Cat; |
b5bd964f | 43 | with Sem_Ch6; use Sem_Ch6; |
996ae0b0 RK |
44 | with Sem_Ch8; use Sem_Ch8; |
45 | with Sem_Res; use Sem_Res; | |
46 | with Sem_Util; use Sem_Util; | |
47 | with Sem_Type; use Sem_Type; | |
48 | with Sem_Warn; use Sem_Warn; | |
49 | with Sinfo; use Sinfo; | |
50 | with Snames; use Snames; | |
51 | with Stand; use Stand; | |
52 | with Stringt; use Stringt; | |
07fc65c4 | 53 | with Tbuild; use Tbuild; |
996ae0b0 RK |
54 | |
55 | package body Sem_Eval is | |
56 | ||
57 | ----------------------------------------- | |
58 | -- Handling of Compile Time Evaluation -- | |
59 | ----------------------------------------- | |
60 | ||
61 | -- The compile time evaluation of expressions is distributed over several | |
f3d57416 | 62 | -- Eval_xxx procedures. These procedures are called immediately after |
996ae0b0 RK |
63 | -- a subexpression is resolved and is therefore accomplished in a bottom |
64 | -- up fashion. The flags are synthesized using the following approach. | |
65 | ||
66 | -- Is_Static_Expression is determined by following the detailed rules | |
67 | -- in RM 4.9(4-14). This involves testing the Is_Static_Expression | |
68 | -- flag of the operands in many cases. | |
69 | ||
70 | -- Raises_Constraint_Error is set if any of the operands have the flag | |
71 | -- set or if an attempt to compute the value of the current expression | |
72 | -- results in detection of a runtime constraint error. | |
73 | ||
74 | -- As described in the spec, the requirement is that Is_Static_Expression | |
75 | -- be accurately set, and in addition for nodes for which this flag is set, | |
76 | -- Raises_Constraint_Error must also be set. Furthermore a node which has | |
77 | -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the | |
78 | -- requirement is that the expression value must be precomputed, and the | |
79 | -- node is either a literal, or the name of a constant entity whose value | |
80 | -- is a static expression. | |
81 | ||
82 | -- The general approach is as follows. First compute Is_Static_Expression. | |
83 | -- If the node is not static, then the flag is left off in the node and | |
84 | -- we are all done. Otherwise for a static node, we test if any of the | |
85 | -- operands will raise constraint error, and if so, propagate the flag | |
86 | -- Raises_Constraint_Error to the result node and we are done (since the | |
87 | -- error was already posted at a lower level). | |
88 | ||
89 | -- For the case of a static node whose operands do not raise constraint | |
90 | -- error, we attempt to evaluate the node. If this evaluation succeeds, | |
91 | -- then the node is replaced by the result of this computation. If the | |
92 | -- evaluation raises constraint error, then we rewrite the node with | |
93 | -- Apply_Compile_Time_Constraint_Error to raise the exception and also | |
94 | -- to post appropriate error messages. | |
95 | ||
96 | ---------------- | |
97 | -- Local Data -- | |
98 | ---------------- | |
99 | ||
100 | type Bits is array (Nat range <>) of Boolean; | |
101 | -- Used to convert unsigned (modular) values for folding logical ops | |
102 | ||
07fc65c4 GB |
103 | -- The following definitions are used to maintain a cache of nodes that |
104 | -- have compile time known values. The cache is maintained only for | |
105 | -- discrete types (the most common case), and is populated by calls to | |
106 | -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value | |
107 | -- since it is possible for the status to change (in particular it is | |
108 | -- possible for a node to get replaced by a constraint error node). | |
109 | ||
110 | CV_Bits : constant := 5; | |
111 | -- Number of low order bits of Node_Id value used to reference entries | |
112 | -- in the cache table. | |
113 | ||
114 | CV_Cache_Size : constant Nat := 2 ** CV_Bits; | |
115 | -- Size of cache for compile time values | |
116 | ||
117 | subtype CV_Range is Nat range 0 .. CV_Cache_Size; | |
118 | ||
119 | type CV_Entry is record | |
120 | N : Node_Id; | |
121 | V : Uint; | |
122 | end record; | |
123 | ||
124 | type CV_Cache_Array is array (CV_Range) of CV_Entry; | |
125 | ||
126 | CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0)); | |
127 | -- This is the actual cache, with entries consisting of node/value pairs, | |
128 | -- and the impossible value Node_High_Bound used for unset entries. | |
129 | ||
305caf42 AC |
130 | type Range_Membership is (In_Range, Out_Of_Range, Unknown); |
131 | -- Range membership may either be statically known to be in range or out | |
132 | -- of range, or not statically known. Used for Test_In_Range below. | |
133 | ||
996ae0b0 RK |
134 | ----------------------- |
135 | -- Local Subprograms -- | |
136 | ----------------------- | |
137 | ||
996ae0b0 RK |
138 | function From_Bits (B : Bits; T : Entity_Id) return Uint; |
139 | -- Converts a bit string of length B'Length to a Uint value to be used | |
140 | -- for a target of type T, which is a modular type. This procedure | |
141 | -- includes the necessary reduction by the modulus in the case of a | |
142 | -- non-binary modulus (for a binary modulus, the bit string is the | |
143 | -- right length any way so all is well). | |
144 | ||
145 | function Get_String_Val (N : Node_Id) return Node_Id; | |
146 | -- Given a tree node for a folded string or character value, returns | |
147 | -- the corresponding string literal or character literal (one of the | |
148 | -- two must be available, or the operand would not have been marked | |
149 | -- as foldable in the earlier analysis of the operation). | |
150 | ||
07fc65c4 GB |
151 | function OK_Bits (N : Node_Id; Bits : Uint) return Boolean; |
152 | -- Bits represents the number of bits in an integer value to be computed | |
153 | -- (but the value has not been computed yet). If this value in Bits is | |
154 | -- reasonable, a result of True is returned, with the implication that | |
155 | -- the caller should go ahead and complete the calculation. If the value | |
156 | -- in Bits is unreasonably large, then an error is posted on node N, and | |
157 | -- False is returned (and the caller skips the proposed calculation). | |
158 | ||
996ae0b0 RK |
159 | procedure Out_Of_Range (N : Node_Id); |
160 | -- This procedure is called if it is determined that node N, which | |
161 | -- appears in a non-static context, is a compile time known value | |
162 | -- which is outside its range, i.e. the range of Etype. This is used | |
163 | -- in contexts where this is an illegality if N is static, and should | |
164 | -- generate a warning otherwise. | |
165 | ||
166 | procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id); | |
167 | -- N and Exp are nodes representing an expression, Exp is known | |
168 | -- to raise CE. N is rewritten in term of Exp in the optimal way. | |
169 | ||
170 | function String_Type_Len (Stype : Entity_Id) return Uint; | |
171 | -- Given a string type, determines the length of the index type, or, | |
172 | -- if this index type is non-static, the length of the base type of | |
173 | -- this index type. Note that if the string type is itself static, | |
174 | -- then the index type is static, so the second case applies only | |
175 | -- if the string type passed is non-static. | |
176 | ||
177 | function Test (Cond : Boolean) return Uint; | |
178 | pragma Inline (Test); | |
179 | -- This function simply returns the appropriate Boolean'Pos value | |
180 | -- corresponding to the value of Cond as a universal integer. It is | |
181 | -- used for producing the result of the static evaluation of the | |
182 | -- logical operators | |
183 | ||
d7567964 | 184 | function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id; |
602a7ec0 AC |
185 | -- Check whether an arithmetic operation with universal operands which |
186 | -- is a rewritten function call with an explicit scope indication is | |
ae77c68b | 187 | -- ambiguous: P."+" (1, 2) will be ambiguous if there is more than one |
602a7ec0 AC |
188 | -- visible numeric type declared in P and the context does not impose a |
189 | -- type on the result (e.g. in the expression of a type conversion). | |
d7567964 TQ |
190 | -- If ambiguous, emit an error and return Empty, else return the result |
191 | -- type of the operator. | |
602a7ec0 | 192 | |
996ae0b0 RK |
193 | procedure Test_Expression_Is_Foldable |
194 | (N : Node_Id; | |
195 | Op1 : Node_Id; | |
196 | Stat : out Boolean; | |
197 | Fold : out Boolean); | |
198 | -- Tests to see if expression N whose single operand is Op1 is foldable, | |
199 | -- i.e. the operand value is known at compile time. If the operation is | |
200 | -- foldable, then Fold is True on return, and Stat indicates whether | |
201 | -- the result is static (i.e. both operands were static). Note that it | |
202 | -- is quite possible for Fold to be True, and Stat to be False, since | |
203 | -- there are cases in which we know the value of an operand even though | |
204 | -- it is not technically static (e.g. the static lower bound of a range | |
205 | -- whose upper bound is non-static). | |
206 | -- | |
3d5952be | 207 | -- If Stat is set False on return, then Test_Expression_Is_Foldable makes a |
996ae0b0 RK |
208 | -- call to Check_Non_Static_Context on the operand. If Fold is False on |
209 | -- return, then all processing is complete, and the caller should | |
210 | -- return, since there is nothing else to do. | |
93c3fca7 AC |
211 | -- |
212 | -- If Stat is set True on return, then Is_Static_Expression is also set | |
213 | -- true in node N. There are some cases where this is over-enthusiastic, | |
214 | -- e.g. in the two operand case below, for string comaprison, the result | |
215 | -- is not static even though the two operands are static. In such cases, | |
216 | -- the caller must reset the Is_Static_Expression flag in N. | |
996ae0b0 RK |
217 | |
218 | procedure Test_Expression_Is_Foldable | |
219 | (N : Node_Id; | |
220 | Op1 : Node_Id; | |
221 | Op2 : Node_Id; | |
222 | Stat : out Boolean; | |
223 | Fold : out Boolean); | |
224 | -- Same processing, except applies to an expression N with two operands | |
225 | -- Op1 and Op2. | |
226 | ||
305caf42 AC |
227 | function Test_In_Range |
228 | (N : Node_Id; | |
229 | Typ : Entity_Id; | |
230 | Assume_Valid : Boolean; | |
231 | Fixed_Int : Boolean; | |
232 | Int_Real : Boolean) return Range_Membership; | |
233 | -- Common processing for Is_In_Range and Is_Out_Of_Range: | |
234 | -- Returns In_Range or Out_Of_Range if it can be guaranteed at compile time | |
235 | -- that expression N is known to be in or out of range of the subtype Typ. | |
236 | -- If not compile time known, Unknown is returned. | |
237 | -- See documentation of Is_In_Range for complete description of parameters. | |
238 | ||
996ae0b0 RK |
239 | procedure To_Bits (U : Uint; B : out Bits); |
240 | -- Converts a Uint value to a bit string of length B'Length | |
241 | ||
242 | ------------------------------ | |
243 | -- Check_Non_Static_Context -- | |
244 | ------------------------------ | |
245 | ||
246 | procedure Check_Non_Static_Context (N : Node_Id) is | |
fbf5a39b AC |
247 | T : constant Entity_Id := Etype (N); |
248 | Checks_On : constant Boolean := | |
996ae0b0 RK |
249 | not Index_Checks_Suppressed (T) |
250 | and not Range_Checks_Suppressed (T); | |
251 | ||
252 | begin | |
fbf5a39b | 253 | -- Ignore cases of non-scalar types or error types |
996ae0b0 | 254 | |
fbf5a39b | 255 | if T = Any_Type or else not Is_Scalar_Type (T) then |
996ae0b0 | 256 | return; |
fbf5a39b | 257 | end if; |
996ae0b0 | 258 | |
fbf5a39b AC |
259 | -- At this stage we have a scalar type. If we have an expression |
260 | -- that raises CE, then we already issued a warning or error msg | |
261 | -- so there is nothing more to be done in this routine. | |
262 | ||
263 | if Raises_Constraint_Error (N) then | |
264 | return; | |
265 | end if; | |
266 | ||
267 | -- Now we have a scalar type which is not marked as raising a | |
268 | -- constraint error exception. The main purpose of this routine | |
269 | -- is to deal with static expressions appearing in a non-static | |
270 | -- context. That means that if we do not have a static expression | |
271 | -- then there is not much to do. The one case that we deal with | |
272 | -- here is that if we have a floating-point value that is out of | |
273 | -- range, then we post a warning that an infinity will result. | |
274 | ||
275 | if not Is_Static_Expression (N) then | |
276 | if Is_Floating_Point_Type (T) | |
c800f862 | 277 | and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) |
fbf5a39b AC |
278 | then |
279 | Error_Msg_N | |
280 | ("?float value out of range, infinity will be generated", N); | |
281 | end if; | |
996ae0b0 | 282 | |
996ae0b0 RK |
283 | return; |
284 | end if; | |
285 | ||
286 | -- Here we have the case of outer level static expression of | |
287 | -- scalar type, where the processing of this procedure is needed. | |
288 | ||
289 | -- For real types, this is where we convert the value to a machine | |
290 | -- number (see RM 4.9(38)). Also see ACVC test C490001. We should | |
291 | -- only need to do this if the parent is a constant declaration, | |
292 | -- since in other cases, gigi should do the necessary conversion | |
293 | -- correctly, but experimentation shows that this is not the case | |
294 | -- on all machines, in particular if we do not convert all literals | |
295 | -- to machine values in non-static contexts, then ACVC test C490001 | |
296 | -- fails on Sparc/Solaris and SGI/Irix. | |
297 | ||
298 | if Nkind (N) = N_Real_Literal | |
299 | and then not Is_Machine_Number (N) | |
300 | and then not Is_Generic_Type (Etype (N)) | |
301 | and then Etype (N) /= Universal_Real | |
996ae0b0 RK |
302 | then |
303 | -- Check that value is in bounds before converting to machine | |
304 | -- number, so as not to lose case where value overflows in the | |
305 | -- least significant bit or less. See B490001. | |
306 | ||
c800f862 | 307 | if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then |
996ae0b0 RK |
308 | Out_Of_Range (N); |
309 | return; | |
310 | end if; | |
311 | ||
312 | -- Note: we have to copy the node, to avoid problems with conformance | |
313 | -- of very similar numbers (see ACVC tests B4A010C and B63103A). | |
314 | ||
315 | Rewrite (N, New_Copy (N)); | |
316 | ||
317 | if not Is_Floating_Point_Type (T) then | |
318 | Set_Realval | |
319 | (N, Corresponding_Integer_Value (N) * Small_Value (T)); | |
320 | ||
321 | elsif not UR_Is_Zero (Realval (N)) then | |
996ae0b0 | 322 | |
fbf5a39b AC |
323 | -- Note: even though RM 4.9(38) specifies biased rounding, |
324 | -- this has been modified by AI-100 in order to prevent | |
325 | -- confusing differences in rounding between static and | |
326 | -- non-static expressions. AI-100 specifies that the effect | |
327 | -- of such rounding is implementation dependent, and in GNAT | |
328 | -- we round to nearest even to match the run-time behavior. | |
996ae0b0 | 329 | |
fbf5a39b AC |
330 | Set_Realval |
331 | (N, Machine (Base_Type (T), Realval (N), Round_Even, N)); | |
996ae0b0 RK |
332 | end if; |
333 | ||
334 | Set_Is_Machine_Number (N); | |
335 | end if; | |
336 | ||
337 | -- Check for out of range universal integer. This is a non-static | |
338 | -- context, so the integer value must be in range of the runtime | |
339 | -- representation of universal integers. | |
340 | ||
341 | -- We do this only within an expression, because that is the only | |
342 | -- case in which non-static universal integer values can occur, and | |
343 | -- furthermore, Check_Non_Static_Context is currently (incorrectly???) | |
344 | -- called in contexts like the expression of a number declaration where | |
345 | -- we certainly want to allow out of range values. | |
346 | ||
347 | if Etype (N) = Universal_Integer | |
348 | and then Nkind (N) = N_Integer_Literal | |
349 | and then Nkind (Parent (N)) in N_Subexpr | |
350 | and then | |
351 | (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer)) | |
352 | or else | |
353 | Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer))) | |
354 | then | |
355 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 GB |
356 | (N, "non-static universal integer value out of range?", |
357 | CE_Range_Check_Failed); | |
996ae0b0 RK |
358 | |
359 | -- Check out of range of base type | |
360 | ||
c800f862 | 361 | elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then |
996ae0b0 RK |
362 | Out_Of_Range (N); |
363 | ||
c800f862 RD |
364 | -- Give warning if outside subtype (where one or both of the bounds of |
365 | -- the subtype is static). This warning is omitted if the expression | |
366 | -- appears in a range that could be null (warnings are handled elsewhere | |
367 | -- for this case). | |
996ae0b0 RK |
368 | |
369 | elsif T /= Base_Type (T) | |
370 | and then Nkind (Parent (N)) /= N_Range | |
371 | then | |
c800f862 | 372 | if Is_In_Range (N, T, Assume_Valid => True) then |
996ae0b0 RK |
373 | null; |
374 | ||
c800f862 | 375 | elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then |
996ae0b0 | 376 | Apply_Compile_Time_Constraint_Error |
07fc65c4 | 377 | (N, "value not in range of}?", CE_Range_Check_Failed); |
996ae0b0 RK |
378 | |
379 | elsif Checks_On then | |
380 | Enable_Range_Check (N); | |
381 | ||
382 | else | |
383 | Set_Do_Range_Check (N, False); | |
384 | end if; | |
385 | end if; | |
386 | end Check_Non_Static_Context; | |
387 | ||
388 | --------------------------------- | |
389 | -- Check_String_Literal_Length -- | |
390 | --------------------------------- | |
391 | ||
392 | procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is | |
393 | begin | |
394 | if not Raises_Constraint_Error (N) | |
395 | and then Is_Constrained (Ttype) | |
396 | then | |
397 | if | |
398 | UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype) | |
399 | then | |
400 | Apply_Compile_Time_Constraint_Error | |
401 | (N, "string length wrong for}?", | |
07fc65c4 | 402 | CE_Length_Check_Failed, |
996ae0b0 RK |
403 | Ent => Ttype, |
404 | Typ => Ttype); | |
405 | end if; | |
406 | end if; | |
407 | end Check_String_Literal_Length; | |
408 | ||
409 | -------------------------- | |
410 | -- Compile_Time_Compare -- | |
411 | -------------------------- | |
412 | ||
fbf5a39b | 413 | function Compile_Time_Compare |
1c7717c3 | 414 | (L, R : Node_Id; |
af02a866 RD |
415 | Assume_Valid : Boolean) return Compare_Result |
416 | is | |
417 | Discard : aliased Uint; | |
418 | begin | |
419 | return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid); | |
420 | end Compile_Time_Compare; | |
421 | ||
422 | function Compile_Time_Compare | |
423 | (L, R : Node_Id; | |
424 | Diff : access Uint; | |
1c7717c3 AC |
425 | Assume_Valid : Boolean; |
426 | Rec : Boolean := False) return Compare_Result | |
fbf5a39b | 427 | is |
93c3fca7 AC |
428 | Ltyp : Entity_Id := Underlying_Type (Etype (L)); |
429 | Rtyp : Entity_Id := Underlying_Type (Etype (R)); | |
1c7717c3 AC |
430 | -- These get reset to the base type for the case of entities where |
431 | -- Is_Known_Valid is not set. This takes care of handling possible | |
432 | -- invalid representations using the value of the base type, in | |
433 | -- accordance with RM 13.9.1(10). | |
996ae0b0 | 434 | |
af02a866 RD |
435 | Discard : aliased Uint; |
436 | ||
996ae0b0 RK |
437 | procedure Compare_Decompose |
438 | (N : Node_Id; | |
439 | R : out Node_Id; | |
440 | V : out Uint); | |
b49365b2 RD |
441 | -- This procedure decomposes the node N into an expression node and a |
442 | -- signed offset, so that the value of N is equal to the value of R plus | |
443 | -- the value V (which may be negative). If no such decomposition is | |
444 | -- possible, then on return R is a copy of N, and V is set to zero. | |
996ae0b0 RK |
445 | |
446 | function Compare_Fixup (N : Node_Id) return Node_Id; | |
b49365b2 RD |
447 | -- This function deals with replacing 'Last and 'First references with |
448 | -- their corresponding type bounds, which we then can compare. The | |
449 | -- argument is the original node, the result is the identity, unless we | |
450 | -- have a 'Last/'First reference in which case the value returned is the | |
451 | -- appropriate type bound. | |
996ae0b0 | 452 | |
57036dcc ES |
453 | function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean; |
454 | -- Even if the context does not assume that values are valid, some | |
455 | -- simple cases can be recognized. | |
456 | ||
996ae0b0 RK |
457 | function Is_Same_Value (L, R : Node_Id) return Boolean; |
458 | -- Returns True iff L and R represent expressions that definitely | |
459 | -- have identical (but not necessarily compile time known) values | |
460 | -- Indeed the caller is expected to have already dealt with the | |
461 | -- cases of compile time known values, so these are not tested here. | |
462 | ||
463 | ----------------------- | |
464 | -- Compare_Decompose -- | |
465 | ----------------------- | |
466 | ||
467 | procedure Compare_Decompose | |
468 | (N : Node_Id; | |
469 | R : out Node_Id; | |
470 | V : out Uint) | |
471 | is | |
472 | begin | |
473 | if Nkind (N) = N_Op_Add | |
474 | and then Nkind (Right_Opnd (N)) = N_Integer_Literal | |
475 | then | |
476 | R := Left_Opnd (N); | |
477 | V := Intval (Right_Opnd (N)); | |
478 | return; | |
479 | ||
480 | elsif Nkind (N) = N_Op_Subtract | |
481 | and then Nkind (Right_Opnd (N)) = N_Integer_Literal | |
482 | then | |
483 | R := Left_Opnd (N); | |
484 | V := UI_Negate (Intval (Right_Opnd (N))); | |
485 | return; | |
486 | ||
487 | elsif Nkind (N) = N_Attribute_Reference then | |
996ae0b0 RK |
488 | if Attribute_Name (N) = Name_Succ then |
489 | R := First (Expressions (N)); | |
490 | V := Uint_1; | |
491 | return; | |
492 | ||
493 | elsif Attribute_Name (N) = Name_Pred then | |
494 | R := First (Expressions (N)); | |
495 | V := Uint_Minus_1; | |
496 | return; | |
497 | end if; | |
498 | end if; | |
499 | ||
500 | R := N; | |
501 | V := Uint_0; | |
502 | end Compare_Decompose; | |
503 | ||
504 | ------------------- | |
505 | -- Compare_Fixup -- | |
506 | ------------------- | |
507 | ||
508 | function Compare_Fixup (N : Node_Id) return Node_Id is | |
509 | Indx : Node_Id; | |
510 | Xtyp : Entity_Id; | |
511 | Subs : Nat; | |
512 | ||
513 | begin | |
514 | if Nkind (N) = N_Attribute_Reference | |
515 | and then (Attribute_Name (N) = Name_First | |
516 | or else | |
517 | Attribute_Name (N) = Name_Last) | |
518 | then | |
519 | Xtyp := Etype (Prefix (N)); | |
520 | ||
521 | -- If we have no type, then just abandon the attempt to do | |
522 | -- a fixup, this is probably the result of some other error. | |
523 | ||
524 | if No (Xtyp) then | |
525 | return N; | |
526 | end if; | |
527 | ||
528 | -- Dereference an access type | |
529 | ||
530 | if Is_Access_Type (Xtyp) then | |
531 | Xtyp := Designated_Type (Xtyp); | |
532 | end if; | |
533 | ||
534 | -- If we don't have an array type at this stage, something | |
535 | -- is peculiar, e.g. another error, and we abandon the attempt | |
536 | -- at a fixup. | |
537 | ||
538 | if not Is_Array_Type (Xtyp) then | |
539 | return N; | |
540 | end if; | |
541 | ||
542 | -- Ignore unconstrained array, since bounds are not meaningful | |
543 | ||
544 | if not Is_Constrained (Xtyp) then | |
545 | return N; | |
546 | end if; | |
547 | ||
c3de5c4c ES |
548 | if Ekind (Xtyp) = E_String_Literal_Subtype then |
549 | if Attribute_Name (N) = Name_First then | |
550 | return String_Literal_Low_Bound (Xtyp); | |
551 | ||
552 | else -- Attribute_Name (N) = Name_Last | |
553 | return Make_Integer_Literal (Sloc (N), | |
554 | Intval => Intval (String_Literal_Low_Bound (Xtyp)) | |
57036dcc | 555 | + String_Literal_Length (Xtyp)); |
c3de5c4c ES |
556 | end if; |
557 | end if; | |
558 | ||
996ae0b0 RK |
559 | -- Find correct index type |
560 | ||
561 | Indx := First_Index (Xtyp); | |
562 | ||
563 | if Present (Expressions (N)) then | |
564 | Subs := UI_To_Int (Expr_Value (First (Expressions (N)))); | |
565 | ||
566 | for J in 2 .. Subs loop | |
567 | Indx := Next_Index (Indx); | |
568 | end loop; | |
569 | end if; | |
570 | ||
571 | Xtyp := Etype (Indx); | |
572 | ||
573 | if Attribute_Name (N) = Name_First then | |
574 | return Type_Low_Bound (Xtyp); | |
575 | ||
576 | else -- Attribute_Name (N) = Name_Last | |
577 | return Type_High_Bound (Xtyp); | |
578 | end if; | |
579 | end if; | |
580 | ||
581 | return N; | |
582 | end Compare_Fixup; | |
583 | ||
57036dcc ES |
584 | ---------------------------- |
585 | -- Is_Known_Valid_Operand -- | |
586 | ---------------------------- | |
587 | ||
588 | function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is | |
589 | begin | |
590 | return (Is_Entity_Name (Opnd) | |
591 | and then | |
592 | (Is_Known_Valid (Entity (Opnd)) | |
593 | or else Ekind (Entity (Opnd)) = E_In_Parameter | |
594 | or else | |
595 | (Ekind (Entity (Opnd)) in Object_Kind | |
596 | and then Present (Current_Value (Entity (Opnd)))))) | |
597 | or else Is_OK_Static_Expression (Opnd); | |
598 | end Is_Known_Valid_Operand; | |
599 | ||
996ae0b0 RK |
600 | ------------------- |
601 | -- Is_Same_Value -- | |
602 | ------------------- | |
603 | ||
604 | function Is_Same_Value (L, R : Node_Id) return Boolean is | |
605 | Lf : constant Node_Id := Compare_Fixup (L); | |
606 | Rf : constant Node_Id := Compare_Fixup (R); | |
607 | ||
fbf5a39b | 608 | function Is_Same_Subscript (L, R : List_Id) return Boolean; |
57036dcc ES |
609 | -- L, R are the Expressions values from two attribute nodes for First |
610 | -- or Last attributes. Either may be set to No_List if no expressions | |
611 | -- are present (indicating subscript 1). The result is True if both | |
612 | -- expressions represent the same subscript (note one case is where | |
613 | -- one subscript is missing and the other is explicitly set to 1). | |
fbf5a39b AC |
614 | |
615 | ----------------------- | |
616 | -- Is_Same_Subscript -- | |
617 | ----------------------- | |
618 | ||
619 | function Is_Same_Subscript (L, R : List_Id) return Boolean is | |
620 | begin | |
621 | if L = No_List then | |
622 | if R = No_List then | |
623 | return True; | |
624 | else | |
625 | return Expr_Value (First (R)) = Uint_1; | |
626 | end if; | |
627 | ||
628 | else | |
629 | if R = No_List then | |
630 | return Expr_Value (First (L)) = Uint_1; | |
631 | else | |
632 | return Expr_Value (First (L)) = Expr_Value (First (R)); | |
633 | end if; | |
634 | end if; | |
635 | end Is_Same_Subscript; | |
636 | ||
637 | -- Start of processing for Is_Same_Value | |
638 | ||
996ae0b0 | 639 | begin |
b49365b2 | 640 | -- Values are the same if they refer to the same entity and the |
c800f862 RD |
641 | -- entity is non-volatile. This does not however apply to Float |
642 | -- types, since we may have two NaN values and they should never | |
643 | -- compare equal. | |
996ae0b0 | 644 | |
b49365b2 RD |
645 | if Nkind_In (Lf, N_Identifier, N_Expanded_Name) |
646 | and then Nkind_In (Rf, N_Identifier, N_Expanded_Name) | |
996ae0b0 | 647 | and then Entity (Lf) = Entity (Rf) |
b49365b2 | 648 | and then Present (Entity (Lf)) |
fbf5a39b | 649 | and then not Is_Floating_Point_Type (Etype (L)) |
c800f862 RD |
650 | and then not Is_Volatile_Reference (L) |
651 | and then not Is_Volatile_Reference (R) | |
996ae0b0 RK |
652 | then |
653 | return True; | |
654 | ||
655 | -- Or if they are compile time known and identical | |
656 | ||
657 | elsif Compile_Time_Known_Value (Lf) | |
658 | and then | |
659 | Compile_Time_Known_Value (Rf) | |
660 | and then Expr_Value (Lf) = Expr_Value (Rf) | |
661 | then | |
662 | return True; | |
663 | ||
b49365b2 RD |
664 | -- False if Nkind of the two nodes is different for remaining cases |
665 | ||
666 | elsif Nkind (Lf) /= Nkind (Rf) then | |
667 | return False; | |
668 | ||
669 | -- True if both 'First or 'Last values applying to the same entity | |
670 | -- (first and last don't change even if value does). Note that we | |
671 | -- need this even with the calls to Compare_Fixup, to handle the | |
672 | -- case of unconstrained array attributes where Compare_Fixup | |
673 | -- cannot find useful bounds. | |
996ae0b0 RK |
674 | |
675 | elsif Nkind (Lf) = N_Attribute_Reference | |
996ae0b0 RK |
676 | and then Attribute_Name (Lf) = Attribute_Name (Rf) |
677 | and then (Attribute_Name (Lf) = Name_First | |
678 | or else | |
679 | Attribute_Name (Lf) = Name_Last) | |
b49365b2 RD |
680 | and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name) |
681 | and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name) | |
996ae0b0 | 682 | and then Entity (Prefix (Lf)) = Entity (Prefix (Rf)) |
fbf5a39b | 683 | and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf)) |
996ae0b0 RK |
684 | then |
685 | return True; | |
686 | ||
b49365b2 RD |
687 | -- True if the same selected component from the same record |
688 | ||
689 | elsif Nkind (Lf) = N_Selected_Component | |
690 | and then Selector_Name (Lf) = Selector_Name (Rf) | |
691 | and then Is_Same_Value (Prefix (Lf), Prefix (Rf)) | |
692 | then | |
693 | return True; | |
694 | ||
695 | -- True if the same unary operator applied to the same operand | |
696 | ||
697 | elsif Nkind (Lf) in N_Unary_Op | |
698 | and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf)) | |
699 | then | |
700 | return True; | |
701 | ||
8682d22c | 702 | -- True if the same binary operator applied to the same operands |
b49365b2 RD |
703 | |
704 | elsif Nkind (Lf) in N_Binary_Op | |
705 | and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf)) | |
706 | and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf)) | |
707 | then | |
708 | return True; | |
709 | ||
8682d22c | 710 | -- All other cases, we can't tell, so return False |
996ae0b0 RK |
711 | |
712 | else | |
713 | return False; | |
714 | end if; | |
715 | end Is_Same_Value; | |
716 | ||
717 | -- Start of processing for Compile_Time_Compare | |
718 | ||
719 | begin | |
af02a866 RD |
720 | Diff.all := No_Uint; |
721 | ||
07fc65c4 GB |
722 | -- If either operand could raise constraint error, then we cannot |
723 | -- know the result at compile time (since CE may be raised!) | |
724 | ||
725 | if not (Cannot_Raise_Constraint_Error (L) | |
726 | and then | |
727 | Cannot_Raise_Constraint_Error (R)) | |
728 | then | |
729 | return Unknown; | |
730 | end if; | |
731 | ||
732 | -- Identical operands are most certainly equal | |
733 | ||
996ae0b0 RK |
734 | if L = R then |
735 | return EQ; | |
736 | ||
93c3fca7 AC |
737 | -- If expressions have no types, then do not attempt to determine if |
738 | -- they are the same, since something funny is going on. One case in | |
739 | -- which this happens is during generic template analysis, when bounds | |
740 | -- are not fully analyzed. | |
996ae0b0 RK |
741 | |
742 | elsif No (Ltyp) or else No (Rtyp) then | |
743 | return Unknown; | |
744 | ||
93c3fca7 AC |
745 | -- We do not attempt comparisons for packed arrays arrays represented as |
746 | -- modular types, where the semantics of comparison is quite different. | |
996ae0b0 | 747 | |
93c3fca7 AC |
748 | elsif Is_Packed_Array_Type (Ltyp) |
749 | and then Is_Modular_Integer_Type (Ltyp) | |
996ae0b0 RK |
750 | then |
751 | return Unknown; | |
752 | ||
93c3fca7 | 753 | -- For access types, the only time we know the result at compile time |
f61580d4 | 754 | -- (apart from identical operands, which we handled already) is if we |
93c3fca7 AC |
755 | -- know one operand is null and the other is not, or both operands are |
756 | -- known null. | |
757 | ||
758 | elsif Is_Access_Type (Ltyp) then | |
759 | if Known_Null (L) then | |
760 | if Known_Null (R) then | |
761 | return EQ; | |
762 | elsif Known_Non_Null (R) then | |
763 | return NE; | |
764 | else | |
765 | return Unknown; | |
766 | end if; | |
767 | ||
f61580d4 | 768 | elsif Known_Non_Null (L) and then Known_Null (R) then |
93c3fca7 AC |
769 | return NE; |
770 | ||
771 | else | |
772 | return Unknown; | |
773 | end if; | |
774 | ||
996ae0b0 RK |
775 | -- Case where comparison involves two compile time known values |
776 | ||
777 | elsif Compile_Time_Known_Value (L) | |
778 | and then Compile_Time_Known_Value (R) | |
779 | then | |
780 | -- For the floating-point case, we have to be a little careful, since | |
781 | -- at compile time we are dealing with universal exact values, but at | |
782 | -- runtime, these will be in non-exact target form. That's why the | |
783 | -- returned results are LE and GE below instead of LT and GT. | |
784 | ||
785 | if Is_Floating_Point_Type (Ltyp) | |
786 | or else | |
787 | Is_Floating_Point_Type (Rtyp) | |
788 | then | |
789 | declare | |
790 | Lo : constant Ureal := Expr_Value_R (L); | |
791 | Hi : constant Ureal := Expr_Value_R (R); | |
792 | ||
793 | begin | |
794 | if Lo < Hi then | |
795 | return LE; | |
796 | elsif Lo = Hi then | |
797 | return EQ; | |
798 | else | |
799 | return GE; | |
800 | end if; | |
801 | end; | |
802 | ||
93c3fca7 AC |
803 | -- For string types, we have two string literals and we proceed to |
804 | -- compare them using the Ada style dictionary string comparison. | |
805 | ||
806 | elsif not Is_Scalar_Type (Ltyp) then | |
807 | declare | |
808 | Lstring : constant String_Id := Strval (Expr_Value_S (L)); | |
809 | Rstring : constant String_Id := Strval (Expr_Value_S (R)); | |
810 | Llen : constant Nat := String_Length (Lstring); | |
811 | Rlen : constant Nat := String_Length (Rstring); | |
812 | ||
813 | begin | |
814 | for J in 1 .. Nat'Min (Llen, Rlen) loop | |
815 | declare | |
816 | LC : constant Char_Code := Get_String_Char (Lstring, J); | |
817 | RC : constant Char_Code := Get_String_Char (Rstring, J); | |
818 | begin | |
819 | if LC < RC then | |
820 | return LT; | |
821 | elsif LC > RC then | |
822 | return GT; | |
823 | end if; | |
824 | end; | |
825 | end loop; | |
826 | ||
827 | if Llen < Rlen then | |
828 | return LT; | |
829 | elsif Llen > Rlen then | |
830 | return GT; | |
831 | else | |
832 | return EQ; | |
833 | end if; | |
834 | end; | |
835 | ||
836 | -- For remaining scalar cases we know exactly (note that this does | |
837 | -- include the fixed-point case, where we know the run time integer | |
f61580d4 | 838 | -- values now). |
996ae0b0 RK |
839 | |
840 | else | |
841 | declare | |
842 | Lo : constant Uint := Expr_Value (L); | |
843 | Hi : constant Uint := Expr_Value (R); | |
844 | ||
845 | begin | |
846 | if Lo < Hi then | |
af02a866 | 847 | Diff.all := Hi - Lo; |
996ae0b0 | 848 | return LT; |
af02a866 | 849 | |
996ae0b0 RK |
850 | elsif Lo = Hi then |
851 | return EQ; | |
af02a866 | 852 | |
996ae0b0 | 853 | else |
af02a866 | 854 | Diff.all := Lo - Hi; |
996ae0b0 RK |
855 | return GT; |
856 | end if; | |
857 | end; | |
858 | end if; | |
859 | ||
860 | -- Cases where at least one operand is not known at compile time | |
861 | ||
862 | else | |
93c3fca7 | 863 | -- Remaining checks apply only for discrete types |
29797f34 RD |
864 | |
865 | if not Is_Discrete_Type (Ltyp) | |
866 | or else not Is_Discrete_Type (Rtyp) | |
93c3fca7 AC |
867 | then |
868 | return Unknown; | |
869 | end if; | |
870 | ||
871 | -- Defend against generic types, or actually any expressions that | |
872 | -- contain a reference to a generic type from within a generic | |
873 | -- template. We don't want to do any range analysis of such | |
874 | -- expressions for two reasons. First, the bounds of a generic type | |
875 | -- itself are junk and cannot be used for any kind of analysis. | |
876 | -- Second, we may have a case where the range at run time is indeed | |
877 | -- known, but we don't want to do compile time analysis in the | |
878 | -- template based on that range since in an instance the value may be | |
879 | -- static, and able to be elaborated without reference to the bounds | |
880 | -- of types involved. As an example, consider: | |
881 | ||
882 | -- (F'Pos (F'Last) + 1) > Integer'Last | |
883 | ||
884 | -- The expression on the left side of > is Universal_Integer and thus | |
885 | -- acquires the type Integer for evaluation at run time, and at run | |
886 | -- time it is true that this condition is always False, but within | |
887 | -- an instance F may be a type with a static range greater than the | |
888 | -- range of Integer, and the expression statically evaluates to True. | |
889 | ||
890 | if References_Generic_Formal_Type (L) | |
891 | or else | |
892 | References_Generic_Formal_Type (R) | |
29797f34 RD |
893 | then |
894 | return Unknown; | |
895 | end if; | |
896 | ||
1c7717c3 AC |
897 | -- Replace types by base types for the case of entities which are |
898 | -- not known to have valid representations. This takes care of | |
899 | -- properly dealing with invalid representations. | |
900 | ||
c800f862 | 901 | if not Assume_Valid and then not Assume_No_Invalid_Values then |
1c7717c3 | 902 | if Is_Entity_Name (L) and then not Is_Known_Valid (Entity (L)) then |
93c3fca7 | 903 | Ltyp := Underlying_Type (Base_Type (Ltyp)); |
1c7717c3 AC |
904 | end if; |
905 | ||
906 | if Is_Entity_Name (R) and then not Is_Known_Valid (Entity (R)) then | |
93c3fca7 | 907 | Rtyp := Underlying_Type (Base_Type (Rtyp)); |
1c7717c3 AC |
908 | end if; |
909 | end if; | |
910 | ||
c800f862 RD |
911 | -- Try range analysis on variables and see if ranges are disjoint |
912 | ||
913 | declare | |
914 | LOK, ROK : Boolean; | |
915 | LLo, LHi : Uint; | |
916 | RLo, RHi : Uint; | |
917 | ||
918 | begin | |
919 | Determine_Range (L, LOK, LLo, LHi, Assume_Valid); | |
920 | Determine_Range (R, ROK, RLo, RHi, Assume_Valid); | |
921 | ||
922 | if LOK and ROK then | |
923 | if LHi < RLo then | |
924 | return LT; | |
925 | ||
926 | elsif RHi < LLo then | |
927 | return GT; | |
928 | ||
929 | elsif LLo = LHi | |
930 | and then RLo = RHi | |
931 | and then LLo = RLo | |
932 | then | |
e27b834b | 933 | |
75ba322d AC |
934 | -- If the range includes a single literal and we can assume |
935 | -- validity then the result is known even if an operand is | |
936 | -- not static. | |
e27b834b AC |
937 | |
938 | if Assume_Valid then | |
939 | return EQ; | |
e27b834b AC |
940 | else |
941 | return Unknown; | |
942 | end if; | |
c800f862 RD |
943 | |
944 | elsif LHi = RLo then | |
945 | return LE; | |
946 | ||
947 | elsif RHi = LLo then | |
948 | return GE; | |
57036dcc ES |
949 | |
950 | elsif not Is_Known_Valid_Operand (L) | |
951 | and then not Assume_Valid | |
952 | then | |
953 | if Is_Same_Value (L, R) then | |
954 | return EQ; | |
955 | else | |
956 | return Unknown; | |
957 | end if; | |
c800f862 RD |
958 | end if; |
959 | end if; | |
960 | end; | |
961 | ||
996ae0b0 RK |
962 | -- Here is where we check for comparisons against maximum bounds of |
963 | -- types, where we know that no value can be outside the bounds of | |
964 | -- the subtype. Note that this routine is allowed to assume that all | |
965 | -- expressions are within their subtype bounds. Callers wishing to | |
966 | -- deal with possibly invalid values must in any case take special | |
967 | -- steps (e.g. conversions to larger types) to avoid this kind of | |
968 | -- optimization, which is always considered to be valid. We do not | |
969 | -- attempt this optimization with generic types, since the type | |
970 | -- bounds may not be meaningful in this case. | |
971 | ||
93c3fca7 | 972 | -- We are in danger of an infinite recursion here. It does not seem |
fbf5a39b AC |
973 | -- useful to go more than one level deep, so the parameter Rec is |
974 | -- used to protect ourselves against this infinite recursion. | |
975 | ||
29797f34 RD |
976 | if not Rec then |
977 | ||
fbf5a39b AC |
978 | -- See if we can get a decisive check against one operand and |
979 | -- a bound of the other operand (four possible tests here). | |
93c3fca7 AC |
980 | -- Note that we avoid testing junk bounds of a generic type. |
981 | ||
982 | if not Is_Generic_Type (Rtyp) then | |
983 | case Compile_Time_Compare (L, Type_Low_Bound (Rtyp), | |
984 | Discard'Access, | |
985 | Assume_Valid, Rec => True) | |
986 | is | |
987 | when LT => return LT; | |
988 | when LE => return LE; | |
989 | when EQ => return LE; | |
990 | when others => null; | |
991 | end case; | |
fbf5a39b | 992 | |
93c3fca7 AC |
993 | case Compile_Time_Compare (L, Type_High_Bound (Rtyp), |
994 | Discard'Access, | |
995 | Assume_Valid, Rec => True) | |
996 | is | |
997 | when GT => return GT; | |
998 | when GE => return GE; | |
999 | when EQ => return GE; | |
1000 | when others => null; | |
1001 | end case; | |
1002 | end if; | |
996ae0b0 | 1003 | |
93c3fca7 AC |
1004 | if not Is_Generic_Type (Ltyp) then |
1005 | case Compile_Time_Compare (Type_Low_Bound (Ltyp), R, | |
1006 | Discard'Access, | |
1007 | Assume_Valid, Rec => True) | |
1008 | is | |
1009 | when GT => return GT; | |
1010 | when GE => return GE; | |
1011 | when EQ => return GE; | |
1012 | when others => null; | |
1013 | end case; | |
996ae0b0 | 1014 | |
93c3fca7 AC |
1015 | case Compile_Time_Compare (Type_High_Bound (Ltyp), R, |
1016 | Discard'Access, | |
1017 | Assume_Valid, Rec => True) | |
1018 | is | |
1019 | when LT => return LT; | |
1020 | when LE => return LE; | |
1021 | when EQ => return LE; | |
1022 | when others => null; | |
1023 | end case; | |
1024 | end if; | |
996ae0b0 RK |
1025 | end if; |
1026 | ||
1027 | -- Next attempt is to decompose the expressions to extract | |
1028 | -- a constant offset resulting from the use of any of the forms: | |
1029 | ||
1030 | -- expr + literal | |
1031 | -- expr - literal | |
1032 | -- typ'Succ (expr) | |
1033 | -- typ'Pred (expr) | |
1034 | ||
1035 | -- Then we see if the two expressions are the same value, and if so | |
1036 | -- the result is obtained by comparing the offsets. | |
1037 | ||
1038 | declare | |
1039 | Lnode : Node_Id; | |
1040 | Loffs : Uint; | |
1041 | Rnode : Node_Id; | |
1042 | Roffs : Uint; | |
1043 | ||
1044 | begin | |
1045 | Compare_Decompose (L, Lnode, Loffs); | |
1046 | Compare_Decompose (R, Rnode, Roffs); | |
1047 | ||
1048 | if Is_Same_Value (Lnode, Rnode) then | |
1049 | if Loffs = Roffs then | |
1050 | return EQ; | |
1051 | ||
1052 | elsif Loffs < Roffs then | |
af02a866 | 1053 | Diff.all := Roffs - Loffs; |
996ae0b0 RK |
1054 | return LT; |
1055 | ||
1056 | else | |
af02a866 | 1057 | Diff.all := Loffs - Roffs; |
996ae0b0 RK |
1058 | return GT; |
1059 | end if; | |
29797f34 RD |
1060 | end if; |
1061 | end; | |
1062 | ||
1063 | -- Next attempt is to see if we have an entity compared with a | |
1064 | -- compile time known value, where there is a current value | |
1065 | -- conditional for the entity which can tell us the result. | |
1066 | ||
1067 | declare | |
1068 | Var : Node_Id; | |
1069 | -- Entity variable (left operand) | |
1070 | ||
1071 | Val : Uint; | |
1072 | -- Value (right operand) | |
1073 | ||
1074 | Inv : Boolean; | |
1075 | -- If False, we have reversed the operands | |
1076 | ||
1077 | Op : Node_Kind; | |
1078 | -- Comparison operator kind from Get_Current_Value_Condition call | |
996ae0b0 | 1079 | |
29797f34 RD |
1080 | Opn : Node_Id; |
1081 | -- Value from Get_Current_Value_Condition call | |
1082 | ||
1083 | Opv : Uint; | |
1084 | -- Value of Opn | |
1085 | ||
1086 | Result : Compare_Result; | |
1087 | -- Known result before inversion | |
1088 | ||
1089 | begin | |
1090 | if Is_Entity_Name (L) | |
1091 | and then Compile_Time_Known_Value (R) | |
1092 | then | |
1093 | Var := L; | |
1094 | Val := Expr_Value (R); | |
1095 | Inv := False; | |
1096 | ||
1097 | elsif Is_Entity_Name (R) | |
1098 | and then Compile_Time_Known_Value (L) | |
1099 | then | |
1100 | Var := R; | |
1101 | Val := Expr_Value (L); | |
1102 | Inv := True; | |
1103 | ||
1104 | -- That was the last chance at finding a compile time result | |
996ae0b0 RK |
1105 | |
1106 | else | |
1107 | return Unknown; | |
1108 | end if; | |
29797f34 RD |
1109 | |
1110 | Get_Current_Value_Condition (Var, Op, Opn); | |
1111 | ||
1112 | -- That was the last chance, so if we got nothing return | |
1113 | ||
1114 | if No (Opn) then | |
1115 | return Unknown; | |
1116 | end if; | |
1117 | ||
1118 | Opv := Expr_Value (Opn); | |
1119 | ||
1120 | -- We got a comparison, so we might have something interesting | |
1121 | ||
1122 | -- Convert LE to LT and GE to GT, just so we have fewer cases | |
1123 | ||
1124 | if Op = N_Op_Le then | |
1125 | Op := N_Op_Lt; | |
1126 | Opv := Opv + 1; | |
af02a866 | 1127 | |
29797f34 RD |
1128 | elsif Op = N_Op_Ge then |
1129 | Op := N_Op_Gt; | |
1130 | Opv := Opv - 1; | |
1131 | end if; | |
1132 | ||
1133 | -- Deal with equality case | |
1134 | ||
1135 | if Op = N_Op_Eq then | |
1136 | if Val = Opv then | |
1137 | Result := EQ; | |
1138 | elsif Opv < Val then | |
1139 | Result := LT; | |
1140 | else | |
1141 | Result := GT; | |
1142 | end if; | |
1143 | ||
1144 | -- Deal with inequality case | |
1145 | ||
1146 | elsif Op = N_Op_Ne then | |
1147 | if Val = Opv then | |
1148 | Result := NE; | |
1149 | else | |
1150 | return Unknown; | |
1151 | end if; | |
1152 | ||
1153 | -- Deal with greater than case | |
1154 | ||
1155 | elsif Op = N_Op_Gt then | |
1156 | if Opv >= Val then | |
1157 | Result := GT; | |
1158 | elsif Opv = Val - 1 then | |
1159 | Result := GE; | |
1160 | else | |
1161 | return Unknown; | |
1162 | end if; | |
1163 | ||
1164 | -- Deal with less than case | |
1165 | ||
1166 | else pragma Assert (Op = N_Op_Lt); | |
1167 | if Opv <= Val then | |
1168 | Result := LT; | |
1169 | elsif Opv = Val + 1 then | |
1170 | Result := LE; | |
1171 | else | |
1172 | return Unknown; | |
1173 | end if; | |
1174 | end if; | |
1175 | ||
1176 | -- Deal with inverting result | |
1177 | ||
1178 | if Inv then | |
1179 | case Result is | |
1180 | when GT => return LT; | |
1181 | when GE => return LE; | |
1182 | when LT => return GT; | |
1183 | when LE => return GE; | |
1184 | when others => return Result; | |
1185 | end case; | |
1186 | end if; | |
1187 | ||
1188 | return Result; | |
996ae0b0 RK |
1189 | end; |
1190 | end if; | |
1191 | end Compile_Time_Compare; | |
1192 | ||
f44fe430 RD |
1193 | ------------------------------- |
1194 | -- Compile_Time_Known_Bounds -- | |
1195 | ------------------------------- | |
1196 | ||
1197 | function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is | |
1198 | Indx : Node_Id; | |
1199 | Typ : Entity_Id; | |
1200 | ||
1201 | begin | |
1202 | if not Is_Array_Type (T) then | |
1203 | return False; | |
1204 | end if; | |
1205 | ||
1206 | Indx := First_Index (T); | |
1207 | while Present (Indx) loop | |
1208 | Typ := Underlying_Type (Etype (Indx)); | |
93c3fca7 AC |
1209 | |
1210 | -- Never look at junk bounds of a generic type | |
1211 | ||
1212 | if Is_Generic_Type (Typ) then | |
1213 | return False; | |
1214 | end if; | |
1215 | ||
1216 | -- Otherwise check bounds for compile time known | |
1217 | ||
f44fe430 RD |
1218 | if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then |
1219 | return False; | |
1220 | elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then | |
1221 | return False; | |
1222 | else | |
1223 | Next_Index (Indx); | |
1224 | end if; | |
1225 | end loop; | |
1226 | ||
1227 | return True; | |
1228 | end Compile_Time_Known_Bounds; | |
1229 | ||
996ae0b0 RK |
1230 | ------------------------------ |
1231 | -- Compile_Time_Known_Value -- | |
1232 | ------------------------------ | |
1233 | ||
1234 | function Compile_Time_Known_Value (Op : Node_Id) return Boolean is | |
07fc65c4 GB |
1235 | K : constant Node_Kind := Nkind (Op); |
1236 | CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size); | |
996ae0b0 RK |
1237 | |
1238 | begin | |
1239 | -- Never known at compile time if bad type or raises constraint error | |
1240 | -- or empty (latter case occurs only as a result of a previous error) | |
1241 | ||
1242 | if No (Op) | |
1243 | or else Op = Error | |
1244 | or else Etype (Op) = Any_Type | |
1245 | or else Raises_Constraint_Error (Op) | |
1246 | then | |
1247 | return False; | |
1248 | end if; | |
1249 | ||
597d7158 GD |
1250 | -- If this is not a static expression or a null literal, and we are in |
1251 | -- configurable run-time mode, then we consider it not known at compile | |
1252 | -- time. This avoids anomalies where whether something is allowed with a | |
1253 | -- given configurable run-time library depends on how good the compiler | |
1254 | -- is at optimizing and knowing that things are constant when they are | |
1255 | -- nonstatic. | |
1256 | ||
1257 | if Configurable_Run_Time_Mode | |
1258 | and then K /= N_Null | |
1259 | and then not Is_Static_Expression (Op) | |
1260 | then | |
fbf5a39b AC |
1261 | return False; |
1262 | end if; | |
1263 | ||
996ae0b0 RK |
1264 | -- If we have an entity name, then see if it is the name of a constant |
1265 | -- and if so, test the corresponding constant value, or the name of | |
1266 | -- an enumeration literal, which is always a constant. | |
1267 | ||
1268 | if Present (Etype (Op)) and then Is_Entity_Name (Op) then | |
1269 | declare | |
1270 | E : constant Entity_Id := Entity (Op); | |
1271 | V : Node_Id; | |
1272 | ||
1273 | begin | |
1274 | -- Never known at compile time if it is a packed array value. | |
1275 | -- We might want to try to evaluate these at compile time one | |
1276 | -- day, but we do not make that attempt now. | |
1277 | ||
1278 | if Is_Packed_Array_Type (Etype (Op)) then | |
1279 | return False; | |
1280 | end if; | |
1281 | ||
1282 | if Ekind (E) = E_Enumeration_Literal then | |
1283 | return True; | |
1284 | ||
07fc65c4 | 1285 | elsif Ekind (E) = E_Constant then |
996ae0b0 RK |
1286 | V := Constant_Value (E); |
1287 | return Present (V) and then Compile_Time_Known_Value (V); | |
1288 | end if; | |
1289 | end; | |
1290 | ||
1291 | -- We have a value, see if it is compile time known | |
1292 | ||
1293 | else | |
07fc65c4 | 1294 | -- Integer literals are worth storing in the cache |
996ae0b0 | 1295 | |
07fc65c4 GB |
1296 | if K = N_Integer_Literal then |
1297 | CV_Ent.N := Op; | |
1298 | CV_Ent.V := Intval (Op); | |
1299 | return True; | |
1300 | ||
1301 | -- Other literals and NULL are known at compile time | |
1302 | ||
1303 | elsif | |
996ae0b0 RK |
1304 | K = N_Character_Literal |
1305 | or else | |
1306 | K = N_Real_Literal | |
1307 | or else | |
1308 | K = N_String_Literal | |
1309 | or else | |
1310 | K = N_Null | |
1311 | then | |
1312 | return True; | |
1313 | ||
1314 | -- Any reference to Null_Parameter is known at compile time. No | |
1315 | -- other attribute references (that have not already been folded) | |
1316 | -- are known at compile time. | |
1317 | ||
1318 | elsif K = N_Attribute_Reference then | |
1319 | return Attribute_Name (Op) = Name_Null_Parameter; | |
07fc65c4 | 1320 | end if; |
996ae0b0 | 1321 | end if; |
07fc65c4 GB |
1322 | |
1323 | -- If we fall through, not known at compile time | |
1324 | ||
1325 | return False; | |
1326 | ||
1327 | -- If we get an exception while trying to do this test, then some error | |
1328 | -- has occurred, and we simply say that the value is not known after all | |
1329 | ||
1330 | exception | |
1331 | when others => | |
1332 | return False; | |
996ae0b0 RK |
1333 | end Compile_Time_Known_Value; |
1334 | ||
1335 | -------------------------------------- | |
1336 | -- Compile_Time_Known_Value_Or_Aggr -- | |
1337 | -------------------------------------- | |
1338 | ||
1339 | function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is | |
1340 | begin | |
1341 | -- If we have an entity name, then see if it is the name of a constant | |
1342 | -- and if so, test the corresponding constant value, or the name of | |
1343 | -- an enumeration literal, which is always a constant. | |
1344 | ||
1345 | if Is_Entity_Name (Op) then | |
1346 | declare | |
1347 | E : constant Entity_Id := Entity (Op); | |
1348 | V : Node_Id; | |
1349 | ||
1350 | begin | |
1351 | if Ekind (E) = E_Enumeration_Literal then | |
1352 | return True; | |
1353 | ||
1354 | elsif Ekind (E) /= E_Constant then | |
1355 | return False; | |
1356 | ||
1357 | else | |
1358 | V := Constant_Value (E); | |
1359 | return Present (V) | |
1360 | and then Compile_Time_Known_Value_Or_Aggr (V); | |
1361 | end if; | |
1362 | end; | |
1363 | ||
1364 | -- We have a value, see if it is compile time known | |
1365 | ||
1366 | else | |
1367 | if Compile_Time_Known_Value (Op) then | |
1368 | return True; | |
1369 | ||
1370 | elsif Nkind (Op) = N_Aggregate then | |
1371 | ||
1372 | if Present (Expressions (Op)) then | |
1373 | declare | |
1374 | Expr : Node_Id; | |
1375 | ||
1376 | begin | |
1377 | Expr := First (Expressions (Op)); | |
1378 | while Present (Expr) loop | |
1379 | if not Compile_Time_Known_Value_Or_Aggr (Expr) then | |
1380 | return False; | |
1381 | end if; | |
1382 | ||
1383 | Next (Expr); | |
1384 | end loop; | |
1385 | end; | |
1386 | end if; | |
1387 | ||
1388 | if Present (Component_Associations (Op)) then | |
1389 | declare | |
1390 | Cass : Node_Id; | |
1391 | ||
1392 | begin | |
1393 | Cass := First (Component_Associations (Op)); | |
1394 | while Present (Cass) loop | |
1395 | if not | |
1396 | Compile_Time_Known_Value_Or_Aggr (Expression (Cass)) | |
1397 | then | |
1398 | return False; | |
1399 | end if; | |
1400 | ||
1401 | Next (Cass); | |
1402 | end loop; | |
1403 | end; | |
1404 | end if; | |
1405 | ||
1406 | return True; | |
1407 | ||
1408 | -- All other types of values are not known at compile time | |
1409 | ||
1410 | else | |
1411 | return False; | |
1412 | end if; | |
1413 | ||
1414 | end if; | |
1415 | end Compile_Time_Known_Value_Or_Aggr; | |
1416 | ||
1417 | ----------------- | |
1418 | -- Eval_Actual -- | |
1419 | ----------------- | |
1420 | ||
1421 | -- This is only called for actuals of functions that are not predefined | |
1422 | -- operators (which have already been rewritten as operators at this | |
1423 | -- stage), so the call can never be folded, and all that needs doing for | |
1424 | -- the actual is to do the check for a non-static context. | |
1425 | ||
1426 | procedure Eval_Actual (N : Node_Id) is | |
1427 | begin | |
1428 | Check_Non_Static_Context (N); | |
1429 | end Eval_Actual; | |
1430 | ||
1431 | -------------------- | |
1432 | -- Eval_Allocator -- | |
1433 | -------------------- | |
1434 | ||
1435 | -- Allocators are never static, so all we have to do is to do the | |
1436 | -- check for a non-static context if an expression is present. | |
1437 | ||
1438 | procedure Eval_Allocator (N : Node_Id) is | |
1439 | Expr : constant Node_Id := Expression (N); | |
1440 | ||
1441 | begin | |
1442 | if Nkind (Expr) = N_Qualified_Expression then | |
1443 | Check_Non_Static_Context (Expression (Expr)); | |
1444 | end if; | |
1445 | end Eval_Allocator; | |
1446 | ||
1447 | ------------------------ | |
1448 | -- Eval_Arithmetic_Op -- | |
1449 | ------------------------ | |
1450 | ||
1451 | -- Arithmetic operations are static functions, so the result is static | |
1452 | -- if both operands are static (RM 4.9(7), 4.9(20)). | |
1453 | ||
1454 | procedure Eval_Arithmetic_Op (N : Node_Id) is | |
1455 | Left : constant Node_Id := Left_Opnd (N); | |
1456 | Right : constant Node_Id := Right_Opnd (N); | |
1457 | Ltype : constant Entity_Id := Etype (Left); | |
1458 | Rtype : constant Entity_Id := Etype (Right); | |
d7567964 | 1459 | Otype : Entity_Id := Empty; |
996ae0b0 RK |
1460 | Stat : Boolean; |
1461 | Fold : Boolean; | |
1462 | ||
1463 | begin | |
1464 | -- If not foldable we are done | |
1465 | ||
1466 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); | |
1467 | ||
1468 | if not Fold then | |
1469 | return; | |
1470 | end if; | |
1471 | ||
d7567964 TQ |
1472 | if Is_Universal_Numeric_Type (Etype (Left)) |
1473 | and then | |
1474 | Is_Universal_Numeric_Type (Etype (Right)) | |
602a7ec0 | 1475 | then |
d7567964 | 1476 | Otype := Find_Universal_Operator_Type (N); |
602a7ec0 AC |
1477 | end if; |
1478 | ||
996ae0b0 RK |
1479 | -- Fold for cases where both operands are of integer type |
1480 | ||
1481 | if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then | |
1482 | declare | |
1483 | Left_Int : constant Uint := Expr_Value (Left); | |
1484 | Right_Int : constant Uint := Expr_Value (Right); | |
1485 | Result : Uint; | |
1486 | ||
1487 | begin | |
1488 | case Nkind (N) is | |
1489 | ||
1490 | when N_Op_Add => | |
1491 | Result := Left_Int + Right_Int; | |
1492 | ||
1493 | when N_Op_Subtract => | |
1494 | Result := Left_Int - Right_Int; | |
1495 | ||
1496 | when N_Op_Multiply => | |
1497 | if OK_Bits | |
1498 | (N, UI_From_Int | |
1499 | (Num_Bits (Left_Int) + Num_Bits (Right_Int))) | |
1500 | then | |
1501 | Result := Left_Int * Right_Int; | |
1502 | else | |
1503 | Result := Left_Int; | |
1504 | end if; | |
1505 | ||
1506 | when N_Op_Divide => | |
1507 | ||
1508 | -- The exception Constraint_Error is raised by integer | |
1509 | -- division, rem and mod if the right operand is zero. | |
1510 | ||
1511 | if Right_Int = 0 then | |
1512 | Apply_Compile_Time_Constraint_Error | |
fbf5a39b AC |
1513 | (N, "division by zero", |
1514 | CE_Divide_By_Zero, | |
1515 | Warn => not Stat); | |
996ae0b0 | 1516 | return; |
fbf5a39b | 1517 | |
996ae0b0 RK |
1518 | else |
1519 | Result := Left_Int / Right_Int; | |
1520 | end if; | |
1521 | ||
1522 | when N_Op_Mod => | |
1523 | ||
1524 | -- The exception Constraint_Error is raised by integer | |
1525 | -- division, rem and mod if the right operand is zero. | |
1526 | ||
1527 | if Right_Int = 0 then | |
1528 | Apply_Compile_Time_Constraint_Error | |
fbf5a39b AC |
1529 | (N, "mod with zero divisor", |
1530 | CE_Divide_By_Zero, | |
1531 | Warn => not Stat); | |
996ae0b0 RK |
1532 | return; |
1533 | else | |
1534 | Result := Left_Int mod Right_Int; | |
1535 | end if; | |
1536 | ||
1537 | when N_Op_Rem => | |
1538 | ||
1539 | -- The exception Constraint_Error is raised by integer | |
1540 | -- division, rem and mod if the right operand is zero. | |
1541 | ||
1542 | if Right_Int = 0 then | |
1543 | Apply_Compile_Time_Constraint_Error | |
fbf5a39b AC |
1544 | (N, "rem with zero divisor", |
1545 | CE_Divide_By_Zero, | |
1546 | Warn => not Stat); | |
996ae0b0 | 1547 | return; |
fbf5a39b | 1548 | |
996ae0b0 RK |
1549 | else |
1550 | Result := Left_Int rem Right_Int; | |
1551 | end if; | |
1552 | ||
1553 | when others => | |
1554 | raise Program_Error; | |
1555 | end case; | |
1556 | ||
1557 | -- Adjust the result by the modulus if the type is a modular type | |
1558 | ||
1559 | if Is_Modular_Integer_Type (Ltype) then | |
1560 | Result := Result mod Modulus (Ltype); | |
82c80734 RD |
1561 | |
1562 | -- For a signed integer type, check non-static overflow | |
1563 | ||
1564 | elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then | |
1565 | declare | |
1566 | BT : constant Entity_Id := Base_Type (Ltype); | |
1567 | Lo : constant Uint := Expr_Value (Type_Low_Bound (BT)); | |
1568 | Hi : constant Uint := Expr_Value (Type_High_Bound (BT)); | |
1569 | begin | |
1570 | if Result < Lo or else Result > Hi then | |
1571 | Apply_Compile_Time_Constraint_Error | |
1572 | (N, "value not in range of }?", | |
1573 | CE_Overflow_Check_Failed, | |
1574 | Ent => BT); | |
1575 | return; | |
1576 | end if; | |
1577 | end; | |
996ae0b0 RK |
1578 | end if; |
1579 | ||
82c80734 RD |
1580 | -- If we get here we can fold the result |
1581 | ||
fbf5a39b | 1582 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
1583 | end; |
1584 | ||
d7567964 TQ |
1585 | -- Cases where at least one operand is a real. We handle the cases of |
1586 | -- both reals, or mixed/real integer cases (the latter happen only for | |
1587 | -- divide and multiply, and the result is always real). | |
996ae0b0 RK |
1588 | |
1589 | elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then | |
1590 | declare | |
1591 | Left_Real : Ureal; | |
1592 | Right_Real : Ureal; | |
1593 | Result : Ureal; | |
1594 | ||
1595 | begin | |
1596 | if Is_Real_Type (Ltype) then | |
1597 | Left_Real := Expr_Value_R (Left); | |
1598 | else | |
1599 | Left_Real := UR_From_Uint (Expr_Value (Left)); | |
1600 | end if; | |
1601 | ||
1602 | if Is_Real_Type (Rtype) then | |
1603 | Right_Real := Expr_Value_R (Right); | |
1604 | else | |
1605 | Right_Real := UR_From_Uint (Expr_Value (Right)); | |
1606 | end if; | |
1607 | ||
1608 | if Nkind (N) = N_Op_Add then | |
1609 | Result := Left_Real + Right_Real; | |
1610 | ||
1611 | elsif Nkind (N) = N_Op_Subtract then | |
1612 | Result := Left_Real - Right_Real; | |
1613 | ||
1614 | elsif Nkind (N) = N_Op_Multiply then | |
1615 | Result := Left_Real * Right_Real; | |
1616 | ||
1617 | else pragma Assert (Nkind (N) = N_Op_Divide); | |
1618 | if UR_Is_Zero (Right_Real) then | |
1619 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 1620 | (N, "division by zero", CE_Divide_By_Zero); |
996ae0b0 RK |
1621 | return; |
1622 | end if; | |
1623 | ||
1624 | Result := Left_Real / Right_Real; | |
1625 | end if; | |
1626 | ||
fbf5a39b | 1627 | Fold_Ureal (N, Result, Stat); |
996ae0b0 RK |
1628 | end; |
1629 | end if; | |
d7567964 TQ |
1630 | |
1631 | -- If the operator was resolved to a specific type, make sure that type | |
1632 | -- is frozen even if the expression is folded into a literal (which has | |
1633 | -- a universal type). | |
1634 | ||
1635 | if Present (Otype) then | |
1636 | Freeze_Before (N, Otype); | |
1637 | end if; | |
996ae0b0 RK |
1638 | end Eval_Arithmetic_Op; |
1639 | ||
1640 | ---------------------------- | |
1641 | -- Eval_Character_Literal -- | |
1642 | ---------------------------- | |
1643 | ||
1644 | -- Nothing to be done! | |
1645 | ||
1646 | procedure Eval_Character_Literal (N : Node_Id) is | |
07fc65c4 | 1647 | pragma Warnings (Off, N); |
996ae0b0 RK |
1648 | begin |
1649 | null; | |
1650 | end Eval_Character_Literal; | |
1651 | ||
c01a9391 AC |
1652 | --------------- |
1653 | -- Eval_Call -- | |
1654 | --------------- | |
1655 | ||
1656 | -- Static function calls are either calls to predefined operators | |
1657 | -- with static arguments, or calls to functions that rename a literal. | |
1658 | -- Only the latter case is handled here, predefined operators are | |
1659 | -- constant-folded elsewhere. | |
29797f34 | 1660 | |
c01a9391 AC |
1661 | -- If the function is itself inherited (see 7423-001) the literal of |
1662 | -- the parent type must be explicitly converted to the return type | |
1663 | -- of the function. | |
1664 | ||
1665 | procedure Eval_Call (N : Node_Id) is | |
1666 | Loc : constant Source_Ptr := Sloc (N); | |
1667 | Typ : constant Entity_Id := Etype (N); | |
1668 | Lit : Entity_Id; | |
1669 | ||
1670 | begin | |
1671 | if Nkind (N) = N_Function_Call | |
1672 | and then No (Parameter_Associations (N)) | |
1673 | and then Is_Entity_Name (Name (N)) | |
1674 | and then Present (Alias (Entity (Name (N)))) | |
1675 | and then Is_Enumeration_Type (Base_Type (Typ)) | |
1676 | then | |
1677 | Lit := Alias (Entity (Name (N))); | |
c01a9391 AC |
1678 | while Present (Alias (Lit)) loop |
1679 | Lit := Alias (Lit); | |
1680 | end loop; | |
1681 | ||
1682 | if Ekind (Lit) = E_Enumeration_Literal then | |
1683 | if Base_Type (Etype (Lit)) /= Base_Type (Typ) then | |
1684 | Rewrite | |
1685 | (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc))); | |
1686 | else | |
1687 | Rewrite (N, New_Occurrence_Of (Lit, Loc)); | |
1688 | end if; | |
1689 | ||
1690 | Resolve (N, Typ); | |
1691 | end if; | |
1692 | end if; | |
1693 | end Eval_Call; | |
1694 | ||
19d846a0 RD |
1695 | -------------------------- |
1696 | -- Eval_Case_Expression -- | |
1697 | -------------------------- | |
1698 | ||
1699 | -- Right now we do not attempt folding of any case expressions, and the | |
1700 | -- language does not require it, so the only required processing is to | |
1701 | -- do the check for all expressions appearing in the case expression. | |
1702 | ||
1703 | procedure Eval_Case_Expression (N : Node_Id) is | |
1704 | Alt : Node_Id; | |
1705 | ||
1706 | begin | |
1707 | Check_Non_Static_Context (Expression (N)); | |
1708 | ||
1709 | Alt := First (Alternatives (N)); | |
1710 | while Present (Alt) loop | |
1711 | Check_Non_Static_Context (Expression (Alt)); | |
1712 | Next (Alt); | |
1713 | end loop; | |
1714 | end Eval_Case_Expression; | |
1715 | ||
996ae0b0 RK |
1716 | ------------------------ |
1717 | -- Eval_Concatenation -- | |
1718 | ------------------------ | |
1719 | ||
3996951a TQ |
1720 | -- Concatenation is a static function, so the result is static if both |
1721 | -- operands are static (RM 4.9(7), 4.9(21)). | |
996ae0b0 RK |
1722 | |
1723 | procedure Eval_Concatenation (N : Node_Id) is | |
f91b40db GB |
1724 | Left : constant Node_Id := Left_Opnd (N); |
1725 | Right : constant Node_Id := Right_Opnd (N); | |
1726 | C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N))); | |
996ae0b0 RK |
1727 | Stat : Boolean; |
1728 | Fold : Boolean; | |
996ae0b0 RK |
1729 | |
1730 | begin | |
3996951a TQ |
1731 | -- Concatenation is never static in Ada 83, so if Ada 83 check operand |
1732 | -- non-static context. | |
996ae0b0 | 1733 | |
0ab80019 | 1734 | if Ada_Version = Ada_83 |
996ae0b0 RK |
1735 | and then Comes_From_Source (N) |
1736 | then | |
1737 | Check_Non_Static_Context (Left); | |
1738 | Check_Non_Static_Context (Right); | |
1739 | return; | |
1740 | end if; | |
1741 | ||
1742 | -- If not foldable we are done. In principle concatenation that yields | |
1743 | -- any string type is static (i.e. an array type of character types). | |
1744 | -- However, character types can include enumeration literals, and | |
1745 | -- concatenation in that case cannot be described by a literal, so we | |
1746 | -- only consider the operation static if the result is an array of | |
1747 | -- (a descendant of) a predefined character type. | |
1748 | ||
1749 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); | |
1750 | ||
3996951a | 1751 | if not (Is_Standard_Character_Type (C_Typ) and then Fold) then |
996ae0b0 RK |
1752 | Set_Is_Static_Expression (N, False); |
1753 | return; | |
1754 | end if; | |
1755 | ||
82c80734 | 1756 | -- Compile time string concatenation |
996ae0b0 | 1757 | |
3996951a TQ |
1758 | -- ??? Note that operands that are aggregates can be marked as static, |
1759 | -- so we should attempt at a later stage to fold concatenations with | |
1760 | -- such aggregates. | |
996ae0b0 RK |
1761 | |
1762 | declare | |
b54ddf5a BD |
1763 | Left_Str : constant Node_Id := Get_String_Val (Left); |
1764 | Left_Len : Nat; | |
1765 | Right_Str : constant Node_Id := Get_String_Val (Right); | |
1766 | Folded_Val : String_Id; | |
996ae0b0 RK |
1767 | |
1768 | begin | |
1769 | -- Establish new string literal, and store left operand. We make | |
1770 | -- sure to use the special Start_String that takes an operand if | |
1771 | -- the left operand is a string literal. Since this is optimized | |
1772 | -- in the case where that is the most recently created string | |
1773 | -- literal, we ensure efficient time/space behavior for the | |
1774 | -- case of a concatenation of a series of string literals. | |
1775 | ||
1776 | if Nkind (Left_Str) = N_String_Literal then | |
f91b40db | 1777 | Left_Len := String_Length (Strval (Left_Str)); |
b54ddf5a BD |
1778 | |
1779 | -- If the left operand is the empty string, and the right operand | |
1780 | -- is a string literal (the case of "" & "..."), the result is the | |
1781 | -- value of the right operand. This optimization is important when | |
1782 | -- Is_Folded_In_Parser, to avoid copying an enormous right | |
1783 | -- operand. | |
1784 | ||
1785 | if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then | |
1786 | Folded_Val := Strval (Right_Str); | |
1787 | else | |
1788 | Start_String (Strval (Left_Str)); | |
1789 | end if; | |
1790 | ||
996ae0b0 RK |
1791 | else |
1792 | Start_String; | |
82c80734 | 1793 | Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str))); |
f91b40db | 1794 | Left_Len := 1; |
996ae0b0 RK |
1795 | end if; |
1796 | ||
b54ddf5a BD |
1797 | -- Now append the characters of the right operand, unless we |
1798 | -- optimized the "" & "..." case above. | |
996ae0b0 RK |
1799 | |
1800 | if Nkind (Right_Str) = N_String_Literal then | |
b54ddf5a BD |
1801 | if Left_Len /= 0 then |
1802 | Store_String_Chars (Strval (Right_Str)); | |
1803 | Folded_Val := End_String; | |
1804 | end if; | |
996ae0b0 | 1805 | else |
82c80734 | 1806 | Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str))); |
b54ddf5a | 1807 | Folded_Val := End_String; |
996ae0b0 RK |
1808 | end if; |
1809 | ||
1810 | Set_Is_Static_Expression (N, Stat); | |
1811 | ||
1812 | if Stat then | |
f91b40db GB |
1813 | |
1814 | -- If left operand is the empty string, the result is the | |
1815 | -- right operand, including its bounds if anomalous. | |
1816 | ||
1817 | if Left_Len = 0 | |
1818 | and then Is_Array_Type (Etype (Right)) | |
1819 | and then Etype (Right) /= Any_String | |
1820 | then | |
1821 | Set_Etype (N, Etype (Right)); | |
1822 | end if; | |
1823 | ||
b54ddf5a | 1824 | Fold_Str (N, Folded_Val, Static => True); |
996ae0b0 RK |
1825 | end if; |
1826 | end; | |
1827 | end Eval_Concatenation; | |
1828 | ||
1829 | --------------------------------- | |
1830 | -- Eval_Conditional_Expression -- | |
1831 | --------------------------------- | |
1832 | ||
4d777a71 | 1833 | -- We can fold to a static expression if the condition and both constituent |
1cf3727f | 1834 | -- expressions are static. Otherwise, the only required processing is to do |
4d777a71 | 1835 | -- the check for non-static context for the then and else expressions. |
996ae0b0 RK |
1836 | |
1837 | procedure Eval_Conditional_Expression (N : Node_Id) is | |
4d777a71 AC |
1838 | Condition : constant Node_Id := First (Expressions (N)); |
1839 | Then_Expr : constant Node_Id := Next (Condition); | |
1840 | Else_Expr : constant Node_Id := Next (Then_Expr); | |
1841 | Result : Node_Id; | |
1842 | Non_Result : Node_Id; | |
1843 | ||
1844 | Rstat : constant Boolean := | |
1845 | Is_Static_Expression (Condition) | |
1846 | and then | |
1847 | Is_Static_Expression (Then_Expr) | |
1848 | and then | |
1849 | Is_Static_Expression (Else_Expr); | |
1850 | ||
996ae0b0 | 1851 | begin |
4d777a71 AC |
1852 | -- If any operand is Any_Type, just propagate to result and do not try |
1853 | -- to fold, this prevents cascaded errors. | |
1854 | ||
1855 | if Etype (Condition) = Any_Type or else | |
1856 | Etype (Then_Expr) = Any_Type or else | |
1857 | Etype (Else_Expr) = Any_Type | |
1858 | then | |
1859 | Set_Etype (N, Any_Type); | |
1860 | Set_Is_Static_Expression (N, False); | |
1861 | return; | |
1862 | ||
1863 | -- Static case where we can fold. Note that we don't try to fold cases | |
1864 | -- where the condition is known at compile time, but the result is | |
1865 | -- non-static. This avoids possible cases of infinite recursion where | |
1866 | -- the expander puts in a redundant test and we remove it. Instead we | |
1867 | -- deal with these cases in the expander. | |
1868 | ||
1869 | elsif Rstat then | |
1870 | ||
1871 | -- Select result operand | |
1872 | ||
1873 | if Is_True (Expr_Value (Condition)) then | |
1874 | Result := Then_Expr; | |
1875 | Non_Result := Else_Expr; | |
1876 | else | |
1877 | Result := Else_Expr; | |
1878 | Non_Result := Then_Expr; | |
1879 | end if; | |
1880 | ||
1881 | -- Note that it does not matter if the non-result operand raises a | |
1882 | -- Constraint_Error, but if the result raises constraint error then | |
1883 | -- we replace the node with a raise constraint error. This will | |
1884 | -- properly propagate Raises_Constraint_Error since this flag is | |
1885 | -- set in Result. | |
1886 | ||
1887 | if Raises_Constraint_Error (Result) then | |
1888 | Rewrite_In_Raise_CE (N, Result); | |
1889 | Check_Non_Static_Context (Non_Result); | |
1890 | ||
1891 | -- Otherwise the result operand replaces the original node | |
1892 | ||
1893 | else | |
1894 | Rewrite (N, Relocate_Node (Result)); | |
1895 | end if; | |
1896 | ||
1897 | -- Case of condition not known at compile time | |
1898 | ||
1899 | else | |
1900 | Check_Non_Static_Context (Condition); | |
1901 | Check_Non_Static_Context (Then_Expr); | |
1902 | Check_Non_Static_Context (Else_Expr); | |
1903 | end if; | |
1904 | ||
1905 | Set_Is_Static_Expression (N, Rstat); | |
996ae0b0 RK |
1906 | end Eval_Conditional_Expression; |
1907 | ||
1908 | ---------------------- | |
1909 | -- Eval_Entity_Name -- | |
1910 | ---------------------- | |
1911 | ||
1912 | -- This procedure is used for identifiers and expanded names other than | |
1913 | -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are | |
1914 | -- static if they denote a static constant (RM 4.9(6)) or if the name | |
1915 | -- denotes an enumeration literal (RM 4.9(22)). | |
1916 | ||
1917 | procedure Eval_Entity_Name (N : Node_Id) is | |
1918 | Def_Id : constant Entity_Id := Entity (N); | |
1919 | Val : Node_Id; | |
1920 | ||
1921 | begin | |
1922 | -- Enumeration literals are always considered to be constants | |
1923 | -- and cannot raise constraint error (RM 4.9(22)). | |
1924 | ||
1925 | if Ekind (Def_Id) = E_Enumeration_Literal then | |
1926 | Set_Is_Static_Expression (N); | |
1927 | return; | |
1928 | ||
1929 | -- A name is static if it denotes a static constant (RM 4.9(5)), and | |
1930 | -- we also copy Raise_Constraint_Error. Notice that even if non-static, | |
1931 | -- it does not violate 10.2.1(8) here, since this is not a variable. | |
1932 | ||
1933 | elsif Ekind (Def_Id) = E_Constant then | |
1934 | ||
1935 | -- Deferred constants must always be treated as nonstatic | |
1936 | -- outside the scope of their full view. | |
1937 | ||
1938 | if Present (Full_View (Def_Id)) | |
1939 | and then not In_Open_Scopes (Scope (Def_Id)) | |
1940 | then | |
1941 | Val := Empty; | |
1942 | else | |
1943 | Val := Constant_Value (Def_Id); | |
1944 | end if; | |
1945 | ||
1946 | if Present (Val) then | |
1947 | Set_Is_Static_Expression | |
1948 | (N, Is_Static_Expression (Val) | |
1949 | and then Is_Static_Subtype (Etype (Def_Id))); | |
1950 | Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val)); | |
1951 | ||
1952 | if not Is_Static_Expression (N) | |
1953 | and then not Is_Generic_Type (Etype (N)) | |
1954 | then | |
1955 | Validate_Static_Object_Name (N); | |
1956 | end if; | |
1957 | ||
1958 | return; | |
1959 | end if; | |
1960 | end if; | |
1961 | ||
82c80734 | 1962 | -- Fall through if the name is not static |
996ae0b0 RK |
1963 | |
1964 | Validate_Static_Object_Name (N); | |
1965 | end Eval_Entity_Name; | |
1966 | ||
1967 | ---------------------------- | |
1968 | -- Eval_Indexed_Component -- | |
1969 | ---------------------------- | |
1970 | ||
8cbb664e MG |
1971 | -- Indexed components are never static, so we need to perform the check |
1972 | -- for non-static context on the index values. Then, we check if the | |
1973 | -- value can be obtained at compile time, even though it is non-static. | |
996ae0b0 RK |
1974 | |
1975 | procedure Eval_Indexed_Component (N : Node_Id) is | |
1976 | Expr : Node_Id; | |
1977 | ||
1978 | begin | |
fbf5a39b AC |
1979 | -- Check for non-static context on index values |
1980 | ||
996ae0b0 RK |
1981 | Expr := First (Expressions (N)); |
1982 | while Present (Expr) loop | |
1983 | Check_Non_Static_Context (Expr); | |
1984 | Next (Expr); | |
1985 | end loop; | |
1986 | ||
fbf5a39b AC |
1987 | -- If the indexed component appears in an object renaming declaration |
1988 | -- then we do not want to try to evaluate it, since in this case we | |
1989 | -- need the identity of the array element. | |
1990 | ||
1991 | if Nkind (Parent (N)) = N_Object_Renaming_Declaration then | |
1992 | return; | |
1993 | ||
1994 | -- Similarly if the indexed component appears as the prefix of an | |
1995 | -- attribute we don't want to evaluate it, because at least for | |
1996 | -- some cases of attributes we need the identify (e.g. Access, Size) | |
1997 | ||
1998 | elsif Nkind (Parent (N)) = N_Attribute_Reference then | |
1999 | return; | |
2000 | end if; | |
2001 | ||
2002 | -- Note: there are other cases, such as the left side of an assignment, | |
2003 | -- or an OUT parameter for a call, where the replacement results in the | |
2004 | -- illegal use of a constant, But these cases are illegal in the first | |
2005 | -- place, so the replacement, though silly, is harmless. | |
2006 | ||
2007 | -- Now see if this is a constant array reference | |
8cbb664e MG |
2008 | |
2009 | if List_Length (Expressions (N)) = 1 | |
2010 | and then Is_Entity_Name (Prefix (N)) | |
2011 | and then Ekind (Entity (Prefix (N))) = E_Constant | |
2012 | and then Present (Constant_Value (Entity (Prefix (N)))) | |
2013 | then | |
2014 | declare | |
2015 | Loc : constant Source_Ptr := Sloc (N); | |
2016 | Arr : constant Node_Id := Constant_Value (Entity (Prefix (N))); | |
2017 | Sub : constant Node_Id := First (Expressions (N)); | |
2018 | ||
2019 | Atyp : Entity_Id; | |
2020 | -- Type of array | |
2021 | ||
2022 | Lin : Nat; | |
2023 | -- Linear one's origin subscript value for array reference | |
2024 | ||
2025 | Lbd : Node_Id; | |
2026 | -- Lower bound of the first array index | |
2027 | ||
2028 | Elm : Node_Id; | |
2029 | -- Value from constant array | |
2030 | ||
2031 | begin | |
2032 | Atyp := Etype (Arr); | |
2033 | ||
2034 | if Is_Access_Type (Atyp) then | |
2035 | Atyp := Designated_Type (Atyp); | |
2036 | end if; | |
2037 | ||
9dbf1c3e RD |
2038 | -- If we have an array type (we should have but perhaps there are |
2039 | -- error cases where this is not the case), then see if we can do | |
2040 | -- a constant evaluation of the array reference. | |
8cbb664e | 2041 | |
ebd34478 | 2042 | if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then |
8cbb664e MG |
2043 | if Ekind (Atyp) = E_String_Literal_Subtype then |
2044 | Lbd := String_Literal_Low_Bound (Atyp); | |
2045 | else | |
2046 | Lbd := Type_Low_Bound (Etype (First_Index (Atyp))); | |
2047 | end if; | |
2048 | ||
2049 | if Compile_Time_Known_Value (Sub) | |
2050 | and then Nkind (Arr) = N_Aggregate | |
2051 | and then Compile_Time_Known_Value (Lbd) | |
2052 | and then Is_Discrete_Type (Component_Type (Atyp)) | |
2053 | then | |
2054 | Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1; | |
2055 | ||
2056 | if List_Length (Expressions (Arr)) >= Lin then | |
2057 | Elm := Pick (Expressions (Arr), Lin); | |
2058 | ||
2059 | -- If the resulting expression is compile time known, | |
2060 | -- then we can rewrite the indexed component with this | |
2061 | -- value, being sure to mark the result as non-static. | |
2062 | -- We also reset the Sloc, in case this generates an | |
2063 | -- error later on (e.g. 136'Access). | |
2064 | ||
2065 | if Compile_Time_Known_Value (Elm) then | |
2066 | Rewrite (N, Duplicate_Subexpr_No_Checks (Elm)); | |
2067 | Set_Is_Static_Expression (N, False); | |
2068 | Set_Sloc (N, Loc); | |
2069 | end if; | |
2070 | end if; | |
9fbb3ae6 AC |
2071 | |
2072 | -- We can also constant-fold if the prefix is a string literal. | |
2073 | -- This will be useful in an instantiation or an inlining. | |
2074 | ||
2075 | elsif Compile_Time_Known_Value (Sub) | |
2076 | and then Nkind (Arr) = N_String_Literal | |
2077 | and then Compile_Time_Known_Value (Lbd) | |
2078 | and then Expr_Value (Lbd) = 1 | |
2079 | and then Expr_Value (Sub) <= | |
2080 | String_Literal_Length (Etype (Arr)) | |
2081 | then | |
2082 | declare | |
2083 | C : constant Char_Code := | |
2084 | Get_String_Char (Strval (Arr), | |
2085 | UI_To_Int (Expr_Value (Sub))); | |
2086 | begin | |
2087 | Set_Character_Literal_Name (C); | |
2088 | ||
2089 | Elm := | |
2090 | Make_Character_Literal (Loc, | |
2091 | Chars => Name_Find, | |
2092 | Char_Literal_Value => UI_From_CC (C)); | |
2093 | Set_Etype (Elm, Component_Type (Atyp)); | |
2094 | Rewrite (N, Duplicate_Subexpr_No_Checks (Elm)); | |
2095 | Set_Is_Static_Expression (N, False); | |
2096 | end; | |
8cbb664e MG |
2097 | end if; |
2098 | end if; | |
2099 | end; | |
2100 | end if; | |
996ae0b0 RK |
2101 | end Eval_Indexed_Component; |
2102 | ||
2103 | -------------------------- | |
2104 | -- Eval_Integer_Literal -- | |
2105 | -------------------------- | |
2106 | ||
2107 | -- Numeric literals are static (RM 4.9(1)), and have already been marked | |
2108 | -- as static by the analyzer. The reason we did it that early is to allow | |
2109 | -- the possibility of turning off the Is_Static_Expression flag after | |
9dbf1c3e RD |
2110 | -- analysis, but before resolution, when integer literals are generated in |
2111 | -- the expander that do not correspond to static expressions. | |
996ae0b0 RK |
2112 | |
2113 | procedure Eval_Integer_Literal (N : Node_Id) is | |
2114 | T : constant Entity_Id := Etype (N); | |
2115 | ||
5d09245e | 2116 | function In_Any_Integer_Context return Boolean; |
1d1bd8ad AC |
2117 | -- If the literal is resolved with a specific type in a context where |
2118 | -- the expected type is Any_Integer, there are no range checks on the | |
2119 | -- literal. By the time the literal is evaluated, it carries the type | |
2120 | -- imposed by the enclosing expression, and we must recover the context | |
2121 | -- to determine that Any_Integer is meant. | |
5d09245e AC |
2122 | |
2123 | ---------------------------- | |
09494c32 | 2124 | -- In_Any_Integer_Context -- |
5d09245e AC |
2125 | ---------------------------- |
2126 | ||
2127 | function In_Any_Integer_Context return Boolean is | |
2128 | Par : constant Node_Id := Parent (N); | |
2129 | K : constant Node_Kind := Nkind (Par); | |
2130 | ||
2131 | begin | |
2132 | -- Any_Integer also appears in digits specifications for real types, | |
1d1bd8ad AC |
2133 | -- but those have bounds smaller that those of any integer base type, |
2134 | -- so we can safely ignore these cases. | |
5d09245e AC |
2135 | |
2136 | return K = N_Number_Declaration | |
2137 | or else K = N_Attribute_Reference | |
2138 | or else K = N_Attribute_Definition_Clause | |
2139 | or else K = N_Modular_Type_Definition | |
2140 | or else K = N_Signed_Integer_Type_Definition; | |
2141 | end In_Any_Integer_Context; | |
2142 | ||
2143 | -- Start of processing for Eval_Integer_Literal | |
2144 | ||
996ae0b0 | 2145 | begin |
5d09245e | 2146 | |
996ae0b0 | 2147 | -- If the literal appears in a non-expression context, then it is |
1d1bd8ad AC |
2148 | -- certainly appearing in a non-static context, so check it. This is |
2149 | -- actually a redundant check, since Check_Non_Static_Context would | |
2150 | -- check it, but it seems worth while avoiding the call. | |
996ae0b0 | 2151 | |
5d09245e AC |
2152 | if Nkind (Parent (N)) not in N_Subexpr |
2153 | and then not In_Any_Integer_Context | |
2154 | then | |
996ae0b0 RK |
2155 | Check_Non_Static_Context (N); |
2156 | end if; | |
2157 | ||
2158 | -- Modular integer literals must be in their base range | |
2159 | ||
2160 | if Is_Modular_Integer_Type (T) | |
c800f862 | 2161 | and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) |
996ae0b0 RK |
2162 | then |
2163 | Out_Of_Range (N); | |
2164 | end if; | |
2165 | end Eval_Integer_Literal; | |
2166 | ||
2167 | --------------------- | |
2168 | -- Eval_Logical_Op -- | |
2169 | --------------------- | |
2170 | ||
2171 | -- Logical operations are static functions, so the result is potentially | |
2172 | -- static if both operands are potentially static (RM 4.9(7), 4.9(20)). | |
2173 | ||
2174 | procedure Eval_Logical_Op (N : Node_Id) is | |
2175 | Left : constant Node_Id := Left_Opnd (N); | |
2176 | Right : constant Node_Id := Right_Opnd (N); | |
2177 | Stat : Boolean; | |
2178 | Fold : Boolean; | |
2179 | ||
2180 | begin | |
2181 | -- If not foldable we are done | |
2182 | ||
2183 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); | |
2184 | ||
2185 | if not Fold then | |
2186 | return; | |
2187 | end if; | |
2188 | ||
2189 | -- Compile time evaluation of logical operation | |
2190 | ||
2191 | declare | |
2192 | Left_Int : constant Uint := Expr_Value (Left); | |
2193 | Right_Int : constant Uint := Expr_Value (Right); | |
2194 | ||
2195 | begin | |
22cb89b5 | 2196 | -- VMS includes bitwise operations on signed types |
001c7783 AC |
2197 | |
2198 | if Is_Modular_Integer_Type (Etype (N)) | |
2199 | or else Is_VMS_Operator (Entity (N)) | |
2200 | then | |
996ae0b0 RK |
2201 | declare |
2202 | Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1); | |
2203 | Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1); | |
2204 | ||
2205 | begin | |
2206 | To_Bits (Left_Int, Left_Bits); | |
2207 | To_Bits (Right_Int, Right_Bits); | |
2208 | ||
2209 | -- Note: should really be able to use array ops instead of | |
2210 | -- these loops, but they weren't working at the time ??? | |
2211 | ||
2212 | if Nkind (N) = N_Op_And then | |
2213 | for J in Left_Bits'Range loop | |
2214 | Left_Bits (J) := Left_Bits (J) and Right_Bits (J); | |
2215 | end loop; | |
2216 | ||
2217 | elsif Nkind (N) = N_Op_Or then | |
2218 | for J in Left_Bits'Range loop | |
2219 | Left_Bits (J) := Left_Bits (J) or Right_Bits (J); | |
2220 | end loop; | |
2221 | ||
2222 | else | |
2223 | pragma Assert (Nkind (N) = N_Op_Xor); | |
2224 | ||
2225 | for J in Left_Bits'Range loop | |
2226 | Left_Bits (J) := Left_Bits (J) xor Right_Bits (J); | |
2227 | end loop; | |
2228 | end if; | |
2229 | ||
fbf5a39b | 2230 | Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat); |
996ae0b0 RK |
2231 | end; |
2232 | ||
2233 | else | |
2234 | pragma Assert (Is_Boolean_Type (Etype (N))); | |
2235 | ||
2236 | if Nkind (N) = N_Op_And then | |
2237 | Fold_Uint (N, | |
fbf5a39b | 2238 | Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat); |
996ae0b0 RK |
2239 | |
2240 | elsif Nkind (N) = N_Op_Or then | |
2241 | Fold_Uint (N, | |
fbf5a39b | 2242 | Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat); |
996ae0b0 RK |
2243 | |
2244 | else | |
2245 | pragma Assert (Nkind (N) = N_Op_Xor); | |
2246 | Fold_Uint (N, | |
fbf5a39b | 2247 | Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat); |
996ae0b0 RK |
2248 | end if; |
2249 | end if; | |
996ae0b0 RK |
2250 | end; |
2251 | end Eval_Logical_Op; | |
2252 | ||
2253 | ------------------------ | |
2254 | -- Eval_Membership_Op -- | |
2255 | ------------------------ | |
2256 | ||
1d1bd8ad AC |
2257 | -- A membership test is potentially static if the expression is static, and |
2258 | -- the range is a potentially static range, or is a subtype mark denoting a | |
2259 | -- static subtype (RM 4.9(12)). | |
996ae0b0 RK |
2260 | |
2261 | procedure Eval_Membership_Op (N : Node_Id) is | |
2262 | Left : constant Node_Id := Left_Opnd (N); | |
2263 | Right : constant Node_Id := Right_Opnd (N); | |
2264 | Def_Id : Entity_Id; | |
2265 | Lo : Node_Id; | |
2266 | Hi : Node_Id; | |
2267 | Result : Boolean; | |
2268 | Stat : Boolean; | |
2269 | Fold : Boolean; | |
2270 | ||
2271 | begin | |
1d1bd8ad AC |
2272 | -- Ignore if error in either operand, except to make sure that Any_Type |
2273 | -- is properly propagated to avoid junk cascaded errors. | |
996ae0b0 | 2274 | |
22cb89b5 | 2275 | if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then |
996ae0b0 RK |
2276 | Set_Etype (N, Any_Type); |
2277 | return; | |
2278 | end if; | |
2279 | ||
2280 | -- Case of right operand is a subtype name | |
2281 | ||
2282 | if Is_Entity_Name (Right) then | |
2283 | Def_Id := Entity (Right); | |
2284 | ||
2285 | if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id)) | |
2286 | and then Is_OK_Static_Subtype (Def_Id) | |
2287 | then | |
2288 | Test_Expression_Is_Foldable (N, Left, Stat, Fold); | |
2289 | ||
2290 | if not Fold or else not Stat then | |
2291 | return; | |
2292 | end if; | |
2293 | else | |
2294 | Check_Non_Static_Context (Left); | |
2295 | return; | |
2296 | end if; | |
2297 | ||
1d1bd8ad | 2298 | -- For string membership tests we will check the length further on |
996ae0b0 RK |
2299 | |
2300 | if not Is_String_Type (Def_Id) then | |
2301 | Lo := Type_Low_Bound (Def_Id); | |
2302 | Hi := Type_High_Bound (Def_Id); | |
2303 | ||
2304 | else | |
2305 | Lo := Empty; | |
2306 | Hi := Empty; | |
2307 | end if; | |
2308 | ||
2309 | -- Case of right operand is a range | |
2310 | ||
2311 | else | |
2312 | if Is_Static_Range (Right) then | |
2313 | Test_Expression_Is_Foldable (N, Left, Stat, Fold); | |
2314 | ||
2315 | if not Fold or else not Stat then | |
2316 | return; | |
2317 | ||
2318 | -- If one bound of range raises CE, then don't try to fold | |
2319 | ||
2320 | elsif not Is_OK_Static_Range (Right) then | |
2321 | Check_Non_Static_Context (Left); | |
2322 | return; | |
2323 | end if; | |
2324 | ||
2325 | else | |
2326 | Check_Non_Static_Context (Left); | |
2327 | return; | |
2328 | end if; | |
2329 | ||
2330 | -- Here we know range is an OK static range | |
2331 | ||
2332 | Lo := Low_Bound (Right); | |
2333 | Hi := High_Bound (Right); | |
2334 | end if; | |
2335 | ||
2336 | -- For strings we check that the length of the string expression is | |
2337 | -- compatible with the string subtype if the subtype is constrained, | |
2338 | -- or if unconstrained then the test is always true. | |
2339 | ||
2340 | if Is_String_Type (Etype (Right)) then | |
2341 | if not Is_Constrained (Etype (Right)) then | |
2342 | Result := True; | |
2343 | ||
2344 | else | |
2345 | declare | |
2346 | Typlen : constant Uint := String_Type_Len (Etype (Right)); | |
2347 | Strlen : constant Uint := | |
22cb89b5 AC |
2348 | UI_From_Int |
2349 | (String_Length (Strval (Get_String_Val (Left)))); | |
996ae0b0 RK |
2350 | begin |
2351 | Result := (Typlen = Strlen); | |
2352 | end; | |
2353 | end if; | |
2354 | ||
1d1bd8ad AC |
2355 | -- Fold the membership test. We know we have a static range and Lo and |
2356 | -- Hi are set to the expressions for the end points of this range. | |
996ae0b0 RK |
2357 | |
2358 | elsif Is_Real_Type (Etype (Right)) then | |
2359 | declare | |
2360 | Leftval : constant Ureal := Expr_Value_R (Left); | |
2361 | ||
2362 | begin | |
2363 | Result := Expr_Value_R (Lo) <= Leftval | |
2364 | and then Leftval <= Expr_Value_R (Hi); | |
2365 | end; | |
2366 | ||
2367 | else | |
2368 | declare | |
2369 | Leftval : constant Uint := Expr_Value (Left); | |
2370 | ||
2371 | begin | |
2372 | Result := Expr_Value (Lo) <= Leftval | |
2373 | and then Leftval <= Expr_Value (Hi); | |
2374 | end; | |
2375 | end if; | |
2376 | ||
2377 | if Nkind (N) = N_Not_In then | |
2378 | Result := not Result; | |
2379 | end if; | |
2380 | ||
fbf5a39b | 2381 | Fold_Uint (N, Test (Result), True); |
d7567964 | 2382 | |
996ae0b0 | 2383 | Warn_On_Known_Condition (N); |
996ae0b0 RK |
2384 | end Eval_Membership_Op; |
2385 | ||
2386 | ------------------------ | |
2387 | -- Eval_Named_Integer -- | |
2388 | ------------------------ | |
2389 | ||
2390 | procedure Eval_Named_Integer (N : Node_Id) is | |
2391 | begin | |
2392 | Fold_Uint (N, | |
fbf5a39b | 2393 | Expr_Value (Expression (Declaration_Node (Entity (N)))), True); |
996ae0b0 RK |
2394 | end Eval_Named_Integer; |
2395 | ||
2396 | --------------------- | |
2397 | -- Eval_Named_Real -- | |
2398 | --------------------- | |
2399 | ||
2400 | procedure Eval_Named_Real (N : Node_Id) is | |
2401 | begin | |
2402 | Fold_Ureal (N, | |
fbf5a39b | 2403 | Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True); |
996ae0b0 RK |
2404 | end Eval_Named_Real; |
2405 | ||
2406 | ------------------- | |
2407 | -- Eval_Op_Expon -- | |
2408 | ------------------- | |
2409 | ||
2410 | -- Exponentiation is a static functions, so the result is potentially | |
2411 | -- static if both operands are potentially static (RM 4.9(7), 4.9(20)). | |
2412 | ||
2413 | procedure Eval_Op_Expon (N : Node_Id) is | |
2414 | Left : constant Node_Id := Left_Opnd (N); | |
2415 | Right : constant Node_Id := Right_Opnd (N); | |
2416 | Stat : Boolean; | |
2417 | Fold : Boolean; | |
2418 | ||
2419 | begin | |
2420 | -- If not foldable we are done | |
2421 | ||
2422 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); | |
2423 | ||
2424 | if not Fold then | |
2425 | return; | |
2426 | end if; | |
2427 | ||
2428 | -- Fold exponentiation operation | |
2429 | ||
2430 | declare | |
2431 | Right_Int : constant Uint := Expr_Value (Right); | |
2432 | ||
2433 | begin | |
2434 | -- Integer case | |
2435 | ||
2436 | if Is_Integer_Type (Etype (Left)) then | |
2437 | declare | |
2438 | Left_Int : constant Uint := Expr_Value (Left); | |
2439 | Result : Uint; | |
2440 | ||
2441 | begin | |
22cb89b5 AC |
2442 | -- Exponentiation of an integer raises Constraint_Error for a |
2443 | -- negative exponent (RM 4.5.6). | |
996ae0b0 RK |
2444 | |
2445 | if Right_Int < 0 then | |
2446 | Apply_Compile_Time_Constraint_Error | |
fbf5a39b AC |
2447 | (N, "integer exponent negative", |
2448 | CE_Range_Check_Failed, | |
2449 | Warn => not Stat); | |
996ae0b0 RK |
2450 | return; |
2451 | ||
2452 | else | |
2453 | if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then | |
2454 | Result := Left_Int ** Right_Int; | |
2455 | else | |
2456 | Result := Left_Int; | |
2457 | end if; | |
2458 | ||
2459 | if Is_Modular_Integer_Type (Etype (N)) then | |
2460 | Result := Result mod Modulus (Etype (N)); | |
2461 | end if; | |
2462 | ||
fbf5a39b | 2463 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
2464 | end if; |
2465 | end; | |
2466 | ||
2467 | -- Real case | |
2468 | ||
2469 | else | |
2470 | declare | |
2471 | Left_Real : constant Ureal := Expr_Value_R (Left); | |
2472 | ||
2473 | begin | |
2474 | -- Cannot have a zero base with a negative exponent | |
2475 | ||
2476 | if UR_Is_Zero (Left_Real) then | |
2477 | ||
2478 | if Right_Int < 0 then | |
2479 | Apply_Compile_Time_Constraint_Error | |
fbf5a39b AC |
2480 | (N, "zero ** negative integer", |
2481 | CE_Range_Check_Failed, | |
2482 | Warn => not Stat); | |
996ae0b0 RK |
2483 | return; |
2484 | else | |
fbf5a39b | 2485 | Fold_Ureal (N, Ureal_0, Stat); |
996ae0b0 RK |
2486 | end if; |
2487 | ||
2488 | else | |
fbf5a39b | 2489 | Fold_Ureal (N, Left_Real ** Right_Int, Stat); |
996ae0b0 RK |
2490 | end if; |
2491 | end; | |
2492 | end if; | |
996ae0b0 RK |
2493 | end; |
2494 | end Eval_Op_Expon; | |
2495 | ||
2496 | ----------------- | |
2497 | -- Eval_Op_Not -- | |
2498 | ----------------- | |
2499 | ||
2500 | -- The not operation is a static functions, so the result is potentially | |
2501 | -- static if the operand is potentially static (RM 4.9(7), 4.9(20)). | |
2502 | ||
2503 | procedure Eval_Op_Not (N : Node_Id) is | |
2504 | Right : constant Node_Id := Right_Opnd (N); | |
2505 | Stat : Boolean; | |
2506 | Fold : Boolean; | |
2507 | ||
2508 | begin | |
2509 | -- If not foldable we are done | |
2510 | ||
2511 | Test_Expression_Is_Foldable (N, Right, Stat, Fold); | |
2512 | ||
2513 | if not Fold then | |
2514 | return; | |
2515 | end if; | |
2516 | ||
2517 | -- Fold not operation | |
2518 | ||
2519 | declare | |
2520 | Rint : constant Uint := Expr_Value (Right); | |
2521 | Typ : constant Entity_Id := Etype (N); | |
2522 | ||
2523 | begin | |
1d1bd8ad AC |
2524 | -- Negation is equivalent to subtracting from the modulus minus one. |
2525 | -- For a binary modulus this is equivalent to the ones-complement of | |
2526 | -- the original value. For non-binary modulus this is an arbitrary | |
2527 | -- but consistent definition. | |
996ae0b0 RK |
2528 | |
2529 | if Is_Modular_Integer_Type (Typ) then | |
fbf5a39b | 2530 | Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat); |
996ae0b0 RK |
2531 | |
2532 | else | |
2533 | pragma Assert (Is_Boolean_Type (Typ)); | |
fbf5a39b | 2534 | Fold_Uint (N, Test (not Is_True (Rint)), Stat); |
996ae0b0 RK |
2535 | end if; |
2536 | ||
2537 | Set_Is_Static_Expression (N, Stat); | |
2538 | end; | |
2539 | end Eval_Op_Not; | |
2540 | ||
2541 | ------------------------------- | |
2542 | -- Eval_Qualified_Expression -- | |
2543 | ------------------------------- | |
2544 | ||
2545 | -- A qualified expression is potentially static if its subtype mark denotes | |
2546 | -- a static subtype and its expression is potentially static (RM 4.9 (11)). | |
2547 | ||
2548 | procedure Eval_Qualified_Expression (N : Node_Id) is | |
2549 | Operand : constant Node_Id := Expression (N); | |
2550 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
2551 | ||
07fc65c4 GB |
2552 | Stat : Boolean; |
2553 | Fold : Boolean; | |
2554 | Hex : Boolean; | |
996ae0b0 RK |
2555 | |
2556 | begin | |
1d1bd8ad | 2557 | -- Can only fold if target is string or scalar and subtype is static. |
22cb89b5 AC |
2558 | -- Also, do not fold if our parent is an allocator (this is because the |
2559 | -- qualified expression is really part of the syntactic structure of an | |
2560 | -- allocator, and we do not want to end up with something that | |
996ae0b0 RK |
2561 | -- corresponds to "new 1" where the 1 is the result of folding a |
2562 | -- qualified expression). | |
2563 | ||
2564 | if not Is_Static_Subtype (Target_Type) | |
2565 | or else Nkind (Parent (N)) = N_Allocator | |
2566 | then | |
2567 | Check_Non_Static_Context (Operand); | |
af152989 | 2568 | |
1d1bd8ad AC |
2569 | -- If operand is known to raise constraint_error, set the flag on the |
2570 | -- expression so it does not get optimized away. | |
af152989 AC |
2571 | |
2572 | if Nkind (Operand) = N_Raise_Constraint_Error then | |
2573 | Set_Raises_Constraint_Error (N); | |
2574 | end if; | |
7324bf49 | 2575 | |
996ae0b0 RK |
2576 | return; |
2577 | end if; | |
2578 | ||
2579 | -- If not foldable we are done | |
2580 | ||
2581 | Test_Expression_Is_Foldable (N, Operand, Stat, Fold); | |
2582 | ||
2583 | if not Fold then | |
2584 | return; | |
2585 | ||
2586 | -- Don't try fold if target type has constraint error bounds | |
2587 | ||
2588 | elsif not Is_OK_Static_Subtype (Target_Type) then | |
2589 | Set_Raises_Constraint_Error (N); | |
2590 | return; | |
2591 | end if; | |
2592 | ||
07fc65c4 GB |
2593 | -- Here we will fold, save Print_In_Hex indication |
2594 | ||
2595 | Hex := Nkind (Operand) = N_Integer_Literal | |
2596 | and then Print_In_Hex (Operand); | |
2597 | ||
996ae0b0 RK |
2598 | -- Fold the result of qualification |
2599 | ||
2600 | if Is_Discrete_Type (Target_Type) then | |
fbf5a39b | 2601 | Fold_Uint (N, Expr_Value (Operand), Stat); |
996ae0b0 | 2602 | |
07fc65c4 GB |
2603 | -- Preserve Print_In_Hex indication |
2604 | ||
2605 | if Hex and then Nkind (N) = N_Integer_Literal then | |
2606 | Set_Print_In_Hex (N); | |
2607 | end if; | |
2608 | ||
996ae0b0 | 2609 | elsif Is_Real_Type (Target_Type) then |
fbf5a39b | 2610 | Fold_Ureal (N, Expr_Value_R (Operand), Stat); |
996ae0b0 RK |
2611 | |
2612 | else | |
fbf5a39b | 2613 | Fold_Str (N, Strval (Get_String_Val (Operand)), Stat); |
996ae0b0 RK |
2614 | |
2615 | if not Stat then | |
2616 | Set_Is_Static_Expression (N, False); | |
2617 | else | |
2618 | Check_String_Literal_Length (N, Target_Type); | |
2619 | end if; | |
2620 | ||
2621 | return; | |
2622 | end if; | |
2623 | ||
fbf5a39b AC |
2624 | -- The expression may be foldable but not static |
2625 | ||
2626 | Set_Is_Static_Expression (N, Stat); | |
2627 | ||
c800f862 | 2628 | if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then |
996ae0b0 RK |
2629 | Out_Of_Range (N); |
2630 | end if; | |
996ae0b0 RK |
2631 | end Eval_Qualified_Expression; |
2632 | ||
2633 | ----------------------- | |
2634 | -- Eval_Real_Literal -- | |
2635 | ----------------------- | |
2636 | ||
2637 | -- Numeric literals are static (RM 4.9(1)), and have already been marked | |
2638 | -- as static by the analyzer. The reason we did it that early is to allow | |
2639 | -- the possibility of turning off the Is_Static_Expression flag after | |
2640 | -- analysis, but before resolution, when integer literals are generated | |
2641 | -- in the expander that do not correspond to static expressions. | |
2642 | ||
2643 | procedure Eval_Real_Literal (N : Node_Id) is | |
a1980be8 GB |
2644 | PK : constant Node_Kind := Nkind (Parent (N)); |
2645 | ||
996ae0b0 | 2646 | begin |
1d1bd8ad AC |
2647 | -- If the literal appears in a non-expression context and not as part of |
2648 | -- a number declaration, then it is appearing in a non-static context, | |
2649 | -- so check it. | |
996ae0b0 | 2650 | |
a1980be8 | 2651 | if PK not in N_Subexpr and then PK /= N_Number_Declaration then |
996ae0b0 RK |
2652 | Check_Non_Static_Context (N); |
2653 | end if; | |
996ae0b0 RK |
2654 | end Eval_Real_Literal; |
2655 | ||
2656 | ------------------------ | |
2657 | -- Eval_Relational_Op -- | |
2658 | ------------------------ | |
2659 | ||
8a95f4e8 RD |
2660 | -- Relational operations are static functions, so the result is static if |
2661 | -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings, | |
2662 | -- the result is never static, even if the operands are. | |
996ae0b0 RK |
2663 | |
2664 | procedure Eval_Relational_Op (N : Node_Id) is | |
2665 | Left : constant Node_Id := Left_Opnd (N); | |
2666 | Right : constant Node_Id := Right_Opnd (N); | |
2667 | Typ : constant Entity_Id := Etype (Left); | |
d7567964 | 2668 | Otype : Entity_Id := Empty; |
996ae0b0 RK |
2669 | Result : Boolean; |
2670 | Stat : Boolean; | |
2671 | Fold : Boolean; | |
2672 | ||
2673 | begin | |
45fc7ddb HK |
2674 | -- One special case to deal with first. If we can tell that the result |
2675 | -- will be false because the lengths of one or more index subtypes are | |
2676 | -- compile time known and different, then we can replace the entire | |
2677 | -- result by False. We only do this for one dimensional arrays, because | |
2678 | -- the case of multi-dimensional arrays is rare and too much trouble! If | |
2679 | -- one of the operands is an illegal aggregate, its type might still be | |
2680 | -- an arbitrary composite type, so nothing to do. | |
996ae0b0 RK |
2681 | |
2682 | if Is_Array_Type (Typ) | |
13f34a3f | 2683 | and then Typ /= Any_Composite |
996ae0b0 | 2684 | and then Number_Dimensions (Typ) = 1 |
13f34a3f | 2685 | and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne) |
996ae0b0 RK |
2686 | then |
2687 | if Raises_Constraint_Error (Left) | |
2688 | or else Raises_Constraint_Error (Right) | |
2689 | then | |
2690 | return; | |
2691 | end if; | |
2692 | ||
45fc7ddb HK |
2693 | -- OK, we have the case where we may be able to do this fold |
2694 | ||
2695 | Length_Mismatch : declare | |
996ae0b0 | 2696 | procedure Get_Static_Length (Op : Node_Id; Len : out Uint); |
1d1bd8ad AC |
2697 | -- If Op is an expression for a constrained array with a known at |
2698 | -- compile time length, then Len is set to this (non-negative | |
13f34a3f | 2699 | -- length). Otherwise Len is set to minus 1. |
996ae0b0 | 2700 | |
fbf5a39b AC |
2701 | ----------------------- |
2702 | -- Get_Static_Length -- | |
2703 | ----------------------- | |
2704 | ||
996ae0b0 RK |
2705 | procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is |
2706 | T : Entity_Id; | |
2707 | ||
2708 | begin | |
45fc7ddb HK |
2709 | -- First easy case string literal |
2710 | ||
996ae0b0 RK |
2711 | if Nkind (Op) = N_String_Literal then |
2712 | Len := UI_From_Int (String_Length (Strval (Op))); | |
45fc7ddb HK |
2713 | return; |
2714 | end if; | |
2715 | ||
2716 | -- Second easy case, not constrained subtype, so no length | |
996ae0b0 | 2717 | |
45fc7ddb | 2718 | if not Is_Constrained (Etype (Op)) then |
996ae0b0 | 2719 | Len := Uint_Minus_1; |
45fc7ddb HK |
2720 | return; |
2721 | end if; | |
996ae0b0 | 2722 | |
45fc7ddb HK |
2723 | -- General case |
2724 | ||
2725 | T := Etype (First_Index (Etype (Op))); | |
2726 | ||
2727 | -- The simple case, both bounds are known at compile time | |
2728 | ||
2729 | if Is_Discrete_Type (T) | |
2730 | and then | |
2731 | Compile_Time_Known_Value (Type_Low_Bound (T)) | |
2732 | and then | |
2733 | Compile_Time_Known_Value (Type_High_Bound (T)) | |
2734 | then | |
2735 | Len := UI_Max (Uint_0, | |
2736 | Expr_Value (Type_High_Bound (T)) - | |
2737 | Expr_Value (Type_Low_Bound (T)) + 1); | |
2738 | return; | |
2739 | end if; | |
2740 | ||
2741 | -- A more complex case, where the bounds are of the form | |
2742 | -- X [+/- K1] .. X [+/- K2]), where X is an expression that is | |
2743 | -- either A'First or A'Last (with A an entity name), or X is an | |
2744 | -- entity name, and the two X's are the same and K1 and K2 are | |
2745 | -- known at compile time, in this case, the length can also be | |
2746 | -- computed at compile time, even though the bounds are not | |
22cb89b5 | 2747 | -- known. A common case of this is e.g. (X'First .. X'First+5). |
45fc7ddb HK |
2748 | |
2749 | Extract_Length : declare | |
2750 | procedure Decompose_Expr | |
2751 | (Expr : Node_Id; | |
2752 | Ent : out Entity_Id; | |
2753 | Kind : out Character; | |
2754 | Cons : out Uint); | |
2755 | -- Given an expression, see if is of the form above, | |
2756 | -- X [+/- K]. If so Ent is set to the entity in X, | |
2757 | -- Kind is 'F','L','E' for 'First/'Last/simple entity, | |
2758 | -- and Cons is the value of K. If the expression is | |
2759 | -- not of the required form, Ent is set to Empty. | |
2760 | ||
2761 | -------------------- | |
2762 | -- Decompose_Expr -- | |
2763 | -------------------- | |
2764 | ||
2765 | procedure Decompose_Expr | |
2766 | (Expr : Node_Id; | |
2767 | Ent : out Entity_Id; | |
2768 | Kind : out Character; | |
2769 | Cons : out Uint) | |
2770 | is | |
2771 | Exp : Node_Id; | |
2772 | ||
2773 | begin | |
2774 | if Nkind (Expr) = N_Op_Add | |
2775 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
2776 | then | |
8a95f4e8 | 2777 | Exp := Left_Opnd (Expr); |
45fc7ddb HK |
2778 | Cons := Expr_Value (Right_Opnd (Expr)); |
2779 | ||
2780 | elsif Nkind (Expr) = N_Op_Subtract | |
2781 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
2782 | then | |
8a95f4e8 | 2783 | Exp := Left_Opnd (Expr); |
45fc7ddb | 2784 | Cons := -Expr_Value (Right_Opnd (Expr)); |
996ae0b0 | 2785 | |
8a95f4e8 RD |
2786 | -- If the bound is a constant created to remove side |
2787 | -- effects, recover original expression to see if it has | |
2788 | -- one of the recognizable forms. | |
2789 | ||
2790 | elsif Nkind (Expr) = N_Identifier | |
2791 | and then not Comes_From_Source (Entity (Expr)) | |
2792 | and then Ekind (Entity (Expr)) = E_Constant | |
2793 | and then | |
2794 | Nkind (Parent (Entity (Expr))) = N_Object_Declaration | |
2795 | then | |
2796 | Exp := Expression (Parent (Entity (Expr))); | |
2797 | Decompose_Expr (Exp, Ent, Kind, Cons); | |
2798 | ||
2799 | -- If original expression includes an entity, create a | |
2800 | -- reference to it for use below. | |
2801 | ||
2802 | if Present (Ent) then | |
2803 | Exp := New_Occurrence_Of (Ent, Sloc (Ent)); | |
2804 | end if; | |
2805 | ||
45fc7ddb | 2806 | else |
8a95f4e8 | 2807 | Exp := Expr; |
45fc7ddb HK |
2808 | Cons := Uint_0; |
2809 | end if; | |
2810 | ||
2811 | -- At this stage Exp is set to the potential X | |
2812 | ||
2813 | if Nkind (Exp) = N_Attribute_Reference then | |
2814 | if Attribute_Name (Exp) = Name_First then | |
2815 | Kind := 'F'; | |
8a95f4e8 | 2816 | |
45fc7ddb HK |
2817 | elsif Attribute_Name (Exp) = Name_Last then |
2818 | Kind := 'L'; | |
8a95f4e8 | 2819 | |
45fc7ddb HK |
2820 | else |
2821 | Ent := Empty; | |
2822 | return; | |
2823 | end if; | |
2824 | ||
2825 | Exp := Prefix (Exp); | |
2826 | ||
2827 | else | |
2828 | Kind := 'E'; | |
2829 | end if; | |
2830 | ||
2831 | if Is_Entity_Name (Exp) | |
2832 | and then Present (Entity (Exp)) | |
2833 | then | |
2834 | Ent := Entity (Exp); | |
2835 | else | |
2836 | Ent := Empty; | |
2837 | end if; | |
2838 | end Decompose_Expr; | |
2839 | ||
2840 | -- Local Variables | |
2841 | ||
2842 | Ent1, Ent2 : Entity_Id; | |
2843 | Kind1, Kind2 : Character; | |
2844 | Cons1, Cons2 : Uint; | |
2845 | ||
2846 | -- Start of processing for Extract_Length | |
2847 | ||
2848 | begin | |
bafc9e1d AC |
2849 | Decompose_Expr |
2850 | (Original_Node (Type_Low_Bound (T)), Ent1, Kind1, Cons1); | |
2851 | Decompose_Expr | |
2852 | (Original_Node (Type_High_Bound (T)), Ent2, Kind2, Cons2); | |
45fc7ddb HK |
2853 | |
2854 | if Present (Ent1) | |
2855 | and then Kind1 = Kind2 | |
2856 | and then Ent1 = Ent2 | |
996ae0b0 | 2857 | then |
45fc7ddb | 2858 | Len := Cons2 - Cons1 + 1; |
996ae0b0 RK |
2859 | else |
2860 | Len := Uint_Minus_1; | |
2861 | end if; | |
45fc7ddb | 2862 | end Extract_Length; |
996ae0b0 RK |
2863 | end Get_Static_Length; |
2864 | ||
45fc7ddb HK |
2865 | -- Local Variables |
2866 | ||
996ae0b0 RK |
2867 | Len_L : Uint; |
2868 | Len_R : Uint; | |
2869 | ||
45fc7ddb HK |
2870 | -- Start of processing for Length_Mismatch |
2871 | ||
996ae0b0 RK |
2872 | begin |
2873 | Get_Static_Length (Left, Len_L); | |
2874 | Get_Static_Length (Right, Len_R); | |
2875 | ||
2876 | if Len_L /= Uint_Minus_1 | |
2877 | and then Len_R /= Uint_Minus_1 | |
2878 | and then Len_L /= Len_R | |
2879 | then | |
fbf5a39b | 2880 | Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False); |
996ae0b0 RK |
2881 | Warn_On_Known_Condition (N); |
2882 | return; | |
2883 | end if; | |
45fc7ddb HK |
2884 | end Length_Mismatch; |
2885 | end if; | |
6eaf4095 | 2886 | |
93c3fca7 | 2887 | -- Test for expression being foldable |
7a3f77d2 | 2888 | |
93c3fca7 | 2889 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); |
996ae0b0 | 2890 | |
93c3fca7 AC |
2891 | -- Only comparisons of scalars can give static results. In particular, |
2892 | -- comparisons of strings never yield a static result, even if both | |
2893 | -- operands are static strings. | |
996ae0b0 RK |
2894 | |
2895 | if not Is_Scalar_Type (Typ) then | |
93c3fca7 AC |
2896 | Stat := False; |
2897 | Set_Is_Static_Expression (N, False); | |
996ae0b0 RK |
2898 | end if; |
2899 | ||
d7567964 TQ |
2900 | -- For operators on universal numeric types called as functions with |
2901 | -- an explicit scope, determine appropriate specific numeric type, and | |
2902 | -- diagnose possible ambiguity. | |
2903 | ||
2904 | if Is_Universal_Numeric_Type (Etype (Left)) | |
2905 | and then | |
2906 | Is_Universal_Numeric_Type (Etype (Right)) | |
2907 | then | |
2908 | Otype := Find_Universal_Operator_Type (N); | |
2909 | end if; | |
2910 | ||
93c3fca7 AC |
2911 | -- For static real type expressions, we cannot use Compile_Time_Compare |
2912 | -- since it worries about run-time results which are not exact. | |
996ae0b0 | 2913 | |
93c3fca7 | 2914 | if Stat and then Is_Real_Type (Typ) then |
996ae0b0 | 2915 | declare |
93c3fca7 AC |
2916 | Left_Real : constant Ureal := Expr_Value_R (Left); |
2917 | Right_Real : constant Ureal := Expr_Value_R (Right); | |
996ae0b0 RK |
2918 | |
2919 | begin | |
2920 | case Nkind (N) is | |
93c3fca7 AC |
2921 | when N_Op_Eq => Result := (Left_Real = Right_Real); |
2922 | when N_Op_Ne => Result := (Left_Real /= Right_Real); | |
2923 | when N_Op_Lt => Result := (Left_Real < Right_Real); | |
2924 | when N_Op_Le => Result := (Left_Real <= Right_Real); | |
2925 | when N_Op_Gt => Result := (Left_Real > Right_Real); | |
2926 | when N_Op_Ge => Result := (Left_Real >= Right_Real); | |
996ae0b0 RK |
2927 | |
2928 | when others => | |
2929 | raise Program_Error; | |
2930 | end case; | |
2931 | ||
93c3fca7 | 2932 | Fold_Uint (N, Test (Result), True); |
996ae0b0 RK |
2933 | end; |
2934 | ||
93c3fca7 | 2935 | -- For all other cases, we use Compile_Time_Compare to do the compare |
996ae0b0 RK |
2936 | |
2937 | else | |
996ae0b0 | 2938 | declare |
93c3fca7 AC |
2939 | CR : constant Compare_Result := |
2940 | Compile_Time_Compare (Left, Right, Assume_Valid => False); | |
996ae0b0 RK |
2941 | |
2942 | begin | |
93c3fca7 AC |
2943 | if CR = Unknown then |
2944 | return; | |
2945 | end if; | |
2946 | ||
996ae0b0 | 2947 | case Nkind (N) is |
93c3fca7 AC |
2948 | when N_Op_Eq => |
2949 | if CR = EQ then | |
2950 | Result := True; | |
2951 | elsif CR = NE or else CR = GT or else CR = LT then | |
2952 | Result := False; | |
2953 | else | |
2954 | return; | |
2955 | end if; | |
2956 | ||
2957 | when N_Op_Ne => | |
2958 | if CR = NE or else CR = GT or else CR = LT then | |
2959 | Result := True; | |
2960 | elsif CR = EQ then | |
2961 | Result := False; | |
2962 | else | |
2963 | return; | |
2964 | end if; | |
2965 | ||
2966 | when N_Op_Lt => | |
2967 | if CR = LT then | |
2968 | Result := True; | |
2969 | elsif CR = EQ or else CR = GT or else CR = GE then | |
2970 | Result := False; | |
2971 | else | |
2972 | return; | |
2973 | end if; | |
2974 | ||
2975 | when N_Op_Le => | |
2976 | if CR = LT or else CR = EQ or else CR = LE then | |
2977 | Result := True; | |
2978 | elsif CR = GT then | |
2979 | Result := False; | |
2980 | else | |
2981 | return; | |
2982 | end if; | |
2983 | ||
2984 | when N_Op_Gt => | |
2985 | if CR = GT then | |
2986 | Result := True; | |
2987 | elsif CR = EQ or else CR = LT or else CR = LE then | |
2988 | Result := False; | |
2989 | else | |
2990 | return; | |
2991 | end if; | |
2992 | ||
2993 | when N_Op_Ge => | |
2994 | if CR = GT or else CR = EQ or else CR = GE then | |
2995 | Result := True; | |
2996 | elsif CR = LT then | |
2997 | Result := False; | |
2998 | else | |
2999 | return; | |
3000 | end if; | |
996ae0b0 RK |
3001 | |
3002 | when others => | |
3003 | raise Program_Error; | |
3004 | end case; | |
996ae0b0 | 3005 | end; |
93c3fca7 AC |
3006 | |
3007 | Fold_Uint (N, Test (Result), Stat); | |
996ae0b0 RK |
3008 | end if; |
3009 | ||
d7567964 TQ |
3010 | -- For the case of a folded relational operator on a specific numeric |
3011 | -- type, freeze operand type now. | |
3012 | ||
3013 | if Present (Otype) then | |
3014 | Freeze_Before (N, Otype); | |
3015 | end if; | |
3016 | ||
996ae0b0 RK |
3017 | Warn_On_Known_Condition (N); |
3018 | end Eval_Relational_Op; | |
3019 | ||
3020 | ---------------- | |
3021 | -- Eval_Shift -- | |
3022 | ---------------- | |
3023 | ||
22cb89b5 AC |
3024 | -- Shift operations are intrinsic operations that can never be static, so |
3025 | -- the only processing required is to perform the required check for a non | |
3026 | -- static context for the two operands. | |
996ae0b0 RK |
3027 | |
3028 | -- Actually we could do some compile time evaluation here some time ??? | |
3029 | ||
3030 | procedure Eval_Shift (N : Node_Id) is | |
3031 | begin | |
3032 | Check_Non_Static_Context (Left_Opnd (N)); | |
3033 | Check_Non_Static_Context (Right_Opnd (N)); | |
3034 | end Eval_Shift; | |
3035 | ||
3036 | ------------------------ | |
3037 | -- Eval_Short_Circuit -- | |
3038 | ------------------------ | |
3039 | ||
22cb89b5 AC |
3040 | -- A short circuit operation is potentially static if both operands are |
3041 | -- potentially static (RM 4.9 (13)). | |
996ae0b0 RK |
3042 | |
3043 | procedure Eval_Short_Circuit (N : Node_Id) is | |
3044 | Kind : constant Node_Kind := Nkind (N); | |
3045 | Left : constant Node_Id := Left_Opnd (N); | |
3046 | Right : constant Node_Id := Right_Opnd (N); | |
3047 | Left_Int : Uint; | |
4d777a71 AC |
3048 | |
3049 | Rstat : constant Boolean := | |
3050 | Is_Static_Expression (Left) | |
3051 | and then | |
3052 | Is_Static_Expression (Right); | |
996ae0b0 RK |
3053 | |
3054 | begin | |
3055 | -- Short circuit operations are never static in Ada 83 | |
3056 | ||
22cb89b5 | 3057 | if Ada_Version = Ada_83 and then Comes_From_Source (N) then |
996ae0b0 RK |
3058 | Check_Non_Static_Context (Left); |
3059 | Check_Non_Static_Context (Right); | |
3060 | return; | |
3061 | end if; | |
3062 | ||
3063 | -- Now look at the operands, we can't quite use the normal call to | |
3064 | -- Test_Expression_Is_Foldable here because short circuit operations | |
3065 | -- are a special case, they can still be foldable, even if the right | |
3066 | -- operand raises constraint error. | |
3067 | ||
22cb89b5 AC |
3068 | -- If either operand is Any_Type, just propagate to result and do not |
3069 | -- try to fold, this prevents cascaded errors. | |
996ae0b0 RK |
3070 | |
3071 | if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then | |
3072 | Set_Etype (N, Any_Type); | |
3073 | return; | |
3074 | ||
3075 | -- If left operand raises constraint error, then replace node N with | |
3076 | -- the raise constraint error node, and we are obviously not foldable. | |
3077 | -- Is_Static_Expression is set from the two operands in the normal way, | |
3078 | -- and we check the right operand if it is in a non-static context. | |
3079 | ||
3080 | elsif Raises_Constraint_Error (Left) then | |
3081 | if not Rstat then | |
3082 | Check_Non_Static_Context (Right); | |
3083 | end if; | |
3084 | ||
3085 | Rewrite_In_Raise_CE (N, Left); | |
3086 | Set_Is_Static_Expression (N, Rstat); | |
3087 | return; | |
3088 | ||
3089 | -- If the result is not static, then we won't in any case fold | |
3090 | ||
3091 | elsif not Rstat then | |
3092 | Check_Non_Static_Context (Left); | |
3093 | Check_Non_Static_Context (Right); | |
3094 | return; | |
3095 | end if; | |
3096 | ||
3097 | -- Here the result is static, note that, unlike the normal processing | |
3098 | -- in Test_Expression_Is_Foldable, we did *not* check above to see if | |
3099 | -- the right operand raises constraint error, that's because it is not | |
3100 | -- significant if the left operand is decisive. | |
3101 | ||
3102 | Set_Is_Static_Expression (N); | |
3103 | ||
3104 | -- It does not matter if the right operand raises constraint error if | |
3105 | -- it will not be evaluated. So deal specially with the cases where | |
3106 | -- the right operand is not evaluated. Note that we will fold these | |
3107 | -- cases even if the right operand is non-static, which is fine, but | |
3108 | -- of course in these cases the result is not potentially static. | |
3109 | ||
3110 | Left_Int := Expr_Value (Left); | |
3111 | ||
3112 | if (Kind = N_And_Then and then Is_False (Left_Int)) | |
514d0fc5 | 3113 | or else |
4d777a71 | 3114 | (Kind = N_Or_Else and then Is_True (Left_Int)) |
996ae0b0 | 3115 | then |
fbf5a39b | 3116 | Fold_Uint (N, Left_Int, Rstat); |
996ae0b0 RK |
3117 | return; |
3118 | end if; | |
3119 | ||
3120 | -- If first operand not decisive, then it does matter if the right | |
3121 | -- operand raises constraint error, since it will be evaluated, so | |
3122 | -- we simply replace the node with the right operand. Note that this | |
3123 | -- properly propagates Is_Static_Expression and Raises_Constraint_Error | |
3124 | -- (both are set to True in Right). | |
3125 | ||
3126 | if Raises_Constraint_Error (Right) then | |
3127 | Rewrite_In_Raise_CE (N, Right); | |
3128 | Check_Non_Static_Context (Left); | |
3129 | return; | |
3130 | end if; | |
3131 | ||
3132 | -- Otherwise the result depends on the right operand | |
3133 | ||
fbf5a39b | 3134 | Fold_Uint (N, Expr_Value (Right), Rstat); |
996ae0b0 | 3135 | return; |
996ae0b0 RK |
3136 | end Eval_Short_Circuit; |
3137 | ||
3138 | ---------------- | |
3139 | -- Eval_Slice -- | |
3140 | ---------------- | |
3141 | ||
22cb89b5 AC |
3142 | -- Slices can never be static, so the only processing required is to check |
3143 | -- for non-static context if an explicit range is given. | |
996ae0b0 RK |
3144 | |
3145 | procedure Eval_Slice (N : Node_Id) is | |
3146 | Drange : constant Node_Id := Discrete_Range (N); | |
996ae0b0 RK |
3147 | begin |
3148 | if Nkind (Drange) = N_Range then | |
3149 | Check_Non_Static_Context (Low_Bound (Drange)); | |
3150 | Check_Non_Static_Context (High_Bound (Drange)); | |
3151 | end if; | |
cd2fb920 | 3152 | |
22cb89b5 | 3153 | -- A slice of the form A (subtype), when the subtype is the index of |
cd2fb920 ES |
3154 | -- the type of A, is redundant, the slice can be replaced with A, and |
3155 | -- this is worth a warning. | |
3156 | ||
3157 | if Is_Entity_Name (Prefix (N)) then | |
3158 | declare | |
3159 | E : constant Entity_Id := Entity (Prefix (N)); | |
3160 | T : constant Entity_Id := Etype (E); | |
3161 | begin | |
3162 | if Ekind (E) = E_Constant | |
3163 | and then Is_Array_Type (T) | |
3164 | and then Is_Entity_Name (Drange) | |
3165 | then | |
3166 | if Is_Entity_Name (Original_Node (First_Index (T))) | |
3167 | and then Entity (Original_Node (First_Index (T))) | |
3168 | = Entity (Drange) | |
3169 | then | |
3170 | if Warn_On_Redundant_Constructs then | |
ed2233dc | 3171 | Error_Msg_N ("redundant slice denotes whole array?", N); |
cd2fb920 ES |
3172 | end if; |
3173 | ||
22cb89b5 | 3174 | -- The following might be a useful optimization???? |
cd2fb920 ES |
3175 | |
3176 | -- Rewrite (N, New_Occurrence_Of (E, Sloc (N))); | |
3177 | end if; | |
3178 | end if; | |
3179 | end; | |
3180 | end if; | |
996ae0b0 RK |
3181 | end Eval_Slice; |
3182 | ||
3183 | ------------------------- | |
3184 | -- Eval_String_Literal -- | |
3185 | ------------------------- | |
3186 | ||
3187 | procedure Eval_String_Literal (N : Node_Id) is | |
91b1417d AC |
3188 | Typ : constant Entity_Id := Etype (N); |
3189 | Bas : constant Entity_Id := Base_Type (Typ); | |
3190 | Xtp : Entity_Id; | |
3191 | Len : Nat; | |
3192 | Lo : Node_Id; | |
996ae0b0 RK |
3193 | |
3194 | begin | |
3195 | -- Nothing to do if error type (handles cases like default expressions | |
22cb89b5 | 3196 | -- or generics where we have not yet fully resolved the type). |
996ae0b0 | 3197 | |
91b1417d | 3198 | if Bas = Any_Type or else Bas = Any_String then |
996ae0b0 | 3199 | return; |
91b1417d | 3200 | end if; |
996ae0b0 RK |
3201 | |
3202 | -- String literals are static if the subtype is static (RM 4.9(2)), so | |
3203 | -- reset the static expression flag (it was set unconditionally in | |
3204 | -- Analyze_String_Literal) if the subtype is non-static. We tell if | |
3205 | -- the subtype is static by looking at the lower bound. | |
3206 | ||
91b1417d AC |
3207 | if Ekind (Typ) = E_String_Literal_Subtype then |
3208 | if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then | |
3209 | Set_Is_Static_Expression (N, False); | |
3210 | return; | |
3211 | end if; | |
3212 | ||
3213 | -- Here if Etype of string literal is normal Etype (not yet possible, | |
22cb89b5 | 3214 | -- but may be possible in future). |
91b1417d AC |
3215 | |
3216 | elsif not Is_OK_Static_Expression | |
3217 | (Type_Low_Bound (Etype (First_Index (Typ)))) | |
3218 | then | |
996ae0b0 | 3219 | Set_Is_Static_Expression (N, False); |
91b1417d AC |
3220 | return; |
3221 | end if; | |
996ae0b0 | 3222 | |
91b1417d AC |
3223 | -- If original node was a type conversion, then result if non-static |
3224 | ||
3225 | if Nkind (Original_Node (N)) = N_Type_Conversion then | |
996ae0b0 | 3226 | Set_Is_Static_Expression (N, False); |
91b1417d AC |
3227 | return; |
3228 | end if; | |
996ae0b0 | 3229 | |
22cb89b5 AC |
3230 | -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95 |
3231 | -- if its bounds are outside the index base type and this index type is | |
3232 | -- static. This can happen in only two ways. Either the string literal | |
3233 | -- is too long, or it is null, and the lower bound is type'First. In | |
3234 | -- either case it is the upper bound that is out of range of the index | |
3235 | -- type. | |
996ae0b0 | 3236 | |
0ab80019 | 3237 | if Ada_Version >= Ada_95 then |
91b1417d AC |
3238 | if Root_Type (Bas) = Standard_String |
3239 | or else | |
3240 | Root_Type (Bas) = Standard_Wide_String | |
996ae0b0 | 3241 | then |
91b1417d | 3242 | Xtp := Standard_Positive; |
996ae0b0 | 3243 | else |
91b1417d | 3244 | Xtp := Etype (First_Index (Bas)); |
996ae0b0 RK |
3245 | end if; |
3246 | ||
91b1417d AC |
3247 | if Ekind (Typ) = E_String_Literal_Subtype then |
3248 | Lo := String_Literal_Low_Bound (Typ); | |
3249 | else | |
3250 | Lo := Type_Low_Bound (Etype (First_Index (Typ))); | |
3251 | end if; | |
3252 | ||
3253 | Len := String_Length (Strval (N)); | |
3254 | ||
3255 | if UI_From_Int (Len) > String_Type_Len (Bas) then | |
996ae0b0 | 3256 | Apply_Compile_Time_Constraint_Error |
07fc65c4 | 3257 | (N, "string literal too long for}", CE_Length_Check_Failed, |
91b1417d AC |
3258 | Ent => Bas, |
3259 | Typ => First_Subtype (Bas)); | |
996ae0b0 | 3260 | |
91b1417d AC |
3261 | elsif Len = 0 |
3262 | and then not Is_Generic_Type (Xtp) | |
3263 | and then | |
3264 | Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp))) | |
996ae0b0 RK |
3265 | then |
3266 | Apply_Compile_Time_Constraint_Error | |
3267 | (N, "null string literal not allowed for}", | |
07fc65c4 | 3268 | CE_Length_Check_Failed, |
91b1417d AC |
3269 | Ent => Bas, |
3270 | Typ => First_Subtype (Bas)); | |
996ae0b0 RK |
3271 | end if; |
3272 | end if; | |
996ae0b0 RK |
3273 | end Eval_String_Literal; |
3274 | ||
3275 | -------------------------- | |
3276 | -- Eval_Type_Conversion -- | |
3277 | -------------------------- | |
3278 | ||
3279 | -- A type conversion is potentially static if its subtype mark is for a | |
3280 | -- static scalar subtype, and its operand expression is potentially static | |
22cb89b5 | 3281 | -- (RM 4.9(10)). |
996ae0b0 RK |
3282 | |
3283 | procedure Eval_Type_Conversion (N : Node_Id) is | |
3284 | Operand : constant Node_Id := Expression (N); | |
3285 | Source_Type : constant Entity_Id := Etype (Operand); | |
3286 | Target_Type : constant Entity_Id := Etype (N); | |
3287 | ||
3288 | Stat : Boolean; | |
3289 | Fold : Boolean; | |
3290 | ||
3291 | function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean; | |
22cb89b5 AC |
3292 | -- Returns true if type T is an integer type, or if it is a fixed-point |
3293 | -- type to be treated as an integer (i.e. the flag Conversion_OK is set | |
3294 | -- on the conversion node). | |
996ae0b0 RK |
3295 | |
3296 | function To_Be_Treated_As_Real (T : Entity_Id) return Boolean; | |
3297 | -- Returns true if type T is a floating-point type, or if it is a | |
3298 | -- fixed-point type that is not to be treated as an integer (i.e. the | |
3299 | -- flag Conversion_OK is not set on the conversion node). | |
3300 | ||
fbf5a39b AC |
3301 | ------------------------------ |
3302 | -- To_Be_Treated_As_Integer -- | |
3303 | ------------------------------ | |
3304 | ||
996ae0b0 RK |
3305 | function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is |
3306 | begin | |
3307 | return | |
3308 | Is_Integer_Type (T) | |
3309 | or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N)); | |
3310 | end To_Be_Treated_As_Integer; | |
3311 | ||
fbf5a39b AC |
3312 | --------------------------- |
3313 | -- To_Be_Treated_As_Real -- | |
3314 | --------------------------- | |
3315 | ||
996ae0b0 RK |
3316 | function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is |
3317 | begin | |
3318 | return | |
3319 | Is_Floating_Point_Type (T) | |
3320 | or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N)); | |
3321 | end To_Be_Treated_As_Real; | |
3322 | ||
3323 | -- Start of processing for Eval_Type_Conversion | |
3324 | ||
3325 | begin | |
82c80734 | 3326 | -- Cannot fold if target type is non-static or if semantic error |
996ae0b0 RK |
3327 | |
3328 | if not Is_Static_Subtype (Target_Type) then | |
3329 | Check_Non_Static_Context (Operand); | |
3330 | return; | |
3331 | ||
3332 | elsif Error_Posted (N) then | |
3333 | return; | |
3334 | end if; | |
3335 | ||
3336 | -- If not foldable we are done | |
3337 | ||
3338 | Test_Expression_Is_Foldable (N, Operand, Stat, Fold); | |
3339 | ||
3340 | if not Fold then | |
3341 | return; | |
3342 | ||
3343 | -- Don't try fold if target type has constraint error bounds | |
3344 | ||
3345 | elsif not Is_OK_Static_Subtype (Target_Type) then | |
3346 | Set_Raises_Constraint_Error (N); | |
3347 | return; | |
3348 | end if; | |
3349 | ||
3350 | -- Remaining processing depends on operand types. Note that in the | |
3351 | -- following type test, fixed-point counts as real unless the flag | |
3352 | -- Conversion_OK is set, in which case it counts as integer. | |
3353 | ||
82c80734 | 3354 | -- Fold conversion, case of string type. The result is not static |
996ae0b0 RK |
3355 | |
3356 | if Is_String_Type (Target_Type) then | |
b11e8d6f | 3357 | Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False); |
996ae0b0 RK |
3358 | |
3359 | return; | |
3360 | ||
3361 | -- Fold conversion, case of integer target type | |
3362 | ||
3363 | elsif To_Be_Treated_As_Integer (Target_Type) then | |
3364 | declare | |
3365 | Result : Uint; | |
3366 | ||
3367 | begin | |
3368 | -- Integer to integer conversion | |
3369 | ||
3370 | if To_Be_Treated_As_Integer (Source_Type) then | |
3371 | Result := Expr_Value (Operand); | |
3372 | ||
3373 | -- Real to integer conversion | |
3374 | ||
3375 | else | |
3376 | Result := UR_To_Uint (Expr_Value_R (Operand)); | |
3377 | end if; | |
3378 | ||
3379 | -- If fixed-point type (Conversion_OK must be set), then the | |
3380 | -- result is logically an integer, but we must replace the | |
3381 | -- conversion with the corresponding real literal, since the | |
3382 | -- type from a semantic point of view is still fixed-point. | |
3383 | ||
3384 | if Is_Fixed_Point_Type (Target_Type) then | |
3385 | Fold_Ureal | |
fbf5a39b | 3386 | (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat); |
996ae0b0 RK |
3387 | |
3388 | -- Otherwise result is integer literal | |
3389 | ||
3390 | else | |
fbf5a39b | 3391 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
3392 | end if; |
3393 | end; | |
3394 | ||
3395 | -- Fold conversion, case of real target type | |
3396 | ||
3397 | elsif To_Be_Treated_As_Real (Target_Type) then | |
3398 | declare | |
3399 | Result : Ureal; | |
3400 | ||
3401 | begin | |
3402 | if To_Be_Treated_As_Real (Source_Type) then | |
3403 | Result := Expr_Value_R (Operand); | |
3404 | else | |
3405 | Result := UR_From_Uint (Expr_Value (Operand)); | |
3406 | end if; | |
3407 | ||
fbf5a39b | 3408 | Fold_Ureal (N, Result, Stat); |
996ae0b0 RK |
3409 | end; |
3410 | ||
3411 | -- Enumeration types | |
3412 | ||
3413 | else | |
fbf5a39b | 3414 | Fold_Uint (N, Expr_Value (Operand), Stat); |
996ae0b0 RK |
3415 | end if; |
3416 | ||
c800f862 | 3417 | if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then |
996ae0b0 RK |
3418 | Out_Of_Range (N); |
3419 | end if; | |
3420 | ||
3421 | end Eval_Type_Conversion; | |
3422 | ||
3423 | ------------------- | |
3424 | -- Eval_Unary_Op -- | |
3425 | ------------------- | |
3426 | ||
3427 | -- Predefined unary operators are static functions (RM 4.9(20)) and thus | |
22cb89b5 | 3428 | -- are potentially static if the operand is potentially static (RM 4.9(7)). |
996ae0b0 RK |
3429 | |
3430 | procedure Eval_Unary_Op (N : Node_Id) is | |
3431 | Right : constant Node_Id := Right_Opnd (N); | |
d7567964 | 3432 | Otype : Entity_Id := Empty; |
996ae0b0 RK |
3433 | Stat : Boolean; |
3434 | Fold : Boolean; | |
3435 | ||
3436 | begin | |
3437 | -- If not foldable we are done | |
3438 | ||
3439 | Test_Expression_Is_Foldable (N, Right, Stat, Fold); | |
3440 | ||
3441 | if not Fold then | |
3442 | return; | |
3443 | end if; | |
3444 | ||
602a7ec0 | 3445 | if Etype (Right) = Universal_Integer |
ae77c68b AC |
3446 | or else |
3447 | Etype (Right) = Universal_Real | |
602a7ec0 | 3448 | then |
d7567964 | 3449 | Otype := Find_Universal_Operator_Type (N); |
602a7ec0 AC |
3450 | end if; |
3451 | ||
996ae0b0 RK |
3452 | -- Fold for integer case |
3453 | ||
3454 | if Is_Integer_Type (Etype (N)) then | |
3455 | declare | |
3456 | Rint : constant Uint := Expr_Value (Right); | |
3457 | Result : Uint; | |
3458 | ||
3459 | begin | |
3460 | -- In the case of modular unary plus and abs there is no need | |
3461 | -- to adjust the result of the operation since if the original | |
3462 | -- operand was in bounds the result will be in the bounds of the | |
3463 | -- modular type. However, in the case of modular unary minus the | |
3464 | -- result may go out of the bounds of the modular type and needs | |
3465 | -- adjustment. | |
3466 | ||
3467 | if Nkind (N) = N_Op_Plus then | |
3468 | Result := Rint; | |
3469 | ||
3470 | elsif Nkind (N) = N_Op_Minus then | |
3471 | if Is_Modular_Integer_Type (Etype (N)) then | |
3472 | Result := (-Rint) mod Modulus (Etype (N)); | |
3473 | else | |
3474 | Result := (-Rint); | |
3475 | end if; | |
3476 | ||
3477 | else | |
3478 | pragma Assert (Nkind (N) = N_Op_Abs); | |
3479 | Result := abs Rint; | |
3480 | end if; | |
3481 | ||
fbf5a39b | 3482 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
3483 | end; |
3484 | ||
3485 | -- Fold for real case | |
3486 | ||
3487 | elsif Is_Real_Type (Etype (N)) then | |
3488 | declare | |
3489 | Rreal : constant Ureal := Expr_Value_R (Right); | |
3490 | Result : Ureal; | |
3491 | ||
3492 | begin | |
3493 | if Nkind (N) = N_Op_Plus then | |
3494 | Result := Rreal; | |
3495 | ||
3496 | elsif Nkind (N) = N_Op_Minus then | |
3497 | Result := UR_Negate (Rreal); | |
3498 | ||
3499 | else | |
3500 | pragma Assert (Nkind (N) = N_Op_Abs); | |
3501 | Result := abs Rreal; | |
3502 | end if; | |
3503 | ||
fbf5a39b | 3504 | Fold_Ureal (N, Result, Stat); |
996ae0b0 RK |
3505 | end; |
3506 | end if; | |
d7567964 TQ |
3507 | |
3508 | -- If the operator was resolved to a specific type, make sure that type | |
3509 | -- is frozen even if the expression is folded into a literal (which has | |
3510 | -- a universal type). | |
3511 | ||
3512 | if Present (Otype) then | |
3513 | Freeze_Before (N, Otype); | |
3514 | end if; | |
996ae0b0 RK |
3515 | end Eval_Unary_Op; |
3516 | ||
3517 | ------------------------------- | |
3518 | -- Eval_Unchecked_Conversion -- | |
3519 | ------------------------------- | |
3520 | ||
3521 | -- Unchecked conversions can never be static, so the only required | |
3522 | -- processing is to check for a non-static context for the operand. | |
3523 | ||
3524 | procedure Eval_Unchecked_Conversion (N : Node_Id) is | |
3525 | begin | |
3526 | Check_Non_Static_Context (Expression (N)); | |
3527 | end Eval_Unchecked_Conversion; | |
3528 | ||
3529 | -------------------- | |
3530 | -- Expr_Rep_Value -- | |
3531 | -------------------- | |
3532 | ||
3533 | function Expr_Rep_Value (N : Node_Id) return Uint is | |
07fc65c4 GB |
3534 | Kind : constant Node_Kind := Nkind (N); |
3535 | Ent : Entity_Id; | |
996ae0b0 RK |
3536 | |
3537 | begin | |
3538 | if Is_Entity_Name (N) then | |
3539 | Ent := Entity (N); | |
3540 | ||
22cb89b5 AC |
3541 | -- An enumeration literal that was either in the source or created |
3542 | -- as a result of static evaluation. | |
996ae0b0 RK |
3543 | |
3544 | if Ekind (Ent) = E_Enumeration_Literal then | |
3545 | return Enumeration_Rep (Ent); | |
3546 | ||
3547 | -- A user defined static constant | |
3548 | ||
3549 | else | |
3550 | pragma Assert (Ekind (Ent) = E_Constant); | |
3551 | return Expr_Rep_Value (Constant_Value (Ent)); | |
3552 | end if; | |
3553 | ||
22cb89b5 AC |
3554 | -- An integer literal that was either in the source or created as a |
3555 | -- result of static evaluation. | |
996ae0b0 RK |
3556 | |
3557 | elsif Kind = N_Integer_Literal then | |
3558 | return Intval (N); | |
3559 | ||
3560 | -- A real literal for a fixed-point type. This must be the fixed-point | |
3561 | -- case, either the literal is of a fixed-point type, or it is a bound | |
3562 | -- of a fixed-point type, with type universal real. In either case we | |
3563 | -- obtain the desired value from Corresponding_Integer_Value. | |
3564 | ||
3565 | elsif Kind = N_Real_Literal then | |
996ae0b0 RK |
3566 | pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N)))); |
3567 | return Corresponding_Integer_Value (N); | |
3568 | ||
07fc65c4 GB |
3569 | -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero |
3570 | ||
3571 | elsif Kind = N_Attribute_Reference | |
3572 | and then Attribute_Name (N) = Name_Null_Parameter | |
3573 | then | |
3574 | return Uint_0; | |
3575 | ||
07fc65c4 | 3576 | -- Otherwise must be character literal |
8cbb664e | 3577 | |
996ae0b0 RK |
3578 | else |
3579 | pragma Assert (Kind = N_Character_Literal); | |
3580 | Ent := Entity (N); | |
3581 | ||
22cb89b5 AC |
3582 | -- Since Character literals of type Standard.Character don't have any |
3583 | -- defining character literals built for them, they do not have their | |
3584 | -- Entity set, so just use their Char code. Otherwise for user- | |
3585 | -- defined character literals use their Pos value as usual which is | |
3586 | -- the same as the Rep value. | |
996ae0b0 RK |
3587 | |
3588 | if No (Ent) then | |
82c80734 | 3589 | return Char_Literal_Value (N); |
996ae0b0 RK |
3590 | else |
3591 | return Enumeration_Rep (Ent); | |
3592 | end if; | |
3593 | end if; | |
3594 | end Expr_Rep_Value; | |
3595 | ||
3596 | ---------------- | |
3597 | -- Expr_Value -- | |
3598 | ---------------- | |
3599 | ||
3600 | function Expr_Value (N : Node_Id) return Uint is | |
07fc65c4 GB |
3601 | Kind : constant Node_Kind := Nkind (N); |
3602 | CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size); | |
3603 | Ent : Entity_Id; | |
3604 | Val : Uint; | |
996ae0b0 RK |
3605 | |
3606 | begin | |
13f34a3f RD |
3607 | -- If already in cache, then we know it's compile time known and we can |
3608 | -- return the value that was previously stored in the cache since | |
3609 | -- compile time known values cannot change. | |
07fc65c4 GB |
3610 | |
3611 | if CV_Ent.N = N then | |
3612 | return CV_Ent.V; | |
3613 | end if; | |
3614 | ||
3615 | -- Otherwise proceed to test value | |
3616 | ||
996ae0b0 RK |
3617 | if Is_Entity_Name (N) then |
3618 | Ent := Entity (N); | |
3619 | ||
22cb89b5 AC |
3620 | -- An enumeration literal that was either in the source or created as |
3621 | -- a result of static evaluation. | |
996ae0b0 RK |
3622 | |
3623 | if Ekind (Ent) = E_Enumeration_Literal then | |
07fc65c4 | 3624 | Val := Enumeration_Pos (Ent); |
996ae0b0 RK |
3625 | |
3626 | -- A user defined static constant | |
3627 | ||
3628 | else | |
3629 | pragma Assert (Ekind (Ent) = E_Constant); | |
07fc65c4 | 3630 | Val := Expr_Value (Constant_Value (Ent)); |
996ae0b0 RK |
3631 | end if; |
3632 | ||
22cb89b5 AC |
3633 | -- An integer literal that was either in the source or created as a |
3634 | -- result of static evaluation. | |
996ae0b0 RK |
3635 | |
3636 | elsif Kind = N_Integer_Literal then | |
07fc65c4 | 3637 | Val := Intval (N); |
996ae0b0 RK |
3638 | |
3639 | -- A real literal for a fixed-point type. This must be the fixed-point | |
3640 | -- case, either the literal is of a fixed-point type, or it is a bound | |
3641 | -- of a fixed-point type, with type universal real. In either case we | |
3642 | -- obtain the desired value from Corresponding_Integer_Value. | |
3643 | ||
3644 | elsif Kind = N_Real_Literal then | |
3645 | ||
996ae0b0 | 3646 | pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N)))); |
07fc65c4 | 3647 | Val := Corresponding_Integer_Value (N); |
996ae0b0 RK |
3648 | |
3649 | -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero | |
3650 | ||
3651 | elsif Kind = N_Attribute_Reference | |
3652 | and then Attribute_Name (N) = Name_Null_Parameter | |
3653 | then | |
07fc65c4 GB |
3654 | Val := Uint_0; |
3655 | ||
996ae0b0 RK |
3656 | -- Otherwise must be character literal |
3657 | ||
3658 | else | |
3659 | pragma Assert (Kind = N_Character_Literal); | |
3660 | Ent := Entity (N); | |
3661 | ||
3662 | -- Since Character literals of type Standard.Character don't | |
3663 | -- have any defining character literals built for them, they | |
3664 | -- do not have their Entity set, so just use their Char | |
3665 | -- code. Otherwise for user-defined character literals use | |
3666 | -- their Pos value as usual. | |
3667 | ||
3668 | if No (Ent) then | |
82c80734 | 3669 | Val := Char_Literal_Value (N); |
996ae0b0 | 3670 | else |
07fc65c4 | 3671 | Val := Enumeration_Pos (Ent); |
996ae0b0 RK |
3672 | end if; |
3673 | end if; | |
3674 | ||
07fc65c4 GB |
3675 | -- Come here with Val set to value to be returned, set cache |
3676 | ||
3677 | CV_Ent.N := N; | |
3678 | CV_Ent.V := Val; | |
3679 | return Val; | |
996ae0b0 RK |
3680 | end Expr_Value; |
3681 | ||
3682 | ------------------ | |
3683 | -- Expr_Value_E -- | |
3684 | ------------------ | |
3685 | ||
3686 | function Expr_Value_E (N : Node_Id) return Entity_Id is | |
3687 | Ent : constant Entity_Id := Entity (N); | |
3688 | ||
3689 | begin | |
3690 | if Ekind (Ent) = E_Enumeration_Literal then | |
3691 | return Ent; | |
3692 | else | |
3693 | pragma Assert (Ekind (Ent) = E_Constant); | |
3694 | return Expr_Value_E (Constant_Value (Ent)); | |
3695 | end if; | |
3696 | end Expr_Value_E; | |
3697 | ||
3698 | ------------------ | |
3699 | -- Expr_Value_R -- | |
3700 | ------------------ | |
3701 | ||
3702 | function Expr_Value_R (N : Node_Id) return Ureal is | |
3703 | Kind : constant Node_Kind := Nkind (N); | |
3704 | Ent : Entity_Id; | |
3705 | Expr : Node_Id; | |
3706 | ||
3707 | begin | |
3708 | if Kind = N_Real_Literal then | |
3709 | return Realval (N); | |
3710 | ||
3711 | elsif Kind = N_Identifier or else Kind = N_Expanded_Name then | |
3712 | Ent := Entity (N); | |
3713 | pragma Assert (Ekind (Ent) = E_Constant); | |
3714 | return Expr_Value_R (Constant_Value (Ent)); | |
3715 | ||
3716 | elsif Kind = N_Integer_Literal then | |
3717 | return UR_From_Uint (Expr_Value (N)); | |
3718 | ||
3719 | -- Strange case of VAX literals, which are at this stage transformed | |
3720 | -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in | |
3721 | -- Exp_Vfpt for further details. | |
3722 | ||
3723 | elsif Vax_Float (Etype (N)) | |
3724 | and then Nkind (N) = N_Unchecked_Type_Conversion | |
3725 | then | |
3726 | Expr := Expression (N); | |
3727 | ||
3728 | if Nkind (Expr) = N_Function_Call | |
3729 | and then Present (Parameter_Associations (Expr)) | |
3730 | then | |
3731 | Expr := First (Parameter_Associations (Expr)); | |
3732 | ||
3733 | if Nkind (Expr) = N_Real_Literal then | |
3734 | return Realval (Expr); | |
3735 | end if; | |
3736 | end if; | |
3737 | ||
3738 | -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0 | |
3739 | ||
3740 | elsif Kind = N_Attribute_Reference | |
3741 | and then Attribute_Name (N) = Name_Null_Parameter | |
3742 | then | |
3743 | return Ureal_0; | |
3744 | end if; | |
3745 | ||
22cb89b5 AC |
3746 | -- If we fall through, we have a node that cannot be interpreted as a |
3747 | -- compile time constant. That is definitely an error. | |
996ae0b0 RK |
3748 | |
3749 | raise Program_Error; | |
3750 | end Expr_Value_R; | |
3751 | ||
3752 | ------------------ | |
3753 | -- Expr_Value_S -- | |
3754 | ------------------ | |
3755 | ||
3756 | function Expr_Value_S (N : Node_Id) return Node_Id is | |
3757 | begin | |
3758 | if Nkind (N) = N_String_Literal then | |
3759 | return N; | |
3760 | else | |
3761 | pragma Assert (Ekind (Entity (N)) = E_Constant); | |
3762 | return Expr_Value_S (Constant_Value (Entity (N))); | |
3763 | end if; | |
3764 | end Expr_Value_S; | |
3765 | ||
74e7891f RD |
3766 | ---------------------------------- |
3767 | -- Find_Universal_Operator_Type -- | |
3768 | ---------------------------------- | |
3769 | ||
3770 | function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is | |
3771 | PN : constant Node_Id := Parent (N); | |
3772 | Call : constant Node_Id := Original_Node (N); | |
3773 | Is_Int : constant Boolean := Is_Integer_Type (Etype (N)); | |
3774 | ||
3775 | Is_Fix : constant Boolean := | |
3776 | Nkind (N) in N_Binary_Op | |
3777 | and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N)); | |
3778 | -- A mixed-mode operation in this context indicates the presence of | |
3779 | -- fixed-point type in the designated package. | |
3780 | ||
3781 | Is_Relational : constant Boolean := Etype (N) = Standard_Boolean; | |
3782 | -- Case where N is a relational (or membership) operator (else it is an | |
3783 | -- arithmetic one). | |
3784 | ||
3785 | In_Membership : constant Boolean := | |
3786 | Nkind (PN) in N_Membership_Test | |
3787 | and then | |
3788 | Nkind (Right_Opnd (PN)) = N_Range | |
3789 | and then | |
3790 | Is_Universal_Numeric_Type (Etype (Left_Opnd (PN))) | |
3791 | and then | |
3792 | Is_Universal_Numeric_Type | |
3793 | (Etype (Low_Bound (Right_Opnd (PN)))) | |
3794 | and then | |
3795 | Is_Universal_Numeric_Type | |
3796 | (Etype (High_Bound (Right_Opnd (PN)))); | |
3797 | -- Case where N is part of a membership test with a universal range | |
3798 | ||
3799 | E : Entity_Id; | |
3800 | Pack : Entity_Id; | |
3801 | Typ1 : Entity_Id := Empty; | |
3802 | Priv_E : Entity_Id; | |
3803 | ||
3804 | function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean; | |
7ec8363d RD |
3805 | -- Check whether one operand is a mixed-mode operation that requires the |
3806 | -- presence of a fixed-point type. Given that all operands are universal | |
3807 | -- and have been constant-folded, retrieve the original function call. | |
74e7891f RD |
3808 | |
3809 | --------------------------- | |
3810 | -- Is_Mixed_Mode_Operand -- | |
3811 | --------------------------- | |
3812 | ||
3813 | function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is | |
7ec8363d | 3814 | Onod : constant Node_Id := Original_Node (Op); |
74e7891f | 3815 | begin |
7ec8363d RD |
3816 | return Nkind (Onod) = N_Function_Call |
3817 | and then Present (Next_Actual (First_Actual (Onod))) | |
3818 | and then Etype (First_Actual (Onod)) /= | |
3819 | Etype (Next_Actual (First_Actual (Onod))); | |
74e7891f RD |
3820 | end Is_Mixed_Mode_Operand; |
3821 | ||
7ec8363d RD |
3822 | -- Start of processing for Find_Universal_Operator_Type |
3823 | ||
74e7891f RD |
3824 | begin |
3825 | if Nkind (Call) /= N_Function_Call | |
3826 | or else Nkind (Name (Call)) /= N_Expanded_Name | |
3827 | then | |
3828 | return Empty; | |
3829 | ||
3830 | -- There are two cases where the context does not imply the type of the | |
3831 | -- operands: either the universal expression appears in a type | |
7ec8363d | 3832 | -- conversion, or we are in the case of a predefined relational |
74e7891f RD |
3833 | -- operator, where the context type is always Boolean. |
3834 | ||
3835 | elsif Nkind (Parent (N)) = N_Type_Conversion | |
7ec8363d RD |
3836 | or else Is_Relational |
3837 | or else In_Membership | |
74e7891f RD |
3838 | then |
3839 | Pack := Entity (Prefix (Name (Call))); | |
3840 | ||
7ec8363d RD |
3841 | -- If the prefix is a package declared elsewhere, iterate over its |
3842 | -- visible entities, otherwise iterate over all declarations in the | |
3843 | -- designated scope. | |
74e7891f RD |
3844 | |
3845 | if Ekind (Pack) = E_Package | |
3846 | and then not In_Open_Scopes (Pack) | |
3847 | then | |
3848 | Priv_E := First_Private_Entity (Pack); | |
3849 | else | |
3850 | Priv_E := Empty; | |
3851 | end if; | |
3852 | ||
3853 | Typ1 := Empty; | |
3854 | E := First_Entity (Pack); | |
3855 | while Present (E) and then E /= Priv_E loop | |
3856 | if Is_Numeric_Type (E) | |
3857 | and then Nkind (Parent (E)) /= N_Subtype_Declaration | |
3858 | and then Comes_From_Source (E) | |
3859 | and then Is_Integer_Type (E) = Is_Int | |
3860 | and then | |
3861 | (Nkind (N) in N_Unary_Op | |
3862 | or else Is_Relational | |
3863 | or else Is_Fixed_Point_Type (E) = Is_Fix) | |
3864 | then | |
3865 | if No (Typ1) then | |
3866 | Typ1 := E; | |
3867 | ||
3868 | -- Before emitting an error, check for the presence of a | |
3869 | -- mixed-mode operation that specifies a fixed point type. | |
3870 | ||
3871 | elsif Is_Relational | |
3872 | and then | |
3873 | (Is_Mixed_Mode_Operand (Left_Opnd (N)) | |
3874 | or else Is_Mixed_Mode_Operand (Right_Opnd (N))) | |
3875 | and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1) | |
3876 | ||
3877 | then | |
3878 | if Is_Fixed_Point_Type (E) then | |
3879 | Typ1 := E; | |
3880 | end if; | |
3881 | ||
3882 | else | |
3883 | -- More than one type of the proper class declared in P | |
3884 | ||
3885 | Error_Msg_N ("ambiguous operation", N); | |
3886 | Error_Msg_Sloc := Sloc (Typ1); | |
3887 | Error_Msg_N ("\possible interpretation (inherited)#", N); | |
3888 | Error_Msg_Sloc := Sloc (E); | |
3889 | Error_Msg_N ("\possible interpretation (inherited)#", N); | |
3890 | return Empty; | |
3891 | end if; | |
3892 | end if; | |
3893 | ||
3894 | Next_Entity (E); | |
3895 | end loop; | |
3896 | end if; | |
3897 | ||
3898 | return Typ1; | |
3899 | end Find_Universal_Operator_Type; | |
3900 | ||
fbf5a39b AC |
3901 | -------------------------- |
3902 | -- Flag_Non_Static_Expr -- | |
3903 | -------------------------- | |
3904 | ||
3905 | procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is | |
3906 | begin | |
3907 | if Error_Posted (Expr) and then not All_Errors_Mode then | |
3908 | return; | |
3909 | else | |
3910 | Error_Msg_F (Msg, Expr); | |
3911 | Why_Not_Static (Expr); | |
3912 | end if; | |
3913 | end Flag_Non_Static_Expr; | |
3914 | ||
996ae0b0 RK |
3915 | -------------- |
3916 | -- Fold_Str -- | |
3917 | -------------- | |
3918 | ||
fbf5a39b | 3919 | procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is |
996ae0b0 RK |
3920 | Loc : constant Source_Ptr := Sloc (N); |
3921 | Typ : constant Entity_Id := Etype (N); | |
3922 | ||
3923 | begin | |
3924 | Rewrite (N, Make_String_Literal (Loc, Strval => Val)); | |
fbf5a39b AC |
3925 | |
3926 | -- We now have the literal with the right value, both the actual type | |
3927 | -- and the expected type of this literal are taken from the expression | |
3928 | -- that was evaluated. | |
3929 | ||
3930 | Analyze (N); | |
3931 | Set_Is_Static_Expression (N, Static); | |
3932 | Set_Etype (N, Typ); | |
3933 | Resolve (N); | |
996ae0b0 RK |
3934 | end Fold_Str; |
3935 | ||
3936 | --------------- | |
3937 | -- Fold_Uint -- | |
3938 | --------------- | |
3939 | ||
fbf5a39b | 3940 | procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is |
996ae0b0 | 3941 | Loc : constant Source_Ptr := Sloc (N); |
fbf5a39b AC |
3942 | Typ : Entity_Id := Etype (N); |
3943 | Ent : Entity_Id; | |
996ae0b0 RK |
3944 | |
3945 | begin | |
22cb89b5 AC |
3946 | -- If we are folding a named number, retain the entity in the literal, |
3947 | -- for ASIS use. | |
fbf5a39b AC |
3948 | |
3949 | if Is_Entity_Name (N) | |
3950 | and then Ekind (Entity (N)) = E_Named_Integer | |
3951 | then | |
3952 | Ent := Entity (N); | |
3953 | else | |
3954 | Ent := Empty; | |
3955 | end if; | |
3956 | ||
3957 | if Is_Private_Type (Typ) then | |
3958 | Typ := Full_View (Typ); | |
3959 | end if; | |
3960 | ||
f3d57416 | 3961 | -- For a result of type integer, substitute an N_Integer_Literal node |
996ae0b0 | 3962 | -- for the result of the compile time evaluation of the expression. |
cd2fb920 ES |
3963 | -- For ASIS use, set a link to the original named number when not in |
3964 | -- a generic context. | |
996ae0b0 | 3965 | |
fbf5a39b | 3966 | if Is_Integer_Type (Typ) then |
996ae0b0 | 3967 | Rewrite (N, Make_Integer_Literal (Loc, Val)); |
cd2fb920 | 3968 | |
fbf5a39b | 3969 | Set_Original_Entity (N, Ent); |
996ae0b0 RK |
3970 | |
3971 | -- Otherwise we have an enumeration type, and we substitute either | |
3972 | -- an N_Identifier or N_Character_Literal to represent the enumeration | |
3973 | -- literal corresponding to the given value, which must always be in | |
3974 | -- range, because appropriate tests have already been made for this. | |
3975 | ||
fbf5a39b | 3976 | else pragma Assert (Is_Enumeration_Type (Typ)); |
996ae0b0 RK |
3977 | Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc)); |
3978 | end if; | |
3979 | ||
3980 | -- We now have the literal with the right value, both the actual type | |
3981 | -- and the expected type of this literal are taken from the expression | |
3982 | -- that was evaluated. | |
3983 | ||
3984 | Analyze (N); | |
fbf5a39b | 3985 | Set_Is_Static_Expression (N, Static); |
996ae0b0 | 3986 | Set_Etype (N, Typ); |
fbf5a39b | 3987 | Resolve (N); |
996ae0b0 RK |
3988 | end Fold_Uint; |
3989 | ||
3990 | ---------------- | |
3991 | -- Fold_Ureal -- | |
3992 | ---------------- | |
3993 | ||
fbf5a39b | 3994 | procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is |
996ae0b0 RK |
3995 | Loc : constant Source_Ptr := Sloc (N); |
3996 | Typ : constant Entity_Id := Etype (N); | |
fbf5a39b | 3997 | Ent : Entity_Id; |
996ae0b0 RK |
3998 | |
3999 | begin | |
22cb89b5 AC |
4000 | -- If we are folding a named number, retain the entity in the literal, |
4001 | -- for ASIS use. | |
fbf5a39b AC |
4002 | |
4003 | if Is_Entity_Name (N) | |
4004 | and then Ekind (Entity (N)) = E_Named_Real | |
4005 | then | |
4006 | Ent := Entity (N); | |
4007 | else | |
4008 | Ent := Empty; | |
4009 | end if; | |
4010 | ||
996ae0b0 | 4011 | Rewrite (N, Make_Real_Literal (Loc, Realval => Val)); |
cd2fb920 | 4012 | |
5a30024a | 4013 | -- Set link to original named number, for ASIS use |
cd2fb920 | 4014 | |
fbf5a39b | 4015 | Set_Original_Entity (N, Ent); |
996ae0b0 RK |
4016 | |
4017 | -- Both the actual and expected type comes from the original expression | |
4018 | ||
fbf5a39b AC |
4019 | Analyze (N); |
4020 | Set_Is_Static_Expression (N, Static); | |
996ae0b0 | 4021 | Set_Etype (N, Typ); |
fbf5a39b | 4022 | Resolve (N); |
996ae0b0 RK |
4023 | end Fold_Ureal; |
4024 | ||
4025 | --------------- | |
4026 | -- From_Bits -- | |
4027 | --------------- | |
4028 | ||
4029 | function From_Bits (B : Bits; T : Entity_Id) return Uint is | |
4030 | V : Uint := Uint_0; | |
4031 | ||
4032 | begin | |
4033 | for J in 0 .. B'Last loop | |
4034 | if B (J) then | |
4035 | V := V + 2 ** J; | |
4036 | end if; | |
4037 | end loop; | |
4038 | ||
4039 | if Non_Binary_Modulus (T) then | |
4040 | V := V mod Modulus (T); | |
4041 | end if; | |
4042 | ||
4043 | return V; | |
4044 | end From_Bits; | |
4045 | ||
4046 | -------------------- | |
4047 | -- Get_String_Val -- | |
4048 | -------------------- | |
4049 | ||
4050 | function Get_String_Val (N : Node_Id) return Node_Id is | |
4051 | begin | |
4052 | if Nkind (N) = N_String_Literal then | |
4053 | return N; | |
4054 | ||
4055 | elsif Nkind (N) = N_Character_Literal then | |
4056 | return N; | |
4057 | ||
4058 | else | |
4059 | pragma Assert (Is_Entity_Name (N)); | |
4060 | return Get_String_Val (Constant_Value (Entity (N))); | |
4061 | end if; | |
4062 | end Get_String_Val; | |
4063 | ||
fbf5a39b AC |
4064 | ---------------- |
4065 | -- Initialize -- | |
4066 | ---------------- | |
4067 | ||
4068 | procedure Initialize is | |
4069 | begin | |
4070 | CV_Cache := (others => (Node_High_Bound, Uint_0)); | |
4071 | end Initialize; | |
4072 | ||
996ae0b0 RK |
4073 | -------------------- |
4074 | -- In_Subrange_Of -- | |
4075 | -------------------- | |
4076 | ||
4077 | function In_Subrange_Of | |
c27f2f15 RD |
4078 | (T1 : Entity_Id; |
4079 | T2 : Entity_Id; | |
4080 | Fixed_Int : Boolean := False) return Boolean | |
996ae0b0 RK |
4081 | is |
4082 | L1 : Node_Id; | |
4083 | H1 : Node_Id; | |
4084 | ||
4085 | L2 : Node_Id; | |
4086 | H2 : Node_Id; | |
4087 | ||
4088 | begin | |
4089 | if T1 = T2 or else Is_Subtype_Of (T1, T2) then | |
4090 | return True; | |
4091 | ||
4092 | -- Never in range if both types are not scalar. Don't know if this can | |
4093 | -- actually happen, but just in case. | |
4094 | ||
4095 | elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then | |
4096 | return False; | |
4097 | ||
d79e621a GD |
4098 | -- If T1 has infinities but T2 doesn't have infinities, then T1 is |
4099 | -- definitely not compatible with T2. | |
4100 | ||
4101 | elsif Is_Floating_Point_Type (T1) | |
4102 | and then Has_Infinities (T1) | |
4103 | and then Is_Floating_Point_Type (T2) | |
4104 | and then not Has_Infinities (T2) | |
4105 | then | |
4106 | return False; | |
4107 | ||
996ae0b0 RK |
4108 | else |
4109 | L1 := Type_Low_Bound (T1); | |
4110 | H1 := Type_High_Bound (T1); | |
4111 | ||
4112 | L2 := Type_Low_Bound (T2); | |
4113 | H2 := Type_High_Bound (T2); | |
4114 | ||
4115 | -- Check bounds to see if comparison possible at compile time | |
4116 | ||
c27f2f15 | 4117 | if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE |
996ae0b0 | 4118 | and then |
c27f2f15 | 4119 | Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE |
996ae0b0 RK |
4120 | then |
4121 | return True; | |
4122 | end if; | |
4123 | ||
4124 | -- If bounds not comparable at compile time, then the bounds of T2 | |
4125 | -- must be compile time known or we cannot answer the query. | |
4126 | ||
4127 | if not Compile_Time_Known_Value (L2) | |
4128 | or else not Compile_Time_Known_Value (H2) | |
4129 | then | |
4130 | return False; | |
4131 | end if; | |
4132 | ||
4133 | -- If the bounds of T1 are know at compile time then use these | |
4134 | -- ones, otherwise use the bounds of the base type (which are of | |
4135 | -- course always static). | |
4136 | ||
4137 | if not Compile_Time_Known_Value (L1) then | |
4138 | L1 := Type_Low_Bound (Base_Type (T1)); | |
4139 | end if; | |
4140 | ||
4141 | if not Compile_Time_Known_Value (H1) then | |
4142 | H1 := Type_High_Bound (Base_Type (T1)); | |
4143 | end if; | |
4144 | ||
4145 | -- Fixed point types should be considered as such only if | |
4146 | -- flag Fixed_Int is set to False. | |
4147 | ||
4148 | if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2) | |
4149 | or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int) | |
4150 | or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int) | |
4151 | then | |
4152 | return | |
4153 | Expr_Value_R (L2) <= Expr_Value_R (L1) | |
4154 | and then | |
4155 | Expr_Value_R (H2) >= Expr_Value_R (H1); | |
4156 | ||
4157 | else | |
4158 | return | |
4159 | Expr_Value (L2) <= Expr_Value (L1) | |
4160 | and then | |
4161 | Expr_Value (H2) >= Expr_Value (H1); | |
4162 | ||
4163 | end if; | |
4164 | end if; | |
4165 | ||
4166 | -- If any exception occurs, it means that we have some bug in the compiler | |
f3d57416 | 4167 | -- possibly triggered by a previous error, or by some unforeseen peculiar |
996ae0b0 RK |
4168 | -- occurrence. However, this is only an optimization attempt, so there is |
4169 | -- really no point in crashing the compiler. Instead we just decide, too | |
4170 | -- bad, we can't figure out the answer in this case after all. | |
4171 | ||
4172 | exception | |
4173 | when others => | |
4174 | ||
4175 | -- Debug flag K disables this behavior (useful for debugging) | |
4176 | ||
4177 | if Debug_Flag_K then | |
4178 | raise; | |
4179 | else | |
4180 | return False; | |
4181 | end if; | |
4182 | end In_Subrange_Of; | |
4183 | ||
4184 | ----------------- | |
4185 | -- Is_In_Range -- | |
4186 | ----------------- | |
4187 | ||
4188 | function Is_In_Range | |
c800f862 RD |
4189 | (N : Node_Id; |
4190 | Typ : Entity_Id; | |
4191 | Assume_Valid : Boolean := False; | |
4192 | Fixed_Int : Boolean := False; | |
4193 | Int_Real : Boolean := False) return Boolean | |
996ae0b0 | 4194 | is |
996ae0b0 | 4195 | begin |
305caf42 AC |
4196 | return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) |
4197 | = In_Range; | |
996ae0b0 RK |
4198 | end Is_In_Range; |
4199 | ||
4200 | ------------------- | |
4201 | -- Is_Null_Range -- | |
4202 | ------------------- | |
4203 | ||
4204 | function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is | |
4205 | Typ : constant Entity_Id := Etype (Lo); | |
4206 | ||
4207 | begin | |
4208 | if not Compile_Time_Known_Value (Lo) | |
4209 | or else not Compile_Time_Known_Value (Hi) | |
4210 | then | |
4211 | return False; | |
4212 | end if; | |
4213 | ||
4214 | if Is_Discrete_Type (Typ) then | |
4215 | return Expr_Value (Lo) > Expr_Value (Hi); | |
4216 | ||
4217 | else | |
4218 | pragma Assert (Is_Real_Type (Typ)); | |
4219 | return Expr_Value_R (Lo) > Expr_Value_R (Hi); | |
4220 | end if; | |
4221 | end Is_Null_Range; | |
4222 | ||
4223 | ----------------------------- | |
4224 | -- Is_OK_Static_Expression -- | |
4225 | ----------------------------- | |
4226 | ||
4227 | function Is_OK_Static_Expression (N : Node_Id) return Boolean is | |
4228 | begin | |
4229 | return Is_Static_Expression (N) | |
4230 | and then not Raises_Constraint_Error (N); | |
4231 | end Is_OK_Static_Expression; | |
4232 | ||
4233 | ------------------------ | |
4234 | -- Is_OK_Static_Range -- | |
4235 | ------------------------ | |
4236 | ||
4237 | -- A static range is a range whose bounds are static expressions, or a | |
4238 | -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)). | |
4239 | -- We have already converted range attribute references, so we get the | |
4240 | -- "or" part of this rule without needing a special test. | |
4241 | ||
4242 | function Is_OK_Static_Range (N : Node_Id) return Boolean is | |
4243 | begin | |
4244 | return Is_OK_Static_Expression (Low_Bound (N)) | |
4245 | and then Is_OK_Static_Expression (High_Bound (N)); | |
4246 | end Is_OK_Static_Range; | |
4247 | ||
4248 | -------------------------- | |
4249 | -- Is_OK_Static_Subtype -- | |
4250 | -------------------------- | |
4251 | ||
22cb89b5 AC |
4252 | -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where |
4253 | -- neither bound raises constraint error when evaluated. | |
996ae0b0 RK |
4254 | |
4255 | function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is | |
4256 | Base_T : constant Entity_Id := Base_Type (Typ); | |
4257 | Anc_Subt : Entity_Id; | |
4258 | ||
4259 | begin | |
4260 | -- First a quick check on the non static subtype flag. As described | |
4261 | -- in further detail in Einfo, this flag is not decisive in all cases, | |
4262 | -- but if it is set, then the subtype is definitely non-static. | |
4263 | ||
4264 | if Is_Non_Static_Subtype (Typ) then | |
4265 | return False; | |
4266 | end if; | |
4267 | ||
4268 | Anc_Subt := Ancestor_Subtype (Typ); | |
4269 | ||
4270 | if Anc_Subt = Empty then | |
4271 | Anc_Subt := Base_T; | |
4272 | end if; | |
4273 | ||
4274 | if Is_Generic_Type (Root_Type (Base_T)) | |
4275 | or else Is_Generic_Actual_Type (Base_T) | |
4276 | then | |
4277 | return False; | |
4278 | ||
4279 | -- String types | |
4280 | ||
4281 | elsif Is_String_Type (Typ) then | |
4282 | return | |
4283 | Ekind (Typ) = E_String_Literal_Subtype | |
4284 | or else | |
4285 | (Is_OK_Static_Subtype (Component_Type (Typ)) | |
4286 | and then Is_OK_Static_Subtype (Etype (First_Index (Typ)))); | |
4287 | ||
4288 | -- Scalar types | |
4289 | ||
4290 | elsif Is_Scalar_Type (Typ) then | |
4291 | if Base_T = Typ then | |
4292 | return True; | |
4293 | ||
4294 | else | |
22cb89b5 AC |
4295 | -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use |
4296 | -- Get_Type_{Low,High}_Bound. | |
996ae0b0 RK |
4297 | |
4298 | return Is_OK_Static_Subtype (Anc_Subt) | |
4299 | and then Is_OK_Static_Expression (Type_Low_Bound (Typ)) | |
4300 | and then Is_OK_Static_Expression (Type_High_Bound (Typ)); | |
4301 | end if; | |
4302 | ||
4303 | -- Types other than string and scalar types are never static | |
4304 | ||
4305 | else | |
4306 | return False; | |
4307 | end if; | |
4308 | end Is_OK_Static_Subtype; | |
4309 | ||
4310 | --------------------- | |
4311 | -- Is_Out_Of_Range -- | |
4312 | --------------------- | |
4313 | ||
4314 | function Is_Out_Of_Range | |
1c7717c3 AC |
4315 | (N : Node_Id; |
4316 | Typ : Entity_Id; | |
c800f862 | 4317 | Assume_Valid : Boolean := False; |
1c7717c3 AC |
4318 | Fixed_Int : Boolean := False; |
4319 | Int_Real : Boolean := False) return Boolean | |
996ae0b0 | 4320 | is |
996ae0b0 | 4321 | begin |
305caf42 AC |
4322 | return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) |
4323 | = Out_Of_Range; | |
996ae0b0 RK |
4324 | end Is_Out_Of_Range; |
4325 | ||
4326 | --------------------- | |
4327 | -- Is_Static_Range -- | |
4328 | --------------------- | |
4329 | ||
4330 | -- A static range is a range whose bounds are static expressions, or a | |
4331 | -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)). | |
4332 | -- We have already converted range attribute references, so we get the | |
4333 | -- "or" part of this rule without needing a special test. | |
4334 | ||
4335 | function Is_Static_Range (N : Node_Id) return Boolean is | |
4336 | begin | |
4337 | return Is_Static_Expression (Low_Bound (N)) | |
4338 | and then Is_Static_Expression (High_Bound (N)); | |
4339 | end Is_Static_Range; | |
4340 | ||
4341 | ----------------------- | |
4342 | -- Is_Static_Subtype -- | |
4343 | ----------------------- | |
4344 | ||
82c80734 | 4345 | -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) |
996ae0b0 RK |
4346 | |
4347 | function Is_Static_Subtype (Typ : Entity_Id) return Boolean is | |
4348 | Base_T : constant Entity_Id := Base_Type (Typ); | |
4349 | Anc_Subt : Entity_Id; | |
4350 | ||
4351 | begin | |
4352 | -- First a quick check on the non static subtype flag. As described | |
4353 | -- in further detail in Einfo, this flag is not decisive in all cases, | |
4354 | -- but if it is set, then the subtype is definitely non-static. | |
4355 | ||
4356 | if Is_Non_Static_Subtype (Typ) then | |
4357 | return False; | |
4358 | end if; | |
4359 | ||
4360 | Anc_Subt := Ancestor_Subtype (Typ); | |
4361 | ||
4362 | if Anc_Subt = Empty then | |
4363 | Anc_Subt := Base_T; | |
4364 | end if; | |
4365 | ||
4366 | if Is_Generic_Type (Root_Type (Base_T)) | |
4367 | or else Is_Generic_Actual_Type (Base_T) | |
4368 | then | |
4369 | return False; | |
4370 | ||
4371 | -- String types | |
4372 | ||
4373 | elsif Is_String_Type (Typ) then | |
4374 | return | |
4375 | Ekind (Typ) = E_String_Literal_Subtype | |
4376 | or else | |
4377 | (Is_Static_Subtype (Component_Type (Typ)) | |
4378 | and then Is_Static_Subtype (Etype (First_Index (Typ)))); | |
4379 | ||
4380 | -- Scalar types | |
4381 | ||
4382 | elsif Is_Scalar_Type (Typ) then | |
4383 | if Base_T = Typ then | |
4384 | return True; | |
4385 | ||
4386 | else | |
4387 | return Is_Static_Subtype (Anc_Subt) | |
4388 | and then Is_Static_Expression (Type_Low_Bound (Typ)) | |
4389 | and then Is_Static_Expression (Type_High_Bound (Typ)); | |
4390 | end if; | |
4391 | ||
4392 | -- Types other than string and scalar types are never static | |
4393 | ||
4394 | else | |
4395 | return False; | |
4396 | end if; | |
4397 | end Is_Static_Subtype; | |
4398 | ||
4399 | -------------------- | |
4400 | -- Not_Null_Range -- | |
4401 | -------------------- | |
4402 | ||
4403 | function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is | |
4404 | Typ : constant Entity_Id := Etype (Lo); | |
4405 | ||
4406 | begin | |
4407 | if not Compile_Time_Known_Value (Lo) | |
4408 | or else not Compile_Time_Known_Value (Hi) | |
4409 | then | |
4410 | return False; | |
4411 | end if; | |
4412 | ||
4413 | if Is_Discrete_Type (Typ) then | |
4414 | return Expr_Value (Lo) <= Expr_Value (Hi); | |
4415 | ||
4416 | else | |
4417 | pragma Assert (Is_Real_Type (Typ)); | |
4418 | ||
4419 | return Expr_Value_R (Lo) <= Expr_Value_R (Hi); | |
4420 | end if; | |
4421 | end Not_Null_Range; | |
4422 | ||
4423 | ------------- | |
4424 | -- OK_Bits -- | |
4425 | ------------- | |
4426 | ||
4427 | function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is | |
4428 | begin | |
4429 | -- We allow a maximum of 500,000 bits which seems a reasonable limit | |
4430 | ||
4431 | if Bits < 500_000 then | |
4432 | return True; | |
4433 | ||
4434 | else | |
4435 | Error_Msg_N ("static value too large, capacity exceeded", N); | |
4436 | return False; | |
4437 | end if; | |
4438 | end OK_Bits; | |
4439 | ||
4440 | ------------------ | |
4441 | -- Out_Of_Range -- | |
4442 | ------------------ | |
4443 | ||
4444 | procedure Out_Of_Range (N : Node_Id) is | |
4445 | begin | |
4446 | -- If we have the static expression case, then this is an illegality | |
4447 | -- in Ada 95 mode, except that in an instance, we never generate an | |
22cb89b5 AC |
4448 | -- error (if the error is legitimate, it was already diagnosed in the |
4449 | -- template). The expression to compute the length of a packed array is | |
4450 | -- attached to the array type itself, and deserves a separate message. | |
996ae0b0 RK |
4451 | |
4452 | if Is_Static_Expression (N) | |
4453 | and then not In_Instance | |
fbf5a39b | 4454 | and then not In_Inlined_Body |
0ab80019 | 4455 | and then Ada_Version >= Ada_95 |
996ae0b0 | 4456 | then |
996ae0b0 RK |
4457 | if Nkind (Parent (N)) = N_Defining_Identifier |
4458 | and then Is_Array_Type (Parent (N)) | |
4459 | and then Present (Packed_Array_Type (Parent (N))) | |
4460 | and then Present (First_Rep_Item (Parent (N))) | |
4461 | then | |
4462 | Error_Msg_N | |
4463 | ("length of packed array must not exceed Integer''Last", | |
4464 | First_Rep_Item (Parent (N))); | |
4465 | Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1)); | |
4466 | ||
4467 | else | |
4468 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 4469 | (N, "value not in range of}", CE_Range_Check_Failed); |
996ae0b0 RK |
4470 | end if; |
4471 | ||
22cb89b5 AC |
4472 | -- Here we generate a warning for the Ada 83 case, or when we are in an |
4473 | -- instance, or when we have a non-static expression case. | |
996ae0b0 RK |
4474 | |
4475 | else | |
996ae0b0 | 4476 | Apply_Compile_Time_Constraint_Error |
07fc65c4 | 4477 | (N, "value not in range of}?", CE_Range_Check_Failed); |
996ae0b0 RK |
4478 | end if; |
4479 | end Out_Of_Range; | |
4480 | ||
4481 | ------------------------- | |
4482 | -- Rewrite_In_Raise_CE -- | |
4483 | ------------------------- | |
4484 | ||
4485 | procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is | |
4486 | Typ : constant Entity_Id := Etype (N); | |
4487 | ||
4488 | begin | |
22cb89b5 AC |
4489 | -- If we want to raise CE in the condition of a N_Raise_CE node |
4490 | -- we may as well get rid of the condition. | |
996ae0b0 RK |
4491 | |
4492 | if Present (Parent (N)) | |
4493 | and then Nkind (Parent (N)) = N_Raise_Constraint_Error | |
4494 | then | |
4495 | Set_Condition (Parent (N), Empty); | |
4496 | ||
22cb89b5 AC |
4497 | -- If the expression raising CE is a N_Raise_CE node, we can use that |
4498 | -- one. We just preserve the type of the context. | |
996ae0b0 RK |
4499 | |
4500 | elsif Nkind (Exp) = N_Raise_Constraint_Error then | |
4501 | Rewrite (N, Exp); | |
4502 | Set_Etype (N, Typ); | |
4503 | ||
22cb89b5 | 4504 | -- Else build an explcit N_Raise_CE |
996ae0b0 RK |
4505 | |
4506 | else | |
07fc65c4 GB |
4507 | Rewrite (N, |
4508 | Make_Raise_Constraint_Error (Sloc (Exp), | |
4509 | Reason => CE_Range_Check_Failed)); | |
996ae0b0 RK |
4510 | Set_Raises_Constraint_Error (N); |
4511 | Set_Etype (N, Typ); | |
4512 | end if; | |
4513 | end Rewrite_In_Raise_CE; | |
4514 | ||
4515 | --------------------- | |
4516 | -- String_Type_Len -- | |
4517 | --------------------- | |
4518 | ||
4519 | function String_Type_Len (Stype : Entity_Id) return Uint is | |
4520 | NT : constant Entity_Id := Etype (First_Index (Stype)); | |
4521 | T : Entity_Id; | |
4522 | ||
4523 | begin | |
4524 | if Is_OK_Static_Subtype (NT) then | |
4525 | T := NT; | |
4526 | else | |
4527 | T := Base_Type (NT); | |
4528 | end if; | |
4529 | ||
4530 | return Expr_Value (Type_High_Bound (T)) - | |
4531 | Expr_Value (Type_Low_Bound (T)) + 1; | |
4532 | end String_Type_Len; | |
4533 | ||
4534 | ------------------------------------ | |
4535 | -- Subtypes_Statically_Compatible -- | |
4536 | ------------------------------------ | |
4537 | ||
4538 | function Subtypes_Statically_Compatible | |
f44fe430 RD |
4539 | (T1 : Entity_Id; |
4540 | T2 : Entity_Id) return Boolean | |
996ae0b0 RK |
4541 | is |
4542 | begin | |
4543 | if Is_Scalar_Type (T1) then | |
4544 | ||
4545 | -- Definitely compatible if we match | |
4546 | ||
4547 | if Subtypes_Statically_Match (T1, T2) then | |
4548 | return True; | |
4549 | ||
4550 | -- If either subtype is nonstatic then they're not compatible | |
4551 | ||
4552 | elsif not Is_Static_Subtype (T1) | |
4553 | or else not Is_Static_Subtype (T2) | |
4554 | then | |
4555 | return False; | |
4556 | ||
4557 | -- If either type has constraint error bounds, then consider that | |
4558 | -- they match to avoid junk cascaded errors here. | |
4559 | ||
4560 | elsif not Is_OK_Static_Subtype (T1) | |
4561 | or else not Is_OK_Static_Subtype (T2) | |
4562 | then | |
4563 | return True; | |
4564 | ||
4565 | -- Base types must match, but we don't check that (should | |
4566 | -- we???) but we do at least check that both types are | |
4567 | -- real, or both types are not real. | |
4568 | ||
fbf5a39b | 4569 | elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then |
996ae0b0 RK |
4570 | return False; |
4571 | ||
4572 | -- Here we check the bounds | |
4573 | ||
4574 | else | |
4575 | declare | |
4576 | LB1 : constant Node_Id := Type_Low_Bound (T1); | |
4577 | HB1 : constant Node_Id := Type_High_Bound (T1); | |
4578 | LB2 : constant Node_Id := Type_Low_Bound (T2); | |
4579 | HB2 : constant Node_Id := Type_High_Bound (T2); | |
4580 | ||
4581 | begin | |
4582 | if Is_Real_Type (T1) then | |
4583 | return | |
4584 | (Expr_Value_R (LB1) > Expr_Value_R (HB1)) | |
4585 | or else | |
4586 | (Expr_Value_R (LB2) <= Expr_Value_R (LB1) | |
4587 | and then | |
4588 | Expr_Value_R (HB1) <= Expr_Value_R (HB2)); | |
4589 | ||
4590 | else | |
4591 | return | |
4592 | (Expr_Value (LB1) > Expr_Value (HB1)) | |
4593 | or else | |
4594 | (Expr_Value (LB2) <= Expr_Value (LB1) | |
4595 | and then | |
4596 | Expr_Value (HB1) <= Expr_Value (HB2)); | |
4597 | end if; | |
4598 | end; | |
4599 | end if; | |
4600 | ||
4601 | elsif Is_Access_Type (T1) then | |
4602 | return not Is_Constrained (T2) | |
4603 | or else Subtypes_Statically_Match | |
4604 | (Designated_Type (T1), Designated_Type (T2)); | |
4605 | ||
4606 | else | |
4607 | return (Is_Composite_Type (T1) and then not Is_Constrained (T2)) | |
4608 | or else Subtypes_Statically_Match (T1, T2); | |
4609 | end if; | |
4610 | end Subtypes_Statically_Compatible; | |
4611 | ||
4612 | ------------------------------- | |
4613 | -- Subtypes_Statically_Match -- | |
4614 | ------------------------------- | |
4615 | ||
4616 | -- Subtypes statically match if they have statically matching constraints | |
4617 | -- (RM 4.9.1(2)). Constraints statically match if there are none, or if | |
4618 | -- they are the same identical constraint, or if they are static and the | |
4619 | -- values match (RM 4.9.1(1)). | |
4620 | ||
4621 | function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is | |
4622 | begin | |
4623 | -- A type always statically matches itself | |
4624 | ||
4625 | if T1 = T2 then | |
4626 | return True; | |
4627 | ||
4628 | -- Scalar types | |
4629 | ||
4630 | elsif Is_Scalar_Type (T1) then | |
4631 | ||
4632 | -- Base types must be the same | |
4633 | ||
4634 | if Base_Type (T1) /= Base_Type (T2) then | |
4635 | return False; | |
4636 | end if; | |
4637 | ||
4638 | -- A constrained numeric subtype never matches an unconstrained | |
4639 | -- subtype, i.e. both types must be constrained or unconstrained. | |
4640 | ||
305caf42 AC |
4641 | -- To understand the requirement for this test, see RM 4.9.1(1). |
4642 | -- As is made clear in RM 3.5.4(11), type Integer, for example is | |
4643 | -- a constrained subtype with constraint bounds matching the bounds | |
4644 | -- of its corresponding unconstrained base type. In this situation, | |
4645 | -- Integer and Integer'Base do not statically match, even though | |
4646 | -- they have the same bounds. | |
996ae0b0 | 4647 | |
22cb89b5 AC |
4648 | -- We only apply this test to types in Standard and types that appear |
4649 | -- in user programs. That way, we do not have to be too careful about | |
4650 | -- setting Is_Constrained right for Itypes. | |
996ae0b0 RK |
4651 | |
4652 | if Is_Numeric_Type (T1) | |
4653 | and then (Is_Constrained (T1) /= Is_Constrained (T2)) | |
4654 | and then (Scope (T1) = Standard_Standard | |
4655 | or else Comes_From_Source (T1)) | |
4656 | and then (Scope (T2) = Standard_Standard | |
4657 | or else Comes_From_Source (T2)) | |
4658 | then | |
4659 | return False; | |
82c80734 | 4660 | |
22cb89b5 AC |
4661 | -- A generic scalar type does not statically match its base type |
4662 | -- (AI-311). In this case we make sure that the formals, which are | |
4663 | -- first subtypes of their bases, are constrained. | |
82c80734 RD |
4664 | |
4665 | elsif Is_Generic_Type (T1) | |
4666 | and then Is_Generic_Type (T2) | |
4667 | and then (Is_Constrained (T1) /= Is_Constrained (T2)) | |
4668 | then | |
4669 | return False; | |
996ae0b0 RK |
4670 | end if; |
4671 | ||
22cb89b5 AC |
4672 | -- If there was an error in either range, then just assume the types |
4673 | -- statically match to avoid further junk errors. | |
996ae0b0 RK |
4674 | |
4675 | if Error_Posted (Scalar_Range (T1)) | |
4676 | or else | |
4677 | Error_Posted (Scalar_Range (T2)) | |
4678 | then | |
4679 | return True; | |
4680 | end if; | |
4681 | ||
4682 | -- Otherwise both types have bound that can be compared | |
4683 | ||
4684 | declare | |
4685 | LB1 : constant Node_Id := Type_Low_Bound (T1); | |
4686 | HB1 : constant Node_Id := Type_High_Bound (T1); | |
4687 | LB2 : constant Node_Id := Type_Low_Bound (T2); | |
4688 | HB2 : constant Node_Id := Type_High_Bound (T2); | |
4689 | ||
4690 | begin | |
4691 | -- If the bounds are the same tree node, then match | |
4692 | ||
4693 | if LB1 = LB2 and then HB1 = HB2 then | |
4694 | return True; | |
4695 | ||
4696 | -- Otherwise bounds must be static and identical value | |
4697 | ||
4698 | else | |
4699 | if not Is_Static_Subtype (T1) | |
4700 | or else not Is_Static_Subtype (T2) | |
4701 | then | |
4702 | return False; | |
4703 | ||
22cb89b5 AC |
4704 | -- If either type has constraint error bounds, then say that |
4705 | -- they match to avoid junk cascaded errors here. | |
996ae0b0 RK |
4706 | |
4707 | elsif not Is_OK_Static_Subtype (T1) | |
4708 | or else not Is_OK_Static_Subtype (T2) | |
4709 | then | |
4710 | return True; | |
4711 | ||
4712 | elsif Is_Real_Type (T1) then | |
4713 | return | |
4714 | (Expr_Value_R (LB1) = Expr_Value_R (LB2)) | |
4715 | and then | |
4716 | (Expr_Value_R (HB1) = Expr_Value_R (HB2)); | |
4717 | ||
4718 | else | |
4719 | return | |
4720 | Expr_Value (LB1) = Expr_Value (LB2) | |
4721 | and then | |
4722 | Expr_Value (HB1) = Expr_Value (HB2); | |
4723 | end if; | |
4724 | end if; | |
4725 | end; | |
4726 | ||
4727 | -- Type with discriminants | |
4728 | ||
4729 | elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then | |
6eaf4095 | 4730 | |
c2bf339e GD |
4731 | -- Because of view exchanges in multiple instantiations, conformance |
4732 | -- checking might try to match a partial view of a type with no | |
4733 | -- discriminants with a full view that has defaulted discriminants. | |
4734 | -- In such a case, use the discriminant constraint of the full view, | |
4735 | -- which must exist because we know that the two subtypes have the | |
4736 | -- same base type. | |
6eaf4095 | 4737 | |
996ae0b0 | 4738 | if Has_Discriminants (T1) /= Has_Discriminants (T2) then |
c2bf339e GD |
4739 | if In_Instance then |
4740 | if Is_Private_Type (T2) | |
4741 | and then Present (Full_View (T2)) | |
4742 | and then Has_Discriminants (Full_View (T2)) | |
4743 | then | |
4744 | return Subtypes_Statically_Match (T1, Full_View (T2)); | |
4745 | ||
4746 | elsif Is_Private_Type (T1) | |
4747 | and then Present (Full_View (T1)) | |
4748 | and then Has_Discriminants (Full_View (T1)) | |
4749 | then | |
4750 | return Subtypes_Statically_Match (Full_View (T1), T2); | |
4751 | ||
4752 | else | |
4753 | return False; | |
4754 | end if; | |
6eaf4095 ES |
4755 | else |
4756 | return False; | |
4757 | end if; | |
996ae0b0 RK |
4758 | end if; |
4759 | ||
4760 | declare | |
4761 | DL1 : constant Elist_Id := Discriminant_Constraint (T1); | |
4762 | DL2 : constant Elist_Id := Discriminant_Constraint (T2); | |
4763 | ||
13f34a3f RD |
4764 | DA1 : Elmt_Id; |
4765 | DA2 : Elmt_Id; | |
996ae0b0 RK |
4766 | |
4767 | begin | |
4768 | if DL1 = DL2 then | |
4769 | return True; | |
996ae0b0 RK |
4770 | elsif Is_Constrained (T1) /= Is_Constrained (T2) then |
4771 | return False; | |
4772 | end if; | |
4773 | ||
13f34a3f | 4774 | -- Now loop through the discriminant constraints |
996ae0b0 | 4775 | |
13f34a3f RD |
4776 | -- Note: the guard here seems necessary, since it is possible at |
4777 | -- least for DL1 to be No_Elist. Not clear this is reasonable ??? | |
996ae0b0 | 4778 | |
13f34a3f RD |
4779 | if Present (DL1) and then Present (DL2) then |
4780 | DA1 := First_Elmt (DL1); | |
4781 | DA2 := First_Elmt (DL2); | |
4782 | while Present (DA1) loop | |
4783 | declare | |
4784 | Expr1 : constant Node_Id := Node (DA1); | |
4785 | Expr2 : constant Node_Id := Node (DA2); | |
996ae0b0 | 4786 | |
13f34a3f RD |
4787 | begin |
4788 | if not Is_Static_Expression (Expr1) | |
4789 | or else not Is_Static_Expression (Expr2) | |
4790 | then | |
4791 | return False; | |
996ae0b0 | 4792 | |
13f34a3f RD |
4793 | -- If either expression raised a constraint error, |
4794 | -- consider the expressions as matching, since this | |
4795 | -- helps to prevent cascading errors. | |
4796 | ||
4797 | elsif Raises_Constraint_Error (Expr1) | |
4798 | or else Raises_Constraint_Error (Expr2) | |
4799 | then | |
4800 | null; | |
4801 | ||
4802 | elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then | |
4803 | return False; | |
4804 | end if; | |
4805 | end; | |
996ae0b0 | 4806 | |
13f34a3f RD |
4807 | Next_Elmt (DA1); |
4808 | Next_Elmt (DA2); | |
4809 | end loop; | |
4810 | end if; | |
996ae0b0 RK |
4811 | end; |
4812 | ||
4813 | return True; | |
4814 | ||
22cb89b5 | 4815 | -- A definite type does not match an indefinite or classwide type. |
0356699b RD |
4816 | -- However, a generic type with unknown discriminants may be |
4817 | -- instantiated with a type with no discriminants, and conformance | |
22cb89b5 AC |
4818 | -- checking on an inherited operation may compare the actual with the |
4819 | -- subtype that renames it in the instance. | |
996ae0b0 RK |
4820 | |
4821 | elsif | |
4822 | Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2) | |
4823 | then | |
7a3f77d2 AC |
4824 | return |
4825 | Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2); | |
996ae0b0 RK |
4826 | |
4827 | -- Array type | |
4828 | ||
4829 | elsif Is_Array_Type (T1) then | |
4830 | ||
22cb89b5 AC |
4831 | -- If either subtype is unconstrained then both must be, and if both |
4832 | -- are unconstrained then no further checking is neede. | |
996ae0b0 RK |
4833 | |
4834 | if not Is_Constrained (T1) or else not Is_Constrained (T2) then | |
4835 | return not (Is_Constrained (T1) or else Is_Constrained (T2)); | |
4836 | end if; | |
4837 | ||
22cb89b5 AC |
4838 | -- Both subtypes are constrained, so check that the index subtypes |
4839 | -- statically match. | |
996ae0b0 RK |
4840 | |
4841 | declare | |
4842 | Index1 : Node_Id := First_Index (T1); | |
4843 | Index2 : Node_Id := First_Index (T2); | |
4844 | ||
4845 | begin | |
4846 | while Present (Index1) loop | |
4847 | if not | |
4848 | Subtypes_Statically_Match (Etype (Index1), Etype (Index2)) | |
4849 | then | |
4850 | return False; | |
4851 | end if; | |
4852 | ||
4853 | Next_Index (Index1); | |
4854 | Next_Index (Index2); | |
4855 | end loop; | |
4856 | ||
4857 | return True; | |
4858 | end; | |
4859 | ||
4860 | elsif Is_Access_Type (T1) then | |
b5bd964f ES |
4861 | if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then |
4862 | return False; | |
4863 | ||
e1b871e9 AC |
4864 | elsif Ekind_In (T1, E_Access_Subprogram_Type, |
4865 | E_Anonymous_Access_Subprogram_Type) | |
7a3f77d2 | 4866 | then |
b5bd964f ES |
4867 | return |
4868 | Subtype_Conformant | |
4869 | (Designated_Type (T1), | |
bb98fe75 | 4870 | Designated_Type (T2)); |
b5bd964f ES |
4871 | else |
4872 | return | |
4873 | Subtypes_Statically_Match | |
4874 | (Designated_Type (T1), | |
4875 | Designated_Type (T2)) | |
4876 | and then Is_Access_Constant (T1) = Is_Access_Constant (T2); | |
4877 | end if; | |
996ae0b0 RK |
4878 | |
4879 | -- All other types definitely match | |
4880 | ||
4881 | else | |
4882 | return True; | |
4883 | end if; | |
4884 | end Subtypes_Statically_Match; | |
4885 | ||
4886 | ---------- | |
4887 | -- Test -- | |
4888 | ---------- | |
4889 | ||
4890 | function Test (Cond : Boolean) return Uint is | |
4891 | begin | |
4892 | if Cond then | |
4893 | return Uint_1; | |
4894 | else | |
4895 | return Uint_0; | |
4896 | end if; | |
4897 | end Test; | |
4898 | ||
4899 | --------------------------------- | |
4900 | -- Test_Expression_Is_Foldable -- | |
4901 | --------------------------------- | |
4902 | ||
4903 | -- One operand case | |
4904 | ||
4905 | procedure Test_Expression_Is_Foldable | |
4906 | (N : Node_Id; | |
4907 | Op1 : Node_Id; | |
4908 | Stat : out Boolean; | |
4909 | Fold : out Boolean) | |
4910 | is | |
4911 | begin | |
4912 | Stat := False; | |
0356699b RD |
4913 | Fold := False; |
4914 | ||
4915 | if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then | |
4916 | return; | |
4917 | end if; | |
996ae0b0 RK |
4918 | |
4919 | -- If operand is Any_Type, just propagate to result and do not | |
4920 | -- try to fold, this prevents cascaded errors. | |
4921 | ||
4922 | if Etype (Op1) = Any_Type then | |
4923 | Set_Etype (N, Any_Type); | |
996ae0b0 RK |
4924 | return; |
4925 | ||
4926 | -- If operand raises constraint error, then replace node N with the | |
4927 | -- raise constraint error node, and we are obviously not foldable. | |
4928 | -- Note that this replacement inherits the Is_Static_Expression flag | |
4929 | -- from the operand. | |
4930 | ||
4931 | elsif Raises_Constraint_Error (Op1) then | |
4932 | Rewrite_In_Raise_CE (N, Op1); | |
996ae0b0 RK |
4933 | return; |
4934 | ||
4935 | -- If the operand is not static, then the result is not static, and | |
4936 | -- all we have to do is to check the operand since it is now known | |
4937 | -- to appear in a non-static context. | |
4938 | ||
4939 | elsif not Is_Static_Expression (Op1) then | |
4940 | Check_Non_Static_Context (Op1); | |
4941 | Fold := Compile_Time_Known_Value (Op1); | |
4942 | return; | |
4943 | ||
4944 | -- An expression of a formal modular type is not foldable because | |
4945 | -- the modulus is unknown. | |
4946 | ||
4947 | elsif Is_Modular_Integer_Type (Etype (Op1)) | |
4948 | and then Is_Generic_Type (Etype (Op1)) | |
4949 | then | |
4950 | Check_Non_Static_Context (Op1); | |
996ae0b0 RK |
4951 | return; |
4952 | ||
4953 | -- Here we have the case of an operand whose type is OK, which is | |
4954 | -- static, and which does not raise constraint error, we can fold. | |
4955 | ||
4956 | else | |
4957 | Set_Is_Static_Expression (N); | |
4958 | Fold := True; | |
4959 | Stat := True; | |
4960 | end if; | |
4961 | end Test_Expression_Is_Foldable; | |
4962 | ||
4963 | -- Two operand case | |
4964 | ||
4965 | procedure Test_Expression_Is_Foldable | |
4966 | (N : Node_Id; | |
4967 | Op1 : Node_Id; | |
4968 | Op2 : Node_Id; | |
4969 | Stat : out Boolean; | |
4970 | Fold : out Boolean) | |
4971 | is | |
4972 | Rstat : constant Boolean := Is_Static_Expression (Op1) | |
4973 | and then Is_Static_Expression (Op2); | |
4974 | ||
4975 | begin | |
4976 | Stat := False; | |
0356699b RD |
4977 | Fold := False; |
4978 | ||
4979 | if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then | |
4980 | return; | |
4981 | end if; | |
996ae0b0 RK |
4982 | |
4983 | -- If either operand is Any_Type, just propagate to result and | |
4984 | -- do not try to fold, this prevents cascaded errors. | |
4985 | ||
4986 | if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then | |
4987 | Set_Etype (N, Any_Type); | |
996ae0b0 RK |
4988 | return; |
4989 | ||
22cb89b5 AC |
4990 | -- If left operand raises constraint error, then replace node N with the |
4991 | -- Raise_Constraint_Error node, and we are obviously not foldable. | |
996ae0b0 RK |
4992 | -- Is_Static_Expression is set from the two operands in the normal way, |
4993 | -- and we check the right operand if it is in a non-static context. | |
4994 | ||
4995 | elsif Raises_Constraint_Error (Op1) then | |
4996 | if not Rstat then | |
4997 | Check_Non_Static_Context (Op2); | |
4998 | end if; | |
4999 | ||
5000 | Rewrite_In_Raise_CE (N, Op1); | |
5001 | Set_Is_Static_Expression (N, Rstat); | |
996ae0b0 RK |
5002 | return; |
5003 | ||
22cb89b5 AC |
5004 | -- Similar processing for the case of the right operand. Note that we |
5005 | -- don't use this routine for the short-circuit case, so we do not have | |
5006 | -- to worry about that special case here. | |
996ae0b0 RK |
5007 | |
5008 | elsif Raises_Constraint_Error (Op2) then | |
5009 | if not Rstat then | |
5010 | Check_Non_Static_Context (Op1); | |
5011 | end if; | |
5012 | ||
5013 | Rewrite_In_Raise_CE (N, Op2); | |
5014 | Set_Is_Static_Expression (N, Rstat); | |
996ae0b0 RK |
5015 | return; |
5016 | ||
82c80734 | 5017 | -- Exclude expressions of a generic modular type, as above |
996ae0b0 RK |
5018 | |
5019 | elsif Is_Modular_Integer_Type (Etype (Op1)) | |
5020 | and then Is_Generic_Type (Etype (Op1)) | |
5021 | then | |
5022 | Check_Non_Static_Context (Op1); | |
996ae0b0 RK |
5023 | return; |
5024 | ||
5025 | -- If result is not static, then check non-static contexts on operands | |
22cb89b5 | 5026 | -- since one of them may be static and the other one may not be static. |
996ae0b0 RK |
5027 | |
5028 | elsif not Rstat then | |
5029 | Check_Non_Static_Context (Op1); | |
5030 | Check_Non_Static_Context (Op2); | |
5031 | Fold := Compile_Time_Known_Value (Op1) | |
5032 | and then Compile_Time_Known_Value (Op2); | |
5033 | return; | |
5034 | ||
22cb89b5 AC |
5035 | -- Else result is static and foldable. Both operands are static, and |
5036 | -- neither raises constraint error, so we can definitely fold. | |
996ae0b0 RK |
5037 | |
5038 | else | |
5039 | Set_Is_Static_Expression (N); | |
5040 | Fold := True; | |
5041 | Stat := True; | |
5042 | return; | |
5043 | end if; | |
5044 | end Test_Expression_Is_Foldable; | |
5045 | ||
305caf42 AC |
5046 | ------------------- |
5047 | -- Test_In_Range -- | |
5048 | ------------------- | |
5049 | ||
5050 | function Test_In_Range | |
5051 | (N : Node_Id; | |
5052 | Typ : Entity_Id; | |
5053 | Assume_Valid : Boolean; | |
5054 | Fixed_Int : Boolean; | |
5055 | Int_Real : Boolean) return Range_Membership | |
5056 | is | |
5057 | Val : Uint; | |
5058 | Valr : Ureal; | |
5059 | ||
5060 | pragma Warnings (Off, Assume_Valid); | |
5061 | -- For now Assume_Valid is unreferenced since the current implementation | |
5062 | -- always returns Unknown if N is not a compile time known value, but we | |
5063 | -- keep the parameter to allow for future enhancements in which we try | |
5064 | -- to get the information in the variable case as well. | |
5065 | ||
5066 | begin | |
5067 | -- Universal types have no range limits, so always in range | |
5068 | ||
5069 | if Typ = Universal_Integer or else Typ = Universal_Real then | |
5070 | return In_Range; | |
5071 | ||
5072 | -- Never known if not scalar type. Don't know if this can actually | |
5073 | -- happen, but our spec allows it, so we must check! | |
5074 | ||
5075 | elsif not Is_Scalar_Type (Typ) then | |
5076 | return Unknown; | |
5077 | ||
5078 | -- Never known if this is a generic type, since the bounds of generic | |
5079 | -- types are junk. Note that if we only checked for static expressions | |
5080 | -- (instead of compile time known values) below, we would not need this | |
5081 | -- check, because values of a generic type can never be static, but they | |
5082 | -- can be known at compile time. | |
5083 | ||
5084 | elsif Is_Generic_Type (Typ) then | |
5085 | return Unknown; | |
5086 | ||
5087 | -- Never known unless we have a compile time known value | |
5088 | ||
5089 | elsif not Compile_Time_Known_Value (N) then | |
5090 | return Unknown; | |
5091 | ||
5092 | -- General processing with a known compile time value | |
5093 | ||
5094 | else | |
5095 | declare | |
5096 | Lo : Node_Id; | |
5097 | Hi : Node_Id; | |
5098 | ||
5099 | LB_Known : Boolean; | |
5100 | HB_Known : Boolean; | |
5101 | ||
5102 | begin | |
5103 | Lo := Type_Low_Bound (Typ); | |
5104 | Hi := Type_High_Bound (Typ); | |
5105 | ||
5106 | LB_Known := Compile_Time_Known_Value (Lo); | |
5107 | HB_Known := Compile_Time_Known_Value (Hi); | |
5108 | ||
5109 | -- Fixed point types should be considered as such only if flag | |
5110 | -- Fixed_Int is set to False. | |
5111 | ||
5112 | if Is_Floating_Point_Type (Typ) | |
5113 | or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int) | |
5114 | or else Int_Real | |
5115 | then | |
5116 | Valr := Expr_Value_R (N); | |
5117 | ||
5118 | if LB_Known and HB_Known then | |
5119 | if Valr >= Expr_Value_R (Lo) | |
5120 | and then | |
5121 | Valr <= Expr_Value_R (Hi) | |
5122 | then | |
5123 | return In_Range; | |
5124 | else | |
5125 | return Out_Of_Range; | |
5126 | end if; | |
5127 | ||
5128 | elsif (LB_Known and then Valr < Expr_Value_R (Lo)) | |
5129 | or else | |
5130 | (HB_Known and then Valr > Expr_Value_R (Hi)) | |
5131 | then | |
5132 | return Out_Of_Range; | |
5133 | ||
5134 | else | |
5135 | return Unknown; | |
5136 | end if; | |
5137 | ||
5138 | else | |
5139 | Val := Expr_Value (N); | |
5140 | ||
5141 | if LB_Known and HB_Known then | |
5142 | if Val >= Expr_Value (Lo) | |
5143 | and then | |
5144 | Val <= Expr_Value (Hi) | |
5145 | then | |
5146 | return In_Range; | |
5147 | else | |
5148 | return Out_Of_Range; | |
5149 | end if; | |
5150 | ||
5151 | elsif (LB_Known and then Val < Expr_Value (Lo)) | |
5152 | or else | |
5153 | (HB_Known and then Val > Expr_Value (Hi)) | |
5154 | then | |
5155 | return Out_Of_Range; | |
5156 | ||
5157 | else | |
5158 | return Unknown; | |
5159 | end if; | |
5160 | end if; | |
5161 | end; | |
5162 | end if; | |
5163 | end Test_In_Range; | |
5164 | ||
996ae0b0 RK |
5165 | -------------- |
5166 | -- To_Bits -- | |
5167 | -------------- | |
5168 | ||
5169 | procedure To_Bits (U : Uint; B : out Bits) is | |
5170 | begin | |
5171 | for J in 0 .. B'Last loop | |
5172 | B (J) := (U / (2 ** J)) mod 2 /= 0; | |
5173 | end loop; | |
5174 | end To_Bits; | |
5175 | ||
fbf5a39b AC |
5176 | -------------------- |
5177 | -- Why_Not_Static -- | |
5178 | -------------------- | |
5179 | ||
5180 | procedure Why_Not_Static (Expr : Node_Id) is | |
5181 | N : constant Node_Id := Original_Node (Expr); | |
5182 | Typ : Entity_Id; | |
5183 | E : Entity_Id; | |
5184 | ||
5185 | procedure Why_Not_Static_List (L : List_Id); | |
22cb89b5 AC |
5186 | -- A version that can be called on a list of expressions. Finds all |
5187 | -- non-static violations in any element of the list. | |
fbf5a39b AC |
5188 | |
5189 | ------------------------- | |
5190 | -- Why_Not_Static_List -- | |
5191 | ------------------------- | |
5192 | ||
5193 | procedure Why_Not_Static_List (L : List_Id) is | |
5194 | N : Node_Id; | |
5195 | ||
5196 | begin | |
5197 | if Is_Non_Empty_List (L) then | |
5198 | N := First (L); | |
5199 | while Present (N) loop | |
5200 | Why_Not_Static (N); | |
5201 | Next (N); | |
5202 | end loop; | |
5203 | end if; | |
5204 | end Why_Not_Static_List; | |
5205 | ||
5206 | -- Start of processing for Why_Not_Static | |
5207 | ||
5208 | begin | |
22cb89b5 AC |
5209 | -- If in ACATS mode (debug flag 2), then suppress all these messages, |
5210 | -- this avoids massive updates to the ACATS base line. | |
fbf5a39b AC |
5211 | |
5212 | if Debug_Flag_2 then | |
5213 | return; | |
5214 | end if; | |
5215 | ||
5216 | -- Ignore call on error or empty node | |
5217 | ||
5218 | if No (Expr) or else Nkind (Expr) = N_Error then | |
5219 | return; | |
5220 | end if; | |
5221 | ||
5222 | -- Preprocessing for sub expressions | |
5223 | ||
5224 | if Nkind (Expr) in N_Subexpr then | |
5225 | ||
5226 | -- Nothing to do if expression is static | |
5227 | ||
5228 | if Is_OK_Static_Expression (Expr) then | |
5229 | return; | |
5230 | end if; | |
5231 | ||
5232 | -- Test for constraint error raised | |
5233 | ||
5234 | if Raises_Constraint_Error (Expr) then | |
5235 | Error_Msg_N | |
5236 | ("expression raises exception, cannot be static " & | |
b11e8d6f | 5237 | "(RM 4.9(34))!", N); |
fbf5a39b AC |
5238 | return; |
5239 | end if; | |
5240 | ||
5241 | -- If no type, then something is pretty wrong, so ignore | |
5242 | ||
5243 | Typ := Etype (Expr); | |
5244 | ||
5245 | if No (Typ) then | |
5246 | return; | |
5247 | end if; | |
5248 | ||
5249 | -- Type must be scalar or string type | |
5250 | ||
5251 | if not Is_Scalar_Type (Typ) | |
5252 | and then not Is_String_Type (Typ) | |
5253 | then | |
5254 | Error_Msg_N | |
5255 | ("static expression must have scalar or string type " & | |
b11e8d6f | 5256 | "(RM 4.9(2))!", N); |
fbf5a39b AC |
5257 | return; |
5258 | end if; | |
5259 | end if; | |
5260 | ||
5261 | -- If we got through those checks, test particular node kind | |
5262 | ||
5263 | case Nkind (N) is | |
5264 | when N_Expanded_Name | N_Identifier | N_Operator_Symbol => | |
5265 | E := Entity (N); | |
5266 | ||
5267 | if Is_Named_Number (E) then | |
5268 | null; | |
5269 | ||
5270 | elsif Ekind (E) = E_Constant then | |
5271 | if not Is_Static_Expression (Constant_Value (E)) then | |
5272 | Error_Msg_NE | |
b11e8d6f | 5273 | ("& is not a static constant (RM 4.9(5))!", N, E); |
fbf5a39b AC |
5274 | end if; |
5275 | ||
5276 | else | |
5277 | Error_Msg_NE | |
5278 | ("& is not static constant or named number " & | |
b11e8d6f | 5279 | "(RM 4.9(5))!", N, E); |
fbf5a39b AC |
5280 | end if; |
5281 | ||
514d0fc5 | 5282 | when N_Binary_Op | N_Short_Circuit | N_Membership_Test => |
fbf5a39b AC |
5283 | if Nkind (N) in N_Op_Shift then |
5284 | Error_Msg_N | |
b11e8d6f | 5285 | ("shift functions are never static (RM 4.9(6,18))!", N); |
fbf5a39b AC |
5286 | |
5287 | else | |
5288 | Why_Not_Static (Left_Opnd (N)); | |
5289 | Why_Not_Static (Right_Opnd (N)); | |
5290 | end if; | |
5291 | ||
5292 | when N_Unary_Op => | |
5293 | Why_Not_Static (Right_Opnd (N)); | |
5294 | ||
5295 | when N_Attribute_Reference => | |
5296 | Why_Not_Static_List (Expressions (N)); | |
5297 | ||
5298 | E := Etype (Prefix (N)); | |
5299 | ||
5300 | if E = Standard_Void_Type then | |
5301 | return; | |
5302 | end if; | |
5303 | ||
5304 | -- Special case non-scalar'Size since this is a common error | |
5305 | ||
5306 | if Attribute_Name (N) = Name_Size then | |
5307 | Error_Msg_N | |
fdfea49e | 5308 | ("size attribute is only static for static scalar type " & |
b11e8d6f | 5309 | "(RM 4.9(7,8))", N); |
fbf5a39b AC |
5310 | |
5311 | -- Flag array cases | |
5312 | ||
5313 | elsif Is_Array_Type (E) then | |
5314 | if Attribute_Name (N) /= Name_First | |
5315 | and then | |
5316 | Attribute_Name (N) /= Name_Last | |
5317 | and then | |
5318 | Attribute_Name (N) /= Name_Length | |
5319 | then | |
5320 | Error_Msg_N | |
5321 | ("static array attribute must be Length, First, or Last " & | |
b11e8d6f | 5322 | "(RM 4.9(8))!", N); |
fbf5a39b AC |
5323 | |
5324 | -- Since we know the expression is not-static (we already | |
5325 | -- tested for this, must mean array is not static). | |
5326 | ||
5327 | else | |
5328 | Error_Msg_N | |
b11e8d6f | 5329 | ("prefix is non-static array (RM 4.9(8))!", Prefix (N)); |
fbf5a39b AC |
5330 | end if; |
5331 | ||
5332 | return; | |
5333 | ||
22cb89b5 AC |
5334 | -- Special case generic types, since again this is a common source |
5335 | -- of confusion. | |
fbf5a39b AC |
5336 | |
5337 | elsif Is_Generic_Actual_Type (E) | |
5338 | or else | |
5339 | Is_Generic_Type (E) | |
5340 | then | |
5341 | Error_Msg_N | |
5342 | ("attribute of generic type is never static " & | |
b11e8d6f | 5343 | "(RM 4.9(7,8))!", N); |
fbf5a39b AC |
5344 | |
5345 | elsif Is_Static_Subtype (E) then | |
5346 | null; | |
5347 | ||
5348 | elsif Is_Scalar_Type (E) then | |
5349 | Error_Msg_N | |
5350 | ("prefix type for attribute is not static scalar subtype " & | |
b11e8d6f | 5351 | "(RM 4.9(7))!", N); |
fbf5a39b AC |
5352 | |
5353 | else | |
5354 | Error_Msg_N | |
5355 | ("static attribute must apply to array/scalar type " & | |
b11e8d6f | 5356 | "(RM 4.9(7,8))!", N); |
fbf5a39b AC |
5357 | end if; |
5358 | ||
5359 | when N_String_Literal => | |
5360 | Error_Msg_N | |
b11e8d6f | 5361 | ("subtype of string literal is non-static (RM 4.9(4))!", N); |
fbf5a39b AC |
5362 | |
5363 | when N_Explicit_Dereference => | |
5364 | Error_Msg_N | |
b11e8d6f | 5365 | ("explicit dereference is never static (RM 4.9)!", N); |
fbf5a39b AC |
5366 | |
5367 | when N_Function_Call => | |
5368 | Why_Not_Static_List (Parameter_Associations (N)); | |
b11e8d6f | 5369 | Error_Msg_N ("non-static function call (RM 4.9(6,18))!", N); |
fbf5a39b AC |
5370 | |
5371 | when N_Parameter_Association => | |
5372 | Why_Not_Static (Explicit_Actual_Parameter (N)); | |
5373 | ||
5374 | when N_Indexed_Component => | |
5375 | Error_Msg_N | |
b11e8d6f | 5376 | ("indexed component is never static (RM 4.9)!", N); |
fbf5a39b AC |
5377 | |
5378 | when N_Procedure_Call_Statement => | |
5379 | Error_Msg_N | |
b11e8d6f | 5380 | ("procedure call is never static (RM 4.9)!", N); |
fbf5a39b AC |
5381 | |
5382 | when N_Qualified_Expression => | |
5383 | Why_Not_Static (Expression (N)); | |
5384 | ||
5385 | when N_Aggregate | N_Extension_Aggregate => | |
5386 | Error_Msg_N | |
b11e8d6f | 5387 | ("an aggregate is never static (RM 4.9)!", N); |
fbf5a39b AC |
5388 | |
5389 | when N_Range => | |
5390 | Why_Not_Static (Low_Bound (N)); | |
5391 | Why_Not_Static (High_Bound (N)); | |
5392 | ||
5393 | when N_Range_Constraint => | |
5394 | Why_Not_Static (Range_Expression (N)); | |
5395 | ||
5396 | when N_Subtype_Indication => | |
5397 | Why_Not_Static (Constraint (N)); | |
5398 | ||
5399 | when N_Selected_Component => | |
5400 | Error_Msg_N | |
b11e8d6f | 5401 | ("selected component is never static (RM 4.9)!", N); |
fbf5a39b AC |
5402 | |
5403 | when N_Slice => | |
5404 | Error_Msg_N | |
b11e8d6f | 5405 | ("slice is never static (RM 4.9)!", N); |
fbf5a39b AC |
5406 | |
5407 | when N_Type_Conversion => | |
5408 | Why_Not_Static (Expression (N)); | |
5409 | ||
5410 | if not Is_Scalar_Type (Etype (Prefix (N))) | |
5411 | or else not Is_Static_Subtype (Etype (Prefix (N))) | |
5412 | then | |
5413 | Error_Msg_N | |
5414 | ("static conversion requires static scalar subtype result " & | |
b11e8d6f | 5415 | "(RM 4.9(9))!", N); |
fbf5a39b AC |
5416 | end if; |
5417 | ||
5418 | when N_Unchecked_Type_Conversion => | |
5419 | Error_Msg_N | |
b11e8d6f | 5420 | ("unchecked type conversion is never static (RM 4.9)!", N); |
fbf5a39b AC |
5421 | |
5422 | when others => | |
5423 | null; | |
5424 | ||
5425 | end case; | |
5426 | end Why_Not_Static; | |
5427 | ||
996ae0b0 | 5428 | end Sem_Eval; |