Skip to content

tsbindgen: bind non-virtual C++ methods by @cppname, portable across OSes - #690

Merged
ASDAlexander77 merged 1 commit into
mainfrom
bindgen-cppname
Oct 25, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
bindgen-cppname

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Follows #689. A generated @cpp class bound each non-virtual method by the host's mangled symbol (@linkname("?id@Shape@geo@@QEAAHH@Z")), so bindings generated on Windows didn't link on Linux. tsbindgen now emits the C++ declaration instead, which @cppname mangles for whichever target compiles it:

@cppname("int32_t fx::Shape::id(int32_t)") id(x: s32): s32;
  • HeaderParser spells the declaration from clang's AST in the subset @cppname accepts: builtins, <stdint.h>/<stddef.h> typedefs as written (other typedefs desugared), records/enums by qualified name with class/union/enum tags, cv pointers and references, trailing const.
  • The method name is always qualified by the C++ class: the TS class can be renamed (--strip-prefix, a dropped namespace), and an unqualified name would be mangled under the TS name.
  • The declaration is kept only when mangleCppName for the parse triple equals clang's symbol; otherwise the method keeps @linkname (function pointers, template specializations, ref-qualified methods, a header's own int64_t that differs from the target's).
  • TsClangImporter compiles ../TypeScript/CppMangler.cpp (LLVM-only) for that check — no dependency on MLIRTypeScript.
  • Free C++ functions: unchanged — tsbindgen already skips non-extern "C" C++ functions; C functions keep @linkname.
  • README / docs/cpp-classes.md updated (README example checked against real tsbindgen output).

Tests

  • TsClangImporterTests 84/84: updated the non-virtual test (same @cppname on Windows and Linux), a type-spelling test across x86_64-windows-msvc, x86_64-linux, aarch64-darwin, aarch64-linux, fallback tests (unspellable, mangles-otherwise), and a --strip-prefix naming test.
  • ctest -C Release -R "bindgen|TsClangImporter|cppname|linkname|MLIRGenTests": 40/40 (Windows Release). The bindgen-cpp e2e test links and runs fx::Shape::id through the generated @cppname (exe + JIT).

🤖 Generated with Claude Code

…OSes

A generated @cpp class bound each non-virtual method by the host's
mangled symbol (@Linkname), so a binding made on Windows didn't link on
Linux. HeaderParser now spells the method's C++ declaration in the
subset @cppname mangles - builtins, <stdint.h>/<stddef.h> typedefs as
written, records and enums by qualified name and tag, cv pointers and
references, a const method - with the name always qualified by the C++
class (the TS class can be renamed by --strip-prefix or a namespace).

The declaration is kept only when tslang's own mangler gives clang's
symbol for the parse target; otherwise (a function pointer, a template
specialization, a ref-qualified method, a header's own int64_t that the
target defines otherwise) the method keeps @Linkname. TsClangImporter
compiles CppMangler.cpp (LLVM-only) for that check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 88ffeb6 into main Oct 25, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the bindgen-cppname branch October 25, 2026 20:12
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