Skip to content

Commit

Permalink
Update SourceModuleSymbol.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 26, 2024
1 parent d0f7360 commit 760a0d1
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ private ImmutableArray<Symbol> BindMembers(IBinderProvider binderProvider)
{
var result = ImmutableArray.CreateBuilder<Symbol>();

// Syntax-declaration
foreach (var declaration in this.declaration.Children)
// A declaration can yield multiple members, like an auto-property a getter and setter
foreach (var member in this.declaration.Children.SelectMany(this.BuildMember))
{
var member = this.BuildMember(declaration);
var earlierMember = result.FirstOrDefault(s => s.Name == member.Name);
result.Add(member);

Expand All @@ -96,18 +95,28 @@ private ImmutableArray<Symbol> BindMembers(IBinderProvider binderProvider)
return result.ToImmutable();
}

private Symbol BuildMember(Declaration declaration) => declaration switch
private IEnumerable<Symbol> BuildMember(Declaration declaration) => declaration switch
{
FunctionDeclaration f => this.BuildFunction(f),
GlobalDeclaration g => this.BuildGlobal(g),
MergedModuleDeclaration m => this.BuildModule(m),
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),
_ => throw new ArgumentOutOfRangeException(nameof(declaration)),
};

private SourceFunctionSymbol BuildFunction(FunctionDeclaration declaration) => new(this, declaration);
private Symbol BuildGlobal(GlobalDeclaration declaration) => declaration.Syntax.FieldModifier is null
? new SourceAutoPropertySymbol(this, declaration)
: new SourceFieldSymbol(this, declaration);
private SourceFieldSymbol BuildGlobalField(GlobalDeclaration declaration) => new(this, declaration);

private IEnumerable<Symbol> 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 SymbolDocumentation BuildDocumentation() =>
Expand Down

0 comments on commit 760a0d1

Please sign in to comment.