Exemplo n.º 1
0
 public void PushBinary(BinaryOperator binaryOperator)
 {
     ExpressionNode rightExpressionNode = _expressionStack.Pop();
     ExpressionNode leftExpressionNode = _expressionStack.Pop();
     BinaryExpression binaryExpression = new BinaryExpression(binaryOperator, leftExpressionNode, rightExpressionNode);
     _expressionStack.Push(binaryExpression);
 }
Exemplo n.º 2
0
 public PartExtractor(LogicalOperator logicalOperator)
 {
     if (logicalOperator == LogicalOperator.And)
         _operatorToBreakAt = BinaryOperator.LogicalAnd;
     else
         _operatorToBreakAt = BinaryOperator.LogicalOr;
 }
Exemplo n.º 3
0
		public static BinaryOperator NegateBinaryOp(BinaryOperator op)
		{
			if (op == BinaryOperator.LogicalAnd)
				return BinaryOperator.LogicalOr;

			if (op == BinaryOperator.LogicalOr)
				return BinaryOperator.LogicalAnd;

			if (op == BinaryOperator.Equal)
				return BinaryOperator.NotEqual;

			if (op == BinaryOperator.NotEqual)
				return BinaryOperator.Equal;

			if (op == BinaryOperator.Greater)
				return BinaryOperator.LessOrEqual;

			if (op == BinaryOperator.GreaterOrEqual)
				return BinaryOperator.Less;

			if (op == BinaryOperator.Less)
				return BinaryOperator.GreaterOrEqual;

			if (op == BinaryOperator.LessOrEqual)
				return BinaryOperator.Greater;

			return null;
		}
Exemplo n.º 4
0
        private static void Add(TokenId tokenId, string text, bool isKeyword, bool isQueryKeyword, UnaryOperator unOp, BinaryOperator binOp)
        {
            TokenInfo info = new TokenInfo(tokenId, text, isKeyword, isQueryKeyword, unOp, binOp);

            _infosById.Add(tokenId, info);

            if (text != null)
                _infosByText.Add(text, info);
        }
Exemplo n.º 5
0
 private TokenInfo(TokenId tokenId, string text, bool isKeyword, bool isQueryKeyword, UnaryOperator unOp, BinaryOperator binOp)
 {
     _tokenId = tokenId;
     _text = text;
     _isKeyword = isKeyword;
     _isQueryKeyword = isQueryKeyword;
     _unaryOperator = unOp;
     _binaryOperator = binOp;
 }
Exemplo n.º 6
0
 void IErrorReporter.InvalidOperatorForAllAny(SourceRange sourceRange, BinaryOperator foundOp)
 {
     string message = String.Format(CultureInfo.CurrentCulture, Resources.InvalidOperatorForAllAny, foundOp.TokenText);
     HandleError(sourceRange, ErrorId.InvalidOperatorForAllAny, message);
 }
Exemplo n.º 7
0
        void IErrorReporter.CannotApplyOperator(BinaryOperator op, Type leftType, Type rightType)
        {
            string message = String.Format(
                CultureInfo.CurrentCulture,
                Resources.CannotApplyBinaryOp,
                op.TokenText,
                FormattingHelpers.FormatType(leftType),
                FormattingHelpers.FormatType(rightType)
            );

            HandleError(ErrorId.CannotApplyBinaryOperator, message);
        }
Exemplo n.º 8
0
        void IErrorReporter.AmbiguousOperatorOverloading(BinaryOperator op, Type leftType, Type rightType)
        {
            string message = String.Format(
                CultureInfo.CurrentCulture,
                Resources.AmbiguousOperatorOverloading,
                op.TokenText,
                FormattingHelpers.FormatType(leftType),
                FormattingHelpers.FormatType(rightType)
            );

            HandleError(ErrorId.AmbiguousOperatorOverloading, message);
        }
