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