xref: /freebsd/contrib/llvm-project/clang/lib/AST/Mangle.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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 
1190b57cec5SDimitry Andric   // In C, functions with no attributes never need to be mangled. Fastpath them.
1200b57cec5SDimitry Andric   if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
1210b57cec5SDimitry Andric     return false;
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   // Any decl can be declared with __asm("foo") on it, and this takes precedence
1240b57cec5SDimitry Andric   // over all other naming in the .o file.
1250b57cec5SDimitry Andric   if (D->hasAttr<AsmLabelAttr>())
1260b57cec5SDimitry Andric     return true;
1270b57cec5SDimitry Andric 
1285ffd83dbSDimitry Andric   // Declarations that don't have identifier names always need to be mangled.
1295ffd83dbSDimitry Andric   if (isa<MSGuidDecl>(D))
1305ffd83dbSDimitry Andric     return true;
1315ffd83dbSDimitry Andric 
1320b57cec5SDimitry Andric   return shouldMangleCXXName(D);
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1355ffd83dbSDimitry Andric void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
1365ffd83dbSDimitry Andric   const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
1370b57cec5SDimitry Andric   // Any decl can be declared with __asm("foo") on it, and this takes precedence
1380b57cec5SDimitry Andric   // over all other naming in the .o file.
1390b57cec5SDimitry Andric   if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
1400b57cec5SDimitry Andric     // If we have an asm name, then we use it as the mangling.
1410b57cec5SDimitry Andric 
142a7dea167SDimitry Andric     // If the label isn't literal, or if this is an alias for an LLVM intrinsic,
143a7dea167SDimitry Andric     // do not add a "\01" prefix.
144a7dea167SDimitry Andric     if (!ALA->getIsLiteralLabel() || ALA->getLabel().startswith("llvm.")) {
145a7dea167SDimitry Andric       Out << ALA->getLabel();
146a7dea167SDimitry Andric       return;
147a7dea167SDimitry Andric     }
148a7dea167SDimitry Andric 
1490b57cec5SDimitry Andric     // Adding the prefix can cause problems when one file has a "foo" and
1500b57cec5SDimitry Andric     // another has a "\01foo". That is known to happen on ELF with the
1510b57cec5SDimitry Andric     // tricks normally used for producing aliases (PR9177). Fortunately the
1520b57cec5SDimitry Andric     // llvm mangler on ELF is a nop, so we can just avoid adding the \01
153a7dea167SDimitry Andric     // marker.
1540b57cec5SDimitry Andric     char GlobalPrefix =
1550b57cec5SDimitry Andric         getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix();
156a7dea167SDimitry Andric     if (GlobalPrefix)
1570b57cec5SDimitry Andric       Out << '\01'; // LLVM IR Marker for __asm("foo")
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric     Out << ALA->getLabel();
1600b57cec5SDimitry Andric     return;
1610b57cec5SDimitry Andric   }
1620b57cec5SDimitry Andric 
1635ffd83dbSDimitry Andric   if (auto *GD = dyn_cast<MSGuidDecl>(D))
1645ffd83dbSDimitry Andric     return mangleMSGuidDecl(GD, Out);
1655ffd83dbSDimitry Andric 
1660b57cec5SDimitry Andric   const ASTContext &ASTContext = getASTContext();
1670b57cec5SDimitry Andric   CCMangling CC = getCallingConvMangling(ASTContext, D);
1685ffd83dbSDimitry Andric 
1695ffd83dbSDimitry Andric   if (CC == CCM_WasmMainArgcArgv) {
1705ffd83dbSDimitry Andric     Out << "__main_argc_argv";
1715ffd83dbSDimitry Andric     return;
1725ffd83dbSDimitry Andric   }
1735ffd83dbSDimitry Andric 
1740b57cec5SDimitry Andric   bool MCXX = shouldMangleCXXName(D);
1750b57cec5SDimitry Andric   const TargetInfo &TI = Context.getTargetInfo();
1760b57cec5SDimitry Andric   if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
1770b57cec5SDimitry Andric     if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
178*e8d8bef9SDimitry Andric       mangleObjCMethodNameAsSourceName(OMD, Out);
1790b57cec5SDimitry Andric     else
1805ffd83dbSDimitry Andric       mangleCXXName(GD, Out);
1810b57cec5SDimitry Andric     return;
1820b57cec5SDimitry Andric   }
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   Out << '\01';
1850b57cec5SDimitry Andric   if (CC == CCM_Std)
1860b57cec5SDimitry Andric     Out << '_';
1870b57cec5SDimitry Andric   else if (CC == CCM_Fast)
1880b57cec5SDimitry Andric     Out << '@';
1890b57cec5SDimitry Andric   else if (CC == CCM_RegCall)
1900b57cec5SDimitry Andric     Out << "__regcall3__";
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   if (!MCXX)
1930b57cec5SDimitry Andric     Out << D->getIdentifier()->getName();
1940b57cec5SDimitry Andric   else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
195*e8d8bef9SDimitry Andric     mangleObjCMethodNameAsSourceName(OMD, Out);
1960b57cec5SDimitry Andric   else
1975ffd83dbSDimitry Andric     mangleCXXName(GD, Out);
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   const FunctionDecl *FD = cast<FunctionDecl>(D);
2000b57cec5SDimitry Andric   const FunctionType *FT = FD->getType()->castAs<FunctionType>();
2010b57cec5SDimitry Andric   const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
2020b57cec5SDimitry Andric   if (CC == CCM_Vector)
2030b57cec5SDimitry Andric     Out << '@';
2040b57cec5SDimitry Andric   Out << '@';
2050b57cec5SDimitry Andric   if (!Proto) {
2060b57cec5SDimitry Andric     Out << '0';
2070b57cec5SDimitry Andric     return;
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric   assert(!Proto->isVariadic());
2100b57cec5SDimitry Andric   unsigned ArgWords = 0;
2110b57cec5SDimitry Andric   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
2120b57cec5SDimitry Andric     if (!MD->isStatic())
2130b57cec5SDimitry Andric       ++ArgWords;
2140b57cec5SDimitry Andric   for (const auto &AT : Proto->param_types())
2150b57cec5SDimitry Andric     // Size should be aligned to pointer size.
2160b57cec5SDimitry Andric     ArgWords +=
2170b57cec5SDimitry Andric         llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
2180b57cec5SDimitry Andric         TI.getPointerWidth(0);
2190b57cec5SDimitry Andric   Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric 
2225ffd83dbSDimitry Andric void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &Out) {
2235ffd83dbSDimitry Andric   // For now, follow the MSVC naming convention for GUID objects on all
2245ffd83dbSDimitry Andric   // targets.
2255ffd83dbSDimitry Andric   MSGuidDecl::Parts P = GD->getParts();
2265ffd83dbSDimitry Andric   Out << llvm::format("_GUID_%08" PRIx32 "_%04" PRIx32 "_%04" PRIx32 "_",
2275ffd83dbSDimitry Andric                       P.Part1, P.Part2, P.Part3);
2285ffd83dbSDimitry Andric   unsigned I = 0;
2295ffd83dbSDimitry Andric   for (uint8_t C : P.Part4And5) {
2305ffd83dbSDimitry Andric     Out << llvm::format("%02" PRIx8, C);
2315ffd83dbSDimitry Andric     if (++I == 2)
2325ffd83dbSDimitry Andric       Out << "_";
2335ffd83dbSDimitry Andric   }
2345ffd83dbSDimitry Andric }
2355ffd83dbSDimitry Andric 
2360b57cec5SDimitry Andric void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
2370b57cec5SDimitry Andric                                       const NamedDecl *ID,
2380b57cec5SDimitry Andric                                       raw_ostream &Out) {
2390b57cec5SDimitry Andric   unsigned discriminator = getBlockId(BD, false);
2400b57cec5SDimitry Andric   if (ID) {
2410b57cec5SDimitry Andric     if (shouldMangleDeclName(ID))
2420b57cec5SDimitry Andric       mangleName(ID, Out);
2430b57cec5SDimitry Andric     else {
2440b57cec5SDimitry Andric       Out << ID->getIdentifier()->getName();
2450b57cec5SDimitry Andric     }
2460b57cec5SDimitry Andric   }
2470b57cec5SDimitry Andric   if (discriminator == 0)
2480b57cec5SDimitry Andric     Out << "_block_invoke";
2490b57cec5SDimitry Andric   else
2500b57cec5SDimitry Andric     Out << "_block_invoke_" << discriminator+1;
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
2540b57cec5SDimitry Andric                                     CXXCtorType CT, const BlockDecl *BD,
2550b57cec5SDimitry Andric                                     raw_ostream &ResStream) {
2560b57cec5SDimitry Andric   SmallString<64> Buffer;
2570b57cec5SDimitry Andric   llvm::raw_svector_ostream Out(Buffer);
2585ffd83dbSDimitry Andric   mangleName(GlobalDecl(CD, CT), Out);
2590b57cec5SDimitry Andric   mangleFunctionBlock(*this, Buffer, BD, ResStream);
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
2630b57cec5SDimitry Andric                                     CXXDtorType DT, const BlockDecl *BD,
2640b57cec5SDimitry Andric                                     raw_ostream &ResStream) {
2650b57cec5SDimitry Andric   SmallString<64> Buffer;
2660b57cec5SDimitry Andric   llvm::raw_svector_ostream Out(Buffer);
2675ffd83dbSDimitry Andric   mangleName(GlobalDecl(DD, DT), Out);
2680b57cec5SDimitry Andric   mangleFunctionBlock(*this, Buffer, BD, ResStream);
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
2720b57cec5SDimitry Andric                                 raw_ostream &Out) {
2730b57cec5SDimitry Andric   assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   SmallString<64> Buffer;
2760b57cec5SDimitry Andric   llvm::raw_svector_ostream Stream(Buffer);
2770b57cec5SDimitry Andric   if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
278*e8d8bef9SDimitry Andric     mangleObjCMethodNameAsSourceName(Method, Stream);
2790b57cec5SDimitry Andric   } else {
2800b57cec5SDimitry Andric     assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
2810b57cec5SDimitry Andric            "expected a NamedDecl or BlockDecl");
2820b57cec5SDimitry Andric     if (isa<BlockDecl>(DC))
2830b57cec5SDimitry Andric       for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
2840b57cec5SDimitry Andric         (void) getBlockId(cast<BlockDecl>(DC), true);
2850b57cec5SDimitry Andric     assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
2860b57cec5SDimitry Andric            "expected a TranslationUnitDecl or a NamedDecl");
2870b57cec5SDimitry Andric     if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
2880b57cec5SDimitry Andric       mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
2890b57cec5SDimitry Andric     else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
2900b57cec5SDimitry Andric       mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
2910b57cec5SDimitry Andric     else if (auto ND = dyn_cast<NamedDecl>(DC)) {
2920b57cec5SDimitry Andric       if (!shouldMangleDeclName(ND) && ND->getIdentifier())
2930b57cec5SDimitry Andric         Stream << ND->getIdentifier()->getName();
2940b57cec5SDimitry Andric       else {
2950b57cec5SDimitry Andric         // FIXME: We were doing a mangleUnqualifiedName() before, but that's
2960b57cec5SDimitry Andric         // a private member of a class that will soon itself be private to the
2970b57cec5SDimitry Andric         // Itanium C++ ABI object. What should we do now? Right now, I'm just
2980b57cec5SDimitry Andric         // calling the mangleName() method on the MangleContext; is there a
2990b57cec5SDimitry Andric         // better way?
3000b57cec5SDimitry Andric         mangleName(ND, Stream);
3010b57cec5SDimitry Andric       }
3020b57cec5SDimitry Andric     }
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric   mangleFunctionBlock(*this, Buffer, BD, Out);
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric 
307*e8d8bef9SDimitry Andric void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
308*e8d8bef9SDimitry Andric                                          raw_ostream &OS,
309*e8d8bef9SDimitry Andric                                          bool includePrefixByte,
310*e8d8bef9SDimitry Andric                                          bool includeCategoryNamespace) {
311*e8d8bef9SDimitry Andric   if (getASTContext().getLangOpts().ObjCRuntime.isGNUFamily()) {
312*e8d8bef9SDimitry Andric     // This is the mangling we've always used on the GNU runtimes, but it
313*e8d8bef9SDimitry Andric     // has obvious collisions in the face of underscores within class
314*e8d8bef9SDimitry Andric     // names, category names, and selectors; maybe we should improve it.
315*e8d8bef9SDimitry Andric 
316*e8d8bef9SDimitry Andric     OS << (MD->isClassMethod() ? "_c_" : "_i_")
317*e8d8bef9SDimitry Andric        << MD->getClassInterface()->getName() << '_';
318*e8d8bef9SDimitry Andric 
319*e8d8bef9SDimitry Andric     if (includeCategoryNamespace) {
320*e8d8bef9SDimitry Andric       if (auto category = MD->getCategory())
321*e8d8bef9SDimitry Andric         OS << category->getName();
322*e8d8bef9SDimitry Andric     }
323*e8d8bef9SDimitry Andric     OS << '_';
324*e8d8bef9SDimitry Andric 
325*e8d8bef9SDimitry Andric     auto selector = MD->getSelector();
326*e8d8bef9SDimitry Andric     for (unsigned slotIndex = 0,
327*e8d8bef9SDimitry Andric                   numArgs = selector.getNumArgs(),
328*e8d8bef9SDimitry Andric                   slotEnd = std::max(numArgs, 1U);
329*e8d8bef9SDimitry Andric            slotIndex != slotEnd; ++slotIndex) {
330*e8d8bef9SDimitry Andric       if (auto name = selector.getIdentifierInfoForSlot(slotIndex))
331*e8d8bef9SDimitry Andric         OS << name->getName();
332*e8d8bef9SDimitry Andric 
333*e8d8bef9SDimitry Andric       // Replace all the positions that would've been ':' with '_'.
334*e8d8bef9SDimitry Andric       // That's after each slot except that a unary selector doesn't
335*e8d8bef9SDimitry Andric       // end in ':'.
336*e8d8bef9SDimitry Andric       if (numArgs)
337*e8d8bef9SDimitry Andric         OS << '_';
338*e8d8bef9SDimitry Andric     }
339*e8d8bef9SDimitry Andric 
340*e8d8bef9SDimitry Andric     return;
341*e8d8bef9SDimitry Andric   }
342*e8d8bef9SDimitry Andric 
343*e8d8bef9SDimitry Andric   // \01+[ContainerName(CategoryName) SelectorName]
344*e8d8bef9SDimitry Andric   if (includePrefixByte) {
345*e8d8bef9SDimitry Andric     OS << '\01';
346*e8d8bef9SDimitry Andric   }
3470b57cec5SDimitry Andric   OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
348*e8d8bef9SDimitry Andric   if (const auto *CID = MD->getCategory()) {
3490b57cec5SDimitry Andric     OS << CID->getClassInterface()->getName();
350*e8d8bef9SDimitry Andric     if (includeCategoryNamespace) {
3510b57cec5SDimitry Andric       OS << '(' << *CID << ')';
352*e8d8bef9SDimitry Andric     }
353*e8d8bef9SDimitry Andric   } else if (const auto *CD =
354*e8d8bef9SDimitry Andric                  dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
3550b57cec5SDimitry Andric     OS << CD->getName();
356*e8d8bef9SDimitry Andric   } else {
357*e8d8bef9SDimitry Andric     llvm_unreachable("Unexpected ObjC method decl context");
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric   OS << ' ';
3600b57cec5SDimitry Andric   MD->getSelector().print(OS);
3610b57cec5SDimitry Andric   OS << ']';
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric 
364*e8d8bef9SDimitry Andric void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
3650b57cec5SDimitry Andric                                                      raw_ostream &Out) {
3660b57cec5SDimitry Andric   SmallString<64> Name;
3670b57cec5SDimitry Andric   llvm::raw_svector_ostream OS(Name);
3680b57cec5SDimitry Andric 
369*e8d8bef9SDimitry Andric   mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
370*e8d8bef9SDimitry Andric                        /*includeCategoryNamespace=*/true);
3710b57cec5SDimitry Andric   Out << OS.str().size() << OS.str();
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric class ASTNameGenerator::Implementation {
3750b57cec5SDimitry Andric   std::unique_ptr<MangleContext> MC;
3760b57cec5SDimitry Andric   llvm::DataLayout DL;
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric public:
3790b57cec5SDimitry Andric   explicit Implementation(ASTContext &Ctx)
3800b57cec5SDimitry Andric       : MC(Ctx.createMangleContext()), DL(Ctx.getTargetInfo().getDataLayout()) {
3810b57cec5SDimitry Andric   }
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   bool writeName(const Decl *D, raw_ostream &OS) {
3840b57cec5SDimitry Andric     // First apply frontend mangling.
3850b57cec5SDimitry Andric     SmallString<128> FrontendBuf;
3860b57cec5SDimitry Andric     llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
3870b57cec5SDimitry Andric     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
3880b57cec5SDimitry Andric       if (FD->isDependentContext())
3890b57cec5SDimitry Andric         return true;
3900b57cec5SDimitry Andric       if (writeFuncOrVarName(FD, FrontendBufOS))
3910b57cec5SDimitry Andric         return true;
3920b57cec5SDimitry Andric     } else if (auto *VD = dyn_cast<VarDecl>(D)) {
3930b57cec5SDimitry Andric       if (writeFuncOrVarName(VD, FrontendBufOS))
3940b57cec5SDimitry Andric         return true;
3950b57cec5SDimitry Andric     } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
396*e8d8bef9SDimitry Andric       MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
397*e8d8bef9SDimitry Andric                                /*includeCategoryNamespace=*/true);
3980b57cec5SDimitry Andric       return false;
3990b57cec5SDimitry Andric     } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4000b57cec5SDimitry Andric       writeObjCClassName(ID, FrontendBufOS);
4010b57cec5SDimitry Andric     } else {
4020b57cec5SDimitry Andric       return true;
4030b57cec5SDimitry Andric     }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric     // Now apply backend mangling.
4060b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
4070b57cec5SDimitry Andric     return false;
4080b57cec5SDimitry Andric   }
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric   std::string getName(const Decl *D) {
4110b57cec5SDimitry Andric     std::string Name;
4120b57cec5SDimitry Andric     {
4130b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Name);
4140b57cec5SDimitry Andric       writeName(D, OS);
4150b57cec5SDimitry Andric     }
4160b57cec5SDimitry Andric     return Name;
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   enum ObjCKind {
4200b57cec5SDimitry Andric     ObjCClass,
4210b57cec5SDimitry Andric     ObjCMetaclass,
4220b57cec5SDimitry Andric   };
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   static StringRef getClassSymbolPrefix(ObjCKind Kind,
4250b57cec5SDimitry Andric                                         const ASTContext &Context) {
4260b57cec5SDimitry Andric     if (Context.getLangOpts().ObjCRuntime.isGNUFamily())
4270b57cec5SDimitry Andric       return Kind == ObjCMetaclass ? "_OBJC_METACLASS_" : "_OBJC_CLASS_";
4280b57cec5SDimitry Andric     return Kind == ObjCMetaclass ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_";
4290b57cec5SDimitry Andric   }
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) {
4320b57cec5SDimitry Andric     StringRef ClassName;
4330b57cec5SDimitry Andric     if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
4340b57cec5SDimitry Andric       ClassName = OID->getObjCRuntimeNameAsString();
4350b57cec5SDimitry Andric     else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
4360b57cec5SDimitry Andric       ClassName = OID->getObjCRuntimeNameAsString();
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric     if (ClassName.empty())
4390b57cec5SDimitry Andric       return {};
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric     auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string {
4420b57cec5SDimitry Andric       SmallString<40> Mangled;
4430b57cec5SDimitry Andric       auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext());
4440b57cec5SDimitry Andric       llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
4455ffd83dbSDimitry Andric       return std::string(Mangled.str());
4460b57cec5SDimitry Andric     };
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric     return {
4490b57cec5SDimitry Andric         Mangle(ObjCClass, ClassName),
4500b57cec5SDimitry Andric         Mangle(ObjCMetaclass, ClassName),
4510b57cec5SDimitry Andric     };
4520b57cec5SDimitry Andric   }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric   std::vector<std::string> getAllManglings(const Decl *D) {
4550b57cec5SDimitry Andric     if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D))
4560b57cec5SDimitry Andric       return getAllManglings(OCD);
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric     if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
4590b57cec5SDimitry Andric       return {};
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric     const NamedDecl *ND = cast<NamedDecl>(D);
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric     ASTContext &Ctx = ND->getASTContext();
4640b57cec5SDimitry Andric     std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric     std::vector<std::string> Manglings;
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric     auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
4690b57cec5SDimitry Andric       auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
4700b57cec5SDimitry Andric                                                      /*IsCXXMethod=*/true);
471a7dea167SDimitry Andric       auto CC = MD->getType()->castAs<FunctionProtoType>()->getCallConv();
4720b57cec5SDimitry Andric       return CC == DefaultCC;
4730b57cec5SDimitry Andric     };
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric     if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
4760b57cec5SDimitry Andric       Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
4790b57cec5SDimitry Andric         if (!CD->getParent()->isAbstract())
4800b57cec5SDimitry Andric           Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete));
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
4830b57cec5SDimitry Andric         if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
4840b57cec5SDimitry Andric           if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
4850b57cec5SDimitry Andric             Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure));
4860b57cec5SDimitry Andric     } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
4870b57cec5SDimitry Andric       Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
4880b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
4890b57cec5SDimitry Andric         Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
4900b57cec5SDimitry Andric         if (DD->isVirtual())
4910b57cec5SDimitry Andric           Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
4920b57cec5SDimitry Andric       }
4930b57cec5SDimitry Andric     } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
4940b57cec5SDimitry Andric       Manglings.emplace_back(getName(ND));
4950b57cec5SDimitry Andric       if (MD->isVirtual())
4960b57cec5SDimitry Andric         if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
4970b57cec5SDimitry Andric           for (const auto &T : *TIV)
4980b57cec5SDimitry Andric             Manglings.emplace_back(getMangledThunk(MD, T));
4990b57cec5SDimitry Andric     }
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric     return Manglings;
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric private:
5050b57cec5SDimitry Andric   bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) {
5060b57cec5SDimitry Andric     if (MC->shouldMangleDeclName(D)) {
5075ffd83dbSDimitry Andric       GlobalDecl GD;
5080b57cec5SDimitry Andric       if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
5095ffd83dbSDimitry Andric         GD = GlobalDecl(CtorD, Ctor_Complete);
5100b57cec5SDimitry Andric       else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
5115ffd83dbSDimitry Andric         GD = GlobalDecl(DtorD, Dtor_Complete);
5125ffd83dbSDimitry Andric       else if (D->hasAttr<CUDAGlobalAttr>())
5135ffd83dbSDimitry Andric         GD = GlobalDecl(cast<FunctionDecl>(D));
5140b57cec5SDimitry Andric       else
5155ffd83dbSDimitry Andric         GD = GlobalDecl(D);
5165ffd83dbSDimitry Andric       MC->mangleName(GD, OS);
5170b57cec5SDimitry Andric       return false;
5180b57cec5SDimitry Andric     } else {
5190b57cec5SDimitry Andric       IdentifierInfo *II = D->getIdentifier();
5200b57cec5SDimitry Andric       if (!II)
5210b57cec5SDimitry Andric         return true;
5220b57cec5SDimitry Andric       OS << II->getName();
5230b57cec5SDimitry Andric       return false;
5240b57cec5SDimitry Andric     }
5250b57cec5SDimitry Andric   }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) {
5280b57cec5SDimitry Andric     OS << getClassSymbolPrefix(ObjCClass, D->getASTContext());
5290b57cec5SDimitry Andric     OS << D->getObjCRuntimeNameAsString();
5300b57cec5SDimitry Andric   }
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric   std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) {
5330b57cec5SDimitry Andric     std::string FrontendBuf;
5340b57cec5SDimitry Andric     llvm::raw_string_ostream FOS(FrontendBuf);
5350b57cec5SDimitry Andric 
5365ffd83dbSDimitry Andric     GlobalDecl GD;
5370b57cec5SDimitry Andric     if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
5385ffd83dbSDimitry Andric       GD = GlobalDecl(CD, static_cast<CXXCtorType>(StructorType));
5390b57cec5SDimitry Andric     else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
5405ffd83dbSDimitry Andric       GD = GlobalDecl(DD, static_cast<CXXDtorType>(StructorType));
5415ffd83dbSDimitry Andric     MC->mangleName(GD, FOS);
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric     std::string BackendBuf;
5440b57cec5SDimitry Andric     llvm::raw_string_ostream BOS(BackendBuf);
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric     return BOS.str();
5490b57cec5SDimitry Andric   }
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric   std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) {
5520b57cec5SDimitry Andric     std::string FrontendBuf;
5530b57cec5SDimitry Andric     llvm::raw_string_ostream FOS(FrontendBuf);
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric     MC->mangleThunk(MD, T, 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 
5660b57cec5SDimitry Andric ASTNameGenerator::ASTNameGenerator(ASTContext &Ctx)
567a7dea167SDimitry Andric     : Impl(std::make_unique<Implementation>(Ctx)) {}
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric ASTNameGenerator::~ASTNameGenerator() {}
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric bool ASTNameGenerator::writeName(const Decl *D, raw_ostream &OS) {
5720b57cec5SDimitry Andric   return Impl->writeName(D, OS);
5730b57cec5SDimitry Andric }
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric std::string ASTNameGenerator::getName(const Decl *D) {
5760b57cec5SDimitry Andric   return Impl->getName(D);
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric std::vector<std::string> ASTNameGenerator::getAllManglings(const Decl *D) {
5800b57cec5SDimitry Andric   return Impl->getAllManglings(D);
5810b57cec5SDimitry Andric }
582