public Build GenerateCode(List <SourceFile> sourcefiles,
                                  IEnumerable <Calculation> calculations)
        {
            // ReSharper disable once CoVariantArrayConversion
            MetadataReference[] references = Assembly.GetExecutingAssembly().GetReferencedAssemblies().Select(_ => AssemblyMetadata.CreateFromFile(Assembly.Load(_).Location).GetReference()).ToArray();

            references = references.Concat(new[] {
                AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(Microsoft.VisualBasic.Constants).Assembly.Location).GetReference()
            }).ToArray();

            Dictionary <string, Calculation> calculationsLookup = calculations.ToDictionary(_ => _.Id);

            using MemoryStream stream = new MemoryStream();

            Build build = GenerateCode(sourcefiles, references, stream, calculationsLookup);

            if (build.Success)
            {
                stream.Seek(0L, SeekOrigin.Begin);

                byte[] data = new byte[stream.Length];
                stream.Read(data, 0, data.Length);

                build.Assembly = data;
            }

            return(build);
        }
Exemplo n.º 2
0
        public BuildResult Build(GenerateResult generateResult, BuildPartition buildPartition, ILogger logger)
        {
            logger.WriteLineInfo($"BuildScript: {generateResult.ArtifactsPaths.BuildScriptFilePath}");

            var syntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(generateResult.ArtifactsPaths.ProgramCodePath));

            var compilationOptions = new CSharpCompilationOptions(
                outputKind: OutputKind.ConsoleApplication,
                optimizationLevel: OptimizationLevel.Release,
                allowUnsafe: true,
                platform: GetPlatform(buildPartition.Platform),
                deterministic: true);

            compilationOptions = compilationOptions.WithIgnoreCorLibraryDuplicatedTypes();

            var references = Generator
                             .GetAllReferences(buildPartition.RepresentativeBenchmarkCase)
                             .Select(assembly => AssemblyMetadata.CreateFromFile(assembly.Location))
                             .Concat(FrameworkAssembliesMetadata.Value)
                             .Distinct()
                             .Select(uniqueMetadata => uniqueMetadata.GetReference())
                             .ToList();

            var(result, missingReferences) = Build(generateResult, syntaxTree, compilationOptions, references);

            if (result.IsBuildSuccess || !missingReferences.Any())
            {
                return(result);
            }

            var withMissingReferences = references.Union(missingReferences.Select(assemblyMetadata => assemblyMetadata.GetReference()));

            return(Build(generateResult, syntaxTree, compilationOptions, withMissingReferences).result);
        }
Exemplo n.º 3
0
        public static Assembly Compile(params string[] sources)
        {
            var assemblyFileName = "gen" + Guid.NewGuid().ToString().Replace("-", "") + ".dll";
            var compilation      = CSharpCompilation.Create(assemblyFileName,
                                                            options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
                                                            syntaxTrees: from source in sources
                                                            select CSharpSyntaxTree.ParseText(source),
                                                            references: new[]
            {
                AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(RuntimeBinderException).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(DynamicAttribute).Assembly.Location).GetReference(),
            });

            EmitResult emitResult;

            using (var ms = new MemoryStream())
            {
                emitResult = compilation.Emit(ms);

                if (emitResult.Success)
                {
                    var assembly = Assembly.Load(ms.GetBuffer());
                    return(assembly);
                }
            }

            var message = string.Join("\r\n", emitResult.Diagnostics);

            throw new ApplicationException(message);
        }
        public void Ctor_Errors()
        {
            Assert.Throws <ArgumentNullException>(() => AssemblyMetadata.CreateFromImage(default(ImmutableArray <byte>)));

            IEnumerable <byte> enumerableImage = null;

            Assert.Throws <ArgumentNullException>(() => AssemblyMetadata.CreateFromImage(enumerableImage));

            byte[] arrayImage = null;
            Assert.Throws <ArgumentNullException>(() => AssemblyMetadata.CreateFromImage(arrayImage));

            Assert.Throws <ArgumentNullException>(() => AssemblyMetadata.Create((ModuleMetadata)null));
            Assert.Throws <ArgumentException>(() => AssemblyMetadata.Create(default(ImmutableArray <ModuleMetadata>)));
            Assert.Throws <ArgumentException>(() => AssemblyMetadata.Create(ImmutableArray.Create <ModuleMetadata>()));

            var m1 = ModuleMetadata.CreateFromImage(TestResources.SymbolsTests.MultiModule.MultiModuleDll);
            var m2 = ModuleMetadata.CreateFromImage(TestResources.SymbolsTests.MultiModule.mod2);
            var m3 = ModuleMetadata.CreateFromImage(TestResources.SymbolsTests.MultiModule.mod3);

            Assert.Throws <ArgumentException>(() => AssemblyMetadata.Create(m1, m2.Copy(), m3));
            Assert.Throws <ArgumentException>(() => AssemblyMetadata.Create(new List <ModuleMetadata>(new ModuleMetadata[] { m1.Copy(), m2.Copy(), m3.Copy() })));
            Assert.Throws <ArgumentNullException>(() => AssemblyMetadata.Create(ImmutableArray.Create(m1, m2, null)));
            Assert.Throws <ArgumentNullException>(() => AssemblyMetadata.Create(ImmutableArray.Create((ModuleMetadata)null)));

            Assert.Throws <ArgumentNullException>(() => AssemblyMetadata.CreateFromFile((string)null));
        }
