Skip to content

Commit

Permalink
Use MarshalDirection type in generators and update design doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky committed Aug 25, 2022
1 parent a958552 commit 7182f4b
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 72 deletions.
2 changes: 1 addition & 1 deletion docs/design/libraries/ComInterfaceGenerator/VTableStubs.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class VirtualMethodIndexAttribute : Attribute

public bool ImplicitThisParameter { get; set; } = true;

public CustomTypeMarshallerDirection Direction { get; set; } = CustomTypeMarshallerDirection.Ref;
public MarshalDirection Direction { get; set; } = MarshalDirection.Bidirectional;

/// <summary>
/// Gets or sets how to marshal string arguments to the method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal sealed record JSImportCodeContext : JSStubCodeContext
public JSImportCodeContext(JSImportData attributeData, StubCodeContext inner)
{
_inner = inner;
Direction = CustomTypeMarshallingDirection.In;
Direction = MarshalDirection.ManagedToUnmanaged;
AttributeData = attributeData;
}

Expand All @@ -37,7 +37,7 @@ internal sealed record JSExportCodeContext : JSStubCodeContext
public JSExportCodeContext(JSExportData attributeData, StubCodeContext inner)
{
_inner = inner;
Direction = CustomTypeMarshallingDirection.Out;
Direction = MarshalDirection.UnmanagedToManaged;
AttributeData = attributeData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public override IEnumerable<StatementSyntax> Generate(TypePositionInfo info, Stu
.Select(a => ParseTypeName(a.FullTypeName))
.ToArray();

if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.In && info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.ManagedToUnmanaged && info.IsManagedReturnPosition)
{
yield return ToManagedMethod(target, source, jsty);
}

if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == CustomTypeMarshallingDirection.Out && info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == MarshalDirection.UnmanagedToManaged && info.IsManagedReturnPosition)
{
yield return ToJSMethod(target, source, jsty);
}
Expand All @@ -68,12 +68,12 @@ public override IEnumerable<StatementSyntax> Generate(TypePositionInfo info, Stu
yield return x;
}

if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == CustomTypeMarshallingDirection.In && !info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == MarshalDirection.ManagedToUnmanaged && !info.IsManagedReturnPosition)
{
yield return ToJSMethod(target, source, jsty);
}

if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.Out && !info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.UnmanagedToManaged && !info.IsManagedReturnPosition)
{
yield return ToManagedMethod(target, source, jsty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public override IEnumerable<StatementSyntax> Generate(TypePositionInfo info, Stu
? Argument(IdentifierName(context.GetIdentifiers(info).native))
: _inner.AsArgument(info, context);

if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.In && info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.ManagedToUnmanaged && info.IsManagedReturnPosition)
{
yield return ToManagedMethod(target, source);
}

if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == CustomTypeMarshallingDirection.Out && info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == MarshalDirection.UnmanagedToManaged && info.IsManagedReturnPosition)
{
yield return ToJSMethod(target, source);
}
Expand All @@ -47,12 +47,12 @@ public override IEnumerable<StatementSyntax> Generate(TypePositionInfo info, Stu
yield return x;
}

if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == CustomTypeMarshallingDirection.In && !info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == MarshalDirection.ManagedToUnmanaged && !info.IsManagedReturnPosition)
{
yield return ToJSMethod(target, source);
}

if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.Out && !info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.UnmanagedToManaged && !info.IsManagedReturnPosition)
{
yield return ToManagedMethod(target, source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public override IEnumerable<StatementSyntax> Generate(TypePositionInfo info, Stu
? Argument(IdentifierName(context.GetIdentifiers(info).native))
: _inner.AsArgument(info, context);

if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.In && info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.ManagedToUnmanaged && info.IsManagedReturnPosition)
{
yield return jsty.ResultTypeInfo.FullTypeName == "void"
? ToManagedMethodVoid(target, source)
: ToManagedMethod(target, source, jsty.ResultTypeInfo.Syntax);
}

if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == CustomTypeMarshallingDirection.Out && info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == MarshalDirection.UnmanagedToManaged && info.IsManagedReturnPosition)
{
yield return jsty.ResultTypeInfo.FullTypeName == "void"
? ToJSMethodVoid(target, source)
Expand All @@ -64,14 +64,14 @@ public override IEnumerable<StatementSyntax> Generate(TypePositionInfo info, Stu
yield return x;
}

if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == CustomTypeMarshallingDirection.In && !info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == MarshalDirection.ManagedToUnmanaged && !info.IsManagedReturnPosition)
{
yield return jsty.ResultTypeInfo.FullTypeName == "void"
? ToJSMethodVoid(target, source)
: ToJSMethod(target, source, jsty.ResultTypeInfo.Syntax);
}

if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.Out && !info.IsManagedReturnPosition)
if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.UnmanagedToManaged && !info.IsManagedReturnPosition)
{
yield return jsty.ResultTypeInfo.FullTypeName == "void"
? ToManagedMethodVoid(target, source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)SourceGenerators\LiveReferencePack.cs" Link="Common\SourceGenerators\LiveReferencePack.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\tests\LibraryImportGenerator.UnitTests\TestUtils.cs" Link="LibraryImportGenerator\TestUtils.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\tests\Common\TestUtils.cs" Link="System\Runtime\InteropServices\Common\TestUtils.cs" />
<Compile Include="CodeSnippets.cs" />
<Compile Include="Fails.cs" />
<Compile Include="Compiles.cs" />
Expand All @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="$(CompilerPlatformTestingVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="$(CompilerPlatformTestingVersion)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\tests\Ancillary.Interop\Ancillary.Interop.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\tests\Ancillary.Interop\Ancillary.Interop.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\gen\JSImportGenerator\JSImportGenerator.csproj" />
<None Include="$(RepoRoot)/NuGet.config" Link="NuGet.config" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<Compile Include="../../tests/Ancillary.Interop/MarshalDirection.cs" />
</ItemGroup>

<ItemGroup>
<None Include="$(TargetPath)" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(AssemblyName).props" Pack="true" PackagePath="build" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// 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;
using Microsoft.CodeAnalysis;

namespace Microsoft.Interop
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<Compile Include="$(CoreLibSharedDir)\System\Runtime\InteropServices\Marshalling\MarshalMode.cs" Link="Production\MarshalMode.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="../../tests/Common/MarshalDirection.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion_4_4)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ public enum Stage
/// </summary>
public Stage CurrentStage { get; init; } = Stage.Invalid;

/// <summary>
/// <c>CustomTypeMarshallingDirection.In</c> means method import like <c>[LibraryImport]</c>.
/// <c>CustomTypeMarshallingDirection.Out</c> means method export like in <c>[UnmanagedCallersOnly]</c> or in <c>[JSExport]</c>
/// </summary>
public CustomTypeMarshallingDirection Direction { get; init; } = CustomTypeMarshallingDirection.In;
public MarshalDirection Direction { get; init; } = MarshalDirection.ManagedToUnmanaged;

/// <summary>
/// Gets the currently targeted framework and version for stub code generation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Description>APIs required for usage of the LibraryImportGenerator and related tools.</Description>
<DefineConstants>$(DefineConstants);LIBRARYIMPORT_GENERATOR_TEST</DefineConstants>
<DefineConstants>$(DefineConstants);ANCILLARY_INTEROP</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="../Common/MarshalDirection.cs" Link="Common/MarshalDirection.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#if ANCILLARY_INTEROP
namespace System.Runtime.InteropServices.Marshalling
#else
namespace Microsoft.Interop
#endif
{
public enum MarshalDirection
{
Expand Down

0 comments on commit 7182f4b

Please sign in to comment.