Skip to content

Commit f90ff70

Browse files
committed
Generator 2.2.1.1 fix
Fixed Generic types Might need to add test for it tho
1 parent 9615480 commit f90ff70

File tree

5 files changed

+128
-53
lines changed

5 files changed

+128
-53
lines changed

EIV_Pack.Example/AnyTypeT_Tset.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using EIV_Pack.Formatters;
2+
3+
namespace EIV_Pack.Example;
4+
5+
[EIV_Packable]
6+
internal partial class AnyTypeT_Tset<AnyStruct> where AnyStruct : class, new()
7+
{
8+
public AnyStruct? Value;
9+
}
10+
11+
[EIV_Packable]
12+
internal partial class AnyTypeT_Tset2<AnyStruct> where AnyStruct : IPackable<AnyStruct>, new()
13+
{
14+
public AnyStruct? Value;
15+
}

EIV_Pack.Example/EIV_Pack.Example.csproj

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net10.0;net8.0;net472</TargetFrameworks>
4+
<TargetFrameworks>net10.0;net8.0;net472;netstandard2.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<LangVersion>latest</LangVersion>
@@ -24,12 +24,5 @@
2424
</ItemGroup>
2525
-->
2626

27-
<PropertyGroup Condition=" $(TargetFramework) != 'net472' ">
28-
<!-- To be native AOT -->
29-
<IsAotCompatible>True</IsAotCompatible>
30-
<PublishAot>true</PublishAot>
31-
<DebuggerSupport>False</DebuggerSupport>
32-
<PublishTrimmed>true</PublishTrimmed>
33-
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
34-
</PropertyGroup>
27+
3528
</Project>

EIV_Pack.Generator/EIV_Pack.Generator.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<!-- NuGet -->
2121
<IsPackable>true</IsPackable>
2222
<Description>Code generator for EIVPack.</Description>
23-
<Version>2.2.1</Version>
24-
<FileVersion>2.2.1</FileVersion>
23+
<Version>2.2.1.1</Version>
24+
<FileVersion>2.2.1.1</FileVersion>
2525
</PropertyGroup>
2626

2727
<ItemGroup>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace EIV_Pack.Generator;
4+
5+
internal class HelperMethods
6+
{
7+
public static string ValueName(INamedTypeSymbol symbol)
8+
{
9+
return (symbol.IsRecord, symbol.IsValueType) switch
10+
{
11+
(true, true) => "record struct",
12+
(true, false) => "record",
13+
(false, true) => "struct",
14+
(false, false) => "class",
15+
};
16+
}
17+
18+
public static string GetTypeParameters(INamedTypeSymbol symbol)
19+
{
20+
string typeParameterStr = string.Empty;
21+
if (symbol.TypeParameters.Length > 0)
22+
{
23+
typeParameterStr += "<";
24+
foreach (var typeParameter in symbol.TypeParameters)
25+
{
26+
typeParameterStr += typeParameter.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
27+
}
28+
typeParameterStr += ">";
29+
}
30+
return typeParameterStr;
31+
}
32+
33+
public static string GetNameSpace(INamedTypeSymbol symbol)
34+
{
35+
List<string> names = [];
36+
INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace;
37+
while (namespaceSymbol != null)
38+
{
39+
if (namespaceSymbol.Name.Contains("<global namespace>"))
40+
break;
41+
42+
names.Add(namespaceSymbol.Name);
43+
namespaceSymbol = namespaceSymbol.ContainingNamespace;
44+
}
45+
46+
names.Reverse();
47+
48+
string namespaceStr = string.Empty;
49+
50+
if (names.Count != 0)
51+
namespaceStr = string.Join(".", names);
52+
53+
if (names.Count > 1)
54+
namespaceStr = namespaceStr.Substring(1);
55+
56+
return namespaceStr;
57+
}
58+
59+
public static IEnumerable<string> GetWhereClauses(INamedTypeSymbol namedType)
60+
{
61+
foreach (var tp in namedType.TypeParameters)
62+
{
63+
var parts = new List<string>();
64+
65+
if (tp.HasReferenceTypeConstraint)
66+
parts.Add("class");
67+
68+
if (tp.HasUnmanagedTypeConstraint)
69+
parts.Add("unmanaged");
70+
71+
if (tp.HasValueTypeConstraint)
72+
parts.Add("struct");
73+
74+
if (tp.HasNotNullConstraint)
75+
parts.Add("notnull");
76+
77+
if (!tp.ConstraintTypes.IsDefaultOrEmpty)
78+
parts.AddRange(tp.ConstraintTypes.Select(t => t.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)));
79+
80+
if (tp.HasConstructorConstraint)
81+
parts.Add("new()");
82+
83+
if (parts.Count > 0)
84+
{
85+
yield return $"where {tp.Name} : {string.Join(", ", parts)}";
86+
}
87+
}
88+
}
89+
}

