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


C# AstNode.GetSelfAndChildrenRecursive方法代码示例

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


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

示例1: Convert

        public static void Convert(AstNode ast, MethodSource currentMethod, AssemblyCompiler compiler)
        {
            foreach (var block in ast.GetSelfAndChildrenRecursive<AstBlock>())
            {
                for (int i = 0; i < block.Body.Count; ++i)
                {
                    var expr = block.Body[i] as AstExpression;
                    if(expr == null) 
                        continue;


                    var interlockedPairs = expr.GetExpressionPairs(p =>
                                            p.Code == AstCode.Call
                                            && ((XMethodReference) p.Operand).DeclaringType.FullName == "System.Threading.Interlocked")
                                            .ToList();
                                                    
                    if(interlockedPairs.Count == 0) 
                        continue;
                    if (interlockedPairs.Count > 1)
                        throw new CompilerException("The Interlocked converter can not handle more than one interlocked call per statement. Try splittig the statement up.");

                    var interlockedCall = interlockedPairs.First().Expression;

                    // first parameter should be a reference to a field,
                    // (but be lenient if we don't find what we expect)
                    
                    var targetExpr = interlockedCall.Arguments[0];

                    XFieldReference field = null;

                    if (targetExpr.InferredType.IsByReference)
                    {
                        field = targetExpr.Operand as XFieldReference;

                        if (field != null)
                        {
                            // check if we have an atomic updater 
                            var updater = field.DeclaringType.Resolve().Fields
                                                             .FirstOrDefault(f => f.Name == field.Name + NameConstants.Atomic.FieldUpdaterPostfix);
                            if (updater != null)
                            {
                                var method = (XMethodReference) interlockedCall.Operand;
                                var methodName = method.Name.Split('$')[0]; // retrieve original name.

                                if (InterlockedUsingUpdater(interlockedCall, methodName, field, updater, targetExpr, interlockedPairs.First().Parent, compiler))
                                    continue;
                            }
                        }
                    }
                    

                    FailsafeInterlockedUsingLocking(field, expr, targetExpr, block, i, compiler, currentMethod);
                }

            }
        }
开发者ID:Xtremrules,项目名称:dot42,代码行数:56,代码来源:InterlockedConverter.cs

示例2: Convert

        public static void Convert(AstNode ast, MethodSource currentMethod, AssemblyCompiler compiler)
        {
            // only work on MoveNext of IAsyncStateMachine implementations.
            if (currentMethod.Name != "MoveNext" && currentMethod.Name != "IAsyncStateMachine_MoveNext")
                return;
            var declaringType = currentMethod.Method.DeclaringType.Resolve();
            if (declaringType.Interfaces.All(i => i.FullName != "System.Runtime.CompilerServices.IAsyncStateMachine"))
                return;

            foreach(var block in ast.GetSelfAndChildrenRecursive<AstBlock>())
                FixBlock(block);
        }
开发者ID:Xtremrules,项目名称:dot42,代码行数:12,代码来源:FixAsyncStateMachine.cs

示例3: Convert

        /// <summary>
        /// Optimize expressions
        /// </summary>
        public static void Convert(AstNode ast, MethodSource currentMethod, AssemblyCompiler compiler)
        {
            var typeSystem = compiler.Module.TypeSystem;

            // Expand instanceof
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.InstanceOf))
            {
                ConvertInstanceOf(compiler, node, typeSystem);
            }

            // Expand isinst
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.Isinst))
            {
                ConvertIsInst(compiler, node, typeSystem);
            }

            // Expand Castclass
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.Castclass))
            {
                ConvertCastclass(compiler, node, typeSystem);
            }
        }
开发者ID:rfcclub,项目名称:dot42,代码行数:25,代码来源:CastConverter.cs

示例4: Convert

        /// <summary>
        /// Optimize expressions
        /// </summary>
        public static void Convert(AstNode ast, MethodSource currentMethod, AssemblyCompiler compiler)
        {
            var typeSystem = compiler.Module.TypeSystem;

            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>())
            {
                switch (node.Code)
                {
                    case AstCode.InstanceOf:
                        ConvertInstanceOf(compiler, node, typeSystem);
                        break;
                    case AstCode.Isinst:
                        ConvertIsInst(compiler, node, typeSystem);
                        break;
                    case AstCode.Castclass:
                        ConvertCastclass(compiler, node, typeSystem);
                        break;
                    case AstCode.Call:
                        ConvertAsNativeIFormattable(node, typeSystem);
                        break;
                    case AstCode.Callvirt:
                        ConvertCallvirtIEnumerable(compiler, node, typeSystem);
                        break;
                    // TODO: this might better be handled in RLBuilder.ConvertTypeBeforeStore()
                    case AstCode.Ret:
                        ConvertRetOrStfldOrStsfld(compiler, currentMethod.Method.ReturnType, node, typeSystem);
                        break;
                    // TODO: this appears to be handled in RLBuilder.ConvertTypeBeforeStore(),
                    //       but is not. why?
                    case AstCode.Stfld:
                    case AstCode.Stsfld:
                        ConvertRetOrStfldOrStsfld(compiler, ((XFieldReference)node.Operand).FieldType, node, typeSystem);
                        break;
                }
            }
        }
