internal override System.Linq.Expressions.Expression Call(Expression context, ActionBinder binder, params Expression[] arguments) { if (Method.IsPublic && Method.DeclaringType.IsVisible) { // TODO: Need to use MethodBinder in here to make this right. return(binder.MakeCallExpression(context, Method, arguments)); } //methodInfo.Invoke(obj, object[] params) if (Method.IsStatic) { return(Ast.Convert( Ast.Call( Ast.Constant(Method), typeof(MethodInfo).GetMethod("Invoke", new Type[] { typeof(object), typeof(object[]) }), Ast.Constant(null), AstUtils.NewArrayHelper(typeof(object), arguments) ), Method.ReturnType)); } if (arguments.Length == 0) { throw Error.NoInstanceForCall(); } return(Ast.Convert( Ast.Call( Ast.Constant(Method), typeof(MethodInfo).GetMethod("Invoke", new Type[] { typeof(object), typeof(object[]) }), arguments[0], AstUtils.NewArrayHelper(typeof(object), ArrayUtils.RemoveFirst(arguments)) ), Method.ReturnType)); }
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)) ); }
internal protected override Expression ToExpression(ParameterBinder parameterBinder, IList <Expression> parameters, bool[] hasBeenUsed) { Expression res = Ast.Call( typeof(BinderOps).GetMethod("MakeSymbolDictionary"), Ast.NewArrayInit(typeof(SymbolId), ConstantNames()), AstUtils.NewArrayHelper(typeof(object), GetParameters(parameters, hasBeenUsed)) ); return(res); }
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) )); }
internal override DynamicMetaObject Call(OverloadResolverFactory resolverFactory, ActionBinder binder, params DynamicMetaObject[] arguments) { if (Method.IsPublic && Method.DeclaringType.IsVisible()) { return(binder.MakeCallExpression(resolverFactory, Method, arguments)); } //methodInfo.Invoke(obj, object[] params) if (Method.IsStatic) { return(new DynamicMetaObject( Expression.Convert( Expression.Call( AstUtils.Constant(Method), typeof(MethodInfo).GetMethod("Invoke", new Type[] { typeof(object), typeof(object[]) }), AstUtils.Constant(null), AstUtils.NewArrayHelper(typeof(object), ArrayUtils.ConvertAll(arguments, x => x.Expression)) ), Method.ReturnType ), BindingRestrictions.Empty ) ); } if (arguments.Length == 0) { throw Error.NoInstanceForCall(); } return(new DynamicMetaObject( Expression.Convert( Expression.Call( AstUtils.Constant(Method), typeof(MethodInfo).GetMethod("Invoke", new Type[] { typeof(object), typeof(object[]) }), arguments[0].Expression, AstUtils.NewArrayHelper(typeof(object), ArrayUtils.ConvertAll(ArrayUtils.RemoveFirst(arguments), x => x.Expression)) ), Method.ReturnType ), BindingRestrictions.Empty )); }
public static MSA.Expression /*!*/ MakeArrayOpCall(List <MSA.Expression> /*!*/ args) { Assert.NotNull(args); switch (args.Count) { case 0: return(Methods.MakeArray0.OpCall()); case 1: return(Methods.MakeArray1.OpCall(AstFactory.Box(args[0]))); case 2: return(Methods.MakeArray2.OpCall(AstFactory.Box(args[0]), AstFactory.Box(args[1]))); case 3: return(Methods.MakeArray3.OpCall(AstFactory.Box(args[0]), AstFactory.Box(args[1]), AstFactory.Box(args[2]))); case 4: return(Methods.MakeArray4.OpCall(AstFactory.Box(args[0]), AstFactory.Box(args[1]), AstFactory.Box(args[2]), AstFactory.Box(args[3]))); case 5: return(Methods.MakeArray5.OpCall(AstFactory.Box(args[0]), AstFactory.Box(args[1]), AstFactory.Box(args[2]), AstFactory.Box(args[3]), AstFactory.Box(args[4]))); default: Debug.Assert(args.Count > Runtime.RubyOps.OptimizedOpCallParamCount); return(Methods.MakeArrayN.OpCall(AstUtils.NewArrayHelper(typeof(object), args))); } }
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)); }
internal Expression MakeExpression(RestrictedArguments restrictedArgs) { bool[] usageMarkers; Expression[] spilledArgs; Expression[] callArgs = GetArgumentExpressions(restrictedArgs, out usageMarkers, out spilledArgs); Expression call; MethodBase mb = _overload.ReflectionInfo; // TODO: make MakeExpression virtual on OverloadInfo? if (mb == null) { throw new InvalidOperationException("Cannot generate an expression for an overload w/o MethodBase"); } MethodInfo mi = mb as MethodInfo; if (mi != null) { Expression instance; if (mi.IsStatic) { instance = null; } else { Debug.Assert(mi != null); instance = _instanceBuilder.ToExpression(ref mi, _resolver, restrictedArgs, usageMarkers); Debug.Assert(instance != null, "Can't skip instance expression"); } if (CompilerHelpers.IsVisible(mi)) { call = AstUtils.SimpleCallHelper(instance, mi, callArgs); } else { call = Ast.Call( typeof(BinderOps).GetMethod("InvokeMethod"), AstUtils.Constant(mi), instance != null ? AstUtils.Convert(instance, typeof(object)) : AstUtils.Constant(null), AstUtils.NewArrayHelper(typeof(object), callArgs) ); } } else { ConstructorInfo ci = (ConstructorInfo)mb; if (CompilerHelpers.IsVisible(ci)) { call = AstUtils.SimpleNewHelper(ci, callArgs); } else { call = Ast.Call( typeof(BinderOps).GetMethod("InvokeConstructor"), AstUtils.Constant(ci), AstUtils.NewArrayHelper(typeof(object), callArgs) ); } } if (spilledArgs != null) { call = Expression.Block(spilledArgs.AddLast(call)); } Expression ret = _returnBuilder.ToExpression(_resolver, _argBuilders, restrictedArgs, call); List <Expression> updates = null; for (int i = 0; i < _argBuilders.Count; i++) { Expression next = _argBuilders[i].UpdateFromReturn(_resolver, restrictedArgs); if (next != null) { if (updates == null) { updates = new List <Expression>(); } updates.Add(next); } } if (updates != null) { if (ret.Type != typeof(void)) { ParameterExpression temp = Ast.Variable(ret.Type, "$ret"); updates.Insert(0, Ast.Assign(temp, ret)); updates.Add(temp); ret = Ast.Block(new[] { temp }, updates.ToArray()); } else { updates.Insert(0, ret); ret = Ast.Block(typeof(void), updates.ToArray()); } } if (_resolver.Temps != null) { ret = Ast.Block(_resolver.Temps, ret); } return(ret); }
internal Expression MakeExpression(ParameterBinder parameterBinder, IList <Expression> parameters) { bool[] usageMarkers; Expression[] spilledArgs; Expression[] args = GetArgumentExpressions(parameterBinder, parameters, out usageMarkers, out spilledArgs); MethodBase mb = Method; MethodInfo mi = mb as MethodInfo; Expression ret, call; if (!mb.IsPublic || (mb.DeclaringType != null && !mb.DeclaringType.IsVisible)) { if (mi != null) { mi = CompilerHelpers.GetCallableMethod(mi, _binder._binder.PrivateBinding); if (mi != null) { mb = mi; } } } ConstructorInfo ci = mb as ConstructorInfo; Debug.Assert(mi != null || ci != null); if (mb.IsPublic && (mb.DeclaringType == null || mb.DeclaringType.IsVisible)) { // public method if (mi != null) { Expression instance = mi.IsStatic ? null : _instanceBuilder.ToExpression(parameterBinder, parameters, usageMarkers); call = AstUtils.SimpleCallHelper(instance, mi, args); } else { call = AstUtils.SimpleNewHelper(ci, args); } } else { // Private binding, invoke via reflection if (mi != null) { Expression instance = mi.IsStatic ? Ast.Constant(null) : _instanceBuilder.ToExpression(parameterBinder, parameters, usageMarkers); Debug.Assert(instance != null, "Can't skip instance expression"); call = Ast.Call( typeof(BinderOps).GetMethod("InvokeMethod"), Ast.Constant(mi), AstUtils.Convert(instance, typeof(object)), AstUtils.NewArrayHelper(typeof(object), args) ); } else { call = Ast.Call( typeof(BinderOps).GetMethod("InvokeConstructor"), Ast.Constant(ci), AstUtils.NewArrayHelper(typeof(object), args) ); } } if (spilledArgs != null) { call = Expression.Block(spilledArgs.AddLast(call)); } ret = _returnBuilder.ToExpression(parameterBinder, _argBuilders, parameters, call); List <Expression> updates = null; for (int i = 0; i < _argBuilders.Count; i++) { Expression next = _argBuilders[i].UpdateFromReturn(parameterBinder, parameters); if (next != null) { if (updates == null) { updates = new List <Expression>(); } updates.Add(next); } } if (updates != null) { if (ret.Type != typeof(void)) { ParameterExpression temp = Ast.Variable(ret.Type, "$ret"); updates.Insert(0, Ast.Assign(temp, ret)); updates.Add(temp); ret = Ast.Block(new [] { temp }, updates.ToArray()); } else { updates.Insert(0, ret); ret = Ast.Convert( Ast.Block(updates.ToArray()), typeof(void) ); } } if (parameterBinder.Temps != null) { ret = Ast.Block(parameterBinder.Temps, ret); } return(ret); }