Skip to content

Commit

Permalink
Add forwarding support for WasmLinkage on LibraryImport (#109234)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky authored Oct 29, 2024
1 parent ece807a commit fd77ab7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
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;
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

0 comments on commit fd77ab7

Please sign in to comment.