public Tuple <Assembly, string> Compile(bool persistGeneratedCode) { string errors = ""; try { if (persistGeneratedCode) { foreach (var t in this.compilation.SyntaxTrees) { var root = t.GetRoot(); ReplaceNode(root, root.NormalizeWhitespace()); // updates compilation } } var assembly = RoslynHelpers.EmitCompilationAndLoadAssembly(compilation, persistGeneratedCode, out errors); return(Tuple.Create(assembly, errors)); } catch (Exception) { RoslynHelpers.DebugPPrint(compilation.SyntaxTrees[0].GetRoot()); // BUG: Need to print entire compilation? System.Diagnostics.Debug.WriteLine("Can't load dll for the above code"); System.Diagnostics.Debug.WriteLine(errors); throw; } }
public void AddAssemblyReferencesNeededFor(params Type[] args) { var rs = args .SelectMany(a => RoslynHelpers.MetadataReferencesNeededForType(a)) .Where(r => !this.metadataReferences.ContainsKey(r.Display)) .Where(r => !this.compilation.References.Any(r2 => r2.Display.Equals(r.Display))) ; compilation = compilation.AddReferences(rs); //addAssemblyReferences(Microsoft.StreamProcessing.CommonTransformer.Transformer.AssemblyReferencesNeededFor(args).ToArray()); }
/// <summary> /// Runs the transformations needed to produce a valid compilation unit. /// </summary> public void Run(bool persistGeneratedCode, bool optimizeCode, long LogTotalSizeBytes, double LogMutableFraction, int LogPageSizeBits) { #if TIMING Stopwatch sw = new Stopwatch(); sw.Start(); #endif // side-effect: creates this.compilation CreateCompilation(persistGeneratedCode, optimizeCode); foreach (var rtTP in this.runtimeTypeParameters) { AddAssemblyReferencesNeededFor(rtTP); } var d = new Dictionary <ISymbol, SyntaxNode>(); var i = 0; UpdateDictionary(d, FindSymbol("Key"), this.runtimeTypeParameters.ElementAt(i++)); UpdateDictionary(d, FindSymbol("Value"), this.runtimeTypeParameters.ElementAt(i++)); UpdateDictionary(d, FindSymbol("Input"), this.runtimeTypeParameters.ElementAt(i++)); UpdateDictionary(d, FindSymbol("Output"), this.runtimeTypeParameters.ElementAt(i++)); UpdateDictionary(d, FindSymbol("Context"), this.runtimeTypeParameters.ElementAt(i++)); UpdateDictionary(d, FindSymbol("Functions"), this.runtimeTypeParameters.ElementAt(i++)); UpdateDictionary(d, FindSymbol("IFasterKV"), this.runtimeTypeParameters.ElementAt(i++)); var pass1 = new TypeReplacer(this.compilation, d); var pass2 = new NamespaceReplacer(this.compilation); var FASTDotCoreNamespaceName = SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("FASTER"), SyntaxFactory.IdentifierName("core")); var usingFASTDotCore = SyntaxFactory.UsingDirective(FASTDotCoreNamespaceName); foreach (var t in compilation.SyntaxTrees) { var oldTree = t; var oldNode = t.GetRoot(); var newNode = pass1.Visit(oldNode); newNode = pass2.Visit(newNode); var newRoot = oldTree.GetRoot().ReplaceNode(oldNode, newNode); var newTree = oldTree .WithRootAndOptions(newRoot, CSharpParseOptions.Default) ; var compilationSyntax = (CompilationUnitSyntax)newTree.GetRoot(); compilationSyntax = compilationSyntax.AddUsings(usingFASTDotCore); newTree = newTree .WithRootAndOptions(compilationSyntax, CSharpParseOptions.Default); compilation = compilation.ReplaceSyntaxTree(oldTree, newTree); } compilation = RoslynHelpers.ReplaceConstantValue(compilation, "LogMutableFraction", LogMutableFraction.ToString(CultureInfo.InvariantCulture)); compilation = RoslynHelpers.ReplaceConstantValue(compilation, "LogTotalSizeBytes", LogTotalSizeBytes.ToString(CultureInfo.InvariantCulture)); compilation = RoslynHelpers.ReplaceConstantValue(compilation, "LogPageSizeBits", LogPageSizeBits.ToString(CultureInfo.InvariantCulture)); #if TIMING sw.Stop(); System.Diagnostics.Debug.WriteLine("Time to run the FasterHashTable compiler: {0}ms", sw.ElapsedMilliseconds); using (var fileStream = new StreamWriter("foo.txt", true)) { fileStream.WriteLine("Time to run the FasterHashTable compiler: {0}ms", sw.ElapsedMilliseconds); } #endif }