diff --git a/README.md b/README.md index e5f5f88ad..7d6ed7e87 100644 --- a/README.md +++ b/README.md @@ -687,7 +687,7 @@ Structs, enums and `extern "C"` functions are emitted, and polymorphic classes ( #### C++ classes -A polymorphic class (one with virtual methods), in any namespace, becomes a [`@cpp` class](docs/cpp-classes.md): tslang calls its virtual methods through the object's vtable and its non-virtual methods by their mangled symbols, with no wrapper. clang computes the slots and the symbols for the target's ABI (`--target`), so generate the bindings once per ABI: MSVC and the Itanium ABI (Linux, Android) number the slots differently. +A polymorphic class (one with virtual methods), in any namespace, becomes a [`@cpp` class](docs/cpp-classes.md): tslang calls its virtual methods through the object's vtable and its non-virtual methods by their C++ declarations (`@cppname`, which tslang mangles for the target it compiles for), with no wrapper. clang computes the slots for the target's ABI (`--target`), so generate the bindings once per ABI: MSVC and the Itanium ABI (Linux, Android) number the slots differently. A non-virtual method `@cppname` can't spell (a function pointer or a template in its signature) is bound by the target's mangled symbol, `@linkname`, instead. ```cpp namespace geo { @@ -717,7 +717,7 @@ class Shape { // geo::Shape @slot(2) scale(by: f64): void; @cppnontrivial @slot(4) handle(): [p: Opaque | null]; - @linkname("?id@Shape@geo@@QEAAHH@Z") id(x: s32): s32; + @cppname("int geo::Shape::id(int)") id(x: s32): s32; } @cpp class Square extends Shape { // geo::Square diff --git a/docs/cpp-classes.md b/docs/cpp-classes.md index cb8f16e2d..5004e0d7d 100644 --- a/docs/cpp-classes.md +++ b/docs/cpp-classes.md @@ -102,8 +102,9 @@ counts it: ## Writing a binding -`tsbindgen` writes the binding from the C++ header, with the slots, the mangled symbols and the -`@cppnontrivial` marks clang computes for the target (README, "C++ classes"): +`tsbindgen` writes the binding from the C++ header, with the slots and the `@cppnontrivial` marks +clang computes for the target, and each non-virtual method's `@cppname` declaration (its +`@linkname` symbol when `@cppname` can't spell it or mangles it otherwise) (README, "C++ classes"): `tsbindgen nvrhi.hpp --target x86_64-pc-windows-msvc --filter 'nvrhi::I*' -- -std=c++17`. Writing one by hand: diff --git a/tslang/include/TsClangImporter/BindingModel.h b/tslang/include/TsClangImporter/BindingModel.h index 6e4a7b5b2..f45aadb6c 100644 --- a/tslang/include/TsClangImporter/BindingModel.h +++ b/tslang/include/TsClangImporter/BindingModel.h @@ -81,7 +81,7 @@ enum class DeclKind Enum, // enum E { A = 0 } FixedEnum, // type E = u8; const A = 1; Typedef, // type T = ...; - CppClass, // @cpp class C extends B { @slot(N) f(...): R; @linkname("...") g(...): R; } + CppClass, // @cpp class C extends B { @slot(N) f(...): R; @cppname("R C::g(...)") g(...): R; } Function // declare function f(...): R; }; @@ -92,6 +92,10 @@ struct Method std::string cppName; // the C++ name, for messages int slot = -1; // a virtual method's vtable slot; -1 for a non-virtual one std::string symbol; // a non-virtual method's mangled symbol + // a non-virtual method's C++ declaration, `int32_t ns::C::id(int32_t) const`, for @cppname to + // mangle for any target; empty when @cppname can't spell it or mangles it to another symbol than + // `symbol`, which @linkname binds instead + std::string cppDeclaration; std::vector parameters; TsType result; bool nonTrivialReturn = false; // returns a record with a destructor or a copy constructor diff --git a/tslang/lib/TsClangImporter/BindingPrinter.cpp b/tslang/lib/TsClangImporter/BindingPrinter.cpp index 38bf17694..40b8d4a60 100644 --- a/tslang/lib/TsClangImporter/BindingPrinter.cpp +++ b/tslang/lib/TsClangImporter/BindingPrinter.cpp @@ -305,8 +305,12 @@ llvm::Expected printBindings(const HeaderModel &model, const PrintO member("@cppnontrivial"); } + // a non-virtual method by its C++ declaration, which @cppname mangles for the + // target compiled for; by this target's symbol when it can't be spelled so auto decorator = method.slot >= 0 ? "@slot(" + std::to_string(method.slot) + ") " - : "@linkname(\"" + method.symbol + "\") "; + : method.cppDeclaration.empty() + ? "@linkname(\"" + method.symbol + "\") " + : "@cppname(\"" + method.cppDeclaration + "\") "; member(decorator + method.name + "(" + parameters + "): " + renderType(method.result, nameOf) + ";"); } diff --git a/tslang/lib/TsClangImporter/CMakeLists.txt b/tslang/lib/TsClangImporter/CMakeLists.txt index 22990ad86..2c6746399 100644 --- a/tslang/lib/TsClangImporter/CMakeLists.txt +++ b/tslang/lib/TsClangImporter/CMakeLists.txt @@ -2,6 +2,7 @@ # wrapper generation is meant to reuse it. It links clang's frontend; tslang itself does not. set(LLVM_LINK_COMPONENTS Support + TargetParser ) add_llvm_library(TsClangImporter @@ -9,6 +10,9 @@ add_llvm_library(TsClangImporter TypeMapper.cpp HeaderParser.cpp BindingPrinter.cpp + # tslang's @cppname mangler (LLVM-only, no MLIR), to check that the declaration a binding carries + # mangles to clang's symbol; tslang and TsClangImporter are never linked together + ../TypeScript/CppMangler.cpp LINK_LIBS clangTooling diff --git a/tslang/lib/TsClangImporter/HeaderParser.cpp b/tslang/lib/TsClangImporter/HeaderParser.cpp index 619aa1f68..f62b53ed0 100644 --- a/tslang/lib/TsClangImporter/HeaderParser.cpp +++ b/tslang/lib/TsClangImporter/HeaderParser.cpp @@ -1,8 +1,10 @@ #include "TsClangImporter/HeaderParser.h" #include "TsClangImporter/TypeMapper.h" +#include "TypeScript/CppMangler.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/AST/GlobalDecl.h" #include "clang/AST/Mangle.h" #include "clang/AST/RecordLayout.h" @@ -28,6 +30,8 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/VirtualFileSystem.h" +#include + namespace tsbindgen { @@ -74,6 +78,230 @@ std::string escapeString(llvm::StringRef text) return escaped + "\""; } +// The name of a record or enum with its namespaces and enclosing classes (`ns::Outer::Inner`), as +// @cppname reads it. An inline namespace is spelled too: both schemes mangle it. Nothing for what +// @cppname can't name: an anonymous namespace or record, a template's specialization or a member of +// one, a function's local type. +std::optional cppQualifiedName(const clang::NamedDecl *decl) +{ + std::vector parts; + for (const clang::DeclContext *context = llvm::cast(decl); !context->isTranslationUnit(); + context = context->getParent()) + { + if (llvm::isa(context)) + { + continue; // extern "C++" { ... } + } + + auto *named = llvm::dyn_cast(context); + if (!named || !named->getIdentifier() || llvm::isa(context) || + !(llvm::isa(context))) + { + return std::nullopt; + } + + parts.insert(parts.begin(), named->getName().str()); + } + + return llvm::join(parts, "::"); +} + +std::optional cppBuiltinName(const clang::BuiltinType *builtin) +{ + switch (builtin->getKind()) + { + case clang::BuiltinType::Void: + return "void"; + case clang::BuiltinType::Bool: + return "bool"; + case clang::BuiltinType::Char_S: + case clang::BuiltinType::Char_U: + return "char"; + case clang::BuiltinType::SChar: + return "signed char"; + case clang::BuiltinType::UChar: + return "unsigned char"; + case clang::BuiltinType::WChar_S: + case clang::BuiltinType::WChar_U: + return "wchar_t"; + case clang::BuiltinType::Char8: + return "char8_t"; + case clang::BuiltinType::Char16: + return "char16_t"; + case clang::BuiltinType::Char32: + return "char32_t"; + case clang::BuiltinType::Short: + return "short"; + case clang::BuiltinType::UShort: + return "unsigned short"; + case clang::BuiltinType::Int: + return "int"; + case clang::BuiltinType::UInt: + return "unsigned int"; + case clang::BuiltinType::Long: + return "long"; + case clang::BuiltinType::ULong: + return "unsigned long"; + case clang::BuiltinType::LongLong: + return "long long"; + case clang::BuiltinType::ULongLong: + return "unsigned long long"; + case clang::BuiltinType::Float: + return "float"; + case clang::BuiltinType::Double: + return "double"; + case clang::BuiltinType::LongDouble: + return "long double"; + default: + return std::nullopt; // __int128, _Float16, nullptr_t, ... + } +} + +// `type` as @cppname reads it (CppMangler.h): a builtin, a / typedef as written +// - it mangles as whatever the target @cppname is compiled for defines it as - or a record or enum by +// its qualified name and tag (`class X`, `union U`, `enum E`; a bare name is a struct), with +// `const`/`volatile`, `*`, `&` and `&&`. Nothing for anything else: an array, a function pointer, a +// template's specialization, ... +std::optional cppTypeSpelling(clang::QualType type) +{ + clang::Qualifiers qualifiers; + std::string base; + for (;;) + { + qualifiers.addQualifiers(type.getLocalQualifiers()); + auto *bare = type.getTypePtr(); + if (auto *typedefType = llvm::dyn_cast(bare)) + { + auto name = typedefType->getDecl()->getName(); + if (llvm::is_contained({"int8_t", "uint8_t", "int16_t", "uint16_t", "int32_t", "uint32_t", "int64_t", + "uint64_t", "size_t", "ptrdiff_t", "intptr_t", "uintptr_t"}, + name)) + { + base = name.str(); + break; + } + } + + auto desugared = bare->getLocallyUnqualifiedSingleStepDesugaredType(); + if (desugared.getTypePtr() == bare && !desugared.hasLocalQualifiers()) + { + break; + } + + type = desugared; + } + + if (qualifiers.hasRestrict() || qualifiers.hasAddressSpace() || qualifiers.hasObjCLifetime() || + qualifiers.hasUnaligned()) + { + return std::nullopt; + } + + auto cv = std::string(qualifiers.hasConst() ? "const" : "") + + (qualifiers.hasConst() && qualifiers.hasVolatile() ? " " : "") + + (qualifiers.hasVolatile() ? "volatile" : ""); + auto *bare = type.getTypePtr(); + if (base.empty()) + { + auto pointer = [&](clang::QualType pointee, const char *declarator) -> std::optional { + auto inner = cppTypeSpelling(pointee); + if (!inner) + { + return std::nullopt; + } + + return *inner + declarator + (cv.empty() ? "" : " " + cv); + }; + + if (auto *pointerType = llvm::dyn_cast(bare)) + { + return pointer(pointerType->getPointeeType(), "*"); + } + + if (auto *reference = llvm::dyn_cast(bare)) + { + return pointer(reference->getPointeeType(), "&"); + } + + if (auto *reference = llvm::dyn_cast(bare)) + { + return pointer(reference->getPointeeType(), "&&"); + } + + if (auto *builtin = llvm::dyn_cast(bare)) + { + auto name = cppBuiltinName(builtin); + if (!name) + { + return std::nullopt; + } + + base = *name; + } + else if (auto *tag = llvm::dyn_cast(bare)) + { + auto *decl = tag->getDecl(); + auto name = cppQualifiedName(decl); + if (!name) + { + return std::nullopt; + } + + switch (decl->getTagKind()) + { + case clang::TagTypeKind::Struct: + base = *name; + break; + case clang::TagTypeKind::Class: + base = "class " + *name; + break; + case clang::TagTypeKind::Union: + base = "union " + *name; + break; + case clang::TagTypeKind::Enum: + base = "enum " + *name; + break; + default: + return std::nullopt; // __interface + } + } + else + { + return std::nullopt; + } + } + + return cv.empty() ? base : cv + " " + base; +} + +// A method's declaration as @cppname takes it, its name qualified with its class's (the TS class +// can be named otherwise: --strip-prefix, a namespace, ...), or nothing when a type can't be spelled +std::optional cppMethodDeclaration(const clang::CXXMethodDecl *method) +{ + auto *prototype = method->getType()->getAs(); + auto className = cppQualifiedName(method->getParent()); + auto result = cppTypeSpelling(method->getReturnType()); + if (!prototype || !className || !result || method->getRefQualifier() != clang::RQ_None) + { + return std::nullopt; + } + + std::string parameters; + for (auto parameter : prototype->getParamTypes()) + { + auto spelled = cppTypeSpelling(parameter); + if (!spelled) + { + return std::nullopt; + } + + parameters += (parameters.empty() ? "" : ", ") + *spelled; + } + + return *result + " " + *className + "::" + method->getName().str() + "(" + parameters + ")" + + (method->isConst() ? " const" : "") + (method->isVolatile() ? " volatile" : ""); +} + // An object-like macro's value as a TS literal: an integer, a float or a string, optionally // negated and parenthesized. "" for anything else. std::string literalValue(clang::Preprocessor &preprocessor, llvm::ArrayRef tokens) @@ -712,6 +940,25 @@ class Collector : public clang::RecursiveASTVisitor if (bound.result.skipped()) { bound.skipReason = "result: " + bound.result.skipReason; + return; + } + + // bound by its declaration when @cppname, mangling it for this target, gives clang's symbol: + // the same binding then links on every other target too + if (!method->isVirtual()) + { + if (auto declaration = cppMethodDeclaration(method)) + { + CppMangleContext mangleContext; + mangleContext.isInstanceMethod = true; + std::string symbol; + std::string error; + if (mangleCppName(*declaration, context.getTargetInfo().getTriple(), mangleContext, symbol, error) && + symbol == bound.symbol) + { + bound.cppDeclaration = std::move(*declaration); + } + } } } diff --git a/tslang/test/bindgen/bindgen_cpp_test.ts b/tslang/test/bindgen/bindgen_cpp_test.ts index 5a4b85fef..6ac1bd11c 100644 --- a/tslang/test/bindgen/bindgen_cpp_test.ts +++ b/tslang/test/bindgen/bindgen_cpp_test.ts @@ -17,7 +17,7 @@ function main() { print(pair.a, pair.b); const handle = shape.handle(); // a non-trivial one (@cppnontrivial) print(handle.p == shape); - print(shape.id(5)); // a non-virtual method, by its mangled symbol + print(shape.id(5)); // a non-virtual method, by its C++ declaration (@cppname) let extent: Extent = [3, 4]; // fx::Extent, a struct in a namespace: a tuple print(shape.fits(Ref(extent), null), shape.fits(Ref(extent), Ref(extent))); // a reference, a pointer or null diff --git a/tslang/test/bindgen/fixture_cpp.hpp b/tslang/test/bindgen/fixture_cpp.hpp index 529336316..27df771ee 100644 --- a/tslang/test/bindgen/fixture_cpp.hpp +++ b/tslang/test/bindgen/fixture_cpp.hpp @@ -1,5 +1,5 @@ // A C++ library for tsbindgen's @cpp classes (run-bindgen-test.cmake, MODE=cpp): its polymorphic -// classes become @cpp classes, called through their vtables and by their mangled symbols. +// classes become @cpp classes, called through their vtables and by their C++ declarations. // // No C++ runtime is needed to link it: no destructors, pure virtual methods, RTTI or exceptions, and // its objects are constant-initialized (constexpr constructors), so a tslang program links its object @@ -100,7 +100,7 @@ class FX_API Shape virtual int32_t fits(const Extent &extent, const Extent *limit); virtual int32_t sum(const Batch &batch); - // non-virtual: called by its mangled symbol + // non-virtual: called by its declaration, @cppname("int32_t fx::Shape::id(int32_t)") int32_t id(int32_t x); int64_t sides; // 8 bytes: no tail padding, which the Itanium ABI would put `side` in diff --git a/tslang/test/bindgen/run-bindgen-test.cmake b/tslang/test/bindgen/run-bindgen-test.cmake index 2eb9b51bf..5783568e2 100644 --- a/tslang/test/bindgen/run-bindgen-test.cmake +++ b/tslang/test/bindgen/run-bindgen-test.cmake @@ -6,7 +6,7 @@ # print expected.txt. MODE=namespace does the same through `--namespace Fx --strip-prefix fx_`, # which only links because @linkname binds each renamed function to its C symbol. MODE=cpp does it # for fixture_cpp.hpp's C++ classes, which become @cpp classes: their virtual methods called through -# the vtable the host's clang made, their non-virtual ones by their mangled symbols. +# the vtable the host's clang made, their non-virtual ones by their declarations (@cppname). cmake_minimum_required(VERSION 3.17.3) diff --git a/tslang/unittests/TsClangImporter/CppClassTest.cpp b/tslang/unittests/TsClangImporter/CppClassTest.cpp index 4aa2dd782..99de61669 100644 --- a/tslang/unittests/TsClangImporter/CppClassTest.cpp +++ b/tslang/unittests/TsClangImporter/CppClassTest.cpp @@ -1,6 +1,7 @@ // A polymorphic C++ class is a @cpp class: its virtual methods @slot(N), with the slot clang's -// vtable layout gives for the target's ABI, its non-virtual ones @linkname with their mangled -// symbols. The slot numbers below are the compilers' own: MSVC's from +// vtable layout gives for the target's ABI, its non-virtual ones @cppname with their C++ +// declarations (@linkname with the target's mangled symbol when @cppname can't spell one). The slot +// numbers below are the compilers' own: MSVC's from // `cl /d1reportSingleClassLayoutCircle`, the Itanium ABI's from clang's -fdump-vtable-layouts. #include "ImporterTestHelper.h" @@ -74,18 +75,85 @@ TEST(CppClass, ItaniumSlots) EXPECT_THAT(out, HasSubstr(" @slot(8) contains(other: Shape): boolean;\n")); } -TEST(CppClass, NonVirtualMethodsAreBoundByTheirMangledSymbols) +// one binding for every target: @cppname mangles the declaration for the one compiled for +TEST(CppClass, NonVirtualMethodsAreBoundByTheirDeclarations) { auto header = "namespace geo { class Shape { public: virtual ~Shape(); int id(int x); int inlined() { return 1; }\n" "static int count(); int operator+(int x); }; }\n"; auto windows = generate(header, {}, windowsTarget, {}, "c++"); - EXPECT_THAT(windows.text, HasSubstr(" @linkname(\"?id@Shape@geo@@QEAAHH@Z\") id(x: s32): s32;\n")); + EXPECT_THAT(windows.text, HasSubstr(" @cppname(\"int geo::Shape::id(int)\") id(x: s32): s32;\n")); EXPECT_THAT(windows.text, HasSubstr(" // skipped: inlined \xE2\x80\x94 inline method: no symbol to link against\n")); EXPECT_THAT(windows.warnings, Contains("skipped geo::Shape::inlined: inline method: no symbol to link against")); // static members and operators aren't methods a @cpp class can have EXPECT_THAT(windows.text, AllOf(Not(HasSubstr("count")), Not(HasSubstr("operator")))); - EXPECT_THAT(cpp(header, linuxTarget), HasSubstr(" @linkname(\"_ZN3geo5Shape2idEi\") id(x: s32): s32;\n")); + EXPECT_THAT(cpp(header, linuxTarget), HasSubstr(" @cppname(\"int geo::Shape::id(int)\") id(x: s32): s32;\n")); +} + +// the types @cppname spells: builtins, typedefs as written (another typedef as what it +// names), records by their qualified names and tags, enums, cv-qualified pointers and references, +// and a const method; the same on every target +TEST(CppClass, DeclarationsSpellEveryTypeCppnameMangles) +{ + auto header = "#include \n#include \n" + "namespace ns { struct Pt { int x; }; class Obj; union U { int i; float f; }; enum E { A };\n" + "enum class Scoped : uint8_t { B }; typedef int myint; typedef struct Pt PtAlias;\n" + "class Widget { public: virtual ~Widget();\n" + " int64_t wide(uint64_t a, int32_t b, size_t c, ptrdiff_t d, uint8_t e);\n" + " const char *name(char *const *argv, const volatile int *v) const;\n" + " void kinds(Pt *p, Obj *o, U &u, E e, Scoped s, const PtAlias *alias, myint m);\n" + " void words(unsigned long long a, long b, unsigned short c, signed char d, wchar_t e, bool f,\n" + " double g, float h, char16_t i);\n" + " Widget *self(Widget &&other); }; }\n"; + for (auto target : {windowsTarget, linuxTarget, "aarch64-apple-darwin", "aarch64-linux-gnu"}) + { + auto out = generate(header, {}, target, {}, "c++"); + EXPECT_THAT(out.text, HasSubstr(" @cppname(\"int64_t ns::Widget::wide(uint64_t, int32_t, size_t, ptrdiff_t, " + "uint8_t)\") wide(")) + << target; + EXPECT_THAT(out.text, HasSubstr(" @cppname(\"const char* ns::Widget::name(char* const*, const volatile " + "int*) const\") name(")) + << target; + EXPECT_THAT(out.text, HasSubstr(" @cppname(\"void ns::Widget::kinds(ns::Pt*, class ns::Obj*, union ns::U&, " + "enum ns::E, enum ns::Scoped, const ns::Pt*, int)\") kinds(")) + << target; + EXPECT_THAT(out.text, HasSubstr(" @cppname(\"void ns::Widget::words(unsigned long long, long, unsigned " + "short, signed char, wchar_t, bool, double, float, char16_t)\") words(")) + << target; + EXPECT_THAT(out.text, HasSubstr(" @cppname(\"class ns::Widget* ns::Widget::self(class ns::Widget&&)\") self(")) + << target; + EXPECT_THAT(out.text, Not(HasSubstr("@linkname"))) << target; + } +} + +// what @cppname can't spell stays bound by this target's symbol: a function pointer, a +// ref-qualified method, a template's specialization +TEST(CppClass, DeclarationsCppnameCantSpellKeepTheSymbol) +{ + auto header = "template struct Box { T v; };\n" + "class Widget { public: virtual ~Widget(); int call(int (*fn)(int)); int ref() &;\n" + " int boxed(Box *box); int plain(int x); };\n"; + auto windows = cpp(header); + EXPECT_THAT(windows, HasSubstr(" @linkname(\"?call@Widget@@QEAAHP6AHH@Z@Z\") call(")); + EXPECT_THAT(windows, HasSubstr(" @linkname(\"?ref@Widget@@QEGAAHXZ\") ref(): s32;\n")); + EXPECT_THAT(windows, HasSubstr(" @linkname(\"?boxed@Widget@@QEAAHPEAU?$Box@H@@@Z\") boxed(")); + EXPECT_THAT(windows, HasSubstr(" @cppname(\"int Widget::plain(int)\") plain(x: s32): s32;\n")); + + auto linux = cpp(header, linuxTarget); + EXPECT_THAT(linux, HasSubstr(" @linkname(\"_ZN6Widget4callEPFiiE\") call(")); + EXPECT_THAT(linux, HasSubstr(" @linkname(\"_ZNR6Widget3refEv\") ref(): s32;\n")); + EXPECT_THAT(linux, HasSubstr(" @linkname(\"_ZN6Widget5boxedEP3BoxIiE\") boxed(")); +} + +// a declaration @cppname spells but mangles to another symbol than clang's stays bound by clang's: +// a header's own `int64_t` of `long long` where the target's is `long` (64-bit Linux) +TEST(CppClass, DeclarationsThatMangleOtherwiseKeepTheSymbol) +{ + auto header = "typedef long long int64_t;\n" + "class Widget { public: virtual ~Widget(); int64_t wide(int64_t x); };\n"; + EXPECT_THAT(cpp(header, linuxTarget), HasSubstr(" @linkname(\"_ZN6Widget4wideEx\") wide(x: int64_t): int64_t;\n")); + // where `int64_t` is `long long`, the declaration is the target's own + EXPECT_THAT(cpp(header), HasSubstr(" @cppname(\"int64_t Widget::wide(int64_t)\") wide(x: int64_t): int64_t;\n")); } // A record returned by value is a tuple with its layout; one C++ can't pass in registers (a diff --git a/tslang/unittests/TsClangImporter/NamingTest.cpp b/tslang/unittests/TsClangImporter/NamingTest.cpp index 2681a9c1b..bad669a81 100644 --- a/tslang/unittests/TsClangImporter/NamingTest.cpp +++ b/tslang/unittests/TsClangImporter/NamingTest.cpp @@ -43,6 +43,19 @@ TEST(Naming, StripPrefixRenamesDeclarationsAndTheirUses) EXPECT_THAT(out, HasSubstr(" @linkname(\"other\") export declare function other(): s32;\n")); } +// a @cpp class renamed (stripped, a namespace dropped): @cppname names the method by its C++ class, +// never by the TS class it is declared in +TEST(Naming, StripPrefixKeepsTheCppClassInCppname) +{ + tsbindgen::PrintOptions options; + options.stripPrefix = "fx_"; + auto out = generate("namespace lib { class fx_Widget { public: virtual ~fx_Widget(); int id(int x); }; }\n", + options, windowsTarget, {}, "c++") + .text; + EXPECT_THAT(out, HasSubstr("class Widget { // lib::fx_Widget\n" + " @cppname(\"int lib::fx_Widget::id(int)\") id(x: s32): s32;\n")); +} + TEST(Naming, StripPrefixKeepsTheCNameWhenStrippingWouldBreakIt) { tsbindgen::PrintOptions options;