override public void LeaveBinaryExpression(BinaryExpression node) { if (AstUtil.GetBinaryOperatorKind(node.Operator) == BinaryOperatorKind.Assignment || node.Operator == BinaryOperatorType.ReferenceEquality || node.Operator == BinaryOperatorType.ReferenceInequality) { return; } if (null == node.Left.ExpressionType || null == node.Right.ExpressionType) { return; } object lhs = GetLiteralValue(node.Left); object rhs = GetLiteralValue(node.Right); if (null == lhs || null == rhs) { return; } Expression folded = null; IType lhsType = GetExpressionType(node.Left); IType rhsType = GetExpressionType(node.Right); if (TypeSystemServices.BoolType == lhsType && TypeSystemServices.BoolType == rhsType) { folded = GetFoldedBoolLiteral(node.Operator, Convert.ToBoolean(lhs), Convert.ToBoolean(rhs)); } else if (TypeSystemServices.IsFloatingPointNumber(lhsType) || TypeSystemServices.IsFloatingPointNumber(rhsType)) { folded = GetFoldedDoubleLiteral(node.Operator, Convert.ToDouble(lhs), Convert.ToDouble(rhs)); } else if (TypeSystemServices.IsIntegerNumber(lhsType) || lhsType.IsEnum) { bool lhsSigned = TypeSystemServices.IsSignedNumber(lhsType); bool rhsSigned = TypeSystemServices.IsSignedNumber(rhsType); if (lhsSigned == rhsSigned) //mixed signed/unsigned not supported for folding { folded = lhsSigned ? GetFoldedIntegerLiteral(node.Operator, Convert.ToInt64(lhs), Convert.ToInt64(rhs)) : GetFoldedIntegerLiteral(node.Operator, Convert.ToUInt64(lhs), Convert.ToUInt64(rhs)); } } else if (node.Operator == BinaryOperatorType.TypeTest && lhsType.IsValueType) { folded = GetFoldedValueTypeTest(node, lhsType, (IType)node.Right.Entity); } if (null != folded) { folded.LexicalInfo = node.LexicalInfo; folded.ExpressionType = GetExpressionType(node); folded.Annotate(FoldedExpression, node); ReplaceCurrentNode(folded); } }
override public void LeaveUnaryExpression(UnaryExpression node) { if (node.Operator == UnaryOperatorType.Explode || node.Operator == UnaryOperatorType.AddressOf || node.Operator == UnaryOperatorType.Indirection || node.Operator == UnaryOperatorType.LogicalNot) { return; } if (null == node.Operand.ExpressionType) { return; } object operand = GetLiteralValue(node.Operand); if (null == operand) { return; } Expression folded = null; IType operandType = GetExpressionType(node.Operand); if (TypeSystemServices.IsFloatingPointNumber(operandType)) { folded = GetFoldedDoubleLiteral(node.Operator, Convert.ToDouble(operand)); } else if (TypeSystemServices.IsIntegerNumber(operandType) || operandType.IsEnum) { folded = GetFoldedIntegerLiteral(node.Operator, Convert.ToInt64(operand)); } if (null != folded) { folded.LexicalInfo = node.LexicalInfo; folded.ExpressionType = GetExpressionType(node); ReplaceCurrentNode(folded); } }