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