当前位置: 首页>>代码示例>>C#>>正文


C# AstExpression.Match方法代码示例

本文整理汇总了C#中AstExpression.Match方法的典型用法代码示例。如果您正苦于以下问题:C# AstExpression.Match方法的具体用法?C# AstExpression.Match怎么用?C# AstExpression.Match使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AstExpression的用法示例。


在下文中一共展示了AstExpression.Match方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TransformDecimalCtorToConstant

        static bool TransformDecimalCtorToConstant(List<AstNode> body, AstExpression expr, int pos)
        {
            XMethodReference r;
            List<AstExpression> args;
            if (expr.Match(AstCode.Newobj, out r, out args) && r.DeclaringType.IsSystemDecimal())
            {
                //if (args.Count == 1)
                //{
                //    int val;
                //    if (args[0].Match(AstCode.Ldc_I4, out val))
                //    {
                //        expr.Code = AstCode.Ldc_Decimal;
                //        expr.Operand = new decimal(val);
                //        expr.InferredType = r.DeclaringType;
                //        expr.Arguments.Clear();
                //        return true;
                //    }
                //}
                //else 
                if (args.Count == 5)
                {
                    int lo, mid, hi, isNegative, scale;
                    if (expr.Arguments[0].Match(AstCode.Ldc_I4, out lo) &&
                        expr.Arguments[1].Match(AstCode.Ldc_I4, out mid) &&
                        expr.Arguments[2].Match(AstCode.Ldc_I4, out hi) &&
                        expr.Arguments[3].Match(AstCode.Ldc_I4, out isNegative) &&
                        expr.Arguments[4].Match(AstCode.Ldc_I4, out scale))
                    {
                        // convert to a static string conversion method.
                        var dec = new decimal(lo, mid, hi, isNegative != 0, (byte)scale);
                        var str = dec.ToString(CultureInfo.InvariantCulture);

                        expr.Code = AstCode.Call;
                        expr.Operand = r.DeclaringType.Resolve().Methods.First(p => p.Name == "FromInvariantString" && p.IsStatic && p.Parameters.Count == 1);
                        expr.Arguments.Clear();
                        expr.Arguments.Add(new AstExpression(expr.SourceLocation, AstCode.Ldstr, str));
                        expr.InferredType = r.DeclaringType;
                        return true;
                    }
                }
            }
            bool modified = false;
            foreach (AstExpression arg in expr.Arguments)
            {
                modified |= TransformDecimalCtorToConstant(null, arg, -1);
            }
            return modified;
        }
开发者ID:Xtremrules,项目名称:dot42,代码行数:48,代码来源:PeepholeTransform.cs

示例2: SimplifyLdObjAndStObj

 static AstExpression SimplifyLdObjAndStObj(AstExpression expr, ref bool modified)
 {
     if (expr.Code == AstCode.Initobj)
     {
         expr.Code = AstCode.Stobj;
         expr.Arguments.Add(new AstExpression(expr.SourceLocation, AstCode.DefaultValue, expr.Operand));
         modified = true;
     }
     else if (expr.Code == AstCode.Cpobj)
     {
         expr.Code = AstCode.Stobj;
         expr.Arguments[1] = new AstExpression(expr.SourceLocation, AstCode.Ldobj, expr.Operand, expr.Arguments[1]);
         modified = true;
     }
     AstExpression arg, arg2;
     XTypeReference type;
     AstCode? newCode = null;
     if (expr.Match(AstCode.Stobj, out type, out arg, out arg2))
     {
         switch (arg.Code)
         {
             case AstCode.Ldelema: newCode = AstCode.Stelem_Any; break;
             case AstCode.Ldloca: newCode = AstCode.Stloc; break;
             case AstCode.Ldflda: newCode = AstCode.Stfld; break;
             case AstCode.Ldsflda: newCode = AstCode.Stsfld; break;
         }
     }
     else if (expr.Match(AstCode.Ldobj, out type, out arg))
     {
         switch (arg.Code)
         {
             case AstCode.Ldelema: newCode = AstCode.Ldelem_Any; break;
             case AstCode.Ldloca: newCode = AstCode.Ldloc; break;
             case AstCode.Ldflda: newCode = AstCode.Ldfld; break;
             case AstCode.Ldsflda: newCode = AstCode.Ldsfld; break;
         }
     }
     if (newCode != null)
     {
         arg.Code = newCode.Value;
         if (expr.Code == AstCode.Stobj)
         {
             arg.InferredType = expr.InferredType;
             arg.ExpectedType = expr.ExpectedType;
             arg.Arguments.Add(arg2);
         }
         arg.ILRanges.AddRange(expr.ILRanges);
         modified = true;
         return arg;
     }
     else
     {
         return expr;
     }
 }
开发者ID:Xtremrules,项目名称:dot42,代码行数:55,代码来源:PeepholeTransform.cs

