Skip to content

[clang AST] move mangling API to namespace clang to allow calls from swift-frontend #137884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/include/clang/AST/Mangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ struct ThisAdjustment;
struct ThunkInfo;
class VarDecl;

/// Extract mangling function name from MangleContext such that swift can call
/// it to prepare for ObjCDirect in swift.
void mangleObjCMethodName(raw_ostream &OS, bool includePrefixByte,
bool isInstanceMethod, StringRef ClassName,
std::optional<StringRef> CategoryName,
StringRef MethodName);

/// MangleContext - Context for tracking state which persists across multiple
/// calls to the C++ name mangler.
class MangleContext {
Expand Down
37 changes: 27 additions & 10 deletions clang/lib/AST/Mangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@

using namespace clang;

void clang::mangleObjCMethodName(raw_ostream &OS, bool includePrefixByte,
bool isInstanceMethod, StringRef ClassName,
std::optional<StringRef> CategoryName,
StringRef MethodName) {
// \01+[ContainerName(CategoryName) SelectorName]
if (includePrefixByte)
OS << "\01";
OS << (isInstanceMethod ? '-' : '+');
OS << '[';
OS << ClassName;
if (CategoryName)
OS << "(" << *CategoryName << ")";
OS << " ";
OS << MethodName;
OS << ']';
}

// FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
// much to be desired. Come up with a better mangling scheme.

Expand Down Expand Up @@ -362,26 +379,26 @@ void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
}

// \01+[ContainerName(CategoryName) SelectorName]
if (includePrefixByte) {
OS << '\01';
}
OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
auto CategoryName = std::optional<StringRef>();
StringRef ClassName = "";
if (const auto *CID = MD->getCategory()) {
if (const auto *CI = CID->getClassInterface()) {
OS << CI->getName();
ClassName = CI->getName();
if (includeCategoryNamespace) {
OS << '(' << *CID << ')';
CategoryName = CID->getName();
}
}
} else if (const auto *CD =
dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
OS << CD->getName();
ClassName = CD->getName();
} else {
llvm_unreachable("Unexpected ObjC method decl context");
}
OS << ' ';
MD->getSelector().print(OS);
OS << ']';
std::string MethodName;
llvm::raw_string_ostream MethodNameOS(MethodName);
MD->getSelector().print(MethodNameOS);
clang::mangleObjCMethodName(OS, includePrefixByte, MD->isInstanceMethod(),
ClassName, CategoryName, MethodName);
}

void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
Expand Down