1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // Antonello Provenzano <antonello@deveel.com>
21 // Federico Di Gregorio <fog@initd.org>
24 using System.Reflection;
27 namespace System.Linq.Expressions
29 public sealed class BinaryExpression : Expression
32 internal BinaryExpression (ExpressionType nt, Expression left, Expression right, MethodInfo method, Type type)
40 internal BinaryExpression (ExpressionType nt, Expression left, Expression right, Type type)
41 : this(nt, left, right, null, type)
47 private Expression left;
48 private Expression right;
49 private MethodInfo method;
53 public Expression Left {
57 public Expression Right {
61 public MethodInfo Method {
62 get { return method; }
66 public bool IsLifted {
71 public bool IsLiftedToNull {
76 #region Internal Methods
77 static void AppendFormat (StringBuilder sb, string format, params object [] args)
79 sb.AppendFormat (format, args);
82 internal override void BuildString(StringBuilder builder)
86 case ExpressionType.Add:
87 AppendFormat (builder, "({0} + {1})", left, right);
90 case ExpressionType.AddChecked:
91 AppendFormat (builder, "({0} + {1})", left, right);
94 // See below for ExpressionType.And.
96 case ExpressionType.AndAlso:
97 AppendFormat (builder, "({0} && {1})", left, right);
100 case ExpressionType.ArrayIndex:
101 AppendFormat (builder, "{0}[{1}]", left, right);
104 case ExpressionType.Coalesce:
105 AppendFormat (builder, "({0} ?? {1})", left, right);
108 case ExpressionType.Divide:
109 AppendFormat (builder, "({0} / {1})", left, right);
112 case ExpressionType.Equal:
113 AppendFormat (builder, "({0} == {1})", left, right);
116 case ExpressionType.ExclusiveOr:
117 AppendFormat (builder, "({0} ^ {1})", left, right);
120 case ExpressionType.GreaterThan:
121 AppendFormat (builder, "({0} > {1})", left, right);
124 case ExpressionType.GreaterThanOrEqual:
125 AppendFormat (builder, "({0} >= {1})", left, right);
128 case ExpressionType.LeftShift:
129 AppendFormat (builder, "({0} << {1})", left, right);
132 case ExpressionType.LessThan:
133 AppendFormat (builder, "({0} < {1})", left, right);
136 case ExpressionType.LessThanOrEqual:
137 AppendFormat (builder, "({0} <= {1})", left, right);
140 case ExpressionType.Modulo:
141 AppendFormat (builder, "({0} % {1})", left, right);
144 case ExpressionType.Multiply:
145 AppendFormat (builder, "({0} * {1})", left, right);
148 case ExpressionType.MultiplyChecked:
149 AppendFormat (builder, "({0} * {1})", left, right);
152 case ExpressionType.NotEqual:
153 AppendFormat (builder, "({0} != {1})", left, right);
156 // See below for ExpressionType.Or.
158 case ExpressionType.OrElse:
159 AppendFormat (builder, "({0} ^ {1})", left, right);
162 case ExpressionType.RightShift:
163 AppendFormat (builder, "({0} >> {1})", left, right);
166 case ExpressionType.Subtract:
167 AppendFormat (builder, "({0} - {1})", left, right);
170 case ExpressionType.SubtractChecked:
171 AppendFormat (builder, "({0} - {1})", left, right);
174 // 'ExpressionType.And' and 'ExpressionType.Or' are special because
175 // when the arguments' type is a bool the operator changes from '&'
176 // or '|' to 'And' or 'Or'.
177 // FIXME: is this correct or it is an error in MS implementation?
179 case ExpressionType.And:
180 if (Type == typeof(bool))
181 AppendFormat (builder, "({0} And {1})", left, right);
183 AppendFormat (builder, "({0} & {1})", left, right);
186 case ExpressionType.Or:
187 if (Type == typeof(bool))
188 AppendFormat (builder, "({0} Or {1})", left, right);
190 AppendFormat (builder, "({0} | {1})", left, right);