public override DynamicMetaObject /*!*/ GetExpression(PythonBinder /*!*/ binder) { PythonOverloadResolver resolve; if (_creating.IsSystemType || _creating.HasSystemCtor) { resolve = new PythonOverloadResolver( binder, Arguments.Arguments, Arguments.Signature, CodeContext ); } else { resolve = new PythonOverloadResolver( binder, ArrayUtils.Insert(Arguments.Self, Arguments.Arguments), GetDynamicNewSignature(), CodeContext ); } return(binder.CallMethod( resolve, _creating.UnderlyingSystemType.GetConstructors(), Arguments.Self.Restrictions, _creating.Name )); }
public override DynamicMetaObject /*!*/ GetExpression(PythonBinder /*!*/ binder) { PythonOverloadResolver resolver; if (_creating.IsSystemType || _creating.HasSystemCtor) { resolver = new PythonOverloadResolver(binder, DynamicMetaObject.EmptyMetaObjects, new CallSignature(0), CodeContext); } else { resolver = new PythonOverloadResolver(binder, new[] { Arguments.Self }, new CallSignature(1), CodeContext); } return(binder.CallMethod(resolver, _creating.UnderlyingSystemType.GetConstructors(), BindingRestrictions.Empty, _creating.Name)); }
/// <summary> /// Tries to get a MethodBinder associated with the slot for the specified type. /// /// If a method is found the binder is set and true is returned. /// If nothing is found binder is null and true is returned. /// If something other than a method is found false is returned. /// /// TODO: Remove rop /// </summary> internal static bool TryGetBinder(PythonContext /*!*/ state, DynamicMetaObject /*!*/[] /*!*/ types, string op, string rop, out SlotOrFunction /*!*/ res, out PythonType declaringType) { declaringType = null; DynamicMetaObject xType = types[0]; BuiltinFunction xBf; if (!BindingHelpers.TryGetStaticFunction(state, op, xType, out xBf)) { res = SlotOrFunction.Empty; return(false); } xBf = CheckAlwaysNotImplemented(xBf); BindingTarget bt; DynamicMetaObject binder; DynamicMetaObject yType = null; BuiltinFunction yBf = null; if (types.Length > 1) { yType = types[1]; if (!BindingHelpers.IsSubclassOf(xType, yType) && !BindingHelpers.TryGetStaticFunction(state, rop, yType, out yBf)) { res = SlotOrFunction.Empty; return(false); } yBf = CheckAlwaysNotImplemented(yBf); } if (yBf == xBf) { yBf = null; } else if (yBf != null && BindingHelpers.IsSubclassOf(yType, xType)) { xBf = null; } var mc = new PythonOverloadResolver( state.Binder, types, new CallSignature(types.Length), AstUtils.Constant(state.SharedContext) ); if (xBf == null) { if (yBf == null) { binder = null; bt = null; } else { declaringType = DynamicHelpers.GetPythonTypeFromType(yBf.DeclaringType); binder = state.Binder.CallMethod(mc, yBf.Targets, BindingRestrictions.Empty, null, PythonNarrowing.None, PythonNarrowing.BinaryOperator, out bt); } } else { if (yBf == null) { declaringType = DynamicHelpers.GetPythonTypeFromType(xBf.DeclaringType); binder = state.Binder.CallMethod(mc, xBf.Targets, BindingRestrictions.Empty, null, PythonNarrowing.None, PythonNarrowing.BinaryOperator, out bt); } else { List <MethodBase> targets = new List <MethodBase>(); targets.AddRange(xBf.Targets); foreach (MethodBase mb in yBf.Targets) { if (!ContainsMethodSignature(targets, mb)) { targets.Add(mb); } } binder = state.Binder.CallMethod(mc, targets.ToArray(), BindingRestrictions.Empty, null, PythonNarrowing.None, PythonNarrowing.BinaryOperator, out bt); foreach (MethodBase mb in yBf.Targets) { if (bt.Overload.ReflectionInfo == mb) { declaringType = DynamicHelpers.GetPythonTypeFromType(yBf.DeclaringType); break; } } if (declaringType == null) { declaringType = DynamicHelpers.GetPythonTypeFromType(xBf.DeclaringType); } } } if (binder != null) { res = new SlotOrFunction(bt, binder); } else { res = SlotOrFunction.Empty; } Debug.Assert(res != null); return(true); }
public override DynamicMetaObject/*!*/ CompleteRuleTarget(DynamicMetaObjectBinder/*!*/ metaBinder, DynamicMetaObject/*!*/[]/*!*/ args, Func<DynamicMetaObject> customFailure) { Assert.NotNull(args); Assert.NotNullItems(args); BindingTarget target; var resolver = new PythonOverloadResolver( Binder, args[0], ArrayUtils.RemoveFirst(args), new CallSignature(args.Length - 1), AstUtils.Constant(PythonContext.SharedContext) ); DynamicMetaObject res = Binder.CallMethod( resolver, _bf.Targets, BindingRestrictions.Combine(args), _bf.Name, PythonNarrowing.None, PythonNarrowing.IndexOperator, out target ); res = BindingHelpers.CheckLightThrowMO(metaBinder, res, target); if (target.Success) { if (IsSetter) { res = new DynamicMetaObject( Ast.Block(res.Expression, args[args.Length - 1].Expression), res.Restrictions ); } WarningInfo info; if (BindingWarnings.ShouldWarn(Binder.Context, target.Overload, out info)) { res = info.AddWarning(Ast.Constant(PythonContext.SharedContext), res); } } else if (customFailure == null || (res = customFailure()) == null) { res = DefaultBinder.MakeError(resolver.MakeInvalidParametersError(target), BindingRestrictions.Combine(ConvertArgs(args)), typeof(object)); } return res; }
/// <summary> /// Tries to get a MethodBinder associated with the slot for the specified type. /// /// If a method is found the binder is set and true is returned. /// If nothing is found binder is null and true is returned. /// If something other than a method is found false is returned. /// /// TODO: Remove rop /// </summary> internal static bool TryGetBinder(PythonContext/*!*/ state, DynamicMetaObject/*!*/[]/*!*/ types, string op, string rop, out SlotOrFunction/*!*/ res, out PythonType declaringType) { declaringType = null; DynamicMetaObject xType = types[0]; BuiltinFunction xBf; if (!BindingHelpers.TryGetStaticFunction(state, op, xType, out xBf)) { res = SlotOrFunction.Empty; return false; } xBf = CheckAlwaysNotImplemented(xBf); BindingTarget bt; DynamicMetaObject binder; DynamicMetaObject yType = null; BuiltinFunction yBf = null; if (types.Length > 1) { yType = types[1]; if (!BindingHelpers.IsSubclassOf(xType, yType) && !BindingHelpers.TryGetStaticFunction(state, rop, yType, out yBf)) { res = SlotOrFunction.Empty; return false; } yBf = CheckAlwaysNotImplemented(yBf); } if (yBf == xBf) { yBf = null; } else if (yBf != null && BindingHelpers.IsSubclassOf(yType, xType)) { xBf = null; } var mc = new PythonOverloadResolver( state.Binder, types, new CallSignature(types.Length), AstUtils.Constant(state.SharedContext) ); if (xBf == null) { if (yBf == null) { binder = null; bt = null; } else { declaringType = DynamicHelpers.GetPythonTypeFromType(yBf.DeclaringType); binder = state.Binder.CallMethod(mc, yBf.Targets, BindingRestrictions.Empty, null, PythonNarrowing.None, PythonNarrowing.BinaryOperator, out bt); } } else { if (yBf == null) { declaringType = DynamicHelpers.GetPythonTypeFromType(xBf.DeclaringType); binder = state.Binder.CallMethod(mc, xBf.Targets, BindingRestrictions.Empty, null, PythonNarrowing.None, PythonNarrowing.BinaryOperator, out bt); } else { List<MethodBase> targets = new List<MethodBase>(); targets.AddRange(xBf.Targets); foreach (MethodBase mb in yBf.Targets) { if (!ContainsMethodSignature(targets, mb)) targets.Add(mb); } binder = state.Binder.CallMethod(mc, targets.ToArray(), BindingRestrictions.Empty, null, PythonNarrowing.None, PythonNarrowing.BinaryOperator, out bt); foreach (MethodBase mb in yBf.Targets) { if (bt.Overload.ReflectionInfo == mb) { declaringType = DynamicHelpers.GetPythonTypeFromType(yBf.DeclaringType); break; } } if (declaringType == null) { declaringType = DynamicHelpers.GetPythonTypeFromType(xBf.DeclaringType); } } } if (binder != null) { res = new SlotOrFunction(bt, binder); } else { res = SlotOrFunction.Empty; } Debug.Assert(res != null); return true; }
private DynamicMetaObject/*!*/ MakeSelfCall(DynamicMetaObjectBinder/*!*/ call, Expression/*!*/ codeContext, DynamicMetaObject/*!*/[]/*!*/ args) { BindingRestrictions selfRestrict = Restrictions.Merge( BindingRestrictionsHelpers.GetRuntimeTypeRestriction( Expression, typeof(BuiltinFunction) ) ).Merge( BindingRestrictions.GetExpressionRestriction( Value.MakeBoundFunctionTest( AstUtils.Convert(Expression, typeof(BuiltinFunction)) ) ) ); Expression instance = Ast.Property( AstUtils.Convert( Expression, typeof(BuiltinFunction) ), typeof(BuiltinFunction).GetProperty("__self__") ); DynamicMetaObject self = GetInstance(instance, CompilerHelpers.GetType(Value.__self__)); return Value.MakeBuiltinFunctionCall( call, codeContext, this, ArrayUtils.Insert(self, args), true, // has self selfRestrict, (newArgs) => { CallSignature signature = BindingHelpers.GetCallSignature(call); DynamicMetaObject res; BinderState state = BinderState.GetBinderState(call); BindingTarget target; PythonOverloadResolver resolver; if (Value.IsReversedOperator) { resolver = new PythonOverloadResolver( state.Binder, newArgs, GetReversedSignature(signature), codeContext ); } else { resolver = new PythonOverloadResolver( state.Binder, self, args, signature, codeContext ); } res = state.Binder.CallMethod( resolver, Value.Targets, self.Restrictions, Value.Name, NarrowingLevel.None, Value.IsBinaryOperator ? PythonNarrowing.BinaryOperator : NarrowingLevel.All, out target ); return new BuiltinFunction.BindingResult(target, res); } ); }
public override DynamicMetaObject/*!*/ GetExpression(PythonBinder/*!*/ binder) { PythonOverloadResolver resolve; if (_creating.IsSystemType || _creating.HasSystemCtor) { resolve = new PythonOverloadResolver( binder, Arguments.Arguments, Arguments.Signature, CodeContext ); } else { resolve = new PythonOverloadResolver( binder, ArrayUtils.Insert(Arguments.Self, Arguments.Arguments), GetDynamicNewSignature(), CodeContext ); } return binder.CallMethod( resolve, _creating.UnderlyingSystemType.GetConstructors(), Arguments.Self.Restrictions, _creating.Name ); }
public override DynamicMetaObject/*!*/ GetExpression(PythonBinder/*!*/ binder) { PythonOverloadResolver resolver; if (_creating.IsSystemType || _creating.HasSystemCtor) { resolver = new PythonOverloadResolver(binder, DynamicMetaObject.EmptyMetaObjects, new CallSignature(0), CodeContext); } else { resolver = new PythonOverloadResolver(binder, new[] { Arguments.Self }, new CallSignature(1), CodeContext); } return binder.CallMethod(resolver, _creating.UnderlyingSystemType.GetConstructors(), BindingRestrictions.Empty, _creating.Name); }
private DynamicMetaObject /*!*/ MakeSelfCall(DynamicMetaObjectBinder /*!*/ call, Expression /*!*/ codeContext, DynamicMetaObject /*!*/[] /*!*/ args) { BindingRestrictions selfRestrict = Restrictions.Merge( BindingRestrictionsHelpers.GetRuntimeTypeRestriction( Expression, LimitType ) ).Merge( BindingRestrictions.GetExpressionRestriction( Value.MakeBoundFunctionTest( AstUtils.Convert(Expression, typeof(BuiltinFunction)) ) ) ); Expression instance = Ast.Call( typeof(PythonOps).GetMethod("GetBuiltinFunctionSelf"), AstUtils.Convert( Expression, typeof(BuiltinFunction) ) ); DynamicMetaObject self = GetInstance(instance, CompilerHelpers.GetType(Value.BindingSelf)); return(Value.MakeBuiltinFunctionCall( call, codeContext, this, ArrayUtils.Insert(self, args), true, // has self selfRestrict, (newArgs) => { CallSignature signature = BindingHelpers.GetCallSignature(call); DynamicMetaObject res; PythonContext state = PythonContext.GetPythonContext(call); BindingTarget target; PythonOverloadResolver resolver; if (Value.IsReversedOperator) { resolver = new PythonOverloadResolver( state.Binder, newArgs, GetReversedSignature(signature), codeContext ); } else { resolver = new PythonOverloadResolver( state.Binder, self, args, signature, codeContext ); } res = state.Binder.CallMethod( resolver, Value.Targets, self.Restrictions, Value.Name, NarrowingLevel.None, Value.IsBinaryOperator ? PythonNarrowing.BinaryOperator : NarrowingLevel.All, out target ); return BindingHelpers.CheckLightThrow(call, res, target); } )); }
public override DynamicMetaObject/*!*/ CompleteRuleTarget(DynamicMetaObject/*!*/[]/*!*/ args, Func<DynamicMetaObject> customFailure) { Assert.NotNull(args); Assert.NotNullItems(args); BindingTarget target; var resolver = new PythonOverloadResolver( Binder, args[0], ArrayUtils.RemoveFirst(args), new CallSignature(args.Length - 1), AstUtils.Constant(BinderState.Context) ); DynamicMetaObject res = Binder.CallMethod( resolver, _bf.Targets, BindingRestrictions.Combine(args), _bf.Name, PythonNarrowing.None, PythonNarrowing.IndexOperator, out target ); if (target.Success) { if (IsSetter) { res = new DynamicMetaObject( Ast.Block(res.Expression, args[args.Length - 1].Expression), res.Restrictions ); } } else if (customFailure == null || (res = customFailure()) == null) { res = DefaultBinder.MakeError(resolver.MakeInvalidParametersError(target), BindingRestrictions.Combine(ConvertArgs(args))); } return res; }