Skip to content

@cppname: bind a C++ function by its declaration, mangled for the target - #689

Merged
ASDAlexander77 merged 2 commits into
mainfrom
cppname-mangling
Oct 25, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
cppname-mangling

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

@linkname takes a raw symbol, so a @cpp class binding non-virtual C++ methods had to be written once per ABI (?scaled@Methods@@QEAAHH@Z vs _ZN7Methods6scaledEi). @cppname takes the C++ declaration and mangles it for the target triple: MSVC on windows-msvc, Itanium (g++/clang) everywhere else, MinGW included. One class now binds the same methods on every OS.

@cpp
class Methods {
    @cppname("int scaled(int)")      scaled(x: s32): s32;
    @cppname("int scaled(double)")   scaled(x: f64): s32;
    @cppname("int get() const")      get(): s32;
}
@cppname("int ns::sum(int, int)") declare function sum(a: s32, b: s32): s32;
  • An unqualified name is a member of the class; static methods and free functions get their own forms.
  • <stdint.h>/<stddef.h> typedefs follow the target (int64_t is long on 64-bit Linux, long long on Windows/macOS).
  • A bare record name is a struct; class X, union U, enum E when it isn't (MSVC encodes the tag).
  • Templates, operators, constructors/destructors, function pointers and arrays are a clear error; @linkname remains for those.
  • Resolved in iterateDecorators to DLL_NAME, exactly as @linkname; a declaration that doesn't mangle is reported by checkLinkNameDecorators.

The mangler (lib/TypeScript/CppMangler.cpp) is hand-written because tslang doesn't link clang's frontend; the exe grows by ~29 KB.

Tests

  • unittests/MLIRGen/CppMangler.cpp: ~30 declarations checked against clang's own symbols (clang++ -target <triple> -S -emit-llvm) for MSVC x64/x86/arm64, MinGW, Linux x64/x86/arm64, Android, macOS, wasm32, armv7, including back-references/substitutions, const results, and errors.
  • abi_msvc.ts / abi_itanium.ts bind scaled with @cppname; the JIT test against the MSVC-built C++ DLL passes under gc, rc, own.
  • cppvtable/cppname.ts compiled for MSVC, Linux and MinGW triples, each symbol checked in the IR.
  • Error cases in test/tester/linkname/cppname-*.ts.
  • Full Release ctest: 5791/5791 passed (before a final small fix; the related 93 tests and unit tests passed after it).

Not covered: @slot numbering still differs between ABIs (MSVC overload order, destructor slots), so classes with virtual overloads still need one declaration per ABI.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits October 25, 2026 19:14
@cppname("int scaled(int)") takes the C++ declaration instead of a raw
symbol and mangles it as the target's C++ compiler does: MSVC on
windows-msvc, Itanium (g++/clang) elsewhere, MinGW included. One @cpp
class now binds the same non-virtual methods on every OS. The
<stdint.h>/<stddef.h> typedefs follow the target (int64_t is long on
64-bit Linux, long long on Windows and macOS).

The mangler is hand-written (tslang doesn't link clang's frontend) and
covers builtin types, records, pointers, references, const methods,
static methods, free functions in namespaces, variadics and both
schemes' back-references/substitutions. Its unit tests compare against
clang's symbols for MSVC x64/x86/arm64, MinGW, Linux x64/x86/arm64,
Android, macOS, wasm32 and armv7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… an error

MSVC encodes the const of any value returned, a builtin's too
(?f@@ya?BHXZ). A method whose class isn't known would have mangled an
unqualified name as a free function's symbol; it is an error now. Test
the error path of a @cpp class's method.

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