public static ReplacementDictionary PrecomputeReplacementNodes(SyntaxDictionary annotationsByNode, Compilation compilation) { #region CodeContracts Contract.Requires(annotationsByNode != null); Contract.Requires(compilation != null); Contract.Ensures(Contract.Result <ReplacementDictionary>() != null); #endregion CodeContracts Output.WriteLine("Precomputing the replacement nodes"); //GetContractRequiresSymbols(comp); var newdict = new ReplacementDictionary(); foreach (var oldkvp in annotationsByNode) { var oldsubdict = oldkvp.Value; var oldfile = oldkvp.Key; var newsubdict = new Dictionary <SyntaxNode, SyntaxNode>(); if (!oldsubdict.Any()) { continue; /* TODO is this right? we have a node with no annotations? */ } SemanticModel = compilation.GetSemanticModel(oldsubdict.First().Key.SyntaxTree); Compilation = compilation; foreach (var oldnode in oldsubdict.Keys) { switch (oldnode.CSharpKind()) { case SyntaxKind.MethodDeclaration: case SyntaxKind.ConstructorDeclaration: case SyntaxKind.SetAccessorDeclaration: case SyntaxKind.GetAccessorDeclaration: case SyntaxKind.AddAccessorDeclaration: // who knew this was a thing? maybe this will work case SyntaxKind.RemoveAccessorDeclaration: // who knew this was a thing? maybe this will work //var oldmethod = oldnode as BaseMethodDeclarationSyntax; //var newmethod = PrecomputeNewMethod(oldmethod, oldsubdict[oldnode]); //if (oldnode.GetText().ToString().Contains("ObjectInvariant")) { int x; } var newmethod = PrecomputeNewMethod(oldnode, oldsubdict[oldnode]); newsubdict.Add(oldnode, newmethod); continue; case SyntaxKind.FieldDeclaration: continue; // we don't need to do anything to read only fields at this point default: RBLogger.Error("Unhandled SyntaxNode kind {0}", oldnode.CSharpKind()); // Debug.Assert(false); // unhandled annotation type continue; } } newdict.Add(oldfile, newsubdict); } return(newdict); }
public static Compilation RewriteCompilation(Compilation original, ReplacementDictionary dict_old_to_new) { Contract.Requires(dict_old_to_new != null); Contract.Ensures(Contract.Result <Compilation>() != null); Output.WriteLine("Rewriting the compilation"); var curr = original.Clone(); Contract.Assume(curr != null); foreach (var st in original.SyntaxTrees) { Dictionary <SyntaxNode, SyntaxNode> subdict; if (dict_old_to_new.TryGetValue(st.FilePath, out subdict)) { var r = new ReplacerInternal(subdict, original); var newtree = r.Replace(st); if (newtree != st) // did something change? { newtree = AddUsingsContracts(newtree); curr = curr.ReplaceSyntaxTree(st, SyntaxFactory.SyntaxTree(newtree.GetRoot(), null, st.FilePath)); Contract.Assume(curr != null); } } } return(curr); //var r = new Replacer(dict_old_to_new, original); //foreach (var st in original.SyntaxTrees) //{ // //var newnode = SyntaxNodeExtensions.ReplaceNodes(st.GetRoot(), dict_old_to_new.Keys, ComputeReplacement); // var newtree = r.Replace(st); // if (newtree != st) // did something change? // { // newtree = AddUsingsContracts(newtree); // curr = curr.ReplaceSyntaxTree(st, newtree); // } //} }