Example #1
0
        public Rewriter(List <CSharpCompilation> compilations, List <HashSet <AbstractNode> > clusters, Dictionary <int, List <int> > ancestorMap, string typename)
        {
            _compilations     = compilations;
            _clusters         = clusters;
            _ancestorMap      = ancestorMap;
            _typename         = typename;
            classDeclarations = new List <string>();
            typeCaster        = new TypeCaster(_typename);
            rewriteWalker     = new RewriteWalker(_typename);

            rewriteWalker.InitRewrites(_clusters);
            int clusterID = 0;

            foreach (var colors in _clusters)
            {
                classDeclarations.Add(GetClusterAsClassDecl(clusterID, ancestorMap[clusterID++]));
            }
        }
Example #2
0
        public void RewriteTypes()
        {
            string namespaceDescription = @"namespace BespokeLattice {\n" + string.Join("\n", classDeclarations) + "\n};\n";

            foreach (var compilation in _compilations)
            {
                var typeLatticeTree = CSharpSyntaxTree.ParseText(namespaceDescription);
                typeLatticeTree = typeLatticeTree.GetRoot().SyntaxTree.WithRootAndOptions(typeLatticeTree.GetRoot(), compilation.SyntaxTrees[0].Options);
                var modifiedCompilation = compilation.AddSyntaxTrees(new SyntaxTree[] { typeLatticeTree });
                var m = modifiedCompilation.GetSemanticModel(typeLatticeTree);

                foreach (var tree in modifiedCompilation.SyntaxTrees)
                {
                    var newSourceFile = tree.FilePath + "_refinym_modified.cs";

                    if (tree.IsEquivalentTo(typeLatticeTree))
                    {
                        continue;
                    }

                    var finalTree = tree;

                    rewriteWalker = new RewriteWalker(_typename)
                    {
                        SemanticModel = modifiedCompilation.GetSemanticModel(tree),
                        Rewrote       = false
                    };
                    rewriteWalker.InitRewrites(_clusters);
                    var        rewriteResult  = rewriteWalker.Visit(tree.GetRoot());
                    SyntaxTree fixedFinalTree = finalTree;

                    if (rewriteWalker.Rewrote)
                    {
                        typeCaster = new TypeCaster(_typename);
                        var fixedRewriteResultSyntaxTree = rewriteResult.SyntaxTree.WithRootAndOptions(rewriteResult.SyntaxTree.GetRoot(), tree.Options);
                        modifiedCompilation = modifiedCompilation.ReplaceSyntaxTree(tree, fixedRewriteResultSyntaxTree);

                        typeCaster.SemanticModel = modifiedCompilation.GetSemanticModel(fixedRewriteResultSyntaxTree);
                        var syntaxTreeTypeRecast = typeCaster.Visit(fixedRewriteResultSyntaxTree.GetRoot());
                        //add inclusion of the BespokeLAttice namespace at the root
                        var compilationUnit = syntaxTreeTypeRecast.SyntaxTree.GetRoot() as CompilationUnitSyntax;

                        UsingDirectiveSyntax newUsing = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("BespokeLattice"));
                        SyntaxTree           newTree;
                        if (compilationUnit.Usings.Count() > 0)
                        {
                            newTree = CSharpSyntaxTree.Create(compilationUnit.InsertNodesAfter(compilationUnit.Usings[0], new[] { newUsing }).NormalizeWhitespace());
                        }
                        else
                        {
                            newTree = CSharpSyntaxTree.Create(
                                compilationUnit.AddUsings(
                                    SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("BespokeLattice"))
                                    ).NormalizeWhitespace()
                                );
                        }

                        //modifiedCompilation = modifiedCompilation.ReplaceSyntaxTree(rewriteResult.SyntaxTree, newTree);

                        File.WriteAllText(newSourceFile, newTree.GetRoot().ToFullString());
                        finalTree      = CSharpSyntaxTree.ParseText(File.ReadAllText(newSourceFile), path: newSourceFile);
                        fixedFinalTree = finalTree.WithRootAndOptions(finalTree.GetRoot(), tree.Options);

                        modifiedCompilation = modifiedCompilation.ReplaceSyntaxTree(fixedRewriteResultSyntaxTree, fixedFinalTree);
                    }

                    var newSemanticModel = modifiedCompilation.GetSemanticModel(fixedFinalTree);
                    foreach (var d in newSemanticModel.GetDiagnostics())
                    {
                        if (d.WarningLevel == 0)
                        {
                            Console.WriteLine(d);
                            if (ErrorHistogram.ContainsKey(d.Id))
                            {
                                ErrorHistogram[d.Id].Add(new Tuple <Diagnostic, string>(d, d.GetMessage()));
                            }
                            else
                            {
                                ErrorHistogram.Add(d.Id, new List <Tuple <Diagnostic, string> > {
                                    new Tuple <Diagnostic, string>(d, d.GetMessage())
                                });
                            }
                        }
                    }
                }
            }
        }