Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DllImportGenerator project for easier extensibility #1119

Conversation

jkoritzinsky
Copy link
Member

Introduce a new Microsoft.Interop.SourceGeneration library which contains all of our marshallers and the default implementation of our marshaller selection algorithm.

Build the DllImport source generator on top of this library and refactor the guts of the P/Invoke body stub generation to be usable for more scenarios outside of GeneratedDllImport. For example, this model could be used for scenarios like the following:

  • QCalls
    • These can't use local functions since they have compiler-generated names.
  • OpenGL/Vulkan wrappers
    • Projects like these could use the refactored generator to generate P/Invokes that invoke function pointers instead of DllImport methods. As a result, this refactoring can enable these wrappers for these libraries to support Add custom entry resolver when using DllImportAttribute runtime#1906 with their own GeneratedDllImport-like attribute that they provide along with their source generator.

Make the PInvoke stub code-gen less opinionated to simplify reuse for in other P/Invoke-style scenarios (like QCalls or function-pointer-based targets).
@jkoritzinsky jkoritzinsky added the area-DllImportGenerator Source Generated stubs for P/Invokes in C# label May 12, 2021
@jkotas
Copy link
Member

jkotas commented May 13, 2021

extensibility

Note that analyzers and source generators with dependencies do not work well due to dotnet/roslyn#41785.

@jkoritzinsky
Copy link
Member Author

I honestly thought Roslyn had fixed that by now.

@jkoritzinsky
Copy link
Member Author

I've converted the Microsoft.Interop.SourceGeneration package to be a source package and have the sources included in DllImportGenerator.

I feel like there's something weird with using resx files in a source package, but I don't remember. If anyone else knows anything about how resource strings should be done with source packages, I'd appreciate any pointers.

@jkoritzinsky jkoritzinsky force-pushed the enhanced-extensibility branch from 7e53797 to 7dca201 Compare May 17, 2021 23:22
@jkoritzinsky jkoritzinsky force-pushed the enhanced-extensibility branch from 7dca201 to 458ccab Compare May 17, 2021 23:34
@jkoritzinsky
Copy link
Member Author

Based on offline conversations as well as the fact that the source generator cookbook has instructions for generators with dependencies, we've decided for the .NET 6 time frame to provide the Microsoft.Interop.SourceGeneration package as an assembly instead of as a source package. Since we're not actually planning on shipping it publicly for .NET 6 (same as the DllImportGenerator), we can revisit this decision in the .NET 7 timeframe.

…atorFactory implementation to enable more localized handling of collection element marshalling support with the decorator pattern.
@jkoritzinsky jkoritzinsky merged commit 9ac16c6 into dotnet:feature/DllImportGenerator Sep 10, 2021
@jkoritzinsky jkoritzinsky deleted the enhanced-extensibility branch September 10, 2021 22:25
jkoritzinsky added a commit to jkoritzinsky/runtime that referenced this pull request Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants