Skip to content

Commit

Permalink
Globals mostly torn out
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 25, 2024
1 parent de75c46 commit f201e99
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 244 deletions.
4 changes: 2 additions & 2 deletions src/Draco.Compiler/Api/Semantics/SemanticModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public sealed partial class SemanticModel : IBinderProvider

// Filled out by incremental binding
private readonly ConcurrentDictionary<SyntaxFunctionSymbol, BoundStatement> boundFunctions = new();
private readonly ConcurrentDictionary<SyntaxGlobalSymbol, GlobalBinding> boundGlobals = new();
private readonly ConcurrentDictionary<SyntaxFieldSymbol, GlobalBinding> boundGlobals = new();
private readonly ConcurrentDictionary<SyntaxNode, BoundNode> boundNodeMap = new();
private readonly ConcurrentDictionary<SyntaxNode, Symbol> symbolMap = new();

Expand Down Expand Up @@ -92,7 +92,7 @@ private ImmutableArray<Diagnostic> GetDiagnostics(SourceSpan? span = null)
{
// We need to search for this global
var globalSymbol = binder.ContainingSymbol?.Members
.OfType<SyntaxGlobalSymbol>()
.OfType<SyntaxFieldSymbol>()
.FirstOrDefault(s => s.Name == varDecl.Name.Text);
globalSymbol?.Bind(this);
break;
Expand Down
8 changes: 0 additions & 8 deletions src/Draco.Compiler/Api/Semantics/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,6 @@ internal sealed class PropertySymbol(Internal.Symbols.PropertySymbol property)
public IFunctionSymbol? Setter => this.Symbol.Setter?.ToApiSymbol();
}

internal sealed class GlobalSymbol(Internal.Symbols.GlobalSymbol global)
: SymbolBase<Internal.Symbols.GlobalSymbol>(global), IGlobalSymbol
{
public bool IsMutable => this.Symbol.IsMutable;
public bool IsStatic => this.Symbol.IsStatic;
public ITypeSymbol Type => this.Symbol.Type.ToApiSymbol();
}

internal sealed class LocalSymbol(Internal.Symbols.LocalSymbol local)
: SymbolBase<Internal.Symbols.LocalSymbol>(local), ILocalSymbol
{
Expand Down
1 change: 0 additions & 1 deletion src/Draco.Compiler/Api/Semantics/SymbolKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public enum SymbolKind
Module,
Field,
Property,
Global,
Local,
Parameter,
Function,
Expand Down
6 changes: 3 additions & 3 deletions src/Draco.Compiler/Internal/Binding/Binder_Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public virtual BoundStatement BindFunction(SourceFunctionSymbol function, Diagno
return statementTask.Result;
}

public virtual GlobalBinding BindGlobal(SourceGlobalSymbol global, DiagnosticBag diagnostics)
public virtual GlobalBinding BindGlobal(SourceFieldSymbol global, DiagnosticBag diagnostics)
{
var globalName = global.DeclaringSyntax.Name.Text;
var constraints = new ConstraintSolver(this, $"global {globalName}");
Expand Down Expand Up @@ -110,7 +110,7 @@ public virtual ScriptBinding BindScript(ScriptModuleSymbol module, DiagnosticBag
{
// Retrieve the symbol
var symbol = module.Members
.OfType<ScriptGlobalSymbol>()
.OfType<ScriptFieldSymbol>()
.First(g => g.DeclaringSyntax == varDecl);

BindGlobal(symbol);
Expand Down Expand Up @@ -170,7 +170,7 @@ public virtual ScriptBinding BindScript(ScriptModuleSymbol module, DiagnosticBag
value: BoundUnitExpression.Default)),
EvalType: evalType);

void BindGlobal(ScriptGlobalSymbol symbol)
void BindGlobal(ScriptFieldSymbol symbol)
{
var typeSyntax = symbol.DeclaringSyntax.Type;
var valueSyntax = symbol.DeclaringSyntax.Value;
Expand Down
2 changes: 1 addition & 1 deletion src/Draco.Compiler/Internal/Binding/IncrementalBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override BoundStatement BindFunction(SourceFunctionSymbol function, Diagn
key: function,
valueFactory: _ => base.BindFunction(function, diagnostics));

public override GlobalBinding BindGlobal(SourceGlobalSymbol global, DiagnosticBag diagnostics) =>
public override GlobalBinding BindGlobal(SourceFieldSymbol global, DiagnosticBag diagnostics) =>
semanticModel.boundGlobals.GetOrAdd(
key: global,
valueFactory: _ => base.BindGlobal(global, diagnostics));
Expand Down
33 changes: 10 additions & 23 deletions src/Draco.Compiler/Internal/Codegen/MetadataCodegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,6 @@ Symbol GetContainingSymbol()
}));
}

