Skip to content

Conversation

@Sergio0694
Copy link
Member

Title. GetMany is an empty stub for now, will implement in a follow up PR.

Examples of codegen:
image

Note

Draft because there's some commits to move and this branch also needs to be rebased.

Updated GetAt and get_Size methods in InteropMethodDefinitionFactory.IReadOnlyList1Impl to accept explicit MemberReference parameters for the interface methods to invoke. Adjusted InteropTypeDefinitionBuilder to pass the correct references. This improves flexibility and clarity in method invocation for interop scenarios.
Introduces IListAdapter<T> as a stateless adapter for IList<T> to expose vector-like operations, including GetAt, Size, IndexOf, and GetMany. Adds IListAdapterExtensions with an IndexOf overload for IList<string> supporting ReadOnlySpan<char> comparisons. Both are marked obsolete for internal use.
Introduces methods for 'GetAt' and 'Size' using IListAdapter<T> in InteropTypeDefinitionBuilder.IList1. Adds new type and member references for IListAdapter<T> and IListAdapterExtensions in InteropReferences, enabling more complete IList1 interop support.
Introduced the GetView method to return an immutable view of the IList<T> as IReadOnlyList<T>. This improves performance by avoiding unnecessary allocations when possible and aligns with the expected behavior for vector views in interop scenarios.
Introduces a new GetView method for IList<T> interop types, including its method factory and reference resolution. Updates type discovery to track IReadOnlyList<T> when IList<T> is found, ensuring correct marshaling for IVector<T>.GetView. This enables proper support for projecting IList<T> to IReadOnlyList<T> in generated interop code.
Adds a check to avoid reprocessing user-defined types during interop type discovery, preventing stack overflows from infinite recursion. Also tracks ReadOnlyCollection<T> when IList<T> is discovered to ensure correct marshaling, and introduces supporting fields and methods for marking user-defined types.
Introduces methods and references to support IListAdapter1<T>.IndexOf in the interop generator. Updates method factories and type builders to correctly handle IndexOf for both IList<T> and IReadOnlyList<T> scenarios, including string specialization.
Implements the SetAt method for IList<T> interop, including method definition generation, reference resolution, and adapter implementation. This enables setting values at a specific index in generated COM interop for IList<T>.
Eliminated an unused variable in InteropMethodDefinitionFactory.IList1Impl.cs and moved the IListAdapter1SetAt method in InteropReferences.cs for better code organization.
Introduces the InsertAt method for IList<T> interop, including its definition in IListAdapter<T>, method generation in InteropMethodDefinitionFactory, and reference handling in InteropReferences. This enables correct handling of the IVector<T>.InsertAt method in generated interop code.
Relocated the IndexOf method and its documentation within IListAdapter<T> to improve code organization and maintain logical grouping of related methods. No functional changes were made.
Implemented the RemoveAt method for IList<T> interop, including its definition in the generator, method factory, and interop references. Also added the actual RemoveAt implementation in IListAdapter<T> to handle item removal and error translation.
Introduces the 'Append' method for IList<T> in the interop generator. Adds method definition, reference, and implementation logic to support exporting the Append method for COM interop scenarios.
Implements the RemoveAtEnd method for IList<T> interop scenarios, including generator, factory, references, and adapter logic. This enables proper handling of the IVector<T>.RemoveAtEnd operation in Windows Runtime interop, with error handling for empty collections.
Implemented the 'Clear' method for IList<T> interop by defining a new method in InteropMethodDefinitionFactory.IList1Impl and wiring it up in InteropTypeDefinitionBuilder. This enables COM interop consumers to call the Clear method on IList<T> implementations.
Introduces the GetMany method to IList<T> interop type definitions, aligning its implementation with IReadOnlyList<T>. Updates method signatures and calls to pass the appropriate getAtMethod reference, enabling consistent method generation for both IList<T> and IReadOnlyList<T> interfaces.
Introduces the ManagedValue method rewrite type, including its model, factory logic, and emit state tracking. Updates the interop generator to handle marshalling of managed values, enabling more flexible and accurate IL generation for managed parameters.
Introduces the CreateLdind method for generating instructions to load values indirectly based on the type signature. This complements the existing CreateStind method and improves support for indirect value loading in IL code generation.
Introduces a new ReplaceAll method for IList1 interop, including its definition, method body, and integration into the type builder. Also updates method calls to use ICollection1.Add instead of IList1.Append and removes the unused IList1Append reference.
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 adds support for emitting full implementation types for IList<T> in 'cswinrtgen'. The implementation involves creating adapter types for marshalling IList<T> instances to/from Windows Runtime interfaces, along with the necessary infrastructure for code generation.

Key changes:

  • Added IListAdapter<T> and IListAdapterExtensions for marshalling IList<T> instances
  • Implemented factory methods for generating interop method definitions for IList<T> implementations
  • Added support for tracking and rewriting managed values during IL generation

Reviewed changes

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

