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 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ name mangling targeting the Microsoft Visual C++ ABI. |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 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; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 32 | raw_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: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 37 | MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 38 | : Context(C), Out(Out_) { } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 39 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 40 | raw_ostream &getStream() const { return Out; } |
| 41 | |
| 42 | void mangle(const NamedDecl *D, StringRef Prefix = "\01?"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 43 | void mangleName(const NamedDecl *ND); |
| 44 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 45 | void mangleVariableEncoding(const VarDecl *VD); |
| 46 | void mangleNumber(int64_t Number); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 47 | void mangleNumber(const llvm::APSInt &Value); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 48 | void mangleType(QualType T, SourceRange Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 52 | mangleUnqualifiedName(ND, ND->getDeclName()); |
| 53 | } |
| 54 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); |
| 55 | void mangleSourceName(const IdentifierInfo *II); |
| 56 | void manglePostfix(const DeclContext *DC, bool NoFunction=false); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 57 | void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 58 | void mangleQualifiers(Qualifiers Quals, bool IsMember); |
| 59 | |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 60 | void mangleUnscopedTemplateName(const TemplateDecl *ND); |
| 61 | void mangleTemplateInstantiationName(const TemplateDecl *TD, |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 62 | const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 63 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 64 | void mangleLocalName(const FunctionDecl *FD); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 65 | |
| 66 | // Declare manglers for every type class. |
| 67 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 68 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 69 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \ |
| 70 | SourceRange Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 71 | #include "clang/AST/TypeNodes.def" |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 72 | #undef ABSTRACT_TYPE |
| 73 | #undef NON_CANONICAL_TYPE |
| 74 | #undef TYPE |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 75 | |
| 76 | void mangleType(const TagType*); |
| 77 | void mangleType(const FunctionType *T, const FunctionDecl *D, |
| 78 | bool IsStructor, bool IsInstMethod); |
| 79 | void mangleType(const ArrayType *T, bool IsGlobal); |
| 80 | void mangleExtraDimensions(QualType T); |
| 81 | void mangleFunctionClass(const FunctionDecl *FD); |
| 82 | void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 83 | void mangleIntegerLiteral(QualType T, const llvm::APSInt &Number); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 84 | void mangleThrowSpecification(const FunctionProtoType *T); |
| 85 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 86 | void mangleTemplateArgs( |
| 87 | const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 88 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /// MicrosoftMangleContext - Overrides the default MangleContext for the |
| 92 | /// Microsoft Visual C++ ABI. |
| 93 | class MicrosoftMangleContext : public MangleContext { |
| 94 | public: |
| 95 | MicrosoftMangleContext(ASTContext &Context, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 96 | DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 97 | virtual bool shouldMangleDeclName(const NamedDecl *D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 98 | virtual void mangleName(const NamedDecl *D, raw_ostream &Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 99 | virtual void mangleThunk(const CXXMethodDecl *MD, |
| 100 | const ThunkInfo &Thunk, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 101 | raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 102 | virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 103 | const ThisAdjustment &ThisAdjustment, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 104 | raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 105 | virtual void mangleCXXVTable(const CXXRecordDecl *RD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 106 | raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 107 | virtual void mangleCXXVTT(const CXXRecordDecl *RD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 109 | virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, |
| 110 | const CXXRecordDecl *Type, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 111 | raw_ostream &); |
| 112 | virtual void mangleCXXRTTI(QualType T, raw_ostream &); |
| 113 | virtual void mangleCXXRTTIName(QualType T, raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 114 | virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 115 | raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 116 | virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 117 | raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 118 | virtual void mangleReferenceTemporary(const clang::VarDecl *, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 119 | raw_ostream &); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } |
| 123 | |
| 124 | static bool isInCLinkageSpecification(const Decl *D) { |
| 125 | D = D->getCanonicalDecl(); |
| 126 | for (const DeclContext *DC = D->getDeclContext(); |
| 127 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 128 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
| 129 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 136 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 137 | if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs()) |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 138 | return false; |
| 139 | |
| 140 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 141 | // over all other naming in the .o file. |
| 142 | if (D->hasAttr<AsmLabelAttr>()) |
| 143 | return true; |
| 144 | |
| 145 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
| 146 | // (always) as does passing a C++ member function and a function |
| 147 | // whose name is not a simple identifier. |
| 148 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 149 | if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || |
| 150 | !FD->getDeclName().isIdentifier())) |
| 151 | return true; |
| 152 | |
| 153 | // Otherwise, no mangling is done outside C++ mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 154 | if (!getASTContext().getLangOpts().CPlusPlus) |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 155 | return false; |
| 156 | |
| 157 | // Variables at global scope with internal linkage are not mangled. |
| 158 | if (!FD) { |
| 159 | const DeclContext *DC = D->getDeclContext(); |
| 160 | if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage) |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | // C functions and "main" are not mangled. |
| 165 | if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) |
| 166 | return false; |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 172 | StringRef Prefix) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 173 | // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. |
| 174 | // Therefore it's really important that we don't decorate the |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 175 | // name with leading underscores or leading/trailing at signs. So, by |
| 176 | // default, we emit an asm marker at the start so we get the name right. |
| 177 | // Callers can override this with a custom prefix. |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 178 | |
| 179 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 180 | // over all other naming in the .o file. |
| 181 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 182 | // If we have an asm name, then we use it as the mangling. |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 183 | Out << '\01' << ALA->getLabel(); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | // <mangled-name> ::= ? <name> <type-encoding> |
| 188 | Out << Prefix; |
| 189 | mangleName(D); |
| 190 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 191 | mangleFunctionEncoding(FD); |
| 192 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 193 | mangleVariableEncoding(VD); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 194 | else { |
| 195 | // TODO: Fields? Can MSVC even mangle them? |
| 196 | // Issue a diagnostic for now. |
| 197 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 198 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 199 | "cannot mangle this declaration yet"); |
| 200 | Diags.Report(D->getLocation(), DiagID) |
| 201 | << D->getSourceRange(); |
| 202 | } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 206 | // <type-encoding> ::= <function-class> <function-type> |
| 207 | |
| 208 | // Don't mangle in the type if this isn't a decl we should typically mangle. |
| 209 | if (!Context.shouldMangleDeclName(FD)) |
| 210 | return; |
| 211 | |
| 212 | // We should never ever see a FunctionNoProtoType at this point. |
| 213 | // We don't even know how to mangle their types anyway :). |
Richard Smith | bd1d18e | 2012-06-04 22:46:59 +0000 | [diff] [blame] | 214 | const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>(); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 215 | |
| 216 | bool InStructor = false, InInstMethod = false; |
| 217 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 218 | if (MD) { |
| 219 | if (MD->isInstance()) |
| 220 | InInstMethod = true; |
| 221 | if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) |
| 222 | InStructor = true; |
| 223 | } |
| 224 | |
| 225 | // First, the function class. |
| 226 | mangleFunctionClass(FD); |
| 227 | |
| 228 | mangleType(FT, FD, InStructor, InInstMethod); |
| 229 | } |
| 230 | |
| 231 | void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { |
| 232 | // <type-encoding> ::= <storage-class> <variable-type> |
| 233 | // <storage-class> ::= 0 # private static member |
| 234 | // ::= 1 # protected static member |
| 235 | // ::= 2 # public static member |
| 236 | // ::= 3 # global |
| 237 | // ::= 4 # static local |
| 238 | |
| 239 | // The first character in the encoding (after the name) is the storage class. |
| 240 | if (VD->isStaticDataMember()) { |
| 241 | // If it's a static member, it also encodes the access level. |
| 242 | switch (VD->getAccess()) { |
| 243 | default: |
| 244 | case AS_private: Out << '0'; break; |
| 245 | case AS_protected: Out << '1'; break; |
| 246 | case AS_public: Out << '2'; break; |
| 247 | } |
| 248 | } |
| 249 | else if (!VD->isStaticLocal()) |
| 250 | Out << '3'; |
| 251 | else |
| 252 | Out << '4'; |
| 253 | // Now mangle the type. |
| 254 | // <variable-type> ::= <type> <cvr-qualifiers> |
| 255 | // ::= <type> A # pointers, references, arrays |
| 256 | // Pointers and references are odd. The type of 'int * const foo;' gets |
| 257 | // mangled as 'QAHA' instead of 'PAHB', for example. |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 258 | TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc(); |
| 259 | QualType Ty = TL.getType(); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 260 | if (Ty->isPointerType() || Ty->isReferenceType()) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 261 | mangleType(Ty, TL.getSourceRange()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 262 | Out << 'A'; |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 263 | } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 264 | // Global arrays are funny, too. |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 265 | mangleType(AT, true); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 266 | Out << 'A'; |
| 267 | } else { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 268 | mangleType(Ty.getLocalUnqualifiedType(), TL.getSourceRange()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 269 | mangleQualifiers(Ty.getLocalQualifiers(), false); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { |
| 274 | // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ |
| 275 | const DeclContext *DC = ND->getDeclContext(); |
| 276 | |
| 277 | // Always start with the unqualified name. |
| 278 | mangleUnqualifiedName(ND); |
| 279 | |
| 280 | // If this is an extern variable declared locally, the relevant DeclContext |
| 281 | // is that of the containing namespace, or the translation unit. |
| 282 | if (isa<FunctionDecl>(DC) && ND->hasLinkage()) |
| 283 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 284 | DC = DC->getParent(); |
| 285 | |
| 286 | manglePostfix(DC); |
| 287 | |
| 288 | // Terminate the whole name with an '@'. |
| 289 | Out << '@'; |
| 290 | } |
| 291 | |
| 292 | void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 293 | // <number> ::= [?] <decimal digit> # 1 <= Number <= 10 |
| 294 | // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc... |
| 295 | // ::= [?] @ # 0 (alternate mangling, not emitted by VC) |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 296 | if (Number < 0) { |
| 297 | Out << '?'; |
| 298 | Number = -Number; |
| 299 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 300 | // There's a special shorter mangling for 0, but Microsoft |
| 301 | // chose not to use it. Instead, 0 gets mangled as "A@". Oh well... |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 302 | if (Number >= 1 && Number <= 10) |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 303 | Out << Number-1; |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 304 | else { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 305 | // We have to build up the encoding in reverse order, so it will come |
| 306 | // out right when we write it out. |
| 307 | char Encoding[16]; |
| 308 | char *EndPtr = Encoding+sizeof(Encoding); |
| 309 | char *CurPtr = EndPtr; |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 310 | do { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 311 | *--CurPtr = 'A' + (Number % 16); |
| 312 | Number /= 16; |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 313 | } while (Number); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 314 | Out.write(CurPtr, EndPtr-CurPtr); |
| 315 | Out << '@'; |
| 316 | } |
| 317 | } |
| 318 | |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 319 | void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) { |
| 320 | if (Value.isSigned() && Value.isNegative()) { |
| 321 | Out << '?'; |
| 322 | mangleNumber(llvm::APSInt(Value.abs())); |
| 323 | return; |
| 324 | } |
Charles Davis | c4d7675 | 2012-05-29 07:01:45 +0000 | [diff] [blame] | 325 | llvm::APSInt Temp(Value); |
| 326 | if (Value.uge(1) && Value.ule(10)) { |
| 327 | --Temp; |
| 328 | Temp.print(Out, false); |
| 329 | } else { |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 330 | // We have to build up the encoding in reverse order, so it will come |
| 331 | // out right when we write it out. |
| 332 | char Encoding[64]; |
| 333 | char *EndPtr = Encoding+sizeof(Encoding); |
| 334 | char *CurPtr = EndPtr; |
Charles Davis | 104e51f | 2012-05-28 03:54:22 +0000 | [diff] [blame] | 335 | llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned()); |
Lang Hames | f17523b | 2012-05-27 21:39:49 +0000 | [diff] [blame] | 336 | NibbleMask = 0xf; |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 337 | for (int i = 0, e = Value.getActiveBits() / 4; i != e; ++i) { |
Charles Davis | c4d7675 | 2012-05-29 07:01:45 +0000 | [diff] [blame] | 338 | *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf); |
| 339 | Temp = Temp.lshr(4); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 340 | } |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 341 | Out.write(CurPtr, EndPtr-CurPtr); |
| 342 | Out << '@'; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static const TemplateDecl * |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 347 | isTemplate(const NamedDecl *ND, |
| 348 | SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) { |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 349 | // Check if we have a function template. |
| 350 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ |
| 351 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 352 | if (FD->getTemplateSpecializationArgsAsWritten()) { |
| 353 | const ASTTemplateArgumentListInfo *ArgList = |
| 354 | FD->getTemplateSpecializationArgsAsWritten(); |
| 355 | TemplateArgs.append(ArgList->getTemplateArgs(), |
| 356 | ArgList->getTemplateArgs() + |
| 357 | ArgList->NumTemplateArgs); |
| 358 | } else { |
| 359 | const TemplateArgumentList *ArgList = |
| 360 | FD->getTemplateSpecializationArgs(); |
| 361 | TemplateArgumentListInfo LI; |
| 362 | for (unsigned i = 0, e = ArgList->size(); i != e; ++i) |
| 363 | TemplateArgs.push_back(TemplateArgumentLoc(ArgList->get(i), |
| 364 | FD->getTypeSourceInfo())); |
| 365 | } |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 366 | return TD; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Check if we have a class template. |
| 371 | if (const ClassTemplateSpecializationDecl *Spec = |
| 372 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 373 | TypeSourceInfo *TSI = Spec->getTypeAsWritten(); |
| 374 | if (TSI) { |
| 375 | TemplateSpecializationTypeLoc &TSTL = |
| 376 | cast<TemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 377 | TemplateArgumentListInfo LI(TSTL.getLAngleLoc(), TSTL.getRAngleLoc()); |
| 378 | for (unsigned i = 0, e = TSTL.getNumArgs(); i != e; ++i) |
| 379 | TemplateArgs.push_back(TSTL.getArgLoc(i)); |
| 380 | } else { |
| 381 | TemplateArgumentListInfo LI; |
| 382 | const TemplateArgumentList &ArgList = |
| 383 | Spec->getTemplateArgs(); |
| 384 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 385 | TemplateArgs.push_back(TemplateArgumentLoc(ArgList[i], |
| 386 | TemplateArgumentLocInfo())); |
| 387 | } |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 388 | return Spec->getSpecializedTemplate(); |
| 389 | } |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 394 | void |
| 395 | MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 396 | DeclarationName Name) { |
| 397 | // <unqualified-name> ::= <operator-name> |
| 398 | // ::= <ctor-dtor-name> |
| 399 | // ::= <source-name> |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 400 | // ::= <template-name> |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 401 | SmallVector<TemplateArgumentLoc, 2> TemplateArgs; |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 402 | // Check if we have a template. |
| 403 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 404 | mangleTemplateInstantiationName(TD, TemplateArgs); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 405 | return; |
| 406 | } |
| 407 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 408 | switch (Name.getNameKind()) { |
| 409 | case DeclarationName::Identifier: { |
| 410 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
| 411 | mangleSourceName(II); |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | // Otherwise, an anonymous entity. We must have a declaration. |
| 416 | assert(ND && "mangling empty name without declaration"); |
| 417 | |
| 418 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 419 | if (NS->isAnonymousNamespace()) { |
| 420 | Out << "?A"; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // We must have an anonymous struct. |
| 426 | const TagDecl *TD = cast<TagDecl>(ND); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 427 | if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 428 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 429 | "Typedef should not be in another decl context!"); |
| 430 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 431 | "Typedef was not named!"); |
| 432 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | // When VC encounters an anonymous type with no tag and no typedef, |
| 437 | // it literally emits '<unnamed-tag>'. |
| 438 | Out << "<unnamed-tag>"; |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | case DeclarationName::ObjCZeroArgSelector: |
| 443 | case DeclarationName::ObjCOneArgSelector: |
| 444 | case DeclarationName::ObjCMultiArgSelector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 445 | llvm_unreachable("Can't mangle Objective-C selector names here!"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 446 | |
| 447 | case DeclarationName::CXXConstructorName: |
Michael J. Spencer | 50118da | 2011-12-01 09:55:00 +0000 | [diff] [blame] | 448 | Out << "?0"; |
| 449 | break; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 450 | |
| 451 | case DeclarationName::CXXDestructorName: |
Michael J. Spencer | 50118da | 2011-12-01 09:55:00 +0000 | [diff] [blame] | 452 | Out << "?1"; |
| 453 | break; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 454 | |
| 455 | case DeclarationName::CXXConversionFunctionName: |
| 456 | // <operator-name> ::= ?B # (cast) |
| 457 | // The target type is encoded as the return type. |
| 458 | Out << "?B"; |
| 459 | break; |
| 460 | |
| 461 | case DeclarationName::CXXOperatorName: |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 462 | mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 463 | break; |
| 464 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 465 | case DeclarationName::CXXLiteralOperatorName: { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 466 | // FIXME: Was this added in VS2010? Does MS even know how to mangle this? |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 467 | DiagnosticsEngine Diags = Context.getDiags(); |
| 468 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 469 | "cannot mangle this literal operator yet"); |
| 470 | Diags.Report(ND->getLocation(), DiagID); |
| 471 | break; |
| 472 | } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 473 | |
| 474 | case DeclarationName::CXXUsingDirective: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 475 | llvm_unreachable("Can't mangle a using directive name!"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, |
| 480 | bool NoFunction) { |
| 481 | // <postfix> ::= <unqualified-name> [<postfix>] |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 482 | // ::= <substitution> [<postfix>] |
| 483 | |
| 484 | if (!DC) return; |
| 485 | |
| 486 | while (isa<LinkageSpecDecl>(DC)) |
| 487 | DC = DC->getParent(); |
| 488 | |
| 489 | if (DC->isTranslationUnit()) |
| 490 | return; |
| 491 | |
| 492 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 493 | Context.mangleBlock(BD, Out); |
| 494 | Out << '@'; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 495 | return manglePostfix(DC->getParent(), NoFunction); |
| 496 | } |
| 497 | |
| 498 | if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) |
| 499 | return; |
| 500 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) |
| 501 | mangleObjCMethodName(Method); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 502 | else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC)) |
| 503 | mangleLocalName(Func); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 504 | else { |
| 505 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 506 | manglePostfix(DC->getParent(), NoFunction); |
| 507 | } |
| 508 | } |
| 509 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 510 | void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, |
| 511 | SourceLocation Loc) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 512 | switch (OO) { |
| 513 | // ?0 # constructor |
| 514 | // ?1 # destructor |
| 515 | // <operator-name> ::= ?2 # new |
| 516 | case OO_New: Out << "?2"; break; |
| 517 | // <operator-name> ::= ?3 # delete |
| 518 | case OO_Delete: Out << "?3"; break; |
| 519 | // <operator-name> ::= ?4 # = |
| 520 | case OO_Equal: Out << "?4"; break; |
| 521 | // <operator-name> ::= ?5 # >> |
| 522 | case OO_GreaterGreater: Out << "?5"; break; |
| 523 | // <operator-name> ::= ?6 # << |
| 524 | case OO_LessLess: Out << "?6"; break; |
| 525 | // <operator-name> ::= ?7 # ! |
| 526 | case OO_Exclaim: Out << "?7"; break; |
| 527 | // <operator-name> ::= ?8 # == |
| 528 | case OO_EqualEqual: Out << "?8"; break; |
| 529 | // <operator-name> ::= ?9 # != |
| 530 | case OO_ExclaimEqual: Out << "?9"; break; |
| 531 | // <operator-name> ::= ?A # [] |
| 532 | case OO_Subscript: Out << "?A"; break; |
| 533 | // ?B # conversion |
| 534 | // <operator-name> ::= ?C # -> |
| 535 | case OO_Arrow: Out << "?C"; break; |
| 536 | // <operator-name> ::= ?D # * |
| 537 | case OO_Star: Out << "?D"; break; |
| 538 | // <operator-name> ::= ?E # ++ |
| 539 | case OO_PlusPlus: Out << "?E"; break; |
| 540 | // <operator-name> ::= ?F # -- |
| 541 | case OO_MinusMinus: Out << "?F"; break; |
| 542 | // <operator-name> ::= ?G # - |
| 543 | case OO_Minus: Out << "?G"; break; |
| 544 | // <operator-name> ::= ?H # + |
| 545 | case OO_Plus: Out << "?H"; break; |
| 546 | // <operator-name> ::= ?I # & |
| 547 | case OO_Amp: Out << "?I"; break; |
| 548 | // <operator-name> ::= ?J # ->* |
| 549 | case OO_ArrowStar: Out << "?J"; break; |
| 550 | // <operator-name> ::= ?K # / |
| 551 | case OO_Slash: Out << "?K"; break; |
| 552 | // <operator-name> ::= ?L # % |
| 553 | case OO_Percent: Out << "?L"; break; |
| 554 | // <operator-name> ::= ?M # < |
| 555 | case OO_Less: Out << "?M"; break; |
| 556 | // <operator-name> ::= ?N # <= |
| 557 | case OO_LessEqual: Out << "?N"; break; |
| 558 | // <operator-name> ::= ?O # > |
| 559 | case OO_Greater: Out << "?O"; break; |
| 560 | // <operator-name> ::= ?P # >= |
| 561 | case OO_GreaterEqual: Out << "?P"; break; |
| 562 | // <operator-name> ::= ?Q # , |
| 563 | case OO_Comma: Out << "?Q"; break; |
| 564 | // <operator-name> ::= ?R # () |
| 565 | case OO_Call: Out << "?R"; break; |
| 566 | // <operator-name> ::= ?S # ~ |
| 567 | case OO_Tilde: Out << "?S"; break; |
| 568 | // <operator-name> ::= ?T # ^ |
| 569 | case OO_Caret: Out << "?T"; break; |
| 570 | // <operator-name> ::= ?U # | |
| 571 | case OO_Pipe: Out << "?U"; break; |
| 572 | // <operator-name> ::= ?V # && |
| 573 | case OO_AmpAmp: Out << "?V"; break; |
| 574 | // <operator-name> ::= ?W # || |
| 575 | case OO_PipePipe: Out << "?W"; break; |
| 576 | // <operator-name> ::= ?X # *= |
| 577 | case OO_StarEqual: Out << "?X"; break; |
| 578 | // <operator-name> ::= ?Y # += |
| 579 | case OO_PlusEqual: Out << "?Y"; break; |
| 580 | // <operator-name> ::= ?Z # -= |
| 581 | case OO_MinusEqual: Out << "?Z"; break; |
| 582 | // <operator-name> ::= ?_0 # /= |
| 583 | case OO_SlashEqual: Out << "?_0"; break; |
| 584 | // <operator-name> ::= ?_1 # %= |
| 585 | case OO_PercentEqual: Out << "?_1"; break; |
| 586 | // <operator-name> ::= ?_2 # >>= |
| 587 | case OO_GreaterGreaterEqual: Out << "?_2"; break; |
| 588 | // <operator-name> ::= ?_3 # <<= |
| 589 | case OO_LessLessEqual: Out << "?_3"; break; |
| 590 | // <operator-name> ::= ?_4 # &= |
| 591 | case OO_AmpEqual: Out << "?_4"; break; |
| 592 | // <operator-name> ::= ?_5 # |= |
| 593 | case OO_PipeEqual: Out << "?_5"; break; |
| 594 | // <operator-name> ::= ?_6 # ^= |
| 595 | case OO_CaretEqual: Out << "?_6"; break; |
| 596 | // ?_7 # vftable |
| 597 | // ?_8 # vbtable |
| 598 | // ?_9 # vcall |
| 599 | // ?_A # typeof |
| 600 | // ?_B # local static guard |
| 601 | // ?_C # string |
| 602 | // ?_D # vbase destructor |
| 603 | // ?_E # vector deleting destructor |
| 604 | // ?_F # default constructor closure |
| 605 | // ?_G # scalar deleting destructor |
| 606 | // ?_H # vector constructor iterator |
| 607 | // ?_I # vector destructor iterator |
| 608 | // ?_J # vector vbase constructor iterator |
| 609 | // ?_K # virtual displacement map |
| 610 | // ?_L # eh vector constructor iterator |
| 611 | // ?_M # eh vector destructor iterator |
| 612 | // ?_N # eh vector vbase constructor iterator |
| 613 | // ?_O # copy constructor closure |
| 614 | // ?_P<name> # udt returning <name> |
| 615 | // ?_Q # <unknown> |
| 616 | // ?_R0 # RTTI Type Descriptor |
| 617 | // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) |
| 618 | // ?_R2 # RTTI Base Class Array |
| 619 | // ?_R3 # RTTI Class Hierarchy Descriptor |
| 620 | // ?_R4 # RTTI Complete Object Locator |
| 621 | // ?_S # local vftable |
| 622 | // ?_T # local vftable constructor closure |
| 623 | // <operator-name> ::= ?_U # new[] |
| 624 | case OO_Array_New: Out << "?_U"; break; |
| 625 | // <operator-name> ::= ?_V # delete[] |
| 626 | case OO_Array_Delete: Out << "?_V"; break; |
| 627 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 628 | case OO_Conditional: { |
| 629 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 630 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 631 | "cannot mangle this conditional operator yet"); |
| 632 | Diags.Report(Loc, DiagID); |
| 633 | break; |
| 634 | } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 635 | |
| 636 | case OO_None: |
| 637 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 638 | llvm_unreachable("Not an overloaded operator"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
| 642 | void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 643 | // <source name> ::= <identifier> @ |
| 644 | Out << II->getName() << '@'; |
| 645 | } |
| 646 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 647 | void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 648 | Context.mangleObjCMethodName(MD, Out); |
| 649 | } |
| 650 | |
| 651 | // Find out how many function decls live above this one and return an integer |
| 652 | // suitable for use as the number in a numbered anonymous scope. |
| 653 | // TODO: Memoize. |
| 654 | static unsigned getLocalNestingLevel(const FunctionDecl *FD) { |
| 655 | const DeclContext *DC = FD->getParent(); |
| 656 | int level = 1; |
| 657 | |
| 658 | while (DC && !DC->isTranslationUnit()) { |
| 659 | if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++; |
| 660 | DC = DC->getParent(); |
| 661 | } |
| 662 | |
| 663 | return 2*level; |
| 664 | } |
| 665 | |
| 666 | void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) { |
| 667 | // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name> |
| 668 | // <numbered-anonymous-scope> ::= ? <number> |
| 669 | // Even though the name is rendered in reverse order (e.g. |
| 670 | // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to |
| 671 | // innermost. So a method bar in class C local to function foo gets mangled |
| 672 | // as something like: |
| 673 | // ?bar@C@?1??foo@@YAXXZ@QAEXXZ |
| 674 | // This is more apparent when you have a type nested inside a method of a |
| 675 | // type nested inside a function. A method baz in class D local to method |
| 676 | // bar of class C local to function foo gets mangled as: |
| 677 | // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ |
| 678 | // This scheme is general enough to support GCC-style nested |
| 679 | // functions. You could have a method baz of class C inside a function bar |
| 680 | // inside a function foo, like so: |
| 681 | // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ |
| 682 | int NestLevel = getLocalNestingLevel(FD); |
| 683 | Out << '?'; |
| 684 | mangleNumber(NestLevel); |
| 685 | Out << '?'; |
| 686 | mangle(FD, "?"); |
| 687 | } |
| 688 | |
| 689 | void MicrosoftCXXNameMangler::mangleTemplateInstantiationName( |
| 690 | const TemplateDecl *TD, |
| 691 | const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) { |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 692 | // <template-name> ::= <unscoped-template-name> <template-args> |
| 693 | // ::= <substitution> |
| 694 | // Always start with the unqualified name. |
| 695 | mangleUnscopedTemplateName(TD); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 696 | mangleTemplateArgs(TemplateArgs); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 699 | void |
| 700 | MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) { |
| 701 | // <unscoped-template-name> ::= ?$ <unqualified-name> |
| 702 | Out << "?$"; |
| 703 | mangleUnqualifiedName(TD); |
| 704 | } |
| 705 | |
| 706 | void |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 707 | MicrosoftCXXNameMangler::mangleIntegerLiteral(QualType T, |
| 708 | const llvm::APSInt &Value) { |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 709 | // <integer-literal> ::= $0 <number> |
| 710 | Out << "$0"; |
| 711 | // Make sure booleans are encoded as 0/1. |
| 712 | if (T->isBooleanType()) |
| 713 | Out << (Value.getBoolValue() ? "0" : "A@"); |
| 714 | else |
| 715 | mangleNumber(Value); |
| 716 | } |
| 717 | |
| 718 | void |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 719 | MicrosoftCXXNameMangler::mangleTemplateArgs( |
| 720 | const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) { |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 721 | // <template-args> ::= {<type> | <integer-literal>}+ @ |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 722 | unsigned NumTemplateArgs = TemplateArgs.size(); |
| 723 | for (unsigned i = 0; i < NumTemplateArgs; ++i) { |
| 724 | const TemplateArgumentLoc &TAL = TemplateArgs[i]; |
| 725 | const TemplateArgument &TA = TAL.getArgument(); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 726 | switch (TA.getKind()) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 727 | case TemplateArgument::Null: |
| 728 | llvm_unreachable("Can't mangle null template arguments!"); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 729 | case TemplateArgument::Type: |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 730 | mangleType(TA.getAsType(), TAL.getSourceRange()); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 731 | break; |
| 732 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 733 | mangleIntegerLiteral(TA.getIntegralType(), TA.getAsIntegral()); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 734 | break; |
| 735 | default: { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 736 | // Issue a diagnostic. |
| 737 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 738 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 739 | "cannot mangle this %select{ERROR|ERROR|pointer/reference|ERROR|" |
| 740 | "template|template pack expansion|expression|parameter pack}0 " |
| 741 | "template argument yet"); |
| 742 | Diags.Report(TAL.getLocation(), DiagID) |
| 743 | << TA.getKind() |
| 744 | << TAL.getSourceRange(); |
Charles Davis | 9fd2359 | 2012-05-26 23:12:19 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | } |
| 748 | Out << '@'; |
| 749 | } |
| 750 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 751 | void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, |
| 752 | bool IsMember) { |
| 753 | // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> |
| 754 | // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); |
| 755 | // 'I' means __restrict (32/64-bit). |
| 756 | // Note that the MSVC __restrict keyword isn't the same as the C99 restrict |
| 757 | // keyword! |
| 758 | // <base-cvr-qualifiers> ::= A # near |
| 759 | // ::= B # near const |
| 760 | // ::= C # near volatile |
| 761 | // ::= D # near const volatile |
| 762 | // ::= E # far (16-bit) |
| 763 | // ::= F # far const (16-bit) |
| 764 | // ::= G # far volatile (16-bit) |
| 765 | // ::= H # far const volatile (16-bit) |
| 766 | // ::= I # huge (16-bit) |
| 767 | // ::= J # huge const (16-bit) |
| 768 | // ::= K # huge volatile (16-bit) |
| 769 | // ::= L # huge const volatile (16-bit) |
| 770 | // ::= M <basis> # based |
| 771 | // ::= N <basis> # based const |
| 772 | // ::= O <basis> # based volatile |
| 773 | // ::= P <basis> # based const volatile |
| 774 | // ::= Q # near member |
| 775 | // ::= R # near const member |
| 776 | // ::= S # near volatile member |
| 777 | // ::= T # near const volatile member |
| 778 | // ::= U # far member (16-bit) |
| 779 | // ::= V # far const member (16-bit) |
| 780 | // ::= W # far volatile member (16-bit) |
| 781 | // ::= X # far const volatile member (16-bit) |
| 782 | // ::= Y # huge member (16-bit) |
| 783 | // ::= Z # huge const member (16-bit) |
| 784 | // ::= 0 # huge volatile member (16-bit) |
| 785 | // ::= 1 # huge const volatile member (16-bit) |
| 786 | // ::= 2 <basis> # based member |
| 787 | // ::= 3 <basis> # based const member |
| 788 | // ::= 4 <basis> # based volatile member |
| 789 | // ::= 5 <basis> # based const volatile member |
| 790 | // ::= 6 # near function (pointers only) |
| 791 | // ::= 7 # far function (pointers only) |
| 792 | // ::= 8 # near method (pointers only) |
| 793 | // ::= 9 # far method (pointers only) |
| 794 | // ::= _A <basis> # based function (pointers only) |
| 795 | // ::= _B <basis> # based function (far?) (pointers only) |
| 796 | // ::= _C <basis> # based method (pointers only) |
| 797 | // ::= _D <basis> # based method (far?) (pointers only) |
| 798 | // ::= _E # block (Clang) |
| 799 | // <basis> ::= 0 # __based(void) |
| 800 | // ::= 1 # __based(segment)? |
| 801 | // ::= 2 <name> # __based(name) |
| 802 | // ::= 3 # ? |
| 803 | // ::= 4 # ? |
| 804 | // ::= 5 # not really based |
| 805 | if (!IsMember) { |
| 806 | if (!Quals.hasVolatile()) { |
| 807 | if (!Quals.hasConst()) |
| 808 | Out << 'A'; |
| 809 | else |
| 810 | Out << 'B'; |
| 811 | } else { |
| 812 | if (!Quals.hasConst()) |
| 813 | Out << 'C'; |
| 814 | else |
| 815 | Out << 'D'; |
| 816 | } |
| 817 | } else { |
| 818 | if (!Quals.hasVolatile()) { |
| 819 | if (!Quals.hasConst()) |
| 820 | Out << 'Q'; |
| 821 | else |
| 822 | Out << 'R'; |
| 823 | } else { |
| 824 | if (!Quals.hasConst()) |
| 825 | Out << 'S'; |
| 826 | else |
| 827 | Out << 'T'; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | // FIXME: For now, just drop all extension qualifiers on the floor. |
| 832 | } |
| 833 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 834 | void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 835 | // Only operate on the canonical type! |
| 836 | T = getASTContext().getCanonicalType(T); |
| 837 | |
| 838 | Qualifiers Quals = T.getLocalQualifiers(); |
| 839 | if (Quals) { |
| 840 | // We have to mangle these now, while we still have enough information. |
| 841 | // <pointer-cvr-qualifiers> ::= P # pointer |
| 842 | // ::= Q # const pointer |
| 843 | // ::= R # volatile pointer |
| 844 | // ::= S # const volatile pointer |
| 845 | if (T->isAnyPointerType() || T->isMemberPointerType() || |
| 846 | T->isBlockPointerType()) { |
| 847 | if (!Quals.hasVolatile()) |
| 848 | Out << 'Q'; |
| 849 | else { |
| 850 | if (!Quals.hasConst()) |
| 851 | Out << 'R'; |
| 852 | else |
| 853 | Out << 'S'; |
| 854 | } |
| 855 | } else |
| 856 | // Just emit qualifiers like normal. |
| 857 | // NB: When we mangle a pointer/reference type, and the pointee |
| 858 | // type has no qualifiers, the lack of qualifier gets mangled |
| 859 | // in there. |
| 860 | mangleQualifiers(Quals, false); |
| 861 | } else if (T->isAnyPointerType() || T->isMemberPointerType() || |
| 862 | T->isBlockPointerType()) { |
| 863 | Out << 'P'; |
| 864 | } |
| 865 | switch (T->getTypeClass()) { |
| 866 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 867 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 868 | case Type::CLASS: \ |
| 869 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
| 870 | return; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 871 | #define TYPE(CLASS, PARENT) \ |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 872 | case Type::CLASS: \ |
| 873 | mangleType(static_cast<const CLASS##Type*>(T.getTypePtr()), Range); \ |
| 874 | break; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 875 | #include "clang/AST/TypeNodes.def" |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 876 | #undef ABSTRACT_TYPE |
| 877 | #undef NON_CANONICAL_TYPE |
| 878 | #undef TYPE |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 882 | void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, |
| 883 | SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 884 | // <type> ::= <builtin-type> |
| 885 | // <builtin-type> ::= X # void |
| 886 | // ::= C # signed char |
| 887 | // ::= D # char |
| 888 | // ::= E # unsigned char |
| 889 | // ::= F # short |
| 890 | // ::= G # unsigned short (or wchar_t if it's not a builtin) |
| 891 | // ::= H # int |
| 892 | // ::= I # unsigned int |
| 893 | // ::= J # long |
| 894 | // ::= K # unsigned long |
| 895 | // L # <none> |
| 896 | // ::= M # float |
| 897 | // ::= N # double |
| 898 | // ::= O # long double (__float80 is mangled differently) |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 899 | // ::= _J # long long, __int64 |
| 900 | // ::= _K # unsigned long long, __int64 |
| 901 | // ::= _L # __int128 |
| 902 | // ::= _M # unsigned __int128 |
| 903 | // ::= _N # bool |
| 904 | // _O # <array in parameter> |
| 905 | // ::= _T # __float80 (Intel) |
| 906 | // ::= _W # wchar_t |
| 907 | // ::= _Z # __float80 (Digital Mars) |
| 908 | switch (T->getKind()) { |
| 909 | case BuiltinType::Void: Out << 'X'; break; |
| 910 | case BuiltinType::SChar: Out << 'C'; break; |
| 911 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; |
| 912 | case BuiltinType::UChar: Out << 'E'; break; |
| 913 | case BuiltinType::Short: Out << 'F'; break; |
| 914 | case BuiltinType::UShort: Out << 'G'; break; |
| 915 | case BuiltinType::Int: Out << 'H'; break; |
| 916 | case BuiltinType::UInt: Out << 'I'; break; |
| 917 | case BuiltinType::Long: Out << 'J'; break; |
| 918 | case BuiltinType::ULong: Out << 'K'; break; |
| 919 | case BuiltinType::Float: Out << 'M'; break; |
| 920 | case BuiltinType::Double: Out << 'N'; break; |
| 921 | // TODO: Determine size and mangle accordingly |
| 922 | case BuiltinType::LongDouble: Out << 'O'; break; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 923 | case BuiltinType::LongLong: Out << "_J"; break; |
| 924 | case BuiltinType::ULongLong: Out << "_K"; break; |
| 925 | case BuiltinType::Int128: Out << "_L"; break; |
| 926 | case BuiltinType::UInt128: Out << "_M"; break; |
| 927 | case BuiltinType::Bool: Out << "_N"; break; |
| 928 | case BuiltinType::WChar_S: |
| 929 | case BuiltinType::WChar_U: Out << "_W"; break; |
| 930 | |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 931 | #define BUILTIN_TYPE(Id, SingletonId) |
| 932 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 933 | case BuiltinType::Id: |
| 934 | #include "clang/AST/BuiltinTypes.def" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 935 | case BuiltinType::Dependent: |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 936 | llvm_unreachable("placeholder types shouldn't get to name mangling"); |
| 937 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 938 | case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; |
| 939 | case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; |
| 940 | case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; |
| 941 | |
| 942 | case BuiltinType::Char16: |
| 943 | case BuiltinType::Char32: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 944 | case BuiltinType::Half: |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 945 | case BuiltinType::NullPtr: { |
| 946 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 947 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 948 | "cannot mangle this built-in %0 type yet"); |
| 949 | Diags.Report(Range.getBegin(), DiagID) |
| 950 | << T->getName(Context.getASTContext().getPrintingPolicy()) |
| 951 | << Range; |
| 952 | } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
| 956 | // <type> ::= <function-type> |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 957 | void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T, |
| 958 | SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 959 | // Structors only appear in decls, so at this point we know it's not a |
| 960 | // structor type. |
| 961 | // I'll probably have mangleType(MemberPointerType) call the mangleType() |
| 962 | // method directly. |
| 963 | mangleType(T, NULL, false, false); |
| 964 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 965 | void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T, |
| 966 | SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 967 | llvm_unreachable("Can't mangle K&R function prototypes"); |
| 968 | } |
| 969 | |
| 970 | void MicrosoftCXXNameMangler::mangleType(const FunctionType *T, |
| 971 | const FunctionDecl *D, |
| 972 | bool IsStructor, |
| 973 | bool IsInstMethod) { |
| 974 | // <function-type> ::= <this-cvr-qualifiers> <calling-convention> |
| 975 | // <return-type> <argument-list> <throw-spec> |
| 976 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 977 | |
| 978 | // If this is a C++ instance method, mangle the CVR qualifiers for the |
| 979 | // this pointer. |
| 980 | if (IsInstMethod) |
| 981 | mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false); |
| 982 | |
| 983 | mangleCallingConvention(T, IsInstMethod); |
| 984 | |
| 985 | // <return-type> ::= <type> |
| 986 | // ::= @ # structors (they have no declared return type) |
| 987 | if (IsStructor) |
| 988 | Out << '@'; |
| 989 | else |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 990 | // FIXME: Get the source range for the result type. Or, better yet, |
| 991 | // implement the unimplemented stuff so we don't need accurate source |
| 992 | // location info anymore :). |
| 993 | mangleType(Proto->getResultType(), SourceRange()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 994 | |
| 995 | // <argument-list> ::= X # void |
| 996 | // ::= <type>+ @ |
| 997 | // ::= <type>* Z # varargs |
| 998 | if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { |
| 999 | Out << 'X'; |
| 1000 | } else { |
| 1001 | if (D) { |
John McCall | 3a8ac07 | 2012-05-01 02:33:44 +0000 | [diff] [blame] | 1002 | // If we got a decl, use the type-as-written to make sure arrays |
| 1003 | // get mangled right. Note that we can't rely on the TSI |
| 1004 | // existing if (for example) the parameter was synthesized. |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1005 | for (FunctionDecl::param_const_iterator Parm = D->param_begin(), |
John McCall | 3a8ac07 | 2012-05-01 02:33:44 +0000 | [diff] [blame] | 1006 | ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) { |
| 1007 | if (TypeSourceInfo *typeAsWritten = (*Parm)->getTypeSourceInfo()) |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1008 | mangleType(typeAsWritten->getType(), |
| 1009 | typeAsWritten->getTypeLoc().getSourceRange()); |
John McCall | 3a8ac07 | 2012-05-01 02:33:44 +0000 | [diff] [blame] | 1010 | else |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1011 | mangleType((*Parm)->getType(), SourceRange()); |
John McCall | 3a8ac07 | 2012-05-01 02:33:44 +0000 | [diff] [blame] | 1012 | } |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1013 | } else { |
| 1014 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
| 1015 | ArgEnd = Proto->arg_type_end(); |
| 1016 | Arg != ArgEnd; ++Arg) |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1017 | mangleType(*Arg, SourceRange()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1018 | } |
| 1019 | // <builtin-type> ::= Z # ellipsis |
| 1020 | if (Proto->isVariadic()) |
| 1021 | Out << 'Z'; |
| 1022 | else |
| 1023 | Out << '@'; |
| 1024 | } |
| 1025 | |
| 1026 | mangleThrowSpecification(Proto); |
| 1027 | } |
| 1028 | |
| 1029 | void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { |
| 1030 | // <function-class> ::= A # private: near |
| 1031 | // ::= B # private: far |
| 1032 | // ::= C # private: static near |
| 1033 | // ::= D # private: static far |
| 1034 | // ::= E # private: virtual near |
| 1035 | // ::= F # private: virtual far |
| 1036 | // ::= G # private: thunk near |
| 1037 | // ::= H # private: thunk far |
| 1038 | // ::= I # protected: near |
| 1039 | // ::= J # protected: far |
| 1040 | // ::= K # protected: static near |
| 1041 | // ::= L # protected: static far |
| 1042 | // ::= M # protected: virtual near |
| 1043 | // ::= N # protected: virtual far |
| 1044 | // ::= O # protected: thunk near |
| 1045 | // ::= P # protected: thunk far |
| 1046 | // ::= Q # public: near |
| 1047 | // ::= R # public: far |
| 1048 | // ::= S # public: static near |
| 1049 | // ::= T # public: static far |
| 1050 | // ::= U # public: virtual near |
| 1051 | // ::= V # public: virtual far |
| 1052 | // ::= W # public: thunk near |
| 1053 | // ::= X # public: thunk far |
| 1054 | // ::= Y # global near |
| 1055 | // ::= Z # global far |
| 1056 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 1057 | switch (MD->getAccess()) { |
| 1058 | default: |
| 1059 | case AS_private: |
| 1060 | if (MD->isStatic()) |
| 1061 | Out << 'C'; |
| 1062 | else if (MD->isVirtual()) |
| 1063 | Out << 'E'; |
| 1064 | else |
| 1065 | Out << 'A'; |
| 1066 | break; |
| 1067 | case AS_protected: |
| 1068 | if (MD->isStatic()) |
| 1069 | Out << 'K'; |
| 1070 | else if (MD->isVirtual()) |
| 1071 | Out << 'M'; |
| 1072 | else |
| 1073 | Out << 'I'; |
| 1074 | break; |
| 1075 | case AS_public: |
| 1076 | if (MD->isStatic()) |
| 1077 | Out << 'S'; |
| 1078 | else if (MD->isVirtual()) |
| 1079 | Out << 'U'; |
| 1080 | else |
| 1081 | Out << 'Q'; |
| 1082 | } |
| 1083 | } else |
| 1084 | Out << 'Y'; |
| 1085 | } |
| 1086 | void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T, |
| 1087 | bool IsInstMethod) { |
| 1088 | // <calling-convention> ::= A # __cdecl |
| 1089 | // ::= B # __export __cdecl |
| 1090 | // ::= C # __pascal |
| 1091 | // ::= D # __export __pascal |
| 1092 | // ::= E # __thiscall |
| 1093 | // ::= F # __export __thiscall |
| 1094 | // ::= G # __stdcall |
| 1095 | // ::= H # __export __stdcall |
| 1096 | // ::= I # __fastcall |
| 1097 | // ::= J # __export __fastcall |
| 1098 | // The 'export' calling conventions are from a bygone era |
| 1099 | // (*cough*Win16*cough*) when functions were declared for export with |
| 1100 | // that keyword. (It didn't actually export them, it just made them so |
| 1101 | // that they could be in a DLL and somebody from another module could call |
| 1102 | // them.) |
| 1103 | CallingConv CC = T->getCallConv(); |
| 1104 | if (CC == CC_Default) |
| 1105 | CC = IsInstMethod ? getASTContext().getDefaultMethodCallConv() : CC_C; |
| 1106 | switch (CC) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 1107 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1108 | llvm_unreachable("Unsupported CC for mangling"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1109 | case CC_Default: |
| 1110 | case CC_C: Out << 'A'; break; |
| 1111 | case CC_X86Pascal: Out << 'C'; break; |
| 1112 | case CC_X86ThisCall: Out << 'E'; break; |
| 1113 | case CC_X86StdCall: Out << 'G'; break; |
| 1114 | case CC_X86FastCall: Out << 'I'; break; |
| 1115 | } |
| 1116 | } |
| 1117 | void MicrosoftCXXNameMangler::mangleThrowSpecification( |
| 1118 | const FunctionProtoType *FT) { |
| 1119 | // <throw-spec> ::= Z # throw(...) (default) |
| 1120 | // ::= @ # throw() or __declspec/__attribute__((nothrow)) |
| 1121 | // ::= <type>+ |
| 1122 | // NOTE: Since the Microsoft compiler ignores throw specifications, they are |
| 1123 | // all actually mangled as 'Z'. (They're ignored because their associated |
| 1124 | // functionality isn't implemented, and probably never will be.) |
| 1125 | Out << 'Z'; |
| 1126 | } |
| 1127 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1128 | void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T, |
| 1129 | SourceRange Range) { |
| 1130 | // Probably should be mangled as a template instantiation; need to see what |
| 1131 | // VC does first. |
| 1132 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1133 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1134 | "cannot mangle this unresolved dependent type yet"); |
| 1135 | Diags.Report(Range.getBegin(), DiagID) |
| 1136 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> |
| 1140 | // <union-type> ::= T <name> |
| 1141 | // <struct-type> ::= U <name> |
| 1142 | // <class-type> ::= V <name> |
| 1143 | // <enum-type> ::= W <size> <name> |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1144 | void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1145 | mangleType(static_cast<const TagType*>(T)); |
| 1146 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1147 | void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1148 | mangleType(static_cast<const TagType*>(T)); |
| 1149 | } |
| 1150 | void MicrosoftCXXNameMangler::mangleType(const TagType *T) { |
| 1151 | switch (T->getDecl()->getTagKind()) { |
| 1152 | case TTK_Union: |
| 1153 | Out << 'T'; |
| 1154 | break; |
| 1155 | case TTK_Struct: |
| 1156 | Out << 'U'; |
| 1157 | break; |
| 1158 | case TTK_Class: |
| 1159 | Out << 'V'; |
| 1160 | break; |
| 1161 | case TTK_Enum: |
| 1162 | Out << 'W'; |
| 1163 | Out << getASTContext().getTypeSizeInChars( |
| 1164 | cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity(); |
| 1165 | break; |
| 1166 | } |
| 1167 | mangleName(T->getDecl()); |
| 1168 | } |
| 1169 | |
| 1170 | // <type> ::= <array-type> |
| 1171 | // <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+] |
| 1172 | // <element-type> # as global |
| 1173 | // ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+] |
| 1174 | // <element-type> # as param |
| 1175 | // It's supposed to be the other way around, but for some strange reason, it |
| 1176 | // isn't. Today this behavior is retained for the sole purpose of backwards |
| 1177 | // compatibility. |
| 1178 | void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) { |
| 1179 | // This isn't a recursive mangling, so now we have to do it all in this |
| 1180 | // one call. |
| 1181 | if (IsGlobal) |
| 1182 | Out << 'P'; |
| 1183 | else |
| 1184 | Out << 'Q'; |
| 1185 | mangleExtraDimensions(T->getElementType()); |
| 1186 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1187 | void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T, |
| 1188 | SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1189 | mangleType(static_cast<const ArrayType *>(T), false); |
| 1190 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1191 | void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T, |
| 1192 | SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1193 | mangleType(static_cast<const ArrayType *>(T), false); |
| 1194 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1195 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T, |
| 1196 | SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1197 | mangleType(static_cast<const ArrayType *>(T), false); |
| 1198 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1199 | void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T, |
| 1200 | SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1201 | mangleType(static_cast<const ArrayType *>(T), false); |
| 1202 | } |
| 1203 | void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1204 | SmallVector<llvm::APInt, 3> Dimensions; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1205 | for (;;) { |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 1206 | if (const ConstantArrayType *CAT = |
| 1207 | getASTContext().getAsConstantArrayType(ElementTy)) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1208 | Dimensions.push_back(CAT->getSize()); |
| 1209 | ElementTy = CAT->getElementType(); |
| 1210 | } else if (ElementTy->isVariableArrayType()) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1211 | const VariableArrayType *VAT = |
| 1212 | getASTContext().getAsVariableArrayType(ElementTy); |
| 1213 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1214 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1215 | "cannot mangle this variable-length array yet"); |
| 1216 | Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID) |
| 1217 | << VAT->getBracketsRange(); |
| 1218 | return; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1219 | } else if (ElementTy->isDependentSizedArrayType()) { |
| 1220 | // The dependent expression has to be folded into a constant (TODO). |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1221 | const DependentSizedArrayType *DSAT = |
| 1222 | getASTContext().getAsDependentSizedArrayType(ElementTy); |
| 1223 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1224 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1225 | "cannot mangle this dependent-length array yet"); |
| 1226 | Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID) |
| 1227 | << DSAT->getBracketsRange(); |
| 1228 | return; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1229 | } else if (ElementTy->isIncompleteArrayType()) continue; |
| 1230 | else break; |
| 1231 | } |
| 1232 | mangleQualifiers(ElementTy.getQualifiers(), false); |
| 1233 | // If there are any additional dimensions, mangle them now. |
| 1234 | if (Dimensions.size() > 0) { |
| 1235 | Out << 'Y'; |
| 1236 | // <dimension-count> ::= <number> # number of extra dimensions |
| 1237 | mangleNumber(Dimensions.size()); |
| 1238 | for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) { |
| 1239 | mangleNumber(Dimensions[Dim].getLimitedValue()); |
| 1240 | } |
| 1241 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1242 | mangleType(ElementTy.getLocalUnqualifiedType(), SourceRange()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | // <type> ::= <pointer-to-member-type> |
| 1246 | // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> |
| 1247 | // <class name> <type> |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1248 | void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T, |
| 1249 | SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1250 | QualType PointeeType = T->getPointeeType(); |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 1251 | if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1252 | Out << '8'; |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 1253 | mangleName(T->getClass()->castAs<RecordType>()->getDecl()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1254 | mangleType(FPT, NULL, false, true); |
| 1255 | } else { |
| 1256 | mangleQualifiers(PointeeType.getQualifiers(), true); |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 1257 | mangleName(T->getClass()->castAs<RecordType>()->getDecl()); |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1258 | mangleType(PointeeType.getLocalUnqualifiedType(), Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1262 | void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T, |
| 1263 | SourceRange Range) { |
| 1264 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1265 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1266 | "cannot mangle this template type parameter type yet"); |
| 1267 | Diags.Report(Range.getBegin(), DiagID) |
| 1268 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1271 | void MicrosoftCXXNameMangler::mangleType( |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1272 | const SubstTemplateTypeParmPackType *T, |
| 1273 | SourceRange Range) { |
| 1274 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1275 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1276 | "cannot mangle this substituted parameter pack yet"); |
| 1277 | Diags.Report(Range.getBegin(), DiagID) |
| 1278 | << Range; |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1281 | // <type> ::= <pointer-type> |
| 1282 | // <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type> |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1283 | void MicrosoftCXXNameMangler::mangleType(const PointerType *T, |
| 1284 | SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1285 | QualType PointeeTy = T->getPointeeType(); |
| 1286 | if (PointeeTy->isArrayType()) { |
| 1287 | // Pointers to arrays are mangled like arrays. |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 1288 | mangleExtraDimensions(PointeeTy); |
| 1289 | } else if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1290 | // Function pointers are special. |
| 1291 | Out << '6'; |
Richard Smith | c0838d2 | 2012-06-08 00:37:04 +0000 | [diff] [blame] | 1292 | mangleType(FT, NULL, false, false); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1293 | } else { |
| 1294 | if (!PointeeTy.hasQualifiers()) |
| 1295 | // Lack of qualifiers is mangled as 'A'. |
| 1296 | Out << 'A'; |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1297 | mangleType(PointeeTy, Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1300 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T, |
| 1301 | SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1302 | // Object pointers never have qualifiers. |
| 1303 | Out << 'A'; |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1304 | mangleType(T->getPointeeType(), Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | // <type> ::= <reference-type> |
| 1308 | // <reference-type> ::= A <cvr-qualifiers> <type> |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1309 | void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T, |
| 1310 | SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1311 | Out << 'A'; |
| 1312 | QualType PointeeTy = T->getPointeeType(); |
| 1313 | if (!PointeeTy.hasQualifiers()) |
| 1314 | // Lack of qualifiers is mangled as 'A'. |
| 1315 | Out << 'A'; |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1316 | mangleType(PointeeTy, Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1319 | void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T, |
| 1320 | SourceRange Range) { |
| 1321 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1322 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1323 | "cannot mangle this r-value reference type yet"); |
| 1324 | Diags.Report(Range.getBegin(), DiagID) |
| 1325 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1328 | void MicrosoftCXXNameMangler::mangleType(const ComplexType *T, |
| 1329 | SourceRange Range) { |
| 1330 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1331 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1332 | "cannot mangle this complex number type yet"); |
| 1333 | Diags.Report(Range.getBegin(), DiagID) |
| 1334 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1337 | void MicrosoftCXXNameMangler::mangleType(const VectorType *T, |
| 1338 | SourceRange Range) { |
| 1339 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1340 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1341 | "cannot mangle this vector type yet"); |
| 1342 | Diags.Report(Range.getBegin(), DiagID) |
| 1343 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1344 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1345 | void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T, |
| 1346 | SourceRange Range) { |
| 1347 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1348 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1349 | "cannot mangle this extended vector type yet"); |
| 1350 | Diags.Report(Range.getBegin(), DiagID) |
| 1351 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1352 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1353 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T, |
| 1354 | SourceRange Range) { |
| 1355 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1356 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1357 | "cannot mangle this dependent-sized extended vector type yet"); |
| 1358 | Diags.Report(Range.getBegin(), DiagID) |
| 1359 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1362 | void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, |
| 1363 | SourceRange) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1364 | // ObjC interfaces have structs underlying them. |
| 1365 | Out << 'U'; |
| 1366 | mangleName(T->getDecl()); |
| 1367 | } |
| 1368 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1369 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T, |
| 1370 | SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1371 | // We don't allow overloading by different protocol qualification, |
| 1372 | // so mangling them isn't necessary. |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1373 | mangleType(T->getBaseType(), Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1376 | void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T, |
| 1377 | SourceRange Range) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1378 | Out << "_E"; |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1379 | mangleType(T->getPointeeType(), Range); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1382 | void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T, |
| 1383 | SourceRange Range) { |
| 1384 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1385 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1386 | "cannot mangle this injected class name type yet"); |
| 1387 | Diags.Report(Range.getBegin(), DiagID) |
| 1388 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1391 | void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T, |
| 1392 | SourceRange Range) { |
| 1393 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1394 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1395 | "cannot mangle this template specialization type yet"); |
| 1396 | Diags.Report(Range.getBegin(), DiagID) |
| 1397 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1400 | void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, |
| 1401 | SourceRange Range) { |
| 1402 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1403 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1404 | "cannot mangle this dependent name type yet"); |
| 1405 | Diags.Report(Range.getBegin(), DiagID) |
| 1406 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | void MicrosoftCXXNameMangler::mangleType( |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1410 | const DependentTemplateSpecializationType *T, |
| 1411 | SourceRange Range) { |
| 1412 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1413 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1414 | "cannot mangle this dependent template specialization type yet"); |
| 1415 | Diags.Report(Range.getBegin(), DiagID) |
| 1416 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1419 | void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, |
| 1420 | SourceRange Range) { |
| 1421 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1422 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1423 | "cannot mangle this pack expansion yet"); |
| 1424 | Diags.Report(Range.getBegin(), DiagID) |
| 1425 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1428 | void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, |
| 1429 | SourceRange Range) { |
| 1430 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1431 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1432 | "cannot mangle this typeof(type) yet"); |
| 1433 | Diags.Report(Range.getBegin(), DiagID) |
| 1434 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1437 | void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, |
| 1438 | SourceRange Range) { |
| 1439 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1440 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1441 | "cannot mangle this typeof(expression) yet"); |
| 1442 | Diags.Report(Range.getBegin(), DiagID) |
| 1443 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1446 | void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, |
| 1447 | SourceRange Range) { |
| 1448 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1449 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1450 | "cannot mangle this decltype() yet"); |
| 1451 | Diags.Report(Range.getBegin(), DiagID) |
| 1452 | << Range; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1455 | void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T, |
| 1456 | SourceRange Range) { |
| 1457 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1458 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1459 | "cannot mangle this unary transform type yet"); |
| 1460 | Diags.Report(Range.getBegin(), DiagID) |
| 1461 | << Range; |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1464 | void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) { |
| 1465 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1466 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1467 | "cannot mangle this 'auto' type yet"); |
| 1468 | Diags.Report(Range.getBegin(), DiagID) |
| 1469 | << Range; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1472 | void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, |
| 1473 | SourceRange Range) { |
| 1474 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1475 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1476 | "cannot mangle this C11 atomic type yet"); |
| 1477 | Diags.Report(Range.getBegin(), DiagID) |
| 1478 | << Range; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1481 | void MicrosoftMangleContext::mangleName(const NamedDecl *D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1482 | raw_ostream &Out) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1483 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 1484 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 1485 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 1486 | "Invalid mangleName() call on 'structor decl!"); |
| 1487 | |
| 1488 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 1489 | getASTContext().getSourceManager(), |
| 1490 | "Mangling declaration"); |
| 1491 | |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 1492 | MicrosoftCXXNameMangler Mangler(*this, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1493 | return Mangler.mangle(D); |
| 1494 | } |
| 1495 | void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD, |
| 1496 | const ThunkInfo &Thunk, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1497 | raw_ostream &) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1498 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
| 1499 | "cannot mangle thunk for this method yet"); |
| 1500 | getDiags().Report(MD->getLocation(), DiagID); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1501 | } |
| 1502 | void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, |
| 1503 | CXXDtorType Type, |
| 1504 | const ThisAdjustment &, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1505 | raw_ostream &) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1506 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
| 1507 | "cannot mangle thunk for this destructor yet"); |
| 1508 | getDiags().Report(DD->getLocation(), DiagID); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1509 | } |
| 1510 | void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1511 | raw_ostream &Out) { |
Kaelyn Uhrain | 28e862a | 2012-06-13 17:05:13 +0000 | [diff] [blame^] | 1512 | // <mangled-name> ::= ? <operator-name> <class-name> <storage-class> |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1513 | // <cvr-qualifiers> [<name>] @ |
| 1514 | // <operator-name> ::= _7 # vftable |
| 1515 | // ::= _8 # vbtable |
| 1516 | // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> |
| 1517 | // is always '6' for vftables and '7' for vbtables. (The difference is |
| 1518 | // beyond me.) |
| 1519 | // TODO: vbtables. |
| 1520 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 1521 | Mangler.getStream() << "\01??_7"; |
| 1522 | Mangler.mangleName(RD); |
| 1523 | Mangler.getStream() << "6B"; |
| 1524 | // TODO: If the class has more than one vtable, mangle in the class it came |
| 1525 | // from. |
| 1526 | Mangler.getStream() << '@'; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1527 | } |
| 1528 | void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1529 | raw_ostream &) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1530 | llvm_unreachable("The MS C++ ABI does not have virtual table tables!"); |
| 1531 | } |
| 1532 | void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, |
| 1533 | int64_t Offset, |
| 1534 | const CXXRecordDecl *Type, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1535 | raw_ostream &) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1536 | llvm_unreachable("The MS C++ ABI does not have constructor vtables!"); |
| 1537 | } |
| 1538 | void MicrosoftMangleContext::mangleCXXRTTI(QualType T, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1539 | raw_ostream &) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1540 | // FIXME: Give a location... |
| 1541 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
| 1542 | "cannot mangle RTTI descriptors for type %0 yet"); |
| 1543 | getDiags().Report(DiagID) |
| 1544 | << T.getBaseTypeIdentifier(); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1545 | } |
| 1546 | void MicrosoftMangleContext::mangleCXXRTTIName(QualType T, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1547 | raw_ostream &) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1548 | // FIXME: Give a location... |
| 1549 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
| 1550 | "cannot mangle the name of type %0 into RTTI descriptors yet"); |
| 1551 | getDiags().Report(DiagID) |
| 1552 | << T.getBaseTypeIdentifier(); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1553 | } |
| 1554 | void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, |
| 1555 | CXXCtorType Type, |
Michael J. Spencer | 50118da | 2011-12-01 09:55:00 +0000 | [diff] [blame] | 1556 | raw_ostream & Out) { |
| 1557 | MicrosoftCXXNameMangler mangler(*this, Out); |
| 1558 | mangler.mangle(D); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1559 | } |
| 1560 | void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, |
| 1561 | CXXDtorType Type, |
Michael J. Spencer | 50118da | 2011-12-01 09:55:00 +0000 | [diff] [blame] | 1562 | raw_ostream & Out) { |
| 1563 | MicrosoftCXXNameMangler mangler(*this, Out); |
| 1564 | mangler.mangle(D); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1565 | } |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1566 | void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1567 | raw_ostream &) { |
Charles Davis | e0deb03 | 2012-06-13 00:18:14 +0000 | [diff] [blame] | 1568 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
| 1569 | "cannot mangle this reference temporary yet"); |
| 1570 | getDiags().Report(VD->getLocation(), DiagID); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1574 | DiagnosticsEngine &Diags) { |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1575 | return new MicrosoftMangleContext(Context, Diags); |
| 1576 | } |