21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
26 using namespace clang::index;
29 std::unique_ptr<MangleContext>
MC;
33 :
MC(Ctx.createMangleContext()),
34 DL(Ctx.getTargetInfo().getDataLayout()) {}
39 llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
40 if (
auto *FD = dyn_cast<FunctionDecl>(D)) {
41 if (FD->isDependentContext())
43 if (writeFuncOrVarName(FD, FrontendBufOS))
45 }
else if (
auto *VD = dyn_cast<VarDecl>(D)) {
46 if (writeFuncOrVarName(VD, FrontendBufOS))
48 }
else if (
auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
49 MC->mangleObjCMethodNameWithoutSize(MD, OS);
51 }
else if (
auto *
ID = dyn_cast<ObjCInterfaceDecl>(D)) {
52 writeObjCClassName(
ID, FrontendBufOS);
58 llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(),
DL);
65 llvm::raw_string_ostream OS(Name);
72 if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
78 std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
80 std::vector<std::string> Manglings;
86 return CC == DefaultCC;
89 if (
const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
90 Manglings.emplace_back(getMangledStructor(CD,
Ctor_Base));
92 if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
93 if (!CD->getParent()->isAbstract())
94 Manglings.emplace_back(getMangledStructor(CD,
Ctor_Complete));
96 if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
97 if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
100 }
else if (
const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
101 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Base));
102 if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
103 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Complete));
105 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Deleting));
107 }
else if (
const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
108 Manglings.emplace_back(
getName(ND));
110 if (
const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
111 for (
const auto &T : *TIV)
112 Manglings.emplace_back(getMangledThunk(MD, T));
119 bool writeFuncOrVarName(
const NamedDecl *D, raw_ostream &OS) {
120 if (
MC->shouldMangleDeclName(D)) {
121 if (
const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
123 else if (
const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
126 MC->mangleName(D, OS);
138 OS << getClassSymbolPrefix();
142 static StringRef getClassSymbolPrefix() {
143 return "OBJC_CLASS_$_";
147 std::string FrontendBuf;
148 llvm::raw_string_ostream FOS(FrontendBuf);
150 if (
const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
151 MC->mangleCXXCtor(CD, static_cast<CXXCtorType>(StructorType), FOS);
152 else if (
const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
153 MC->mangleCXXDtor(DD, static_cast<CXXDtorType>(StructorType), FOS);
155 std::string BackendBuf;
156 llvm::raw_string_ostream BOS(BackendBuf);
158 llvm::Mangler::getNameWithPrefix(BOS, FOS.str(),
DL);
164 std::string FrontendBuf;
165 llvm::raw_string_ostream FOS(FrontendBuf);
167 MC->mangleThunk(MD, T, FOS);
169 std::string BackendBuf;
170 llvm::raw_string_ostream BOS(BackendBuf);
172 llvm::Mangler::getNameWithPrefix(BOS, FOS.str(),
DL);
186 return Impl->writeName(D, OS);
190 return Impl->getName(D);
194 return Impl->getAllManglings(D);
StringRef getObjCRuntimeNameAsString() const
Produce a name to be used for class's metadata.
Defines the clang::ASTContext interface.
std::string getName(const Decl *D)
CodegenNameGenerator(ASTContext &Ctx)
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Default closure variant of a ctor.
One of these records is kept for each identifier that is lexed.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
The this pointer adjustment as well as an optional return adjustment for a thunk. ...
std::vector< std::string > getAllManglings(const Decl *D)
This can return multiple mangled names when applicable, e.g.
static bool hasDefaultCXXMethodCC(ASTContext &Context, const CXXMethodDecl *MD)
bool writeName(const Decl *D, raw_ostream &OS)
Represents an ObjC class declaration.
Represents a prototype with parameter type info, e.g.
StringRef getName() const
Return the actual identifier string.
std::unique_ptr< MangleContext > MC
std::string getName(const Decl *D)
Version of writeName function that returns a string.
Represents a static or instance method of a struct/union/class.
bool writeName(const Decl *D, raw_ostream &OS)
std::vector< std::string > getAllManglings(const Decl *D)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
CallingConv getDefaultCallingConvention(bool isVariadic, bool IsCXXMethod) const
Retrieves the default calling convention for the current target.
Defines the clang::TargetInfo interface.
Implementation(ASTContext &Ctx)
NamedDecl - This represents a decl with a name.