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