Exemplo n.º 5
0
            > GetAnalyzerTypeNameMap(
            string fullPath,
            Type attributeType,
            AttributeLanguagesFunc languagesFunc
            )
        {
            using var assembly = AssemblyMetadata.CreateFromFile(fullPath);

            // This is longer than strictly necessary to avoid thrashing the GC with string allocations
            // in the call to GetFullyQualifiedTypeNames. Specifically, this checks for the presence of
            // supported languages prior to creating the type names.
            var typeNameMap =
                from module in assembly.GetModules()
                from typeDefHandle in module.MetadataReader.TypeDefinitions
                let typeDef = module.MetadataReader.GetTypeDefinition(typeDefHandle)
                              let supportedLanguages = GetSupportedLanguages(
                    typeDef,
                    module.Module,
                    attributeType,
                    languagesFunc
                    )
                                                       where supportedLanguages.Any()
                                                       let typeName = GetFullyQualifiedTypeName(typeDef, module.Module)
                                                                      from supportedLanguage in supportedLanguages
                                                                      group typeName by supportedLanguage;

            return(typeNameMap.ToImmutableSortedDictionary(
                       g => g.Key,
                       g => g.ToImmutableSortedSet(StringComparer.OrdinalIgnoreCase),
                       StringComparer.OrdinalIgnoreCase
                       ));
        }
Exemplo n.º 6
0
        private AssemblyMetadata Compile(string[] source)
        {
            var assemblyName = Path.GetFileNameWithoutExtension(this.FirstSource);

            var cacheFolder = this.AssembliesCachePath;

            if (!Directory.Exists(cacheFolder))
            {
                Directory.CreateDirectory(cacheFolder);
            }

            var compiled    = false;
            var dllFilePath = Path.Combine(cacheFolder, string.Concat(assemblyName, ".dll"));
            var pdbFilePath = Path.Combine(cacheFolder, string.Concat(assemblyName, ".pdb"));

            using (var dllStream = new FileStream(dllFilePath, FileMode.Create))
            {
                using (var pdbStream = new FileStream(pdbFilePath, FileMode.Create))
                {
                    compiled = CompileTo(source, dllStream, pdbStream);
                }
            }

            if (!compiled)
            {
                File.Delete(dllFilePath);
                File.Delete(pdbFilePath);
                return(null);
            }

            return(AssemblyMetadata.CreateFromFile(dllFilePath));
        }
Exemplo n.º 7
0
        public void BadImageFormat()
        {
            var invalidModuleName = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.Invalid.InvalidModuleName);
            var metadata          = AssemblyMetadata.CreateFromFile(invalidModuleName.Path);

            Assert.Throws <BadImageFormatException>(() => metadata.GetModules());
        }
Exemplo n.º 8
0
        public static void Go(string outDir, IEnumerable <string> extraTranslation, string exePath = "",
                              IEnumerable <string> cSharp = null, string testName = "")
        {
            var compilation = CSharpCompilation.Create(testName, cSharp.Select(o => CSharpSyntaxTree.ParseText(o)),
                                                       new MetadataReference[]
            {
                AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(RuntimeBinderException).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(DynamicAttribute).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(Queryable).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(DataTable).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(XmlAttribute).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(CSharpCodeProvider).Assembly.Location).GetReference(),
                //                AssemblyMetadata.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute).Assembly.Location).GetReference(),
                //                AssemblyMetadata.CreateFromFile(typeof(System.ComponentModel.Composition.ImportAttribute).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(HttpRequest).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(CSharpCodeProvider).Assembly.Location).GetReference()
            }, new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true));

            _compilation = compilation;
            OutDir       = outDir;

            Context.Update(compilation);
            Task.WaitAll(Task.Run(() => Build(exePath)), Task.Run(() => Generate(extraTranslation)));
        }
Exemplo n.º 9
0
        public Build GenerateCode(List <SourceFile> sourcefiles)
        {
            MetadataReference[] references =
            {
                AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference(),
                AssemblyMetadata.CreateFromFile(typeof(Microsoft.VisualBasic.Constants).Assembly.Location).GetReference()
            };

            using (var ms = new MemoryStream())
            {
                var build = GenerateCode(sourcefiles, references, ms);

                if (build.Success)
                {
                    ms.Seek(0L, SeekOrigin.Begin);

                    byte[] data = new byte[ms.Length];
                    ms.Read(data, 0, data.Length);

                    build.Assembly = data;
                }

                return(build);
            }
        }
