xref: /freebsd/contrib/llvm-project/clang/lib/AST/Mangle.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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"
28*5ffd83dbSDimitry 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,
54*5ffd83dbSDimitry Andric   CCM_Std,
55*5ffd83dbSDimitry 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();
61*5ffd83dbSDimitry Andric   if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
62*5ffd83dbSDimitry Andric     return VD->isExternC();
63*5ffd83dbSDimitry 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();
70*5ffd83dbSDimitry Andric 
71*5ffd83dbSDimitry Andric   // On wasm, the argc/argv form of "main" is renamed so that the startup code
72*5ffd83dbSDimitry Andric   // can call it with the correct function signature.
73*5ffd83dbSDimitry Andric   // On Emscripten, users may be exporting "main" and expecting to call it
74*5ffd83dbSDimitry Andric   // themselves, so we can't mangle it.
75*5ffd83dbSDimitry Andric   if (Triple.isWasm() && !Triple.isOSEmscripten())
76*5ffd83dbSDimitry Andric     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
77*5ffd83dbSDimitry Andric       if (FD->isMain() && FD->hasPrototype() && FD->param_size() == 2)
78*5ffd83dbSDimitry Andric         return CCM_WasmMainArgcArgv;
79*5ffd83dbSDimitry 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 
128*5ffd83dbSDimitry Andric   // Declarations that don't have identifier names always need to be mangled.
129*5ffd83dbSDimitry Andric   if (isa<MSGuidDecl>(D))
130*5ffd83dbSDimitry Andric     return true;
131*5ffd83dbSDimitry Andric 
1320b57cec5SDimitry Andric   return shouldMangleCXXName(D);
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
135*5ffd83dbSDimitry Andric void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
136*5ffd83dbSDimitry 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 
163*5ffd83dbSDimitry Andric   if (auto *GD = dyn_cast<MSGuidDecl>(D))
164*5ffd83dbSDimitry Andric     return mangleMSGuidDecl(GD, Out);
165*5ffd83dbSDimitry Andric 
1660b57cec5SDimitry Andric   const ASTContext &ASTContext = getASTContext();
1670b57cec5SDimitry Andric   CCMangling CC = getCallingConvMangling(ASTContext, D);
168*5ffd83dbSDimitry Andric 
169*5ffd83dbSDimitry Andric   if (CC == CCM_WasmMainArgcArgv) {
170*5ffd83dbSDimitry Andric     Out << "__main_argc_argv";
171*5ffd83dbSDimitry Andric     return;
172*5ffd83dbSDimitry Andric   }
173*5ffd83dbSDimitry 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))
1780b57cec5SDimitry Andric       mangleObjCMethodName(OMD, Out);
1790b57cec5SDimitry Andric     else
180*5ffd83dbSDimitry 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))
1950b57cec5SDimitry Andric     mangleObjCMethodName(OMD, Out);
1960b57cec5SDimitry Andric   else
197*5ffd83dbSDimitry 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 
222*5ffd83dbSDimitry Andric void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &Out) {
223*5ffd83dbSDimitry Andric   // For now, follow the MSVC naming convention for GUID objects on all
224*5ffd83dbSDimitry Andric   // targets.
225*5ffd83dbSDimitry Andric   MSGuidDecl::Parts P = GD->getParts();
226*5ffd83dbSDimitry Andric   Out << llvm::format("_GUID_%08" PRIx32 "_%04" PRIx32 "_%04" PRIx32 "_",
227*5ffd83dbSDimitry Andric                       P.Part1, P.Part2, P.Part3);
228*5ffd83dbSDimitry Andric   unsigned I = 0;
229*5ffd83dbSDimitry Andric   for (uint8_t C : P.Part4And5) {
230*5ffd83dbSDimitry Andric     Out << llvm::format("%02" PRIx8, C);
231*5ffd83dbSDimitry Andric     if (++I == 2)
232*5ffd83dbSDimitry Andric       Out << "_";
233*5ffd83dbSDimitry Andric   }
234*5ffd83dbSDimitry Andric }
235*5ffd83dbSDimitry 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);
258*5ffd83dbSDimitry 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);
267*5ffd83dbSDimitry 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)) {
2780b57cec5SDimitry Andric     mangleObjCMethodName(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 
3070b57cec5SDimitry Andric void MangleContext::mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD,
3080b57cec5SDimitry Andric                                                     raw_ostream &OS) {
3090b57cec5SDimitry Andric   const ObjCContainerDecl *CD =
3100b57cec5SDimitry Andric   dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
3110b57cec5SDimitry Andric   assert (CD && "Missing container decl in GetNameForMethod");
3120b57cec5SDimitry Andric   OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
3130b57cec5SDimitry Andric   if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
3140b57cec5SDimitry Andric     OS << CID->getClassInterface()->getName();
3150b57cec5SDimitry Andric     OS << '(' << *CID << ')';
3160b57cec5SDimitry Andric   } else {
3170b57cec5SDimitry Andric     OS << CD->getName();
3180b57cec5SDimitry Andric   }
3190b57cec5SDimitry Andric   OS << ' ';
3200b57cec5SDimitry Andric   MD->getSelector().print(OS);
3210b57cec5SDimitry Andric   OS << ']';
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
3250b57cec5SDimitry Andric                                          raw_ostream &Out) {
3260b57cec5SDimitry Andric   SmallString<64> Name;
3270b57cec5SDimitry Andric   llvm::raw_svector_ostream OS(Name);
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   mangleObjCMethodNameWithoutSize(MD, OS);
3300b57cec5SDimitry Andric   Out << OS.str().size() << OS.str();
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric class ASTNameGenerator::Implementation {
3340b57cec5SDimitry Andric   std::unique_ptr<MangleContext> MC;
3350b57cec5SDimitry Andric   llvm::DataLayout DL;
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric public:
3380b57cec5SDimitry Andric   explicit Implementation(ASTContext &Ctx)
3390b57cec5SDimitry Andric       : MC(Ctx.createMangleContext()), DL(Ctx.getTargetInfo().getDataLayout()) {
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   bool writeName(const Decl *D, raw_ostream &OS) {
3430b57cec5SDimitry Andric     // First apply frontend mangling.
3440b57cec5SDimitry Andric     SmallString<128> FrontendBuf;
3450b57cec5SDimitry Andric     llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
3460b57cec5SDimitry Andric     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
3470b57cec5SDimitry Andric       if (FD->isDependentContext())
3480b57cec5SDimitry Andric         return true;
3490b57cec5SDimitry Andric       if (writeFuncOrVarName(FD, FrontendBufOS))
3500b57cec5SDimitry Andric         return true;
3510b57cec5SDimitry Andric     } else if (auto *VD = dyn_cast<VarDecl>(D)) {
3520b57cec5SDimitry Andric       if (writeFuncOrVarName(VD, FrontendBufOS))
3530b57cec5SDimitry Andric         return true;
3540b57cec5SDimitry Andric     } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
3550b57cec5SDimitry Andric       MC->mangleObjCMethodNameWithoutSize(MD, OS);
3560b57cec5SDimitry Andric       return false;
3570b57cec5SDimitry Andric     } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
3580b57cec5SDimitry Andric       writeObjCClassName(ID, FrontendBufOS);
3590b57cec5SDimitry Andric     } else {
3600b57cec5SDimitry Andric       return true;
3610b57cec5SDimitry Andric     }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric     // Now apply backend mangling.
3640b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
3650b57cec5SDimitry Andric     return false;
3660b57cec5SDimitry Andric   }
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   std::string getName(const Decl *D) {
3690b57cec5SDimitry Andric     std::string Name;
3700b57cec5SDimitry Andric     {
3710b57cec5SDimitry Andric       llvm::raw_string_ostream OS(Name);
3720b57cec5SDimitry Andric       writeName(D, OS);
3730b57cec5SDimitry Andric     }
3740b57cec5SDimitry Andric     return Name;
3750b57cec5SDimitry Andric   }
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   enum ObjCKind {
3780b57cec5SDimitry Andric     ObjCClass,
3790b57cec5SDimitry Andric     ObjCMetaclass,
3800b57cec5SDimitry Andric   };
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric   static StringRef getClassSymbolPrefix(ObjCKind Kind,
3830b57cec5SDimitry Andric                                         const ASTContext &Context) {
3840b57cec5SDimitry Andric     if (Context.getLangOpts().ObjCRuntime.isGNUFamily())
3850b57cec5SDimitry Andric       return Kind == ObjCMetaclass ? "_OBJC_METACLASS_" : "_OBJC_CLASS_";
3860b57cec5SDimitry Andric     return Kind == ObjCMetaclass ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_";
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) {
3900b57cec5SDimitry Andric     StringRef ClassName;
3910b57cec5SDimitry Andric     if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
3920b57cec5SDimitry Andric       ClassName = OID->getObjCRuntimeNameAsString();
3930b57cec5SDimitry Andric     else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
3940b57cec5SDimitry Andric       ClassName = OID->getObjCRuntimeNameAsString();
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric     if (ClassName.empty())
3970b57cec5SDimitry Andric       return {};
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric     auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string {
4000b57cec5SDimitry Andric       SmallString<40> Mangled;
4010b57cec5SDimitry Andric       auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext());
4020b57cec5SDimitry Andric       llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
403*5ffd83dbSDimitry Andric       return std::string(Mangled.str());
4040b57cec5SDimitry Andric     };
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric     return {
4070b57cec5SDimitry Andric         Mangle(ObjCClass, ClassName),
4080b57cec5SDimitry Andric         Mangle(ObjCMetaclass, ClassName),
4090b57cec5SDimitry Andric     };
4100b57cec5SDimitry Andric   }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric   std::vector<std::string> getAllManglings(const Decl *D) {
4130b57cec5SDimitry Andric     if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D))
4140b57cec5SDimitry Andric       return getAllManglings(OCD);
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric     if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
4170b57cec5SDimitry Andric       return {};
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric     const NamedDecl *ND = cast<NamedDecl>(D);
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric     ASTContext &Ctx = ND->getASTContext();
4220b57cec5SDimitry Andric     std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric     std::vector<std::string> Manglings;
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric     auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
4270b57cec5SDimitry Andric       auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
4280b57cec5SDimitry Andric                                                      /*IsCXXMethod=*/true);
429a7dea167SDimitry Andric       auto CC = MD->getType()->castAs<FunctionProtoType>()->getCallConv();
4300b57cec5SDimitry Andric       return CC == DefaultCC;
4310b57cec5SDimitry Andric     };
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric     if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
4340b57cec5SDimitry Andric       Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
4370b57cec5SDimitry Andric         if (!CD->getParent()->isAbstract())
4380b57cec5SDimitry Andric           Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete));
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
4410b57cec5SDimitry Andric         if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
4420b57cec5SDimitry Andric           if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
4430b57cec5SDimitry Andric             Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure));
4440b57cec5SDimitry Andric     } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
4450b57cec5SDimitry Andric       Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
4460b57cec5SDimitry Andric       if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
4470b57cec5SDimitry Andric         Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
4480b57cec5SDimitry Andric         if (DD->isVirtual())
4490b57cec5SDimitry Andric           Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
4500b57cec5SDimitry Andric       }
4510b57cec5SDimitry Andric     } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
4520b57cec5SDimitry Andric       Manglings.emplace_back(getName(ND));
4530b57cec5SDimitry Andric       if (MD->isVirtual())
4540b57cec5SDimitry Andric         if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
4550b57cec5SDimitry Andric           for (const auto &T : *TIV)
4560b57cec5SDimitry Andric             Manglings.emplace_back(getMangledThunk(MD, T));
4570b57cec5SDimitry Andric     }
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric     return Manglings;
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric private:
4630b57cec5SDimitry Andric   bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) {
4640b57cec5SDimitry Andric     if (MC->shouldMangleDeclName(D)) {
465*5ffd83dbSDimitry Andric       GlobalDecl GD;
4660b57cec5SDimitry Andric       if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
467*5ffd83dbSDimitry Andric         GD = GlobalDecl(CtorD, Ctor_Complete);
4680b57cec5SDimitry Andric       else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
469*5ffd83dbSDimitry Andric         GD = GlobalDecl(DtorD, Dtor_Complete);
470*5ffd83dbSDimitry Andric       else if (D->hasAttr<CUDAGlobalAttr>())
471*5ffd83dbSDimitry Andric         GD = GlobalDecl(cast<FunctionDecl>(D));
4720b57cec5SDimitry Andric       else
473*5ffd83dbSDimitry Andric         GD = GlobalDecl(D);
474*5ffd83dbSDimitry Andric       MC->mangleName(GD, OS);
4750b57cec5SDimitry Andric       return false;
4760b57cec5SDimitry Andric     } else {
4770b57cec5SDimitry Andric       IdentifierInfo *II = D->getIdentifier();
4780b57cec5SDimitry Andric       if (!II)
4790b57cec5SDimitry Andric         return true;
4800b57cec5SDimitry Andric       OS << II->getName();
4810b57cec5SDimitry Andric       return false;
4820b57cec5SDimitry Andric     }
4830b57cec5SDimitry Andric   }
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) {
4860b57cec5SDimitry Andric     OS << getClassSymbolPrefix(ObjCClass, D->getASTContext());
4870b57cec5SDimitry Andric     OS << D->getObjCRuntimeNameAsString();
4880b57cec5SDimitry Andric   }
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) {
4910b57cec5SDimitry Andric     std::string FrontendBuf;
4920b57cec5SDimitry Andric     llvm::raw_string_ostream FOS(FrontendBuf);
4930b57cec5SDimitry Andric 
494*5ffd83dbSDimitry Andric     GlobalDecl GD;
4950b57cec5SDimitry Andric     if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
496*5ffd83dbSDimitry Andric       GD = GlobalDecl(CD, static_cast<CXXCtorType>(StructorType));
4970b57cec5SDimitry Andric     else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
498*5ffd83dbSDimitry Andric       GD = GlobalDecl(DD, static_cast<CXXDtorType>(StructorType));
499*5ffd83dbSDimitry Andric     MC->mangleName(GD, FOS);
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric     std::string BackendBuf;
5020b57cec5SDimitry Andric     llvm::raw_string_ostream BOS(BackendBuf);
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric     return BOS.str();
5070b57cec5SDimitry Andric   }
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) {
5100b57cec5SDimitry Andric     std::string FrontendBuf;
5110b57cec5SDimitry Andric     llvm::raw_string_ostream FOS(FrontendBuf);
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric     MC->mangleThunk(MD, T, FOS);
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric     std::string BackendBuf;
5160b57cec5SDimitry Andric     llvm::raw_string_ostream BOS(BackendBuf);
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric     llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric     return BOS.str();
5210b57cec5SDimitry Andric   }
5220b57cec5SDimitry Andric };
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric ASTNameGenerator::ASTNameGenerator(ASTContext &Ctx)
525a7dea167SDimitry Andric     : Impl(std::make_unique<Implementation>(Ctx)) {}
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric ASTNameGenerator::~ASTNameGenerator() {}
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric bool ASTNameGenerator::writeName(const Decl *D, raw_ostream &OS) {
5300b57cec5SDimitry Andric   return Impl->writeName(D, OS);
5310b57cec5SDimitry Andric }
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric std::string ASTNameGenerator::getName(const Decl *D) {
5340b57cec5SDimitry Andric   return Impl->getName(D);
5350b57cec5SDimitry Andric }
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric std::vector<std::string> ASTNameGenerator::getAllManglings(const Decl *D) {
5380b57cec5SDimitry Andric   return Impl->getAllManglings(D);
5390b57cec5SDimitry Andric }
540