-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Prepare ComInterfaceGenerator for going into the public API #83894
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.