protected override MSA.Expression /*!*/ VisitDynamic(MSA.DynamicExpression /*!*/ node) { var callAction = node.Binder as RubyCallAction; if (callAction != null) { var args = new MSA.Expression[node.Arguments.Count]; for (int i = 0; i < args.Length; i++) { args[i] = node.Arguments[i]; } Debug.Assert(args.Length > 0); int last = args.Length - 1; args[last] = typeof(TracingRubyCallAction).GetMethod("EnterCallSite").MakeGenericMethod(args[last].Type).OpCall( args[last], AstUtils.Constant(_sourceId), AstUtils.Constant(_sites[node].Start.Index) ); return(Ast.Dynamic( new TracingRubyCallAction(callAction.MethodName, callAction.Signature), node.Type, args )); } else { return(base.VisitDynamic(node)); } }
protected override Expression VisitDynamic(DynamicExpression node) { var expr = CallStatic(node); if (expr == null) return base.VisitDynamic(node); else return expr; }
internal static DynamicConvert DynamicConvert(DynamicExpression expression) { return new DynamicConvert() { Type = TypeRef.Serialize(expression.Type), Arguments = expression.Arguments.Select(Serialize).ToArray(), }; }
internal static DynamicGetIndex DynamicGetIndex(DynamicExpression expression) { return new DynamicGetIndex() { Type = TypeRef.Serialize(expression.Type), Arguments = expression.Arguments.Select(Serialize).ToArray(), ArgumentNames = ((YacqGetIndexBinder) expression.Binder).CallInfo.ArgumentNames.ToArray(), }; }
protected override Expression VisitDynamic(DynamicExpression node) { CallSiteBinder binder = node.Binder; if (!(binder is InterceptorSiteBinder)) { binder = new InterceptorSiteBinder(binder); return Expression.MakeDynamic(node.DelegateType, binder, node.Arguments); } else { return node; } }
internal static DynamicBinaryOperation DynamicBinaryOperation(DynamicExpression expression) { return new DynamicBinaryOperation() { Type = TypeRef.Serialize(expression.Type), Operation = ((YacqBinaryOperationBinder) expression.Binder).Operation, Arguments = expression.Arguments.Select(Serialize).ToArray(), }; }
internal static DynamicSetMember DynamicSetMember(DynamicExpression expression) { return new DynamicSetMember() { Type = TypeRef.Serialize(expression.Type), Arguments = expression.Arguments.Select(Serialize).ToArray(), Name = ((YacqSetMemberBinder) expression.Binder).Name, }; }
/// <summary> /// Visits the children of the <see cref="DynamicExpression" />. /// </summary> /// <param name="node">The expression to visit.</param> /// <returns>The modified expression, if it or any subexpression was modified; /// otherwise, returns the original expression.</returns> protected internal virtual Expression VisitDynamic(DynamicExpression node) { Expression[] a = ExpressionVisitorUtils.VisitArguments(this, node); if (a == null) { return node; } return node.Rewrite(a); }
private Expression CallStatic(DynamicExpression node) { var binder = node.Binder as InvokeBinder; if (binder == null) return null; var target = node.Arguments[0] as ParameterExpression; if (target == null) return null; SuffixFuncInfo info; if (_dic.TryGetValue(target, out info) == false) return null; var expr = SuffixFunc.SortAndInvoke(info.FuncExpr, info.Suffix, binder.CallInfo, i => node.Arguments[i + 1]); if (expr is InvocationExpression) info.IsUsed = true; return expr; }
protected override Expression VisitDynamic(DynamicExpression node) { Type siteType = typeof(CallSite<>).MakeGenericType(node.DelegateType); // Rewite call site as constant var siteExpr = VisitConstant(Expression.Constant(DynamicSiteHelpers.MakeSite(node.Binder, siteType))); // Rewrite all of the arguments var args = Visit(node.Arguments); var siteVar = Expression.Variable(siteExpr.Type, "$site"); // ($site = siteExpr).Target.Invoke($site, *args) return Expression.Block( new [] { siteVar }, Expression.Call( Expression.Field( Expression.Assign(siteVar, siteExpr), siteType.GetField("Target") ), node.DelegateType.GetMethod("Invoke"), ArrayUtils.Insert(siteVar, args) ) ); }
/// <summary> /// Reduces the provided DynamicExpression into site.Target(site, *args). /// </summary> public Expression ReduceDyn(DynamicExpression node) { MaybeInit(); Type delegateType; if (RewriteDelegate(node.DelegateType, out delegateType)) { node = Expression.MakeDynamic(delegateType, node.Binder, node.Arguments); } CallSite cs = CallSite.Create(node.DelegateType, node.Binder); Expression access = RewriteCallSite(cs, _typeGen); // ($site = siteExpr).Target.Invoke($site, *args) ParameterExpression site = Expression.Variable(cs.GetType(), "$site"); return Expression.Block( new[] { site }, Expression.Call( Expression.Field( Expression.Assign(site, access), cs.GetType().GetField("Target") ), node.DelegateType.GetMethod("Invoke"), DynUtils.ArrayInsert(site, node.Arguments) ) ); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided /// <see cref="CallSiteBinder" /> and three arguments. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <param name="arg2">The third argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2) { return(DynamicExpression.MakeDynamic(delegateType, binder, arg0, arg1, arg2)); }
/// <summary> /// Visits the children of <see cref="System.Linq.Expressions.DynamicExpression"/>. /// </summary> /// <param name="node">The expression to visit.</param> /// <returns>The modified expression, if it or any subexpression was modified; otherwise, /// returns the original expression.</returns> protected override Expression VisitDynamic(System.Linq.Expressions.DynamicExpression node) { throw new NotSupportedException(string.Format(Resources.EX_PROCESS_NODE_NOT_SUPPORT, node.GetType().Name)); }
protected override Expression VisitDynamic(DynamicExpression node) { throw new NotSupportedException("Dynamic expressions are not supported"); }
protected override Expression VisitDynamic(System.Linq.Expressions.DynamicExpression node) { throw new NotSupportedException($"Node type {node.GetType().Name} is not supported."); }
protected override Expression VisitDynamic(DynamicExpression node) { DynamicMetaObjectBinder metaBinder = node.Binder as DynamicMetaObjectBinder; if (metaBinder == null) { // don't rewrite non meta-binder nodes, we can't compose them return node; } // gather the real arguments for the new dynamic site node var args = node.Arguments; bool foundSideEffectingArgs = false; List<Expression> inputs = new List<Expression>(); // parameter mapping is 1 List<ComboParameterMappingInfo> for each meta binder, the inner list // contains the mapping info for each particular binder List<BinderMappingInfo> binders = new List<BinderMappingInfo>(); List<ParameterMappingInfo> myInfo = new List<ParameterMappingInfo>(); int actionCount = 0; for (int i = 0; i < args.Count; i++) { Expression e = args[i]; if (!foundSideEffectingArgs) { // attempt to combine the arguments... Expression rewritten = Visit(e); ComboDynamicSiteExpression combo = rewritten as ComboDynamicSiteExpression; ConstantExpression ce; if (combo != null) { // an action expression we can combine with our own expression // remember how many actions we have so far - if any of our children consume // actions their offset is bumped up int baseActionCount = actionCount; foreach (BinderMappingInfo comboInfo in combo.Binders) { List<ParameterMappingInfo> newInfo = new List<ParameterMappingInfo>(); foreach (ParameterMappingInfo info in comboInfo.MappingInfo) { if (info.IsParameter) { // all of the inputs from the child now become ours newInfo.Add(ParameterMappingInfo.Parameter(inputs.Count)); inputs.Add(combo.Inputs[info.ParameterIndex]); } else if (info.IsAction) { newInfo.Add(ParameterMappingInfo.Action(info.ActionIndex + baseActionCount)); actionCount++; } else { Debug.Assert(info.Constant != null); // constants can just flow through newInfo.Add(info); } } binders.Add(new BinderMappingInfo(comboInfo.Binder, newInfo)); } myInfo.Add(ParameterMappingInfo.Action(actionCount++)); } else if ((ce = rewritten as ConstantExpression) != null) { // we can hoist the constant into the combo myInfo.Add(ParameterMappingInfo.Fixed(ce)); } else if (IsSideEffectFree(rewritten)) { // we can treat this as an input parameter myInfo.Add(ParameterMappingInfo.Parameter(inputs.Count)); inputs.Add(rewritten); } else { // this argument is doing something we don't understand - we have to leave // it as is (an input we consume) and all the remaining arguments need to be // evaluated normally as this could have side effects on them. foundSideEffectingArgs = true; myInfo.Add(ParameterMappingInfo.Parameter(inputs.Count)); inputs.Add(e); } } else { // we've already seen an argument which may have side effects, don't do // any more combinations. myInfo.Add(ParameterMappingInfo.Parameter(inputs.Count)); inputs.Add(e); } } binders.Add(new BinderMappingInfo(metaBinder, myInfo)); // TODO: Remove any duplicate inputs (e.g. locals being fed in multiple times) return new ComboDynamicSiteExpression(node.Type, binders, inputs.ToArray()); }
// Reduce dynamic expression so that the lambda can be emitted as a non-dynamic method. protected override Expression VisitDynamic(DynamicExpression node) { return Visit(CompilerHelpers.Reduce(node)); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <param name="arg2">The third argument to the dynamic operation.</param> /// <param name="arg3">The fourth argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1, Expression arg2, Expression arg3) => DynamicExpression.Dynamic(binder, returnType, arg0, arg1, arg2, arg3);
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, params Expression[] arguments) => DynamicExpression.Dynamic(binder, returnType, arguments);
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, IEnumerable <Expression>?arguments) => DynamicExpression.MakeDynamic(delegateType, binder, arguments);
protected override Expression VisitDynamic(System.Linq.Expressions.DynamicExpression node) { throw new NotSupportedException(); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" /> and four arguments. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <param name="arg2">The third argument to the dynamic operation.</param> /// <param name="arg3">The fourth argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2, Expression arg3) => DynamicExpression.MakeDynamic(delegateType, binder, arg0, arg1, arg2, arg3);
protected override Expression VisitDynamic(DynamicExpression node) { // 動的な実行はそのまま return node; }
public DynamicExpressionProxy(DynamicExpression node) { _node = node; }
public static void EmitDynamicCallPreamble(DynamicExpression dyn, IPersistentMap spanMap, string methodName, Type returnType, List<ParameterExpression> paramExprs, Type[] paramTypes, CljILGen ilg, out LambdaExpression lambda, out Type delType, out MethodBuilder mbLambda) { Expression call = dyn; GenContext context = Compiler.CompilerContextVar.deref() as GenContext; if (context != null && context.DynInitHelper != null) call = context.DynInitHelper.ReduceDyn(dyn); if (returnType == typeof(void)) { call = Expression.Block(call, Expression.Default(typeof(object))); returnType = typeof(object); } else if (returnType != call.Type) { call = Expression.Convert(call, returnType); } call = GenContext.AddDebugInfo(call, spanMap); delType = Microsoft.Scripting.Generation.Snippets.Shared.DefineDelegate("__interop__", returnType, paramTypes); lambda = Expression.Lambda(delType, call, paramExprs); mbLambda = null; if (context == null) { // light compile Delegate d = lambda.Compile(); int key = RT.nextID(); CacheDelegate(key, d); ilg.EmitInt(key); ilg.Emit(OpCodes.Call, Method_MethodExpr_GetDelegate); ilg.Emit(OpCodes.Castclass, delType); } else { mbLambda = context.TB.DefineMethod(methodName, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, returnType, paramTypes); lambda.CompileToMethod(mbLambda); } }
private static string VisitDynamic(DynamicExpression node) { throw new NotImplementedException(); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided /// <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1) { return(DynamicExpression.Dynamic(binder, returnType, arg0, arg1)); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, IEnumerable <Expression> arguments) => DynamicExpression.Dynamic(binder, returnType, arguments);
protected override Expression VisitDynamic(DynamicExpression node) { throw new NotSupportedException(); }
}//end static method internal XElement DynamicExpressionToXElement(DynamicExpression e) { object value; string xName = "DynamicExpression"; object[] XElementValues = new object[6]; value = ((DynamicExpression)e).Type; XElementValues[0] = GenerateXmlFromProperty(typeof(System.Type), "Type", value ?? string.Empty); value = ((DynamicExpression)e).NodeType; XElementValues[1] = GenerateXmlFromProperty(typeof(System.Linq.Expressions.ExpressionType), "NodeType", value ?? string.Empty); value = ((DynamicExpression)e).Binder; XElementValues[2] = GenerateXmlFromProperty(typeof(System.Runtime.CompilerServices.CallSiteBinder), "Binder", value ?? string.Empty); value = ((DynamicExpression)e).DelegateType; XElementValues[3] = GenerateXmlFromProperty(typeof(System.Type), "DelegateType", value ?? string.Empty); value = ((DynamicExpression)e).Arguments; XElementValues[4] = GenerateXmlFromProperty(typeof(System.Collections.ObjectModel.ReadOnlyCollection<System.Linq.Expressions.Expression>), "Arguments", value ?? string.Empty); value = ((DynamicExpression)e).CanReduce; XElementValues[5] = GenerateXmlFromProperty(typeof(System.Boolean), "CanReduce", value ?? string.Empty); return new XElement(xName, XElementValues); }//end static method
internal MSAst.Expression TransformAndDynamicConvert(Expression expression, Type /*!*/ type) { Debug.Assert(expression != null); MSAst.Expression res = expression; // Do we need conversion? if (!CanAssign(type, expression.Type)) { // ensure we're reduced before we check for dynamic expressions. var reduced = expression.Reduce(); if (reduced is LightDynamicExpression) { reduced = reduced.Reduce(); } // Add conversion step to the AST MSAst.DynamicExpression ae = reduced as MSAst.DynamicExpression; ReducableDynamicExpression rde = reduced as ReducableDynamicExpression; if ((ae != null && ae.Binder is PythonBinaryOperationBinder) || (rde != null && rde.Binder is PythonBinaryOperationBinder)) { // create a combo site which does the conversion PythonBinaryOperationBinder binder; IList <MSAst.Expression> args; if (ae != null) { binder = (PythonBinaryOperationBinder)ae.Binder; args = ArrayUtils.ToArray(ae.Arguments); } else { binder = (PythonBinaryOperationBinder)rde.Binder; args = rde.Args; } ParameterMappingInfo[] infos = new ParameterMappingInfo[args.Count]; for (int i = 0; i < infos.Length; i++) { infos[i] = ParameterMappingInfo.Parameter(i); } res = Expression.Dynamic( GlobalParent.PyContext.BinaryOperationRetType( binder, GlobalParent.PyContext.Convert( type, ConversionResultKind.ExplicitCast ) ), type, args ); } else { res = GlobalParent.Convert( type, ConversionResultKind.ExplicitCast, reduced ); } } return(res); }
private Variable VisitDynamic(DynamicExpression node) { //this.Out(FormatBinder(node.Binder)); //this.VisitExpressions<Expression>('(', node.Arguments, ')'); throw new NotSupportedException("Expression of type " + node.NodeType + " is not supported"); }
protected override MSAst.Expression VisitDynamic(MSAst.DynamicExpression node) { return(VisitCall(base.VisitDynamic(node))); }
internal override void TraceCallSite(Expression /*!*/ expression, MSA.DynamicExpression /*!*/ callSite) { _sites.Add(callSite, expression.Location); }
protected override MSAst.Expression /*!*/ VisitDynamic(MSAst.DynamicExpression /*!*/ node) { return(_profiler.AddInnerProfiling(node, _tick, _profileIndex)); }
internal static DynamicInvokeMember DynamicInvokeMember(DynamicExpression expression) { return new DynamicInvokeMember() { Type = TypeRef.Serialize(expression.Type), Arguments = expression.Arguments.Select(Serialize).ToArray(), Name = ((YacqInvokeMemberBinder) expression.Binder).Name, ArgumentNames = ((YacqInvokeMemberBinder) expression.Binder).CallInfo.ArgumentNames.ToArray(), }; }