diff --git a/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertyGetterSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertyGetterSymbol.cs new file mode 100644 index 000000000..ea351a191 --- /dev/null +++ b/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertyGetterSymbol.cs @@ -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; + +/// +/// An auto-generated getter for an auto-property. +/// +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 Parameters => ImmutableArray.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)); +} diff --git a/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertySetterSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertySetterSymbol.cs new file mode 100644 index 000000000..fbf012737 --- /dev/null +++ b/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertySetterSymbol.cs @@ -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; + +/// +/// An auto-generated setter for an auto-property. +/// +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 Parameters => InterlockedUtils.InitializeDefault(ref this.parameters, this.BuildParameters); + private ImmutableArray 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 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)); +}