Skip to content

Commit

Permalink
New source generation name mangling rules
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed May 29, 2024
1 parent 43fc567 commit 4baa8ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
42 changes: 32 additions & 10 deletions code/Binary.SourceGeneration.Tests/DirectTests/SymbolsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class Hotel
public int[][] Entry;
}
""";
yield return new object[] { a, "Array", "global::System.Int32[,]", "Array2D_l_System_Int32_r" };
yield return new object[] { b, "Value", "global::System.String[]", "Array_l_System_String_r" };
yield return new object[] { c, "Items", "global::System.Double[,,,]", "Array4D_l_System_Double_r" };
yield return new object[] { d, "Entry", "global::System.Int32[][]", "Array_l_Array_l_System_Int32_r_r" };
yield return new object[] { a, "Array", "global::System.Int32[,]", "a_2_p_g_System_0_Int32_q" };
yield return new object[] { b, "Value", "global::System.String[]", "a_1_p_g_System_0_String_q" };
yield return new object[] { c, "Items", "global::System.Double[,,,]", "a_4_p_g_System_0_Double_q" };
yield return new object[] { d, "Entry", "global::System.Int32[][]", "a_1_p_a_1_p_g_System_0_Int32_q_q" };
}

public static IEnumerable<object[]> GlobalNamespaceTypeData()
Expand All @@ -74,8 +74,8 @@ class Bravo
public B<int> Data;
}
""";
yield return new object[] { a, "Item", "global::A", "g_A" };
yield return new object[] { b, "Data", "global::B<global::System.Int32>", "g_B_l_System_Int32_r" };
yield return new object[] { a, "Item", "global::A", "g_0_A" };
yield return new object[] { b, "Data", "global::B<global::System.Int32>", "g_1_B_b_g_System_0_Int32_d" };
}

public static IEnumerable<object[]> NestedTypeData()
Expand Down Expand Up @@ -112,8 +112,8 @@ class Bravo
public B<int>.X.Y<string> Data;
}
""";
yield return new object[] { a, "Item", "global::One.Two.A.X.Y", "One_Two_A_X_Y" };
yield return new object[] { b, "Data", "global::B<global::System.Int32>.X.Y<global::System.String>", "g_B_l_System_Int32_r_X_Y_l_System_String_r" };
yield return new object[] { a, "Item", "global::One.Two.A.X.Y", "g_One_Two_0_A_0_X_0_Y" };
yield return new object[] { b, "Data", "global::B<global::System.Int32>.X.Y<global::System.String>", "g_1_B_b_g_System_0_Int32_d_0_X_1_Y_b_g_System_0_String_d" };
}

public static IEnumerable<object[]> SpecialNameData()
Expand All @@ -134,15 +134,37 @@ class Ignore
public @public.@class Bravo;
}
""";
yield return new object[] { a, "Alpha", "global::@class.@yield.@namespace.@async.@await.@public", "class_yield_namespace_async_await_public" };
yield return new object[] { a, "Bravo", "global::@class.@yield.@namespace.@async.@await.@public.@class", "class_yield_namespace_async_await_public_class" };
yield return new object[] { a, "Alpha", "global::@class.@yield.@namespace.@async.@await.@public", "g_class_yield_namespace_async_await_0_public" };
yield return new object[] { a, "Bravo", "global::@class.@yield.@namespace.@async.@await.@public.@class", "g_class_yield_namespace_async_await_0_public_0_class" };
}

public static IEnumerable<object[]> NestedMultipleTypeArgumentsGenericTypeData()
{
var source =
"""
namespace Alpha.Bravo;
class Generic<T, U>
{
public class NestedGeneric<X, Y, Z> { }
}
class Alpha
{
public Generic<int, string>.NestedGeneric<int, string, double> Intent;
}
""";
var symbolFullName = "global::Alpha.Bravo.Generic<global::System.Int32, global::System.String>.NestedGeneric<global::System.Int32, global::System.String, global::System.Double>";
var outputFullName = "g_Alpha_Bravo_2_Generic_b_g_System_0_Int32_g_System_0_String_d_3_NestedGeneric_b_g_System_0_Int32_g_System_0_String_g_System_0_Double_d";
yield return new object[] { source, "Intent", symbolFullName, outputFullName };
}

[Theory(DisplayName = "Get Full Name Test")]
[MemberData(nameof(ArrayData))]
[MemberData(nameof(GlobalNamespaceTypeData))]
[MemberData(nameof(NestedTypeData))]
[MemberData(nameof(SpecialNameData))]
[MemberData(nameof(NestedMultipleTypeArgumentsGenericTypeData))]
public void GetFullNameTest(string source, string memberName, string symbolFullName, string outputFullName)
{
var compilation = CompilationModule.CreateCompilation(source);
Expand Down
39 changes: 16 additions & 23 deletions code/Binary.SourceGeneration/Symbols.Normalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,21 @@ static void Invoke(StringBuilder target, ITypeSymbol symbol)

static void InvokeNamespaceSymbol(StringBuilder target, INamespaceSymbol @namespace)
{
if (@namespace.IsGlobalNamespace is false)
{
var source = @namespace.ToDisplayParts();
foreach (var i in source)
{
if (i.Symbol is { } symbol)
_ = target.Append(symbol.Name);
else
_ = target.Append('_');
}
_ = target.Append('g');
if (@namespace.IsGlobalNamespace)
return;
}
_ = target.Append("g_");
foreach (var i in @namespace.ToDisplayParts())
if (i.Symbol is { } symbol)
_ = target.AppendFormat("_{0}", symbol.Name);
}

static void InvokeArrayTypeSymbol(StringBuilder target, IArrayTypeSymbol symbol)
{
_ = target.Append("Array");
if (symbol.Rank is not 1)
_ = target.Append($"{symbol.Rank}D");
_ = target.Append("_l_");
_ = target.Append("a_");
_ = target.Append(symbol.Rank);
_ = target.Append("_p_");
Invoke(target, symbol.ElementType);
_ = target.Append("_r");
_ = target.Append("_q");
}

static void InvokeNamedTypeSymbol(StringBuilder target, INamedTypeSymbol symbol)
Expand All @@ -156,22 +148,23 @@ static void InvokeNamedTypeSymbol(StringBuilder target, INamedTypeSymbol symbol)
else
InvokeNamespaceSymbol(target, @namespace);

if (containing is not null || @namespace.IsGlobalNamespace is false)
_ = target.Append('_');
_ = target.Append(symbol.Name);
var arguments = symbol.TypeArguments;
_ = target.Append('_');
_ = target.Append(arguments.Length);
_ = target.Append('_');
_ = target.Append(symbol.Name);
if (arguments.Length is 0)
return;

_ = target.Append("_l_");
_ = target.Append("_b_");
for (var i = 0; i < arguments.Length; i++)
{
Invoke(target, arguments[i]);
if (i == arguments.Length - 1)
break;
_ = target.Append("_c_");
_ = target.Append('_');
}
_ = target.Append("_r");
_ = target.Append("_d");
}

var target = new StringBuilder();
Expand Down

0 comments on commit 4baa8ce

Please sign in to comment.