BinaryExpression.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. public Expression Left {
  49. get { return left; }
  50. }
  51. public Expression Right {
  52. get { return right; }
  53. }
  54. public MethodInfo Method {
  55. get { return method; }
  56. }
  57. [MonoTODO]
  58. public bool IsLifted {
  59. get { return false; }
  60. }
  61. [MonoTODO]
  62. public bool IsLiftedToNull {
  63. get { return false; }
  64. }
  65. #endregion
  66. #region Internal Methods
  67. internal override void BuildString(StringBuilder builder)
  68. {
  69. switch (NodeType)
  70. {
  71. case ExpressionType.Add:
  72. builder.AppendFormat ("({0} + {1})", left, right);
  73. break;
  74. case ExpressionType.AddChecked:
  75. builder.AppendFormat ("({0} + {1})", left, right);
  76. break;
  77. // See below for ExpressionType.And.
  78. case ExpressionType.AndAlso:
  79. builder.AppendFormat ("({0} && {1})", left, right);
  80. break;
  81. case ExpressionType.ArrayIndex:
  82. builder.AppendFormat ("{0}[{1}]", left, right);
  83. break;
  84. case ExpressionType.Coalesce:
  85. builder.AppendFormat ("({0} ?? {1})", left, right);
  86. break;
  87. case ExpressionType.Divide:
  88. builder.AppendFormat ("({0} / {1})", left, right);
  89. break;
  90. case ExpressionType.Equal:
  91. builder.AppendFormat ("({0} == {1})", left, right);
  92. break;
  93. case ExpressionType.ExclusiveOr:
  94. builder.AppendFormat ("({0} ^ {1})", left, right);
  95. break;
  96. case ExpressionType.GreaterThan:
  97. builder.AppendFormat ("({0} > {1})", left, right);
  98. break;
  99. case ExpressionType.GreaterThanOrEqual:
  100. builder.AppendFormat ("({0} >= {1})", left, right);
  101. break;
  102. case ExpressionType.LeftShift:
  103. builder.AppendFormat ("({0} << {1})", left, right);
  104. break;
  105. case ExpressionType.LessThan:
  106. builder.AppendFormat ("({0} < {1})", left, right);
  107. break;
  108. case ExpressionType.LessThanOrEqual:
  109. builder.AppendFormat ("({0} <= {1})", left, right);
  110. break;
  111. case ExpressionType.Modulo:
  112. builder.AppendFormat ("({0} % {1})", left, right);
  113. break;
  114. case ExpressionType.Multiply:
  115. builder.AppendFormat ("({0} * {1})", left, right);
  116. break;
  117. case ExpressionType.MultiplyChecked:
  118. builder.AppendFormat ("({0} * {1})", left, right);
  119. break;
  120. case ExpressionType.NotEqual:
  121. builder.AppendFormat ("({0} != {1})", left, right);
  122. break;
  123. // See below for ExpressionType.Or.
  124. case ExpressionType.OrElse:
  125. builder.AppendFormat ("({0} ^ {1})", left, right);
  126. break;
  127. case ExpressionType.RightShift:
  128. builder.AppendFormat ("({0} >> {1})", left, right);
  129. break;
  130. case ExpressionType.Subtract:
  131. builder.AppendFormat ("({0} - {1})", left, right);
  132. break;
  133. case ExpressionType.SubtractChecked:
  134. builder.AppendFormat ("({0} - {1})", left, right);
  135. break;
  136. // 'ExpressionType.And' and 'ExpressionType.Or' are special because
  137. // when the arguments' type is a bool the operator changes from '&'
  138. // or '|' to 'And' or 'Or'.
  139. // FIXME: is this correct or it is an error in MS implementation?
  140. case ExpressionType.And:
  141. if (Type == typeof(bool))
  142. builder.AppendFormat ("({0} And {1})", left, right);
  143. else
  144. builder.AppendFormat ("({0} & {1})", left, right);
  145. break;
  146. case ExpressionType.Or:
  147. if (Type == typeof(bool))
  148. builder.AppendFormat ("({0} Or {1})", left, right);
  149. else
  150. builder.AppendFormat ("({0} | {1})", left, right);
  151. break;
  152. }
  153. }
  154. #endregion
  155. }
  156. }