示例#1
0
        public static IDecompilerTypeSystem GetSpecializingTypeSystem(this IDecompilerTypeSystem typeSystem, ITypeResolveContext decompilationContext)
        {
            IReadOnlyList <IType> classTypeParameters  = null;
            IReadOnlyList <IType> methodTypeParameters = null;

            if (decompilationContext.CurrentTypeDefinition != null && decompilationContext.CurrentTypeDefinition.TypeParameterCount > 0)
            {
                classTypeParameters = decompilationContext.CurrentTypeDefinition.TypeArguments;
            }
            IMethod method = decompilationContext.CurrentMember as IMethod;

            if (method != null && method.TypeParameters.Count > 0)
            {
                methodTypeParameters = method.TypeArguments;
            }
            if (typeSystem is SpecializingDecompilerTypeSystem)
            {
                typeSystem = ((SpecializingDecompilerTypeSystem)typeSystem).Context;
            }
            if ((classTypeParameters != null && classTypeParameters.Count > 0) || (methodTypeParameters != null && methodTypeParameters.Count > 0))
            {
                return(new SpecializingDecompilerTypeSystem(typeSystem, new TypeParameterSubstitution(classTypeParameters, methodTypeParameters)));
            }
            else
            {
                return(typeSystem);
            }
        }
示例#2
0
 public CallBuilder(ExpressionBuilder expressionBuilder, IDecompilerTypeSystem typeSystem, DecompilerSettings settings)
 {
     this.expressionBuilder = expressionBuilder;
     this.resolver          = expressionBuilder.resolver;
     this.settings          = settings;
     this.typeSystem        = typeSystem;
 }
示例#3
0
 public ILTransformContext(ILFunction function, IDecompilerTypeSystem typeSystem, DecompilerSettings settings = null)
 {
     this.Function   = function ?? throw new ArgumentNullException(nameof(function));
     this.TypeSystem = typeSystem ?? throw new ArgumentNullException(nameof(typeSystem));
     this.Settings   = settings ?? new DecompilerSettings();
     Stepper         = new Stepper();
 }
示例#4
0
 XamlContext(IDecompilerTypeSystem typeSystem)
 {
     TypeSystem       = typeSystem;
     NodeMap          = new Dictionary <BamlRecord, BamlBlockNode>();
     XmlNs            = new XmlnsDictionary();
     GeneratedMembers = new List <EntityHandle>();
     XClassNames      = new List <string>();
 }
示例#5
0
 internal TransformContext(IDecompilerTypeSystem typeSystem, DecompileRun decompileRun, ITypeResolveContext decompilationContext, TypeSystemAstBuilder typeSystemAstBuilder)
 {
     this.TypeSystem           = typeSystem;
     this.DecompileRun         = decompileRun;
     this.decompilationContext = decompilationContext;
     this.TypeSystemAstBuilder = typeSystemAstBuilder;
     this.CancellationToken    = decompileRun.CancellationToken;
     this.Settings             = decompileRun.Settings;
 }
 public StatementBuilder(IDecompilerTypeSystem typeSystem, ITypeResolveContext decompilationContext, IMethod currentMethod, ILFunction currentFunction, DecompilerSettings settings, CancellationToken cancellationToken)
 {
     Debug.Assert(typeSystem != null && decompilationContext != null && currentMethod != null);
     this.exprBuilder       = new ExpressionBuilder(typeSystem, decompilationContext, settings, cancellationToken);
     this.currentFunction   = currentFunction;
     this.currentMethod     = currentMethod;
     this.settings          = settings;
     this.cancellationToken = cancellationToken;
 }
示例#7
0
        BamlContext(IDecompilerTypeSystem typeSystem)
        {
            this.TypeSystem = typeSystem;
            KnownThings     = new KnownThings(typeSystem);

            AssemblyIdMap  = new Dictionary <ushort, AssemblyInfoRecord>();
            AttributeIdMap = new Dictionary <ushort, AttributeInfoRecord>();
            StringIdMap    = new Dictionary <ushort, StringInfoRecord>();
            TypeIdMap      = new Dictionary <ushort, TypeInfoRecord>();
        }
示例#8
0
 internal BlockBuilder(Mono.Cecil.Cil.MethodBody body, IDecompilerTypeSystem typeSystem,
                       Dictionary <Mono.Cecil.Cil.ExceptionHandler, ILVariable> variableByExceptionHandler)
 {
     Debug.Assert(body != null);
     Debug.Assert(typeSystem != null);
     Debug.Assert(variableByExceptionHandler != null);
     this.body       = body;
     this.typeSystem = typeSystem;
     this.variableByExceptionHandler = variableByExceptionHandler;
 }
