Beispiel #1
0
        protected ResolveResult Resolve(string code)
        {
            CompilationUnit cu = new CSharpParser().Parse(new StringReader(code.Replace("$", "")));

            TextLocation[] dollars = FindDollarSigns(code).ToArray();
            Assert.AreEqual(2, dollars.Length, "Expected 2 dollar signs marking start+end of desired node");

            SetUp();

            CSharpParsedFile         parsedFile     = new CSharpParsedFile("test.cs", resolver.CurrentUsingScope);
            TypeSystemConvertVisitor convertVisitor = new TypeSystemConvertVisitor(parsedFile, resolver.CurrentUsingScope, null);

            cu.AcceptVisitor(convertVisitor, null);
            project.UpdateProjectContent(null, convertVisitor.ParsedFile);

            FindNodeVisitor fnv = new FindNodeVisitor(dollars[0], dollars[1]);

            cu.AcceptVisitor(fnv, null);
            Assert.IsNotNull(fnv.ResultNode, "Did not find DOM node at the specified location");

            Debug.WriteLine(new string('=', 70));
            Debug.WriteLine("Starting new resolver for " + fnv.ResultNode);

            var           navigator = new NodeListResolveVisitorNavigator(new[] { fnv.ResultNode });
            ResolveResult rr;

            using (var context = this.context.Synchronize()) {
                ResolveVisitor rv = new ResolveVisitor(new CSharpResolver(context), convertVisitor.ParsedFile, navigator);
                rv.Scan(cu);
                rr = rv.GetResolveResult(fnv.ResultNode);
            }
            Assert.IsNotNull(rr, "ResolveResult is null - did something go wrong while navigating to the target node?");
            Debug.WriteLine("ResolveResult is " + rr);
            return(rr);
        }
Beispiel #2
0
        void ResolveButtonClick(object sender, EventArgs e)
        {
            SimpleProjectContent     project        = new SimpleProjectContent();
            TypeSystemConvertVisitor convertVisitor = new TypeSystemConvertVisitor(project, "dummy.cs");

            compilationUnit.AcceptVisitor(convertVisitor, null);
            project.UpdateProjectContent(null, convertVisitor.ParsedFile.TopLevelTypeDefinitions, null, null);

            List <ITypeResolveContext> projects = new List <ITypeResolveContext>();

            projects.Add(project);
            projects.AddRange(builtInLibs.Value);

            using (var context = new CompositeTypeResolveContext(projects).Synchronize()) {
                CSharpResolver resolver = new CSharpResolver(context);

                IResolveVisitorNavigator navigator = null;
                if (csharpTreeView.SelectedNode != null)
                {
                    navigator = new NodeListResolveVisitorNavigator(new[] { (AstNode)csharpTreeView.SelectedNode.Tag });
                }
                ResolveVisitor visitor = new ResolveVisitor(resolver, convertVisitor.ParsedFile, navigator);
                visitor.Scan(compilationUnit);
                csharpTreeView.BeginUpdate();
                ShowResolveResultsInTree(csharpTreeView.Nodes, visitor);
                csharpTreeView.EndUpdate();
            }
        }
Beispiel #3
0
        void FindReferencesButtonClick(object sender, EventArgs e)
        {
            if (csharpTreeView.SelectedNode == null)
            {
                return;
            }

            SimpleProjectContent project = new SimpleProjectContent();
            var parsedFile = new TypeSystemConvertVisitor(project, "dummy.cs").Convert(compilationUnit);

            project.UpdateProjectContent(null, parsedFile);

            List <ITypeResolveContext> projects = new List <ITypeResolveContext>();

            projects.Add(project);
            projects.AddRange(builtInLibs.Value);

            using (var context = new CompositeTypeResolveContext(projects).Synchronize()) {
                CSharpResolver resolver = new CSharpResolver(context);

                AstNode node = (AstNode)csharpTreeView.SelectedNode.Tag;
                IResolveVisitorNavigator navigator = new NodeListResolveVisitorNavigator(new[] { node });
                ResolveVisitor           visitor   = new ResolveVisitor(resolver, parsedFile, navigator);
                visitor.Scan(compilationUnit);
                IEntity             entity;
                MemberResolveResult mrr = visitor.GetResolveResult(node) as MemberResolveResult;
                TypeResolveResult   trr = visitor.GetResolveResult(node) as TypeResolveResult;
                if (mrr != null)
                {
                    entity = mrr.Member;
                }
                else if (trr != null)
                {
                    entity = trr.Type.GetDefinition();
                }
                else
                {
                    return;
                }

                FindReferences         fr             = new FindReferences();
                int                    referenceCount = 0;
                FoundReferenceCallback callback       = delegate(AstNode matchNode, ResolveResult result) {
                    referenceCount++;
                };

                var searchScopes = fr.GetSearchScopes(entity);
                navigator = new CompositeResolveVisitorNavigator(searchScopes.Select(s => s.GetNavigator(callback)).ToArray());
                visitor   = new ResolveVisitor(resolver, parsedFile, navigator);
                visitor.Scan(compilationUnit);

                csharpTreeView.BeginUpdate();
                ShowResolveResultsInTree(csharpTreeView.Nodes, visitor);
                csharpTreeView.EndUpdate();

                MessageBox.Show("Found " + referenceCount + " references to " + entity.FullName);
            }
        }
        public void SetUp()
        {
            pc = new SimpleProjectContent();
            var cu = new CSharpParser().Parse(new StringReader(program));

            parsedFile = new TypeSystemConvertVisitor(pc, "program.cs").Convert(cu);
            pc.UpdateProjectContent(null, parsedFile);

            ctx = new CompositeTypeResolveContext(new[] { pc, CecilLoaderTests.Mscorlib });

            baseClass    = pc.GetTypeDefinition(string.Empty, "Base", 1, StringComparer.Ordinal);
            nestedClass  = baseClass.NestedTypes.Single();
            derivedClass = pc.GetTypeDefinition(string.Empty, "Derived", 2, StringComparer.Ordinal);
            systemClass  = pc.GetTypeDefinition("NS", "System", 0, StringComparer.Ordinal);
        }
Beispiel #5
0
        public void GenerateTypeSystem()
        {
            SimpleProjectContent pc     = new SimpleProjectContent();
            CSharpParser         parser = new CSharpParser();

            parser.GenerateTypeSystemMode = true;
            foreach (string fileName in fileNames)
            {
                CompilationUnit cu;
                using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) {
                    cu = parser.Parse(fs);
                }
                TypeSystemConvertVisitor cv = new TypeSystemConvertVisitor(pc, fileName);
                pc.UpdateProjectContent(null, cv.Convert(cu));
            }
        }
        internal static IProjectContent ParseTestCase()
        {
            const string fileName = "TypeSystemTests.TestCase.cs";

            CSharpParser    parser = new CSharpParser();
            CompilationUnit cu;

            using (Stream s = typeof(TypeSystemTests).Assembly.GetManifestResourceStream(typeof(TypeSystemTests), fileName)) {
                cu = parser.Parse(s);
            }

            var testCasePC = new SimpleProjectContent();
            CSharpParsedFile parsedFile = new TypeSystemConvertVisitor(testCasePC, fileName).Convert(cu);

            parsedFile.Freeze();
            testCasePC.UpdateProjectContent(null, parsedFile);
            return(testCasePC);
        }