Exemplo n.º 10
0
        public BuildResult Build(GenerateResult generateResult, ILogger logger, Benchmark benchmark, IResolver resolver)
        {
            logger.WriteLineInfo($"BuildScript: {generateResult.ArtifactsPaths.BuildScriptFilePath}");

            var syntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(generateResult.ArtifactsPaths.ProgramCodePath));

            var compilationOptions = new CSharpCompilationOptions(
                outputKind: OutputKind.ConsoleApplication,
                optimizationLevel: OptimizationLevel.Release,
                allowUnsafe: true,
                platform: GetPlatform(benchmark.Job.ResolveValue(EnvMode.PlatformCharacteristic, resolver)),
                deterministic: true);

            var references = Generator
                             .GetAllReferences(benchmark)
                             .Select(assembly => AssemblyMetadata.CreateFromFile(assembly.Location))
                             .Concat(FrameworkAssembliesMetadata.Value)
                             .Distinct()
                             .Select(uniqueMetadata => uniqueMetadata.GetReference());

            (var result, var missingReferences) = Build(generateResult, benchmark, syntaxTree, compilationOptions, references);

            if (result.IsBuildSuccess || !missingReferences.Any())
            {
                return(result);
            }

            var withMissingReferences = references.Union(missingReferences.Select(assemblyMetadata => assemblyMetadata.GetReference()));

            return(Build(generateResult, benchmark, syntaxTree, compilationOptions, withMissingReferences).result);
        }
Exemplo n.º 11
0
 private static IEnumerable <MetadataReference> GetCompileTimeReferences()
 {
     return
         (from library in DependencyContext.Default.CompileLibraries
          from path in library.ResolveReferencePaths()
          select AssemblyMetadata.CreateFromFile(path)
          into assembly
          select assembly.GetReference());
 }
Exemplo n.º 12
0
 /// <summary>
 /// Cache metadata references to avoid loading them between projects
 /// </summary>
 private MetadataReference GetOrCreateMetaDataReference(string file)
 {
     if (!_references.TryGetValue(file, out var meta))
     {
         meta = AssemblyMetadata.CreateFromFile(file);
         _references.Add(file, meta);
     }
     return(meta.GetReference(filePath: file));
 }
Exemplo n.º 13
0
        public static Assembly BuildCode(string filepath)
        {
            lasterror = "";
            var syntaxTree =
                CSharpSyntaxTree.ParseText(File.ReadAllText(filepath, Encoding.UTF8), path: filepath,
                                           encoding: Encoding.UTF8);
            var assemblyName = Path.GetFileNameWithoutExtension(filepath); //Guid.NewGuid().ToString();

            var refs     = AppDomain.CurrentDomain.GetAssemblies();
            var refFiles = refs.Where(a =>
                                      !a.IsDynamic && !a.FullName.Contains("MissionPlanner.Drawing"))
                           .Select(a => a.Location);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                refFiles = refs.Where(a => !a.IsDynamic).Select(a => a.Location);
            }
            var refmeta = refFiles.Select(a =>
            {
                try
                {
                    if (a == "" || !File.Exists(a))
                    {
                        return(null);
                    }
                    return(AssemblyMetadata.CreateFromFile(a).GetReference());
                }
                catch
                {
                    return(null);
                }
            }).Where(a => a != null).ToArray();

            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                new[] { syntaxTree }, refmeta,
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var dllStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var emitResult = compilation.Emit(dllStream, pdbStream);
                    if (!emitResult.Success)
                    {
                        // emitResult.Diagnostics
                        emitResult.Diagnostics.ForEach(a => Console.WriteLine("CodeGenRoslyn " + Path.GetFileName(filepath) + ": {0}", a.ToString()));
                        lasterror = emitResult.Diagnostics.Aggregate("", (a, b) => a + b.ToString() + "\n");
                    }
                    else
                    {
                        return(Assembly.Load(dllStream.ToArray(), pdbStream.ToArray()));
                    }

                    return(null);
                }
        }