示例3: MatchParameterVariableAssignment

 bool MatchParameterVariableAssignment(AstExpression expr)
 {
     // stloc(v, call(Expression::Parameter, call(Type::GetTypeFromHandle, ldtoken(...)), ldstr(...)))
     AstVariable v;
     AstExpression init;
     if (!expr.Match(AstCode.Stloc, out v, out init))
         return false;
     if (v.IsGenerated || v.IsParameter || v.IsPinned)
         return false;
     if (v.Type == null || v.Type.FullName != "System.Linq.Expressions.ParameterExpression")
         return false;
     XMethodReference parameterMethod;
     AstExpression typeArg, nameArg;
     if (!init.Match(AstCode.Call, out parameterMethod, out typeArg, out nameArg))
         return false;
     if (!(parameterMethod.Name == "Parameter" && parameterMethod.DeclaringType.FullName == "System.Linq.Expressions.Expression"))
         return false;
     XMethodReference getTypeFromHandle;
     AstExpression typeToken;
     if (!typeArg.Match(AstCode.Call, out getTypeFromHandle, out typeToken))
         return false;
     if (!(getTypeFromHandle.Name == "GetTypeFromHandle" && getTypeFromHandle.DeclaringType.FullName == "System.Type"))
         return false;
     return typeToken.Code == AstCode.Ldtoken && nameArg.Code == AstCode.Ldstr;
 }
开发者ID:Xtremrules,项目名称:dot42,代码行数:25,代码来源:PeepholeTransform.cs

示例4: InlineExpressionTreeParameterDeclarations

        bool InlineExpressionTreeParameterDeclarations(List<AstNode> body, AstExpression expr, int pos)
        {
            // When there is a Expression.Lambda() call, and the parameters are declared in the
            // IL statement immediately prior to the one containing the Lambda() call,
            // using this code for the3 declaration:
            //   stloc(v, call(Expression::Parameter, call(Type::GetTypeFromHandle, ldtoken(...)), ldstr(...)))
            // and the variables v are assigned only once (in that statements), and read only in a Expression::Lambda
            // call that immediately follows the assignment statements, then we will inline those assignments
            // into the Lambda call using AstCode.ExpressionTreeParameterDeclarations.

            // This is sufficient to allow inlining over the expression tree construction. The remaining translation
            // of expression trees into C# will be performed by a C# AST transformer.

            for (int i = expr.Arguments.Count - 1; i >= 0; i--)
            {
                if (InlineExpressionTreeParameterDeclarations(body, expr.Arguments[i], pos))
                    return true;
            }

            XMethodReference mr;
            AstExpression lambdaBodyExpr, parameterArray;
            if (!(expr.Match(AstCode.Call, out mr, out lambdaBodyExpr, out parameterArray) && mr.Name == "Lambda"))
                return false;
            if (!(parameterArray.Code == AstCode.InitArray && mr.DeclaringType.FullName == "System.Linq.Expressions.Expression"))
                return false;
            int firstParameterPos = pos - parameterArray.Arguments.Count;
            if (firstParameterPos < 0)
                return false;

            AstExpression[] parameterInitExpressions = new AstExpression[parameterArray.Arguments.Count + 1];
            for (int i = 0; i < parameterArray.Arguments.Count; i++)
            {
                parameterInitExpressions[i] = body[firstParameterPos + i] as AstExpression;
                if (!MatchParameterVariableAssignment(parameterInitExpressions[i]))
                    return false;
                AstVariable v = (AstVariable)parameterInitExpressions[i].Operand;
                if (!parameterArray.Arguments[i].MatchLdloc(v))
                    return false;
                // TODO: validate that the variable is only used here and within 'body'
            }

            parameterInitExpressions[parameterInitExpressions.Length - 1] = lambdaBodyExpr;
            Debug.Assert(expr.Arguments[0] == lambdaBodyExpr);
            expr.Arguments[0] = new AstExpression(expr.SourceLocation, AstCode.ExpressionTreeParameterDeclarations, null, parameterInitExpressions);

            body.RemoveRange(firstParameterPos, parameterArray.Arguments.Count);

            return true;
        }
开发者ID:Xtremrules,项目名称:dot42,代码行数:49,代码来源:PeepholeTransform.cs

