]>
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 | -- -- | |
8d0d46f4 | 9 | -- Copyright (C) 1992-2021, 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 | ||
104f58db BD |
26 | with Aspects; use Aspects; |
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Debug; use Debug; | |
30 | with Einfo; use Einfo; | |
76f9c7f4 | 31 | with Einfo.Entities; use Einfo.Entities; |
104f58db BD |
32 | with Einfo.Utils; use Einfo.Utils; |
33 | with Elists; use Elists; | |
34 | with Errout; use Errout; | |
35 | with Eval_Fat; use Eval_Fat; | |
36 | with Exp_Util; use Exp_Util; | |
37 | with Freeze; use Freeze; | |
38 | with Lib; use Lib; | |
39 | with Namet; use Namet; | |
40 | with Nmake; use Nmake; | |
41 | with Nlists; use Nlists; | |
42 | with Opt; use Opt; | |
43 | with Par_SCO; use Par_SCO; | |
44 | with Rtsfind; use Rtsfind; | |
45 | with Sem; use Sem; | |
46 | with Sem_Aux; use Sem_Aux; | |
47 | with Sem_Cat; use Sem_Cat; | |
48 | with Sem_Ch3; use Sem_Ch3; | |
49 | with Sem_Ch6; use Sem_Ch6; | |
50 | with Sem_Ch8; use Sem_Ch8; | |
51 | with Sem_Elab; use Sem_Elab; | |
52 | with Sem_Res; use Sem_Res; | |
53 | with Sem_Util; use Sem_Util; | |
54 | with Sem_Type; use Sem_Type; | |
55 | with Sem_Warn; use Sem_Warn; | |
56 | with Sinfo; use Sinfo; | |
57 | with Sinfo.Nodes; use Sinfo.Nodes; | |
58 | with Sinfo.Utils; use Sinfo.Utils; | |
59 | with Snames; use Snames; | |
60 | with Stand; use Stand; | |
61 | with Stringt; use Stringt; | |
62 | with Tbuild; use Tbuild; | |
996ae0b0 RK |
63 | |
64 | package body Sem_Eval is | |
65 | ||
66 | ----------------------------------------- | |
67 | -- Handling of Compile Time Evaluation -- | |
68 | ----------------------------------------- | |
69 | ||
70 | -- The compile time evaluation of expressions is distributed over several | |
f3d57416 | 71 | -- Eval_xxx procedures. These procedures are called immediately after |
996ae0b0 RK |
72 | -- a subexpression is resolved and is therefore accomplished in a bottom |
73 | -- up fashion. The flags are synthesized using the following approach. | |
74 | ||
1e3c434f BD |
75 | -- Is_Static_Expression is determined by following the rules in |
76 | -- RM-4.9. This involves testing the Is_Static_Expression flag of | |
77 | -- the operands in many cases. | |
78 | ||
79 | -- Raises_Constraint_Error is usually set if any of the operands have | |
80 | -- the flag set or if an attempt to compute the value of the current | |
81 | -- expression results in Constraint_Error. | |
996ae0b0 RK |
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 | |
1e3c434f | 86 | -- operands will raise Constraint_Error, and if so, propagate the flag |
996ae0b0 RK |
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 | |
1e3c434f | 93 | -- evaluation raises Constraint_Error, then we rewrite the node with |
996ae0b0 RK |
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 | ||
80298c3b | 104 | -- The following declarations are used to maintain a cache of nodes that |
d3bbfc59 | 105 | -- have compile-time-known values. The cache is maintained only for |
07fc65c4 GB |
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 | |
1e3c434f | 109 | -- possible for a node to get replaced by a Constraint_Error node). |
07fc65c4 GB |
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 | ||
edab6088 RD |
125 | type Match_Result is (Match, No_Match, Non_Static); |
126 | -- Result returned from functions that test for a matching result. If the | |
127 | -- operands are not OK_Static then Non_Static will be returned. Otherwise | |
128 | -- Match/No_Match is returned depending on whether the match succeeds. | |
129 | ||
07fc65c4 GB |
130 | type CV_Cache_Array is array (CV_Range) of CV_Entry; |
131 | ||
3cde9f1c | 132 | CV_Cache : CV_Cache_Array; |
07fc65c4 GB |
133 | -- This is the actual cache, with entries consisting of node/value pairs, |
134 | -- and the impossible value Node_High_Bound used for unset entries. | |
135 | ||
305caf42 AC |
136 | type Range_Membership is (In_Range, Out_Of_Range, Unknown); |
137 | -- Range membership may either be statically known to be in range or out | |
138 | -- of range, or not statically known. Used for Test_In_Range below. | |
139 | ||
bbab2db3 GD |
140 | Checking_For_Potentially_Static_Expression : Boolean := False; |
141 | -- Global flag that is set True during Analyze_Static_Expression_Function | |
142 | -- in order to verify that the result expression of a static expression | |
81e68a19 | 143 | -- function is a potentially static function (see RM2022 6.8(5.3)). |
bbab2db3 | 144 | |
996ae0b0 RK |
145 | ----------------------- |
146 | -- Local Subprograms -- | |
147 | ----------------------- | |
148 | ||
b626569a YM |
149 | procedure Check_Non_Static_Context_For_Overflow |
150 | (N : Node_Id; | |
151 | Stat : Boolean; | |
152 | Result : Uint); | |
153 | -- For a signed integer type, check non-static overflow in Result when | |
154 | -- Stat is False. This applies also inside inlined code, where the static | |
155 | -- property may be an effect of the inlining, which should not be allowed | |
156 | -- to remove run-time checks (whether during compilation, or even more | |
157 | -- crucially in the special inlining-for-proof in GNATprove mode). | |
158 | ||
edab6088 RD |
159 | function Choice_Matches |
160 | (Expr : Node_Id; | |
161 | Choice : Node_Id) return Match_Result; | |
162 | -- Determines whether given value Expr matches the given Choice. The Expr | |
163 | -- can be of discrete, real, or string type and must be a compile time | |
164 | -- known value (it is an error to make the call if these conditions are | |
165 | -- not met). The choice can be a range, subtype name, subtype indication, | |
166 | -- or expression. The returned result is Non_Static if Choice is not | |
167 | -- OK_Static, otherwise either Match or No_Match is returned depending | |
168 | -- on whether Choice matches Expr. This is used for case expression | |
169 | -- alternatives, and also for membership tests. In each case, more | |
170 | -- possibilities are tested than the syntax allows (e.g. membership allows | |
171 | -- subtype indications and non-discrete types, and case allows an OTHERS | |
172 | -- choice), but it does not matter, since we have already done a full | |
173 | -- semantic and syntax check of the construct, so the extra possibilities | |
174 | -- just will not arise for correct expressions. | |
175 | -- | |
176 | -- Note: if Choice_Matches finds that a choice raises Constraint_Error, e.g | |
177 | -- a reference to a type, one of whose bounds raises Constraint_Error, then | |
178 | -- it also sets the Raises_Constraint_Error flag on the Choice itself. | |
179 | ||
180 | function Choices_Match | |
181 | (Expr : Node_Id; | |
182 | Choices : List_Id) return Match_Result; | |
183 | -- This function applies Choice_Matches to each element of Choices. If the | |
184 | -- result is No_Match, then it continues and checks the next element. If | |
185 | -- the result is Match or Non_Static, this result is immediately given | |
186 | -- as the result without checking the rest of the list. Expr can be of | |
d3bbfc59 | 187 | -- discrete, real, or string type and must be a compile-time-known value |
edab6088 RD |
188 | -- (it is an error to make the call if these conditions are not met). |
189 | ||
8cd5951d AC |
190 | procedure Eval_Intrinsic_Call (N : Node_Id; E : Entity_Id); |
191 | -- Evaluate a call N to an intrinsic subprogram E. | |
192 | ||
87feba05 AC |
193 | function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id; |
194 | -- Check whether an arithmetic operation with universal operands which is a | |
195 | -- rewritten function call with an explicit scope indication is ambiguous: | |
196 | -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric | |
197 | -- type declared in P and the context does not impose a type on the result | |
198 | -- (e.g. in the expression of a type conversion). If ambiguous, emit an | |
199 | -- error and return Empty, else return the result type of the operator. | |
200 | ||
8cd5951d AC |
201 | procedure Fold_Dummy (N : Node_Id; Typ : Entity_Id); |
202 | -- Rewrite N as a constant dummy value in the relevant type if possible. | |
203 | ||
204 | procedure Fold_Shift | |
205 | (N : Node_Id; | |
206 | Left : Node_Id; | |
207 | Right : Node_Id; | |
208 | Op : Node_Kind; | |
209 | Static : Boolean := False; | |
210 | Check_Elab : Boolean := False); | |
211 | -- Rewrite N as the result of evaluating Left <shift op> Right if possible. | |
212 | -- Op represents the shift operation. | |
213 | -- Static indicates whether the resulting node should be marked static. | |
214 | -- Check_Elab indicates whether checks for elaboration calls should be | |
215 | -- inserted when relevant. | |
216 | ||
996ae0b0 | 217 | function From_Bits (B : Bits; T : Entity_Id) return Uint; |
80298c3b AC |
218 | -- Converts a bit string of length B'Length to a Uint value to be used for |
219 | -- a target of type T, which is a modular type. This procedure includes the | |
a95f708e | 220 | -- necessary reduction by the modulus in the case of a nonbinary modulus |
80298c3b AC |
221 | -- (for a binary modulus, the bit string is the right length any way so all |
222 | -- is well). | |
996ae0b0 | 223 | |
87feba05 AC |
224 | function Get_String_Val (N : Node_Id) return Node_Id; |
225 | -- Given a tree node for a folded string or character value, returns the | |
226 | -- corresponding string literal or character literal (one of the two must | |
227 | -- be available, or the operand would not have been marked as foldable in | |
228 | -- the earlier analysis of the operation). | |
edab6088 RD |
229 | |
230 | function Is_OK_Static_Choice (Choice : Node_Id) return Boolean; | |
231 | -- Given a choice (from a case expression or membership test), returns | |
232 | -- True if the choice is static and does not raise a Constraint_Error. | |
233 | ||
234 | function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean; | |
235 | -- Given a choice list (from a case expression or membership test), return | |
236 | -- True if all choices are static in the sense of Is_OK_Static_Choice. | |
237 | ||
87feba05 AC |
238 | function Is_Static_Choice (Choice : Node_Id) return Boolean; |
239 | -- Given a choice (from a case expression or membership test), returns | |
240 | -- True if the choice is static. No test is made for raising of constraint | |
241 | -- error, so this function is used only for legality tests. | |
242 | ||
243 | function Is_Static_Choice_List (Choices : List_Id) return Boolean; | |
244 | -- Given a choice list (from a case expression or membership test), return | |
245 | -- True if all choices are static in the sense of Is_Static_Choice. | |
246 | ||
edab6088 RD |
247 | function Is_Static_Range (N : Node_Id) return Boolean; |
248 | -- Determine if range is static, as defined in RM 4.9(26). The only allowed | |
249 | -- argument is an N_Range node (but note that the semantic analysis of | |
250 | -- equivalent range attribute references already turned them into the | |
251 | -- equivalent range). This differs from Is_OK_Static_Range (which is what | |
252 | -- must be used by clients) in that it does not care whether the bounds | |
253 | -- raise Constraint_Error or not. Used for checking whether expressions are | |
254 | -- static in the 4.9 sense (without worrying about exceptions). | |
255 | ||
07fc65c4 GB |
256 | function OK_Bits (N : Node_Id; Bits : Uint) return Boolean; |
257 | -- Bits represents the number of bits in an integer value to be computed | |
258 | -- (but the value has not been computed yet). If this value in Bits is | |
80298c3b AC |
259 | -- reasonable, a result of True is returned, with the implication that the |
260 | -- caller should go ahead and complete the calculation. If the value in | |
261 | -- Bits is unreasonably large, then an error is posted on node N, and | |
07fc65c4 GB |
262 | -- False is returned (and the caller skips the proposed calculation). |
263 | ||
996ae0b0 | 264 | procedure Out_Of_Range (N : Node_Id); |
80298c3b | 265 | -- This procedure is called if it is determined that node N, which appears |
d3bbfc59 | 266 | -- in a non-static context, is a compile-time-known value which is outside |
80298c3b AC |
267 | -- its range, i.e. the range of Etype. This is used in contexts where |
268 | -- this is an illegality if N is static, and should generate a warning | |
269 | -- otherwise. | |
996ae0b0 | 270 | |
fc3a3f3b RD |
271 | function Real_Or_String_Static_Predicate_Matches |
272 | (Val : Node_Id; | |
273 | Typ : Entity_Id) return Boolean; | |
274 | -- This is the function used to evaluate real or string static predicates. | |
275 | -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which | |
276 | -- represents the value to be tested against the predicate. Typ is the | |
277 | -- type with the predicate, from which the predicate expression can be | |
278 | -- extracted. The result returned is True if the given value satisfies | |
279 | -- the predicate. | |
280 | ||
996ae0b0 | 281 | procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id); |
80298c3b AC |
282 | -- N and Exp are nodes representing an expression, Exp is known to raise |
283 | -- CE. N is rewritten in term of Exp in the optimal way. | |
996ae0b0 RK |
284 | |
285 | function String_Type_Len (Stype : Entity_Id) return Uint; | |
80298c3b AC |
286 | -- Given a string type, determines the length of the index type, or, if |
287 | -- this index type is non-static, the length of the base type of this index | |
288 | -- type. Note that if the string type is itself static, then the index type | |
289 | -- is static, so the second case applies only if the string type passed is | |
290 | -- non-static. | |
996ae0b0 RK |
291 | |
292 | function Test (Cond : Boolean) return Uint; | |
293 | pragma Inline (Test); | |
294 | -- This function simply returns the appropriate Boolean'Pos value | |
295 | -- corresponding to the value of Cond as a universal integer. It is | |
296 | -- used for producing the result of the static evaluation of the | |
297 | -- logical operators | |
298 | ||
299 | procedure Test_Expression_Is_Foldable | |
300 | (N : Node_Id; | |
301 | Op1 : Node_Id; | |
302 | Stat : out Boolean; | |
303 | Fold : out Boolean); | |
304 | -- Tests to see if expression N whose single operand is Op1 is foldable, | |
305 | -- i.e. the operand value is known at compile time. If the operation is | |
80298c3b AC |
306 | -- foldable, then Fold is True on return, and Stat indicates whether the |
307 | -- result is static (i.e. the operand was static). Note that it is quite | |
308 | -- possible for Fold to be True, and Stat to be False, since there are | |
309 | -- cases in which we know the value of an operand even though it is not | |
310 | -- technically static (e.g. the static lower bound of a range whose upper | |
311 | -- bound is non-static). | |
996ae0b0 | 312 | -- |
80298c3b AC |
313 | -- If Stat is set False on return, then Test_Expression_Is_Foldable makes |
314 | -- a call to Check_Non_Static_Context on the operand. If Fold is False on | |
315 | -- return, then all processing is complete, and the caller should return, | |
316 | -- since there is nothing else to do. | |
93c3fca7 AC |
317 | -- |
318 | -- If Stat is set True on return, then Is_Static_Expression is also set | |
319 | -- true in node N. There are some cases where this is over-enthusiastic, | |
80298c3b AC |
320 | -- e.g. in the two operand case below, for string comparison, the result is |
321 | -- not static even though the two operands are static. In such cases, the | |
322 | -- caller must reset the Is_Static_Expression flag in N. | |
5df1266a AC |
323 | -- |
324 | -- If Fold and Stat are both set to False then this routine performs also | |
325 | -- the following extra actions: | |
326 | -- | |
80298c3b AC |
327 | -- If either operand is Any_Type then propagate it to result to prevent |
328 | -- cascaded errors. | |
5df1266a | 329 | -- |
1e3c434f BD |
330 | -- If some operand raises Constraint_Error, then replace the node N |
331 | -- with the raise Constraint_Error node. This replacement inherits the | |
70805b88 | 332 | -- Is_Static_Expression flag from the operands. |
996ae0b0 RK |
333 | |
334 | procedure Test_Expression_Is_Foldable | |
6c3c671e AC |
335 | (N : Node_Id; |
336 | Op1 : Node_Id; | |
337 | Op2 : Node_Id; | |
338 | Stat : out Boolean; | |
339 | Fold : out Boolean; | |
340 | CRT_Safe : Boolean := False); | |
996ae0b0 | 341 | -- Same processing, except applies to an expression N with two operands |
6c3c671e AC |
342 | -- Op1 and Op2. The result is static only if both operands are static. If |
343 | -- CRT_Safe is set True, then CRT_Safe_Compile_Time_Known_Value is used | |
344 | -- for the tests that the two operands are known at compile time. See | |
345 | -- spec of this routine for further details. | |
996ae0b0 | 346 | |
305caf42 AC |
347 | function Test_In_Range |
348 | (N : Node_Id; | |
349 | Typ : Entity_Id; | |
350 | Assume_Valid : Boolean; | |
351 | Fixed_Int : Boolean; | |
352 | Int_Real : Boolean) return Range_Membership; | |
9479ded4 AC |
353 | -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range |
354 | -- or Out_Of_Range if it can be guaranteed at compile time that expression | |
355 | -- N is known to be in or out of range of the subtype Typ. If not compile | |
356 | -- time known, Unknown is returned. See documentation of Is_In_Range for | |
357 | -- complete description of parameters. | |
305caf42 | 358 | |
996ae0b0 RK |
359 | procedure To_Bits (U : Uint; B : out Bits); |
360 | -- Converts a Uint value to a bit string of length B'Length | |
361 | ||
edab6088 RD |
362 | ----------------------------------------------- |
363 | -- Check_Expression_Against_Static_Predicate -- | |
364 | ----------------------------------------------- | |
365 | ||
366 | procedure Check_Expression_Against_Static_Predicate | |
24eda9e7 GD |
367 | (Expr : Node_Id; |
368 | Typ : Entity_Id; | |
369 | Static_Failure_Is_Error : Boolean := False) | |
edab6088 RD |
370 | is |
371 | begin | |
372 | -- Nothing to do if expression is not known at compile time, or the | |
373 | -- type has no static predicate set (will be the case for all non-scalar | |
374 | -- types, so no need to make a special test for that). | |
375 | ||
376 | if not (Has_Static_Predicate (Typ) | |
60f908dd | 377 | and then Compile_Time_Known_Value (Expr)) |
edab6088 RD |
378 | then |
379 | return; | |
380 | end if; | |
381 | ||
382 | -- Here we have a static predicate (note that it could have arisen from | |
383 | -- an explicitly specified Dynamic_Predicate whose expression met the | |
d9c59db4 AC |
384 | -- rules for being predicate-static). If the expression is known at |
385 | -- compile time and obeys the predicate, then it is static and must be | |
386 | -- labeled as such, which matters e.g. for case statements. The original | |
387 | -- expression may be a type conversion of a variable with a known value, | |
388 | -- which might otherwise not be marked static. | |
edab6088 | 389 | |
fc3a3f3b | 390 | -- Case of real static predicate |
edab6088 | 391 | |
fc3a3f3b RD |
392 | if Is_Real_Type (Typ) then |
393 | if Real_Or_String_Static_Predicate_Matches | |
394 | (Val => Make_Real_Literal (Sloc (Expr), Expr_Value_R (Expr)), | |
395 | Typ => Typ) | |
396 | then | |
d9c59db4 | 397 | Set_Is_Static_Expression (Expr); |
fc3a3f3b RD |
398 | return; |
399 | end if; | |
edab6088 | 400 | |
fc3a3f3b | 401 | -- Case of string static predicate |
edab6088 | 402 | |
fc3a3f3b RD |
403 | elsif Is_String_Type (Typ) then |
404 | if Real_Or_String_Static_Predicate_Matches | |
f9e333ab | 405 | (Val => Expr_Value_S (Expr), Typ => Typ) |
fc3a3f3b | 406 | then |
d9c59db4 | 407 | Set_Is_Static_Expression (Expr); |
fc3a3f3b RD |
408 | return; |
409 | end if; | |
edab6088 | 410 | |
fc3a3f3b | 411 | -- Case of discrete static predicate |
edab6088 | 412 | |
fc3a3f3b RD |
413 | else |
414 | pragma Assert (Is_Discrete_Type (Typ)); | |
415 | ||
416 | -- If static predicate matches, nothing to do | |
417 | ||
418 | if Choices_Match (Expr, Static_Discrete_Predicate (Typ)) = Match then | |
d9c59db4 | 419 | Set_Is_Static_Expression (Expr); |
fc3a3f3b RD |
420 | return; |
421 | end if; | |
edab6088 RD |
422 | end if; |
423 | ||
424 | -- Here we know that the predicate will fail | |
425 | ||
426 | -- Special case of static expression failing a predicate (other than one | |
24eda9e7 GD |
427 | -- that was explicitly specified with a Dynamic_Predicate aspect). If |
428 | -- the expression comes from a qualified_expression or type_conversion | |
429 | -- this is an error (Static_Failure_Is_Error); otherwise we only issue | |
430 | -- a warning and the expression is no longer considered static. | |
edab6088 RD |
431 | |
432 | if Is_Static_Expression (Expr) | |
433 | and then not Has_Dynamic_Predicate_Aspect (Typ) | |
434 | then | |
24eda9e7 GD |
435 | if Static_Failure_Is_Error then |
436 | Error_Msg_NE | |
437 | ("static expression fails static predicate check on &", | |
438 | Expr, Typ); | |
439 | ||
440 | else | |
441 | Error_Msg_NE | |
442 | ("??static expression fails static predicate check on &", | |
443 | Expr, Typ); | |
444 | Error_Msg_N | |
445 | ("\??expression is no longer considered static", Expr); | |
446 | ||
447 | Set_Is_Static_Expression (Expr, False); | |
448 | end if; | |
edab6088 RD |
449 | |
450 | -- In all other cases, this is just a warning that a test will fail. | |
451 | -- It does not matter if the expression is static or not, or if the | |
452 | -- predicate comes from a dynamic predicate aspect or not. | |
453 | ||
454 | else | |
455 | Error_Msg_NE | |
456 | ("??expression fails predicate check on &", Expr, Typ); | |
24eda9e7 GD |
457 | |
458 | -- Force a check here, which is potentially a redundant check, but | |
459 | -- this ensures a check will be done in cases where the expression | |
460 | -- is folded, and since this is definitely a failure, extra checks | |
461 | -- are OK. | |
462 | ||
8861bdd5 SB |
463 | if Predicate_Enabled (Typ) then |
464 | Insert_Action (Expr, | |
465 | Make_Predicate_Check | |
466 | (Typ, Duplicate_Subexpr (Expr)), Suppress => All_Checks); | |
467 | end if; | |
edab6088 RD |
468 | end if; |
469 | end Check_Expression_Against_Static_Predicate; | |
60f908dd | 470 | |
996ae0b0 RK |
471 | ------------------------------ |
472 | -- Check_Non_Static_Context -- | |
473 | ------------------------------ | |
474 | ||
475 | procedure Check_Non_Static_Context (N : Node_Id) is | |
fbf5a39b AC |
476 | T : constant Entity_Id := Etype (N); |
477 | Checks_On : constant Boolean := | |
996ae0b0 RK |
478 | not Index_Checks_Suppressed (T) |
479 | and not Range_Checks_Suppressed (T); | |
480 | ||
481 | begin | |
86f0e17a AC |
482 | -- Ignore cases of non-scalar types, error types, or universal real |
483 | -- types that have no usable bounds. | |
996ae0b0 | 484 | |
86f0e17a AC |
485 | if T = Any_Type |
486 | or else not Is_Scalar_Type (T) | |
487 | or else T = Universal_Fixed | |
488 | or else T = Universal_Real | |
489 | then | |
996ae0b0 | 490 | return; |
fbf5a39b | 491 | end if; |
996ae0b0 | 492 | |
86f0e17a | 493 | -- At this stage we have a scalar type. If we have an expression that |
80298c3b AC |
494 | -- raises CE, then we already issued a warning or error msg so there is |
495 | -- nothing more to be done in this routine. | |
fbf5a39b AC |
496 | |
497 | if Raises_Constraint_Error (N) then | |
498 | return; | |
499 | end if; | |
500 | ||
86f0e17a AC |
501 | -- Now we have a scalar type which is not marked as raising a constraint |
502 | -- error exception. The main purpose of this routine is to deal with | |
503 | -- static expressions appearing in a non-static context. That means | |
504 | -- that if we do not have a static expression then there is not much | |
505 | -- to do. The one case that we deal with here is that if we have a | |
506 | -- floating-point value that is out of range, then we post a warning | |
507 | -- that an infinity will result. | |
fbf5a39b AC |
508 | |
509 | if not Is_Static_Expression (N) then | |
d030f3a4 AC |
510 | if Is_Floating_Point_Type (T) then |
511 | if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then | |
512 | Error_Msg_N | |
513 | ("??float value out of range, infinity will be generated", N); | |
514 | ||
515 | -- The literal may be the result of constant-folding of a non- | |
516 | -- static subexpression of a larger expression (e.g. a conversion | |
517 | -- of a non-static variable whose value happens to be known). At | |
518 | -- this point we must reduce the value of the subexpression to a | |
519 | -- machine number (RM 4.9 (38/2)). | |
520 | ||
521 | elsif Nkind (N) = N_Real_Literal | |
522 | and then Nkind (Parent (N)) in N_Subexpr | |
523 | then | |
524 | Rewrite (N, New_Copy (N)); | |
525 | Set_Realval | |
526 | (N, Machine (Base_Type (T), Realval (N), Round_Even, N)); | |
527 | end if; | |
fbf5a39b | 528 | end if; |
996ae0b0 | 529 | |
996ae0b0 RK |
530 | return; |
531 | end if; | |
532 | ||
86f0e17a AC |
533 | -- Here we have the case of outer level static expression of scalar |
534 | -- type, where the processing of this procedure is needed. | |
996ae0b0 RK |
535 | |
536 | -- For real types, this is where we convert the value to a machine | |
86f0e17a AC |
537 | -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only |
538 | -- need to do this if the parent is a constant declaration, since in | |
539 | -- other cases, gigi should do the necessary conversion correctly, but | |
540 | -- experimentation shows that this is not the case on all machines, in | |
541 | -- particular if we do not convert all literals to machine values in | |
542 | -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris | |
543 | -- and SGI/Irix. | |
996ae0b0 | 544 | |
9d4f9832 AC |
545 | -- This conversion is always done by GNATprove on real literals in |
546 | -- non-static expressions, by calling Check_Non_Static_Context from | |
547 | -- gnat2why, as GNATprove cannot do the conversion later contrary | |
548 | -- to gigi. The frontend computes the information about which | |
549 | -- expressions are static, which is used by gnat2why to call | |
550 | -- Check_Non_Static_Context on exactly those real literals that are | |
2da8c8e2 | 551 | -- not subexpressions of static expressions. |
9d4f9832 | 552 | |
996ae0b0 RK |
553 | if Nkind (N) = N_Real_Literal |
554 | and then not Is_Machine_Number (N) | |
555 | and then not Is_Generic_Type (Etype (N)) | |
556 | and then Etype (N) /= Universal_Real | |
996ae0b0 RK |
557 | then |
558 | -- Check that value is in bounds before converting to machine | |
559 | -- number, so as not to lose case where value overflows in the | |
560 | -- least significant bit or less. See B490001. | |
561 | ||
c800f862 | 562 | if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then |
996ae0b0 RK |
563 | Out_Of_Range (N); |
564 | return; | |
565 | end if; | |
566 | ||
567 | -- Note: we have to copy the node, to avoid problems with conformance | |
568 | -- of very similar numbers (see ACVC tests B4A010C and B63103A). | |
569 | ||
570 | Rewrite (N, New_Copy (N)); | |
571 | ||
572 | if not Is_Floating_Point_Type (T) then | |
573 | Set_Realval | |
574 | (N, Corresponding_Integer_Value (N) * Small_Value (T)); | |
575 | ||
576 | elsif not UR_Is_Zero (Realval (N)) then | |
996ae0b0 | 577 | |
86f0e17a AC |
578 | -- Note: even though RM 4.9(38) specifies biased rounding, this |
579 | -- has been modified by AI-100 in order to prevent confusing | |
580 | -- differences in rounding between static and non-static | |
581 | -- expressions. AI-100 specifies that the effect of such rounding | |
582 | -- is implementation dependent, and in GNAT we round to nearest | |
ad075b50 AC |
583 | -- even to match the run-time behavior. Note that this applies |
584 | -- to floating point literals, not fixed points ones, even though | |
585 | -- their compiler representation is also as a universal real. | |
996ae0b0 | 586 | |
fbf5a39b AC |
587 | Set_Realval |
588 | (N, Machine (Base_Type (T), Realval (N), Round_Even, N)); | |
ad075b50 | 589 | Set_Is_Machine_Number (N); |
996ae0b0 RK |
590 | end if; |
591 | ||
996ae0b0 RK |
592 | end if; |
593 | ||
594 | -- Check for out of range universal integer. This is a non-static | |
595 | -- context, so the integer value must be in range of the runtime | |
596 | -- representation of universal integers. | |
597 | ||
598 | -- We do this only within an expression, because that is the only | |
599 | -- case in which non-static universal integer values can occur, and | |
600 | -- furthermore, Check_Non_Static_Context is currently (incorrectly???) | |
601 | -- called in contexts like the expression of a number declaration where | |
602 | -- we certainly want to allow out of range values. | |
603 | ||
c4a2e585 ES |
604 | -- We inhibit the warning when expansion is disabled, because the |
605 | -- preanalysis of a range of a 64-bit modular type may appear to | |
606 | -- violate the constraint on non-static Universal_Integer. If there | |
607 | -- is a true overflow it will be diagnosed during full analysis. | |
608 | ||
996ae0b0 RK |
609 | if Etype (N) = Universal_Integer |
610 | and then Nkind (N) = N_Integer_Literal | |
611 | and then Nkind (Parent (N)) in N_Subexpr | |
c4a2e585 | 612 | and then Expander_Active |
996ae0b0 RK |
613 | and then |
614 | (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer)) | |
80298c3b | 615 | or else |
996ae0b0 RK |
616 | Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer))) |
617 | then | |
618 | Apply_Compile_Time_Constraint_Error | |
4a28b181 | 619 | (N, "non-static universal integer value out of range<<", |
07fc65c4 | 620 | CE_Range_Check_Failed); |
996ae0b0 RK |
621 | |
622 | -- Check out of range of base type | |
623 | ||
c800f862 | 624 | elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then |
996ae0b0 RK |
625 | Out_Of_Range (N); |
626 | ||
31fde973 GD |
627 | -- Give a warning or error on the value outside the subtype. A warning |
628 | -- is omitted if the expression appears in a range that could be null | |
629 | -- (warnings are handled elsewhere for this case). | |
996ae0b0 | 630 | |
80298c3b | 631 | elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then |
c800f862 | 632 | if Is_In_Range (N, T, Assume_Valid => True) then |
996ae0b0 RK |
633 | null; |
634 | ||
c800f862 | 635 | elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then |
88ad52c9 AC |
636 | -- Ignore out of range values for System.Priority in CodePeer |
637 | -- mode since the actual target compiler may provide a wider | |
638 | -- range. | |
639 | ||
0f7b6a2e | 640 | if CodePeer_Mode and then Is_RTE (T, RE_Priority) then |
88ad52c9 | 641 | Set_Do_Range_Check (N, False); |
33defa7c | 642 | |
31fde973 GD |
643 | -- Determine if the out-of-range violation constitutes a warning |
644 | -- or an error based on context, according to RM 4.9 (34/3). | |
33defa7c | 645 | |
4a08c95c AC |
646 | elsif Nkind (Original_Node (N)) in |
647 | N_Type_Conversion | N_Qualified_Expression | |
33defa7c JS |
648 | and then Comes_From_Source (Original_Node (N)) |
649 | then | |
650 | Apply_Compile_Time_Constraint_Error | |
651 | (N, "value not in range of}", CE_Range_Check_Failed); | |
88ad52c9 AC |
652 | else |
653 | Apply_Compile_Time_Constraint_Error | |
654 | (N, "value not in range of}<<", CE_Range_Check_Failed); | |
655 | end if; | |
996ae0b0 RK |
656 | |
657 | elsif Checks_On then | |
658 | Enable_Range_Check (N); | |
659 | ||
660 | else | |
661 | Set_Do_Range_Check (N, False); | |
662 | end if; | |
663 | end if; | |
664 | end Check_Non_Static_Context; | |
665 | ||
b626569a YM |
666 | ------------------------------------------- |
667 | -- Check_Non_Static_Context_For_Overflow -- | |
668 | ------------------------------------------- | |
669 | ||
670 | procedure Check_Non_Static_Context_For_Overflow | |
671 | (N : Node_Id; | |
672 | Stat : Boolean; | |
673 | Result : Uint) | |
674 | is | |
675 | begin | |
676 | if (not Stat or else In_Inlined_Body) | |
677 | and then Is_Signed_Integer_Type (Etype (N)) | |
678 | then | |
679 | declare | |
680 | BT : constant Entity_Id := Base_Type (Etype (N)); | |
681 | Lo : constant Uint := Expr_Value (Type_Low_Bound (BT)); | |
682 | Hi : constant Uint := Expr_Value (Type_High_Bound (BT)); | |
683 | begin | |
684 | if Result < Lo or else Result > Hi then | |
685 | Apply_Compile_Time_Constraint_Error | |
686 | (N, "value not in range of }??", | |
687 | CE_Overflow_Check_Failed, | |
688 | Ent => BT); | |
689 | end if; | |
690 | end; | |
691 | end if; | |
692 | end Check_Non_Static_Context_For_Overflow; | |
693 | ||
996ae0b0 RK |
694 | --------------------------------- |
695 | -- Check_String_Literal_Length -- | |
696 | --------------------------------- | |
697 | ||
698 | procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is | |
699 | begin | |
324ac540 | 700 | if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then |
80298c3b | 701 | if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype) |
996ae0b0 RK |
702 | then |
703 | Apply_Compile_Time_Constraint_Error | |
324ac540 | 704 | (N, "string length wrong for}??", |
07fc65c4 | 705 | CE_Length_Check_Failed, |
996ae0b0 RK |
706 | Ent => Ttype, |
707 | Typ => Ttype); | |
708 | end if; | |
709 | end if; | |
710 | end Check_String_Literal_Length; | |
711 | ||
bbab2db3 GD |
712 | -------------------------------------------- |
713 | -- Checking_Potentially_Static_Expression -- | |
714 | -------------------------------------------- | |
715 | ||
716 | function Checking_Potentially_Static_Expression return Boolean is | |
717 | begin | |
718 | return Checking_For_Potentially_Static_Expression; | |
719 | end Checking_Potentially_Static_Expression; | |
720 | ||
edab6088 RD |
721 | -------------------- |
722 | -- Choice_Matches -- | |
723 | -------------------- | |
724 | ||
725 | function Choice_Matches | |
726 | (Expr : Node_Id; | |
727 | Choice : Node_Id) return Match_Result | |
728 | is | |
729 | Etyp : constant Entity_Id := Etype (Expr); | |
730 | Val : Uint; | |
731 | ValR : Ureal; | |
732 | ValS : Node_Id; | |
733 | ||
734 | begin | |
735 | pragma Assert (Compile_Time_Known_Value (Expr)); | |
736 | pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp)); | |
737 | ||
738 | if not Is_OK_Static_Choice (Choice) then | |
739 | Set_Raises_Constraint_Error (Choice); | |
740 | return Non_Static; | |
741 | ||
87feba05 | 742 | -- When the choice denotes a subtype with a static predictate, check the |
bb9e2aa2 AC |
743 | -- expression against the predicate values. Different procedures apply |
744 | -- to discrete and non-discrete types. | |
87feba05 AC |
745 | |
746 | elsif (Nkind (Choice) = N_Subtype_Indication | |
b63d61f7 AC |
747 | or else (Is_Entity_Name (Choice) |
748 | and then Is_Type (Entity (Choice)))) | |
87feba05 AC |
749 | and then Has_Predicates (Etype (Choice)) |
750 | and then Has_Static_Predicate (Etype (Choice)) | |
751 | then | |
bb9e2aa2 | 752 | if Is_Discrete_Type (Etype (Choice)) then |
b63d61f7 AC |
753 | return |
754 | Choices_Match | |
755 | (Expr, Static_Discrete_Predicate (Etype (Choice))); | |
87feba05 | 756 | |
b63d61f7 | 757 | elsif Real_Or_String_Static_Predicate_Matches (Expr, Etype (Choice)) |
bb9e2aa2 AC |
758 | then |
759 | return Match; | |
760 | ||
761 | else | |
762 | return No_Match; | |
763 | end if; | |
764 | ||
765 | -- Discrete type case only | |
edab6088 | 766 | |
87feba05 | 767 | elsif Is_Discrete_Type (Etyp) then |
edab6088 RD |
768 | Val := Expr_Value (Expr); |
769 | ||
770 | if Nkind (Choice) = N_Range then | |
771 | if Val >= Expr_Value (Low_Bound (Choice)) | |
772 | and then | |
773 | Val <= Expr_Value (High_Bound (Choice)) | |
774 | then | |
775 | return Match; | |
776 | else | |
777 | return No_Match; | |
778 | end if; | |
779 | ||
780 | elsif Nkind (Choice) = N_Subtype_Indication | |
87feba05 | 781 | or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))) |
edab6088 RD |
782 | then |
783 | if Val >= Expr_Value (Type_Low_Bound (Etype (Choice))) | |
784 | and then | |
785 | Val <= Expr_Value (Type_High_Bound (Etype (Choice))) | |
786 | then | |
787 | return Match; | |
788 | else | |
789 | return No_Match; | |
790 | end if; | |
791 | ||
792 | elsif Nkind (Choice) = N_Others_Choice then | |
793 | return Match; | |
794 | ||
795 | else | |
796 | if Val = Expr_Value (Choice) then | |
797 | return Match; | |
798 | else | |
799 | return No_Match; | |
800 | end if; | |
801 | end if; | |
802 | ||
87feba05 | 803 | -- Real type case |
edab6088 | 804 | |
87feba05 | 805 | elsif Is_Real_Type (Etyp) then |
edab6088 RD |
806 | ValR := Expr_Value_R (Expr); |
807 | ||
808 | if Nkind (Choice) = N_Range then | |
809 | if ValR >= Expr_Value_R (Low_Bound (Choice)) | |
810 | and then | |
811 | ValR <= Expr_Value_R (High_Bound (Choice)) | |
812 | then | |
813 | return Match; | |
814 | else | |
815 | return No_Match; | |
816 | end if; | |
817 | ||
818 | elsif Nkind (Choice) = N_Subtype_Indication | |
87feba05 | 819 | or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))) |
edab6088 RD |
820 | then |
821 | if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice))) | |
822 | and then | |
823 | ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice))) | |
824 | then | |
825 | return Match; | |
826 | else | |
827 | return No_Match; | |
828 | end if; | |
829 | ||
830 | else | |
831 | if ValR = Expr_Value_R (Choice) then | |
832 | return Match; | |
833 | else | |
834 | return No_Match; | |
835 | end if; | |
836 | end if; | |
837 | ||
87feba05 | 838 | -- String type cases |
edab6088 RD |
839 | |
840 | else | |
87feba05 | 841 | pragma Assert (Is_String_Type (Etyp)); |
edab6088 RD |
842 | ValS := Expr_Value_S (Expr); |
843 | ||
844 | if Nkind (Choice) = N_Subtype_Indication | |
87feba05 | 845 | or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))) |
edab6088 RD |
846 | then |
847 | if not Is_Constrained (Etype (Choice)) then | |
848 | return Match; | |
849 | ||
850 | else | |
851 | declare | |
852 | Typlen : constant Uint := | |
853 | String_Type_Len (Etype (Choice)); | |
854 | Strlen : constant Uint := | |
855 | UI_From_Int (String_Length (Strval (ValS))); | |
856 | begin | |
857 | if Typlen = Strlen then | |
858 | return Match; | |
859 | else | |
860 | return No_Match; | |
861 | end if; | |
862 | end; | |
863 | end if; | |
864 | ||
865 | else | |
866 | if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice))) | |
867 | then | |
868 | return Match; | |
869 | else | |
870 | return No_Match; | |
871 | end if; | |
872 | end if; | |
873 | end if; | |
874 | end Choice_Matches; | |
875 | ||
876 | ------------------- | |
877 | -- Choices_Match -- | |
878 | ------------------- | |
879 | ||
880 | function Choices_Match | |
881 | (Expr : Node_Id; | |
882 | Choices : List_Id) return Match_Result | |
883 | is | |
884 | Choice : Node_Id; | |
885 | Result : Match_Result; | |
886 | ||
887 | begin | |
888 | Choice := First (Choices); | |
889 | while Present (Choice) loop | |
890 | Result := Choice_Matches (Expr, Choice); | |
891 | ||
892 | if Result /= No_Match then | |
893 | return Result; | |
894 | end if; | |
895 | ||
896 | Next (Choice); | |
897 | end loop; | |
898 | ||
899 | return No_Match; | |
900 | end Choices_Match; | |
901 | ||
996ae0b0 RK |
902 | -------------------------- |
903 | -- Compile_Time_Compare -- | |
904 | -------------------------- | |
905 | ||
fbf5a39b | 906 | function Compile_Time_Compare |
1c7717c3 | 907 | (L, R : Node_Id; |
af02a866 RD |
908 | Assume_Valid : Boolean) return Compare_Result |
909 | is | |
a0f2ee7a | 910 | Discard : aliased Uint; |
af02a866 | 911 | begin |
a0f2ee7a | 912 | return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid); |
af02a866 RD |
913 | end Compile_Time_Compare; |
914 | ||
915 | function Compile_Time_Compare | |
916 | (L, R : Node_Id; | |
a0f2ee7a | 917 | Diff : access Uint; |
1c7717c3 AC |
918 | Assume_Valid : Boolean; |
919 | Rec : Boolean := False) return Compare_Result | |
fbf5a39b | 920 | is |
08f52d9f AC |
921 | Ltyp : Entity_Id := Etype (L); |
922 | Rtyp : Entity_Id := Etype (R); | |
996ae0b0 | 923 | |
a0f2ee7a | 924 | Discard : aliased Uint; |
af02a866 | 925 | |
996ae0b0 RK |
926 | procedure Compare_Decompose |
927 | (N : Node_Id; | |
928 | R : out Node_Id; | |
929 | V : out Uint); | |
b49365b2 RD |
930 | -- This procedure decomposes the node N into an expression node and a |
931 | -- signed offset, so that the value of N is equal to the value of R plus | |
932 | -- the value V (which may be negative). If no such decomposition is | |
933 | -- possible, then on return R is a copy of N, and V is set to zero. | |
996ae0b0 RK |
934 | |
935 | function Compare_Fixup (N : Node_Id) return Node_Id; | |
b49365b2 RD |
936 | -- This function deals with replacing 'Last and 'First references with |
937 | -- their corresponding type bounds, which we then can compare. The | |
938 | -- argument is the original node, the result is the identity, unless we | |
939 | -- have a 'Last/'First reference in which case the value returned is the | |
940 | -- appropriate type bound. | |
996ae0b0 | 941 | |
57036dcc ES |
942 | function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean; |
943 | -- Even if the context does not assume that values are valid, some | |
944 | -- simple cases can be recognized. | |
945 | ||
996ae0b0 | 946 | function Is_Same_Value (L, R : Node_Id) return Boolean; |
86f0e17a | 947 | -- Returns True iff L and R represent expressions that definitely have |
d3bbfc59 | 948 | -- identical (but not necessarily compile-time-known) values Indeed the |
86f0e17a AC |
949 | -- caller is expected to have already dealt with the cases of compile |
950 | -- time known values, so these are not tested here. | |
996ae0b0 RK |
951 | |
952 | ----------------------- | |
953 | -- Compare_Decompose -- | |
954 | ----------------------- | |
955 | ||
956 | procedure Compare_Decompose | |
957 | (N : Node_Id; | |
958 | R : out Node_Id; | |
959 | V : out Uint) | |
960 | is | |
961 | begin | |
962 | if Nkind (N) = N_Op_Add | |
963 | and then Nkind (Right_Opnd (N)) = N_Integer_Literal | |
964 | then | |
965 | R := Left_Opnd (N); | |
966 | V := Intval (Right_Opnd (N)); | |
967 | return; | |
968 | ||
969 | elsif Nkind (N) = N_Op_Subtract | |
970 | and then Nkind (Right_Opnd (N)) = N_Integer_Literal | |
971 | then | |
972 | R := Left_Opnd (N); | |
973 | V := UI_Negate (Intval (Right_Opnd (N))); | |
974 | return; | |
975 | ||
21d7ef70 | 976 | elsif Nkind (N) = N_Attribute_Reference then |
996ae0b0 RK |
977 | if Attribute_Name (N) = Name_Succ then |
978 | R := First (Expressions (N)); | |
979 | V := Uint_1; | |
980 | return; | |
981 | ||
982 | elsif Attribute_Name (N) = Name_Pred then | |
983 | R := First (Expressions (N)); | |
984 | V := Uint_Minus_1; | |
985 | return; | |
986 | end if; | |
987 | end if; | |
988 | ||
989 | R := N; | |
990 | V := Uint_0; | |
991 | end Compare_Decompose; | |
992 | ||
993 | ------------------- | |
994 | -- Compare_Fixup -- | |
995 | ------------------- | |
996 | ||
997 | function Compare_Fixup (N : Node_Id) return Node_Id is | |
998 | Indx : Node_Id; | |
999 | Xtyp : Entity_Id; | |
1000 | Subs : Nat; | |
1001 | ||
1002 | begin | |
7a6de2e2 AC |
1003 | -- Fixup only required for First/Last attribute reference |
1004 | ||
996ae0b0 | 1005 | if Nkind (N) = N_Attribute_Reference |
4a08c95c | 1006 | and then Attribute_Name (N) in Name_First | Name_Last |
996ae0b0 RK |
1007 | then |
1008 | Xtyp := Etype (Prefix (N)); | |
1009 | ||
1010 | -- If we have no type, then just abandon the attempt to do | |
1011 | -- a fixup, this is probably the result of some other error. | |
1012 | ||
1013 | if No (Xtyp) then | |
1014 | return N; | |
1015 | end if; | |
1016 | ||
1017 | -- Dereference an access type | |
1018 | ||
1019 | if Is_Access_Type (Xtyp) then | |
1020 | Xtyp := Designated_Type (Xtyp); | |
1021 | end if; | |
1022 | ||
80298c3b AC |
1023 | -- If we don't have an array type at this stage, something is |
1024 | -- peculiar, e.g. another error, and we abandon the attempt at | |
1025 | -- a fixup. | |
996ae0b0 RK |
1026 | |
1027 | if not Is_Array_Type (Xtyp) then | |
1028 | return N; | |
1029 | end if; | |
1030 | ||
1031 | -- Ignore unconstrained array, since bounds are not meaningful | |
1032 | ||
1033 | if not Is_Constrained (Xtyp) then | |
1034 | return N; | |
1035 | end if; | |
1036 | ||
c3de5c4c ES |
1037 | if Ekind (Xtyp) = E_String_Literal_Subtype then |
1038 | if Attribute_Name (N) = Name_First then | |
1039 | return String_Literal_Low_Bound (Xtyp); | |
5f44f0d4 | 1040 | else |
80298c3b AC |
1041 | return |
1042 | Make_Integer_Literal (Sloc (N), | |
1043 | Intval => Intval (String_Literal_Low_Bound (Xtyp)) + | |
1044 | String_Literal_Length (Xtyp)); | |
c3de5c4c ES |
1045 | end if; |
1046 | end if; | |
1047 | ||
996ae0b0 RK |
1048 | -- Find correct index type |
1049 | ||
1050 | Indx := First_Index (Xtyp); | |
1051 | ||
1052 | if Present (Expressions (N)) then | |
1053 | Subs := UI_To_Int (Expr_Value (First (Expressions (N)))); | |
1054 | ||
1055 | for J in 2 .. Subs loop | |
99859ea7 | 1056 | Next_Index (Indx); |
996ae0b0 RK |
1057 | end loop; |
1058 | end if; | |
1059 | ||
1060 | Xtyp := Etype (Indx); | |
1061 | ||
1062 | if Attribute_Name (N) = Name_First then | |
1063 | return Type_Low_Bound (Xtyp); | |
7a6de2e2 | 1064 | else |
996ae0b0 RK |
1065 | return Type_High_Bound (Xtyp); |
1066 | end if; | |
1067 | end if; | |
1068 | ||
1069 | return N; | |
1070 | end Compare_Fixup; | |
1071 | ||
57036dcc ES |
1072 | ---------------------------- |
1073 | -- Is_Known_Valid_Operand -- | |
1074 | ---------------------------- | |
1075 | ||
1076 | function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is | |
1077 | begin | |
1078 | return (Is_Entity_Name (Opnd) | |
1079 | and then | |
1080 | (Is_Known_Valid (Entity (Opnd)) | |
1081 | or else Ekind (Entity (Opnd)) = E_In_Parameter | |
1082 | or else | |
a1447c2a | 1083 | (Is_Object (Entity (Opnd)) |
80298c3b | 1084 | and then Present (Current_Value (Entity (Opnd)))))) |
57036dcc ES |
1085 | or else Is_OK_Static_Expression (Opnd); |
1086 | end Is_Known_Valid_Operand; | |
1087 | ||
996ae0b0 RK |
1088 | ------------------- |
1089 | -- Is_Same_Value -- | |
1090 | ------------------- | |
1091 | ||
1092 | function Is_Same_Value (L, R : Node_Id) return Boolean is | |
1093 | Lf : constant Node_Id := Compare_Fixup (L); | |
1094 | Rf : constant Node_Id := Compare_Fixup (R); | |
1095 | ||
708fb956 YM |
1096 | function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean; |
1097 | -- An attribute reference to Loop_Entry may have been rewritten into | |
1098 | -- its prefix as a way to avoid generating a constant for that | |
1099 | -- attribute when the corresponding pragma is ignored. These nodes | |
1100 | -- should be ignored when deciding if they can be equal to one | |
1101 | -- another. | |
1102 | ||
fbf5a39b | 1103 | function Is_Same_Subscript (L, R : List_Id) return Boolean; |
57036dcc ES |
1104 | -- L, R are the Expressions values from two attribute nodes for First |
1105 | -- or Last attributes. Either may be set to No_List if no expressions | |
1106 | -- are present (indicating subscript 1). The result is True if both | |
1107 | -- expressions represent the same subscript (note one case is where | |
1108 | -- one subscript is missing and the other is explicitly set to 1). | |
fbf5a39b | 1109 | |
708fb956 YM |
1110 | ----------------------------- |
1111 | -- Is_Rewritten_Loop_Entry -- | |
1112 | ----------------------------- | |
1113 | ||
1114 | function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean is | |
1115 | Orig_N : constant Node_Id := Original_Node (N); | |
1116 | begin | |
1117 | return Orig_N /= N | |
1118 | and then Nkind (Orig_N) = N_Attribute_Reference | |
1119 | and then Get_Attribute_Id (Attribute_Name (Orig_N)) = | |
1120 | Attribute_Loop_Entry; | |
1121 | end Is_Rewritten_Loop_Entry; | |
1122 | ||
fbf5a39b AC |
1123 | ----------------------- |
1124 | -- Is_Same_Subscript -- | |
1125 | ----------------------- | |
1126 | ||
1127 | function Is_Same_Subscript (L, R : List_Id) return Boolean is | |
1128 | begin | |
1129 | if L = No_List then | |
1130 | if R = No_List then | |
1131 | return True; | |
1132 | else | |
1133 | return Expr_Value (First (R)) = Uint_1; | |
1134 | end if; | |
1135 | ||
1136 | else | |
1137 | if R = No_List then | |
1138 | return Expr_Value (First (L)) = Uint_1; | |
1139 | else | |
1140 | return Expr_Value (First (L)) = Expr_Value (First (R)); | |
1141 | end if; | |
1142 | end if; | |
1143 | end Is_Same_Subscript; | |
1144 | ||
1145 | -- Start of processing for Is_Same_Value | |
1146 | ||
996ae0b0 | 1147 | begin |
708fb956 YM |
1148 | -- Loop_Entry nodes rewritten into their prefix inside ignored |
1149 | -- pragmas should never lead to a decision of equality. | |
996ae0b0 | 1150 | |
708fb956 YM |
1151 | if Is_Rewritten_Loop_Entry (Lf) |
1152 | or else Is_Rewritten_Loop_Entry (Rf) | |
1153 | then | |
1154 | return False; | |
f08b2371 | 1155 | |
708fb956 YM |
1156 | -- Values are the same if they refer to the same entity and the |
1157 | -- entity is nonvolatile. | |
4fb0b3f0 | 1158 | |
4a08c95c AC |
1159 | elsif Nkind (Lf) in N_Identifier | N_Expanded_Name |
1160 | and then Nkind (Rf) in N_Identifier | N_Expanded_Name | |
996ae0b0 | 1161 | and then Entity (Lf) = Entity (Rf) |
708fb956 YM |
1162 | |
1163 | -- If the entity is a discriminant, the two expressions may be | |
1164 | -- bounds of components of objects of the same discriminated type. | |
1165 | -- The values of the discriminants are not static, and therefore | |
1166 | -- the result is unknown. | |
1167 | ||
4fb0b3f0 | 1168 | and then Ekind (Entity (Lf)) /= E_Discriminant |
b49365b2 | 1169 | and then Present (Entity (Lf)) |
708fb956 YM |
1170 | |
1171 | -- This does not however apply to Float types, since we may have | |
1172 | -- two NaN values and they should never compare equal. | |
1173 | ||
fbf5a39b | 1174 | and then not Is_Floating_Point_Type (Etype (L)) |
c800f862 RD |
1175 | and then not Is_Volatile_Reference (L) |
1176 | and then not Is_Volatile_Reference (R) | |
996ae0b0 RK |
1177 | then |
1178 | return True; | |
1179 | ||
d3bbfc59 | 1180 | -- Or if they are compile-time-known and identical |
996ae0b0 RK |
1181 | |
1182 | elsif Compile_Time_Known_Value (Lf) | |
1183 | and then | |
1184 | Compile_Time_Known_Value (Rf) | |
1185 | and then Expr_Value (Lf) = Expr_Value (Rf) | |
1186 | then | |
1187 | return True; | |
1188 | ||
b49365b2 RD |
1189 | -- False if Nkind of the two nodes is different for remaining cases |
1190 | ||
1191 | elsif Nkind (Lf) /= Nkind (Rf) then | |
1192 | return False; | |
1193 | ||
1194 | -- True if both 'First or 'Last values applying to the same entity | |
1195 | -- (first and last don't change even if value does). Note that we | |
1196 | -- need this even with the calls to Compare_Fixup, to handle the | |
1197 | -- case of unconstrained array attributes where Compare_Fixup | |
1198 | -- cannot find useful bounds. | |
996ae0b0 RK |
1199 | |
1200 | elsif Nkind (Lf) = N_Attribute_Reference | |
996ae0b0 | 1201 | and then Attribute_Name (Lf) = Attribute_Name (Rf) |
4a08c95c AC |
1202 | and then Attribute_Name (Lf) in Name_First | Name_Last |
1203 | and then Nkind (Prefix (Lf)) in N_Identifier | N_Expanded_Name | |
1204 | and then Nkind (Prefix (Rf)) in N_Identifier | N_Expanded_Name | |
996ae0b0 | 1205 | and then Entity (Prefix (Lf)) = Entity (Prefix (Rf)) |
fbf5a39b | 1206 | and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf)) |
996ae0b0 RK |
1207 | then |
1208 | return True; | |
1209 | ||
b49365b2 RD |
1210 | -- True if the same selected component from the same record |
1211 | ||
1212 | elsif Nkind (Lf) = N_Selected_Component | |
1213 | and then Selector_Name (Lf) = Selector_Name (Rf) | |
1214 | and then Is_Same_Value (Prefix (Lf), Prefix (Rf)) | |
1215 | then | |
1216 | return True; | |
1217 | ||
1218 | -- True if the same unary operator applied to the same operand | |
1219 | ||
1220 | elsif Nkind (Lf) in N_Unary_Op | |
1221 | and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf)) | |
1222 | then | |
1223 | return True; | |
1224 | ||
8682d22c | 1225 | -- True if the same binary operator applied to the same operands |
b49365b2 RD |
1226 | |
1227 | elsif Nkind (Lf) in N_Binary_Op | |
1228 | and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf)) | |
1229 | and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf)) | |
1230 | then | |
1231 | return True; | |
1232 | ||
8682d22c | 1233 | -- All other cases, we can't tell, so return False |
996ae0b0 RK |
1234 | |
1235 | else | |
1236 | return False; | |
1237 | end if; | |
1238 | end Is_Same_Value; | |
1239 | ||
1240 | -- Start of processing for Compile_Time_Compare | |
1241 | ||
1242 | begin | |
a0f2ee7a | 1243 | Diff.all := No_Uint; |
af02a866 | 1244 | |
37c1f923 AC |
1245 | -- In preanalysis mode, always return Unknown unless the expression |
1246 | -- is static. It is too early to be thinking we know the result of a | |
1247 | -- comparison, save that judgment for the full analysis. This is | |
1248 | -- particularly important in the case of pre and postconditions, which | |
1249 | -- otherwise can be prematurely collapsed into having True or False | |
1250 | -- conditions when this is inappropriate. | |
1251 | ||
1252 | if not (Full_Analysis | |
edab6088 | 1253 | or else (Is_OK_Static_Expression (L) |
db318f46 | 1254 | and then |
edab6088 | 1255 | Is_OK_Static_Expression (R))) |
37c1f923 | 1256 | then |
05b34c18 AC |
1257 | return Unknown; |
1258 | end if; | |
1259 | ||
1e3c434f | 1260 | -- If either operand could raise Constraint_Error, then we cannot |
a90bd866 | 1261 | -- know the result at compile time (since CE may be raised). |
07fc65c4 GB |
1262 | |
1263 | if not (Cannot_Raise_Constraint_Error (L) | |
1264 | and then | |
1265 | Cannot_Raise_Constraint_Error (R)) | |
1266 | then | |
1267 | return Unknown; | |
1268 | end if; | |
1269 | ||
1270 | -- Identical operands are most certainly equal | |
1271 | ||
996ae0b0 RK |
1272 | if L = R then |
1273 | return EQ; | |
08f52d9f | 1274 | end if; |
996ae0b0 | 1275 | |
93c3fca7 AC |
1276 | -- If expressions have no types, then do not attempt to determine if |
1277 | -- they are the same, since something funny is going on. One case in | |
1278 | -- which this happens is during generic template analysis, when bounds | |
1279 | -- are not fully analyzed. | |
996ae0b0 | 1280 | |
08f52d9f AC |
1281 | if No (Ltyp) or else No (Rtyp) then |
1282 | return Unknown; | |
1283 | end if; | |
1284 | ||
1285 | -- These get reset to the base type for the case of entities where | |
1286 | -- Is_Known_Valid is not set. This takes care of handling possible | |
1287 | -- invalid representations using the value of the base type, in | |
1288 | -- accordance with RM 13.9.1(10). | |
1289 | ||
1290 | Ltyp := Underlying_Type (Ltyp); | |
1291 | Rtyp := Underlying_Type (Rtyp); | |
1292 | ||
1293 | -- Same rationale as above, but for Underlying_Type instead of Etype | |
1294 | ||
1295 | if No (Ltyp) or else No (Rtyp) then | |
996ae0b0 | 1296 | return Unknown; |
08f52d9f | 1297 | end if; |
996ae0b0 | 1298 | |
0a3ec628 | 1299 | -- We do not attempt comparisons for packed arrays represented as |
93c3fca7 | 1300 | -- modular types, where the semantics of comparison is quite different. |
996ae0b0 | 1301 | |
08f52d9f | 1302 | if Is_Packed_Array_Impl_Type (Ltyp) |
93c3fca7 | 1303 | and then Is_Modular_Integer_Type (Ltyp) |
996ae0b0 RK |
1304 | then |
1305 | return Unknown; | |
1306 | ||
93c3fca7 | 1307 | -- For access types, the only time we know the result at compile time |
f61580d4 | 1308 | -- (apart from identical operands, which we handled already) is if we |
93c3fca7 AC |
1309 | -- know one operand is null and the other is not, or both operands are |
1310 | -- known null. | |
1311 | ||
1312 | elsif Is_Access_Type (Ltyp) then | |
1313 | if Known_Null (L) then | |
1314 | if Known_Null (R) then | |
1315 | return EQ; | |
1316 | elsif Known_Non_Null (R) then | |
1317 | return NE; | |
1318 | else | |
1319 | return Unknown; | |
1320 | end if; | |
1321 | ||
f61580d4 | 1322 | elsif Known_Non_Null (L) and then Known_Null (R) then |
93c3fca7 AC |
1323 | return NE; |
1324 | ||
1325 | else | |
1326 | return Unknown; | |
1327 | end if; | |
1328 | ||
d3bbfc59 | 1329 | -- Case where comparison involves two compile-time-known values |
996ae0b0 RK |
1330 | |
1331 | elsif Compile_Time_Known_Value (L) | |
80298c3b AC |
1332 | and then |
1333 | Compile_Time_Known_Value (R) | |
996ae0b0 RK |
1334 | then |
1335 | -- For the floating-point case, we have to be a little careful, since | |
1336 | -- at compile time we are dealing with universal exact values, but at | |
1337 | -- runtime, these will be in non-exact target form. That's why the | |
1338 | -- returned results are LE and GE below instead of LT and GT. | |
1339 | ||
1340 | if Is_Floating_Point_Type (Ltyp) | |
1341 | or else | |
1342 | Is_Floating_Point_Type (Rtyp) | |
1343 | then | |
1344 | declare | |
1345 | Lo : constant Ureal := Expr_Value_R (L); | |
1346 | Hi : constant Ureal := Expr_Value_R (R); | |
996ae0b0 RK |
1347 | begin |
1348 | if Lo < Hi then | |
1349 | return LE; | |
1350 | elsif Lo = Hi then | |
1351 | return EQ; | |
1352 | else | |
1353 | return GE; | |
1354 | end if; | |
1355 | end; | |
1356 | ||
93c3fca7 AC |
1357 | -- For string types, we have two string literals and we proceed to |
1358 | -- compare them using the Ada style dictionary string comparison. | |
1359 | ||
1360 | elsif not Is_Scalar_Type (Ltyp) then | |
1361 | declare | |
1362 | Lstring : constant String_Id := Strval (Expr_Value_S (L)); | |
1363 | Rstring : constant String_Id := Strval (Expr_Value_S (R)); | |
1364 | Llen : constant Nat := String_Length (Lstring); | |
1365 | Rlen : constant Nat := String_Length (Rstring); | |
1366 | ||
1367 | begin | |
1368 | for J in 1 .. Nat'Min (Llen, Rlen) loop | |
1369 | declare | |
1370 | LC : constant Char_Code := Get_String_Char (Lstring, J); | |
1371 | RC : constant Char_Code := Get_String_Char (Rstring, J); | |
1372 | begin | |
1373 | if LC < RC then | |
1374 | return LT; | |
1375 | elsif LC > RC then | |
1376 | return GT; | |
1377 | end if; | |
1378 | end; | |
1379 | end loop; | |
1380 | ||
1381 | if Llen < Rlen then | |
1382 | return LT; | |
1383 | elsif Llen > Rlen then | |
1384 | return GT; | |
1385 | else | |
1386 | return EQ; | |
1387 | end if; | |
1388 | end; | |
1389 | ||
1390 | -- For remaining scalar cases we know exactly (note that this does | |
1391 | -- include the fixed-point case, where we know the run time integer | |
f61580d4 | 1392 | -- values now). |
996ae0b0 RK |
1393 | |
1394 | else | |
1395 | declare | |
1396 | Lo : constant Uint := Expr_Value (L); | |
1397 | Hi : constant Uint := Expr_Value (R); | |
996ae0b0 RK |
1398 | begin |
1399 | if Lo < Hi then | |
a0f2ee7a | 1400 | Diff.all := Hi - Lo; |
996ae0b0 RK |
1401 | return LT; |
1402 | elsif Lo = Hi then | |
1403 | return EQ; | |
1404 | else | |
a0f2ee7a | 1405 | Diff.all := Lo - Hi; |
996ae0b0 RK |
1406 | return GT; |
1407 | end if; | |
1408 | end; | |
1409 | end if; | |
1410 | ||
1411 | -- Cases where at least one operand is not known at compile time | |
1412 | ||
1413 | else | |
93c3fca7 | 1414 | -- Remaining checks apply only for discrete types |
29797f34 RD |
1415 | |
1416 | if not Is_Discrete_Type (Ltyp) | |
80298c3b AC |
1417 | or else |
1418 | not Is_Discrete_Type (Rtyp) | |
93c3fca7 AC |
1419 | then |
1420 | return Unknown; | |
1421 | end if; | |
1422 | ||
1423 | -- Defend against generic types, or actually any expressions that | |
1424 | -- contain a reference to a generic type from within a generic | |
1425 | -- template. We don't want to do any range analysis of such | |
1426 | -- expressions for two reasons. First, the bounds of a generic type | |
1427 | -- itself are junk and cannot be used for any kind of analysis. | |
1428 | -- Second, we may have a case where the range at run time is indeed | |
1429 | -- known, but we don't want to do compile time analysis in the | |
1430 | -- template based on that range since in an instance the value may be | |
1431 | -- static, and able to be elaborated without reference to the bounds | |
1432 | -- of types involved. As an example, consider: | |
1433 | ||
1434 | -- (F'Pos (F'Last) + 1) > Integer'Last | |
1435 | ||
1436 | -- The expression on the left side of > is Universal_Integer and thus | |
1437 | -- acquires the type Integer for evaluation at run time, and at run | |
1438 | -- time it is true that this condition is always False, but within | |
1439 | -- an instance F may be a type with a static range greater than the | |
1440 | -- range of Integer, and the expression statically evaluates to True. | |
1441 | ||
1442 | if References_Generic_Formal_Type (L) | |
1443 | or else | |
1444 | References_Generic_Formal_Type (R) | |
29797f34 RD |
1445 | then |
1446 | return Unknown; | |
1447 | end if; | |
1448 | ||
41a58113 | 1449 | -- Replace types by base types for the case of values which are not |
80298c3b AC |
1450 | -- known to have valid representations. This takes care of properly |
1451 | -- dealing with invalid representations. | |
1c7717c3 | 1452 | |
41a58113 RD |
1453 | if not Assume_Valid then |
1454 | if not (Is_Entity_Name (L) | |
1455 | and then (Is_Known_Valid (Entity (L)) | |
1456 | or else Assume_No_Invalid_Values)) | |
1457 | then | |
93c3fca7 | 1458 | Ltyp := Underlying_Type (Base_Type (Ltyp)); |
1c7717c3 AC |
1459 | end if; |
1460 | ||
41a58113 RD |
1461 | if not (Is_Entity_Name (R) |
1462 | and then (Is_Known_Valid (Entity (R)) | |
1463 | or else Assume_No_Invalid_Values)) | |
1464 | then | |
93c3fca7 | 1465 | Rtyp := Underlying_Type (Base_Type (Rtyp)); |
1c7717c3 AC |
1466 | end if; |
1467 | end if; | |
1468 | ||
a40ada7e RD |
1469 | -- First attempt is to decompose the expressions to extract a |
1470 | -- constant offset resulting from the use of any of the forms: | |
1471 | ||
1472 | -- expr + literal | |
1473 | -- expr - literal | |
1474 | -- typ'Succ (expr) | |
1475 | -- typ'Pred (expr) | |
1476 | ||
1477 | -- Then we see if the two expressions are the same value, and if so | |
1478 | -- the result is obtained by comparing the offsets. | |
1479 | ||
1480 | -- Note: the reason we do this test first is that it returns only | |
1481 | -- decisive results (with diff set), where other tests, like the | |
1482 | -- range test, may not be as so decisive. Consider for example | |
1483 | -- J .. J + 1. This code can conclude LT with a difference of 1, | |
1484 | -- even if the range of J is not known. | |
1485 | ||
22564ca9 EB |
1486 | declare |
1487 | Lnode : Node_Id; | |
1488 | Loffs : Uint; | |
1489 | Rnode : Node_Id; | |
1490 | Roffs : Uint; | |
a40ada7e | 1491 | |
22564ca9 EB |
1492 | begin |
1493 | Compare_Decompose (L, Lnode, Loffs); | |
1494 | Compare_Decompose (R, Rnode, Roffs); | |
a40ada7e | 1495 | |
22564ca9 EB |
1496 | if Is_Same_Value (Lnode, Rnode) then |
1497 | if Loffs = Roffs then | |
1498 | return EQ; | |
1499 | end if; | |
1500 | ||
1501 | -- When the offsets are not equal, we can go farther only if | |
1502 | -- the types are not modular (e.g. X < X + 1 is False if X is | |
1503 | -- the largest number). | |
0a3ec628 | 1504 | |
22564ca9 EB |
1505 | if not Is_Modular_Integer_Type (Ltyp) |
1506 | and then not Is_Modular_Integer_Type (Rtyp) | |
1507 | then | |
1508 | if Loffs < Roffs then | |
a0f2ee7a | 1509 | Diff.all := Roffs - Loffs; |
0a3ec628 AC |
1510 | return LT; |
1511 | else | |
a0f2ee7a | 1512 | Diff.all := Loffs - Roffs; |
0a3ec628 AC |
1513 | return GT; |
1514 | end if; | |
a40ada7e | 1515 | end if; |
22564ca9 EB |
1516 | end if; |
1517 | end; | |
a40ada7e RD |
1518 | |
1519 | -- Next, try range analysis and see if operand ranges are disjoint | |
c800f862 RD |
1520 | |
1521 | declare | |
1522 | LOK, ROK : Boolean; | |
1523 | LLo, LHi : Uint; | |
1524 | RLo, RHi : Uint; | |
1525 | ||
b6b5cca8 AC |
1526 | Single : Boolean; |
1527 | -- True if each range is a single point | |
1528 | ||
c800f862 RD |
1529 | begin |
1530 | Determine_Range (L, LOK, LLo, LHi, Assume_Valid); | |
1531 | Determine_Range (R, ROK, RLo, RHi, Assume_Valid); | |
1532 | ||
1533 | if LOK and ROK then | |
b6b5cca8 AC |
1534 | Single := (LLo = LHi) and then (RLo = RHi); |
1535 | ||
c800f862 | 1536 | if LHi < RLo then |
b6b5cca8 | 1537 | if Single and Assume_Valid then |
a0f2ee7a | 1538 | Diff.all := RLo - LLo; |
b6b5cca8 AC |
1539 | end if; |
1540 | ||
c800f862 RD |
1541 | return LT; |
1542 | ||
1543 | elsif RHi < LLo then | |
b6b5cca8 | 1544 | if Single and Assume_Valid then |
a0f2ee7a | 1545 | Diff.all := LLo - RLo; |
b6b5cca8 AC |
1546 | end if; |
1547 | ||
c800f862 RD |
1548 | return GT; |
1549 | ||
b6b5cca8 | 1550 | elsif Single and then LLo = RLo then |
e27b834b | 1551 | |
75ba322d AC |
1552 | -- If the range includes a single literal and we can assume |
1553 | -- validity then the result is known even if an operand is | |
1554 | -- not static. | |
e27b834b AC |
1555 | |
1556 | if Assume_Valid then | |
1557 | return EQ; | |
e27b834b AC |
1558 | else |
1559 | return Unknown; | |
1560 | end if; | |
c800f862 RD |
1561 | |
1562 | elsif LHi = RLo then | |
1563 | return LE; | |
1564 | ||
1565 | elsif RHi = LLo then | |
1566 | return GE; | |
57036dcc ES |
1567 | |
1568 | elsif not Is_Known_Valid_Operand (L) | |
1569 | and then not Assume_Valid | |
1570 | then | |
1571 | if Is_Same_Value (L, R) then | |
1572 | return EQ; | |
1573 | else | |
1574 | return Unknown; | |
1575 | end if; | |
c800f862 | 1576 | end if; |
f9ad6b62 | 1577 | |
2c1b72d7 AC |
1578 | -- If the range of either operand cannot be determined, nothing |
1579 | -- further can be inferred. | |
f9ad6b62 | 1580 | |
2c1b72d7 | 1581 | else |
f9ad6b62 | 1582 | return Unknown; |
c800f862 RD |
1583 | end if; |
1584 | end; | |
1585 | ||
996ae0b0 RK |
1586 | -- Here is where we check for comparisons against maximum bounds of |
1587 | -- types, where we know that no value can be outside the bounds of | |
1588 | -- the subtype. Note that this routine is allowed to assume that all | |
1589 | -- expressions are within their subtype bounds. Callers wishing to | |
1590 | -- deal with possibly invalid values must in any case take special | |
1591 | -- steps (e.g. conversions to larger types) to avoid this kind of | |
1592 | -- optimization, which is always considered to be valid. We do not | |
1593 | -- attempt this optimization with generic types, since the type | |
1594 | -- bounds may not be meaningful in this case. | |
1595 | ||
93c3fca7 | 1596 | -- We are in danger of an infinite recursion here. It does not seem |
fbf5a39b AC |
1597 | -- useful to go more than one level deep, so the parameter Rec is |
1598 | -- used to protect ourselves against this infinite recursion. | |
1599 | ||
29797f34 RD |
1600 | if not Rec then |
1601 | ||
80298c3b AC |
1602 | -- See if we can get a decisive check against one operand and a |
1603 | -- bound of the other operand (four possible tests here). Note | |
1604 | -- that we avoid testing junk bounds of a generic type. | |
93c3fca7 AC |
1605 | |
1606 | if not Is_Generic_Type (Rtyp) then | |
1607 | case Compile_Time_Compare (L, Type_Low_Bound (Rtyp), | |
a0f2ee7a | 1608 | Discard'Access, |
93c3fca7 AC |
1609 | Assume_Valid, Rec => True) |
1610 | is | |
1611 | when LT => return LT; | |
1612 | when LE => return LE; | |
1613 | when EQ => return LE; | |
1614 | when others => null; | |
1615 | end case; | |
fbf5a39b | 1616 | |
93c3fca7 | 1617 | case Compile_Time_Compare (L, Type_High_Bound (Rtyp), |
a0f2ee7a | 1618 | Discard'Access, |
93c3fca7 AC |
1619 | Assume_Valid, Rec => True) |
1620 | is | |
1621 | when GT => return GT; | |
1622 | when GE => return GE; | |
1623 | when EQ => return GE; | |
1624 | when others => null; | |
1625 | end case; | |
1626 | end if; | |
996ae0b0 | 1627 | |
93c3fca7 AC |
1628 | if not Is_Generic_Type (Ltyp) then |
1629 | case Compile_Time_Compare (Type_Low_Bound (Ltyp), R, | |
a0f2ee7a | 1630 | Discard'Access, |
93c3fca7 AC |
1631 | Assume_Valid, Rec => True) |
1632 | is | |
1633 | when GT => return GT; | |
1634 | when GE => return GE; | |
1635 | when EQ => return GE; | |
1636 | when others => null; | |
1637 | end case; | |
996ae0b0 | 1638 | |
93c3fca7 | 1639 | case Compile_Time_Compare (Type_High_Bound (Ltyp), R, |
a0f2ee7a | 1640 | Discard'Access, |
93c3fca7 AC |
1641 | Assume_Valid, Rec => True) |
1642 | is | |
1643 | when LT => return LT; | |
1644 | when LE => return LE; | |
1645 | when EQ => return LE; | |
1646 | when others => null; | |
1647 | end case; | |
1648 | end if; | |
996ae0b0 RK |
1649 | end if; |
1650 | ||
29797f34 | 1651 | -- Next attempt is to see if we have an entity compared with a |
d3bbfc59 | 1652 | -- compile-time-known value, where there is a current value |
29797f34 RD |
1653 | -- conditional for the entity which can tell us the result. |
1654 | ||
1655 | declare | |
1656 | Var : Node_Id; | |
1657 | -- Entity variable (left operand) | |
1658 | ||
1659 | Val : Uint; | |
1660 | -- Value (right operand) | |
1661 | ||
1662 | Inv : Boolean; | |
1663 | -- If False, we have reversed the operands | |
1664 | ||
1665 | Op : Node_Kind; | |
1666 | -- Comparison operator kind from Get_Current_Value_Condition call | |
996ae0b0 | 1667 | |
29797f34 RD |
1668 | Opn : Node_Id; |
1669 | -- Value from Get_Current_Value_Condition call | |
1670 | ||
1671 | Opv : Uint; | |
1672 | -- Value of Opn | |
1673 | ||
1674 | Result : Compare_Result; | |
1675 | -- Known result before inversion | |
1676 | ||
1677 | begin | |
1678 | if Is_Entity_Name (L) | |
1679 | and then Compile_Time_Known_Value (R) | |
1680 | then | |
1681 | Var := L; | |
1682 | Val := Expr_Value (R); | |
1683 | Inv := False; | |
1684 | ||
1685 | elsif Is_Entity_Name (R) | |
1686 | and then Compile_Time_Known_Value (L) | |
1687 | then | |
1688 | Var := R; | |
1689 | Val := Expr_Value (L); | |
1690 | Inv := True; | |
1691 | ||
1692 | -- That was the last chance at finding a compile time result | |
996ae0b0 RK |
1693 | |
1694 | else | |
1695 | return Unknown; | |
1696 | end if; | |
29797f34 RD |
1697 | |
1698 | Get_Current_Value_Condition (Var, Op, Opn); | |
1699 | ||
1700 | -- That was the last chance, so if we got nothing return | |
1701 | ||
1702 | if No (Opn) then | |
1703 | return Unknown; | |
1704 | end if; | |
1705 | ||
1706 | Opv := Expr_Value (Opn); | |
1707 | ||
1708 | -- We got a comparison, so we might have something interesting | |
1709 | ||
1710 | -- Convert LE to LT and GE to GT, just so we have fewer cases | |
1711 | ||
1712 | if Op = N_Op_Le then | |
1713 | Op := N_Op_Lt; | |
1714 | Opv := Opv + 1; | |
af02a866 | 1715 | |
29797f34 RD |
1716 | elsif Op = N_Op_Ge then |
1717 | Op := N_Op_Gt; | |
1718 | Opv := Opv - 1; | |
1719 | end if; | |
1720 | ||
1721 | -- Deal with equality case | |
1722 | ||
1723 | if Op = N_Op_Eq then | |
1724 | if Val = Opv then | |
1725 | Result := EQ; | |
1726 | elsif Opv < Val then | |
1727 | Result := LT; | |
1728 | else | |
1729 | Result := GT; | |
1730 | end if; | |
1731 | ||
1732 | -- Deal with inequality case | |
1733 | ||
1734 | elsif Op = N_Op_Ne then | |
1735 | if Val = Opv then | |
1736 | Result := NE; | |
1737 | else | |
1738 | return Unknown; | |
1739 | end if; | |
1740 | ||
1741 | -- Deal with greater than case | |
1742 | ||
1743 | elsif Op = N_Op_Gt then | |
1744 | if Opv >= Val then | |
1745 | Result := GT; | |
1746 | elsif Opv = Val - 1 then | |
1747 | Result := GE; | |
1748 | else | |
1749 | return Unknown; | |
1750 | end if; | |
1751 | ||
1752 | -- Deal with less than case | |
1753 | ||
1754 | else pragma Assert (Op = N_Op_Lt); | |
1755 | if Opv <= Val then | |
1756 | Result := LT; | |
1757 | elsif Opv = Val + 1 then | |
1758 | Result := LE; | |
1759 | else | |
1760 | return Unknown; | |
1761 | end if; | |
1762 | end if; | |
1763 | ||
1764 | -- Deal with inverting result | |
1765 | ||
1766 | if Inv then | |
1767 | case Result is | |
1768 | when GT => return LT; | |
1769 | when GE => return LE; | |
1770 | when LT => return GT; | |
1771 | when LE => return GE; | |
1772 | when others => return Result; | |
1773 | end case; | |
1774 | end if; | |
1775 | ||
1776 | return Result; | |
996ae0b0 RK |
1777 | end; |
1778 | end if; | |
1779 | end Compile_Time_Compare; | |
1780 | ||
f44fe430 RD |
1781 | ------------------------------- |
1782 | -- Compile_Time_Known_Bounds -- | |
1783 | ------------------------------- | |
1784 | ||
1785 | function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is | |
1786 | Indx : Node_Id; | |
1787 | Typ : Entity_Id; | |
1788 | ||
1789 | begin | |
f5f6d8d7 | 1790 | if T = Any_Composite or else not Is_Array_Type (T) then |
f44fe430 RD |
1791 | return False; |
1792 | end if; | |
1793 | ||
1794 | Indx := First_Index (T); | |
1795 | while Present (Indx) loop | |
1796 | Typ := Underlying_Type (Etype (Indx)); | |
93c3fca7 AC |
1797 | |
1798 | -- Never look at junk bounds of a generic type | |
1799 | ||
1800 | if Is_Generic_Type (Typ) then | |
1801 | return False; | |
1802 | end if; | |
1803 | ||
d3bbfc59 | 1804 | -- Otherwise check bounds for compile-time-known |
93c3fca7 | 1805 | |
f44fe430 RD |
1806 | if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then |
1807 | return False; | |
1808 | elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then | |
1809 | return False; | |
1810 | else | |
1811 | Next_Index (Indx); | |
1812 | end if; | |
1813 | end loop; | |
1814 | ||
1815 | return True; | |
1816 | end Compile_Time_Known_Bounds; | |
1817 | ||
996ae0b0 RK |
1818 | ------------------------------ |
1819 | -- Compile_Time_Known_Value -- | |
1820 | ------------------------------ | |
1821 | ||
6c3c671e | 1822 | function Compile_Time_Known_Value (Op : Node_Id) return Boolean is |
07fc65c4 GB |
1823 | K : constant Node_Kind := Nkind (Op); |
1824 | CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size); | |
996ae0b0 RK |
1825 | |
1826 | begin | |
1e3c434f | 1827 | -- Never known at compile time if bad type or raises Constraint_Error |
ee2ba856 | 1828 | -- or empty (latter case occurs only as a result of a previous error). |
996ae0b0 | 1829 | |
ee2ba856 AC |
1830 | if No (Op) then |
1831 | Check_Error_Detected; | |
1832 | return False; | |
1833 | ||
1834 | elsif Op = Error | |
996ae0b0 RK |
1835 | or else Etype (Op) = Any_Type |
1836 | or else Raises_Constraint_Error (Op) | |
1837 | then | |
1838 | return False; | |
1839 | end if; | |
1840 | ||
1841 | -- If we have an entity name, then see if it is the name of a constant | |
705bcbfe HK |
1842 | -- and if so, test the corresponding constant value, or the name of an |
1843 | -- enumeration literal, which is always a constant. | |
996ae0b0 RK |
1844 | |
1845 | if Present (Etype (Op)) and then Is_Entity_Name (Op) then | |
1846 | declare | |
705bcbfe HK |
1847 | Ent : constant Entity_Id := Entity (Op); |
1848 | Val : Node_Id; | |
996ae0b0 RK |
1849 | |
1850 | begin | |
705bcbfe HK |
1851 | -- Never known at compile time if it is a packed array value. We |
1852 | -- might want to try to evaluate these at compile time one day, | |
1853 | -- but we do not make that attempt now. | |
996ae0b0 | 1854 | |
8ca597af | 1855 | if Is_Packed_Array_Impl_Type (Etype (Op)) then |
996ae0b0 | 1856 | return False; |
996ae0b0 | 1857 | |
705bcbfe | 1858 | elsif Ekind (Ent) = E_Enumeration_Literal then |
996ae0b0 RK |
1859 | return True; |
1860 | ||
705bcbfe HK |
1861 | elsif Ekind (Ent) = E_Constant then |
1862 | Val := Constant_Value (Ent); | |
1863 | ||
1864 | if Present (Val) then | |
1865 | ||
1866 | -- Guard against an illegal deferred constant whose full | |
1867 | -- view is initialized with a reference to itself. Treat | |
d3bbfc59 | 1868 | -- this case as a value not known at compile time. |
705bcbfe HK |
1869 | |
1870 | if Is_Entity_Name (Val) and then Entity (Val) = Ent then | |
1871 | return False; | |
1872 | else | |
1873 | return Compile_Time_Known_Value (Val); | |
1874 | end if; | |
1875 | ||
d3bbfc59 | 1876 | -- Otherwise, the constant does not have a compile-time-known |
705bcbfe HK |
1877 | -- value. |
1878 | ||
1879 | else | |
1880 | return False; | |
1881 | end if; | |
996ae0b0 RK |
1882 | end if; |
1883 | end; | |
1884 | ||
d3bbfc59 | 1885 | -- We have a value, see if it is compile-time-known |
996ae0b0 RK |
1886 | |
1887 | else | |
07fc65c4 | 1888 | -- Integer literals are worth storing in the cache |
996ae0b0 | 1889 | |
07fc65c4 GB |
1890 | if K = N_Integer_Literal then |
1891 | CV_Ent.N := Op; | |
1892 | CV_Ent.V := Intval (Op); | |
1893 | return True; | |
1894 | ||
1895 | -- Other literals and NULL are known at compile time | |
1896 | ||
4a08c95c AC |
1897 | elsif K in |
1898 | N_Character_Literal | N_Real_Literal | N_String_Literal | N_Null | |
996ae0b0 RK |
1899 | then |
1900 | return True; | |
84be0369 AC |
1901 | |
1902 | -- Evaluate static discriminants, to eliminate dead paths and | |
1903 | -- redundant discriminant checks. | |
1904 | ||
1905 | elsif Is_Static_Discriminant_Component (Op) then | |
1906 | return True; | |
07fc65c4 | 1907 | end if; |
996ae0b0 | 1908 | end if; |
07fc65c4 GB |
1909 | |
1910 | -- If we fall through, not known at compile time | |
1911 | ||
1912 | return False; | |
1913 | ||
1914 | -- If we get an exception while trying to do this test, then some error | |
1915 | -- has occurred, and we simply say that the value is not known after all | |
1916 | ||
1917 | exception | |
1918 | when others => | |
a34da56b PT |
1919 | -- With debug flag K we will get an exception unless an error has |
1920 | -- already occurred (useful for debugging). | |
1921 | ||
1922 | if Debug_Flag_K then | |
1923 | Check_Error_Detected; | |
1924 | end if; | |
1925 | ||
07fc65c4 | 1926 | return False; |
996ae0b0 RK |
1927 | end Compile_Time_Known_Value; |
1928 | ||
1929 | -------------------------------------- | |
1930 | -- Compile_Time_Known_Value_Or_Aggr -- | |
1931 | -------------------------------------- | |
1932 | ||
1933 | function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is | |
1934 | begin | |
1935 | -- If we have an entity name, then see if it is the name of a constant | |
1936 | -- and if so, test the corresponding constant value, or the name of | |
1937 | -- an enumeration literal, which is always a constant. | |
1938 | ||
1939 | if Is_Entity_Name (Op) then | |
1940 | declare | |
1941 | E : constant Entity_Id := Entity (Op); | |
1942 | V : Node_Id; | |
1943 | ||
1944 | begin | |
1945 | if Ekind (E) = E_Enumeration_Literal then | |
1946 | return True; | |
1947 | ||
1948 | elsif Ekind (E) /= E_Constant then | |
1949 | return False; | |
1950 | ||
1951 | else | |
1952 | V := Constant_Value (E); | |
1953 | return Present (V) | |
1954 | and then Compile_Time_Known_Value_Or_Aggr (V); | |
1955 | end if; | |
1956 | end; | |
1957 | ||
d3bbfc59 | 1958 | -- We have a value, see if it is compile-time-known |
996ae0b0 RK |
1959 | |
1960 | else | |
1961 | if Compile_Time_Known_Value (Op) then | |
1962 | return True; | |
1963 | ||
1964 | elsif Nkind (Op) = N_Aggregate then | |
1965 | ||
1966 | if Present (Expressions (Op)) then | |
1967 | declare | |
1968 | Expr : Node_Id; | |
996ae0b0 RK |
1969 | begin |
1970 | Expr := First (Expressions (Op)); | |
1971 | while Present (Expr) loop | |
1972 | if not Compile_Time_Known_Value_Or_Aggr (Expr) then | |
1973 | return False; | |
80298c3b AC |
1974 | else |
1975 | Next (Expr); | |
996ae0b0 | 1976 | end if; |
996ae0b0 RK |
1977 | end loop; |
1978 | end; | |
1979 | end if; | |
1980 | ||
1981 | if Present (Component_Associations (Op)) then | |
1982 | declare | |
1983 | Cass : Node_Id; | |
1984 | ||
1985 | begin | |
1986 | Cass := First (Component_Associations (Op)); | |
1987 | while Present (Cass) loop | |
1988 | if not | |
1989 | Compile_Time_Known_Value_Or_Aggr (Expression (Cass)) | |
1990 | then | |
1991 | return False; | |
1992 | end if; | |
1993 | ||
1994 | Next (Cass); | |
1995 | end loop; | |
1996 | end; | |
1997 | end if; | |
1998 | ||
1999 | return True; | |
2000 | ||
5e9cb404 AC |
2001 | elsif Nkind (Op) = N_Qualified_Expression then |
2002 | return Compile_Time_Known_Value_Or_Aggr (Expression (Op)); | |
2003 | ||
996ae0b0 RK |
2004 | -- All other types of values are not known at compile time |
2005 | ||
2006 | else | |
2007 | return False; | |
2008 | end if; | |
2009 | ||
2010 | end if; | |
2011 | end Compile_Time_Known_Value_Or_Aggr; | |
2012 | ||
6c3c671e AC |
2013 | --------------------------------------- |
2014 | -- CRT_Safe_Compile_Time_Known_Value -- | |
2015 | --------------------------------------- | |
2016 | ||
2017 | function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is | |
2018 | begin | |
2019 | if (Configurable_Run_Time_Mode or No_Run_Time_Mode) | |
2020 | and then not Is_OK_Static_Expression (Op) | |
2021 | then | |
2022 | return False; | |
2023 | else | |
2024 | return Compile_Time_Known_Value (Op); | |
2025 | end if; | |
2026 | end CRT_Safe_Compile_Time_Known_Value; | |
2027 | ||
996ae0b0 RK |
2028 | ----------------- |
2029 | -- Eval_Actual -- | |
2030 | ----------------- | |
2031 | ||
2032 | -- This is only called for actuals of functions that are not predefined | |
2033 | -- operators (which have already been rewritten as operators at this | |
2034 | -- stage), so the call can never be folded, and all that needs doing for | |
2035 | -- the actual is to do the check for a non-static context. | |
2036 | ||
2037 | procedure Eval_Actual (N : Node_Id) is | |
2038 | begin | |
2039 | Check_Non_Static_Context (N); | |
2040 | end Eval_Actual; | |
2041 | ||
2042 | -------------------- | |
2043 | -- Eval_Allocator -- | |
2044 | -------------------- | |
2045 | ||
2046 | -- Allocators are never static, so all we have to do is to do the | |
2047 | -- check for a non-static context if an expression is present. | |
2048 | ||
2049 | procedure Eval_Allocator (N : Node_Id) is | |
2050 | Expr : constant Node_Id := Expression (N); | |
996ae0b0 RK |
2051 | begin |
2052 | if Nkind (Expr) = N_Qualified_Expression then | |
2053 | Check_Non_Static_Context (Expression (Expr)); | |
2054 | end if; | |
2055 | end Eval_Allocator; | |
2056 | ||
2057 | ------------------------ | |
2058 | -- Eval_Arithmetic_Op -- | |
2059 | ------------------------ | |
2060 | ||
2061 | -- Arithmetic operations are static functions, so the result is static | |
2062 | -- if both operands are static (RM 4.9(7), 4.9(20)). | |
2063 | ||
2064 | procedure Eval_Arithmetic_Op (N : Node_Id) is | |
2065 | Left : constant Node_Id := Left_Opnd (N); | |
2066 | Right : constant Node_Id := Right_Opnd (N); | |
2067 | Ltype : constant Entity_Id := Etype (Left); | |
2068 | Rtype : constant Entity_Id := Etype (Right); | |
d7567964 | 2069 | Otype : Entity_Id := Empty; |
996ae0b0 RK |
2070 | Stat : Boolean; |
2071 | Fold : Boolean; | |
2072 | ||
2073 | begin | |
2074 | -- If not foldable we are done | |
2075 | ||
2076 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); | |
2077 | ||
2078 | if not Fold then | |
2079 | return; | |
2080 | end if; | |
2081 | ||
6c3c671e AC |
2082 | -- Otherwise attempt to fold |
2083 | ||
d7567964 TQ |
2084 | if Is_Universal_Numeric_Type (Etype (Left)) |
2085 | and then | |
2086 | Is_Universal_Numeric_Type (Etype (Right)) | |
602a7ec0 | 2087 | then |
d7567964 | 2088 | Otype := Find_Universal_Operator_Type (N); |
602a7ec0 AC |
2089 | end if; |
2090 | ||
996ae0b0 RK |
2091 | -- Fold for cases where both operands are of integer type |
2092 | ||
2093 | if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then | |
2094 | declare | |
2095 | Left_Int : constant Uint := Expr_Value (Left); | |
2096 | Right_Int : constant Uint := Expr_Value (Right); | |
2097 | Result : Uint; | |
2098 | ||
2099 | begin | |
2100 | case Nkind (N) is | |
996ae0b0 RK |
2101 | when N_Op_Add => |
2102 | Result := Left_Int + Right_Int; | |
2103 | ||
2104 | when N_Op_Subtract => | |
2105 | Result := Left_Int - Right_Int; | |
2106 | ||
2107 | when N_Op_Multiply => | |
2108 | if OK_Bits | |
2109 | (N, UI_From_Int | |
2110 | (Num_Bits (Left_Int) + Num_Bits (Right_Int))) | |
2111 | then | |
2112 | Result := Left_Int * Right_Int; | |
2113 | else | |
2114 | Result := Left_Int; | |
2115 | end if; | |
2116 | ||
2117 | when N_Op_Divide => | |
2118 | ||
2119 | -- The exception Constraint_Error is raised by integer | |
2120 | -- division, rem and mod if the right operand is zero. | |
2121 | ||
2122 | if Right_Int = 0 then | |
520c0201 AC |
2123 | |
2124 | -- When SPARK_Mode is On, force a warning instead of | |
2125 | -- an error in that case, as this likely corresponds | |
2126 | -- to deactivated code. | |
2127 | ||
996ae0b0 | 2128 | Apply_Compile_Time_Constraint_Error |
80298c3b | 2129 | (N, "division by zero", CE_Divide_By_Zero, |
520c0201 | 2130 | Warn => not Stat or SPARK_Mode = On); |
996ae0b0 | 2131 | return; |
fbf5a39b | 2132 | |
edab6088 RD |
2133 | -- Otherwise we can do the division |
2134 | ||
996ae0b0 RK |
2135 | else |
2136 | Result := Left_Int / Right_Int; | |
2137 | end if; | |
2138 | ||
2139 | when N_Op_Mod => | |
2140 | ||
2141 | -- The exception Constraint_Error is raised by integer | |
2142 | -- division, rem and mod if the right operand is zero. | |
2143 | ||
2144 | if Right_Int = 0 then | |
520c0201 AC |
2145 | |
2146 | -- When SPARK_Mode is On, force a warning instead of | |
2147 | -- an error in that case, as this likely corresponds | |
2148 | -- to deactivated code. | |
2149 | ||
996ae0b0 | 2150 | Apply_Compile_Time_Constraint_Error |
80298c3b | 2151 | (N, "mod with zero divisor", CE_Divide_By_Zero, |
520c0201 | 2152 | Warn => not Stat or SPARK_Mode = On); |
996ae0b0 | 2153 | return; |
520c0201 | 2154 | |
996ae0b0 RK |
2155 | else |
2156 | Result := Left_Int mod Right_Int; | |
2157 | end if; | |
2158 | ||
2159 | when N_Op_Rem => | |
2160 | ||
2161 | -- The exception Constraint_Error is raised by integer | |
2162 | -- division, rem and mod if the right operand is zero. | |
2163 | ||
2164 | if Right_Int = 0 then | |
520c0201 AC |
2165 | |
2166 | -- When SPARK_Mode is On, force a warning instead of | |
2167 | -- an error in that case, as this likely corresponds | |
2168 | -- to deactivated code. | |
2169 | ||
996ae0b0 | 2170 | Apply_Compile_Time_Constraint_Error |
80298c3b | 2171 | (N, "rem with zero divisor", CE_Divide_By_Zero, |
520c0201 | 2172 | Warn => not Stat or SPARK_Mode = On); |
996ae0b0 | 2173 | return; |
fbf5a39b | 2174 | |
996ae0b0 RK |
2175 | else |
2176 | Result := Left_Int rem Right_Int; | |
2177 | end if; | |
2178 | ||
2179 | when others => | |
2180 | raise Program_Error; | |
2181 | end case; | |
2182 | ||
2183 | -- Adjust the result by the modulus if the type is a modular type | |
2184 | ||
2185 | if Is_Modular_Integer_Type (Ltype) then | |
2186 | Result := Result mod Modulus (Ltype); | |
2187 | end if; | |
2188 | ||
b626569a YM |
2189 | Check_Non_Static_Context_For_Overflow (N, Stat, Result); |
2190 | ||
82c80734 RD |
2191 | -- If we get here we can fold the result |
2192 | ||
fbf5a39b | 2193 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
2194 | end; |
2195 | ||
d7567964 TQ |
2196 | -- Cases where at least one operand is a real. We handle the cases of |
2197 | -- both reals, or mixed/real integer cases (the latter happen only for | |
2198 | -- divide and multiply, and the result is always real). | |
996ae0b0 RK |
2199 | |
2200 | elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then | |
2201 | declare | |
2202 | Left_Real : Ureal; | |
2203 | Right_Real : Ureal; | |
2204 | Result : Ureal; | |
2205 | ||
2206 | begin | |
2207 | if Is_Real_Type (Ltype) then | |
2208 | Left_Real := Expr_Value_R (Left); | |
2209 | else | |
2210 | Left_Real := UR_From_Uint (Expr_Value (Left)); | |
2211 | end if; | |
2212 | ||
2213 | if Is_Real_Type (Rtype) then | |
2214 | Right_Real := Expr_Value_R (Right); | |
2215 | else | |
2216 | Right_Real := UR_From_Uint (Expr_Value (Right)); | |
2217 | end if; | |
2218 | ||
2219 | if Nkind (N) = N_Op_Add then | |
2220 | Result := Left_Real + Right_Real; | |
2221 | ||
2222 | elsif Nkind (N) = N_Op_Subtract then | |
2223 | Result := Left_Real - Right_Real; | |
2224 | ||
2225 | elsif Nkind (N) = N_Op_Multiply then | |
2226 | Result := Left_Real * Right_Real; | |
2227 | ||
2228 | else pragma Assert (Nkind (N) = N_Op_Divide); | |
2229 | if UR_Is_Zero (Right_Real) then | |
2230 | Apply_Compile_Time_Constraint_Error | |
07fc65c4 | 2231 | (N, "division by zero", CE_Divide_By_Zero); |
996ae0b0 RK |
2232 | return; |
2233 | end if; | |
2234 | ||
2235 | Result := Left_Real / Right_Real; | |
2236 | end if; | |
2237 | ||
fbf5a39b | 2238 | Fold_Ureal (N, Result, Stat); |
996ae0b0 RK |
2239 | end; |
2240 | end if; | |
d7567964 TQ |
2241 | |
2242 | -- If the operator was resolved to a specific type, make sure that type | |
2243 | -- is frozen even if the expression is folded into a literal (which has | |
2244 | -- a universal type). | |
2245 | ||
2246 | if Present (Otype) then | |
2247 | Freeze_Before (N, Otype); | |
2248 | end if; | |
996ae0b0 RK |
2249 | end Eval_Arithmetic_Op; |
2250 | ||
2251 | ---------------------------- | |
2252 | -- Eval_Character_Literal -- | |
2253 | ---------------------------- | |
2254 | ||
a90bd866 | 2255 | -- Nothing to be done |
996ae0b0 RK |
2256 | |
2257 | procedure Eval_Character_Literal (N : Node_Id) is | |
07fc65c4 | 2258 | pragma Warnings (Off, N); |
996ae0b0 RK |
2259 | begin |
2260 | null; | |
2261 | end Eval_Character_Literal; | |
2262 | ||
c01a9391 AC |
2263 | --------------- |
2264 | -- Eval_Call -- | |
2265 | --------------- | |
2266 | ||
2267 | -- Static function calls are either calls to predefined operators | |
2268 | -- with static arguments, or calls to functions that rename a literal. | |
2269 | -- Only the latter case is handled here, predefined operators are | |
2270 | -- constant-folded elsewhere. | |
29797f34 | 2271 | |
8cd5951d AC |
2272 | -- If the function is itself inherited the literal of the parent type must |
2273 | -- be explicitly converted to the return type of the function. | |
c01a9391 AC |
2274 | |
2275 | procedure Eval_Call (N : Node_Id) is | |
2276 | Loc : constant Source_Ptr := Sloc (N); | |
2277 | Typ : constant Entity_Id := Etype (N); | |
2278 | Lit : Entity_Id; | |
2279 | ||
2280 | begin | |
2281 | if Nkind (N) = N_Function_Call | |
2282 | and then No (Parameter_Associations (N)) | |
2283 | and then Is_Entity_Name (Name (N)) | |
2284 | and then Present (Alias (Entity (Name (N)))) | |
2285 | and then Is_Enumeration_Type (Base_Type (Typ)) | |
2286 | then | |
b81a5940 | 2287 | Lit := Ultimate_Alias (Entity (Name (N))); |
c01a9391 AC |
2288 | |
2289 | if Ekind (Lit) = E_Enumeration_Literal then | |
2290 | if Base_Type (Etype (Lit)) /= Base_Type (Typ) then | |
2291 | Rewrite | |
2292 | (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc))); | |
2293 | else | |
2294 | Rewrite (N, New_Occurrence_Of (Lit, Loc)); | |
2295 | end if; | |
2296 | ||
2297 | Resolve (N, Typ); | |
2298 | end if; | |
bbab2db3 | 2299 | |
8cd5951d AC |
2300 | elsif Nkind (N) = N_Function_Call |
2301 | and then Is_Entity_Name (Name (N)) | |
2302 | and then Is_Intrinsic_Subprogram (Entity (Name (N))) | |
2303 | then | |
2304 | Eval_Intrinsic_Call (N, Entity (Name (N))); | |
2305 | ||
81e68a19 | 2306 | -- Ada 2022 (AI12-0075): If checking for potentially static expressions |
8cd5951d AC |
2307 | -- is enabled and we have a call to a static function, substitute a |
2308 | -- static value for the call, to allow folding the expression. This | |
2309 | -- supports checking the requirement of RM 6.8(5.3/5) in | |
2310 | -- Analyze_Expression_Function. | |
bbab2db3 GD |
2311 | |
2312 | elsif Checking_Potentially_Static_Expression | |
8cd5951d | 2313 | and then Is_Static_Function_Call (N) |
bbab2db3 | 2314 | then |
8cd5951d | 2315 | Fold_Dummy (N, Typ); |
c01a9391 AC |
2316 | end if; |
2317 | end Eval_Call; | |
2318 | ||
19d846a0 RD |
2319 | -------------------------- |
2320 | -- Eval_Case_Expression -- | |
2321 | -------------------------- | |
2322 | ||
ed7b9d6e | 2323 | -- A conditional expression is static if all its conditions and dependent |
edab6088 RD |
2324 | -- expressions are static. Note that we do not care if the dependent |
2325 | -- expressions raise CE, except for the one that will be selected. | |
19d846a0 RD |
2326 | |
2327 | procedure Eval_Case_Expression (N : Node_Id) is | |
edab6088 RD |
2328 | Alt : Node_Id; |
2329 | Choice : Node_Id; | |
19d846a0 RD |
2330 | |
2331 | begin | |
edab6088 | 2332 | Set_Is_Static_Expression (N, False); |
ed7b9d6e | 2333 | |
a6354842 AC |
2334 | if Error_Posted (Expression (N)) |
2335 | or else not Is_Static_Expression (Expression (N)) | |
2336 | then | |
ed7b9d6e | 2337 | Check_Non_Static_Context (Expression (N)); |
edab6088 | 2338 | return; |
ed7b9d6e | 2339 | end if; |
19d846a0 | 2340 | |
edab6088 | 2341 | -- First loop, make sure all the alternatives are static expressions |
1e3c434f | 2342 | -- none of which raise Constraint_Error. We make the Constraint_Error |
edab6088 RD |
2343 | -- check because part of the legality condition for a correct static |
2344 | -- case expression is that the cases are covered, like any other case | |
2345 | -- expression. And we can't do that if any of the conditions raise an | |
2346 | -- exception, so we don't even try to evaluate if that is the case. | |
2347 | ||
19d846a0 | 2348 | Alt := First (Alternatives (N)); |
edab6088 | 2349 | while Present (Alt) loop |
ed7b9d6e | 2350 | |
edab6088 RD |
2351 | -- The expression must be static, but we don't care at this stage |
2352 | -- if it raises Constraint_Error (the alternative might not match, | |
2353 | -- in which case the expression is statically unevaluated anyway). | |
ed7b9d6e | 2354 | |
edab6088 RD |
2355 | if not Is_Static_Expression (Expression (Alt)) then |
2356 | Check_Non_Static_Context (Expression (Alt)); | |
2357 | return; | |
2358 | end if; | |
ed7b9d6e | 2359 | |
edab6088 RD |
2360 | -- The choices of a case always have to be static, and cannot raise |
2361 | -- an exception. If this condition is not met, then the expression | |
2362 | -- is plain illegal, so just abandon evaluation attempts. No need | |
2363 | -- to check non-static context when we have something illegal anyway. | |
ed7b9d6e | 2364 | |
edab6088 RD |
2365 | if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then |
2366 | return; | |
ed7b9d6e AC |
2367 | end if; |
2368 | ||
19d846a0 | 2369 | Next (Alt); |
edab6088 | 2370 | end loop; |
ed7b9d6e | 2371 | |
edab6088 RD |
2372 | -- OK, if the above loop gets through it means that all choices are OK |
2373 | -- static (don't raise exceptions), so the whole case is static, and we | |
2374 | -- can find the matching alternative. | |
2375 | ||
2376 | Set_Is_Static_Expression (N); | |
2377 | ||
1e3c434f | 2378 | -- Now to deal with propagating a possible Constraint_Error |
edab6088 RD |
2379 | |
2380 | -- If the selecting expression raises CE, propagate and we are done | |
2381 | ||
2382 | if Raises_Constraint_Error (Expression (N)) then | |
2383 | Set_Raises_Constraint_Error (N); | |
2384 | ||
2385 | -- Otherwise we need to check the alternatives to find the matching | |
2386 | -- one. CE's in other than the matching one are not relevant. But we | |
2387 | -- do need to check the matching one. Unlike the first loop, we do not | |
2388 | -- have to go all the way through, when we find the matching one, quit. | |
ed7b9d6e AC |
2389 | |
2390 | else | |
edab6088 RD |
2391 | Alt := First (Alternatives (N)); |
2392 | Search : loop | |
2393 | ||
4bd4bb7f | 2394 | -- We must find a match among the alternatives. If not, this must |
edab6088 RD |
2395 | -- be due to other errors, so just ignore, leaving as non-static. |
2396 | ||
2397 | if No (Alt) then | |
2398 | Set_Is_Static_Expression (N, False); | |
2399 | return; | |
2400 | end if; | |
2401 | ||
2402 | -- Otherwise loop through choices of this alternative | |
2403 | ||
2404 | Choice := First (Discrete_Choices (Alt)); | |
2405 | while Present (Choice) loop | |
2406 | ||
2407 | -- If we find a matching choice, then the Expression of this | |
2408 | -- alternative replaces N (Raises_Constraint_Error flag is | |
2409 | -- included, so we don't have to special case that). | |
2410 | ||
2411 | if Choice_Matches (Expression (N), Choice) = Match then | |
2412 | Rewrite (N, Relocate_Node (Expression (Alt))); | |
2413 | return; | |
2414 | end if; | |
2415 | ||
2416 | Next (Choice); | |
2417 | end loop; | |
2418 | ||
2419 | Next (Alt); | |
2420 | end loop Search; | |
ed7b9d6e | 2421 | end if; |
19d846a0 RD |
2422 | end Eval_Case_Expression; |
2423 | ||
996ae0b0 RK |
2424 | ------------------------ |
2425 | -- Eval_Concatenation -- | |
2426 | ------------------------ | |
2427 | ||
3996951a TQ |
2428 | -- Concatenation is a static function, so the result is static if both |
2429 | -- operands are static (RM 4.9(7), 4.9(21)). | |
996ae0b0 RK |
2430 | |
2431 | procedure Eval_Concatenation (N : Node_Id) is | |
f91b40db GB |
2432 | Left : constant Node_Id := Left_Opnd (N); |
2433 | Right : constant Node_Id := Right_Opnd (N); | |
2434 | C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N))); | |
996ae0b0 RK |
2435 | Stat : Boolean; |
2436 | Fold : Boolean; | |
996ae0b0 RK |
2437 | |
2438 | begin | |
3996951a TQ |
2439 | -- Concatenation is never static in Ada 83, so if Ada 83 check operand |
2440 | -- non-static context. | |
996ae0b0 | 2441 | |
0ab80019 | 2442 | if Ada_Version = Ada_83 |
996ae0b0 RK |
2443 | and then Comes_From_Source (N) |
2444 | then | |
2445 | Check_Non_Static_Context (Left); | |
2446 | Check_Non_Static_Context (Right); | |
2447 | return; | |
2448 | end if; | |
2449 | ||
2450 | -- If not foldable we are done. In principle concatenation that yields | |
2451 | -- any string type is static (i.e. an array type of character types). | |
2452 | -- However, character types can include enumeration literals, and | |
2453 | -- concatenation in that case cannot be described by a literal, so we | |
2454 | -- only consider the operation static if the result is an array of | |
2455 | -- (a descendant of) a predefined character type. | |
2456 | ||
2457 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); | |
2458 | ||
3996951a | 2459 | if not (Is_Standard_Character_Type (C_Typ) and then Fold) then |
996ae0b0 RK |
2460 | Set_Is_Static_Expression (N, False); |
2461 | return; | |
2462 | end if; | |
2463 | ||
82c80734 | 2464 | -- Compile time string concatenation |
996ae0b0 | 2465 | |
3996951a TQ |
2466 | -- ??? Note that operands that are aggregates can be marked as static, |
2467 | -- so we should attempt at a later stage to fold concatenations with | |
2468 | -- such aggregates. | |
996ae0b0 RK |
2469 | |
2470 | declare | |
b54ddf5a BD |
2471 | Left_Str : constant Node_Id := Get_String_Val (Left); |
2472 | Left_Len : Nat; | |
2473 | Right_Str : constant Node_Id := Get_String_Val (Right); | |
dcd5fd67 | 2474 | Folded_Val : String_Id := No_String; |
996ae0b0 RK |
2475 | |
2476 | begin | |
2477 | -- Establish new string literal, and store left operand. We make | |
2478 | -- sure to use the special Start_String that takes an operand if | |
2479 | -- the left operand is a string literal. Since this is optimized | |
2480 | -- in the case where that is the most recently created string | |
2481 | -- literal, we ensure efficient time/space behavior for the | |
2482 | -- case of a concatenation of a series of string literals. | |
2483 | ||
2484 | if Nkind (Left_Str) = N_String_Literal then | |
c8307596 | 2485 | Left_Len := String_Length (Strval (Left_Str)); |
b54ddf5a BD |
2486 | |
2487 | -- If the left operand is the empty string, and the right operand | |
2488 | -- is a string literal (the case of "" & "..."), the result is the | |
2489 | -- value of the right operand. This optimization is important when | |
2490 | -- Is_Folded_In_Parser, to avoid copying an enormous right | |
2491 | -- operand. | |
2492 | ||
2493 | if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then | |
2494 | Folded_Val := Strval (Right_Str); | |
2495 | else | |
2496 | Start_String (Strval (Left_Str)); | |
2497 | end if; | |
2498 | ||
996ae0b0 RK |
2499 | else |
2500 | Start_String; | |
82c80734 | 2501 | Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str))); |
f91b40db | 2502 | Left_Len := 1; |
996ae0b0 RK |
2503 | end if; |
2504 | ||
b54ddf5a BD |
2505 | -- Now append the characters of the right operand, unless we |
2506 | -- optimized the "" & "..." case above. | |
996ae0b0 RK |
2507 | |
2508 | if Nkind (Right_Str) = N_String_Literal then | |
b54ddf5a BD |
2509 | if Left_Len /= 0 then |
2510 | Store_String_Chars (Strval (Right_Str)); | |
2511 | Folded_Val := End_String; | |
2512 | end if; | |
996ae0b0 | 2513 | else |
82c80734 | 2514 | Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str))); |
b54ddf5a | 2515 | Folded_Val := End_String; |
996ae0b0 RK |
2516 | end if; |
2517 | ||
2518 | Set_Is_Static_Expression (N, Stat); | |
2519 | ||
354c3840 AC |
2520 | -- If left operand is the empty string, the result is the |
2521 | -- right operand, including its bounds if anomalous. | |
f91b40db | 2522 | |
354c3840 AC |
2523 | if Left_Len = 0 |
2524 | and then Is_Array_Type (Etype (Right)) | |
2525 | and then Etype (Right) /= Any_String | |
2526 | then | |
2527 | Set_Etype (N, Etype (Right)); | |
996ae0b0 | 2528 | end if; |
354c3840 AC |
2529 | |
2530 | Fold_Str (N, Folded_Val, Static => Stat); | |
996ae0b0 RK |
2531 | end; |
2532 | end Eval_Concatenation; | |
2533 | ||
9b16cb57 RD |
2534 | ---------------------- |
2535 | -- Eval_Entity_Name -- | |
2536 | ---------------------- | |
2537 | ||
2538 | -- This procedure is used for identifiers and expanded names other than | |
2539 | -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are | |
2540 | -- static if they denote a static constant (RM 4.9(6)) or if the name | |
2541 | -- denotes an enumeration literal (RM 4.9(22)). | |
2542 | ||
2543 | procedure Eval_Entity_Name (N : Node_Id) is | |
2544 | Def_Id : constant Entity_Id := Entity (N); | |
2545 | Val : Node_Id; | |
2546 | ||
2547 | begin | |
2548 | -- Enumeration literals are always considered to be constants | |
1e3c434f | 2549 | -- and cannot raise Constraint_Error (RM 4.9(22)). |
9b16cb57 RD |
2550 | |
2551 | if Ekind (Def_Id) = E_Enumeration_Literal then | |
2552 | Set_Is_Static_Expression (N); | |
2553 | return; | |
2554 | ||
2555 | -- A name is static if it denotes a static constant (RM 4.9(5)), and | |
2556 | -- we also copy Raise_Constraint_Error. Notice that even if non-static, | |
2557 | -- it does not violate 10.2.1(8) here, since this is not a variable. | |
2558 | ||
2559 | elsif Ekind (Def_Id) = E_Constant then | |
2560 | ||
e03f7ccf AC |
2561 | -- Deferred constants must always be treated as nonstatic outside the |
2562 | -- scope of their full view. | |
9b16cb57 RD |
2563 | |
2564 | if Present (Full_View (Def_Id)) | |
2565 | and then not In_Open_Scopes (Scope (Def_Id)) | |
2566 | then | |
2567 | Val := Empty; | |
2568 | else | |
2569 | Val := Constant_Value (Def_Id); | |
2570 | end if; | |
2571 | ||
2572 | if Present (Val) then | |
2573 | Set_Is_Static_Expression | |
2574 | (N, Is_Static_Expression (Val) | |
2575 | and then Is_Static_Subtype (Etype (Def_Id))); | |
2576 | Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val)); | |
2577 | ||
2578 | if not Is_Static_Expression (N) | |
2579 | and then not Is_Generic_Type (Etype (N)) | |
2580 | then | |
2581 | Validate_Static_Object_Name (N); | |
2582 | end if; | |
2583 | ||
e03f7ccf AC |
2584 | -- Mark constant condition in SCOs |
2585 | ||
2586 | if Generate_SCO | |
2587 | and then Comes_From_Source (N) | |
2588 | and then Is_Boolean_Type (Etype (Def_Id)) | |
2589 | and then Compile_Time_Known_Value (N) | |
2590 | then | |
2591 | Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True); | |
2592 | end if; | |
2593 | ||
9b16cb57 RD |
2594 | return; |
2595 | end if; | |
bbab2db3 | 2596 | |
81e68a19 | 2597 | -- Ada 2022 (AI12-0075): If checking for potentially static expressions |
bbab2db3 GD |
2598 | -- is enabled and we have a reference to a formal parameter of mode in, |
2599 | -- substitute a static value for the reference, to allow folding the | |
2600 | -- expression. This supports checking the requirement of RM 6.8(5.3/5) | |
2601 | -- in Analyze_Expression_Function. | |
2602 | ||
2603 | elsif Ekind (Def_Id) = E_In_Parameter | |
2604 | and then Checking_Potentially_Static_Expression | |
8cd5951d | 2605 | and then Is_Static_Function (Scope (Def_Id)) |
bbab2db3 | 2606 | then |
8cd5951d | 2607 | Fold_Dummy (N, Etype (Def_Id)); |
9b16cb57 RD |
2608 | end if; |
2609 | ||
2610 | -- Fall through if the name is not static | |
2611 | ||
2612 | Validate_Static_Object_Name (N); | |
2613 | end Eval_Entity_Name; | |
2614 | ||
2615 | ------------------------ | |
2616 | -- Eval_If_Expression -- | |
2617 | ------------------------ | |
996ae0b0 | 2618 | |
9b16cb57 | 2619 | -- We can fold to a static expression if the condition and both dependent |
1cf3727f | 2620 | -- expressions are static. Otherwise, the only required processing is to do |
4d777a71 | 2621 | -- the check for non-static context for the then and else expressions. |
996ae0b0 | 2622 | |
9b16cb57 | 2623 | procedure Eval_If_Expression (N : Node_Id) is |
4d777a71 AC |
2624 | Condition : constant Node_Id := First (Expressions (N)); |
2625 | Then_Expr : constant Node_Id := Next (Condition); | |
2626 | Else_Expr : constant Node_Id := Next (Then_Expr); | |
2627 | Result : Node_Id; | |
2628 | Non_Result : Node_Id; | |
2629 | ||
2630 | Rstat : constant Boolean := | |
2631 | Is_Static_Expression (Condition) | |
2632 | and then | |
2633 | Is_Static_Expression (Then_Expr) | |
2634 | and then | |
2635 | Is_Static_Expression (Else_Expr); | |
edab6088 | 2636 | -- True if result is static |
4d777a71 | 2637 | |
996ae0b0 | 2638 | begin |
edab6088 RD |
2639 | -- If result not static, nothing to do, otherwise set static result |
2640 | ||
2641 | if not Rstat then | |
2642 | return; | |
2643 | else | |
2644 | Set_Is_Static_Expression (N); | |
2645 | end if; | |
2646 | ||
4d777a71 AC |
2647 | -- If any operand is Any_Type, just propagate to result and do not try |
2648 | -- to fold, this prevents cascaded errors. | |
2649 | ||
2650 | if Etype (Condition) = Any_Type or else | |
2651 | Etype (Then_Expr) = Any_Type or else | |
2652 | Etype (Else_Expr) = Any_Type | |
2653 | then | |
2654 | Set_Etype (N, Any_Type); | |
2655 | Set_Is_Static_Expression (N, False); | |
2656 | return; | |
edab6088 RD |
2657 | end if; |
2658 | ||
1e3c434f | 2659 | -- If condition raises Constraint_Error then we have already signaled |
edab6088 RD |
2660 | -- an error, and we just propagate to the result and do not fold. |
2661 | ||
2662 | if Raises_Constraint_Error (Condition) then | |
2663 | Set_Raises_Constraint_Error (N); | |
2664 | return; | |
2665 | end if; | |
4d777a71 AC |
2666 | |
2667 | -- Static case where we can fold. Note that we don't try to fold cases | |
2668 | -- where the condition is known at compile time, but the result is | |
2669 | -- non-static. This avoids possible cases of infinite recursion where | |
2670 | -- the expander puts in a redundant test and we remove it. Instead we | |
2671 | -- deal with these cases in the expander. | |
2672 | ||
edab6088 | 2673 | -- Select result operand |
4d777a71 | 2674 | |
edab6088 RD |
2675 | if Is_True (Expr_Value (Condition)) then |
2676 | Result := Then_Expr; | |
2677 | Non_Result := Else_Expr; | |
2678 | else | |
2679 | Result := Else_Expr; | |
2680 | Non_Result := Then_Expr; | |
2681 | end if; | |
4d777a71 | 2682 | |
edab6088 | 2683 | -- Note that it does not matter if the non-result operand raises a |
1e3c434f BD |
2684 | -- Constraint_Error, but if the result raises Constraint_Error then we |
2685 | -- replace the node with a raise Constraint_Error. This will properly | |
edab6088 | 2686 | -- propagate Raises_Constraint_Error since this flag is set in Result. |
4d777a71 | 2687 | |
edab6088 RD |
2688 | if Raises_Constraint_Error (Result) then |
2689 | Rewrite_In_Raise_CE (N, Result); | |
2690 | Check_Non_Static_Context (Non_Result); | |
4d777a71 | 2691 | |
edab6088 | 2692 | -- Otherwise the result operand replaces the original node |
4d777a71 AC |
2693 | |
2694 | else | |
edab6088 RD |
2695 | Rewrite (N, Relocate_Node (Result)); |
2696 | Set_Is_Static_Expression (N); | |
4d777a71 | 2697 | end if; |
9b16cb57 | 2698 | end Eval_If_Expression; |
996ae0b0 RK |
2699 | |
2700 | ---------------------------- | |
2701 | -- Eval_Indexed_Component -- | |
2702 | ---------------------------- | |
2703 | ||
8cbb664e MG |
2704 | -- Indexed components are never static, so we need to perform the check |
2705 | -- for non-static context on the index values. Then, we check if the | |
2706 | -- value can be obtained at compile time, even though it is non-static. | |
996ae0b0 RK |
2707 | |
2708 | procedure Eval_Indexed_Component (N : Node_Id) is | |
2709 | Expr : Node_Id; | |
2710 | ||
2711 | begin | |
fbf5a39b AC |
2712 | -- Check for non-static context on index values |
2713 | ||
996ae0b0 RK |
2714 | Expr := First (Expressions (N)); |
2715 | while Present (Expr) loop | |
2716 | Check_Non_Static_Context (Expr); | |
2717 | Next (Expr); | |
2718 | end loop; | |
2719 | ||
fbf5a39b AC |
2720 | -- If the indexed component appears in an object renaming declaration |
2721 | -- then we do not want to try to evaluate it, since in this case we | |
2722 | -- need the identity of the array element. | |
2723 | ||
2724 | if Nkind (Parent (N)) = N_Object_Renaming_Declaration then | |
2725 | return; | |
2726 | ||
2727 | -- Similarly if the indexed component appears as the prefix of an | |
2728 | -- attribute we don't want to evaluate it, because at least for | |
c94bbfbe | 2729 | -- some cases of attributes we need the identify (e.g. Access, Size). |
fbf5a39b AC |
2730 | |
2731 | elsif Nkind (Parent (N)) = N_Attribute_Reference then | |
2732 | return; | |
2733 | end if; | |
2734 | ||
2735 | -- Note: there are other cases, such as the left side of an assignment, | |
2736 | -- or an OUT parameter for a call, where the replacement results in the | |
2737 | -- illegal use of a constant, But these cases are illegal in the first | |
2738 | -- place, so the replacement, though silly, is harmless. | |
2739 | ||
2740 | -- Now see if this is a constant array reference | |
8cbb664e MG |
2741 | |
2742 | if List_Length (Expressions (N)) = 1 | |
2743 | and then Is_Entity_Name (Prefix (N)) | |
2744 | and then Ekind (Entity (Prefix (N))) = E_Constant | |
2745 | and then Present (Constant_Value (Entity (Prefix (N)))) | |
2746 | then | |
2747 | declare | |
2748 | Loc : constant Source_Ptr := Sloc (N); | |
2749 | Arr : constant Node_Id := Constant_Value (Entity (Prefix (N))); | |
2750 | Sub : constant Node_Id := First (Expressions (N)); | |
2751 | ||
2752 | Atyp : Entity_Id; | |
2753 | -- Type of array | |
2754 | ||
2755 | Lin : Nat; | |
2756 | -- Linear one's origin subscript value for array reference | |
2757 | ||
2758 | Lbd : Node_Id; | |
2759 | -- Lower bound of the first array index | |
2760 | ||
2761 | Elm : Node_Id; | |
2762 | -- Value from constant array | |
2763 | ||
2764 | begin | |
2765 | Atyp := Etype (Arr); | |
2766 | ||
2767 | if Is_Access_Type (Atyp) then | |
2768 | Atyp := Designated_Type (Atyp); | |
2769 | end if; | |
2770 | ||
9dbf1c3e RD |
2771 | -- If we have an array type (we should have but perhaps there are |
2772 | -- error cases where this is not the case), then see if we can do | |
2773 | -- a constant evaluation of the array reference. | |
8cbb664e | 2774 | |
ebd34478 | 2775 | if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then |
8cbb664e MG |
2776 | if Ekind (Atyp) = E_String_Literal_Subtype then |
2777 | Lbd := String_Literal_Low_Bound (Atyp); | |
2778 | else | |
2779 | Lbd := Type_Low_Bound (Etype (First_Index (Atyp))); | |
2780 | end if; | |
2781 | ||
2782 | if Compile_Time_Known_Value (Sub) | |
2783 | and then Nkind (Arr) = N_Aggregate | |
2784 | and then Compile_Time_Known_Value (Lbd) | |
2785 | and then Is_Discrete_Type (Component_Type (Atyp)) | |
2786 | then | |
2787 | Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1; | |
2788 | ||
2789 | if List_Length (Expressions (Arr)) >= Lin then | |
2790 | Elm := Pick (Expressions (Arr), Lin); | |
2791 | ||
d3bbfc59 | 2792 | -- If the resulting expression is compile-time-known, |
8cbb664e MG |
2793 | -- then we can rewrite the indexed component with this |
2794 | -- value, being sure to mark the result as non-static. | |
2795 | -- We also reset the Sloc, in case this generates an | |
2796 | -- error later on (e.g. 136'Access). | |
2797 | ||
2798 | if Compile_Time_Known_Value (Elm) then | |
2799 | Rewrite (N, Duplicate_Subexpr_No_Checks (Elm)); | |
2800 | Set_Is_Static_Expression (N, False); | |
2801 | Set_Sloc (N, Loc); | |
2802 | end if; | |
2803 | end if; | |
9fbb3ae6 AC |
2804 | |
2805 | -- We can also constant-fold if the prefix is a string literal. | |
2806 | -- This will be useful in an instantiation or an inlining. | |
2807 | ||
2808 | elsif Compile_Time_Known_Value (Sub) | |
2809 | and then Nkind (Arr) = N_String_Literal | |
2810 | and then Compile_Time_Known_Value (Lbd) | |
2811 | and then Expr_Value (Lbd) = 1 | |
2812 | and then Expr_Value (Sub) <= | |
2813 | String_Literal_Length (Etype (Arr)) | |
2814 | then | |
2815 | declare | |
2816 | C : constant Char_Code := | |
2817 | Get_String_Char (Strval (Arr), | |
2818 | UI_To_Int (Expr_Value (Sub))); | |
2819 | begin | |
2820 | Set_Character_Literal_Name (C); | |
2821 | ||
2822 | Elm := | |
2823 | Make_Character_Literal (Loc, | |
2824 | Chars => Name_Find, | |
2825 | Char_Literal_Value => UI_From_CC (C)); | |
2826 | Set_Etype (Elm, Component_Type (Atyp)); | |
2827 | Rewrite (N, Duplicate_Subexpr_No_Checks (Elm)); | |
2828 | Set_Is_Static_Expression (N, False); | |
2829 | end; | |
8cbb664e MG |
2830 | end if; |
2831 | end if; | |
2832 | end; | |
2833 | end if; | |
996ae0b0 RK |
2834 | end Eval_Indexed_Component; |
2835 | ||
2836 | -------------------------- | |
2837 | -- Eval_Integer_Literal -- | |
2838 | -------------------------- | |
2839 | ||
2840 | -- Numeric literals are static (RM 4.9(1)), and have already been marked | |
2841 | -- as static by the analyzer. The reason we did it that early is to allow | |
2842 | -- the possibility of turning off the Is_Static_Expression flag after | |
9dbf1c3e RD |
2843 | -- analysis, but before resolution, when integer literals are generated in |
2844 | -- the expander that do not correspond to static expressions. | |
996ae0b0 RK |
2845 | |
2846 | procedure Eval_Integer_Literal (N : Node_Id) is | |
400ad4e9 | 2847 | function In_Any_Integer_Context (Context : Node_Id) return Boolean; |
1d1bd8ad AC |
2848 | -- If the literal is resolved with a specific type in a context where |
2849 | -- the expected type is Any_Integer, there are no range checks on the | |
2850 | -- literal. By the time the literal is evaluated, it carries the type | |
2851 | -- imposed by the enclosing expression, and we must recover the context | |
2852 | -- to determine that Any_Integer is meant. | |
5d09245e AC |
2853 | |
2854 | ---------------------------- | |
09494c32 | 2855 | -- In_Any_Integer_Context -- |
5d09245e AC |
2856 | ---------------------------- |
2857 | ||
400ad4e9 | 2858 | function In_Any_Integer_Context (Context : Node_Id) return Boolean is |
5d09245e AC |
2859 | begin |
2860 | -- Any_Integer also appears in digits specifications for real types, | |
1d1bd8ad AC |
2861 | -- but those have bounds smaller that those of any integer base type, |
2862 | -- so we can safely ignore these cases. | |
5d09245e | 2863 | |
400ad4e9 | 2864 | return |
4a08c95c AC |
2865 | Nkind (Context) in N_Attribute_Definition_Clause |
2866 | | N_Attribute_Reference | |
2867 | | N_Modular_Type_Definition | |
2868 | | N_Number_Declaration | |
2869 | | N_Signed_Integer_Type_Definition; | |
5d09245e AC |
2870 | end In_Any_Integer_Context; |
2871 | ||
400ad4e9 HK |
2872 | -- Local variables |
2873 | ||
2874 | Par : constant Node_Id := Parent (N); | |
2875 | Typ : constant Entity_Id := Etype (N); | |
2876 | ||
5d09245e AC |
2877 | -- Start of processing for Eval_Integer_Literal |
2878 | ||
996ae0b0 RK |
2879 | begin |
2880 | -- If the literal appears in a non-expression context, then it is | |
1d1bd8ad AC |
2881 | -- certainly appearing in a non-static context, so check it. This is |
2882 | -- actually a redundant check, since Check_Non_Static_Context would | |
42f9f0fc | 2883 | -- check it, but it seems worthwhile to optimize out the call. |
996ae0b0 | 2884 | |
721500ab JS |
2885 | -- Additionally, when the literal appears within an if or case |
2886 | -- expression it must be checked as well. However, due to the literal | |
2887 | -- appearing within a conditional statement, expansion greatly changes | |
2888 | -- the nature of its context and performing some of the checks within | |
2889 | -- Check_Non_Static_Context on an expanded literal may lead to spurious | |
2890 | -- and misleading warnings. | |
a51368fa | 2891 | |
4a08c95c | 2892 | if (Nkind (Par) in N_Case_Expression_Alternative | N_If_Expression |
c94bbfbe | 2893 | or else Nkind (Par) not in N_Subexpr) |
4a08c95c AC |
2894 | and then (Nkind (Par) not in N_Case_Expression_Alternative |
2895 | | N_If_Expression | |
721500ab | 2896 | or else Comes_From_Source (N)) |
400ad4e9 | 2897 | and then not In_Any_Integer_Context (Par) |
5d09245e | 2898 | then |
996ae0b0 RK |
2899 | Check_Non_Static_Context (N); |
2900 | end if; | |
2901 | ||
2902 | -- Modular integer literals must be in their base range | |
2903 | ||
400ad4e9 HK |
2904 | if Is_Modular_Integer_Type (Typ) |
2905 | and then Is_Out_Of_Range (N, Base_Type (Typ), Assume_Valid => True) | |
996ae0b0 RK |
2906 | then |
2907 | Out_Of_Range (N); | |
2908 | end if; | |
2909 | end Eval_Integer_Literal; | |
2910 | ||
8cd5951d AC |
2911 | ------------------------- |
2912 | -- Eval_Intrinsic_Call -- | |
2913 | ------------------------- | |
2914 | ||
2915 | procedure Eval_Intrinsic_Call (N : Node_Id; E : Entity_Id) is | |
2916 | ||
2917 | procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind); | |
2918 | -- Evaluate an intrinsic shift call N on the given subprogram E. | |
2919 | -- Op is the kind for the shift node. | |
2920 | ||
2921 | ---------------- | |
2922 | -- Eval_Shift -- | |
2923 | ---------------- | |
2924 | ||
2925 | procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind) is | |
2926 | Left : constant Node_Id := First_Actual (N); | |
2927 | Right : constant Node_Id := Next_Actual (Left); | |
2928 | Static : constant Boolean := Is_Static_Function (E); | |
2929 | ||
2930 | begin | |
2931 | if Static then | |
2932 | if Checking_Potentially_Static_Expression then | |
2933 | Fold_Dummy (N, Etype (N)); | |
2934 | return; | |
2935 | end if; | |
2936 | end if; | |
2937 | ||
2938 | Fold_Shift | |
2939 | (N, Left, Right, Op, Static => Static, Check_Elab => not Static); | |
2940 | end Eval_Shift; | |
2941 | ||
2942 | Nam : Name_Id; | |
2943 | ||
2944 | begin | |
2945 | -- Nothing to do if the intrinsic is handled by the back end. | |
2946 | ||
2947 | if Present (Interface_Name (E)) then | |
2948 | return; | |
2949 | end if; | |
2950 | ||
2951 | -- Intrinsic calls as part of a static function is a language extension. | |
2952 | ||
2953 | if Checking_Potentially_Static_Expression | |
2954 | and then not Extensions_Allowed | |
2955 | then | |
2956 | return; | |
2957 | end if; | |
2958 | ||
2959 | -- If we have a renaming, expand the call to the original operation, | |
2960 | -- which must itself be intrinsic, since renaming requires matching | |
2961 | -- conventions and this has already been checked. | |
2962 | ||
2963 | if Present (Alias (E)) then | |
2964 | Eval_Intrinsic_Call (N, Alias (E)); | |
2965 | return; | |
2966 | end if; | |
2967 | ||
2968 | -- If the intrinsic subprogram is generic, gets its original name | |
2969 | ||
2970 | if Present (Parent (E)) | |
2971 | and then Present (Generic_Parent (Parent (E))) | |
2972 | then | |
2973 | Nam := Chars (Generic_Parent (Parent (E))); | |
2974 | else | |
2975 | Nam := Chars (E); | |
2976 | end if; | |
2977 | ||
2978 | case Nam is | |
8ad6af8f AC |
2979 | when Name_Shift_Left => |
2980 | Eval_Shift (N, E, N_Op_Shift_Left); | |
2981 | when Name_Shift_Right => | |
2982 | Eval_Shift (N, E, N_Op_Shift_Right); | |
2983 | when Name_Shift_Right_Arithmetic => | |
2984 | Eval_Shift (N, E, N_Op_Shift_Right_Arithmetic); | |
2985 | when others => | |
2986 | null; | |
8cd5951d AC |
2987 | end case; |
2988 | end Eval_Intrinsic_Call; | |
2989 | ||
996ae0b0 RK |
2990 | --------------------- |
2991 | -- Eval_Logical_Op -- | |
2992 | --------------------- | |
2993 | ||
2994 | -- Logical operations are static functions, so the result is potentially | |
2995 | -- static if both operands are potentially static (RM 4.9(7), 4.9(20)). | |
2996 | ||
2997 | procedure Eval_Logical_Op (N : Node_Id) is | |
2998 | Left : constant Node_Id := Left_Opnd (N); | |
2999 | Right : constant Node_Id := Right_Opnd (N); | |
3000 | Stat : Boolean; | |
3001 | Fold : Boolean; | |
3002 | ||
3003 | begin | |
3004 | -- If not foldable we are done | |
3005 | ||
3006 | Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold); | |
3007 | ||
3008 | if not Fold then | |
3009 | return; | |
3010 | end if; | |
3011 | ||
3012 | -- Compile time evaluation of logical operation | |
3013 | ||
3014 | declare | |
3015 | Left_Int : constant Uint := Expr_Value (Left); | |
3016 | Right_Int : constant Uint := Expr_Value (Right); | |
3017 | ||
3018 | begin | |
7a5b62b0 | 3019 | if Is_Modular_Integer_Type (Etype (N)) then |
996ae0b0 RK |
3020 | declare |
3021 | Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1); | |
3022 | Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1); | |
3023 | ||
3024 | begin | |
3025 | To_Bits (Left_Int, Left_Bits); | |
3026 | To_Bits (Right_Int, Right_Bits); | |
3027 | ||
3028 | -- Note: should really be able to use array ops instead of | |
8cd5951d AC |
3029 | -- these loops, but they break the build with a cryptic error |
3030 | -- during the bind of gnat1 likely due to a wrong computation | |
3031 | -- of a date or checksum. | |
996ae0b0 RK |
3032 | |
3033 | if Nkind (N) = N_Op_And then | |
3034 | for J in Left_Bits'Range loop | |
3035 | Left_Bits (J) := Left_Bits (J) and Right_Bits (J); | |
3036 | end loop; | |
3037 | ||
3038 | elsif Nkind (N) = N_Op_Or then | |
3039 | for J in Left_Bits'Range loop | |
3040 | Left_Bits (J) := Left_Bits (J) or Right_Bits (J); | |
3041 | end loop; | |
3042 | ||
3043 | else | |
3044 | pragma Assert (Nkind (N) = N_Op_Xor); | |
3045 | ||
3046 | for J in Left_Bits'Range loop | |
3047 | Left_Bits (J) := Left_Bits (J) xor Right_Bits (J); | |
3048 | end loop; | |
3049 | end if; | |
3050 | ||
fbf5a39b | 3051 | Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat); |
996ae0b0 RK |
3052 | end; |
3053 | ||
3054 | else | |
3055 | pragma Assert (Is_Boolean_Type (Etype (N))); | |
3056 | ||
3057 | if Nkind (N) = N_Op_And then | |
3058 | Fold_Uint (N, | |
fbf5a39b | 3059 | Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat); |
996ae0b0 RK |
3060 | |
3061 | elsif Nkind (N) = N_Op_Or then | |
3062 | Fold_Uint (N, | |
fbf5a39b | 3063 | Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat); |
996ae0b0 RK |
3064 | |
3065 | else | |
3066 | pragma Assert (Nkind (N) = N_Op_Xor); | |
3067 | Fold_Uint (N, | |
fbf5a39b | 3068 | Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat); |
996ae0b0 RK |
3069 | end if; |
3070 | end if; | |
996ae0b0 RK |
3071 | end; |
3072 | end Eval_Logical_Op; | |
3073 | ||
3074 | ------------------------ | |
3075 | -- Eval_Membership_Op -- | |
3076 | ------------------------ | |
3077 | ||
1d1bd8ad AC |
3078 | -- A membership test is potentially static if the expression is static, and |
3079 | -- the range is a potentially static range, or is a subtype mark denoting a | |
3080 | -- static subtype (RM 4.9(12)). | |
996ae0b0 RK |
3081 | |
3082 | procedure Eval_Membership_Op (N : Node_Id) is | |
edab6088 | 3083 | Alts : constant List_Id := Alternatives (N); |
87feba05 AC |
3084 | Choice : constant Node_Id := Right_Opnd (N); |
3085 | Expr : constant Node_Id := Left_Opnd (N); | |
edab6088 | 3086 | Result : Match_Result; |
996ae0b0 RK |
3087 | |
3088 | begin | |
1d1bd8ad AC |
3089 | -- Ignore if error in either operand, except to make sure that Any_Type |
3090 | -- is properly propagated to avoid junk cascaded errors. | |
996ae0b0 | 3091 | |
87feba05 AC |
3092 | if Etype (Expr) = Any_Type |
3093 | or else (Present (Choice) and then Etype (Choice) = Any_Type) | |
edab6088 | 3094 | then |
996ae0b0 RK |
3095 | Set_Etype (N, Any_Type); |
3096 | return; | |
3097 | end if; | |
3098 | ||
edab6088 | 3099 | -- If left operand non-static, then nothing to do |
996ae0b0 | 3100 | |
87feba05 | 3101 | if not Is_Static_Expression (Expr) then |
edab6088 RD |
3102 | return; |
3103 | end if; | |
996ae0b0 | 3104 | |
edab6088 | 3105 | -- If choice is non-static, left operand is in non-static context |
996ae0b0 | 3106 | |
87feba05 | 3107 | if (Present (Choice) and then not Is_Static_Choice (Choice)) |
edab6088 RD |
3108 | or else (Present (Alts) and then not Is_Static_Choice_List (Alts)) |
3109 | then | |
87feba05 | 3110 | Check_Non_Static_Context (Expr); |
edab6088 RD |
3111 | return; |
3112 | end if; | |
996ae0b0 | 3113 | |
edab6088 | 3114 | -- Otherwise we definitely have a static expression |
996ae0b0 | 3115 | |
edab6088 | 3116 | Set_Is_Static_Expression (N); |
996ae0b0 | 3117 | |
1e3c434f | 3118 | -- If left operand raises Constraint_Error, propagate and we are done |
996ae0b0 | 3119 | |
87feba05 | 3120 | if Raises_Constraint_Error (Expr) then |
edab6088 | 3121 | Set_Raises_Constraint_Error (N, True); |
996ae0b0 | 3122 | |
edab6088 | 3123 | -- See if we match |
996ae0b0 | 3124 | |
edab6088 | 3125 | else |
87feba05 AC |
3126 | if Present (Choice) then |
3127 | Result := Choice_Matches (Expr, Choice); | |
996ae0b0 | 3128 | else |
87feba05 | 3129 | Result := Choices_Match (Expr, Alts); |
996ae0b0 RK |
3130 | end if; |
3131 | ||
edab6088 RD |
3132 | -- If result is Non_Static, it means that we raise Constraint_Error, |
3133 | -- since we already tested that the operands were themselves static. | |
996ae0b0 | 3134 | |
edab6088 RD |
3135 | if Result = Non_Static then |
3136 | Set_Raises_Constraint_Error (N); | |
996ae0b0 | 3137 | |
edab6088 | 3138 | -- Otherwise we have our result (flipped if NOT IN case) |
996ae0b0 RK |
3139 | |
3140 | else | |
edab6088 RD |
3141 | Fold_Uint |
3142 | (N, Test ((Result = Match) xor (Nkind (N) = N_Not_In)), True); | |
3143 | Warn_On_Known_Condition (N); | |
996ae0b0 | 3144 | end if; |
996ae0b0 | 3145 | end if; |
996ae0b0 RK |
3146 | end Eval_Membership_Op; |
3147 | ||
3148 | ------------------------ | |
3149 | -- Eval_Named_Integer -- | |
3150 | ------------------------ | |
3151 | ||
3152 | procedure Eval_Named_Integer (N : Node_Id) is | |
3153 | begin | |
3154 | Fold_Uint (N, | |
fbf5a39b | 3155 | Expr_Value (Expression (Declaration_Node (Entity (N)))), True); |
996ae0b0 RK |
3156 | end Eval_Named_Integer; |
3157 | ||
3158 | --------------------- | |
3159 | -- Eval_Named_Real -- | |
3160 | --------------------- | |
3161 | ||
3162 | procedure Eval_Named_Real (N : Node_Id) is | |
3163 | begin | |
3164 | Fold_Ureal (N, | |
fbf5a39b | 3165 | Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True); |
996ae0b0 RK |
3166 | end Eval_Named_Real; |
3167 | ||
3168 | ------------------- | |
3169 | -- Eval_Op_Expon -- | |
3170 | ------------------- | |
3171 | ||
3172 | -- Exponentiation is a static functions, so the result is potentially | |
3173 | -- static if both operands are potentially static (RM 4.9(7), 4.9(20)). | |
3174 | ||
3175 | procedure Eval_Op_Expon (N : Node_Id) is | |
3176 | Left : constant Node_Id := Left_Opnd (N); | |
3177 | Right : constant Node_Id := Right_Opnd (N); | |
3178 | Stat : Boolean; | |
3179 | Fold : Boolean; | |
3180 | ||
3181 | begin | |
3182 | -- If not foldable we are done | |
3183 | ||
6c3c671e AC |
3184 | Test_Expression_Is_Foldable |
3185 | (N, Left, Right, Stat, Fold, CRT_Safe => True); | |
3186 | ||
3187 | -- Return if not foldable | |
996ae0b0 RK |
3188 | |
3189 | if not Fold then | |
3190 | return; | |
3191 | end if; | |
3192 | ||
6c3c671e AC |
3193 | if Configurable_Run_Time_Mode and not Stat then |
3194 | return; | |
3195 | end if; | |
3196 | ||
996ae0b0 RK |
3197 | -- Fold exponentiation operation |
3198 | ||
3199 | declare | |
3200 | Right_Int : constant Uint := Expr_Value (Right); | |
3201 | ||
3202 | begin | |
3203 | -- Integer case | |
3204 | ||
3205 | if Is_Integer_Type (Etype (Left)) then | |
3206 | declare | |
3207 | Left_Int : constant Uint := Expr_Value (Left); | |
3208 | Result : Uint; | |
3209 | ||
3210 | begin | |
22cb89b5 AC |
3211 | -- Exponentiation of an integer raises Constraint_Error for a |
3212 | -- negative exponent (RM 4.5.6). | |
996ae0b0 RK |
3213 | |
3214 | if Right_Int < 0 then | |
3215 | Apply_Compile_Time_Constraint_Error | |
80298c3b | 3216 | (N, "integer exponent negative", CE_Range_Check_Failed, |
fbf5a39b | 3217 | Warn => not Stat); |
996ae0b0 RK |
3218 | return; |
3219 | ||
3220 | else | |
3221 | if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then | |
3222 | Result := Left_Int ** Right_Int; | |
3223 | else | |
3224 | Result := Left_Int; | |
3225 | end if; | |
3226 | ||
3227 | if Is_Modular_Integer_Type (Etype (N)) then | |
3228 | Result := Result mod Modulus (Etype (N)); | |
3229 | end if; | |
3230 | ||
b626569a YM |
3231 | Check_Non_Static_Context_For_Overflow (N, Stat, Result); |
3232 | ||
fbf5a39b | 3233 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
3234 | end if; |
3235 | end; | |
3236 | ||
3237 | -- Real case | |
3238 | ||
3239 | else | |
3240 | declare | |
3241 | Left_Real : constant Ureal := Expr_Value_R (Left); | |
3242 | ||
3243 | begin | |
3244 | -- Cannot have a zero base with a negative exponent | |
3245 | ||
3246 | if UR_Is_Zero (Left_Real) then | |
3247 | ||
3248 | if Right_Int < 0 then | |
3249 | Apply_Compile_Time_Constraint_Error | |
80298c3b | 3250 | (N, "zero ** negative integer", CE_Range_Check_Failed, |
fbf5a39b | 3251 | Warn => not Stat); |
996ae0b0 RK |
3252 | return; |
3253 | else | |
fbf5a39b | 3254 | Fold_Ureal (N, Ureal_0, Stat); |
996ae0b0 RK |
3255 | end if; |
3256 | ||
3257 | else | |
fbf5a39b | 3258 | Fold_Ureal (N, Left_Real ** Right_Int, Stat); |
996ae0b0 RK |
3259 | end if; |
3260 | end; | |
3261 | end if; | |
996ae0b0 RK |
3262 | end; |
3263 | end Eval_Op_Expon; | |
3264 | ||
3265 | ----------------- | |
3266 | -- Eval_Op_Not -- | |
3267 | ----------------- | |
3268 | ||
23a9215f | 3269 | -- The not operation is a static function, so the result is potentially |
996ae0b0 RK |
3270 | -- static if the operand is potentially static (RM 4.9(7), 4.9(20)). |
3271 | ||
3272 | procedure Eval_Op_Not (N : Node_Id) is | |
3273 | Right : constant Node_Id := Right_Opnd (N); | |
3274 | Stat : Boolean; | |
3275 | Fold : Boolean; | |
3276 | ||
3277 | begin | |
3278 | -- If not foldable we are done | |
3279 | ||
3280 | Test_Expression_Is_Foldable (N, Right, Stat, Fold); | |
3281 | ||
3282 | if not Fold then | |
3283 | return; | |
3284 | end if; | |
3285 | ||
3286 | -- Fold not operation | |
3287 | ||
3288 | declare | |
3289 | Rint : constant Uint := Expr_Value (Right); | |
3290 | Typ : constant Entity_Id := Etype (N); | |
3291 | ||
3292 | begin | |
1d1bd8ad AC |
3293 | -- Negation is equivalent to subtracting from the modulus minus one. |
3294 | -- For a binary modulus this is equivalent to the ones-complement of | |
a95f708e | 3295 | -- the original value. For a nonbinary modulus this is an arbitrary |
1d1bd8ad | 3296 | -- but consistent definition. |
996ae0b0 RK |
3297 | |
3298 | if Is_Modular_Integer_Type (Typ) then | |
fbf5a39b | 3299 | Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat); |
80298c3b | 3300 | else pragma Assert (Is_Boolean_Type (Typ)); |
fbf5a39b | 3301 | Fold_Uint (N, Test (not Is_True (Rint)), Stat); |
996ae0b0 RK |
3302 | end if; |
3303 | ||
3304 | Set_Is_Static_Expression (N, Stat); | |
3305 | end; | |
3306 | end Eval_Op_Not; | |
3307 | ||
3308 | ------------------------------- | |
3309 | -- Eval_Qualified_Expression -- | |
3310 | ------------------------------- | |
3311 | ||
3312 | -- A qualified expression is potentially static if its subtype mark denotes | |
c94bbfbe | 3313 | -- a static subtype and its expression is potentially static (RM 4.9 (10)). |
996ae0b0 RK |
3314 | |
3315 | procedure Eval_Qualified_Expression (N : Node_Id) is | |
3316 | Operand : constant Node_Id := Expression (N); | |
3317 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
3318 | ||
07fc65c4 GB |
3319 | Stat : Boolean; |
3320 | Fold : Boolean; | |
3321 | Hex : Boolean; | |
996ae0b0 RK |
3322 | |
3323 | begin | |
1d1bd8ad | 3324 | -- Can only fold if target is string or scalar and subtype is static. |
22cb89b5 AC |
3325 | -- Also, do not fold if our parent is an allocator (this is because the |
3326 | -- qualified expression is really part of the syntactic structure of an | |
3327 | -- allocator, and we do not want to end up with something that | |
996ae0b0 RK |
3328 | -- corresponds to "new 1" where the 1 is the result of folding a |
3329 | -- qualified expression). | |
3330 | ||
3331 | if not Is_Static_Subtype (Target_Type) | |
3332 | or else Nkind (Parent (N)) = N_Allocator | |
3333 | then | |
3334 | Check_Non_Static_Context (Operand); | |
af152989 | 3335 | |
c94bbfbe | 3336 | -- If operand is known to raise Constraint_Error, set the flag on the |
1d1bd8ad | 3337 | -- expression so it does not get optimized away. |
af152989 AC |
3338 | |
3339 | if Nkind (Operand) = N_Raise_Constraint_Error then | |
3340 | Set_Raises_Constraint_Error (N); | |
3341 | end if; | |
7324bf49 | 3342 | |
996ae0b0 | 3343 | return; |
85f6d7e2 GD |
3344 | |
3345 | -- Also return if a semantic error has been posted on the node, as we | |
3346 | -- don't want to fold in that case (for GNATprove, the node might lead | |
3347 | -- to Constraint_Error but won't have been replaced with a raise node | |
3348 | -- or marked as raising CE). | |
3349 | ||
3350 | elsif Error_Posted (N) then | |
3351 | return; | |
996ae0b0 RK |
3352 | end if; |
3353 | ||
3354 | -- If not foldable we are done | |
3355 | ||
3356 | Test_Expression_Is_Foldable (N, Operand, Stat, Fold); | |
3357 | ||
3358 | if not Fold then | |
3359 | return; | |
3360 | ||
1e3c434f | 3361 | -- Don't try fold if target type has Constraint_Error bounds |
996ae0b0 RK |
3362 | |
3363 | elsif not Is_OK_Static_Subtype (Target_Type) then | |
3364 | Set_Raises_Constraint_Error (N); | |
3365 | return; | |
3366 | end if; | |
3367 | ||
3368 | -- Fold the result of qualification | |
3369 | ||
3370 | if Is_Discrete_Type (Target_Type) then | |
c94bbfbe PT |
3371 | |
3372 | -- Save Print_In_Hex indication | |
3373 | ||
3374 | Hex := Nkind (Operand) = N_Integer_Literal | |
3375 | and then Print_In_Hex (Operand); | |
3376 | ||
fbf5a39b | 3377 | Fold_Uint (N, Expr_Value (Operand), Stat); |
996ae0b0 | 3378 | |
07fc65c4 GB |
3379 | -- Preserve Print_In_Hex indication |
3380 | ||
3381 | if Hex and then Nkind (N) = N_Integer_Literal then | |
3382 | Set_Print_In_Hex (N); | |
3383 | end if; | |
3384 | ||
996ae0b0 | 3385 | elsif Is_Real_Type (Target_Type) then |
fbf5a39b | 3386 | Fold_Ureal (N, Expr_Value_R (Operand), Stat); |
996ae0b0 RK |
3387 | |
3388 | else | |
fbf5a39b | 3389 | Fold_Str (N, Strval (Get_String_Val (Operand)), Stat); |
996ae0b0 RK |
3390 | |
3391 | if not Stat then | |
3392 | Set_Is_Static_Expression (N, False); | |
3393 | else | |
3394 | Check_String_Literal_Length (N, Target_Type); | |
3395 | end if; | |
3396 | ||
3397 | return; | |
3398 | end if; | |
3399 | ||
fbf5a39b AC |
3400 | -- The expression may be foldable but not static |
3401 | ||
3402 | Set_Is_Static_Expression (N, Stat); | |
3403 | ||
c800f862 | 3404 | if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then |
996ae0b0 RK |
3405 | Out_Of_Range (N); |
3406 | end if; | |
996ae0b0 RK |
3407 | end Eval_Qualified_Expression; |
3408 | ||
3409 | ----------------------- | |
3410 | -- Eval_Real_Literal -- | |
3411 | ----------------------- | |
3412 | ||
3413 | -- Numeric literals are static (RM 4.9(1)), and have already been marked | |
3414 | -- as static by the analyzer. The reason we did it that early is to allow | |
3415 | -- the possibility of turning off the Is_Static_Expression flag after | |
3416 | -- analysis, but before resolution, when integer literals are generated | |
3417 | -- in the expander that do not correspond to static expressions. | |
3418 | ||
3419 | procedure Eval_Real_Literal (N : Node_Id) is | |
a1980be8 GB |
3420 | PK : constant Node_Kind := Nkind (Parent (N)); |
3421 | ||
996ae0b0 | 3422 | begin |
1d1bd8ad AC |
3423 | -- If the literal appears in a non-expression context and not as part of |
3424 | -- a number declaration, then it is appearing in a non-static context, | |
3425 | -- so check it. | |
996ae0b0 | 3426 | |
a1980be8 | 3427 | if PK not in N_Subexpr and then PK /= N_Number_Declaration then |
996ae0b0 RK |
3428 | Check_Non_Static_Context (N); |
3429 | end if; | |
996ae0b0 RK |
3430 | end Eval_Real_Literal; |
3431 | ||
3432 | ------------------------ | |
3433 | -- Eval_Relational_Op -- | |
3434 | ------------------------ | |
3435 | ||
8a95f4e8 | 3436 | -- Relational operations are static functions, so the result is static if |
3795dac6 AC |
3437 | -- both operands are static (RM 4.9(7), 4.9(20)), except that up to Ada |
3438 | -- 2012, for strings the result is never static, even if the operands are. | |
81e68a19 | 3439 | -- The string case was relaxed in Ada 2022, see AI12-0201. |
996ae0b0 | 3440 | |
fc3a3f3b RD |
3441 | -- However, for internally generated nodes, we allow string equality and |
3442 | -- inequality to be static. This is because we rewrite A in "ABC" as an | |
3443 | -- equality test A = "ABC", and the former is definitely static. | |
3444 | ||
996ae0b0 | 3445 | procedure Eval_Relational_Op (N : Node_Id) is |
634a926b AC |
3446 | Left : constant Node_Id := Left_Opnd (N); |
3447 | Right : constant Node_Id := Right_Opnd (N); | |
996ae0b0 | 3448 | |
634a926b AC |
3449 | procedure Decompose_Expr |
3450 | (Expr : Node_Id; | |
3451 | Ent : out Entity_Id; | |
3452 | Kind : out Character; | |
3453 | Cons : out Uint; | |
3454 | Orig : Boolean := True); | |
3455 | -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent | |
3456 | -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or | |
3457 | -- simple entity, and Cons is the value of K. If the expression is not | |
3458 | -- of the required form, Ent is set to Empty. | |
3459 | -- | |
3460 | -- Orig indicates whether Expr is the original expression to consider, | |
2da8c8e2 | 3461 | -- or if we are handling a subexpression (e.g. recursive call to |
634a926b AC |
3462 | -- Decompose_Expr). |
3463 | ||
3464 | procedure Fold_General_Op (Is_Static : Boolean); | |
3465 | -- Attempt to fold arbitrary relational operator N. Flag Is_Static must | |
3466 | -- be set when the operator denotes a static expression. | |
3467 | ||
3468 | procedure Fold_Static_Real_Op; | |
3469 | -- Attempt to fold static real type relational operator N | |
3470 | ||
3471 | function Static_Length (Expr : Node_Id) return Uint; | |
3472 | -- If Expr is an expression for a constrained array whose length is | |
3473 | -- known at compile time, return the non-negative length, otherwise | |
3474 | -- return -1. | |
3475 | ||
3476 | -------------------- | |
3477 | -- Decompose_Expr -- | |
3478 | -------------------- | |
3479 | ||
3480 | procedure Decompose_Expr | |
3481 | (Expr : Node_Id; | |
3482 | Ent : out Entity_Id; | |
3483 | Kind : out Character; | |
3484 | Cons : out Uint; | |
3485 | Orig : Boolean := True) | |
3486 | is | |
3487 | Exp : Node_Id; | |
996ae0b0 | 3488 | |
634a926b AC |
3489 | begin |
3490 | -- Assume that the expression does not meet the expected form | |
3491 | ||
3492 | Cons := No_Uint; | |
3493 | Ent := Empty; | |
3494 | Kind := '?'; | |
3495 | ||
3496 | if Nkind (Expr) = N_Op_Add | |
3497 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
996ae0b0 | 3498 | then |
634a926b AC |
3499 | Exp := Left_Opnd (Expr); |
3500 | Cons := Expr_Value (Right_Opnd (Expr)); | |
3501 | ||
3502 | elsif Nkind (Expr) = N_Op_Subtract | |
3503 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
3504 | then | |
3505 | Exp := Left_Opnd (Expr); | |
3506 | Cons := -Expr_Value (Right_Opnd (Expr)); | |
3507 | ||
3508 | -- If the bound is a constant created to remove side effects, recover | |
3509 | -- the original expression to see if it has one of the recognizable | |
3510 | -- forms. | |
3511 | ||
3512 | elsif Nkind (Expr) = N_Identifier | |
3513 | and then not Comes_From_Source (Entity (Expr)) | |
3514 | and then Ekind (Entity (Expr)) = E_Constant | |
3515 | and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration | |
3516 | then | |
3517 | Exp := Expression (Parent (Entity (Expr))); | |
3518 | Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False); | |
3519 | ||
3520 | -- If original expression includes an entity, create a reference | |
3521 | -- to it for use below. | |
3522 | ||
3523 | if Present (Ent) then | |
3524 | Exp := New_Occurrence_Of (Ent, Sloc (Ent)); | |
3525 | else | |
3526 | return; | |
3527 | end if; | |
3528 | ||
3529 | else | |
3530 | -- Only consider the case of X + 0 for a full expression, and | |
3531 | -- not when recursing, otherwise we may end up with evaluating | |
3532 | -- expressions not known at compile time to 0. | |
3533 | ||
3534 | if Orig then | |
3535 | Exp := Expr; | |
3536 | Cons := Uint_0; | |
3537 | else | |
3538 | return; | |
3539 | end if; | |
996ae0b0 RK |
3540 | end if; |
3541 | ||
634a926b | 3542 | -- At this stage Exp is set to the potential X |
45fc7ddb | 3543 | |
634a926b AC |
3544 | if Nkind (Exp) = N_Attribute_Reference then |
3545 | if Attribute_Name (Exp) = Name_First then | |
3546 | Kind := 'F'; | |
3547 | elsif Attribute_Name (Exp) = Name_Last then | |
3548 | Kind := 'L'; | |
3549 | else | |
3550 | return; | |
3551 | end if; | |
996ae0b0 | 3552 | |
634a926b | 3553 | Exp := Prefix (Exp); |
fbf5a39b | 3554 | |
634a926b AC |
3555 | else |
3556 | Kind := 'E'; | |
3557 | end if; | |
996ae0b0 | 3558 | |
634a926b AC |
3559 | if Is_Entity_Name (Exp) and then Present (Entity (Exp)) then |
3560 | Ent := Entity (Exp); | |
3561 | end if; | |
3562 | end Decompose_Expr; | |
3563 | ||
3564 | --------------------- | |
3565 | -- Fold_General_Op -- | |
3566 | --------------------- | |
3567 | ||
3568 | procedure Fold_General_Op (Is_Static : Boolean) is | |
3569 | CR : constant Compare_Result := | |
3570 | Compile_Time_Compare (Left, Right, Assume_Valid => False); | |
45fc7ddb | 3571 | |
634a926b AC |
3572 | Result : Boolean; |
3573 | ||
3574 | begin | |
3575 | if CR = Unknown then | |
3576 | return; | |
3577 | end if; | |
3578 | ||
3579 | case Nkind (N) is | |
3580 | when N_Op_Eq => | |
3581 | if CR = EQ then | |
3582 | Result := True; | |
3583 | elsif CR = NE or else CR = GT or else CR = LT then | |
3584 | Result := False; | |
3585 | else | |
45fc7ddb HK |
3586 | return; |
3587 | end if; | |
3588 | ||
634a926b AC |
3589 | when N_Op_Ge => |
3590 | if CR = GT or else CR = EQ or else CR = GE then | |
3591 | Result := True; | |
3592 | elsif CR = LT then | |
3593 | Result := False; | |
3594 | else | |
45fc7ddb HK |
3595 | return; |
3596 | end if; | |
996ae0b0 | 3597 | |
634a926b AC |
3598 | when N_Op_Gt => |
3599 | if CR = GT then | |
3600 | Result := True; | |
3601 | elsif CR = EQ or else CR = LT or else CR = LE then | |
3602 | Result := False; | |
3603 | else | |
3604 | return; | |
3605 | end if; | |
45fc7ddb | 3606 | |
634a926b AC |
3607 | when N_Op_Le => |
3608 | if CR = LT or else CR = EQ or else CR = LE then | |
3609 | Result := True; | |
3610 | elsif CR = GT then | |
3611 | Result := False; | |
3612 | else | |
3613 | return; | |
3614 | end if; | |
45fc7ddb | 3615 | |
634a926b AC |
3616 | when N_Op_Lt => |
3617 | if CR = LT then | |
3618 | Result := True; | |
3619 | elsif CR = EQ or else CR = GT or else CR = GE then | |
3620 | Result := False; | |
3621 | else | |
3622 | return; | |
3623 | end if; | |
45fc7ddb | 3624 | |
634a926b AC |
3625 | when N_Op_Ne => |
3626 | if CR = NE or else CR = GT or else CR = LT then | |
3627 | Result := True; | |
3628 | elsif CR = EQ then | |
3629 | Result := False; | |
3630 | else | |
45fc7ddb HK |
3631 | return; |
3632 | end if; | |
3633 | ||
634a926b AC |
3634 | when others => |
3635 | raise Program_Error; | |
3636 | end case; | |
45fc7ddb | 3637 | |
634a926b AC |
3638 | -- Determine the potential outcome of the relation assuming the |
3639 | -- operands are valid and emit a warning when the relation yields | |
3640 | -- True or False only in the presence of invalid values. | |
3fbbbd1e | 3641 | |
634a926b | 3642 | Warn_On_Constant_Valid_Condition (N); |
e49de265 | 3643 | |
634a926b AC |
3644 | Fold_Uint (N, Test (Result), Is_Static); |
3645 | end Fold_General_Op; | |
e49de265 | 3646 | |
634a926b AC |
3647 | ------------------------- |
3648 | -- Fold_Static_Real_Op -- | |
3649 | ------------------------- | |
45fc7ddb | 3650 | |
634a926b AC |
3651 | procedure Fold_Static_Real_Op is |
3652 | Left_Real : constant Ureal := Expr_Value_R (Left); | |
3653 | Right_Real : constant Ureal := Expr_Value_R (Right); | |
3654 | Result : Boolean; | |
996ae0b0 | 3655 | |
634a926b AC |
3656 | begin |
3657 | case Nkind (N) is | |
3658 | when N_Op_Eq => Result := (Left_Real = Right_Real); | |
3659 | when N_Op_Ge => Result := (Left_Real >= Right_Real); | |
3660 | when N_Op_Gt => Result := (Left_Real > Right_Real); | |
3661 | when N_Op_Le => Result := (Left_Real <= Right_Real); | |
3662 | when N_Op_Lt => Result := (Left_Real < Right_Real); | |
3663 | when N_Op_Ne => Result := (Left_Real /= Right_Real); | |
3664 | when others => raise Program_Error; | |
3665 | end case; | |
3666 | ||
3667 | Fold_Uint (N, Test (Result), True); | |
3668 | end Fold_Static_Real_Op; | |
8a95f4e8 | 3669 | |
634a926b AC |
3670 | ------------------- |
3671 | -- Static_Length -- | |
3672 | ------------------- | |
8a95f4e8 | 3673 | |
634a926b AC |
3674 | function Static_Length (Expr : Node_Id) return Uint is |
3675 | Cons1 : Uint; | |
3676 | Cons2 : Uint; | |
3677 | Ent1 : Entity_Id; | |
3678 | Ent2 : Entity_Id; | |
3679 | Kind1 : Character; | |
3680 | Kind2 : Character; | |
3681 | Typ : Entity_Id; | |
8a95f4e8 | 3682 | |
634a926b AC |
3683 | begin |
3684 | -- First easy case string literal | |
8a95f4e8 | 3685 | |
634a926b AC |
3686 | if Nkind (Expr) = N_String_Literal then |
3687 | return UI_From_Int (String_Length (Strval (Expr))); | |
45fc7ddb | 3688 | |
fe44c442 YM |
3689 | -- With frontend inlining as performed in GNATprove mode, a variable |
3690 | -- may be inserted that has a string literal subtype. Deal with this | |
3691 | -- specially as for the previous case. | |
3692 | ||
3693 | elsif Ekind (Etype (Expr)) = E_String_Literal_Subtype then | |
3694 | return String_Literal_Length (Etype (Expr)); | |
3695 | ||
634a926b | 3696 | -- Second easy case, not constrained subtype, so no length |
45fc7ddb | 3697 | |
634a926b AC |
3698 | elsif not Is_Constrained (Etype (Expr)) then |
3699 | return Uint_Minus_1; | |
3700 | end if; | |
45fc7ddb | 3701 | |
634a926b | 3702 | -- General case |
45fc7ddb | 3703 | |
634a926b | 3704 | Typ := Etype (First_Index (Etype (Expr))); |
45fc7ddb | 3705 | |
634a926b | 3706 | -- The simple case, both bounds are known at compile time |
45fc7ddb | 3707 | |
634a926b AC |
3708 | if Is_Discrete_Type (Typ) |
3709 | and then Compile_Time_Known_Value (Type_Low_Bound (Typ)) | |
3710 | and then Compile_Time_Known_Value (Type_High_Bound (Typ)) | |
3711 | then | |
3712 | return | |
3713 | UI_Max (Uint_0, Expr_Value (Type_High_Bound (Typ)) - | |
3714 | Expr_Value (Type_Low_Bound (Typ)) + 1); | |
3715 | end if; | |
45fc7ddb | 3716 | |
634a926b AC |
3717 | -- A more complex case, where the bounds are of the form X [+/- K1] |
3718 | -- .. X [+/- K2]), where X is an expression that is either A'First or | |
3719 | -- A'Last (with A an entity name), or X is an entity name, and the | |
3720 | -- two X's are the same and K1 and K2 are known at compile time, in | |
3721 | -- this case, the length can also be computed at compile time, even | |
3722 | -- though the bounds are not known. A common case of this is e.g. | |
3723 | -- (X'First .. X'First+5). | |
3724 | ||
3725 | Decompose_Expr | |
3726 | (Original_Node (Type_Low_Bound (Typ)), Ent1, Kind1, Cons1); | |
3727 | Decompose_Expr | |
3728 | (Original_Node (Type_High_Bound (Typ)), Ent2, Kind2, Cons2); | |
3729 | ||
3730 | if Present (Ent1) and then Ent1 = Ent2 and then Kind1 = Kind2 then | |
3731 | return Cons2 - Cons1 + 1; | |
3732 | else | |
3733 | return Uint_Minus_1; | |
3734 | end if; | |
3735 | end Static_Length; | |
45fc7ddb | 3736 | |
634a926b | 3737 | -- Local variables |
45fc7ddb | 3738 | |
634a926b AC |
3739 | Left_Typ : constant Entity_Id := Etype (Left); |
3740 | Right_Typ : constant Entity_Id := Etype (Right); | |
3741 | Fold : Boolean; | |
3742 | Left_Len : Uint; | |
3743 | Op_Typ : Entity_Id := Empty; | |
3744 | Right_Len : Uint; | |
996ae0b0 | 3745 | |
634a926b | 3746 | Is_Static_Expression : Boolean; |
45fc7ddb | 3747 | |
634a926b | 3748 | -- Start of processing for Eval_Relational_Op |
996ae0b0 | 3749 | |
634a926b AC |
3750 | begin |
3751 | -- One special case to deal with first. If we can tell that the result | |
3752 | -- will be false because the lengths of one or more index subtypes are | |
2da8c8e2 GD |
3753 | -- compile-time known and different, then we can replace the entire |
3754 | -- result by False. We only do this for one-dimensional arrays, because | |
3755 | -- the case of multidimensional arrays is rare and too much trouble. If | |
634a926b AC |
3756 | -- one of the operands is an illegal aggregate, its type might still be |
3757 | -- an arbitrary composite type, so nothing to do. | |
45fc7ddb | 3758 | |
634a926b AC |
3759 | if Is_Array_Type (Left_Typ) |
3760 | and then Left_Typ /= Any_Composite | |
3761 | and then Number_Dimensions (Left_Typ) = 1 | |
4a08c95c | 3762 | and then Nkind (N) in N_Op_Eq | N_Op_Ne |
634a926b AC |
3763 | then |
3764 | if Raises_Constraint_Error (Left) | |
3765 | or else | |
3766 | Raises_Constraint_Error (Right) | |
3767 | then | |
3768 | return; | |
cc7c52c1 | 3769 | end if; |
996ae0b0 | 3770 | |
634a926b AC |
3771 | -- OK, we have the case where we may be able to do this fold |
3772 | ||
cc7c52c1 PT |
3773 | Left_Len := Static_Length (Left); |
3774 | Right_Len := Static_Length (Right); | |
634a926b | 3775 | |
cc7c52c1 PT |
3776 | if Left_Len /= Uint_Minus_1 |
3777 | and then Right_Len /= Uint_Minus_1 | |
3778 | and then Left_Len /= Right_Len | |
3779 | then | |
81e68a19 | 3780 | -- AI12-0201: comparison of string is static in Ada 2022 |
cc7c52c1 PT |
3781 | |
3782 | Fold_Uint | |
3783 | (N, | |
3784 | Test (Nkind (N) = N_Op_Ne), | |
81e68a19 | 3785 | Static => Ada_Version >= Ada_2022 |
cc7c52c1 PT |
3786 | and then Is_String_Type (Left_Typ)); |
3787 | Warn_On_Known_Condition (N); | |
3788 | return; | |
634a926b | 3789 | end if; |
cc7c52c1 | 3790 | end if; |
80298c3b | 3791 | |
634a926b | 3792 | -- General case |
996ae0b0 | 3793 | |
cc7c52c1 PT |
3794 | -- Initialize the value of Is_Static_Expression. The value of Fold |
3795 | -- returned by Test_Expression_Is_Foldable is not needed since, even | |
3796 | -- when some operand is a variable, we can still perform the static | |
3797 | -- evaluation of the expression in some cases (for example, for a | |
3798 | -- variable of a subtype of Integer we statically know that any value | |
3799 | -- stored in such variable is smaller than Integer'Last). | |
5df1266a | 3800 | |
cc7c52c1 PT |
3801 | Test_Expression_Is_Foldable |
3802 | (N, Left, Right, Is_Static_Expression, Fold); | |
3803 | ||
3804 | -- Comparisons of scalars can give static results. | |
81e68a19 | 3805 | -- In addition starting with Ada 2022 (AI12-0201), comparison of strings |
cc7c52c1 PT |
3806 | -- can also give static results, and as noted above, we also allow for |
3807 | -- earlier Ada versions internally generated equality and inequality for | |
3808 | -- strings. | |
81e68a19 AC |
3809 | -- The Comes_From_Source test below isn't correct and will accept |
3810 | -- some cases that are illegal in Ada 2012 and before. Now that Ada | |
3811 | -- 2022 has relaxed the rules, this doesn't really matter. | |
cc7c52c1 PT |
3812 | |
3813 | if Is_String_Type (Left_Typ) then | |
81e68a19 | 3814 | if Ada_Version < Ada_2022 |
cc7c52c1 PT |
3815 | and then (Comes_From_Source (N) |
3816 | or else Nkind (N) not in N_Op_Eq | N_Op_Ne) | |
3817 | then | |
5df1266a AC |
3818 | Is_Static_Expression := False; |
3819 | Set_Is_Static_Expression (N, False); | |
3820 | end if; | |
d7567964 | 3821 | |
cc7c52c1 PT |
3822 | elsif not Is_Scalar_Type (Left_Typ) then |
3823 | Is_Static_Expression := False; | |
3824 | Set_Is_Static_Expression (N, False); | |
3825 | end if; | |
d7567964 | 3826 | |
cc7c52c1 PT |
3827 | -- For operators on universal numeric types called as functions with an |
3828 | -- explicit scope, determine appropriate specific numeric type, and | |
3829 | -- diagnose possible ambiguity. | |
996ae0b0 | 3830 | |
cc7c52c1 PT |
3831 | if Is_Universal_Numeric_Type (Left_Typ) |
3832 | and then | |
3833 | Is_Universal_Numeric_Type (Right_Typ) | |
3834 | then | |
3835 | Op_Typ := Find_Universal_Operator_Type (N); | |
3836 | end if; | |
996ae0b0 | 3837 | |
cc7c52c1 PT |
3838 | -- Attempt to fold the relational operator |
3839 | ||
3840 | if Is_Static_Expression and then Is_Real_Type (Left_Typ) then | |
3841 | Fold_Static_Real_Op; | |
3842 | else | |
3843 | Fold_General_Op (Is_Static_Expression); | |
634a926b | 3844 | end if; |
996ae0b0 | 3845 | |
d7567964 | 3846 | -- For the case of a folded relational operator on a specific numeric |
634a926b | 3847 | -- type, freeze the operand type now. |
d7567964 | 3848 | |
634a926b AC |
3849 | if Present (Op_Typ) then |
3850 | Freeze_Before (N, Op_Typ); | |
d7567964 TQ |
3851 | end if; |
3852 | ||
996ae0b0 RK |
3853 | Warn_On_Known_Condition (N); |
3854 | end Eval_Relational_Op; | |
3855 | ||
84be0369 AC |
3856 | ----------------------------- |
3857 | -- Eval_Selected_Component -- | |
3858 | ----------------------------- | |
3859 | ||
3860 | procedure Eval_Selected_Component (N : Node_Id) is | |
df5f901c AC |
3861 | Node : Node_Id; |
3862 | Comp : Node_Id; | |
3863 | C : Node_Id; | |
3864 | Nam : Name_Id; | |
3865 | ||
84be0369 AC |
3866 | begin |
3867 | -- If an attribute reference or a LHS, nothing to do. | |
3868 | -- Also do not fold if N is an [in] out subprogram parameter. | |
3869 | -- Fold will perform the other relevant tests. | |
3870 | ||
3871 | if Nkind (Parent (N)) /= N_Attribute_Reference | |
3872 | and then Is_LHS (N) = No | |
3873 | and then not Is_Actual_Out_Or_In_Out_Parameter (N) | |
3874 | then | |
df5f901c AC |
3875 | -- Simplify a selected_component on an aggregate by extracting |
3876 | -- the field directly. | |
3877 | ||
43d51382 | 3878 | Node := Unqualify (Prefix (N)); |
df5f901c | 3879 | |
5485d0e5 AC |
3880 | if Nkind (Node) = N_Aggregate |
3881 | and then Compile_Time_Known_Aggregate (Node) | |
3882 | then | |
df5f901c AC |
3883 | Comp := First (Component_Associations (Node)); |
3884 | Nam := Chars (Selector_Name (N)); | |
3885 | ||
3886 | while Present (Comp) loop | |
3887 | C := First (Choices (Comp)); | |
3888 | ||
3889 | while Present (C) loop | |
3890 | if Chars (C) = Nam then | |
3891 | Rewrite (N, Relocate_Node (Expression (Comp))); | |
3892 | return; | |
3893 | end if; | |
3894 | ||
3895 | Next (C); | |
3896 | end loop; | |
3897 | ||
3898 | Next (Comp); | |
3899 | end loop; | |
3900 | else | |
3901 | Fold (N); | |
3902 | end if; | |
84be0369 AC |
3903 | end if; |
3904 | end Eval_Selected_Component; | |
3905 | ||
996ae0b0 RK |
3906 | ---------------- |
3907 | -- Eval_Shift -- | |
3908 | ---------------- | |
3909 | ||
996ae0b0 RK |
3910 | procedure Eval_Shift (N : Node_Id) is |
3911 | begin | |
8cd5951d AC |
3912 | -- This procedure is only called for compiler generated code (e.g. |
3913 | -- packed arrays), so there is nothing to do except attempting to fold | |
3914 | -- the expression. | |
3915 | ||
3916 | Fold_Shift (N, Left_Opnd (N), Right_Opnd (N), Nkind (N)); | |
996ae0b0 RK |
3917 | end Eval_Shift; |
3918 | ||
3919 | ------------------------ | |
3920 | -- Eval_Short_Circuit -- | |
3921 | ------------------------ | |
3922 | ||
22cb89b5 AC |
3923 | -- A short circuit operation is potentially static if both operands are |
3924 | -- potentially static (RM 4.9 (13)). | |
996ae0b0 RK |
3925 | |
3926 | procedure Eval_Short_Circuit (N : Node_Id) is | |
3927 | Kind : constant Node_Kind := Nkind (N); | |
3928 | Left : constant Node_Id := Left_Opnd (N); | |
3929 | Right : constant Node_Id := Right_Opnd (N); | |
3930 | Left_Int : Uint; | |
4d777a71 AC |
3931 | |
3932 | Rstat : constant Boolean := | |
3933 | Is_Static_Expression (Left) | |
3934 | and then | |
3935 | Is_Static_Expression (Right); | |
996ae0b0 RK |
3936 | |
3937 | begin | |
3938 | -- Short circuit operations are never static in Ada 83 | |
3939 | ||
22cb89b5 | 3940 | if Ada_Version = Ada_83 and then Comes_From_Source (N) then |
996ae0b0 RK |
3941 | Check_Non_Static_Context (Left); |
3942 | Check_Non_Static_Context (Right); | |
3943 | return; | |
3944 | end if; | |
3945 | ||
3946 | -- Now look at the operands, we can't quite use the normal call to | |
3947 | -- Test_Expression_Is_Foldable here because short circuit operations | |
3948 | -- are a special case, they can still be foldable, even if the right | |
1e3c434f | 3949 | -- operand raises Constraint_Error. |
996ae0b0 | 3950 | |
22cb89b5 AC |
3951 | -- If either operand is Any_Type, just propagate to result and do not |
3952 | -- try to fold, this prevents cascaded errors. | |
996ae0b0 RK |
3953 | |
3954 | if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then | |
3955 | Set_Etype (N, Any_Type); | |
3956 | return; | |
3957 | ||
1e3c434f BD |
3958 | -- If left operand raises Constraint_Error, then replace node N with |
3959 | -- the raise Constraint_Error node, and we are obviously not foldable. | |
996ae0b0 RK |
3960 | -- Is_Static_Expression is set from the two operands in the normal way, |
3961 | -- and we check the right operand if it is in a non-static context. | |
3962 | ||
3963 | elsif Raises_Constraint_Error (Left) then | |
3964 | if not Rstat then | |
3965 | Check_Non_Static_Context (Right); | |
3966 | end if; | |
3967 | ||
3968 | Rewrite_In_Raise_CE (N, Left); | |
3969 | Set_Is_Static_Expression (N, Rstat); | |
3970 | return; | |
3971 | ||
3972 | -- If the result is not static, then we won't in any case fold | |
3973 | ||
3974 | elsif not Rstat then | |
3975 | Check_Non_Static_Context (Left); | |
3976 | Check_Non_Static_Context (Right); | |
3977 | return; | |
3978 | end if; | |
3979 | ||
3980 | -- Here the result is static, note that, unlike the normal processing | |
3981 | -- in Test_Expression_Is_Foldable, we did *not* check above to see if | |
1e3c434f | 3982 | -- the right operand raises Constraint_Error, that's because it is not |
996ae0b0 RK |
3983 | -- significant if the left operand is decisive. |
3984 | ||
3985 | Set_Is_Static_Expression (N); | |
3986 | ||
1e3c434f | 3987 | -- It does not matter if the right operand raises Constraint_Error if |
996ae0b0 RK |
3988 | -- it will not be evaluated. So deal specially with the cases where |
3989 | -- the right operand is not evaluated. Note that we will fold these | |
3990 | -- cases even if the right operand is non-static, which is fine, but | |
3991 | -- of course in these cases the result is not potentially static. | |
3992 | ||
3993 | Left_Int := Expr_Value (Left); | |
3994 | ||
3995 | if (Kind = N_And_Then and then Is_False (Left_Int)) | |
db318f46 | 3996 | or else |
4d777a71 | 3997 | (Kind = N_Or_Else and then Is_True (Left_Int)) |
996ae0b0 | 3998 | then |
fbf5a39b | 3999 | Fold_Uint (N, Left_Int, Rstat); |
996ae0b0 RK |
4000 | return; |
4001 | end if; | |
4002 | ||
4003 | -- If first operand not decisive, then it does matter if the right | |
1e3c434f | 4004 | -- operand raises Constraint_Error, since it will be evaluated, so |
996ae0b0 RK |
4005 | -- we simply replace the node with the right operand. Note that this |
4006 | -- properly propagates Is_Static_Expression and Raises_Constraint_Error | |
4007 | -- (both are set to True in Right). | |
4008 | ||
4009 | if Raises_Constraint_Error (Right) then | |
4010 | Rewrite_In_Raise_CE (N, Right); | |
4011 | Check_Non_Static_Context (Left); | |
4012 | return; | |
4013 | end if; | |
4014 | ||
4015 | -- Otherwise the result depends on the right operand | |
4016 | ||
fbf5a39b | 4017 | Fold_Uint (N, Expr_Value (Right), Rstat); |
996ae0b0 | 4018 | return; |
996ae0b0 RK |
4019 | end Eval_Short_Circuit; |
4020 | ||
4021 | ---------------- | |
4022 | -- Eval_Slice -- | |
4023 | ---------------- | |
4024 | ||
22cb89b5 AC |
4025 | -- Slices can never be static, so the only processing required is to check |
4026 | -- for non-static context if an explicit range is given. | |
996ae0b0 RK |
4027 | |
4028 | procedure Eval_Slice (N : Node_Id) is | |
4029 | Drange : constant Node_Id := Discrete_Range (N); | |
bc0c82e9 | 4030 | Name : constant Node_Id := Prefix (N); |
80298c3b | 4031 | |
996ae0b0 RK |
4032 | begin |
4033 | if Nkind (Drange) = N_Range then | |
4034 | Check_Non_Static_Context (Low_Bound (Drange)); | |
4035 | Check_Non_Static_Context (High_Bound (Drange)); | |
4036 | end if; | |
cd2fb920 | 4037 | |
22cb89b5 | 4038 | -- A slice of the form A (subtype), when the subtype is the index of |
cd2fb920 ES |
4039 | -- the type of A, is redundant, the slice can be replaced with A, and |
4040 | -- this is worth a warning. | |
4041 | ||
bc0c82e9 | 4042 | if Is_Entity_Name (Name) then |
cd2fb920 | 4043 | declare |
bc0c82e9 | 4044 | E : constant Entity_Id := Entity (Name); |
cd2fb920 | 4045 | T : constant Entity_Id := Etype (E); |
80298c3b | 4046 | |
cd2fb920 | 4047 | begin |
81c629f8 | 4048 | if Is_Object (E) |
cd2fb920 ES |
4049 | and then Is_Array_Type (T) |
4050 | and then Is_Entity_Name (Drange) | |
4051 | then | |
4052 | if Is_Entity_Name (Original_Node (First_Index (T))) | |
4053 | and then Entity (Original_Node (First_Index (T))) | |
4054 | = Entity (Drange) | |
4055 | then | |
4056 | if Warn_On_Redundant_Constructs then | |
324ac540 | 4057 | Error_Msg_N ("redundant slice denotes whole array?r?", N); |
cd2fb920 ES |
4058 | end if; |
4059 | ||
324ac540 | 4060 | -- The following might be a useful optimization??? |
cd2fb920 ES |
4061 | |
4062 | -- Rewrite (N, New_Occurrence_Of (E, Sloc (N))); | |
4063 | end if; | |
4064 | end if; | |
4065 | end; | |
4066 | end if; | |
996ae0b0 RK |
4067 | end Eval_Slice; |
4068 | ||
4069 | ------------------------- | |
4070 | -- Eval_String_Literal -- | |
4071 | ------------------------- | |
4072 | ||
4073 | procedure Eval_String_Literal (N : Node_Id) is | |
91b1417d AC |
4074 | Typ : constant Entity_Id := Etype (N); |
4075 | Bas : constant Entity_Id := Base_Type (Typ); | |
4076 | Xtp : Entity_Id; | |
4077 | Len : Nat; | |
4078 | Lo : Node_Id; | |
996ae0b0 RK |
4079 | |
4080 | begin | |
4081 | -- Nothing to do if error type (handles cases like default expressions | |
22cb89b5 | 4082 | -- or generics where we have not yet fully resolved the type). |
996ae0b0 | 4083 | |
91b1417d | 4084 | if Bas = Any_Type or else Bas = Any_String then |
996ae0b0 | 4085 | return; |
91b1417d | 4086 | end if; |
996ae0b0 RK |
4087 | |
4088 | -- String literals are static if the subtype is static (RM 4.9(2)), so | |
4089 | -- reset the static expression flag (it was set unconditionally in | |
4090 | -- Analyze_String_Literal) if the subtype is non-static. We tell if | |
4091 | -- the subtype is static by looking at the lower bound. | |
4092 | ||
91b1417d AC |
4093 | if Ekind (Typ) = E_String_Literal_Subtype then |
4094 | if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then | |
4095 | Set_Is_Static_Expression (N, False); | |
4096 | return; | |
4097 | end if; | |
4098 | ||
4099 | -- Here if Etype of string literal is normal Etype (not yet possible, | |
22cb89b5 | 4100 | -- but may be possible in future). |
91b1417d AC |
4101 | |
4102 | elsif not Is_OK_Static_Expression | |
80298c3b | 4103 | (Type_Low_Bound (Etype (First_Index (Typ)))) |
91b1417d | 4104 | then |
996ae0b0 | 4105 | Set_Is_Static_Expression (N, False); |
91b1417d AC |
4106 | return; |
4107 | end if; | |
996ae0b0 | 4108 | |
91b1417d | 4109 | -- If original node was a type conversion, then result if non-static |
81e68a19 | 4110 | -- up to Ada 2012. AI12-0201 changes that with Ada 2022. |
91b1417d | 4111 | |
3795dac6 AC |
4112 | if Nkind (Original_Node (N)) = N_Type_Conversion |
4113 | and then Ada_Version <= Ada_2012 | |
4114 | then | |
996ae0b0 | 4115 | Set_Is_Static_Expression (N, False); |
91b1417d AC |
4116 | return; |
4117 | end if; | |
996ae0b0 | 4118 | |
22cb89b5 AC |
4119 | -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95 |
4120 | -- if its bounds are outside the index base type and this index type is | |
4121 | -- static. This can happen in only two ways. Either the string literal | |
bc3c2eca AC |
4122 | -- is too long, or it is null, and the lower bound is type'First. Either |
4123 | -- way it is the upper bound that is out of range of the index type. | |
4124 | ||
0ab80019 | 4125 | if Ada_Version >= Ada_95 then |
bc3c2eca | 4126 | if Is_Standard_String_Type (Bas) then |
91b1417d | 4127 | Xtp := Standard_Positive; |
996ae0b0 | 4128 | else |
91b1417d | 4129 | Xtp := Etype (First_Index (Bas)); |
996ae0b0 RK |
4130 | end if; |
4131 | ||
91b1417d AC |
4132 | if Ekind (Typ) = E_String_Literal_Subtype then |
4133 | Lo := String_Literal_Low_Bound (Typ); | |
4134 | else | |
4135 | Lo := Type_Low_Bound (Etype (First_Index (Typ))); | |
4136 | end if; | |
4137 | ||
354c3840 AC |
4138 | -- Check for string too long |
4139 | ||
91b1417d AC |
4140 | Len := String_Length (Strval (N)); |
4141 | ||
1c3e11c0 | 4142 | if Len > String_Type_Len (Bas) then |
354c3840 AC |
4143 | |
4144 | -- Issue message. Note that this message is a warning if the | |
4145 | -- string literal is not marked as static (happens in some cases | |
4146 | -- of folding strings known at compile time, but not static). | |
4147 | -- Furthermore in such cases, we reword the message, since there | |
a90bd866 | 4148 | -- is no string literal in the source program. |
354c3840 AC |
4149 | |
4150 | if Is_Static_Expression (N) then | |
4151 | Apply_Compile_Time_Constraint_Error | |
4152 | (N, "string literal too long for}", CE_Length_Check_Failed, | |
4153 | Ent => Bas, | |
4154 | Typ => First_Subtype (Bas)); | |
4155 | else | |
4156 | Apply_Compile_Time_Constraint_Error | |
4157 | (N, "string value too long for}", CE_Length_Check_Failed, | |
4158 | Ent => Bas, | |
4159 | Typ => First_Subtype (Bas), | |
4160 | Warn => True); | |
4161 | end if; | |
4162 | ||
4163 | -- Test for null string not allowed | |
996ae0b0 | 4164 | |
91b1417d AC |
4165 | elsif Len = 0 |
4166 | and then not Is_Generic_Type (Xtp) | |
4167 | and then | |
4168 | Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp))) | |
996ae0b0 | 4169 | then |
354c3840 AC |
4170 | -- Same specialization of message |
4171 | ||
4172 | if Is_Static_Expression (N) then | |
4173 | Apply_Compile_Time_Constraint_Error | |
4174 | (N, "null string literal not allowed for}", | |
4175 | CE_Length_Check_Failed, | |
4176 | Ent => Bas, | |
4177 | Typ => First_Subtype (Bas)); | |
4178 | else | |
4179 | Apply_Compile_Time_Constraint_Error | |
4180 | (N, "null string value not allowed for}", | |
4181 | CE_Length_Check_Failed, | |
4182 | Ent => Bas, | |
4183 | Typ => First_Subtype (Bas), | |
4184 | Warn => True); | |
4185 | end if; | |
996ae0b0 RK |
4186 | end if; |
4187 | end if; | |
996ae0b0 RK |
4188 | end Eval_String_Literal; |
4189 | ||
4190 | -------------------------- | |
4191 | -- Eval_Type_Conversion -- | |
4192 | -------------------------- | |
4193 | ||
4194 | -- A type conversion is potentially static if its subtype mark is for a | |
4195 | -- static scalar subtype, and its operand expression is potentially static | |
22cb89b5 | 4196 | -- (RM 4.9(10)). |
3795dac6 | 4197 | -- Also add support for static string types. |
996ae0b0 RK |
4198 | |
4199 | procedure Eval_Type_Conversion (N : Node_Id) is | |
4200 | Operand : constant Node_Id := Expression (N); | |
4201 | Source_Type : constant Entity_Id := Etype (Operand); | |
4202 | Target_Type : constant Entity_Id := Etype (N); | |
4203 | ||
996ae0b0 | 4204 | function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean; |
22cb89b5 AC |
4205 | -- Returns true if type T is an integer type, or if it is a fixed-point |
4206 | -- type to be treated as an integer (i.e. the flag Conversion_OK is set | |
4207 | -- on the conversion node). | |
996ae0b0 RK |
4208 | |
4209 | function To_Be_Treated_As_Real (T : Entity_Id) return Boolean; | |
4210 | -- Returns true if type T is a floating-point type, or if it is a | |
4211 | -- fixed-point type that is not to be treated as an integer (i.e. the | |
4212 | -- flag Conversion_OK is not set on the conversion node). | |
4213 | ||
fbf5a39b AC |
4214 | ------------------------------ |
4215 | -- To_Be_Treated_As_Integer -- | |
4216 | ------------------------------ | |
4217 | ||
996ae0b0 RK |
4218 | function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is |
4219 | begin | |
4220 | return | |
4221 | Is_Integer_Type (T) | |
4222 | or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N)); | |
4223 | end To_Be_Treated_As_Integer; | |
4224 | ||
fbf5a39b AC |
4225 | --------------------------- |
4226 | -- To_Be_Treated_As_Real -- | |
4227 | --------------------------- | |
4228 | ||
996ae0b0 RK |
4229 | function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is |
4230 | begin | |
4231 | return | |
4232 | Is_Floating_Point_Type (T) | |
4233 | or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N)); | |
4234 | end To_Be_Treated_As_Real; | |
4235 | ||
48bb06a7 AC |
4236 | -- Local variables |
4237 | ||
4238 | Fold : Boolean; | |
4239 | Stat : Boolean; | |
4240 | ||
996ae0b0 RK |
4241 | -- Start of processing for Eval_Type_Conversion |
4242 | ||
4243 | begin | |
82c80734 | 4244 | -- Cannot fold if target type is non-static or if semantic error |
996ae0b0 RK |
4245 | |
4246 | if not Is_Static_Subtype (Target_Type) then | |
4247 | Check_Non_Static_Context (Operand); | |
4248 | return; | |
996ae0b0 RK |
4249 | elsif Error_Posted (N) then |
4250 | return; | |
4251 | end if; | |
4252 | ||
4253 | -- If not foldable we are done | |
4254 | ||
4255 | Test_Expression_Is_Foldable (N, Operand, Stat, Fold); | |
4256 | ||
4257 | if not Fold then | |
4258 | return; | |
4259 | ||
1e3c434f | 4260 | -- Don't try fold if target type has Constraint_Error bounds |
996ae0b0 RK |
4261 | |
4262 | elsif not Is_OK_Static_Subtype (Target_Type) then | |
4263 | Set_Raises_Constraint_Error (N); | |
4264 | return; | |
4265 | end if; | |
4266 | ||
4267 | -- Remaining processing depends on operand types. Note that in the | |
4268 | -- following type test, fixed-point counts as real unless the flag | |
4269 | -- Conversion_OK is set, in which case it counts as integer. | |
4270 | ||
3795dac6 | 4271 | -- Fold conversion, case of string type. The result is static starting |
81e68a19 | 4272 | -- with Ada 2022 (AI12-0201). |
996ae0b0 RK |
4273 | |
4274 | if Is_String_Type (Target_Type) then | |
3795dac6 AC |
4275 | Fold_Str |
4276 | (N, | |
4277 | Strval (Get_String_Val (Operand)), | |
81e68a19 | 4278 | Static => Ada_Version >= Ada_2022); |
996ae0b0 RK |
4279 | return; |
4280 | ||
4281 | -- Fold conversion, case of integer target type | |
4282 | ||
4283 | elsif To_Be_Treated_As_Integer (Target_Type) then | |
4284 | declare | |
4285 | Result : Uint; | |
4286 | ||
4287 | begin | |
4288 | -- Integer to integer conversion | |
4289 | ||
4290 | if To_Be_Treated_As_Integer (Source_Type) then | |
4291 | Result := Expr_Value (Operand); | |
4292 | ||
4293 | -- Real to integer conversion | |
4294 | ||
8eda13a4 | 4295 | elsif To_Be_Treated_As_Real (Source_Type) then |
996ae0b0 | 4296 | Result := UR_To_Uint (Expr_Value_R (Operand)); |
8eda13a4 AC |
4297 | |
4298 | -- Enumeration to integer conversion, aka 'Enum_Rep | |
4299 | ||
4300 | else | |
4301 | Result := Expr_Rep_Value (Operand); | |
996ae0b0 RK |
4302 | end if; |
4303 | ||
4304 | -- If fixed-point type (Conversion_OK must be set), then the | |
4305 | -- result is logically an integer, but we must replace the | |
4306 | -- conversion with the corresponding real literal, since the | |
4307 | -- type from a semantic point of view is still fixed-point. | |
4308 | ||
4309 | if Is_Fixed_Point_Type (Target_Type) then | |
4310 | Fold_Ureal | |
fbf5a39b | 4311 | (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat); |
996ae0b0 RK |
4312 | |
4313 | -- Otherwise result is integer literal | |
4314 | ||
4315 | else | |
fbf5a39b | 4316 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
4317 | end if; |
4318 | end; | |
4319 | ||
4320 | -- Fold conversion, case of real target type | |
4321 | ||
4322 | elsif To_Be_Treated_As_Real (Target_Type) then | |
4323 | declare | |
4324 | Result : Ureal; | |
4325 | ||
4326 | begin | |
4327 | if To_Be_Treated_As_Real (Source_Type) then | |
4328 | Result := Expr_Value_R (Operand); | |
4329 | else | |
4330 | Result := UR_From_Uint (Expr_Value (Operand)); | |
4331 | end if; | |
4332 | ||
fbf5a39b | 4333 | Fold_Ureal (N, Result, Stat); |
996ae0b0 RK |
4334 | end; |
4335 | ||
4336 | -- Enumeration types | |
4337 | ||
4338 | else | |
fbf5a39b | 4339 | Fold_Uint (N, Expr_Value (Operand), Stat); |
996ae0b0 RK |
4340 | end if; |
4341 | ||
c800f862 | 4342 | if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then |
996ae0b0 RK |
4343 | Out_Of_Range (N); |
4344 | end if; | |
996ae0b0 RK |
4345 | end Eval_Type_Conversion; |
4346 | ||
4347 | ------------------- | |
4348 | -- Eval_Unary_Op -- | |
4349 | ------------------- | |
4350 | ||
4351 | -- Predefined unary operators are static functions (RM 4.9(20)) and thus | |
22cb89b5 | 4352 | -- are potentially static if the operand is potentially static (RM 4.9(7)). |
996ae0b0 RK |
4353 | |
4354 | procedure Eval_Unary_Op (N : Node_Id) is | |
4355 | Right : constant Node_Id := Right_Opnd (N); | |
d7567964 | 4356 | Otype : Entity_Id := Empty; |
996ae0b0 RK |
4357 | Stat : Boolean; |
4358 | Fold : Boolean; | |
4359 | ||
4360 | begin | |
4361 | -- If not foldable we are done | |
4362 | ||
4363 | Test_Expression_Is_Foldable (N, Right, Stat, Fold); | |
4364 | ||
4365 | if not Fold then | |
4366 | return; | |
4367 | end if; | |
4368 | ||
785d39ac | 4369 | if Is_Universal_Numeric_Type (Etype (Right)) then |
d7567964 | 4370 | Otype := Find_Universal_Operator_Type (N); |
602a7ec0 AC |
4371 | end if; |
4372 | ||
996ae0b0 RK |
4373 | -- Fold for integer case |
4374 | ||
4375 | if Is_Integer_Type (Etype (N)) then | |
4376 | declare | |
4377 | Rint : constant Uint := Expr_Value (Right); | |
4378 | Result : Uint; | |
4379 | ||
4380 | begin | |
4381 | -- In the case of modular unary plus and abs there is no need | |
4382 | -- to adjust the result of the operation since if the original | |
4383 | -- operand was in bounds the result will be in the bounds of the | |
4384 | -- modular type. However, in the case of modular unary minus the | |
4385 | -- result may go out of the bounds of the modular type and needs | |
4386 | -- adjustment. | |
4387 | ||
4388 | if Nkind (N) = N_Op_Plus then | |
4389 | Result := Rint; | |
4390 | ||
4391 | elsif Nkind (N) = N_Op_Minus then | |
4392 | if Is_Modular_Integer_Type (Etype (N)) then | |
4393 | Result := (-Rint) mod Modulus (Etype (N)); | |
4394 | else | |
4395 | Result := (-Rint); | |
4396 | end if; | |
4397 | ||
4398 | else | |
4399 | pragma Assert (Nkind (N) = N_Op_Abs); | |
4400 | Result := abs Rint; | |
4401 | end if; | |
4402 | ||
b626569a YM |
4403 | Check_Non_Static_Context_For_Overflow (N, Stat, Result); |
4404 | ||
fbf5a39b | 4405 | Fold_Uint (N, Result, Stat); |
996ae0b0 RK |
4406 | end; |
4407 | ||
4408 | -- Fold for real case | |
4409 | ||
4410 | elsif Is_Real_Type (Etype (N)) then | |
4411 | declare | |
4412 | Rreal : constant Ureal := Expr_Value_R (Right); | |
4413 | Result : Ureal; | |
4414 | ||
4415 | begin | |
4416 | if Nkind (N) = N_Op_Plus then | |
4417 | Result := Rreal; | |
996ae0b0 RK |
4418 | elsif Nkind (N) = N_Op_Minus then |
4419 | Result := UR_Negate (Rreal); | |
996ae0b0 RK |
4420 | else |
4421 | pragma Assert (Nkind (N) = N_Op_Abs); | |
4422 | Result := abs Rreal; | |
4423 | end if; | |
4424 | ||
fbf5a39b | 4425 | Fold_Ureal (N, Result, Stat); |
996ae0b0 RK |
4426 | end; |
4427 | end if; | |
d7567964 TQ |
4428 | |
4429 | -- If the operator was resolved to a specific type, make sure that type | |
4430 | -- is frozen even if the expression is folded into a literal (which has | |
4431 | -- a universal type). | |
4432 | ||
4433 | if Present (Otype) then | |
4434 | Freeze_Before (N, Otype); | |
4435 | end if; | |
996ae0b0 RK |
4436 | end Eval_Unary_Op; |
4437 | ||
4438 | ------------------------------- | |
4439 | -- Eval_Unchecked_Conversion -- | |
4440 | ------------------------------- | |
4441 | ||
4442 | -- Unchecked conversions can never be static, so the only required | |
4443 | -- processing is to check for a non-static context for the operand. | |
4444 | ||
4445 | procedure Eval_Unchecked_Conversion (N : Node_Id) is | |
4d3a70f2 PT |
4446 | Target_Type : constant Entity_Id := Etype (N); |
4447 | Operand : constant Node_Id := Expression (N); | |
4448 | Operand_Type : constant Entity_Id := Etype (Operand); | |
4449 | ||
996ae0b0 | 4450 | begin |
4d3a70f2 PT |
4451 | Check_Non_Static_Context (Operand); |
4452 | ||
4453 | -- If we have a conversion of a compile time known value to a target | |
4454 | -- type and the value is in range of the target type, then we can simply | |
4455 | -- replace the construct by an integer literal of the correct type. We | |
4456 | -- only apply this to discrete types being converted. Possibly it may | |
4457 | -- apply in other cases, but it is too much trouble to worry about. | |
4458 | ||
4459 | -- Note that we do not do this transformation if the Kill_Range_Check | |
4460 | -- flag is set, since then the value may be outside the expected range. | |
4461 | -- This happens in the Normalize_Scalars case. | |
4462 | ||
4463 | -- We also skip this if either the target or operand type is biased | |
4464 | -- because in this case, the unchecked conversion is supposed to | |
4465 | -- preserve the bit pattern, not the integer value. | |
4466 | ||
4467 | if Is_Integer_Type (Target_Type) | |
4468 | and then not Has_Biased_Representation (Target_Type) | |
4469 | and then Is_Discrete_Type (Operand_Type) | |
4470 | and then not Has_Biased_Representation (Operand_Type) | |
4471 | and then Compile_Time_Known_Value (Operand) | |
4472 | and then not Kill_Range_Check (N) | |
4473 | then | |
4474 | declare | |
4475 | Val : constant Uint := Expr_Rep_Value (Operand); | |
4476 | ||
4477 | begin | |
4478 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
4479 | and then | |
4480 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
4481 | and then | |
4482 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
4483 | and then | |
4484 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
4485 | then | |
4486 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
4487 | ||
4488 | -- If Address is the target type, just set the type to avoid a | |
4489 | -- spurious type error on the literal when Address is a visible | |
4490 | -- integer type. | |
4491 | ||
4492 | if Is_Descendant_Of_Address (Target_Type) then | |
4493 | Set_Etype (N, Target_Type); | |
4494 | else | |
4495 | Analyze_And_Resolve (N, Target_Type); | |
4496 | end if; | |
4497 | ||
4498 | return; | |
4499 | end if; | |
4500 | end; | |
4501 | end if; | |
996ae0b0 RK |
4502 | end Eval_Unchecked_Conversion; |
4503 | ||
4504 | -------------------- | |
4505 | -- Expr_Rep_Value -- | |
4506 | -------------------- | |
4507 | ||
4508 | function Expr_Rep_Value (N : Node_Id) return Uint is | |
07fc65c4 GB |
4509 | Kind : constant Node_Kind := Nkind (N); |
4510 | Ent : Entity_Id; | |
996ae0b0 RK |
4511 | |
4512 | begin | |
4513 | if Is_Entity_Name (N) then | |
4514 | Ent := Entity (N); | |
4515 | ||
22cb89b5 AC |
4516 | -- An enumeration literal that was either in the source or created |
4517 | -- as a result of static evaluation. | |
996ae0b0 RK |
4518 | |
4519 | if Ekind (Ent) = E_Enumeration_Literal then | |
4520 | return Enumeration_Rep (Ent); | |
4521 | ||
4522 | -- A user defined static constant | |
4523 | ||
4524 | else | |
4525 | pragma Assert (Ekind (Ent) = E_Constant); | |
4526 | return Expr_Rep_Value (Constant_Value (Ent)); | |
4527 | end if; | |
4528 | ||
22cb89b5 AC |
4529 | -- An integer literal that was either in the source or created as a |
4530 | -- result of static evaluation. | |
996ae0b0 RK |
4531 | |
4532 | elsif Kind = N_Integer_Literal then | |
4533 | return Intval (N); | |
4534 | ||
4535 | -- A real literal for a fixed-point type. This must be the fixed-point | |
4536 | -- case, either the literal is of a fixed-point type, or it is a bound | |
4537 | -- of a fixed-point type, with type universal real. In either case we | |
4538 | -- obtain the desired value from Corresponding_Integer_Value. | |
4539 | ||
4540 | elsif Kind = N_Real_Literal then | |
996ae0b0 RK |
4541 | pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N)))); |
4542 | return Corresponding_Integer_Value (N); | |
4543 | ||
c7d19317 | 4544 | -- The NULL access value |
8cbb664e | 4545 | |
c7d19317 EB |
4546 | elsif Kind = N_Null then |
4547 | pragma Assert (Is_Access_Type (Underlying_Type (Etype (N))) | |
4548 | or else Error_Posted (N)); | |
4549 | return Uint_0; | |
4550 | ||
4551 | -- Character literal | |
4552 | ||
4553 | elsif Kind = N_Character_Literal then | |
996ae0b0 RK |
4554 | Ent := Entity (N); |
4555 | ||
22cb89b5 AC |
4556 | -- Since Character literals of type Standard.Character don't have any |
4557 | -- defining character literals built for them, they do not have their | |
4558 | -- Entity set, so just use their Char code. Otherwise for user- | |
4559 | -- defined character literals use their Pos value as usual which is | |
4560 | -- the same as the Rep value. | |
996ae0b0 RK |
4561 | |
4562 | if No (Ent) then | |
82c80734 | 4563 | return Char_Literal_Value (N); |
996ae0b0 RK |
4564 | else |
4565 | return Enumeration_Rep (Ent); | |
4566 | end if; | |
c7d19317 EB |
4567 | |
4568 | -- Unchecked conversion, which can come from System'To_Address (X) | |
4569 | -- where X is a static integer expression. Recursively evaluate X. | |
4570 | ||
4571 | elsif Kind = N_Unchecked_Type_Conversion then | |
4572 | return Expr_Rep_Value (Expression (N)); | |
4573 | ||
84be0369 AC |
4574 | -- Static discriminant value |
4575 | ||
4576 | elsif Is_Static_Discriminant_Component (N) then | |
4577 | return Expr_Rep_Value | |
4578 | (Get_Discriminant_Value | |
4579 | (Entity (Selector_Name (N)), | |
4580 | Etype (Prefix (N)), | |
4581 | Discriminant_Constraint (Etype (Prefix (N))))); | |
4582 | ||
c7d19317 EB |
4583 | else |
4584 | raise Program_Error; | |
996ae0b0 RK |
4585 | end if; |
4586 | end Expr_Rep_Value; | |
4587 | ||
4588 | ---------------- | |
4589 | -- Expr_Value -- | |
4590 | ---------------- | |
4591 | ||
4592 | function Expr_Value (N : Node_Id) return Uint is | |
07fc65c4 GB |
4593 | Kind : constant Node_Kind := Nkind (N); |
4594 | CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size); | |
4595 | Ent : Entity_Id; | |
4596 | Val : Uint; | |
996ae0b0 RK |
4597 | |
4598 | begin | |
d3bbfc59 | 4599 | -- If already in cache, then we know it's compile-time-known and we can |
13f34a3f | 4600 | -- return the value that was previously stored in the cache since |
d3bbfc59 | 4601 | -- compile-time-known values cannot change. |
07fc65c4 GB |
4602 | |
4603 | if CV_Ent.N = N then | |
4604 | return CV_Ent.V; | |
4605 | end if; | |
4606 | ||
4607 | -- Otherwise proceed to test value | |
4608 | ||
996ae0b0 RK |
4609 | if Is_Entity_Name (N) then |
4610 | Ent := Entity (N); | |
4611 | ||
22cb89b5 AC |
4612 | -- An enumeration literal that was either in the source or created as |
4613 | -- a result of static evaluation. | |
996ae0b0 RK |
4614 | |
4615 | if Ekind (Ent) = E_Enumeration_Literal then | |
07fc65c4 | 4616 | Val := Enumeration_Pos (Ent); |
996ae0b0 RK |
4617 | |
4618 | -- A user defined static constant | |
4619 | ||
4620 | else | |
4621 | pragma Assert (Ekind (Ent) = E_Constant); | |
07fc65c4 | 4622 | Val := Expr_Value (Constant_Value (Ent)); |
996ae0b0 RK |
4623 | end if; |
4624 | ||
22cb89b5 AC |
4625 | -- An integer literal that was either in the source or created as a |
4626 | -- result of static evaluation. | |
996ae0b0 RK |
4627 | |
4628 | elsif Kind = N_Integer_Literal then | |
07fc65c4 | 4629 | Val := Intval (N); |
996ae0b0 RK |
4630 | |
4631 | -- A real literal for a fixed-point type. This must be the fixed-point | |
4632 | -- case, either the literal is of a fixed-point type, or it is a bound | |
4633 | -- of a fixed-point type, with type universal real. In either case we | |
4634 | -- obtain the desired value from Corresponding_Integer_Value. | |
4635 | ||
4636 | elsif Kind = N_Real_Literal then | |
996ae0b0 | 4637 | pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N)))); |
07fc65c4 | 4638 | Val := Corresponding_Integer_Value (N); |
996ae0b0 | 4639 | |
333e4f86 AC |
4640 | -- The NULL access value |
4641 | ||
4642 | elsif Kind = N_Null then | |
50a73953 SB |
4643 | pragma Assert (Is_Access_Type (Underlying_Type (Etype (N))) |
4644 | or else Error_Posted (N)); | |
333e4f86 AC |
4645 | Val := Uint_0; |
4646 | ||
f2a35a2f | 4647 | -- Character literal |
996ae0b0 | 4648 | |
f2a35a2f | 4649 | elsif Kind = N_Character_Literal then |
996ae0b0 RK |
4650 | Ent := Entity (N); |
4651 | ||
4652 | -- Since Character literals of type Standard.Character don't | |
4653 | -- have any defining character literals built for them, they | |
4654 | -- do not have their Entity set, so just use their Char | |
4655 | -- code. Otherwise for user-defined character literals use | |
4656 | -- their Pos value as usual. | |
4657 | ||
4658 | if No (Ent) then | |
82c80734 | 4659 | Val := Char_Literal_Value (N); |
996ae0b0 | 4660 | else |
07fc65c4 | 4661 | Val := Enumeration_Pos (Ent); |
996ae0b0 | 4662 | end if; |
f2a35a2f BD |
4663 | |
4664 | -- Unchecked conversion, which can come from System'To_Address (X) | |
4665 | -- where X is a static integer expression. Recursively evaluate X. | |
4666 | ||
4667 | elsif Kind = N_Unchecked_Type_Conversion then | |
4668 | Val := Expr_Value (Expression (N)); | |
4669 | ||
84be0369 AC |
4670 | -- Static discriminant value |
4671 | ||
4672 | elsif Is_Static_Discriminant_Component (N) then | |
4673 | Val := Expr_Value | |
4674 | (Get_Discriminant_Value | |
4675 | (Entity (Selector_Name (N)), | |
4676 | Etype (Prefix (N)), | |
4677 | Discriminant_Constraint (Etype (Prefix (N))))); | |
4678 | ||
f2a35a2f BD |
4679 | else |
4680 | raise Program_Error; | |
996ae0b0 RK |
4681 | end if; |
4682 | ||
07fc65c4 GB |
4683 | -- Come here with Val set to value to be returned, set cache |
4684 | ||
4685 | CV_Ent.N := N; | |
4686 | CV_Ent.V := Val; | |
4687 | return Val; | |
996ae0b0 RK |
4688 | end Expr_Value; |
4689 | ||
4690 | ------------------ | |
4691 | -- Expr_Value_E -- | |
4692 | ------------------ | |
4693 | ||
4694 | function Expr_Value_E (N : Node_Id) return Entity_Id is | |
4695 | Ent : constant Entity_Id := Entity (N); | |
996ae0b0 RK |
4696 | begin |
4697 | if Ekind (Ent) = E_Enumeration_Literal then | |
4698 | return Ent; | |
4699 | else | |
4700 | pragma Assert (Ekind (Ent) = E_Constant); | |
924e3532 JS |
4701 | |
4702 | -- We may be dealing with a enumerated character type constant, so | |
4703 | -- handle that case here. | |
4704 | ||
4705 | if Nkind (Constant_Value (Ent)) = N_Character_Literal then | |
4706 | return Ent; | |
4707 | else | |
4708 | return Expr_Value_E (Constant_Value (Ent)); | |
4709 | end if; | |
996ae0b0 RK |
4710 | end if; |
4711 | end Expr_Value_E; | |
4712 | ||
4713 | ------------------ | |
4714 | -- Expr_Value_R -- | |
4715 | ------------------ | |
4716 | ||
4717 | function Expr_Value_R (N : Node_Id) return Ureal is | |
4718 | Kind : constant Node_Kind := Nkind (N); | |
4719 | Ent : Entity_Id; | |
996ae0b0 RK |
4720 | |
4721 | begin | |
4722 | if Kind = N_Real_Literal then | |
4723 | return Realval (N); | |
4724 | ||
4725 | elsif Kind = N_Identifier or else Kind = N_Expanded_Name then | |
4726 | Ent := Entity (N); | |
4727 | pragma Assert (Ekind (Ent) = E_Constant); | |
4728 | return Expr_Value_R (Constant_Value (Ent)); | |
4729 | ||
4730 | elsif Kind = N_Integer_Literal then | |
4731 | return UR_From_Uint (Expr_Value (N)); | |
4732 | ||
7a5b62b0 AC |
4733 | -- Here, we have a node that cannot be interpreted as a compile time |
4734 | -- constant. That is definitely an error. | |
996ae0b0 | 4735 | |
7a5b62b0 AC |
4736 | else |
4737 | raise Program_Error; | |
996ae0b0 | 4738 | end if; |
996ae0b0 RK |
4739 | end Expr_Value_R; |
4740 | ||
4741 | ------------------ | |
4742 | -- Expr_Value_S -- | |
4743 | ------------------ | |
4744 | ||
4745 | function Expr_Value_S (N : Node_Id) return Node_Id is | |
4746 | begin | |
4747 | if Nkind (N) = N_String_Literal then | |
4748 | return N; | |
4749 | else | |
4750 | pragma Assert (Ekind (Entity (N)) = E_Constant); | |
4751 | return Expr_Value_S (Constant_Value (Entity (N))); | |
4752 | end if; | |
4753 | end Expr_Value_S; | |
4754 | ||
74e7891f RD |
4755 | ---------------------------------- |
4756 | -- Find_Universal_Operator_Type -- | |
4757 | ---------------------------------- | |
4758 | ||
4759 | function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is | |
4760 | PN : constant Node_Id := Parent (N); | |
4761 | Call : constant Node_Id := Original_Node (N); | |
4762 | Is_Int : constant Boolean := Is_Integer_Type (Etype (N)); | |
4763 | ||
4764 | Is_Fix : constant Boolean := | |
4765 | Nkind (N) in N_Binary_Op | |
4766 | and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N)); | |
4767 | -- A mixed-mode operation in this context indicates the presence of | |
4768 | -- fixed-point type in the designated package. | |
4769 | ||
4770 | Is_Relational : constant Boolean := Etype (N) = Standard_Boolean; | |
4771 | -- Case where N is a relational (or membership) operator (else it is an | |
4772 | -- arithmetic one). | |
4773 | ||
4774 | In_Membership : constant Boolean := | |
4775 | Nkind (PN) in N_Membership_Test | |
4776 | and then | |
4777 | Nkind (Right_Opnd (PN)) = N_Range | |
4778 | and then | |
4779 | Is_Universal_Numeric_Type (Etype (Left_Opnd (PN))) | |
4780 | and then | |
4781 | Is_Universal_Numeric_Type | |
4782 | (Etype (Low_Bound (Right_Opnd (PN)))) | |
4783 | and then | |
4784 | Is_Universal_Numeric_Type | |
4785 | (Etype (High_Bound (Right_Opnd (PN)))); | |
4786 | -- Case where N is part of a membership test with a universal range | |
4787 | ||
4788 | E : Entity_Id; | |
4789 | Pack : Entity_Id; | |
4790 | Typ1 : Entity_Id := Empty; | |
4791 | Priv_E : Entity_Id; | |
4792 | ||
4793 | function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean; | |
7ec8363d RD |
4794 | -- Check whether one operand is a mixed-mode operation that requires the |
4795 | -- presence of a fixed-point type. Given that all operands are universal | |
4796 | -- and have been constant-folded, retrieve the original function call. | |
74e7891f RD |
4797 | |
4798 | --------------------------- | |
4799 | -- Is_Mixed_Mode_Operand -- | |
4800 | --------------------------- | |
4801 | ||
4802 | function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is | |
7ec8363d | 4803 | Onod : constant Node_Id := Original_Node (Op); |
74e7891f | 4804 | begin |
7ec8363d RD |
4805 | return Nkind (Onod) = N_Function_Call |
4806 | and then Present (Next_Actual (First_Actual (Onod))) | |
4807 | and then Etype (First_Actual (Onod)) /= | |
4808 | Etype (Next_Actual (First_Actual (Onod))); | |
74e7891f RD |
4809 | end Is_Mixed_Mode_Operand; |
4810 | ||
7ec8363d RD |
4811 | -- Start of processing for Find_Universal_Operator_Type |
4812 | ||
74e7891f RD |
4813 | begin |
4814 | if Nkind (Call) /= N_Function_Call | |
4815 | or else Nkind (Name (Call)) /= N_Expanded_Name | |
4816 | then | |
4817 | return Empty; | |
4818 | ||
946db1e2 AC |
4819 | -- There are several cases where the context does not imply the type of |
4820 | -- the operands: | |
4821 | -- - the universal expression appears in a type conversion; | |
4822 | -- - the expression is a relational operator applied to universal | |
4823 | -- operands; | |
4824 | -- - the expression is a membership test with a universal operand | |
4825 | -- and a range with universal bounds. | |
74e7891f RD |
4826 | |
4827 | elsif Nkind (Parent (N)) = N_Type_Conversion | |
7ec8363d RD |
4828 | or else Is_Relational |
4829 | or else In_Membership | |
74e7891f RD |
4830 | then |
4831 | Pack := Entity (Prefix (Name (Call))); | |
4832 | ||
7ec8363d RD |
4833 | -- If the prefix is a package declared elsewhere, iterate over its |
4834 | -- visible entities, otherwise iterate over all declarations in the | |
4835 | -- designated scope. | |
74e7891f RD |
4836 | |
4837 | if Ekind (Pack) = E_Package | |
4838 | and then not In_Open_Scopes (Pack) | |
4839 | then | |
4840 | Priv_E := First_Private_Entity (Pack); | |
4841 | else | |
4842 | Priv_E := Empty; | |
4843 | end if; | |
4844 | ||
4845 | Typ1 := Empty; | |
4846 | E := First_Entity (Pack); | |
4847 | while Present (E) and then E /= Priv_E loop | |
4848 | if Is_Numeric_Type (E) | |
4849 | and then Nkind (Parent (E)) /= N_Subtype_Declaration | |
4850 | and then Comes_From_Source (E) | |
4851 | and then Is_Integer_Type (E) = Is_Int | |
80298c3b AC |
4852 | and then (Nkind (N) in N_Unary_Op |
4853 | or else Is_Relational | |
4854 | or else Is_Fixed_Point_Type (E) = Is_Fix) | |
74e7891f RD |
4855 | then |
4856 | if No (Typ1) then | |
4857 | Typ1 := E; | |
4858 | ||
676e8420 AC |
4859 | -- Before emitting an error, check for the presence of a |
4860 | -- mixed-mode operation that specifies a fixed point type. | |
74e7891f RD |
4861 | |
4862 | elsif Is_Relational | |
4863 | and then | |
4864 | (Is_Mixed_Mode_Operand (Left_Opnd (N)) | |
676e8420 | 4865 | or else Is_Mixed_Mode_Operand (Right_Opnd (N))) |
74e7891f RD |
4866 | and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1) |
4867 | ||
4868 | then | |
4869 | if Is_Fixed_Point_Type (E) then | |
4870 | Typ1 := E; | |
4871 | end if; | |
4872 | ||
4873 | else | |
4874 | -- More than one type of the proper class declared in P | |
4875 | ||
4876 | Error_Msg_N ("ambiguous operation", N); | |
4877 | Error_Msg_Sloc := Sloc (Typ1); | |
4878 | Error_Msg_N ("\possible interpretation (inherited)#", N); | |
4879 | Error_Msg_Sloc := Sloc (E); | |
4880 | Error_Msg_N ("\possible interpretation (inherited)#", N); | |
4881 | return Empty; | |
4882 | end if; | |
4883 | end if; | |
4884 | ||
4885 | Next_Entity (E); | |
4886 | end loop; | |
4887 | end if; | |
4888 | ||
4889 | return Typ1; | |
4890 | end Find_Universal_Operator_Type; | |
4891 | ||
fbf5a39b AC |
4892 | -------------------------- |
4893 | -- Flag_Non_Static_Expr -- | |
4894 | -------------------------- | |
4895 | ||
4896 | procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is | |
4897 | begin | |
4898 | if Error_Posted (Expr) and then not All_Errors_Mode then | |
4899 | return; | |
4900 | else | |
4901 | Error_Msg_F (Msg, Expr); | |
4902 | Why_Not_Static (Expr); | |
4903 | end if; | |
4904 | end Flag_Non_Static_Expr; | |
4905 | ||
84be0369 AC |
4906 | ---------- |
4907 | -- Fold -- | |
4908 | ---------- | |
4909 | ||
4910 | procedure Fold (N : Node_Id) is | |
4911 | Typ : constant Entity_Id := Etype (N); | |
4912 | begin | |
4913 | -- If not known at compile time or if already a literal, nothing to do | |
4914 | ||
4915 | if Nkind (N) in N_Numeric_Or_String_Literal | |
4916 | or else not Compile_Time_Known_Value (N) | |
4917 | then | |
4918 | null; | |
4919 | ||
4920 | elsif Is_Discrete_Type (Typ) then | |
4921 | Fold_Uint (N, Expr_Value (N), Static => Is_Static_Expression (N)); | |
4922 | ||
4923 | elsif Is_Real_Type (Typ) then | |
4924 | Fold_Ureal (N, Expr_Value_R (N), Static => Is_Static_Expression (N)); | |
4925 | ||
4926 | elsif Is_String_Type (Typ) then | |
4927 | Fold_Str | |
4928 | (N, Strval (Expr_Value_S (N)), Static => Is_Static_Expression (N)); | |
4929 | end if; | |
4930 | end Fold; | |
4931 | ||
8cd5951d AC |
4932 | ---------------- |
4933 | -- Fold_Dummy -- | |
4934 | ---------------- | |
4935 | ||
4936 | procedure Fold_Dummy (N : Node_Id; Typ : Entity_Id) is | |
4937 | begin | |
4938 | if Is_Integer_Type (Typ) then | |
4939 | Fold_Uint (N, Uint_1, Static => True); | |
4940 | ||
4941 | elsif Is_Real_Type (Typ) then | |
4942 | Fold_Ureal (N, Ureal_1, Static => True); | |
4943 | ||
4944 | elsif Is_Enumeration_Type (Typ) then | |
4945 | Fold_Uint | |
4946 | (N, | |
4947 | Expr_Value (Type_Low_Bound (Base_Type (Typ))), | |
4948 | Static => True); | |
4949 | ||
4950 | elsif Is_String_Type (Typ) then | |
4951 | Fold_Str | |
4952 | (N, | |
4953 | Strval (Make_String_Literal (Sloc (N), "")), | |
4954 | Static => True); | |
4955 | end if; | |
4956 | end Fold_Dummy; | |
4957 | ||
4958 | ---------------- | |
4959 | -- Fold_Shift -- | |
4960 | ---------------- | |
4961 | ||
4962 | procedure Fold_Shift | |
4963 | (N : Node_Id; | |
4964 | Left : Node_Id; | |
4965 | Right : Node_Id; | |
4966 | Op : Node_Kind; | |
4967 | Static : Boolean := False; | |
4968 | Check_Elab : Boolean := False) | |
4969 | is | |
415791cf | 4970 | Typ : constant Entity_Id := Base_Type (Etype (Left)); |
8cd5951d AC |
4971 | |
4972 | procedure Check_Elab_Call; | |
4973 | -- Add checks related to calls in elaboration code | |
4974 | ||
4975 | --------------------- | |
4976 | -- Check_Elab_Call -- | |
4977 | --------------------- | |
4978 | ||
4979 | procedure Check_Elab_Call is | |
4980 | begin | |
4981 | if Check_Elab then | |
4982 | if Legacy_Elaboration_Checks then | |
4983 | Check_Elab_Call (N); | |
4984 | end if; | |
4985 | ||
4986 | Build_Call_Marker (N); | |
4987 | end if; | |
4988 | end Check_Elab_Call; | |
4989 | ||
fde5868f | 4990 | Modulus, Val : Uint; |
4a3b4c2a | 4991 | |
8cd5951d | 4992 | begin |
8ad6af8f | 4993 | if Compile_Time_Known_Value (Left) |
8cd5951d AC |
4994 | and then Compile_Time_Known_Value (Right) |
4995 | then | |
8ad6af8f AC |
4996 | pragma Assert (not Non_Binary_Modulus (Typ)); |
4997 | ||
8cd5951d AC |
4998 | if Op = N_Op_Shift_Left then |
4999 | Check_Elab_Call; | |
5000 | ||
fde5868f | 5001 | if Is_Modular_Integer_Type (Typ) then |
76f9c7f4 | 5002 | Modulus := Einfo.Entities.Modulus (Typ); |
fde5868f AC |
5003 | else |
5004 | Modulus := Uint_2 ** RM_Size (Typ); | |
5005 | end if; | |
8cd5951d | 5006 | |
fde5868f AC |
5007 | -- Fold Shift_Left (X, Y) by computing |
5008 | -- (X * 2**Y) rem modulus [- Modulus] | |
e480bca2 | 5009 | |
fde5868f AC |
5010 | Val := (Expr_Value (Left) * (Uint_2 ** Expr_Value (Right))) |
5011 | rem Modulus; | |
5012 | ||
5013 | if Is_Modular_Integer_Type (Typ) | |
5014 | or else Val < Modulus / Uint_2 | |
5015 | then | |
5016 | Fold_Uint (N, Val, Static => Static); | |
5017 | else | |
5018 | Fold_Uint (N, Val - Modulus, Static => Static); | |
5019 | end if; | |
8cd5951d AC |
5020 | |
5021 | elsif Op = N_Op_Shift_Right then | |
5022 | Check_Elab_Call; | |
5023 | ||
4a3b4c2a | 5024 | -- X >> 0 is a no-op |
8cd5951d | 5025 | |
4a3b4c2a AC |
5026 | if Expr_Value (Right) = Uint_0 then |
5027 | Fold_Uint (N, Expr_Value (Left), Static => Static); | |
5028 | else | |
5029 | if Is_Modular_Integer_Type (Typ) then | |
76f9c7f4 | 5030 | Modulus := Einfo.Entities.Modulus (Typ); |
4a3b4c2a AC |
5031 | else |
5032 | Modulus := Uint_2 ** RM_Size (Typ); | |
5033 | end if; | |
5034 | ||
5035 | -- Fold X >> Y by computing (X [+ Modulus]) / 2**Y | |
5036 | -- Note that after a Shift_Right operation (with Y > 0), the | |
5037 | -- result is always positive, even if the original operand was | |
5038 | -- negative. | |
8ad6af8f | 5039 | |
4a3b4c2a AC |
5040 | Fold_Uint |
5041 | (N, | |
5042 | (Expr_Value (Left) + | |
5043 | (if Expr_Value (Left) >= Uint_0 then Uint_0 else Modulus)) | |
5044 | / (Uint_2 ** Expr_Value (Right)), | |
5045 | Static => Static); | |
5046 | end if; | |
8ad6af8f AC |
5047 | elsif Op = N_Op_Shift_Right_Arithmetic then |
5048 | Check_Elab_Call; | |
5049 | ||
5050 | declare | |
fde5868f | 5051 | Two_Y : constant Uint := Uint_2 ** Expr_Value (Right); |
8ad6af8f AC |
5052 | begin |
5053 | if Is_Modular_Integer_Type (Typ) then | |
76f9c7f4 | 5054 | Modulus := Einfo.Entities.Modulus (Typ); |
8ad6af8f AC |
5055 | else |
5056 | Modulus := Uint_2 ** RM_Size (Typ); | |
5057 | end if; | |
5058 | ||
5059 | -- X / 2**Y if X if positive or a small enough modular integer | |
5060 | ||
5061 | if (Is_Modular_Integer_Type (Typ) | |
5062 | and then Expr_Value (Left) < Modulus / Uint_2) | |
5063 | or else | |
5064 | (not Is_Modular_Integer_Type (Typ) | |
5065 | and then Expr_Value (Left) >= 0) | |
5066 | then | |
5067 | Fold_Uint (N, Expr_Value (Left) / Two_Y, Static => Static); | |
5068 | ||
5069 | -- -1 (aka all 1's) if Y is larger than the number of bits | |
5070 | -- available or if X = -1. | |
5071 | ||
5072 | elsif Two_Y > Modulus | |
5073 | or else Expr_Value (Left) = Uint_Minus_1 | |
5074 | then | |
5075 | if Is_Modular_Integer_Type (Typ) then | |
5076 | Fold_Uint (N, Modulus - Uint_1, Static => Static); | |
5077 | else | |
5078 | Fold_Uint (N, Uint_Minus_1, Static => Static); | |
5079 | end if; | |
5080 | ||
5081 | -- Large modular integer, compute via multiply/divide the | |
5082 | -- following: X >> Y + (1 << Y - 1) << (RM_Size - Y) | |
5083 | ||
5084 | elsif Is_Modular_Integer_Type (Typ) then | |
5085 | Fold_Uint | |
5086 | (N, | |
5087 | (Expr_Value (Left)) / Two_Y | |
5088 | + (Two_Y - Uint_1) | |
5089 | * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right)), | |
5090 | Static => Static); | |
5091 | ||
5092 | -- Negative signed integer, compute via multiple/divide the | |
5093 | -- following: | |
5094 | -- (Modulus + X) >> Y + (1 << Y - 1) << (RM_Size - Y) - Modulus | |
5095 | ||
5096 | else | |
5097 | Fold_Uint | |
5098 | (N, | |
5099 | (Modulus + Expr_Value (Left)) / Two_Y | |
5100 | + (Two_Y - Uint_1) | |
5101 | * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right)) | |
5102 | - Modulus, | |
5103 | Static => Static); | |
5104 | end if; | |
5105 | end; | |
8cd5951d AC |
5106 | end if; |
5107 | end if; | |
5108 | end Fold_Shift; | |
5109 | ||
996ae0b0 RK |
5110 | -------------- |
5111 | -- Fold_Str -- | |
5112 | -------------- | |
5113 | ||
fbf5a39b | 5114 | procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is |
996ae0b0 RK |
5115 | Loc : constant Source_Ptr := Sloc (N); |
5116 | Typ : constant Entity_Id := Etype (N); | |
5117 | ||
5118 | begin | |
edab6088 RD |
5119 | if Raises_Constraint_Error (N) then |
5120 | Set_Is_Static_Expression (N, Static); | |
5121 | return; | |
5122 | end if; | |
5123 | ||
996ae0b0 | 5124 | Rewrite (N, Make_String_Literal (Loc, Strval => Val)); |
fbf5a39b AC |
5125 | |
5126 | -- We now have the literal with the right value, both the actual type | |
5127 | -- and the expected type of this literal are taken from the expression | |
9479ded4 AC |
5128 | -- that was evaluated. So now we do the Analyze and Resolve. |
5129 | ||
5130 | -- Note that we have to reset Is_Static_Expression both after the | |
5131 | -- analyze step (because Resolve will evaluate the literal, which | |
5132 | -- will cause semantic errors if it is marked as static), and after | |
354c3840 | 5133 | -- the Resolve step (since Resolve in some cases resets this flag). |
fbf5a39b AC |
5134 | |
5135 | Analyze (N); | |
5136 | Set_Is_Static_Expression (N, Static); | |
5137 | Set_Etype (N, Typ); | |
5138 | Resolve (N); | |
9479ded4 | 5139 | Set_Is_Static_Expression (N, Static); |
996ae0b0 RK |
5140 | end Fold_Str; |
5141 | ||
5142 | --------------- | |
5143 | -- Fold_Uint -- | |
5144 | --------------- | |
5145 | ||
fbf5a39b | 5146 | procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is |
996ae0b0 | 5147 | Loc : constant Source_Ptr := Sloc (N); |
fbf5a39b AC |
5148 | Typ : Entity_Id := Etype (N); |
5149 | Ent : Entity_Id; | |
996ae0b0 RK |
5150 | |
5151 | begin | |
edab6088 RD |
5152 | if Raises_Constraint_Error (N) then |
5153 | Set_Is_Static_Expression (N, Static); | |
5154 | return; | |
5155 | end if; | |
5156 | ||
3aeb5ebe AC |
5157 | -- If we are folding a named number, retain the entity in the literal |
5158 | -- in the original tree. | |
fbf5a39b | 5159 | |
80298c3b | 5160 | if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then |
fbf5a39b AC |
5161 | Ent := Entity (N); |
5162 | else | |
5163 | Ent := Empty; | |
5164 | end if; | |
5165 | ||
5166 | if Is_Private_Type (Typ) then | |
5167 | Typ := Full_View (Typ); | |
5168 | end if; | |
5169 | ||
f3d57416 | 5170 | -- For a result of type integer, substitute an N_Integer_Literal node |
996ae0b0 | 5171 | -- for the result of the compile time evaluation of the expression. |
3aeb5ebe AC |
5172 | -- Set a link to the original named number when not in a generic context |
5173 | -- for reference in the original tree. | |
996ae0b0 | 5174 | |
fbf5a39b | 5175 | if Is_Integer_Type (Typ) then |
996ae0b0 | 5176 | Rewrite (N, Make_Integer_Literal (Loc, Val)); |
fbf5a39b | 5177 | Set_Original_Entity (N, Ent); |
996ae0b0 RK |
5178 | |
5179 | -- Otherwise we have an enumeration type, and we substitute either | |
5180 | -- an N_Identifier or N_Character_Literal to represent the enumeration | |
5181 | -- literal corresponding to the given value, which must always be in | |
5182 | -- range, because appropriate tests have already been made for this. | |
5183 | ||
fbf5a39b | 5184 | else pragma Assert (Is_Enumeration_Type (Typ)); |
996ae0b0 RK |
5185 | Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc)); |
5186 | end if; | |
5187 | ||
5188 | -- We now have the literal with the right value, both the actual type | |
5189 | -- and the expected type of this literal are taken from the expression | |
9479ded4 AC |
5190 | -- that was evaluated. So now we do the Analyze and Resolve. |
5191 | ||
5192 | -- Note that we have to reset Is_Static_Expression both after the | |
5193 | -- analyze step (because Resolve will evaluate the literal, which | |
5194 | -- will cause semantic errors if it is marked as static), and after | |
5195 | -- the Resolve step (since Resolve in some cases sets this flag). | |
996ae0b0 RK |
5196 | |
5197 | Analyze (N); | |
fbf5a39b | 5198 | Set_Is_Static_Expression (N, Static); |
996ae0b0 | 5199 | Set_Etype (N, Typ); |
fbf5a39b | 5200 | Resolve (N); |
9479ded4 | 5201 | Set_Is_Static_Expression (N, Static); |
996ae0b0 RK |
5202 | end Fold_Uint; |
5203 | ||
5204 | ---------------- | |
5205 | -- Fold_Ureal -- | |
5206 | ---------------- | |
5207 | ||
fbf5a39b | 5208 | procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is |
996ae0b0 RK |
5209 | Loc : constant Source_Ptr := Sloc (N); |
5210 | Typ : constant Entity_Id := Etype (N); | |
fbf5a39b | 5211 | Ent : Entity_Id; |
996ae0b0 RK |
5212 | |
5213 | begin | |
edab6088 RD |
5214 | if Raises_Constraint_Error (N) then |
5215 | Set_Is_Static_Expression (N, Static); | |
5216 | return; | |
5217 | end if; | |
5218 | ||
3aeb5ebe AC |
5219 | -- If we are folding a named number, retain the entity in the literal |
5220 | -- in the original tree. | |
fbf5a39b | 5221 | |
80298c3b | 5222 | if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then |
fbf5a39b AC |
5223 | Ent := Entity (N); |
5224 | else | |
5225 | Ent := Empty; | |
5226 | end if; | |
5227 | ||
996ae0b0 | 5228 | Rewrite (N, Make_Real_Literal (Loc, Realval => Val)); |
cd2fb920 | 5229 | |
3aeb5ebe | 5230 | -- Set link to original named number |
cd2fb920 | 5231 | |
fbf5a39b | 5232 | Set_Original_Entity (N, Ent); |
996ae0b0 | 5233 | |
9479ded4 AC |
5234 | -- We now have the literal with the right value, both the actual type |
5235 | -- and the expected type of this literal are taken from the expression | |
5236 | -- that was evaluated. So now we do the Analyze and Resolve. | |
5237 | ||
5238 | -- Note that we have to reset Is_Static_Expression both after the | |
5239 | -- analyze step (because Resolve will evaluate the literal, which | |
5240 | -- will cause semantic errors if it is marked as static), and after | |
5241 | -- the Resolve step (since Resolve in some cases sets this flag). | |
996ae0b0 | 5242 | |
7800a8fb YM |
5243 | -- We mark the node as analyzed so that its type is not erased by |
5244 | -- calling Analyze_Real_Literal. | |
5245 | ||
fbf5a39b AC |
5246 | Analyze (N); |
5247 | Set_Is_Static_Expression (N, Static); | |
996ae0b0 | 5248 | Set_Etype (N, Typ); |
fbf5a39b | 5249 | Resolve (N); |
7800a8fb | 5250 | Set_Analyzed (N); |
9479ded4 | 5251 | Set_Is_Static_Expression (N, Static); |
996ae0b0 RK |
5252 | end Fold_Ureal; |
5253 | ||
5254 | --------------- | |
5255 | -- From_Bits -- | |
5256 | --------------- | |
5257 | ||
5258 | function From_Bits (B : Bits; T : Entity_Id) return Uint is | |
5259 | V : Uint := Uint_0; | |
5260 | ||
5261 | begin | |
5262 | for J in 0 .. B'Last loop | |
5263 | if B (J) then | |
5264 | V := V + 2 ** J; | |
5265 | end if; | |
5266 | end loop; | |
5267 | ||
5268 | if Non_Binary_Modulus (T) then | |
5269 | V := V mod Modulus (T); | |
5270 | end if; | |
5271 | ||
5272 | return V; | |
5273 | end From_Bits; | |
5274 | ||
5275 | -------------------- | |
5276 | -- Get_String_Val -- | |
5277 | -------------------- | |
5278 | ||
5279 | function Get_String_Val (N : Node_Id) return Node_Id is | |
5280 | begin | |
4a08c95c | 5281 | if Nkind (N) in N_String_Literal | N_Character_Literal then |
996ae0b0 | 5282 | return N; |
996ae0b0 RK |
5283 | else |
5284 | pragma Assert (Is_Entity_Name (N)); | |
5285 | return Get_String_Val (Constant_Value (Entity (N))); | |
5286 | end if; | |
5287 | end Get_String_Val; | |
5288 | ||
fbf5a39b AC |
5289 | ---------------- |
5290 | -- Initialize -- | |
5291 | ---------------- | |
5292 | ||
5293 | procedure Initialize is | |
5294 | begin | |
5295 | CV_Cache := (others => (Node_High_Bound, Uint_0)); | |
5296 | end Initialize; | |
5297 | ||
996ae0b0 RK |
5298 | -------------------- |
5299 | -- In_Subrange_Of -- | |
5300 | -------------------- | |
5301 | ||
5302 | function In_Subrange_Of | |
c27f2f15 RD |
5303 | (T1 : Entity_Id; |
5304 | T2 : Entity_Id; | |
5305 | Fixed_Int : Boolean := False) return Boolean | |
996ae0b0 RK |
5306 | is |
5307 | L1 : Node_Id; | |
5308 | H1 : Node_Id; | |
5309 | ||
5310 | L2 : Node_Id; | |
5311 | H2 : Node_Id; | |
5312 | ||
5313 | begin | |
5314 | if T1 = T2 or else Is_Subtype_Of (T1, T2) then | |
5315 | return True; | |
5316 | ||
5317 | -- Never in range if both types are not scalar. Don't know if this can | |
5318 | -- actually happen, but just in case. | |
5319 | ||
9d08a38d | 5320 | elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then |
996ae0b0 RK |
5321 | return False; |
5322 | ||
d79e621a GD |
5323 | -- If T1 has infinities but T2 doesn't have infinities, then T1 is |
5324 | -- definitely not compatible with T2. | |
5325 | ||
5326 | elsif Is_Floating_Point_Type (T1) | |
5327 | and then Has_Infinities (T1) | |
5328 | and then Is_Floating_Point_Type (T2) | |
5329 | and then not Has_Infinities (T2) | |
5330 | then | |
5331 | return False; | |
5332 | ||
996ae0b0 RK |
5333 | else |
5334 | L1 := Type_Low_Bound (T1); | |
5335 | H1 := Type_High_Bound (T1); | |
5336 | ||
5337 | L2 := Type_Low_Bound (T2); | |
5338 | H2 := Type_High_Bound (T2); | |
5339 | ||
5340 | -- Check bounds to see if comparison possible at compile time | |
5341 | ||
c27f2f15 | 5342 | if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE |
996ae0b0 | 5343 | and then |
c27f2f15 | 5344 | Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE |
996ae0b0 RK |
5345 | then |
5346 | return True; | |
5347 | end if; | |
5348 | ||
5349 | -- If bounds not comparable at compile time, then the bounds of T2 | |
d3bbfc59 | 5350 | -- must be compile-time-known or we cannot answer the query. |
996ae0b0 RK |
5351 | |
5352 | if not Compile_Time_Known_Value (L2) | |
5353 | or else not Compile_Time_Known_Value (H2) | |
5354 | then | |
5355 | return False; | |
5356 | end if; | |
5357 | ||
5358 | -- If the bounds of T1 are know at compile time then use these | |
5359 | -- ones, otherwise use the bounds of the base type (which are of | |
5360 | -- course always static). | |
5361 | ||
5362 | if not Compile_Time_Known_Value (L1) then | |
5363 | L1 := Type_Low_Bound (Base_Type (T1)); | |
5364 | end if; | |
5365 | ||
5366 | if not Compile_Time_Known_Value (H1) then | |
5367 | H1 := Type_High_Bound (Base_Type (T1)); | |
5368 | end if; | |
5369 | ||
5370 | -- Fixed point types should be considered as such only if | |
5371 | -- flag Fixed_Int is set to False. | |
5372 | ||
5373 | if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2) | |
5374 | or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int) | |
5375 | or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int) | |
5376 | then | |
5377 | return | |
5378 | Expr_Value_R (L2) <= Expr_Value_R (L1) | |
5379 | and then | |
5380 | Expr_Value_R (H2) >= Expr_Value_R (H1); | |
5381 | ||
5382 | else | |
5383 | return | |
5384 | Expr_Value (L2) <= Expr_Value (L1) | |
5385 | and then | |
5386 | Expr_Value (H2) >= Expr_Value (H1); | |
5387 | ||
5388 | end if; | |
5389 | end if; | |
5390 | ||
5391 | -- If any exception occurs, it means that we have some bug in the compiler | |
f3d57416 | 5392 | -- possibly triggered by a previous error, or by some unforeseen peculiar |
996ae0b0 RK |
5393 | -- occurrence. However, this is only an optimization attempt, so there is |
5394 | -- really no point in crashing the compiler. Instead we just decide, too | |
5395 | -- bad, we can't figure out the answer in this case after all. | |
5396 | ||
5397 | exception | |
5398 | when others => | |
a34da56b PT |
5399 | -- With debug flag K we will get an exception unless an error has |
5400 | -- already occurred (useful for debugging). | |
996ae0b0 RK |
5401 | |
5402 | if Debug_Flag_K then | |
a34da56b | 5403 | Check_Error_Detected; |
996ae0b0 | 5404 | end if; |
a34da56b PT |
5405 | |
5406 | return False; | |
996ae0b0 RK |
5407 | end In_Subrange_Of; |
5408 | ||
5409 | ----------------- | |
5410 | -- Is_In_Range -- | |
5411 | ----------------- | |
5412 | ||
5413 | function Is_In_Range | |
c800f862 RD |
5414 | (N : Node_Id; |
5415 | Typ : Entity_Id; | |
5416 | Assume_Valid : Boolean := False; | |
5417 | Fixed_Int : Boolean := False; | |
5418 | Int_Real : Boolean := False) return Boolean | |
996ae0b0 | 5419 | is |
996ae0b0 | 5420 | begin |
80298c3b AC |
5421 | return |
5422 | Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range; | |
996ae0b0 RK |
5423 | end Is_In_Range; |
5424 | ||
5425 | ------------------- | |
5426 | -- Is_Null_Range -- | |
5427 | ------------------- | |
5428 | ||
5429 | function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is | |
996ae0b0 | 5430 | begin |
791f2d03 PT |
5431 | if Compile_Time_Known_Value (Lo) |
5432 | and then Compile_Time_Known_Value (Hi) | |
996ae0b0 | 5433 | then |
791f2d03 | 5434 | declare |
a2fcf1e0 | 5435 | Typ : Entity_Id := Etype (Lo); |
791f2d03 PT |
5436 | begin |
5437 | -- When called from the frontend, as part of the analysis of | |
5438 | -- potentially static expressions, Typ will be the full view of a | |
5439 | -- type with all the info needed to answer this query. When called | |
5440 | -- from the backend, for example to know whether a range of a loop | |
5441 | -- is null, Typ might be a private type and we need to explicitly | |
5442 | -- switch to its corresponding full view to access the same info. | |
5443 | ||
a2fcf1e0 PT |
5444 | if Is_Incomplete_Or_Private_Type (Typ) |
5445 | and then Present (Full_View (Typ)) | |
5446 | then | |
5447 | Typ := Full_View (Typ); | |
791f2d03 | 5448 | end if; |
996ae0b0 | 5449 | |
791f2d03 PT |
5450 | if Is_Discrete_Type (Typ) then |
5451 | return Expr_Value (Lo) > Expr_Value (Hi); | |
5452 | else pragma Assert (Is_Real_Type (Typ)); | |
5453 | return Expr_Value_R (Lo) > Expr_Value_R (Hi); | |
5454 | end if; | |
5455 | end; | |
5456 | else | |
5457 | return False; | |
996ae0b0 RK |
5458 | end if; |
5459 | end Is_Null_Range; | |
5460 | ||
edab6088 RD |
5461 | ------------------------- |
5462 | -- Is_OK_Static_Choice -- | |
5463 | ------------------------- | |
5464 | ||
5465 | function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is | |
5466 | begin | |
5467 | -- Check various possibilities for choice | |
5468 | ||
5469 | -- Note: for membership tests, we test more cases than are possible | |
5470 | -- (in particular subtype indication), but it doesn't matter because | |
5471 | -- it just won't occur (we have already done a syntax check). | |
5472 | ||
5473 | if Nkind (Choice) = N_Others_Choice then | |
5474 | return True; | |
5475 | ||
5476 | elsif Nkind (Choice) = N_Range then | |
5477 | return Is_OK_Static_Range (Choice); | |
5478 | ||
5479 | elsif Nkind (Choice) = N_Subtype_Indication | |
87feba05 | 5480 | or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))) |
edab6088 RD |
5481 | then |
5482 | return Is_OK_Static_Subtype (Etype (Choice)); | |
5483 | ||
5484 | else | |
5485 | return Is_OK_Static_Expression (Choice); | |
5486 | end if; | |
5487 | end Is_OK_Static_Choice; | |
5488 | ||
5489 | ------------------------------ | |
5490 | -- Is_OK_Static_Choice_List -- | |
5491 | ------------------------------ | |
5492 | ||
5493 | function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is | |
5494 | Choice : Node_Id; | |
5495 | ||
5496 | begin | |
5497 | if not Is_Static_Choice_List (Choices) then | |
5498 | return False; | |
5499 | end if; | |
5500 | ||
5501 | Choice := First (Choices); | |
5502 | while Present (Choice) loop | |
5503 | if not Is_OK_Static_Choice (Choice) then | |
5504 | Set_Raises_Constraint_Error (Choice); | |
5505 | return False; | |
5506 | end if; | |
5507 | ||
5508 | Next (Choice); | |
5509 | end loop; | |
5510 | ||
5511 | return True; | |
5512 | end Is_OK_Static_Choice_List; | |
5513 | ||
996ae0b0 RK |
5514 | ----------------------------- |
5515 | -- Is_OK_Static_Expression -- | |
5516 | ----------------------------- | |
5517 | ||
5518 | function Is_OK_Static_Expression (N : Node_Id) return Boolean is | |
5519 | begin | |
80298c3b | 5520 | return Is_Static_Expression (N) and then not Raises_Constraint_Error (N); |
996ae0b0 RK |
5521 | end Is_OK_Static_Expression; |
5522 | ||
5523 | ------------------------ | |
5524 | -- Is_OK_Static_Range -- | |
5525 | ------------------------ | |
5526 | ||
5527 | -- A static range is a range whose bounds are static expressions, or a | |
5528 | -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)). | |
5529 | -- We have already converted range attribute references, so we get the | |
5530 | -- "or" part of this rule without needing a special test. | |
5531 | ||
5532 | function Is_OK_Static_Range (N : Node_Id) return Boolean is | |
5533 | begin | |
5534 | return Is_OK_Static_Expression (Low_Bound (N)) | |
5535 | and then Is_OK_Static_Expression (High_Bound (N)); | |
5536 | end Is_OK_Static_Range; | |
5537 | ||
5538 | -------------------------- | |
5539 | -- Is_OK_Static_Subtype -- | |
5540 | -------------------------- | |
5541 | ||
22cb89b5 | 5542 | -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where |
1e3c434f | 5543 | -- neither bound raises Constraint_Error when evaluated. |
996ae0b0 RK |
5544 | |
5545 | function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is | |
5546 | Base_T : constant Entity_Id := Base_Type (Typ); | |
5547 | Anc_Subt : Entity_Id; | |
5548 | ||
5549 | begin | |
5550 | -- First a quick check on the non static subtype flag. As described | |
5551 | -- in further detail in Einfo, this flag is not decisive in all cases, | |
5552 | -- but if it is set, then the subtype is definitely non-static. | |
5553 | ||
5554 | if Is_Non_Static_Subtype (Typ) then | |
5555 | return False; | |
5556 | end if; | |
5557 | ||
1075946d GL |
5558 | -- Then, check if the subtype is strictly static. This takes care of |
5559 | -- checking for generics and predicates. | |
996ae0b0 | 5560 | |
1075946d | 5561 | if not Is_Static_Subtype (Typ) then |
87feba05 | 5562 | return False; |
1075946d | 5563 | end if; |
87feba05 | 5564 | |
996ae0b0 RK |
5565 | -- String types |
5566 | ||
1075946d | 5567 | if Is_String_Type (Typ) then |
996ae0b0 RK |
5568 | return |
5569 | Ekind (Typ) = E_String_Literal_Subtype | |
5570 | or else | |
011f9d5d AC |
5571 | (Is_OK_Static_Subtype (Component_Type (Typ)) |
5572 | and then Is_OK_Static_Subtype (Etype (First_Index (Typ)))); | |
996ae0b0 RK |
5573 | |
5574 | -- Scalar types | |
5575 | ||
5576 | elsif Is_Scalar_Type (Typ) then | |
5577 | if Base_T = Typ then | |
5578 | return True; | |
5579 | ||
5580 | else | |
1075946d GL |
5581 | Anc_Subt := Ancestor_Subtype (Typ); |
5582 | ||
5583 | if No (Anc_Subt) then | |
5584 | Anc_Subt := Base_T; | |
5585 | end if; | |
5586 | ||
22cb89b5 AC |
5587 | -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use |
5588 | -- Get_Type_{Low,High}_Bound. | |
996ae0b0 RK |
5589 | |
5590 | return Is_OK_Static_Subtype (Anc_Subt) | |
5591 | and then Is_OK_Static_Expression (Type_Low_Bound (Typ)) | |
5592 | and then Is_OK_Static_Expression (Type_High_Bound (Typ)); | |
5593 | end if; | |
5594 | ||
5595 | -- Types other than string and scalar types are never static | |
5596 | ||
5597 | else | |
5598 | return False; | |
5599 | end if; | |
5600 | end Is_OK_Static_Subtype; | |
5601 | ||
5602 | --------------------- | |
5603 | -- Is_Out_Of_Range -- | |
5604 | --------------------- | |
5605 | ||
5606 | function Is_Out_Of_Range | |
1c7717c3 AC |
5607 | (N : Node_Id; |
5608 | Typ : Entity_Id; | |
c800f862 | 5609 | Assume_Valid : Boolean := False; |
1c7717c3 AC |
5610 | Fixed_Int : Boolean := False; |
5611 | Int_Real : Boolean := False) return Boolean | |
996ae0b0 | 5612 | is |
996ae0b0 | 5613 | begin |
80298c3b AC |
5614 | return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = |
5615 | Out_Of_Range; | |
996ae0b0 RK |
5616 | end Is_Out_Of_Range; |
5617 | ||
edab6088 RD |
5618 | ---------------------- |
5619 | -- Is_Static_Choice -- | |
5620 | ---------------------- | |
5621 | ||
5622 | function Is_Static_Choice (Choice : Node_Id) return Boolean is | |
5623 | begin | |
5624 | -- Check various possibilities for choice | |
5625 | ||
5626 | -- Note: for membership tests, we test more cases than are possible | |
5627 | -- (in particular subtype indication), but it doesn't matter because | |
5628 | -- it just won't occur (we have already done a syntax check). | |
5629 | ||
5630 | if Nkind (Choice) = N_Others_Choice then | |
5631 | return True; | |
5632 | ||
5633 | elsif Nkind (Choice) = N_Range then | |
5634 | return Is_Static_Range (Choice); | |
5635 | ||
5636 | elsif Nkind (Choice) = N_Subtype_Indication | |
87feba05 | 5637 | or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))) |
edab6088 RD |
5638 | then |
5639 | return Is_Static_Subtype (Etype (Choice)); | |
5640 | ||
5641 | else | |
5642 | return Is_Static_Expression (Choice); | |
5643 | end if; | |
5644 | end Is_Static_Choice; | |
5645 | ||
5646 | --------------------------- | |
5647 | -- Is_Static_Choice_List -- | |
5648 | --------------------------- | |
5649 | ||
5650 | function Is_Static_Choice_List (Choices : List_Id) return Boolean is | |
5651 | Choice : Node_Id; | |
5652 | ||
5653 | begin | |
5654 | Choice := First (Choices); | |
5655 | while Present (Choice) loop | |
5656 | if not Is_Static_Choice (Choice) then | |
5657 | return False; | |
5658 | end if; | |
5659 | ||
5660 | Next (Choice); | |
5661 | end loop; | |
5662 | ||
5663 | return True; | |
5664 | end Is_Static_Choice_List; | |
5665 | ||
87feba05 | 5666 | --------------------- |
996ae0b0 RK |
5667 | -- Is_Static_Range -- |
5668 | --------------------- | |
5669 | ||
5670 | -- A static range is a range whose bounds are static expressions, or a | |
5671 | -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)). | |
5672 | -- We have already converted range attribute references, so we get the | |
5673 | -- "or" part of this rule without needing a special test. | |
5674 | ||
5675 | function Is_Static_Range (N : Node_Id) return Boolean is | |
5676 | begin | |
edab6088 | 5677 | return Is_Static_Expression (Low_Bound (N)) |
80298c3b AC |
5678 | and then |
5679 | Is_Static_Expression (High_Bound (N)); | |
996ae0b0 RK |
5680 | end Is_Static_Range; |
5681 | ||
5682 | ----------------------- | |
5683 | -- Is_Static_Subtype -- | |
5684 | ----------------------- | |
5685 | ||
82c80734 | 5686 | -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) |
996ae0b0 RK |
5687 | |
5688 | function Is_Static_Subtype (Typ : Entity_Id) return Boolean is | |
5689 | Base_T : constant Entity_Id := Base_Type (Typ); | |
5690 | Anc_Subt : Entity_Id; | |
5691 | ||
5692 | begin | |
5693 | -- First a quick check on the non static subtype flag. As described | |
5694 | -- in further detail in Einfo, this flag is not decisive in all cases, | |
5695 | -- but if it is set, then the subtype is definitely non-static. | |
5696 | ||
5697 | if Is_Non_Static_Subtype (Typ) then | |
5698 | return False; | |
5699 | end if; | |
5700 | ||
5701 | Anc_Subt := Ancestor_Subtype (Typ); | |
5702 | ||
5703 | if Anc_Subt = Empty then | |
5704 | Anc_Subt := Base_T; | |
5705 | end if; | |
5706 | ||
5707 | if Is_Generic_Type (Root_Type (Base_T)) | |
5708 | or else Is_Generic_Actual_Type (Base_T) | |
5709 | then | |
5710 | return False; | |
5711 | ||
ca0eb951 AC |
5712 | -- If there is a dynamic predicate for the type (declared or inherited) |
5713 | -- the expression is not static. | |
5714 | ||
5715 | elsif Has_Dynamic_Predicate_Aspect (Typ) | |
5716 | or else (Is_Derived_Type (Typ) | |
5717 | and then Has_Aspect (Typ, Aspect_Dynamic_Predicate)) | |
5718 | then | |
87feba05 AC |
5719 | return False; |
5720 | ||
996ae0b0 RK |
5721 | -- String types |
5722 | ||
5723 | elsif Is_String_Type (Typ) then | |
5724 | return | |
5725 | Ekind (Typ) = E_String_Literal_Subtype | |
011f9d5d AC |
5726 | or else (Is_Static_Subtype (Component_Type (Typ)) |
5727 | and then Is_Static_Subtype (Etype (First_Index (Typ)))); | |
996ae0b0 RK |
5728 | |
5729 | -- Scalar types | |
5730 | ||
5731 | elsif Is_Scalar_Type (Typ) then | |
5732 | if Base_T = Typ then | |
5733 | return True; | |
5734 | ||
5735 | else | |
5736 | return Is_Static_Subtype (Anc_Subt) | |
5737 | and then Is_Static_Expression (Type_Low_Bound (Typ)) | |
5738 | and then Is_Static_Expression (Type_High_Bound (Typ)); | |
5739 | end if; | |
5740 | ||
5741 | -- Types other than string and scalar types are never static | |
5742 | ||
5743 | else | |
5744 | return False; | |
5745 | end if; | |
5746 | end Is_Static_Subtype; | |
5747 | ||
edab6088 RD |
5748 | ------------------------------- |
5749 | -- Is_Statically_Unevaluated -- | |
5750 | ------------------------------- | |
5751 | ||
5752 | function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is | |
5753 | function Check_Case_Expr_Alternative | |
5754 | (CEA : Node_Id) return Match_Result; | |
5755 | -- We have a message emanating from the Expression of a case expression | |
5756 | -- alternative. We examine this alternative, as follows: | |
5757 | -- | |
5758 | -- If the selecting expression of the parent case is non-static, or | |
5759 | -- if any of the discrete choices of the given case alternative are | |
5760 | -- non-static or raise Constraint_Error, return Non_Static. | |
5761 | -- | |
5762 | -- Otherwise check if the selecting expression matches any of the given | |
4bd4bb7f AC |
5763 | -- discrete choices. If so, the alternative is executed and we return |
5764 | -- Match, otherwise, the alternative can never be executed, and so we | |
5765 | -- return No_Match. | |
edab6088 RD |
5766 | |
5767 | --------------------------------- | |
5768 | -- Check_Case_Expr_Alternative -- | |
5769 | --------------------------------- | |
5770 | ||
5771 | function Check_Case_Expr_Alternative | |
5772 | (CEA : Node_Id) return Match_Result | |
5773 | is | |
5774 | Case_Exp : constant Node_Id := Parent (CEA); | |
5775 | Choice : Node_Id; | |
5776 | Prev_CEA : Node_Id; | |
5777 | ||
5778 | begin | |
5779 | pragma Assert (Nkind (Case_Exp) = N_Case_Expression); | |
5780 | ||
4bd4bb7f | 5781 | -- Check that selecting expression is static |
edab6088 RD |
5782 | |
5783 | if not Is_OK_Static_Expression (Expression (Case_Exp)) then | |
5784 | return Non_Static; | |
5785 | end if; | |
5786 | ||
5787 | if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then | |
5788 | return Non_Static; | |
5789 | end if; | |
5790 | ||
5791 | -- All choices are now known to be static. Now see if alternative | |
5792 | -- matches one of the choices. | |
5793 | ||
5794 | Choice := First (Discrete_Choices (CEA)); | |
5795 | while Present (Choice) loop | |
5796 | ||
4bd4bb7f | 5797 | -- Check various possibilities for choice, returning Match if we |
edab6088 RD |
5798 | -- find the selecting value matches any of the choices. Note that |
5799 | -- we know we are the last choice, so we don't have to keep going. | |
5800 | ||
5801 | if Nkind (Choice) = N_Others_Choice then | |
5802 | ||
5803 | -- Others choice is a bit annoying, it matches if none of the | |
5804 | -- previous alternatives matches (note that we know we are the | |
5805 | -- last alternative in this case, so we can just go backwards | |
5806 | -- from us to see if any previous one matches). | |
5807 | ||
5808 | Prev_CEA := Prev (CEA); | |
5809 | while Present (Prev_CEA) loop | |
5810 | if Check_Case_Expr_Alternative (Prev_CEA) = Match then | |
5811 | return No_Match; | |
5812 | end if; | |
5813 | ||
5814 | Prev (Prev_CEA); | |
5815 | end loop; | |
5816 | ||
5817 | return Match; | |
5818 | ||
5819 | -- Else we have a normal static choice | |
5820 | ||
5821 | elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then | |
5822 | return Match; | |
5823 | end if; | |
5824 | ||
5825 | -- If we fall through, it means that the discrete choice did not | |
5826 | -- match the selecting expression, so continue. | |
5827 | ||
5828 | Next (Choice); | |
5829 | end loop; | |
5830 | ||
4bd4bb7f AC |
5831 | -- If we get through that loop then all choices were static, and none |
5832 | -- of them matched the selecting expression. So return No_Match. | |
edab6088 RD |
5833 | |
5834 | return No_Match; | |
5835 | end Check_Case_Expr_Alternative; | |
5836 | ||
5837 | -- Local variables | |
5838 | ||
5839 | P : Node_Id; | |
5840 | OldP : Node_Id; | |
5841 | Choice : Node_Id; | |
5842 | ||
5843 | -- Start of processing for Is_Statically_Unevaluated | |
5844 | ||
5845 | begin | |
5846 | -- The (32.x) references here are from RM section 4.9 | |
5847 | ||
5848 | -- (32.1) An expression is statically unevaluated if it is part of ... | |
5849 | ||
5850 | -- This means we have to climb the tree looking for one of the cases | |
5851 | ||
5852 | P := Expr; | |
5853 | loop | |
5854 | OldP := P; | |
5855 | P := Parent (P); | |
5856 | ||
5857 | -- (32.2) The right operand of a static short-circuit control form | |
5858 | -- whose value is determined by its left operand. | |
5859 | ||
5860 | -- AND THEN with False as left operand | |
5861 | ||
5862 | if Nkind (P) = N_And_Then | |
5863 | and then Compile_Time_Known_Value (Left_Opnd (P)) | |
5864 | and then Is_False (Expr_Value (Left_Opnd (P))) | |
5865 | then | |
5866 | return True; | |
5867 | ||
5868 | -- OR ELSE with True as left operand | |
5869 | ||
5870 | elsif Nkind (P) = N_Or_Else | |
5871 | and then Compile_Time_Known_Value (Left_Opnd (P)) | |
5872 | and then Is_True (Expr_Value (Left_Opnd (P))) | |
5873 | then | |
5874 | return True; | |
5875 | ||
5876 | -- (32.3) A dependent_expression of an if_expression whose associated | |
5877 | -- condition is static and equals False. | |
5878 | ||
5879 | elsif Nkind (P) = N_If_Expression then | |
5880 | declare | |
5881 | Cond : constant Node_Id := First (Expressions (P)); | |
5882 | Texp : constant Node_Id := Next (Cond); | |
5883 | Fexp : constant Node_Id := Next (Texp); | |
5884 | ||
5885 | begin | |
5886 | if Compile_Time_Known_Value (Cond) then | |
5887 | ||
5888 | -- Condition is True and we are in the right operand | |
5889 | ||
5890 | if Is_True (Expr_Value (Cond)) and then OldP = Fexp then | |
5891 | return True; | |
5892 | ||
5893 | -- Condition is False and we are in the left operand | |
5894 | ||
5895 | elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then | |
5896 | return True; | |
5897 | end if; | |
5898 | end if; | |
5899 | end; | |
5900 | ||
5901 | -- (32.4) A condition or dependent_expression of an if_expression | |
5902 | -- where the condition corresponding to at least one preceding | |
5903 | -- dependent_expression of the if_expression is static and equals | |
5904 | -- True. | |
5905 | ||
5906 | -- This refers to cases like | |
5907 | ||
4bd4bb7f | 5908 | -- (if True then 1 elsif 1/0=2 then 2 else 3) |
edab6088 RD |
5909 | |
5910 | -- But we expand elsif's out anyway, so the above looks like: | |
5911 | ||
4bd4bb7f | 5912 | -- (if True then 1 else (if 1/0=2 then 2 else 3)) |
edab6088 RD |
5913 | |
5914 | -- So for us this is caught by the above check for the 32.3 case. | |
5915 | ||
5916 | -- (32.5) A dependent_expression of a case_expression whose | |
5917 | -- selecting_expression is static and whose value is not covered | |
5918 | -- by the corresponding discrete_choice_list. | |
5919 | ||
5920 | elsif Nkind (P) = N_Case_Expression_Alternative then | |
5921 | ||
5922 | -- First, we have to be in the expression to suppress messages. | |
5923 | -- If we are within one of the choices, we want the message. | |
5924 | ||
5925 | if OldP = Expression (P) then | |
5926 | ||
5927 | -- Statically unevaluated if alternative does not match | |
5928 | ||
5929 | if Check_Case_Expr_Alternative (P) = No_Match then | |
5930 | return True; | |
5931 | end if; | |
5932 | end if; | |
5933 | ||
5934 | -- (32.6) A choice_expression (or a simple_expression of a range | |
5935 | -- that occurs as a membership_choice of a membership_choice_list) | |
5936 | -- of a static membership test that is preceded in the enclosing | |
5937 | -- membership_choice_list by another item whose individual | |
5938 | -- membership test (see (RM 4.5.2)) statically yields True. | |
5939 | ||
5940 | elsif Nkind (P) in N_Membership_Test then | |
5941 | ||
5942 | -- Only possibly unevaluated if simple expression is static | |
5943 | ||
5944 | if not Is_OK_Static_Expression (Left_Opnd (P)) then | |
5945 | null; | |
5946 | ||
5947 | -- All members of the choice list must be static | |
5948 | ||
5949 | elsif (Present (Right_Opnd (P)) | |
5950 | and then not Is_OK_Static_Choice (Right_Opnd (P))) | |
5951 | or else (Present (Alternatives (P)) | |
5952 | and then | |
5953 | not Is_OK_Static_Choice_List (Alternatives (P))) | |
5954 | then | |
5955 | null; | |
5956 | ||
5957 | -- If expression is the one and only alternative, then it is | |
5958 | -- definitely not statically unevaluated, so we only have to | |
5959 | -- test the case where there are alternatives present. | |
5960 | ||
5961 | elsif Present (Alternatives (P)) then | |
5962 | ||
5963 | -- Look for previous matching Choice | |
5964 | ||
5965 | Choice := First (Alternatives (P)); | |
5966 | while Present (Choice) loop | |
5967 | ||
5968 | -- If we reached us and no previous choices matched, this | |
5969 | -- is not the case where we are statically unevaluated. | |
5970 | ||
5971 | exit when OldP = Choice; | |
5972 | ||
5973 | -- If a previous choice matches, then that is the case where | |
5974 | -- we know our choice is statically unevaluated. | |
5975 | ||
5976 | if Choice_Matches (Left_Opnd (P), Choice) = Match then | |
5977 | return True; | |
5978 | end if; | |
5979 | ||
5980 | Next (Choice); | |
5981 | end loop; | |
5982 | ||
5983 | -- If we fall through the loop, we were not one of the choices, | |
5984 | -- we must have been the expression, so that is not covered by | |
5985 | -- this rule, and we keep going. | |
5986 | ||
5987 | null; | |
5988 | end if; | |
5989 | end if; | |
5990 | ||
5991 | -- OK, not statically unevaluated at this level, see if we should | |
5992 | -- keep climbing to look for a higher level reason. | |
5993 | ||
5994 | -- Special case for component association in aggregates, where | |
5995 | -- we want to keep climbing up to the parent aggregate. | |
5996 | ||
5997 | if Nkind (P) = N_Component_Association | |
5998 | and then Nkind (Parent (P)) = N_Aggregate | |
5999 | then | |
6000 | null; | |
6001 | ||
6002 | -- All done if not still within subexpression | |
6003 | ||
6004 | else | |
6005 | exit when Nkind (P) not in N_Subexpr; | |
6006 | end if; | |
6007 | end loop; | |
6008 | ||
6009 | -- If we fall through the loop, not one of the cases covered! | |
6010 | ||
6011 | return False; | |
6012 | end Is_Statically_Unevaluated; | |
6013 | ||
996ae0b0 RK |
6014 | -------------------- |
6015 | -- Not_Null_Range -- | |
6016 | -------------------- | |
6017 | ||
6018 | function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is | |
996ae0b0 | 6019 | begin |
791f2d03 PT |
6020 | if Compile_Time_Known_Value (Lo) |
6021 | and then Compile_Time_Known_Value (Hi) | |
996ae0b0 | 6022 | then |
791f2d03 | 6023 | declare |
a2fcf1e0 | 6024 | Typ : Entity_Id := Etype (Lo); |
791f2d03 PT |
6025 | begin |
6026 | -- When called from the frontend, as part of the analysis of | |
6027 | -- potentially static expressions, Typ will be the full view of a | |
6028 | -- type with all the info needed to answer this query. When called | |
6029 | -- from the backend, for example to know whether a range of a loop | |
6030 | -- is null, Typ might be a private type and we need to explicitly | |
6031 | -- switch to its corresponding full view to access the same info. | |
6032 | ||
a2fcf1e0 PT |
6033 | if Is_Incomplete_Or_Private_Type (Typ) |
6034 | and then Present (Full_View (Typ)) | |
6035 | then | |
6036 | Typ := Full_View (Typ); | |
791f2d03 PT |
6037 | end if; |
6038 | ||
6039 | if Is_Discrete_Type (Typ) then | |
6040 | return Expr_Value (Lo) <= Expr_Value (Hi); | |
6041 | else pragma Assert (Is_Real_Type (Typ)); | |
6042 | return Expr_Value_R (Lo) <= Expr_Value_R (Hi); | |
6043 | end if; | |
6044 | end; | |
6045 | else | |
996ae0b0 RK |
6046 | return False; |
6047 | end if; | |
6048 | ||
996ae0b0 RK |
6049 | end Not_Null_Range; |
6050 | ||
6051 | ------------- | |
6052 | -- OK_Bits -- | |
6053 | ------------- | |
6054 | ||
6055 | function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is | |
6056 | begin | |
6057 | -- We allow a maximum of 500,000 bits which seems a reasonable limit | |
6058 | ||
6059 | if Bits < 500_000 then | |
6060 | return True; | |
6061 | ||
80298c3b AC |
6062 | -- Error if this maximum is exceeded |
6063 | ||
996ae0b0 RK |
6064 | else |
6065 | Error_Msg_N ("static value too large, capacity exceeded", N); | |
6066 | return False; | |
6067 | end if; | |
6068 | end OK_Bits; | |
6069 | ||
6070 | ------------------ | |
6071 | -- Out_Of_Range -- | |
6072 | ------------------ | |
6073 | ||
6074 | procedure Out_Of_Range (N : Node_Id) is | |
6075 | begin | |
6076 | -- If we have the static expression case, then this is an illegality | |
6077 | -- in Ada 95 mode, except that in an instance, we never generate an | |
22cb89b5 | 6078 | -- error (if the error is legitimate, it was already diagnosed in the |
ac072cb2 | 6079 | -- template). |
996ae0b0 RK |
6080 | |
6081 | if Is_Static_Expression (N) | |
6082 | and then not In_Instance | |
fbf5a39b | 6083 | and then not In_Inlined_Body |
0ab80019 | 6084 | and then Ada_Version >= Ada_95 |
996ae0b0 | 6085 | then |
4bd4bb7f | 6086 | -- No message if we are statically unevaluated |
ac072cb2 AC |
6087 | |
6088 | if Is_Statically_Unevaluated (N) then | |
6089 | null; | |
6090 | ||
6091 | -- The expression to compute the length of a packed array is attached | |
6092 | -- to the array type itself, and deserves a separate message. | |
6093 | ||
6094 | elsif Nkind (Parent (N)) = N_Defining_Identifier | |
996ae0b0 | 6095 | and then Is_Array_Type (Parent (N)) |
8ca597af | 6096 | and then Present (Packed_Array_Impl_Type (Parent (N))) |
996ae0b0 RK |
6097 | and then Present (First_Rep_Item (Parent (N))) |
6098 | then | |
6099 | Error_Msg_N | |
6100 | ("length of packed array must not exceed Integer''Last", | |
6101 | First_Rep_Item (Parent (N))); | |
6102 | Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1)); | |
6103 | ||
88ad52c9 AC |
6104 | -- All cases except the special array case. |
6105 | -- No message if we are dealing with System.Priority values in | |
6106 | -- CodePeer mode where the target runtime may have more priorities. | |
ac072cb2 | 6107 | |
3477e0b2 PT |
6108 | elsif not CodePeer_Mode |
6109 | or else not Is_RTE (Etype (N), RE_Priority) | |
6110 | then | |
31fde973 GD |
6111 | -- Determine if the out-of-range violation constitutes a warning |
6112 | -- or an error based on context, according to RM 4.9 (34/3). | |
33defa7c JS |
6113 | |
6114 | if Nkind (Original_Node (N)) = N_Type_Conversion | |
6115 | and then not Comes_From_Source (Original_Node (N)) | |
6116 | then | |
6117 | Apply_Compile_Time_Constraint_Error | |
6118 | (N, "value not in range of}??", CE_Range_Check_Failed); | |
6119 | else | |
6120 | Apply_Compile_Time_Constraint_Error | |
6121 | (N, "value not in range of}", CE_Range_Check_Failed); | |
6122 | end if; | |
996ae0b0 RK |
6123 | end if; |
6124 | ||
22cb89b5 AC |
6125 | -- Here we generate a warning for the Ada 83 case, or when we are in an |
6126 | -- instance, or when we have a non-static expression case. | |
996ae0b0 RK |
6127 | |
6128 | else | |
996ae0b0 | 6129 | Apply_Compile_Time_Constraint_Error |
324ac540 | 6130 | (N, "value not in range of}??", CE_Range_Check_Failed); |
996ae0b0 RK |
6131 | end if; |
6132 | end Out_Of_Range; | |
6133 | ||
0faf0503 EB |
6134 | --------------------------- |
6135 | -- Predicates_Compatible -- | |
6136 | --------------------------- | |
6137 | ||
6138 | function Predicates_Compatible (T1, T2 : Entity_Id) return Boolean is | |
6139 | ||
6140 | function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean; | |
6141 | -- Return True if the rep item for Nam is either absent on T2 or also | |
6142 | -- applies to T1. | |
6143 | ||
6144 | ------------------------------- | |
6145 | -- T2_Rep_Item_Applies_To_T1 -- | |
6146 | ------------------------------- | |
6147 | ||
6148 | function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean is | |
6149 | Rep_Item : constant Node_Id := Get_Rep_Item (T2, Nam); | |
6150 | ||
6151 | begin | |
6152 | return No (Rep_Item) or else Get_Rep_Item (T1, Nam) = Rep_Item; | |
6153 | end T2_Rep_Item_Applies_To_T1; | |
6154 | ||
6155 | -- Start of processing for Predicates_Compatible | |
6156 | ||
6157 | begin | |
6158 | if Ada_Version < Ada_2012 then | |
6159 | return True; | |
6160 | ||
6161 | -- If T2 has no predicates, there is no compatibility issue | |
6162 | ||
6163 | elsif not Has_Predicates (T2) then | |
6164 | return True; | |
6165 | ||
6166 | -- T2 has predicates, if T1 has none then we defer to the static check | |
6167 | ||
6168 | elsif not Has_Predicates (T1) then | |
6169 | null; | |
6170 | ||
6171 | -- Both T2 and T1 have predicates, check that all predicates that apply | |
6172 | -- to T2 apply also to T1 (RM 4.9.1(9/3)). | |
6173 | ||
6174 | elsif T2_Rep_Item_Applies_To_T1 (Name_Static_Predicate) | |
6175 | and then T2_Rep_Item_Applies_To_T1 (Name_Dynamic_Predicate) | |
6176 | and then T2_Rep_Item_Applies_To_T1 (Name_Predicate) | |
6177 | then | |
6178 | return True; | |
6179 | end if; | |
6180 | ||
6181 | -- Implement the static check prescribed by RM 4.9.1(10/3) | |
6182 | ||
6183 | if Is_Static_Subtype (T1) and then Is_Static_Subtype (T2) then | |
6184 | -- We just need to query Interval_Lists for discrete types | |
6185 | ||
6186 | if Is_Discrete_Type (T1) and then Is_Discrete_Type (T2) then | |
6187 | declare | |
6188 | Interval_List1 : constant Interval_Lists.Discrete_Interval_List | |
6189 | := Interval_Lists.Type_Intervals (T1); | |
6190 | Interval_List2 : constant Interval_Lists.Discrete_Interval_List | |
6191 | := Interval_Lists.Type_Intervals (T2); | |
6192 | begin | |
6193 | return Interval_Lists.Is_Subset (Interval_List1, Interval_List2) | |
6194 | and then not (Has_Predicates (T1) | |
6195 | and then not Predicate_Checks_Suppressed (T2) | |
6196 | and then Predicate_Checks_Suppressed (T1)); | |
6197 | end; | |
6198 | ||
6199 | else | |
6200 | -- TBD: Implement Interval_Lists for real types | |
6201 | ||
6202 | return False; | |
6203 | end if; | |
6204 | ||
6205 | -- If either subtype is not static, the predicates are not compatible | |
6206 | ||
6207 | else | |
6208 | return False; | |
6209 | end if; | |
6210 | end Predicates_Compatible; | |
6211 | ||
7f568bfa AC |
6212 | ---------------------- |
6213 | -- Predicates_Match -- | |
6214 | ---------------------- | |
6215 | ||
6216 | function Predicates_Match (T1, T2 : Entity_Id) return Boolean is | |
4331490b EB |
6217 | |
6218 | function Have_Same_Rep_Item (Nam : Name_Id) return Boolean; | |
6219 | -- Return True if T1 and T2 have the same rep item for Nam | |
6220 | ||
6221 | ------------------------ | |
6222 | -- Have_Same_Rep_Item -- | |
6223 | ------------------------ | |
6224 | ||
6225 | function Have_Same_Rep_Item (Nam : Name_Id) return Boolean is | |
6226 | begin | |
6227 | return Get_Rep_Item (T1, Nam) = Get_Rep_Item (T2, Nam); | |
6228 | end Have_Same_Rep_Item; | |
6229 | ||
6230 | -- Start of processing for Predicates_Match | |
7f568bfa AC |
6231 | |
6232 | begin | |
6233 | if Ada_Version < Ada_2012 then | |
6234 | return True; | |
6235 | ||
4331490b EB |
6236 | -- If T2 has no predicates, match if and only if T1 has none |
6237 | ||
6238 | elsif not Has_Predicates (T2) then | |
6239 | return not Has_Predicates (T1); | |
6240 | ||
6241 | -- T2 has predicates, no match if T1 has none | |
7f568bfa | 6242 | |
4331490b | 6243 | elsif not Has_Predicates (T1) then |
7f568bfa AC |
6244 | return False; |
6245 | ||
4331490b EB |
6246 | -- Both T2 and T1 have predicates, check that they all come |
6247 | -- from the same declarations. | |
7f568bfa AC |
6248 | |
6249 | else | |
4331490b EB |
6250 | return Have_Same_Rep_Item (Name_Static_Predicate) |
6251 | and then Have_Same_Rep_Item (Name_Dynamic_Predicate) | |
6252 | and then Have_Same_Rep_Item (Name_Predicate); | |
7f568bfa AC |
6253 | end if; |
6254 | end Predicates_Match; | |
6255 | ||
fc3a3f3b RD |
6256 | --------------------------------------------- |
6257 | -- Real_Or_String_Static_Predicate_Matches -- | |
6258 | --------------------------------------------- | |
6259 | ||
6260 | function Real_Or_String_Static_Predicate_Matches | |
6261 | (Val : Node_Id; | |
6262 | Typ : Entity_Id) return Boolean | |
6263 | is | |
6264 | Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ); | |
6265 | -- The predicate expression from the type | |
6266 | ||
6267 | Pfun : constant Entity_Id := Predicate_Function (Typ); | |
6268 | -- The entity for the predicate function | |
6269 | ||
6270 | Ent_Name : constant Name_Id := Chars (First_Formal (Pfun)); | |
6271 | -- The name of the formal of the predicate function. Occurrences of the | |
6272 | -- type name in Expr have been rewritten as references to this formal, | |
6273 | -- and it has a unique name, so we can identify references by this name. | |
6274 | ||
6275 | Copy : Node_Id; | |
6276 | -- Copy of the predicate function tree | |
6277 | ||
6278 | function Process (N : Node_Id) return Traverse_Result; | |
6279 | -- Function used to process nodes during the traversal in which we will | |
6280 | -- find occurrences of the entity name, and replace such occurrences | |
6281 | -- by a real literal with the value to be tested. | |
6282 | ||
6283 | procedure Traverse is new Traverse_Proc (Process); | |
6284 | -- The actual traversal procedure | |
6285 | ||
6286 | ------------- | |
6287 | -- Process -- | |
6288 | ------------- | |
6289 | ||
6290 | function Process (N : Node_Id) return Traverse_Result is | |
6291 | begin | |
6292 | if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then | |
6293 | declare | |
6294 | Nod : constant Node_Id := New_Copy (Val); | |
6295 | begin | |
6296 | Set_Sloc (Nod, Sloc (N)); | |
6297 | Rewrite (N, Nod); | |
6298 | return Skip; | |
6299 | end; | |
6300 | ||
e4d04166 AC |
6301 | -- The predicate function may contain string-comparison operations |
6302 | -- that have been converted into calls to run-time array-comparison | |
6303 | -- routines. To evaluate the predicate statically, we recover the | |
6304 | -- original comparison operation and replace the occurrence of the | |
6305 | -- formal by the static string value. The actuals of the generated | |
6306 | -- call are of the form X'Address. | |
6307 | ||
6308 | elsif Nkind (N) in N_Op_Compare | |
6309 | and then Nkind (Left_Opnd (N)) = N_Function_Call | |
6310 | then | |
6311 | declare | |
6312 | C : constant Node_Id := Left_Opnd (N); | |
6313 | F : constant Node_Id := First (Parameter_Associations (C)); | |
6314 | L : constant Node_Id := Prefix (F); | |
6315 | R : constant Node_Id := Prefix (Next (F)); | |
6316 | ||
6317 | begin | |
6318 | -- If an operand is an entity name, it is the formal of the | |
6319 | -- predicate function, so replace it with the string value. | |
6320 | -- It may be either operand in the call. The other operand | |
6321 | -- is a static string from the original predicate. | |
6322 | ||
6323 | if Is_Entity_Name (L) then | |
6324 | Rewrite (Left_Opnd (N), New_Copy (Val)); | |
6325 | Rewrite (Right_Opnd (N), New_Copy (R)); | |
6326 | ||
6327 | else | |
6328 | Rewrite (Left_Opnd (N), New_Copy (L)); | |
6329 | Rewrite (Right_Opnd (N), New_Copy (Val)); | |
6330 | end if; | |
6331 | ||
6332 | return Skip; | |
6333 | end; | |
6334 | ||
fc3a3f3b RD |
6335 | else |
6336 | return OK; | |
6337 | end if; | |
6338 | end Process; | |
6339 | ||
6340 | -- Start of processing for Real_Or_String_Static_Predicate_Matches | |
6341 | ||
6342 | begin | |
6343 | -- First deal with special case of inherited predicate, where the | |
6344 | -- predicate expression looks like: | |
6345 | ||
9bdc432a | 6346 | -- xxPredicate (typ (Ent)) and then Expr |
fc3a3f3b RD |
6347 | |
6348 | -- where Expr is the predicate expression for this level, and the | |
9bdc432a | 6349 | -- left operand is the call to evaluate the inherited predicate. |
fc3a3f3b RD |
6350 | |
6351 | if Nkind (Expr) = N_And_Then | |
9bdc432a AC |
6352 | and then Nkind (Left_Opnd (Expr)) = N_Function_Call |
6353 | and then Is_Predicate_Function (Entity (Name (Left_Opnd (Expr)))) | |
fc3a3f3b RD |
6354 | then |
6355 | -- OK we have the inherited case, so make a call to evaluate the | |
6356 | -- inherited predicate. If that fails, so do we! | |
6357 | ||
6358 | if not | |
6359 | Real_Or_String_Static_Predicate_Matches | |
6360 | (Val => Val, | |
9bdc432a | 6361 | Typ => Etype (First_Formal (Entity (Name (Left_Opnd (Expr)))))) |
fc3a3f3b RD |
6362 | then |
6363 | return False; | |
6364 | end if; | |
6365 | ||
9bdc432a | 6366 | -- Use the right operand for the continued processing |
fc3a3f3b | 6367 | |
9bdc432a | 6368 | Copy := Copy_Separate_Tree (Right_Opnd (Expr)); |
fc3a3f3b | 6369 | |
622599c6 RD |
6370 | -- Case where call to predicate function appears on its own (this means |
6371 | -- that the predicate at this level is just inherited from the parent). | |
fc3a3f3b | 6372 | |
1b1d88b1 | 6373 | elsif Nkind (Expr) = N_Function_Call then |
622599c6 RD |
6374 | declare |
6375 | Typ : constant Entity_Id := | |
6376 | Etype (First_Formal (Entity (Name (Expr)))); | |
fc3a3f3b | 6377 | |
622599c6 RD |
6378 | begin |
6379 | -- If the inherited predicate is dynamic, just ignore it. We can't | |
6380 | -- go trying to evaluate a dynamic predicate as a static one! | |
fc3a3f3b | 6381 | |
622599c6 RD |
6382 | if Has_Dynamic_Predicate_Aspect (Typ) then |
6383 | return True; | |
6384 | ||
6385 | -- Otherwise inherited predicate is static, check for match | |
6386 | ||
6387 | else | |
6388 | return Real_Or_String_Static_Predicate_Matches (Val, Typ); | |
6389 | end if; | |
6390 | end; | |
fc3a3f3b | 6391 | |
622599c6 | 6392 | -- If not just an inherited predicate, copy whole expression |
fc3a3f3b RD |
6393 | |
6394 | else | |
6395 | Copy := Copy_Separate_Tree (Expr); | |
6396 | end if; | |
6397 | ||
6398 | -- Now we replace occurrences of the entity by the value | |
6399 | ||
6400 | Traverse (Copy); | |
6401 | ||
6402 | -- And analyze the resulting static expression to see if it is True | |
6403 | ||
6404 | Analyze_And_Resolve (Copy, Standard_Boolean); | |
6405 | return Is_True (Expr_Value (Copy)); | |
6406 | end Real_Or_String_Static_Predicate_Matches; | |
6407 | ||
996ae0b0 RK |
6408 | ------------------------- |
6409 | -- Rewrite_In_Raise_CE -- | |
6410 | ------------------------- | |
6411 | ||
6412 | procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is | |
edab6088 | 6413 | Stat : constant Boolean := Is_Static_Expression (N); |
61770974 | 6414 | Typ : constant Entity_Id := Etype (N); |
996ae0b0 RK |
6415 | |
6416 | begin | |
edab6088 RD |
6417 | -- If we want to raise CE in the condition of a N_Raise_CE node, we |
6418 | -- can just clear the condition if the reason is appropriate. We do | |
6419 | -- not do this operation if the parent has a reason other than range | |
6420 | -- check failed, because otherwise we would change the reason. | |
996ae0b0 RK |
6421 | |
6422 | if Present (Parent (N)) | |
6423 | and then Nkind (Parent (N)) = N_Raise_Constraint_Error | |
edab6088 RD |
6424 | and then Reason (Parent (N)) = |
6425 | UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed)) | |
996ae0b0 RK |
6426 | then |
6427 | Set_Condition (Parent (N), Empty); | |
6428 | ||
edab6088 | 6429 | -- Else build an explicit N_Raise_CE |
996ae0b0 RK |
6430 | |
6431 | else | |
5b4f211d AC |
6432 | if Nkind (Exp) = N_Raise_Constraint_Error then |
6433 | Rewrite (N, | |
6434 | Make_Raise_Constraint_Error (Sloc (Exp), | |
6435 | Reason => Reason (Exp))); | |
6436 | else | |
6437 | Rewrite (N, | |
6438 | Make_Raise_Constraint_Error (Sloc (Exp), | |
6439 | Reason => CE_Range_Check_Failed)); | |
6440 | end if; | |
6441 | ||
996ae0b0 RK |
6442 | Set_Raises_Constraint_Error (N); |
6443 | Set_Etype (N, Typ); | |
6444 | end if; | |
edab6088 RD |
6445 | |
6446 | -- Set proper flags in result | |
6447 | ||
6448 | Set_Raises_Constraint_Error (N, True); | |
6449 | Set_Is_Static_Expression (N, Stat); | |
996ae0b0 RK |
6450 | end Rewrite_In_Raise_CE; |
6451 | ||
bbab2db3 GD |
6452 | ------------------------------------------------ |
6453 | -- Set_Checking_Potentially_Static_Expression -- | |
6454 | ------------------------------------------------ | |
6455 | ||
6456 | procedure Set_Checking_Potentially_Static_Expression (Value : Boolean) is | |
6457 | begin | |
6458 | -- Verify that we're not currently checking for a potentially static | |
6459 | -- expression unless we're disabling such checking. | |
6460 | ||
6461 | pragma Assert | |
6462 | (not Checking_For_Potentially_Static_Expression or else not Value); | |
6463 | ||
6464 | Checking_For_Potentially_Static_Expression := Value; | |
6465 | end Set_Checking_Potentially_Static_Expression; | |
6466 | ||
996ae0b0 RK |
6467 | --------------------- |
6468 | -- String_Type_Len -- | |
6469 | --------------------- | |
6470 | ||
6471 | function String_Type_Len (Stype : Entity_Id) return Uint is | |
6472 | NT : constant Entity_Id := Etype (First_Index (Stype)); | |
6473 | T : Entity_Id; | |
6474 | ||
6475 | begin | |
6476 | if Is_OK_Static_Subtype (NT) then | |
6477 | T := NT; | |
6478 | else | |
6479 | T := Base_Type (NT); | |
6480 | end if; | |
6481 | ||
6482 | return Expr_Value (Type_High_Bound (T)) - | |
6483 | Expr_Value (Type_Low_Bound (T)) + 1; | |
6484 | end String_Type_Len; | |
6485 | ||
6486 | ------------------------------------ | |
6487 | -- Subtypes_Statically_Compatible -- | |
6488 | ------------------------------------ | |
6489 | ||
6490 | function Subtypes_Statically_Compatible | |
c97d7285 AC |
6491 | (T1 : Entity_Id; |
6492 | T2 : Entity_Id; | |
6493 | Formal_Derived_Matching : Boolean := False) return Boolean | |
996ae0b0 RK |
6494 | is |
6495 | begin | |
0faf0503 EB |
6496 | -- A type is always statically compatible with itself |
6497 | ||
6498 | if T1 = T2 then | |
6499 | return True; | |
6500 | ||
6501 | -- Not compatible if predicates are not compatible | |
6502 | ||
6503 | elsif not Predicates_Compatible (T1, T2) then | |
6504 | return False; | |
6505 | ||
437f8c1e AC |
6506 | -- Scalar types |
6507 | ||
0faf0503 | 6508 | elsif Is_Scalar_Type (T1) then |
996ae0b0 RK |
6509 | |
6510 | -- Definitely compatible if we match | |
6511 | ||
6512 | if Subtypes_Statically_Match (T1, T2) then | |
6513 | return True; | |
6514 | ||
55fae09d ES |
6515 | -- A scalar subtype S1 is compatible with S2 if their bounds |
6516 | -- are static and compatible, even if S1 has dynamic predicates | |
6517 | -- and is thus non-static. Predicate compatibility has been | |
6518 | -- checked above. | |
996ae0b0 | 6519 | |
55fae09d ES |
6520 | elsif not Is_Static_Range (Scalar_Range (T1)) |
6521 | or else not Is_Static_Range (Scalar_Range (T2)) | |
996ae0b0 RK |
6522 | then |
6523 | return False; | |
6524 | ||
26df19ce AC |
6525 | -- Base types must match, but we don't check that (should we???) but |
6526 | -- we do at least check that both types are real, or both types are | |
6527 | -- not real. | |
996ae0b0 | 6528 | |
fbf5a39b | 6529 | elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then |
996ae0b0 RK |
6530 | return False; |
6531 | ||
6532 | -- Here we check the bounds | |
6533 | ||
6534 | else | |
6535 | declare | |
6536 | LB1 : constant Node_Id := Type_Low_Bound (T1); | |
6537 | HB1 : constant Node_Id := Type_High_Bound (T1); | |
6538 | LB2 : constant Node_Id := Type_Low_Bound (T2); | |
6539 | HB2 : constant Node_Id := Type_High_Bound (T2); | |
6540 | ||
6541 | begin | |
6542 | if Is_Real_Type (T1) then | |
6543 | return | |
304757d2 | 6544 | Expr_Value_R (LB1) > Expr_Value_R (HB1) |
996ae0b0 | 6545 | or else |
304757d2 AC |
6546 | (Expr_Value_R (LB2) <= Expr_Value_R (LB1) |
6547 | and then Expr_Value_R (HB1) <= Expr_Value_R (HB2)); | |
996ae0b0 RK |
6548 | |
6549 | else | |
6550 | return | |
304757d2 | 6551 | Expr_Value (LB1) > Expr_Value (HB1) |
996ae0b0 | 6552 | or else |
304757d2 AC |
6553 | (Expr_Value (LB2) <= Expr_Value (LB1) |
6554 | and then Expr_Value (HB1) <= Expr_Value (HB2)); | |
996ae0b0 RK |
6555 | end if; |
6556 | end; | |
6557 | end if; | |
6558 | ||
437f8c1e AC |
6559 | -- Access types |
6560 | ||
996ae0b0 | 6561 | elsif Is_Access_Type (T1) then |
304757d2 AC |
6562 | return |
6563 | (not Is_Constrained (T2) | |
6564 | or else Subtypes_Statically_Match | |
6565 | (Designated_Type (T1), Designated_Type (T2))) | |
26df19ce AC |
6566 | and then not (Can_Never_Be_Null (T2) |
6567 | and then not Can_Never_Be_Null (T1)); | |
437f8c1e | 6568 | |
55fae09d ES |
6569 | -- Private types without discriminants can be handled specially. |
6570 | -- Predicate matching has been checked above. | |
6571 | ||
6572 | elsif Is_Private_Type (T1) | |
6573 | and then not Has_Discriminants (T1) | |
6574 | then | |
6575 | return not Has_Discriminants (T2); | |
6576 | ||
437f8c1e | 6577 | -- All other cases |
996ae0b0 RK |
6578 | |
6579 | else | |
304757d2 AC |
6580 | return |
6581 | (Is_Composite_Type (T1) and then not Is_Constrained (T2)) | |
6582 | or else Subtypes_Statically_Match | |
6583 | (T1, T2, Formal_Derived_Matching); | |
996ae0b0 RK |
6584 | end if; |
6585 | end Subtypes_Statically_Compatible; | |
6586 | ||
6587 | ------------------------------- | |
6588 | -- Subtypes_Statically_Match -- | |
6589 | ------------------------------- | |
6590 | ||
6591 | -- Subtypes statically match if they have statically matching constraints | |
6592 | -- (RM 4.9.1(2)). Constraints statically match if there are none, or if | |
6593 | -- they are the same identical constraint, or if they are static and the | |
6594 | -- values match (RM 4.9.1(1)). | |
6595 | ||
a0367005 | 6596 | -- In addition, in GNAT, the object size (Esize) values of the types must |
c97d7285 AC |
6597 | -- match if they are set (unless checking an actual for a formal derived |
6598 | -- type). The use of 'Object_Size can cause this to be false even if the | |
c846eedd | 6599 | -- types would otherwise match in the Ada 95 RM sense, but this deviation |
81e68a19 | 6600 | -- is adopted by AI12-059 which introduces Object_Size in Ada 2022. |
c97d7285 AC |
6601 | |
6602 | function Subtypes_Statically_Match | |
6603 | (T1 : Entity_Id; | |
6604 | T2 : Entity_Id; | |
6605 | Formal_Derived_Matching : Boolean := False) return Boolean | |
6606 | is | |
996ae0b0 RK |
6607 | begin |
6608 | -- A type always statically matches itself | |
6609 | ||
6610 | if T1 = T2 then | |
6611 | return True; | |
6612 | ||
c97d7285 AC |
6613 | -- No match if sizes different (from use of 'Object_Size). This test |
6614 | -- is excluded if Formal_Derived_Matching is True, as the base types | |
f8f50235 | 6615 | -- can be different in that case and typically have different sizes. |
a0367005 | 6616 | |
c97d7285 | 6617 | elsif not Formal_Derived_Matching |
ebb6b0bd AC |
6618 | and then Known_Static_Esize (T1) |
6619 | and then Known_Static_Esize (T2) | |
a0367005 RD |
6620 | and then Esize (T1) /= Esize (T2) |
6621 | then | |
6622 | return False; | |
6623 | ||
308aab0b AC |
6624 | -- No match if predicates do not match |
6625 | ||
7f568bfa | 6626 | elsif not Predicates_Match (T1, T2) then |
308aab0b AC |
6627 | return False; |
6628 | ||
996ae0b0 RK |
6629 | -- Scalar types |
6630 | ||
6631 | elsif Is_Scalar_Type (T1) then | |
6632 | ||
6633 | -- Base types must be the same | |
6634 | ||
6635 | if Base_Type (T1) /= Base_Type (T2) then | |
6636 | return False; | |
6637 | end if; | |
6638 | ||
6639 | -- A constrained numeric subtype never matches an unconstrained | |
6640 | -- subtype, i.e. both types must be constrained or unconstrained. | |
6641 | ||
305caf42 AC |
6642 | -- To understand the requirement for this test, see RM 4.9.1(1). |
6643 | -- As is made clear in RM 3.5.4(11), type Integer, for example is | |
6644 | -- a constrained subtype with constraint bounds matching the bounds | |
6645 | -- of its corresponding unconstrained base type. In this situation, | |
6646 | -- Integer and Integer'Base do not statically match, even though | |
6647 | -- they have the same bounds. | |
996ae0b0 | 6648 | |
22cb89b5 AC |
6649 | -- We only apply this test to types in Standard and types that appear |
6650 | -- in user programs. That way, we do not have to be too careful about | |
6651 | -- setting Is_Constrained right for Itypes. | |
996ae0b0 RK |
6652 | |
6653 | if Is_Numeric_Type (T1) | |
6654 | and then (Is_Constrained (T1) /= Is_Constrained (T2)) | |
6655 | and then (Scope (T1) = Standard_Standard | |
6656 | or else Comes_From_Source (T1)) | |
6657 | and then (Scope (T2) = Standard_Standard | |
6658 | or else Comes_From_Source (T2)) | |
6659 | then | |
6660 | return False; | |
82c80734 | 6661 | |
22cb89b5 AC |
6662 | -- A generic scalar type does not statically match its base type |
6663 | -- (AI-311). In this case we make sure that the formals, which are | |
6664 | -- first subtypes of their bases, are constrained. | |
82c80734 RD |
6665 | |
6666 | elsif Is_Generic_Type (T1) | |
6667 | and then Is_Generic_Type (T2) | |
6668 | and then (Is_Constrained (T1) /= Is_Constrained (T2)) | |
6669 | then | |
6670 | return False; | |
996ae0b0 RK |
6671 | end if; |
6672 | ||
22cb89b5 AC |
6673 | -- If there was an error in either range, then just assume the types |
6674 | -- statically match to avoid further junk errors. | |
996ae0b0 | 6675 | |
199c6a10 AC |
6676 | if No (Scalar_Range (T1)) or else No (Scalar_Range (T2)) |
6677 | or else Error_Posted (Scalar_Range (T1)) | |
6678 | or else Error_Posted (Scalar_Range (T2)) | |
996ae0b0 RK |
6679 | then |
6680 | return True; | |
6681 | end if; | |
6682 | ||
308aab0b | 6683 | -- Otherwise both types have bounds that can be compared |
996ae0b0 RK |
6684 | |
6685 | declare | |
6686 | LB1 : constant Node_Id := Type_Low_Bound (T1); | |
6687 | HB1 : constant Node_Id := Type_High_Bound (T1); | |
6688 | LB2 : constant Node_Id := Type_Low_Bound (T2); | |
6689 | HB2 : constant Node_Id := Type_High_Bound (T2); | |
6690 | ||
6691 | begin | |
308aab0b | 6692 | -- If the bounds are the same tree node, then match (common case) |
996ae0b0 RK |
6693 | |
6694 | if LB1 = LB2 and then HB1 = HB2 then | |
308aab0b | 6695 | return True; |
996ae0b0 RK |
6696 | |
6697 | -- Otherwise bounds must be static and identical value | |
6698 | ||
6699 | else | |
edab6088 | 6700 | if not Is_OK_Static_Subtype (T1) |
304757d2 AC |
6701 | or else |
6702 | not Is_OK_Static_Subtype (T2) | |
996ae0b0 RK |
6703 | then |
6704 | return False; | |
6705 | ||
996ae0b0 RK |
6706 | elsif Is_Real_Type (T1) then |
6707 | return | |
304757d2 | 6708 | Expr_Value_R (LB1) = Expr_Value_R (LB2) |
996ae0b0 | 6709 | and then |
304757d2 | 6710 | Expr_Value_R (HB1) = Expr_Value_R (HB2); |
996ae0b0 RK |
6711 | |
6712 | else | |
6713 | return | |
6714 | Expr_Value (LB1) = Expr_Value (LB2) | |
6715 | and then | |
6716 | Expr_Value (HB1) = Expr_Value (HB2); | |
6717 | end if; | |
6718 | end if; | |
6719 | end; | |
6720 | ||
6721 | -- Type with discriminants | |
6722 | ||
6723 | elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then | |
6eaf4095 | 6724 | |
009668e3 JM |
6725 | -- Handle derivations of private subtypes. For example S1 statically |
6726 | -- matches the full view of T1 in the following example: | |
6727 | ||
6728 | -- type T1(<>) is new Root with private; | |
6729 | -- subtype S1 is new T1; | |
6730 | -- overriding proc P1 (P : S1); | |
6731 | -- private | |
6732 | -- type T1 (D : Disc) is new Root with ... | |
6733 | ||
6734 | if Ekind (T2) = E_Record_Subtype_With_Private | |
6735 | and then not Has_Discriminants (T2) | |
6736 | and then Partial_View_Has_Unknown_Discr (T1) | |
6737 | and then Etype (T2) = T1 | |
6738 | then | |
6739 | return True; | |
6740 | ||
6741 | elsif Ekind (T1) = E_Record_Subtype_With_Private | |
6742 | and then not Has_Discriminants (T1) | |
6743 | and then Partial_View_Has_Unknown_Discr (T2) | |
6744 | and then Etype (T1) = T2 | |
6745 | then | |
6746 | return True; | |
6747 | ||
c2bf339e GD |
6748 | -- Because of view exchanges in multiple instantiations, conformance |
6749 | -- checking might try to match a partial view of a type with no | |
6750 | -- discriminants with a full view that has defaulted discriminants. | |
6751 | -- In such a case, use the discriminant constraint of the full view, | |
6752 | -- which must exist because we know that the two subtypes have the | |
6753 | -- same base type. | |
6eaf4095 | 6754 | |
009668e3 | 6755 | elsif Has_Discriminants (T1) /= Has_Discriminants (T2) then |
7f078d5b | 6756 | if In_Instance then |
c2bf339e GD |
6757 | if Is_Private_Type (T2) |
6758 | and then Present (Full_View (T2)) | |
6759 | and then Has_Discriminants (Full_View (T2)) | |
6760 | then | |
6761 | return Subtypes_Statically_Match (T1, Full_View (T2)); | |
6762 | ||
6763 | elsif Is_Private_Type (T1) | |
6764 | and then Present (Full_View (T1)) | |
6765 | and then Has_Discriminants (Full_View (T1)) | |
6766 | then | |
6767 | return Subtypes_Statically_Match (Full_View (T1), T2); | |
6768 | ||
6769 | else | |
6770 | return False; | |
6771 | end if; | |
6eaf4095 ES |
6772 | else |
6773 | return False; | |
6774 | end if; | |
996ae0b0 RK |
6775 | end if; |
6776 | ||
6777 | declare | |
f6fd9533 GD |
6778 | |
6779 | function Original_Discriminant_Constraint | |
6780 | (Typ : Entity_Id) return Elist_Id; | |
6781 | -- Returns Typ's discriminant constraint, or if the constraint | |
6782 | -- is inherited from an ancestor type, then climbs the parent | |
6783 | -- types to locate and return the constraint farthest up the | |
6784 | -- parent chain that Typ's constraint is ultimately inherited | |
6785 | -- from (stopping before a parent that doesn't impose a constraint | |
6786 | -- or a parent that has new discriminants). This ensures a proper | |
6787 | -- result from the equality comparison of Elist_Ids below (as | |
6788 | -- otherwise, derived types that inherit constraints may appear | |
6789 | -- to be unequal, because each level of derivation can have its | |
6790 | -- own copy of the constraint). | |
6791 | ||
6792 | function Original_Discriminant_Constraint | |
6793 | (Typ : Entity_Id) return Elist_Id | |
6794 | is | |
6795 | begin | |
6796 | if not Has_Discriminants (Typ) then | |
6797 | return No_Elist; | |
6798 | ||
6799 | -- If Typ is not a derived type, then directly return the | |
6800 | -- its constraint. | |
6801 | ||
6802 | elsif not Is_Derived_Type (Typ) then | |
6803 | return Discriminant_Constraint (Typ); | |
6804 | ||
6805 | -- If the parent type doesn't have discriminants, doesn't | |
6806 | -- have a constraint, or has new discriminants, then stop | |
6807 | -- and return Typ's constraint. | |
6808 | ||
6809 | elsif not Has_Discriminants (Etype (Typ)) | |
6810 | ||
6811 | -- No constraint on the parent type | |
6812 | ||
6813 | or else not Present (Discriminant_Constraint (Etype (Typ))) | |
6814 | or else Is_Empty_Elmt_List | |
6815 | (Discriminant_Constraint (Etype (Typ))) | |
6816 | ||
6817 | -- The parent type defines new discriminants | |
6818 | ||
6819 | or else | |
6820 | (Is_Base_Type (Etype (Typ)) | |
6821 | and then Present (Discriminant_Specifications | |
6822 | (Parent (Etype (Typ))))) | |
6823 | then | |
6824 | return Discriminant_Constraint (Typ); | |
6825 | ||
6826 | -- Otherwise, make a recursive call on the parent type | |
6827 | ||
6828 | else | |
6829 | return Original_Discriminant_Constraint (Etype (Typ)); | |
6830 | end if; | |
6831 | end Original_Discriminant_Constraint; | |
6832 | ||
6833 | -- Local variables | |
6834 | ||
6835 | DL1 : constant Elist_Id := Original_Discriminant_Constraint (T1); | |
6836 | DL2 : constant Elist_Id := Original_Discriminant_Constraint (T2); | |
996ae0b0 | 6837 | |
13f34a3f RD |
6838 | DA1 : Elmt_Id; |
6839 | DA2 : Elmt_Id; | |
996ae0b0 RK |
6840 | |
6841 | begin | |
6842 | if DL1 = DL2 then | |
6843 | return True; | |
996ae0b0 RK |
6844 | elsif Is_Constrained (T1) /= Is_Constrained (T2) then |
6845 | return False; | |
6846 | end if; | |
6847 | ||
13f34a3f | 6848 | -- Now loop through the discriminant constraints |
996ae0b0 | 6849 | |
13f34a3f RD |
6850 | -- Note: the guard here seems necessary, since it is possible at |
6851 | -- least for DL1 to be No_Elist. Not clear this is reasonable ??? | |
996ae0b0 | 6852 | |
13f34a3f RD |
6853 | if Present (DL1) and then Present (DL2) then |
6854 | DA1 := First_Elmt (DL1); | |
6855 | DA2 := First_Elmt (DL2); | |
6856 | while Present (DA1) loop | |
6857 | declare | |
6858 | Expr1 : constant Node_Id := Node (DA1); | |
6859 | Expr2 : constant Node_Id := Node (DA2); | |
996ae0b0 | 6860 | |
13f34a3f | 6861 | begin |
edab6088 RD |
6862 | if not Is_OK_Static_Expression (Expr1) |
6863 | or else not Is_OK_Static_Expression (Expr2) | |
13f34a3f RD |
6864 | then |
6865 | return False; | |
996ae0b0 | 6866 | |
1e3c434f | 6867 | -- If either expression raised a Constraint_Error, |
13f34a3f RD |
6868 | -- consider the expressions as matching, since this |
6869 | -- helps to prevent cascading errors. | |
6870 | ||
6871 | elsif Raises_Constraint_Error (Expr1) | |
6872 | or else Raises_Constraint_Error (Expr2) | |
6873 | then | |
6874 | null; | |
6875 | ||
6876 | elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then | |
6877 | return False; | |
6878 | end if; | |
6879 | end; | |
996ae0b0 | 6880 | |
13f34a3f RD |
6881 | Next_Elmt (DA1); |
6882 | Next_Elmt (DA2); | |
6883 | end loop; | |
6884 | end if; | |
996ae0b0 RK |
6885 | end; |
6886 | ||
6887 | return True; | |
6888 | ||
22cb89b5 | 6889 | -- A definite type does not match an indefinite or classwide type. |
0356699b RD |
6890 | -- However, a generic type with unknown discriminants may be |
6891 | -- instantiated with a type with no discriminants, and conformance | |
22cb89b5 AC |
6892 | -- checking on an inherited operation may compare the actual with the |
6893 | -- subtype that renames it in the instance. | |
996ae0b0 | 6894 | |
80298c3b | 6895 | elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2) |
996ae0b0 | 6896 | then |
7a3f77d2 AC |
6897 | return |
6898 | Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2); | |
996ae0b0 RK |
6899 | |
6900 | -- Array type | |
6901 | ||
6902 | elsif Is_Array_Type (T1) then | |
6903 | ||
22cb89b5 | 6904 | -- If either subtype is unconstrained then both must be, and if both |
308e6f3a | 6905 | -- are unconstrained then no further checking is needed. |
996ae0b0 RK |
6906 | |
6907 | if not Is_Constrained (T1) or else not Is_Constrained (T2) then | |
6908 | return not (Is_Constrained (T1) or else Is_Constrained (T2)); | |
6909 | end if; | |
6910 | ||
22cb89b5 AC |
6911 | -- Both subtypes are constrained, so check that the index subtypes |
6912 | -- statically match. | |
996ae0b0 RK |
6913 | |
6914 | declare | |
6915 | Index1 : Node_Id := First_Index (T1); | |
6916 | Index2 : Node_Id := First_Index (T2); | |
6917 | ||
6918 | begin | |
6919 | while Present (Index1) loop | |
6920 | if not | |
6921 | Subtypes_Statically_Match (Etype (Index1), Etype (Index2)) | |
6922 | then | |
6923 | return False; | |
6924 | end if; | |
6925 | ||
6926 | Next_Index (Index1); | |
6927 | Next_Index (Index2); | |
6928 | end loop; | |
6929 | ||
6930 | return True; | |
6931 | end; | |
6932 | ||
6933 | elsif Is_Access_Type (T1) then | |
b5bd964f ES |
6934 | if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then |
6935 | return False; | |
6936 | ||
4a08c95c AC |
6937 | elsif Ekind (T1) in E_Access_Subprogram_Type |
6938 | | E_Anonymous_Access_Subprogram_Type | |
7a3f77d2 | 6939 | then |
b5bd964f ES |
6940 | return |
6941 | Subtype_Conformant | |
6942 | (Designated_Type (T1), | |
bb98fe75 | 6943 | Designated_Type (T2)); |
b5bd964f ES |
6944 | else |
6945 | return | |
6946 | Subtypes_Statically_Match | |
6947 | (Designated_Type (T1), | |
6948 | Designated_Type (T2)) | |
6949 | and then Is_Access_Constant (T1) = Is_Access_Constant (T2); | |
6950 | end if; | |
996ae0b0 RK |
6951 | |
6952 | -- All other types definitely match | |
6953 | ||
6954 | else | |
6955 | return True; | |
6956 | end if; | |
6957 | end Subtypes_Statically_Match; | |
6958 | ||
6959 | ---------- | |
6960 | -- Test -- | |
6961 | ---------- | |
6962 | ||
6963 | function Test (Cond : Boolean) return Uint is | |
6964 | begin | |
6965 | if Cond then | |
6966 | return Uint_1; | |
6967 | else | |
6968 | return Uint_0; | |
6969 | end if; | |
6970 | end Test; | |
6971 | ||
634a926b AC |
6972 | --------------------- |
6973 | -- Test_Comparison -- | |
6974 | --------------------- | |
6975 | ||
6976 | procedure Test_Comparison | |
6977 | (Op : Node_Id; | |
6978 | Assume_Valid : Boolean; | |
6979 | True_Result : out Boolean; | |
6980 | False_Result : out Boolean) | |
6981 | is | |
6982 | Left : constant Node_Id := Left_Opnd (Op); | |
6983 | Left_Typ : constant Entity_Id := Etype (Left); | |
6984 | Orig_Op : constant Node_Id := Original_Node (Op); | |
6985 | ||
6986 | procedure Replacement_Warning (Msg : String); | |
2da8c8e2 | 6987 | -- Emit a warning on a comparison that can be replaced by '=' |
634a926b AC |
6988 | |
6989 | ------------------------- | |
6990 | -- Replacement_Warning -- | |
6991 | ------------------------- | |
6992 | ||
6993 | procedure Replacement_Warning (Msg : String) is | |
6994 | begin | |
6995 | if Constant_Condition_Warnings | |
6996 | and then Comes_From_Source (Orig_Op) | |
6997 | and then Is_Integer_Type (Left_Typ) | |
6998 | and then not Error_Posted (Op) | |
6999 | and then not Has_Warnings_Off (Left_Typ) | |
7000 | and then not In_Instance | |
7001 | then | |
7002 | Error_Msg_N (Msg, Op); | |
7003 | end if; | |
7004 | end Replacement_Warning; | |
7005 | ||
7006 | -- Local variables | |
7007 | ||
7008 | Res : constant Compare_Result := | |
7009 | Compile_Time_Compare (Left, Right_Opnd (Op), Assume_Valid); | |
7010 | ||
7011 | -- Start of processing for Test_Comparison | |
7012 | ||
7013 | begin | |
7014 | case N_Op_Compare (Nkind (Op)) is | |
7015 | when N_Op_Eq => | |
7016 | True_Result := Res = EQ; | |
7017 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
7018 | ||
7019 | when N_Op_Ge => | |
7020 | True_Result := Res in Compare_GE; | |
7021 | False_Result := Res = LT; | |
7022 | ||
7023 | if Res = LE and then Nkind (Orig_Op) = N_Op_Ge then | |
7024 | Replacement_Warning | |
7025 | ("can never be greater than, could replace by ""'=""?c?"); | |
7026 | end if; | |
7027 | ||
7028 | when N_Op_Gt => | |
7029 | True_Result := Res = GT; | |
7030 | False_Result := Res in Compare_LE; | |
7031 | ||
7032 | when N_Op_Le => | |
7033 | True_Result := Res in Compare_LE; | |
7034 | False_Result := Res = GT; | |
7035 | ||
7036 | if Res = GE and then Nkind (Orig_Op) = N_Op_Le then | |
7037 | Replacement_Warning | |
7038 | ("can never be less than, could replace by ""'=""?c?"); | |
7039 | end if; | |
7040 | ||
7041 | when N_Op_Lt => | |
7042 | True_Result := Res = LT; | |
7043 | False_Result := Res in Compare_GE; | |
7044 | ||
7045 | when N_Op_Ne => | |
7046 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
7047 | False_Result := Res = EQ; | |
7048 | end case; | |
7049 | end Test_Comparison; | |
7050 | ||
996ae0b0 RK |
7051 | --------------------------------- |
7052 | -- Test_Expression_Is_Foldable -- | |
7053 | --------------------------------- | |
7054 | ||
7055 | -- One operand case | |
7056 | ||
7057 | procedure Test_Expression_Is_Foldable | |
7058 | (N : Node_Id; | |
7059 | Op1 : Node_Id; | |
7060 | Stat : out Boolean; | |
7061 | Fold : out Boolean) | |
7062 | is | |
7063 | begin | |
7064 | Stat := False; | |
0356699b RD |
7065 | Fold := False; |
7066 | ||
7067 | if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then | |
7068 | return; | |
7069 | end if; | |
996ae0b0 RK |
7070 | |
7071 | -- If operand is Any_Type, just propagate to result and do not | |
7072 | -- try to fold, this prevents cascaded errors. | |
7073 | ||
7074 | if Etype (Op1) = Any_Type then | |
7075 | Set_Etype (N, Any_Type); | |
996ae0b0 RK |
7076 | return; |
7077 | ||
1e3c434f BD |
7078 | -- If operand raises Constraint_Error, then replace node N with the |
7079 | -- raise Constraint_Error node, and we are obviously not foldable. | |
996ae0b0 RK |
7080 | -- Note that this replacement inherits the Is_Static_Expression flag |
7081 | -- from the operand. | |
7082 | ||
7083 | elsif Raises_Constraint_Error (Op1) then | |
7084 | Rewrite_In_Raise_CE (N, Op1); | |
996ae0b0 RK |
7085 | return; |
7086 | ||
7087 | -- If the operand is not static, then the result is not static, and | |
7088 | -- all we have to do is to check the operand since it is now known | |
7089 | -- to appear in a non-static context. | |
7090 | ||
7091 | elsif not Is_Static_Expression (Op1) then | |
7092 | Check_Non_Static_Context (Op1); | |
7093 | Fold := Compile_Time_Known_Value (Op1); | |
7094 | return; | |
7095 | ||
7096 | -- An expression of a formal modular type is not foldable because | |
7097 | -- the modulus is unknown. | |
7098 | ||
7099 | elsif Is_Modular_Integer_Type (Etype (Op1)) | |
7100 | and then Is_Generic_Type (Etype (Op1)) | |
7101 | then | |
7102 | Check_Non_Static_Context (Op1); | |
996ae0b0 RK |
7103 | return; |
7104 | ||
7105 | -- Here we have the case of an operand whose type is OK, which is | |
1e3c434f | 7106 | -- static, and which does not raise Constraint_Error, we can fold. |
996ae0b0 RK |
7107 | |
7108 | else | |
7109 | Set_Is_Static_Expression (N); | |
7110 | Fold := True; | |
7111 | Stat := True; | |
7112 | end if; | |
7113 | end Test_Expression_Is_Foldable; | |
7114 | ||
7115 | -- Two operand case | |
7116 | ||
7117 | procedure Test_Expression_Is_Foldable | |
6c3c671e AC |
7118 | (N : Node_Id; |
7119 | Op1 : Node_Id; | |
7120 | Op2 : Node_Id; | |
7121 | Stat : out Boolean; | |
7122 | Fold : out Boolean; | |
7123 | CRT_Safe : Boolean := False) | |
996ae0b0 RK |
7124 | is |
7125 | Rstat : constant Boolean := Is_Static_Expression (Op1) | |
80298c3b AC |
7126 | and then |
7127 | Is_Static_Expression (Op2); | |
996ae0b0 RK |
7128 | |
7129 | begin | |
7130 | Stat := False; | |
0356699b RD |
7131 | Fold := False; |
7132 | ||
4a28b181 AC |
7133 | -- Inhibit folding if -gnatd.f flag set |
7134 | ||
0356699b RD |
7135 | if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then |
7136 | return; | |
7137 | end if; | |
996ae0b0 RK |
7138 | |
7139 | -- If either operand is Any_Type, just propagate to result and | |
7140 | -- do not try to fold, this prevents cascaded errors. | |
7141 | ||
7142 | if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then | |
7143 | Set_Etype (N, Any_Type); | |
996ae0b0 RK |
7144 | return; |
7145 | ||
1e3c434f | 7146 | -- If left operand raises Constraint_Error, then replace node N with the |
22cb89b5 | 7147 | -- Raise_Constraint_Error node, and we are obviously not foldable. |
996ae0b0 RK |
7148 | -- Is_Static_Expression is set from the two operands in the normal way, |
7149 | -- and we check the right operand if it is in a non-static context. | |
7150 | ||
7151 | elsif Raises_Constraint_Error (Op1) then | |
7152 | if not Rstat then | |
7153 | Check_Non_Static_Context (Op2); | |
7154 | end if; | |
7155 | ||
7156 | Rewrite_In_Raise_CE (N, Op1); | |
7157 | Set_Is_Static_Expression (N, Rstat); | |
996ae0b0 RK |
7158 | return; |
7159 | ||
22cb89b5 AC |
7160 | -- Similar processing for the case of the right operand. Note that we |
7161 | -- don't use this routine for the short-circuit case, so we do not have | |
7162 | -- to worry about that special case here. | |
996ae0b0 RK |
7163 | |
7164 | elsif Raises_Constraint_Error (Op2) then | |
7165 | if not Rstat then | |
7166 | Check_Non_Static_Context (Op1); | |
7167 | end if; | |
7168 | ||
7169 | Rewrite_In_Raise_CE (N, Op2); | |
7170 | Set_Is_Static_Expression (N, Rstat); | |
996ae0b0 RK |
7171 | return; |
7172 | ||
82c80734 | 7173 | -- Exclude expressions of a generic modular type, as above |
996ae0b0 RK |
7174 | |
7175 | elsif Is_Modular_Integer_Type (Etype (Op1)) | |
7176 | and then Is_Generic_Type (Etype (Op1)) | |
7177 | then | |
7178 | Check_Non_Static_Context (Op1); | |
996ae0b0 RK |
7179 | return; |
7180 | ||
7181 | -- If result is not static, then check non-static contexts on operands | |
22cb89b5 | 7182 | -- since one of them may be static and the other one may not be static. |
996ae0b0 RK |
7183 | |
7184 | elsif not Rstat then | |
7185 | Check_Non_Static_Context (Op1); | |
7186 | Check_Non_Static_Context (Op2); | |
6c3c671e AC |
7187 | |
7188 | if CRT_Safe then | |
7189 | Fold := CRT_Safe_Compile_Time_Known_Value (Op1) | |
7190 | and then CRT_Safe_Compile_Time_Known_Value (Op2); | |
7191 | else | |
7192 | Fold := Compile_Time_Known_Value (Op1) | |
7193 | and then Compile_Time_Known_Value (Op2); | |
7194 | end if; | |
7195 | ||
996ae0b0 RK |
7196 | return; |
7197 | ||
22cb89b5 | 7198 | -- Else result is static and foldable. Both operands are static, and |
1e3c434f | 7199 | -- neither raises Constraint_Error, so we can definitely fold. |
996ae0b0 RK |
7200 | |
7201 | else | |
7202 | Set_Is_Static_Expression (N); | |
7203 | Fold := True; | |
7204 | Stat := True; | |
7205 | return; | |
7206 | end if; | |
7207 | end Test_Expression_Is_Foldable; | |
7208 | ||
305caf42 AC |
7209 | ------------------- |
7210 | -- Test_In_Range -- | |
7211 | ------------------- | |
7212 | ||
7213 | function Test_In_Range | |
7214 | (N : Node_Id; | |
7215 | Typ : Entity_Id; | |
7216 | Assume_Valid : Boolean; | |
7217 | Fixed_Int : Boolean; | |
7218 | Int_Real : Boolean) return Range_Membership | |
7219 | is | |
7220 | Val : Uint; | |
7221 | Valr : Ureal; | |
7222 | ||
7223 | pragma Warnings (Off, Assume_Valid); | |
7224 | -- For now Assume_Valid is unreferenced since the current implementation | |
d3bbfc59 | 7225 | -- always returns Unknown if N is not a compile-time-known value, but we |
305caf42 AC |
7226 | -- keep the parameter to allow for future enhancements in which we try |
7227 | -- to get the information in the variable case as well. | |
7228 | ||
7229 | begin | |
8bef7ba9 AC |
7230 | -- If an error was posted on expression, then return Unknown, we do not |
7231 | -- want cascaded errors based on some false analysis of a junk node. | |
7232 | ||
7233 | if Error_Posted (N) then | |
7234 | return Unknown; | |
7235 | ||
1e3c434f | 7236 | -- Expression that raises Constraint_Error is an odd case. We certainly |
7b536495 AC |
7237 | -- do not want to consider it to be in range. It might make sense to |
7238 | -- consider it always out of range, but this causes incorrect error | |
7239 | -- messages about static expressions out of range. So we just return | |
7240 | -- Unknown, which is always safe. | |
7241 | ||
8bef7ba9 | 7242 | elsif Raises_Constraint_Error (N) then |
7b536495 AC |
7243 | return Unknown; |
7244 | ||
305caf42 AC |
7245 | -- Universal types have no range limits, so always in range |
7246 | ||
785d39ac | 7247 | elsif Is_Universal_Numeric_Type (Typ) then |
305caf42 AC |
7248 | return In_Range; |
7249 | ||
7250 | -- Never known if not scalar type. Don't know if this can actually | |
a90bd866 | 7251 | -- happen, but our spec allows it, so we must check. |
305caf42 AC |
7252 | |
7253 | elsif not Is_Scalar_Type (Typ) then | |
7254 | return Unknown; | |
7255 | ||
7256 | -- Never known if this is a generic type, since the bounds of generic | |
7257 | -- types are junk. Note that if we only checked for static expressions | |
d3bbfc59 | 7258 | -- (instead of compile-time-known values) below, we would not need this |
305caf42 AC |
7259 | -- check, because values of a generic type can never be static, but they |
7260 | -- can be known at compile time. | |
7261 | ||
7262 | elsif Is_Generic_Type (Typ) then | |
7263 | return Unknown; | |
7264 | ||
7b536495 AC |
7265 | -- Case of a known compile time value, where we can check if it is in |
7266 | -- the bounds of the given type. | |
305caf42 | 7267 | |
7b536495 | 7268 | elsif Compile_Time_Known_Value (N) then |
305caf42 AC |
7269 | declare |
7270 | Lo : Node_Id; | |
7271 | Hi : Node_Id; | |
7272 | ||
7273 | LB_Known : Boolean; | |
7274 | HB_Known : Boolean; | |
7275 | ||
7276 | begin | |
7277 | Lo := Type_Low_Bound (Typ); | |
7278 | Hi := Type_High_Bound (Typ); | |
7279 | ||
7280 | LB_Known := Compile_Time_Known_Value (Lo); | |
7281 | HB_Known := Compile_Time_Known_Value (Hi); | |
7282 | ||
7283 | -- Fixed point types should be considered as such only if flag | |
7284 | -- Fixed_Int is set to False. | |
7285 | ||
7286 | if Is_Floating_Point_Type (Typ) | |
7287 | or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int) | |
7288 | or else Int_Real | |
7289 | then | |
7290 | Valr := Expr_Value_R (N); | |
7291 | ||
7292 | if LB_Known and HB_Known then | |
7293 | if Valr >= Expr_Value_R (Lo) | |
7294 | and then | |
7295 | Valr <= Expr_Value_R (Hi) | |
7296 | then | |
7297 | return In_Range; | |
7298 | else | |
7299 | return Out_Of_Range; | |
7300 | end if; | |
7301 | ||
7302 | elsif (LB_Known and then Valr < Expr_Value_R (Lo)) | |
7303 | or else | |
7304 | (HB_Known and then Valr > Expr_Value_R (Hi)) | |
7305 | then | |
7306 | return Out_Of_Range; | |
7307 | ||
7308 | else | |
7309 | return Unknown; | |
7310 | end if; | |
7311 | ||
7312 | else | |
7313 | Val := Expr_Value (N); | |
7314 | ||
7315 | if LB_Known and HB_Known then | |
80298c3b | 7316 | if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi) |
305caf42 AC |
7317 | then |
7318 | return In_Range; | |
7319 | else | |
7320 | return Out_Of_Range; | |
7321 | end if; | |
7322 | ||
7323 | elsif (LB_Known and then Val < Expr_Value (Lo)) | |
7324 | or else | |
7325 | (HB_Known and then Val > Expr_Value (Hi)) | |
7326 | then | |
7327 | return Out_Of_Range; | |
7328 | ||
7329 | else | |
7330 | return Unknown; | |
7331 | end if; | |
7332 | end if; | |
7333 | end; | |
7b536495 AC |
7334 | |
7335 | -- Here for value not known at compile time. Case of expression subtype | |
7336 | -- is Typ or is a subtype of Typ, and we can assume expression is valid. | |
7337 | -- In this case we know it is in range without knowing its value. | |
7338 | ||
7339 | elsif Assume_Valid | |
7340 | and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ)) | |
7341 | then | |
7342 | return In_Range; | |
7343 | ||
6c56d9b8 AC |
7344 | -- Another special case. For signed integer types, if the target type |
7345 | -- has Is_Known_Valid set, and the source type does not have a larger | |
7346 | -- size, then the source value must be in range. We exclude biased | |
7347 | -- types, because they bizarrely can generate out of range values. | |
7348 | ||
7349 | elsif Is_Signed_Integer_Type (Etype (N)) | |
7350 | and then Is_Known_Valid (Typ) | |
7351 | and then Esize (Etype (N)) <= Esize (Typ) | |
7352 | and then not Has_Biased_Representation (Etype (N)) | |
7353 | then | |
7354 | return In_Range; | |
7355 | ||
7b536495 AC |
7356 | -- For all other cases, result is unknown |
7357 | ||
7358 | else | |
7359 | return Unknown; | |
305caf42 AC |
7360 | end if; |
7361 | end Test_In_Range; | |
7362 | ||
996ae0b0 RK |
7363 | -------------- |
7364 | -- To_Bits -- | |
7365 | -------------- | |
7366 | ||
7367 | procedure To_Bits (U : Uint; B : out Bits) is | |
7368 | begin | |
7369 | for J in 0 .. B'Last loop | |
7370 | B (J) := (U / (2 ** J)) mod 2 /= 0; | |
7371 | end loop; | |
7372 | end To_Bits; | |
7373 | ||
fbf5a39b AC |
7374 | -------------------- |
7375 | -- Why_Not_Static -- | |
7376 | -------------------- | |
7377 | ||
7378 | procedure Why_Not_Static (Expr : Node_Id) is | |
66c19cd4 AC |
7379 | N : constant Node_Id := Original_Node (Expr); |
7380 | Typ : Entity_Id := Empty; | |
fbf5a39b | 7381 | E : Entity_Id; |
edab6088 RD |
7382 | Alt : Node_Id; |
7383 | Exp : Node_Id; | |
fbf5a39b AC |
7384 | |
7385 | procedure Why_Not_Static_List (L : List_Id); | |
22cb89b5 AC |
7386 | -- A version that can be called on a list of expressions. Finds all |
7387 | -- non-static violations in any element of the list. | |
fbf5a39b AC |
7388 | |
7389 | ------------------------- | |
7390 | -- Why_Not_Static_List -- | |
7391 | ------------------------- | |
7392 | ||
7393 | procedure Why_Not_Static_List (L : List_Id) is | |
7394 | N : Node_Id; | |
fbf5a39b AC |
7395 | begin |
7396 | if Is_Non_Empty_List (L) then | |
7397 | N := First (L); | |
7398 | while Present (N) loop | |
7399 | Why_Not_Static (N); | |
7400 | Next (N); | |
7401 | end loop; | |
7402 | end if; | |
7403 | end Why_Not_Static_List; | |
7404 | ||
7405 | -- Start of processing for Why_Not_Static | |
7406 | ||
7407 | begin | |
fbf5a39b AC |
7408 | -- Ignore call on error or empty node |
7409 | ||
7410 | if No (Expr) or else Nkind (Expr) = N_Error then | |
7411 | return; | |
7412 | end if; | |
7413 | ||
7414 | -- Preprocessing for sub expressions | |
7415 | ||
7416 | if Nkind (Expr) in N_Subexpr then | |
7417 | ||
7418 | -- Nothing to do if expression is static | |
7419 | ||
7420 | if Is_OK_Static_Expression (Expr) then | |
7421 | return; | |
7422 | end if; | |
7423 | ||
1e3c434f | 7424 | -- Test for Constraint_Error raised |
fbf5a39b AC |
7425 | |
7426 | if Raises_Constraint_Error (Expr) then | |
edab6088 RD |
7427 | |
7428 | -- Special case membership to find out which piece to flag | |
7429 | ||
7430 | if Nkind (N) in N_Membership_Test then | |
7431 | if Raises_Constraint_Error (Left_Opnd (N)) then | |
7432 | Why_Not_Static (Left_Opnd (N)); | |
7433 | return; | |
7434 | ||
7435 | elsif Present (Right_Opnd (N)) | |
7436 | and then Raises_Constraint_Error (Right_Opnd (N)) | |
7437 | then | |
7438 | Why_Not_Static (Right_Opnd (N)); | |
7439 | return; | |
7440 | ||
7441 | else | |
7442 | pragma Assert (Present (Alternatives (N))); | |
7443 | ||
7444 | Alt := First (Alternatives (N)); | |
7445 | while Present (Alt) loop | |
7446 | if Raises_Constraint_Error (Alt) then | |
7447 | Why_Not_Static (Alt); | |
7448 | return; | |
7449 | else | |
7450 | Next (Alt); | |
7451 | end if; | |
7452 | end loop; | |
7453 | end if; | |
7454 | ||
7455 | -- Special case a range to find out which bound to flag | |
7456 | ||
7457 | elsif Nkind (N) = N_Range then | |
7458 | if Raises_Constraint_Error (Low_Bound (N)) then | |
7459 | Why_Not_Static (Low_Bound (N)); | |
7460 | return; | |
7461 | ||
7462 | elsif Raises_Constraint_Error (High_Bound (N)) then | |
7463 | Why_Not_Static (High_Bound (N)); | |
7464 | return; | |
7465 | end if; | |
7466 | ||
7467 | -- Special case attribute to see which part to flag | |
7468 | ||
7469 | elsif Nkind (N) = N_Attribute_Reference then | |
7470 | if Raises_Constraint_Error (Prefix (N)) then | |
7471 | Why_Not_Static (Prefix (N)); | |
7472 | return; | |
7473 | end if; | |
7474 | ||
7475 | if Present (Expressions (N)) then | |
7476 | Exp := First (Expressions (N)); | |
7477 | while Present (Exp) loop | |
7478 | if Raises_Constraint_Error (Exp) then | |
7479 | Why_Not_Static (Exp); | |
7480 | return; | |
7481 | end if; | |
7482 | ||
7483 | Next (Exp); | |
7484 | end loop; | |
7485 | end if; | |
7486 | ||
7487 | -- Special case a subtype name | |
7488 | ||
7489 | elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then | |
7490 | Error_Msg_NE | |
7491 | ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr)); | |
7492 | return; | |
7493 | end if; | |
7494 | ||
7495 | -- End of special cases | |
7496 | ||
fbf5a39b | 7497 | Error_Msg_N |
80298c3b AC |
7498 | ("!expression raises exception, cannot be static (RM 4.9(34))", |
7499 | N); | |
fbf5a39b AC |
7500 | return; |
7501 | end if; | |
7502 | ||
7503 | -- If no type, then something is pretty wrong, so ignore | |
7504 | ||
7505 | Typ := Etype (Expr); | |
7506 | ||
7507 | if No (Typ) then | |
7508 | return; | |
7509 | end if; | |
7510 | ||
65f7ed64 AC |
7511 | -- Type must be scalar or string type (but allow Bignum, since this |
7512 | -- is really a scalar type from our point of view in this diagnosis). | |
fbf5a39b AC |
7513 | |
7514 | if not Is_Scalar_Type (Typ) | |
7515 | and then not Is_String_Type (Typ) | |
65f7ed64 | 7516 | and then not Is_RTE (Typ, RE_Bignum) |
fbf5a39b AC |
7517 | then |
7518 | Error_Msg_N | |
c8a3028c | 7519 | ("!static expression must have scalar or string type " & |
8fde064e | 7520 | "(RM 4.9(2))", N); |
fbf5a39b AC |
7521 | return; |
7522 | end if; | |
7523 | end if; | |
7524 | ||
7525 | -- If we got through those checks, test particular node kind | |
7526 | ||
7527 | case Nkind (N) is | |
8fde064e AC |
7528 | |
7529 | -- Entity name | |
7530 | ||
d8f43ee6 HK |
7531 | when N_Expanded_Name |
7532 | | N_Identifier | |
7533 | | N_Operator_Symbol | |
7534 | => | |
fbf5a39b AC |
7535 | E := Entity (N); |
7536 | ||
7537 | if Is_Named_Number (E) then | |
7538 | null; | |
7539 | ||
7540 | elsif Ekind (E) = E_Constant then | |
8fde064e | 7541 | |
80a09e02 | 7542 | -- One case we can give a better message is when we have a |
8fde064e AC |
7543 | -- string literal created by concatenating an aggregate with |
7544 | -- an others expression. | |
7545 | ||
7546 | Entity_Case : declare | |
7547 | CV : constant Node_Id := Constant_Value (E); | |
7548 | CO : constant Node_Id := Original_Node (CV); | |
7549 | ||
7550 | function Is_Aggregate (N : Node_Id) return Boolean; | |
7551 | -- See if node N came from an others aggregate, if so | |
7552 | -- return True and set Error_Msg_Sloc to aggregate. | |
7553 | ||
7554 | ------------------ | |
7555 | -- Is_Aggregate -- | |
7556 | ------------------ | |
7557 | ||
7558 | function Is_Aggregate (N : Node_Id) return Boolean is | |
7559 | begin | |
7560 | if Nkind (Original_Node (N)) = N_Aggregate then | |
7561 | Error_Msg_Sloc := Sloc (Original_Node (N)); | |
7562 | return True; | |
80298c3b | 7563 | |
8fde064e AC |
7564 | elsif Is_Entity_Name (N) |
7565 | and then Ekind (Entity (N)) = E_Constant | |
7566 | and then | |
7567 | Nkind (Original_Node (Constant_Value (Entity (N)))) = | |
7568 | N_Aggregate | |
7569 | then | |
7570 | Error_Msg_Sloc := | |
7571 | Sloc (Original_Node (Constant_Value (Entity (N)))); | |
7572 | return True; | |
80298c3b | 7573 | |
8fde064e AC |
7574 | else |
7575 | return False; | |
7576 | end if; | |
7577 | end Is_Aggregate; | |
7578 | ||
7579 | -- Start of processing for Entity_Case | |
7580 | ||
7581 | begin | |
7582 | if Is_Aggregate (CV) | |
7583 | or else (Nkind (CO) = N_Op_Concat | |
7584 | and then (Is_Aggregate (Left_Opnd (CO)) | |
7585 | or else | |
7586 | Is_Aggregate (Right_Opnd (CO)))) | |
7587 | then | |
c8a3028c | 7588 | Error_Msg_N ("!aggregate (#) is never static", N); |
8fde064e | 7589 | |
aa500b7a | 7590 | elsif No (CV) or else not Is_Static_Expression (CV) then |
8fde064e | 7591 | Error_Msg_NE |
c8a3028c | 7592 | ("!& is not a static constant (RM 4.9(5))", N, E); |
8fde064e AC |
7593 | end if; |
7594 | end Entity_Case; | |
fbf5a39b | 7595 | |
edab6088 RD |
7596 | elsif Is_Type (E) then |
7597 | Error_Msg_NE | |
7598 | ("!& is not a static subtype (RM 4.9(26))", N, E); | |
7599 | ||
fbf5a39b AC |
7600 | else |
7601 | Error_Msg_NE | |
c8a3028c | 7602 | ("!& is not static constant or named number " |
8fde064e | 7603 | & "(RM 4.9(5))", N, E); |
fbf5a39b AC |
7604 | end if; |
7605 | ||
8fde064e AC |
7606 | -- Binary operator |
7607 | ||
d8f43ee6 HK |
7608 | when N_Binary_Op |
7609 | | N_Membership_Test | |
7610 | | N_Short_Circuit | |
7611 | => | |
fbf5a39b AC |
7612 | if Nkind (N) in N_Op_Shift then |
7613 | Error_Msg_N | |
d8f43ee6 | 7614 | ("!shift functions are never static (RM 4.9(6,18))", N); |
fbf5a39b AC |
7615 | else |
7616 | Why_Not_Static (Left_Opnd (N)); | |
7617 | Why_Not_Static (Right_Opnd (N)); | |
7618 | end if; | |
7619 | ||
8fde064e AC |
7620 | -- Unary operator |
7621 | ||
fbf5a39b AC |
7622 | when N_Unary_Op => |
7623 | Why_Not_Static (Right_Opnd (N)); | |
7624 | ||
8fde064e AC |
7625 | -- Attribute reference |
7626 | ||
fbf5a39b AC |
7627 | when N_Attribute_Reference => |
7628 | Why_Not_Static_List (Expressions (N)); | |
7629 | ||
7630 | E := Etype (Prefix (N)); | |
7631 | ||
7632 | if E = Standard_Void_Type then | |
7633 | return; | |
7634 | end if; | |
7635 | ||
7636 | -- Special case non-scalar'Size since this is a common error | |
7637 | ||
7638 | if Attribute_Name (N) = Name_Size then | |
7639 | Error_Msg_N | |
c8a3028c | 7640 | ("!size attribute is only static for static scalar type " |
8fde064e | 7641 | & "(RM 4.9(7,8))", N); |
fbf5a39b AC |
7642 | |
7643 | -- Flag array cases | |
7644 | ||
7645 | elsif Is_Array_Type (E) then | |
4a08c95c AC |
7646 | if Attribute_Name (N) |
7647 | not in Name_First | Name_Last | Name_Length | |
fbf5a39b AC |
7648 | then |
7649 | Error_Msg_N | |
c8a3028c | 7650 | ("!static array attribute must be Length, First, or Last " |
8fde064e | 7651 | & "(RM 4.9(8))", N); |
fbf5a39b AC |
7652 | |
7653 | -- Since we know the expression is not-static (we already | |
7654 | -- tested for this, must mean array is not static). | |
7655 | ||
7656 | else | |
7657 | Error_Msg_N | |
c8a3028c | 7658 | ("!prefix is non-static array (RM 4.9(8))", Prefix (N)); |
fbf5a39b AC |
7659 | end if; |
7660 | ||
7661 | return; | |
7662 | ||
22cb89b5 AC |
7663 | -- Special case generic types, since again this is a common source |
7664 | -- of confusion. | |
fbf5a39b | 7665 | |
80298c3b | 7666 | elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then |
fbf5a39b | 7667 | Error_Msg_N |
c8a3028c | 7668 | ("!attribute of generic type is never static " |
8fde064e | 7669 | & "(RM 4.9(7,8))", N); |
fbf5a39b | 7670 | |
edab6088 | 7671 | elsif Is_OK_Static_Subtype (E) then |
fbf5a39b AC |
7672 | null; |
7673 | ||
7674 | elsif Is_Scalar_Type (E) then | |
7675 | Error_Msg_N | |
c8a3028c | 7676 | ("!prefix type for attribute is not static scalar subtype " |
8fde064e | 7677 | & "(RM 4.9(7))", N); |
fbf5a39b AC |
7678 | |
7679 | else | |
7680 | Error_Msg_N | |
c8a3028c | 7681 | ("!static attribute must apply to array/scalar type " |
8fde064e | 7682 | & "(RM 4.9(7,8))", N); |
fbf5a39b AC |
7683 | end if; |
7684 | ||
8fde064e AC |
7685 | -- String literal |
7686 | ||
fbf5a39b AC |
7687 | when N_String_Literal => |
7688 | Error_Msg_N | |
c8a3028c | 7689 | ("!subtype of string literal is non-static (RM 4.9(4))", N); |
8fde064e AC |
7690 | |
7691 | -- Explicit dereference | |
fbf5a39b AC |
7692 | |
7693 | when N_Explicit_Dereference => | |
7694 | Error_Msg_N | |
c8a3028c | 7695 | ("!explicit dereference is never static (RM 4.9)", N); |
8fde064e AC |
7696 | |
7697 | -- Function call | |
fbf5a39b AC |
7698 | |
7699 | when N_Function_Call => | |
7700 | Why_Not_Static_List (Parameter_Associations (N)); | |
65f7ed64 AC |
7701 | |
7702 | -- Complain about non-static function call unless we have Bignum | |
7703 | -- which means that the underlying expression is really some | |
7704 | -- scalar arithmetic operation. | |
7705 | ||
7706 | if not Is_RTE (Typ, RE_Bignum) then | |
c8a3028c | 7707 | Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N); |
65f7ed64 | 7708 | end if; |
fbf5a39b | 7709 | |
8fde064e AC |
7710 | -- Parameter assocation (test actual parameter) |
7711 | ||
fbf5a39b AC |
7712 | when N_Parameter_Association => |
7713 | Why_Not_Static (Explicit_Actual_Parameter (N)); | |
7714 | ||
8fde064e AC |
7715 | -- Indexed component |
7716 | ||
fbf5a39b | 7717 | when N_Indexed_Component => |
c8a3028c | 7718 | Error_Msg_N ("!indexed component is never static (RM 4.9)", N); |
8fde064e AC |
7719 | |
7720 | -- Procedure call | |
fbf5a39b AC |
7721 | |
7722 | when N_Procedure_Call_Statement => | |
c8a3028c | 7723 | Error_Msg_N ("!procedure call is never static (RM 4.9)", N); |
8fde064e AC |
7724 | |
7725 | -- Qualified expression (test expression) | |
fbf5a39b AC |
7726 | |
7727 | when N_Qualified_Expression => | |
7728 | Why_Not_Static (Expression (N)); | |
7729 | ||
8fde064e AC |
7730 | -- Aggregate |
7731 | ||
d8f43ee6 HK |
7732 | when N_Aggregate |
7733 | | N_Extension_Aggregate | |
7734 | => | |
c8a3028c | 7735 | Error_Msg_N ("!an aggregate is never static (RM 4.9)", N); |
8fde064e AC |
7736 | |
7737 | -- Range | |
fbf5a39b AC |
7738 | |
7739 | when N_Range => | |
7740 | Why_Not_Static (Low_Bound (N)); | |
7741 | Why_Not_Static (High_Bound (N)); | |
7742 | ||
8fde064e AC |
7743 | -- Range constraint, test range expression |
7744 | ||
fbf5a39b AC |
7745 | when N_Range_Constraint => |
7746 | Why_Not_Static (Range_Expression (N)); | |
7747 | ||
8fde064e AC |
7748 | -- Subtype indication, test constraint |
7749 | ||
fbf5a39b AC |
7750 | when N_Subtype_Indication => |
7751 | Why_Not_Static (Constraint (N)); | |
7752 | ||
8fde064e AC |
7753 | -- Selected component |
7754 | ||
fbf5a39b | 7755 | when N_Selected_Component => |
c8a3028c | 7756 | Error_Msg_N ("!selected component is never static (RM 4.9)", N); |
8fde064e AC |
7757 | |
7758 | -- Slice | |
fbf5a39b AC |
7759 | |
7760 | when N_Slice => | |
c8a3028c | 7761 | Error_Msg_N ("!slice is never static (RM 4.9)", N); |
fbf5a39b AC |
7762 | |
7763 | when N_Type_Conversion => | |
7764 | Why_Not_Static (Expression (N)); | |
7765 | ||
23b86353 | 7766 | if not Is_Scalar_Type (Entity (Subtype_Mark (N))) |
edab6088 | 7767 | or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) |
fbf5a39b AC |
7768 | then |
7769 | Error_Msg_N | |
c8a3028c | 7770 | ("!static conversion requires static scalar subtype result " |
8fde064e | 7771 | & "(RM 4.9(9))", N); |
fbf5a39b AC |
7772 | end if; |
7773 | ||
8fde064e AC |
7774 | -- Unchecked type conversion |
7775 | ||
fbf5a39b AC |
7776 | when N_Unchecked_Type_Conversion => |
7777 | Error_Msg_N | |
c8a3028c | 7778 | ("!unchecked type conversion is never static (RM 4.9)", N); |
8fde064e AC |
7779 | |
7780 | -- All other cases, no reason to give | |
fbf5a39b AC |
7781 | |
7782 | when others => | |
7783 | null; | |
fbf5a39b AC |
7784 | end case; |
7785 | end Why_Not_Static; | |
7786 | ||
996ae0b0 | 7787 | end Sem_Eval; |