public SourceEmitter GetSourceEmitter(CodeLanguage lang, IModule module, SourceEmitterOutputString output)
        {
            var moduleInfo = _moduleInfoList.Single(m => m.Module.Name.UniqueKey == module.Name.UniqueKey);
            var reader     = moduleInfo.PdbReader;

            return(new VisualSourceEmitter(output, _host, reader, noIL: lang == CodeLanguage.CSharp,
                                           printCompilerGeneratedMembers: false));
        }
示例#2
0
        private string PrintExpression(IExpression expression)
        {
            SourceEmitterOutputString sourceEmitterOutput = new SourceEmitterOutputString();
            SourceEmitter             CSSourceEmitter     = new SourceEmitter(sourceEmitterOutput);

            CSSourceEmitter.Traverse(expression);
            return(sourceEmitterOutput.Data);
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        protected string WriteNeutralFromAST(IExpression expression)
        {
            Contract.Requires(expression != null);

            var sourceEmitterOutput = new SourceEmitterOutputString();
            var sourceEmitter       = new NeutralSourceEmitter(sourceEmitterOutput);
            var es = new ExpressionSimplifier();

            expression = es.Rewrite(expression);
            sourceEmitter.Traverse(expression);
            return(sourceEmitterOutput.Data);
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        protected string WriteCSharpFromAST(IExpression expression)
        {
            Contract.Requires(expression != null);

            SourceEmitterOutputString sourceEmitterOutput = new SourceEmitterOutputString();
            SourceEmitter             CSSourceEmitter     = new SourceEmitter(sourceEmitterOutput);
            ExpressionSimplifier      es = new ExpressionSimplifier();

            expression = es.Rewrite(expression);
            CSSourceEmitter.Traverse(expression);
            return(sourceEmitterOutput.Data);
        }
示例#5
0
        public string Visualize(CodeLanguage language, ICciModuleSource modules)
        {
            var sb = new StringBuilder();

            foreach (var assembly in modules.Modules)
            {
                var sourceEmitterOutput = new SourceEmitterOutputString();
                var sourceEmitter       = modules.GetSourceEmitter(language, assembly.Module, sourceEmitterOutput);
                sourceEmitter.Traverse(assembly.Module);
                sb.Append(sourceEmitterOutput.Data);
            }

            return(sb.ToString());
        }
示例#6
0
        public string Visualize(CodeLanguage language, IMethodDefinition method, ICciModuleSource moduSource)
        {
            if (method == null)
            {
                return("No method to visualize.");
            }
            _log.Info("Visualize: " + method);
            var module = (IModule)TypeHelper.GetDefiningUnit(method.ContainingTypeDefinition);
            var sourceEmitterOutput = new SourceEmitterOutputString();
            var sourceEmitter       = moduSource.GetSourceEmitter(language, module, sourceEmitterOutput);

            sourceEmitter.Traverse(method);
            return(sourceEmitterOutput.Data);
        }
示例#7
0
        public static string PrintIndexer(IMethodDefinition method, bool isGetter)
        {
            Contract.Requires(method != null);

            var sourceEmitterOutput     = new SourceEmitterOutputString();
            var csSourceEmitter         = new SourceEmitter(sourceEmitterOutput);
            var indexerSignatureBuilder = new StringBuilder();

            csSourceEmitter.Traverse(method.Type);
            indexerSignatureBuilder.Append(sourceEmitterOutput.Data);
            sourceEmitterOutput.ClearData();
            indexerSignatureBuilder.Append(" ");
            // Indexers are always identified with a 'this' keyword, but might have an interface prefix
            string id      = method.Name.Value;
            int    lastDot = id.LastIndexOf('.');

            if (lastDot != -1)
            {
                indexerSignatureBuilder.Append(id.Substring(0, lastDot + 1));
            }
            indexerSignatureBuilder.Append("this");
            indexerSignatureBuilder.Append("[");
            bool fFirstParameter = true;
            var  parms           = method.Parameters;

            if (!isGetter)
            {
                // Use the setter's names except for the final 'value' parameter
                var l = new List <IParameterDefinition>(method.Parameters);
                if (l.Count > 0)
                {
                    l.RemoveAt(l.Count - 1);
                }
                parms = l;
            }
            foreach (IParameterDefinition parameterDefinition in parms)
            {
                if (!fFirstParameter)
                {
                    indexerSignatureBuilder.Append(", ");
                }
                Contract.Assume(parameterDefinition != null);
                csSourceEmitter.Traverse(parameterDefinition);
                indexerSignatureBuilder.Append(sourceEmitterOutput.Data);
                sourceEmitterOutput.ClearData();
                fFirstParameter = false;
            }
            indexerSignatureBuilder.Append("]");
            return(indexerSignatureBuilder.ToString());
        }
示例#8
0
        public string Visualize(CodeLanguage language, IMethodDefinition mainMethod, System.Collections.Generic.List <IMethodDefinition> addMethods, ICciModuleSource moduSource)
        {
            if (mainMethod == null)
            {
                return("No method to visualize.");
            }
            _log.Info("Visualize: " + mainMethod);
            var module = (IModule)TypeHelper.GetDefiningUnit(addMethods[0].ContainingTypeDefinition);
            var sourceEmitterOutput = new SourceEmitterOutputString();
            var sourceEmitter       = moduSource.GetSourceEmitter(language, module, sourceEmitterOutput);

            sourceEmitter.Traverse(addMethods[0]);

            return(sourceEmitterOutput.Data);
        }
示例#9
0
        // Get the source for a namespace/type at root scope
        private string Decompile(string rootName)
        {
            // Use CCI's formatting style for consistency
            SourceEmitterOutputString sourceEmitterOutput = new SourceEmitterOutputString(2);
            SourceEmitter             csSourceEmitter     = new SourceEmitter(sourceEmitterOutput);

            csSourceEmitter.LeftCurlyOnNewLine = false;

            var members = new List <INamespaceMember>(
                m_inputAsm.UnitNamespaceRoot.GetMembersNamed(m_host.NameTable.GetNameFor(rootName), false));

            Assert.AreEqual(1, members.Count, "Didn't find one root namespace member named \"" + rootName + "\".");

            csSourceEmitter.Traverse(members[0]);

            return(sourceEmitterOutput.Data);
        }
示例#10
0
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                Console.WriteLine("usage: peToText [path]fileName.ext [noIL] [noStack]");
                return;
            }
            bool noIL    = args.Length >= 2;
            bool noStack = args.Length >= 3;

            using (var host = new HostEnvironment())
            {
                IModule /*?*/ module = host.LoadUnitFrom(args[0]) as IModule;
                if (module == null || module is Dummy)
                {
                    Console.WriteLine(args[0] + " is not a PE file containing a CLR module or assembly.");
                    return;
                }

                PdbReader /*?*/ pdbReader = null;
                string          pdbFile   = Path.ChangeExtension(module.Location, "pdb");
                if (File.Exists(pdbFile))
                {
                    using (var pdbStream = File.OpenRead(pdbFile)) {
                        pdbReader = new PdbReader(pdbStream, host);
                    }
                }
                using (pdbReader) {
                    var options = DecompilerOptions.AnonymousDelegates | DecompilerOptions.Iterators | DecompilerOptions.Loops;
                    if (noStack)
                    {
                        options |= DecompilerOptions.Unstack;
                    }
                    module = Decompiler.GetCodeModelFromMetadataModel(host, module, pdbReader, options);
                    SourceEmitterOutputString sourceEmitterOutput = new SourceEmitterOutputString();
                    SourceEmitter             csSourceEmitter     = new SourceEmitter(sourceEmitterOutput, host, pdbReader, noIL, printCompilerGeneratedMembers: true);
                    csSourceEmitter.Traverse(module);
                    string txtFile = Path.ChangeExtension(pdbFile, "txt");
                    File.WriteAllText(txtFile, sourceEmitterOutput.Data, System.Text.Encoding.UTF8);
                }
            }
        }