Exemplo n.º 14
0
            public override PortableExecutableReference GetReference(string fullPath, MetadataReferenceProperties properties = default(MetadataReferenceProperties))
            {
                AssemblyMetadata metadata;

                if (_cache.TryGetValue(fullPath, out metadata))
                {
                    return(metadata.GetReference(MakeDocumentationProvider()));
                }

                _cache.Add(fullPath, metadata = AssemblyMetadata.CreateFromFile(fullPath));
                return(metadata.GetReference(MakeDocumentationProvider()));
            }
        /// <summary>
        /// Opens the analyzer dll with the metadata reader and builds a map of language -> analyzer type names.
        /// </summary>
        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        /// <exception cref="IOException">IO error reading the metadata.</exception>
        private static ImmutableDictionary <string, ImmutableHashSet <string> > GetAnalyzerTypeNameMap(string fullPath, AttributePredicate attributePredicate)
        {
            using (AssemblyMetadata assembly = AssemblyMetadata.CreateFromFile(fullPath))
            {
                IEnumerable <IGrouping <string, string> > typeNameMap = from module in assembly.GetModules()
                                                                        from typeDefHandle in module.MetadataReader.TypeDefinitions
                                                                        let typeDef = module.MetadataReader.GetTypeDefinition(typeDefHandle)
                                                                                      let typeName = GetFullyQualifiedTypeName(typeDef, module.Module)
                                                                                                     from supportedLanguage in GetSupportedLanguages(typeDef, module.Module, attributePredicate)
                                                                                                     group typeName by supportedLanguage;

                return(typeNameMap.ToImmutableDictionary(g => g.Key, g => g.ToImmutableHashSet()));
            }
        }
Exemplo n.º 16
0
        /*
         * Given the path to a solution file, import will open the solution and it internal projects.
         * For each of the documents inside this sucessive projects, its going to vist it AST representation
         */
        public Repository ImportProject(string solutionPath)
        {
            //The code that causes the error goes here.

            var msWorkspace = MSBuildWorkspace.Create(new Dictionary <string, string>()
            {
                { "Configuration", "Debug" }, { "Platform", "AnyCPU" }, { "CheckForSystemRuntimeDependency", "true" }
            });

            var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;

            ignoreFolder = this.PrivateDirectoryNameFor(solutionPath);

            this.InitializeForImport();
            MetadataReference mscoreLibReference =
                AssemblyMetadata
                .CreateFromFile(typeof(string).Assembly.Location)
                .GetReference();


            var documents = new List <Document>();

            for (int i = 0; i < solution.Projects.Count <Project>(); i++)
            {
                var project = solution.Projects.ElementAt <Project>(i);

                var compilationAsync = project.GetCompilationAsync().Result;
                compilationAsync.AddReferences(
                    MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
                    ).AddReferences(
                    MetadataReference.CreateFromFile(typeof(RequiredAttribute).Assembly.Location));
                var diagnostics = compilationAsync.GetDiagnostics();

                for (int j = 0; j < project.Documents.Count <Document>(); j++)
                {
                    var document = project.Documents.ElementAt <Document>(j);
                    if (document.SupportsSyntaxTree)
                    {
                        this.log("(project " + (i + 1) + " / " + solution.Projects.Count <Project>() + ")");
                        this.logln("(document " + (j + 1) + " / " + project.Documents.Count <Document>() + " " + document.FilePath + ")");
                        var tree          = document.GetSyntaxTreeAsync().Result;
                        var semanticModel = compilationAsync.GetSemanticModel(tree);
                        var model         = semanticModel.GetDiagnostics();
                        this.Import(tree, semanticModel);
                    }
                }
            }
            return(_repository);
        }
Exemplo n.º 17
0
        private static CSharpCompilation Compile(string refPath, string source)
        {
            var dotnetCoreDirectory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
            var compilation         = CSharpCompilation.Create("test",
                                                               syntaxTrees: new[] { SyntaxFactory.ParseSyntaxTree(source) },
                                                               references: new[]
            {
                AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference(),
                MetadataReference.CreateFromFile(Path.Combine(dotnetCoreDirectory, "netstandard.dll")),
                MetadataReference.CreateFromFile(Path.Combine(dotnetCoreDirectory, "System.Runtime.dll")),
                MetadataReference.CreateFromFile(refPath),
            });

            return(compilation);
        }
