Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use BOOL instead of bool in native function pointers #174

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion test/SpellChecker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

spellCheckerFactory->IsSupported(
"en-US",
out bool supported).ThrowOnFailure();
out BOOL supported).ThrowOnFailure();

if (!supported)
{
Expand Down