Skip to content

Commit

Permalink
Simplified (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 authored Aug 20, 2024
1 parent 698fb6e commit 4bef7ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
7 changes: 6 additions & 1 deletion src/Draco.Compiler/Api/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public readonly record struct EmitResult(
/// </summary>
public sealed class Compilation : IBinderProvider
{
/// <summary>
/// An empty compilation.
/// </summary>
public static Compilation Empty { get; } = Create(syntaxTrees: []);

/// <summary>
/// Constructs a <see cref="Compilation"/>.
/// </summary>
Expand Down Expand Up @@ -329,7 +334,7 @@ internal Binder GetBinder(Symbol symbol)
Binder IBinderProvider.GetBinder(SyntaxNode syntax) => this.GetBinder(syntax);
Binder IBinderProvider.GetBinder(Symbol symbol) => this.GetBinder(symbol);

private DeclarationTable BuildDeclarationTable() => DeclarationTable.From(this.SyntaxTrees, this);
private DeclarationTable BuildDeclarationTable() => new(this);
private ModuleSymbol BuildSourceModule() => new SourceModuleSymbol(this, null, this.DeclarationTable.MergedRoot);
private ImmutableDictionary<MetadataReference, MetadataAssemblySymbol> BuildMetadataAssemblies() => this.MetadataReferences
.ToImmutableDictionary(
Expand Down
45 changes: 7 additions & 38 deletions src/Draco.Compiler/Internal/Declarations/DeclarationTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ namespace Draco.Compiler.Internal.Declarations;
/// <summary>
/// Keeps track of all declarations from the parse trees.
/// </summary>
internal sealed class DeclarationTable
internal sealed class DeclarationTable(Compilation compilation)
{
/// <summary>
/// Constructs a new declaration table from the given syntax trees.
/// </summary>
/// <param name="syntaxTrees">The syntax trees to construct the declarations from.</param>
/// <param name="compilation">The compilation using this declaration table.</param>
/// <returns>The declaration table containing <paramref name="syntaxTrees"/>.</returns>
public static DeclarationTable From(ImmutableArray<SyntaxTree> syntaxTrees, Compilation compilation) => new(syntaxTrees, compilation);

/// <summary>
/// An empty declaration table.
/// </summary>
public static DeclarationTable Empty { get; } = new([], Compilation.Create([]));
public static DeclarationTable Empty { get; } = new(Compilation.Empty);

/// <summary>
/// The merged root module.
Expand All @@ -38,23 +30,16 @@ internal sealed class DeclarationTable
/// <summary>
/// The root path of this declaration table.
/// </summary>
public string RootPath => this.compilation.RootModulePath;
public string RootPath => compilation.RootModulePath;

private readonly Compilation compilation;
private readonly ImmutableArray<SyntaxTree> syntaxTrees;

private DeclarationTable(ImmutableArray<SyntaxTree> syntaxTrees, Compilation compilation)
{
this.syntaxTrees = syntaxTrees;
this.compilation = compilation;
}
internal ImmutableArray<SyntaxTree> SyntaxTrees => compilation.SyntaxTrees;

private MergedModuleDeclaration BuildMergedRoot()
{
// If we don't have root path, we put all file into top level module
if (string.IsNullOrEmpty(this.RootPath))
{
var singleModules = this.syntaxTrees
var singleModules = this.SyntaxTrees
.Select(s => new SingleModuleDeclaration(
name: string.Empty,
path: SplitPath.Empty,
Expand All @@ -71,7 +56,7 @@ private MergedModuleDeclaration BuildMergedRoot()
var pathBeforeRoot = rootPath.Slice(..^1);

var modules = ImmutableArray.CreateBuilder<SingleModuleDeclaration>();
foreach (var tree in this.syntaxTrees)
foreach (var tree in this.SyntaxTrees)
{
var path = SplitPath.FromFilePath(tree.SourceText.Path?.LocalPath ?? string.Empty);

Expand All @@ -88,7 +73,7 @@ private MergedModuleDeclaration BuildMergedRoot()
// Add error if path doesn't start with root path
if (!path.StartsWith(rootPath))
{
this.compilation.GlobalDiagnosticBag.Add(
compilation.GlobalDiagnosticBag.Add(
Diagnostic.Create(
template: SymbolResolutionErrors.FilePathOutsideOfRootPath,
location: null,
Expand Down Expand Up @@ -117,22 +102,6 @@ private MergedModuleDeclaration BuildMergedRoot()
declarations: modules.ToImmutable());
}

/// <summary>
/// Adds a syntax-tree to this table.
/// </summary>
/// <param name="syntaxTree">The syntax tree to add.</param>
/// <returns>The new table, containing declarations in <paramref name="syntaxTree"/>.</returns>
public DeclarationTable AddCompilationUnit(SyntaxTree syntaxTree) =>
new(this.syntaxTrees.Add(syntaxTree), this.compilation);

/// <summary>
/// Adds a syntax-trees to this table.
/// </summary>
/// <param name="syntaxTrees">The syntax trees to add.</param>
/// <returns>The new table, containing <paramref name="syntaxTrees"/>.</returns>
public DeclarationTable AddCompilationUnits(IEnumerable<SyntaxTree> syntaxTrees) =>
new(this.syntaxTrees.AddRange(syntaxTrees), this.compilation);

/// <summary>
/// Retrieves the DOT graph of the declaration tree for debugging purposes.
/// </summary>
Expand Down

0 comments on commit 4bef7ef

Please sign in to comment.