Skip to content

Conversation

@Sergio0694
Copy link
Member

This PR handles user-defined types in the following cases:

  • Foo : Bar<T> (with a fixed T)
  • Baz<T> (with T defined in code somewhere)

It also updates the discovery logic to analyze arguments of newobj and newarr instructions.

Consider this code:

image

We now gather this:

image

Copy link

Copilot AI left a 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 enhances the cswinrtgen tool to handle user-defined types with constructed generic type arguments in two key scenarios: types inheriting from generic base types with fixed type parameters (e.g., Foo : Bar<T> where T is fixed), and generic types with type parameters defined in the codebase (e.g., Baz<T>). The changes refactor the discovery logic by extracting type tracking into dedicated helper methods and extend the analysis to examine newobj and newarr IL instructions to discover additional type instantiations.

Key changes:

  • Added [Guid] attributes to Windows Foundation interface types for proper COM interop identification
  • Refactored discovery logic into new InteropTypeDiscovery helper class with focused methods for different type categories
  • Enhanced type signature enumeration to analyze IL instructions for object and array instantiations
  • Updated error messages for better clarity around interface resolution failures

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
IAsyncOperation{TResult}.cs Added [Guid] attribute for COM interface identification
IAsyncOperationWithProgress{TResult, TProgress}.cs Added [Guid] attribute for COM interface identification
IAsyncInfo.cs Added [Guid] attribute for COM interface identification
IAsyncActionWithProgress{TProgress}.cs Added [Guid] attribute for COM interface identification
IAsyncAction.cs Added [Guid] attribute for COM interface identification
IVectorChangedEventArgs.cs Added [Guid] attribute for COM interface identification
IObservableVector{T}.cs Added [Guid] attribute for COM interface identification
IObservableMap{K, V}.cs Added [Guid] attribute for COM interface identification
IMapChangedEventArgs{K}.cs Added [Guid] attribute for COM interface identification
InteropGenerator.Discover.cs Refactored discovery methods to delegate to new InteropTypeDiscovery helper class
TypeSignatureExtensions.cs Added EnumerateAllInterfaces() extension method for traversing type hierarchies
TypeDefinitionExtensions.cs Added HasBaseType() helper and refactored hierarchy enumeration; removed EnumerateAllInterfaces()
ModuleDefinitionExtensions.cs Extended enumeration to analyze newobj and newarr IL instructions
WellKnownInteropExceptions.cs Renamed and reorganized warning methods for clarity
InteropTypeDiscovery.cs New helper class with methods for tracking type hierarchy and user-defined types
InteropTypeDiscovery.Generics.cs New helper class with methods for tracking generic type instances and arrays
InteropTypeDefinitionBuilder.IObservableVector1.cs Added tracking for implementation types used in COM interface entries

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/generic-ccw2 branch 2 times, most recently from db40bcf to 1f0c48f Compare December 15, 2025 23:34
Sergio0694 and others added 15 commits December 16, 2025 09:34
Introduces EnumerateAllInterfaces to TypeSignatureExtensions, allowing traversal of all interfaces implemented by a type, including those from base types. This method handles generic types and ensures robust traversal even with incomplete type hierarchies.
Updated the logic to yield base interfaces directly without re-instantiating their generic types, as they are already instantiated when returned. This prevents unnecessary operations and improves code clarity.
Added a call to emitState.TrackTypeDefinition for implType and vectorType in the InteropTypeDefinitionBuilder.IObservableVector1 implementation. This ensures the type is tracked, which may be required for COM interface entries involving user-defined types.
Renamed and updated warning methods in WellKnownInteropExceptions for improved clarity regarding interface and base type resolution failures. Refactored InteropGenerator.Discover.cs to use the new warning methods, streamline interface discovery, and ensure correct handling of generic interface instantiations.
Deleted the EnumerateAllInterfaces method, which enumerated all interface implementations for a type and its base types. This cleanup may be due to redundancy, lack of use, or a refactor in interface enumeration logic.
Introduced HasBaseType extension to encapsulate base type checks and refactored related logic in TypeDefinitionExtensions and InteropGenerator.Discover. This improves code clarity and reduces duplication when handling type hierarchies and base type resolution.
Moved the logic for tracking exposed user-defined types from InteropGenerator.Discover.cs into a new InteropTypeDiscovery helper class. This improves code organization and reusability by encapsulating the discovery process in a dedicated static method.
Moved logic for tracking constructed generic types from InteropGenerator.Discover.cs into a new InteropTypeDiscovery.Generics.cs file. This refactoring centralizes and organizes generic type discovery, improving maintainability and separation of concerns.
Extended the EnumerateTypeSignatures method to process 'newobj' and 'newarr' CIL instructions, ensuring that object and array instantiations are included in the signature enumeration. This improves coverage of type signatures encountered in method bodies.
Moved SZ array and type hierarchy tracking logic from InteropGenerator.Discover.cs into dedicated helper methods in InteropTypeDiscovery. This improves code reuse and maintainability by centralizing the logic for type analysis and tracking.
Updated InteropTypeDiscovery tracking methods to return void instead of bool, as their return values were not used. Adjusted all call sites and removed related comments and unreachable code for clarity.
Added checks to skip unconstructed generic type definitions during interop type discovery and generation. This ensures only constructed generic types are considered for marshalling code, improving accuracy and avoiding unnecessary processing of generic definitions.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Introduced new test cases to validate activation and interface QueryInterface (QI) for constructed generic types and their derived types. Refactored QI logic into a reusable ComHelpers.EnsureQueryInterface method. Added several generic and derived classes to exercise interface exposure and activation scenarios.
Introduces tests for IAsyncActionWithProgress<int> and IAsyncOperation<TimeSpan> using AsyncInfo.Run with explicit and transitive type arguments. Ensures correct COM interface querying and memory management for these async operations.
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/generic-ccw2 branch from ecc4d6d to 54cf730 Compare December 16, 2025 17:34
Eliminated filtering of constructed generic type signatures using IsConstructedGenericTypeVisitor in the type and array discovery logic. This simplifies the code and ensures all type signatures are processed for marshalling code generation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants