/// <summary>
        /// Runs the transformations needed to produce a valid compilation unit.
        /// </summary>
        public void Run(bool persistGeneratedCode, bool optimizeCode)
        {
#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);
            }

#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
        }
Example #2
0
        private SyntaxNode RewriteTypeDefinition(TypeDeclarationSyntax node)
        {
            var attrs            = node.AttributeLists;
            var dictionaryMapKey =
                attrs
                .SelectMany(al =>
                            al
                            .Attributes
                            .Where(a => a.Name.ToFullString().EndsWith("TypeKind"))
                            .Select(a => a.ArgumentList.Arguments[0].ToFullString().Trim('"')))
                .FirstOrDefault();

            if (dictionaryMapKey != null && this.dictionaryMap.TryGetValue(dictionaryMapKey, out IDictionary <ISymbol, SyntaxNode> d))
            {
                var replacer = new TypeReplacer(this.compilation, d);
                return(replacer.Visit(node));
            }
            else
            {
                return(node);
            }
        }