示例5: IntroducePostIncrementForVariables

		bool IntroducePostIncrementForVariables(List<AstNode> body, AstExpression expr, int pos)
		{
			// Works for variables and static fields/properties
			
			// expr = ldloc(i)
			// stloc(i, add(expr, ldc.i4(1)))
			// ->
			// expr = postincrement(1, ldloca(i))
			AstVariable exprVar;
			AstExpression exprInit;
			if (!(expr.Match(AstCode.Stloc, out exprVar, out exprInit) && exprVar.IsGenerated))
				return false;
			
			//The next expression
			AstExpression nextExpr = body.ElementAtOrDefault(pos + 1) as AstExpression;
			if (nextExpr == null)
				return false;
			
			AstCode loadInstruction = exprInit.Code;
			AstCode storeInstruction = nextExpr.Code;
			bool recombineVariable = false;
			
			// We only recognise local variables, static fields, and static getters with no arguments
			switch (loadInstruction) {
				case AstCode.Ldloc:
					//Must be a matching store type
					if (storeInstruction != AstCode.Stloc)
						return false;
					AstVariable loadVar = (AstVariable)exprInit.Operand;
					AstVariable storeVar = (AstVariable)nextExpr.Operand;
					if (loadVar != storeVar) {
						if (loadVar.OriginalVariable != null && loadVar.OriginalVariable == storeVar.OriginalVariable)
							recombineVariable = true;
						else
							return false;
					}
					break;
				case AstCode.Ldsfld:
					if (storeInstruction != AstCode.Stsfld)
						return false;
					if (exprInit.Operand != nextExpr.Operand)
						return false;
					break;
				default:
					return false;
			}
			
			AstExpression addExpr = nextExpr.Arguments[0];
			
			int incrementAmount;
			AstCode incrementCode = GetIncrementCode(addExpr, out incrementAmount);
			if (!(incrementAmount != 0 && addExpr.Arguments[0].MatchLdloc(exprVar)))
				return false;
			
			if (recombineVariable) {
				// Split local variable, unsplit these two instances
				// replace nextExpr.Operand with exprInit.Operand
				ReplaceVariables(method, oldVar => oldVar == nextExpr.Operand ? (AstVariable)exprInit.Operand : oldVar);
			}
			
			switch (loadInstruction) {
				case AstCode.Ldloc:
					exprInit.Code = AstCode.Ldloca;
					break;
				case AstCode.Ldsfld:
					exprInit.Code = AstCode.Ldsflda;
					break;
			}
			expr.Arguments[0] = new AstExpression(incrementCode, incrementAmount, exprInit);
			body.RemoveAt(pos + 1); // TODO ILRanges
			return true;
		}
开发者ID:Xtremrules,项目名称:dot42,代码行数:72,代码来源:PeepholeTransform.cs

示例6: MakeAssignmentExpression

        bool MakeAssignmentExpression(List<AstNode> body, AstExpression expr, int pos)
        {
            // exprVar = ...
            // stloc(v, exprVar)
            // ->
            // exprVar = stloc(v, ...))
            AstVariable exprVar;
            AstExpression initializer;
            if (!(expr.Match(AstCode.Stloc, out exprVar, out initializer) && exprVar.IsGenerated))
                return false;
            var nextExpr = body.ElementAtOrDefault(pos + 1) as AstExpression;
            AstVariable v;
            AstExpression stLocArg;
            if (nextExpr.Match(AstCode.Stloc, out v, out stLocArg) && stLocArg.MatchLdloc(exprVar))
            {
                var store2 = body.ElementAtOrDefault(pos + 2) as AstExpression;
                if (StoreCanBeConvertedToAssignment(store2, exprVar))
                {
                    // expr_44 = ...
                    // stloc(v1, expr_44)
                    // anystore(v2, expr_44)
                    // ->
                    // stloc(v1, anystore(v2, ...))
                    var inlining = new AstInlining(method);
                    if (inlining.numLdloc.GetOrDefault(exprVar) == 2 && inlining.numStloc.GetOrDefault(exprVar) == 1)
                    {
                        body.RemoveAt(pos + 2); // remove store2
                        body.RemoveAt(pos); // remove expr = ...
                        nextExpr.Arguments[0] = store2;
                        store2.Arguments[store2.Arguments.Count - 1] = initializer;

                        inlining.InlineIfPossible(body, ref pos);

                        return true;
                    }
                }

                body.RemoveAt(pos + 1); // remove stloc
                nextExpr.Arguments[0] = initializer;
                ((AstExpression)body[pos]).Arguments[0] = nextExpr;
                return true;
            }
            
            if ((nextExpr != null) && (nextExpr.Code == AstCode.Stsfld) && nextExpr.Arguments.Count == 1)
            {
                // exprVar = ...
                // stsfld(fld, exprVar)
                // ->
                // exprVar = stsfld(fld, ...))
                if (nextExpr.Arguments[0].MatchLdloc(exprVar))
                {
                    body.RemoveAt(pos + 1); // remove stsfld
                    nextExpr.Arguments[0] = initializer;
                    ((AstExpression)body[pos]).Arguments[0] = nextExpr;
                    return true;
                }
            }
            return false;
        }
开发者ID:Xtremrules,项目名称:dot42,代码行数:59,代码来源:PeepholeTransform.cs

示例7: SimplifyLdcI4ConvI8

 static bool SimplifyLdcI4ConvI8(List<AstNode> body, AstExpression expr, int pos)
 {
     AstExpression ldc;
     int val;
     if (expr.Match(AstCode.Conv_I8, out ldc) && ldc.Match(AstCode.Ldc_I4, out val))
     {
         expr.Code = AstCode.Ldc_I8;
         expr.Operand = (long)val;
         expr.Arguments.Clear();
         return true;
     }
     bool modified = false;
     foreach (AstExpression arg in expr.Arguments)
     {
         modified |= SimplifyLdcI4ConvI8(null, arg, -1);
     }
     return modified;
 }
开发者ID:Xtremrules,项目名称:dot42,代码行数:18,代码来源:PeepholeTransform.cs


注:本文中的AstExpression.Match方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。