Skip to content

Commit

Permalink
Update source generation support for inline arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed Sep 27, 2023
1 parent e44543b commit 9b11392
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 18 deletions.
6 changes: 0 additions & 6 deletions code/Binary.SourceGeneration/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ public static class Constants

public const string TupleKeyAttributeTypeName = "Mikodev.Binary.Attributes.TupleKeyAttribute";

public const string AllocatorTypeName = "Mikodev.Binary.Allocator";

public const string ConverterTypeName = "Mikodev.Binary.Converter";

public const string IConverterTypeName = "Mikodev.Binary.IConverter";

public const string IConverterCreatorTypeName = "Mikodev.Binary.IConverterCreator";

public const string IGeneratorContextTypeName = "Mikodev.Binary.IGeneratorContext";

public const string ConverterAttributeTypeName = "Mikodev.Binary.Attributes.ConverterAttribute";

public const string ConverterCreatorAttributeTypeName = "Mikodev.Binary.Attributes.ConverterCreatorAttribute";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private void AppendEncodeMethod()
{
var info = this.info;
var elements = info.ElementTypes;
Output.AppendIndent(2, $"public override void Encode(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"public override void Encode(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"{{");
AppendEnsureContext();
Output.AppendIndent(3, $"foreach (var i in item)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ private void AppendConverterTail()

private void AppendEncodeMethod()
{
var info = this.info;
Output.AppendIndent(2, $"public override void Encode(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"public override void Encode(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"{{");
Output.AppendIndent(3, $"var buffer = System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpan(ref System.Runtime.CompilerServices.Unsafe.As<{SymbolTypeFullName}, {GetTypeFullName(0)}>(ref item), {info.Length});");
Output.AppendIndent(3, $"var buffer = (System.ReadOnlySpan<{GetTypeFullName(0)}>)item;");
Output.AppendIndent(3, $"for (var i = 0; i < buffer.Length; i++)");
Output.AppendIndent(4, $"cvt0.EncodeAuto(ref allocator, buffer[i]);");
Output.AppendIndent(2, $"}}");
Expand All @@ -43,13 +42,12 @@ private void AppendEncodeMethod()

private void AppendDecodeMethod()
{
var info = this.info;
Output.AppendIndent();
Output.AppendIndent(2, $"public override {SymbolTypeFullName} Decode(in System.ReadOnlySpan<byte> span)");
Output.AppendIndent(2, $"{{");
Output.AppendIndent(3, $"var body = span;");
Output.AppendIndent(3, $"var result = default({SymbolTypeFullName});");
Output.AppendIndent(3, $"var buffer = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.As<{SymbolTypeFullName}, {GetTypeFullName(0)}>(ref result), {info.Length});");
Output.AppendIndent(3, $"var buffer = (System.Span<{GetTypeFullName(0)}>)result;");
Output.AppendIndent(3, $"for (var i = 0; i < buffer.Length; i++)");
Output.AppendIndent(4, $"buffer[i] = cvt0.DecodeAuto(ref body);");
Output.AppendIndent(3, $"return result;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ private void AppendEnsureContext()

private void AppendEncodeContext(int indent, int i)
{
Output.AppendIndent(indent, $"{Constants.AllocatorTypeName}.Append(ref allocator, new System.ReadOnlySpan<byte>(keys[{i}]));");
Output.AppendIndent(indent, $"Mikodev.Binary.Allocator.Append(ref allocator, new System.ReadOnlySpan<byte>(keys[{i}]));");
Output.AppendIndent(indent, $"cvt{i}.EncodeWithLengthPrefix(ref allocator, var{i});");
CancellationToken.ThrowIfCancellationRequested();
}

private void AppendEncodeMethod()
{
var members = this.members;
Output.AppendIndent(2, $"public override void Encode(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"public override void Encode(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"{{");
AppendEnsureContext();
for (var i = 0; i < members.Length; i++)
Expand Down Expand Up @@ -110,7 +110,7 @@ private void AppendConverterCreatorBody()
var members = this.members;
Output.AppendIndent(3, $"var names = new string[] {{ ", $" }};", members.Length, x => members[x].NamedKeyLiteral);
Output.AppendIndent(3, $"var optional = new bool[] {{ ", $" }};", members.Length, x => members[x].IsOptional ? "true" : "false");
Output.AppendIndent(3, $"var encoding = ({Constants.ConverterTypeName}<string>)context.GetConverter(typeof(string));");
Output.AppendIndent(3, $"var encoding = (Mikodev.Binary.Converter<string>)context.GetConverter(typeof(string));");
for (var i = 0; i < members.Length; i++)
{
var member = members[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void AppendEncodeMethod(bool auto)
var methodName = auto ? "EncodeAuto" : "Encode";
if (auto)
Output.AppendIndent();
Output.AppendIndent(2, $"public override void {methodName}(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"public override void {methodName}(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)");
Output.AppendIndent(2, $"{{");
AppendEnsureContext();
for (var i = 0; i < members.Length; i++)
Expand Down
2 changes: 1 addition & 1 deletion code/Binary.SourceGeneration/SourceGeneratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public string GetConverterTypeFullName(ITypeSymbol symbol)
{
var dictionary = this.converterTypeFullNameCache;
if (dictionary.TryGetValue(symbol, out var result) is false)
dictionary.Add(symbol, result = $"{Constants.ConverterTypeName}<{GetTypeFullName(symbol)}>");
dictionary.Add(symbol, result = $"Mikodev.Binary.Converter<{GetTypeFullName(symbol)}>");
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion code/Binary.SourceGeneration/SymbolConverterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void AppendConverterCreatorHead()
Output.AppendIndent(1, $"private sealed class {OutputConverterCreatorTypeName} : {Constants.IConverterCreatorTypeName}");
Output.AppendIndent(1, $"{{");

Output.AppendIndent(2, $"public {Constants.IConverterTypeName} GetConverter({Constants.IGeneratorContextTypeName} context, System.Type type)");
Output.AppendIndent(2, $"public {Constants.IConverterTypeName} GetConverter(Mikodev.Binary.IGeneratorContext context, System.Type type)");
Output.AppendIndent(2, $"{{");
Output.AppendIndent(3, $"if (type != typeof({SymbolTypeFullName}))");
Output.AppendIndent(4, $"return null;");
Expand Down

0 comments on commit 9b11392

Please sign in to comment.