Exemplo n.º 9
0
        void IErrorReporter.AmbiguousOperator(BinaryOperator op, Type leftType, Type rightType, MethodInfo opMethod1, MethodInfo opMethod2)
        {
            string message = String.Format(
                CultureInfo.CurrentCulture,
                Resources.AmbiguousBinaryOperator,
                op.TokenText,
                FormattingHelpers.FormatType(leftType),
                FormattingHelpers.FormatType(rightType),
                FormattingHelpers.FormatMethodInfo(opMethod1),
                FormattingHelpers.FormatMethodInfo(opMethod2)
            );

            HandleError(ErrorId.AmbiguousBinaryOperator, message);
        }
Exemplo n.º 10
0
        public static RuntimeException BinaryOperatorFailed(BinaryOperator binaryOperator, MethodInfo operatorMethod, Type leftOperandType, Type rightOperandType, object left, object right, Exception exception)
        {
            if (binaryOperator == null)
                throw new ArgumentNullException("binaryOperator");

            if (operatorMethod == null)
                throw new ArgumentNullException("operatorMethod");

            if (leftOperandType == null)
                throw new ArgumentNullException("leftOperandType");

            if (rightOperandType == null)
                throw new ArgumentNullException("rightOperandType");

            if (exception == null)
                throw new ArgumentNullException("exception");

            string message = String.Format(
                CultureInfo.CurrentCulture,
                Resources.BinaryOperatorFailed,
                FormattingHelpers.FormatType(leftOperandType),
                binaryOperator.TokenText,
                FormattingHelpers.FormatType(rightOperandType),
                FormattingHelpers.FormatMethodInfo(operatorMethod),
                left,
                right,
                exception.Message,
                Environment.NewLine
            );

            return new RuntimeException(message, exception);
        }
Exemplo n.º 11
0
		public BinaryExpression(BinaryOperator op, ExpressionNode left, ExpressionNode right)
		{
			_op = op;
			_left = left;
			_right = right;
		}
Exemplo n.º 12
0
		public MethodInfo BindOperator(BinaryOperator op, Type leftOperandType, Type rightOperandType)
		{
			if (leftOperandType == typeof(DBNull) || rightOperandType == typeof(DBNull))
				return _binaryOperatorPlaceholder;

			MethodInfo result;

			try
			{
				List<MethodInfo> methodList = new List<MethodInfo>();

				// Get the operator method from left...
				methodList.AddRange(OperatorMethodCache.GetOperatorMethods(leftOperandType, op));

				if (leftOperandType != rightOperandType)
				{
					List<Type> declaringTypes = new List<Type>();
					foreach (MethodInfo info in methodList)
					{
						if (!declaringTypes.Contains(info.DeclaringType))
							declaringTypes.Add(info.DeclaringType);
					}

					foreach (MethodInfo rightOperatorMethod in OperatorMethodCache.GetOperatorMethods(rightOperandType, op))
					{
						if (!declaringTypes.Contains(rightOperatorMethod.DeclaringType))
							methodList.Add(rightOperatorMethod);
					}
				}

				// ...from the builtin ones.
				methodList.AddRange(OperatorMethodCache.GetOperatorMethods(typeof(BuiltInOperators), op));

				// Perform overload resolution.
				MethodInfo[] methods = methodList.ToArray();

				result = BindMethodInfo(methods, new Type[] {leftOperandType, rightOperandType});
			}
			catch (InvocationIsAmbiguousException ex)
			{
				_errorReporter.AmbiguousOperator(op, leftOperandType, rightOperandType, ex.Method1, ex.Method2);

				// Avoid cascading errors
				result = ex.Method1;
			}

			// Due to the lack of generic operator methods we have to manually some rules here.
			//
			// 1. = and <>. There exists an operator method op_X(object, object) that would allow
			//    comparing any type to any other type. This is not correct. We want to enforce
			//    that both type are compatible (or if any operand is an interface).
			// 2. & and | for enums. There exists an operator method op_X(Enum, Enum) that would allow
			//    combining any enum with any other enum. This is not correct. We want to enforce
			//    that both enums are the same.

			if (result == _generalEqualityOperator || result == _generalInequalityOperator)
			{
				if (!leftOperandType.IsAssignableFrom(rightOperandType) && 
					!rightOperandType.IsAssignableFrom(leftOperandType) &&
					!leftOperandType.IsInterface &&
					!rightOperandType.IsInterface)
					return null;
			}
			else if (result == _enumBitAndOperator || result == _enumBitOrOperator)
			{
				if (leftOperandType != rightOperandType)
					return null;
			}

			return result;
		}