Exemplo n.º 18
0
        public void CreateFromFile()
        {
            var dir = Temp.CreateDirectory();
            var mm  = dir.CreateFile("MultiModule.dll").WriteAllBytes(TestResources.SymbolsTests.MultiModule.MultiModuleDll).Path;

            dir.CreateFile("mod2.netmodule").WriteAllBytes(TestResources.SymbolsTests.MultiModule.mod2);
            dir.CreateFile("mod3.netmodule").WriteAllBytes(TestResources.SymbolsTests.MultiModule.mod3);

            using (var a = AssemblyMetadata.CreateFromFile(mm))
            {
                Assert.Equal(3, a.GetModules().Length);
                Assert.Equal("MultiModule.dll", a.GetModules()[0].Name);
                Assert.Equal("mod2.netmodule", a.GetModules()[1].Name);
                Assert.Equal("mod3.netmodule", a.GetModules()[2].Name);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Gets or creates metadata for specified file path.
        /// </summary>
        /// <param name="fullPath">Full path to an assembly manifest module file or a standalone module file.</param>
        /// <param name="kind">Metadata kind (assembly or module).</param>
        /// <returns>Metadata for the specified file.</returns>
        /// <exception cref="IOException">Error reading file <paramref name="fullPath"/>. See <see cref="Exception.InnerException"/> for details.</exception>
        public Metadata GetMetadata(string fullPath, MetadataImageKind kind)
        {
            if (NeedsShadowCopy(fullPath))
            {
                return(GetMetadataShadowCopyNoCheck(fullPath, kind).Metadata);
            }

            FileKey key = FileKey.Create(fullPath);

            lock (Guard)
            {
                CacheEntry <Metadata> existing;
                if (_noShadowCopyCache.TryGetValue(key, out existing))
                {
                    return(existing.Public);
                }
            }

            Metadata newMetadata;

            if (kind == MetadataImageKind.Assembly)
            {
                newMetadata = AssemblyMetadata.CreateFromFile(fullPath);
            }
            else
            {
                newMetadata = ModuleMetadata.CreateFromFile(fullPath);
            }

            // the files are locked (memory mapped) now
            key = FileKey.Create(fullPath);

            lock (Guard)
            {
                CacheEntry <Metadata> existing;
                if (_noShadowCopyCache.TryGetValue(key, out existing))
                {
                    newMetadata.Dispose();
                    return(existing.Public);
                }

                Metadata publicMetadata = newMetadata.Copy();
                _noShadowCopyCache.Add(key, new CacheEntry <Metadata>(publicMetadata, newMetadata));
                return(publicMetadata);
            }
        }
Exemplo n.º 20
0
        public BuildResult Build(GenerateResult generateResult, ILogger logger, Benchmark benchmark, IResolver resolver)
        {
            logger.WriteLineInfo($"BuildScript: {generateResult.ArtifactsPaths.BuildScriptFilePath}");

            var syntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(generateResult.ArtifactsPaths.ProgramCodePath));

            var compilationOptions = new CSharpCompilationOptions(
                outputKind: OutputKind.ConsoleApplication,
                optimizationLevel: OptimizationLevel.Release,
                allowUnsafe: true,
                platform: GetPlatform(benchmark.Job.ResolveValue(EnvMode.PlatformCharacteristic, resolver)),
                deterministic: true);

            var references = Generator
                             .GetAllReferences(benchmark)
                             .Select(assembly => AssemblyMetadata.CreateFromFile(assembly.Location))
                             .Concat(FrameworkAssembliesMetadata.Value)
                             .Distinct()
                             .Select(uniqueMetadata => uniqueMetadata.GetReference());

            var compilation = CSharpCompilation
                              .Create(assemblyName: Path.GetFileName(generateResult.ArtifactsPaths.ExecutablePath))
                              .AddSyntaxTrees(syntaxTree)
                              .WithOptions(compilationOptions)
                              .AddReferences(references);

            using (var executable = File.Create(generateResult.ArtifactsPaths.ExecutablePath))
            {
                var emitResult = compilation.Emit(executable);

                if (emitResult.Success)
                {
                    return(BuildResult.Success(generateResult));
                }

                foreach (var diagnostic in emitResult.Diagnostics
                         .Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error))
                {
                    logger.WriteError($"{diagnostic.Id}: {diagnostic.GetMessage()}");
                }

                return(BuildResult.Failure(generateResult));
            }
        }
Exemplo n.º 21
0
        private static ImmutableDictionary <string, ImmutableHashSet <string> > GetAnalyzerTypeNameMap(string fullPath, AttributePredicate attributePredicate)
        {
            using (var assembly = AssemblyMetadata.CreateFromFile(fullPath))
            {
                // This is longer than strictly necessary to avoid thrashing the GC with string allocations
                // in the call to GetFullyQualifiedTypeNames. Specifically, this checks for the presence of
                // supported languages prior to creating the type names.
                var typeNameMap = from module in assembly.GetModules()
                                  from typeDefHandle in module.MetadataReader.TypeDefinitions
                                  let typeDef = module.MetadataReader.GetTypeDefinition(typeDefHandle)
                                                let supportedLanguages = GetSupportedLanguages(typeDef, module.Module, attributePredicate)
                                                                         where supportedLanguages.Any()
                                                                         let typeName = GetFullyQualifiedTypeName(typeDef, module.Module)
                                                                                        from supportedLanguage in supportedLanguages
                                                                                        group typeName by supportedLanguage;

                return(typeNameMap.ToImmutableDictionary(g => g.Key, g => g.ToImmutableHashSet()));
            }
        }