开发者ID:Xtremrules,项目名称:dot42,代码行数:39,代码来源:CastConverter.cs

示例5: Convert


//.........这里部分代码省略.........
                        var numericValue = new AstExpression(node.SourceLocation,
                                                             isWide ? AstCode.Enum_to_long : AstCode.Enum_to_int, null);
                        numericValue.InferredType = isWide ? typeSystem.Long : typeSystem.Int;
                        numericValue.Arguments.Add(enumValue);
                        node.Arguments.Clear();
                        node.Arguments.Add(numericValue);
                    }
                }

                // Note: no else here
                {
                    // Need to value type?
                    var inferredType = node.InferredType;
                    var expectedType = node.ExpectedType;

                    if ((inferredType != null) && (expectedType != null) && !inferredType.IsSame(expectedType))
                    {
                        // don't convert if either one is the abstract base class.
                        if (inferredType.IsInternalEnum() || expectedType.IsInternalEnum())
                            continue;

                        // don't convert if either one is a reference type
                        if (inferredType.IsByReference || expectedType.IsByReference)
                            continue;

                        XTypeDefinition expectedTypeDef;
                        if (expectedType.TryResolve(out expectedTypeDef) && expectedTypeDef.IsEnum)
                        {
                            // Enum expected, non-enum or different enum found
                            var underlyingType = expectedTypeDef.GetEnumUnderlyingType();
                            var isWide = underlyingType.IsWide();

                            // If inferred type is another enum, convert to primitive first
                            if (inferredType.IsEnum())
                            {
                                var toPrimitive = new AstExpression(node) { ExpectedType = null };
                                node.Code = isWide ? AstCode.Enum_to_long : AstCode.Enum_to_int;
                                node.SetArguments(toPrimitive);
                                node.Operand = null;
                                node.SetType(isWide ? typeSystem.Long : typeSystem.Int);
                            }
                            else if (inferredType.IsPrimitive)
                            {
                                // Convert float/double to int/long before converting to enum
                                if (inferredType.IsDouble() || inferredType.IsFloat())
                                {
                                    var code = isWide ? AstCode.Conv_I8 : AstCode.Conv_I4;
                                    var clone = new AstExpression(node) { ExpectedType = null };
                                    node.Code = code;
                                    node.SetArguments(clone);
                                    node.Operand = null;
                                    node.SetType(underlyingType);                                    
                                }
                            }

                            var actualValue = new AstExpression(node) {ExpectedType = null};
                            node.Code = isWide ? AstCode.Long_to_enum : AstCode.Int_to_enum;
                            node.Operand = null;
                            node.SetArguments(actualValue);
                            node.SetType(expectedTypeDef);
                        }
                        else
                        {
                            XTypeDefinition inferredTypeDef;
                            if (inferredType.TryResolve(out inferredTypeDef) && inferredTypeDef.IsEnum)
                            {
                                if (!expectedType.IsSystemObject())
                                {
                                    // Enum found, non-enum expected
                                    var underlyingType = inferredTypeDef.GetEnumUnderlyingType();
                                    var isWide = underlyingType.IsWide();

                                    var actualValue = new AstExpression(node) {ExpectedType = null};
                                    node.Code = isWide ? AstCode.Enum_to_long : AstCode.Enum_to_int;
                                    node.Operand = null;
                                    node.SetArguments(actualValue);
                                    node.SetType(isWide ? typeSystem.Long : typeSystem.Int);
                                    ConvertIfNeeded(node, underlyingType);
                                }
                            }
                        }
                    }
                }
            }

            // Convert value of switch node
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstSwitch>())
            {
                var typeRef = node.Condition.InferredType;
                XTypeDefinition typeDef;
                if ((typeRef != null) && typeRef.TryResolve(out typeDef) && typeDef.IsEnum)
                {
                    var isWide = typeDef.GetEnumUnderlyingType().IsWide();
                    var toValue = new AstExpression(node.Condition.SourceLocation, isWide ? AstCode.Enum_to_long : AstCode.Enum_to_int, null);
                    toValue.InferredType = isWide ? typeSystem.Long : typeSystem.Int;
                    toValue.Arguments.Add(node.Condition);
                    node.Condition = toValue;
                }                
            }
        }
