diff --git a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataNamespaceSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataNamespaceSymbol.cs index 3c455a260..805f7e2b8 100644 --- a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataNamespaceSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataNamespaceSymbol.cs @@ -57,7 +57,7 @@ private ImmutableArray BuildMembers() var symbol = MetadataSymbol.ToSymbol(this, typeDef); result.Add(symbol); // Add additional symbols - result.AddRange(GetAdditionalSymbols(symbol)); + result.AddRange(symbol.GetAdditionalSymbols()); } // Done diff --git a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataStaticClassSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataStaticClassSymbol.cs index c8e44672b..066379ad9 100644 --- a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataStaticClassSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataStaticClassSymbol.cs @@ -67,7 +67,7 @@ private ImmutableArray BuildMembers() var symbol = MetadataSymbol.ToSymbol(this, typeDef); result.Add(symbol); // Add additional symbols - result.AddRange(GetAdditionalSymbols(symbol)); + result.AddRange(symbol.GetAdditionalSymbols()); } // Methods diff --git a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataTypeSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataTypeSymbol.cs index c3cd96f10..022b03bf3 100644 --- a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataTypeSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataTypeSymbol.cs @@ -156,7 +156,7 @@ private ImmutableArray BuildMembers() var symbol = MetadataSymbol.ToSymbol(this, typeDef); result.Add(symbol); // Add additional symbols - result.AddRange(GetAdditionalSymbols(symbol)); + result.AddRange(symbol.GetAdditionalSymbols()); } // Methods diff --git a/src/Draco.Compiler/Internal/Symbols/Script/ScriptModuleSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Script/ScriptModuleSymbol.cs index 89b1a65f8..612e7a283 100644 --- a/src/Draco.Compiler/Internal/Symbols/Script/ScriptModuleSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Script/ScriptModuleSymbol.cs @@ -89,7 +89,7 @@ private ImmutableArray BindMembers(IBinderProvider binderProvider) var earlierMember = result.FirstOrDefault(s => s.Name == member.Name); result.Add(member); - result.AddRange(GetAdditionalSymbols(member)); + result.AddRange(member.GetAdditionalSymbols()); // We check for illegal shadowing if (earlierMember is null) continue; diff --git a/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs index 337a54694..a35e2264a 100644 --- a/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs @@ -77,7 +77,7 @@ private ImmutableArray BindMembers(IBinderProvider binderProvider) { var earlierMember = result.FirstOrDefault(s => s.Name == member.Name); result.Add(member); - result.AddRange(GetAdditionalSymbols(member)); + result.AddRange(member.GetAdditionalSymbols()); // We check for illegal shadowing if (earlierMember is null) continue; diff --git a/src/Draco.Compiler/Internal/Symbols/Symbol.cs b/src/Draco.Compiler/Internal/Symbols/Symbol.cs index 05b0589a1..0819db402 100644 --- a/src/Draco.Compiler/Internal/Symbols/Symbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Symbol.cs @@ -333,28 +333,10 @@ private protected string GenericsToString() _ => throw new InvalidOperationException($"illegal visibility modifier token {kind}"), }; - // TODO: We could have this as a base member for symbols /// - /// Retrieves additional symbols for the given symbol that should live in the same scope as the symbol itself. + /// Retrieves additional symbols for this symbol that should live in the same scope as this 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) - { - 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 SyntaxAutoPropertySymbol 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; - } - } + /// The additional symbols for this symbol. + protected internal virtual IEnumerable GetAdditionalSymbols() => []; } diff --git a/src/Draco.Compiler/Internal/Symbols/Syntax/SyntaxAutoPropertySymbol.cs b/src/Draco.Compiler/Internal/Symbols/Syntax/SyntaxAutoPropertySymbol.cs index 830deefc3..d940920e7 100644 --- a/src/Draco.Compiler/Internal/Symbols/Syntax/SyntaxAutoPropertySymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Syntax/SyntaxAutoPropertySymbol.cs @@ -8,6 +8,7 @@ using Draco.Compiler.Internal.Symbols.Source; using Draco.Compiler.Internal.Utilities; using Draco.Compiler.Internal.Symbols.Synthetized.AutoProperty; +using System.Collections.Generic; namespace Draco.Compiler.Internal.Symbols.Syntax; @@ -63,4 +64,12 @@ private SymbolDocumentation BuildDocumentation() => private FunctionSymbol BuildGetter() => new AutoPropertyGetterSymbol(this.ContainingSymbol, this); private FunctionSymbol? BuildSetter() => new AutoPropertySetterSymbol(this.ContainingSymbol, this); private FieldSymbol BuildBackingField() => new AutoPropertyBackingFieldSymbol(this.ContainingSymbol, this); + + protected internal override sealed IEnumerable GetAdditionalSymbols() + { + // For auto-properties we provide the backing field and the accessors in the same scope + if (this.Getter is not null) yield return this.Getter; + if (this.Setter is not null) yield return this.Setter; + yield return this.BackingField; + } } diff --git a/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs b/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs index b0b782bac..b23fdd510 100644 --- a/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs @@ -4,6 +4,7 @@ using System.Linq; using Draco.Compiler.Api.Semantics; using Draco.Compiler.Internal.Symbols.Generic; +using Draco.Compiler.Internal.Symbols.Synthetized; using Draco.Compiler.Internal.Utilities; namespace Draco.Compiler.Internal.Symbols; @@ -213,4 +214,11 @@ public override TypeSymbol GenericInstantiate(Symbol? containingSymbol, GenericC public override TResult Accept(SymbolVisitor visitor) => visitor.VisitType(this); public override abstract string ToString(); + + protected internal override sealed IEnumerable GetAdditionalSymbols() + { + if (this.IsAbstract) yield break; + // For non-abstract types we provide constructor functions + foreach (var ctor in this.Constructors) yield return new ConstructorFunctionSymbol(ctor); + } }