|
6 | 6 | using System.Linq; |
7 | 7 | using System.Text; |
8 | 8 | using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.CSharp; |
9 | 10 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
10 | 11 | using Microsoft.Macios.Generator.Attributes; |
11 | 12 | using Microsoft.Macios.Generator.DataModel; |
@@ -47,6 +48,34 @@ static partial class BindingSyntaxFactory { |
47 | 48 | return castExpression; |
48 | 49 | } |
49 | 50 |
|
| 51 | + /// <summary> |
| 52 | + /// Returns the expression needed to cast a bool to a byte to be used in a native call. |
| 53 | + /// </summary> |
| 54 | + /// <param name="parameter">The parameter to cast.</param> |
| 55 | + /// <returns>A conditional expression that casts a bool to a byte.</returns> |
| 56 | + internal static ConditionalExpressionSyntax? CastToByte (in Parameter parameter) |
| 57 | + { |
| 58 | + if (parameter.Type.SpecialType != SpecialType.System_Boolean) |
| 59 | + return null; |
| 60 | + // (byte) 1 |
| 61 | + var castOne = CastExpression ( |
| 62 | + PredefinedType (Token (SyntaxKind.ByteKeyword)), |
| 63 | + LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (1)).WithLeadingTrivia (Space).WithTrailingTrivia (Space) |
| 64 | + ); |
| 65 | + // (byte) 0 |
| 66 | + var castZero = CastExpression ( |
| 67 | + PredefinedType (Token (SyntaxKind.ByteKeyword)), |
| 68 | + LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (0)).WithLeadingTrivia (Space) |
| 69 | + ).WithLeadingTrivia (Space); |
| 70 | + |
| 71 | + // with this exact space count |
| 72 | + // foo ? (byte) 1 : (byte) 0 |
| 73 | + return ConditionalExpression ( |
| 74 | + condition: IdentifierName (parameter.Name).WithTrailingTrivia (Space), |
| 75 | + whenTrue: castOne.WithLeadingTrivia (Space), |
| 76 | + whenFalse: castZero); |
| 77 | + } |
| 78 | + |
50 | 79 | static string? GetObjCMessageSendMethodName<T> (ExportData<T> exportData, |
51 | 80 | TypeInfo returnType, ImmutableArray<Parameter> parameters, bool isSuper = false, bool isStret = false) where T : Enum |
52 | 81 | { |
|
0 commit comments