Skip to content

Commit

Permalink
Step 1
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Nov 12, 2024
1 parent d418c40 commit 2dbd54c
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/Draco.Compiler/Internal/Binding/BinderCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Draco.Compiler.Api;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Symbols;
using Draco.Compiler.Internal.Symbols.Script;
using Draco.Compiler.Internal.Symbols.Source;
using Draco.Compiler.Internal.Symbols.Syntax;
Expand Down Expand Up @@ -73,7 +74,7 @@ private ModuleBinder BuildModuleBinder(ModuleDeclarationSyntax syntax)
// For that we unwrap from the injected import layer(s)
var parent = UnwrapFromImportBinder(binder);
var moduleSymbol = parent.DeclaredSymbols
.OfType<SourceModuleSymbol>()
.OfType<ModuleSymbol>()
.FirstOrDefault(member => member.Name == syntax.Name.Text);
Debug.Assert(moduleSymbol is not null);
binder = WrapInImportBinder(binder, syntax);
Expand All @@ -88,7 +89,7 @@ private FunctionBinder BuildFunctionDeclarationBinder(FunctionDeclarationSyntax
// For that we unwrap from the injected import layer(s)
var parent = UnwrapFromImportBinder(binder);
var functionSymbol = parent.DeclaredSymbols
.OfType<SyntaxFunctionSymbol>()
.OfType<FunctionSymbol>()
.FirstOrDefault(member => member.DeclaringSyntax == syntax);
Debug.Assert(functionSymbol is not null);
// NOTE: We are not using the unwrapped parent, we need the injected import layers
Expand All @@ -103,7 +104,7 @@ private ClassBinder BuildClassDeclarationBinder(ClassDeclarationSyntax syntax)
// For that we unwrap from the injected import layer(s)
var parent = UnwrapFromImportBinder(binder);
var classSymbol = parent.DeclaredSymbols
.OfType<SourceClassSymbol>()
.OfType<TypeSymbol>()
.First(member => member.DeclaringSyntax == syntax); // should we shove that in a helper ?
return new ClassBinder(binder, classSymbol);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,13 @@ public override void VisitFunction(FunctionSymbol functionSymbol)

public override void VisitField(FieldSymbol fieldSymbol)
{
if (fieldSymbol is not SyntaxFieldSymbol and not AutoPropertyBackingFieldSymbol) return;
@class.DefineField(fieldSymbol);

// TODO: Initializer value
@class.DefineField(fieldSymbol);
}

public override void VisitProperty(PropertySymbol propertySymbol)
{
// TODO: Not flexible, won't work for non-auto props
if (propertySymbol is not SyntaxAutoPropertySymbol) return;

@class.DefineProperty(propertySymbol);

// TODO: Initializer value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ private void Complete()

public override void VisitField(FieldSymbol fieldSymbol)
{
if (fieldSymbol is not SyntaxFieldSymbol and not AutoPropertyBackingFieldSymbol) return;

this.module.DefineField(fieldSymbol);

if (fieldSymbol is not SourceFieldSymbol sourceGlobal) return;
Expand Down Expand Up @@ -79,9 +77,6 @@ public override void VisitType(TypeSymbol typeSymbol)

public override void VisitProperty(PropertySymbol propertySymbol)
{
// TODO: Not flexible, won't work for non-auto props
if (propertySymbol is not SyntaxAutoPropertySymbol) return;

this.module.DefineProperty(propertySymbol);

if (propertySymbol is not SourceAutoPropertySymbol sourceAutoProp) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using Draco.Compiler.Api.Syntax;

namespace Draco.Compiler.Internal.Symbols.Generic;

Expand All @@ -18,6 +19,7 @@ internal sealed class FieldInstanceSymbol(
public override bool IsMutable => this.GenericDefinition.IsMutable;
public override bool IsStatic => this.GenericDefinition.IsStatic;
public override Api.Semantics.Visibility Visibility => this.GenericDefinition.Visibility;
public override SyntaxNode? DeclaringSyntax => this.GenericDefinition.DeclaringSyntax;

public override Symbol? ContainingSymbol { get; } = containingSymbol;
public override FieldSymbol GenericDefinition { get; } = genericDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using System.Threading;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Utilities;

Expand Down Expand Up @@ -59,6 +60,7 @@ public override ImmutableArray<TypeSymbol> GenericArguments
public override BoundStatement? Body => this.GenericDefinition.Body;
public override CodegenDelegate? Codegen => this.GenericDefinition.Codegen;
public override Api.Semantics.Visibility Visibility => this.GenericDefinition.Visibility;
public override SyntaxNode? DeclaringSyntax => this.GenericDefinition.DeclaringSyntax;

public override Symbol? ContainingSymbol { get; } = containingSymbol;
public override FunctionSymbol GenericDefinition { get; } = genericDefinition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using Draco.Compiler.Api.Syntax;

namespace Draco.Compiler.Internal.Symbols.Generic;

Expand All @@ -16,6 +17,7 @@ internal sealed class ParameterInstanceSymbol(

public override bool IsVariadic => this.GenericDefinition.IsVariadic;
public override string Name => this.GenericDefinition.Name;
public override SyntaxNode? DeclaringSyntax => this.GenericDefinition.DeclaringSyntax;

public override FunctionSymbol ContainingSymbol { get; } = containingSymbol;
public override ParameterSymbol GenericDefinition { get; } = genericDefinition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Draco.Compiler.Api.Syntax;

namespace Draco.Compiler.Internal.Symbols.Generic;

/// <summary>
Expand All @@ -14,4 +16,5 @@ internal sealed class PropertyAccessorInstanceSymbol(
{
public PropertySymbol Property { get; } = property;
public override Api.Semantics.Visibility Visibility => this.GenericDefinition.Visibility;
public override SyntaxNode? DeclaringSyntax => this.GenericDefinition.DeclaringSyntax;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Utilities;

namespace Draco.Compiler.Internal.Symbols.Generic;
Expand Down Expand Up @@ -26,6 +27,7 @@ internal sealed class PropertyInstanceSymbol(
public override bool IsStatic => this.GenericDefinition.IsStatic;
public override bool IsExplicitImplementation => this.GenericDefinition.IsExplicitImplementation;
public override Api.Semantics.Visibility Visibility => this.GenericDefinition.Visibility;
public override SyntaxNode? DeclaringSyntax => this.GenericDefinition.DeclaringSyntax;

public override Symbol? ContainingSymbol { get; } = containingSymbol;
public override PropertySymbol GenericDefinition { get; } = genericDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Utilities;

namespace Draco.Compiler.Internal.Symbols.Generic;
Expand Down Expand Up @@ -63,6 +64,7 @@ public override ImmutableArray<TypeSymbol> GenericArguments
public override bool IsSealed => this.GenericDefinition.IsSealed;
public override string Name => this.GenericDefinition.Name;
public override Api.Semantics.Visibility Visibility => this.GenericDefinition.Visibility;
public override SyntaxNode? DeclaringSyntax => this.GenericDefinition.DeclaringSyntax;

public override Symbol? ContainingSymbol { get; } = containingSymbol;

Expand Down
22 changes: 21 additions & 1 deletion src/Draco.Compiler/Internal/Symbols/Source/SourceClassSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Binding;
using Draco.Compiler.Internal.Declarations;
using Draco.Compiler.Internal.Solver.Constraints;
using Draco.Compiler.Internal.Symbols.Generic;
using Draco.Compiler.Internal.Symbols.Synthetized;
using Draco.Compiler.Internal.Utilities;

Expand Down Expand Up @@ -92,7 +94,6 @@ private ImmutableArray<Symbol> BindMembers(IBinderProvider binderProvider)

var earlierMember = result.FirstOrDefault(s => s.Name == member.Name);
result.Add(member);
result.AddRange(member.GetAdditionalSymbols());

// We check for illegal shadowing
if (earlierMember is null) continue;
Expand All @@ -108,6 +109,25 @@ private ImmutableArray<Symbol> BindMembers(IBinderProvider binderProvider)
location: syntax.Location,
formatArgs: member.Name));
}

// If this is a generic definition, we generic instantiate the members
if (this.IsGenericDefinition)
{
var genericContext = new GenericContext(this.GenericParameters.ToImmutableDictionary(t => t, t => t as TypeSymbol));
for (var i = 0; i < result.Count; ++i)
{
result[i] = result[i].GenericInstantiate(this, genericContext);
}
}

// Add additional symbols for each
var origCount = result.Count;
for (var i = 0; i < origCount; ++i)
{
var member = result[i];
result.AddRange(member.GetAdditionalSymbols());
}

return result.ToImmutable();
}

Expand Down

0 comments on commit 2dbd54c

Please sign in to comment.