-
Notifications
You must be signed in to change notification settings - Fork 122
Emit array marshaller types in 'cswinrtgen' #2178
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/dictionary-impl
Are you sure you want to change the base?
Emit array marshaller types in 'cswinrtgen' #2178
Conversation
Introduces HStringArrayMarshaller to handle marshalling and cleanup of arrays of Windows Runtime HSTRINGs. Includes methods for converting, copying, and freeing unmanaged HSTRING arrays, with error handling and memory management.
Introduces BlittableValueTypeArrayMarshaller to handle conversion and copying between managed and unmanaged arrays of blittable Windows Runtime types. Includes methods for marshaling, copying, and freeing memory, with validation and error handling.
Renamed BlittableValueTypeArrayMarshaller to WindowsRuntimeBlittableValueTypeArrayMarshaller and made it a generic static class. Updated method signatures to remove redundant generic parameters and constraints, improving clarity and type safety.
Renamed the file to use a generic type parameter in the filename and fixed a typo in the XML documentation. Also updated the Free method to use the generic type pointer for the array parameter.
Corrected 'THe' to 'The' in a comment to improve readability and maintain code quality.
Introduced IWindowsRuntimeUnmanagedValueTypeArrayElementMarshaller<T, TAbi> interface and WindowsRuntimeUnmanagedValueTypeArrayMarshaller<T, TAbi> static class to support marshalling arrays of unmanaged Windows Runtime value types. These additions provide methods for converting, copying, and freeing arrays between managed and unmanaged representations, with support for custom element marshallers.
Introduces IWindowsRuntimeManagedValueTypeArrayElementMarshaller and WindowsRuntimeManagedValueTypeArrayMarshaller to support marshalling arrays of managed Windows Runtime value types. Also fixes exception handling in WindowsRuntimeUnmanagedValueTypeArrayMarshaller to rethrow after freeing memory.
Introduces generic and non-generic marshallers for arrays of Windows Runtime reference types, including an interface for element marshalling. Also fixes a minor documentation typo in WindowsRuntimeObjectMarshaller.
Changed the loop variable from int to uint and corrected the index variable from 'j' to 'i' in WindowsRuntimeReferenceTypeArrayMarshaller. This ensures proper iteration and memory management for arrays.
Introduces IWindowsRuntimeKeyValuePairTypeArrayElementMarshaller and WindowsRuntimeKeyValuePairTypeArrayMarshaller to support marshalling arrays of KeyValuePair<TKey, TValue> between managed and Windows Runtime representations. These types are marked obsolete and intended for internal use.
Moved HStringArrayMarshaller to the SzArrays namespace and updated its implementation to use void** instead of HSTRING*, with improved exception safety and nullable string support. Also updated WindowsRuntimeReferenceTypeArrayMarshaller to accept nullable types and removed an unused using directive.
Introduces TypeArrayMarshaller to handle marshalling of arrays of Windows Runtime Type objects. Provides methods for converting, copying, and freeing unmanaged and managed representations, with proper resource management and error handling.
Introduces a marshaller for arrays of Windows Runtime Exception types, providing methods for converting, copying, and freeing unmanaged and managed representations. This supports interop scenarios where arrays of exceptions need to be marshalled between managed and unmanaged code.
Eliminated [MethodImpl(MethodImplOptions.NoInlining)] attributes and related using directives from Free methods in array marshaller classes to simplify code and reduce unnecessary metadata.
Deleted WindowsRuntimeReferenceTypeArrayMarshaller.cs and moved the Free<TElementMarshaller> method to WindowsRuntimeReferenceTypeArrayMarshaller<T>.cs. This consolidates array marshalling logic into the generic class and removes the obsolete static class.
Removed WindowsRuntimeArrayHelpers and replaced its usage with the new WindowsRuntimeArrayMarshallerHelpers in all relevant marshaller classes. Updated validation logic to use the new helper methods, improving clarity and encapsulation of array size checks.
Introduces TypeReference and MemberReference properties for various WindowsRuntime array marshallers and their related interfaces. Adds methods to retrieve references for marshalling operations such as ConvertToUnmanaged, ConvertToManaged, CopyToUnmanaged, CopyToManaged, and Free for array marshallers, including support for generic element and ABI types.
The Free method in WindowsRuntimeReferenceTypeArrayMarshaller<T> no longer requires a generic parameter TElementMarshaller. Updated InteropReferences to reflect this change and simplified method signature and reference creation.
Eliminated usage and references to WindowsRuntimeArrayHelpers and its marshalling stubs from InteropMethodDefinitionFactory.SzArrayMarshaller and InteropReferences. This simplifies the codebase and removes dependency on shared array helper methods.
Replaced explicit summary comments with <inheritdoc cref="InteropMethodDefinitionFactory"/> in partial class declarations for consistency and improved documentation inheritance.
Introduces the SzArrayElementMarshaller static class to InteropTypeDefinitionFactory, providing factory methods for generating element marshaller type definitions for SZ array types. Supports unmanaged value types, managed value types, KeyValuePair types, and reference types, each with appropriate interface implementations and stubbed conversion methods.
Replaces the previous implementation of ConvertToManaged methods, which threw exceptions, with a Nop followed by a Ret instruction. Adds tracking of managed parameter method rewrites using a marker instruction for later code generation or analysis.
Introduces logic to emit direct calls to ConvertToUnmanaged for non-blittable parameter types during interop method generation. Adds a new MethodRewriteInfo.ConvertToUnmanaged type, tracking and handling these rewrites in InteropGeneratorEmitState and InteropGenerator.Emit. Also includes a minor comment correction in ManagedValue.cs.
Replaces throwing implementations of ConvertToUnmanaged with stubs that load the argument and return, and introduces NOP markers for later rewriting. Adds tracking of these methods for further processing by emitState.
Renamed ConvertToUnmanaged method rewrite logic and related types to RawRetVal, updating all references and tracking methods accordingly. This change clarifies the rewrite's purpose and adds support for nullable value types by calling BoxToUnmanaged when appropriate.
Introduces logic to track and emit IL for disposing or releasing values in generated interop methods. Adds MethodRewriteInfo.Dispose, corresponding factory logic, error handling, and integration into the emit pipeline to handle managed, string, and native types appropriately.
Added calls to ReferenceRemove for markers when parameter types are blittable in ManagedValue and RawRetVal factories. Also fixed a typo in a comment and removed an unnecessary pragma warning directive.
Introduces a NOP marker for disposal and updates the 'Dispose' method to use it. Also adds tracking for disposal rewrites to improve clarity and maintainability of the marshaller's disposal process.
Introduces a static Free method to WindowsRuntimeKeyValuePairTypeArrayMarshaller<TKey, TValue> for releasing memory of marshalled arrays. Also adds a corresponding MemberReference accessor in InteropReferences for code generation and interop support.
Introduces InteropTypeDefinitionFactory.SzArrayMarshaller for generating marshaller types for SZ arrays, supporting various element types. Also adds <returns> documentation to SzArrayElementMarshaller methods for clarity.
Introduces static methods to create marshaller TypeDefinitions for string, System.Type, and System.Exception SZ array types, utilizing corresponding interop reference methods. Also removes unused elementAbiType variable from the existing method.
Updated the SZ array marshaller builder and related factory methods to accept and use an InteropGeneratorEmitState parameter. This enables more flexible and context-aware marshaller generation for different element types, and simplifies the method signatures in the marshaller factory by removing unused emitState parameters where not needed.
Deleted the InteropMethodDefinitionFactory.SzArrayMarshaller.cs file, removing the static helper for generating 'Free' marshaller methods for SZ array types. This may be part of a refactor or cleanup to consolidate marshalling logic.
Updated the marshaller factory to extract key and value types from GenericInstanceTypeSignature instead of using the base type directly. Adjusted method calls to pass keyType and valueType separately, improving type safety and clarity.
Introduces GetRawAbiType to retrieve the raw ABI type for a given type without unwrapping. Handles special case for 'void*' by returning WindowsRuntimeObjectReferenceValue to support proper lifetime management.
Extracted common element marshaller creation into a new static ElementMarshaller method to reduce code duplication and improve maintainability. Updated all marshaller factory methods to use the new helper, simplifying method implementations and centralizing marshaller logic.
Simplifies and restructures the logic for generating type names, especially for SZ arrays and generic types. Introduces a helper method to handle generic type arguments and ensures consistent formatting for arrays with generic element types.
Moved the Free method for releasing arrays of unknown COM interfaces from WindowsRuntimeKeyValuePairTypeArrayMarshaller and WindowsRuntimeReferenceTypeArrayMarshaller into a new WindowsRuntimeUnknownArrayMarshaller class. This reduces code duplication and centralizes the array cleanup logic.
Replaces type-specific Free method references for key-value pair and reference type array marshallers with a unified WindowsRuntimeUnknownArrayMarshaller.Free reference. Removes now-unused methods and type references from InteropReferences to simplify and centralize array marshaller cleanup logic.
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 adds support for emitting array marshaller types in the code generator and fixes type name mangling for arrays of generic types. The changes introduce a comprehensive set of marshaller implementations for different array element types (blittable, managed/unmanaged value types, reference types, strings, etc.) and refactors the code generation to use specialized array marshallers instead of the previous helper-based approach.
Changes:
- Introduced new array marshaller types in WinRT.Runtime2 for various element types (blittable, reference types, strings, Type, Exception, etc.)
- Replaced WindowsRuntimeArrayHelpers with type-specific marshaller implementations
- Fixed type name generation for arrays of generic types by reordering the mangling logic
- Added new two-pass IL generation support for raw return values and disposal operations
Reviewed changes
Copilot reviewed 36 out of 36 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| WindowsRuntimeObjectMarshaller.cs | Fixed duplicate word in documentation comment |
| EventSource{T}.cs | Fixed typo in comment ("THe" → "The") |
| WindowsRuntimeUnknownArrayMarshaller.cs | New marshaller for arrays of unknown COM interface types |
| WindowsRuntimeReferenceTypeArrayMarshaller{T}.cs | New marshaller for arrays of reference types |
| WindowsRuntimeBlittableValueTypeArrayMarshaller{T}.cs | New marshaller for arrays of blittable value types |
| WindowsRuntimeArrayMarshallerHelpers.cs | New helper for array marshaller validation |
| TypeArrayMarshaller.cs | New marshaller for Type[] arrays |
| HStringArrayMarshaller.cs | New marshaller for string[] arrays |
| ExceptionArrayMarshaller.cs | New marshaller for Exception[] arrays |
| IWindowsRuntimeReferenceTypeArrayElementMarshaller{T}.cs | New interface for element marshallers |
| WindowsRuntimeArrayHelpers.cs | Deleted - replaced by new marshaller types |
| InteropReferences.cs | Added references to new marshaller types and methods |
| MethodRewriteInfo.*.cs | New rewrite info classes for RawRetVal and Dispose operations |
| InteropGeneratorEmitState.cs | Added tracking methods for new rewrite scenarios |
| InteropGenerator.Emit.cs | Integrated new array marshaller generation |
| InteropUtf8NameFactory.cs | Fixed type name generation for generic array types |
| InteropTypeDefinitionFactory.SzArray*.cs | New factories for array marshallers and element marshallers |
| InteropMethodRewriteFactory.*.cs | New IL rewrite logic for raw return values and disposal |
| InteropMethodDefinitionFactory.*.cs | Doc comment consistency updates |
| WindowsRuntimeExtensions.cs | Added GetRawAbiType method |
| WellKnownInteropExceptions.cs | Added exception for invalid Dispose operations |
| InteropTypeDefinitionBuilder.SzArray.cs | Refactored to use new factory-based approach |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ModuleDefinition module) | ||
| { | ||
| // Validate that we do have some IL body for the input method (this should always be the case) | ||
| // Validate that we do have some IL body for the input method (this should always be thevv case) |
Copilot
AI
Jan 14, 2026
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.
Corrected spelling of 'thevv' to 'the'.
| // Validate that we do have some IL body for the input method (this should always be thevv case) | |
| // Validate that we do have some IL body for the input method (this should always be the case) |
Introduces an isValueType parameter to control whether element marshaller types are emitted as value types or reference types. This enables full specialization and inlining for value type arrays and key-value pairs, improving performance and reducing code size when both key and value are value types.
Replaces the use of UnmanagedValueType with ReferenceType when creating the marshallerType for SzArrayMarshaller. This change likely aligns the marshaller behavior with expected reference type semantics.
Title. Also fixes the mangled type names for arrays of generic types.
Examples of codegen:
Note
Draft because there's some commits to move and this branch also needs to be rebased.