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