Exemplo n.º 13
0
		private static JoinOperator JoinOperatorFromBinaryOperator(BinaryOperator op)
		{
			// TODO: Uncomment the following lines if we can create optimized nodes
			//       for other joins than equijoin.

			if (op == BinaryOperator.Equal)
				return JoinOperator.Equal;
//			else if (expression.Op == BinaryOperator.Less)
//				return JoinOperator.Less;
//			else if (expression.Op == BinaryOperator.LessOrEqual)
//				return JoinOperator.LessOrEqual;
//			else if (expression.Op == BinaryOperator.Greater)
//				return JoinOperator.Greater;
//			else if (expression.Op == BinaryOperator.GreaterOrEqual)
//				return JoinOperator.GreaterOrEqual;
			else
				return JoinOperator.None;
		}
Exemplo n.º 14
0
        public override ExpressionNode VisitUnaryExpression(UnaryExpression expression)
        {
            // First visit arguments
            base.VisitUnaryExpression(expression);

            if (expression.Op == UnaryOperator.LogicalNot)
            {
                // Replace "NOT NOT expr" by "expr"

                UnaryExpression unOp = expression.Operand as UnaryExpression;
                if (unOp != null)
                {
                    if (unOp.Op == UnaryOperator.LogicalNot)
                    {
                        return(VisitExpression(unOp.Operand));
                    }
                }

                // Replace "NOT expr IS NULL" and "NOT expr IS NOT NULL" by
                //         "expr IS NOT NULL" and "expr IS NULL" resp.

                IsNullExpression isNull = expression.Operand as IsNullExpression;
                if (isNull != null)
                {
                    isNull.Negated = !isNull.Negated;
                    return(VisitExpression(isNull));
                }

                // Apply negation on EXISTS

                ExistsSubselect existsSubselect = expression.Operand as ExistsSubselect;
                if (existsSubselect != null)
                {
                    existsSubselect.Negated = !existsSubselect.Negated;
                    return(existsSubselect);
                }

                // Apply negation on ALL/ANY subquery

                AllAnySubselect allAnySubselect = expression.Operand as AllAnySubselect;
                if (allAnySubselect != null)
                {
                    allAnySubselect.Op   = AstUtil.NegateBinaryOp(allAnySubselect.Op);
                    allAnySubselect.Type = (allAnySubselect.Type == AllAnySubselect.AllAnyType.All) ? AllAnySubselect.AllAnyType.Any : AllAnySubselect.AllAnyType.All;
                    return(allAnySubselect);
                }

                // Apply De Morgan's law

                BinaryExpression binOp = expression.Operand as BinaryExpression;

                if (binOp != null)
                {
                    BinaryOperator negatedOp = AstUtil.NegateBinaryOp(binOp.Op);

                    if (negatedOp != null)
                    {
                        ExpressionNode newLeft;
                        ExpressionNode newRight;

                        if (binOp.Op == BinaryOperator.LogicalAnd || binOp.Op == BinaryOperator.LogicalOr)
                        {
                            newLeft  = new UnaryExpression(expression.Op, binOp.Left);
                            newRight = new UnaryExpression(expression.Op, binOp.Right);
                        }
                        else
                        {
                            newLeft  = binOp.Left;
                            newRight = binOp.Right;
                        }

                        binOp.Op    = negatedOp;
                        binOp.Left  = newLeft;
                        binOp.Right = newRight;
                        return(VisitExpression(binOp));
                    }
                }
            }

            return(expression);
        }