PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Microsoft.Scripting.Core/Actions/UnaryOperationBinder.cs

#
C# | 129 lines | 63 code | 18 blank | 48 comment | 4 complexity | 4dbb2b3bb84faea8f98e1fa15fac605e MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if CLR2
  16. using Microsoft.Scripting.Ast;
  17. #else
  18. using System.Linq.Expressions;
  19. #endif
  20. using System.Dynamic.Utils;
  21. namespace System.Dynamic {
  22. /// <summary>
  23. /// Represents the unary dynamic operation at the call site, providing the binding semantic and the details about the operation.
  24. /// </summary>
  25. public abstract class UnaryOperationBinder : DynamicMetaObjectBinder {
  26. private ExpressionType _operation;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="BinaryOperationBinder"/> class.
  29. /// </summary>
  30. /// <param name="operation">The unary operation kind.</param>
  31. protected UnaryOperationBinder(ExpressionType operation) {
  32. ContractUtils.Requires(OperationIsValid(operation), "operation");
  33. _operation = operation;
  34. }
  35. /// <summary>
  36. /// The result type of the operation.
  37. /// </summary>
  38. public override sealed Type ReturnType {
  39. get {
  40. switch(_operation) {
  41. case ExpressionType.IsFalse:
  42. case ExpressionType.IsTrue:
  43. return typeof(bool);
  44. default:
  45. return typeof(object);
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// The unary operation kind.
  51. /// </summary>
  52. public ExpressionType Operation {
  53. get {
  54. return _operation;
  55. }
  56. }
  57. /// <summary>
  58. /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
  59. /// </summary>
  60. /// <param name="target">The target of the dynamic unary operation.</param>
  61. /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
  62. public DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target) {
  63. return FallbackUnaryOperation(target, null);
  64. }
  65. /// <summary>
  66. /// Performs the binding of the unary dynamic operation if the target dynamic object cannot bind.
  67. /// </summary>
  68. /// <param name="target">The target of the dynamic unary operation.</param>
  69. /// <param name="errorSuggestion">The binding result in case the binding fails, or null.</param>
  70. /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
  71. public abstract DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion);
  72. /// <summary>
  73. /// Performs the binding of the dynamic unary operation.
  74. /// </summary>
  75. /// <param name="target">The target of the dynamic operation.</param>
  76. /// <param name="args">An array of arguments of the dynamic operation.</param>
  77. /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
  78. public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
  79. ContractUtils.RequiresNotNull(target, "target");
  80. ContractUtils.Requires(args == null || args.Length == 0, "args");
  81. return target.BindUnaryOperation(this);
  82. }
  83. // this is a standard DynamicMetaObjectBinder
  84. internal override sealed bool IsStandardBinder {
  85. get {
  86. return true;
  87. }
  88. }
  89. internal static bool OperationIsValid(ExpressionType operation) {
  90. switch (operation) {
  91. #region Generated Unary Operation Binder Validator
  92. // *** BEGIN GENERATED CODE ***
  93. // generated by function: gen_unop_validator from: generate_tree.py
  94. case ExpressionType.Negate:
  95. case ExpressionType.UnaryPlus:
  96. case ExpressionType.Not:
  97. case ExpressionType.Decrement:
  98. case ExpressionType.Increment:
  99. case ExpressionType.OnesComplement:
  100. case ExpressionType.IsTrue:
  101. case ExpressionType.IsFalse:
  102. // *** END GENERATED CODE ***
  103. #endregion
  104. case ExpressionType.Extension:
  105. return true;
  106. default:
  107. return false;
  108. }
  109. }
  110. }
  111. }