PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinaryOperationBinder.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 193 lines | 150 code | 16 blank | 27 comment | 14 complexity | e1489976c51ae11b8df51f5bf987d493 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // CSharpBinaryOperationBinder.cs
  3. //
  4. // Authors:
  5. // Marek Safar <marek.safar@gmail.com>
  6. //
  7. // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Dynamic;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Linq.Expressions;
  33. using System.Runtime.CompilerServices;
  34. using Compiler = Mono.CSharp;
  35. namespace Microsoft.CSharp.RuntimeBinder
  36. {
  37. public class CSharpBinaryOperationBinder : BinaryOperationBinder
  38. {
  39. IList<CSharpArgumentInfo> argumentInfo;
  40. bool is_checked, is_member_access;
  41. public CSharpBinaryOperationBinder (ExpressionType operation, bool isChecked, bool isMemberAccess, IEnumerable<CSharpArgumentInfo> argumentInfo)
  42. : base (operation)
  43. {
  44. this.argumentInfo = new ReadOnlyCollectionBuilder<CSharpArgumentInfo> (argumentInfo);
  45. if (this.argumentInfo.Count != 2)
  46. throw new ArgumentException ("argumentInfo != 2");
  47. this.is_checked = isChecked;
  48. this.is_member_access = isMemberAccess;
  49. }
  50. public IList<CSharpArgumentInfo> ArgumentInfo {
  51. get {
  52. return argumentInfo;
  53. }
  54. }
  55. public bool IsChecked {
  56. get {
  57. return is_checked;
  58. }
  59. }
  60. public bool IsMemberAccess {
  61. get {
  62. return is_member_access;
  63. }
  64. }
  65. public override bool Equals (object obj)
  66. {
  67. var other = obj as CSharpBinaryOperationBinder;
  68. return other != null && base.Equals (obj) && other.is_checked == is_checked && other.is_member_access == is_member_access &&
  69. other.argumentInfo.SequenceEqual (argumentInfo);
  70. }
  71. public override int GetHashCode ()
  72. {
  73. return Extensions.HashCode (
  74. base.GetHashCode (),
  75. is_checked.GetHashCode (),
  76. is_member_access.GetHashCode (),
  77. argumentInfo[0].GetHashCode (), argumentInfo[1].GetHashCode ());
  78. }
  79. Compiler.Binary.Operator GetOperator (out bool isCompound)
  80. {
  81. isCompound = false;
  82. switch (Operation) {
  83. case ExpressionType.Add:
  84. return Compiler.Binary.Operator.Addition;
  85. case ExpressionType.AddAssign:
  86. isCompound = true;
  87. return Compiler.Binary.Operator.Addition;
  88. case ExpressionType.And:
  89. return Compiler.Binary.Operator.BitwiseAnd;
  90. case ExpressionType.AndAssign:
  91. isCompound = true;
  92. return Compiler.Binary.Operator.BitwiseAnd;
  93. case ExpressionType.Divide:
  94. return Compiler.Binary.Operator.Division;
  95. case ExpressionType.DivideAssign:
  96. isCompound = true;
  97. return Compiler.Binary.Operator.Division;
  98. case ExpressionType.Equal:
  99. return Compiler.Binary.Operator.Equality;
  100. case ExpressionType.ExclusiveOr:
  101. return Compiler.Binary.Operator.ExclusiveOr;
  102. case ExpressionType.ExclusiveOrAssign:
  103. isCompound = true;
  104. return Compiler.Binary.Operator.ExclusiveOr;
  105. case ExpressionType.GreaterThan:
  106. return Compiler.Binary.Operator.GreaterThan;
  107. case ExpressionType.GreaterThanOrEqual:
  108. return Compiler.Binary.Operator.GreaterThanOrEqual;
  109. case ExpressionType.LeftShift:
  110. return Compiler.Binary.Operator.LeftShift;
  111. case ExpressionType.LeftShiftAssign:
  112. isCompound = true;
  113. return Compiler.Binary.Operator.LeftShift;
  114. case ExpressionType.LessThan:
  115. return Compiler.Binary.Operator.LessThan;
  116. case ExpressionType.LessThanOrEqual:
  117. return Compiler.Binary.Operator.LessThanOrEqual;
  118. case ExpressionType.Modulo:
  119. return Compiler.Binary.Operator.Modulus;
  120. case ExpressionType.ModuloAssign:
  121. isCompound = true;
  122. return Compiler.Binary.Operator.Modulus;
  123. case ExpressionType.Multiply:
  124. return Compiler.Binary.Operator.Multiply;
  125. case ExpressionType.MultiplyAssign:
  126. isCompound = true;
  127. return Compiler.Binary.Operator.Multiply;
  128. case ExpressionType.NotEqual:
  129. return Compiler.Binary.Operator.Inequality;
  130. case ExpressionType.Or:
  131. return Compiler.Binary.Operator.BitwiseOr;
  132. case ExpressionType.OrAssign:
  133. isCompound = true;
  134. return Compiler.Binary.Operator.BitwiseOr;
  135. case ExpressionType.OrElse:
  136. return Compiler.Binary.Operator.LogicalOr;
  137. case ExpressionType.RightShift:
  138. return Compiler.Binary.Operator.RightShift;
  139. case ExpressionType.RightShiftAssign:
  140. isCompound = true;
  141. return Compiler.Binary.Operator.RightShift;
  142. case ExpressionType.Subtract:
  143. return Compiler.Binary.Operator.Subtraction;
  144. case ExpressionType.SubtractAssign:
  145. isCompound = true;
  146. return Compiler.Binary.Operator.Subtraction;
  147. default:
  148. throw new NotImplementedException (Operation.ToString ());
  149. }
  150. }
  151. public override DynamicMetaObject FallbackBinaryOperation (DynamicMetaObject target, DynamicMetaObject arg, DynamicMetaObject errorSuggestion)
  152. {
  153. var left = CSharpBinder.CreateCompilerExpression (argumentInfo [0], target, true);
  154. var right = CSharpBinder.CreateCompilerExpression (argumentInfo [1], arg, true);
  155. bool is_compound;
  156. var oper = GetOperator (out is_compound);
  157. Compiler.Expression expr;
  158. if (is_compound) {
  159. var target_expr = CSharpBinder.CreateCompilerExpression (argumentInfo[0], target, false);
  160. expr = new Compiler.CompoundAssign (oper, target_expr, right, left);
  161. } else {
  162. expr = new Compiler.Binary (oper, left, right);
  163. expr = new Compiler.Cast (new Compiler.TypeExpression (typeof (object), Compiler.Location.Null), expr);
  164. }
  165. if (is_checked)
  166. expr = new Compiler.CheckedExpr (expr, Compiler.Location.Null);
  167. var restrictions = CreateRestrictionsOnTarget (target).Merge (CreateRestrictionsOnTarget (arg));
  168. return CSharpBinder.Bind (target, expr, restrictions, errorSuggestion);
  169. }
  170. static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject arg)
  171. {
  172. return arg.HasValue && arg.Value == null ?
  173. BindingRestrictions.GetInstanceRestriction (arg.Expression, null) :
  174. BindingRestrictions.GetTypeRestriction (arg.Expression, arg.LimitType);
  175. }
  176. }
  177. }