-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare ComInterfaceGenerator for going into the public API (#83894)
- Loading branch information
1 parent
e6226e6
commit b2f421f
Showing
15 changed files
with
167 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...opServices/gen/ComInterfaceGenerator/Marshallers/ComInterfaceDispatchMarshallerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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.Text; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace Microsoft.Interop | ||
{ | ||
internal sealed record ComInterfaceDispatchMarshallingInfo : MarshallingInfo | ||
{ | ||
public static readonly ComInterfaceDispatchMarshallingInfo Instance = new(); | ||
} | ||
|
||
internal sealed class ComInterfaceDispatchMarshallerFactory : IMarshallingGeneratorFactory | ||
{ | ||
private readonly IMarshallingGeneratorFactory _inner; | ||
public ComInterfaceDispatchMarshallerFactory(IMarshallingGeneratorFactory inner) | ||
{ | ||
_inner = inner; | ||
} | ||
|
||
public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context) | ||
=> info.MarshallingAttributeInfo is ComInterfaceDispatchMarshallingInfo ? new Marshaller() : _inner.Create(info, context); | ||
|
||
private sealed class Marshaller : IMarshallingGenerator | ||
{ | ||
public ManagedTypeInfo AsNativeType(TypePositionInfo info) => | ||
new PointerTypeInfo( | ||
$"{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", | ||
$"{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", | ||
IsFunctionPointer: false); | ||
public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeContext context) | ||
{ | ||
if (context.CurrentStage != StubCodeContext.Stage.Unmarshal) | ||
{ | ||
yield break; | ||
} | ||
|
||
var (managed, native) = context.GetIdentifiers(info); | ||
|
||
// <managed> = ComWrappers.ComInterfaceDispatch.GetInstance<<managedType>>(<native>); | ||
yield return ExpressionStatement( | ||
AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, | ||
IdentifierName(managed), | ||
InvocationExpression( | ||
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, | ||
ParseName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch), | ||
GenericName( | ||
Identifier("GetInstance"), | ||
TypeArgumentList(SingletonSeparatedList(info.ManagedType.Syntax)))), | ||
ArgumentList( | ||
SingletonSeparatedList( | ||
Argument( | ||
IdentifierName(native))))))); | ||
} | ||
|
||
public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => SignatureBehavior.NativeType; | ||
public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context) => ValueBoundaryBehavior.NativeIdentifier; | ||
public bool IsSupported(TargetFramework target, Version version) | ||
=> target == TargetFramework.Net && version >= new Version(5, 0); | ||
public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => false; | ||
public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...stem.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGeneratorHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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 Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Interop | ||
{ | ||
internal static class VtableIndexStubGeneratorHelpers | ||
{ | ||
public static MarshallingGeneratorFactoryKey<(TargetFramework, Version)> CreateGeneratorFactory(StubEnvironment env, MarshalDirection direction) | ||
{ | ||
IMarshallingGeneratorFactory generatorFactory; | ||
|
||
// If we're in a "supported" scenario, then emit a diagnostic as our final fallback. | ||
generatorFactory = new UnsupportedMarshallingFactory(); | ||
|
||
generatorFactory = new NoMarshallingInfoErrorMarshallingFactory(generatorFactory); | ||
|
||
// The presence of System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute is tied to TFM, | ||
// so we use TFM in the generator factory key instead of the Compilation as the compilation changes on every keystroke. | ||
IAssemblySymbol coreLibraryAssembly = env.Compilation.GetSpecialType(SpecialType.System_Object).ContainingAssembly; | ||
ITypeSymbol? disabledRuntimeMarshallingAttributeType = coreLibraryAssembly.GetTypeByMetadataName(TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute); | ||
bool runtimeMarshallingDisabled = disabledRuntimeMarshallingAttributeType is not null | ||
&& env.Compilation.Assembly.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, disabledRuntimeMarshallingAttributeType)); | ||
|
||
// Since the char type can go into the P/Invoke signature here, we can only use it when | ||
// runtime marshalling is disabled. | ||
generatorFactory = new CharMarshallingGeneratorFactory(generatorFactory, useBlittableMarshallerForUtf16: runtimeMarshallingDisabled); | ||
|
||
InteropGenerationOptions interopGenerationOptions = new(UseMarshalType: true); | ||
generatorFactory = new MarshalAsMarshallingGeneratorFactory(interopGenerationOptions, generatorFactory); | ||
|
||
IMarshallingGeneratorFactory elementFactory = new AttributedMarshallingModelGeneratorFactory( | ||
// Since the char type in an array will not be part of the P/Invoke signature, we can | ||
// use the regular blittable marshaller in all cases. | ||
new CharMarshallingGeneratorFactory(generatorFactory, useBlittableMarshallerForUtf16: true), | ||
new AttributedMarshallingModelOptions(runtimeMarshallingDisabled, MarshalMode.ElementIn, MarshalMode.ElementRef, MarshalMode.ElementOut)); | ||
// We don't need to include the later generator factories for collection elements | ||
// as the later generator factories only apply to parameters. | ||
generatorFactory = new AttributedMarshallingModelGeneratorFactory( | ||
generatorFactory, | ||
elementFactory, | ||
new AttributedMarshallingModelOptions( | ||
runtimeMarshallingDisabled, | ||
direction == MarshalDirection.ManagedToUnmanaged | ||
? MarshalMode.ManagedToUnmanagedIn | ||
: MarshalMode.UnmanagedToManagedOut, | ||
direction == MarshalDirection.ManagedToUnmanaged | ||
? MarshalMode.ManagedToUnmanagedRef | ||
: MarshalMode.UnmanagedToManagedRef, | ||
direction == MarshalDirection.ManagedToUnmanaged | ||
? MarshalMode.ManagedToUnmanagedOut | ||
: MarshalMode.UnmanagedToManagedIn)); | ||
|
||
generatorFactory = new ObjectUnwrapperMarshallerFactory(generatorFactory); | ||
|
||
generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory); | ||
|
||
return MarshallingGeneratorFactoryKey.Create((env.TargetFramework, env.TargetFrameworkVersion), generatorFactory); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.