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