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