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

Add forwarding support for WasmLinkage on LibraryImport #109234

Merged
merged 1 commit into from
Oct 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterConcatenatedSyntaxOutputs(generateSingleStub.Select((data, ct) => data.Item1), "LibraryImports.g.cs");
}

private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(AttributeData? suppressGCTransitionAttribute, AttributeData? unmanagedCallConvAttribute, AttributeData? defaultDllImportSearchPathsAttribute)
private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(AttributeData? suppressGCTransitionAttribute, AttributeData? unmanagedCallConvAttribute, AttributeData? defaultDllImportSearchPathsAttribute, AttributeData? wasmImportLinkageAttribute)
{
const string CallConvsField = "CallConvs";
// Manually rehydrate the forwarded attributes with fully qualified types so we don't have to worry about any using directives.
Expand Down Expand Up @@ -153,6 +153,10 @@ private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(Attrib
LiteralExpression(SyntaxKind.NumericLiteralExpression,
Literal((int)defaultDllImportSearchPathsAttribute.ConstructorArguments[0].Value!))))));
}
if (wasmImportLinkageAttribute is not null)
{
attributes.Add(Attribute(NameSyntaxes.WasmImportLinkageAttribute));
}
return attributes;
}

Expand Down Expand Up @@ -223,12 +227,14 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
INamedTypeSymbol? suppressGCTransitionAttrType = environment.SuppressGCTransitionAttrType;
INamedTypeSymbol? unmanagedCallConvAttrType = environment.UnmanagedCallConvAttrType;
INamedTypeSymbol? defaultDllImportSearchPathsAttrType = environment.DefaultDllImportSearchPathsAttrType;
INamedTypeSymbol? wasmImportLinkageAttrType = environment.WasmImportLinkageAttrType;
// Get any attributes of interest on the method
AttributeData? generatedDllImportAttr = null;
AttributeData? lcidConversionAttr = null;
AttributeData? suppressGCTransitionAttribute = null;
AttributeData? unmanagedCallConvAttribute = null;
AttributeData? defaultDllImportSearchPathsAttribute = null;
AttributeData? wasmImportLinkageAttribute = null;
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
foreach (AttributeData attr in symbol.GetAttributes())
{
if (attr.AttributeClass is not null
Expand All @@ -252,6 +258,10 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
{
defaultDllImportSearchPathsAttribute = attr;
}
else if (wasmImportLinkageAttrType is not null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, wasmImportLinkageAttrType))
{
wasmImportLinkageAttribute = attr;
}
}

Debug.Assert(generatedDllImportAttr is not null);
Expand Down Expand Up @@ -299,7 +309,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(

var methodSyntaxTemplate = new ContainingSyntax(originalSyntax.Modifiers, SyntaxKind.MethodDeclaration, originalSyntax.Identifier, originalSyntax.TypeParameterList);

List<AttributeSyntax> additionalAttributes = GenerateSyntaxForForwardedAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute, defaultDllImportSearchPathsAttribute);
List<AttributeSyntax> additionalAttributes = GenerateSyntaxForForwardedAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute, defaultDllImportSearchPathsAttribute, wasmImportLinkageAttribute);
return new IncrementalStubGenerationContext(
signatureContext,
containingTypeContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,19 @@ public INamedTypeSymbol? DefaultDllImportSearchPathsAttrType
return _defaultDllImportSearchPathsAttrType.Value;
}
}

private Optional<INamedTypeSymbol?> _wasmImportLinkageAttrType;
public INamedTypeSymbol? WasmImportLinkageAttrType
{
get
{
if (_wasmImportLinkageAttrType.HasValue)
{
return _wasmImportLinkageAttrType.Value;
}
_wasmImportLinkageAttrType = new Optional<INamedTypeSymbol?>(Compilation.GetTypeByMetadataName(TypeNames.WasmImportLinkageAttribute));
return _wasmImportLinkageAttrType.Value;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static class NameSyntaxes

private static NameSyntax? _UnmanagedCallersOnlyAttribute;
public static NameSyntax UnmanagedCallersOnlyAttribute => _UnmanagedCallersOnlyAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.UnmanagedCallersOnlyAttribute);

private static NameSyntax? _WasmImportLinkageAttribute;
public static NameSyntax WasmImportLinkageAttribute => _WasmImportLinkageAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.WasmImportLinkageAttribute);
}

public static class TypeSyntaxes
Expand Down Expand Up @@ -312,5 +315,6 @@ public static string MarshalEx(InteropGenerationOptions options)
public const string CallConvMemberFunctionName = "System.Runtime.CompilerServices.CallConvMemberFunction";
public const string Nint = "nint";
public const string ComVariantMarshaller = "System.Runtime.InteropServices.Marshalling.ComVariantMarshaller";
public const string WasmImportLinkageAttribute = "System.Runtime.InteropServices.WasmImportLinkageAttribute";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class AttributeForwarding
[Theory]
[InlineData("SuppressGCTransition", "System.Runtime.InteropServices.SuppressGCTransitionAttribute")]
[InlineData("UnmanagedCallConv", "System.Runtime.InteropServices.UnmanagedCallConvAttribute")]
[InlineData("WasmImportLinkage", "System.Runtime.InteropServices.WasmImportLinkageAttribute")]
public async Task KnownParameterlessAttribute(string attributeSourceName, string attributeMetadataName)
{
string source = $$"""
Expand Down
Loading