Skip to content

Commit

Permalink
Added getter and setter
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 25, 2024
1 parent d6ef74f commit da78448
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Symbols.Source;
using System;
using System.Collections.Immutable;
using System.Threading;
using static Draco.Compiler.Internal.BoundTree.BoundTreeFactory;

namespace Draco.Compiler.Internal.Symbols.Synthetized.AutoProperty;

/// <summary>
/// An auto-generated getter for an auto-property.
/// </summary>
internal sealed class AutoPropertyGetterSymbol(
Symbol containingSymbol,
SourceAutoPropertySymbol property) : FunctionSymbol, IPropertyAccessorSymbol
{
public override Symbol ContainingSymbol { get; } = containingSymbol;

public override string Name => $"{this.Property.Name}_Getter";
public override bool IsStatic => this.Property.IsStatic;
public override Api.Semantics.Visibility Visibility => this.Property.Visibility;

public override ImmutableArray<ParameterSymbol> Parameters => ImmutableArray<ParameterSymbol>.Empty;
public override TypeSymbol ReturnType => this.Property.Type;

public override BoundStatement Body => LazyInitializer.EnsureInitialized(ref this.body, this.BuildBody);
private BoundStatement? body;

PropertySymbol IPropertyAccessorSymbol.Property => this.Property;
public SourceAutoPropertySymbol Property { get; } = property;

private BoundStatement BuildBody() => ExpressionStatement(BlockExpression(
locals: [],
statements: [ExpressionStatement(ReturnExpression(FieldExpression(
receiver: this.IsStatic
? null
: throw new NotImplementedException("TODO: for classes"),
field: this.Property.BackingField)))],
value: BoundUnitExpression.Default));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Symbols.Source;
using Draco.Compiler.Internal.Utilities;
using System;
using System.Collections.Immutable;
using System.Threading;
using static Draco.Compiler.Internal.BoundTree.BoundTreeFactory;

namespace Draco.Compiler.Internal.Symbols.Synthetized.AutoProperty;

/// <summary>
/// An auto-generated setter for an auto-property.
/// </summary>
internal sealed class AutoPropertySetterSymbol(
Symbol containingSymbol,
SourceAutoPropertySymbol property) : FunctionSymbol, IPropertyAccessorSymbol
{
public override Symbol ContainingSymbol { get; } = containingSymbol;

public override string Name => $"{this.Property.Name}_Setter";
public override bool IsStatic => this.Property.IsStatic;
public override Api.Semantics.Visibility Visibility => this.Property.Visibility;

public override ImmutableArray<ParameterSymbol> Parameters => InterlockedUtils.InitializeDefault(ref this.parameters, this.BuildParameters);
private ImmutableArray<ParameterSymbol> parameters;

public override TypeSymbol ReturnType => WellKnownTypes.Unit;

public override BoundStatement Body => LazyInitializer.EnsureInitialized(ref this.body, this.BuildBody);
private BoundStatement? body;

PropertySymbol IPropertyAccessorSymbol.Property => this.Property;
public SourceAutoPropertySymbol Property { get; } = property;

private ImmutableArray<ParameterSymbol> BuildParameters() =>
[new SynthetizedParameterSymbol(this, "value", this.Property.Type)];

private BoundStatement BuildBody() => ExpressionStatement(BlockExpression(
locals: [],
statements: [
ExpressionStatement(AssignmentExpression(
compoundOperator: null,
left: FieldLvalue(
receiver: this.IsStatic
? null
: throw new NotImplementedException("TODO: classes"),
field: this.Property.BackingField),
right: ParameterExpression(this.Parameters[^1]))),
ExpressionStatement(ReturnExpression(BoundUnitExpression.Default))],
value: BoundUnitExpression.Default));
}

0 comments on commit da78448

Please sign in to comment.