From f3dc0422841084cf6e9b38374b486b89700d680f Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Thu, 10 Aug 2023 20:35:39 +0000 Subject: [PATCH 1/5] Prefix type names with global and use static TypeSyntax and NameSyntax instead of Parse(Type)Name --- ...omImportToGeneratedComInterfaceAnalyzer.cs | 2 +- .../ComClassGenerator.cs | 21 ++-- .../ComInterfaceGenerator.cs | 14 +-- .../ComInterfaceGenerator/ComMethodContext.cs | 2 +- .../ManagedToNativeVTableMethodGenerator.cs | 4 +- .../ComInterfaceDispatchMarshallerFactory.cs | 4 +- ...anagedHResultExceptionMarshallerFactory.cs | 2 +- .../ObjectUnwrapperMarshallerFactory.cs | 2 +- .../VirtualMethodPointerStubGenerator.cs | 4 +- .../VtableIndexStubGenerator.cs | 5 +- .../ConvertToLibraryImportAnalyzer.cs | 2 +- .../Analyzers/ConvertToLibraryImportFixer.cs | 2 +- .../LibraryImportGenerator.cs | 14 +-- .../GeneratedStatements.cs | 2 +- ...eneratorInitializationContextExtensions.cs | 2 +- .../Marshalling/DelegateMarshaller.cs | 4 +- .../Marshalling/ElementsMarshalling.cs | 18 +-- .../Marshalling/MarshallerHelpers.cs | 12 +- .../MarshallingGeneratorExtensions.cs | 10 +- .../PointerNativeTypeAssignmentRewriter.cs | 4 +- .../Marshalling/SafeHandleMarshaller.cs | 4 +- .../SignatureContext.cs | 6 +- .../TypeNames.cs | 116 +++++++++++++++++- .../ComInterfaceGenerator.Tests.csproj | 1 + .../CustomMarshallingTests.cs | 2 + .../SharedTypes/ComInterfaces/ISystem.cs | 19 +++ 26 files changed, 201 insertions(+), 77 deletions(-) create mode 100644 src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/ISystem.cs diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceAnalyzer.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceAnalyzer.cs index d476547ba50fc2..de661865516610 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceAnalyzer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceAnalyzer.cs @@ -46,7 +46,7 @@ public override void Initialize(AnalysisContext context) context.Compilation, targetFramework.TargetFramework, targetFramework.Version, - context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata)); + context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute)); context.RegisterSymbolAction(context => { diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs index 250ef2067d9c51..ce96a17b1d6a9b 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs @@ -124,12 +124,11 @@ private static ClassDeclarationSyntax GenerateClassInfoType(ImmutableArray), sizeof(ComInterfaceEntry) * ); LocalDeclarationStatement( VariableDeclaration( - PointerType( - ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), + PointerType(TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry), SingletonSeparatedList( VariableDeclarator(vtablesLocal) .WithInitializer(EqualsValueClause( CastExpression( - PointerType( - ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), + PointerType(TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry), InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.System_Runtime_CompilerServices_RuntimeHelpers), + TypeSyntaxes.System_Runtime_CompilerServices_RuntimeHelpers, IdentifierName("AllocateTypeAssociatedMemory"))) .AddArgumentListArguments( Argument(TypeOfExpression(IdentifierName(ClassInfoTypeName))), Argument( BinaryExpression( SyntaxKind.MultiplyExpression, - SizeOfExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)), + SizeOfExpression(TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry), LiteralExpression( SyntaxKind.NumericLiteralExpression, Literal(implementedInterfaces.Length))))))))))), // IIUnknownDerivedDetails details; LocalDeclarationStatement( VariableDeclaration( - ParseTypeName(TypeNames.IIUnknownDerivedDetails), + TypeSyntaxes.IIUnknownDerivedDetails, SingletonSeparatedList( VariableDeclarator(detailsTempLocal)))) }; @@ -180,7 +177,7 @@ private static ClassDeclarationSyntax GenerateClassInfoType(ImmutableArray ctx.Stub.Node) .Concat(shadowImplementations) .Concat(inheritedStubs))) - .AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(ParseName(TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute))))); + .AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(NameSyntaxes.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute)))); } private static InterfaceDeclarationSyntax GenerateImplementationVTableMethods(ComInterfaceAndMethodsContext comInterfaceAndMethods, CancellationToken _) @@ -496,7 +496,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf CastExpression(VoidStarStarSyntax, InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.System_Runtime_CompilerServices_RuntimeHelpers), + TypeSyntaxes.System_Runtime_CompilerServices_RuntimeHelpers, IdentifierName("AllocateTypeAssociatedMemory"))) .AddArgumentListArguments( Argument(TypeOfExpression(interfaceType.Syntax)), @@ -525,7 +525,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers), + TypeSyntaxes.System_Runtime_InteropServices_ComWrappers, IdentifierName("GetIUnknownImpl"))) .AddArgumentListArguments( Argument(IdentifierName("v0")) @@ -586,7 +586,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.System_Runtime_InteropServices_NativeMemory), + TypeSyntaxes.System_Runtime_InteropServices_NativeMemory, IdentifierName("Copy"))) .WithArgumentList( ArgumentList( @@ -601,7 +601,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf SyntaxKind.SimpleMemberAccessExpression, MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.StrategyBasedComWrappers), + TypeSyntaxes.StrategyBasedComWrappers, IdentifierName("DefaultIUnknownInterfaceDetailsStrategy")), IdentifierName("GetIUnknownDerivedDetails"))) .WithArgumentList( @@ -643,14 +643,14 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf private static readonly ClassDeclarationSyntax InterfaceInformationTypeTemplate = ClassDeclaration("InterfaceInformation") .AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.UnsafeKeyword)) - .AddBaseListTypes(SimpleBaseType(ParseTypeName(TypeNames.IIUnknownInterfaceType))); + .AddBaseListTypes(SimpleBaseType(TypeSyntaxes.IIUnknownInterfaceType)); private static ClassDeclarationSyntax GenerateInterfaceInformation(ComInterfaceInfo context, CancellationToken _) { ClassDeclarationSyntax interfaceInformationType = InterfaceInformationTypeTemplate .AddMembers( // public static System.Guid Iid { get; } = new(); - PropertyDeclaration(ParseTypeName(TypeNames.System_Guid), "Iid") + PropertyDeclaration(TypeSyntaxes.System_Guid, "Iid") .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) .AddAccessorListAccessors( AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken))) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComMethodContext.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComMethodContext.cs index 72dcb670f5b9fe..e81e2ce06895de 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComMethodContext.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComMethodContext.cs @@ -114,7 +114,7 @@ private MethodDeclarationSyntax CreateUnreachableExceptionStub() .WithExpressionBody(ArrowExpressionClause( ThrowExpression( ObjectCreationExpression( - ParseTypeName(TypeNames.UnreachableException)) + TypeSyntaxes.UnreachableException) .WithArgumentList(ArgumentList())))); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ManagedToNativeVTableMethodGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ManagedToNativeVTableMethodGenerator.cs index dd18f01bb7c88b..4207abc73af3e4 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ManagedToNativeVTableMethodGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ManagedToNativeVTableMethodGenerator.cs @@ -115,7 +115,7 @@ public BlockSyntax GenerateStubBody(int index, ImmutableArray new PointerTypeInfo( - $"{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", + $"global::{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", $"{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", IsFunctionPointer: false); public IEnumerable Generate(TypePositionInfo info, StubCodeContext context) @@ -47,7 +47,7 @@ public IEnumerable Generate(TypePositionInfo info, StubCodeCont IdentifierName(managed), InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch), + TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch, GenericName( Identifier("GetInstance"), TypeArgumentList(SingletonSeparatedList(info.ManagedType.Syntax)))), diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ManagedHResultExceptionMarshallerFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ManagedHResultExceptionMarshallerFactory.cs index 63017cf51265a4..ad96c9b6b3bc87 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ManagedHResultExceptionMarshallerFactory.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ManagedHResultExceptionMarshallerFactory.cs @@ -62,7 +62,7 @@ public IEnumerable Generate(TypePositionInfo info, StubCodeCont yield return ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_Marshal), + TypeSyntaxes.System_Runtime_InteropServices_Marshal, IdentifierName("ThrowExceptionForHR")), ArgumentList( SingletonSeparatedList(Argument(IdentifierName(managedIdentifier)))))); diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ObjectUnwrapperMarshallerFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ObjectUnwrapperMarshallerFactory.cs index c04a3a7ccfbb10..1ed3dd0d49ec5c 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ObjectUnwrapperMarshallerFactory.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ObjectUnwrapperMarshallerFactory.cs @@ -47,7 +47,7 @@ public IEnumerable Generate(TypePositionInfo info, StubCodeCont InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.UnmanagedObjectUnwrapper), + TypeSyntaxes.UnmanagedObjectUnwrapper, GenericName(Identifier("GetObjectForUnmanagedWrapper")) .WithTypeArgumentList( TypeArgumentList( diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs index fe931a812bf341..2fc5f14f649eaa 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs @@ -85,7 +85,7 @@ public static (MethodDeclarationSyntax, ImmutableArray) Generate (ParameterListSyntax unmanagedParameterList, TypeSyntax returnType, _) = stubGenerator.GenerateAbiMethodSignatureData(); AttributeSyntax unmanagedCallersOnlyAttribute = Attribute( - ParseName(TypeNames.UnmanagedCallersOnlyAttribute)); + NameSyntaxes.UnmanagedCallersOnlyAttribute); if (methodStub.CallingConvention.Array.Length != 0) { @@ -94,7 +94,7 @@ public static (MethodDeclarationSyntax, ImmutableArray) Generate ImplicitArrayCreationExpression( InitializerExpression(SyntaxKind.CollectionInitializerExpression, SeparatedList( - methodStub.CallingConvention.Array.Select(callConv => TypeOfExpression(ParseName($"System.Runtime.CompilerServices.CallConv{callConv.Name.ValueText}"))))))) + methodStub.CallingConvention.Array.Select(callConv => TypeOfExpression(ParseName($"global::System.Runtime.CompilerServices.CallConv{callConv.Name.ValueText}"))))))) .WithNameEquals(NameEquals(IdentifierName("CallConvs")))); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs index e5faa6fbadd6f2..f826cd130a7d2b 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs @@ -413,9 +413,6 @@ private static (MemberDeclarationSyntax, ImmutableArray) Generat // Verify there is an [UnmanagedObjectUnwrapperAttribute] if (!method.ContainingType.GetAttributes().Any(att => att.AttributeClass.IsOfType(TypeNames.UnmanagedObjectUnwrapperAttribute))) - //!method.ContainingType.GetAttributes().Any(att => - //att.AttributeClass.MetadataName == TypeNames.UnmanagedObjectUnwrapperAttribute.Substring(TypeNames.UnmanagedObjectUnwrapperAttribute.LastIndexOf('.') + 1) - //&& att.AttributeClass.OriginalDefinition.ToDisplayString().Substring(0, att.AttributeClass.OriginalDefinition.ToDisplayString().LastIndexOf(att.AttributeClass.ToDisplayString())) == TypeNames.UnmanagedObjectUnwrapperAttribute.Substring(0, TypeNames.UnmanagedObjectUnwrapperAttribute.LastIndexOf('.')))) { return Diagnostic.Create(GeneratorDiagnostics.InvalidAttributedMethodContainingTypeMissingUnmanagedObjectUnwrapperAttribute, methodSyntax.Identifier.GetLocation(), method.Name); } @@ -429,7 +426,7 @@ private static MemberDeclarationSyntax GenerateNativeInterfaceMetadata(Containin InterfaceDeclaration("Native") .WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.PartialKeyword))) .WithBaseList(BaseList(SingletonSeparatedList((BaseTypeSyntax)SimpleBaseType(IdentifierName(context.ContainingSyntax[0].Identifier))))) - .AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(ParseName(TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute)))))); + .AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(NameSyntaxes.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute))))); } private static MemberDeclarationSyntax GeneratePopulateVTableMethod(IGrouping vtableMethods) diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportAnalyzer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportAnalyzer.cs index 558a4fb161437e..ee34a5931abf32 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportAnalyzer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportAnalyzer.cs @@ -62,7 +62,7 @@ public override void Initialize(AnalysisContext context) context.Compilation, targetFramework.TargetFramework, targetFramework.Version, - context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata)); + context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute)); context.RegisterSymbolAction(symbolContext => AnalyzeSymbol(symbolContext, libraryImportAttrType, env), SymbolKind.Method); }); diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs index 60d6796869d338..8fd974ba2f90c0 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs @@ -196,7 +196,7 @@ private static async Task ConvertMethodDeclarationToLibraryImport( char? entryPointSuffix, CancellationToken cancellationToken) { - INamedTypeSymbol? dllImportAttrType = editor.SemanticModel.Compilation.GetBestTypeByMetadataName(typeof(DllImportAttribute).FullName); + INamedTypeSymbol? dllImportAttrType = editor.SemanticModel.Compilation.GetBestTypeByMetadataName(TypeNames.DllImportAttribute); if (dllImportAttrType == null) return methodSyntax; diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs index fcd915ea25f606..cd6d0e4833a4f4 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs @@ -113,11 +113,11 @@ private static List GenerateSyntaxForForwardedAttributes(Attrib if (suppressGCTransitionAttribute is not null) { - attributes.Add(Attribute(ParseName(TypeNames.SuppressGCTransitionAttribute))); + attributes.Add(Attribute(NameSyntaxes.SuppressGCTransitionAttribute)); } if (unmanagedCallConvAttribute is not null) { - AttributeSyntax unmanagedCallConvSyntax = Attribute(ParseName(TypeNames.UnmanagedCallConvAttribute)); + AttributeSyntax unmanagedCallConvSyntax = Attribute(NameSyntaxes.UnmanagedCallConvAttribute); foreach (KeyValuePair arg in unmanagedCallConvAttribute.NamedArguments) { if (arg.Key == CallConvsField) @@ -129,7 +129,7 @@ private static List GenerateSyntaxForForwardedAttributes(Attrib TypeOfExpression(((ITypeSymbol)callConv.Value!).AsTypeSyntax())); } - ArrayTypeSyntax arrayOfSystemType = ArrayType(ParseTypeName(TypeNames.System_Type), SingletonList(ArrayRankSpecifier())); + ArrayTypeSyntax arrayOfSystemType = ArrayType(TypeSyntaxes.System_Type, SingletonList(ArrayRankSpecifier())); unmanagedCallConvSyntax = unmanagedCallConvSyntax.AddArgumentListArguments( AttributeArgument( @@ -143,9 +143,9 @@ private static List GenerateSyntaxForForwardedAttributes(Attrib if (defaultDllImportSearchPathsAttribute is not null) { attributes.Add( - Attribute(ParseName(TypeNames.DefaultDllImportSearchPathsAttribute)).AddArgumentListArguments( + Attribute(NameSyntaxes.DefaultDllImportSearchPathsAttribute).AddArgumentListArguments( AttributeArgument( - CastExpression(ParseTypeName(TypeNames.DllImportSearchPath), + CastExpression(TypeSyntaxes.DllImportSearchPath, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)defaultDllImportSearchPathsAttribute.ConstructorArguments[0].Value!)))))); } @@ -424,7 +424,7 @@ private static LocalFunctionStatementSyntax CreateTargetDllImportAsLocalStatemen SingletonList(AttributeList( SingletonSeparatedList( Attribute( - ParseName(typeof(DllImportAttribute).FullName), + NameSyntaxes.DllImportAttribute, AttributeArgumentList( SeparatedList( new[] @@ -486,7 +486,7 @@ private static AttributeSyntax CreateForwarderDllImport(LibraryImportData target // Create new attribute return Attribute( - ParseName(typeof(DllImportAttribute).FullName), + NameSyntaxes.DllImportAttribute, AttributeArgumentList(SeparatedList(newAttributeArgs))); static ExpressionSyntax CreateBoolExpressionSyntax(bool trueOrFalse) diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs index d611eb59cd3b40..896487328de530 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs @@ -168,7 +168,7 @@ private static ImmutableArray GenerateCatchClauseForManagedEx managedExceptionMarshaller.TypeInfo, context with { CurrentStage = StubCodeContext.Stage.PinnedMarshal })); return ImmutableArray.Create( CatchClause( - CatchDeclaration(ParseTypeName(TypeNames.System_Exception), Identifier(managed)), + CatchDeclaration(TypeSyntaxes.System_Exception, Identifier(managed)), filter: null, Block(List(catchClauseBuilder)))); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/IncrementalGeneratorInitializationContextExtensions.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/IncrementalGeneratorInitializationContextExtensions.cs index 7a30a442e1085e..18ae8e90d58cd2 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/IncrementalGeneratorInitializationContextExtensions.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/IncrementalGeneratorInitializationContextExtensions.cs @@ -21,7 +21,7 @@ public static IncrementalValueProvider CreateStubEnvironmentPro var isModuleSkipLocalsInit = context.SyntaxProvider .ForAttributeWithMetadataName( - TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata, + TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute, (node, ct) => node is ICompilationUnitSyntax, // If SkipLocalsInit is applied at the top level, it is either applied to the module // or is invalid syntax. As a result, we just need to know if there's any top-level diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/DelegateMarshaller.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/DelegateMarshaller.cs index 3f8631a3d4ab2a..b4b4c01c5b3d4e 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/DelegateMarshaller.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/DelegateMarshaller.cs @@ -53,7 +53,7 @@ public IEnumerable Generate(TypePositionInfo info, StubCodeCont InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_Marshal), + TypeSyntaxes.System_Runtime_InteropServices_Marshal, IdentifierName("GetFunctionPointerForDelegate")), ArgumentList(SingletonSeparatedList(Argument(IdentifierName(managedIdentifier))))), LiteralExpression(SyntaxKind.DefaultLiteralExpression)))); @@ -75,7 +75,7 @@ public IEnumerable Generate(TypePositionInfo info, StubCodeCont InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_Marshal), + TypeSyntaxes.System_Runtime_InteropServices_Marshal, GenericName(Identifier("GetDelegateForFunctionPointer")) .WithTypeArgumentList( TypeArgumentList( diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs index 136de0e4cca534..c59a474635df1f 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs @@ -113,7 +113,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal), + TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal, IdentifierName("CreateSpan")), ArgumentList( SeparatedList(new[] @@ -121,7 +121,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState Argument( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal), + TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal, IdentifierName("GetReference")), ArgumentList(SingletonSeparatedList( Argument(CollectionSource.GetUnmanagedValuesSource(info, context)))))) @@ -167,7 +167,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta ExpressionSyntax destination = InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal), + TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal, IdentifierName("CreateSpan")), ArgumentList( SeparatedList(new[] @@ -175,7 +175,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta Argument( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal), + TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal, IdentifierName("GetReference")), ArgumentList(SingletonSeparatedList( Argument(CollectionSource.GetManagedValuesSource(info, context)))))) @@ -223,7 +223,7 @@ private ExpressionSyntax CastToManagedIfNecessary(ExpressionSyntax expression) return InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.System_Runtime_InteropServices_MemoryMarshal), + TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal, GenericName( Identifier("Cast"), TypeArgumentList(SeparatedList( @@ -369,7 +369,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal), + TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal, IdentifierName("CreateSpan"))) .WithArgumentList( ArgumentList( @@ -379,7 +379,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta Argument( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_CompilerServices_Unsafe), + NameSyntaxes.System_Runtime_CompilerServices_Unsafe, IdentifierName("AsRef")), ArgumentList(SingletonSeparatedList( Argument( @@ -493,7 +493,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal), + TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal, IdentifierName("CreateSpan"))) .WithArgumentList( ArgumentList( @@ -503,7 +503,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState Argument( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_CompilerServices_Unsafe), + NameSyntaxes.System_Runtime_CompilerServices_Unsafe, IdentifierName("AsRef")), ArgumentList(SingletonSeparatedList( Argument( diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs index 325f4741600ac4..e58728554bbecd 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs @@ -12,7 +12,7 @@ namespace Microsoft.Interop { public static class MarshallerHelpers { - public static readonly TypeSyntax SystemIntPtrType = ParseTypeName(TypeNames.System_IntPtr); + public static readonly TypeSyntax SystemIntPtrType = TypeSyntaxes.System_IntPtr; public static ForStatementSyntax GetForLoop(ExpressionSyntax lengthExpression, string indexerIdentifier) { @@ -96,7 +96,7 @@ public static TypeSyntax GetCompatibleGenericTypeParameterSyntax(this TypeSyntax if (spanElementTypeSyntax is PointerTypeSyntax) { // Pointers cannot be passed to generics, so use IntPtr for this case. - spanElementTypeSyntax = SystemIntPtrType; + spanElementTypeSyntax = TypeSyntaxes.System_IntPtr; } return spanElementTypeSyntax; } @@ -108,7 +108,7 @@ public static StatementSyntax CreateClearLastSystemErrorStatement(int errorCode) InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_Marshal), + TypeSyntaxes.System_Runtime_InteropServices_Marshal, IdentifierName("SetLastSystemError")), ArgumentList(SingletonSeparatedList( Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(errorCode))))))); @@ -122,7 +122,7 @@ public static StatementSyntax CreateGetLastSystemErrorStatement(string lastError InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_Marshal), + TypeSyntaxes.System_Runtime_InteropServices_Marshal, IdentifierName("GetLastSystemError"))))); // Marshal.SetLastPInvokeError(); @@ -131,7 +131,7 @@ public static StatementSyntax CreateSetLastPInvokeErrorStatement(string lastErro InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_InteropServices_Marshal), + TypeSyntaxes.System_Runtime_InteropServices_Marshal, IdentifierName("SetLastPInvokeError")), ArgumentList(SingletonSeparatedList( Argument(IdentifierName(lastErrorIdentifier)))))); @@ -327,7 +327,7 @@ public static StatementSyntax SkipInitOrDefaultInit(TypePositionInfo info, StubC return ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseName(TypeNames.System_Runtime_CompilerServices_Unsafe), + NameSyntaxes.System_Runtime_CompilerServices_Unsafe, IdentifierName("SkipInit"))) .WithArgumentList( ArgumentList(SingletonSeparatedList( diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallingGeneratorExtensions.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallingGeneratorExtensions.cs index 2c11351959b038..7773df2475d2cb 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallingGeneratorExtensions.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallingGeneratorExtensions.cs @@ -137,9 +137,9 @@ private static bool TryRehydrateMarshalAsAttribute(TypePositionInfo info, out At // so explicitly do not resurface a [MarshalAs(UnmanagdType.CustomMarshaler)] attribute. if (info.MarshallingAttributeInfo is MarshalAsInfo { UnmanagedType: not UnmanagedType.CustomMarshaler } marshalAs) { - marshalAsAttribute = Attribute(ParseName(TypeNames.System_Runtime_InteropServices_MarshalAsAttribute)) + marshalAsAttribute = Attribute(NameSyntaxes.System_Runtime_InteropServices_MarshalAsAttribute) .WithArgumentList(AttributeArgumentList(SingletonSeparatedList(AttributeArgument( - CastExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_UnmanagedType), + CastExpression(TypeSyntaxes.System_Runtime_InteropServices_UnmanagedType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)marshalAs.UnmanagedType))))))); return true; @@ -184,7 +184,7 @@ private static bool TryRehydrateMarshalAsAttribute(TypePositionInfo info, out At List marshalAsArguments = new List { AttributeArgument( - CastExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_UnmanagedType), + CastExpression(TypeSyntaxes.System_Runtime_InteropServices_UnmanagedType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)UnmanagedType.LPArray)))) }; @@ -213,12 +213,12 @@ private static bool TryRehydrateMarshalAsAttribute(TypePositionInfo info, out At { marshalAsArguments.Add( AttributeArgument(NameEquals("ArraySubType"), null, - CastExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_UnmanagedType), + CastExpression(TypeSyntaxes.System_Runtime_InteropServices_UnmanagedType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)elementMarshalAs.UnmanagedType)))) ); } - marshalAsAttribute = Attribute(ParseName(TypeNames.System_Runtime_InteropServices_MarshalAsAttribute)) + marshalAsAttribute = Attribute(NameSyntaxes.System_Runtime_InteropServices_MarshalAsAttribute) .WithArgumentList(AttributeArgumentList(SeparatedList(marshalAsArguments))); return true; } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PointerNativeTypeAssignmentRewriter.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PointerNativeTypeAssignmentRewriter.cs index 6ce89e8377f4b4..5f4f5d43a4a3df 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PointerNativeTypeAssignmentRewriter.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PointerNativeTypeAssignmentRewriter.cs @@ -35,7 +35,7 @@ public PointerNativeTypeAssignmentRewriter(string nativeIdentifier, PointerTypeS { return node.WithInitializer( EqualsValueClause( - CastExpression(MarshallerHelpers.SystemIntPtrType, node.Initializer.Value))); + CastExpression(TypeSyntaxes.System_IntPtr, node.Initializer.Value))); } if (node.Initializer.Value.ToString() == _nativeIdentifier) { @@ -52,7 +52,7 @@ public override SyntaxNode VisitAssignmentExpression(AssignmentExpressionSyntax if (node.Left.ToString() == _nativeIdentifier) { return node.WithRight( - CastExpression(MarshallerHelpers.SystemIntPtrType, node.Right)); + CastExpression(TypeSyntaxes.System_IntPtr, node.Right)); } if (node.Right.ToString() == _nativeIdentifier) { diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/SafeHandleMarshaller.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/SafeHandleMarshaller.cs index 9f4700ec00f936..c45b94483bd445 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/SafeHandleMarshaller.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/SafeHandleMarshaller.cs @@ -77,7 +77,7 @@ public IEnumerable Generate(TypePositionInfo info, StubCodeCont InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.System_Activator), + TypeSyntaxes.System_Activator, IdentifierName("CreateInstance"))) .WithArgumentList( ArgumentList( @@ -168,7 +168,7 @@ public IEnumerable Generate(TypePositionInfo info, StubCodeCont StatementSyntax unmarshalStatement = ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - ParseTypeName(TypeNames.System_Runtime_InteropServices_Marshal), + TypeSyntaxes.System_Runtime_InteropServices_Marshal, IdentifierName("InitHandle")), ArgumentList(SeparatedList( new[] diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/SignatureContext.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/SignatureContext.cs index 6c1365f9265f1f..5a93922a5bb138 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/SignatureContext.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/SignatureContext.cs @@ -86,7 +86,7 @@ public static SignatureContext Create( AttributeList( SingletonSeparatedList( Attribute( - ParseName(TypeNames.System_CodeDom_Compiler_GeneratedCodeAttribute_WithGlobal), + NameSyntaxes.System_CodeDom_Compiler_GeneratedCodeAttribute, AttributeArgumentList( SeparatedList( new[] @@ -104,7 +104,7 @@ public static SignatureContext Create( // Adding the skip locals init indiscriminately since the source generator is // targeted at non-blittable method signatures which typically will contain locals // in the generated code. - Attribute(ParseName(TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_WithGlobal))))); + Attribute(NameSyntaxes.System_Runtime_CompilerServices_SkipLocalsInitAttribute)))); } return new SignatureContext() @@ -187,7 +187,7 @@ private static bool MethodIsSkipLocalsInit(StubEnvironment env, IMethodSymbol me return false; static bool IsSkipLocalsInitAttribute(AttributeData a) - => a.AttributeClass?.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata; + => a.AttributeClass?.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute; } } } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs index dac6c2658ccd5b..3208062d81e6dd 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs @@ -1,8 +1,118 @@ // 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.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + namespace Microsoft.Interop { + public static class NameSyntaxes + { + private static NameSyntax? _DllImportAttribute; + public static NameSyntax DllImportAttribute => _DllImportAttribute ??= ParseName("global::" + TypeNames.DllImportAttribute); + + private static NameSyntax? _LibraryImportAttribute; + public static NameSyntax LibraryImportAttribute => _LibraryImportAttribute ??= ParseName("global::" + TypeNames.LibraryImportAttribute); + + private static NameSyntax? _System_Runtime_CompilerServices_Unsafe; + public static NameSyntax System_Runtime_CompilerServices_Unsafe => _System_Runtime_CompilerServices_Unsafe ??= ParseName("global::" + TypeNames.System_Runtime_CompilerServices_Unsafe); + + private static NameSyntax? _System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute; + public static NameSyntax System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute => _System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute ??= ParseName("global::" + TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute); + + private static NameSyntax? _System_Runtime_InteropServices_MarshalAsAttribute; + public static NameSyntax System_Runtime_InteropServices_MarshalAsAttribute => _System_Runtime_InteropServices_MarshalAsAttribute ??= ParseName("global::" + TypeNames.System_Runtime_InteropServices_MarshalAsAttribute); + + private static NameSyntax? _DefaultDllImportSearchPathsAttribute; + public static NameSyntax DefaultDllImportSearchPathsAttribute => _DefaultDllImportSearchPathsAttribute ??= ParseName("global::" + TypeNames.DefaultDllImportSearchPathsAttribute); + + private static NameSyntax? _SuppressGCTransitionAttribute; + public static NameSyntax SuppressGCTransitionAttribute => _SuppressGCTransitionAttribute ??= ParseName("global::" + TypeNames.SuppressGCTransitionAttribute); + + private static NameSyntax? _UnmanagedCallConvAttribute; + public static NameSyntax UnmanagedCallConvAttribute => _UnmanagedCallConvAttribute ??= ParseName("global::" + TypeNames.UnmanagedCallConvAttribute); + + private static NameSyntax? _System_Runtime_CompilerServices_SkipLocalsInitAttribute; + public static NameSyntax System_Runtime_CompilerServices_SkipLocalsInitAttribute => _System_Runtime_CompilerServices_SkipLocalsInitAttribute ??= ParseName("global::" + TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute); + + private static NameSyntax? _System_CodeDom_Compiler_GeneratedCodeAttribute; + public static NameSyntax System_CodeDom_Compiler_GeneratedCodeAttribute => _System_CodeDom_Compiler_GeneratedCodeAttribute ??= ParseName("global::" + TypeNames.System_CodeDom_Compiler_GeneratedCodeAttribute); + + private static NameSyntax? _UnmanagedCallersOnlyAttribute; + public static NameSyntax UnmanagedCallersOnlyAttribute => _UnmanagedCallersOnlyAttribute ??= ParseName("global::" + TypeNames.UnmanagedCallersOnlyAttribute); + } + + public static class TypeSyntaxes + { + private static TypeSyntax? _StringMarshalling; + public static TypeSyntax StringMarshalling => _StringMarshalling ??= ParseTypeName("global::" + TypeNames.StringMarshalling); + + private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry; + public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry => _System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry); + + private static TypeSyntax? _System_Runtime_InteropServices_NativeMemory; + public static TypeSyntax System_Runtime_InteropServices_NativeMemory => _System_Runtime_InteropServices_NativeMemory ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_NativeMemory); + + private static TypeSyntax? _StrategyBasedComWrappers; + public static TypeSyntax StrategyBasedComWrappers => _StrategyBasedComWrappers ??= ParseTypeName("global::" + TypeNames.StrategyBasedComWrappers); + + private static TypeSyntax? _IUnmanagedVirtualMethodTableProvider; + public static TypeSyntax IUnmanagedVirtualMethodTableProvider => _IUnmanagedVirtualMethodTableProvider ??= ParseTypeName("global::" + TypeNames.IUnmanagedVirtualMethodTableProvider); + + private static TypeSyntax? _IIUnknownInterfaceType; + public static TypeSyntax IIUnknownInterfaceType => _IIUnknownInterfaceType ??= ParseTypeName("global::" + TypeNames.IIUnknownInterfaceType); + + private static TypeSyntax? _IIUnknownDerivedDetails; + public static TypeSyntax IIUnknownDerivedDetails => _IIUnknownDerivedDetails ??= ParseTypeName("global::" + TypeNames.IIUnknownDerivedDetails); + + private static TypeSyntax? _UnmanagedObjectUnwrapper; + public static TypeSyntax UnmanagedObjectUnwrapper => _UnmanagedObjectUnwrapper ??= ParseTypeName("global::" + TypeNames.UnmanagedObjectUnwrapper); + + private static TypeSyntax? _IComExposedClass; + public static TypeSyntax IComExposedClass => _IComExposedClass ??= ParseTypeName("global::" + TypeNames.IComExposedClass); + + private static TypeSyntax? _UnreachableException; + public static TypeSyntax UnreachableException => _UnreachableException ??= ParseTypeName("global::" + TypeNames.UnreachableException); + + private static TypeSyntax? _System_Runtime_CompilerServices_RuntimeHelpers; + public static TypeSyntax System_Runtime_CompilerServices_RuntimeHelpers => _System_Runtime_CompilerServices_RuntimeHelpers ??= ParseTypeName("global::" + TypeNames.System_Runtime_CompilerServices_RuntimeHelpers); + + private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers; + public static TypeSyntax System_Runtime_InteropServices_ComWrappers => _System_Runtime_InteropServices_ComWrappers ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_ComWrappers); + + private static TypeSyntax? _System_IntPtr; + public static TypeSyntax System_IntPtr => _System_IntPtr ??= ParseTypeName("global::" + TypeNames.System_IntPtr); + + private static TypeSyntax? _System_Guid; + public static TypeSyntax System_Guid => _System_Guid ??= ParseTypeName("global::" + TypeNames.System_Guid); + + private static TypeSyntax? _DllImportSearchPath; + public static TypeSyntax DllImportSearchPath => _DllImportSearchPath ??= ParseTypeName("global::" + TypeNames.DllImportSearchPath); + + private static TypeSyntax? _System_Type; + public static TypeSyntax System_Type => _System_Type ??= ParseTypeName("global::" + TypeNames.System_Type); + + private static TypeSyntax? _System_Activator; + public static TypeSyntax System_Activator => _System_Activator ??= ParseTypeName("global::" + TypeNames.System_Activator); + + private static TypeSyntax? _System_Runtime_InteropServices_Marshal; + public static TypeSyntax System_Runtime_InteropServices_Marshal => _System_Runtime_InteropServices_Marshal ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_Marshal); + + private static TypeSyntax? _System_Runtime_InteropServices_UnmanagedType; + public static TypeSyntax System_Runtime_InteropServices_UnmanagedType => _System_Runtime_InteropServices_UnmanagedType ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_UnmanagedType); + + private static TypeSyntax? _System_Runtime_InteropServices_MemoryMarshal; + public static TypeSyntax System_Runtime_InteropServices_MemoryMarshal => _System_Runtime_InteropServices_MemoryMarshal ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_MemoryMarshal); + + private static TypeSyntax? _System_Exception; + public static TypeSyntax System_Exception => _System_Exception ??= ParseTypeName("global::" + TypeNames.System_Exception); + + private static TypeSyntax? _System_GC; + public static TypeSyntax System_GC => _System_GC ??= ParseTypeName("global::" + TypeNames.System_GC); + + private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch; + public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch => _System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch); + } public static class TypeNames { public const string DllImportAttribute = "System.Runtime.InteropServices.DllImportAttribute"; @@ -94,9 +204,7 @@ public static string MarshalEx(InteropGenerationOptions options) public const string System_Runtime_InteropServices_InAttribute = "System.Runtime.InteropServices.InAttribute"; - public const string System_Runtime_CompilerServices_SkipLocalsInitAttribute_WithGlobal = "global::System.Runtime.CompilerServices.SkipLocalsInitAttribute"; - - public const string System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata = "System.Runtime.CompilerServices.SkipLocalsInitAttribute"; + public const string System_Runtime_CompilerServices_SkipLocalsInitAttribute = "System.Runtime.CompilerServices.SkipLocalsInitAttribute"; public const string System_Runtime_CompilerServices_Unsafe = "System.Runtime.CompilerServices.Unsafe"; @@ -106,7 +214,7 @@ public static string MarshalEx(InteropGenerationOptions options) public const string DllImportSearchPath = "System.Runtime.InteropServices.DllImportSearchPath"; - public const string System_CodeDom_Compiler_GeneratedCodeAttribute_WithGlobal = "global::System.CodeDom.Compiler.GeneratedCodeAttribute"; + public const string System_CodeDom_Compiler_GeneratedCodeAttribute = "System.CodeDom.Compiler.GeneratedCodeAttribute"; public const string System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute = "System.Runtime.InteropServices.DynamicInterfaceCastableImplementationAttribute"; diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/ComInterfaceGenerator.Tests.csproj b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/ComInterfaceGenerator.Tests.csproj index 7385b403905e02..4c953e51ddb7a8 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/ComInterfaceGenerator.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/ComInterfaceGenerator.Tests.csproj @@ -13,6 +13,7 @@ + diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs index 4608a11b6a2d30..9262e5a36d5151 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs @@ -16,6 +16,8 @@ partial class NativeExportsNE { internal partial class Stateless { + public static string System = "Make sure generated code prefixes type references with global::"; + [LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_deepduplicate")] public static partial void DeepDuplicateStrings(StringContainer strings, out StringContainer pStringsOut); diff --git a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/ISystem.cs b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/ISystem.cs new file mode 100644 index 00000000000000..0e4584c0982587 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/ISystem.cs @@ -0,0 +1,19 @@ +// 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; + +namespace SharedTypes.ComInterfaces +{ + [GeneratedComInterface(StringMarshalling = StringMarshalling.Utf8)] + [Guid(IID)] + internal partial interface ISystem + { + // Make sure method names System and Microsoft don't interfere with framework type / method references + void Microsoft(int p); + void System(int p); + public const string IID = "3BFFE3FD-D11E-4195-8250-0C73321977A0"; + } +} From dfa96c4d85740a11783ef5a545cae26eb4f90bb7 Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Fri, 11 Aug 2023 17:54:48 +0000 Subject: [PATCH 2/5] wip --- .../ComInterfaceGenerator.cs | 2 +- .../ComInterfaceGenerator/ComInterfaceInfo.cs | 2 +- .../VirtualMethodPointerStubGenerator.cs | 2 +- .../LibraryImportGenerator.cs | 38 +++---- .../Marshalling/ElementsMarshalling.cs | 4 +- .../Marshalling/MarshallerHelpers.cs | 2 +- .../TypeNames.cs | 101 +++++++++++------- .../AdditionalAttributesOnStub.cs | 3 +- .../TestAssets/SharedTypes/SharedTypes.csproj | 3 + 9 files changed, 93 insertions(+), 64 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs index 54ec749e233e3a..d0f4f207df31b4 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs @@ -193,7 +193,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.RegisterSourceOutput(filesToGenerate, (context, data) => { - context.AddSource(data.TypeName.Replace("global::", ""), data.Source); + context.AddSource(data.TypeName.Replace(TypeNames.GlobalAlias, ""), data.Source); }); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs index 30c0e66f3cf3dd..0aed46284aa53e 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs @@ -123,7 +123,7 @@ private static bool StringMarshallingIsValid( stringMarshallingDiagnostic = DiagnosticInfo.Create( GeneratorDiagnostics.StringMarshallingCustomTypeNotAccessibleByGeneratedCode, syntax.Identifier.GetLocation(), - attrInfo.StringMarshallingCustomType.FullTypeName.Replace("global::", ""), + attrInfo.StringMarshallingCustomType.FullTypeName.Replace(TypeNames.GlobalAlias, ""), details); return false; } diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs index 2fc5f14f649eaa..f46790817c4ba2 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs @@ -94,7 +94,7 @@ public static (MethodDeclarationSyntax, ImmutableArray) Generate ImplicitArrayCreationExpression( InitializerExpression(SyntaxKind.CollectionInitializerExpression, SeparatedList( - methodStub.CallingConvention.Array.Select(callConv => TypeOfExpression(ParseName($"global::System.Runtime.CompilerServices.CallConv{callConv.Name.ValueText}"))))))) + methodStub.CallingConvention.Array.Select(callConv => TypeOfExpression(TypeSyntaxes.CallConv(callConv.Name.ValueText))))))) .WithNameEquals(NameEquals(IdentifierName("CallConvs")))); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs index cd6d0e4833a4f4..6b3bd184664973 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs @@ -425,25 +425,25 @@ private static LocalFunctionStatementSyntax CreateTargetDllImportAsLocalStatemen SingletonSeparatedList( Attribute( NameSyntaxes.DllImportAttribute, - AttributeArgumentList( - SeparatedList( - new[] - { - AttributeArgument(LiteralExpression( - SyntaxKind.StringLiteralExpression, - Literal(libraryImportData.ModuleName))), - AttributeArgument( - NameEquals(nameof(DllImportAttribute.EntryPoint)), - null, - LiteralExpression( - SyntaxKind.StringLiteralExpression, - Literal(libraryImportData.EntryPoint ?? stubMethodName))), - AttributeArgument( - NameEquals(nameof(DllImportAttribute.ExactSpelling)), - null, - LiteralExpression(SyntaxKind.TrueLiteralExpression)) - } - ))))))) + AttributeArgumentList( + SeparatedList( + new[] + { + AttributeArgument(LiteralExpression( + SyntaxKind.StringLiteralExpression, + Literal(libraryImportData.ModuleName))), + AttributeArgument( + NameEquals(nameof(DllImportAttribute.EntryPoint)), + null, + LiteralExpression( + SyntaxKind.StringLiteralExpression, + Literal(libraryImportData.EntryPoint ?? stubMethodName))), + AttributeArgument( + NameEquals(nameof(DllImportAttribute.ExactSpelling)), + null, + LiteralExpression(SyntaxKind.TrueLiteralExpression)) + } + ))))))) .WithParameterList(parameterList); if (returnTypeAttributes is not null) { diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs index c59a474635df1f..10c7d7a8c34fd8 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs @@ -379,7 +379,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta Argument( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - NameSyntaxes.System_Runtime_CompilerServices_Unsafe, + TypeSyntaxes.System_Runtime_CompilerServices_Unsafe, IdentifierName("AsRef")), ArgumentList(SingletonSeparatedList( Argument( @@ -503,7 +503,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState Argument( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - NameSyntaxes.System_Runtime_CompilerServices_Unsafe, + TypeSyntaxes.System_Runtime_CompilerServices_Unsafe, IdentifierName("AsRef")), ArgumentList(SingletonSeparatedList( Argument( diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs index e58728554bbecd..38200e9f694b56 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshallerHelpers.cs @@ -327,7 +327,7 @@ public static StatementSyntax SkipInitOrDefaultInit(TypePositionInfo info, StubC return ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - NameSyntaxes.System_Runtime_CompilerServices_Unsafe, + TypeSyntaxes.System_Runtime_CompilerServices_Unsafe, IdentifierName("SkipInit"))) .WithArgumentList( ArgumentList(SingletonSeparatedList( diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs index 3208062d81e6dd..79a228afce9202 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs @@ -1,6 +1,8 @@ // 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 Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -9,112 +11,130 @@ namespace Microsoft.Interop public static class NameSyntaxes { private static NameSyntax? _DllImportAttribute; - public static NameSyntax DllImportAttribute => _DllImportAttribute ??= ParseName("global::" + TypeNames.DllImportAttribute); + public static NameSyntax DllImportAttribute => _DllImportAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.DllImportAttribute); private static NameSyntax? _LibraryImportAttribute; - public static NameSyntax LibraryImportAttribute => _LibraryImportAttribute ??= ParseName("global::" + TypeNames.LibraryImportAttribute); - - private static NameSyntax? _System_Runtime_CompilerServices_Unsafe; - public static NameSyntax System_Runtime_CompilerServices_Unsafe => _System_Runtime_CompilerServices_Unsafe ??= ParseName("global::" + TypeNames.System_Runtime_CompilerServices_Unsafe); + public static NameSyntax LibraryImportAttribute => _LibraryImportAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.LibraryImportAttribute); private static NameSyntax? _System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute; - public static NameSyntax System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute => _System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute ??= ParseName("global::" + TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute); + public static NameSyntax System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute => _System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute); private static NameSyntax? _System_Runtime_InteropServices_MarshalAsAttribute; - public static NameSyntax System_Runtime_InteropServices_MarshalAsAttribute => _System_Runtime_InteropServices_MarshalAsAttribute ??= ParseName("global::" + TypeNames.System_Runtime_InteropServices_MarshalAsAttribute); + public static NameSyntax System_Runtime_InteropServices_MarshalAsAttribute => _System_Runtime_InteropServices_MarshalAsAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_MarshalAsAttribute); private static NameSyntax? _DefaultDllImportSearchPathsAttribute; - public static NameSyntax DefaultDllImportSearchPathsAttribute => _DefaultDllImportSearchPathsAttribute ??= ParseName("global::" + TypeNames.DefaultDllImportSearchPathsAttribute); + public static NameSyntax DefaultDllImportSearchPathsAttribute => _DefaultDllImportSearchPathsAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.DefaultDllImportSearchPathsAttribute); private static NameSyntax? _SuppressGCTransitionAttribute; - public static NameSyntax SuppressGCTransitionAttribute => _SuppressGCTransitionAttribute ??= ParseName("global::" + TypeNames.SuppressGCTransitionAttribute); + public static NameSyntax SuppressGCTransitionAttribute => _SuppressGCTransitionAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.SuppressGCTransitionAttribute); private static NameSyntax? _UnmanagedCallConvAttribute; - public static NameSyntax UnmanagedCallConvAttribute => _UnmanagedCallConvAttribute ??= ParseName("global::" + TypeNames.UnmanagedCallConvAttribute); + public static NameSyntax UnmanagedCallConvAttribute => _UnmanagedCallConvAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.UnmanagedCallConvAttribute); private static NameSyntax? _System_Runtime_CompilerServices_SkipLocalsInitAttribute; - public static NameSyntax System_Runtime_CompilerServices_SkipLocalsInitAttribute => _System_Runtime_CompilerServices_SkipLocalsInitAttribute ??= ParseName("global::" + TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute); + public static NameSyntax System_Runtime_CompilerServices_SkipLocalsInitAttribute => _System_Runtime_CompilerServices_SkipLocalsInitAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute); private static NameSyntax? _System_CodeDom_Compiler_GeneratedCodeAttribute; - public static NameSyntax System_CodeDom_Compiler_GeneratedCodeAttribute => _System_CodeDom_Compiler_GeneratedCodeAttribute ??= ParseName("global::" + TypeNames.System_CodeDom_Compiler_GeneratedCodeAttribute); + public static NameSyntax System_CodeDom_Compiler_GeneratedCodeAttribute => _System_CodeDom_Compiler_GeneratedCodeAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_CodeDom_Compiler_GeneratedCodeAttribute); private static NameSyntax? _UnmanagedCallersOnlyAttribute; - public static NameSyntax UnmanagedCallersOnlyAttribute => _UnmanagedCallersOnlyAttribute ??= ParseName("global::" + TypeNames.UnmanagedCallersOnlyAttribute); + public static NameSyntax UnmanagedCallersOnlyAttribute => _UnmanagedCallersOnlyAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.UnmanagedCallersOnlyAttribute); } public static class TypeSyntaxes { private static TypeSyntax? _StringMarshalling; - public static TypeSyntax StringMarshalling => _StringMarshalling ??= ParseTypeName("global::" + TypeNames.StringMarshalling); + public static TypeSyntax StringMarshalling => _StringMarshalling ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.StringMarshalling); private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry; - public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry => _System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry); + public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry => _System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry); private static TypeSyntax? _System_Runtime_InteropServices_NativeMemory; - public static TypeSyntax System_Runtime_InteropServices_NativeMemory => _System_Runtime_InteropServices_NativeMemory ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_NativeMemory); + public static TypeSyntax System_Runtime_InteropServices_NativeMemory => _System_Runtime_InteropServices_NativeMemory ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_NativeMemory); private static TypeSyntax? _StrategyBasedComWrappers; - public static TypeSyntax StrategyBasedComWrappers => _StrategyBasedComWrappers ??= ParseTypeName("global::" + TypeNames.StrategyBasedComWrappers); + public static TypeSyntax StrategyBasedComWrappers => _StrategyBasedComWrappers ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.StrategyBasedComWrappers); private static TypeSyntax? _IUnmanagedVirtualMethodTableProvider; - public static TypeSyntax IUnmanagedVirtualMethodTableProvider => _IUnmanagedVirtualMethodTableProvider ??= ParseTypeName("global::" + TypeNames.IUnmanagedVirtualMethodTableProvider); + public static TypeSyntax IUnmanagedVirtualMethodTableProvider => _IUnmanagedVirtualMethodTableProvider ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IUnmanagedVirtualMethodTableProvider); private static TypeSyntax? _IIUnknownInterfaceType; - public static TypeSyntax IIUnknownInterfaceType => _IIUnknownInterfaceType ??= ParseTypeName("global::" + TypeNames.IIUnknownInterfaceType); + public static TypeSyntax IIUnknownInterfaceType => _IIUnknownInterfaceType ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IIUnknownInterfaceType); private static TypeSyntax? _IIUnknownDerivedDetails; - public static TypeSyntax IIUnknownDerivedDetails => _IIUnknownDerivedDetails ??= ParseTypeName("global::" + TypeNames.IIUnknownDerivedDetails); + public static TypeSyntax IIUnknownDerivedDetails => _IIUnknownDerivedDetails ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IIUnknownDerivedDetails); private static TypeSyntax? _UnmanagedObjectUnwrapper; - public static TypeSyntax UnmanagedObjectUnwrapper => _UnmanagedObjectUnwrapper ??= ParseTypeName("global::" + TypeNames.UnmanagedObjectUnwrapper); + public static TypeSyntax UnmanagedObjectUnwrapper => _UnmanagedObjectUnwrapper ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.UnmanagedObjectUnwrapper); private static TypeSyntax? _IComExposedClass; - public static TypeSyntax IComExposedClass => _IComExposedClass ??= ParseTypeName("global::" + TypeNames.IComExposedClass); + public static TypeSyntax IComExposedClass => _IComExposedClass ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IComExposedClass); private static TypeSyntax? _UnreachableException; - public static TypeSyntax UnreachableException => _UnreachableException ??= ParseTypeName("global::" + TypeNames.UnreachableException); + public static TypeSyntax UnreachableException => _UnreachableException ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.UnreachableException); private static TypeSyntax? _System_Runtime_CompilerServices_RuntimeHelpers; - public static TypeSyntax System_Runtime_CompilerServices_RuntimeHelpers => _System_Runtime_CompilerServices_RuntimeHelpers ??= ParseTypeName("global::" + TypeNames.System_Runtime_CompilerServices_RuntimeHelpers); + public static TypeSyntax System_Runtime_CompilerServices_RuntimeHelpers => _System_Runtime_CompilerServices_RuntimeHelpers ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_RuntimeHelpers); private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers; - public static TypeSyntax System_Runtime_InteropServices_ComWrappers => _System_Runtime_InteropServices_ComWrappers ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_ComWrappers); + public static TypeSyntax System_Runtime_InteropServices_ComWrappers => _System_Runtime_InteropServices_ComWrappers ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_ComWrappers); private static TypeSyntax? _System_IntPtr; - public static TypeSyntax System_IntPtr => _System_IntPtr ??= ParseTypeName("global::" + TypeNames.System_IntPtr); + public static TypeSyntax System_IntPtr => _System_IntPtr ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_IntPtr); private static TypeSyntax? _System_Guid; - public static TypeSyntax System_Guid => _System_Guid ??= ParseTypeName("global::" + TypeNames.System_Guid); + public static TypeSyntax System_Guid => _System_Guid ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Guid); private static TypeSyntax? _DllImportSearchPath; - public static TypeSyntax DllImportSearchPath => _DllImportSearchPath ??= ParseTypeName("global::" + TypeNames.DllImportSearchPath); + public static TypeSyntax DllImportSearchPath => _DllImportSearchPath ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.DllImportSearchPath); private static TypeSyntax? _System_Type; - public static TypeSyntax System_Type => _System_Type ??= ParseTypeName("global::" + TypeNames.System_Type); + public static TypeSyntax System_Type => _System_Type ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Type); private static TypeSyntax? _System_Activator; - public static TypeSyntax System_Activator => _System_Activator ??= ParseTypeName("global::" + TypeNames.System_Activator); + public static TypeSyntax System_Activator => _System_Activator ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Activator); private static TypeSyntax? _System_Runtime_InteropServices_Marshal; - public static TypeSyntax System_Runtime_InteropServices_Marshal => _System_Runtime_InteropServices_Marshal ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_Marshal); + public static TypeSyntax System_Runtime_InteropServices_Marshal => _System_Runtime_InteropServices_Marshal ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_Marshal); private static TypeSyntax? _System_Runtime_InteropServices_UnmanagedType; - public static TypeSyntax System_Runtime_InteropServices_UnmanagedType => _System_Runtime_InteropServices_UnmanagedType ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_UnmanagedType); + public static TypeSyntax System_Runtime_InteropServices_UnmanagedType => _System_Runtime_InteropServices_UnmanagedType ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_UnmanagedType); private static TypeSyntax? _System_Runtime_InteropServices_MemoryMarshal; - public static TypeSyntax System_Runtime_InteropServices_MemoryMarshal => _System_Runtime_InteropServices_MemoryMarshal ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_MemoryMarshal); + public static TypeSyntax System_Runtime_InteropServices_MemoryMarshal => _System_Runtime_InteropServices_MemoryMarshal ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_MemoryMarshal); private static TypeSyntax? _System_Exception; - public static TypeSyntax System_Exception => _System_Exception ??= ParseTypeName("global::" + TypeNames.System_Exception); + public static TypeSyntax System_Exception => _System_Exception ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Exception); private static TypeSyntax? _System_GC; - public static TypeSyntax System_GC => _System_GC ??= ParseTypeName("global::" + TypeNames.System_GC); + public static TypeSyntax System_GC => _System_GC ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_GC); private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch; - public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch => _System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch ??= ParseTypeName("global::" + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch); + public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch => _System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch); + + private static TypeSyntax? _System_Runtime_CompilerServices_Unsafe; + public static TypeSyntax System_Runtime_CompilerServices_Unsafe => _System_Runtime_CompilerServices_Unsafe ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_Unsafe); + + private static TypeSyntax? _CallConvCDecl; + private static TypeSyntax? _CallConvFastcall; + private static TypeSyntax? _CallConvStdcall; + private static TypeSyntax? _CallConvThiscall; + public static TypeSyntax CallConv(string callConv) + { + return callConv switch + { + "CDecl" => _CallConvCDecl ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvCDeclName), + "Fastcall" => _CallConvFastcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvFastcallName), + "Stdcall" => _CallConvStdcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvStdcallName), + "Thiscall" => _CallConvThiscall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvThiscallName), + _ => throw new ArgumentException("Unexpected CallConv") + }; + } } public static class TypeNames { + public const string GlobalAlias = "global::"; + public const string DllImportAttribute = "System.Runtime.InteropServices.DllImportAttribute"; public const string LibraryImportAttribute = "System.Runtime.InteropServices.LibraryImportAttribute"; public const string LibraryImportAttribute_ShortName = "LibraryImportAttribute"; @@ -156,9 +176,9 @@ public static class TypeNames public const string IUnmanagedInterfaceType_Metadata = "System.Runtime.InteropServices.Marshalling.IUnmanagedInterfaceType"; public const string System_Span_Metadata = "System.Span`1"; - public const string System_Span = "System.Span"; + public const string System_Span = GlobalAlias + "System.Span"; public const string System_ReadOnlySpan_Metadata = "System.ReadOnlySpan`1"; - public const string System_ReadOnlySpan = "System.ReadOnlySpan"; + public const string System_ReadOnlySpan = GlobalAlias + "System.ReadOnlySpan"; public const string System_IntPtr = "System.IntPtr"; @@ -270,5 +290,10 @@ public static string MarshalEx(InteropGenerationOptions options) public const string System_Runtime_InteropServices_CULong = "System.Runtime.InteropServices.CULong"; public const string System_Runtime_InteropServices_NFloat = "System.Runtime.InteropServices.NFloat"; + + public const string CallConvCDeclName = "System.Runtime.CompilerServices.CallConvCDecl"; + public const string CallConvFastcallName = "System.Runtime.CompilerServices.CallConvFastcall"; + public const string CallConvStdcallName = "System.Runtime.CompilerServices.CallConvStdcall"; + public const string CallConvThiscallName = "System.Runtime.CompilerServices.CallConvThiscall"; } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs index 72e9a8bb87c0b3..cf85b92c678c95 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Testing; +using Microsoft.Interop; using Microsoft.Interop.UnitTests; using System.Collections.Generic; using System.Linq; @@ -286,7 +287,7 @@ bool ValidateAttribute(AttributeData attr) } AttributeSyntax syntax = (AttributeSyntax)attr.ApplicationSyntaxReference!.GetSyntax(); - return syntax.Name.ToString().StartsWith("global::"); + return syntax.Name.ToString().StartsWith(TypeNames.GlobalAlias); } } else diff --git a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj index c68eadac7ca4b4..e15d132162ce4d 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj @@ -3,10 +3,13 @@ $(NetCoreAppCurrent) true + $(OutputPath)\Generated + $(InteropCompilerGeneratedFilesOutputPath)\ComInterfaceGenerator.Tests + From a46d5feb5099e8d426b26fe856bc21c87065c6a1 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Fri, 11 Aug 2023 13:28:54 -0700 Subject: [PATCH 3/5] wip --- .../Analyzers/AddMarshalAsToElementFixer.cs | 4 ++-- ...rtComImportToGeneratedComInterfaceFixer.cs | 2 +- .../ComClassGenerator.cs | 2 +- .../ComInterfaceGenerator.cs | 2 +- .../ComInterfaceDispatchMarshallerFactory.cs | 2 +- .../VirtualMethodPointerStubGenerator.cs | 2 +- ...DisableRuntimeMarshallingAttributeFixer.cs | 2 +- .../Analyzers/ConvertToLibraryImportFixer.cs | 2 +- .../MarshalAsAttributeParser.cs | 8 ++++---- .../MarshallingAttributeInfo.cs | 19 +++++++------------ .../StringMarshallingInfoProvider.cs | 4 ++-- .../TypeNames.cs | 8 +++++++- 12 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs index dc1037aa527607..e0d2dfc7496ba1 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs @@ -53,10 +53,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) SyntaxGenerator gen = editor.Generator; SyntaxNode marshalAsAttribute = gen.Attribute( - TypeNames.System_Runtime_InteropServices_MarshalAsAttribute, + TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_MarshalAsAttribute, gen.AttributeArgument( gen.MemberAccessExpression( - gen.DottedName(TypeNames.System_Runtime_InteropServices_UnmanagedType), + gen.DottedName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_UnmanagedType), gen.IdentifierName(unmanagedTypeName.Trim())))); if (node.IsKind(SyntaxKind.MethodDeclaration)) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs index 8db74fa70244c9..03621a0a4d12b3 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs @@ -69,7 +69,7 @@ private static async Task ConvertComImportToGeneratedComInterfaceAsync(DocumentE generatedComInterfaceAttribute, new[] { - gen.AttributeArgument("StringMarshalling", gen.MemberAccessExpression(gen.DottedName(TypeNames.StringMarshalling), gen.IdentifierName(nameof(StringMarshalling.Custom)))), + gen.AttributeArgument("StringMarshalling", gen.MemberAccessExpression(gen.DottedName(TypeNames.GlobalAlias + TypeNames.StringMarshalling), gen.IdentifierName(nameof(StringMarshalling.Custom)))), gen.AttributeArgument("StringMarshallingCustomType", gen.TypeOfExpression(gen.TypeExpression(comp.GetTypeByMetadataName(TypeNames.BStrStringMarshaller)))) }); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs index ce96a17b1d6a9b..49549260a129be 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs @@ -104,7 +104,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) private static readonly AttributeSyntax s_comExposedClassAttributeTemplate = Attribute( - GenericName(TypeNames.ComExposedClassAttribute) + GenericName(TypeNames.GlobalAlias + TypeNames.ComExposedClassAttribute) .AddTypeArgumentListArguments( IdentifierName(ClassInfoTypeName))); private static MemberDeclarationSyntax GenerateClassInfoAttributeOnUserType(ContainingSyntaxContext containingSyntaxContext, ContainingSyntax classSyntax) => diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs index d0f4f207df31b4..c39340c7e1edd2 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs @@ -199,7 +199,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) private static readonly AttributeSyntax s_iUnknownDerivedAttributeTemplate = Attribute( - GenericName(TypeNames.IUnknownDerivedAttribute) + GenericName(TypeNames.GlobalAlias + TypeNames.IUnknownDerivedAttribute) .AddTypeArgumentListArguments( IdentifierName("InterfaceInformation"), IdentifierName("InterfaceImplementation"))); diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ComInterfaceDispatchMarshallerFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ComInterfaceDispatchMarshallerFactory.cs index 3c6dba299113de..a75cd8cad77268 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ComInterfaceDispatchMarshallerFactory.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/ComInterfaceDispatchMarshallerFactory.cs @@ -29,7 +29,7 @@ private sealed class Marshaller : IMarshallingGenerator { public ManagedTypeInfo AsNativeType(TypePositionInfo info) => new PointerTypeInfo( - $"global::{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", + $"{TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", $"{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*", IsFunctionPointer: false); public IEnumerable Generate(TypePositionInfo info, StubCodeContext context) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs index f46790817c4ba2..647312729b28dc 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodPointerStubGenerator.cs @@ -132,7 +132,7 @@ private static ImmutableArray AddImplicitElementInfos(Incremen { elements.Add( new TypePositionInfo( - new ReferenceTypeInfo($"global::{TypeNames.System_Exception}", TypeNames.System_Exception), + new ReferenceTypeInfo(TypeNames.GlobalAlias + TypeNames.System_Exception, TypeNames.System_Exception), methodStub.ExceptionMarshallingInfo) { InstanceIdentifier = "__exception", diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs index c7ede679e59831..e7ed6b3a93ab1f 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs @@ -65,7 +65,7 @@ private static async Task AddDisableRuntimeMarshallingAttributeApplica syntaxRoot, editor.Generator.AddAttributes( syntaxRoot, - editor.Generator.Attribute(editor.Generator.DottedName(TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute)))); + editor.Generator.Attribute(editor.Generator.DottedName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute)))); return editor.GetChangedDocument().Project.Solution; diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs index 8fd974ba2f90c0..7a80245383f6a5 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs @@ -602,7 +602,7 @@ private static bool TryCreateUnmanagedCallConvAttributeToEmit( return false; } - unmanagedCallConvAttribute = generator.Attribute(TypeNames.UnmanagedCallConvAttribute, + unmanagedCallConvAttribute = generator.Attribute(TypeNames.GlobalAlias + TypeNames.UnmanagedCallConvAttribute, generator.AttributeArgument("CallConvs", generator.ArrayCreationExpression( generator.TypeExpression(editor.SemanticModel.Compilation.GetBestTypeByMetadataName(TypeNames.System_Type)), diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs index 476c1c6d17f756..27bfbb30d7e233 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs @@ -162,10 +162,10 @@ private MarshallingInfo CreateStringMarshallingInfo( { string? marshallerName = unmanagedType switch { - UnmanagedType.BStr => TypeNames.BStrStringMarshaller, - UnmanagedType.LPStr => TypeNames.AnsiStringMarshaller, - UnmanagedType.LPTStr or UnmanagedType.LPWStr => TypeNames.Utf16StringMarshaller, - MarshalAsInfo.UnmanagedType_LPUTF8Str => TypeNames.Utf8StringMarshaller, + UnmanagedType.BStr => TypeNames.GlobalAlias + TypeNames.BStrStringMarshaller, + UnmanagedType.LPStr => TypeNames.GlobalAlias + TypeNames.AnsiStringMarshaller, + UnmanagedType.LPTStr or UnmanagedType.LPWStr => TypeNames.GlobalAlias + TypeNames.Utf16StringMarshaller, + MarshalAsInfo.UnmanagedType_LPUTF8Str => TypeNames.GlobalAlias + TypeNames.Utf8StringMarshaller, _ => null }; diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs index 89c85ee235a34e..1c9e1974d82373 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs @@ -1,14 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Diagnostics; -using System.Linq; -using System.Runtime.InteropServices; namespace Microsoft.Interop { @@ -155,12 +150,12 @@ internal static MarshallingInfo CreateSpecificMarshallingInfo(ManagedTypeInfo un { return unmanagedReturnType switch { - SpecialTypeInfo(_, _, SpecialType.System_Void) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsVoidMarshaller}", unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_Int32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_UInt32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_Single) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_Double) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), - _ => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsDefaultMarshaller}<{MarshallerHelpers.GetCompatibleGenericTypeParameterSyntax(SyntaxFactory.ParseTypeName(unmanagedReturnType.FullTypeName))}>", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Void) => CreateWellKnownComExceptionMarshallingData(TypeNames.GlobalAlias + TypeNames.ExceptionAsVoidMarshaller, unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Int32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_UInt32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Single) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Double) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), + _ => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsDefaultMarshaller}<{MarshallerHelpers.GetCompatibleGenericTypeParameterSyntax(SyntaxFactory.ParseTypeName(unmanagedReturnType.FullTypeName))}>", unmanagedReturnType), }; static NativeMarshallingAttributeInfo CreateWellKnownComExceptionMarshallingData(string marshallerName, ManagedTypeInfo unmanagedType) diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs index 05a060019ab3f5..c157589db446a7 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs @@ -61,8 +61,8 @@ public MarshallingInfo GetMarshallingInfo(ITypeSymbol type, int indirectionDepth // No marshalling info was computed, but a character encoding was provided. return _defaultMarshallingInfo.CharEncoding switch { - CharEncoding.Utf16 => CreateStringMarshallingInfo(_compilation, type, TypeNames.Utf16StringMarshaller), - CharEncoding.Utf8 => CreateStringMarshallingInfo(_compilation, type, TypeNames.Utf8StringMarshaller), + CharEncoding.Utf16 => CreateStringMarshallingInfo(_compilation, type, TypeNames.GlobalAlias + TypeNames.Utf16StringMarshaller), + CharEncoding.Utf8 => CreateStringMarshallingInfo(_compilation, type, TypeNames.GlobalAlias + TypeNames.Utf8StringMarshaller), _ => throw new InvalidOperationException() }; } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs index 79a228afce9202..5b0ae5c3658d11 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs @@ -117,7 +117,9 @@ public static class TypeSyntaxes private static TypeSyntax? _CallConvCDecl; private static TypeSyntax? _CallConvFastcall; + private static TypeSyntax? _CallConvMemberFunction; private static TypeSyntax? _CallConvStdcall; + private static TypeSyntax? _CallConvSuppressGCTransition; private static TypeSyntax? _CallConvThiscall; public static TypeSyntax CallConv(string callConv) { @@ -125,9 +127,11 @@ public static TypeSyntax CallConv(string callConv) { "CDecl" => _CallConvCDecl ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvCDeclName), "Fastcall" => _CallConvFastcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvFastcallName), + "MemberFunction" => _CallConvMemberFunction ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvMemberFunctionName), "Stdcall" => _CallConvStdcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvStdcallName), + "SuppressGCTransition" => _CallConvSuppressGCTransition ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvSuppressGCTransitionName), "Thiscall" => _CallConvThiscall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvThiscallName), - _ => throw new ArgumentException("Unexpected CallConv") + _ => throw new ArgumentException($"Unexpected CallConv: {callConv}") }; } } @@ -295,5 +299,7 @@ public static string MarshalEx(InteropGenerationOptions options) public const string CallConvFastcallName = "System.Runtime.CompilerServices.CallConvFastcall"; public const string CallConvStdcallName = "System.Runtime.CompilerServices.CallConvStdcall"; public const string CallConvThiscallName = "System.Runtime.CompilerServices.CallConvThiscall"; + public const string CallConvSuppressGCTransitionName = "System.Runtime.CompilerServices.CallConvSuppressGCTransition"; + public const string CallConvMemberFunctionName = "System.Runtime.CompilerServices.CallConvMemberFunction"; } } From b691e3f182bc19c2ff1cfd2cbcc1ccbc0276e3ec Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Fri, 11 Aug 2023 14:10:02 -0700 Subject: [PATCH 4/5] Fixed all cases --- .../Analyzers/AddMarshalAsToElementFixer.cs | 8 ++------ ...ConvertComImportToGeneratedComInterfaceFixer.cs | 2 +- .../AddDisableRuntimeMarshallingAttributeFixer.cs | 4 +--- .../Analyzers/ConvertToLibraryImportFixer.cs | 2 +- .../MarshalAsAttributeParser.cs | 8 ++++---- .../MarshallingAttributeInfo.cs | 14 +++++++------- .../StringMarshallingInfoProvider.cs | 6 ++---- .../TypeNames.cs | 7 +++---- ...DisableRuntimeMarshallingAttributeFixerTests.cs | 3 +-- 9 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs index e0d2dfc7496ba1..ebb902ff46ff62 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs @@ -1,12 +1,8 @@ // 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.Collections.Immutable; using System.Composition; -using System.Runtime.InteropServices; -using System.Text; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; @@ -53,10 +49,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) SyntaxGenerator gen = editor.Generator; SyntaxNode marshalAsAttribute = gen.Attribute( - TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_MarshalAsAttribute, + TypeNames.System_Runtime_InteropServices_MarshalAsAttribute, gen.AttributeArgument( gen.MemberAccessExpression( - gen.DottedName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_UnmanagedType), + gen.DottedName(TypeNames.System_Runtime_InteropServices_UnmanagedType), gen.IdentifierName(unmanagedTypeName.Trim())))); if (node.IsKind(SyntaxKind.MethodDeclaration)) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs index 03621a0a4d12b3..8db74fa70244c9 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceFixer.cs @@ -69,7 +69,7 @@ private static async Task ConvertComImportToGeneratedComInterfaceAsync(DocumentE generatedComInterfaceAttribute, new[] { - gen.AttributeArgument("StringMarshalling", gen.MemberAccessExpression(gen.DottedName(TypeNames.GlobalAlias + TypeNames.StringMarshalling), gen.IdentifierName(nameof(StringMarshalling.Custom)))), + gen.AttributeArgument("StringMarshalling", gen.MemberAccessExpression(gen.DottedName(TypeNames.StringMarshalling), gen.IdentifierName(nameof(StringMarshalling.Custom)))), gen.AttributeArgument("StringMarshallingCustomType", gen.TypeOfExpression(gen.TypeExpression(comp.GetTypeByMetadataName(TypeNames.BStrStringMarshaller)))) }); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs index e7ed6b3a93ab1f..d08e76d952cf9b 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs @@ -1,12 +1,10 @@ // 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.Collections.Immutable; using System.Composition; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; @@ -65,7 +63,7 @@ private static async Task AddDisableRuntimeMarshallingAttributeApplica syntaxRoot, editor.Generator.AddAttributes( syntaxRoot, - editor.Generator.Attribute(editor.Generator.DottedName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute)))); + editor.Generator.Attribute(editor.Generator.DottedName(TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute)))); return editor.GetChangedDocument().Project.Solution; diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs index 7a80245383f6a5..8fd974ba2f90c0 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs @@ -602,7 +602,7 @@ private static bool TryCreateUnmanagedCallConvAttributeToEmit( return false; } - unmanagedCallConvAttribute = generator.Attribute(TypeNames.GlobalAlias + TypeNames.UnmanagedCallConvAttribute, + unmanagedCallConvAttribute = generator.Attribute(TypeNames.UnmanagedCallConvAttribute, generator.AttributeArgument("CallConvs", generator.ArrayCreationExpression( generator.TypeExpression(editor.SemanticModel.Compilation.GetBestTypeByMetadataName(TypeNames.System_Type)), diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs index 27bfbb30d7e233..476c1c6d17f756 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshalAsAttributeParser.cs @@ -162,10 +162,10 @@ private MarshallingInfo CreateStringMarshallingInfo( { string? marshallerName = unmanagedType switch { - UnmanagedType.BStr => TypeNames.GlobalAlias + TypeNames.BStrStringMarshaller, - UnmanagedType.LPStr => TypeNames.GlobalAlias + TypeNames.AnsiStringMarshaller, - UnmanagedType.LPTStr or UnmanagedType.LPWStr => TypeNames.GlobalAlias + TypeNames.Utf16StringMarshaller, - MarshalAsInfo.UnmanagedType_LPUTF8Str => TypeNames.GlobalAlias + TypeNames.Utf8StringMarshaller, + UnmanagedType.BStr => TypeNames.BStrStringMarshaller, + UnmanagedType.LPStr => TypeNames.AnsiStringMarshaller, + UnmanagedType.LPTStr or UnmanagedType.LPWStr => TypeNames.Utf16StringMarshaller, + MarshalAsInfo.UnmanagedType_LPUTF8Str => TypeNames.Utf8StringMarshaller, _ => null }; diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs index 1c9e1974d82373..c382262bd47b4a 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs @@ -150,17 +150,17 @@ internal static MarshallingInfo CreateSpecificMarshallingInfo(ManagedTypeInfo un { return unmanagedReturnType switch { - SpecialTypeInfo(_, _, SpecialType.System_Void) => CreateWellKnownComExceptionMarshallingData(TypeNames.GlobalAlias + TypeNames.ExceptionAsVoidMarshaller, unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_Int32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_UInt32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_Single) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), - SpecialTypeInfo(_, _, SpecialType.System_Double) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), - _ => CreateWellKnownComExceptionMarshallingData($"{TypeNames.GlobalAlias + TypeNames.ExceptionAsDefaultMarshaller}<{MarshallerHelpers.GetCompatibleGenericTypeParameterSyntax(SyntaxFactory.ParseTypeName(unmanagedReturnType.FullTypeName))}>", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Void) => CreateWellKnownComExceptionMarshallingData(TypeNames.ExceptionAsVoidMarshaller, unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Int32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_UInt32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsHResultMarshaller}", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Single) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), + SpecialTypeInfo(_, _, SpecialType.System_Double) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsNaNMarshaller}", unmanagedReturnType), + _ => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsDefaultMarshaller}<{MarshallerHelpers.GetCompatibleGenericTypeParameterSyntax(SyntaxFactory.ParseTypeName(unmanagedReturnType.FullTypeName))}>", unmanagedReturnType), }; static NativeMarshallingAttributeInfo CreateWellKnownComExceptionMarshallingData(string marshallerName, ManagedTypeInfo unmanagedType) { - ManagedTypeInfo marshallerTypeInfo = new ReferenceTypeInfo(marshallerName, marshallerName); + ManagedTypeInfo marshallerTypeInfo = new ReferenceTypeInfo(TypeNames.GlobalAlias + marshallerName, marshallerName); return new NativeMarshallingAttributeInfo(marshallerTypeInfo, new CustomTypeMarshallers(ImmutableDictionary.Empty.Add( MarshalMode.UnmanagedToManagedOut, diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs index c157589db446a7..12f69adfef136b 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StringMarshallingInfoProvider.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Collections.Generic; -using System.Text; using Microsoft.CodeAnalysis; namespace Microsoft.Interop @@ -61,8 +59,8 @@ public MarshallingInfo GetMarshallingInfo(ITypeSymbol type, int indirectionDepth // No marshalling info was computed, but a character encoding was provided. return _defaultMarshallingInfo.CharEncoding switch { - CharEncoding.Utf16 => CreateStringMarshallingInfo(_compilation, type, TypeNames.GlobalAlias + TypeNames.Utf16StringMarshaller), - CharEncoding.Utf8 => CreateStringMarshallingInfo(_compilation, type, TypeNames.GlobalAlias + TypeNames.Utf8StringMarshaller), + CharEncoding.Utf16 => CreateStringMarshallingInfo(_compilation, type, TypeNames.Utf16StringMarshaller), + CharEncoding.Utf8 => CreateStringMarshallingInfo(_compilation, type, TypeNames.Utf8StringMarshaller), _ => throw new InvalidOperationException() }; } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs index 5b0ae5c3658d11..ff75472d71feec 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -115,7 +114,7 @@ public static class TypeSyntaxes private static TypeSyntax? _System_Runtime_CompilerServices_Unsafe; public static TypeSyntax System_Runtime_CompilerServices_Unsafe => _System_Runtime_CompilerServices_Unsafe ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_Unsafe); - private static TypeSyntax? _CallConvCDecl; + private static TypeSyntax? _CallConvCdecl; private static TypeSyntax? _CallConvFastcall; private static TypeSyntax? _CallConvMemberFunction; private static TypeSyntax? _CallConvStdcall; @@ -125,7 +124,7 @@ public static TypeSyntax CallConv(string callConv) { return callConv switch { - "CDecl" => _CallConvCDecl ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvCDeclName), + "Cdecl" => _CallConvCdecl ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvCdeclName), "Fastcall" => _CallConvFastcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvFastcallName), "MemberFunction" => _CallConvMemberFunction ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvMemberFunctionName), "Stdcall" => _CallConvStdcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvStdcallName), @@ -295,7 +294,7 @@ public static string MarshalEx(InteropGenerationOptions options) public const string System_Runtime_InteropServices_NFloat = "System.Runtime.InteropServices.NFloat"; - public const string CallConvCDeclName = "System.Runtime.CompilerServices.CallConvCDecl"; + public const string CallConvCdeclName = "System.Runtime.CompilerServices.CallConvCdecl"; public const string CallConvFastcallName = "System.Runtime.CompilerServices.CallConvFastcall"; public const string CallConvStdcallName = "System.Runtime.CompilerServices.CallConvStdcall"; public const string CallConvThiscallName = "System.Runtime.CompilerServices.CallConvThiscall"; diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AddDisableRuntimeMarshallingAttributeFixerTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AddDisableRuntimeMarshallingAttributeFixerTests.cs index 92a6aab6b177b0..e54b6b77a1a1fc 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AddDisableRuntimeMarshallingAttributeFixerTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AddDisableRuntimeMarshallingAttributeFixerTests.cs @@ -4,12 +4,11 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Testing; using Microsoft.Interop; using Xunit; -using System.IO; - using VerifyCS = Microsoft.Interop.UnitTests.Verifiers.CSharpCodeFixVerifier< Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer, Microsoft.Interop.Analyzers.AddDisableRuntimeMarshallingAttributeFixer>; From 4817d72f7f2759799f4bc2f750de329a8e2e0776 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Fri, 11 Aug 2023 14:11:54 -0700 Subject: [PATCH 5/5] undo csproj debug changes --- .../tests/TestAssets/SharedTypes/SharedTypes.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj index e15d132162ce4d..c68eadac7ca4b4 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/SharedTypes.csproj @@ -3,13 +3,10 @@ $(NetCoreAppCurrent) true - $(OutputPath)\Generated - $(InteropCompilerGeneratedFilesOutputPath)\ComInterfaceGenerator.Tests -