/DICK.B1/IronPython/Runtime/Binding/PythonProtocol.Operations.cs
C# | 2498 lines | 1927 code | 321 blank | 250 comment | 381 complexity | 671e079969dfd580e5b24a8f9d4e150f MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /* ****************************************************************************
- *
- * Copyright (c) Microsoft Corporation.
- *
- * This source code is subject to terms and conditions of the Microsoft Public License. A
- * copy of the license can be found in the License.html file at the root of this distribution. If
- * you cannot locate the Microsoft Public License, please send an email to
- * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
- * by the terms of the Microsoft Public License.
- *
- * You must not remove this notice, or any other, from this software.
- *
- *
- * ***************************************************************************/
-
- #if !CLR2
- using System.Linq.Expressions;
- using System.Numerics;
- #else
- using Microsoft.Scripting.Math;
- using Complex = Microsoft.Scripting.Math.Complex64;
- #endif
-
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Dynamic;
- using System.Reflection;
- using System.Text;
-
- using Microsoft.Scripting;
- using Microsoft.Scripting.Actions;
- using Microsoft.Scripting.Actions.Calls;
- using Microsoft.Scripting.Ast;
- using Microsoft.Scripting.Generation;
- using Microsoft.Scripting.Runtime;
- using Microsoft.Scripting.Utils;
-
- using IronPython.Runtime.Operations;
- using IronPython.Runtime.Types;
-
- namespace IronPython.Runtime.Binding {
- using Ast = Expression;
- using AstUtils = Microsoft.Scripting.Ast.Utils;
-
- static partial class PythonProtocol {
- private const string DisallowCoerce = "DisallowCoerce";
-
- public static DynamicMetaObject/*!*/ Operation(BinaryOperationBinder/*!*/ operation, DynamicMetaObject target, DynamicMetaObject arg, DynamicMetaObject errorSuggestion) {
- PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Fallback BinaryOperator " + target.LimitType.FullName + " " + operation.Operation + " " + arg.LimitType.FullName);
- PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, operation.Operation.ToString());
-
- DynamicMetaObject[] args = new[] { target, arg };
- if (BindingHelpers.NeedsDeferral(args)) {
- return operation.Defer(target, arg);
- }
-
- ValidationInfo valInfo = BindingHelpers.GetValidationInfo(args);
-
- PythonOperationKind? pyOperator = null;
- switch (operation.Operation) {
- case ExpressionType.Add: pyOperator = PythonOperationKind.Add; break;
- case ExpressionType.And: pyOperator = PythonOperationKind.BitwiseAnd; break;
- case ExpressionType.Divide: pyOperator = PythonOperationKind.Divide; break;
- case ExpressionType.ExclusiveOr: pyOperator = PythonOperationKind.ExclusiveOr; break;
- case ExpressionType.Modulo: pyOperator = PythonOperationKind.Mod; break;
- case ExpressionType.Multiply: pyOperator = PythonOperationKind.Multiply; break;
- case ExpressionType.Or: pyOperator = PythonOperationKind.BitwiseOr; break;
- case ExpressionType.Power: pyOperator = PythonOperationKind.Power; break;
- case ExpressionType.RightShift: pyOperator = PythonOperationKind.RightShift; break;
- case ExpressionType.LeftShift: pyOperator = PythonOperationKind.LeftShift; break;
- case ExpressionType.Subtract: pyOperator = PythonOperationKind.Subtract; break;
-
- case ExpressionType.AddAssign: pyOperator = PythonOperationKind.InPlaceAdd; break;
- case ExpressionType.AndAssign: pyOperator = PythonOperationKind.InPlaceBitwiseAnd; break;
- case ExpressionType.DivideAssign: pyOperator = PythonOperationKind.InPlaceDivide; break;
- case ExpressionType.ExclusiveOrAssign: pyOperator = PythonOperationKind.InPlaceExclusiveOr; break;
- case ExpressionType.ModuloAssign: pyOperator = PythonOperationKind.InPlaceMod; break;
- case ExpressionType.MultiplyAssign: pyOperator = PythonOperationKind.InPlaceMultiply; break;
- case ExpressionType.OrAssign: pyOperator = PythonOperationKind.InPlaceBitwiseOr; break;
- case ExpressionType.PowerAssign: pyOperator = PythonOperationKind.InPlacePower; break;
- case ExpressionType.RightShiftAssign: pyOperator = PythonOperationKind.InPlaceRightShift; break;
- case ExpressionType.LeftShiftAssign: pyOperator = PythonOperationKind.InPlaceLeftShift; break;
- case ExpressionType.SubtractAssign: pyOperator = PythonOperationKind.InPlaceSubtract; break;
-
- case ExpressionType.Equal: pyOperator = PythonOperationKind.Equal; break;
- case ExpressionType.GreaterThan: pyOperator = PythonOperationKind.GreaterThan; break;
- case ExpressionType.GreaterThanOrEqual: pyOperator = PythonOperationKind.GreaterThanOrEqual; break;
- case ExpressionType.LessThan: pyOperator = PythonOperationKind.LessThan; break;
- case ExpressionType.LessThanOrEqual: pyOperator = PythonOperationKind.LessThanOrEqual; break;
- case ExpressionType.NotEqual: pyOperator = PythonOperationKind.NotEqual; break;
- }
-
- DynamicMetaObject res = null;
- if (pyOperator != null) {
- res = MakeBinaryOperation(operation, args, pyOperator.Value, errorSuggestion);
- } else {
- res = operation.FallbackBinaryOperation(target, arg);
- }
-
- return BindingHelpers.AddDynamicTestAndDefer(operation, BindingHelpers.AddPythonBoxing(res), args, valInfo);
- }
-
- public static DynamicMetaObject/*!*/ Operation(UnaryOperationBinder/*!*/ operation, DynamicMetaObject arg, DynamicMetaObject errorSuggestion) {
- PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Fallback UnaryOperator " + " " + operation.Operation + " " + arg.LimitType.FullName);
- PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, operation.Operation.ToString());
-
- DynamicMetaObject[] args = new[] { arg };
- if (arg.NeedsDeferral()) {
- return operation.Defer(arg);
- }
-
- ValidationInfo valInfo = BindingHelpers.GetValidationInfo(args);
-
- DynamicMetaObject res = null;
- Type retType = typeof(object);
- switch (operation.Operation) {
- case ExpressionType.UnaryPlus:
- res = BindingHelpers.AddPythonBoxing(MakeUnaryOperation(operation, arg, "__pos__", errorSuggestion));
- break;
- case ExpressionType.Negate:
- res = BindingHelpers.AddPythonBoxing(MakeUnaryOperation(operation, arg, "__neg__", errorSuggestion));
- break;
- case ExpressionType.OnesComplement:
- res = BindingHelpers.AddPythonBoxing(MakeUnaryOperation(operation, arg, "__invert__", errorSuggestion));
- break;
- case ExpressionType.Not:
- res = MakeUnaryNotOperation(operation, arg, typeof(object), errorSuggestion);
- break;
- case ExpressionType.IsFalse:
- res = MakeUnaryNotOperation(operation, arg, typeof(bool), errorSuggestion);
- retType = typeof(bool);
- break;
- case ExpressionType.IsTrue:
- res = PythonProtocol.ConvertToBool(operation, arg);
- retType = typeof(bool);
- break;
- default:
- res = TypeError(operation, "unknown operation: " + operation.ToString(), args);
- break;
-
- }
-
- return BindingHelpers.AddDynamicTestAndDefer(operation, res, args, valInfo, retType);
- }
-
- public static DynamicMetaObject/*!*/ Index(DynamicMetaObjectBinder/*!*/ operation, PythonIndexType index, DynamicMetaObject[] args) {
- return Index(operation, index, args, null);
- }
-
- public static DynamicMetaObject/*!*/ Index(DynamicMetaObjectBinder/*!*/ operation, PythonIndexType index, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion) {
- if (args.Length >= 3) {
- PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Fallback Index " + " " + index + " " + args[0].LimitType + ", " + args[1].LimitType + ", " + args[2].LimitType + args.Length);
- } else {
- PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Fallback Index " + " " + index + " " + args[0].LimitType + ", " + args[1].LimitType + args.Length);
- }
- PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, index.ToString());
- if (BindingHelpers.NeedsDeferral(args)) {
- return operation.Defer(args);
- }
-
- ValidationInfo valInfo = BindingHelpers.GetValidationInfo(args[0]);
-
- DynamicMetaObject res = BindingHelpers.AddPythonBoxing(MakeIndexerOperation(operation, index, args, errorSuggestion));
-
- return BindingHelpers.AddDynamicTestAndDefer(operation, res, args, valInfo);
- }
-
- public static DynamicMetaObject/*!*/ Operation(PythonOperationBinder/*!*/ operation, params DynamicMetaObject/*!*/[]/*!*/ args) {
- if (args.Length == 1) {
- PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Fallback PythonOp " + " " + operation.Operation + " " + args[0].LimitType);
- } else {
- PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Fallback PythonOp " + " " + operation.Operation + " " + args[0].LimitType + ", " + args[1].LimitType);
- }
- PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, operation.Operation.ToString());
- if (BindingHelpers.NeedsDeferral(args)) {
- return operation.Defer(args);
- }
-
- return MakeOperationRule(operation, args);
- }
-
- private static DynamicMetaObject/*!*/ MakeOperationRule(PythonOperationBinder/*!*/ operation, DynamicMetaObject/*!*/[]/*!*/ args) {
- ValidationInfo valInfo = BindingHelpers.GetValidationInfo(args);
- DynamicMetaObject res;
-
- Type deferType = typeof(object);
- switch (NormalizeOperator(operation.Operation)) {
- case PythonOperationKind.Documentation:
- res = BindingHelpers.AddPythonBoxing(MakeDocumentationOperation(operation, args));
- break;
- case PythonOperationKind.CallSignatures:
- res = BindingHelpers.AddPythonBoxing(MakeCallSignatureOperation(args[0], CompilerHelpers.GetMethodTargets(args[0].Value)));
- break;
- case PythonOperationKind.IsCallable:
- res = MakeIscallableOperation(operation, args);
- break;
- case PythonOperationKind.Hash:
- res = MakeHashOperation(operation, args[0]);
- break;
- case PythonOperationKind.Contains:
- res = MakeContainsOperation(operation, args);
- break;
- case PythonOperationKind.AbsoluteValue:
- res = BindingHelpers.AddPythonBoxing(MakeUnaryOperation(operation, args[0], "__abs__", null));
- break;
- case PythonOperationKind.Compare:
- res = MakeSortComparisonRule(args, operation, operation.Operation);
- Debug.Assert(res.LimitType == typeof(int));
- break;
- case PythonOperationKind.GetEnumeratorForIteration:
- res = MakeEnumeratorOperation(operation, args[0]);
- break;
- default:
- res = BindingHelpers.AddPythonBoxing(MakeBinaryOperation(operation, args, operation.Operation, null));
- break;
- }
-
-
- return BindingHelpers.AddDynamicTestAndDefer(operation, res, args, valInfo, deferType);
-
- }
-
- private static DynamicMetaObject MakeBinaryOperation(DynamicMetaObjectBinder operation, DynamicMetaObject/*!*/[] args, PythonOperationKind opStr, DynamicMetaObject errorSuggestion) {
- if (IsComparison(opStr)) {
- return MakeComparisonOperation(args, operation, opStr, errorSuggestion);
- }
-
- return MakeSimpleOperation(args, operation, opStr, errorSuggestion);
- }
-
- #region Unary Operations
-
- /// <summary>
- /// Creates a rule for the contains operator. This is exposed via "x in y" in
- /// IronPython. It is implemented by calling the __contains__ method on x and
- /// passing in y.
- ///
- /// If a type doesn't define __contains__ but does define __getitem__ then __getitem__ is
- /// called repeatedly in order to see if the object is there.
- ///
- /// For normal .NET enumerables we'll walk the iterator and see if it's present.
- /// </summary>
- private static DynamicMetaObject/*!*/ MakeContainsOperation(PythonOperationBinder/*!*/ operation, DynamicMetaObject/*!*/[]/*!*/ types) {
- DynamicMetaObject res;
- // the paramteres come in backwards from how we look up __contains__, flip them.
- Debug.Assert(types.Length == 2);
- ArrayUtils.SwapLastTwo(types);
-
- PythonContext state = PythonContext.GetPythonContext(operation);
- SlotOrFunction sf = SlotOrFunction.GetSlotOrFunction(state, "__contains__", types);
-
- if (sf.Success) {
- // just a call to __contains__
- res = sf.Target;
- } else {
- RestrictTypes(types);
-
- sf = SlotOrFunction.GetSlotOrFunction(state, "__iter__", types[0]);
- if (sf.Success) {
- // iterate using __iter__
- res = new DynamicMetaObject(
- Ast.Call(
- typeof(PythonOps).GetMethod("ContainsFromEnumerable"),
- AstUtils.Constant(state.SharedContext),
- sf.Target.Expression,
- AstUtils.Convert(types[1].Expression, typeof(object))
- ),
- BindingRestrictions.Combine(types)
- );
- } else {
- ParameterExpression curIndex = Ast.Variable(typeof(int), "count");
- sf = SlotOrFunction.GetSlotOrFunction(state, "__getitem__", types[0], new DynamicMetaObject(curIndex, BindingRestrictions.Empty));
- if (sf.Success) {
- // defines __getitem__, need to loop over the indexes and see if we match
-
- ParameterExpression getItemRes = Ast.Variable(sf.ReturnType, "getItemRes");
- ParameterExpression containsRes = Ast.Variable(typeof(bool), "containsRes");
-
- LabelTarget target = Ast.Label();
- res = new DynamicMetaObject(
- Ast.Block(
- new ParameterExpression[] { curIndex, getItemRes, containsRes },
- Utils.Loop(
- null, // test
- Ast.Assign(curIndex, Ast.Add(curIndex, AstUtils.Constant(1))), // increment
- Ast.Block( // body
- // getItemRes = param0.__getitem__(curIndex)
- Utils.Try(
- Ast.Block(
- Ast.Assign(
- getItemRes,
- sf.Target.Expression
- ),
- Ast.Empty()
- )
- ).Catch(
- // end of indexes, return false
- typeof(IndexOutOfRangeException),
- Ast.Break(target)
- ),
- // if(getItemRes == param1) return true
- Utils.If(
- Ast.Dynamic(
- state.BinaryOperationRetType(
- state.BinaryOperation(ExpressionType.Equal),
- state.Convert(typeof(bool), ConversionResultKind.ExplicitCast)
- ),
- typeof(bool),
- types[1].Expression,
- getItemRes
- ),
- Ast.Assign(containsRes, AstUtils.Constant(true)),
- Ast.Break(target)
- ),
- AstUtils.Empty()
- ),
- null, // loop else
- target, // break label target
- null
- ),
- containsRes
- ),
- BindingRestrictions.Combine(types)
- );
- } else {
- // non-iterable object
- res = new DynamicMetaObject(
- Ast.Throw(
- Ast.Call(
- typeof(PythonOps).GetMethod("TypeErrorForNonIterableObject"),
- AstUtils.Convert(
- types[1].Expression,
- typeof(object)
- )
- ),
- typeof(bool)
- ),
- BindingRestrictions.Combine(types)
- );
- }
- }
- }
-
- if (res.GetLimitType() != typeof(bool) && res.GetLimitType() != typeof(void)) {
- res = new DynamicMetaObject(
- Ast.Dynamic(
- state.Convert(
- typeof(bool),
- ConversionResultKind.ExplicitCast
- ),
- typeof(bool),
- res.Expression
- ),
- res.Restrictions
- );
- }
-
- return res;
- }
-
- private static void RestrictTypes(DynamicMetaObject/*!*/[] types) {
- for (int i = 0; i < types.Length; i++) {
- types[i] = types[i].Restrict(types[i].GetLimitType());
- }
- }
-
- private static DynamicMetaObject/*!*/ MakeHashOperation(PythonOperationBinder/*!*/ operation, DynamicMetaObject/*!*/ self) {
- self = self.Restrict(self.GetLimitType());
-
- PythonContext state = PythonContext.GetPythonContext(operation);
- SlotOrFunction func = SlotOrFunction.GetSlotOrFunction(state, "__hash__", self);
- DynamicMetaObject res = func.Target;
-
- if (func.IsNull) {
- // Python 2.6 setting __hash__ = None makes the type unhashable
- res = new DynamicMetaObject(
- Expression.Throw(
- Expression.Call(
- typeof(PythonOps).GetMethod("TypeErrorForUnhashableObject"),
- self.Expression
- ),
- typeof(int)
- ),
- res.Restrictions
- );
- } else if (func.ReturnType != typeof(int)) {
- if (func.ReturnType == typeof(BigInteger)) {
- // Python 2.5 defines the result of returning a long as hashing the long
- res = new DynamicMetaObject(
- HashBigInt(operation, res.Expression),
- res.Restrictions
- );
- } else if (func.ReturnType == typeof(object)) {
- // need to get the integer value here...
- ParameterExpression tempVar = Ast.Parameter(typeof(object), "hashTemp");
-
- res = new DynamicMetaObject(
- Expression.Block(
- new[] { tempVar },
- Expression.Assign(tempVar, res.Expression),
- Expression.Condition(
- Expression.TypeIs(tempVar, typeof(int)),
- Expression.Convert(tempVar, typeof(int)),
- Expression.Condition(
- Expression.TypeIs(tempVar, typeof(BigInteger)),
- HashBigInt(operation, tempVar),
- HashConvertToInt(state, tempVar)
- )
- )
- ),
- res.Restrictions
- );
- } else {
- // need to convert unknown value to object
- res = new DynamicMetaObject(
- HashConvertToInt(state, res.Expression),
- res.Restrictions
- );
- }
- }
-
- return res;
- }
-
- private static DynamicExpression/*!*/ HashBigInt(PythonOperationBinder/*!*/ operation, Expression/*!*/ expression) {
- return Ast.Dynamic(
- operation,
- typeof(int),
- expression
- );
- }
-
- private static DynamicExpression/*!*/ HashConvertToInt(PythonContext/*!*/ state, Expression/*!*/ expression) {
- return Ast.Dynamic(
- state.Convert(
- typeof(int),
- ConversionResultKind.ExplicitCast
- ),
- typeof(int),
- expression
- );
- }
-
- private static DynamicMetaObject MakeUnaryOperation(DynamicMetaObjectBinder binder, DynamicMetaObject self, string symbol, DynamicMetaObject errorSuggestion) {
- self = self.Restrict(self.GetLimitType());
-
- SlotOrFunction func = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(binder), symbol, self);
-
- if (!func.Success) {
- // we get the error message w/ {0} so that PythonBinderHelper.TypeError formats it correctly
- return errorSuggestion ?? TypeError(binder, MakeUnaryOpErrorMessage(symbol, "{0}"), self);
- }
-
- return func.Target;
- }
-
- private static DynamicMetaObject MakeEnumeratorOperation(PythonOperationBinder operation, DynamicMetaObject self) {
- if (self.GetLimitType() == typeof(string)) {
- self = self.Restrict(self.GetLimitType());
-
- return new DynamicMetaObject(
- Expression.Call(
- typeof(PythonOps).GetMethod("StringEnumerator"),
- self.Expression
- ),
- self.Restrictions
- );
- } else if (self.GetLimitType() == typeof(Bytes)) {
- self = self.Restrict(self.GetLimitType());
-
- if (operation.Context.PythonOptions.Python30) {
- return new DynamicMetaObject(
- Expression.Call(
- typeof(PythonOps).GetMethod("BytesIntEnumerator"),
- self.Expression
- ),
- self.Restrictions
- );
- } else {
- return new DynamicMetaObject(
- Expression.Call(
- typeof(PythonOps).GetMethod("BytesEnumerator"),
- self.Expression
- ),
- self.Restrictions
- );
- }
- } else if ((self.Value is IEnumerable ||
- typeof(IEnumerable).IsAssignableFrom(self.GetLimitType())) && !(self.Value is PythonGenerator)) {
- self = self.Restrict(self.GetLimitType());
-
- return new DynamicMetaObject(
- Expression.Call(
- typeof(PythonOps).GetMethod("GetEnumeratorFromEnumerable"),
- Expression.Convert(
- self.Expression,
- typeof(IEnumerable)
- )
- ),
- self.Restrictions
- );
-
- } else if (self.Value is IEnumerator || // check for COM object (and fast check when we have values)
- typeof(IEnumerator).IsAssignableFrom(self.GetLimitType())) { // check if we don't have a value
- DynamicMetaObject ieres = new DynamicMetaObject(
- MakeEnumeratorResult(
- Ast.Convert(
- self.Expression,
- typeof(IEnumerator)
- )
- ),
- self.Restrict(self.GetLimitType()).Restrictions
- );
-
- #if !SILVERLIGHT
- if (Microsoft.Scripting.ComInterop.ComBinder.IsComObject(self.Value)) {
- ieres = new DynamicMetaObject(
- MakeEnumeratorResult(
- Expression.Convert(
- self.Expression,
- typeof(IEnumerator)
- )
- ),
- ieres.Restrictions.Merge(
- BindingRestrictions.GetExpressionRestriction(
- Ast.TypeIs(self.Expression, typeof(IEnumerator))
- )
- )
- );
- }
- #endif
-
- return ieres;
- }
-
- ParameterExpression tmp = Ast.Parameter(typeof(IEnumerator), "enum");
- IPythonConvertible pyConv = self as IPythonConvertible;
- PythonConversionBinder convBinder = PythonContext.GetPythonContext(operation).Convert(typeof(IEnumerator), ConversionResultKind.ExplicitTry);
-
- DynamicMetaObject res;
- if (pyConv != null) {
- res = pyConv.BindConvert(convBinder);
- } else {
- res = convBinder.Bind(self, new DynamicMetaObject[0]);
- }
-
- return new DynamicMetaObject(
- Expression.Block(
- new[] { tmp },
- Ast.Condition(
- Ast.NotEqual(
- Ast.Assign(tmp, res.Expression),
- AstUtils.Constant(null)
- ),
- MakeEnumeratorResult(tmp),
- Ast.Call(
- typeof(PythonOps).GetMethod("ThrowTypeErrorForBadIteration"),
- PythonContext.GetCodeContext(operation),
- self.Expression
- )
- )
- ),
- res.Restrictions
- );
- }
-
- private static NewExpression MakeEnumeratorResult(Expression tmp) {
- return Expression.New(
- typeof(KeyValuePair<IEnumerator, IDisposable>).GetConstructor(new[] { typeof(IEnumerator), typeof(IDisposable) }),
- tmp,
- Expression.Constant(null, typeof(IDisposable))
- );
- }
-
- private static DynamicMetaObject/*!*/ MakeUnaryNotOperation(DynamicMetaObjectBinder/*!*/ operation, DynamicMetaObject/*!*/ self, Type retType, DynamicMetaObject errorSuggestion) {
- self = self.Restrict(self.GetLimitType());
-
- SlotOrFunction nonzero = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), "__nonzero__", self);
- SlotOrFunction length = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), "__len__", self);
-
- Expression notExpr;
-
- if (!nonzero.Success && !length.Success) {
- // no __len__ or __nonzero__, for None this is always false, everything else is True. If we have
- // an error suggestion though we'll go with that.
- if (errorSuggestion == null) {
- notExpr = (self.GetLimitType() == typeof(DynamicNull)) ? AstUtils.Constant(true) : AstUtils.Constant(false);
- } else {
- notExpr = errorSuggestion.Expression;
- }
- } else {
- SlotOrFunction target = nonzero.Success ? nonzero : length;
-
- notExpr = target.Target.Expression;
-
- if (nonzero.Success) {
- // call non-zero and negate it
- if (notExpr.Type == typeof(bool)) {
- notExpr = Ast.Equal(notExpr, AstUtils.Constant(false));
- } else {
- notExpr = Ast.Call(
- typeof(PythonOps).GetMethod("Not"),
- AstUtils.Convert(notExpr, typeof(object))
- );
- }
- } else {
- // call len, compare w/ zero
- if (notExpr.Type == typeof(int)) {
- notExpr = Ast.Equal(notExpr, AstUtils.Constant(0));
- } else {
- notExpr =
- Ast.Equal(
- Ast.Dynamic(
- PythonContext.GetPythonContext(operation).Operation(
- PythonOperationKind.Compare
- ),
- typeof(int),
- notExpr,
- AstUtils.Constant(0)
- ),
- AstUtils.Constant(0)
- );
- }
- }
- }
-
- if (retType == typeof(object) && notExpr.Type == typeof(bool)) {
- notExpr = BindingHelpers.AddPythonBoxing(notExpr);
- }
-
- return new DynamicMetaObject(
- notExpr,
- self.Restrictions.Merge(nonzero.Target.Restrictions.Merge(length.Target.Restrictions))
- );
- }
-
-
- #endregion
-
- #region Reflective Operations
-
- private static DynamicMetaObject/*!*/ MakeDocumentationOperation(PythonOperationBinder/*!*/ operation, DynamicMetaObject/*!*/[]/*!*/ args) {
- PythonContext state = PythonContext.GetPythonContext(operation);
-
- return new DynamicMetaObject(
- Binders.Convert(
- PythonContext.GetCodeContext(operation),
- state,
- typeof(string),
- ConversionResultKind.ExplicitCast,
- Binders.Get(
- PythonContext.GetCodeContext(operation),
- state,
- typeof(object),
- "__doc__",
- args[0].Expression
- )
- ),
- args[0].Restrictions
- );
- }
-
- internal static DynamicMetaObject/*!*/ MakeCallSignatureOperation(DynamicMetaObject/*!*/ self, IList<MethodBase/*!*/>/*!*/ targets) {
- List<string> arrres = new List<string>();
- foreach (MethodBase mb in targets) {
- StringBuilder res = new StringBuilder();
- string comma = "";
-
- Type retType = mb.GetReturnType();
- if (retType != typeof(void)) {
- res.Append(DynamicHelpers.GetPythonTypeFromType(retType).Name);
- res.Append(" ");
- }
-
- MethodInfo mi = mb as MethodInfo;
- if (mi != null) {
- string name;
- NameConverter.TryGetName(DynamicHelpers.GetPythonTypeFromType(mb.DeclaringType), mi, out name);
- res.Append(name);
- } else {
- res.Append(DynamicHelpers.GetPythonTypeFromType(mb.DeclaringType).Name);
- }
-
- res.Append("(");
- if (!CompilerHelpers.IsStatic(mb)) {
- res.Append("self");
- comma = ", ";
- }
-
- foreach (ParameterInfo pi in mb.GetParameters()) {
- if (pi.ParameterType == typeof(CodeContext)) continue;
-
- res.Append(comma);
- res.Append(DynamicHelpers.GetPythonTypeFromType(pi.ParameterType).Name + " " + pi.Name);
- comma = ", ";
- }
- res.Append(")");
- arrres.Add(res.ToString());
- }
-
- return new DynamicMetaObject(
- AstUtils.Constant(arrres.ToArray()),
- self.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(self.Expression, self.Value))
- );
- }
-
- private static DynamicMetaObject/*!*/ MakeIscallableOperation(PythonOperationBinder/*!*/ operation, DynamicMetaObject/*!*/[]/*!*/ args) {
- // Certain non-python types (encountered during interop) are callable, but don't have
- // a __call__ attribute. The default base binder also checks these, but since we're overriding
- // the base binder, we check them here.
- DynamicMetaObject self = args[0];
-
- // only applies when called from a Python site
- if (typeof(Delegate).IsAssignableFrom(self.GetLimitType()) ||
- typeof(MethodGroup).IsAssignableFrom(self.GetLimitType())) {
- return new DynamicMetaObject(
- AstUtils.Constant(true),
- self.Restrict(self.GetLimitType()).Restrictions
- );
- }
-
- PythonContext state = PythonContext.GetPythonContext(operation);
- Expression isCallable = Ast.NotEqual(
- Binders.TryGet(
- PythonContext.GetCodeContext(operation),
- state,
- typeof(object),
- "__call__",
- self.Expression
- ),
- AstUtils.Constant(OperationFailed.Value)
- );
-
- return new DynamicMetaObject(
- isCallable,
- self.Restrict(self.GetLimitType()).Restrictions
- );
- }
-
- #endregion
-
- #region Common Binary Operations
-
- private static DynamicMetaObject/*!*/ MakeSimpleOperation(DynamicMetaObject/*!*/[]/*!*/ types, DynamicMetaObjectBinder/*!*/ binder, PythonOperationKind operation, DynamicMetaObject errorSuggestion) {
- RestrictTypes(types);
-
- SlotOrFunction fbinder;
- SlotOrFunction rbinder;
- PythonTypeSlot fSlot;
- PythonTypeSlot rSlot;
- GetOpreatorMethods(types, operation, PythonContext.GetPythonContext(binder), out fbinder, out rbinder, out fSlot, out rSlot);
-
- return MakeBinaryOperatorResult(types, binder, operation, fbinder, rbinder, fSlot, rSlot, errorSuggestion);
- }
-
- private static void GetOpreatorMethods(DynamicMetaObject/*!*/[]/*!*/ types, PythonOperationKind oper, PythonContext state, out SlotOrFunction fbinder, out SlotOrFunction rbinder, out PythonTypeSlot fSlot, out PythonTypeSlot rSlot) {
- oper = NormalizeOperator(oper);
- oper &= ~PythonOperationKind.InPlace;
-
- string op, rop;
- if (!IsReverseOperator(oper)) {
- op = Symbols.OperatorToSymbol(oper);
- rop = Symbols.OperatorToReversedSymbol(oper);
- } else {
- // coming back after coercion, just try reverse operator.
- rop = Symbols.OperatorToSymbol(oper);
- op = Symbols.OperatorToReversedSymbol(oper);
- }
-
- fSlot = null;
- rSlot = null;
- PythonType fParent, rParent;
-
- if (oper == PythonOperationKind.Multiply &&
- IsSequence(types[0]) &&
- !PythonOps.IsNonExtensibleNumericType(types[1].GetLimitType())) {
- // class M:
- // def __rmul__(self, other):
- // print "CALLED"
- // return 1
- //
- // print [1,2] * M()
- //
- // in CPython this results in a successful call to __rmul__ on the type ignoring the forward
- // multiplication. But calling the __mul__ method directly does NOT return NotImplemented like
- // one might expect. Therefore we explicitly convert the MetaObject argument into an Index
- // for binding purposes. That allows this to work at multiplication time but not with
- // a direct call to __mul__.
-
- DynamicMetaObject[] newTypes = new DynamicMetaObject[2];
- newTypes[0] = types[0];
- newTypes[1] = new DynamicMetaObject(
- Ast.New(
- typeof(Index).GetConstructor(new Type[] { typeof(object) }),
- AstUtils.Convert(types[1].Expression, typeof(object))
- ),
- BindingRestrictions.Empty
- );
- types = newTypes;
- }
-
- if (!SlotOrFunction.TryGetBinder(state, types, op, null, out fbinder, out fParent)) {
- foreach (PythonType pt in MetaPythonObject.GetPythonType(types[0]).ResolutionOrder) {
- if (pt.TryLookupSlot(state.SharedContext, op, out fSlot)) {
- fParent = pt;
- break;
- }
- }
- }
-
- if (!SlotOrFunction.TryGetBinder(state, types, null, rop, out rbinder, out rParent)) {
- foreach (PythonType pt in MetaPythonObject.GetPythonType(types[1]).ResolutionOrder) {
- if (pt.TryLookupSlot(state.SharedContext, rop, out rSlot)) {
- rParent = pt;
- break;
- }
- }
- }
-
- if (fParent != null && (rbinder.Success || rSlot != null) && rParent != fParent && rParent.IsSubclassOf(fParent)) {
- // Python says if x + subx and subx defines __r*__ we should call r*.
- fbinder = SlotOrFunction.Empty;
- fSlot = null;
- }
-
- if (!fbinder.Success && !rbinder.Success && fSlot == null && rSlot == null) {
- if (op == "__truediv__" || op == "__rtruediv__") {
- // true div on a type which doesn't support it, go ahead and try normal divide
- PythonOperationKind newOp = op == "__truediv__" ? PythonOperationKind.Divide : PythonOperationKind.ReverseDivide;
-
- GetOpreatorMethods(types, newOp, state, out fbinder, out rbinder, out fSlot, out rSlot);
- }
- }
- }
-
- private static bool IsReverseOperator(PythonOperationKind oper) {
- return (oper & PythonOperationKind.Reversed) != 0;
- }
-
- private static bool IsSequence(DynamicMetaObject/*!*/ metaObject) {
- if (typeof(List).IsAssignableFrom(metaObject.GetLimitType()) ||
- typeof(PythonTuple).IsAssignableFrom(metaObject.GetLimitType()) ||
- typeof(String).IsAssignableFrom(metaObject.GetLimitType())) {
- return true;
- }
- return false;
- }
-
- private static DynamicMetaObject/*!*/ MakeBinaryOperatorResult(DynamicMetaObject/*!*/[]/*!*/ types, DynamicMetaObjectBinder/*!*/ operation, PythonOperationKind op, SlotOrFunction/*!*/ fCand, SlotOrFunction/*!*/ rCand, PythonTypeSlot fSlot, PythonTypeSlot rSlot, DynamicMetaObject errorSuggestion) {
- Assert.NotNull(operation, fCand, rCand);
-
- SlotOrFunction fTarget, rTarget;
- PythonContext state = PythonContext.GetPythonContext(operation);
-
- ConditionalBuilder bodyBuilder = new ConditionalBuilder(operation);
-
- if ((op & PythonOperationKind.InPlace) != 0) {
- // in place operator, see if there's a specific method that handles it.
- SlotOrFunction function = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), Symbols.OperatorToSymbol(op), types);
-
- // we don't do a coerce for in place operators if the lhs implements __iop__
- if (!MakeOneCompareGeneric(function, false, types, MakeCompareReturn, bodyBuilder, typeof(object))) {
- // the method handles it and always returns a useful value.
- return bodyBuilder.GetMetaObject(types);
- }
- }
-
- if (!SlotOrFunction.GetCombinedTargets(fCand, rCand, out fTarget, out rTarget) &&
- fSlot == null &&
- rSlot == null &&
- !ShouldCoerce(state, op, types[0], types[1], false) &&
- !ShouldCoerce(state, op, types[1], types[0], false) &&
- bodyBuilder.NoConditions) {
- return MakeRuleForNoMatch(operation, op, errorSuggestion, types);
- }
-
- if (ShouldCoerce(state, op, types[0], types[1], false) &&
- (op != PythonOperationKind.Mod || !MetaPythonObject.GetPythonType(types[0]).IsSubclassOf(TypeCache.String))) {
- // need to try __coerce__ first.
- DoCoerce(state, bodyBuilder, op, types, false);
- }
-
- if (MakeOneTarget(PythonContext.GetPythonContext(operation), fTarget, fSlot, bodyBuilder, false, types)) {
- if (ShouldCoerce(state, op, types[1], types[0], false)) {
- // need to try __coerce__ on the reverse first
- DoCoerce(state, bodyBuilder, op, new DynamicMetaObject[] { types[1], types[0] }, true);
- }
-
- if (rSlot != null) {
- MakeSlotCall(PythonContext.GetPythonContext(operation), types, bodyBuilder, rSlot, true);
- bodyBuilder.FinishCondition(MakeBinaryThrow(operation, op, types).Expression, typeof(object));
- } else if (MakeOneTarget(PythonContext.GetPythonContext(operation), rTarget, rSlot, bodyBuilder, false, types)) {
- // need to fallback to throwing or coercion
- bodyBuilder.FinishCondition(MakeBinaryThrow(operation, op, types).Expression, typeof(object));
- }
- }
-
- return bodyBuilder.GetMetaObject(types);
- }
-
- private static void MakeCompareReturn(ConditionalBuilder/*!*/ bodyBuilder, Expression retCondition, Expression/*!*/ retValue, bool isReverse, Type retType) {
- if (retCondition != null) {
- bodyBuilder.AddCondition(retCondition, retValue);
- } else {
- bodyBuilder.FinishCondition(retValue, retType);
- }
- }
-
- /// <summary>
- /// Delegate for finishing the comparison. This takes in a condition and a return value and needs to update the ConditionalBuilder
- /// with the appropriate resulting body. The condition may be null.
- /// </summary>
- private delegate void ComparisonHelper(ConditionalBuilder/*!*/ bodyBuilder, Expression retCondition, Expression/*!*/ retValue, bool isReverse, Type retType);
-
- /// <summary>
- /// Helper to handle a comparison operator call. Checks to see if the call can
- /// return NotImplemented and allows the caller to modify the expression that
- /// is ultimately returned (e.g. to turn __cmp__ into a bool after a comparison)
- /// </summary>
- private static bool MakeOneCompareGeneric(SlotOrFunction/*!*/ target, bool reverse, DynamicMetaObject/*!*/[]/*!*/ types, ComparisonHelper returner, ConditionalBuilder/*!*/ bodyBuilder, Type retType) {
- if (target == SlotOrFunction.Empty || !target.Success) return true;
-
- ParameterExpression tmp;
-
- if (target.ReturnType == typeof(bool)) {
- tmp = bodyBuilder.CompareRetBool;
- } else {
- tmp = Ast.Variable(target.ReturnType, "compareRetValue");
- bodyBuilder.AddVariable(tmp);
- }
-
- if (target.MaybeNotImplemented) {
- Expression call = target.Target.Expression;
- Expression assign = Ast.Assign(tmp, call);
-
- returner(
- bodyBuilder,
- Ast.NotEqual(
- assign,
- AstUtils.Constant(PythonOps.NotImplemented)
- ),
- tmp,
- reverse,
- retType);
- return true;
- } else {
- returner(
- bodyBuilder,
- null,
- target.Target.Expression,
- reverse,
- retType
- );
- return false;
- }
- }
-
- private static bool MakeOneTarget(PythonContext/*!*/ state, SlotOrFunction/*!*/ target, PythonTypeSlot slotTarget, ConditionalBuilder/*!*/ bodyBuilder, bool reverse, DynamicMetaObject/*!*/[]/*!*/ types) {
- if (target == SlotOrFunction.Empty && slotTarget == null) return true;
-
- if (slotTarget != null) {
- MakeSlotCall(state, types, bodyBuilder, slotTarget, reverse);
- return true;
- } else if (target.MaybeNotImplemented) {
- Debug.Assert(target.ReturnType == typeof(object));
-
- ParameterExpression tmp = Ast.Variable(typeof(object), "slot");
- bodyBuilder.AddVariable(tmp);
-
- bodyBuilder.AddCondition(
- Ast.NotEqual(
- Ast.Assign(
- tmp,
- target.Target.Expression
- ),
- Ast.Property(null, typeof(PythonOps).GetProperty("NotImplemented"))
- ),
- tmp
- );
-
- return true;
- } else {
- bodyBuilder.FinishCondition(target.Target.Expression, typeof(object));
- return false;
- }
- }
-
- private static void MakeSlotCall(PythonContext/*!*/ state, DynamicMetaObject/*!*/[]/*!*/ types, ConditionalBuilder/*!*/ bodyBuilder, PythonTypeSlot/*!*/ slotTarget, bool reverse) {
- Debug.Assert(slotTarget != null);
-
- Expression self, other;
- if (reverse) {
- self = types[1].Expression;
- other = types[0].Expression;
- } else {
- self = types[0].Expression;
- other = types[1].Expression;
- }
-
- MakeSlotCallWorker(state, slotTarget, self, bodyBuilder, other);
- }
-
- private static void MakeSlotCallWorker(PythonContext/*!*/ state, PythonTypeSlot/*!*/ slotTarget, Expression/*!*/ self, ConditionalBuilder/*!*/ bodyBuilder, params Expression/*!*/[]/*!*/ args) {
- // Generate:
- //
- // SlotTryGetValue(context, slot, selfType, out callable) && (tmp=callable(args)) != NotImplemented) ?
- // tmp :
- // RestOfOperation
- //
- ParameterExpression callable = Ast.Variable(typeof(object), "slot");
- ParameterExpression tmp = Ast.Variable(typeof(object), "slot");
-
- bodyBuilder.AddCondition(
- Ast.AndAlso(
- Ast.Call(
- typeof(PythonOps).GetMethod("Slot…
Large files files are truncated, but you can click here to view the full file