Skip to content

Commit

Permalink
Placed additional symbols into respective class
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Nov 2, 2024
1 parent bb06a72 commit 7072883
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private ImmutableArray<Symbol> BuildMembers()
var symbol = MetadataSymbol.ToSymbol(this, typeDef);
result.Add(symbol);
// Add additional symbols
result.AddRange(GetAdditionalSymbols(symbol));
result.AddRange(symbol.GetAdditionalSymbols());
}

// Done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private ImmutableArray<Symbol> BuildMembers()
var symbol = MetadataSymbol.ToSymbol(this, typeDef);
result.Add(symbol);
// Add additional symbols
result.AddRange(GetAdditionalSymbols(symbol));
result.AddRange(symbol.GetAdditionalSymbols());
}

// Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private ImmutableArray<Symbol> BuildMembers()
var symbol = MetadataSymbol.ToSymbol(this, typeDef);
result.Add(symbol);
// Add additional symbols
result.AddRange(GetAdditionalSymbols(symbol));
result.AddRange(symbol.GetAdditionalSymbols());
}

// Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private ImmutableArray<Symbol> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private ImmutableArray<Symbol> 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;
Expand Down
24 changes: 3 additions & 21 deletions src/Draco.Compiler/Internal/Symbols/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <summary>
/// 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.
/// </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)
{
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;
}
}
/// <returns>The additional symbols for this symbol.</returns>
protected internal virtual IEnumerable<Symbol> GetAdditionalSymbols() => [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Symbol> 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;
}
}
8 changes: 8 additions & 0 deletions src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -213,4 +214,11 @@ public override TypeSymbol GenericInstantiate(Symbol? containingSymbol, GenericC
public override TResult Accept<TResult>(SymbolVisitor<TResult> visitor) => visitor.VisitType(this);

public override abstract string ToString();

protected internal override sealed IEnumerable<Symbol> 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);
}
}

0 comments on commit 7072883

Please sign in to comment.