Example #1
0
        private MetadataReference FindMetadataReference(IAssemblySymbol assembly)
        {
            if (_assemblyToReferenceMap == null)
            {
                Interlocked.CompareExchange(ref _assemblyToReferenceMap, Compilations.ToImmutableDictionary(f => f.Assembly, f => f.GetMetadataReference(f.Assembly)), null);
            }

            if (_assemblyToReferenceMap.TryGetValue(assembly, out MetadataReference metadataReference))
            {
                return(metadataReference);
            }

            foreach (Compilation compilation in Compilations)
            {
                foreach (MetadataReference externalReference in compilation.ExternalReferences)
                {
                    ISymbol assemblyOrModule = compilation.GetAssemblyOrModuleSymbol(externalReference);

                    if (SymbolEqualityComparer.Default.Equals(assembly, assemblyOrModule))
                    {
                        return(ImmutableInterlocked.GetOrAdd(ref _assemblyToReferenceMap, assembly, externalReference));
                    }
                }
            }

            return(null);
        }
Example #2
0
 void VerifyTempDir()
 {
     if (TempDir == null)
     {
         TempDir = ProcessHelper.CurrentProcessFile.GetParent().GetDirectory("temp").GetDirectory(DateTime.Now.ToFileTime().ToString());
     }
     Compilations.Where(t => t.TempDir == null).ForEach(t => t.TempDir = TempDir);
     TempDir.VerifyExists();
 }
        private Compilation FindCompilation(IAssemblySymbol assembly)
        {
            if (Compilations.Length == 1)
                return Compilations[0];

            if (_compilationMap == null)
                Interlocked.CompareExchange(ref _compilationMap, Compilations.ToImmutableDictionary(f => f.Assembly, f => f), null);

            return _compilationMap[assembly];
        }
Example #4
0
        public PortableExecutableReference TryGetAssemblyPEReference(TypeMember type)
        {
            var typeSymbol = type.GetBindingOrThrow <INamedTypeSymbol>();
            PortableExecutableReference reference;

            if (typeSymbol.ContainingAssembly != null)
            {
                reference = Compilations
                            .Select(x => x.GetMetadataReference(typeSymbol.ContainingAssembly))
                            .OfType <PortableExecutableReference>()
                            .FirstOrDefault();
            }
            else
            {
                reference = null;
            }

            return(reference);
        }
Example #5
0
 static void Main(string[] args)
 {
     var original   = Compilations.FromCsprojAsync(args[0]).Result;
     var transpiled = Driver.Transpile(original);
 }
Example #6
0
        public override void Init(LoggerProvider p, bool reInit = false)
        {
            if (this.Initialized && !reInit)
            {
                return;
            }
            this.Logger = p;
            this.Types  = new List <NamedTypeBase>();

            this.symbols        = new List <ISymbol>();
            this.SemanticModels = new Dictionary <ISymbol, SemanticModel>();
            this.TreeModels     = new Dictionary <SyntaxTree, SemanticModel>();
            this.SyntaxTrees    = new Dictionary <ISymbol, SyntaxTree>();

            List <Tuple <ISymbol, SemanticModel> > tempSymbols = new List <Tuple <ISymbol, SemanticModel> >();

            using (ProfilerService.Current.Section("Getting symbols"))
            {
                for (int i = 0; i < Compilations.Count; i++)
                {
                    var comp      = Compilations[i];
                    var compTrees = comp.SyntaxTrees.ToList();
                    for (int j = 0; j < compTrees.Count; j++)
                    {
                        tempSymbols.AddRange(GetSymbols(compTrees[j], comp));
                    }
                }
            }

            using (ProfilerService.Current.Section("Building symbol info"))
            {
                this.symbols = tempSymbols.Select(t => t.Item1).ToList();
                for (int i = 0; i < tempSymbols.Count; i++)
                {
                    if (tempSymbols[i].Item1.Locations.Length > 0 && tempSymbols[i].Item1.Locations[0].IsInSource)
                    {
                        var sTree = tempSymbols[i].Item1.Locations[0].SourceTree;

                        if (!this.SemanticModels.ContainsKey(tempSymbols[i].Item1))
                        {
                            SyntaxTrees.Add(tempSymbols[i].Item1, sTree);
                        }

                        var sComp = Compilations.SingleOrDefault(c => c.SyntaxTrees.Contains(sTree));

                        if (sComp == null)
                        {
                            Logger.WriteToLog(string.Format("Cannot find compilation for tree {0}", sTree.FilePath));
                        }
                        else
                        {
                            if (!this.SemanticModels.ContainsKey(tempSymbols[i].Item1))
                            {
                                this.SemanticModels.Add(tempSymbols[i].Item1, sComp.GetSemanticModel(sTree));
                            }
                            if (!this.TreeModels.ContainsKey(sTree))
                            {
                                this.TreeModels.Add(sTree, sComp.GetSemanticModel(sTree));
                            }
                        }
                    }
                    else
                    {
                        if (!this.SemanticModels.ContainsKey(tempSymbols[i].Item1))
                        {
                            this.SemanticModels.Add(tempSymbols[i].Item1, tempSymbols[i].Item2);
                        }
                    }
                }
            }


            CSharpModelBuilder.InitLogger(p);

            using (ProfilerService.Current.Method("BuildTypeList"))
            {
                CSharpModelBuilder.BuildTypeList(Types, symbols.Where(s => s.Kind == SymbolKind.NamedType).Distinct().Cast <INamedTypeSymbol>());
            }

            this.Types = this.Types.Distinct(new TypeComparer()).ToList();

            using (ProfilerService.Current.Method("BuildArrayTypeList"))
            {
                CSharpModelBuilder.BuildArrayTypeList(Types, symbols.Where(s => s.Kind == SymbolKind.ArrayType).Distinct().Cast <IArrayTypeSymbol>());
            }

            using (ProfilerService.Current.Method("BuildMethodList"))
            {
                CSharpModelBuilder.BuildMethodList(Types, symbols.Where(s => s.Kind == SymbolKind.Method).Distinct().Cast <IMethodSymbol>()
                                                   .Where(s => s.MethodKind == MethodKind.Ordinary || s.MethodKind == MethodKind.Constructor || s.MethodKind == MethodKind.PropertyGet || s.MethodKind == MethodKind.PropertySet), this.SemanticModels);
            }

            using (ProfilerService.Current.Method("BuildFieldList"))
            {
                CSharpModelBuilder.BuildFieldList(Types, symbols.Where(s => s.Kind == SymbolKind.Field).Distinct().Cast <IFieldSymbol>(), symbols.Where(s => s.Kind == SymbolKind.Property).Cast <IPropertySymbol>(), this.SemanticModels);
            }

            using (ProfilerService.Current.Method("BuildCallGraph"))
            {
                this.CallGraph = CSharpModelBuilder.BuildCallGraph(this.TreeModels, this.Types, this.Logger);
            }

            this.Initialized = true;

            using (var fs = File.Create(string.Join(".", this.Name, "bin")))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, this.Types);
            }
        }
Example #7
0
 public void Run(CsmCompilation cc)
 {
     Compilations.Add(cc);
     VerifyTempDir();
     cc.Run();
 }
Example #8
0
 public void Run()
 {
     VerifyTempDir();
     Compilations.ForEach(t => t.Run());
 }