Skip to content

Commit a2bc333

Browse files
committed
Add support for collection expressions
1 parent 988a319 commit a2bc333

File tree

5 files changed

+91
-26
lines changed

5 files changed

+91
-26
lines changed

sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.Visit.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,17 @@ public void WriteIid(string name, Guid value)
8787
WriteIndentedLine("get");
8888
WriteBlockStart();
8989

90-
WriteIndentedLine("ReadOnlySpan<byte> data = new byte[] {");
90+
WriteIndented("ReadOnlySpan<byte> data = ");
91+
92+
if (_config.GeneratePreviewCode)
93+
{
94+
WriteLine('[');
95+
}
96+
else
97+
{
98+
WriteLine("new byte[] {");
99+
}
100+
91101
IncreaseIndentation();
92102
WriteIndentation();
93103

@@ -113,7 +123,17 @@ public void WriteIid(string name, Guid value)
113123

114124
WriteNewline();
115125
DecreaseIndentation();
116-
WriteIndentedLine("};");
126+
127+
if (_config.GeneratePreviewCode)
128+
{
129+
WriteIndented(']');
130+
}
131+
else
132+
{
133+
WriteIndented('}');
134+
}
135+
136+
WriteLine(';');
117137

118138
NeedsNewline = true;
119139

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,14 +1501,34 @@ void HandleUnmanagedConstant(CSharpOutputBuilder outputBuilder, InitListExpr ini
15011501
outputBuilder.AddUsingDirective("System.Runtime.CompilerServices");
15021502
outputBuilder.AddUsingDirective("System.Runtime.InteropServices");
15031503

1504-
outputBuilder.WriteIndentedLine("ReadOnlySpan<byte> data = new byte[] {");
1504+
outputBuilder.WriteIndented("ReadOnlySpan<byte> data = ");
1505+
1506+
if (_config.GeneratePreviewCode)
1507+
{
1508+
outputBuilder.WriteLine("[");
1509+
}
1510+
else
1511+
{
1512+
outputBuilder.WriteLine("new byte[] {");
1513+
}
1514+
15051515
outputBuilder.IncreaseIndentation();
15061516

15071517
HandleInitListExpr(outputBuilder, initListExpr);
15081518

15091519
outputBuilder.WriteNewline();
15101520
outputBuilder.DecreaseIndentation();
1511-
outputBuilder.WriteIndentedLine("};");
1521+
1522+
if (_config.GeneratePreviewCode)
1523+
{
1524+
outputBuilder.WriteIndented(']');
1525+
}
1526+
else
1527+
{
1528+
outputBuilder.WriteIndented('}');
1529+
}
1530+
1531+
outputBuilder.WriteLine(';');
15121532

15131533
outputBuilder.NeedsNewline = true;
15141534
long rootSize = -1;
@@ -2533,7 +2553,14 @@ private void VisitStringLiteral(StringLiteral stringLiteral)
25332553

25342554
case CX_CLK_UTF32:
25352555
{
2536-
outputBuilder.Write("new uint[] { ");
2556+
if (_config.GeneratePreviewCode)
2557+
{
2558+
outputBuilder.Write('[');
2559+
}
2560+
else
2561+
{
2562+
outputBuilder.Write("new uint[] { ");
2563+
}
25372564

25382565
var bytes = Encoding.UTF32.GetBytes(stringLiteral.String);
25392566
var codepoints = MemoryMarshal.Cast<byte, uint>(bytes);
@@ -2545,7 +2572,16 @@ private void VisitStringLiteral(StringLiteral stringLiteral)
25452572
outputBuilder.Write(", ");
25462573
}
25472574

2548-
outputBuilder.Write("0x00000000 }");
2575+
outputBuilder.Write("0x00000000");
2576+
2577+
if (_config.GeneratePreviewCode)
2578+
{
2579+
outputBuilder.Write(']');
2580+
}
2581+
else
2582+
{
2583+
outputBuilder.Write(" }");
2584+
}
25492585
break;
25502586
}
25512587

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ static string GetFoundRemappingString(string name, HashSet<string> remappings)
17691769
else
17701770
{
17711771
result += ' ';
1772-
remainingRemappings = remappings.Except(new string[] { recommendedRemapping });
1772+
remainingRemappings = remappings.Except([recommendedRemapping]);
17731773
remainingString = "Other";
17741774
}
17751775
}
@@ -1848,7 +1848,6 @@ private void AddUsingDirective(IOutputBuilder? outputBuilder, string namespaceNa
18481848
private void CloseOutputBuilder(Stream stream, IOutputBuilder outputBuilder, bool isMethodClass, bool leaveStreamOpen, bool emitNamespaceDeclaration)
18491849
{
18501850
ArgumentNullException.ThrowIfNull(stream);
1851-
18521851
ArgumentNullException.ThrowIfNull(outputBuilder);
18531852

18541853
using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
@@ -1917,11 +1916,17 @@ private void CloseOutputBuilder(Stream stream, IOutputBuilder outputBuilder, boo
19171916

19181917
if (outputBuilder is CSharpOutputBuilder csOutputBuilder)
19191918
{
1920-
ForCSharp(csOutputBuilder);
1919+
if (csOutputBuilder.Contents.Any())
1920+
{
1921+
ForCSharp(csOutputBuilder);
1922+
}
19211923
}
19221924
else if (outputBuilder is XmlOutputBuilder xmlOutputBuilder)
19231925
{
1924-
ForXml(xmlOutputBuilder);
1926+
if (xmlOutputBuilder.Contents.Any())
1927+
{
1928+
ForXml(xmlOutputBuilder);
1929+
}
19251930
}
19261931

19271932
void ForCSharp(CSharpOutputBuilder csharpOutputBuilder)

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/VarDeclarationTest.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,25 @@ protected override Task WideStringLiteralConstTestImpl()
156156
const wchar_t* MyConst2 = L""Test"";
157157
const wchar_t* const MyConst3 = L""Test"";";
158158

159-
var expectedOutputContents = $@"namespace ClangSharp.Test
159+
var expectedOutputContents = $@"using System;
160+
161+
namespace ClangSharp.Test
160162
{{
161163
public static partial class Methods
162164
{{
163165
[NativeTypeName(""const wchar_t[5]"")]
164-
public static readonly uint[] MyConst1 = new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }};
166+
public static ReadOnlySpan<uint> MyConst1 => [0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000];
165167
166168
[NativeTypeName(""const wchar_t *"")]
167-
public static uint[] MyConst2 = new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }};
169+
public static uint[] MyConst2 = [0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000];
168170
169171
[NativeTypeName(""const wchar_t *const"")]
170-
public static readonly uint[] MyConst3 = new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }};
172+
public static ReadOnlySpan<uint> MyConst3 => [0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000];
171173
}}
172174
}}
173175
";
174176