开发者ID:Xtremrules,项目名称:dot42,代码行数:101,代码来源:EnumConverter.cs

示例6: Convert

        /// <summary>
        /// Optimize expressions
        /// </summary>
        public static void Convert(AstNode ast, MethodSource currentMethod, AssemblyCompiler compiler)
        {
            var typeSystem = compiler.Module.TypeSystem;

            // Expand typeof
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.TypeOf))
            {
                var type = (XTypeReference) node.Operand;
                var typeHelperType = compiler.GetDot42InternalType(InternalConstants.TypeHelperName).Resolve();
                var loadExpr = LoadTypeForGenericInstance(node.SourceLocation, currentMethod, type, compiler, typeHelperType, typeSystem, TypeConversion.EnsureTrueOrMarkerType);
                node.CopyFrom(loadExpr);
            }

            // Expand instanceOf
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.SimpleInstanceOf))
            {
                var type = (XTypeReference)node.Operand;
                var gp = type as XGenericParameter;
                if (gp == null) continue;

                var typeHelperType = compiler.GetDot42InternalType(InternalConstants.TypeHelperName).Resolve();
                var loadExpr = LoadTypeForGenericInstance(node.SourceLocation, currentMethod, type, compiler, typeHelperType, typeSystem, TypeConversion.EnsureRuntimeType);
                //// both types are boxed, no need for conversion.
                var typeType = compiler.GetDot42InternalType("System", "Type").Resolve();
                var isInstanceOfType = typeType.Methods.Single(n => n.Name == "JavaIsInstance" && n.Parameters.Count == 1);
                var call = new AstExpression(node.SourceLocation, AstCode.Call, isInstanceOfType, loadExpr, node.Arguments[0]);
                node.CopyFrom(call);
            }

            // Expand newarr
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.Newarr))
            {
                var type = (XTypeReference)node.Operand;
                if (!type.IsDefinitionOrReferenceOrPrimitive())
                {
                    // Resolve type to a Class<?>
                    var typeHelperType = compiler.GetDot42InternalType(InternalConstants.TypeHelperName).Resolve();
                    // while having primitive arrays for primitive types would be nice, a lot of boxing and unboxing
                    // would be needed. only for-primitive-specialized generic classes we could optimize this.
                    var ldType = LoadTypeForGenericInstance(node.SourceLocation, currentMethod, type, compiler, typeHelperType, typeSystem, TypeConversion.EnsureRuntimeType);
                    var newInstanceExpr = new AstExpression(node.SourceLocation, AstCode.ArrayNewInstance, null, ldType, node.Arguments[0]) { ExpectedType = typeSystem.Object };
                    var arrayType = new XArrayType(type);
                    var cast = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, arrayType, newInstanceExpr) { ExpectedType = arrayType };
                    node.CopyFrom(cast);
                }
            }

            // Add generic instance call arguments
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code.IsCall()))
            {
                var method = (XMethodReference)node.Operand;
                if (method.DeclaringType.IsArray)
                    continue;
                XMethodDefinition methodDef;
                if (!method.TryResolve(out methodDef)) 
                    continue;
                if (methodDef.HasDexNativeAttribute())
                    continue;

                if (methodDef.NeedsGenericInstanceTypeParameter)
                {
                    // Add generic instance type parameter value
                    var arg = CreateGenericInstanceCallArguments(node.SourceLocation, method.DeclaringType, currentMethod, compiler);
                    node.Arguments.AddRange(arg);
                    node.GenericInstanceArgCount += arg.Count;
                }

                if (methodDef.NeedsGenericInstanceMethodParameter)
                {
                    // Add generic instance method parameter
                    var arg = CreateGenericInstanceCallArguments(node.SourceLocation, method, currentMethod, compiler);
                    node.Arguments.AddRange(arg);
                    node.GenericInstanceArgCount += arg.Count;
                }
            }

            // Add generic instance Delegate arguments for static methods.
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.Delegate))
            {
                var delegateInfo = (Tuple<XTypeDefinition, XMethodReference>)node.Operand;

                var genMethodDef = delegateInfo.Item2 as IXGenericInstance;
                var genTypeDef = delegateInfo.Item2.DeclaringType as IXGenericInstance;

                // Add generic instance type parameter value, if method is static
                if (genTypeDef != null && delegateInfo.Item2.Resolve().IsStatic)
                {
                    var arg = CreateGenericInstanceCallArguments(node.SourceLocation, delegateInfo.Item2.DeclaringType, currentMethod, compiler);
                    node.Arguments.AddRange(arg);
                    node.GenericInstanceArgCount += arg.Count;
                }

                // add generic method type parameter value.
                if (genMethodDef != null)
                {
                    var arg = CreateGenericInstanceCallArguments(node.SourceLocation, delegateInfo.Item2, currentMethod, compiler);
                    node.Arguments.AddRange(arg);
//.........这里部分代码省略.........
开发者ID:Xtremrules,项目名称:dot42,代码行数:101,代码来源:GenericInstanceConverter.cs

示例7: Convert

        /// <summary>
        /// Optimize expressions
        /// </summary>
        public static void Convert(AstNode ast, MethodSource currentMethod, AssemblyCompiler compiler)
        {
            var typeSystem = compiler.Module.TypeSystem;

            // Expand typeof
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.TypeOf))
            {
                var type = (XTypeReference) node.Operand;
                var typeHelperType = compiler.GetDot42InternalType(InternalConstants.TypeHelperName).Resolve();
                var loadExpr = LoadTypeForGenericInstance(node.SourceLocation, currentMethod, type, typeHelperType, typeSystem, false);
                node.CopyFrom(loadExpr);
            }

            // Expand newarr
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.Newarr))
            {
                var type = (XTypeReference)node.Operand;
                if (!type.IsDefinitionOrReferenceOrPrimitive())
                {
                    // Resolve type to a Class<?>
                    var typeHelperType = compiler.GetDot42InternalType(InternalConstants.TypeHelperName).Resolve();
                    var ldType = LoadTypeForGenericInstance(node.SourceLocation, currentMethod, type, typeHelperType, typeSystem, false);
                    var newInstanceExpr = new AstExpression(node.SourceLocation, AstCode.ArrayNewInstance, null, ldType, node.Arguments[0]) { ExpectedType = typeSystem.Object };
                    var arrayType = new XArrayType(type);
                    var cast = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, arrayType, newInstanceExpr) { ExpectedType = arrayType };
                    node.CopyFrom(cast);
                }
            }

            // Add generic instance call arguments
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code.IsCall()))
            {
                var method = (XMethodReference)node.Operand;
                if (method.DeclaringType.IsArray)
                    continue;
                XMethodDefinition methodDef;
                if (!method.TryResolve(out methodDef)) 
                    continue;
                if (methodDef.HasDexNativeAttribute())
                    continue;

                if (methodDef.NeedsGenericInstanceTypeParameter)
                {
                    // Add generic instance type parameter value
                    var arg = CreateGenericInstance(node.SourceLocation, method.DeclaringType, currentMethod, compiler);
                    node.Arguments.Add(arg);
                    node.GenericInstanceArgCount++;
                }

                if (methodDef.NeedsGenericInstanceMethodParameter)
                {
                    // Add generic instance method parameter
                    var arg = CreateGenericInstance(node.SourceLocation, method, currentMethod, compiler);
                    node.Arguments.Add(arg);
                    node.GenericInstanceArgCount++;
                }
            }

            // Convert NewObj when needed
            foreach (var node in ast.GetSelfAndChildrenRecursive<AstExpression>(x => x.Code == AstCode.Newobj))
            {
                var ctorRef = (XMethodReference)node.Operand;
                var declaringType = ctorRef.DeclaringType;
                if (declaringType.IsArray)
                {
                    // New multi dimensional array
                    // Get element type
                    var elemType = ((XArrayType) declaringType).ElementType;
                    var typeExpr = new AstExpression(node.SourceLocation, AstCode.TypeOf, elemType);

                    // Create dimensions array
                    var intArrayType = new XArrayType(typeSystem.Int);
                    var dimArrayExpr = new AstExpression(node.SourceLocation, AstCode.InitArrayFromArguments, intArrayType, node.Arguments).SetType(intArrayType);

                    // Call java.lang.reflect.Array.newInstance(type, int[])
                    var newInstanceExpr = new AstExpression(node.SourceLocation, AstCode.ArrayNewInstance2, null, typeExpr, dimArrayExpr).SetType(typeSystem.Object);

                    // Cast to correct type
                    var cast = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, declaringType, newInstanceExpr).SetType(declaringType);

                    // Replace node
                    node.CopyFrom(cast);
                }
                else
                {
                    // Normal "new object"
                    XMethodDefinition ctorDef;
                    if (ctorRef.TryResolve(out ctorDef) && ctorDef.NeedsGenericInstanceTypeParameter)
                    {
                        // Add generic instance type parameter value
                        var arg = CreateGenericInstance(node.SourceLocation, ctorRef.DeclaringType, currentMethod, compiler);
                        node.Arguments.Add(arg);
                        node.GenericInstanceArgCount++;
                    }
                }
            }
        }
开发者ID:rfcclub,项目名称:dot42,代码行数:100,代码来源:GenericInstanceConverter.cs


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