-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add new proposed API to Ancillary.Interop #81063
Changes from 23 commits
51539a7
3113a20
0f061fd
e51c0d8
4b936c7
d2cecc6
1403ae8
b4205b4
046b9d1
65012f8
2cddf1c
53baeeb
0d15662
f4afef5
1599274
3bb44a1
8a4d125
6d0198a
e5b10e2
e4529db
0356350
9be677a
b643733
c4f4cc0
d63a2da
8cd216a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace Microsoft.Interop | ||
{ | ||
internal static class InlinedTypes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this is just prep for later? I don't see it used yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is unused but may be needed later. |
||
{ | ||
/// <summary> | ||
/// Returns the ClassDeclarationSyntax for: | ||
/// <code> | ||
/// public sealed unsafe class ComWrappersUnwrapper : IUnmanagedObjectUnwrapper | ||
/// { | ||
/// public static object GetObjectForUnmanagedWrapper(void* ptr) | ||
/// { | ||
/// return ComWrappers.ComInterfaceDispatch.GetInstance<object>((ComWrappers.ComInterfaceDispatch*)ptr); | ||
/// } | ||
/// } | ||
/// </code> | ||
/// </summary> | ||
public static ClassDeclarationSyntax ComWrappersUnwrapper { get; } | ||
|
||
public static ClassDeclarationSyntax GetComWrappersUnwrapper() | ||
{ | ||
return ClassDeclaration("ComWrappersUnwrapper") | ||
.AddModifiers(Token(SyntaxKind.SealedKeyword), | ||
Token(SyntaxKind.UnsafeKeyword), | ||
Token(SyntaxKind.StaticKeyword), | ||
Token(SyntaxKind.FileKeyword)) | ||
.AddMembers( | ||
MethodDeclaration( | ||
PredefinedType(Token(SyntaxKind.ObjectKeyword)), | ||
Identifier("GetComObjectForUnmanagedWrapper")) | ||
.AddModifiers(Token(SyntaxKind.PublicKeyword), | ||
Token(SyntaxKind.StaticKeyword)) | ||
.AddParameterListParameters( | ||
Parameter(Identifier("ptr")) | ||
.WithType(PointerType(PredefinedType(Token(SyntaxKind.VoidKeyword))))) | ||
.WithBody(body: Body())); | ||
|
||
static BlockSyntax Body() | ||
{ | ||
var invocation = InvocationExpression( | ||
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, | ||
MemberAccessExpression( | ||
SyntaxKind.SimpleMemberAccessExpression, | ||
IdentifierName("ComWrappers"), | ||
IdentifierName("ComInterfaceDispatch")), | ||
GenericName( | ||
Identifier("GetInstance"), | ||
TypeArgumentList( | ||
SeparatedList<SyntaxNode>( | ||
new[] { PredefinedType(Token(SyntaxKind.ObjectKeyword)) }))))) | ||
.AddArgumentListArguments( | ||
Argument( | ||
null, | ||
Token(SyntaxKind.None), | ||
CastExpression( | ||
PointerType( | ||
QualifiedName( | ||
IdentifierName("ComWrappers"), | ||
IdentifierName("ComInterfaceDispatch"))), | ||
IdentifierName("ptr")))); | ||
|
||
return Block(ReturnStatement(invocation)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <code> | ||
/// file static class UnmanagedObjectUnwrapper | ||
/// { | ||
/// public static object GetObjectForUnmanagedWrapper<T>(void* ptr) where T : IUnmanagedObjectUnwrapper | ||
/// { | ||
/// return T.GetObjectForUnmanagedWrapper(ptr); | ||
/// } | ||
/// } | ||
/// </code> | ||
/// </summary> | ||
public static ClassDeclarationSyntax UnmanagedObjectUnwrapper { get; } = GetUnmanagedObjectUnwrapper(); | ||
|
||
private static ClassDeclarationSyntax GetUnmanagedObjectUnwrapper() | ||
{ | ||
const string tUnwrapper = "TUnwrapper"; | ||
return ClassDeclaration("UnmanagedObjectUnwrapper") | ||
.AddModifiers(Token(SyntaxKind.FileKeyword), | ||
Token(SyntaxKind.StaticKeyword)) | ||
.AddMembers( | ||
MethodDeclaration( | ||
PredefinedType(Token(SyntaxKind.ObjectKeyword)), | ||
Identifier("GetObjectForUnmanagedWrapper")) | ||
.AddModifiers(Token(SyntaxKind.PublicKeyword), | ||
Token(SyntaxKind.StaticKeyword)) | ||
.AddTypeParameterListParameters( | ||
TypeParameter(Identifier(tUnwrapper))) | ||
.AddParameterListParameters( | ||
Parameter(Identifier("ptr")) | ||
.WithType(PointerType(PredefinedType(Token(SyntaxKind.VoidKeyword))))) | ||
.AddConstraintClauses(TypeParameterConstraintClause(IdentifierName(tUnwrapper)) | ||
.AddConstraints(TypeConstraint(ParseTypeName(TypeNames.IUnmanagedObjectUnwrapper)))) | ||
.WithBody(body: Body())); | ||
|
||
static BlockSyntax Body() | ||
{ | ||
var invocation = InvocationExpression( | ||
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, | ||
IdentifierName("T"), | ||
IdentifierName("GetObjectForUnmanagedWrapper"))) | ||
.AddArgumentListArguments( | ||
Argument( | ||
null, | ||
Token(SyntaxKind.None), | ||
IdentifierName("ptr"))); | ||
|
||
return Block(ReturnStatement(invocation)); | ||
} | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,4 +207,7 @@ | |
<data name="InterfaceTypeNotSupportedMessage" xml:space="preserve"> | ||
<value>Using 'GeneratedComInterfaceAttribute' and 'InterfaceTypeAttribute' is not supported with 'ComInterfaceType' value '{0}'.</value> | ||
</data> | ||
<data name="InvalidAttributedMethodContainingTypeMissingUnmanagedObjectUnwrapperAttribute" xml:space="preserve"> | ||
<value>Containing type of method with VirtualMethodIndexAttribute does not have a UnmanagedObjectUnwrapperAttribute. </value> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: single quote around type names |
||
</data> | ||
</root> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(for later change) We should probably have a different ID for the diagnostics here.