BinaryExpression.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // BinaryExpression.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // Contains code from the Mono C# compiler:
  9. // Marek Safar ([email protected])
  10. // Martin Baulig ([email protected])
  11. // Raja Harinath ([email protected])
  12. //
  13. // (C) 2001-2003 Ximian, Inc.
  14. // (C) 2004-2008 Novell, Inc. (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using System;
  36. using System.Reflection;
  37. using System.Reflection.Emit;
  38. namespace System.Linq.Expressions {
  39. public sealed class BinaryExpression : Expression {
  40. Expression left;
  41. Expression right;
  42. LambdaExpression conversion;
  43. MethodInfo method;
  44. bool lift_to_null, is_lifted;
  45. public Expression Left {
  46. get { return left; }
  47. }
  48. public Expression Right {
  49. get { return right; }
  50. }
  51. public MethodInfo Method {
  52. get { return method; }
  53. }
  54. public bool IsLifted {
  55. get { return is_lifted; }
  56. }
  57. public bool IsLiftedToNull {
  58. get { return lift_to_null; }
  59. }
  60. public LambdaExpression Conversion {
  61. get { return conversion; }
  62. }
  63. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right)
  64. : base (node_type, type)
  65. {
  66. this.left = left;
  67. this.right = right;
  68. }
  69. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, MethodInfo method)
  70. : base (node_type, type)
  71. {
  72. this.left = left;
  73. this.right = right;
  74. this.method = method;
  75. }
  76. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, bool lift_to_null,
  77. bool is_lifted, MethodInfo method, LambdaExpression conversion) : base (node_type, type)
  78. {
  79. this.left = left;
  80. this.right = right;
  81. this.method = method;
  82. this.conversion = conversion;
  83. this.lift_to_null = lift_to_null;
  84. this.is_lifted = is_lifted;
  85. }
  86. static void EmitMethod ()
  87. {
  88. throw new NotImplementedException ("Support for MethodInfo-based BinaryExpressions not yet supported");
  89. }
  90. static MethodInfo GetMethodNoPar (Type t, string name)
  91. {
  92. MethodInfo [] methods = t.GetMethods ();
  93. foreach (MethodInfo m in methods){
  94. if (m.Name != name)
  95. continue;
  96. if (m.GetParameters ().Length == 0)
  97. return m;
  98. }
  99. throw new Exception (String.Format ("Internal error: method {0} with no parameters not found on {1}",
  100. name, t));
  101. }
  102. internal override void Emit (EmitContext ec)
  103. {
  104. ILGenerator ig = ec.ig;
  105. OpCode opcode;
  106. if (method != null){
  107. EmitMethod ();
  108. return;
  109. }
  110. Label? empty_value = null;
  111. LocalBuilder ret = null;
  112. if (IsLifted){
  113. LocalBuilder vleft, vright;
  114. empty_value = ig.DefineLabel ();
  115. ret = ig.DeclareLocal (Type);
  116. vleft = ig.DeclareLocal (left.Type);
  117. left.Emit (ec);
  118. ig.Emit (OpCodes.Stloc, vleft);
  119. vright = ig.DeclareLocal (right.Type);
  120. right.Emit (ec);
  121. ig.Emit (OpCodes.Stloc, vright);
  122. MethodInfo has_value = left.Type.GetMethod ("get_HasValue");
  123. ig.Emit (OpCodes.Ldloca, vleft);
  124. ig.Emit (OpCodes.Call, has_value);
  125. ig.Emit (OpCodes.Brfalse, empty_value.Value);
  126. ig.Emit (OpCodes.Ldloca, vright);
  127. ig.Emit (OpCodes.Call, has_value);
  128. ig.Emit (OpCodes.Brfalse, empty_value.Value);
  129. MethodInfo get_value_or_default = GetMethodNoPar (left.Type, "GetValueOrDefault");
  130. ig.Emit (OpCodes.Ldloca, vleft);
  131. ig.Emit (OpCodes.Call, get_value_or_default);
  132. ig.Emit (OpCodes.Ldloca, vright);
  133. ig.Emit (OpCodes.Call, get_value_or_default);
  134. } else {
  135. left.Emit (ec);
  136. right.Emit (ec);
  137. }
  138. bool is_unsigned = IsUnsigned (left.Type);
  139. switch (NodeType){
  140. case ExpressionType.Add:
  141. opcode = OpCodes.Add;
  142. break;
  143. case ExpressionType.AddChecked:
  144. if (left.Type == typeof (int) || left.Type == typeof (long))
  145. opcode = OpCodes.Add_Ovf;
  146. else if (is_unsigned)
  147. opcode = OpCodes.Add_Ovf_Un;
  148. else
  149. opcode = OpCodes.Add;
  150. break;
  151. case ExpressionType.Subtract:
  152. opcode = OpCodes.Sub;
  153. break;
  154. case ExpressionType.SubtractChecked:
  155. if (left.Type == typeof (int) || left.Type == typeof (long))
  156. opcode = OpCodes.Sub_Ovf;
  157. else if (is_unsigned)
  158. opcode = OpCodes.Sub_Ovf_Un;
  159. else
  160. opcode = OpCodes.Sub;
  161. break;
  162. case ExpressionType.Multiply:
  163. opcode = OpCodes.Mul;
  164. break;
  165. case ExpressionType.MultiplyChecked:
  166. if (left.Type == typeof (int) || left.Type == typeof (long))
  167. opcode = OpCodes.Mul_Ovf;
  168. else if (is_unsigned)
  169. opcode = OpCodes.Mul_Ovf_Un;
  170. else
  171. opcode = OpCodes.Mul;
  172. break;
  173. case ExpressionType.Divide:
  174. if (is_unsigned)
  175. opcode = OpCodes.Div_Un;
  176. else
  177. opcode = OpCodes.Div;
  178. break;
  179. case ExpressionType.Modulo:
  180. if (is_unsigned)
  181. opcode = OpCodes.Rem_Un;
  182. else
  183. opcode = OpCodes.Rem;
  184. break;
  185. case ExpressionType.RightShift:
  186. if (is_unsigned)
  187. opcode = OpCodes.Shr_Un;
  188. else
  189. opcode = OpCodes.Shr;
  190. break;
  191. case ExpressionType.LeftShift:
  192. opcode = OpCodes.Shl;
  193. break;
  194. case ExpressionType.And:
  195. opcode = OpCodes.And;
  196. break;
  197. case ExpressionType.Or:
  198. opcode = OpCodes.Or;
  199. break;
  200. case ExpressionType.ExclusiveOr:
  201. opcode = OpCodes.Xor;
  202. break;
  203. case ExpressionType.AndAlso:
  204. case ExpressionType.Coalesce:
  205. case ExpressionType.Equal:
  206. case ExpressionType.GreaterThan:
  207. case ExpressionType.GreaterThanOrEqual:
  208. case ExpressionType.LessThan:
  209. case ExpressionType.LessThanOrEqual:
  210. case ExpressionType.NotEqual:
  211. case ExpressionType.OrElse:
  212. case ExpressionType.Power:
  213. throw new NotImplementedException (String.Format ("No support for {0} node yet", NodeType));
  214. default:
  215. throw new Exception (String.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
  216. }
  217. ig.Emit (opcode);
  218. if (IsLifted){
  219. ig.Emit (OpCodes.Newobj, left.Type.GetConstructors ()[0]);
  220. Label skip = ig.DefineLabel ();
  221. ig.Emit (OpCodes.Br, skip);
  222. ig.MarkLabel (empty_value.Value);
  223. ig.Emit (OpCodes.Ldloc, ret);
  224. ig.Emit (OpCodes.Initobj, Type);
  225. ig.MarkLabel (skip);
  226. }
  227. }
  228. }
  229. }