BinaryExpression.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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
  18. //
  19. // Authors:
  20. // Antonello Provenzano <[email protected]>
  21. // Federico Di Gregorio <[email protected]>
  22. //
  23. using System.Reflection;
  24. using System.Text;
  25. namespace System.Linq.Expressions
  26. {
  27. public sealed class BinaryExpression : Expression
  28. {
  29. #region .ctor
  30. internal BinaryExpression (ExpressionType nt, Expression left, Expression right, MethodInfo method, Type type)
  31. : base(nt, type)
  32. {
  33. this.left = left;
  34. this.right = right;
  35. this.method = method;
  36. }
  37. internal BinaryExpression (ExpressionType nt, Expression left, Expression right, Type type)
  38. : this(nt, left, right, null, type)
  39. {
  40. }
  41. #endregion
  42. #region Fields
  43. private Expression left;
  44. private Expression right;
  45. private MethodInfo method;
  46. #endregion
  47. #region Properties
  48. [MonoTODO]
  49. public LambdaExpression Conversion {
  50. get { throw new System.NotImplementedException (); }
  51. }
  52. public Expression Left {
  53. get { return left; }
  54. }
  55. public Expression Right {
  56. get { return right; }
  57. }
  58. public MethodInfo Method {
  59. get { return method; }
  60. }
  61. [MonoTODO]
  62. public bool IsLifted {
  63. get { return false; }
  64. }
  65. [MonoTODO]
  66. public bool IsLiftedToNull {
  67. get { return false; }
  68. }
  69. #endregion
  70. #region Internal Methods
  71. internal override void BuildString(StringBuilder builder)
  72. {
  73. switch (NodeType)
  74. {
  75. case ExpressionType.Add:
  76. builder.AppendFormat ("({0} + {1})", left, right);
  77. break;
  78. case ExpressionType.AddChecked:
  79. builder.AppendFormat ("({0} + {1})", left, right);
  80. break;
  81. // See below for ExpressionType.And.
  82. case ExpressionType.AndAlso:
  83. builder.AppendFormat ("({0} && {1})", left, right);
  84. break;
  85. case ExpressionType.ArrayIndex:
  86. builder.AppendFormat ("{0}[{1}]", left, right);
  87. break;
  88. case ExpressionType.Coalesce:
  89. builder.AppendFormat ("({0} ?? {1})", left, right);
  90. break;
  91. case ExpressionType.Divide:
  92. builder.AppendFormat ("({0} / {1})", left, right);
  93. break;
  94. case ExpressionType.Equal:
  95. builder.AppendFormat ("({0} == {1})", left, right);
  96. break;
  97. case ExpressionType.ExclusiveOr:
  98. builder.AppendFormat ("({0} ^ {1})", left, right);
  99. break;
  100. case ExpressionType.GreaterThan:
  101. builder.AppendFormat ("({0} > {1})", left, right);
  102. break;
  103. case ExpressionType.GreaterThanOrEqual:
  104. builder.AppendFormat ("({0} >= {1})", left, right);
  105. break;
  106. case ExpressionType.LeftShift:
  107. builder.AppendFormat ("({0} << {1})", left, right);
  108. break;
  109. case ExpressionType.LessThan:
  110. builder.AppendFormat ("({0} < {1})", left, right);
  111. break;
  112. case ExpressionType.LessThanOrEqual:
  113. builder.AppendFormat ("({0} <= {1})", left, right);
  114. break;
  115. case ExpressionType.Modulo:
  116. builder.AppendFormat ("({0} % {1})", left, right);
  117. break;
  118. case ExpressionType.Multiply:
  119. builder.AppendFormat ("({0} * {1})", left, right);
  120. break;
  121. case ExpressionType.MultiplyChecked:
  122. builder.AppendFormat ("({0} * {1})", left, right);
  123. break;
  124. case ExpressionType.NotEqual:
  125. builder.AppendFormat ("({0} != {1})", left, right);
  126. break;
  127. // See below for ExpressionType.Or.
  128. case ExpressionType.OrElse:
  129. builder.AppendFormat ("({0} || {1})", left, right);
  130. break;
  131. case ExpressionType.RightShift:
  132. builder.AppendFormat ("({0} >> {1})", left, right);
  133. break;
  134. case ExpressionType.Subtract:
  135. builder.AppendFormat ("({0} - {1})", left, right);
  136. break;
  137. case ExpressionType.SubtractChecked:
  138. builder.AppendFormat ("({0} - {1})", left, right);
  139. break;
  140. // 'ExpressionType.And' and 'ExpressionType.Or' are special because
  141. // when the arguments' type is a bool the operator changes from '&'
  142. // or '|' to 'And' or 'Or'.
  143. // FIXME: is this correct or it is an error in MS implementation?
  144. case ExpressionType.And:
  145. if (Type == typeof(bool))
  146. builder.AppendFormat ("({0} And {1})", left, right);
  147. else
  148. builder.AppendFormat ("({0} & {1})", left, right);
  149. break;
  150. case ExpressionType.Or:
  151. if (Type == typeof(bool))
  152. builder.AppendFormat ("({0} Or {1})", left, right);
  153. else
  154. builder.AppendFormat ("({0} | {1})", left, right);
  155. break;
  156. }
  157. }
  158. #endregion
  159. }
  160. }