Skip to content

Commit 233410a

Browse files
committed
0.16.0
1 parent fd0ea4a commit 233410a

File tree

7 files changed

+67
-5
lines changed

7 files changed

+67
-5
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 0.15.0
1+
## 0.16.0
2+
- Add
3+
- Default parameter support for record mode
4+
5+
## 0.15.0
26
- Breaking Changes
37
- Record mode no longer generates record structures, only view structures
48
- View structs now generate ref structs on runtimes that support ref structs

Coplt.Union.Analyzers/Generators/Templates/TemplateStructUnion.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public record struct RecordItem(
2929
string Type,
3030
string Name,
3131
UnionCaseTypeKind Kind,
32-
bool IsGeneric
32+
bool IsGeneric,
33+
string? DefaultValue
3334
);
3435

3536
public record struct UnionCase(
@@ -1059,6 +1060,10 @@ private void GenMake(string impl_name, string tags_name)
10591060
if (first) first = false;
10601061
else sb.Append(", ");
10611062
sb.Append($"{item.Type} {item.Name}");
1063+
if (item.DefaultValue != null)
1064+
{
1065+
sb.Append($" = {item.DefaultValue}");
1066+
}
10621067
}
10631068
}
10641069
else if (@case.Type != "void")

Coplt.Union.Analyzers/Generators/UnionGenerator.cs

+32-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,28 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
7777
}
7878
var cases = ImmutableArray.CreateBuilder<UnionCase>();
7979
var AnyGeneric = false;
80+
var variant_names = new HashSet<string>();
8081
foreach (var (template, _) in Templates)
8182
{
8283
foreach (var (member, i) in template.Members.Select((a, b) => (a, b)))
8384
{
8485
if (member is MethodDeclarationSyntax mds)
8586
{
8687
var case_name = mds.Identifier.ToString();
88+
if (!variant_names.Add(case_name))
89+
{
90+
var desc = Utils.MakeError(Id,
91+
Strings.Get("Generator.Union.Error.RecordOverloaded"));
92+
if (member is BaseTypeDeclarationSyntax bts)
93+
{
94+
diagnostics.Add(Diagnostic.Create(desc, bts.Identifier.GetLocation()));
95+
}
96+
else
97+
{
98+
diagnostics.Add(Diagnostic.Create(desc, member.GetLocation()));
99+
}
100+
continue;
101+
}
87102
var member_symbol = (IMethodSymbol)semantic_model.GetDeclaredSymbol(mds)!;
88103
var ret_type_symbol = member_symbol.ReturnType;
89104
var is_void = ret_type_symbol.SpecialType == SpecialType.System_Void;
@@ -246,7 +261,23 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
246261
}
247262
}
248263
}
249-
items.Add(new RecordItem(type_name, arg_name, kind, is_generic));
264+
var is_enum = type_symbol is { TypeKind: TypeKind.Enum };
265+
var defv = parameter.HasExplicitDefaultValue
266+
? is_enum
267+
? $"({type_name}){parameter.ExplicitDefaultValue}"
268+
: parameter.ExplicitDefaultValue switch
269+
{
270+
null => "default",
271+
bool v => v ? "true" : "false",
272+
string v => $"\"{v.Replace("\"", "\\\"")}\"",
273+
(byte or sbyte or short or ushort or int or uint or long or ulong or double) and var v
274+
=> $"{v}",
275+
float v => $"{v}f",
276+
decimal v => $"{v}m",
277+
var v => $"({type_name}){v}",
278+
}
279+
: null;
280+
items.Add(new RecordItem(type_name, arg_name, kind, is_generic, defv));
250281
}
251282
var meta = new RecordMeta(union_attr.ViewName);
252283
if (variant_attr != null)

Coplt.Union.Analyzers/Resources/Strings.resx

+3
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@
4242
<data name="Generator.Union.Error.ByRef" xml:space="preserve">
4343
<value>Union does not support ByRef</value>
4444
</data>
45+
<data name="Generator.Union.Error.RecordOverloaded" xml:space="preserve">
46+
<value>Variants do not allow overloading</value>
47+
</data>
4548
</root>

Coplt.Union.Analyzers/Resources/Strings.zh-Hans.resx

+3
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@
4343
<data name="Generator.Union.Error.ByRef" xml:space="preserve">
4444
<value>联合不支持 ByRef</value>
4545
</data>
46+
<data name="Generator.Union.Error.RecordOverloaded" xml:space="preserve">
47+
<value>变体不允许重载</value>
48+
</data>
4649
</root>

Coplt.Union.Source/Coplt.Union.Source.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
77
<PackageId>Coplt.Union</PackageId>
8-
<Version>0.15.0</Version>
8+
<Version>0.16.0</Version>
99
<IsPackable>true</IsPackable>
1010
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1111
<NoWarn>CS9113</NoWarn>

Tests/Unions.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Coplt.Union;
1+
using System.Runtime.InteropServices;
2+
using Coplt.Union;
23

34
namespace Tests;
45

@@ -119,3 +120,18 @@ private interface Template
119120
void Foo((float, string) a, (float, string) b, (int, string) c);
120121
}
121122
}
123+
124+
public struct Union12Foo { }
125+
126+
[Union]
127+
public partial class Union12
128+
{
129+
[UnionTemplate]
130+
private interface Template
131+
{
132+
void Foo(int a = 1, bool b = true, string c = "foo", int? d = null, object? e = null);
133+
void Foo2(byte a = 1, uint b = 1, Union12Foo c = default, nuint d = 1);
134+
void Foo3(float a = 1.0f, double b = 1.0, decimal c = 1.0m, ulong d = 1);
135+
void Foo4(LayoutKind a = LayoutKind.Auto, LayoutKind b = LayoutKind.Sequential);
136+
}
137+
}

0 commit comments

Comments
 (0)