示例#9
0
 public RecordDecompiler(IDecompilerTypeSystem dts, ITypeDefinition recordTypeDef, CancellationToken cancellationToken)
 {
     this.typeSystem        = dts;
     this.recordTypeDef     = recordTypeDef;
     this.cancellationToken = cancellationToken;
     this.baseClass         = recordTypeDef.DirectBaseTypes.FirstOrDefault(b => b.Kind == TypeKind.Class);
     this.isInheritedRecord = !baseClass.IsKnownType(KnownTypeCode.Object);
     DetectAutomaticProperties();
     this.orderedMembers = DetectMemberOrder(recordTypeDef, backingFieldToAutoProperty);
 }
示例#10
0
 public SpecializingDecompilerTypeSystem(IDecompilerTypeSystem context, TypeParameterSubstitution substitution)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     if (substitution == null)
     {
         throw new ArgumentNullException(nameof(substitution));
     }
     this.context      = context;
     this.substitution = substitution;
 }
示例#11
0
        public static XamlContext Construct(IDecompilerTypeSystem typeSystem, BamlDocument document, CancellationToken token, BamlDecompilerOptions bamlDecompilerOptions)
        {
            var ctx = new XamlContext(typeSystem);

            ctx.CancellationToken     = token;
            ctx.BamlDecompilerOptions = bamlDecompilerOptions ?? new BamlDecompilerOptions();

            ctx.Baml     = BamlContext.ConstructContext(typeSystem, document, token);
            ctx.RootNode = BamlNode.Parse(document, token);

            ctx.BuildPIMappings(document);
            ctx.BuildNodeMap(ctx.RootNode as BamlBlockNode);

            return(ctx);
        }
示例#12
0
 public TextTokenWriter(ITextOutput output, DecompilerSettings settings, IDecompilerTypeSystem typeSystem)
 {
     if (output == null)
     {
         throw new ArgumentNullException(nameof(output));
     }
     if (settings == null)
     {
         throw new ArgumentNullException(nameof(settings));
     }
     if (typeSystem == null)
     {
         throw new ArgumentNullException(nameof(typeSystem));
     }
     this.output     = output;
     this.settings   = settings;
     this.typeSystem = typeSystem;
 }
示例#13
0
        public static BamlContext ConstructContext(IDecompilerTypeSystem typeSystem, BamlDocument document, CancellationToken token)
        {
            var ctx = new BamlContext(typeSystem);

            foreach (var record in document)
            {
                token.ThrowIfCancellationRequested();

                if (record is AssemblyInfoRecord assemblyInfo)
                {
                    if (assemblyInfo.AssemblyId == ctx.AssemblyIdMap.Count)
                    {
                        ctx.AssemblyIdMap.Add(assemblyInfo.AssemblyId, assemblyInfo);
                    }
                }
                else if (record is AttributeInfoRecord attrInfo)
                {
                    if (attrInfo.AttributeId == ctx.AttributeIdMap.Count)
                    {
                        ctx.AttributeIdMap.Add(attrInfo.AttributeId, attrInfo);
                    }
                }
                else if (record is StringInfoRecord strInfo)
                {
                    if (strInfo.StringId == ctx.StringIdMap.Count)
                    {
                        ctx.StringIdMap.Add(strInfo.StringId, strInfo);
                    }
                }
                else if (record is TypeInfoRecord typeInfo)
                {
                    if (typeInfo.TypeId == ctx.TypeIdMap.Count)
                    {
                        ctx.TypeIdMap.Add(typeInfo.TypeId, typeInfo);
                    }
                }
            }

            return(ctx);
        }
