-
Notifications
You must be signed in to change notification settings - Fork 121
Emit full "NativeDelegate" types for delegates in 'cswinrtgen' #2163
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
base: user/sergiopedri/delegate-codegen
Are you sure you want to change the base?
Emit full "NativeDelegate" types for delegates in 'cswinrtgen' #2163
Conversation
Added a new case to return the IID for the IMapChangedEventArgs1 interface in WellKnownInterfaceIIDs. This supports recognition of the interface in interop scenarios.
Simplifies and restructures the construction of the delegate 'Invoke' method body by reducing local variables, consolidating instruction setup, and streamlining exception handling. This refactor improves readability and maintainability of the interop delegate generation logic.
Renamed and added jump labels for try and finally blocks in delegate generation to improve clarity and support parameter-specific cleanup. Updated exception handler to use new label names.
Replaces the ldloca_0_invoke variable with an inlined instruction in the InteropTypeDefinitionBuilder.Delegate code, simplifying the instruction list and improving code clarity.
Introduces the NativeParameter class to InteropMethodRewriteFactory, providing initial structure and validation for marshalling native parameters in method rewrites. Includes parameter type checks, marker validation, and basic handling for blittable value types, with placeholders for additional type cases.
Added logic to directly marshal Exception parameters as unmanaged types in InteropMethodRewriteFactory.NativeParameter. This includes removing try/finally markers and inserting instructions to convert Exception to its unmanaged representation using the ExceptionMarshaller.
Introduces a new method to check if an ITypeDescriptor is a void pointer type, improving type analysis capabilities in WindowsRuntimeExtensions.
Adds marshalling and disposal logic for various parameter types in InteropMethodRewriteFactory.NativeParameter, including constructed key-value pairs, nullable value types, strings, types, exceptions, and other value/reference types. Introduces the RewriteBody helper to handle marshalling and cleanup, improving support for complex parameter scenarios in interop method rewriting.
Introduces the NativeParameter class to MethodRewriteInfo for handling native parameters in two-pass IL generation. Also updates ManagedParameter XML documentation to reference the correct factory method.
Introduces handling for MethodRewriteInfo.NativeParameter in the method rewrite switch, enabling native parameter rewriting via InteropMethodRewriteFactory.NativeParameter.RewriteMethod.
Introduces the TrackNativeParameterMethodRewrite method to track method rewrites involving native parameters, complementing the existing managed parameter tracking. Updates XML documentation to clarify the distinction between managed and native parameter rewrites.
The NativeDelegateType method now accepts an InteropGeneratorEmitState parameter and uses it to track native parameter method rewrites for delegate parameters. This change enables better tracking and management of parameter rewrites during interop delegate generation.
Introduces an extension for IList<T> that provides ReferenceIndexOf, which returns the index of an item using reference equality instead of value equality.
Introduces ReferenceContains, which checks for reference equality in IList<T>. This provides a way to determine if a specific object instance exists in the list, complementing the existing ReferenceIndexOf method.
Replaces value-based Contains and IndexOf calls with new ReferenceContains and ReferenceIndexOf methods to ensure instruction and local variable checks are performed by reference equality. This improves correctness when working with IL instruction collections where object identity matters.
Introduces ReferenceRemove to IListExtensions for removing items by reference equality, and ReferenceRemoveRange to CilInstructionCollectionExtensions for batch removal. Also updates ReplaceRange to use consistent parameter naming.
Refactored method names from ReplaceRange to ReferenceReplaceRange and RemoveRange to ReferenceRemoveRange in CilInstructionCollectionExtensions and related factory classes to clarify usage of reference-based instruction replacement and removal. This improves code readability and consistency across instruction manipulation logic.
Standardizes and improves the comparison logic for MethodRewriteInfo and its subclasses. Introduces a generic CompareByMethodRewriteInfo<T> method, uses MemberDefinitionComparer and TypeDescriptorComparer for consistent ordering, and ensures instruction and parameter indices are compared using ReferenceIndexOf for accuracy. Cleans up redundant code and clarifies comparison order for ManagedParameter, NativeParameter, ReturnValue, and RetVal.
Updated logic in InteropTypeDefinitionBuilder for IList, IReadOnlyList, IDictionary, and IReadOnlyDictionary to determine vtable sharing based on whether the ABI type is 'void*' using GetAbiType().IsTypeOfVoidPointer(), instead of checking for reference types or KeyValuePair types. This improves accuracy and consistency in vtable sharing decisions.
Introduces the HasReferenceAbiType method to determine if a type uses a reference ABI type, handling generics, value types, and special cases like Type and Exception. This aids in ABI type resolution for interop scenarios.
Replaces calls to GetAbiType(...).IsTypeOfVoidPointer() with HasReferenceAbiType(...) for improved clarity and consistency when determining if types use reference ABI types in interop builders.
Updated DelegateVftbl and InvokeImpl methods to support custom sender and args types for delegate vtable generation. This enables more flexible vtable definitions beyond the previous fixed void* parameters.
Refactored variable name from sharedReadOnlyDictionaryType to sharedDictionaryType for clarity and consistency in vtable type generation within InteropTypeDefinitionBuilder.IDictionary2.
Introduces logic to share vtable types for IDelegate interfaces when sender and args types are reference types, reducing redundant type generation. Adds new methods and state to track and reuse vtable types in InteropGeneratorEmitState, and updates delegate type generation to utilize this sharing mechanism.
Adds the vftblType parameter to InteropTypeDefinitionBuilder.Delegate.ImplType and updates its usage in InteropGenerator.Emit. This change clarifies the source of the vftbl type and improves method signature accuracy.
Updated XML documentation comments to specify AsmResolver.DotNet.TypeReference instead of the more generic TypeReference, improving clarity for consumers of the InteropReferences class.
Introduces RewriteBodyForTypeOfType to properly handle parameters of type 'Type' in interop method rewriting. This includes declaring necessary local variables, pinning, marshalling, and cleanup logic for TypeReference parameters.
Introduces new type and member references for Nullable<int>, HStringReference, and related marshalling methods. Also adds references for string member accessors and updates XML documentation for clarity.
Implemented RewriteBodyForTypeOfString to correctly process string parameters by pinning, length calculation, and HStringReference marshalling. This replaces the previous placeholder logic and ensures proper handling of string arguments in interop scenarios.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements comprehensive code generation for delegate types in cswinrtgen, specifically focusing on emitting full "NativeDelegate" types with proper Vftbl generation for generic delegates and two-pass rewriting for native parameters.
Key Changes:
- Implements two-pass IL rewriting for native parameters (parameters passed to native methods)
- Adds
Vftbltype generation for generic delegates with proper type parameter handling - Enhances comparison logic for method rewrite operations to support new native parameter tracking
- Adds infrastructure for marshalling native parameters including
stringandTypewith specialized handling
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| WellKnownInterfaceIIDs.cs | Added IID mapping for IMapChangedEventArgs1 interface |
| InteropReferences.cs | Updated XML doc comments and added new references for marshalling infrastructure (TypeReference, HStringReference, NullableInt32, string methods) |
| MethodRewriteInfo/*.cs | Refactored comparison logic and added new NativeParameter class for two-pass IL generation |
| InteropGeneratorEmitState.cs | Added tracking for delegate vtable types and native parameter method rewrites |
| InteropGenerator.Emit.cs | Integrated Vftbl generation and native parameter rewriting into the emit pipeline |
| WellKnownTypeSignatureFactory.cs | Updated InvokeImpl signature to accept sender and args types instead of using void* |
| WellKnownTypeDefinitionFactory.cs | Enhanced DelegateVftbl with overload accepting specific sender/args types |
| InteropMethodRewriteFactory.*.cs | Implemented native parameter marshalling logic with specialized handling for strings and types |
| WindowsRuntimeExtensions.cs | Added IsTypeOfVoidPointer and HasReferenceAbiType helper methods |
| IListExtensions.cs | New extension methods for reference-based collection operations |
| CilInstructionCollectionExtensions.cs | Added reference-based instruction removal and replacement methods |
| InteropTypeDefinitionBuilder.*.cs | Updated to use new HasReferenceAbiType helper for cleaner ABI type checking |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/WinRT.Interop.Generator/Builders/InteropTypeDefinitionBuilder.IDictionary2.cs
Outdated
Show resolved
Hide resolved
src/WinRT.Interop.Generator/Extensions/WindowsRuntimeExtensions.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Clarified and corrected XML documentation for HasReferenceAbiType and GetAbiType methods to better describe their return values and behavior.
Title. This also implements two-pass rewriting for the native parameters, and
Vftblgeneration for generic delegates.Example of codegen:
Note
Draft because there's some commits to move and this branch also needs to be rebased.