public Expression SetValue(Compiler compiler, Expression rhs) { ParameterExpression left = Expression.Variable(rhs.Type); int count = this.ArrayLiteral.Elements.Count; List<Expression> expressions = new List<Expression> { Expression.Assign(left, rhs) }; for (int i = 0; i < count; i++) { Expression expression2 = Expression.Call(left, CachedReflectionInfo.IList_get_Item, new Expression[] { ExpressionCache.Constant(i) }); ExpressionAst ast = this.ArrayLiteral.Elements[i]; ArrayLiteralAst pureExpression = ast as ArrayLiteralAst; if (ast is ParenExpressionAst) { pureExpression = ((ParenExpressionAst) ast).Pipeline.GetPureExpression() as ArrayLiteralAst; } if (pureExpression != null) { expression2 = Expression.Dynamic(PSArrayAssignmentRHSBinder.Get(pureExpression.Elements.Count), typeof(IList), expression2); } expressions.Add(compiler.ReduceAssignment((ISupportsAssignment) ast, TokenKind.Equals, expression2)); } expressions.Add(left); return Expression.Block(new ParameterExpression[] { left }, expressions); }
public Expression SetValue(Compiler compiler, Expression rhs) { StringConstantExpressionAst member = this.MemberExpression.Member as StringConstantExpressionAst; if (member != null) { return Expression.Dynamic(PSSetMemberBinder.Get(member.Value, this.MemberExpression.Static), typeof(object), this.CachedTarget ?? this.GetTargetExpr(compiler), rhs); } return Expression.Dynamic(PSSetDynamicMemberBinder.Get(this.MemberExpression.Static), typeof(object), this.CachedTarget ?? this.GetTargetExpr(compiler), this.CachedPropertyExpr ?? this.GetPropertyExpr(compiler), rhs); }
Expression IAssignableValue.SetValue(Compiler compiler, Expression rhs) { List<AttributeBaseAst> attributes = this.GetAttributes(); IAssignableValue assignableValue = this.GetActualAssignableAst().GetAssignableValue(); VariableExpressionAst ast = assignableValue as VariableExpressionAst; if (ast == null) { return assignableValue.SetValue(compiler, Compiler.ConvertValue(rhs, attributes)); } return Compiler.CallSetVariable(Expression.Constant(ast.VariablePath), rhs, Expression.Constant(attributes.ToArray())); }
private IEnumerable<Expression> GetArgumentExprs(Compiler compiler) { if (this._argExprTemps != null) { return this._argExprTemps; } if (this.InvokeMemberExpressionAst.Arguments != null) { return this.InvokeMemberExpressionAst.Arguments.Select<ExpressionAst, Expression>(new Func<ExpressionAst, Expression>(compiler.Compile)).ToArray<Expression>(); } return new Expression[0]; }
public Expression SetValue(Compiler compiler, Expression rhs) { PSMethodInvocationConstraints invokeMemberConstraints = Compiler.GetInvokeMemberConstraints(this.InvokeMemberExpressionAst); StringConstantExpressionAst member = this.InvokeMemberExpressionAst.Member as StringConstantExpressionAst; Expression targetExpr = this.GetTargetExpr(compiler); IEnumerable<Expression> argumentExprs = this.GetArgumentExprs(compiler); if (member == null) { throw new NotImplementedException("invoke method dynamic name"); } return Compiler.InvokeMember(member.Value, invokeMemberConstraints, targetExpr, argumentExprs.Append<Expression>(rhs), false, true); }
private static object CompileAndInvoke(Ast ast) { object obj2; try { Compiler visitor = new Compiler { CompilingConstantExpression = true }; obj2 = Expression.Lambda((Expression) ast.Accept(visitor), new ParameterExpression[0]).Compile().DynamicInvoke(new object[0]); } catch (TargetInvocationException exception) { throw exception.InnerException; } return obj2; }
public Expression SetValue(Compiler compiler, Expression rhs) { Expression expression3; ParameterExpression element = Expression.Variable(rhs.Type); ArrayLiteralAst index = this.IndexExpressionAst.Index as ArrayLiteralAst; PSMethodInvocationConstraints invocationConstraints = this.GetInvocationConstraints(); Expression targetExpr = this.GetTargetExpr(compiler); if (index != null) { expression3 = Expression.Dynamic(PSSetIndexBinder.Get(index.Elements.Count, invocationConstraints), typeof(object), index.Elements.Select<ExpressionAst, Expression>(new Func<ExpressionAst, Expression>(compiler.Compile)).Prepend<Expression>(targetExpr).Append<Expression>(element)); } else { expression3 = Expression.Dynamic(PSSetIndexBinder.Get(1, invocationConstraints), typeof(object), targetExpr, this.GetIndexExpr(compiler), element); } return Expression.Block(new ParameterExpression[] { element }, new Expression[] { Expression.Assign(element, rhs), expression3, element }); }
public Expression GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { PSMethodInvocationConstraints invokeMemberConstraints = Compiler.GetInvokeMemberConstraints(this.InvokeMemberExpressionAst); Expression targetExpr = this.GetTargetExpr(compiler); this._targetExprTemp = Expression.Variable(targetExpr.Type); exprs.Add(Expression.Assign(this._targetExprTemp, targetExpr)); IEnumerable<Expression> argumentExprs = this.GetArgumentExprs(compiler); this._argExprTemps = (from arg in argumentExprs select Expression.Variable(arg.Type)).ToArray<ParameterExpression>(); exprs.AddRange((IEnumerable<Expression>) argumentExprs.Zip<Expression, ParameterExpression, BinaryExpression>(this._argExprTemps, (arg, temp) => Expression.Assign(temp, arg))); temps.Add(this._targetExprTemp); temps.AddRange(this._argExprTemps); StringConstantExpressionAst member = this.InvokeMemberExpressionAst.Member as StringConstantExpressionAst; if (member == null) { throw new NotImplementedException("invoke method dynamic name"); } return Compiler.InvokeMember(member.Value, invokeMemberConstraints, this._targetExprTemp, this._argExprTemps, false, false); }
public Expression GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { Expression targetExpr = this.GetTargetExpr(compiler); ParameterExpression item = Expression.Parameter(targetExpr.Type); temps.Add(item); this.CachedTarget = item; exprs.Add(Expression.Assign(item, targetExpr)); StringConstantExpressionAst member = this.MemberExpression.Member as StringConstantExpressionAst; if (member != null) { return Expression.Dynamic(PSGetMemberBinder.Get(member.Value, this.MemberExpression.Static), typeof(object), item); } ParameterExpression expression4 = Expression.Parameter(this.GetPropertyExpr(compiler).Type); temps.Add(expression4); exprs.Add(Expression.Assign(expression4, compiler.Compile(this.MemberExpression.Member))); this.CachedPropertyExpr = expression4; return Expression.Dynamic(PSGetDynamicMemberBinder.Get(this.MemberExpression.Static), typeof(object), item, expression4); }
public Expression GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { Expression right = compiler.Compile(this.IndexExpressionAst.Target); this._targetExprTemp = Expression.Variable(right.Type); temps.Add(this._targetExprTemp); exprs.Add(Expression.Assign(this._targetExprTemp, right)); ExpressionAst index = this.IndexExpressionAst.Index; ArrayLiteralAst ast2 = index as ArrayLiteralAst; PSMethodInvocationConstraints invocationConstraints = this.GetInvocationConstraints(); if (ast2 != null) { return Expression.Dynamic(PSGetIndexBinder.Get(ast2.Elements.Count, invocationConstraints, true), typeof(object), ast2.Elements.Select<ExpressionAst, Expression>(new Func<ExpressionAst, Expression>(compiler.Compile)).Prepend<Expression>(this._targetExprTemp)); } Expression expression3 = compiler.Compile(index); this._indexExprTemp = Expression.Variable(expression3.Type); temps.Add(this._indexExprTemp); exprs.Add(Expression.Assign(this._indexExprTemp, expression3)); return Expression.Dynamic(PSGetIndexBinder.Get(1, invocationConstraints, true), typeof(object), this._targetExprTemp, this._indexExprTemp); }
public Expression GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { var targetExpr = compiler.Compile(IndexExpressionAst.Target); _targetExprTemp = Expression.Variable(targetExpr.Type); temps.Add(_targetExprTemp); exprs.Add(Expression.Assign(_targetExprTemp, targetExpr)); var index = IndexExpressionAst.Index; var arrayLiteral = (index as ArrayLiteralAst); var constraints = GetInvocationConstraints(); Expression result; if (arrayLiteral != null) { // If assignment to slices were allowed, we'd need to save the elements in temps // like we do when doing normal assignment (below). But it's not allowed, so it // doesn't matter. result = DynamicExpression.Dynamic(PSGetIndexBinder.Get(arrayLiteral.Elements.Count, constraints), typeof(object), arrayLiteral.Elements.Select(compiler.Compile).Prepend(_targetExprTemp)); } else { var indexExpr = compiler.Compile(index); _indexExprTemp = Expression.Variable(indexExpr.Type); temps.Add(_indexExprTemp); exprs.Add(Expression.Assign(_indexExprTemp, indexExpr)); result = DynamicExpression.Dynamic(PSGetIndexBinder.Get(1, constraints), typeof(object), _targetExprTemp, _indexExprTemp); } return result; }
private Expression GetIndexExpr(Compiler compiler) { return _indexExprTemp ?? compiler.Compile(IndexExpressionAst.Index); }
private Expression GetTargetExpr(Compiler compiler) { return _targetExprTemp ?? compiler.Compile(IndexExpressionAst.Target); }
Expression IAssignableValue.GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { return (Expression) this.Accept(compiler); }
public Expression SetValue(Compiler compiler, Expression rhs) { Diagnostics.Assert(rhs.Type == typeof(IList), "Code generation must ensure we have an IList to assign from."); var rhsTemp = Expression.Variable(rhs.Type); int count = ArrayLiteral.Elements.Count; var exprs = new List<Expression>(); exprs.Add(Expression.Assign(rhsTemp, rhs)); for (int i = 0; i < count; ++i) { Expression indexedRHS = Expression.Call(rhsTemp, CachedReflectionInfo.IList_get_Item, ExpressionCache.Constant(i)); var lhsElement = ArrayLiteral.Elements[i]; var nestedArrayLHS = lhsElement as ArrayLiteralAst; var parenExpressionAst = lhsElement as ParenExpressionAst; if (parenExpressionAst != null) { nestedArrayLHS = parenExpressionAst.Pipeline.GetPureExpression() as ArrayLiteralAst; } if (nestedArrayLHS != null) { // Need to turn the rhs into an IList indexedRHS = DynamicExpression.Dynamic(PSArrayAssignmentRHSBinder.Get(nestedArrayLHS.Elements.Count), typeof(IList), indexedRHS); } exprs.Add(compiler.ReduceAssignment((ISupportsAssignment)lhsElement, TokenKind.Equals, indexedRHS)); } // Add the temp as the last expression for chained assignment, i.e. $x = $y,$z = 1,2 exprs.Add(rhsTemp); return Expression.Block(new[] { rhsTemp }, exprs); }
public Expression SetValue(Compiler compiler, Expression rhs) { var memberNameAst = MemberExpression.Member as StringConstantExpressionAst; if (memberNameAst != null) { string name = memberNameAst.Value; return DynamicExpression.Dynamic(PSSetMemberBinder.Get(name, compiler._memberFunctionType, MemberExpression.Static), typeof(object), CachedTarget ?? GetTargetExpr(compiler), rhs); } return DynamicExpression.Dynamic(PSSetDynamicMemberBinder.Get(compiler._memberFunctionType, MemberExpression.Static), typeof(object), CachedTarget ?? GetTargetExpr(compiler), CachedPropertyExpr ?? GetPropertyExpr(compiler), rhs); }
public Expression GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { var target = GetTargetExpr(compiler); var targetTemp = Expression.Parameter(target.Type); temps.Add(targetTemp); CachedTarget = targetTemp; exprs.Add(Expression.Assign(targetTemp, target)); var memberNameAst = MemberExpression.Member as StringConstantExpressionAst; if (memberNameAst != null) { string name = memberNameAst.Value; return DynamicExpression.Dynamic(PSGetMemberBinder.Get(name, compiler._memberFunctionType, MemberExpression.Static), typeof(object), targetTemp); } var propertyExpr = GetPropertyExpr(compiler); var propertyNameTemp = Expression.Parameter(propertyExpr.Type); temps.Add(propertyNameTemp); exprs.Add(Expression.Assign(propertyNameTemp, compiler.Compile(MemberExpression.Member))); CachedPropertyExpr = propertyNameTemp; return DynamicExpression.Dynamic(PSGetDynamicMemberBinder.Get(compiler._memberFunctionType, MemberExpression.Static), typeof(object), targetTemp, propertyNameTemp); }
private Expression GetPropertyExpr(Compiler compiler) { return compiler.Compile(MemberExpression.Member); }
private Expression GetTargetExpr(Compiler compiler) { return compiler.Compile(MemberExpression.Expression); }
internal AutomaticVarSaver(Compiler compiler, VariablePath autoVarPath, int automaticVar) { _compiler = compiler; _autoVarPath = autoVarPath; _automaticVar = automaticVar; }
private Tuple<Action<FunctionContext>, Type> CompileTrap(TrapStatementAst trap) { var compiler = new Compiler(_sequencePoints) { _compilingTrap = true }; string funcName = _currentFunctionName + "<trap>"; if (trap.TrapType != null) { funcName += "<" + trap.TrapType.TypeName.Name + ">"; } // We generate code as though we're dotting the trap, but in reality we don't dot it, // a new scope is always created. The code gen for dotting means we can avoid passing // around the array of local variable names. We assume traps don't need to perform well, // and that they rarely if ever actually create any local variables. We still do the // variable analysis though because we must mark automatic variables like $_. var analysis = VariableAnalysis.AnalyzeTrap(trap); compiler.LocalVariablesTupleType = analysis.Item1; compiler.LocalVariablesParameter = Expression.Variable(compiler.LocalVariablesTupleType, "locals"); var lambda = compiler.CompileSingleLambda(trap.Body.Statements, trap.Body.Traps, funcName, null, null, null); return Tuple.Create(lambda.Compile(), compiler.LocalVariablesTupleType); }
public Expression SetValue(Compiler compiler, Expression rhs) { // Save the rhs for multi-assign, i.e. $x = $y[0] = 2 // This seems wrong though - the value $y[0] should be assigned to $x, not 2. // It's not often they would be different values, but if a conversion is involved, they could // be. At any rate, V2 did it this way, so we do as well. var temp = Expression.Variable(rhs.Type); var index = IndexExpressionAst.Index; var arrayLiteral = (index as ArrayLiteralAst); var constraints = GetInvocationConstraints(); var targetExpr = GetTargetExpr(compiler); Expression setExpr; if (arrayLiteral != null) { setExpr = DynamicExpression.Dynamic(PSSetIndexBinder.Get(arrayLiteral.Elements.Count, constraints), typeof(object), arrayLiteral.Elements.Select(compiler.Compile).Prepend(targetExpr).Append(temp)); } else { setExpr = DynamicExpression.Dynamic(PSSetIndexBinder.Get(1, constraints), typeof(object), targetExpr, GetIndexExpr(compiler), temp); } return Expression.Block(new[] { temp }, Expression.Assign(temp, rhs), setExpr, temp); }
public Expression GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { Diagnostics.Assert(false, "Array assignment does not support read/modify/write operators"); return null; }
private IEnumerable<Expression> GetArgumentExprs(Compiler compiler) { if (_argExprTemps != null) { return _argExprTemps; } return InvokeMemberExpressionAst.Arguments == null ? Utils.EmptyArray<Expression>() : (InvokeMemberExpressionAst.Arguments.Select(compiler.Compile)).ToArray(); }
private void ReallyCompile(bool optimize) { var sw = new Stopwatch(); sw.Start(); if (!IsProductCode && SecuritySupport.IsProductBinary(((Ast)_ast).Extent.File)) { this.IsProductCode = true; } bool etwEnabled = ParserEventSource.Log.IsEnabled(); if (etwEnabled) { var extent = _ast.Body.Extent; var text = extent.Text; ParserEventSource.Log.CompileStart(ParserEventSource.GetFileOrScript(extent.File, text), text.Length, optimize); } PerformSecurityChecks(); Compiler compiler = new Compiler(); compiler.Compile(this, optimize); if (!IsProductCode) { TelemetryAPI.ReportScriptTelemetry((Ast)_ast, !optimize, sw.ElapsedMilliseconds); } if (etwEnabled) ParserEventSource.Log.CompileStop(); }
public Expression GetValue(Compiler compiler, List<Expression> exprs, List<ParameterExpression> temps) { var constraints = Compiler.GetInvokeMemberConstraints(InvokeMemberExpressionAst); var targetExpr = GetTargetExpr(compiler); _targetExprTemp = Expression.Variable(targetExpr.Type); exprs.Add(Expression.Assign(_targetExprTemp, targetExpr)); int exprsIndex = exprs.Count; var args = GetArgumentExprs(compiler); _argExprTemps = args.Select(arg => Expression.Variable(arg.Type)).ToArray(); exprs.AddRange(args.Zip(_argExprTemps, (arg, temp) => Expression.Assign(temp, arg))); temps.Add(_targetExprTemp); int tempsIndex = temps.Count; temps.AddRange(_argExprTemps); var memberNameAst = InvokeMemberExpressionAst.Member as StringConstantExpressionAst; if (memberNameAst != null) { return compiler.InvokeMember(memberNameAst.Value, constraints, _targetExprTemp, _argExprTemps, @static: false, propertySet: false); } var memberNameExpr = GetMemberNameExpr(compiler); _memberNameExprTemp = Expression.Variable(memberNameExpr.Type); exprs.Insert(exprsIndex, Expression.Assign(_memberNameExprTemp, memberNameExpr)); temps.Insert(tempsIndex, _memberNameExprTemp); return compiler.InvokeDynamicMember(_memberNameExprTemp, constraints, _targetExprTemp, _argExprTemps, @static: false, propertySet: false); }
private Expression GetTargetExpr(Compiler compiler) { return (this._targetExprTemp ?? compiler.Compile(this.InvokeMemberExpressionAst.Expression)); }
public Expression SetValue(Compiler compiler, Expression rhs) { var constraints = Compiler.GetInvokeMemberConstraints(InvokeMemberExpressionAst); var memberNameAst = InvokeMemberExpressionAst.Member as StringConstantExpressionAst; var target = GetTargetExpr(compiler); var args = GetArgumentExprs(compiler); if (memberNameAst != null) { return compiler.InvokeMember(memberNameAst.Value, constraints, target, args.Append(rhs), @static: false, propertySet: true); } var memberNameExpr = GetMemberNameExpr(compiler); return compiler.InvokeDynamicMember(memberNameExpr, constraints, target, args.Append(rhs), @static: false, propertySet: true); }
private Expression GetMemberNameExpr(Compiler compiler) { return _memberNameExprTemp ?? compiler.Compile(InvokeMemberExpressionAst.Member); }
private static object CompileAndInvoke(Ast ast) { try { var compiler = new Compiler { CompilingConstantExpression = true }; return Expression.Lambda((Expression)ast.Accept(compiler)).Compile().DynamicInvoke(); } catch (TargetInvocationException tie) { throw tie.InnerException; } }