Skip to content

Add support for custom overload names to the WinMD generator#2454

Open
Sergio0694 wants to merge 3 commits into
staging/3.0from
user/sergiopedri/overload-names
Open

Add support for custom overload names to the WinMD generator#2454
Sergio0694 wants to merge 3 commits into
staging/3.0from
user/sergiopedri/overload-names

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

Summary

Honor an author-applied [Windows.Foundation.Metadata.Overload("Name")] attribute when generating a component's .winmd, emitting that name instead of an auto-generated sequential one. This ports #2275 (which targeted CsWinRT 2.x) to the CsWinRT 3.0 WinMD generator.

Motivation

Rust (windows-rs) and some other Windows Runtime consumers cannot represent overloaded methods, so they rely on the [Overload] attribute to give each overload a distinct, ergonomic ABI name. Previously, author-specified names were ignored and the generator always auto-generated sequential suffixes (Method2, Method3, …). PR #2275 added support for honoring them in CsWinRT 2.x. CsWinRT 3.0 produces the .winmd with a different, post-build tool (cswinrtwinmdgen, which analyzes the compiled .dll with AsmResolver rather than running a Roslyn source generator), so the behavior had to be re-implemented there.

Changes

Generator (src/WinRT.WinMD.Generator/):

  • Writers/WinMDWriter.Finalization.cs: AddOverloadAttributesForType now honors an author-specified [Overload("...")] name when present (otherwise it keeps auto-generating a sequential name). It also selects the default overload (the one that keeps the original ABI name) from the [DefaultOverload] attribute rather than from metadata order, so an author-specified name is not dropped when the default is not declared first, and auto-generated names skip any name already used by another member or overload to avoid collisions. Adds the RecordUserSpecifiedOverloadName and HasDefaultOverloadAttribute helpers.
  • Writers/WinMDWriter.Members.cs: records the author-specified overload name while emitting interface methods (authored and synthesized), so finalization can honor it.
  • Writers/WinMDWriter.Attributes.cs: ShouldCopyAttribute no longer copies [Overload] verbatim; it is emitted exactly once by the overload phase as the single source of truth, preventing duplicates.
  • Writers/WinMDWriter.cs: adds the _userSpecifiedOverloadNames map.

[Overload] is emitted only on interfaces (authored and synthesized), not on runtime classes, matching the Windows Runtime metadata convention and CsWinRT 2.x (a runtime class exposes its ABI through interfaces).

Tests:

  • src/Tests/AuthoringTest/Program.cs: the ICustomOverloadNames interface and CustomOverloadNamesClass from PR Add support for custom overload names to CsWinMD #2275, plus types covering additional behaviors (OverloadCollisionClass, IDefaultOverloadNotFirst / DefaultOverloadNotFirstClass).
  • src/Tests/AuthoringConsumptionTest/test.cpp: the ported CustomOverloadNames consumption test, plus new tests for an auto-generated name coexisting with an author-specified one, collision avoidance, and [DefaultOverload] on a non-first overload.
  • src/Tests/AuthoringConsumptionTest/AuthoringConsumptionTest.exe.manifest: activatableClass entries for the newly activatable test classes (including CustomOverloadNamesClass, whose entry the original PR omitted).

Sergio0694 and others added 3 commits June 18, 2026 11:55
Port custom overload name support from PR #2275 (CsWinRT 2.x) to the
CsWinRT 3.0 WinMD generator (cswinrtwinmdgen). When an authored method
carries [Windows.Foundation.Metadata.Overload("Name")], emit that name in
the generated .winmd instead of an auto-generated sequential one. windows-rs
consumers require explicit overload names because Rust has no method overloading.

- Record author-specified overload names while emitting interface methods and
  honor them in AddOverloadAttributesForType; stop copying the [Overload]
  attribute verbatim so it is never duplicated.
- Select the default overload (which keeps the original name) from the
  [DefaultOverload] attribute rather than metadata order, so an author-specified
  name is not dropped when the default is not declared first.
- Auto-generated names skip any name already used by another method or an
  author-specified overload, avoiding collisions.
- Emit [Overload] only on interfaces (authored and synthesized), matching the
  Windows Runtime metadata convention; runtime classes expose their ABI through
  interfaces (consistent with CsWinRT 2.x).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add the ICustomOverloadNames interface and CustomOverloadNamesClass authoring
types, and the CustomOverloadNames consumption test. The test validates that
author-specified [Overload] names (LookupByIndex, LookupByFlag, TransformNumber)
appear in the generated projection's ABI vtables, on both an authored interface
and a class's synthesized interface.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…lt selection

Extend the overload-name tests to cover behaviors beyond the original PR:

- Verify the auto-generated ABI name (Transform2) coexists with an author-
  specified one (TransformNumber) in the same overload group.
- OverloadCollisionClass: an author-specified overload name matching the auto
  pattern ("M2") forces the auto-generated name of the remaining overload to
  skip it ("M3").
- DefaultOverloadNotFirstClass / IDefaultOverloadNotFirst: [DefaultOverload] on
  a non-first overload keeps the original ABI name, while the author-specified
  name ("GetByIndex") on the first overload is still honored.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 added enhancement New feature or request authoring Related to authoring feature work cswinmd CsWinRT 3.0 labels Jun 18, 2026
@Sergio0694 Sergio0694 requested a review from manodasanW June 18, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

authoring Related to authoring feature work cswinmd CsWinRT 3.0 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant