protected internal override Expression VisitGoto(GotoExpression node) { Type type = node.Value.Type; if (!typeof(IQueryable).IsAssignableFrom(type)) { return(base.VisitGoto(node)); } LabelTarget target = VisitLabelTarget(node.Target); Expression value = Visit(node.Value); return(Expression.MakeGoto(node.Kind, target, value, GetEquivalentType(typeof(EnumerableQuery).IsAssignableFrom(type) ? value.Type : type))); }
private ParsedGotoExpression Serialize(GotoExpression expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } return(new ParsedGotoExpression( expression.Kind, expression.Target == null ? null : Serialize(expression.Target), expression.Type == null ? null : Serialize(expression.Type), expression.Value == null ? null : Serialize(expression.Value))); }
private GotoExpression VisitReturn(GotoExpression @goto) { codeWriter.Write("return", currentIndent); if (@goto.Value != null) { codeWriter.Write(" "); insideExpressionCount++; Visit(@goto.Value); insideExpressionCount--; } codeWriter.NewLine(); return(null); }
protected override Expression VisitGoto(GotoExpression node) { var target = node.Target; if (!_labels.Contains(target)) { var data = default(LeaveLabelData); if (!LeaveLabels.TryGetValue(target, out data)) { var parameter = default(ParameterExpression); if (target.Type != typeof(void)) { parameter = Expression.Parameter(target.Type, FormattableString.Invariant($"__goto{LeaveLabels.Count}")); } data = new LeaveLabelData { Index = LeaveLabels.Count + 1, Target = target, Value = parameter }; LeaveLabels.Add(target, data); } var res = default(Expression); if (data.Value != null) { res = Expression.Block( Expression.Assign(_pendingBranch, Helpers.CreateConstantInt32(data.Index)), Expression.Assign(data.Value, node.Value), Expression.Goto(_exit, node.Type) ); } else { res = Expression.Block( Expression.Assign(_pendingBranch, Helpers.CreateConstantInt32(data.Index)), Expression.Goto(_exit, node.Type) ); } return(res); } return(base.VisitGoto(node)); }
/// <summary> /// 根据一个类型并使用参数创建一个类型实例 /// </summary> /// <param name="type">要创建实例的类型</param> /// <param name="arguments">要使用的参数</param> /// <returns></returns> public static object CreateInstance(Type type, params object[] arguments) { var key = type.Name; var types = Type.EmptyTypes; if (arguments != null) { types = new Type[arguments.Length]; for (int i = 0; i < arguments.Length; i++) { types[i] = arguments[i].GetType(); key = key + "|" + types[i].Name; } } //find in cache if (Cache.ContainsKey(key)) { return((Cache[key] as Func <object[], object>)(arguments)); } var constructor = type.GetConstructor(types); var parms = constructor.GetParameters(); var exps = new List <Expression>(); LabelTarget target = Expression.Label(typeof(object)); var args = Expression.Parameter(typeof(object[]), "arguments"); for (int i = 0; i < parms.Length; i++) { var method = typeof(object[]).GetMethod("GetValue", new[] { typeof(int) }); MethodCallExpression med = Expression.Call(args, method, Expression.Constant(i)); var c = Expression.Convert(med, parms[i].ParameterType); exps.Add(c); } var nexpression = Expression.New(constructor, exps); GotoExpression ret = Expression.Return(target, nexpression); LabelExpression lbl = Expression.Label(target, Expression.Constant(null)); var block = Expression.Block(ret, lbl); var lam = Expression.Lambda <Func <object[], object> >(block, args); var del = lam.Compile(); Cache.Add(key, del); return(del(arguments)); }
protected override IEnumerable <Expression> GetShortCircuitReturns(GotoExpression returnNull, ObjectMapperData mapperData) { if (!mapperData.Context.IsForDerivedType && mapperData.TargetMemberIsEnumerableElement()) { yield return(Expression.IfThen(mapperData.SourceObject.GetIsDefaultComparison(), returnNull)); } var alreadyMappedShortCircuit = GetAlreadyMappedObjectShortCircuitOrNull(returnNull.Target, mapperData); if (alreadyMappedShortCircuit != null) { yield return(alreadyMappedShortCircuit); } }
protected override Expression VisitGoto(GotoExpression node) { if (node.Target.Type == typeof(void)) { return(base.VisitGoto(node)); } LabelInfo info = GetLabelInfo(node.Target); return(Expression.Block( MakeAssign(info.Temp, Visit(node.Value)), Expression.MakeGoto(node.Kind, info.NewLabel, null, node.Type) )); }
protected override Expression VisitGoto(GotoExpression expression) { ArgumentUtility.CheckNotNull("expression", expression); var vistedExpression = base.VisitGoto(expression); // Testing the parent expression is only required if all children are evaluatable if (_isCurrentSubtreeEvaluatable) { _isCurrentSubtreeEvaluatable = _evaluatableExpressionFilter.IsEvaluatableGoto(expression); } return(vistedExpression); }
protected override Expression VisitGoto(GotoExpression node) { Out(node.Kind.ToString().ToLower(CultureInfo.CurrentCulture)); DumpLabel(node.Target); if (node.Value != null) { Out(" ("); Visit(node.Value); Out(") "); } return(node); }
protected override Expression VisitGoto(GotoExpression node) { Out(node.Kind.ToString().ToLower()); Out(' '); OutLabel(node.Target); if (node.Value == null) { return(node); } Out('('); Visit(node.Value); Out(')'); return(node); }
protected override Expression VisitGoto(GotoExpression gotoExpression) { Check.NotNull(gotoExpression, nameof(gotoExpression)); AppendLine("return (" + gotoExpression.Target.Type.ShortDisplayName() + ")" + gotoExpression.Target + " {"); using (_stringBuilder.Indent()) { Visit(gotoExpression.Value); } _stringBuilder.Append("}"); return(gotoExpression); }
protected override Expression VisitGoto(GotoExpression node) { switch (node.Kind) { case GotoExpressionKind.Goto: if (!Options.HasFlag(ExpressionOptions.AllowGoto)) { throw new ExpressionSecurityException("Goto is not permitted."); } break; } return(base.VisitGoto(node)); }
protected virtual int GetHashCode(GotoExpression node, object state) { if (node == null) { return(0); } return(CombineHash( node.Kind.GetHashCode() , GetHashCode(node.Type, state) , GetHashCode(node.Target, state) , GetHashCode(node.Value, state) )); }
public IConvertResult <TFrom, TTo> Create <TFrom, TTo>() { var expBuilder = new ExpBuilder <TFrom, TTo>(); if (expBuilder.UnderlyingToType == typeof(T) && expBuilder.UnderlyingFromType != typeof(string) && expBuilder.UnderlyingFromType != typeof(object)) { var methodInfo = typeof(System.Convert) .GetRuntimeMethods() .Where(x => x.ReturnType == expBuilder.UnderlyingToType && x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == expBuilder.UnderlyingFromType) .FirstOrDefault(); if (methodInfo != null) { if (_invalidCastTypes.Exists(x => x == expBuilder.UnderlyingFromType)) { return(expBuilder .Add(expBuilder.LabelExpression) .ToResult(this)); } else { Expression guardedInput = expBuilder.FromType.IsNullable() ? Expression.Property(expBuilder.Input, "Value") : (Expression)expBuilder.Input; MethodCallExpression callExpression = Expression.Call( methodInfo, guardedInput); GotoExpression returnExpression = expBuilder.ToType.IsNullable() ? Expression.Return(expBuilder.LabelTarget, Expression.Convert(callExpression, expBuilder.ToType)) : Expression.Return(expBuilder.LabelTarget, callExpression); return(expBuilder .AddGuards() .Add(returnExpression) .Add(expBuilder.LabelExpression) .ToResult(this)); } } } return(null); }
private string VisitReturn(GotoExpression @goto) { var codeWriter = new XzaarCodeWriter(); codeWriter.Write("return", currentIndent); if (@goto.Value != null && [email protected]()) { codeWriter.Write(" "); insideExpressionCount++; codeWriter.Write(Visit(@goto.Value)); insideExpressionCount--; } codeWriter.NewLine(); return(codeWriter.ToString()); }
/// <inheritdoc/> protected override Expression VisitGoto(GotoExpression node) { Debug.Assert(node != null); var expected = (GotoExpression)mExpectedExpression; if (!CheckEqual(node.Kind, expected.Kind)) { return(node); } if (!CheckEqual(node.Target, expected.Target)) { return(node); } return(base.VisitGoto(node)); }
protected override Expression VisitGoto(GotoExpression node) { if (node.Target == _target) { return(Expression.Goto( _gotoInfo.VoidTarget, Expression.Block( _rewriter.MakeAssign(_gotoInfo.Variable, node.Value), Expression.Default(typeof(void)) ), node.Type )); } return(base.VisitGoto(node)); }
protected virtual bool CompareGoto(GotoExpression node1, GotoExpression node2, object state) { if (AreBothNull(node1, node2)) { return(true); } if (AreEitherNull(node1, node2)) { return(false); } return(node1.Kind == node2.Kind && CompareType(node1.Type, node2.Type, state) && CompareLabelTarget(node1.Target, node2.Target, state) && Compare(node1.Value, node2.Value, state)); }
protected override Expression VisitGoto(GotoExpression node) { BranchLabel label; LabelTarget key = node.Target; Expression expression = this.Visit(node.Value); if (!this._labelMapping.TryGetValue(key, out label)) { return(node.Update(key, expression)); } if ((label.TargetIndex >= this._loopStartInstructionIndex) && (label.TargetIndex < this._loopEndInstructionIndex)) { return(node.Update(key, expression)); } return(Expression.Return(this._returnLabel, ((expression != null) && (expression.Type != typeof(void))) ? Expression.Call(this._frameVar, InterpretedFrame.GotoMethod, Expression.Constant(label.LabelIndex), Utils.Box(expression)) : Expression.Call(this._frameVar, InterpretedFrame.VoidGotoMethod, new Expression[] { Expression.Constant(label.LabelIndex) }), node.Type)); }
protected override Expression VisitGoto(GotoExpression expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } var vistedExpression = base.VisitGoto(expression); // Testing the parent expression is only required if all children are evaluatable if (IsCurrentSubtreeEvaluatable) { IsCurrentSubtreeEvaluatable = EvaluatableExpressionFilter.IsEvaluatableGoto(expression); } return(vistedExpression); }
protected override Expression VisitGoto(GotoExpression node) { if (node.Kind == GotoExpressionKind.Break) { if (node.Value != null) { return(Expression.Goto(_break, node.Value, node.Type)); } return(Expression.Goto(_break, node.Type)); } else if (node.Kind == GotoExpressionKind.Continue) { return(Expression.Continue(_continue, _continue.Type)); } return(node); }
protected override Expression VisitGoto(GotoExpression node) { if (TryGetLabelInfo(node.Target, out LabelInfo info)) { var variable = info.Value; var newTarget = info.Target; var res = Expression.Block( Expression.Assign(variable, node.Value), Expression.Goto(newTarget, node.Type) ); return(res); } return(base.VisitGoto(node)); }
public string Visit(GotoExpression @goto) { switch (@goto.Kind) { case GotoExpressionKind.Break: return(VisitBreak(@goto)); case GotoExpressionKind.Continue: return(VisitContinue(@goto)); case GotoExpressionKind.Return: return(VisitReturn(@goto)); case GotoExpressionKind.Goto: return(VisitGoto(@goto)); } return(null); }
public Expression BuildInjectionExpression(Expression targetExpression, Expression valueExpression, Type memberType) { UnaryExpression castedValue = Expression.Convert(valueExpression, typeof(IEnumerable)); MethodCallExpression getEnumerator = Expression.Call(castedValue, InjectionReflectionInfos.GetEnumeratorMethodInfo); ParameterExpression enumerator = Expression.Variable(typeof(IEnumerator)); BinaryExpression assignEnumerator = Expression.Assign(enumerator, getEnumerator); MethodCallExpression moveNext = Expression.Call(enumerator, InjectionReflectionInfos.EnumeratorMoveNextMethodInfo); MemberExpression current = Expression.Property(enumerator, InjectionReflectionInfos.EnumeratorCurrentMethodInfo); UnaryExpression castedCurrent = Expression.Convert(current, PopulateMethodInfo.GetParameters()[0].ParameterType); MethodCallExpression populate = Expression.Call(targetExpression, PopulateMethodInfo, castedCurrent); LabelTarget loopBreakTarget = Expression.Label(); GotoExpression loopBreak = Expression.Break(loopBreakTarget); LoopExpression populateWhileMoveNext = Expression.Loop(Expression.IfThenElse(Expression.IsTrue(moveNext), populate, loopBreak), loopBreakTarget); return(Expression.Block(new [] { enumerator }, assignEnumerator, populateWhileMoveNext)); }
public static CloneDel <T> CreateShallowClone <T>() { var type = typeof(T); ParameterExpression source = Expression.Parameter(type, "source"); ParameterExpression context = Expression.Parameter(typeof(CopyContext), "context"); ConstantExpression defaultValue = Expression.Constant(default(T), type); LabelTarget returnTarget = Expression.Label(type); LabelExpression returnLabel = Expression.Label(returnTarget, defaultValue); GotoExpression returnExpression = Expression.Return( returnTarget, source, type); var block = Expression.Block( returnExpression, returnLabel); return(Expression.Lambda <CloneDel <T> >(block, source, context).Compile()); }
protected override IEnumerable <Expression> GetShortCircuitReturns(GotoExpression returnNull, IObjectMappingData mappingData) { var mapperData = mappingData.MapperData; if (SourceObjectCouldBeNull(mapperData)) { yield return(Expression.IfThen(mapperData.SourceObject.GetIsDefaultComparison(), returnNull)); } var alreadyMappedShortCircuit = GetAlreadyMappedObjectShortCircuitOrNull(mapperData); if (alreadyMappedShortCircuit != null) { yield return(alreadyMappedShortCircuit); } if (TryGetShortCircuitFactory(mapperData, out var sourceShortCircuitFactory)) { yield return(sourceShortCircuitFactory.GetShortCircuit(mappingData)); } }
protected override Expression VisitGoto(GotoExpression node) { if (node.Kind == GotoExpressionKind.Return) { if (node.Value == null) { this.WriteLine("return"); } else { this.Write("return "); this.Visit(node.Value); } } else if (node.Kind == GotoExpressionKind.Continue) { this.Write("continue"); } return(node); }
public PropertySagaInstanceFactory() { var constructorInfo = typeof(TSaga).GetConstructor(Type.EmptyTypes); ReadWriteProperty <TSaga> property; if (constructorInfo == null) { throw new ArgumentException($"The saga {TypeMetadataCache<TSaga>.ShortName} does not have a default public constructor"); } if (!TypeMetadataCache <TSaga> .ReadWritePropertyCache.TryGetValue("CorrelationId", out property)) { throw new ArgumentException($"The saga {TypeMetadataCache<TSaga>.ShortName} does not have a writeable CorrelationId property"); } ParameterExpression correlationId = Expression.Parameter(typeof(Guid), "correlationId"); NewExpression newSaga = Expression.New(constructorInfo); var saga = Expression.Variable(typeof(TSaga), "saga"); var assign = Expression.Assign(saga, newSaga); MethodCallExpression call = Expression.Call(saga, property.Property.SetMethod, correlationId); LabelTarget returnTarget = Expression.Label(typeof(TSaga)); GotoExpression returnExpression = Expression.Return(returnTarget, saga, typeof(TSaga)); LabelExpression returnLabel = Expression.Label(returnTarget, Expression.Default(typeof(TSaga))); var block = Expression.Block(new[] { saga }, assign, call, returnExpression, returnLabel); FactoryMethod = Expression.Lambda <SagaInstanceFactoryMethod <TSaga> >(block, correlationId).Compile(); }
public object Visit(GotoExpression @goto) { switch (@goto.Kind) { case GotoExpressionKind.Return: { var returnValue = Visit(@goto.Value); var il = ctx.GetILGenerator(); TryLoadReference(returnValue, il); if (ctx.CurrentMethod.MethodInfo.ReturnType == typeof(object)) { } il.Return(); return(null); } case GotoExpressionKind.Goto: throw new System.NotImplementedException(); case GotoExpressionKind.Break: { var il = ctx.GetILGenerator(); il.BranchToShortForm(ctx.CurrentScope.CurrentEndLabel); return(null); } case GotoExpressionKind.Continue: { var il = ctx.GetILGenerator(); il.BranchToShortForm(ctx.CurrentScope.CurrentStartLabel); return(null); } } throw new System.NotImplementedException(); }
public T?Simplify <T>(T?expression) where T : Expression { if (expression is null) { return(null); } Expression expr = expression.Reduce() switch { UnaryExpression unaryExpr => unaryExpr.Update(Simplify(unaryExpr.Operand)), BinaryExpression binaryExpr => binaryExpr.Update(Simplify(binaryExpr.Left), binaryExpr.Conversion, Simplify(binaryExpr.Right)), LambdaExpression lambdaExpr => Expression.Lambda(Simplify(lambdaExpr.Body), lambdaExpr.Name, lambdaExpr.TailCall, Simplify(lambdaExpr.Parameters)), TryExpression tryExpr => tryExpr.Update(Simplify(tryExpr.Body), Simplify(tryExpr.Handlers), Simplify(tryExpr.Finally), Simplify(tryExpr.Fault)), NewExpression newExpr => newExpr.Update(Simplify(newExpr.Arguments)), GotoExpression gotoExpr => gotoExpr.Update(gotoExpr.Target, Simplify(gotoExpr.Value)), LoopExpression loopExpr => loopExpr.Update(loopExpr.BreakLabel, loopExpr.ContinueLabel, Simplify(loopExpr.Body)), BlockExpression blockExpr => blockExpr.Update(Simplify(blockExpr.Variables), Simplify(blockExpr.Expressions)), IndexExpression indexExpr => indexExpr.Update(Simplify(indexExpr.Object) !, Simplify(indexExpr.Arguments)), LabelExpression labelExpr => labelExpr.Update(labelExpr.Target, Simplify(labelExpr.DefaultValue)), MemberExpression memberExpr => memberExpr.Update(Simplify(memberExpr.Expression)), SwitchExpression switchExpr => switchExpr.Update(Simplify(switchExpr.SwitchValue), Simplify(switchExpr.Cases), Simplify(switchExpr.DefaultBody)), DynamicExpression dynamicExpr => dynamicExpr.Update(Simplify(dynamicExpr.Arguments)), ListInitExpression listInitExpr => listInitExpr.Update(Simplify(listInitExpr.NewExpression), Simplify(listInitExpr.Initializers)), NewArrayExpression newArrayExpr => newArrayExpr.Update(Simplify(newArrayExpr.Expressions)), InvocationExpression invokeExpr => invokeExpr.Update(Simplify(invokeExpr.Expression), Simplify(invokeExpr.Arguments)), MemberInitExpression memberInitExpr => memberInitExpr.Update(Simplify(memberInitExpr.NewExpression), memberInitExpr.Bindings), MethodCallExpression methodCallExpr => methodCallExpr.Update(Simplify(methodCallExpr.Object), Simplify(methodCallExpr.Arguments)), TypeBinaryExpression typeBinaryExpr => typeBinaryExpr.Update(Simplify(typeBinaryExpr.Expression)), ConditionalExpression condExpr => condExpr.Update(Simplify(condExpr.Test), Simplify(condExpr.IfTrue), Simplify(condExpr.IfFalse)), RuntimeVariablesExpression runtimeVarExpr => runtimeVarExpr.Update(Simplify(runtimeVarExpr.Variables)), _ => expression }; foreach (var transform in transformers) { expr = transform.Transform(expr, this); } return((T)expr); }
public GotoExpressionProxy(GotoExpression node) { _node = node; }