Exemplo n.º 22
0
        // a utility method that creates Roslyn compilation
        // for the passed code.
        // The compilation references the collection of
        // passed "references" arguments plus
        // the mscore library (which is required for the basic
        // functionality).
        public static CSharpCompilation CreateCompilationWithMscorlib
        (
            string assemblyOrModuleName,
            string code,
            CSharpCompilationOptions compilerOptions   = null,
            IEnumerable <MetadataReference> references = null)
        {
            // create the syntax tree
            SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(code, null, "");

            // get the reference to mscore library
            MetadataReference mscoreLibReference =
                AssemblyMetadata
                .CreateFromFile(typeof(string).Assembly.Location)
                .GetReference();

            // create the allReferences collection consisting of
            // mscore reference and all the references passed to the method
            var allReferences =
                new List <MetadataReference>()
            {
                mscoreLibReference
            };

            if (references != null)
            {
                allReferences.AddRange(references);
            }

            // create and return the compilation
            CSharpCompilation compilation = CSharpCompilation.Create
                                            (
                assemblyOrModuleName,
                new[] { syntaxTree },
                options: compilerOptions,
                references: allReferences
                                            );

            return(compilation);
        }
Exemplo n.º 23
0
    private static Compilation Compile(string source)
    {
        var opt  = new CSharpParseOptions(languageVersion: LanguageVersion.Preview, kind: SourceCodeKind.Regular, documentationMode: DocumentationMode.Parse);
        var copt = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

        var dotnetCoreDirectory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
        var compilation         = CSharpCompilation.Create("test",
                                                           syntaxTrees: new[] { SyntaxFactory.ParseSyntaxTree(source, opt) },
                                                           references: new[]
        {
            AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference(),
            MetadataReference.CreateFromFile(Path.Combine(dotnetCoreDirectory, "netstandard.dll")),
            MetadataReference.CreateFromFile(Path.Combine(dotnetCoreDirectory, "System.Runtime.dll")),
        },
                                                           options: copt);

        // apply the source generator
        var driver = new CSharpGeneratorDriver(opt, ImmutableArray.Create <ISourceGenerator>(new MemberAccessGenerator.MemberAccessGenerator()), null, ImmutableArray <AdditionalText> .Empty);

        driver.RunFullGeneration(compilation, out var resultCompilation, out _);

        return(resultCompilation);
    }
Exemplo n.º 24
0
        private static IEnumerable <MetadataReference> GetApplicationReferences()
        {
            var references = new List <MetadataReference>();
            var depsFiles  = AppContext.GetData("APP_CONTEXT_DEPS_FILE") as string;
            var files      = depsFiles?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            var application = files != null && files.Length > 0 ? files[0] : null;

            using (var stream = File.OpenRead(application))
            {
                var dependencyContext = new DependencyContextJsonReader().Read(stream);
                var libraries         = dependencyContext
                                        .GetRuntimeAssemblyNames(dependencyContext.Target.Runtime)
                                        .Select(x => Assembly.Load(x.FullName).Location);

                foreach (var library in libraries)
                {
                    try
                    {
                        var metadata = AssemblyMetadata.CreateFromFile(library);

                        references.Add(metadata.GetReference());
                    }
                    catch (Exception ex)
                        when(ex is NotSupportedException ||
                             ex is ArgumentException ||
                             ex is BadImageFormatException ||
                             ex is IOException)
                        {
                            continue;
                        }
                }
            }

            return(references);
        }
Exemplo n.º 25
0
        private static CSharpCompilation CreateCompilationWithMscorlib(string assemblyName, string code, CSharpCompilationOptions compilerOptions = null, IEnumerable <MetadataReference> references = null)
        {
            SourceText sourceText = SourceText.From(code, Encoding.UTF8);
            SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(sourceText, null, "");

            MetadataReference mscoreLibReference = AssemblyMetadata.CreateFromFile(typeof(string).Assembly.Location).GetReference();

            IEnumerable <MetadataReference> allReferences = new MetadataReference[] { mscoreLibReference };

            if (references != null)
            {
                allReferences = allReferences.Concat(references);
            }

            CSharpCompilation compilation = CSharpCompilation.Create
                                            (
                assemblyName,
                new[] { syntaxTree },
                options: compilerOptions,
                references: allReferences
                                            );

            return(compilation);
        }
Exemplo n.º 26
0
        private BuildResult Build(GenerateResult generateResult, BuildPartition buildPartition, ILogger logger, CancellationToken cancellationToken)
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                text: File.ReadAllText(generateResult.ArtifactsPaths.ProgramCodePath),
                // this version is used to parse the boilerplate code generated by BDN, so th benchmark themselves can use more recent version
                options: new CSharpParseOptions(LanguageVersion.CSharp7_3),
                cancellationToken: cancellationToken);

            var compilationOptions = new CSharpCompilationOptions(
                outputKind: OutputKind.ConsoleApplication,
                optimizationLevel: OptimizationLevel.Release,
                allowUnsafe: true,
                platform: GetPlatform(buildPartition.Platform),
                deterministic: true);

            compilationOptions = compilationOptions.WithIgnoreCorLibraryDuplicatedTypes();

            var references = Generator
                             .GetAllReferences(buildPartition.RepresentativeBenchmarkCase)
                             .Select(assembly => AssemblyMetadata.CreateFromFile(assembly.Location))
                             .Concat(FrameworkAssembliesMetadata.Value)
                             .Distinct()
                             .Select(uniqueMetadata => uniqueMetadata.GetReference())
                             .ToList();

            var(result, missingReferences) = Build(generateResult, syntaxTree, compilationOptions, references, cancellationToken);

            if (result.IsBuildSuccess || !missingReferences.Any())
            {
                return(result);
            }

            var withMissingReferences = references.Union(missingReferences.Select(assemblyMetadata => assemblyMetadata.GetReference()));

            return(Build(generateResult, syntaxTree, compilationOptions, withMissingReferences, cancellationToken).result);
        }