175-
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents);
177+
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents);
176178
}
177179

178180
protected override Task StringLiteralConstTestImpl()
@@ -208,23 +210,25 @@ protected override Task WideStringLiteralStaticConstTestImpl()
208210
static const wchar_t* MyConst2 = L""Test"";
209211
static const wchar_t* const MyConst3 = L""Test"";";
210212

211-
var expectedOutputContents = $@"namespace ClangSharp.Test
213+
var expectedOutputContents = $@"using System;
214+
215+
namespace ClangSharp.Test
212216
{{
213217
public static partial class Methods
214218
{{
215219
[NativeTypeName(""const wchar_t[5]"")]
216-
public static readonly uint[] MyConst1 = new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }};
220+
public static ReadOnlySpan<uint> MyConst1 => [0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000];
217221
218222
[NativeTypeName(""const wchar_t *"")]
219-
public static uint[] MyConst2 = new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }};
223+
public static uint[] MyConst2 = [0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000];
220224
221225
[NativeTypeName(""const wchar_t *const"")]
222-
public static readonly uint[] MyConst3 = new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }};
226+
public static ReadOnlySpan<uint> MyConst3 => [0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000];
223227
}}
224228
}}
225229
";
226230

227-
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents);
231+
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents);
228232
}
229233

230234
protected override Task StringLiteralStaticConstTestImpl()

tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/VarDeclarationTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,19 @@ protected override Task WideStringLiteralConstTestImpl()
198198
<constant name=""MyConst1"" access=""public"">
199199
<type primitive=""False"">ReadOnlySpan&lt;uint&gt;</type>
200200
<value>
201-
<code>new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }}</code>
201+
<code>[0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000]</code>
202202
</value>
203203
</constant>
204204
<field name=""MyConst2"" access=""public"">
205205
<type primitive=""False"">uint[]</type>
206206
<value>
207-
<code>new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }}</code>
207+
<code>[0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000]</code>
208208
</value>
209209
</field>
210210
<constant name=""MyConst3"" access=""public"">
211211
<type primitive=""False"">ReadOnlySpan&lt;uint&gt;</type>
212212
<value>
213-
<code>new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }}</code>
213+
<code>[0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000]</code>
214214
</value>
215215
</constant>
216216
</class>
@@ -270,19 +270,19 @@ protected override Task WideStringLiteralStaticConstTestImpl()
270270
<constant name=""MyConst1"" access=""public"">
271271
<type primitive=""False"">ReadOnlySpan&lt;uint&gt;</type>
272272
<value>
273-
<code>new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }}</code>
273+
<code>[0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000]</code>
274274
</value>
275275
</constant>
276276
<field name=""MyConst2"" access=""public"">
277277
<type primitive=""False"">uint[]</type>
278278
<value>
279-
<code>new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }}</code>
279+
<code>[0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000]</code>
280280
</value>
281281
</field>
282282
<constant name=""MyConst3"" access=""public"">
283283
<type primitive=""False"">ReadOnlySpan&lt;uint&gt;</type>
284284
<value>
285-
<code>new uint[] {{ 0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000 }}</code>
285+
<code>[0x00000054, 0x00000065, 0x00000073, 0x00000074, 0x00000000]</code>
286286
</value>
287287
</constant>
288288
</class>

0 commit comments

Comments
 (0)