case GlobalSymbol global:
{
return this.AddMemberReference(
parent: this.GetEntityHandle(global.ContainingSymbol
?? throw new InvalidOperationException()),
name: global.Name,
signature: this.EncodeBlob(e =>
{
var encoder = e.Field();
this.EncodeSignatureType(encoder.Type(), global.Type);
}));
}

default:
throw new ArgumentOutOfRangeException(nameof(symbol));
}
Expand Down Expand Up @@ -454,10 +441,10 @@ private void EncodeModule(IModule module, TypeDefinitionHandle? parentModule = n
{
var currentFieldIndex = fieldIndex;
var currentProcIndex = procIndex;
// Go through globals
foreach (var global in module.Globals)
// Go through global fields
foreach (var field in module.Fields)
{
this.EncodeGlobal(global);
this.EncodeField(field);
currentFieldIndex++;
}

Expand Down Expand Up @@ -511,21 +498,21 @@ private void EncodeModule(IModule module, TypeDefinitionHandle? parentModule = n
}
}

private FieldDefinitionHandle EncodeGlobal(GlobalSymbol global)
private FieldDefinitionHandle EncodeField(FieldSymbol field)
{
var visibility = global.Visibility switch
var visibility = field.Visibility switch
{
Api.Semantics.Visibility.Public => FieldAttributes.Public,
Api.Semantics.Visibility.Internal => FieldAttributes.Assembly,
Api.Semantics.Visibility.Private => FieldAttributes.Private,
_ => throw new ArgumentOutOfRangeException(nameof(global)),
_ => throw new ArgumentOutOfRangeException(nameof(field)),
};

// Definition
return this.AddFieldDefinition(
attributes: visibility | FieldAttributes.Static,
name: global.Name,
signature: this.EncodeGlobalSignature(global));
name: field.Name,
signature: this.EncodeFieldSignature(field));
}

private MethodDefinitionHandle EncodeProcedure(IProcedure procedure, string? specialName = null)
Expand Down Expand Up @@ -607,8 +594,8 @@ private MethodDefinitionHandle EncodeProcedure(IProcedure procedure, string? spe
return definitionHandle;
}

private BlobHandle EncodeGlobalSignature(GlobalSymbol global) =>
this.EncodeBlob(e => this.EncodeSignatureType(e.Field().Type(), global.Type));
private BlobHandle EncodeFieldSignature(FieldSymbol field) =>
this.EncodeBlob(e => this.EncodeSignatureType(e.Field().Type(), field.Type));

