Skip to content

Commit

Permalink
Simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Nov 2, 2024
1 parent 0cd287e commit 35e0a51
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
41 changes: 10 additions & 31 deletions src/Draco.Compiler/Internal/Symbols/Source/SourceModuleSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,16 +73,11 @@ private ImmutableArray<Symbol> BindMembers(IBinderProvider binderProvider)
var result = ImmutableArray.CreateBuilder<Symbol>();

// 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;
Expand All @@ -106,32 +102,15 @@ private ImmutableArray<Symbol> BindMembers(IBinderProvider binderProvider)
return result.ToImmutable();
}

private IEnumerable<Symbol> 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<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 SourceClassSymbol BuildClass(ClassDeclaration declaration) => new(this, declaration);

private SymbolDocumentation BuildDocumentation() =>
MarkdownDocumentationExtractor.Extract(this);
private SymbolDocumentation BuildDocumentation() => MarkdownDocumentationExtractor.Extract(this);
}
22 changes: 17 additions & 5 deletions src/Draco.Compiler/Internal/Symbols/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -332,15 +333,26 @@ private protected string GenericsToString()
};

/// <summary>
/// Retrieves additional symbols for the given <paramref name="typeSymbol"/>.
/// 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.
/// </summary>
/// <param name="symbol">The symbol to get additional symbols for.</param>
/// <returns>The additional symbols for the given <paramref name="symbol"/>.</returns>
public static IEnumerable<Symbol> 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;
}
}
}

0 comments on commit 35e0a51

Please sign in to comment.