xref: /freebsd/contrib/llvm-project/clang/lib/AST/Mangle.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implements generic name mangling support for blocks and Objective-C.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric #include "clang/AST/Attr.h"
130b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
140b57cec5SDimitry Andric #include "clang/AST/Decl.h"
150b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h"
160b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h"
170b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h"
180b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h"
190b57cec5SDimitry Andric #include "clang/AST/Mangle.h"
200b57cec5SDimitry Andric #include "clang/AST/VTableBuilder.h"
210b57cec5SDimitry Andric #include "clang/Basic/ABI.h"
220b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h"
230b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h"
240b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
250b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
260b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
270b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
285ffd83dbSDimitry Andric #include "llvm/Support/Format.h"
290b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric using namespace clang;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
340b57cec5SDimitry Andric // much to be desired. Come up with a better mangling scheme.
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric static void mangleFunctionBlock(MangleContext &Context,
370b57cec5SDimitry Andric                                 StringRef Outer,
380b57cec5SDimitry Andric                                 const BlockDecl *BD,
390b57cec5SDimitry Andric                                 raw_ostream &Out) {
400b57cec5SDimitry Andric   unsigned discriminator = Context.getBlockId(BD, true);
410b57cec5SDimitry Andric   if (discriminator == 0)
420b57cec5SDimitry Andric     Out << "__" << Outer << "_block_invoke";
430b57cec5SDimitry Andric   else
440b57cec5SDimitry Andric     Out << "__" << Outer << "_block_invoke_" << discriminator+1;
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric void MangleContext::anchor() { }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric enum CCMangling {
500b57cec5SDimitry Andric   CCM_Other,
510b57cec5SDimitry Andric   CCM_Fast,
520b57cec5SDimitry Andric   CCM_RegCall,
530b57cec5SDimitry Andric   CCM_Vector,
545ffd83dbSDimitry Andric   CCM_Std,
555ffd83dbSDimitry Andric   CCM_WasmMainArgcArgv
560b57cec5SDimitry Andric };
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric static bool isExternC(const NamedDecl *ND) {
590b57cec5SDimitry Andric   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
600b57cec5SDimitry Andric     return FD->isExternC();
615ffd83dbSDimitry Andric   if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
625ffd83dbSDimitry Andric     return VD->isExternC();
635ffd83dbSDimitry Andric   return false;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric static CCMangling getCallingConvMangling(const ASTContext &Context,
670b57cec5SDimitry Andric                                          const NamedDecl *ND) {
680b57cec5SDimitry Andric   const TargetInfo &TI = Context.getTargetInfo();
690b57cec5SDimitry Andric   const llvm::Triple &Triple = TI.getTriple();
705ffd83dbSDimitry Andric 
715ffd83dbSDimitry Andric   // On wasm, the argc/argv form of "main" is renamed so that the startup code
725ffd83dbSDimitry Andric   // can call it with the correct function signature.
735ffd83dbSDimitry Andric   // On Emscripten, users may be exporting "main" and expecting to call it
745ffd83dbSDimitry Andric   // themselves, so we can't mangle it.
755ffd83dbSDimitry Andric   if (Triple.isWasm() && !Triple.isOSEmscripten())
765ffd83dbSDimitry Andric     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
775ffd83dbSDimitry Andric       if (FD->isMain() && FD->hasPrototype() && FD->param_size() == 2)
785ffd83dbSDimitry Andric         return CCM_WasmMainArgcArgv;
795ffd83dbSDimitry Andric 
80480093f4SDimitry Andric   if (!Triple.isOSWindows() || !Triple.isX86())
810b57cec5SDimitry Andric     return CCM_Other;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
840b57cec5SDimitry Andric       TI.getCXXABI() == TargetCXXABI::Microsoft)
850b57cec5SDimitry Andric     return CCM_Other;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
880b57cec5SDimitry Andric   if (!FD)
890b57cec5SDimitry Andric     return CCM_Other;
900b57cec5SDimitry Andric   QualType T = FD->getType();
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   const FunctionType *FT = T->castAs<FunctionType>();
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   CallingConv CC = FT->getCallConv();
950b57cec5SDimitry Andric   switch (CC) {
960b57cec5SDimitry Andric   default:
970b57cec5SDimitry Andric     return CCM_Other;
980b57cec5SDimitry Andric   case CC_X86FastCall:
990b57cec5SDimitry Andric     return CCM_Fast;
1000b57cec5SDimitry Andric   case CC_X86StdCall:
1010b57cec5SDimitry Andric     return CCM_Std;
1020b57cec5SDimitry Andric   case CC_X86VectorCall:
1030b57cec5SDimitry Andric     return CCM_Vector;
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
1080b57cec5SDimitry Andric   const ASTContext &ASTContext = getASTContext();
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   CCMangling CC = getCallingConvMangling(ASTContext, D);
1110b57cec5SDimitry Andric   if (CC != CCM_Other)
1120b57cec5SDimitry Andric     return true;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   // If the declaration has an owning module for linkage purposes that needs to
1150b57cec5SDimitry Andric   // be mangled, we must mangle its name.
1160b57cec5SDimitry Andric   if (!D->hasExternalFormalLinkage() && D->getOwningModuleForLinkage())
1170b57cec5SDimitry Andric     return true;
1180b57cec5SDimitry Andric 
119*fe6060f1SDimitry Andric   // C functions with internal linkage have to be mangled with option
120*fe6060f1SDimitry Andric   // -funique-internal-linkage-names.
121*fe6060f1SDimitry Andric   if (!getASTContext().getLangOpts().CPlusPlus &&
122*fe6060f1SDimitry Andric       isUniqueInternalLinkageDecl(D))
123*fe6060f1SDimitry Andric     return true;
124*fe6060f1SDimitry Andric 
1250b57cec5SDimitry Andric   // In C, functions with no attributes never need to be mangled. Fastpath them.
1260b57cec5SDimitry Andric   if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
1270b57cec5SDimitry Andric     return false;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   // Any decl can be declared with __asm("foo") on it, and this takes precedence
1300b57cec5SDimitry Andric   // over all other naming in the .o file.
1310b57cec5SDimitry Andric   if (D->hasAttr<AsmLabelAttr>())
1320b57cec5SDimitry Andric     return true;
1330b57cec5SDimitry Andric 
1345ffd83dbSDimitry Andric   // Declarations that don't have identifier names always need to be mangled.
1355ffd83dbSDimitry Andric   if (isa<MSGuidDecl>(D))
1365ffd83dbSDimitry Andric     return true;
1375ffd83dbSDimitry Andric 
1380b57cec5SDimitry Andric   return shouldMangleCXXName(D);
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric 
1415ffd83dbSDimitry Andric void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
142*fe6060f1SDimitry Andric   const ASTContext &ASTContext = getASTContext();
1435ffd83dbSDimitry Andric   const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
144*fe6060f1SDimitry Andric 
1450b57cec5SDimitry Andric   // Any decl can be declared with __asm("foo") on it, and this takes precedence
1460b57cec5SDimitry Andric   // over all other naming in the .o file.
1470b57cec5SDimitry Andric   if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
1480b57cec5SDimitry Andric     // If we have an asm name, then we use it as the mangling.
1490b57cec5SDimitry Andric 
150a7dea167SDimitry Andric     // If the label isn't literal, or if this is an alias for an LLVM intrinsic,
151a7dea167SDimitry Andric     // do not add a "\01" prefix.
152a7dea167SDimitry Andric     if (!ALA->getIsLiteralLabel() || ALA->getLabel().startswith("llvm.")) {
153a7dea167SDimitry Andric       Out << ALA->getLabel();
154a7dea167SDimitry Andric       return;
155a7dea167SDimitry Andric     }
156a7dea167SDimitry Andric 
1570b57cec5SDimitry Andric     // Adding the prefix can cause problems when one file has a "foo" and
1580b57cec5SDimitry Andric     // another has a "\01foo". That is known to happen on ELF with the
1590b57cec5SDimitry Andric     // tricks normally used for producing aliases (PR9177). Fortunately the
1600b57cec5SDimitry Andric     // llvm mangler on ELF is a nop, so we can just avoid adding the \01
161a7dea167SDimitry Andric     // marker.
162*fe6060f1SDimitry Andric     StringRef UserLabelPrefix =
163*fe6060f1SDimitry Andric         getASTContext().getTargetInfo().getUserLabelPrefix();
164*fe6060f1SDimitry Andric #ifndef NDEBUG
1650b57cec5SDimitry Andric     char GlobalPrefix =
166*fe6060f1SDimitry Andric         llvm::DataLayout(getASTContext().getTargetInfo().getDataLayoutString())
167*fe6060f1SDimitry Andric             .getGlobalPrefix();
168*fe6060f1SDimitry Andric     assert((UserLabelPrefix.empty() && !GlobalPrefix) ||
169*fe6060f1SDimitry Andric            (UserLabelPrefix.size() == 1 && UserLabelPrefix[0] == GlobalPrefix));
170*fe6060f1SDimitry Andric #endif
171*fe6060f1SDimitry Andric     if (!UserLabelPrefix.empty())
1720b57cec5SDimitry Andric       Out << '\01'; // LLVM IR Marker for __asm("foo")
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric     Out << ALA->getLabel();
1750b57cec5SDimitry Andric     return;
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric 
1785ffd83dbSDimitry Andric   if (auto *GD = dyn_cast<MSGuidDecl>(D))
1795ffd83dbSDimitry Andric     return mangleMSGuidDecl(GD, Out);
1805ffd83dbSDimitry Andric 
1810b57cec5SDimitry Andric   CCMangling CC = getCallingConvMangling(ASTContext, D);
1825ffd83dbSDimitry Andric 
1835ffd83dbSDimitry Andric   if (CC == CCM_WasmMainArgcArgv) {
1845ffd83dbSDimitry Andric     Out << "__main_argc_argv";
1855ffd83dbSDimitry Andric     return;
1865ffd83dbSDimitry Andric   }
1875ffd83dbSDimitry Andric 
1880b57cec5SDimitry Andric   bool MCXX = shouldMangleCXXName(D);
1890b57cec5SDimitry Andric   const TargetInfo &TI = Context.getTargetInfo();
1900b57cec5SDimitry Andric   if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
1910b57cec5SDimitry Andric     if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
192e8d8bef9SDimitry Andric       mangleObjCMethodNameAsSourceName(OMD, Out);
1930b57cec5SDimitry Andric     else
1945ffd83dbSDimitry Andric       mangleCXXName(GD, Out);
1950b57cec5SDimitry Andric     return;
1960b57cec5SDimitry Andric   }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   Out << '\01';
1990b57cec5SDimitry Andric   if (CC == CCM_Std)
2000b57cec5SDimitry Andric     Out << '_';
2010b57cec5SDimitry Andric   else if (CC == CCM_Fast)
2020b57cec5SDimitry Andric     Out << '@';
2030b57cec5SDimitry Andric   else if (CC == CCM_RegCall)
2040b57cec5SDimitry Andric     Out << "__regcall3__";
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   if (!MCXX)
2070b57cec5SDimitry Andric     Out << D->getIdentifier()->getName();
2080b57cec5SDimitry Andric   else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
209e8d8bef9SDimitry Andric     mangleObjCMethodNameAsSourceName(OMD, Out);
2100b57cec5SDimitry Andric   else
2115ffd83dbSDimitry Andric     mangleCXXName(GD, Out);
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   const FunctionDecl *FD = cast<FunctionDecl>(D);
2140b57cec5SDimitry Andric   const FunctionType *FT = FD->getType()->castAs<FunctionType>();
2150b57cec5SDimitry Andric   const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
2160b57cec5SDimitry Andric   if (CC == CCM_Vector)
2170b57cec5SDimitry Andric     Out << '@';
2180b57cec5SDimitry Andric   Out << '@';
2190b57cec5SDimitry Andric   if (!Proto) {
2200b57cec5SDimitry Andric     Out << '0';
2210b57cec5SDimitry Andric     return;
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric   assert(!Proto->isVariadic());
2240b57cec5SDimitry Andric   unsigned ArgWords = 0;
2250b57cec5SDimitry Andric   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
2260b57cec5SDimitry Andric     if (!MD->isStatic())
2270b57cec5SDimitry Andric       ++ArgWords;
2280b57cec5SDimitry Andric   for (const auto &AT : Proto->param_types())
2290b57cec5SDimitry Andric     // Size should be aligned to pointer size.
2300b57cec5SDimitry Andric     ArgWords +=
2310b57cec5SDimitry Andric         llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
2320b57cec5SDimitry Andric         TI.getPointerWidth(0);
2330b57cec5SDimitry Andric   Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
2365ffd83dbSDimitry Andric void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &Out) {
2375ffd83dbSDimitry Andric   // For now, follow the MSVC naming convention for GUID objects on all
2385ffd83dbSDimitry Andric   // targets.
2395ffd83dbSDimitry Andric   MSGuidDecl::Parts P = GD->getParts();
2405ffd83dbSDimitry Andric   Out << llvm::format("_GUID_%08" PRIx32 "_%04" PRIx32 "_%04" PRIx32 "_",
2415ffd83dbSDimitry Andric                       P.Part1, P.Part2, P.Part3);
2425ffd83dbSDimitry Andric   unsigned I = 0;
2435ffd83dbSDimitry Andric   for (uint8_t C : P.Part4And5) {
2445ffd83dbSDimitry Andric     Out << llvm::format("%02" PRIx8, C);
2455ffd83dbSDimitry Andric     if (++I == 2)
2465ffd83dbSDimitry Andric       Out << "_";
2475ffd83dbSDimitry Andric   }
2485ffd83dbSDimitry Andric }
2495ffd83dbSDimitry Andric 
2500b57cec5SDimitry Andric void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
2510b57cec5SDimitry Andric                                       const NamedDecl *ID,
2520b57cec5SDimitry Andric                                       raw_ostream &Out) {
2530b57cec5SDimitry Andric   unsigned discriminator = getBlockId(BD, false);
2540b57cec5SDimitry Andric   if (ID) {
2550b57cec5SDimitry Andric     if (shouldMangleDeclName(ID))
2560b57cec5SDimitry Andric       mangleName(ID, Out);
2570b57cec5SDimitry Andric     else {
2580b57cec5SDimitry Andric       Out << ID->getIdentifier()->getName();
2590b57cec5SDimitry Andric     }
2600b57cec5SDimitry Andric   }
2610b57cec5SDimitry Andric   if (discriminator == 0)
2620b57cec5SDimitry Andric     Out << "_block_invoke";
2630b57cec5SDimitry Andric   else
2640b57cec5SDimitry Andric     Out << "_block_invoke_" << discriminator+1;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
2680b57cec5SDimitry Andric                                     CXXCtorType CT, const BlockDecl *BD,
2690b57cec5SDimitry Andric                                     raw_ostream &ResStream) {
2700b57cec5SDimitry Andric   SmallString<64> Buffer;
2710b57cec5SDimitry Andric   llvm::raw_svector_ostream Out(Buffer);
2725ffd83dbSDimitry Andric   mangleName(GlobalDecl(CD, CT), Out);
2730b57cec5SDimitry Andric   mangleFunctionBlock(*this, Buffer, BD, ResStream);
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
2770b57cec5SDimitry Andric                                     CXXDtorType DT, const BlockDecl *BD,
2780b57cec5SDimitry Andric                                     raw_ostream &ResStream) {
2790b57cec5SDimitry Andric   SmallString<64> Buffer;
2800b57cec5SDimitry Andric   llvm::raw_svector_ostream Out(Buffer);
2815ffd83dbSDimitry Andric   mangleName(GlobalDecl(DD, DT), Out);
2820b57cec5SDimitry Andric   mangleFunctionBlock(*this, Buffer, BD, ResStream);
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
2860b57cec5SDimitry Andric                                 raw_ostream &Out) {
2870b57cec5SDimitry Andric   assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   SmallString<64> Buffer;
2900b57cec5SDimitry Andric   llvm::raw_svector_ostream Stream(Buffer);
2910b57cec5SDimitry Andric   if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
292e8d8bef9SDimitry Andric     mangleObjCMethodNameAsSourceName(Method, Stream);
2930b57cec5SDimitry Andric   } else {
2940b57cec5SDimitry Andric     assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
2950b57cec5SDimitry Andric            "expected a NamedDecl or BlockDecl");
2960b57cec5SDimitry Andric     if (isa<BlockDecl>(DC))
2970b57cec5SDimitry Andric       for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
2980b57cec5SDimitry Andric         (void) getBlockId(cast<BlockDecl>(DC), true);
2990b57cec5SDimitry Andric     assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
3000b57cec5SDimitry Andric            "expected a TranslationUnitDecl or a NamedDecl");
3010b57cec5SDimitry Andric     if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
3020b57cec5SDimitry Andric       mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
3030b57cec5SDimitry Andric     else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
3040b57cec5SDimitry Andric       mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
3050b57cec5SDimitry Andric     else if (auto ND = dyn_cast<NamedDecl>(DC)) {
3060b57cec5SDimitry Andric       if (!shouldMangleDeclName(ND) && ND->getIdentifier())
3070b57cec5SDimitry Andric         Stream << ND->getIdentifier()->getName();
3080b57cec5SDimitry Andric       else {
3090b57cec5SDimitry Andric         // FIXME: We were doing a mangleUnqualifiedName() before, but that's
3100b57cec5SDimitry Andric         // a private member of a class that will soon itself be private to the
3110b57cec5SDimitry Andric         // Itanium C++ ABI object. What should we do now? Right now, I'm just
3120b57cec5SDimitry Andric         // calling the mangleName() method on the MangleContext; is there a
3130b57cec5SDimitry Andric         // better way?
3140b57cec5SDimitry Andric         mangleName(ND, Stream);
3150b57cec5SDimitry Andric       }
3160b57cec5SDimitry Andric     }
3170b57cec5SDimitry Andric   }
3180b57cec5SDimitry Andric   mangleFunctionBlock(*this, Buffer, BD, Out);
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
321e8d8bef9SDimitry Andric void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
322e8d8bef9SDimitry Andric                                          raw_ostream &OS,
323e8d8bef9SDimitry Andric                                          bool includePrefixByte,
324e8d8bef9SDimitry Andric                                          bool includeCategoryNamespace) {
325e8d8bef9SDimitry Andric   if (getASTContext().getLangOpts().ObjCRuntime.isGNUFamily()) {
326e8d8bef9SDimitry Andric     // This is the mangling we've always used on the GNU runtimes, but it
327e8d8bef9SDimitry Andric     // has obvious collisions in the face of underscores within class
328e8d8bef9SDimitry Andric     // names, category names, and selectors; maybe we should improve it.
329e8d8bef9SDimitry Andric 
330e8d8bef9SDimitry Andric     OS << (MD->isClassMethod() ? "_c_" : "_i_")
331e8d8bef9SDimitry Andric        << MD->getClassInterface()->getName() << '_';
332e8d8bef9SDimitry Andric 
333e8d8bef9SDimitry Andric     if (includeCategoryNamespace) {
334e8d8bef9SDimitry Andric       if (auto category = MD->getCategory())
335e8d8bef9SDimitry Andric         OS << category->getName();
336e8d8bef9SDimitry Andric     }
337e8d8bef9SDimitry Andric     OS << '_';
338e8d8bef9SDimitry Andric 
339e8d8bef9SDimitry Andric     auto selector = MD->getSelector();
340e8d8bef9SDimitry Andric     for (unsigned slotIndex = 0,
341e8d8bef9SDimitry Andric                   numArgs = selector.getNumArgs(),
342e8d8bef9SDimitry Andric                   slotEnd = std::max(numArgs, 1U);
343e8d8bef9SDimitry Andric            slotIndex != slotEnd; ++slotIndex) {
344e8d8bef9SDimitry Andric       if (auto name = selector.getIdentifierInfoForSlot(slotIndex))
345e8d8bef9SDimitry Andric         OS << name->getName();
346e8d8bef9SDimitry Andric 
347e8d8bef9SDimitry Andric       // Replace all the positions that would've been ':' with '_'.
348e8d8bef9SDimitry Andric       // That's after each slot except that a unary selector doesn't
349e8d8bef9SDimitry Andric       // end in ':'.
350e8d8bef9SDimitry Andric       if (numArgs)
351e8d8bef9SDimitry Andric         OS << '_';
352e8d8bef9SDimitry Andric     }
353e8d8bef9SDimitry Andric 
354e8d8bef9SDimitry Andric     return;
355e8d8bef9SDimitry Andric   }
356e8d8bef9SDimitry Andric 
357e8d8bef9SDimitry Andric   // \01+[ContainerName(CategoryName) SelectorName]
358e8d8bef9SDimitry Andric   if (includePrefixByte) {
359e8d8bef9SDimitry Andric     OS << '\01';
360e8d8bef9SDimitry Andric   }
3610b57cec5SDimitry Andric   OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
362e8d8bef9SDimitry Andric   if (const auto *CID = MD->getCategory()) {
3630b57cec5SDimitry Andric     OS << CID->getClassInterface()->getName();
364e8d8bef9SDimitry Andric     if (includeCategoryNamespace) {
3650b57cec5SDimitry Andric       OS << '(' << *CID << ')';
366e8d8bef9SDimitry Andric     }
367e8d8bef9SDimitry Andric   } else if (const auto *CD =
368e8d8bef9SDimitry Andric                  dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
3690b57cec5SDimitry Andric     OS << CD->getName();
370e8d8bef9SDimitry Andric   } else {
371e8d8bef9SDimitry Andric     llvm_unreachable("Unexpected ObjC method decl context");
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric   OS << ' ';
3740b57cec5SDimitry Andric   MD->getSelector().print(OS);
3750b57cec5SDimitry Andric   OS << ']';
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric 
378e8d8bef9SDimitry Andric void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
3790b57cec5SDimitry Andric                                                      raw_ostream &Out) {
3800b57cec5SDimitry Andric   SmallString<64> Name;
3810b57cec5SDimitry Andric   llvm::raw_svector_ostream OS(Name);
3820b57cec5SDimitry Andric 
383e8d8bef9SDimitry Andric   mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
384e8d8bef9SDimitry Andric                        /*includeCategoryNamespace=*/true);
3850b57cec5SDimitry Andric   Out << OS.str().size() << OS.str();
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric class ASTNameGenerator::Implementation {
3890b57cec5SDimitry Andric   std::unique_ptr<MangleContext> MC;
3900b57cec5SDimitry Andric   llvm::DataLayout DL;
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric public:
3930b57cec5SDimitry Andric   explicit Implementation(ASTContext &Ctx)
394*fe6060f1SDimitry Andric       : MC(Ctx.createMangleContext()),
395*fe6060f1SDimitry Andric         DL(Ctx.getTargetInfo().getDataLayoutString()) {}
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   bool writeName(const Decl *D, raw_ostream &OS) {
3980b57cec5SDimitry Andric     // First apply frontend mangling.
3990b57cec5SDimitry Andric     SmallString<128> FrontendBuf;
4000b57cec5SDimitry Andric     llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
4010b57cec5SDimitry Andric     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
4020b57cec5SDimitry Andric       if (FD->isDependentContext())
4030b57cec5SDimitry Andric         return true;
4040b57cec5SDimitry Andric       if (writeFuncOrVarName(FD, FrontendBufOS))
4050b57cec5SDimitry Andric         return true;
4060b57cec5SDimitry Andric     } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4070b57cec5SDimitry Andric       if (writeFuncOrVarName(VD, FrontendBufOS))
4080b57cec5SDimitry Andric         return true;
4090b57cec5SDimitry Andric     } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
410e8d8bef9SDimitry Andric       MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
411e8d8bef9SDimitry Andric                                /*includeCategoryNamespace=*/true);
4120b57cec5SDimitry Andric       return false;
4130b57cec5SDimitry Andric     } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4140b57cec5SDimitry Andric       writeObjCClassName(ID, FrontendBufOS);
4150b57cec5SDimitry Andric     } else {
4160b57cec5SDimitry Andric       return true;
4170b57cec5SDimitry Andric     }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric     // Now apply backend mangling.
4200b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
4210b57cec5SDimitry Andric     return false;
4220b57cec5SDimitry Andric   }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   std::string getName(const Decl *D) {
4250b57cec5SDimitry Andric     std::string Name;
4260b57cec5SDimitry Andric     {
4270b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Name);
4280b57cec5SDimitry Andric       writeName(D, OS);
4290b57cec5SDimitry Andric     }
4300b57cec5SDimitry Andric     return Name;
4310b57cec5SDimitry Andric   }
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   enum ObjCKind {
4340b57cec5SDimitry Andric     ObjCClass,
4350b57cec5SDimitry Andric     ObjCMetaclass,
4360b57cec5SDimitry Andric   };
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   static StringRef getClassSymbolPrefix(ObjCKind Kind,
4390b57cec5SDimitry Andric                                         const ASTContext &Context) {
4400b57cec5SDimitry Andric     if (Context.getLangOpts().ObjCRuntime.isGNUFamily())
4410b57cec5SDimitry Andric       return Kind == ObjCMetaclass ? "_OBJC_METACLASS_" : "_OBJC_CLASS_";
4420b57cec5SDimitry Andric     return Kind == ObjCMetaclass ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_";
4430b57cec5SDimitry Andric   }
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) {
4460b57cec5SDimitry Andric     StringRef ClassName;
4470b57cec5SDimitry Andric     if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
4480b57cec5SDimitry Andric       ClassName = OID->getObjCRuntimeNameAsString();
4490b57cec5SDimitry Andric     else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
4500b57cec5SDimitry Andric       ClassName = OID->getObjCRuntimeNameAsString();
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric     if (ClassName.empty())
4530b57cec5SDimitry Andric       return {};
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric     auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string {
4560b57cec5SDimitry Andric       SmallString<40> Mangled;
4570b57cec5SDimitry Andric       auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext());
4580b57cec5SDimitry Andric       llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
4595ffd83dbSDimitry Andric       return std::string(Mangled.str());
4600b57cec5SDimitry Andric     };
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric     return {
4630b57cec5SDimitry Andric         Mangle(ObjCClass, ClassName),
4640b57cec5SDimitry Andric         Mangle(ObjCMetaclass, ClassName),
4650b57cec5SDimitry Andric     };
4660b57cec5SDimitry Andric   }
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   std::vector<std::string> getAllManglings(const Decl *D) {
4690b57cec5SDimitry Andric     if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D))
4700b57cec5SDimitry Andric       return getAllManglings(OCD);
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric     if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
4730b57cec5SDimitry Andric       return {};
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric     const NamedDecl *ND = cast<NamedDecl>(D);
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric     ASTContext &Ctx = ND->getASTContext();
4780b57cec5SDimitry Andric     std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric     std::vector<std::string> Manglings;
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric     auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
4830b57cec5SDimitry Andric       auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
4840b57cec5SDimitry Andric                                                      /*IsCXXMethod=*/true);
485a7dea167SDimitry Andric       auto CC = MD->getType()->castAs<FunctionProtoType>()->getCallConv();
4860b57cec5SDimitry Andric       return CC == DefaultCC;
4870b57cec5SDimitry Andric     };
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric     if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
4900b57cec5SDimitry Andric       Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
4930b57cec5SDimitry Andric         if (!CD->getParent()->isAbstract())
4940b57cec5SDimitry Andric           Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete));
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
4970b57cec5SDimitry Andric         if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
4980b57cec5SDimitry Andric           if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
4990b57cec5SDimitry Andric             Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure));
5000b57cec5SDimitry Andric     } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
5010b57cec5SDimitry Andric       Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
5020b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
5030b57cec5SDimitry Andric         Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
5040b57cec5SDimitry Andric         if (DD->isVirtual())
5050b57cec5SDimitry Andric           Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
5060b57cec5SDimitry Andric       }
5070b57cec5SDimitry Andric     } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
5080b57cec5SDimitry Andric       Manglings.emplace_back(getName(ND));
5090b57cec5SDimitry Andric       if (MD->isVirtual())
5100b57cec5SDimitry Andric         if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
5110b57cec5SDimitry Andric           for (const auto &T : *TIV)
5120b57cec5SDimitry Andric             Manglings.emplace_back(getMangledThunk(MD, T));
5130b57cec5SDimitry Andric     }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric     return Manglings;
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric private:
5190b57cec5SDimitry Andric   bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) {
5200b57cec5SDimitry Andric     if (MC->shouldMangleDeclName(D)) {
5215ffd83dbSDimitry Andric       GlobalDecl GD;
5220b57cec5SDimitry Andric       if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
5235ffd83dbSDimitry Andric         GD = GlobalDecl(CtorD, Ctor_Complete);
5240b57cec5SDimitry Andric       else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
5255ffd83dbSDimitry Andric         GD = GlobalDecl(DtorD, Dtor_Complete);
5265ffd83dbSDimitry Andric       else if (D->hasAttr<CUDAGlobalAttr>())
5275ffd83dbSDimitry Andric         GD = GlobalDecl(cast<FunctionDecl>(D));
5280b57cec5SDimitry Andric       else
5295ffd83dbSDimitry Andric         GD = GlobalDecl(D);
5305ffd83dbSDimitry Andric       MC->mangleName(GD, OS);
5310b57cec5SDimitry Andric       return false;
5320b57cec5SDimitry Andric     } else {
5330b57cec5SDimitry Andric       IdentifierInfo *II = D->getIdentifier();
5340b57cec5SDimitry Andric       if (!II)
5350b57cec5SDimitry Andric         return true;
5360b57cec5SDimitry Andric       OS << II->getName();
5370b57cec5SDimitry Andric       return false;
5380b57cec5SDimitry Andric     }
5390b57cec5SDimitry Andric   }
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric   void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) {
5420b57cec5SDimitry Andric     OS << getClassSymbolPrefix(ObjCClass, D->getASTContext());
5430b57cec5SDimitry Andric     OS << D->getObjCRuntimeNameAsString();
5440b57cec5SDimitry Andric   }
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric   std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) {
5470b57cec5SDimitry Andric     std::string FrontendBuf;
5480b57cec5SDimitry Andric     llvm::raw_string_ostream FOS(FrontendBuf);
5490b57cec5SDimitry Andric 
5505ffd83dbSDimitry Andric     GlobalDecl GD;
5510b57cec5SDimitry Andric     if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
5525ffd83dbSDimitry Andric       GD = GlobalDecl(CD, static_cast<CXXCtorType>(StructorType));
5530b57cec5SDimitry Andric     else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
5545ffd83dbSDimitry Andric       GD = GlobalDecl(DD, static_cast<CXXDtorType>(StructorType));
5555ffd83dbSDimitry Andric     MC->mangleName(GD, FOS);
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric     std::string BackendBuf;
5580b57cec5SDimitry Andric     llvm::raw_string_ostream BOS(BackendBuf);
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric     return BOS.str();
5630b57cec5SDimitry Andric   }
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) {
5660b57cec5SDimitry Andric     std::string FrontendBuf;
5670b57cec5SDimitry Andric     llvm::raw_string_ostream FOS(FrontendBuf);
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric     MC->mangleThunk(MD, T, FOS);
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric     std::string BackendBuf;
5720b57cec5SDimitry Andric     llvm::raw_string_ostream BOS(BackendBuf);
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric     return BOS.str();
5770b57cec5SDimitry Andric   }
5780b57cec5SDimitry Andric };
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric ASTNameGenerator::ASTNameGenerator(ASTContext &Ctx)
581a7dea167SDimitry Andric     : Impl(std::make_unique<Implementation>(Ctx)) {}
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric ASTNameGenerator::~ASTNameGenerator() {}
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric bool ASTNameGenerator::writeName(const Decl *D, raw_ostream &OS) {
5860b57cec5SDimitry Andric   return Impl->writeName(D, OS);
5870b57cec5SDimitry Andric }
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric std::string ASTNameGenerator::getName(const Decl *D) {
5900b57cec5SDimitry Andric   return Impl->getName(D);
5910b57cec5SDimitry Andric }
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric std::vector<std::string> ASTNameGenerator::getAllManglings(const Decl *D) {
5940b57cec5SDimitry Andric   return Impl->getAllManglings(D);
5950b57cec5SDimitry Andric }
596