Mangle.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Implements generic name mangling support for blocks and Objective-C.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/Attr.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclCXX.h"
  17. #include "clang/AST/DeclObjC.h"
  18. #include "clang/AST/DeclTemplate.h"
  19. #include "clang/AST/ExprCXX.h"
  20. #include "clang/AST/Mangle.h"
  21. #include "clang/Basic/ABI.h"
  22. #include "clang/Basic/SourceManager.h"
  23. #include "clang/Basic/TargetInfo.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. // //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. #define MANGLE_CHECKER 0
  30. #if MANGLE_CHECKER
  31. #include <cxxabi.h>
  32. #endif
  33. using namespace clang;
  34. // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
  35. // much to be desired. Come up with a better mangling scheme.
  36. static void mangleFunctionBlock(MangleContext &Context,
  37. StringRef Outer,
  38. const BlockDecl *BD,
  39. raw_ostream &Out) {
  40. unsigned discriminator = Context.getBlockId(BD, true);
  41. if (discriminator == 0)
  42. Out << "__" << Outer << "_block_invoke";
  43. else
  44. Out << "__" << Outer << "_block_invoke_" << discriminator+1;
  45. }
  46. void MangleContext::anchor() { }
  47. enum CCMangling {
  48. CCM_Other,
  49. CCM_Fast,
  50. CCM_Vector,
  51. CCM_Std
  52. };
  53. static bool isExternC(const NamedDecl *ND) {
  54. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
  55. return FD->isExternC();
  56. return cast<VarDecl>(ND)->isExternC();
  57. }
  58. static CCMangling getCallingConvMangling(const ASTContext &Context,
  59. const NamedDecl *ND) {
  60. const TargetInfo &TI = Context.getTargetInfo();
  61. const llvm::Triple &Triple = TI.getTriple();
  62. if (!Triple.isOSWindows() ||
  63. !(Triple.getArch() == llvm::Triple::x86 ||
  64. Triple.getArch() == llvm::Triple::x86_64))
  65. return CCM_Other;
  66. if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
  67. TI.getCXXABI() == TargetCXXABI::Microsoft)
  68. return CCM_Other;
  69. const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
  70. if (!FD)
  71. return CCM_Other;
  72. QualType T = FD->getType();
  73. const FunctionType *FT = T->castAs<FunctionType>();
  74. CallingConv CC = FT->getCallConv();
  75. switch (CC) {
  76. default:
  77. return CCM_Other;
  78. case CC_X86FastCall:
  79. return CCM_Fast;
  80. case CC_X86StdCall:
  81. return CCM_Std;
  82. case CC_X86VectorCall:
  83. return CCM_Vector;
  84. }
  85. }
  86. bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
  87. const ASTContext &ASTContext = getASTContext();
  88. CCMangling CC = getCallingConvMangling(ASTContext, D);
  89. if (CC != CCM_Other)
  90. return true;
  91. // In C, functions with no attributes never need to be mangled. Fastpath them.
  92. if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
  93. return false;
  94. // Any decl can be declared with __asm("foo") on it, and this takes precedence
  95. // over all other naming in the .o file.
  96. if (D->hasAttr<AsmLabelAttr>())
  97. return true;
  98. return shouldMangleCXXName(D);
  99. }
  100. void MangleContext::mangleName(const NamedDecl *D, raw_ostream &Out) {
  101. // Any decl can be declared with __asm("foo") on it, and this takes precedence
  102. // over all other naming in the .o file.
  103. if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
  104. // If we have an asm name, then we use it as the mangling.
  105. // Adding the prefix can cause problems when one file has a "foo" and
  106. // another has a "\01foo". That is known to happen on ELF with the
  107. // tricks normally used for producing aliases (PR9177). Fortunately the
  108. // llvm mangler on ELF is a nop, so we can just avoid adding the \01
  109. // marker. We also avoid adding the marker if this is an alias for an
  110. // LLVM intrinsic.
  111. StringRef UserLabelPrefix =
  112. getASTContext().getTargetInfo().getUserLabelPrefix();
  113. if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
  114. Out << '\01'; // LLVM IR Marker for __asm("foo")
  115. Out << ALA->getLabel();
  116. return;
  117. }
  118. const ASTContext &ASTContext = getASTContext();
  119. CCMangling CC = getCallingConvMangling(ASTContext, D);
  120. bool MCXX = shouldMangleCXXName(D);
  121. const TargetInfo &TI = Context.getTargetInfo();
  122. if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
  123. if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
  124. mangleObjCMethodName(OMD, Out);
  125. else
  126. mangleCXXName(D, Out);
  127. return;
  128. }
  129. Out << '\01';
  130. if (CC == CCM_Std)
  131. Out << '_';
  132. else if (CC == CCM_Fast)
  133. Out << '@';
  134. if (!MCXX)
  135. Out << D->getIdentifier()->getName();
  136. else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
  137. mangleObjCMethodName(OMD, Out);
  138. else
  139. mangleCXXName(D, Out);
  140. const FunctionDecl *FD = cast<FunctionDecl>(D);
  141. const FunctionType *FT = FD->getType()->castAs<FunctionType>();
  142. const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
  143. if (CC == CCM_Vector)
  144. Out << '@';
  145. Out << '@';
  146. if (!Proto) {
  147. Out << '0';
  148. return;
  149. }
  150. assert(!Proto->isVariadic());
  151. unsigned ArgWords = 0;
  152. if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
  153. if (!MD->isStatic())
  154. ++ArgWords;
  155. for (const auto &AT : Proto->param_types())
  156. // Size should be aligned to pointer size.
  157. ArgWords += llvm::RoundUpToAlignment(ASTContext.getTypeSize(AT),
  158. TI.getPointerWidth(0)) /
  159. TI.getPointerWidth(0);
  160. Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
  161. }
  162. void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
  163. const NamedDecl *ID,
  164. raw_ostream &Out) {
  165. unsigned discriminator = getBlockId(BD, false);
  166. if (ID) {
  167. if (shouldMangleDeclName(ID))
  168. mangleName(ID, Out);
  169. else {
  170. Out << ID->getIdentifier()->getName();
  171. }
  172. }
  173. if (discriminator == 0)
  174. Out << "_block_invoke";
  175. else
  176. Out << "_block_invoke_" << discriminator+1;
  177. }
  178. void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
  179. CXXCtorType CT, const BlockDecl *BD,
  180. raw_ostream &ResStream) {
  181. SmallString<64> Buffer;
  182. llvm::raw_svector_ostream Out(Buffer);
  183. mangleCXXCtor(CD, CT, Out);
  184. Out.flush();
  185. mangleFunctionBlock(*this, Buffer, BD, ResStream);
  186. }
  187. void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
  188. CXXDtorType DT, const BlockDecl *BD,
  189. raw_ostream &ResStream) {
  190. SmallString<64> Buffer;
  191. llvm::raw_svector_ostream Out(Buffer);
  192. mangleCXXDtor(DD, DT, Out);
  193. Out.flush();
  194. mangleFunctionBlock(*this, Buffer, BD, ResStream);
  195. }
  196. void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
  197. raw_ostream &Out) {
  198. assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
  199. SmallString<64> Buffer;
  200. llvm::raw_svector_ostream Stream(Buffer);
  201. if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
  202. mangleObjCMethodName(Method, Stream);
  203. } else {
  204. assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
  205. "expected a NamedDecl or BlockDecl");
  206. if (isa<BlockDecl>(DC))
  207. for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
  208. (void) getBlockId(cast<BlockDecl>(DC), true);
  209. assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
  210. "expected a TranslationUnitDecl or a NamedDecl");
  211. if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
  212. mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
  213. else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
  214. mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
  215. else if (auto ND = dyn_cast<NamedDecl>(DC)) {
  216. if (!shouldMangleDeclName(ND) && ND->getIdentifier())
  217. Stream << ND->getIdentifier()->getName();
  218. else {
  219. // FIXME: We were doing a mangleUnqualifiedName() before, but that's
  220. // a private member of a class that will soon itself be private to the
  221. // Itanium C++ ABI object. What should we do now? Right now, I'm just
  222. // calling the mangleName() method on the MangleContext; is there a
  223. // better way?
  224. mangleName(ND, Stream);
  225. }
  226. }
  227. }
  228. Stream.flush();
  229. mangleFunctionBlock(*this, Buffer, BD, Out);
  230. }
  231. void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
  232. raw_ostream &Out) {
  233. SmallString<64> Name;
  234. llvm::raw_svector_ostream OS(Name);
  235. const ObjCContainerDecl *CD =
  236. dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
  237. assert (CD && "Missing container decl in GetNameForMethod");
  238. OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
  239. if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
  240. OS << '(' << *CID << ')';
  241. OS << ' ';
  242. MD->getSelector().print(OS);
  243. OS << ']';
  244. Out << OS.str().size() << OS.str();
  245. }