|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Collections.Immutable; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 11 | +using Microsoft.CodeAnalysis.CSharp; |
| 12 | + |
| 13 | +namespace Microsoft.Interop |
| 14 | +{ |
| 15 | + [Generator] |
| 16 | + public class ComClassGenerator : IIncrementalGenerator |
| 17 | + { |
| 18 | + private sealed record ComClassInfo(string ClassName, ContainingSyntaxContext ContainingSyntaxContext, ContainingSyntax ClassSyntax, SequenceEqualImmutableArray<string> ImplementedInterfacesNames); |
| 19 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 20 | + { |
| 21 | + // Get all types with the [GeneratedComClassAttribute] attribute. |
| 22 | + var attributedClasses = context.SyntaxProvider |
| 23 | + .ForAttributeWithMetadataName( |
| 24 | + TypeNames.GeneratedComClassAttribute, |
| 25 | + static (node, ct) => node is ClassDeclarationSyntax, |
| 26 | + static (context, ct) => |
| 27 | + { |
| 28 | + var type = (INamedTypeSymbol)context.TargetSymbol; |
| 29 | + var syntax = (ClassDeclarationSyntax)context.TargetNode; |
| 30 | + ImmutableArray<string>.Builder names = ImmutableArray.CreateBuilder<string>(); |
| 31 | + foreach (INamedTypeSymbol iface in type.AllInterfaces) |
| 32 | + { |
| 33 | + if (iface.GetAttributes().Any(attr => attr.AttributeClass?.ToDisplayString() == TypeNames.GeneratedComInterfaceAttribute)) |
| 34 | + { |
| 35 | + names.Add(iface.ToDisplayString()); |
| 36 | + } |
| 37 | + } |
| 38 | + return new ComClassInfo( |
| 39 | + type.ToDisplayString(), |
| 40 | + new ContainingSyntaxContext(syntax), |
| 41 | + new ContainingSyntax(syntax.Modifiers, syntax.Kind(), syntax.Identifier, syntax.TypeParameterList), |
| 42 | + new(names.ToImmutable())); |
| 43 | + }); |
| 44 | + |
| 45 | + var className = attributedClasses.Select(static (info, ct) => info.ClassName); |
| 46 | + |
| 47 | + var classInfoType = attributedClasses |
| 48 | + .Select(static (info, ct) => new { info.ClassName, info.ImplementedInterfacesNames }) |
| 49 | + .Select(static (info, ct) => GenerateClassInfoType(info.ImplementedInterfacesNames.Array).NormalizeWhitespace()); |
| 50 | + |
| 51 | + var attribute = attributedClasses |
| 52 | + .Select(static (info, ct) => new { info.ContainingSyntaxContext, info.ClassSyntax }) |
| 53 | + .Select(static (info, ct) => GenerateClassInfoAttributeOnUserType(info.ContainingSyntaxContext, info.ClassSyntax).NormalizeWhitespace()); |
| 54 | + |
| 55 | + context.RegisterSourceOutput(className.Zip(classInfoType).Zip(attribute), static (context, classInfo) => |
| 56 | + { |
| 57 | + var ((className, classInfoType), attribute) = classInfo; |
| 58 | + StringWriter writer = new(); |
| 59 | + writer.WriteLine(classInfoType.ToFullString()); |
| 60 | + writer.WriteLine(); |
| 61 | + writer.WriteLine(attribute); |
| 62 | + context.AddSource(className, writer.ToString()); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private const string ClassInfoTypeName = "ComClassInformation"; |
| 67 | + |
| 68 | + private static readonly AttributeSyntax s_comExposedClassAttributeTemplate = |
| 69 | + Attribute( |
| 70 | + GenericName(TypeNames.ComExposedClassAttribute) |
| 71 | + .AddTypeArgumentListArguments( |
| 72 | + IdentifierName(ClassInfoTypeName))); |
| 73 | + private static MemberDeclarationSyntax GenerateClassInfoAttributeOnUserType(ContainingSyntaxContext containingSyntaxContext, ContainingSyntax classSyntax) => |
| 74 | + containingSyntaxContext.WrapMemberInContainingSyntaxWithUnsafeModifier( |
| 75 | + TypeDeclaration(classSyntax.TypeKind, classSyntax.Identifier) |
| 76 | + .WithModifiers(classSyntax.Modifiers) |
| 77 | + .WithTypeParameterList(classSyntax.TypeParameters) |
| 78 | + .AddAttributeLists(AttributeList(SingletonSeparatedList(s_comExposedClassAttributeTemplate)))); |
| 79 | + private static ClassDeclarationSyntax GenerateClassInfoType(ImmutableArray<string> implementedInterfaces) |
| 80 | + { |
| 81 | + const string vtablesField = "s_vtables"; |
| 82 | + const string vtablesLocal = "vtables"; |
| 83 | + const string detailsTempLocal = "details"; |
| 84 | + const string countIdentifier = "count"; |
| 85 | + var typeDeclaration = ClassDeclaration(ClassInfoTypeName) |
| 86 | + .AddModifiers( |
| 87 | + Token(SyntaxKind.FileKeyword), |
| 88 | + Token(SyntaxKind.SealedKeyword), |
| 89 | + Token(SyntaxKind.UnsafeKeyword)) |
| 90 | + .AddBaseListTypes(SimpleBaseType(ParseTypeName(TypeNames.IComExposedClass))) |
| 91 | + .AddMembers( |
| 92 | + FieldDeclaration( |
| 93 | + VariableDeclaration( |
| 94 | + PointerType( |
| 95 | + ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), |
| 96 | + SingletonSeparatedList(VariableDeclarator(vtablesField)))) |
| 97 | + .AddModifiers( |
| 98 | + Token(SyntaxKind.PrivateKeyword), |
| 99 | + Token(SyntaxKind.StaticKeyword), |
| 100 | + Token(SyntaxKind.VolatileKeyword))); |
| 101 | + List<StatementSyntax> vtableInitializationBlock = new() |
| 102 | + { |
| 103 | + // ComInterfaceEntry* vtables = (ComInterfaceEntry*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(<ClassInfoTypeName>), sizeof(ComInterfaceEntry) * <numInterfaces>); |
| 104 | + LocalDeclarationStatement( |
| 105 | + VariableDeclaration( |
| 106 | + PointerType( |
| 107 | + ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), |
| 108 | + SingletonSeparatedList( |
| 109 | + VariableDeclarator(vtablesLocal) |
| 110 | + .WithInitializer(EqualsValueClause( |
| 111 | + CastExpression( |
| 112 | + PointerType( |
| 113 | + ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), |
| 114 | + InvocationExpression( |
| 115 | + MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, |
| 116 | + ParseTypeName(TypeNames.System_Runtime_CompilerServices_RuntimeHelpers), |
| 117 | + IdentifierName("AllocateTypeAssociatedMemory"))) |
| 118 | + .AddArgumentListArguments( |
| 119 | + Argument(TypeOfExpression(IdentifierName(ClassInfoTypeName))), |
| 120 | + Argument( |
| 121 | + BinaryExpression( |
| 122 | + SyntaxKind.MultiplyExpression, |
| 123 | + SizeOfExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), |
| 124 | + LiteralExpression( |
| 125 | + SyntaxKind.NumericLiteralExpression, |
| 126 | + Literal(implementedInterfaces.Length))))))))))), |
| 127 | + // IIUnknownDerivedDetails details; |
| 128 | + LocalDeclarationStatement( |
| 129 | + VariableDeclaration( |
| 130 | + ParseTypeName(TypeNames.IIUnknownDerivedDetails), |
| 131 | + SingletonSeparatedList( |
| 132 | + VariableDeclarator(detailsTempLocal)))) |
| 133 | + }; |
| 134 | + for (int i = 0; i < implementedInterfaces.Length; i++) |
| 135 | + { |
| 136 | + string ifaceName = implementedInterfaces[i]; |
| 137 | + |
| 138 | + // details = StrategyBasedComWrappers.DefaultIUnknownInterfaceDetailsStrategy.GetIUnknownDerivedDetails(typeof(<ifaceName>).TypeHandle); |
| 139 | + vtableInitializationBlock.Add( |
| 140 | + ExpressionStatement( |
| 141 | + AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, |
| 142 | + IdentifierName(detailsTempLocal), |
| 143 | + InvocationExpression( |
| 144 | + MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, |
| 145 | + MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, |
| 146 | + ParseTypeName(TypeNames.StrategyBasedComWrappers), |
| 147 | + IdentifierName("DefaultIUnknownInterfaceDetailsStrategy")), |
| 148 | + IdentifierName("GetIUnknownDerivedDetails")), |
| 149 | + ArgumentList( |
| 150 | + SingletonSeparatedList( |
| 151 | + Argument( |
| 152 | + MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, |
| 153 | + TypeOfExpression(ParseName(ifaceName)), |
| 154 | + IdentifierName("TypeHandle"))))))))); |
| 155 | + // vtable[i] = new() { IID = details.Iid, Vtable = details.ManagedVirtualMethodTable }; |
| 156 | + vtableInitializationBlock.Add( |
| 157 | + ExpressionStatement( |
| 158 | + AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, |
| 159 | + ElementAccessExpression( |
| 160 | + IdentifierName(vtablesLocal), |
| 161 | + BracketedArgumentList( |
| 162 | + SingletonSeparatedList( |
| 163 | + Argument( |
| 164 | + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i)))))), |
| 165 | + ImplicitObjectCreationExpression( |
| 166 | + ArgumentList(), |
| 167 | + InitializerExpression(SyntaxKind.ObjectInitializerExpression, |
| 168 | + SeparatedList( |
| 169 | + new ExpressionSyntax[] |
| 170 | + { |
| 171 | + AssignmentExpression( |
| 172 | + SyntaxKind.SimpleAssignmentExpression, |
| 173 | + IdentifierName("IID"), |
| 174 | + MemberAccessExpression( |
| 175 | + SyntaxKind.SimpleMemberAccessExpression, |
| 176 | + IdentifierName(detailsTempLocal), |
| 177 | + IdentifierName("Iid"))), |
| 178 | + AssignmentExpression( |
| 179 | + SyntaxKind.SimpleAssignmentExpression, |
| 180 | + IdentifierName("Vtable"), |
| 181 | + CastExpression( |
| 182 | + IdentifierName("nint"), |
| 183 | + MemberAccessExpression( |
| 184 | + SyntaxKind.SimpleMemberAccessExpression, |
| 185 | + IdentifierName(detailsTempLocal), |
| 186 | + IdentifierName("ManagedVirtualMethodTable")))) |
| 187 | + })))))); |
| 188 | + } |
| 189 | + |
| 190 | + // s_vtable = vtable; |
| 191 | + vtableInitializationBlock.Add( |
| 192 | + ExpressionStatement( |
| 193 | + AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, |
| 194 | + IdentifierName(vtablesField), |
| 195 | + IdentifierName(vtablesLocal)))); |
| 196 | + |
| 197 | + BlockSyntax getComInterfaceEntriesMethodBody = Block( |
| 198 | + // count = <count>; |
| 199 | + ExpressionStatement( |
| 200 | + AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, |
| 201 | + IdentifierName(countIdentifier), |
| 202 | + LiteralExpression(SyntaxKind.NumericLiteralExpression, |
| 203 | + Literal(implementedInterfaces.Length)))), |
| 204 | + // if (s_vtable == null) |
| 205 | + // { initializer block } |
| 206 | + IfStatement( |
| 207 | + BinaryExpression(SyntaxKind.EqualsExpression, |
| 208 | + IdentifierName(vtablesField), |
| 209 | + LiteralExpression(SyntaxKind.NullLiteralExpression)), |
| 210 | + Block(vtableInitializationBlock)), |
| 211 | + // return s_vtable; |
| 212 | + ReturnStatement(IdentifierName(vtablesField))); |
| 213 | + |
| 214 | + typeDeclaration = typeDeclaration.AddMembers( |
| 215 | + // public static unsafe ComWrappers.ComInterfaceDispatch* GetComInterfaceEntries(out int count) |
| 216 | + // { body } |
| 217 | + MethodDeclaration( |
| 218 | + PointerType( |
| 219 | + ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), |
| 220 | + "GetComInterfaceEntries") |
| 221 | + .AddParameterListParameters( |
| 222 | + Parameter(Identifier(countIdentifier)) |
| 223 | + .WithType(PredefinedType(Token(SyntaxKind.IntKeyword))) |
| 224 | + .AddModifiers(Token(SyntaxKind.OutKeyword))) |
| 225 | + .WithBody(getComInterfaceEntriesMethodBody) |
| 226 | + .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))); |
| 227 | + |
| 228 | + return typeDeclaration; |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments