Skip to content

Commit

Permalink
Unification of ParseOptions usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermannhubert committed Apr 1, 2024
1 parent 09748ae commit 490f95c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
8 changes: 5 additions & 3 deletions CSharpRepl.Services/Disassembly/Disassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ internal class Disassembler
{
private readonly AssemblyReferenceService referenceService;
private readonly (string name, CompileDelegate compile)[] compilers;
private readonly CSharpParseOptions parseOptions;
private readonly CSharpCompilationOptions compilationOptions;

public Disassembler(CSharpCompilationOptions compilationOptions, AssemblyReferenceService referenceService, ScriptRunner scriptRunner)
public Disassembler(CSharpParseOptions parseOptions, CSharpCompilationOptions compilationOptions, AssemblyReferenceService referenceService, ScriptRunner scriptRunner)
{
this.parseOptions = parseOptions.WithKind(SourceCodeKind.Regular);
this.compilationOptions = compilationOptions;
this.referenceService = referenceService;

Expand All @@ -45,7 +47,7 @@ public Disassembler(CSharpCompilationOptions compilationOptions, AssemblyReferen
compile: (code, optimizationLevel) => Compile(code, optimizationLevel, OutputKind.DynamicallyLinkedLibrary)),
// Compiling as a script will work for most other cases, but it's quite verbose so we use it as a last resort.
(name: "Scripting session (will be overly verbose)",
compile: (code, optimizationLevel) => scriptRunner.CompileTransient(code, optimizationLevel))
compile: scriptRunner.CompileTransient)
];
}

Expand Down Expand Up @@ -151,7 +153,7 @@ static PlainTextOutput DisassembleAll(PEFile file, PlainTextOutput ilCodeOutput)

private Compilation Compile(string code, OptimizationLevel optimizationLevel, OutputKind outputKind)
{
var ast = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Latest));
var ast = CSharpSyntaxTree.ParseText(code, parseOptions);
var compilation = CSharpCompilation.Create("CompilationForDisassembly",
[ast],
referenceService.LoadedReferenceAssemblies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ internal sealed class AssemblyReferenceService
private readonly HashSet<string> implementationAssemblyPaths;
private readonly HashSet<string> sharedFrameworkImplementationAssemblyPaths;
private readonly HashSet<UsingDirectiveSyntax> usings;
private readonly CSharpParseOptions parseOptions;

public IReadOnlySet<string> ImplementationAssemblyPaths => implementationAssemblyPaths;
public IReadOnlySet<MetadataReference> LoadedImplementationAssemblies => loadedImplementationAssemblies;
public IReadOnlySet<MetadataReference> LoadedReferenceAssemblies => loadedReferenceAssemblies;
public IReadOnlyCollection<UsingDirectiveSyntax> Usings => usings;

public AssemblyReferenceService(Configuration config, ITraceLogger logger)
public AssemblyReferenceService(Configuration config, CSharpParseOptions parseOptions, ITraceLogger logger)
{
this.parseOptions = parseOptions;
this.dotnetInstallationLocator = new DotNetInstallationLocator(logger);
this.referenceAssemblyPaths = [];
this.implementationAssemblyPaths = [];
Expand Down Expand Up @@ -201,7 +203,7 @@ public void LoadSharedFrameworkConfiguration(SharedFramework[] sharedFrameworks)
}

internal IReadOnlyCollection<UsingDirectiveSyntax> GetUsings(string code) =>
CSharpSyntaxTree.ParseText(code)
CSharpSyntaxTree.ParseText(code, parseOptions)
.GetRoot()
.DescendantNodes()
.OfType<UsingDirectiveSyntax>()
Expand All @@ -210,7 +212,7 @@ internal IReadOnlyCollection<UsingDirectiveSyntax> GetUsings(string code) =>
internal void TrackUsings(IReadOnlyCollection<UsingDirectiveSyntax> usingsToAdd) =>
usings.UnionWith(usingsToAdd);

private IReadOnlyCollection<MetadataReference> CreateDefaultReferences(string assemblyPath, IReadOnlyCollection<string> assemblies)
private List<PortableExecutableReference> CreateDefaultReferences(string assemblyPath, IReadOnlyCollection<string> assemblies)
{
return assemblies
.AsParallel()
Expand Down
7 changes: 4 additions & 3 deletions CSharpRepl.Services/Roslyn/RoslynServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public sealed partial class RoslynServices
private readonly SemaphoreSlim semaphore = new(1);
private readonly IPromptCallbacks defaultPromptCallbacks = new PromptCallbacks();
private readonly ThreadLocal<OverloadItemGenerator> overloadItemGenerator;
private readonly CSharpParseOptions parseOptions = CSharpParseOptions.Default.WithKind(SourceCodeKind.Script).WithLanguageVersion(LanguageVersion.Latest);
private ScriptRunner? scriptRunner;
private WorkspaceManager? workspaceManager;
private Disassembler? disassembler;
Expand Down Expand Up @@ -78,7 +79,7 @@ public RoslynServices(IConsoleEx console, Configuration config, ITraceLogger log
{
logger.Log("Starting background initialization");

this.referenceService = new AssemblyReferenceService(config, logger);
this.referenceService = new AssemblyReferenceService(config, parseOptions, logger);

this.compilationOptions = new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
Expand All @@ -91,9 +92,9 @@ public RoslynServices(IConsoleEx console, Configuration config, ITraceLogger log
// is updated alongside. The workspace is a datamodel used in "editor services" like
// syntax highlighting, autocompletion, and roslyn symbol queries.
this.workspaceManager = new WorkspaceManager(compilationOptions, referenceService, logger);
this.scriptRunner = new ScriptRunner(workspaceManager, compilationOptions, referenceService, console, config);
this.scriptRunner = new ScriptRunner(workspaceManager, parseOptions, compilationOptions, referenceService, console, config);

this.disassembler = new Disassembler(compilationOptions, referenceService, scriptRunner);
this.disassembler = new Disassembler(parseOptions, compilationOptions, referenceService, scriptRunner);
this.prettyPrinter = new PrettyPrinter(console, highlighter, config);
this.symbolExplorer = new SymbolExplorer(referenceService, scriptRunner);
this.autocompleteService = new AutoCompleteService(highlighter, cache, config);
Expand Down
9 changes: 5 additions & 4 deletions CSharpRepl.Services/Roslyn/Scripting/ScriptRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ internal sealed class ScriptRunner
private readonly CompositeAlternativeReferenceResolver alternativeReferenceResolver;
private readonly MetadataReferenceResolver metadataResolver;
private readonly WorkspaceManager workspaceManager;
private readonly CSharpParseOptions parseOptions;
private readonly AssemblyReferenceService referenceAssemblyService;
private ScriptOptions scriptOptions;
private ScriptState<object>? state;

public ScriptRunner(
WorkspaceManager workspaceManager,
CSharpParseOptions parseOptions,
CSharpCompilationOptions compilationOptions,
AssemblyReferenceService referenceAssemblyService,
IConsoleEx console,
Configuration configuration)
{
this.console = console;
this.workspaceManager = workspaceManager;
this.parseOptions = parseOptions;
this.referenceAssemblyService = referenceAssemblyService;
this.assemblyLoader = new InteractiveAssemblyLoader(new MetadataShadowCopyProvider());

Expand Down Expand Up @@ -110,7 +113,7 @@ public Compilation CompileTransient(string code, OptimizationLevel optimizationL
{
return CSharpCompilation.CreateScriptCompilation(
"CompilationTransient",
CSharpSyntaxTree.ParseText(code, CSharpParseOptions.Default.WithKind(SourceCodeKind.Script).WithLanguageVersion(LanguageVersion.Latest)),
CSharpSyntaxTree.ParseText(code, parseOptions),
scriptOptions.MetadataReferences,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: scriptOptions.Imports, optimizationLevel: optimizationLevel, allowUnsafe: scriptOptions.AllowUnsafe, metadataReferenceResolver: metadataResolver),
previousScriptCompilation: state?.Script.GetCompilation() is CSharpCompilation previous ? previous : null,
Expand Down Expand Up @@ -165,7 +168,5 @@ await tree.GetRootAsync(cancellationToken).ConfigureAwait(false) is CompilationU
}

private ScriptGlobals CreateGlobalsObject(string[]? args)
{
return new ScriptGlobals(console, args ?? []);
}
=> new(console, args ?? []);
}

0 comments on commit 490f95c

Please sign in to comment.