23#include "llvm/ADT/StringExtras.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/Mangler.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/Format.h"
28#include "llvm/Support/raw_ostream.h"
34 std::optional<StringRef> CategoryName,
35 StringRef MethodName) {
37 if (includePrefixByte)
43 OS <<
"(" << *CategoryName <<
")";
56 unsigned discriminator = Context.getBlockId(BD,
true);
57 if (discriminator == 0)
58 Out <<
"__" << Outer <<
"_block_invoke";
60 Out <<
"__" << Outer <<
"_block_invoke_" << discriminator+1;
63void MangleContext::anchor() { }
76 return FD->isExternC();
77 if (
const VarDecl *VD = dyn_cast<VarDecl>(ND))
78 return VD->isExternC();
85 const llvm::Triple &Triple = TI.
getTriple();
91 if (FD->isMain() && FD->getNumParams() == 2)
98 TI.
getCXXABI() == TargetCXXABI::Microsoft)
149 if (isa<MSGuidDecl>(
D))
161 if (
const AsmLabelAttr *ALA =
D->
getAttr<AsmLabelAttr>()) {
166 if (ALA->getLabel().starts_with(
"llvm.")) {
167 Out << ALA->getLabel();
176 StringRef UserLabelPrefix =
180 llvm::DataLayout(
getASTContext().getTargetInfo().getDataLayoutString())
182 assert((UserLabelPrefix.empty() && !GlobalPrefix) ||
183 (UserLabelPrefix.size() == 1 && UserLabelPrefix[0] == GlobalPrefix));
185 if (!UserLabelPrefix.empty())
188 Out << ALA->getLabel();
192 if (
auto *GD = dyn_cast<MSGuidDecl>(
D))
198 Out <<
"__main_argc_argv";
219 Out <<
"__regcall4__";
221 Out <<
"__regcall3__";
225 Out <<
D->getIdentifier()->getName();
241 assert(!Proto->isVariadic());
242 unsigned ArgWords = 0;
244 if (MD->isImplicitObjectMemberFunction())
247 for (
const auto &AT : Proto->param_types()) {
251 if (AT->isIncompleteType())
257 Out << ((DefaultPtrWidth / 8) * ArgWords);
261 raw_ostream &Out)
const {
265 Out << llvm::format(
"_GUID_%08" PRIx32
"_%04" PRIx32
"_%04" PRIx32
"_",
266 P.Part1,
P.Part2,
P.Part3);
268 for (uint8_t
C :
P.Part4And5) {
269 Out << llvm::format(
"%02" PRIx8,
C);
278 unsigned discriminator =
getBlockId(BD,
false);
283 Out << ID->getIdentifier()->getName();
286 if (discriminator == 0)
287 Out <<
"_block_invoke";
289 Out <<
"_block_invoke_" << discriminator+1;
294 raw_ostream &ResStream) {
296 llvm::raw_svector_ostream Out(Buffer);
303 raw_ostream &ResStream) {
305 llvm::raw_svector_ostream Out(Buffer);
312 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
315 llvm::raw_svector_ostream Stream(Buffer);
319 assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
320 "expected a NamedDecl or BlockDecl");
321 for (; isa_and_nonnull<BlockDecl>(DC); DC = DC->
getParent())
323 assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
324 "expected a TranslationUnitDecl or a NamedDecl");
325 if (
const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
327 else if (
const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
329 else if (
auto ND = dyn_cast<NamedDecl>(DC)) {
331 Stream << ND->getIdentifier()->getName();
347 bool includePrefixByte,
348 bool includeCategoryNamespace)
const {
357 if (includeCategoryNamespace) {
359 OS << category->getName();
364 for (
unsigned slotIndex = 0,
365 numArgs = selector.getNumArgs(),
366 slotEnd = std::max(numArgs, 1U);
367 slotIndex != slotEnd; ++slotIndex) {
368 if (
auto name = selector.getIdentifierInfoForSlot(slotIndex))
369 OS << name->getName();
382 auto CategoryName = std::optional<StringRef>();
383 StringRef ClassName =
"";
385 if (
const auto *CI = CID->getClassInterface()) {
386 ClassName = CI->getName();
387 if (includeCategoryNamespace) {
388 CategoryName = CID->getName();
391 }
else if (
const auto *CD =
393 ClassName = CD->getName();
395 llvm_unreachable(
"Unexpected ObjC method decl context");
397 std::string MethodName;
398 llvm::raw_string_ostream MethodNameOS(MethodName);
401 ClassName, CategoryName, MethodName);
405 raw_ostream &Out)
const {
407 llvm::raw_svector_ostream OS(Name);
411 Out << OS.str().size() << OS.str();
415 std::unique_ptr<MangleContext> MC;
420 : MC(Ctx.createMangleContext()),
421 DL(Ctx.getTargetInfo().getDataLayoutString()) {}
426 llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
427 if (
auto *FD = dyn_cast<FunctionDecl>(
D)) {
428 if (FD->isDependentContext())
430 if (writeFuncOrVarName(FD, FrontendBufOS))
432 }
else if (
auto *VD = dyn_cast<VarDecl>(
D)) {
433 if (writeFuncOrVarName(VD, FrontendBufOS))
435 }
else if (
auto *MD = dyn_cast<ObjCMethodDecl>(
D)) {
436 MC->mangleObjCMethodName(MD, OS,
false,
439 }
else if (
auto *ID = dyn_cast<ObjCInterfaceDecl>(
D)) {
440 writeObjCClassName(ID, FrontendBufOS);
446 llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
453 llvm::raw_string_ostream OS(Name);
467 return Kind ==
ObjCMetaclass ?
"_OBJC_METACLASS_" :
"_OBJC_CLASS_";
468 return Kind ==
ObjCMetaclass ?
"OBJC_METACLASS_$_" :
"OBJC_CLASS_$_";
473 if (
const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
474 ClassName = OID->getObjCRuntimeNameAsString();
475 else if (
const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
476 ClassName = OID->getObjCRuntimeNameAsString();
478 if (ClassName.empty())
481 auto Mangle = [&](
ObjCKind Kind, StringRef ClassName) -> std::string {
484 llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
485 return std::string(Mangled);
495 if (
const auto *OCD = dyn_cast<ObjCContainerDecl>(
D))
498 if (!(isa<CXXRecordDecl>(
D) || isa<CXXMethodDecl>(
D)))
506 std::vector<std::string> Manglings;
509 auto DefaultCC =
C.getDefaultCallingConvention(
false,
512 return CC == DefaultCC;
515 if (
const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
516 Manglings.emplace_back(getMangledStructor(CD,
Ctor_Base));
519 if (!CD->getParent()->isAbstract())
520 Manglings.emplace_back(getMangledStructor(CD,
Ctor_Complete));
523 if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
526 }
else if (
const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
527 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Base));
529 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Complete));
531 Manglings.emplace_back(getMangledStructor(DD,
Dtor_Deleting));
533 }
else if (
const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
534 Manglings.emplace_back(
getName(ND));
535 if (MD->isVirtual()) {
537 for (
const auto &
T : *TIV) {
538 std::string ThunkName;
539 std::string ContextualizedName =
540 getMangledThunk(MD,
T,
false);
542 ThunkName = getMangledThunk(MD,
T,
true);
544 ThunkName = ContextualizedName;
545 Manglings.emplace_back(ThunkName);
555 bool writeFuncOrVarName(
const NamedDecl *
D, raw_ostream &OS) {
556 if (MC->shouldMangleDeclName(
D)) {
558 if (
const auto *CtorD = dyn_cast<CXXConstructorDecl>(
D))
560 else if (
const auto *DtorD = dyn_cast<CXXDestructorDecl>(
D))
562 else if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(
D)) {
566 MC->mangleName(GD, OS);
579 OS <<
D->getObjCRuntimeNameAsString();
582 std::string getMangledStructor(
const NamedDecl *ND,
unsigned StructorType) {
583 std::string FrontendBuf;
584 llvm::raw_string_ostream FOS(FrontendBuf);
587 if (
const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
589 else if (
const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
591 MC->mangleName(GD, FOS);
593 std::string BackendBuf;
594 llvm::raw_string_ostream BOS(BackendBuf);
596 llvm::Mangler::getNameWithPrefix(BOS, FrontendBuf, DL);
602 bool ElideOverrideInfo) {
603 std::string FrontendBuf;
604 llvm::raw_string_ostream FOS(FrontendBuf);
606 MC->mangleThunk(MD,
T, ElideOverrideInfo, FOS);
608 std::string BackendBuf;
609 llvm::raw_string_ostream BOS(BackendBuf);
611 llvm::Mangler::getNameWithPrefix(BOS, FrontendBuf, DL);
623 return Impl->writeName(
D, OS);
627 return Impl->getName(
D);
631 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)
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
bool useAbbreviatedThunkName(GlobalDecl VirtualMethodDecl, StringRef MangledName)
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
DeclContext * getDeclContext()
Module * getOwningModuleForLinkage() const
Get the module that owns this declaration for linkage purposes.
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)
virtual void mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &) const
void mangleName(GlobalDecl GD, raw_ostream &)
virtual void mangleCXXName(GlobalDecl GD, raw_ostream &)=0
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &OS, bool includePrefixByte=true, bool includeCategoryNamespace=true) const
void mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD, raw_ostream &) const
This represents a decl that may have a name.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
ObjCContainerDecl - Represents a container for method declarations.
Represents an ObjC class declaration.
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.
bool shouldUseMicrosoftCCforMangling() const
Should the Microsoft mangling scheme be used for C Calling Convention.
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.
The JSON file list parser is used to communicate input to InstallAPI.
CXXCtorType
C++ constructor types.
@ Ctor_Base
Base object ctor.
@ Ctor_DefaultClosure
Default closure variant of a ctor.
@ Ctor_Complete
Complete object ctor.
bool isInstanceMethod(const Decl *D)
CXXDtorType
C++ destructor types.
@ Dtor_Base
Base object dtor.
@ Dtor_Complete
Complete object dtor.
@ Dtor_Deleting
Deleting dtor.
const FunctionProtoType * T
CallingConv
CallingConv - Specifies the calling convention that a function uses.
void mangleObjCMethodName(raw_ostream &OS, bool includePrefixByte, bool isInstanceMethod, StringRef ClassName, std::optional< StringRef > CategoryName, StringRef MethodName)
Extract mangling function name from MangleContext such that swift can call it to prepare for ObjCDire...
Parts of a decomposed MSGuidDecl.
The this pointer adjustment as well as an optional return adjustment for a thunk.