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