Skip to content

Commit 957976c

Browse files
committed
Directly output an int if an integer literal is available
1 parent 8d35d64 commit 957976c

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,25 @@ private void VisitBinaryOperator(BinaryOperator binaryOperator)
4242
outputBuilder.Write(binaryOperator.OpcodeStr);
4343
outputBuilder.Write(' ');
4444

45-
if ((binaryOperator.IsShiftOp || binaryOperator.IsShiftAssignOp) && binaryOperator.RHS.Type.Kind != CXType_Int)
45+
if (binaryOperator.IsShiftOp || binaryOperator.IsShiftAssignOp)
4646
{
4747
// RHS of shift operation in C# must be an int
48-
outputBuilder.Write("(int)");
49-
outputBuilder.Write('(');
50-
51-
Visit(binaryOperator.RHS);
48+
if (binaryOperator.RHS.Type.Kind is CXType_Int or CXType_Long)
49+
{
50+
Visit(binaryOperator.RHS);
51+
}
52+
else if (binaryOperator.RHS is IntegerLiteral literal)
53+
{
54+
outputBuilder.Write(literal.Value);
55+
}
56+
else
57+
{
58+
outputBuilder.Write("(int)");
5259

53-
outputBuilder.Write(')');
60+
outputBuilder.Write('(');
61+
Visit(binaryOperator.RHS);
62+
outputBuilder.Write(')');
63+
}
5464
}
5565
else
5666
{

tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,11 @@ public Task UnsignedIntBitshiftTest()
285285
{
286286
var inputContents = @"
287287
const int Signed = 1;
288-
const unsigned int Unsigned = 1U;
288+
const long SignedLong = 1;
289+
const unsigned int Unsigned = 1;
289290
290291
const int ShiftSigned = 1 << Signed;
292+
const int ShiftSignedLong = 1 << SignedLong;
291293
const int ShiftUnsigned = 1 << Unsigned;
292294
293295
const int CInt = 1 << 1;
@@ -309,41 +311,47 @@ public static partial class Methods
309311
[NativeTypeName(""const int"")]
310312
public const int Signed = 1;
311313
314+
[NativeTypeName(""const long"")]
315+
public const int SignedLong = 1;
316+
312317
[NativeTypeName(""const unsigned int"")]
313-
public const uint Unsigned = 1U;
318+
public const uint Unsigned = 1;
314319
315320
[NativeTypeName(""const int"")]
316321
public const int ShiftSigned = 1 << Signed;
317322
323+
[NativeTypeName(""const int"")]
324+
public const int ShiftSignedLong = 1 << SignedLong;
325+
318326
[NativeTypeName(""const int"")]
319327
public const int ShiftUnsigned = 1 << (int)(Unsigned);
320328
321329
[NativeTypeName(""const int"")]
322330
public const int CInt = 1 << 1;
323331
324332
[NativeTypeName(""const int"")]
325-
public const int CUint = 1 << (int)(1U);
333+
public const int CUint = 1 << 1;
326334
327335
[NativeTypeName(""#define Left 1 << 1U"")]
328-
public const int Left = 1 << (int)(1U);
336+
public const int Left = 1 << 1;
329337
330338
[NativeTypeName(""#define Right 1 >> 1U"")]
331-
public const int Right = 1 >> (int)(1U);
339+
public const int Right = 1 >> 1;
332340
333341
[NativeTypeName(""#define Int 1 << 1"")]
334342
public const int Int = 1 << 1;
335343
336344
[NativeTypeName(""#define Long 1 << 1L"")]
337-
public const int Long = 1 << (int)(1);
345+
public const int Long = 1 << 1;
338346
339347
[NativeTypeName(""#define LongLong 1 << 1LL"")]
340-
public const int LongLong = 1 << (int)(1L);
348+
public const int LongLong = 1 << 1;
341349
342350
[NativeTypeName(""#define ULong 1 << 1UL"")]
343-
public const int ULong = 1 << (int)(1U);
351+
public const int ULong = 1 << 1;
344352
345353
[NativeTypeName(""#define ULongLong 1 << 1ULL"")]
346-
public const int ULongLong = 1 << (int)(1UL);
354+
public const int ULongLong = 1 << 1;
347355
}
348356
}
349357
";

0 commit comments

Comments
 (0)