Skip to content

Commit

Permalink
Always use BOOL instead of bool in native function pointers
Browse files Browse the repository at this point in the history
This in response to #167 (comment)
  • Loading branch information
AArnott committed Mar 9, 2021
1 parent 9ab2c0b commit 21deb90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,11 +1533,11 @@ private FunctionPointerParameterSyntax TranslateDelegateToFunctionPointer(Functi
{
if (this.IsDelegateReference(parameter.Type as IdentifierNameSyntax, out TypeDefinition delegateTypeDef))
{
return FunctionPointerParameter(this.FunctionPointer(delegateTypeDef, this.signatureTypeProvider));
return FunctionPointerParameter(this.FunctionPointer(delegateTypeDef));
}
else if (parameter.Type is PointerTypeSyntax { ElementType: IdentifierNameSyntax idName } && this.IsDelegateReference(idName, out TypeDefinition delegateTypeDef2))
{
return FunctionPointerParameter(PointerType(this.FunctionPointer(delegateTypeDef2, this.signatureTypeProvider)));
return FunctionPointerParameter(PointerType(this.FunctionPointer(delegateTypeDef2)));
}

return parameter;
Expand Down Expand Up @@ -1894,7 +1894,7 @@ private bool TryGetTypeDefHandle(TypeReference typeRef, out TypeDefinitionHandle
string methodName = this.mr.GetString(methodDefinition.Name);
IdentifierNameSyntax innerMethodName = IdentifierName($"{methodName}_{methodCounter}");

MethodSignature<TypeSyntax> signature = methodDefinition.DecodeSignature(this.signatureTypeProvider, null);
MethodSignature<TypeSyntax> signature = methodDefinition.DecodeSignature(this.signatureTypeProviderNoMarshaledTypes, null);
CustomAttributeHandleCollection? returnTypeAttributes = this.GetReturnTypeCustomAttributes(methodDefinition);

TypeSyntax returnType = signature.ReturnType;
Expand Down Expand Up @@ -3058,7 +3058,7 @@ private ParameterSyntax CreateParameter(MethodSignature<TypeSyntax> methodSignat
{
if (originalType is PointerTypeSyntax { ElementType: IdentifierNameSyntax idName } && this.IsDelegateReference(idName, out TypeDefinition delegateTypeDef))
{
return (this.FunctionPointer(delegateTypeDef, this.signatureTypeProvider), null);
return (this.FunctionPointer(delegateTypeDef), null);
}

if (!isReturnOrOutParam && originalType.HasAnnotation(IsSafeHandleTypeAnnotation))
Expand Down Expand Up @@ -3261,23 +3261,23 @@ ExpressionSyntax GetHiddenFieldAccess() => MemberAccessExpression(
// If the field is a delegate type, we have to replace that with a native function pointer to avoid the struct becoming a 'managed type'.
if (originalType is PointerTypeSyntax { ElementType: IdentifierNameSyntax idName } && this.IsDelegateReference(idName, out TypeDefinition typeDef))
{
return (this.FunctionPointer(typeDef, this.signatureTypeProviderNoMarshaledTypes), default);
return (this.FunctionPointer(typeDef), default);
}
else if (originalType is IdentifierNameSyntax idName2 && this.IsDelegateReference(idName2, out typeDef))
{
return (this.FunctionPointer(typeDef, this.signatureTypeProviderNoMarshaledTypes), default);
return (this.FunctionPointer(typeDef), default);
}

return (originalType, default);
}

private FunctionPointerTypeSyntax FunctionPointer(TypeDefinition delegateType, SignatureTypeProvider signatureTypeProvider)
private FunctionPointerTypeSyntax FunctionPointer(TypeDefinition delegateType)
{
CustomAttribute ufpAtt = delegateType.GetCustomAttributes().Select(ah => this.mr.GetCustomAttribute(ah)).Single(a => this.IsAttribute(a, SystemRuntimeInteropServices, nameof(UnmanagedFunctionPointerAttribute)));
var attArgs = ufpAtt.DecodeValue(this.customAttributeTypeProvider);
CallingConvention callingConvention = (CallingConvention)attArgs.FixedArguments[0].Value!;

this.GetSignatureForDelegate(delegateType, signatureTypeProvider, out MethodDefinition invokeMethodDef, out MethodSignature<TypeSyntax> signature);
this.GetSignatureForDelegate(delegateType, this.signatureTypeProviderNoMarshaledTypes, out MethodDefinition invokeMethodDef, out MethodSignature<TypeSyntax> signature);
return this.FunctionPointer(CallingConvention.StdCall, signature, this.mr.GetString(delegateType.Name));
}

Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ public void NativeArray_SizeParamIndex_ProducesSimplerFriendlyOverload()
}

[Fact]
public void BOOL_ReturnTypeBecomes_Boolean_InCOMInterface()
public void BOOL_ReturnTypeRemains_BOOL_InCOMInterface()
{
this.generator = new Generator(this.metadataStream, compilation: this.compilation, parseOptions: this.parseOptions);
Assert.True(this.generator.TryGenerate("ISpellCheckerFactory", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
MethodDeclarationSyntax? method = this.FindGeneratedMethod("IsSupported").FirstOrDefault();
Assert.NotNull(method);
Assert.Equal(SyntaxKind.BoolKeyword, Assert.IsType<PredefinedTypeSyntax>(method!.ParameterList.Parameters.Last().Type).Keyword.Kind());
Assert.Equal("BOOL", Assert.IsType<IdentifierNameSyntax>(method!.ParameterList.Parameters.Last().Type).Identifier.ValueText);
}

/// <summary>
Expand Down

0 comments on commit 21deb90

Please sign in to comment.