Skip to content

Commit 505326e

Browse files
committed
Prefer mangled name over printer
1 parent ed856e0 commit 505326e

3 files changed

Lines changed: 42 additions & 19 deletions

File tree

‎cpp2rust/converter/converter.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
3535
const char *keyword_mut = keyword::kMut)
3636
: rs_code_(&rs_code), ctx_(ctx), keyword_unsafe_(keyword_unsafe),
3737
keyword_mut_(keyword_mut) {
38-
if (!GetNameGenerator()) {
39-
name_generator_ = std::make_unique<clang::ASTNameGenerator>(ctx);
40-
SetNameGenerator(name_generator_.get());
38+
if (!GetMangleContext()) {
39+
mangle_context_.reset(ctx.createMangleContext());
40+
SetMangleContext(mangle_context_.get());
4141
}
4242
}
4343

4444
virtual ~Converter() {
45-
if (name_generator_) {
46-
SetNameGenerator(nullptr);
45+
if (mangle_context_) {
46+
SetMangleContext(nullptr);
4747
}
4848
}
4949

@@ -806,7 +806,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
806806

807807
std::string *rs_code_;
808808
clang::ASTContext &ctx_;
809-
std::unique_ptr<clang::ASTNameGenerator> name_generator_;
809+
std::unique_ptr<clang::MangleContext> mangle_context_;
810810
clang::FunctionDecl *curr_function_ = nullptr;
811811
bool in_function_formals_ = false;
812812
enum class MethodTarget : uint8_t {

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
#include "converter/lex.h"
3030
#include "converter/mapper.h"
31-
#include "converter/printer.h"
3231
#include "converter/rules/registry.h"
3332

3433
// https://doc.rust-lang.org/reference/keywords.html
@@ -863,40 +862,64 @@ static std::string GetParamSignature(const clang::Decl *decl) {
863862
return args;
864863
}
865864

866-
static clang::ASTNameGenerator *name_generator = nullptr;
865+
static clang::MangleContext *mangle_context = nullptr;
867866

868-
void SetNameGenerator(clang::ASTNameGenerator *generator) {
869-
name_generator = generator;
867+
void SetMangleContext(clang::MangleContext *context) {
868+
mangle_context = context;
870869
}
871870

872-
clang::ASTNameGenerator *GetNameGenerator() { return name_generator; }
871+
clang::MangleContext *GetMangleContext() { return mangle_context; }
873872

874873
static std::string GetMangledName(const clang::Decl *decl) {
875-
assert(name_generator);
876-
return name_generator->getName(decl);
874+
assert(mangle_context);
875+
auto *named = clang::cast<clang::NamedDecl>(decl);
876+
if (!mangle_context->shouldMangleDeclName(named)) {
877+
return named->getNameAsString();
878+
}
879+
clang::GlobalDecl global;
880+
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(decl)) {
881+
global = clang::GlobalDecl(ctor, clang::Ctor_Complete);
882+
} else if (auto *dtor = clang::dyn_cast<clang::CXXDestructorDecl>(decl)) {
883+
global = clang::GlobalDecl(dtor, clang::Dtor_Complete);
884+
} else {
885+
global = clang::GlobalDecl(named);
886+
}
887+
std::string name;
888+
llvm::raw_string_ostream os(name);
889+
mangle_context->mangleName(global, os);
890+
return name;
891+
}
892+
893+
static std::string
894+
GetMangledTypeName(const clang::ClassTemplateSpecializationDecl *decl) {
895+
assert(mangle_context);
896+
std::string name;
897+
llvm::raw_string_ostream os(name);
898+
mangle_context->mangleCanonicalTypeName(
899+
decl->getASTContext().getCanonicalTagType(decl), os);
900+
return name;
877901
}
878902

879903
static std::string GetLexicalSpecializationID(const clang::Decl *decl) {
880-
auto &ctx = decl->getASTContext();
881904
std::string id;
882905
if (const auto *var =
883906
clang::dyn_cast<clang::VarTemplateSpecializationDecl>(decl)) {
884907
id += GetMangledName(var);
885908
}
886909
if (const auto *self =
887910
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
888-
id += Printer::ToString(ctx, GetTypeForDecl(ctx, self));
911+
id += GetMangledTypeName(self);
889912
}
890913
if (const auto *spec =
891914
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(
892915
decl->getLexicalDeclContext());
893916
spec && decl->getLexicalDeclContext() != decl->getDeclContext()) {
894-
id += Printer::ToString(ctx, GetTypeForDecl(ctx, spec));
917+
id += GetMangledTypeName(spec);
895918
}
896919
for (const auto *dc = decl->getDeclContext(); dc; dc = dc->getParent()) {
897920
if (const auto *spec =
898921
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(dc)) {
899-
id += Printer::ToString(ctx, GetTypeForDecl(ctx, spec));
922+
id += GetMangledTypeName(spec);
900923
}
901924
if (const auto *fn = clang::dyn_cast<clang::FunctionDecl>(dc);
902925
fn && fn->getTemplateSpecializationArgs()) {

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ unsigned GetColumnNumber(const clang::Decl *decl);
188188

189189
unsigned GetArraySize(clang::QualType array_type);
190190

191-
void SetNameGenerator(clang::ASTNameGenerator *generator);
191+
void SetMangleContext(clang::MangleContext *context);
192192

193-
clang::ASTNameGenerator *GetNameGenerator();
193+
clang::MangleContext *GetMangleContext();
194194

195195
std::string GetID(const clang::Decl *decl);
196196
std::string GetMethodID(const clang::CXXMethodDecl *decl);

0 commit comments

Comments
 (0)