Skip to content

Conversation

@Sergio0694
Copy link
Member

This PR adds an extensible way to special-case types to exclude in 'cswinrtgen', such as Task<T> instantiations.

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.
Applied the [WindowsRuntimeManagedOnlyType] attribute to the WindowsRuntimeObjectReference class to indicate it is intended for managed use only.
Introduces a TypeExclusions helper class to centralize logic for excluding specific types, such as Task<T>, from the interop API surface. Also adds a Task1 reference to InteropReferences to support this exclusion logic.
Introduced checks in InteropTypeDiscovery to skip types explicitly excluded via TypeExclusions, including support for SZ arrays. This prevents processing of types that should not be considered for interop marshalling, improving filtering and correctness.
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 introduces an exclusion list mechanism for the 'cswinrtgen' tool to filter out specific types from the interop API surface, with the initial exclusion being Task<T> instantiations.

Key Changes:

  • Created a new TypeExclusions helper class with extensible exclusion logic
  • Added Task<T> reference to InteropReferences for use in exclusion checks
  • Integrated exclusion checks into three type discovery methods to prevent tracking of excluded types

Reviewed changes

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

Show a summary per file
File Description
src/WinRT.Runtime2/InteropServices/ObjectReference/WindowsRuntimeObjectReference.cs Added [WindowsRuntimeManagedOnlyType] attribute to mark the class as managed-only
src/WinRT.Interop.Generator/References/InteropReferences.cs Added Task1 property to provide reference to Task<TResult> generic type definition
src/WinRT.Interop.Generator/Helpers/TypeExclusions.cs Created new helper class containing exclusion logic for types that should be filtered from interop API surface
src/WinRT.Interop.Generator/Discovery/InteropTypeDiscovery.cs Added exclusion check in TryTrackExposedUserDefinedType to filter excluded types
src/WinRT.Interop.Generator/Discovery/InteropTypeDiscovery.Generics.cs Added exclusion checks in TryTrackGenericTypeInstance and TryTrackSzArrayType to filter excluded types

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

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/generic-ccw2 branch from ecc4d6d to 54cf730 Compare December 16, 2025 17:34
Applied the [WindowsRuntimeManagedOnlyType] attribute to various Windows Runtime collection and async types in the WinRT.Runtime2 project. This change clarifies that these types are managed-only implementations, improving code clarity and maintainability.
}

// Check if the input type matches any of our exclusions
foreach (TypeReference excludedType in (ReadOnlySpan<TypeReference>)[interopReferences.Task1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in a separate global variable or so if we are planning on adding to it or is it just this?

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.

4 participants