Skip to content

Commit 451d8f6

Browse files
Add support for marshalling signed/unsigned boolean types. (#69402)
1 parent f4c60fd commit 451d8f6

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/BoolMarshaller.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
114114
/// </remarks>
115115
public sealed class ByteBoolMarshaller : BoolMarshallerBase
116116
{
117-
public ByteBoolMarshaller()
118-
: base(PredefinedType(Token(SyntaxKind.ByteKeyword)), trueValue: 1, falseValue: 0, compareToTrue: false)
117+
/// <summary>
118+
/// Constructor a <see cref="ByteBoolMarshaller" instance.
119+
/// </summary>
120+
/// <param name="signed">True if the byte should be signed, otherwise false</param>
121+
public ByteBoolMarshaller(bool signed)
122+
: base(PredefinedType(Token(signed ? SyntaxKind.SByteKeyword : SyntaxKind.ByteKeyword)), trueValue: 1, falseValue: 0, compareToTrue: false)
119123
{
120124
}
121125
}
@@ -128,8 +132,12 @@ public ByteBoolMarshaller()
128132
/// </remarks>
129133
public sealed class WinBoolMarshaller : BoolMarshallerBase
130134
{
131-
public WinBoolMarshaller()
132-
: base(PredefinedType(Token(SyntaxKind.IntKeyword)), trueValue: 1, falseValue: 0, compareToTrue: false)
135+
/// <summary>
136+
/// Constructor a <see cref="WinBoolMarshaller" instance.
137+
/// </summary>
138+
/// <param name="signed">True if the int should be signed, otherwise false</param>
139+
public WinBoolMarshaller(bool signed)
140+
: base(PredefinedType(Token(signed ? SyntaxKind.IntKeyword : SyntaxKind.UIntKeyword)), trueValue: 1, falseValue: 0, compareToTrue: false)
133141
{
134142
}
135143
}

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshalAsMarshallingGeneratorFactory.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ namespace Microsoft.Interop
99
{
1010
public sealed class MarshalAsMarshallingGeneratorFactory : IMarshallingGeneratorFactory
1111
{
12-
private static readonly ByteBoolMarshaller s_byteBool = new();
13-
private static readonly WinBoolMarshaller s_winBool = new();
12+
private static readonly ByteBoolMarshaller s_byteBool = new(signed: false);
13+
private static readonly ByteBoolMarshaller s_signed_byteBool = new(signed: true);
14+
private static readonly WinBoolMarshaller s_winBool = new(signed: false);
15+
private static readonly WinBoolMarshaller s_signed_winBool = new(signed: true);
1416
private static readonly VariantBoolMarshaller s_variantBool = new();
1517

1618
private static readonly Utf16CharMarshaller s_utf16Char = new();
@@ -78,10 +80,14 @@ public IMarshallingGenerator Create(
7880
{
7981
NotSupportedDetails = SR.MarshallingBoolAsUndefinedNotSupported
8082
};
81-
case { ManagedType: SpecialTypeInfo { SpecialType: SpecialType.System_Boolean }, MarshallingAttributeInfo: MarshalAsInfo(UnmanagedType.I1 or UnmanagedType.U1, _) }:
83+
case { ManagedType: SpecialTypeInfo { SpecialType: SpecialType.System_Boolean }, MarshallingAttributeInfo: MarshalAsInfo(UnmanagedType.U1, _) }:
8284
return s_byteBool;
83-
case { ManagedType: SpecialTypeInfo { SpecialType: SpecialType.System_Boolean }, MarshallingAttributeInfo: MarshalAsInfo(UnmanagedType.I4 or UnmanagedType.U4 or UnmanagedType.Bool, _) }:
85+
case { ManagedType: SpecialTypeInfo { SpecialType: SpecialType.System_Boolean }, MarshallingAttributeInfo: MarshalAsInfo(UnmanagedType.I1, _) }:
86+
return s_signed_byteBool;
87+
case { ManagedType: SpecialTypeInfo { SpecialType: SpecialType.System_Boolean }, MarshallingAttributeInfo: MarshalAsInfo(UnmanagedType.U4, _) }:
8488
return s_winBool;
89+
case { ManagedType: SpecialTypeInfo { SpecialType: SpecialType.System_Boolean }, MarshallingAttributeInfo: MarshalAsInfo(UnmanagedType.I4 or UnmanagedType.Bool, _) }:
90+
return s_signed_winBool;
8591
case { ManagedType: SpecialTypeInfo { SpecialType: SpecialType.System_Boolean }, MarshallingAttributeInfo: MarshalAsInfo(UnmanagedType.VariantBool, _) }:
8692
return s_variantBool;
8793

0 commit comments

Comments
 (0)