@@ -25,14 +25,25 @@ internal class BinaryTranslation :
25
25
private readonly INodeTranslation _rightOperandTranslation ;
26
26
private bool _suppressParentheses ;
27
27
28
- private BinaryTranslation ( BinaryExpression binary , ITranslationContext context ) :
29
- base ( IsChecked ( binary . NodeType ) , "(" , ")" )
28
+ private BinaryTranslation (
29
+ BinaryExpression binary ,
30
+ ITranslationContext context ) :
31
+ this ( binary . Left , binary . NodeType , binary . Right , context )
32
+ {
33
+ }
34
+
35
+ private BinaryTranslation (
36
+ Expression leftOperand ,
37
+ ExpressionType nodeType ,
38
+ Expression rightOperand ,
39
+ ITranslationContext context ) :
40
+ base ( IsChecked ( nodeType ) , "(" , ")" )
30
41
{
31
42
_context = context ;
32
- NodeType = binary . NodeType ;
33
- _leftOperandTranslation = context . GetTranslationFor ( binary . Left ) ;
43
+ NodeType = nodeType ;
44
+ _leftOperandTranslation = context . GetTranslationFor ( leftOperand ) ;
34
45
_operator = GetOperator ( NodeType ) ;
35
- _rightOperandTranslation = context . GetTranslationFor ( binary . Right ) ;
46
+ _rightOperandTranslation = context . GetTranslationFor ( rightOperand ) ;
36
47
37
48
if ( _leftOperandTranslation is BinaryTranslation leftNestedBinary &&
38
49
HasComplimentaryOperator ( leftNestedBinary ) )
@@ -138,32 +149,45 @@ public static INodeTranslation For(
138
149
goto default ;
139
150
140
151
default :
141
- TryGetEnumComparisonExpression ( ref binary ) ;
152
+ var isEnumComparison = TryGetEnumComparisonExpression (
153
+ binary ,
154
+ out var leftOperand ,
155
+ out var rightOperand ) ;
156
+
157
+ if ( isEnumComparison )
158
+ {
159
+ return new BinaryTranslation (
160
+ leftOperand ,
161
+ binary . NodeType ,
162
+ rightOperand ,
163
+ context ) ;
164
+ }
165
+
142
166
break ;
143
167
}
144
168
145
169
return new BinaryTranslation ( binary , context ) ;
146
170
}
147
171
148
- public static void TryGetEnumComparisonExpression (
149
- ref BinaryExpression comparison )
172
+ private static bool TryGetEnumComparisonExpression (
173
+ BinaryExpression comparison ,
174
+ out Expression leftOperand ,
175
+ out Expression rightOperand )
150
176
{
151
- var leftOperandIsEnum =
152
- IsEnumType ( comparison . Left , out var leftExpression ) ;
153
-
154
- var rightOperandIsEnum =
155
- IsEnumType ( comparison . Right , out var rightExpression ) ;
177
+ var leftOperandIsEnum = IsEnumType ( comparison . Left , out leftOperand ) ;
178
+ var rightOperandIsEnum = IsEnumType ( comparison . Right , out rightOperand ) ;
156
179
157
- if ( leftOperandIsEnum || rightOperandIsEnum )
180
+ if ( ! ( leftOperandIsEnum || rightOperandIsEnum ) )
158
181
{
159
- var enumType = leftOperandIsEnum
160
- ? leftExpression . Type : rightExpression . Type ;
161
-
162
- comparison = comparison . Update (
163
- GetEnumValue ( leftExpression , enumType ) ,
164
- comparison . Conversion ,
165
- GetEnumValue ( rightExpression , enumType ) ) ;
182
+ return false ;
166
183
}
184
+
185
+ var enumType = leftOperandIsEnum
186
+ ? leftOperand . Type : rightOperand . Type ;
187
+
188
+ leftOperand = GetEnumValue ( leftOperand , enumType ) ;
189
+ rightOperand = GetEnumValue ( rightOperand , enumType ) ;
190
+ return true ;
167
191
}
168
192
169
193
private static bool IsEnumType (
@@ -187,16 +211,17 @@ private static bool IsEnumType(
187
211
188
212
private static Expression GetEnumValue (
189
213
Expression expression ,
190
- Type enumType )
214
+ Type enumValueType )
191
215
{
192
216
if ( expression . NodeType != Constant )
193
217
{
194
218
return expression ;
195
219
}
196
220
197
221
var value = ( ( ConstantExpression ) expression ) . Value ;
222
+ var enumType = enumValueType . GetNonNullableType ( ) ;
198
223
var enumValue = Enum . Parse ( enumType , value . ToString ( ) ) ;
199
- return Expression . Constant ( enumValue , enumType ) ;
224
+ return Expression . Constant ( enumValue , enumValueType ) ;
200
225
}
201
226
202
227
#endregion
0 commit comments