From 46695afa36b24f058000b7a789959a4d70b6a19c Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 25 Oct 2026 19:14:37 +0000 Subject: [PATCH 1/2] @cppname: bind a C++ function by its declaration, mangled for the target @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 / 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 --- docs/cpp-classes.md | 24 +- tslang/include/TypeScript/CppMangler.h | 37 + tslang/include/TypeScript/Defines.h | 3 + tslang/lib/TypeScript/CMakeLists.txt | 1 + tslang/lib/TypeScript/CppMangler.cpp | 1049 +++++++++++++++++ tslang/lib/TypeScript/MLIRGenClasses.cpp | 5 +- tslang/lib/TypeScript/MLIRGenImpl.h | 56 + tslang/test/tester/CMakeLists.txt | 26 +- tslang/test/tester/cppvtable/abi_itanium.ts | 7 +- tslang/test/tester/cppvtable/abi_msvc.ts | 7 +- tslang/test/tester/cppvtable/cppname.ts | 24 + .../tester/linkname/cppname-const-function.ts | 6 + .../tester/linkname/cppname-unparsable.ts | 6 + .../test/tester/linkname/cppname-variable.ts | 6 + tslang/unittests/MLIRGen/CMakeLists.txt | 1 + tslang/unittests/MLIRGen/CppMangler.cpp | 262 ++++ 16 files changed, 1510 insertions(+), 10 deletions(-) create mode 100644 tslang/include/TypeScript/CppMangler.h create mode 100644 tslang/lib/TypeScript/CppMangler.cpp create mode 100644 tslang/test/tester/cppvtable/cppname.ts create mode 100644 tslang/test/tester/linkname/cppname-const-function.ts create mode 100644 tslang/test/tester/linkname/cppname-unparsable.ts create mode 100644 tslang/test/tester/linkname/cppname-variable.ts create mode 100644 tslang/unittests/MLIRGen/CppMangler.cpp diff --git a/docs/cpp-classes.md b/docs/cpp-classes.md index 136c9c496..cb8f16e2d 100644 --- a/docs/cpp-classes.md +++ b/docs/cpp-classes.md @@ -47,6 +47,7 @@ virtual only as in C++: | `@slot(N) name(...): T;` | a C++ virtual method: slot N of the vtable, counted from 0 with the base classes' slots | | `name(...): T;` overriding a base class's `@slot` method | the same slot, as in C++ (`@slot` may be repeated, with the same number) | | `@linkname("symbol") name(...): T;` | a C++ non-virtual method, called by its (mangled) symbol as `symbol(this, args...)` | +| `@cppname("T name(...)") name(...): T;` | the same, by its C++ declaration: the compiler mangles it for the target (see below) | | `name(...): T { ... }` with no `@slot`, overriding nothing | a method of TypeScript's own, called directly; not in the vtable | The slots may be given in any order, and a slot no method names is skipped: declare only the @@ -68,7 +69,7 @@ errors and not warnings. | Not allowed | Why | | --- | --- | -| a method without a body and without `@slot` or `@linkname`, overriding nothing | it's neither a slot nor a symbol: a forgotten `@slot` | +| a method without a body and without `@slot`, `@linkname` or `@cppname`, overriding nothing | it's neither a slot nor a symbol: a forgotten `@slot` | | an `abstract` method without `@slot` | it is virtual, so it needs its slot | | `@slot` on a method of an ordinary class | an ordinary class's vtable is tslang's own | | a constructor or a field initializer, in a class none of whose virtual methods has a body | the objects and their code are C++'s (see "Implementing C++ methods") | @@ -134,6 +135,27 @@ Writing one by hand: - **Non-virtual methods** are bound by their mangled symbol: `@linkname("?scaled@Methods@@QEAAHH@Z")` under MSVC, `@linkname("_ZN7Methods6scaledEi")` under the Itanium ABI. The symbol has to be exported by the C++ library (`__declspec(dllexport)` on the method or the class). + + `@cppname` takes the C++ declaration instead and mangles it for the target: MSVC's scheme on + `windows-msvc`, the Itanium one (g++, clang) everywhere else, MinGW included. That way one class + works on every OS: + + ```ts + @cppname("int scaled(int)") scaled(x: s32): s32; // ?scaled@Methods@@QEAAHH@Z, _ZN7Methods6scaledEi + @cppname("int scaled(double)") scaled(x: f64): s32; + @cppname("int get() const") get(): s32; // a const method + @cppname("size_t length(const char* text)") length(text: string): u64; + ``` + + An unqualified name is a member of the class. A qualified one is taken as written + (`ns::Methods::scaled`). On a `declare function`, `@cppname("int ns::sum(int, int)")` binds a + free function. The types are C++'s builtin ones; the ``/`` typedefs as the + target defines them (`int64_t` is `long` on 64-bit Linux and `long long` on Windows and macOS, and + each mangles differently); and records by qualified name (`ns::Pt*`, `const Pair&`), all with + `const`, `*`, `&`, `&&`. A bare record name is a `struct`: write `class Obj`, `union U` + or `enum E` when it isn't, because MSVC encodes which one it is. Names aren't looked up, so write + a record in another namespace with its full name. Templates, operators, constructors, + destructors, function pointers and arrays aren't supported: use `@linkname` for those. - **Struct returned by value.** A C++ virtual method that returns a class or struct by value (for example `nvrhi::RefCountPtr` or `nvrhi::Object`) returns a record. Declare its return type as a tuple with the record's layout: diff --git a/tslang/include/TypeScript/CppMangler.h b/tslang/include/TypeScript/CppMangler.h new file mode 100644 index 000000000..5809452a4 --- /dev/null +++ b/tslang/include/TypeScript/CppMangler.h @@ -0,0 +1,37 @@ +#ifndef TYPESCRIPT_CPPMANGLER_H_ +#define TYPESCRIPT_CPPMANGLER_H_ + +#include "llvm/ADT/StringRef.h" +#include "llvm/TargetParser/Triple.h" + +#include + +// @cppname("int Methods::scaled(int)"): a C++ declaration, turned into the symbol the target's C++ +// compiler gives it - MSVC's scheme on windows-msvc, the Itanium one of g++ and clang everywhere +// else (MinGW included) - so one declaration binds the same C++ function on every OS, where +// @linkname needs each OS's symbol spelled out. +// +// The declaration is a return type, a name, and parameter types, and `const` after them for a +// const method. Types: the builtin ones (`unsigned long long`, `char16_t`, ...), the +// and typedefs (`int64_t`, `size_t`, ...) as the target defines them - `int64_t` is +// `long` on 64-bit Linux and `long long` on Windows and macOS, which mangle differently - and a +// struct, class, union or enum by its qualified name (`ns::Pt`, `class Obj`; a bare name is a +// struct), with `const`/`volatile`, `*`, `&` and `&&`. Templates, operators, constructors, +// destructors and function pointers are not supported. +struct CppMangleContext +{ + // the class an unqualified name is a member of (`int scaled(int)` in a @cpp class `Methods` is + // `Methods::scaled`); empty outside a class + llvm::StringRef className; + // a method with `this` (MSVC encodes it, and its `const`); a static method or a free function + // has none + bool isInstanceMethod = false; + // a static method: MSVC tells it from a free function in a namespace of that name + bool isStaticMethod = false; +}; + +// The symbol, or false with `error` saying what is wrong with the declaration. +bool mangleCppName(llvm::StringRef declaration, const llvm::Triple &triple, const CppMangleContext &context, + std::string &symbol, std::string &error); + +#endif // TYPESCRIPT_CPPMANGLER_H_ diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 0ab1aa3ca..44626ac46 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -352,6 +352,9 @@ #define DLL_IMPORT "dllimport" #define DLL_NAME "dllname" #define LINK_NAME "linkname" +// @cppname("int Methods::scaled(int)"): @linkname under the symbol the target's C++ compiler gives +// that declaration (see CppMangler.h) +#define CPP_NAME "cppname" // A class laid over an existing C++ object: its vtable is the C++ one (see ClassInfo::isCppVTable) #define CPP_VTABLE "cpp" // `@final class C`: no class may extend C, so a call on a value of type C is a direct call to the diff --git a/tslang/lib/TypeScript/CMakeLists.txt b/tslang/lib/TypeScript/CMakeLists.txt index 69c6de3b7..02ffb8cda 100644 --- a/tslang/lib/TypeScript/CMakeLists.txt +++ b/tslang/lib/TypeScript/CMakeLists.txt @@ -16,6 +16,7 @@ add_mlir_dialect_library(MLIRTypeScript TypeScriptDialectTranslation.cpp AsyncDialectTranslation.cpp DiagnosticHelper.cpp + CppMangler.cpp MLIRGen.cpp MLIRGenAccessCall.cpp MLIRGenCast.cpp diff --git a/tslang/lib/TypeScript/CppMangler.cpp b/tslang/lib/TypeScript/CppMangler.cpp new file mode 100644 index 000000000..884d3c701 --- /dev/null +++ b/tslang/lib/TypeScript/CppMangler.cpp @@ -0,0 +1,1049 @@ +#include "TypeScript/CppMangler.h" + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringSwitch.h" + +#include +#include + +// Hand-written rather than clang's MangleContext: tslang doesn't link clang's frontend, and a +// declaration of builtin types, records, pointers and references needs only a small part of either +// scheme. The expected symbols in unittests/MLIRGen/CppMangler.cpp come from clang. + +namespace +{ + +enum class Builtin +{ + Void, + Bool, + Char, + SChar, + UChar, + Short, + UShort, + Int, + UInt, + Long, + ULong, + LongLong, + ULongLong, + Float, + Double, + LongDouble, + WChar, + Char8, + Char16, + Char32 +}; + +enum class Tag +{ + Struct, + Class, + Union, + Enum +}; + +struct Type; +using TypePtr = std::shared_ptr; + +struct Type +{ + enum class Kind + { + Builtin, + Named, + Pointer, + LValueReference, + RValueReference + }; + + Kind kind = Kind::Builtin; + Builtin builtin = Builtin::Void; + Tag tag = Tag::Struct; + // a record's or an enum's qualified name, the outermost scope first + llvm::SmallVector name; + bool isConst = false; + bool isVolatile = false; + TypePtr pointee; +}; + +struct Declaration +{ + TypePtr result; + // the function's qualified name, the outermost scope first + llvm::SmallVector name; + llvm::SmallVector params; + bool isVariadic = false; + // a const (volatile) method + bool isConst = false; + bool isVolatile = false; +}; + +// The / typedefs that differ between targets, as clang's TargetInfo defines them +struct TargetTypedefs +{ + Builtin int64 = Builtin::LongLong; + Builtin size = Builtin::ULong; + Builtin ptrdiff = Builtin::Long; + Builtin intptr = Builtin::Long; + + explicit TargetTypedefs(const llvm::Triple &triple) + { + auto is64 = triple.isArch64Bit(); + if (triple.isOSWindows()) + { + // LLP64: long stays 32-bit + size = is64 ? Builtin::ULongLong : Builtin::UInt; + ptrdiff = intptr = is64 ? Builtin::LongLong : Builtin::Int; + } + else if (triple.isOSDarwin()) + { + ptrdiff = is64 ? Builtin::Long : Builtin::Int; + } + else if (triple.isWasm()) + { + int64 = is64 ? Builtin::Long : Builtin::LongLong; + } + else if (is64) + { + int64 = Builtin::Long; + } + else + { + size = Builtin::UInt; + ptrdiff = intptr = Builtin::Int; + } + } +}; + +Builtin unsignedOf(Builtin builtin) +{ + switch (builtin) + { + case Builtin::Int: + return Builtin::UInt; + case Builtin::Long: + return Builtin::ULong; + case Builtin::LongLong: + return Builtin::ULongLong; + default: + return builtin; + } +} + +std::string nameKey(llvm::ArrayRef name) +{ + return llvm::join(name, "::"); +} + +// a type, for telling whether two are the same: a record is its qualified name, so the key of a +// record and of the scope of that name match +std::string typeKey(const Type &type) +{ + std::string key; + switch (type.kind) + { + case Type::Kind::Builtin: + key = "#" + std::to_string(static_cast(type.builtin)); + break; + case Type::Kind::Named: + key = nameKey(type.name); + break; + case Type::Kind::Pointer: + key = "P(" + typeKey(*type.pointee) + ")"; + break; + case Type::Kind::LValueReference: + key = "R(" + typeKey(*type.pointee) + ")"; + break; + case Type::Kind::RValueReference: + key = "O(" + typeKey(*type.pointee) + ")"; + break; + } + + if (type.isConst || type.isVolatile) + { + key = std::string(type.isVolatile ? "V" : "") + (type.isConst ? "K" : "") + "(" + key + ")"; + } + + return key; +} + +class Parser +{ + llvm::StringRef text; + size_t pos = 0; + TargetTypedefs typedefs; + std::string &error; + + // one token: an identifier, `::`, `&&`, `...`, or one punctuation character; empty at the end + llvm::StringRef peek() + { + while (pos < text.size() && llvm::isSpace(text[pos])) + { + ++pos; + } + + if (pos >= text.size()) + { + return {}; + } + + auto rest = text.drop_front(pos); + if (llvm::isAlpha(rest[0]) || rest[0] == '_') + { + return rest.take_while([](char c) { return llvm::isAlnum(c) || c == '_'; }); + } + + for (auto multi : {"::", "&&", "..."}) + { + if (rest.starts_with(multi)) + { + return rest.take_front(llvm::StringRef(multi).size()); + } + } + + return rest.take_front(1); + } + + llvm::StringRef next() + { + auto token = peek(); + pos += token.size(); + return token; + } + + bool accept(llvm::StringRef token) + { + if (peek() == token) + { + next(); + return true; + } + + return false; + } + + static bool isIdentifier(llvm::StringRef token) + { + return !token.empty() && (llvm::isAlpha(token[0]) || token[0] == '_'); + } + + bool fail(const llvm::Twine &message) + { + if (error.empty()) + { + auto token = peek(); + error = (message + (token.empty() ? llvm::Twine(" at the end") : " at '" + token + "'")).str(); + } + + return false; + } + + bool acceptQualifiers(bool &isConst, bool &isVolatile) + { + auto any = false; + for (;;) + { + if (accept("const")) + { + isConst = any = true; + } + else if (accept("volatile")) + { + isVolatile = any = true; + } + else + { + return any; + } + } + } + + bool parseQualifiedName(llvm::SmallVectorImpl &name) + { + do + { + auto token = peek(); + if (token == "operator" || token == "~") + { + return fail("operators, constructors and destructors aren't supported"); + } + + if (!isIdentifier(token)) + { + return fail("expected a name"); + } + + name.push_back(next().str()); + } while (accept("::")); + + if (peek() == "<") + { + return fail("templates aren't supported"); + } + + return true; + } + + std::optional typedefBuiltin(llvm::StringRef token) + { + return llvm::StringSwitch>(token) + .Case("int8_t", Builtin::SChar) + .Case("uint8_t", Builtin::UChar) + .Case("int16_t", Builtin::Short) + .Case("uint16_t", Builtin::UShort) + .Case("int32_t", Builtin::Int) + .Case("uint32_t", Builtin::UInt) + .Case("int64_t", typedefs.int64) + .Case("uint64_t", unsignedOf(typedefs.int64)) + .Case("size_t", typedefs.size) + .Case("ptrdiff_t", typedefs.ptrdiff) + .Case("intptr_t", typedefs.intptr) + .Case("uintptr_t", unsignedOf(typedefs.intptr)) + .Default(std::nullopt); + } + + // the type specifiers: a builtin type of one or more words, a typedef, or a record's name + bool parseBaseType(Type &type) + { + int isSigned = 0, isUnsigned = 0, chars = 0, shorts = 0, ints = 0, longs = 0, doubles = 0; + std::optional single; + auto words = 0; + for (;;) + { + if (acceptQualifiers(type.isConst, type.isVolatile)) + { + continue; + } + + auto token = peek(); + auto loneWord = llvm::StringSwitch>(token) + .Case("void", Builtin::Void) + .Case("bool", Builtin::Bool) + .Case("float", Builtin::Float) + .Case("wchar_t", Builtin::WChar) + .Case("char8_t", Builtin::Char8) + .Case("char16_t", Builtin::Char16) + .Case("char32_t", Builtin::Char32) + .Default(std::nullopt); + auto counter = llvm::StringSwitch(token) + .Case("signed", &isSigned) + .Case("unsigned", &isUnsigned) + .Case("char", &chars) + .Case("short", &shorts) + .Case("int", &ints) + .Case("long", &longs) + .Case("double", &doubles) + .Default(nullptr); + if (loneWord) + { + single = loneWord; + } + else if (counter) + { + ++*counter; + } + else + { + break; + } + + next(); + ++words; + } + + if (words == 0) + { + type.kind = Type::Kind::Named; + auto token = peek(); + auto tag = llvm::StringSwitch>(token) + .Case("struct", Tag::Struct) + .Case("class", Tag::Class) + .Case("union", Tag::Union) + .Case("enum", Tag::Enum) + .Default(std::nullopt); + if (tag) + { + next(); + type.tag = *tag; + } + + if (!isIdentifier(peek())) + { + return fail("expected a type"); + } + + if (!parseQualifiedName(type.name)) + { + return false; + } + + // `int64_t`, `std::size_t` + if (!tag && (type.name.size() == 1 || (type.name.size() == 2 && type.name.front() == "std"))) + { + if (auto builtin = typedefBuiltin(type.name.back())) + { + type.kind = Type::Kind::Builtin; + type.builtin = *builtin; + type.name.clear(); + } + } + + acceptQualifiers(type.isConst, type.isVolatile); + return true; + } + + type.kind = Type::Kind::Builtin; + auto sign = isSigned + isUnsigned; + auto counted = chars + shorts + ints + longs + doubles; + if (single) + { + if (words != 1) + { + return fail("invalid combination of type specifiers"); + } + + type.builtin = *single; + } + else if (sign > 1 || chars > 1 || shorts > 1 || ints > 1 || longs > 2 || doubles > 1) + { + return fail("invalid combination of type specifiers"); + } + else if (doubles) + { + if (sign || counted != 1 + longs || longs > 1) + { + return fail("invalid combination of type specifiers"); + } + + type.builtin = longs ? Builtin::LongDouble : Builtin::Double; + } + else if (chars) + { + if (counted != 1) + { + return fail("invalid combination of type specifiers"); + } + + type.builtin = isSigned ? Builtin::SChar : isUnsigned ? Builtin::UChar : Builtin::Char; + } + else if (shorts) + { + if (longs) + { + return fail("invalid combination of type specifiers"); + } + + type.builtin = isUnsigned ? Builtin::UShort : Builtin::Short; + } + else if (longs) + { + type.builtin = longs == 2 ? Builtin::LongLong : Builtin::Long; + type.builtin = isUnsigned ? unsignedOf(type.builtin) : type.builtin; + } + else + { + type.builtin = isUnsigned ? Builtin::UInt : Builtin::Int; + } + + acceptQualifiers(type.isConst, type.isVolatile); + return true; + } + + // the specifiers, then `*` (and its own const), `&`, `&&` + TypePtr parseType() + { + auto type = std::make_shared(); + if (!parseBaseType(*type)) + { + return nullptr; + } + + for (;;) + { + auto kind = peek() == "*" ? Type::Kind::Pointer + : peek() == "&" ? Type::Kind::LValueReference + : peek() == "&&" ? Type::Kind::RValueReference + : Type::Kind::Builtin; + if (kind == Type::Kind::Builtin) + { + break; + } + + next(); + if (type->kind == Type::Kind::LValueReference || type->kind == Type::Kind::RValueReference) + { + fail("a pointer or reference to a reference isn't a type"); + return nullptr; + } + + auto outer = std::make_shared(); + outer->kind = kind; + outer->pointee = type; + if (kind == Type::Kind::Pointer) + { + acceptQualifiers(outer->isConst, outer->isVolatile); + } + + type = outer; + } + + // `int (*)(int)`, `int[4]`; a `(` alone is a parameter list after a missing name + auto save = pos; + auto isFunctionPointer = accept("(") && (peek() == "*" || peek() == "&"); + pos = save; + if (isFunctionPointer || peek() == "[") + { + fail("function pointers and arrays aren't supported"); + return nullptr; + } + + return type; + } + + bool parseParams(Declaration &declaration) + { + if (accept(")")) + { + return true; + } + + // `f(void)` + auto save = pos; + if (accept("void") && accept(")")) + { + return true; + } + + pos = save; + do + { + if (accept("...")) + { + declaration.isVariadic = true; + break; + } + + auto param = parseType(); + if (!param) + { + return false; + } + + if (param->kind == Type::Kind::Builtin && param->builtin == Builtin::Void) + { + return fail("a parameter can't be void"); + } + + declaration.params.push_back(param); + // the parameter's name, which doesn't change the symbol + if (isIdentifier(peek()) && peek() != "const" && peek() != "volatile") + { + next(); + } + } while (accept(",")); + + if (!accept(")")) + { + return fail(peek() == "=" ? "default arguments aren't supported" : "expected ')'"); + } + + return true; + } + +public: + Parser(llvm::StringRef text, const llvm::Triple &triple, std::string &error) : text(text), typedefs(triple), error(error) + { + } + + bool parse(Declaration &declaration) + { + // nothing a symbol depends on + while (accept("inline") || accept("static") || accept("extern")) + { + } + + if (peek() == "virtual") + { + return fail("a virtual method is called through its vtable slot, @slot(N), not by its symbol"); + } + + declaration.result = parseType(); + if (!declaration.result || !parseQualifiedName(declaration.name)) + { + return false; + } + + if (!accept("(")) + { + return fail("expected '(' after the function's name"); + } + + if (!parseParams(declaration)) + { + return false; + } + + acceptQualifiers(declaration.isConst, declaration.isVolatile); + if (peek() == "&" || peek() == "&&" || peek() == "noexcept" || peek() == "throw") + { + return fail("ref-qualifiers and exception specifications aren't supported"); + } + + accept(";"); + if (!peek().empty()) + { + return fail("unexpected text after the declaration"); + } + + return true; + } +}; + +// https://learn.microsoft.com/cpp/build/reference/decorated-names, and clang's MicrosoftMangle.cpp +class MicrosoftMangler +{ + std::string out; + bool is64; + bool isX86; + // the first ten names, and the first ten parameter types longer than a letter (by typeKey): a + // later one is their index + llvm::SmallVector names; + llvm::SmallVector paramTypes; + + void sourceName(llvm::StringRef name) + { + auto it = llvm::find(names, name); + if (it != names.end()) + { + out += static_cast('0' + (it - names.begin())); + return; + } + + out += name; + out += '@'; + if (names.size() < 10) + { + names.push_back(name.str()); + } + } + + // innermost first, each ended by `@`, and one more `@` after them + void qualifiedName(llvm::ArrayRef name) + { + for (auto &part : llvm::reverse(name)) + { + sourceName(part); + } + + out += '@'; + } + + static char qualifiers(bool isConst, bool isVolatile) + { + return isConst ? (isVolatile ? 'D' : 'B') : (isVolatile ? 'C' : 'A'); + } + + void builtin(Builtin builtin) + { + switch (builtin) + { + case Builtin::Void: out += "X"; break; + case Builtin::Bool: out += "_N"; break; + case Builtin::Char: out += "D"; break; + case Builtin::SChar: out += "C"; break; + case Builtin::UChar: out += "E"; break; + case Builtin::Short: out += "F"; break; + case Builtin::UShort: out += "G"; break; + case Builtin::Int: out += "H"; break; + case Builtin::UInt: out += "I"; break; + case Builtin::Long: out += "J"; break; + case Builtin::ULong: out += "K"; break; + case Builtin::LongLong: out += "_J"; break; + case Builtin::ULongLong: out += "_K"; break; + case Builtin::Float: out += "M"; break; + case Builtin::Double: out += "N"; break; + case Builtin::LongDouble: out += "O"; break; + case Builtin::WChar: out += "_W"; break; + case Builtin::Char8: out += "_Q"; break; + case Builtin::Char16: out += "_S"; break; + case Builtin::Char32: out += "_U"; break; + } + } + + void named(const Type &type) + { + switch (type.tag) + { + case Tag::Struct: out += "U"; break; + case Tag::Class: out += "V"; break; + case Tag::Union: out += "T"; break; + case Tag::Enum: out += "W4"; break; + } + + qualifiedName(type.name); + } + + // a type's own const is in the letter of what points at it, or dropped at the top of a parameter; + // a pointer's own const is in its first letter, `QEAH` for `int *const` + void type(const Type &type) + { + switch (type.kind) + { + case Type::Kind::Builtin: + builtin(type.builtin); + break; + case Type::Kind::Named: + named(type); + break; + case Type::Kind::Pointer: + case Type::Kind::LValueReference: + case Type::Kind::RValueReference: + if (type.kind == Type::Kind::Pointer) + { + out += type.isConst ? (type.isVolatile ? 'S' : 'Q') : (type.isVolatile ? 'R' : 'P'); + } + else + { + out += type.kind == Type::Kind::LValueReference ? "A" : "$$Q"; + } + + if (is64) + { + out += 'E'; + } + + out += qualifiers(type.pointee->isConst, type.pointee->isVolatile); + this->type(*type.pointee); + break; + } + } + + void resultType(const Type &type) + { + if (type.kind == Type::Kind::Named) + { + // a record or an enum returned by value + out += '?'; + out += qualifiers(type.isConst, type.isVolatile); + } + + this->type(type); + } + + // by the type, not by its text: a record's name is its index the second time + void paramType(const Type &type) + { + auto unqualified = type; + if (type.kind == Type::Kind::Builtin || type.kind == Type::Kind::Named) + { + unqualified.isConst = unqualified.isVolatile = false; + } + + auto key = typeKey(unqualified); + auto it = llvm::find(paramTypes, key); + if (it != paramTypes.end()) + { + out += static_cast('0' + (it - paramTypes.begin())); + return; + } + + auto start = out.size(); + this->type(type); + if (out.size() - start > 1 && paramTypes.size() < 10) + { + paramTypes.push_back(key); + } + } + +public: + explicit MicrosoftMangler(const llvm::Triple &triple) + : is64(triple.isArch64Bit()), isX86(triple.getArch() == llvm::Triple::x86) + { + } + + std::string mangle(const Declaration &declaration, const CppMangleContext &context) + { + out = "?"; + qualifiedName(declaration.name); + + if (context.isInstanceMethod) + { + // public, not virtual + out += 'Q'; + if (is64) + { + out += 'E'; + } + + out += qualifiers(declaration.isConst, declaration.isVolatile); + // __thiscall on x86, __cdecl for a variadic method there and everywhere else + out += isX86 && !declaration.isVariadic ? 'E' : 'A'; + } + else + { + // public static, or a free function; __cdecl + out += context.isStaticMethod ? "SA" : "YA"; + } + + resultType(*declaration.result); + + for (auto ¶m : declaration.params) + { + paramType(*param); + } + + if (declaration.params.empty() && !declaration.isVariadic) + { + out += 'X'; + } + else + { + out += declaration.isVariadic ? 'Z' : '@'; + } + + // no exception specification + out += 'Z'; + return out; + } +}; + +// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling, and clang's ItaniumMangle.cpp +class ItaniumMangler +{ + std::string out; + // the substitution candidates seen so far, by key: a scope's or a record's qualified name, or a + // qualified, pointer or reference type in parentheses + llvm::SmallVector substitutions; + + bool substitute(llvm::StringRef key) + { + auto it = llvm::find(substitutions, key); + if (it == substitutions.end()) + { + return false; + } + + // S_, S0_, S1_, ... S9_, SA_, ... SZ_, S10_, ... + auto index = it - substitutions.begin(); + out += 'S'; + if (index > 0) + { + std::string digits; + for (auto value = index - 1;; value /= 36) + { + auto digit = value % 36; + digits.insert(digits.begin(), static_cast(digit < 10 ? '0' + digit : 'A' + digit - 10)); + if (value < 36) + { + break; + } + } + + out += digits; + } + + out += '_'; + return true; + } + + void sourceName(llvm::StringRef name) + { + out += std::to_string(name.size()); + out += name; + } + + // the scopes of a nested name: each one a candidate + void prefix(llvm::ArrayRef name) + { + if (name.empty() || substitute(nameKey(name))) + { + return; + } + + prefix(name.drop_back()); + sourceName(name.back()); + substitutions.push_back(nameKey(name)); + } + + void recordName(llvm::ArrayRef name) + { + if (substitute(nameKey(name))) + { + return; + } + + if (name.size() == 1) + { + sourceName(name.front()); + } + else + { + out += 'N'; + prefix(name.drop_back()); + sourceName(name.back()); + out += 'E'; + } + + substitutions.push_back(nameKey(name)); + } + + void builtin(Builtin builtin) + { + switch (builtin) + { + case Builtin::Void: out += "v"; break; + case Builtin::Bool: out += "b"; break; + case Builtin::Char: out += "c"; break; + case Builtin::SChar: out += "a"; break; + case Builtin::UChar: out += "h"; break; + case Builtin::Short: out += "s"; break; + case Builtin::UShort: out += "t"; break; + case Builtin::Int: out += "i"; break; + case Builtin::UInt: out += "j"; break; + case Builtin::Long: out += "l"; break; + case Builtin::ULong: out += "m"; break; + case Builtin::LongLong: out += "x"; break; + case Builtin::ULongLong: out += "y"; break; + case Builtin::Float: out += "f"; break; + case Builtin::Double: out += "d"; break; + case Builtin::LongDouble: out += "e"; break; + case Builtin::WChar: out += "w"; break; + case Builtin::Char8: out += "Du"; break; + case Builtin::Char16: out += "Ds"; break; + case Builtin::Char32: out += "Di"; break; + } + } + + void unqualifiedType(const Type &type) + { + switch (type.kind) + { + case Type::Kind::Builtin: + builtin(type.builtin); + return; + case Type::Kind::Named: + recordName(type.name); + return; + default: + break; + } + + Type unqualified = type; + unqualified.isConst = unqualified.isVolatile = false; + auto key = typeKey(unqualified); + if (substitute(key)) + { + return; + } + + out += type.kind == Type::Kind::Pointer ? 'P' : type.kind == Type::Kind::LValueReference ? 'R' : 'O'; + this->type(*type.pointee); + substitutions.push_back(key); + } + + void type(const Type &type) + { + if (!type.isConst && !type.isVolatile) + { + unqualifiedType(type); + return; + } + + auto key = typeKey(type); + if (substitute(key)) + { + return; + } + + out += type.isVolatile ? "V" : ""; + out += type.isConst ? "K" : ""; + unqualifiedType(type); + substitutions.push_back(key); + } + +public: + std::string mangle(const Declaration &declaration) + { + out = "_Z"; + if (declaration.name.size() == 1) + { + sourceName(declaration.name.front()); + } + else + { + out += 'N'; + out += declaration.isVolatile ? "V" : ""; + out += declaration.isConst ? "K" : ""; + prefix(llvm::ArrayRef(declaration.name).drop_back()); + sourceName(declaration.name.back()); + out += 'E'; + } + + // the result type isn't part of a function's symbol; a parameter's own const isn't either + for (auto ¶m : declaration.params) + { + auto unqualified = *param; + unqualified.isConst = unqualified.isVolatile = false; + type(unqualified); + } + + if (declaration.isVariadic) + { + out += 'z'; + } + else if (declaration.params.empty()) + { + out += 'v'; + } + + return out; + } +}; + +} // namespace + +bool mangleCppName(llvm::StringRef declarationText, const llvm::Triple &triple, const CppMangleContext &context, + std::string &symbol, std::string &error) +{ + error.clear(); + Declaration declaration; + Parser parser(declarationText, triple, error); + if (!parser.parse(declaration)) + { + return false; + } + + if (declaration.name.size() == 1 && !context.className.empty()) + { + declaration.name.insert(declaration.name.begin(), context.className.str()); + } + + if (declaration.name.size() >= 2 && declaration.name.back() == declaration.name[declaration.name.size() - 2]) + { + error = "constructors aren't supported"; + return false; + } + + if ((declaration.isConst || declaration.isVolatile) && !context.isInstanceMethod) + { + error = "only a method with 'this' can be const or volatile"; + return false; + } + + // MSVC on Windows unless the environment says GNU (MinGW) or Cygwin, as clang decides it + if (triple.isWindowsMSVCEnvironment()) + { + symbol = MicrosoftMangler(triple).mangle(declaration, context); + } + else + { + symbol = ItaniumMangler().mangle(declaration); + } + + return true; +} diff --git a/tslang/lib/TypeScript/MLIRGenClasses.cpp b/tslang/lib/TypeScript/MLIRGenClasses.cpp index d0cd32fc2..7266ff6ff 100644 --- a/tslang/lib/TypeScript/MLIRGenClasses.cpp +++ b/tslang/lib/TypeScript/MLIRGenClasses.cpp @@ -884,14 +884,15 @@ namespace mlirgen { auto hasLinkName = false; iterateDecorators(classMember, genContext, [&](StringRef name, SmallVector args) { - hasLinkName |= name == DLL_NAME && !args.empty(); + // a @cppname that doesn't mangle is reported where the method is made + hasLinkName |= (name == DLL_NAME || name == CPP_NAME) && !args.empty(); }); if (!hasLinkName) { error(memberLocation) << "'" << cppOverloadedName(methodName) << "' has no @" CPP_SLOT ", so it isn't virtual: give a " << "C++ virtual method its slot, @" CPP_SLOT "(N), or a non-virtual one its " - << "symbol, @" LINK_NAME "(\"...\")"; + << "symbol, @" LINK_NAME "(\"...\"), or its C++ declaration, @" CPP_NAME "(\"...\")"; } } } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index d15a4bb01..b307939f1 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -15,6 +15,7 @@ #include "TypeScript/TypeScriptDialect.h" #include "TypeScript/TypeScriptOps.h" #include "TypeScript/DiagnosticHelper.h" +#include "TypeScript/CppMangler.h" #include "TypeScript/MLIRLogic/MLIRCodeLogic.h" #include "TypeScript/MLIRLogic/MLIRGenContext.h" @@ -14473,6 +14474,24 @@ class MLIRGenImpl name = DLL_NAME; } + // @cppname is @linkname under the symbol the target's C++ compiler gives the + // declaration; one that doesn't mangle stays @cppname, carrying the reason, for + // checkLinkNameDecorators to report + if (name == CPP_NAME) + { + std::string symbol; + std::string error = "expects a C++ declaration, @" CPP_NAME "(\"int f(int)\")"; + if (!args.empty() && mangleCppNameOf(node, args.front(), symbol, error)) + { + name = DLL_NAME; + args = {StringRef(symbol).copy(stringAllocator)}; + } + else + { + args = {StringRef(error).copy(stringAllocator)}; + } + } + functor(name, args); } } @@ -14486,7 +14505,14 @@ class MLIRGenImpl { StringRef first; StringRef second; + StringRef cppNameError; iterateDecorators(node, genContext, [&](StringRef name, SmallVector args) { + if (name == CPP_NAME) + { + cppNameError = args.front(); + return; + } + if (name != DLL_NAME || args.empty()) { return; @@ -14502,6 +14528,12 @@ class MLIRGenImpl } }); + if (!cppNameError.empty()) + { + emitError(location) << "@" CPP_NAME ": " << cppNameError; + return mlir::failure(); + } + if (!second.empty()) { emitError(location) << "conflicting @dllname/@linkname: '" << first << "' vs '" << second << "'"; @@ -14511,6 +14543,30 @@ class MLIRGenImpl return mlir::success(); } + // @cppname on a function binds a free C++ function; on a method, a member of the class, which an + // unqualified name is in + bool mangleCppNameOf(Node node, StringRef declaration, std::string &symbol, std::string &error) + { + CppMangleContext context; + if (node == SyntaxKind::MethodDeclaration) + { + context.isStaticMethod = hasModifier(node, SyntaxKind::StaticKeyword); + context.isInstanceMethod = !context.isStaticMethod; + auto parent = node->parent; + if (parent == SyntaxKind::ClassDeclaration || parent == SyntaxKind::ClassExpression) + { + context.className = MLIRHelper::getName(parent.as()->name, stringAllocator); + } + } + else if (node != SyntaxKind::FunctionDeclaration) + { + error = "is for a function or a method"; + return false; + } + + return mangleCppName(declaration, llvm::Triple(compileOptions.moduleTargetTriple), context, symbol, error); + } + bool isAddedToExport(mlir::Type type) { if (stage != Stages::SourceGeneration) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 7ee8c3649..8a29e3fcd 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -3532,6 +3532,27 @@ foreach(cppvtable_itanium_triple x86_64-unknown-linux-gnu aarch64-unknown-linux- FAIL_REGULAR_EXPRESSION "error:|Stack dump|Assertion failed") endforeach() +# @cppname: one C++ declaration, the symbol of each target's C++ compiler (cppvtable/cppname.ts) +set(cppname_symbols_x86_64-pc-windows-msvc + "\\?scaled@Methods@@QEAAHH@Z" "\\?scaled@Methods@@QEAAHN@Z" "\\?get@Methods@@QEBAHXZ" + "\\?total@Methods@@QEAA_J_J@Z" "\\?sum@cppname@@YAHHH@Z") +set(cppname_symbols_x86_64-unknown-linux-gnu + "_ZN7Methods6scaledEi" "_ZN7Methods6scaledEd" "_ZNK7Methods3getEv" "_ZN7Methods5totalEl" "_ZN7cppname3sumEii") +set(cppname_symbols_x86_64-pc-windows-gnu + "_ZN7Methods6scaledEi" "_ZN7Methods6scaledEd" "_ZNK7Methods3getEv" "_ZN7Methods5totalEx" "_ZN7cppname3sumEii") +foreach(cppname_triple x86_64-pc-windows-msvc x86_64-unknown-linux-gnu x86_64-pc-windows-gnu) + set(cppname_index 0) + foreach(cppname_symbol ${cppname_symbols_${cppname_triple}}) + add_test(NAME test-compile-cppname-${cppname_triple}-${cppname_index} + COMMAND $ --emit=llvm --no-default-lib -mm=none -mtriple=${cppname_triple} + "${PROJECT_SOURCE_DIR}/test/tester/cppvtable/cppname.ts" -o -) + set_tests_properties(test-compile-cppname-${cppname_triple}-${cppname_index} + PROPERTIES PASS_REGULAR_EXPRESSION "declare [^\n]*@\"?${cppname_symbol}\"?\\(" + FAIL_REGULAR_EXPRESSION "error:|Stack dump|Assertion failed") + math(EXPR cppname_index "${cppname_index} + 1") + endforeach() +endforeach() + # Records passed by value as each ABI passes them (abi_host.cpp's byValue: 4, 8, 12, 16, 24 bytes, # then an int), on any host: MSVC x64 integers of 4 and 8 bytes, then pointers to copies; SysV the # 4- and 8-byte ones in a register each, the 12-byte one in two, the 16- and 24-byte ones on the @@ -4578,7 +4599,10 @@ set(linkname_cases "type-mismatch|binds symbol 'strlen' with type" "two-definitions|both define symbol 'linkname_dup'" "not-a-function|binds symbol 'linkname_counter', which is not a function" - "chain|'a' binds symbol 'linkname_chain_b', which @dllname/@linkname renames to 'linkname_chain_c'") + "chain|'a' binds symbol 'linkname_chain_b', which @dllname/@linkname renames to 'linkname_chain_c'" + "cppname-unparsable|@cppname: expected '\\)' at the end" + "cppname-variable|@cppname: is for a function or a method" + "cppname-const-function|@cppname: only a method with 'this' can be const or volatile") foreach(linkname_case ${linkname_cases}) string(FIND "${linkname_case}" "|" linkname_sep) string(SUBSTRING "${linkname_case}" 0 ${linkname_sep} linkname_name) diff --git a/tslang/test/tester/cppvtable/abi_itanium.ts b/tslang/test/tester/cppvtable/abi_itanium.ts index 445e95f23..2e466d618 100644 --- a/tslang/test/tester/cppvtable/abi_itanium.ts +++ b/tslang/test/tester/cppvtable/abi_itanium.ts @@ -40,10 +40,11 @@ class Methods { @slot(11) pair(d: S16, tail: s32): u64; @slot(12) flags(x: s32): Flags; @slot(13) r12(x: s32): R12; - // a C++ non-virtual method (no @slot), called by its symbol - @linkname("_ZN7Methods6scaledEi") + // a C++ non-virtual method (no @slot), called by its symbol: the symbols of their C++ + // declarations (_ZN7Methods6scaledEi, _ZN7Methods6scaledEd) + @cppname("int scaled(int)") scaled(x: s32): s32; - @linkname("_ZN7Methods6scaledEd") + @cppname("int scaled(double)") scaled(x: f64): s32; } diff --git a/tslang/test/tester/cppvtable/abi_msvc.ts b/tslang/test/tester/cppvtable/abi_msvc.ts index 900821cfe..ac396c2b4 100644 --- a/tslang/test/tester/cppvtable/abi_msvc.ts +++ b/tslang/test/tester/cppvtable/abi_msvc.ts @@ -38,10 +38,11 @@ class Methods { @slot(10) pair(d: S16, tail: s32): u64; @slot(11) flags(x: s32): Flags; @slot(12) r12(x: s32): R12; - // a C++ non-virtual method (no @slot), called by its symbol, and its overload - @linkname("?scaled@Methods@@QEAAHH@Z") + // a C++ non-virtual method (no @slot), called by its symbol, and its overload: the symbols of + // their C++ declarations (?scaled@Methods@@QEAAHH@Z, ?scaled@Methods@@QEAAHN@Z) + @cppname("int scaled(int)") scaled(x: s32): s32; - @linkname("?scaled@Methods@@QEAAHN@Z") + @cppname("int scaled(double)") scaled(x: f64): s32; } diff --git a/tslang/test/tester/cppvtable/cppname.ts b/tslang/test/tester/cppvtable/cppname.ts new file mode 100644 index 000000000..38aa5e595 --- /dev/null +++ b/tslang/test/tester/cppvtable/cppname.ts @@ -0,0 +1,24 @@ +// @cppname: a C++ declaration, bound by the symbol each target's C++ compiler gives it - one class +// for MSVC (Windows) and Itanium (Linux, Android, MinGW), where @linkname needs each one spelled out. +// `int64_t` is `long` on 64-bit Linux and `long long` on Windows, and mangles as each. +declare function cppname_object(): Opaque; + +@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("int64_t total(int64_t value)") + total(x: s64): s64; +} + +@cppname("int cppname::sum(int, int)") +declare function sum(a: s32, b: s32): s32; + +function main() { + const methods = cppname_object(); + print(methods.scaled(1), methods.scaled(2.5), methods.get(), methods.total(3), sum(5, 6)); +} diff --git a/tslang/test/tester/linkname/cppname-const-function.ts b/tslang/test/tester/linkname/cppname-const-function.ts new file mode 100644 index 000000000..ed9bbe120 --- /dev/null +++ b/tslang/test/tester/linkname/cppname-const-function.ts @@ -0,0 +1,6 @@ +@cppname("int cppname_get() const") +declare function get(): s32; + +function main() { + print(get()); +} diff --git a/tslang/test/tester/linkname/cppname-unparsable.ts b/tslang/test/tester/linkname/cppname-unparsable.ts new file mode 100644 index 000000000..12ded1a9b --- /dev/null +++ b/tslang/test/tester/linkname/cppname-unparsable.ts @@ -0,0 +1,6 @@ +@cppname("int cppname_f(int") +declare function f(x: s32): s32; + +function main() { + print(f(1)); +} diff --git a/tslang/test/tester/linkname/cppname-variable.ts b/tslang/test/tester/linkname/cppname-variable.ts new file mode 100644 index 000000000..ed4dc41dc --- /dev/null +++ b/tslang/test/tester/linkname/cppname-variable.ts @@ -0,0 +1,6 @@ +@cppname("int cppname_counter") +declare let counter: s32; + +function main() { + print(counter); +} diff --git a/tslang/unittests/MLIRGen/CMakeLists.txt b/tslang/unittests/MLIRGen/CMakeLists.txt index 4757d9ba1..136165274 100644 --- a/tslang/unittests/MLIRGen/CMakeLists.txt +++ b/tslang/unittests/MLIRGen/CMakeLists.txt @@ -5,6 +5,7 @@ add_mlir_unittest(MLIRGenTests TypeHelper.cpp TargetInfo.cpp LoweringDataLayout.cpp + CppMangler.cpp ) get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) diff --git a/tslang/unittests/MLIRGen/CppMangler.cpp b/tslang/unittests/MLIRGen/CppMangler.cpp new file mode 100644 index 000000000..8b2a82178 --- /dev/null +++ b/tslang/unittests/MLIRGen/CppMangler.cpp @@ -0,0 +1,262 @@ +#include "TypeScript/CppMangler.h" + +#include "llvm/TargetParser/Triple.h" + +#include "gmock/gmock.h" + +// @cppname's symbols, against the ones clang gives the same declarations +// (clang++ -target -S -emit-llvm): MSVC's scheme on windows-msvc, Itanium's elsewhere, +// and the typedefs as each target defines them. +namespace +{ + +enum Kind +{ + Free, + Method, + Static +}; + +struct Case +{ + const char *declaration; + Kind kind; +}; + +std::string mangle(const char *triple, const char *declaration, Kind kind = Free, llvm::StringRef className = {}) +{ + CppMangleContext context; + context.className = className; + context.isInstanceMethod = kind == Method; + context.isStaticMethod = kind == Static; + std::string symbol, error; + if (!mangleCppName(declaration, llvm::Triple(llvm::Triple::normalize(triple)), context, symbol, error)) + { + return "error: " + error; + } + + return symbol; +} + +const Case cases[] = { + {"int Methods::scaled(int)", Method}, + {"int Methods::scaled(double)", Method}, + {"Pair Methods::pair()", Method}, + {"int Methods::get() const", Method}, + {"int Methods::stat(int, int)", Static}, + {"void Methods::two(Pair*, Pair*)", Method}, + {"void Methods::refs(const Pair&, Pair&&, const Pair&)", Method}, + {"void Methods::ptrs(int*, const int*, int* const, const char*, void*)", Method}, + {"void Methods::self(Methods*, const Methods&)", Method}, + {"void Methods::all(bool, char, signed char, unsigned char, short, unsigned short, int, unsigned, long, unsigned long, " + "long long, unsigned long long, float, double)", + Method}, + {"void Methods::wide(wchar_t, char16_t, char32_t)", Method}, + {"void Methods::std(int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, size_t, ptrdiff_t, " + "intptr_t, uintptr_t)", + Method}, + {"void Methods::kinds(class Obj*, union U*, enum E, ns::Pt*, ns::in::Q*, ns::Pt*)", Method}, + {"void Methods::pp(int**, const char**, char* const*)", Method}, + {"void Methods::vol(volatile int*, const volatile int*)", Method}, + {"void ns::free(int)", Free}, + {"void ns::Pt::m(ns::Pt*, ns::in::Q*)", Method}, + {"int ns::in::deep(ns::Pt*, ns::in::Q*)", Free}, + {"void plain()", Free}, + {"void varf(int, ...)", Free}, + {"long long ret()", Free}, + {"const char* rets(int)", Free}, + {"void rep(const int*, const int*, double, double)", Free}, + {"Pair* retp(Pair)", Free}, + {"void many(Pair*, class Obj*, union U*, ns::Pt*, Methods*, const Pair*, const class Obj*, const union U*, " + "const ns::Pt*, const Methods*, Pair*, Methods&, class Obj&)", + Free}, + {"Pair* Methods::back(Pair*)", Method}, + {"long long* Methods::lb(long long*)", Method}, + {"int Methods::vf(int, ...)", Method}, + {"void rr(Pair&&, Pair&&)", Free}, + {"const char* const* cc(const char* const*)", Free}, +}; + +const char *const msvcX64[] = { + "?scaled@Methods@@QEAAHH@Z", + "?scaled@Methods@@QEAAHN@Z", + "?pair@Methods@@QEAA?AUPair@@XZ", + "?get@Methods@@QEBAHXZ", + "?stat@Methods@@SAHHH@Z", + "?two@Methods@@QEAAXPEAUPair@@0@Z", + "?refs@Methods@@QEAAXAEBUPair@@$$QEAU2@0@Z", + "?ptrs@Methods@@QEAAXPEAHPEBHQEAHPEBDPEAX@Z", + "?self@Methods@@QEAAXPEAU1@AEBU1@@Z", + "?all@Methods@@QEAAX_NDCEFGHIJK_J_KMN@Z", + "?wide@Methods@@QEAAX_W_S_U@Z", + "?std@Methods@@QEAAXCEFGHI_J_K1001@Z", + "?kinds@Methods@@QEAAXPEAVObj@@PEATU@@W4E@@PEAUPt@ns@@PEAUQ@in@6@3@Z", + "?pp@Methods@@QEAAXPEAPEAHPEAPEBDPEBQEAD@Z", + "?vol@Methods@@QEAAXPECHPEDH@Z", + "?free@ns@@YAXH@Z", + "?m@Pt@ns@@QEAAXPEAU12@PEAUQ@in@2@@Z", + "?deep@in@ns@@YAHPEAUPt@2@PEAUQ@12@@Z", + "?plain@@YAXXZ", + "?varf@@YAXHZZ", + "?ret@@YA_JXZ", + "?rets@@YAPEBDH@Z", + "?rep@@YAXPEBH0NN@Z", + "?retp@@YAPEAUPair@@U1@@Z", + "?many@@YAXPEAUPair@@PEAVObj@@PEATU@@PEAUPt@ns@@PEAUMethods@@PEBU1@PEBV2@PEBT3@PEBU45@PEBU6@0AEAU6@AEAV2@@Z", + "?back@Methods@@QEAAPEAUPair@@PEAU2@@Z", + "?lb@Methods@@QEAAPEA_JPEA_J@Z", + "?vf@Methods@@QEAAHHZZ", + "?rr@@YAX$$QEAUPair@@0@Z", + "?cc@@YAPEBQEBDPEBQEBD@Z", +}; + +const char *const msvcX86[] = { + "?scaled@Methods@@QAEHH@Z", + "?scaled@Methods@@QAEHN@Z", + "?pair@Methods@@QAE?AUPair@@XZ", + "?get@Methods@@QBEHXZ", + "?stat@Methods@@SAHHH@Z", + "?two@Methods@@QAEXPAUPair@@0@Z", + "?refs@Methods@@QAEXABUPair@@$$QAU2@0@Z", + "?ptrs@Methods@@QAEXPAHPBHQAHPBDPAX@Z", + "?self@Methods@@QAEXPAU1@ABU1@@Z", + "?all@Methods@@QAEX_NDCEFGHIJK_J_KMN@Z", + "?wide@Methods@@QAEX_W_S_U@Z", + "?std@Methods@@QAEXCEFGHI_J_KIHHI@Z", + "?kinds@Methods@@QAEXPAVObj@@PATU@@W4E@@PAUPt@ns@@PAUQ@in@6@3@Z", + "?pp@Methods@@QAEXPAPAHPAPBDPBQAD@Z", + "?vol@Methods@@QAEXPCHPDH@Z", + "?free@ns@@YAXH@Z", + "?m@Pt@ns@@QAEXPAU12@PAUQ@in@2@@Z", + "?deep@in@ns@@YAHPAUPt@2@PAUQ@12@@Z", + "?plain@@YAXXZ", + "?varf@@YAXHZZ", + "?ret@@YA_JXZ", + "?rets@@YAPBDH@Z", + "?rep@@YAXPBH0NN@Z", + "?retp@@YAPAUPair@@U1@@Z", + "?many@@YAXPAUPair@@PAVObj@@PATU@@PAUPt@ns@@PAUMethods@@PBU1@PBV2@PBT3@PBU45@PBU6@0AAU6@AAV2@@Z", + "?back@Methods@@QAEPAUPair@@PAU2@@Z", + "?lb@Methods@@QAEPA_JPA_J@Z", + "?vf@Methods@@QAAHHZZ", + "?rr@@YAX$$QAUPair@@0@Z", + "?cc@@YAPBQBDPBQBD@Z", +}; + +// Itanium, with the typedefs of 64-bit Linux in `std` +const char *const itaniumLinux64[] = { + "_ZN7Methods6scaledEi", + "_ZN7Methods6scaledEd", + "_ZN7Methods4pairEv", + "_ZNK7Methods3getEv", + "_ZN7Methods4statEii", + "_ZN7Methods3twoEP4PairS1_", + "_ZN7Methods4refsERK4PairOS0_S2_", + "_ZN7Methods4ptrsEPiPKiS0_PKcPv", + "_ZN7Methods4selfEPS_RKS_", + "_ZN7Methods3allEbcahstijlmxyfd", + "_ZN7Methods4wideEwDsDi", + "_ZN7Methods3stdEahstijlmmllm", + "_ZN7Methods5kindsEP3ObjP1U1EPN2ns2PtEPNS5_2in1QES7_", + "_ZN7Methods2ppEPPiPPKcPKPc", + "_ZN7Methods3volEPViPVKi", + "_ZN2ns4freeEi", + "_ZN2ns2Pt1mEPS0_PNS_2in1QE", + "_ZN2ns2in4deepEPNS_2PtEPNS0_1QE", + "_Z5plainv", + "_Z4varfiz", + "_Z3retv", + "_Z4retsi", + "_Z3repPKiS0_dd", + "_Z4retp4Pair", + "_Z4manyP4PairP3ObjP1UPN2ns2PtEP7MethodsPKS_PKS1_PKS3_PKS6_PKS8_S0_RS8_RS1_", + "_ZN7Methods4backEP4Pair", + "_ZN7Methods2lbEPx", + "_ZN7Methods2vfEiz", + "_Z2rrO4PairS0_", + "_Z2ccPKPKc", +}; + +const size_t stdCase = 11; + +void expectAll(const char *triple, const char *const (&expected)[std::size(cases)], const char *stdSymbol = nullptr) +{ + for (size_t i = 0; i < std::size(cases); ++i) + { + auto want = i == stdCase && stdSymbol ? stdSymbol : expected[i]; + EXPECT_EQ(mangle(triple, cases[i].declaration, cases[i].kind), want) << triple << ": " << cases[i].declaration; + } +} + +TEST(CppMangler, MsvcX64) +{ + expectAll("x86_64-pc-windows-msvc", msvcX64); +} + +TEST(CppMangler, MsvcArm64IsMsvcX64) +{ + expectAll("aarch64-pc-windows-msvc", msvcX64); +} + +TEST(CppMangler, MsvcX86) +{ + expectAll("i686-pc-windows-msvc", msvcX86); +} + +TEST(CppMangler, ItaniumLinux64) +{ + expectAll("x86_64-unknown-linux-gnu", itaniumLinux64); + expectAll("aarch64-unknown-linux-gnu", itaniumLinux64); + expectAll("aarch64-unknown-linux-android29", itaniumLinux64); +} + +// the same scheme; only the typedefs differ +TEST(CppMangler, ItaniumTypedefsByTarget) +{ + expectAll("x86_64-pc-windows-gnu", itaniumLinux64, "_ZN7Methods3stdEahstijxyyxxy"); + expectAll("i686-unknown-linux-gnu", itaniumLinux64, "_ZN7Methods3stdEahstijxyjiij"); + expectAll("x86_64-apple-darwin", itaniumLinux64, "_ZN7Methods3stdEahstijxymllm"); + expectAll("arm64-apple-darwin", itaniumLinux64, "_ZN7Methods3stdEahstijxymllm"); + + const char *td = "void td(int64_t, size_t, ptrdiff_t, intptr_t, long)"; + EXPECT_EQ(mangle("wasm32-unknown-unknown", td), "_Z2tdxmlll"); + EXPECT_EQ(mangle("armv7-unknown-linux-gnueabihf", td), "_Z2tdxjiil"); + EXPECT_EQ(mangle("i686-apple-darwin", td), "_Z2tdxmill"); + EXPECT_EQ(mangle("x86_64-unknown-linux-gnu", td), "_Z2tdlmlll"); + EXPECT_EQ(mangle("x86_64-pc-windows-msvc", td), "?td@@YAX_J_K00J@Z"); + EXPECT_EQ(mangle("i686-pc-windows-msvc", td), "?td@@YAX_JIHHJ@Z"); +} + +// in a @cpp class an unqualified name is the class's method; parameter names and `std::` are +// what a C++ header has +TEST(CppMangler, ClassContextAndHeaderSpelling) +{ + EXPECT_EQ(mangle("x86_64-pc-windows-msvc", "int scaled(int x)", Method, "Methods"), "?scaled@Methods@@QEAAHH@Z"); + EXPECT_EQ(mangle("x86_64-unknown-linux-gnu", "int scaled(int x)", Method, "Methods"), "_ZN7Methods6scaledEi"); + EXPECT_EQ(mangle("x86_64-pc-windows-msvc", "struct Pair pair();", Method, "Base"), "?pair@Base@@QEAA?AUPair@@XZ"); + EXPECT_EQ(mangle("x86_64-pc-windows-msvc", "int stat(int, int)", Static, "Methods"), "?stat@Methods@@SAHHH@Z"); + EXPECT_EQ(mangle("x86_64-unknown-linux-gnu", "void f(std::size_t n, unsigned long int m, long double d)"), "_Z1fmme"); + EXPECT_EQ(mangle("x86_64-unknown-linux-gnu", "void f(void)"), "_Z1fv"); + EXPECT_EQ(mangle("x86_64-unknown-linux-gnu", "void f(...)"), "_Z1fz"); + EXPECT_EQ(mangle("x86_64-pc-windows-msvc", "void f(...)"), "?f@@YAXZZ"); +} + +TEST(CppMangler, Errors) +{ + auto msvc = "x86_64-pc-windows-msvc"; + EXPECT_EQ(mangle(msvc, "int f(int"), "error: expected ')' at the end"); + EXPECT_EQ(mangle(msvc, "int f(int) const"), "error: only a method with 'this' can be const or volatile"); + EXPECT_EQ(mangle(msvc, "void f(std::vector)"), "error: templates aren't supported at '<'"); + EXPECT_EQ(mangle(msvc, "int operator+(int)"), "error: operators, constructors and destructors aren't supported at 'operator'"); + EXPECT_EQ(mangle(msvc, "void ~A()"), "error: operators, constructors and destructors aren't supported at '~'"); + EXPECT_EQ(mangle(msvc, "void A::A()", Method), "error: constructors aren't supported"); + EXPECT_EQ(mangle(msvc, "virtual int f()", Method), "error: a virtual method is called through its vtable slot, @slot(N), not by its symbol at 'virtual'"); + EXPECT_EQ(mangle(msvc, "void f(int (*)(int))"), "error: function pointers and arrays aren't supported at '('"); + EXPECT_EQ(mangle(msvc, "void f(int = 0)"), "error: default arguments aren't supported at '='"); + EXPECT_EQ(mangle(msvc, "void f(int, void)"), "error: a parameter can't be void at ')'"); + EXPECT_EQ(mangle(msvc, "unsigned double f()"), "error: invalid combination of type specifiers at 'f'"); + EXPECT_EQ(mangle(msvc, "int f() noexcept"), "error: ref-qualifiers and exception specifications aren't supported at 'noexcept'"); + EXPECT_EQ(mangle(msvc, "f()"), "error: expected a name at '('"); +} + +} // namespace From ea3e07ee596378f9643785f7743f5b72a65802b8 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 25 Oct 2026 19:16:58 +0000 Subject: [PATCH 2/2] @cppname: MSVC const builtin results; a method of an unknown class is 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 --- tslang/lib/TypeScript/CppMangler.cpp | 10 ++++++++-- tslang/test/tester/CMakeLists.txt | 3 ++- .../tester/linkname/cppname-method-unparsable.ts | 10 ++++++++++ tslang/unittests/MLIRGen/CppMangler.cpp | 12 ++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tslang/test/tester/linkname/cppname-method-unparsable.ts diff --git a/tslang/lib/TypeScript/CppMangler.cpp b/tslang/lib/TypeScript/CppMangler.cpp index 884d3c701..86cb87040 100644 --- a/tslang/lib/TypeScript/CppMangler.cpp +++ b/tslang/lib/TypeScript/CppMangler.cpp @@ -725,9 +725,9 @@ class MicrosoftMangler void resultType(const Type &type) { - if (type.kind == Type::Kind::Named) + if (type.kind == Type::Kind::Named || (type.kind == Type::Kind::Builtin && (type.isConst || type.isVolatile))) { - // a record or an enum returned by value + // a record or an enum returned by value, or a const builtin: `?AUPair@@`, `?BH` out += '?'; out += qualifiers(type.isConst, type.isVolatile); } @@ -1022,6 +1022,12 @@ bool mangleCppName(llvm::StringRef declarationText, const llvm::Triple &triple, { declaration.name.insert(declaration.name.begin(), context.className.str()); } + else if (declaration.name.size() == 1 && (context.isInstanceMethod || context.isStaticMethod)) + { + // it would be a free function's symbol + error = "the method's class isn't known: qualify the name, Class::" + declaration.name.front(); + return false; + } if (declaration.name.size() >= 2 && declaration.name.back() == declaration.name[declaration.name.size() - 2]) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 8a29e3fcd..5e51a33f9 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -4602,7 +4602,8 @@ set(linkname_cases "chain|'a' binds symbol 'linkname_chain_b', which @dllname/@linkname renames to 'linkname_chain_c'" "cppname-unparsable|@cppname: expected '\\)' at the end" "cppname-variable|@cppname: is for a function or a method" - "cppname-const-function|@cppname: only a method with 'this' can be const or volatile") + "cppname-const-function|@cppname: only a method with 'this' can be const or volatile" + "cppname-method-unparsable|cppname-method-unparsable\\.ts:5:5: error: @cppname: expected '\\)' at the end") foreach(linkname_case ${linkname_cases}) string(FIND "${linkname_case}" "|" linkname_sep) string(SUBSTRING "${linkname_case}" 0 ${linkname_sep} linkname_name) diff --git a/tslang/test/tester/linkname/cppname-method-unparsable.ts b/tslang/test/tester/linkname/cppname-method-unparsable.ts new file mode 100644 index 000000000..565a25e48 --- /dev/null +++ b/tslang/test/tester/linkname/cppname-method-unparsable.ts @@ -0,0 +1,10 @@ +// a @cpp class's method whose @cppname doesn't parse: reported, not taken for a method without +// a symbol +@cpp +class Methods { + @cppname("int scaled(int") + scaled(x: s32): s32; +} + +function main() { +} diff --git a/tslang/unittests/MLIRGen/CppMangler.cpp b/tslang/unittests/MLIRGen/CppMangler.cpp index 8b2a82178..ff5f5ba3a 100644 --- a/tslang/unittests/MLIRGen/CppMangler.cpp +++ b/tslang/unittests/MLIRGen/CppMangler.cpp @@ -241,6 +241,17 @@ TEST(CppMangler, ClassContextAndHeaderSpelling) EXPECT_EQ(mangle("x86_64-pc-windows-msvc", "void f(...)"), "?f@@YAXZZ"); } +// MSVC encodes a returned value's const, a builtin's as well as a record's; Itanium has no result +TEST(CppMangler, QualifiedResult) +{ + auto msvc = "x86_64-pc-windows-msvc"; + EXPECT_EQ(mangle(msvc, "const int f()"), "?f@@YA?BHXZ"); + EXPECT_EQ(mangle(msvc, "const struct P g()"), "?g@@YA?BUP@@XZ"); + EXPECT_EQ(mangle(msvc, "volatile int h()"), "?h@@YA?CHXZ"); + EXPECT_EQ(mangle(msvc, "const int* const k()"), "?k@@YAQEBHXZ"); + EXPECT_EQ(mangle("x86_64-unknown-linux-gnu", "const int f()"), "_Z1fv"); +} + TEST(CppMangler, Errors) { auto msvc = "x86_64-pc-windows-msvc"; @@ -250,6 +261,7 @@ TEST(CppMangler, Errors) EXPECT_EQ(mangle(msvc, "int operator+(int)"), "error: operators, constructors and destructors aren't supported at 'operator'"); EXPECT_EQ(mangle(msvc, "void ~A()"), "error: operators, constructors and destructors aren't supported at '~'"); EXPECT_EQ(mangle(msvc, "void A::A()", Method), "error: constructors aren't supported"); + EXPECT_EQ(mangle(msvc, "int f()", Method), "error: the method's class isn't known: qualify the name, Class::f"); EXPECT_EQ(mangle(msvc, "virtual int f()", Method), "error: a virtual method is called through its vtable slot, @slot(N), not by its symbol at 'virtual'"); EXPECT_EQ(mangle(msvc, "void f(int (*)(int))"), "error: function pointers and arrays aren't supported at '('"); EXPECT_EQ(mangle(msvc, "void f(int = 0)"), "error: default arguments aren't supported at '='");