private BlobHandle EncodeProcedureSignature(IProcedure procedure) => this.EncodeBlob(e =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void AnalyzeFunction(SourceFunctionSymbol symbol, DiagnosticBag di
/// </summary>
/// <param name="symbol">The symbol to analyze.</param>
/// <param name="diagnostics">The diagnostics to report errors to.</param>
public static void AnalyzeValue(SourceGlobalSymbol symbol, DiagnosticBag diagnostics)
public static void AnalyzeValue(SourceFieldSymbol symbol, DiagnosticBag diagnostics)
{
if (symbol.Value is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ private void Complete()
this.globalInitializer.Write(Ret(default(Void)));
}

public override void VisitGlobal(GlobalSymbol globalSymbol)
public override void VisitField(FieldSymbol fieldSymbol)
{
if (globalSymbol is not SyntaxGlobalSymbol syntaxGlobal) return;
if (fieldSymbol is not SyntaxFieldSymbol syntaxField) return;

this.module.DefineGlobal(syntaxGlobal);
this.module.DefineField(syntaxField);

if (syntaxGlobal is not SourceGlobalSymbol sourceGlobal) return;
if (syntaxField is not SourceFieldSymbol sourceGlobal) return;

// If there's a value, compile it
if (sourceGlobal.Value is not null)
Expand Down
4 changes: 2 additions & 2 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ internal interface IModule
public IReadOnlyDictionary<ModuleSymbol, IModule> Submodules { get; }

/// <summary>
/// The globals within this module.
/// The fields within this module.
/// </summary>
public IReadOnlySet<GlobalSymbol> Globals { get; }
public IReadOnlySet<FieldSymbol> Fields { get; }

/// <summary>
/// The compiled procedures within this module.
Expand Down
10 changes: 5 additions & 5 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class Module : IModule
public IDictionary<ModuleSymbol, IModule> Submodules => this.submodules;
IReadOnlyDictionary<ModuleSymbol, IModule> IModule.Submodules => this.submodules;

public IReadOnlySet<GlobalSymbol> Globals => this.globals;
public IReadOnlySet<FieldSymbol> Fields => this.fields;

public Procedure GlobalInitializer { get; }
IProcedure IModule.GlobalInitializer => this.GlobalInitializer;
Expand All @@ -32,7 +32,7 @@ internal sealed class Module : IModule
public Module? Parent { get; }
IModule? IModule.Parent => this.Parent;

private readonly HashSet<GlobalSymbol> globals = [];
private readonly HashSet<FieldSymbol> fields = [];
private readonly Dictionary<FunctionSymbol, IProcedure> procedures = [];
private readonly Dictionary<ModuleSymbol, IModule> submodules = [];

Expand All @@ -58,7 +58,7 @@ public ImmutableArray<IProcedure> GetProcedures()
return result.ToImmutable();
}

public void DefineGlobal(GlobalSymbol globalSymbol) => this.globals.Add(globalSymbol);
public void DefineField(FieldSymbol fieldSymbol) => this.fields.Add(fieldSymbol);

public Procedure DefineProcedure(FunctionSymbol functionSymbol)
{
Expand All @@ -84,8 +84,8 @@ public override string ToString()
{
var result = new StringBuilder();
result.AppendLine($"module {this.Symbol.Name}");
result.AppendJoin(Environment.NewLine, this.globals);
if (this.globals.Count > 0 && this.procedures.Count > 1) result.Append(doubleNewline);
result.AppendJoin(Environment.NewLine, this.fields);
if (this.fields.Count > 0 && this.procedures.Count > 1) result.Append(doubleNewline);
result.AppendJoin(doubleNewline, this.procedures.Values);
if (this.procedures.Count > 0 && this.submodules.Count > 0) result.Append(doubleNewline);
result.AppendJoin(doubleNewline, this.submodules.Values);
Expand Down
14 changes: 12 additions & 2 deletions src/Draco.Compiler/Internal/Symbols/FieldSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
namespace Draco.Compiler.Internal.Symbols;

/// <summary>
/// Represents a nonstatic field.
/// Represents a field either on a module-level (global variable) or on a type-level (instance field).
/// </summary>
internal abstract class FieldSymbol : VariableSymbol, IMemberSymbol
{
public bool IsStatic => false;
public bool IsStatic => this.ContainingSymbol is ModuleSymbol;
public bool IsExplicitImplementation => false;

// NOTE: Override for covariant return type
public override FieldSymbol? GenericDefinition => null;
public override SymbolKind Kind => SymbolKind.Field;

/// <summary>
/// True, if this global is a literal, meaning its value is known at compile-time and has to be inlined.
/// </summary>
public virtual bool IsLiteral => false;

/// <summary>
/// The literal value of this global, if it is a literal.
/// </summary>
public virtual object? LiteralValue => null;

public override FieldSymbol GenericInstantiate(Symbol? containingSymbol, ImmutableArray<TypeSymbol> arguments) =>
(FieldSymbol)base.GenericInstantiate(containingSymbol, arguments);
public override FieldSymbol GenericInstantiate(Symbol? containingSymbol, GenericContext context) =>
Expand Down

This file was deleted.

38 changes: 0 additions & 38 deletions src/Draco.Compiler/Internal/Symbols/GlobalSymbol.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public override Api.Semantics.Visibility Visibility
}
}

public override bool IsLiteral => this.fieldDefinition.Attributes.HasFlag(FieldAttributes.Literal);

public override object? LiteralValue => InterlockedUtils.InitializeMaybeNull(ref this.literalValue, this.BuildLiteralValue);
private object? literalValue;

public override SymbolDocumentation Documentation => LazyInitializer.EnsureInitialized(ref this.documentation, this.BuildDocumentation);
private SymbolDocumentation? documentation;

Expand Down Expand Up @@ -85,6 +90,15 @@ private ImmutableArray<AttributeInstance> BuildAttributes() =>
private TypeSymbol BuildType() =>
this.fieldDefinition.DecodeSignature(this.Assembly.DeclaringCompilation.TypeProvider, this);

private object? BuildLiteralValue()
{
var constantHandle = this.fieldDefinition.GetDefaultValue();
if (constantHandle.IsNil) return null;

var constant = this.MetadataReader.GetConstant(constantHandle);
return MetadataSymbol.DecodeConstant(constant, this.MetadataReader);
}

private SymbolDocumentation BuildDocumentation() =>
XmlDocumentationExtractor.Extract(this);

Expand Down
Loading

0 comments on commit f201e99

Please sign in to comment.