Repository navigation
@cpp: @virtual and @destructor - one binding for every target - #837
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
@cppclass could only give each virtual method its slot by hand with@slot(N). MSVC and Itanium lay vtables out differently, so one.tsbinding was right for one target only. Now the compiler numbers the slots for the target it compiles for.Language
@virtualon a method of a@cppclass: the compiler gives it its slot for the target.@destructor name(): voiddeclares 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.__declsprint it, so-sharedimports keep the numbering.@slot(N)stays as the escape hatch, and existing bindings are unchanged. Errors:@virtualand new@slot(N)methods;@vtablesizeis smaller than the computed size;@virtual/@destructor/@slot/@vtablesizeis used outside a@cppclass.tsbindgen
--virtual-slotsprints@virtual/@destructorinstead of this target's@slot(N), so one binding works for every target. Non-public virtuals are included (withprotected/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
cl /FAsandclang -emit-llvmfor a class with overloads, a destructor and overrides (abi_host.cppVirtualBase/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-sharedexport/import tests (gc, own) were added.@slot.Docs:
docs/cpp-classes.mdgets the section "One binding for every target:@virtual".🤖 Generated with Claude Code