示例#14
0
        //private static EcsFile GenerateFile(TypeDefinition t, CSharpDecompiler codeDomBuilder, string attributeString, OutputMode mode, string ext)
        //{
        //	var sourceRes = t.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + attributeString + "File");
        //	var sourceAtt = t.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + attributeString);
        //	if (sourceRes != null) {
        //		var sourceName = t.Name + ext;
        //		sourceName = (string)sourceRes.ConstructorArguments[0].Value;
        //		var sourceContent = EmbeddedFileLoader.GetResourceFile(sourceName, t.Module);
        //		return new EcsFile(sourceName, sourceContent);
        //	}
        //	if (sourceAtt != null) {
        //		var sourceContent = (string)sourceAtt.ConstructorArguments[0].Value;
        //		return new EcsFile(t.Name + ext, sourceContent);
        //	}

        //	return new EcsFile(t.Name + ext, WriteType(codeDomBuilder, mode));
        //}

        string WriteCode(DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem, OutputMode mode)
        {
            //syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });

            var ms = new MemoryStream();

            using (StreamWriter w = new StreamWriter(ms)) {
                //codeDomBuilder.GenerateCode(new PlainTextOutput(w), mode);
                TokenWriter tokenWriter = new TextTokenWriter(new PlainTextOutput(w), settings, typeSystem)
                {
                    FoldBraces = settings.FoldBraces, ExpandMemberDefinitions = settings.ExpandMemberDefinitions
                };
                //syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions));

                syntaxTree.AcceptVisitor(new COutputVisitor(tokenWriter, settings.CSharpFormattingOptions, mode));

                w.Flush();
                ms.Position = 0;
                var sr    = new StreamReader(ms);
                var myStr = sr.ReadToEnd();
                return(myStr);
            }
        }
示例#15
0
        public XDocument Decompile(IDecompilerTypeSystem typeSystem, BamlDocument document, CancellationToken token, BamlDecompilerOptions bamlDecompilerOptions, List <string> assemblyReferences)
        {
            var ctx = XamlContext.Construct(typeSystem, document, token, bamlDecompilerOptions);

            var handler = HandlerMap.LookupHandler(ctx.RootNode.Type);
            var elem    = handler.Translate(ctx, ctx.RootNode, null);

            var xaml = new XDocument();

            xaml.Add(elem.Xaml.Element);

            foreach (var pass in rewritePasses)
            {
                token.ThrowIfCancellationRequested();
                pass.Run(ctx, xaml);
            }

            if (assemblyReferences != null)
            {
                assemblyReferences.AddRange(ctx.Baml.AssemblyIdMap.Select(a => a.Value.AssemblyFullName));
            }

            return(xaml);
        }
示例#16
0
        void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem)
        {
            syntaxTree.AcceptVisitor(new InsertParenthesesVisitor {
                InsertParenthesesForReadability = true
            });
            output.IndentationString = settings.CSharpFormattingOptions.IndentationString;
            TokenWriter tokenWriter = new TextTokenWriter(output, settings, typeSystem);

            if (output is ISmartTextOutput highlightingOutput)
            {
                tokenWriter = new CSharpHighlightingTokenWriter(tokenWriter, highlightingOutput);
            }
            syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions));
        }
示例#17
0
        void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem)
        {
            syntaxTree.AcceptVisitor(new InsertParenthesesVisitor {
                InsertParenthesesForReadability = true
            });
            TokenWriter tokenWriter = new TextTokenWriter(output, settings, typeSystem)
            {
                FoldBraces = settings.FoldBraces, ExpandMemberDefinitions = settings.ExpandMemberDefinitions
            };

            if (output is ISmartTextOutput highlightingOutput)
            {
                tokenWriter = new CSharpHighlightingTokenWriter(tokenWriter, highlightingOutput);
            }
            syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions));
        }
示例#18
0
        static void WriteCode(TextWriter output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem)
        {
            syntaxTree.AcceptVisitor(new InsertParenthesesVisitor {
                InsertParenthesesForReadability = true
            });
            TokenWriter tokenWriter = new TextWriterTokenWriter(output);

            tokenWriter = TokenWriter.WrapInWriterThatSetsLocationsInAST(tokenWriter);
            syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions));
        }
示例#19
0
        void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem)
        {
            syntaxTree.AcceptVisitor(new InsertParenthesesVisitor {
                InsertParenthesesForReadability = true
            });
            var outputFormatter = new TextTokenWriter(output, settings, typeSystem)
            {
                FoldBraces = settings.FoldBraces
            };
            var formattingPolicy = settings.CSharpFormattingOptions;

            syntaxTree.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, formattingPolicy));
        }
示例#20
0
 XamlContext(IDecompilerTypeSystem typeSystem)
 {
     TypeSystem = typeSystem;
     NodeMap    = new Dictionary <BamlRecord, BamlBlockNode>();
     XmlNs      = new XmlnsDictionary();
 }