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

Update to use CustomMarshaller entry point type #71398

Merged
merged 6 commits into from
Jun 28, 2022
Merged
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
4 changes: 2 additions & 2 deletions eng/generators.targets
Original file line number Diff line number Diff line change
@@ -69,8 +69,8 @@
</PropertyGroup>

<ItemGroup Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)' and '$(IncludeLibraryImportGeneratorSources)' != 'false'">
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices/tests/Ancillary.Interop/CustomTypeMarshallersAttributeBase.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices/tests/Ancillary.Interop/ManagedToUnmanagedMarshallersAttribute.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices/tests/Ancillary.Interop/CustomMarshallerAttribute.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices/tests/Ancillary.Interop/Scenario.cs" />
</ItemGroup>
</Target>

2 changes: 1 addition & 1 deletion src/libraries/Common/src/Interop/Interop.Ldap.cs
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ internal struct SEC_WINNT_AUTH_IDENTITY_EX
public int packageListLength;

#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(SEC_WINNT_AUTH_IDENTITY_EX), InMarshaller = typeof(Marshaller))]
[CustomMarshaller(typeof(SEC_WINNT_AUTH_IDENTITY_EX), Scenario.ManagedToUnmanagedIn, typeof(Marshaller))]
internal static class Marshaller
{
public static Native ConvertToUnmanaged(SEC_WINNT_AUTH_IDENTITY_EX managed)
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ internal struct CRYPTUI_VIEWCERTIFICATE_STRUCTW
internal uint nStartPage;

#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW), InMarshaller = typeof(Marshaller), RefMarshaller = typeof(Marshaller), OutMarshaller = typeof(Marshaller))]
[CustomMarshaller(typeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW), Scenario.Default, typeof(Marshaller))]
public static class Marshaller
{
public static Native ConvertToUnmanaged(CRYPTUI_VIEWCERTIFICATE_STRUCTW managed) => new(managed);
@@ -152,7 +152,7 @@ internal struct CRYPTUI_SELECTCERTIFICATE_STRUCTW
internal IntPtr hSelectedCertStore;

#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(CRYPTUI_SELECTCERTIFICATE_STRUCTW), InMarshaller = typeof(Marshaller), RefMarshaller = typeof(Marshaller), OutMarshaller = typeof(Marshaller))]
[CustomMarshaller(typeof(CRYPTUI_SELECTCERTIFICATE_STRUCTW), Scenario.Default, typeof(Marshaller))]
public static class Marshaller
{
public static Native ConvertToUnmanaged(CRYPTUI_SELECTCERTIFICATE_STRUCTW managed) => new(managed);
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ public static partial bool WinHttpAddRequestHeaders(
uint modifiers);

#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(StringBuilder), InMarshaller = typeof(SimpleStringBufferMarshaller))]
[CustomMarshaller(typeof(StringBuilder), Scenario.ManagedToUnmanagedIn, typeof(SimpleStringBufferMarshaller))]
private static unsafe class SimpleStringBufferMarshaller
{
public static void* ConvertToUnmanaged(StringBuilder builder)
Original file line number Diff line number Diff line change
@@ -262,7 +262,7 @@ public struct WINHTTP_AUTOPROXY_OPTIONS
[MarshalAs(UnmanagedType.Bool)]
public bool AutoLoginIfChallenged;
#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(WINHTTP_AUTOPROXY_OPTIONS), InMarshaller = typeof(Marshaller), RefMarshaller = typeof(Marshaller), OutMarshaller = typeof(Marshaller))]
[CustomMarshaller(typeof(WINHTTP_AUTOPROXY_OPTIONS), Scenario.Default, typeof(Marshaller))]
public static class Marshaller
{
public static Native ConvertToUnmanaged(WINHTTP_AUTOPROXY_OPTIONS managed) => new(managed);
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ internal struct WAVEOUTCAPS
private ushort wReserved1;
private ushort dwSupport;
#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(WAVEOUTCAPS), InMarshaller = typeof(Marshaller), RefMarshaller = typeof(Marshaller), OutMarshaller = typeof(Marshaller))]
[CustomMarshaller(typeof(WAVEOUTCAPS), Scenario.Default, typeof(Marshaller))]
public static class Marshaller
{
public static Native ConvertToUnmanaged(WAVEOUTCAPS managed) => new(managed);
Original file line number Diff line number Diff line change
@@ -74,7 +74,8 @@ internal struct IPv6MulticastRequest
{
internal byte[] MulticastAddress; // IP address of group.
internal int InterfaceIndex; // Local interface index.
[ManagedToUnmanagedMarshallers(typeof(IPv6MulticastRequest), InMarshaller = typeof(Marshaller), RefMarshaller = typeof(Marshaller), OutMarshaller = typeof(Marshaller))]

[CustomMarshaller(typeof(IPv6MulticastRequest), Scenario.Default, typeof(Marshaller))]
public static class Marshaller
{
public static Native ConvertToUnmanaged(IPv6MulticastRequest managed) => new(managed);
Original file line number Diff line number Diff line change
@@ -703,7 +703,7 @@ internal struct EvtStringVariant
public uint Type;

#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(EvtStringVariant), InMarshaller = typeof(Marshaller), RefMarshaller = typeof(Marshaller), OutMarshaller = typeof(Marshaller))]
[CustomMarshaller(typeof(EvtStringVariant), Scenario.Default, typeof(Marshaller))]
public static class Marshaller
{
public static Native ConvertToUnmanaged(EvtStringVariant managed) => new(managed);
Original file line number Diff line number Diff line change
@@ -188,7 +188,7 @@ internal sealed class DOCINFO
internal int fwType;

#if NET7_0_OR_GREATER
[ManagedToUnmanagedMarshallers(typeof(DOCINFO), InMarshaller = typeof(Marshaller))]
[CustomMarshaller(typeof(DOCINFO), Scenario.ManagedToUnmanagedIn, typeof(Marshaller))]
public static class Marshaller
{
public static Native ConvertToUnmanaged(DOCINFO managed) => new(managed);
Original file line number Diff line number Diff line change
@@ -405,7 +405,7 @@ private void PrepareForAnalysis(CompilationStartAnalysisContext context)

// Analyze NativeMarshalling/MarshalUsing for correctness
context.RegisterSymbolAction(PerCompilationAnalyzer.AnalyzeTypeDefinition, SymbolKind.NamedType);
context.RegisterSymbolAction(PerCompilationAnalyzer.AnalyzeElement, SymbolKind.Parameter, SymbolKind.Field);
context.RegisterSymbolAction(PerCompilationAnalyzer.AnalyzeParameterOrField, SymbolKind.Parameter, SymbolKind.Field);
context.RegisterSymbolAction(PerCompilationAnalyzer.AnalyzeReturnType, SymbolKind.Method);

// Analyze marshaller type to validate shape.
@@ -431,18 +431,16 @@ public PerCompilationAnalyzer(INamedTypeSymbol spanOfT, INamedTypeSymbol? spanOf
public static void AnalyzeTypeDefinition(SymbolAnalysisContext context)
{
INamedTypeSymbol type = (INamedTypeSymbol)context.Symbol;

(AttributeData? attributeData, INamedTypeSymbol? marshallerType) = ManualTypeMarshallingHelper.GetDefaultMarshallerInfo(type);

(AttributeData? attributeData, INamedTypeSymbol? entryType) = ManualTypeMarshallingHelper.GetDefaultMarshallerEntryType(type);
if (attributeData is null)
{
return;
}

AnalyzeManagedTypeMarshallingInfo(context, type, attributeData, marshallerType);
AnalyzeManagedTypeMarshallingInfo(context, type, attributeData, entryType);
}

public static void AnalyzeElement(SymbolAnalysisContext context)
public static void AnalyzeParameterOrField(SymbolAnalysisContext context)
{
ITypeSymbol managedType = context.Symbol switch
{
@@ -455,6 +453,7 @@ public static void AnalyzeElement(SymbolAnalysisContext context)
{
return;
}

AnalyzeManagedTypeMarshallingInfo(context, managedType, attributeData, attributeData.ConstructorArguments[0].Value as INamedTypeSymbol);
}

@@ -467,39 +466,44 @@ public static void AnalyzeReturnType(SymbolAnalysisContext context)
{
return;
}

AnalyzeManagedTypeMarshallingInfo(context, managedType, attributeData, attributeData.ConstructorArguments[0].Value as INamedTypeSymbol);
}

private static void AnalyzeManagedTypeMarshallingInfo(SymbolAnalysisContext context, ITypeSymbol type, AttributeData attributeData, INamedTypeSymbol? marshallerType)
private static void AnalyzeManagedTypeMarshallingInfo(
SymbolAnalysisContext context,
ITypeSymbol managedType,
AttributeData attributeData,
INamedTypeSymbol? entryType)
{
if (marshallerType is null)
if (entryType is null)
{
context.ReportDiagnostic(
attributeData.CreateDiagnostic(
NativeTypeMustHaveCustomTypeMarshallerAttributeRule,
type.ToDisplayString()));
managedType.ToDisplayString()));
return;
}

if (marshallerType.IsUnboundGenericType)
if (entryType.IsUnboundGenericType)
{
context.ReportDiagnostic(
attributeData.CreateDiagnostic(
NativeGenericTypeMustBeClosedOrMatchArityRule,
marshallerType.ToDisplayString(),
type.ToDisplayString()));
entryType.ToDisplayString(),
managedType.ToDisplayString()));
}

(bool hasCustomTypeMarshallerAttribute, ITypeSymbol? marshallerManagedType, _) = ManualTypeMarshallingHelper_V1.GetMarshallerShapeInfo(marshallerType);
(bool hasCustomTypeMarshallerAttribute, ITypeSymbol? marshallerManagedType, _) = ManualTypeMarshallingHelper_V1.GetMarshallerShapeInfo(entryType);

marshallerManagedType = ManualTypeMarshallingHelper.ResolveManagedType(marshallerManagedType, marshallerType, context.Compilation);
marshallerManagedType = ManualTypeMarshallingHelper.ResolveManagedType(marshallerManagedType, entryType, context.Compilation);

if (!hasCustomTypeMarshallerAttribute)
{
context.ReportDiagnostic(
attributeData.CreateDiagnostic(
NativeTypeMustHaveCustomTypeMarshallerAttributeRule,
type.ToDisplayString()));
managedType.ToDisplayString()));
return;
}

@@ -508,16 +512,16 @@ private static void AnalyzeManagedTypeMarshallingInfo(SymbolAnalysisContext cont
context.ReportDiagnostic(
attributeData.CreateDiagnostic(
NativeTypeMustHaveCustomTypeMarshallerAttributeRule,
type.ToDisplayString()));
managedType.ToDisplayString()));
return;
}

if (!TypeSymbolsConstructedFromEqualTypes(type, marshallerManagedType))
if (!TypeSymbolsConstructedFromEqualTypes(managedType, marshallerManagedType))
{
context.ReportDiagnostic(
attributeData.CreateDiagnostic(
NativeTypeMustHaveCustomTypeMarshallerAttributeRule,
type.ToDisplayString()));
managedType.ToDisplayString()));
return;
}
}
@@ -846,7 +850,7 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
type.ToDisplayString()));
}

if (SymbolEqualityComparer.Default.Equals(ManualTypeMarshallingHelper.GetDefaultMarshallerInfo(type).marshallerType, marshallerType)
if (SymbolEqualityComparer.Default.Equals(ManualTypeMarshallingHelper.GetDefaultMarshallerEntryType(type).entryType, marshallerType)
&& ManualTypeMarshallingHelper.FindGetPinnableReference(type) is IMethodSymbol managedGetPinnableReferenceMethod)
{
if (!managedGetPinnableReferenceMethod.ReturnType.IsConsideredBlittable())
Loading