From 35e0a51666a8a1682260740a559bf13e34c4b80d Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Sat, 2 Nov 2024 18:28:22 +0100 Subject: [PATCH] Simplification --- .../Symbols/Source/SourceModuleSymbol.cs | 41 +++++-------------- src/Draco.Compiler/Internal/Symbols/Symbol.cs | 22 +++++++--- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs index c5302f34a..664fce64c 100644 --- a/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs @@ -6,6 +6,7 @@ using System.Threading; using Draco.Compiler.Api; using Draco.Compiler.Api.Diagnostics; +using Draco.Compiler.Api.Semantics; using Draco.Compiler.Api.Syntax; using Draco.Compiler.Internal.Binding; using Draco.Compiler.Internal.Declarations; @@ -72,16 +73,11 @@ private ImmutableArray BindMembers(IBinderProvider binderProvider) var result = ImmutableArray.CreateBuilder(); // A declaration can yield multiple members, like an auto-property a getter and setter - foreach (var member in this.declaration.Children.SelectMany(this.BuildMember)) + foreach (var member in this.declaration.Children.Select(this.BuildMember)) { var earlierMember = result.FirstOrDefault(s => s.Name == member.Name); result.Add(member); - - if (member is TypeSymbol typeSymbol) - { - result.AddRange(GetAdditionalSymbols(typeSymbol)); - } - + result.AddRange(GetAdditionalSymbols(member)); // We check for illegal shadowing if (earlierMember is null) continue; @@ -106,32 +102,15 @@ private ImmutableArray BindMembers(IBinderProvider binderProvider) return result.ToImmutable(); } - private IEnumerable BuildMember(Declaration declaration) => declaration switch + private Symbol BuildMember(Declaration declaration) => declaration switch { - FunctionDeclaration f => [this.BuildFunction(f)], - MergedModuleDeclaration m => [this.BuildModule(m)], - GlobalDeclaration g when g.Syntax.FieldModifier is not null => [this.BuildGlobalField(g)], - GlobalDeclaration g when g.Syntax.FieldModifier is null => this.BuildGlobalProperty(g), - ClassDeclaration c => [this.BuildClass(c)], + FunctionDeclaration f => new SourceFunctionSymbol(this, f), + MergedModuleDeclaration m => new SourceModuleSymbol(this.DeclaringCompilation, this, m), + GlobalDeclaration g when g.Syntax.FieldModifier is not null => new SourceFieldSymbol(this, g), + GlobalDeclaration g when g.Syntax.FieldModifier is null => new SourceAutoPropertySymbol(this, g), + ClassDeclaration c => new SourceClassSymbol(this, c), _ => throw new ArgumentOutOfRangeException(nameof(declaration)), }; - private SourceFunctionSymbol BuildFunction(FunctionDeclaration declaration) => new(this, declaration); - private SourceFieldSymbol BuildGlobalField(GlobalDeclaration declaration) => new(this, declaration); - - private IEnumerable BuildGlobalProperty(GlobalDeclaration declaration) - { - // Auto-property, need to add getter, setter and backing field - var prop = new SourceAutoPropertySymbol(this, declaration); - yield return prop; - if (prop.Getter is not null) yield return prop.Getter; - if (prop.Setter is not null) yield return prop.Setter; - yield return prop.BackingField; - } - - private SourceModuleSymbol BuildModule(MergedModuleDeclaration declaration) => new(this.DeclaringCompilation, this, declaration); - private SourceClassSymbol BuildClass(ClassDeclaration declaration) => new(this, declaration); - - private SymbolDocumentation BuildDocumentation() => - MarkdownDocumentationExtractor.Extract(this); + private SymbolDocumentation BuildDocumentation() => MarkdownDocumentationExtractor.Extract(this); } diff --git a/src/Draco.Compiler/Internal/Symbols/Symbol.cs b/src/Draco.Compiler/Internal/Symbols/Symbol.cs index 2e460c13f..c4d8453be 100644 --- a/src/Draco.Compiler/Internal/Symbols/Symbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Symbol.cs @@ -8,6 +8,7 @@ using Draco.Compiler.Internal.Documentation; using Draco.Compiler.Internal.Symbols.Generic; using Draco.Compiler.Internal.Symbols.Metadata; +using Draco.Compiler.Internal.Symbols.Source; using Draco.Compiler.Internal.Symbols.Synthetized; using Draco.Compiler.Internal.Utilities; @@ -332,15 +333,26 @@ private protected string GenericsToString() }; /// - /// Retrieves additional symbols for the given . + /// Retrieves additional symbols for the given symbol that should live in the same scope as the symbol itself. + /// This returns the constructor functions for types for example. /// /// The symbol to get additional symbols for. /// The additional symbols for the given . public static IEnumerable GetAdditionalSymbols(Symbol symbol) { - if (symbol is not TypeSymbol typeSymbol) return []; - if (typeSymbol.IsAbstract) return []; - // For other types we provide constructor functions - return typeSymbol.Constructors.Select(ctor => new ConstructorFunctionSymbol(ctor)); + switch (symbol) + { + case TypeSymbol typeSymbol: + if (typeSymbol.IsAbstract) yield break; + // For non-abstract types we provide constructor functions + foreach (var ctor in typeSymbol.Constructors) yield return new ConstructorFunctionSymbol(ctor); + break; + case SourceAutoPropertySymbol autoProp: + // For auto-properties we provide the backing field and the accessors in the same scope + if (autoProp.Getter is not null) yield return autoProp.Getter; + if (autoProp.Setter is not null) yield return autoProp.Setter; + yield return autoProp.BackingField; + break; + } } }