Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 1 | //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 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 C++ name mangling according to the Itanium C++ ABI, |
| 11 | // which is used in GCC 3.2 and newer (and many compilers that are |
| 12 | // ABI-compatible with GCC): |
| 13 | // |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "Mangle.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | using namespace clang; |
| 26 | |
| 27 | namespace { |
| 28 | class VISIBILITY_HIDDEN CXXNameMangler { |
| 29 | ASTContext &Context; |
| 30 | llvm::raw_ostream &Out; |
| 31 | |
| 32 | public: |
| 33 | CXXNameMangler(ASTContext &C, llvm::raw_ostream &os) |
| 34 | : Context(C), Out(os) { } |
| 35 | |
| 36 | bool mangle(const NamedDecl *D); |
| 37 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 38 | void mangleName(const NamedDecl *ND); |
| 39 | void mangleUnqualifiedName(const NamedDecl *ND); |
| 40 | void mangleSourceName(const IdentifierInfo *II); |
| 41 | void mangleNestedName(const NamedDecl *ND); |
| 42 | void manglePrefix(const DeclContext *DC); |
| 43 | void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); |
| 44 | void mangleCVQualifiers(unsigned Quals); |
| 45 | void mangleType(QualType T); |
| 46 | void mangleType(const BuiltinType *T); |
| 47 | void mangleType(const FunctionType *T); |
| 48 | void mangleBareFunctionType(const FunctionType *T, bool MangleReturnType); |
| 49 | void mangleType(const TagType *T); |
| 50 | void mangleType(const ArrayType *T); |
| 51 | void mangleType(const MemberPointerType *T); |
| 52 | void mangleType(const TemplateTypeParmType *T); |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 53 | void mangleType(const ObjCInterfaceType *T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 54 | void mangleExpression(Expr *E); |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | bool CXXNameMangler::mangle(const NamedDecl *D) { |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 60 | // Any decl can be declared with __asm("foo") on it, and this takes |
| 61 | // precedence over all other naming in the .o file. |
| 62 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 63 | // If we have an asm name, then we use it as the mangling. |
| 64 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 65 | Out << ALA->getLabel(); |
| 66 | return true; |
| 67 | } |
| 68 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 69 | // <mangled-name> ::= _Z <encoding> |
| 70 | // ::= <data name> |
| 71 | // ::= <special-name> |
| 72 | |
| 73 | // FIXME: Actually use a visitor to decode these? |
Chris Lattner | bc7a029 | 2009-03-21 06:19:20 +0000 | [diff] [blame] | 74 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 75 | if (!FD) // Can only mangle functions so far. |
| 76 | return false; |
| 77 | |
| 78 | // Clang's "overloadable" attribute extension to C/C++ implies |
| 79 | // name mangling (always). |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 80 | if (FD->getAttr<OverloadableAttr>()) { |
Chris Lattner | bc7a029 | 2009-03-21 06:19:20 +0000 | [diff] [blame] | 81 | ; // fall into mangling code unconditionally. |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 82 | } else if (// C functions are not mangled |
| 83 | !Context.getLangOptions().CPlusPlus || |
| 84 | // "main" is not mangled in C++ |
| 85 | FD->isMain() || |
| 86 | // No mangling in an "implicit extern C" header. |
| 87 | Context.getSourceManager().getFileCharacteristic(FD->getLocation()) |
| 88 | == SrcMgr::C_ExternCSystem) |
Chris Lattner | bc7a029 | 2009-03-21 06:19:20 +0000 | [diff] [blame] | 89 | return false; |
| 90 | else { |
| 91 | // No name mangling in a C linkage specification. |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 92 | |
Chris Lattner | bc7a029 | 2009-03-21 06:19:20 +0000 | [diff] [blame] | 93 | for (const DeclContext *DC = FD->getDeclContext(); |
| 94 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 95 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 96 | // extern "C" functions don't use name mangling. |
| 97 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
| 98 | return false; |
| 99 | // Others do. |
| 100 | break; |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 101 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | bc7a029 | 2009-03-21 06:19:20 +0000 | [diff] [blame] | 103 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 104 | |
Chris Lattner | bc7a029 | 2009-03-21 06:19:20 +0000 | [diff] [blame] | 105 | // If we get here, mangle the decl name! |
| 106 | Out << "_Z"; |
| 107 | mangleFunctionEncoding(FD); |
| 108 | return true; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 112 | // <encoding> ::= <function name> <bare-function-type> |
| 113 | mangleName(FD); |
| 114 | mangleBareFunctionType(FD->getType()->getAsFunctionType(), false); |
| 115 | } |
| 116 | |
| 117 | static bool isStdNamespace(const DeclContext *DC) { |
| 118 | if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit()) |
| 119 | return false; |
| 120 | |
| 121 | const NamespaceDecl *NS = cast<NamespaceDecl>(DC); |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 122 | return NS->getOriginalNamespace()->getIdentifier()->isStr("std"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 126 | // <name> ::= <nested-name> |
| 127 | // ::= <unscoped-name> |
| 128 | // ::= <unscoped-template-name> <template-args> |
| 129 | // ::= <local-name> # See Scope Encoding below |
| 130 | // |
| 131 | // <unscoped-name> ::= <unqualified-name> |
| 132 | // ::= St <unqualified-name> # ::std:: |
| 133 | if (ND->getDeclContext()->isTranslationUnit()) |
| 134 | mangleUnqualifiedName(ND); |
| 135 | else if (isStdNamespace(ND->getDeclContext())) { |
| 136 | Out << "St"; |
| 137 | mangleUnqualifiedName(ND); |
| 138 | } else { |
| 139 | mangleNestedName(ND); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) { |
| 144 | // <unqualified-name> ::= <operator-name> |
| 145 | // ::= <ctor-dtor-name> |
| 146 | // ::= <source-name> |
| 147 | DeclarationName Name = ND->getDeclName(); |
| 148 | switch (Name.getNameKind()) { |
| 149 | case DeclarationName::Identifier: |
| 150 | mangleSourceName(Name.getAsIdentifierInfo()); |
| 151 | break; |
| 152 | |
| 153 | case DeclarationName::ObjCZeroArgSelector: |
| 154 | case DeclarationName::ObjCOneArgSelector: |
| 155 | case DeclarationName::ObjCMultiArgSelector: |
| 156 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 157 | break; |
| 158 | |
| 159 | case DeclarationName::CXXConstructorName: |
| 160 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 161 | // ::= C2 # base object constructor |
| 162 | // ::= C3 # complete object allocating constructor |
| 163 | // |
| 164 | // FIXME: We don't even have all of these constructors |
| 165 | // in the AST yet. |
| 166 | Out << "C1"; |
| 167 | break; |
| 168 | |
| 169 | case DeclarationName::CXXDestructorName: |
| 170 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 171 | // ::= D1 # complete object destructor |
| 172 | // ::= D2 # base object destructor |
| 173 | // |
| 174 | // FIXME: We don't even have all of these destructors in the AST |
| 175 | // yet. |
| 176 | Out << "D0"; |
| 177 | break; |
| 178 | |
| 179 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 180 | // <operator-name> ::= cv <type> # (cast) |
| 181 | Out << "cv"; |
| 182 | mangleType(Context.getCanonicalType(Name.getCXXNameType())); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 183 | break; |
| 184 | |
| 185 | case DeclarationName::CXXOperatorName: |
| 186 | mangleOperatorName(Name.getCXXOverloadedOperator(), |
| 187 | cast<FunctionDecl>(ND)->getNumParams()); |
| 188 | break; |
| 189 | |
| 190 | case DeclarationName::CXXUsingDirective: |
| 191 | assert(false && "Can't mangle a using directive name!"); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 192 | break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 197 | // <source-name> ::= <positive length number> <identifier> |
| 198 | // <number> ::= [n] <non-negative decimal integer> |
| 199 | // <identifier> ::= <unqualified source code identifier> |
| 200 | Out << II->getLength() << II->getName(); |
| 201 | } |
| 202 | |
| 203 | void CXXNameMangler::mangleNestedName(const NamedDecl *ND) { |
| 204 | // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
| 205 | // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 206 | // FIXME: no template support |
| 207 | Out << 'N'; |
| 208 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) |
| 209 | mangleCVQualifiers(Method->getTypeQualifiers()); |
| 210 | manglePrefix(ND->getDeclContext()); |
| 211 | mangleUnqualifiedName(ND); |
| 212 | Out << 'E'; |
| 213 | } |
| 214 | |
| 215 | void CXXNameMangler::manglePrefix(const DeclContext *DC) { |
| 216 | // <prefix> ::= <prefix> <unqualified-name> |
| 217 | // ::= <template-prefix> <template-args> |
| 218 | // ::= <template-param> |
| 219 | // ::= # empty |
| 220 | // ::= <substitution> |
| 221 | // FIXME: We only handle mangling of namespaces and classes at the moment. |
| 222 | if (DC->getParent() != DC) |
| 223 | manglePrefix(DC); |
| 224 | |
| 225 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC)) |
| 226 | mangleSourceName(Namespace->getIdentifier()); |
| 227 | else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) |
| 228 | mangleSourceName(Record->getIdentifier()); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 233 | switch (OO) { |
| 234 | // <operator-name> ::= nw # new |
| 235 | case OO_New: Out << "nw"; break; |
| 236 | // ::= na # new[] |
| 237 | case OO_Array_New: Out << "na"; break; |
| 238 | // ::= dl # delete |
| 239 | case OO_Delete: Out << "dl"; break; |
| 240 | // ::= da # delete[] |
| 241 | case OO_Array_Delete: Out << "da"; break; |
| 242 | // ::= ps # + (unary) |
| 243 | // ::= pl # + |
| 244 | case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break; |
| 245 | // ::= ng # - (unary) |
| 246 | // ::= mi # - |
| 247 | case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break; |
| 248 | // ::= ad # & (unary) |
| 249 | // ::= an # & |
| 250 | case OO_Amp: Out << (Arity == 1? "ad" : "an"); break; |
| 251 | // ::= de # * (unary) |
| 252 | // ::= ml # * |
| 253 | case OO_Star: Out << (Arity == 1? "de" : "ml"); break; |
| 254 | // ::= co # ~ |
| 255 | case OO_Tilde: Out << "co"; break; |
| 256 | // ::= dv # / |
| 257 | case OO_Slash: Out << "dv"; break; |
| 258 | // ::= rm # % |
| 259 | case OO_Percent: Out << "rm"; break; |
| 260 | // ::= or # | |
| 261 | case OO_Pipe: Out << "or"; break; |
| 262 | // ::= eo # ^ |
| 263 | case OO_Caret: Out << "eo"; break; |
| 264 | // ::= aS # = |
| 265 | case OO_Equal: Out << "aS"; break; |
| 266 | // ::= pL # += |
| 267 | case OO_PlusEqual: Out << "pL"; break; |
| 268 | // ::= mI # -= |
| 269 | case OO_MinusEqual: Out << "mI"; break; |
| 270 | // ::= mL # *= |
| 271 | case OO_StarEqual: Out << "mL"; break; |
| 272 | // ::= dV # /= |
| 273 | case OO_SlashEqual: Out << "dV"; break; |
| 274 | // ::= rM # %= |
| 275 | case OO_PercentEqual: Out << "rM"; break; |
| 276 | // ::= aN # &= |
| 277 | case OO_AmpEqual: Out << "aN"; break; |
| 278 | // ::= oR # |= |
| 279 | case OO_PipeEqual: Out << "oR"; break; |
| 280 | // ::= eO # ^= |
| 281 | case OO_CaretEqual: Out << "eO"; break; |
| 282 | // ::= ls # << |
| 283 | case OO_LessLess: Out << "ls"; break; |
| 284 | // ::= rs # >> |
| 285 | case OO_GreaterGreater: Out << "rs"; break; |
| 286 | // ::= lS # <<= |
| 287 | case OO_LessLessEqual: Out << "lS"; break; |
| 288 | // ::= rS # >>= |
| 289 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
| 290 | // ::= eq # == |
| 291 | case OO_EqualEqual: Out << "eq"; break; |
| 292 | // ::= ne # != |
| 293 | case OO_ExclaimEqual: Out << "ne"; break; |
| 294 | // ::= lt # < |
| 295 | case OO_Less: Out << "lt"; break; |
| 296 | // ::= gt # > |
| 297 | case OO_Greater: Out << "gt"; break; |
| 298 | // ::= le # <= |
| 299 | case OO_LessEqual: Out << "le"; break; |
| 300 | // ::= ge # >= |
| 301 | case OO_GreaterEqual: Out << "ge"; break; |
| 302 | // ::= nt # ! |
| 303 | case OO_Exclaim: Out << "nt"; break; |
| 304 | // ::= aa # && |
| 305 | case OO_AmpAmp: Out << "aa"; break; |
| 306 | // ::= oo # || |
| 307 | case OO_PipePipe: Out << "oo"; break; |
| 308 | // ::= pp # ++ |
| 309 | case OO_PlusPlus: Out << "pp"; break; |
| 310 | // ::= mm # -- |
| 311 | case OO_MinusMinus: Out << "mm"; break; |
| 312 | // ::= cm # , |
| 313 | case OO_Comma: Out << "cm"; break; |
| 314 | // ::= pm # ->* |
| 315 | case OO_ArrowStar: Out << "pm"; break; |
| 316 | // ::= pt # -> |
| 317 | case OO_Arrow: Out << "pt"; break; |
| 318 | // ::= cl # () |
| 319 | case OO_Call: Out << "cl"; break; |
| 320 | // ::= ix # [] |
| 321 | case OO_Subscript: Out << "ix"; break; |
| 322 | // UNSUPPORTED: ::= qu # ? |
| 323 | |
| 324 | case OO_None: |
| 325 | case NUM_OVERLOADED_OPERATORS: |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 326 | assert(false && "Not an overloaded operator"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void CXXNameMangler::mangleCVQualifiers(unsigned Quals) { |
| 332 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
| 333 | if (Quals & QualType::Restrict) |
| 334 | Out << 'r'; |
| 335 | if (Quals & QualType::Volatile) |
| 336 | Out << 'V'; |
| 337 | if (Quals & QualType::Const) |
| 338 | Out << 'K'; |
| 339 | } |
| 340 | |
| 341 | void CXXNameMangler::mangleType(QualType T) { |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 342 | // Only operate on the canonical type! |
| 343 | T = Context.getCanonicalType(T); |
| 344 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 345 | // FIXME: Should we have a TypeNodes.def to make this easier? (YES!) |
| 346 | |
| 347 | // <type> ::= <CV-qualifiers> <type> |
| 348 | mangleCVQualifiers(T.getCVRQualifiers()); |
| 349 | |
| 350 | // ::= <builtin-type> |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 351 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 352 | mangleType(BT); |
| 353 | // ::= <function-type> |
| 354 | else if (const FunctionType *FT = dyn_cast<FunctionType>(T.getTypePtr())) |
| 355 | mangleType(FT); |
| 356 | // ::= <class-enum-type> |
| 357 | else if (const TagType *TT = dyn_cast<TagType>(T.getTypePtr())) |
| 358 | mangleType(TT); |
| 359 | // ::= <array-type> |
| 360 | else if (const ArrayType *AT = dyn_cast<ArrayType>(T.getTypePtr())) |
| 361 | mangleType(AT); |
| 362 | // ::= <pointer-to-member-type> |
| 363 | else if (const MemberPointerType *MPT |
| 364 | = dyn_cast<MemberPointerType>(T.getTypePtr())) |
| 365 | mangleType(MPT); |
| 366 | // ::= <template-param> |
| 367 | else if (const TemplateTypeParmType *TypeParm |
| 368 | = dyn_cast<TemplateTypeParmType>(T.getTypePtr())) |
| 369 | mangleType(TypeParm); |
| 370 | // FIXME: ::= <template-template-param> <template-args> |
| 371 | // FIXME: ::= <substitution> # See Compression below |
| 372 | // ::= P <type> # pointer-to |
| 373 | else if (const PointerType *PT = dyn_cast<PointerType>(T.getTypePtr())) { |
| 374 | Out << 'P'; |
| 375 | mangleType(PT->getPointeeType()); |
| 376 | } |
| 377 | // ::= R <type> # reference-to |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 378 | else if (const LValueReferenceType *RT = |
| 379 | dyn_cast<LValueReferenceType>(T.getTypePtr())) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 380 | Out << 'R'; |
| 381 | mangleType(RT->getPointeeType()); |
| 382 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 383 | // ::= O <type> # rvalue reference-to (C++0x) |
| 384 | else if (const RValueReferenceType *RT = |
| 385 | dyn_cast<RValueReferenceType>(T.getTypePtr())) { |
| 386 | Out << 'O'; |
| 387 | mangleType(RT->getPointeeType()); |
| 388 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 389 | // ::= C <type> # complex pair (C 2000) |
| 390 | else if (const ComplexType *CT = dyn_cast<ComplexType>(T.getTypePtr())) { |
| 391 | Out << 'C'; |
| 392 | mangleType(CT->getElementType()); |
| 393 | } else if (const VectorType *VT = dyn_cast<VectorType>(T.getTypePtr())) { |
| 394 | // GNU extension: vector types |
| 395 | Out << "U8__vector"; |
| 396 | mangleType(VT->getElementType()); |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 397 | } else if (const ObjCInterfaceType *IT = |
| 398 | dyn_cast<ObjCInterfaceType>(T.getTypePtr())) { |
| 399 | mangleType(IT); |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 400 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 401 | // FIXME: ::= G <type> # imaginary (C 2000) |
| 402 | // FIXME: ::= U <source-name> <type> # vendor extended type qualifier |
| 403 | else |
| 404 | assert(false && "Cannot mangle unknown type"); |
| 405 | } |
| 406 | |
| 407 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
| 408 | // <builtin-type> ::= v # void |
| 409 | // ::= w # wchar_t |
| 410 | // ::= b # bool |
| 411 | // ::= c # char |
| 412 | // ::= a # signed char |
| 413 | // ::= h # unsigned char |
| 414 | // ::= s # short |
| 415 | // ::= t # unsigned short |
| 416 | // ::= i # int |
| 417 | // ::= j # unsigned int |
| 418 | // ::= l # long |
| 419 | // ::= m # unsigned long |
| 420 | // ::= x # long long, __int64 |
| 421 | // ::= y # unsigned long long, __int64 |
| 422 | // ::= n # __int128 |
| 423 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 424 | // ::= f # float |
| 425 | // ::= d # double |
| 426 | // ::= e # long double, __float80 |
| 427 | // UNSUPPORTED: ::= g # __float128 |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 428 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 429 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 430 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 431 | // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) |
| 432 | // UNSUPPORTED: ::= Di # char32_t |
| 433 | // UNSUPPORTED: ::= Ds # char16_t |
| 434 | // ::= u <source-name> # vendor extended type |
| 435 | switch (T->getKind()) { |
| 436 | case BuiltinType::Void: Out << 'v'; break; |
| 437 | case BuiltinType::Bool: Out << 'b'; break; |
| 438 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 439 | case BuiltinType::UChar: Out << 'h'; break; |
| 440 | case BuiltinType::UShort: Out << 't'; break; |
| 441 | case BuiltinType::UInt: Out << 'j'; break; |
| 442 | case BuiltinType::ULong: Out << 'm'; break; |
| 443 | case BuiltinType::ULongLong: Out << 'y'; break; |
| 444 | case BuiltinType::SChar: Out << 'a'; break; |
| 445 | case BuiltinType::WChar: Out << 'w'; break; |
| 446 | case BuiltinType::Short: Out << 's'; break; |
| 447 | case BuiltinType::Int: Out << 'i'; break; |
| 448 | case BuiltinType::Long: Out << 'l'; break; |
| 449 | case BuiltinType::LongLong: Out << 'x'; break; |
| 450 | case BuiltinType::Float: Out << 'f'; break; |
| 451 | case BuiltinType::Double: Out << 'd'; break; |
| 452 | case BuiltinType::LongDouble: Out << 'e'; break; |
| 453 | |
| 454 | case BuiltinType::Overload: |
| 455 | case BuiltinType::Dependent: |
| 456 | assert(false && |
| 457 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void CXXNameMangler::mangleType(const FunctionType *T) { |
| 463 | // <function-type> ::= F [Y] <bare-function-type> E |
| 464 | Out << 'F'; |
| 465 | // FIXME: We don't have enough information in the AST to produce the |
| 466 | // 'Y' encoding for extern "C" function types. |
| 467 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 468 | Out << 'E'; |
| 469 | } |
| 470 | |
| 471 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 472 | bool MangleReturnType) { |
| 473 | // <bare-function-type> ::= <signature type>+ |
| 474 | if (MangleReturnType) |
| 475 | mangleType(T->getResultType()); |
| 476 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 477 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 478 | assert(Proto && "Can't mangle K&R function prototypes"); |
| 479 | |
Anders Carlsson | c6c91bc | 2009-04-01 00:15:23 +0000 | [diff] [blame^] | 480 | if (Proto->getNumArgs() == 0) { |
| 481 | Out << 'v'; |
| 482 | return; |
| 483 | } |
| 484 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 485 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 486 | ArgEnd = Proto->arg_type_end(); |
| 487 | Arg != ArgEnd; ++Arg) |
| 488 | mangleType(*Arg); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 489 | |
| 490 | // <builtin-type> ::= z # ellipsis |
| 491 | if (Proto->isVariadic()) |
| 492 | Out << 'z'; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void CXXNameMangler::mangleType(const TagType *T) { |
| 496 | // <class-enum-type> ::= <name> |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 497 | |
| 498 | if (!T->getDecl()->getIdentifier()) |
| 499 | mangleName(T->getDecl()->getTypedefForAnonDecl()); |
| 500 | else |
| 501 | mangleName(T->getDecl()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void CXXNameMangler::mangleType(const ArrayType *T) { |
| 505 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 506 | // ::= A [<dimension expression>] _ <element type> |
| 507 | Out << 'A'; |
| 508 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) |
| 509 | Out << CAT->getSize(); |
| 510 | else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(T)) |
| 511 | mangleExpression(VAT->getSizeExpr()); |
| 512 | else if (const DependentSizedArrayType *DSAT |
| 513 | = dyn_cast<DependentSizedArrayType>(T)) |
| 514 | mangleExpression(DSAT->getSizeExpr()); |
| 515 | |
| 516 | Out << '_'; |
| 517 | mangleType(T->getElementType()); |
| 518 | } |
| 519 | |
| 520 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
| 521 | // <pointer-to-member-type> ::= M <class type> <member type> |
| 522 | Out << 'M'; |
| 523 | mangleType(QualType(T->getClass(), 0)); |
| 524 | mangleType(T->getPointeeType()); |
| 525 | } |
| 526 | |
| 527 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
| 528 | // <template-param> ::= T_ # first template parameter |
| 529 | // ::= T <parameter-2 non-negative number> _ |
| 530 | if (T->getIndex() == 0) |
| 531 | Out << "T_"; |
| 532 | else |
| 533 | Out << 'T' << (T->getIndex() - 1) << '_'; |
| 534 | } |
| 535 | |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 536 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 537 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 538 | } |
| 539 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 540 | void CXXNameMangler::mangleExpression(Expr *E) { |
| 541 | assert(false && "Cannot mangle expressions yet"); |
| 542 | } |
| 543 | |
| 544 | namespace clang { |
| 545 | /// \brief Mangles the name of the declaration D and emits that name |
| 546 | /// to the given output stream. |
| 547 | /// |
| 548 | /// If the declaration D requires a mangled name, this routine will |
| 549 | /// emit that mangled name to \p os and return true. Otherwise, \p |
| 550 | /// os will be unchanged and this routine will return false. In this |
| 551 | /// case, the caller should just emit the identifier of the declaration |
| 552 | /// (\c D->getIdentifier()) as its name. |
| 553 | bool mangleName(const NamedDecl *D, ASTContext &Context, |
| 554 | llvm::raw_ostream &os) { |
| 555 | CXXNameMangler Mangler(Context, os); |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 556 | if (!Mangler.mangle(D)) |
| 557 | return false; |
| 558 | |
| 559 | os.flush(); |
| 560 | return true; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |