Skip to content

Commit

Permalink
Update to use CustomMarshaller entry point type (dotnet#71398)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Jun 28, 2022
1 parent 333742b commit 6c10d0e
Show file tree
Hide file tree
Showing 22 changed files with 313 additions and 186 deletions.
4 changes: 2 additions & 2 deletions eng/generators.targets
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Common/src/Interop/Interop.Ldap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
{
Expand All @@ -455,6 +453,7 @@ public static void AnalyzeElement(SymbolAnalysisContext context)
{
return;
}

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

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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())
Expand Down
Loading

0 comments on commit 6c10d0e

Please sign in to comment.