Exemplo n.º 27
0
        public static void Compile(string code, bool isDebug, out Assembly compiledAssembly, out IList <string> errors, out IList <string> warnings)
        {
            var assemblyName = "CodeGenerationAssembly" + Interlocked.Increment(ref _suffix);
            var metadataList =
                SerializerDebugging.CodeSerializerDependentAssemblies.Select(
                    a =>
                    a is string
                    ?AssemblyMetadata.CreateFromFile(a as string)
                        : AssemblyMetadata.CreateFromImage(a as byte[])
                    ).ToArray();

            try
            {
                var compilation =
                    CSharpCompilation.Create(
                        assemblyName,
                        new[] { CSharpSyntaxTree.ParseText(code) },
                        metadataList.Select(m => m.GetReference()),
                        new CSharpCompilationOptions(
                            OutputKind.DynamicallyLinkedLibrary,
                            optimizationLevel: isDebug?OptimizationLevel.Debug: OptimizationLevel.Release,
                            // Suppress CS0436 because gen/*.cs will conflict with testing serializers.
                            specificDiagnosticOptions: new[] { new KeyValuePair <string, ReportDiagnostic>("CS0436", ReportDiagnostic.Suppress) }
                            )
                        );

                var        emitOptions = new EmitOptions(runtimeMetadataVersion: "v4.0.30319");
                EmitResult result;
                if (SerializerDebugging.DumpEnabled)
                {
                    var assemblyPath = Path.Combine(SerializerDebugging.DumpDirectory, assemblyName + ".dll");

                    using (var fileStream = File.OpenWrite(assemblyPath))
                    {
                        result = compilation.Emit(fileStream, options: emitOptions);
                        fileStream.Flush();
                    }

                    if (result.Success)
                    {
                        compiledAssembly = Assembly.LoadFrom(assemblyPath);
                        SerializerDebugging.AddCompiledCodeAssembly(assemblyPath);
                    }
                    else
                    {
                        compiledAssembly = null;
                    }
                }
                else
                {
                    using (var buffer = new MemoryStream())
                    {
                        result = compilation.Emit(buffer, options: emitOptions);
                        if (result.Success)
                        {
                            var image = buffer.ToArray();
                            compiledAssembly = Assembly.Load(image);
                            SerializerDebugging.AddCompiledCodeAssembly(assemblyName, image);
                        }
                        else
                        {
                            compiledAssembly = null;
                        }
                    }
                }

                errors   = BuildCompilationError(result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error));
                warnings = BuildCompilationError(result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning));
            }
            finally
            {
                foreach (var metadata in metadataList)
                {
                    metadata.Dispose();
                }
            }
        }
Exemplo n.º 28
0
        // TODO: implement resources https://stackoverflow.com/a/26853131/2350244
        private static VB.VisualBasicCompilation CreateVisualBasicCompilation(
            IEnumerable <string> sourceFiles,
            string assemblyName,
            IEnumerable <string> references,
            IEnumerable <KeyValuePair <string, object> > defines,
            bool debug,
            out List <string> messages
            )
        {
            // parse the source files
            var parseOpts =
                VB.VisualBasicParseOptions.Default
                .WithLanguageVersion(MaxVBLanguageVersion)
                .WithPreprocessorSymbols(defines);

            // and build syntax tree
            List <SyntaxTree> syntaxTrees = new List <SyntaxTree>();

            foreach (var sourceFile in sourceFiles)
            {
                syntaxTrees.Add(
                    VB.VisualBasicSyntaxTree.ParseText(
                        text: File.ReadAllText(sourceFile),
                        options: parseOpts,
                        path: sourceFile,
                        encoding: Encoding.UTF8
                        )
                    );
            }

            // collect references
            var refs = new List <string>();

            // add mscorelib
            refs.Add(typeof(object).Assembly.Location);
            foreach (var refFile in references)
            {
                refs.Add(refFile);
            }

            messages = new List <string>();
            messages.Add($"Define: {string.Join(";", defines)}");
            var mdataRefs = new List <MetadataReference>();

            foreach (var refPath in refs)
            {
                messages.Add($"Reference: {refPath}");
                mdataRefs.Add(
                    AssemblyMetadata.CreateFromFile(refPath).GetReference()
                    );
            }

            // compile options
            var compileOpts =
                new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                .WithOverflowChecks(true)
                .WithPlatform(Platform.X64)
                .WithOptimizationLevel(debug ? OptimizationLevel.Debug : OptimizationLevel.Release);

            // create compilation job
            return(VB.VisualBasicCompilation.Create(
                       assemblyName: assemblyName,
                       syntaxTrees: syntaxTrees,
                       references: mdataRefs,
                       options: compileOpts
                       ));
        }
