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