Skip to content

Commit

Permalink
Simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Nov 3, 2024
1 parent 3178f1b commit 91db04d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Draco.Compiler/Internal/Binding/Binder_Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ private BindingTask<BoundExpression> BindThisExpression(ThisExpressionSyntax syn
}

// Check, if the function has a this argument
var thisArg = function.ThisArgument;
var thisArg = function.ThisParameter;
if (thisArg is null)
{
// No, report error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ public SourceFunctionSymbol(Symbol containingSymbol, FunctionDeclaration declara
{
}

public override bool IsStatic => this.ThisArgument == null;

public override void Bind(IBinderProvider binderProvider)
{
this.BindAttributesIfNeeded(binderProvider);
this.BindGenericParametersIfNeeded(binderProvider);
this.BindThisParameterIfNeeded(binderProvider);
this.BindParametersIfNeeded(binderProvider);
// Force binding of parameters, as the type is lazy too
foreach (var param in this.Parameters.Cast<SourceParameterSymbol>()) param.Bind(binderProvider);
Expand Down
51 changes: 27 additions & 24 deletions src/Draco.Compiler/Internal/Symbols/Syntax/SyntaxFunctionSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal abstract class SyntaxFunctionSymbol(
public override Symbol ContainingSymbol => containingSymbol;
public override FunctionDeclarationSyntax DeclaringSyntax => syntax;
public override string Name => this.DeclaringSyntax.Name.Text;
public override bool IsStatic => this.ThisParameter is null;

public override Api.Semantics.Visibility Visibility =>
GetVisibilityFromTokenKind(this.DeclaringSyntax.VisibilityModifier?.Kind);
Expand All @@ -35,15 +36,11 @@ internal abstract class SyntaxFunctionSymbol(
public override ImmutableArray<ParameterSymbol> Parameters => this.BindParametersIfNeeded(this.DeclaringCompilation!);
private ImmutableArray<ParameterSymbol> parameters;

public SourceThisParameterSymbol? ThisArgument
{
get
{
this.BindParametersIfNeeded(this.DeclaringCompilation!);
return this.thisArgument;
}
}
private SourceThisParameterSymbol? thisArgument;
/// <summary>
/// An optional this parameter, if the function is an instance method.
/// </summary>
public ParameterSymbol? ThisParameter => this.BindThisParameterIfNeeded(this.DeclaringCompilation!);
private ParameterSymbol? thisParameter;

public override TypeSymbol ReturnType => this.BindReturnTypeIfNeeded(this.DeclaringCompilation!);
private TypeSymbol? returnType;
Expand Down Expand Up @@ -101,6 +98,16 @@ private ImmutableArray<TypeParameterSymbol> BindGenericParameters(IBinderProvide
return genericParams.ToImmutable();
}

protected ParameterSymbol? BindThisParameterIfNeeded(IBinderProvider binderProvider) =>
InterlockedUtils.InitializeMaybeNull(ref this.thisParameter, () => this.BindThisParameter(binderProvider));

private ParameterSymbol? BindThisParameter(IBinderProvider binderProvider)
{
if (this.DeclaringSyntax.ParameterList.Values.FirstOrDefault() is not ThisParameterSyntax thisSyntax) return null;

return new SourceThisParameterSymbol(this, thisSyntax);
}

protected ImmutableArray<ParameterSymbol> BindParametersIfNeeded(IBinderProvider binderProvider) =>
InterlockedUtils.InitializeDefault(ref this.parameters, () => this.BindParameters(binderProvider));

Expand All @@ -109,29 +116,25 @@ private ImmutableArray<ParameterSymbol> BindParameters(IBinderProvider binderPro
var parameterSyntaxes = this.DeclaringSyntax.ParameterList.Values.ToList();
var parameters = ImmutableArray.CreateBuilder<ParameterSymbol>();

var isInstance = null as SourceThisParameterSymbol;

for (var i = 0; i < parameterSyntaxes.Count; ++i)
// NOTE: If the first parameter is 'this', we skip it
var start = parameterSyntaxes.FirstOrDefault() is ThisParameterSyntax ? 1 : 0;
for (var i = start; i < parameterSyntaxes.Count; ++i)
{
var syntax = parameterSyntaxes[i];

if (syntax is ThisParameterSyntax thisParameter)
if (syntax is ThisParameterSyntax thisSyntax)
{
var asSymbol = new SourceThisParameterSymbol(this, thisParameter);
if (i == 0)
{
this.thisArgument = asSymbol;
isInstance = asSymbol;
continue;
}
// NOTE: Do we want to construct an error here, or regular symbol is fine?
// Error, 'this' at an illegal place
var thisSymbol = new SourceThisParameterSymbol(this, thisSyntax);
parameters.Add(thisSymbol);
binderProvider.DiagnosticBag.Add(Diagnostic.Create(
template: SymbolResolutionErrors.ThisParameterNotFirst,
location: thisParameter.Location));
parameters.Add(asSymbol);
location: thisSyntax.Location));
continue;
}
var parameterSyntax = (NormalParameterSyntax)syntax;

// We assume it's a regular parameter
var parameterSyntax = (NormalParameterSyntax)syntax;
var parameterName = parameterSyntax.Name.Text;

var usedBefore = parameters.Any(p => p.Name == parameterName);
Expand Down

0 comments on commit 91db04d

Please sign in to comment.