示例#11
0
        public static void TestCodeModel()
        {
            using (var host = new HostEnvironment()) {
                var             location = typeof(CodeModelTestInput.Class1).Assembly.Location;
                IAssembly /*?*/ assembly = host.LoadUnitFrom(location) as IAssembly;
                Assert.True(assembly != null, "Failed to read in test executable as test data");

                PdbReader /*?*/ pdbReader = null;
                string          pdbFile   = Path.ChangeExtension(assembly.Location, "pdb");
                if (File.Exists(pdbFile))
                {
                    using (var pdbStream = File.OpenRead(pdbFile)) {
                        pdbReader = new PdbReader(pdbStream, host);
                    }
                }
                using (pdbReader) {
                    SourceEmitterOutputString sourceEmitterOutput = new SourceEmitterOutputString();
                    SourceEmitter             csSourceEmitter     = new SourceEmitter(sourceEmitterOutput, host, pdbReader, noIL: true, printCompilerGeneratedMembers: false, noStack: false);
                    csSourceEmitter.Traverse((INamespaceDefinition)assembly.UnitNamespaceRoot);
                    string result = sourceEmitterOutput.Data;
                    string expected;
                    using (var resource = typeof(Test).Assembly.GetManifestResourceStream("CodeModelTests.CodeModelTestInput.txt")) {
                        using (var reader = new StreamReader(resource)) {
                            expected = reader.ReadToEnd();
                        }
                    }

                    if (result != expected)
                    {
                        string resultFile = Path.GetFullPath("CodeModelTestOutput.txt");
                        File.WriteAllText(resultFile, result);
                        Assert.True(false, "Output didn't match CodeModelTestInput.txt: " + resultFile);
                    }
                }
            }
        }