-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandleBooleanAsInteger.cs
More file actions
61 lines (52 loc) · 2.09 KB
/
Copy pathHandleBooleanAsInteger.cs
File metadata and controls
61 lines (52 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ICSharpCode.Decompiler.ILAst;
using JSIL.Ast;
using JSIL.Internal;
using Mono.Cecil;
namespace JSIL.Transforms {
public class HandleBooleanAsInteger : JSAstVisitor {
public readonly TypeSystem TypeSystem;
public readonly JSSpecialIdentifiers JS;
public HandleBooleanAsInteger (TypeSystem typeSystem, JSSpecialIdentifiers js) {
TypeSystem = typeSystem;
JS = js;
}
protected JSInvocationExpression CastToInteger (JSExpression booleanExpression) {
return JSInvocationExpression.InvokeMethod(
JS.valueOf(TypeSystem.SByte),
booleanExpression, null, true
);
}
public void VisitNode (JSBinaryOperatorExpression boe) {
var leftType = boe.Left.GetActualType(TypeSystem);
var rightType = boe.Right.GetActualType(TypeSystem);
var leftIsBool = (leftType.FullName == "System.Boolean");
var rightIsBool = (rightType.FullName == "System.Boolean");
var leftIsNumeric = TypeUtil.IsNumericOrEnum(leftType);
var rightIsNumeric = TypeUtil.IsNumericOrEnum(rightType);
if (
(leftIsBool != rightIsBool) &&
(leftIsNumeric || rightIsNumeric) &&
!(boe.Operator is JSAssignmentOperator)
) {
JSBinaryOperatorExpression replacement;
if (leftIsBool)
replacement = new JSBinaryOperatorExpression(
boe.Operator, CastToInteger(boe.Left), boe.Right, boe.ActualType
);
else
replacement = new JSBinaryOperatorExpression(
boe.Operator, boe.Left, CastToInteger(boe.Right), boe.ActualType
);
ParentNode.ReplaceChild(boe, replacement);
VisitReplacement(replacement);
} else {
VisitChildren(boe);
}
}
}
}