diff --git a/src/Draco.Compiler/Api/Compilation.cs b/src/Draco.Compiler/Api/Compilation.cs index 0fef1dae7..77231445d 100644 --- a/src/Draco.Compiler/Api/Compilation.cs +++ b/src/Draco.Compiler/Api/Compilation.cs @@ -37,6 +37,11 @@ public readonly record struct EmitResult( /// public sealed class Compilation : IBinderProvider { + /// + /// An empty compilation. + /// + public static Compilation Empty { get; } = Create(syntaxTrees: []); + /// /// Constructs a . /// @@ -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 BuildMetadataAssemblies() => this.MetadataReferences .ToImmutableDictionary( diff --git a/src/Draco.Compiler/Internal/Declarations/DeclarationTable.cs b/src/Draco.Compiler/Internal/Declarations/DeclarationTable.cs index 7eb81bc2b..2c75f17fe 100644 --- a/src/Draco.Compiler/Internal/Declarations/DeclarationTable.cs +++ b/src/Draco.Compiler/Internal/Declarations/DeclarationTable.cs @@ -13,20 +13,12 @@ namespace Draco.Compiler.Internal.Declarations; /// /// Keeps track of all declarations from the parse trees. /// -internal sealed class DeclarationTable +internal sealed class DeclarationTable(Compilation compilation) { - /// - /// Constructs a new declaration table from the given syntax trees. - /// - /// The syntax trees to construct the declarations from. - /// The compilation using this declaration table. - /// The declaration table containing . - public static DeclarationTable From(ImmutableArray syntaxTrees, Compilation compilation) => new(syntaxTrees, compilation); - /// /// An empty declaration table. /// - public static DeclarationTable Empty { get; } = new([], Compilation.Create([])); + public static DeclarationTable Empty { get; } = new(Compilation.Empty); /// /// The merged root module. @@ -38,23 +30,16 @@ internal sealed class DeclarationTable /// /// The root path of this declaration table. /// - public string RootPath => this.compilation.RootModulePath; + public string RootPath => compilation.RootModulePath; - private readonly Compilation compilation; - private readonly ImmutableArray syntaxTrees; - - private DeclarationTable(ImmutableArray syntaxTrees, Compilation compilation) - { - this.syntaxTrees = syntaxTrees; - this.compilation = compilation; - } + internal ImmutableArray 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, @@ -71,7 +56,7 @@ private MergedModuleDeclaration BuildMergedRoot() var pathBeforeRoot = rootPath.Slice(..^1); var modules = ImmutableArray.CreateBuilder(); - foreach (var tree in this.syntaxTrees) + foreach (var tree in this.SyntaxTrees) { var path = SplitPath.FromFilePath(tree.SourceText.Path?.LocalPath ?? string.Empty); @@ -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, @@ -117,22 +102,6 @@ private MergedModuleDeclaration BuildMergedRoot() declarations: modules.ToImmutable()); } - /// - /// Adds a syntax-tree to this table. - /// - /// The syntax tree to add. - /// The new table, containing declarations in . - public DeclarationTable AddCompilationUnit(SyntaxTree syntaxTree) => - new(this.syntaxTrees.Add(syntaxTree), this.compilation); - - /// - /// Adds a syntax-trees to this table. - /// - /// The syntax trees to add. - /// The new table, containing . - public DeclarationTable AddCompilationUnits(IEnumerable syntaxTrees) => - new(this.syntaxTrees.AddRange(syntaxTrees), this.compilation); - /// /// Retrieves the DOT graph of the declaration tree for debugging purposes. ///