@@ -6,7 +6,7 @@ import java
6
6
7
7
signature boolean getBoolValSig ( Expr e ) ;
8
8
9
- signature int getIntValSig ( Expr e ) ;
9
+ signature QlBuiltins :: BigInt getBigIntValSig ( Expr e ) ;
10
10
11
11
/**
12
12
* Given predicates defining boolean and integer constants, this module
@@ -15,18 +15,18 @@ signature int getIntValSig(Expr e);
15
15
*
16
16
* The input and output predicates are expected to be mutually recursive.
17
17
*/
18
- module CalculateConstants< getBoolValSig / 1 getBoolVal, getIntValSig / 1 getIntVal > {
18
+ module CalculateConstants< getBoolValSig / 1 getBoolVal, getBigIntValSig / 1 getBigIntVal > {
19
19
/** Gets the value of a constant boolean expression. */
20
20
boolean calculateBooleanValue ( Expr e ) {
21
21
// No casts relevant to booleans.
22
22
// `!` is the only unary operator that evaluates to a boolean.
23
23
result = getBoolVal ( e .( LogNotExpr ) .getExpr ( ) ) .booleanNot ( )
24
24
or
25
25
// Handle binary expressions that have integer operands and a boolean result.
26
- exists ( BinaryExpr b , int left , int right |
26
+ exists ( BinaryExpr b , QlBuiltins :: BigInt left , QlBuiltins :: BigInt right |
27
27
b = e and
28
- left = getIntVal ( b .getLeftOperand ( ) ) and
29
- right = getIntVal ( b .getRightOperand ( ) )
28
+ left = getBigIntVal ( b .getLeftOperand ( ) ) and
29
+ right = getBigIntVal ( b .getRightOperand ( ) )
30
30
|
31
31
(
32
32
b instanceof LTExpr and
@@ -97,33 +97,35 @@ module CalculateConstants<getBoolValSig/1 getBoolVal, getIntValSig/1 getIntVal>
97
97
)
98
98
}
99
99
100
- /** Gets the value of a constant integer expression. */
101
- int calculateIntValue ( Expr e ) {
102
- exists ( IntegralType t | e .getType ( ) = t | t . getName ( ) . toLowerCase ( ) != "long" ) and
100
+ /** Gets the big int value of a constant integer expression. */
101
+ QlBuiltins :: BigInt calculateBigIntValue ( Expr e ) {
102
+ e .getType ( ) instanceof IntegralType and
103
103
(
104
- exists ( CastingExpr cast , int val | cast = e and val = getIntVal ( cast .getExpr ( ) ) |
104
+ exists ( CastingExpr cast , QlBuiltins:: BigInt val |
105
+ cast = e and val = getBigIntVal ( cast .getExpr ( ) )
106
+ |
105
107
if cast .getType ( ) .hasName ( "byte" )
106
- then result = ( val + 128 ) .bitAnd ( 255 ) - 128
108
+ then result = ( val + 128 . toBigInt ( ) ) .bitAnd ( 255 . toBigInt ( ) ) - 128 . toBigInt ( )
107
109
else
108
110
if cast .getType ( ) .hasName ( "short" )
109
- then result = ( val + 32768 ) .bitAnd ( 65535 ) - 32768
111
+ then result = ( val + 32768 . toBigInt ( ) ) .bitAnd ( 65535 . toBigInt ( ) ) - 32768 . toBigInt ( )
110
112
else
111
113
if cast .getType ( ) .hasName ( "char" )
112
- then result = val .bitAnd ( 65535 )
114
+ then result = val .bitAnd ( 65535 . toBigInt ( ) )
113
115
else result = val
114
116
)
115
117
or
116
- result = getIntVal ( e .( PlusExpr ) .getExpr ( ) )
118
+ result = getBigIntVal ( e .( PlusExpr ) .getExpr ( ) )
117
119
or
118
- result = - getIntVal ( e .( MinusExpr ) .getExpr ( ) )
120
+ result = - getBigIntVal ( e .( MinusExpr ) .getExpr ( ) )
119
121
or
120
- result = getIntVal ( e .( BitNotExpr ) .getExpr ( ) ) .bitNot ( )
122
+ result = getBigIntVal ( e .( BitNotExpr ) .getExpr ( ) ) .bitNot ( )
121
123
or
122
124
// No `int` value for `LogNotExpr`.
123
- exists ( BinaryExpr b , int v1 , int v2 |
125
+ exists ( BinaryExpr b , QlBuiltins :: BigInt v1 , QlBuiltins :: BigInt v2 |
124
126
b = e and
125
- v1 = getIntVal ( b .getLeftOperand ( ) ) and
126
- v2 = getIntVal ( b .getRightOperand ( ) )
127
+ v1 = getBigIntVal ( b .getLeftOperand ( ) ) and
128
+ v2 = getBigIntVal ( b .getRightOperand ( ) )
127
129
|
128
130
b instanceof MulExpr and result = v1 * v2
129
131
or
@@ -135,11 +137,12 @@ module CalculateConstants<getBoolValSig/1 getBoolVal, getIntValSig/1 getIntVal>
135
137
or
136
138
b instanceof SubExpr and result = v1 - v2
137
139
or
138
- b instanceof LeftShiftExpr and result = v1 .bitShiftLeft ( v2 )
140
+ b instanceof LeftShiftExpr and result = v1 .bitShiftLeft ( v2 . toInt ( ) )
139
141
or
140
- b instanceof RightShiftExpr and result = v1 .bitShiftRightSigned ( v2 )
142
+ b instanceof RightShiftExpr and result = v1 .bitShiftRightSigned ( v2 . toInt ( ) )
141
143
or
142
- b instanceof UnsignedRightShiftExpr and result = v1 .bitShiftRight ( v2 )
144
+ b instanceof UnsignedRightShiftExpr and
145
+ result = v1 .toInt ( ) .bitShiftRight ( v2 .toInt ( ) ) .toBigInt ( ) // bitShiftRight not implemented on bigints (yet)
143
146
or
144
147
b instanceof AndBitwiseExpr and result = v1 .bitAnd ( v2 )
145
148
or
@@ -154,12 +157,12 @@ module CalculateConstants<getBoolValSig/1 getBoolVal, getIntValSig/1 getIntVal>
154
157
exists ( ConditionalExpr ce , boolean condition |
155
158
ce = e and
156
159
condition = getBoolVal ( ce .getCondition ( ) ) and
157
- result = getIntVal ( ce .getBranchExpr ( condition ) )
160
+ result = getBigIntVal ( ce .getBranchExpr ( condition ) )
158
161
)
159
162
or
160
163
// If a `Variable` is final, its value is its initializer, if it exists.
161
164
exists ( Variable v | e = v .getAnAccess ( ) and v .isFinal ( ) |
162
- result = getIntVal ( v .getInitializer ( ) )
165
+ result = getBigIntVal ( v .getInitializer ( ) )
163
166
)
164
167
)
165
168
}
0 commit comments