Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ private AssignmentOperationExpr build() {
}

// Check type for XOR and assignment operator (^=).
// TODO(summerji): Complete the type-checking for ^= and unit test.
if (operator.equals(OperatorKind.ASSIGNMENT_XOR)) {
Preconditions.checkState(isValidXORAssignmentType(lhsType, rhsType), errorMsg);
Preconditions.checkState(isValidXorAssignmentType(lhsType, rhsType), errorMsg);
}
return assignmentOperationExpr;
}
Expand Down Expand Up @@ -130,9 +129,25 @@ private boolean isValidMultiplyAssignmentType(TypeNode variableType, TypeNode va
return false;
}

// TODO(summerji): Complete the type-checking for ^= and unit test.
private boolean isValidXORAssignmentType(TypeNode variableType, TypeNode valueType) {
return true;
// isValidXorAssignmentType validates the types for LHS variable expr and RHS expr.
// ^= can be applied on boolean and non-floating-point numeric type.
private boolean isValidXorAssignmentType(TypeNode variableType, TypeNode valueType) {
// LHS is boolean or its boxed type, RHS should be boolean or its boxed type.
if (variableType.equals(TypeNode.BOOLEAN)) {
return valueType.equals(variableType);
}
// LHS is integer boxed type, RHS should be non-floating-point numeric types or their boxed
// types.
if (variableType.equals(TypeNode.INT)) {
return TypeNode.isNumericType(valueType) && !TypeNode.isFloatingPointType(valueType);
}
// LHS is non-floating-point numeric types, RHS should be non-float-point numeric types or
// their boxed types.
return TypeNode.isNumericType(variableType)
&& TypeNode.isNumericType(valueType)
&& !TypeNode.isFloatingPointType(variableType)
&& !TypeNode.isFloatingPointType(valueType)
&& !TypeNode.isBoxedType(variableType);
}
}
}
Loading