24#include "llvm/ADT/StringExtras.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/Mangler.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/Format.h"
29#include "llvm/Support/raw_ostream.h"
40 unsigned discriminator = Context.getBlockId(BD,
true);
41 if (discriminator == 0)
42 Out <<
"__" << Outer <<
"_block_invoke";
44 Out <<
"__" << Outer <<
"_block_invoke_" << discriminator+1;
47void MangleContext::anchor() { }
60 return FD->isExternC();
61 if (
const VarDecl *VD = dyn_cast<VarDecl>(ND))
62 return VD->isExternC();
69 const llvm::Triple &Triple = TI.
getTriple();
75 if (FD->isMain() && FD->getNumParams() == 2)
78 if (!Triple.isOSWindows() || !Triple.isX86())
82 TI.
getCXXABI() == TargetCXXABI::Microsoft)
129 if (D->
hasAttr<AsmLabelAttr>())
133 if (isa<MSGuidDecl>(D))
145 if (
const AsmLabelAttr *ALA = D->
getAttr<AsmLabelAttr>()) {
150 if (!ALA->getIsLiteralLabel() || ALA->getLabel().startswith(
"llvm.")) {
151 Out << ALA->getLabel();
160 StringRef UserLabelPrefix =
164 llvm::DataLayout(
getASTContext().getTargetInfo().getDataLayoutString())
166 assert((UserLabelPrefix.empty() && !GlobalPrefix) ||
167 (UserLabelPrefix.size() == 1 && UserLabelPrefix[0] == GlobalPrefix));
169 if (!UserLabelPrefix.empty())
172 Out << ALA->getLabel();
176 if (
auto *GD = dyn_cast<MSGuidDecl>(D))
182 Out <<
"__main_argc_argv";
203 Out <<
"__regcall4__";
205 Out <<
"__regcall3__";
225 assert(!Proto->isVariadic());
226 unsigned ArgWords = 0;
228 if (MD->isImplicitObjectMemberFunction())
231 for (
const auto &AT : Proto->param_types()) {
235 if (AT->isIncompleteType())
241 Out << ((DefaultPtrWidth / 8) * ArgWords);
248 Out << llvm::format(
"_GUID_%08" PRIx32
"_%04" PRIx32
"_%04" PRIx32
"_",
249 P.Part1,
P.Part2,
P.Part3);
251 for (uint8_t
C :
P.Part4And5) {
252 Out << llvm::format(
"%02" PRIx8,
C);
261 unsigned discriminator =
getBlockId(BD,
false);
266 Out << ID->getIdentifier()->getName();
269 if (discriminator == 0)
270 Out <<
"_block_invoke";
272 Out <<
"_block_invoke_" << discriminator+1;
277 raw_ostream &ResStream) {
279 llvm::raw_svector_ostream Out(Buffer);
286 raw_ostream &ResStream) {
288 llvm::raw_svector_ostream Out(Buffer);
295 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
298 llvm::raw_svector_ostream Stream(Buffer);
299 if (
const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
302 assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
303 "expected a NamedDecl or BlockDecl");
304 if (isa<BlockDecl>(DC))
305 for (; DC && isa<BlockDecl>(DC); DC = DC->
getParent())
307 assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
308 "expected a TranslationUnitDecl or a NamedDecl");
309 if (
const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
311 else if (
const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
313 else if (
auto ND = dyn_cast<NamedDecl>(DC)) {
315 Stream << ND->getIdentifier()->getName();
331 bool includePrefixByte,
332 bool includeCategoryNamespace) {
341 if (includeCategoryNamespace) {
343 OS << category->getName();
348 for (
unsigned slotIndex = 0,
349 numArgs = selector.getNumArgs(),
350 slotEnd = std::max(numArgs, 1U);
351 slotIndex != slotEnd; ++slotIndex) {
352 if (
auto name = selector.getIdentifierInfoForSlot(slotIndex))
353 OS << name->getName();
366 if (includePrefixByte) {
371 OS << CID->getClassInterface()->getName();
372 if (includeCategoryNamespace) {
373 OS <<
'(' << *CID <<
')';
375 }
else if (
const auto *CD =
379 llvm_unreachable(
"Unexpected ObjC method decl context");
389 llvm::raw_svector_ostream OS(Name);
393 Out << OS.str().size() << OS.str();
397 std::unique_ptr<MangleContext> MC;
402 : MC(Ctx.createMangleContext()),
403 DL(Ctx.getTargetInfo().getDataLayoutString()) {}
408 llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
409 if (
auto *FD = dyn_cast<FunctionDecl>(D)) {
410 if (FD->isDependentContext())
412 if (writeFuncOrVarName(FD, FrontendBufOS))
414 }
else if (
auto *VD = dyn_cast<VarDecl>(D)) {
415 if (writeFuncOrVarName(VD, FrontendBufOS))
417 }
else if (
auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
418 MC->mangleObjCMethodName(MD, OS,
false,
421 }
else if (
auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
422 writeObjCClassName(ID, FrontendBufOS);
428 llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
435 llvm::raw_string_ostream OS(Name);
449 return Kind ==
ObjCMetaclass ?
"_OBJC_METACLASS_" :
"_OBJC_CLASS_";
450 return Kind ==
ObjCMetaclass ?
"OBJC_METACLASS_$_" :
"OBJC_CLASS_$_";
455 if (
const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
456 ClassName = OID->getObjCRuntimeNameAsString();
457 else if (
const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
458 ClassName = OID->getObjCRuntimeNameAsString();
460 if (ClassName.empty())
463 auto Mangle = [&](
ObjCKind Kind, StringRef ClassName) -> std::string {
466 llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
467 return std::string(Mangled.str());
477 if (
const auto *OCD = dyn_cast<ObjCContainerDecl>(D))
480 if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
483 const NamedDecl *ND = cast<NamedDecl>(D);
488 std::vector<std::string> Manglings;
491 auto DefaultCC =
C.getDefaultCallingConvention(
false,
494 return CC == DefaultCC;
497 if (
const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
498 Manglings.emplace_back(getMangledStructor(CD,
Ctor_Base));
501 if (!CD->getParent()->isAbstract())
502 Manglings.emplace_back(getMangledStructor(CD,
Ctor_Complete));
505 if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
508 }
else if (
const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
509 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Base));
511 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Complete));
513 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Deleting));
515 }
else if (
const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
516 Manglings.emplace_back(
getName(ND));
519 for (
const auto &T : *TIV)
520 Manglings.emplace_back(getMangledThunk(MD, T));
527 bool writeFuncOrVarName(
const NamedDecl *D, raw_ostream &OS) {
528 if (MC->shouldMangleDeclName(D)) {
530 if (
const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
532 else if (
const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
534 else if (D->
hasAttr<CUDAGlobalAttr>())
538 MC->mangleName(GD, OS);
554 std::string getMangledStructor(
const NamedDecl *ND,
unsigned StructorType) {
555 std::string FrontendBuf;
556 llvm::raw_string_ostream FOS(FrontendBuf);
559 if (
const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
561 else if (
const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
563 MC->mangleName(GD, FOS);
565 std::string BackendBuf;
566 llvm::raw_string_ostream BOS(BackendBuf);
568 llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
574 std::string FrontendBuf;
575 llvm::raw_string_ostream FOS(FrontendBuf);
577 MC->mangleThunk(MD, T, FOS);
579 std::string BackendBuf;
580 llvm::raw_string_ostream BOS(BackendBuf);
582 llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
594 return Impl->writeName(D, OS);
598 return Impl->getName(D);
602 return Impl->getAllManglings(D);
Enums/classes describing ABI related information about constructors, destructors and thunks.
Defines the clang::ASTContext interface.
static bool hasDefaultCXXMethodCC(ASTContext &Context, const CXXMethodDecl *MD)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Defines the clang::Expr interface and subclasses for C++ expressions.
static void mangleFunctionBlock(MangleContext &Context, StringRef Outer, const BlockDecl *BD, raw_ostream &Out)
static bool isExternC(const NamedDecl *ND)
static CCMangling getCallingConvMangling(const ASTContext &Context, const NamedDecl *ND)
Defines the SourceManager interface.
std::string getName(const Decl *D)
std::vector< std::string > getAllManglings(const Decl *D)
static StringRef getClassSymbolPrefix(ObjCKind Kind, const ASTContext &Context)
bool writeName(const Decl *D, raw_ostream &OS)
Implementation(ASTContext &Ctx)
std::vector< std::string > getAllManglings(const ObjCContainerDecl *OCD)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
const LangOptions & getLangOpts() const
VTableContextBase * getVTableContext()
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
const TargetInfo & getTargetInfo() const
std::string getName(const Decl *D)
ASTNameGenerator(ASTContext &Ctx)
bool writeName(const Decl *D, raw_ostream &OS)
Writes name for D to OS.
std::vector< std::string > getAllManglings(const Decl *D)
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Represents a C++ constructor within a class.
Represents a C++ destructor within a class.
Represents a static or instance method of a struct/union/class.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Decl - This represents one declaration (or definition), e.g.
ASTContext & getASTContext() const LLVM_READONLY
Module * getOwningModuleForLinkage(bool IgnoreLinkage=false) const
Get the module that owns this declaration for linkage purposes.
DeclContext * getDeclContext()
Represents a function declaration or definition.
Represents a prototype with parameter type info, e.g.
FunctionType - C99 6.7.5.3 - Function Declarators.
CallingConv getCallConv() const
GlobalDecl - represents a global declaration.
const Decl * getDecl() const
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
clang::ObjCRuntime ObjCRuntime
Parts getParts() const
Get the decomposed parts of this declaration.
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
unsigned getBlockId(const BlockDecl *BD, bool Local)
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
ASTContext & getASTContext() const
void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID, raw_ostream &Out)
virtual bool isUniqueInternalLinkageDecl(const NamedDecl *ND)
bool shouldMangleDeclName(const NamedDecl *D)
void mangleName(GlobalDecl GD, raw_ostream &)
void mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD, raw_ostream &)
virtual void mangleCXXName(GlobalDecl GD, raw_ostream &)=0
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &OS, bool includePrefixByte=true, bool includeCategoryNamespace=true)
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
virtual void mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &)
This represents a decl that may have a name.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
bool hasExternalFormalLinkage() const
True if this decl has external linkage.
ObjCContainerDecl - Represents a container for method declarations.
Represents an ObjC class declaration.
StringRef getObjCRuntimeNameAsString() const
Produce a name to be used for class's metadata.
ObjCMethodDecl - Represents an instance or class method declaration.
Selector getSelector() const
bool isInstanceMethod() const
ObjCCategoryDecl * getCategory()
If this method is declared or implemented in a category, return that category.
bool isClassMethod() const
ObjCInterfaceDecl * getClassInterface()
The basic abstraction for the target Objective-C runtime.
bool isGNUFamily() const
Is this runtime basically of the GNU family of runtimes?
A (possibly-)qualified type.
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
bool isItaniumFamily() const
Does this ABI generally fall into the Itanium family of ABIs?
Exposes information about the current target.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
const char * getUserLabelPrefix() const
Returns the default value of the USER_LABEL_PREFIX macro, which is the prefix given to user symbols b...
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
const T * castAs() const
Member-template castAs<specific type>.
virtual const ThunkInfoVectorTy * getThunkInfo(GlobalDecl GD)
Represents a variable declaration or definition.
Defines the clang::TargetInfo interface.
CXXCtorType
C++ constructor types.
@ Ctor_Base
Base object ctor.
@ Ctor_DefaultClosure
Default closure variant of a ctor.
@ Ctor_Complete
Complete object ctor.
CXXDtorType
C++ destructor types.
@ Dtor_Base
Base object dtor.
@ Dtor_Complete
Complete object dtor.
@ Dtor_Deleting
Deleting dtor.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Parts of a decomposed MSGuidDecl.
The this pointer adjustment as well as an optional return adjustment for a thunk.