Show a summary per file
File Description
src/WinRT.Runtime2/InteropServices/Collections/IListAdapter{T}.cs New adapter providing stateless marshalling methods for IList<T> to Windows Runtime IVector<T>
src/WinRT.Runtime2/InteropServices/Collections/IListAdapterExtensions.cs Extension methods for IListAdapter<T>, specifically for string handling
src/WinRT.Interop.Generator/References/InteropReferences.cs Added type and method references for IListAdapter<T> and moved existing references
src/WinRT.Interop.Generator/Models/MethodRewriteInfo/MethodRewriteInfo.ManagedValue.cs New model for tracking method rewrites involving managed values
src/WinRT.Interop.Generator/Generation/InteropGeneratorEmitState.cs Added tracking support for managed value method rewrites
src/WinRT.Interop.Generator/Generation/InteropGeneratorDiscoveryState.cs Added support for marking user-defined types to prevent duplicate processing
src/WinRT.Interop.Generator/Generation/InteropGenerator.Emit.cs Added case handling for managed value method rewrites
src/WinRT.Interop.Generator/Factories/InteropMethodRewriteFactory.ManagedValue.cs New factory for rewriting methods to marshal managed values
src/WinRT.Interop.Generator/Factories/InteropMethodRewriteFactory.ManagedParameter.cs Removed duplicate comments and consolidated marshalling logic
src/WinRT.Interop.Generator/Factories/InteropMethodDefinitionFactory.IReadOnlyList1Impl.cs Refactored to support both IReadOnlyList<T> and IList<T> implementations
src/WinRT.Interop.Generator/Factories/InteropMethodDefinitionFactory.IList1Impl.cs New factory for creating method definitions for IList<T> implementations
src/WinRT.Interop.Generator/Factories/InteropMethodDefinitionFactory.IEnumerator1Impl.cs Updated comment for accuracy
src/WinRT.Interop.Generator/Extensions/CilInstructionExtensions.cs Added CreateLdind method for loading values indirectly
src/WinRT.Interop.Generator/Discovery/InteropTypeDiscovery.cs Added early exit to prevent infinite recursion when discovering user-defined types
src/WinRT.Interop.Generator/Discovery/InteropTypeDiscovery.Generics.cs Added tracking of IReadOnlyList<T> and ReadOnlyCollection<T> when discovering IList<T>
src/WinRT.Interop.Generator/Builders/InteropTypeDefinitionBuilder.IReadOnlyList1.cs Updated to pass method references to factory methods
src/WinRT.Interop.Generator/Builders/InteropTypeDefinitionBuilder.IList1.cs Implemented full vtable methods for IList<T> implementations

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

{
// This list is not really read-only: once marshalled, native code could do
// a 'QueryInterface' call back to 'IVector<T>', which would succeed, and would
// return a modifiable reference for this view. We believe this is accetable, as
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'accetable' to 'acceptable'.

Suggested change
// return a modifiable reference for this view. We believe this is accetable, as
// return a modifiable reference for this view. We believe this is acceptable, as

Copilot uses AI. Check for mistakes.
// If the type has been seen before, it means that it either has already been fully processed, or that it
// is currently being processed (possibly by another thread, if multi-threading discovery is enabled). The
// reason for this check is not so much to improve performance (although it does avoid some repeated work),
// but most importantly to avoid stack overlows due to infinite recursion in cases where user-defined types
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'overlows' to 'overflows'.

Suggested change
// but most importantly to avoid stack overlows due to infinite recursion in cases where user-defined types
// but most importantly to avoid stack overflows due to infinite recursion in cases where user-defined types

Copilot uses AI. Check for mistakes.
/// <param name="interopReferences">The <see cref="InteropReferences"/> instance to use.</param>
/// <param name="emitState">The emit state for this invocation.</param>
/// <param name="module">The interop module being built.</param>
/// /// <remarks>
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

Extra forward slashes in XML documentation comment. Should have only three slashes total.

Suggested change
/// /// <remarks>
/// <remarks>

Copilot uses AI. Check for mistakes.
/// <param name="interopReferences">The <see cref="InteropReferences"/> instance to use.</param>
/// <param name="module">The interop module being built.</param>
/// <remarks>
/// This method can also be used to define the <c>GetAt</c> method for <see cref="System.Collections.Generic.IList{T}"/> interfaces.
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

Documentation incorrectly states 'GetAt' but this method is for 'get_Size'.

Suggested change
/// This method can also be used to define the <c>GetAt</c> method for <see cref="System.Collections.Generic.IList{T}"/> interfaces.
/// This method can also be used to define the <c>get_Size</c> method for <see cref="System.Collections.Generic.IList{T}"/> interfaces.

Copilot uses AI. Check for mistakes.
Replaces usage of 'elementType' with explicit 'GenericParameterSignature(GenericParameterType.Type, 0)' in IList1 and IReadOnlyList1 generic reference type creation for method signatures. This ensures consistent and correct handling of generic parameters in interop reference methods.
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