Skip to content

Commit

Permalink
Make marshaller types specific to each scenario (#89894)
Browse files Browse the repository at this point in the history
Make the marshaller types for the test interfaces only include the necessary methods for each scenario to catch any generated code that uses a method from another scenario
  • Loading branch information
jtschuster committed Aug 4, 2023
1 parent 3be370e commit d19a146
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,7 @@ private static bool UsesLastIndexMarshalled(TypePositionInfo info, StubCodeConte
{
return false;
}
bool usesLastIndexMarshalled = !shouldCleanupAllElements && !onlyUnmarshals;
return usesLastIndexMarshalled;
return true;
}

private static bool ShouldCleanUpAllElements(TypePositionInfo info, StubCodeContext context)
Expand All @@ -640,7 +639,7 @@ private static bool ShouldCleanUpAllElements(TypePositionInfo info, StubCodeCont
_ = context;
// AdditionalTemporaryStateLivesAcrossStages implies that it is an outer collection
// Out parameters means that the contents are created by the P/Invoke and assumed to have successfully created all elements
return !context.AdditionalTemporaryStateLivesAcrossStages || info.ByValueContentsMarshalKind == ByValueContentsMarshalKind.Out || info.RefKind == RefKind.Out;
return !context.AdditionalTemporaryStateLivesAcrossStages || info.ByValueContentsMarshalKind == ByValueContentsMarshalKind.Out || info.RefKind == RefKind.Out || info.IsNativeReturnPosition;
}

public override StatementSyntax GenerateSetupStatement(TypePositionInfo info, StubCodeContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,79 @@ internal struct StatefulFinallyNative
public int i;
}

[CustomMarshaller(typeof(StatefulFinallyType), MarshalMode.Default, typeof(StatefulFinallyTypeMarshaller))]
[CustomMarshaller(typeof(StatefulFinallyType), MarshalMode.ManagedToUnmanagedIn, typeof(ManagedToUnmanaged))]
[CustomMarshaller(typeof(StatefulFinallyType), MarshalMode.UnmanagedToManagedOut, typeof(ManagedToUnmanaged))]
[CustomMarshaller(typeof(StatefulFinallyType), MarshalMode.ManagedToUnmanagedOut, typeof(UnmanagedToManaged))]
[CustomMarshaller(typeof(StatefulFinallyType), MarshalMode.UnmanagedToManagedIn, typeof(UnmanagedToManaged))]
[CustomMarshaller(typeof(StatefulFinallyType), MarshalMode.UnmanagedToManagedRef, typeof(Bidirectional))]
[CustomMarshaller(typeof(StatefulFinallyType), MarshalMode.ManagedToUnmanagedRef, typeof(Bidirectional))]
internal struct StatefulFinallyTypeMarshaller
{
int managed_i;
int unmanaged_i;
public void FromManaged(StatefulFinallyType managed)
internal struct Bidirectional
{
managed_i = managed.i;
}
int managed_i;
int unmanaged_i;

public StatefulFinallyNative ToUnmanaged()
{
return new StatefulFinallyNative() { i = this.managed_i };
}
public void FromManaged(StatefulFinallyType managed)
{
managed_i = managed.i;
}

public void FromUnmanaged(StatefulFinallyNative unmanaged)
{
unmanaged_i = unmanaged.i;
public StatefulFinallyNative ToUnmanaged()
{
return new StatefulFinallyNative() { i = this.managed_i };
}

public void FromUnmanaged(StatefulFinallyNative unmanaged)
{
unmanaged_i = unmanaged.i;
}

public StatefulFinallyType ToManagedFinally()
{
return new StatefulFinallyType() { i = unmanaged_i };
}

public void Free() { }

public void OnInvoked() { }
}

public StatefulFinallyType ToManagedFinally()
internal struct ManagedToUnmanaged
{
return new StatefulFinallyType() { i = unmanaged_i };
int managed_i;

public void FromManaged(StatefulFinallyType managed)
{
managed_i = managed.i;
}

public StatefulFinallyNative ToUnmanaged()
{
return new StatefulFinallyNative() { i = this.managed_i };
}

public void Free() { }

public void OnInvoked() { }
}

public void Free()
internal struct UnmanagedToManaged
{
int unmanaged_i;

public void FromUnmanaged(StatefulFinallyNative unmanaged)
{
unmanaged_i = unmanaged.i;
}
public StatefulFinallyType ToManagedFinally()
{
return new StatefulFinallyType() { i = unmanaged_i };
}

public void Free() { }

public void OnInvoked() { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,82 @@ internal class StatefulType
{
}

[CustomMarshaller(typeof(StatefulType), MarshalMode.Default, typeof(StatefulTypeMarshaller))]
[CustomMarshaller(typeof(StatefulType), MarshalMode.ManagedToUnmanagedIn, typeof(ManagedToUnmanaged))]
[CustomMarshaller(typeof(StatefulType), MarshalMode.UnmanagedToManagedOut, typeof(ManagedToUnmanaged))]
[CustomMarshaller(typeof(StatefulType), MarshalMode.ManagedToUnmanagedOut, typeof(UnmanagedToManaged))]
[CustomMarshaller(typeof(StatefulType), MarshalMode.UnmanagedToManagedIn, typeof(UnmanagedToManaged))]
[CustomMarshaller(typeof(StatefulType), MarshalMode.UnmanagedToManagedRef, typeof(Bidirectional))]
[CustomMarshaller(typeof(StatefulType), MarshalMode.ManagedToUnmanagedRef, typeof(Bidirectional))]
internal struct StatefulTypeMarshaller
{
public void FromManaged(StatefulType managed)
internal struct Bidirectional
{
throw new System.NotImplementedException();
}
public void FromManaged(StatefulType managed)
{
throw new System.NotImplementedException();
}

public nint ToUnmanaged()
{
throw new System.NotImplementedException();
}
public nint ToUnmanaged()
{
throw new System.NotImplementedException();
}

public void FromUnmanaged(nint unmanaged)
{
throw new System.NotImplementedException();
public void FromUnmanaged(nint unmanaged)
{
throw new System.NotImplementedException();
}

public StatefulType ToManaged()
{
throw new System.NotImplementedException();
}

public void Free()
{
throw new System.NotImplementedException();
}

public void OnInvoked() { }
}

public StatefulType ToManaged()
internal struct ManagedToUnmanaged
{
throw new System.NotImplementedException();
public void FromManaged(StatefulType managed)
{
throw new System.NotImplementedException();
}

public nint ToUnmanaged()
{
throw new System.NotImplementedException();
}

public void Free()
{
throw new System.NotImplementedException();
}

public void OnInvoked() { }
}

public void Free()
internal struct UnmanagedToManaged
{
throw new System.NotImplementedException();
}
public void FromUnmanaged(nint unmanaged)
{
throw new System.NotImplementedException();
}

public void OnInvoked() { }
public StatefulType ToManaged()
{
throw new System.NotImplementedException();
}

public void Free()
{
throw new System.NotImplementedException();
}

public void OnInvoked() { }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace SharedTypes.ComInterfaces
{
[GeneratedComInterface(), Guid("0A52B77C-E08B-4274-A1F4-1A2BF2C07E60")]
partial interface IStatelessCollectionStatelessElement
internal partial interface IStatelessCollectionStatelessElement
{
void Method(
[MarshalUsing(CountElementName = nameof(size))] StatelessCollection<StatelessType> p,
Expand Down Expand Up @@ -38,43 +39,103 @@ internal class StatelessCollection<T>
{
}

internal struct NativeCollection<T>
{

}

[ContiguousCollectionMarshaller]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.Default, typeof(StatelessCollectionMarshaller<,>.Default))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.ManagedToUnmanagedIn, typeof(StatelessCollectionMarshaller<,>.ManagedToUnmanaged))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.UnmanagedToManagedOut, typeof(StatelessCollectionMarshaller<,>.ManagedToUnmanaged))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.ElementIn, typeof(StatelessCollectionMarshaller<,>.ManagedToUnmanaged))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.ManagedToUnmanagedOut, typeof(StatelessCollectionMarshaller<,>.UnmanagedToManaged))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.UnmanagedToManagedIn, typeof(StatelessCollectionMarshaller<,>.UnmanagedToManaged))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.ElementOut, typeof(StatelessCollectionMarshaller<,>.UnmanagedToManaged))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.UnmanagedToManagedRef, typeof(StatelessCollectionMarshaller<,>.Bidirectional))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.ManagedToUnmanagedRef, typeof(StatelessCollectionMarshaller<,>.Bidirectional))]
[CustomMarshaller(typeof(StatelessCollection<>), MarshalMode.ElementRef, typeof(StatelessCollectionMarshaller<,>.Bidirectional))]
internal static unsafe class StatelessCollectionMarshaller<T, TUnmanagedElement> where TUnmanagedElement : unmanaged
{
internal static class Default
internal static class Bidirectional
{
public static NativeCollection<T> AllocateContainerForUnmanagedElements(StatelessCollection<T> managed, out int numElements)
{
throw new NotImplementedException();
}

public static StatelessCollection<T> AllocateContainerForManagedElements(NativeCollection<T> unmanaged, int numElements)
{
throw new NotImplementedException();
}

public static ReadOnlySpan<T> GetManagedValuesSource(StatelessCollection<T> managed)
{
throw new NotImplementedException();
}

public static Span<TUnmanagedElement> GetUnmanagedValuesDestination(NativeCollection<T> unmanaged, int numElements)
{
throw new NotImplementedException();
}

public static ReadOnlySpan<TUnmanagedElement> GetUnmanagedValuesSource(NativeCollection<T> unmanaged, int numElements)
{
throw new NotImplementedException();
}

public static Span<T> GetManagedValuesDestination(StatelessCollection<T> managed)
{
throw new NotImplementedException();
}

public static void Free(NativeCollection<T> unmanaged) { }
}

internal static class ManagedToUnmanaged
{
public static nint AllocateContainerForUnmanagedElements(StatelessCollection<T> managed, out int numElements)
public static NativeCollection<T> AllocateContainerForUnmanagedElements(StatelessCollection<T> managed, out int numElements)
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

public static StatelessCollection<T> AllocateContainerForManagedElements(nint unmanaged, int numElements)
public static ReadOnlySpan<T> GetManagedValuesSource(StatelessCollection<T> managed)
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

public static System.ReadOnlySpan<T> GetManagedValuesSource(StatelessCollection<T> managed)
public static Span<TUnmanagedElement> GetUnmanagedValuesDestination(NativeCollection<T> unmanaged, int numElements)
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

public static System.Span<TUnmanagedElement> GetUnmanagedValuesDestination(nint unmanaged, int numElements)
public static void Free(NativeCollection<T> unmanaged) => throw new NotImplementedException();

}

internal static class UnmanagedToManaged
{
public static StatelessCollection<T> AllocateContainerForManagedElements(NativeCollection<T> unmanaged, int numElements)
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

public static System.ReadOnlySpan<TUnmanagedElement> GetUnmanagedValuesSource(nint unmanaged, int numElements)
public static ReadOnlySpan<TUnmanagedElement> GetUnmanagedValuesSource(NativeCollection<T> unmanaged, int numElements)
{
throw new NotImplementedException();
}
// Should be removed: https://github.com/dotnet/runtime/issues/89885
public static Span<TUnmanagedElement> GetUnmanagedValuesDestination(NativeCollection<T> unmanaged, int numElements)
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

public static System.Span<T> GetManagedValuesDestination(StatelessCollection<T> managed)
public static Span<T> GetManagedValuesDestination(StatelessCollection<T> managed)
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

public static void Free(nint unmanaged) { }
public static void Free(NativeCollection<T> unmanaged) => throw new NotImplementedException();

}
}
}
Loading

0 comments on commit d19a146

Please sign in to comment.