public Type GetBestType(OverloadResolver /*!*/ resolver, Dictionary <Type, Type> /*!*/ binding, Dictionary <DynamicMetaObject, BindingRestrictions> /*!*/ restrictions) { Type curType = null; for (int i = 0; i < _parameterTypes.Count; i++) { Type nextType = GetInferedType(resolver, _genericParam, _parameterTypes[i], _inputs[i], binding, restrictions); if (nextType == null) { // no mapping available return(null); } else if (curType == null || curType.IsAssignableFrom(nextType)) { curType = nextType; } else if (!nextType.IsAssignableFrom(curType)) { // inconsistent constraint. return(null); } else { curType = nextType; } } return(curType); }
protected override Expression DoResolve(ResolveContext ec) { TypeSpec del_type = InstanceExpr.Type; if (del_type == null) { return(null); } // // Do only core overload resolution the rest of the checks has been // done on primary expression // method = Delegate.GetInvokeMethod(del_type); var res = new OverloadResolver(new MemberSpec[] { method }, OverloadResolver.Restrictions.DelegateInvoke, loc); var valid = res.ResolveMember <MethodSpec> (ec, ref arguments); if (valid == null && !res.BestCandidateIsDynamic) { return(null); } type = method.ReturnType; if (conditionalAccessReceiver) { type = LiftMemberType(ec, type); } eclass = ExprClass.Value; return(this); }
/// <summary> /// Provides generic type inference for a single parameter. /// </summary> /// <remarks> /// For example: /// M{T}(T x) /// M{T}(IList{T} x) /// M{T}(ref T x) /// M{T}(T[] x) /// M{T}(ref Dictionary{T,T}[] x) /// </remarks> internal static Type GetInferedType(OverloadResolver /*!*/ resolver, Type /*!*/ genericParameter, Type /*!*/ parameterType, DynamicMetaObject /*!*/ input, Dictionary <Type, Type> /*!*/ binding, Dictionary <DynamicMetaObject, BindingRestrictions> /*!*/ restrictions) { if (parameterType.IsSubclassOf(typeof(Delegate))) { // see if we have an invokable object which can be used to infer into this delegate IInferableInvokable invokeInfer = input as IInferableInvokable; if (invokeInfer != null) { InferenceResult inference = invokeInfer.GetInferredType(parameterType, genericParameter); if (inference != null) { if (inference.Restrictions != BindingRestrictions.Empty) { restrictions[input] = inference.Restrictions; } binding[genericParameter] = inference.Type; if (ConstraintsViolated(inference.Type, genericParameter, binding)) { return(null); } return(inference.Type); } } } return(GetInferedType(genericParameter, parameterType, resolver.GetGenericInferenceType(input), input.LimitType, binding)); }
public OverloadableOperation(Operator op, params Operand[] operands) { this.op = op; this.operands = operands; List <ApplicableFunction> candidates = null; foreach (Operand operand in operands) { if ((object)operand != null && !operand.Type.IsPrimitive) { // try overloads candidates = op.FindUserCandidates(operands); break; } } if (candidates == null) { candidates = OverloadResolver.FindApplicable(op.GetStandardCandidates(operands), operands); } if (candidates == null) { throw new InvalidOperationException(string.Format(null, Properties.Messages.ErrInvalidOperation, op.methodName, string.Join(", ", Array.ConvertAll <Operand, string>(operands, Operand.GetTypeName)))); } af = OverloadResolver.FindBest(candidates); if (af == null) { throw new AmbiguousMatchException(Properties.Messages.ErrAmbiguousBinding); } }
void PrepareAfNormally(ITypeMapper typeMapper, Operand[] afOperands) { List <ApplicableFunction> candidates = null; foreach (Operand operand in afOperands) { if ((object)operand != null && !operand.GetReturnType(typeMapper).IsPrimitive) { // try overloads candidates = _op.FindUserCandidates(typeMapper, afOperands); break; } } if (candidates == null) { candidates = OverloadResolver.FindApplicable(_op.GetStandardCandidates(typeMapper, afOperands), typeMapper, afOperands); } if (candidates == null) { throw new InvalidOperationException( string.Format( null, Properties.Messages.ErrInvalidOperation, _op.MethodName, string.Join(", ", ConvertAll <Operand, string>(afOperands, op => op.GetReturnType(typeMapper).FullName)))); } _af = OverloadResolver.FindBest(candidates, typeMapper); }
internal protected override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { Debug.Assert(hasBeenUsed.Length == args.Length); Debug.Assert(_index < args.Length); Debug.Assert(!hasBeenUsed[Index]); hasBeenUsed[_index] = true; return(resolver.Convert(args.GetObject(_index), args.GetType(_index), ParameterInfo, _parameterType)); }
internal override Expression ToReturnExpression(OverloadResolver resolver) { if (_isRef) { return(_tmp); } return(GetDefaultValue()); }
protected internal override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { Type dictType = ParameterInfo.ParameterType; return Expression.Call( GetCreationDelegate(dictType).GetMethodInfo(), Expression.NewArrayInit(typeof(string), ConstantNames()), AstUtils.NewArrayHelper(typeof(object), GetParameters(args, hasBeenUsed)) ); }
public override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { if (_isRef) { return(_tmp ?? (_tmp = resolver.GetTemporary(_parameterType, "outParam"))); } return(GetDefaultValue()); }
public BinderFactory() { var overloadResolver = new OverloadResolver(new ArgumentConverter()); _memberResolver = new MemberResolver(); _indexResolver = new IndexResolver(overloadResolver); _methodResolver = new MethodResolver(overloadResolver); _operatorResolver = new OperatorResolver(overloadResolver); }
internal protected override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { if (_tmp == null) { _tmp = resolver.GetTemporary(Type, "outParam"); } return(Ast.Block(Ast.Assign(_tmp, base.ToExpression(resolver, args, hasBeenUsed)), _tmp)); }
internal override Expression UpdateFromReturn(OverloadResolver resolver, RestrictedArguments args) { return(Expression.Assign( Expression.Field( Expression.Convert(args.GetObject(Index).Expression, Type), Type.GetField("Value") ), _tmp )); }
internal ParameterMapping(OverloadResolver resolver, OverloadInfo method, IList <string> argNames) { Assert.NotNull(resolver, method); _resolver = resolver; _overload = method; _argNames = argNames; _parameters = new List <ParameterWrapper>(); _arguments = new List <ArgBuilder>(method.ParameterCount); _defaultArguments = new List <ArgBuilder>(); }
public override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { Debug.Assert(BuilderExpectsSingleParameter(_builder)); int index = GetKeywordIndex(args.Length); Debug.Assert(!hasBeenUsed[index]); hasBeenUsed[index] = true; return(_builder.ToExpression(resolver, MakeRestrictedArg(args, index), new bool[1])); }
protected internal override Func <object[], object> ToDelegate(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { Func <object[], object> conv = resolver.GetConvertor(_index + 1, args.GetObject(_index), ParameterInfo, _parameterType); if (conv != null) { return(conv); } return((Func <object[], object>)Delegate.CreateDelegate( typeof(Func <object[], object>), _index + 1, typeof(ArgBuilder).GetMethod("ArgumentRead"))); }
protected internal override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { Type dictType = ParameterInfo.ParameterType; var names = ConstantNames(); var expressions = GetParameters(args, hasBeenUsed); Debug.Assert(names.Length == expressions.Count); return(Expression.Call( GetCreationDelegate(dictType).GetMethodInfo(), Expression.NewArrayInit(typeof(string), names), AstUtils.NewArrayHelper(typeof(object), expressions) )); }
// TODO: revisit private static Expression ConvertToHelper(OverloadResolver resolver, Expression value, Type type) { if (type == value.Type) { return(value); } if (type.IsAssignableFrom(value.Type)) { return(AstUtils.Convert(value, type)); } return(resolver.GetDynamicConversion(value, type)); }
protected override Expression DoResolve(ResolveContext rc) { if (rc.IsRuntimeBinder) { if (UnwrapLeft == null && !Left.Type.IsNullableType) { Left = LiftOperand(rc, Left); } if (UnwrapRight == null && !Right.Type.IsNullableType) { Right = LiftOperand(rc, Right); } } else { if (UnwrapLeft == null && Left != null && Left.Type.IsNullableType) { Left = Unwrap.CreateUnwrapped(Left); UnwrapLeft = Left as Unwrap; } if (UnwrapRight == null && Right != null && Right.Type.IsNullableType) { Right = Unwrap.CreateUnwrapped(Right); UnwrapRight = Right as Unwrap; } if (Left.Type.BuiltinType == BuiltinTypeSpec.Type.Decimal) { var decimal_operators = MemberCache.GetUserOperator(Left.Type, Binary.ConvertBinaryToUserOperator(Binary.Oper), false); Arguments args = new Arguments(2); args.Add(new Argument(Left)); args.Add(new Argument(Right)); const OverloadResolver.Restrictions restr = OverloadResolver.Restrictions.ProbingOnly | OverloadResolver.Restrictions.NoBaseMembers | OverloadResolver.Restrictions.BaseMembersIncluded; var res = new OverloadResolver(decimal_operators, restr, loc); UserOperator = res.ResolveOperator(rc, ref args); } } type = Binary.Type; eclass = Binary.eclass; return(this); }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference")] // TODO internal protected virtual Expression ToExpression(ref MethodInfo method, OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { if (_index == -1) { return(AstUtils.Constant(null)); } Debug.Assert(hasBeenUsed.Length == args.Length); Debug.Assert(_index < args.Length); Debug.Assert(!hasBeenUsed[_index]); hasBeenUsed[_index] = true; GetCallableMethod(args, ref method); return(resolver.Convert(args.GetObject(_index), args.GetType(_index), null, method.DeclaringType)); }
public override Type GetInferedType(OverloadResolver resolver, Type genericParameter, DynamicMetaObject input, Dictionary <Type, Type> prevConstraints, Dictionary <DynamicMetaObject, BindingRestrictions> restrictions) { Type inputType = resolver.GetGenericInferenceType(input); if (inputType != null) { prevConstraints[genericParameter] = inputType; if (ConstraintsViolated(inputType, genericParameter, prevConstraints)) { return(null); } } return(inputType); }
public override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { object value = ParameterInfo.GetDefaultValue(); if (value is Missing) { value = CompilerHelpers.GetMissingValue(ParameterInfo.ParameterType); } if (ParameterInfo.ParameterType.IsByRef) { return(AstUtils.Constant(value, ParameterInfo.ParameterType.GetElementType())); } var metaValue = new DynamicMetaObject(AstUtils.Constant(value), BindingRestrictions.Empty, value); return(resolver.Convert(metaValue, CompilerHelpers.GetType(value), ParameterInfo, ParameterInfo.ParameterType)); }
protected internal override Func <object[], object> ToDelegate(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { string[] names = _names; int[] indexes = GetParameters(hasBeenUsed); Type dictType = ParameterInfo.ParameterType; Func <string[], object[], object> func = GetCreationDelegate(dictType); return((actualArgs) => { object[] values = new object[indexes.Length]; for (int i = 0; i < indexes.Length; i++) { values[i] = actualArgs[indexes[i] + 1]; } return func(names, values); }); }
internal MethodCandidate(OverloadResolver resolver, OverloadInfo method, List <ParameterWrapper> parameters, ParameterWrapper paramsDict, ReturnBuilder returnBuilder, InstanceBuilder instanceBuilder, IList <ArgBuilder> argBuilders, Dictionary <DynamicMetaObject, BindingRestrictions> restrictions) { Assert.NotNull(resolver, method, instanceBuilder, returnBuilder); Assert.NotNullItems(parameters); Assert.NotNullItems(argBuilders); Resolver = resolver; Overload = method; _instanceBuilder = instanceBuilder; ArgBuilders = argBuilders; ReturnBuilder = returnBuilder; _parameters = parameters; _paramsDict = paramsDict; Restrictions = restrictions; ParamsArrayIndex = ParameterWrapper.IndexOfParamsArray(parameters); parameters.TrimExcess(); }
protected internal override Func <object[], object> ToDelegate(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { if (ParameterInfo.ParameterType.IsByRef) { return(null); } else if (ParameterInfo.DefaultValue is Missing && CompilerHelpers.GetMissingValue(ParameterInfo.ParameterType) is Missing) { // reflection throws when we do this return(null); } object val = ParameterInfo.DefaultValue; if (val is Missing) { val = CompilerHelpers.GetMissingValue(ParameterInfo.ParameterType); } Debug.Assert(val != Missing.Value); return((_) => val); }
internal override Expression ToExpression(OverloadResolver resolver, IList <ArgBuilder> builders, RestrictedArguments args, Expression ret) { if (_returnArgs.Count == 1) { if (_returnArgs[0] == -1) { return(ret); } return(Ast.Block(ret, builders[_returnArgs[0]].ToReturnExpression(resolver))); } Expression[] retValues = new Expression[_returnArgs.Count]; int rIndex = 0; bool usesRet = false; foreach (int index in _returnArgs) { if (index == -1) { usesRet = true; retValues[rIndex++] = ret; } else { retValues[rIndex++] = builders[index].ToReturnExpression(resolver); } } Expression retArray = AstUtils.NewArrayHelper(typeof(object), retValues); if (!usesRet) { retArray = Ast.Block(ret, retArray); } return(resolver.GetByRefArrayExpression(retArray)); }
void PrepareAf(ITypeMapper typeMapper) { if (_af != null) { return; } List <ApplicableFunction> candidates = null; foreach (Operand operand in _operands) { if ((object)operand != null && !operand.GetReturnType(typeMapper).IsPrimitive) { // try overloads candidates = _op.FindUserCandidates(typeMapper, _operands); break; } } if (candidates == null) { candidates = OverloadResolver.FindApplicable(_op.GetStandardCandidates(typeMapper, _operands), typeMapper, _operands); } if (candidates == null) { throw new InvalidOperationException(string.Format(null, Messages.ErrInvalidOperation, _op.MethodName, string.Join(", ", Array.ConvertAll <Operand, string>(_operands, op => op.GetReturnType(typeMapper).FullName)))); } _af = OverloadResolver.FindBest(candidates, typeMapper); if (_af == null) { throw new AmbiguousMatchException(Messages.ErrAmbiguousBinding); } }
protected override Expression DoResolve (ResolveContext ec) { if (InstanceExpr is EventExpr) { ((EventExpr) InstanceExpr).Error_CannotAssign (ec); return null; } TypeSpec del_type = InstanceExpr.Type; if (del_type == null) return null; // // Do only core overload resolution the rest of the checks has been // done on primary expression // method = Delegate.GetInvokeMethod (ec.Compiler, del_type); var res = new OverloadResolver (new MemberSpec[] { method }, OverloadResolver.Restrictions.DelegateInvoke, loc); var valid = res.ResolveMember<MethodSpec> (ec, ref arguments); if (valid == null && !res.BestCandidateIsDynamic) return null; type = method.ReturnType; eclass = ExprClass.Value; return this; }
protected override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed) { return(AstUtils.Constant(Activator.CreateInstance(ParameterInfo.ParameterType))); }
protected override Expression DoResolve (ResolveContext ec) { TypeSpec del_type = InstanceExpr.Type; if (del_type == null) return null; // // Do only core overload resolution the rest of the checks has been // done on primary expression // method = Delegate.GetInvokeMethod (del_type); var res = new OverloadResolver (new MemberSpec[] { method }, OverloadResolver.Restrictions.DelegateInvoke, loc); var valid = res.ResolveMember<MethodSpec> (ec, ref arguments); if (valid == null && !res.BestCandidateIsDynamic) return null; type = method.ReturnType; if (conditionalAccessReceiver) type = LiftMemberType (ec, type); eclass = ExprClass.Value; return this; }
protected override Expression /*!*/ ToExpression(ref MethodInfo /*!*/ method, OverloadResolver /*!*/ resolver, RestrictedArguments /*!*/ args, bool[] /*!*/ hasBeenUsed) { return(((RubyOverloadResolver)resolver)._args.TargetExpression); }
protected override Expression ToExpression(OverloadResolver /*!*/ resolver, RestrictedArguments /*!*/ args, bool[] /*!*/ hasBeenUsed) { return(((RubyOverloadResolver)resolver).ScopeExpression); }
protected override Expression ToExpression(OverloadResolver /*!*/ resolver, RestrictedArguments /*!*/ args, bool[] /*!*/ hasBeenUsed) { return(AstUtils.Constant(Activator.CreateInstance(ParameterInfo.ParameterType, ((RubyOverloadResolver)resolver).Context))); }