Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1 | //===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===// |
| 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 | // This provides C++ name mangling targetting the Microsoft Visual C++ ABI. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Mangle.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/CharUnits.h" |
| 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
| 21 | #include "clang/AST/ExprCXX.h" |
| 22 | #include "clang/Basic/ABI.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | /// MicrosoftCXXNameMangler - Manage the mangling of a single name for the |
| 29 | /// Microsoft Visual C++ ABI. |
| 30 | class MicrosoftCXXNameMangler { |
| 31 | MangleContext &Context; |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 32 | llvm::raw_svector_ostream &Out; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 33 | |
| 34 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 35 | |
| 36 | public: |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 37 | MicrosoftCXXNameMangler(MangleContext &C, llvm::raw_svector_ostream &Out_) |
| 38 | : Context(C), Out(Out_) { } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 39 | |
| 40 | void mangle(const NamedDecl *D, llvm::StringRef Prefix = "?"); |
| 41 | void mangleName(const NamedDecl *ND); |
| 42 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 43 | void mangleVariableEncoding(const VarDecl *VD); |
| 44 | void mangleNumber(int64_t Number); |
| 45 | void mangleType(QualType T); |
| 46 | |
| 47 | private: |
| 48 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 49 | mangleUnqualifiedName(ND, ND->getDeclName()); |
| 50 | } |
| 51 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); |
| 52 | void mangleSourceName(const IdentifierInfo *II); |
| 53 | void manglePostfix(const DeclContext *DC, bool NoFunction=false); |
| 54 | void mangleOperatorName(OverloadedOperatorKind OO); |
| 55 | void mangleQualifiers(Qualifiers Quals, bool IsMember); |
| 56 | |
| 57 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
| 58 | |
| 59 | // Declare manglers for every type class. |
| 60 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 61 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 62 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); |
| 63 | #include "clang/AST/TypeNodes.def" |
| 64 | |
| 65 | void mangleType(const TagType*); |
| 66 | void mangleType(const FunctionType *T, const FunctionDecl *D, |
| 67 | bool IsStructor, bool IsInstMethod); |
| 68 | void mangleType(const ArrayType *T, bool IsGlobal); |
| 69 | void mangleExtraDimensions(QualType T); |
| 70 | void mangleFunctionClass(const FunctionDecl *FD); |
| 71 | void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false); |
| 72 | void mangleThrowSpecification(const FunctionProtoType *T); |
| 73 | |
| 74 | }; |
| 75 | |
| 76 | /// MicrosoftMangleContext - Overrides the default MangleContext for the |
| 77 | /// Microsoft Visual C++ ABI. |
| 78 | class MicrosoftMangleContext : public MangleContext { |
| 79 | public: |
| 80 | MicrosoftMangleContext(ASTContext &Context, |
| 81 | Diagnostic &Diags) : MangleContext(Context, Diags) { } |
| 82 | virtual bool shouldMangleDeclName(const NamedDecl *D); |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 83 | virtual void mangleName(const NamedDecl *D, llvm::raw_svector_ostream &Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 84 | virtual void mangleThunk(const CXXMethodDecl *MD, |
| 85 | const ThunkInfo &Thunk, |
| 86 | llvm::SmallVectorImpl<char> &); |
| 87 | virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 88 | const ThisAdjustment &ThisAdjustment, |
| 89 | llvm::SmallVectorImpl<char> &); |
| 90 | virtual void mangleCXXVTable(const CXXRecordDecl *RD, |
| 91 | llvm::SmallVectorImpl<char> &); |
| 92 | virtual void mangleCXXVTT(const CXXRecordDecl *RD, |
| 93 | llvm::SmallVectorImpl<char> &); |
| 94 | virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, |
| 95 | const CXXRecordDecl *Type, |
| 96 | llvm::SmallVectorImpl<char> &); |
| 97 | virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); |
| 98 | virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); |
| 99 | virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 100 | llvm::raw_svector_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 101 | virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 102 | llvm::raw_svector_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 103 | virtual void mangleReferenceTemporary(const clang::VarDecl *, |
| 104 | llvm::SmallVectorImpl<char> &); |
| 105 | }; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | static bool isInCLinkageSpecification(const Decl *D) { |
| 110 | D = D->getCanonicalDecl(); |
| 111 | for (const DeclContext *DC = D->getDeclContext(); |
| 112 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 113 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
| 114 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 115 | } |
| 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 121 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 122 | if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) |
| 123 | return false; |
| 124 | |
| 125 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 126 | // over all other naming in the .o file. |
| 127 | if (D->hasAttr<AsmLabelAttr>()) |
| 128 | return true; |
| 129 | |
| 130 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
| 131 | // (always) as does passing a C++ member function and a function |
| 132 | // whose name is not a simple identifier. |
| 133 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 134 | if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || |
| 135 | !FD->getDeclName().isIdentifier())) |
| 136 | return true; |
| 137 | |
| 138 | // Otherwise, no mangling is done outside C++ mode. |
| 139 | if (!getASTContext().getLangOptions().CPlusPlus) |
| 140 | return false; |
| 141 | |
| 142 | // Variables at global scope with internal linkage are not mangled. |
| 143 | if (!FD) { |
| 144 | const DeclContext *DC = D->getDeclContext(); |
| 145 | if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage) |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // C functions and "main" are not mangled. |
| 150 | if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) |
| 151 | return false; |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, |
| 157 | llvm::StringRef Prefix) { |
| 158 | // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. |
| 159 | // Therefore it's really important that we don't decorate the |
| 160 | // name with leading underscores or leading/trailing at signs. So, emit a |
| 161 | // asm marker at the start so we get the name right. |
| 162 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 163 | |
| 164 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 165 | // over all other naming in the .o file. |
| 166 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 167 | // If we have an asm name, then we use it as the mangling. |
| 168 | Out << ALA->getLabel(); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | // <mangled-name> ::= ? <name> <type-encoding> |
| 173 | Out << Prefix; |
| 174 | mangleName(D); |
| 175 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 176 | mangleFunctionEncoding(FD); |
| 177 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 178 | mangleVariableEncoding(VD); |
| 179 | // TODO: Fields? Can MSVC even mangle them? |
| 180 | } |
| 181 | |
| 182 | void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 183 | // <type-encoding> ::= <function-class> <function-type> |
| 184 | |
| 185 | // Don't mangle in the type if this isn't a decl we should typically mangle. |
| 186 | if (!Context.shouldMangleDeclName(FD)) |
| 187 | return; |
| 188 | |
| 189 | // We should never ever see a FunctionNoProtoType at this point. |
| 190 | // We don't even know how to mangle their types anyway :). |
| 191 | const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType()); |
| 192 | |
| 193 | bool InStructor = false, InInstMethod = false; |
| 194 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 195 | if (MD) { |
| 196 | if (MD->isInstance()) |
| 197 | InInstMethod = true; |
| 198 | if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) |
| 199 | InStructor = true; |
| 200 | } |
| 201 | |
| 202 | // First, the function class. |
| 203 | mangleFunctionClass(FD); |
| 204 | |
| 205 | mangleType(FT, FD, InStructor, InInstMethod); |
| 206 | } |
| 207 | |
| 208 | void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { |
| 209 | // <type-encoding> ::= <storage-class> <variable-type> |
| 210 | // <storage-class> ::= 0 # private static member |
| 211 | // ::= 1 # protected static member |
| 212 | // ::= 2 # public static member |
| 213 | // ::= 3 # global |
| 214 | // ::= 4 # static local |
| 215 | |
| 216 | // The first character in the encoding (after the name) is the storage class. |
| 217 | if (VD->isStaticDataMember()) { |
| 218 | // If it's a static member, it also encodes the access level. |
| 219 | switch (VD->getAccess()) { |
| 220 | default: |
| 221 | case AS_private: Out << '0'; break; |
| 222 | case AS_protected: Out << '1'; break; |
| 223 | case AS_public: Out << '2'; break; |
| 224 | } |
| 225 | } |
| 226 | else if (!VD->isStaticLocal()) |
| 227 | Out << '3'; |
| 228 | else |
| 229 | Out << '4'; |
| 230 | // Now mangle the type. |
| 231 | // <variable-type> ::= <type> <cvr-qualifiers> |
| 232 | // ::= <type> A # pointers, references, arrays |
| 233 | // Pointers and references are odd. The type of 'int * const foo;' gets |
| 234 | // mangled as 'QAHA' instead of 'PAHB', for example. |
| 235 | QualType Ty = VD->getType(); |
| 236 | if (Ty->isPointerType() || Ty->isReferenceType()) { |
| 237 | mangleType(Ty); |
| 238 | Out << 'A'; |
| 239 | } else if (Ty->isArrayType()) { |
| 240 | // Global arrays are funny, too. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 241 | mangleType(cast<ArrayType>(Ty.getTypePtr()), true); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 242 | Out << 'A'; |
| 243 | } else { |
| 244 | mangleType(Ty.getLocalUnqualifiedType()); |
| 245 | mangleQualifiers(Ty.getLocalQualifiers(), false); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { |
| 250 | // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ |
| 251 | const DeclContext *DC = ND->getDeclContext(); |
| 252 | |
| 253 | // Always start with the unqualified name. |
| 254 | mangleUnqualifiedName(ND); |
| 255 | |
| 256 | // If this is an extern variable declared locally, the relevant DeclContext |
| 257 | // is that of the containing namespace, or the translation unit. |
| 258 | if (isa<FunctionDecl>(DC) && ND->hasLinkage()) |
| 259 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 260 | DC = DC->getParent(); |
| 261 | |
| 262 | manglePostfix(DC); |
| 263 | |
| 264 | // Terminate the whole name with an '@'. |
| 265 | Out << '@'; |
| 266 | } |
| 267 | |
| 268 | void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { |
| 269 | // <number> ::= [?] <decimal digit> # <= 9 |
| 270 | // ::= [?] <hex digit>+ @ # > 9; A = 0, B = 1, etc... |
| 271 | if (Number < 0) { |
| 272 | Out << '?'; |
| 273 | Number = -Number; |
| 274 | } |
| 275 | if (Number >= 1 && Number <= 10) { |
| 276 | Out << Number-1; |
| 277 | } else { |
| 278 | // We have to build up the encoding in reverse order, so it will come |
| 279 | // out right when we write it out. |
| 280 | char Encoding[16]; |
| 281 | char *EndPtr = Encoding+sizeof(Encoding); |
| 282 | char *CurPtr = EndPtr; |
| 283 | while (Number) { |
| 284 | *--CurPtr = 'A' + (Number % 16); |
| 285 | Number /= 16; |
| 286 | } |
| 287 | Out.write(CurPtr, EndPtr-CurPtr); |
| 288 | Out << '@'; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void |
| 293 | MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 294 | DeclarationName Name) { |
| 295 | // <unqualified-name> ::= <operator-name> |
| 296 | // ::= <ctor-dtor-name> |
| 297 | // ::= <source-name> |
| 298 | switch (Name.getNameKind()) { |
| 299 | case DeclarationName::Identifier: { |
| 300 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
| 301 | mangleSourceName(II); |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | // Otherwise, an anonymous entity. We must have a declaration. |
| 306 | assert(ND && "mangling empty name without declaration"); |
| 307 | |
| 308 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 309 | if (NS->isAnonymousNamespace()) { |
| 310 | Out << "?A"; |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // We must have an anonymous struct. |
| 316 | const TagDecl *TD = cast<TagDecl>(ND); |
| 317 | if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { |
| 318 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 319 | "Typedef should not be in another decl context!"); |
| 320 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 321 | "Typedef was not named!"); |
| 322 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | // When VC encounters an anonymous type with no tag and no typedef, |
| 327 | // it literally emits '<unnamed-tag>'. |
| 328 | Out << "<unnamed-tag>"; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | case DeclarationName::ObjCZeroArgSelector: |
| 333 | case DeclarationName::ObjCOneArgSelector: |
| 334 | case DeclarationName::ObjCMultiArgSelector: |
| 335 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 336 | break; |
| 337 | |
| 338 | case DeclarationName::CXXConstructorName: |
| 339 | assert(false && "Can't mangle constructors yet!"); |
| 340 | break; |
| 341 | |
| 342 | case DeclarationName::CXXDestructorName: |
| 343 | assert(false && "Can't mangle destructors yet!"); |
| 344 | break; |
| 345 | |
| 346 | case DeclarationName::CXXConversionFunctionName: |
| 347 | // <operator-name> ::= ?B # (cast) |
| 348 | // The target type is encoded as the return type. |
| 349 | Out << "?B"; |
| 350 | break; |
| 351 | |
| 352 | case DeclarationName::CXXOperatorName: |
| 353 | mangleOperatorName(Name.getCXXOverloadedOperator()); |
| 354 | break; |
| 355 | |
| 356 | case DeclarationName::CXXLiteralOperatorName: |
| 357 | // FIXME: Was this added in VS2010? Does MS even know how to mangle this? |
| 358 | assert(false && "Don't know how to mangle literal operators yet!"); |
| 359 | break; |
| 360 | |
| 361 | case DeclarationName::CXXUsingDirective: |
| 362 | assert(false && "Can't mangle a using directive name!"); |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, |
| 368 | bool NoFunction) { |
| 369 | // <postfix> ::= <unqualified-name> [<postfix>] |
| 370 | // ::= <template-postfix> <template-args> [<postfix>] |
| 371 | // ::= <template-param> |
| 372 | // ::= <substitution> [<postfix>] |
| 373 | |
| 374 | if (!DC) return; |
| 375 | |
| 376 | while (isa<LinkageSpecDecl>(DC)) |
| 377 | DC = DC->getParent(); |
| 378 | |
| 379 | if (DC->isTranslationUnit()) |
| 380 | return; |
| 381 | |
| 382 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 383 | Context.mangleBlock(BD, Out); |
| 384 | Out << '@'; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 385 | return manglePostfix(DC->getParent(), NoFunction); |
| 386 | } |
| 387 | |
| 388 | if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) |
| 389 | return; |
| 390 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) |
| 391 | mangleObjCMethodName(Method); |
| 392 | else { |
| 393 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 394 | manglePostfix(DC->getParent(), NoFunction); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) { |
| 399 | switch (OO) { |
| 400 | // ?0 # constructor |
| 401 | // ?1 # destructor |
| 402 | // <operator-name> ::= ?2 # new |
| 403 | case OO_New: Out << "?2"; break; |
| 404 | // <operator-name> ::= ?3 # delete |
| 405 | case OO_Delete: Out << "?3"; break; |
| 406 | // <operator-name> ::= ?4 # = |
| 407 | case OO_Equal: Out << "?4"; break; |
| 408 | // <operator-name> ::= ?5 # >> |
| 409 | case OO_GreaterGreater: Out << "?5"; break; |
| 410 | // <operator-name> ::= ?6 # << |
| 411 | case OO_LessLess: Out << "?6"; break; |
| 412 | // <operator-name> ::= ?7 # ! |
| 413 | case OO_Exclaim: Out << "?7"; break; |
| 414 | // <operator-name> ::= ?8 # == |
| 415 | case OO_EqualEqual: Out << "?8"; break; |
| 416 | // <operator-name> ::= ?9 # != |
| 417 | case OO_ExclaimEqual: Out << "?9"; break; |
| 418 | // <operator-name> ::= ?A # [] |
| 419 | case OO_Subscript: Out << "?A"; break; |
| 420 | // ?B # conversion |
| 421 | // <operator-name> ::= ?C # -> |
| 422 | case OO_Arrow: Out << "?C"; break; |
| 423 | // <operator-name> ::= ?D # * |
| 424 | case OO_Star: Out << "?D"; break; |
| 425 | // <operator-name> ::= ?E # ++ |
| 426 | case OO_PlusPlus: Out << "?E"; break; |
| 427 | // <operator-name> ::= ?F # -- |
| 428 | case OO_MinusMinus: Out << "?F"; break; |
| 429 | // <operator-name> ::= ?G # - |
| 430 | case OO_Minus: Out << "?G"; break; |
| 431 | // <operator-name> ::= ?H # + |
| 432 | case OO_Plus: Out << "?H"; break; |
| 433 | // <operator-name> ::= ?I # & |
| 434 | case OO_Amp: Out << "?I"; break; |
| 435 | // <operator-name> ::= ?J # ->* |
| 436 | case OO_ArrowStar: Out << "?J"; break; |
| 437 | // <operator-name> ::= ?K # / |
| 438 | case OO_Slash: Out << "?K"; break; |
| 439 | // <operator-name> ::= ?L # % |
| 440 | case OO_Percent: Out << "?L"; break; |
| 441 | // <operator-name> ::= ?M # < |
| 442 | case OO_Less: Out << "?M"; break; |
| 443 | // <operator-name> ::= ?N # <= |
| 444 | case OO_LessEqual: Out << "?N"; break; |
| 445 | // <operator-name> ::= ?O # > |
| 446 | case OO_Greater: Out << "?O"; break; |
| 447 | // <operator-name> ::= ?P # >= |
| 448 | case OO_GreaterEqual: Out << "?P"; break; |
| 449 | // <operator-name> ::= ?Q # , |
| 450 | case OO_Comma: Out << "?Q"; break; |
| 451 | // <operator-name> ::= ?R # () |
| 452 | case OO_Call: Out << "?R"; break; |
| 453 | // <operator-name> ::= ?S # ~ |
| 454 | case OO_Tilde: Out << "?S"; break; |
| 455 | // <operator-name> ::= ?T # ^ |
| 456 | case OO_Caret: Out << "?T"; break; |
| 457 | // <operator-name> ::= ?U # | |
| 458 | case OO_Pipe: Out << "?U"; break; |
| 459 | // <operator-name> ::= ?V # && |
| 460 | case OO_AmpAmp: Out << "?V"; break; |
| 461 | // <operator-name> ::= ?W # || |
| 462 | case OO_PipePipe: Out << "?W"; break; |
| 463 | // <operator-name> ::= ?X # *= |
| 464 | case OO_StarEqual: Out << "?X"; break; |
| 465 | // <operator-name> ::= ?Y # += |
| 466 | case OO_PlusEqual: Out << "?Y"; break; |
| 467 | // <operator-name> ::= ?Z # -= |
| 468 | case OO_MinusEqual: Out << "?Z"; break; |
| 469 | // <operator-name> ::= ?_0 # /= |
| 470 | case OO_SlashEqual: Out << "?_0"; break; |
| 471 | // <operator-name> ::= ?_1 # %= |
| 472 | case OO_PercentEqual: Out << "?_1"; break; |
| 473 | // <operator-name> ::= ?_2 # >>= |
| 474 | case OO_GreaterGreaterEqual: Out << "?_2"; break; |
| 475 | // <operator-name> ::= ?_3 # <<= |
| 476 | case OO_LessLessEqual: Out << "?_3"; break; |
| 477 | // <operator-name> ::= ?_4 # &= |
| 478 | case OO_AmpEqual: Out << "?_4"; break; |
| 479 | // <operator-name> ::= ?_5 # |= |
| 480 | case OO_PipeEqual: Out << "?_5"; break; |
| 481 | // <operator-name> ::= ?_6 # ^= |
| 482 | case OO_CaretEqual: Out << "?_6"; break; |
| 483 | // ?_7 # vftable |
| 484 | // ?_8 # vbtable |
| 485 | // ?_9 # vcall |
| 486 | // ?_A # typeof |
| 487 | // ?_B # local static guard |
| 488 | // ?_C # string |
| 489 | // ?_D # vbase destructor |
| 490 | // ?_E # vector deleting destructor |
| 491 | // ?_F # default constructor closure |
| 492 | // ?_G # scalar deleting destructor |
| 493 | // ?_H # vector constructor iterator |
| 494 | // ?_I # vector destructor iterator |
| 495 | // ?_J # vector vbase constructor iterator |
| 496 | // ?_K # virtual displacement map |
| 497 | // ?_L # eh vector constructor iterator |
| 498 | // ?_M # eh vector destructor iterator |
| 499 | // ?_N # eh vector vbase constructor iterator |
| 500 | // ?_O # copy constructor closure |
| 501 | // ?_P<name> # udt returning <name> |
| 502 | // ?_Q # <unknown> |
| 503 | // ?_R0 # RTTI Type Descriptor |
| 504 | // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) |
| 505 | // ?_R2 # RTTI Base Class Array |
| 506 | // ?_R3 # RTTI Class Hierarchy Descriptor |
| 507 | // ?_R4 # RTTI Complete Object Locator |
| 508 | // ?_S # local vftable |
| 509 | // ?_T # local vftable constructor closure |
| 510 | // <operator-name> ::= ?_U # new[] |
| 511 | case OO_Array_New: Out << "?_U"; break; |
| 512 | // <operator-name> ::= ?_V # delete[] |
| 513 | case OO_Array_Delete: Out << "?_V"; break; |
| 514 | |
| 515 | case OO_Conditional: |
| 516 | assert(false && "Don't know how to mangle ?:"); |
| 517 | break; |
| 518 | |
| 519 | case OO_None: |
| 520 | case NUM_OVERLOADED_OPERATORS: |
| 521 | assert(false && "Not an overloaded operator"); |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 527 | // <source name> ::= <identifier> @ |
| 528 | Out << II->getName() << '@'; |
| 529 | } |
| 530 | |
| 531 | void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 532 | llvm::SmallString<64> Buffer; |
| 533 | Context.mangleObjCMethodName(MD, Buffer); |
| 534 | Out << Buffer; |
| 535 | } |
| 536 | |
| 537 | void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, |
| 538 | bool IsMember) { |
| 539 | // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> |
| 540 | // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); |
| 541 | // 'I' means __restrict (32/64-bit). |
| 542 | // Note that the MSVC __restrict keyword isn't the same as the C99 restrict |
| 543 | // keyword! |
| 544 | // <base-cvr-qualifiers> ::= A # near |
| 545 | // ::= B # near const |
| 546 | // ::= C # near volatile |
| 547 | // ::= D # near const volatile |
| 548 | // ::= E # far (16-bit) |
| 549 | // ::= F # far const (16-bit) |
| 550 | // ::= G # far volatile (16-bit) |
| 551 | // ::= H # far const volatile (16-bit) |
| 552 | // ::= I # huge (16-bit) |
| 553 | // ::= J # huge const (16-bit) |
| 554 | // ::= K # huge volatile (16-bit) |
| 555 | // ::= L # huge const volatile (16-bit) |
| 556 | // ::= M <basis> # based |
| 557 | // ::= N <basis> # based const |
| 558 | // ::= O <basis> # based volatile |
| 559 | // ::= P <basis> # based const volatile |
| 560 | // ::= Q # near member |
| 561 | // ::= R # near const member |
| 562 | // ::= S # near volatile member |
| 563 | // ::= T # near const volatile member |
| 564 | // ::= U # far member (16-bit) |
| 565 | // ::= V # far const member (16-bit) |
| 566 | // ::= W # far volatile member (16-bit) |
| 567 | // ::= X # far const volatile member (16-bit) |
| 568 | // ::= Y # huge member (16-bit) |
| 569 | // ::= Z # huge const member (16-bit) |
| 570 | // ::= 0 # huge volatile member (16-bit) |
| 571 | // ::= 1 # huge const volatile member (16-bit) |
| 572 | // ::= 2 <basis> # based member |
| 573 | // ::= 3 <basis> # based const member |
| 574 | // ::= 4 <basis> # based volatile member |
| 575 | // ::= 5 <basis> # based const volatile member |
| 576 | // ::= 6 # near function (pointers only) |
| 577 | // ::= 7 # far function (pointers only) |
| 578 | // ::= 8 # near method (pointers only) |
| 579 | // ::= 9 # far method (pointers only) |
| 580 | // ::= _A <basis> # based function (pointers only) |
| 581 | // ::= _B <basis> # based function (far?) (pointers only) |
| 582 | // ::= _C <basis> # based method (pointers only) |
| 583 | // ::= _D <basis> # based method (far?) (pointers only) |
| 584 | // ::= _E # block (Clang) |
| 585 | // <basis> ::= 0 # __based(void) |
| 586 | // ::= 1 # __based(segment)? |
| 587 | // ::= 2 <name> # __based(name) |
| 588 | // ::= 3 # ? |
| 589 | // ::= 4 # ? |
| 590 | // ::= 5 # not really based |
| 591 | if (!IsMember) { |
| 592 | if (!Quals.hasVolatile()) { |
| 593 | if (!Quals.hasConst()) |
| 594 | Out << 'A'; |
| 595 | else |
| 596 | Out << 'B'; |
| 597 | } else { |
| 598 | if (!Quals.hasConst()) |
| 599 | Out << 'C'; |
| 600 | else |
| 601 | Out << 'D'; |
| 602 | } |
| 603 | } else { |
| 604 | if (!Quals.hasVolatile()) { |
| 605 | if (!Quals.hasConst()) |
| 606 | Out << 'Q'; |
| 607 | else |
| 608 | Out << 'R'; |
| 609 | } else { |
| 610 | if (!Quals.hasConst()) |
| 611 | Out << 'S'; |
| 612 | else |
| 613 | Out << 'T'; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | // FIXME: For now, just drop all extension qualifiers on the floor. |
| 618 | } |
| 619 | |
| 620 | void MicrosoftCXXNameMangler::mangleType(QualType T) { |
| 621 | // Only operate on the canonical type! |
| 622 | T = getASTContext().getCanonicalType(T); |
| 623 | |
| 624 | Qualifiers Quals = T.getLocalQualifiers(); |
| 625 | if (Quals) { |
| 626 | // We have to mangle these now, while we still have enough information. |
| 627 | // <pointer-cvr-qualifiers> ::= P # pointer |
| 628 | // ::= Q # const pointer |
| 629 | // ::= R # volatile pointer |
| 630 | // ::= S # const volatile pointer |
| 631 | if (T->isAnyPointerType() || T->isMemberPointerType() || |
| 632 | T->isBlockPointerType()) { |
| 633 | if (!Quals.hasVolatile()) |
| 634 | Out << 'Q'; |
| 635 | else { |
| 636 | if (!Quals.hasConst()) |
| 637 | Out << 'R'; |
| 638 | else |
| 639 | Out << 'S'; |
| 640 | } |
| 641 | } else |
| 642 | // Just emit qualifiers like normal. |
| 643 | // NB: When we mangle a pointer/reference type, and the pointee |
| 644 | // type has no qualifiers, the lack of qualifier gets mangled |
| 645 | // in there. |
| 646 | mangleQualifiers(Quals, false); |
| 647 | } else if (T->isAnyPointerType() || T->isMemberPointerType() || |
| 648 | T->isBlockPointerType()) { |
| 649 | Out << 'P'; |
| 650 | } |
| 651 | switch (T->getTypeClass()) { |
| 652 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 653 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
| 654 | case Type::CLASS: \ |
| 655 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
| 656 | return; |
| 657 | #define TYPE(CLASS, PARENT) \ |
| 658 | case Type::CLASS: \ |
| 659 | mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \ |
| 660 | break; |
| 661 | #include "clang/AST/TypeNodes.def" |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) { |
| 666 | // <type> ::= <builtin-type> |
| 667 | // <builtin-type> ::= X # void |
| 668 | // ::= C # signed char |
| 669 | // ::= D # char |
| 670 | // ::= E # unsigned char |
| 671 | // ::= F # short |
| 672 | // ::= G # unsigned short (or wchar_t if it's not a builtin) |
| 673 | // ::= H # int |
| 674 | // ::= I # unsigned int |
| 675 | // ::= J # long |
| 676 | // ::= K # unsigned long |
| 677 | // L # <none> |
| 678 | // ::= M # float |
| 679 | // ::= N # double |
| 680 | // ::= O # long double (__float80 is mangled differently) |
| 681 | // ::= _D # __int8 (yup, it's a distinct type in MSVC) |
| 682 | // ::= _E # unsigned __int8 |
| 683 | // ::= _F # __int16 |
| 684 | // ::= _G # unsigned __int16 |
| 685 | // ::= _H # __int32 |
| 686 | // ::= _I # unsigned __int32 |
| 687 | // ::= _J # long long, __int64 |
| 688 | // ::= _K # unsigned long long, __int64 |
| 689 | // ::= _L # __int128 |
| 690 | // ::= _M # unsigned __int128 |
| 691 | // ::= _N # bool |
| 692 | // _O # <array in parameter> |
| 693 | // ::= _T # __float80 (Intel) |
| 694 | // ::= _W # wchar_t |
| 695 | // ::= _Z # __float80 (Digital Mars) |
| 696 | switch (T->getKind()) { |
| 697 | case BuiltinType::Void: Out << 'X'; break; |
| 698 | case BuiltinType::SChar: Out << 'C'; break; |
| 699 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; |
| 700 | case BuiltinType::UChar: Out << 'E'; break; |
| 701 | case BuiltinType::Short: Out << 'F'; break; |
| 702 | case BuiltinType::UShort: Out << 'G'; break; |
| 703 | case BuiltinType::Int: Out << 'H'; break; |
| 704 | case BuiltinType::UInt: Out << 'I'; break; |
| 705 | case BuiltinType::Long: Out << 'J'; break; |
| 706 | case BuiltinType::ULong: Out << 'K'; break; |
| 707 | case BuiltinType::Float: Out << 'M'; break; |
| 708 | case BuiltinType::Double: Out << 'N'; break; |
| 709 | // TODO: Determine size and mangle accordingly |
| 710 | case BuiltinType::LongDouble: Out << 'O'; break; |
| 711 | // TODO: __int8 and friends |
| 712 | case BuiltinType::LongLong: Out << "_J"; break; |
| 713 | case BuiltinType::ULongLong: Out << "_K"; break; |
| 714 | case BuiltinType::Int128: Out << "_L"; break; |
| 715 | case BuiltinType::UInt128: Out << "_M"; break; |
| 716 | case BuiltinType::Bool: Out << "_N"; break; |
| 717 | case BuiltinType::WChar_S: |
| 718 | case BuiltinType::WChar_U: Out << "_W"; break; |
| 719 | |
| 720 | case BuiltinType::Overload: |
| 721 | case BuiltinType::Dependent: |
| 722 | assert(false && |
| 723 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 724 | break; |
| 725 | case BuiltinType::UndeducedAuto: |
| 726 | assert(0 && "Should not see undeduced auto here"); |
| 727 | break; |
| 728 | case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; |
| 729 | case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; |
| 730 | case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; |
| 731 | |
| 732 | case BuiltinType::Char16: |
| 733 | case BuiltinType::Char32: |
| 734 | case BuiltinType::NullPtr: |
| 735 | assert(false && "Don't know how to mangle this type"); |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | // <type> ::= <function-type> |
| 741 | void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) { |
| 742 | // Structors only appear in decls, so at this point we know it's not a |
| 743 | // structor type. |
| 744 | // I'll probably have mangleType(MemberPointerType) call the mangleType() |
| 745 | // method directly. |
| 746 | mangleType(T, NULL, false, false); |
| 747 | } |
| 748 | void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
| 749 | llvm_unreachable("Can't mangle K&R function prototypes"); |
| 750 | } |
| 751 | |
| 752 | void MicrosoftCXXNameMangler::mangleType(const FunctionType *T, |
| 753 | const FunctionDecl *D, |
| 754 | bool IsStructor, |
| 755 | bool IsInstMethod) { |
| 756 | // <function-type> ::= <this-cvr-qualifiers> <calling-convention> |
| 757 | // <return-type> <argument-list> <throw-spec> |
| 758 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 759 | |
| 760 | // If this is a C++ instance method, mangle the CVR qualifiers for the |
| 761 | // this pointer. |
| 762 | if (IsInstMethod) |
| 763 | mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false); |
| 764 | |
| 765 | mangleCallingConvention(T, IsInstMethod); |
| 766 | |
| 767 | // <return-type> ::= <type> |
| 768 | // ::= @ # structors (they have no declared return type) |
| 769 | if (IsStructor) |
| 770 | Out << '@'; |
| 771 | else |
| 772 | mangleType(Proto->getResultType()); |
| 773 | |
| 774 | // <argument-list> ::= X # void |
| 775 | // ::= <type>+ @ |
| 776 | // ::= <type>* Z # varargs |
| 777 | if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { |
| 778 | Out << 'X'; |
| 779 | } else { |
| 780 | if (D) { |
| 781 | // If we got a decl, use the "types-as-written" to make sure arrays |
| 782 | // get mangled right. |
| 783 | for (FunctionDecl::param_const_iterator Parm = D->param_begin(), |
| 784 | ParmEnd = D->param_end(); |
| 785 | Parm != ParmEnd; ++Parm) |
| 786 | mangleType((*Parm)->getTypeSourceInfo()->getType()); |
| 787 | } else { |
| 788 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
| 789 | ArgEnd = Proto->arg_type_end(); |
| 790 | Arg != ArgEnd; ++Arg) |
| 791 | mangleType(*Arg); |
| 792 | } |
| 793 | // <builtin-type> ::= Z # ellipsis |
| 794 | if (Proto->isVariadic()) |
| 795 | Out << 'Z'; |
| 796 | else |
| 797 | Out << '@'; |
| 798 | } |
| 799 | |
| 800 | mangleThrowSpecification(Proto); |
| 801 | } |
| 802 | |
| 803 | void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { |
| 804 | // <function-class> ::= A # private: near |
| 805 | // ::= B # private: far |
| 806 | // ::= C # private: static near |
| 807 | // ::= D # private: static far |
| 808 | // ::= E # private: virtual near |
| 809 | // ::= F # private: virtual far |
| 810 | // ::= G # private: thunk near |
| 811 | // ::= H # private: thunk far |
| 812 | // ::= I # protected: near |
| 813 | // ::= J # protected: far |
| 814 | // ::= K # protected: static near |
| 815 | // ::= L # protected: static far |
| 816 | // ::= M # protected: virtual near |
| 817 | // ::= N # protected: virtual far |
| 818 | // ::= O # protected: thunk near |
| 819 | // ::= P # protected: thunk far |
| 820 | // ::= Q # public: near |
| 821 | // ::= R # public: far |
| 822 | // ::= S # public: static near |
| 823 | // ::= T # public: static far |
| 824 | // ::= U # public: virtual near |
| 825 | // ::= V # public: virtual far |
| 826 | // ::= W # public: thunk near |
| 827 | // ::= X # public: thunk far |
| 828 | // ::= Y # global near |
| 829 | // ::= Z # global far |
| 830 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 831 | switch (MD->getAccess()) { |
| 832 | default: |
| 833 | case AS_private: |
| 834 | if (MD->isStatic()) |
| 835 | Out << 'C'; |
| 836 | else if (MD->isVirtual()) |
| 837 | Out << 'E'; |
| 838 | else |
| 839 | Out << 'A'; |
| 840 | break; |
| 841 | case AS_protected: |
| 842 | if (MD->isStatic()) |
| 843 | Out << 'K'; |
| 844 | else if (MD->isVirtual()) |
| 845 | Out << 'M'; |
| 846 | else |
| 847 | Out << 'I'; |
| 848 | break; |
| 849 | case AS_public: |
| 850 | if (MD->isStatic()) |
| 851 | Out << 'S'; |
| 852 | else if (MD->isVirtual()) |
| 853 | Out << 'U'; |
| 854 | else |
| 855 | Out << 'Q'; |
| 856 | } |
| 857 | } else |
| 858 | Out << 'Y'; |
| 859 | } |
| 860 | void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T, |
| 861 | bool IsInstMethod) { |
| 862 | // <calling-convention> ::= A # __cdecl |
| 863 | // ::= B # __export __cdecl |
| 864 | // ::= C # __pascal |
| 865 | // ::= D # __export __pascal |
| 866 | // ::= E # __thiscall |
| 867 | // ::= F # __export __thiscall |
| 868 | // ::= G # __stdcall |
| 869 | // ::= H # __export __stdcall |
| 870 | // ::= I # __fastcall |
| 871 | // ::= J # __export __fastcall |
| 872 | // The 'export' calling conventions are from a bygone era |
| 873 | // (*cough*Win16*cough*) when functions were declared for export with |
| 874 | // that keyword. (It didn't actually export them, it just made them so |
| 875 | // that they could be in a DLL and somebody from another module could call |
| 876 | // them.) |
| 877 | CallingConv CC = T->getCallConv(); |
| 878 | if (CC == CC_Default) |
| 879 | CC = IsInstMethod ? getASTContext().getDefaultMethodCallConv() : CC_C; |
| 880 | switch (CC) { |
| 881 | case CC_Default: |
| 882 | case CC_C: Out << 'A'; break; |
| 883 | case CC_X86Pascal: Out << 'C'; break; |
| 884 | case CC_X86ThisCall: Out << 'E'; break; |
| 885 | case CC_X86StdCall: Out << 'G'; break; |
| 886 | case CC_X86FastCall: Out << 'I'; break; |
| 887 | } |
| 888 | } |
| 889 | void MicrosoftCXXNameMangler::mangleThrowSpecification( |
| 890 | const FunctionProtoType *FT) { |
| 891 | // <throw-spec> ::= Z # throw(...) (default) |
| 892 | // ::= @ # throw() or __declspec/__attribute__((nothrow)) |
| 893 | // ::= <type>+ |
| 894 | // NOTE: Since the Microsoft compiler ignores throw specifications, they are |
| 895 | // all actually mangled as 'Z'. (They're ignored because their associated |
| 896 | // functionality isn't implemented, and probably never will be.) |
| 897 | Out << 'Z'; |
| 898 | } |
| 899 | |
| 900 | void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T) { |
| 901 | assert(false && "Don't know how to mangle UnresolvedUsingTypes yet!"); |
| 902 | } |
| 903 | |
| 904 | // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> |
| 905 | // <union-type> ::= T <name> |
| 906 | // <struct-type> ::= U <name> |
| 907 | // <class-type> ::= V <name> |
| 908 | // <enum-type> ::= W <size> <name> |
| 909 | void MicrosoftCXXNameMangler::mangleType(const EnumType *T) { |
| 910 | mangleType(static_cast<const TagType*>(T)); |
| 911 | } |
| 912 | void MicrosoftCXXNameMangler::mangleType(const RecordType *T) { |
| 913 | mangleType(static_cast<const TagType*>(T)); |
| 914 | } |
| 915 | void MicrosoftCXXNameMangler::mangleType(const TagType *T) { |
| 916 | switch (T->getDecl()->getTagKind()) { |
| 917 | case TTK_Union: |
| 918 | Out << 'T'; |
| 919 | break; |
| 920 | case TTK_Struct: |
| 921 | Out << 'U'; |
| 922 | break; |
| 923 | case TTK_Class: |
| 924 | Out << 'V'; |
| 925 | break; |
| 926 | case TTK_Enum: |
| 927 | Out << 'W'; |
| 928 | Out << getASTContext().getTypeSizeInChars( |
| 929 | cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity(); |
| 930 | break; |
| 931 | } |
| 932 | mangleName(T->getDecl()); |
| 933 | } |
| 934 | |
| 935 | // <type> ::= <array-type> |
| 936 | // <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+] |
| 937 | // <element-type> # as global |
| 938 | // ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+] |
| 939 | // <element-type> # as param |
| 940 | // It's supposed to be the other way around, but for some strange reason, it |
| 941 | // isn't. Today this behavior is retained for the sole purpose of backwards |
| 942 | // compatibility. |
| 943 | void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) { |
| 944 | // This isn't a recursive mangling, so now we have to do it all in this |
| 945 | // one call. |
| 946 | if (IsGlobal) |
| 947 | Out << 'P'; |
| 948 | else |
| 949 | Out << 'Q'; |
| 950 | mangleExtraDimensions(T->getElementType()); |
| 951 | } |
| 952 | void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T) { |
| 953 | mangleType(static_cast<const ArrayType *>(T), false); |
| 954 | } |
| 955 | void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T) { |
| 956 | mangleType(static_cast<const ArrayType *>(T), false); |
| 957 | } |
| 958 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T) { |
| 959 | mangleType(static_cast<const ArrayType *>(T), false); |
| 960 | } |
| 961 | void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T) { |
| 962 | mangleType(static_cast<const ArrayType *>(T), false); |
| 963 | } |
| 964 | void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) { |
| 965 | llvm::SmallVector<llvm::APInt, 3> Dimensions; |
| 966 | for (;;) { |
| 967 | if (ElementTy->isConstantArrayType()) { |
| 968 | const ConstantArrayType *CAT = |
| 969 | static_cast<const ConstantArrayType *>(ElementTy.getTypePtr()); |
| 970 | Dimensions.push_back(CAT->getSize()); |
| 971 | ElementTy = CAT->getElementType(); |
| 972 | } else if (ElementTy->isVariableArrayType()) { |
| 973 | assert(false && "Don't know how to mangle VLAs!"); |
| 974 | } else if (ElementTy->isDependentSizedArrayType()) { |
| 975 | // The dependent expression has to be folded into a constant (TODO). |
| 976 | assert(false && "Don't know how to mangle dependent-sized arrays!"); |
| 977 | } else if (ElementTy->isIncompleteArrayType()) continue; |
| 978 | else break; |
| 979 | } |
| 980 | mangleQualifiers(ElementTy.getQualifiers(), false); |
| 981 | // If there are any additional dimensions, mangle them now. |
| 982 | if (Dimensions.size() > 0) { |
| 983 | Out << 'Y'; |
| 984 | // <dimension-count> ::= <number> # number of extra dimensions |
| 985 | mangleNumber(Dimensions.size()); |
| 986 | for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) { |
| 987 | mangleNumber(Dimensions[Dim].getLimitedValue()); |
| 988 | } |
| 989 | } |
| 990 | mangleType(ElementTy.getLocalUnqualifiedType()); |
| 991 | } |
| 992 | |
| 993 | // <type> ::= <pointer-to-member-type> |
| 994 | // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> |
| 995 | // <class name> <type> |
| 996 | void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T) { |
| 997 | QualType PointeeType = T->getPointeeType(); |
| 998 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
| 999 | Out << '8'; |
| 1000 | mangleName(cast<RecordType>(T->getClass())->getDecl()); |
| 1001 | mangleType(FPT, NULL, false, true); |
| 1002 | } else { |
| 1003 | mangleQualifiers(PointeeType.getQualifiers(), true); |
| 1004 | mangleName(cast<RecordType>(T->getClass())->getDecl()); |
| 1005 | mangleType(PointeeType.getLocalUnqualifiedType()); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
| 1010 | assert(false && "Don't know how to mangle TemplateTypeParmTypes yet!"); |
| 1011 | } |
| 1012 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1013 | void MicrosoftCXXNameMangler::mangleType( |
| 1014 | const SubstTemplateTypeParmPackType *T) { |
| 1015 | assert(false && |
| 1016 | "Don't know how to mangle SubstTemplateTypeParmPackTypes yet!"); |
| 1017 | } |
| 1018 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1019 | // <type> ::= <pointer-type> |
| 1020 | // <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type> |
| 1021 | void MicrosoftCXXNameMangler::mangleType(const PointerType *T) { |
| 1022 | QualType PointeeTy = T->getPointeeType(); |
| 1023 | if (PointeeTy->isArrayType()) { |
| 1024 | // Pointers to arrays are mangled like arrays. |
| 1025 | mangleExtraDimensions(T->getPointeeType()); |
| 1026 | } else if (PointeeTy->isFunctionType()) { |
| 1027 | // Function pointers are special. |
| 1028 | Out << '6'; |
| 1029 | mangleType(static_cast<const FunctionType *>(PointeeTy.getTypePtr()), |
| 1030 | NULL, false, false); |
| 1031 | } else { |
| 1032 | if (!PointeeTy.hasQualifiers()) |
| 1033 | // Lack of qualifiers is mangled as 'A'. |
| 1034 | Out << 'A'; |
| 1035 | mangleType(PointeeTy); |
| 1036 | } |
| 1037 | } |
| 1038 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T) { |
| 1039 | // Object pointers never have qualifiers. |
| 1040 | Out << 'A'; |
| 1041 | mangleType(T->getPointeeType()); |
| 1042 | } |
| 1043 | |
| 1044 | // <type> ::= <reference-type> |
| 1045 | // <reference-type> ::= A <cvr-qualifiers> <type> |
| 1046 | void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 1047 | Out << 'A'; |
| 1048 | QualType PointeeTy = T->getPointeeType(); |
| 1049 | if (!PointeeTy.hasQualifiers()) |
| 1050 | // Lack of qualifiers is mangled as 'A'. |
| 1051 | Out << 'A'; |
| 1052 | mangleType(PointeeTy); |
| 1053 | } |
| 1054 | |
| 1055 | void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T) { |
| 1056 | assert(false && "Don't know how to mangle RValueReferenceTypes yet!"); |
| 1057 | } |
| 1058 | |
| 1059 | void MicrosoftCXXNameMangler::mangleType(const ComplexType *T) { |
| 1060 | assert(false && "Don't know how to mangle ComplexTypes yet!"); |
| 1061 | } |
| 1062 | |
| 1063 | void MicrosoftCXXNameMangler::mangleType(const VectorType *T) { |
| 1064 | assert(false && "Don't know how to mangle VectorTypes yet!"); |
| 1065 | } |
| 1066 | void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T) { |
| 1067 | assert(false && "Don't know how to mangle ExtVectorTypes yet!"); |
| 1068 | } |
| 1069 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { |
| 1070 | assert(false && "Don't know how to mangle DependentSizedExtVectorTypes yet!"); |
| 1071 | } |
| 1072 | |
| 1073 | void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 1074 | // ObjC interfaces have structs underlying them. |
| 1075 | Out << 'U'; |
| 1076 | mangleName(T->getDecl()); |
| 1077 | } |
| 1078 | |
| 1079 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T) { |
| 1080 | // We don't allow overloading by different protocol qualification, |
| 1081 | // so mangling them isn't necessary. |
| 1082 | mangleType(T->getBaseType()); |
| 1083 | } |
| 1084 | |
| 1085 | void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T) { |
| 1086 | Out << "_E"; |
| 1087 | mangleType(T->getPointeeType()); |
| 1088 | } |
| 1089 | |
| 1090 | void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T) { |
| 1091 | assert(false && "Don't know how to mangle InjectedClassNameTypes yet!"); |
| 1092 | } |
| 1093 | |
| 1094 | void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T) { |
| 1095 | assert(false && "Don't know how to mangle TemplateSpecializationTypes yet!"); |
| 1096 | } |
| 1097 | |
| 1098 | void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T) { |
| 1099 | assert(false && "Don't know how to mangle DependentNameTypes yet!"); |
| 1100 | } |
| 1101 | |
| 1102 | void MicrosoftCXXNameMangler::mangleType( |
| 1103 | const DependentTemplateSpecializationType *T) { |
| 1104 | assert(false && |
| 1105 | "Don't know how to mangle DependentTemplateSpecializationTypes yet!"); |
| 1106 | } |
| 1107 | |
| 1108 | void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T) { |
| 1109 | assert(false && "Don't know how to mangle PackExpansionTypes yet!"); |
| 1110 | } |
| 1111 | |
| 1112 | void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T) { |
| 1113 | assert(false && "Don't know how to mangle TypeOfTypes yet!"); |
| 1114 | } |
| 1115 | |
| 1116 | void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T) { |
| 1117 | assert(false && "Don't know how to mangle TypeOfExprTypes yet!"); |
| 1118 | } |
| 1119 | |
| 1120 | void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) { |
| 1121 | assert(false && "Don't know how to mangle DecltypeTypes yet!"); |
| 1122 | } |
| 1123 | |
| 1124 | void MicrosoftMangleContext::mangleName(const NamedDecl *D, |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 1125 | llvm::raw_svector_ostream &Out) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1126 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 1127 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 1128 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 1129 | "Invalid mangleName() call on 'structor decl!"); |
| 1130 | |
| 1131 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 1132 | getASTContext().getSourceManager(), |
| 1133 | "Mangling declaration"); |
| 1134 | |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 1135 | MicrosoftCXXNameMangler Mangler(*this, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1136 | return Mangler.mangle(D); |
| 1137 | } |
| 1138 | void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD, |
| 1139 | const ThunkInfo &Thunk, |
| 1140 | llvm::SmallVectorImpl<char> &) { |
| 1141 | assert(false && "Can't yet mangle thunks!"); |
| 1142 | } |
| 1143 | void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, |
| 1144 | CXXDtorType Type, |
| 1145 | const ThisAdjustment &, |
| 1146 | llvm::SmallVectorImpl<char> &) { |
| 1147 | assert(false && "Can't yet mangle destructor thunks!"); |
| 1148 | } |
| 1149 | void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, |
| 1150 | llvm::SmallVectorImpl<char> &) { |
| 1151 | assert(false && "Can't yet mangle virtual tables!"); |
| 1152 | } |
| 1153 | void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
| 1154 | llvm::SmallVectorImpl<char> &) { |
| 1155 | llvm_unreachable("The MS C++ ABI does not have virtual table tables!"); |
| 1156 | } |
| 1157 | void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, |
| 1158 | int64_t Offset, |
| 1159 | const CXXRecordDecl *Type, |
| 1160 | llvm::SmallVectorImpl<char> &) { |
| 1161 | llvm_unreachable("The MS C++ ABI does not have constructor vtables!"); |
| 1162 | } |
| 1163 | void MicrosoftMangleContext::mangleCXXRTTI(QualType T, |
| 1164 | llvm::SmallVectorImpl<char> &) { |
| 1165 | assert(false && "Can't yet mangle RTTI!"); |
| 1166 | } |
| 1167 | void MicrosoftMangleContext::mangleCXXRTTIName(QualType T, |
| 1168 | llvm::SmallVectorImpl<char> &) { |
| 1169 | assert(false && "Can't yet mangle RTTI names!"); |
| 1170 | } |
| 1171 | void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, |
| 1172 | CXXCtorType Type, |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 1173 | llvm::raw_svector_ostream &) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1174 | assert(false && "Can't yet mangle constructors!"); |
| 1175 | } |
| 1176 | void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, |
| 1177 | CXXDtorType Type, |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame^] | 1178 | llvm::raw_svector_ostream &) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1179 | assert(false && "Can't yet mangle destructors!"); |
| 1180 | } |
| 1181 | void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *, |
| 1182 | llvm::SmallVectorImpl<char> &) { |
| 1183 | assert(false && "Can't yet mangle reference temporaries!"); |
| 1184 | } |
| 1185 | |
| 1186 | MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context, |
| 1187 | Diagnostic &Diags) { |
| 1188 | return new MicrosoftMangleContext(Context, Diags); |
| 1189 | } |