Repository navigation
@cppname: bind a C++ function by its declaration, mangled for the target - #689
Merged
Merged
Conversation
@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>
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.
Summary
@linknametakes a raw symbol, so a@cppclass binding non-virtual C++ methods had to be written once per ABI (?scaled@Methods@@QEAAHH@Zvs_ZN7Methods6scaledEi).@cppnametakes the C++ declaration and mangles it for the target triple: MSVC onwindows-msvc, Itanium (g++/clang) everywhere else, MinGW included. One class now binds the same methods on every OS.<stdint.h>/<stddef.h>typedefs follow the target (int64_tislongon 64-bit Linux,long longon Windows/macOS).struct;class X,union U,enum Ewhen it isn't (MSVC encodes the tag).@linknameremains for those.iterateDecoratorstoDLL_NAME, exactly as@linkname; a declaration that doesn't mangle is reported bycheckLinkNameDecorators.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.tsbindscaledwith@cppname; the JIT test against the MSVC-built C++ DLL passes under gc, rc, own.cppvtable/cppname.tscompiled for MSVC, Linux and MinGW triples, each symbol checked in the IR.test/tester/linkname/cppname-*.ts.Not covered:
@slotnumbering still differs between ABIs (MSVC overload order, destructor slots), so classes with virtual overloads still need one declaration per ABI.🤖 Generated with Claude Code