Skip to content

@cpp: @virtual and @destructor - one binding for every target - #837

Merged
ASDAlexander77 merged 1 commit into
mainfrom
cpp-virtual-slots
Nov 13, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
cpp-virtual-slots

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

A @cpp class could only give each virtual method its slot by hand with @slot(N). MSVC and Itanium lay vtables out differently, so one .ts binding was right for one target only. Now the compiler numbers the slots for the target it compiles for.

Language

  • @virtual on a method of a @cpp class: the compiler gives it its slot for the target.
    • Base slots come first, and an override (same name and parameter types) reuses its base's slot.
    • MSVC: new virtuals go in declaration order, and the overloads of one name are grouped at the first one's place in reverse order. The destructor takes 1 slot.
    • Itanium: declaration order. The destructor takes 2 slots (complete + deleting).
  • @destructor name(): void declares the virtual destructor's slot or slots. Calling it is an error, because the object has to be released the way the C++ library does.
  • @vtablesize(N) on a class whose base is declared elsewhere (or not at all) says where its new slots start. __decls print it, so -shared imports keep the numbering.
  • @slot(N) stays as the escape hatch, and existing bindings are unchanged. Errors:
    • a class mixes new @virtual and new @slot(N) methods;
    • the base's size is unknown;
    • @vtablesize is smaller than the computed size;
    • @virtual/@destructor/@slot/@vtablesize is used outside a @cpp class.

tsbindgen

--virtual-slots prints @virtual/@destructor instead of this target's @slot(N), so one binding works for every target. Non-public virtuals are included (with protected/private), because their slots shift the ones after them. If a class (or a base) has a virtual that can't be declared, it keeps this target's @slot(N), and tsbindgen prints a comment and a warning.

Verification

  • Slot numbers were checked against cl /FAs and clang -emit-llvm for a class with overloads, a destructor and overrides (abi_host.cpp VirtualBase/VirtualDerived).
  • test-jit-cppvtable-virtual-slots-{none,rc,own} call through the real C++ vtable: MSVC on Windows, Itanium on Linux.
  • test-compile-cppvtable-virtual-*-{msvc,itanium} check the indexes for both triples on any host. Error tests and -shared export/import tests (gc, own) were added.
  • Unit tests check that the tsbindgen output for Windows equals the output for Itanium, and that a class with a skipped virtual keeps @slot.
  • Full ctest: Windows 7056/7056 pass; Linux (WSL) 7040/7040 pass.

Docs: docs/cpp-classes.md gets the section "One binding for every target: @virtual".

🤖 Generated with Claude Code

A @cpp class's @slot(N) numbers are the target's: MSVC gives a virtual
destructor one slot and groups overloads where the first is declared, in
reverse order; the Itanium ABI gives the destructor two slots and keeps the
declaration order. So a binding for both was two files.

@virtual numbers a method's slot for the target compiled for, as its C++
compiler does (mlirGenClassCppVirtualSlots), before the rest of @cpp sees
it as @slot(N): base class slots first, an override (same name and
parameter types as a base virtual) in the base's slot, new methods per the
ABI. @Destructor is the virtual destructor (1 or 2 slots); calling it is an
error. A class numbering new methods with @slot(N) can't use @virtual too,
and a base's size must be known (@virtual, or @vtablesize(N)). __decls
carries @vtablesize and @Destructor so an importer can extend the class.
@slot(N) is unchanged and wins on the same method.

tsbindgen --virtual-slots writes @virtual/@Destructor (non-public virtuals
included), falling back to the target's @slot(N) for a class with a
virtual it can't declare.

Tests: cppvtable/virtual_slots.ts against abi_host.cpp's real C++ vtable
(MSVC on Windows, Itanium on Linux; none/rc/own), slot numbers for both
triples, @vtablesize, the errors, an export/import pair, tsbindgen units.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 1c18820 into main Nov 13, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the cpp-virtual-slots branch November 13, 2026 10:44
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.

1 participant