EIV_Pack.Generator/PackGenerator.cs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using Microsoft.CodeAnalysis;
2-
using Microsoft.CodeAnalysis.CSharp;
3-
using Microsoft.CodeAnalysis.CSharp.Syntax;
42
using System.Text;
53

64
namespace EIV_Pack.Generator;
@@ -24,13 +22,11 @@ internal static void Generate(GeneratorClass generatorClass, SourceProductionCon
2422
.Replace("<", "_")
2523
.Replace(">", "_");
2624

27-
var classOrStructOrRecord = (typeSymbol.IsRecord, typeSymbol.IsValueType) switch
28-
{
29-
(true, true) => "record struct",
30-
(true, false) => "record",
31-
(false, true) => "struct",
32-
(false, false) => "class",
33-
};
25+
var TypeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
26+
27+
var classOrStructOrRecord = HelperMethods.ValueName(typeSymbol);
28+
29+
string typeParameterStr = HelperMethods.GetTypeParameters(typeSymbol);
3430

3531
StringBuilder sb = new();
3632
sb.AppendLine("// Generated with EIV_Pack.Generator.");
@@ -44,26 +40,7 @@ internal static void Generate(GeneratorClass generatorClass, SourceProductionCon
4440
sb.AppendLine();
4541
}
4642

47-
List<string> names = [];
48-
INamespaceSymbol namespaceSymbol = typeSymbol.ContainingNamespace;
49-
while (namespaceSymbol != null)
50-
{
51-
if (namespaceSymbol.Name.Contains("<global namespace>"))
52-
break;
53-
54-
names.Add(namespaceSymbol.Name);
55-
namespaceSymbol = namespaceSymbol.ContainingNamespace;
56-
}
57-
58-
names.Reverse();
59-
60-
string namespaceStr = string.Empty;
61-
62-
if (names.Count != 0)
63-
namespaceStr = string.Join(".", names);
64-
65-
if (names.Count > 1)
66-
namespaceStr = namespaceStr.Substring(1);
43+
string namespaceStr = HelperMethods.GetNameSpace(typeSymbol);
6744

6845
if (!string.IsNullOrEmpty(namespaceStr))
6946
{
@@ -79,7 +56,7 @@ internal static void Generate(GeneratorClass generatorClass, SourceProductionCon
7956
}
8057

8158
sb.AppendLine();
82-
sb.AppendLine($"partial {classOrStructOrRecord} {typeSymbol.Name} : IPackable<{typeSymbol.Name}>");
59+
sb.AppendLine($"partial {classOrStructOrRecord} {TypeName} : IPackable<{TypeName}>");
8360
sb.AppendLine("{");
8461

8562
if (!typeSymbol.GetMembers().Any(x => x.IsStatic && x.Kind == SymbolKind.Method && x.Name == ".cctor"))
@@ -97,21 +74,21 @@ internal static void Generate(GeneratorClass generatorClass, SourceProductionCon
9774

9875
bool hasRegister = PreProcessing.HasRegisterFormatter(typeSymbol);
9976

100-
string formatterName = generatorClass.IsNet8OrGreater ? $"EIV_Formatter<{typeSymbol.Name}>" : $"{typeSymbol.Name}_Formatter";
77+
string formatterName = generatorClass.IsNet8OrGreater ? $"EIV_Formatter<{TypeName}>" : $"{typeSymbol.Name}_Formatter{typeParameterStr}";
10178

10279
sb.AppendLine(
10380
$$"""
10481
10582
public static{{(hasRegister ? " new " : " ")}}void RegisterFormatter()
10683
{
107-
if (!FormatterProvider.IsRegistered<{{typeSymbol.Name}}>())
84+
if (!FormatterProvider.IsRegistered<{{TypeName}}>())
10885
{
109-
FormatterProvider.Register<{{typeSymbol.Name}}>(new {{formatterName}}());
86+
FormatterProvider.Register<{{TypeName}}>(new {{formatterName}}());
11087
}
11188
112-
if (!FormatterProvider.IsRegistered<{{typeSymbol.Name}}[]>())
89+
if (!FormatterProvider.IsRegistered<{{TypeName}}[]>())
11390
{
114-
FormatterProvider.Register(new ArrayFormatter<{{typeSymbol.Name}}>());
91+
FormatterProvider.Register(new ArrayFormatter<{{TypeName}}>());
11592
}
11693
}
11794
@@ -147,16 +124,16 @@ internal static void Generate(GeneratorClass generatorClass, SourceProductionCon
147124
}
148125

149126
sb.AppendLine($$"""
150-
sealed class {{typeSymbol.Name}}_Formatter : BaseFormatter<{{typeSymbol.Name}}>
127+
sealed class {{typeSymbol.Name}}_Formatter{{typeParameterStr}} : BaseFormatter<{{TypeName}}>{{ string.Join("\n, ", HelperMethods.GetWhereClauses(typeSymbol))}}
151128
{
152-
public override void Serialize(ref PackWriter writer, scoped ref readonly {{typeSymbol.Name}} value)
129+
public override void Serialize(ref PackWriter writer, scoped ref readonly {{TypeName}} value)
153130
{
154-
{{typeSymbol.Name}}.SerializePackable(ref writer, in value);
131+
{{TypeName}}.SerializePackable(ref writer, in value);
155132
}
156133
157-
public override void Deserialize(ref PackReader reader, scoped ref {{typeSymbol.Name}} value)
134+
public override void Deserialize(ref PackReader reader, scoped ref {{TypeName}} value)
158135
{
159-
{{typeSymbol.Name}}.DeserializePackable(ref reader, ref value);
136+
{{TypeName}}.DeserializePackable(ref reader, ref value);
160137
}
161138
}
162139
""");
@@ -173,14 +150,15 @@ public override void Deserialize(ref PackReader reader, scoped ref {{typeSymbol.
173150

174151
internal static void GeneratePackable(ref INamedTypeSymbol typeSymbol, ref StringBuilder sb, ref List<ISymbol> FieldAndParamList, ref List<ISymbol> initOnly, bool isNet8OrGreater)
175152
{
153+
var TypeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
176154
var nullable = !typeSymbol.IsValueType && isNet8OrGreater ? "?" : string.Empty;
177155
var exceptInit = FieldAndParamList.Except(initOnly);
178156
bool hasInitOnly = initOnly.Count > 0;
179157
string ending = hasInitOnly ? string.Empty : ";";
180158

181159
sb.AppendLine($"\tconst int EIV_PACK_FieldAndParamCount = {FieldAndParamList.Count};");
182160
sb.AppendLine();
183-
sb.AppendLine($"\tpublic static void DeserializePackable(ref PackReader reader, scoped ref {typeSymbol.Name}{nullable} value)");
161+
sb.AppendLine($"\tpublic static void DeserializePackable(ref PackReader reader, scoped ref {TypeName}{nullable} value)");
184162
sb.AppendLine("\t{");
185163
if (FieldAndParamList.Count <= 255)
186164
sb.AppendLine($"\t\tif (!reader.TryReadSmallHeader(out byte header) || header != EIV_PACK_FieldAndParamCount)");
@@ -219,7 +197,7 @@ internal static void GeneratePackable(ref INamedTypeSymbol typeSymbol, ref Strin
219197

220198
sb.AppendLine("\t}");
221199
sb.AppendLine();
222-
sb.AppendLine($"\tpublic static void SerializePackable(ref PackWriter writer, scoped ref readonly {typeSymbol.Name}{nullable} value)");
200+
sb.AppendLine($"\tpublic static void SerializePackable(ref PackWriter writer, scoped ref readonly {TypeName}{nullable} value)");
223201
sb.AppendLine("\t{");
224202

225203
string useSmall = FieldAndParamList.Count <= 255 ? "Small" : string.Empty;

0 commit comments

Comments
 (0)