Exemplo n.º 29
0
        // TODO: implement resources https://stackoverflow.com/a/26853131/2350244
        private static CSharpCompilation CreateCSharpCompilation(
            IEnumerable <string> sourceFiles,
            string assemblyName,
            IEnumerable <string> references,
            IEnumerable <string> defines,
            out List <string> messages
            )
        {
            // parse the source files
            var parseOpts =
                CSharpParseOptions.Default
                .WithLanguageVersion(MaxLanguageVersion)
                .WithPreprocessorSymbols(defines);

            // and build syntax tree
            List <SyntaxTree> syntaxTree = new List <SyntaxTree>();

            foreach (var sourceFile in sourceFiles)
            {
                syntaxTree.Add(
                    CSharpSyntaxTree.ParseText(
                        text: File.ReadAllText(sourceFile),
                        options: parseOpts,
                        path: sourceFile
                        )
                    );
            }

            // collect references
            var refs = new List <string>();

            // add mscorelib
            refs.Add(typeof(object).Assembly.Location);
            foreach (var refFile in references)
            {
                refs.Add(refFile);
            }

            messages = new List <string>();
            var mdataRefs = new List <MetadataReference>();

            foreach (var refPath in refs)
            {
                messages.Add($"Reference: {refPath}");
                mdataRefs.Add(
                    AssemblyMetadata.CreateFromFile(refPath).GetReference()
                    );
            }

            // compile options
            var compileOpts =
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                .WithOverflowChecks(true)
                .WithPlatform(Platform.X64)
                .WithOptimizationLevel(OptimizationLevel.Release);

            // create compilation job
            return(CSharpCompilation.Create(
                       assemblyName,
                       syntaxTree,
                       mdataRefs,
                       compileOpts
                       ));
        }
Exemplo n.º 30
0
        // 根据生成的类,动态编译把json转成protobuf
        private static void ExportExcelProtobuf(ConfigType configType)
        {
            string            classPath   = GetClassDir(configType);
            List <SyntaxTree> syntaxTrees = new List <SyntaxTree>();
            List <string>     protoNames  = new List <string>();

            foreach (string classFile in Directory.GetFiles(classPath, "*.cs"))
            {
                protoNames.Add(Path.GetFileNameWithoutExtension(classFile));
                syntaxTrees.Add(CSharpSyntaxTree.ParseText(File.ReadAllText(classFile)));
            }

            List <PortableExecutableReference> references = new List <PortableExecutableReference>();

            string assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);

            references.Add(AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(typeof(ProtoMemberAttribute).Assembly.Location).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(typeof(BsonDefaultValueAttribute).Assembly.Location).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(typeof(IConfig).Assembly.Location).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(typeof(Attribute).Assembly.Location).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "mscorlib.dll")).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "System.dll")).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "netstandard.dll")).GetReference());
            references.Add(AssemblyMetadata.CreateFromFile(typeof(ISupportInitialize).Assembly.Location).GetReference());


            CSharpCompilation compilation = CSharpCompilation.Create(
                null,
                syntaxTrees.ToArray(),
                references.ToArray(),
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using MemoryStream memSteam = new MemoryStream();

            EmitResult emitResult = compilation.Emit(memSteam);

            if (!emitResult.Success)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (Diagnostic t in emitResult.Diagnostics)
                {
                    stringBuilder.AppendLine(t.GetMessage());
                }
                throw new Exception($"动态编译失败:\n{stringBuilder}");
            }

            memSteam.Seek(0, SeekOrigin.Begin);

            Assembly ass = Assembly.Load(memSteam.ToArray());

            string dir = GetProtoDir(configType);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            foreach (string protoName in protoNames)
            {
                Type type    = ass.GetType($"ET.{protoName}Category");
                Type subType = ass.GetType($"ET.{protoName}");
                Serializer.NonGeneric.PrepareSerializer(type);
                Serializer.NonGeneric.PrepareSerializer(subType);


                string json        = File.ReadAllText(Path.Combine(string.Format(jsonDir, configType), $"{protoName}.txt"));
                object deserialize = BsonSerializer.Deserialize(json, type);

                string path = Path.Combine(dir, $"{protoName}Category.bytes");

                using FileStream file = File.Create(path);
                Serializer.Serialize(file, deserialize);
            }
        }