Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 1 | //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implements C++ name mangling according to the Itanium C++ ABI, |
| 11 | // which is used in GCC 3.2 and newer (and many compilers that are |
| 12 | // ABI-compatible with GCC): |
| 13 | // |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "Mangle.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
| 31 | namespace { |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 32 | /// CXXNameMangler - Manage the mangling of a single name. |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 33 | class CXXNameMangler { |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 34 | MangleContext &Context; |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 35 | llvm::raw_svector_ostream Out; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 37 | const CXXMethodDecl *Structor; |
| 38 | unsigned StructorType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 40 | llvm::DenseMap<uintptr_t, unsigned> Substitutions; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 41 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 42 | public: |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 43 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) |
| 44 | : Context(C), Out(Res), Structor(0), StructorType(0) { } |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 45 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 46 | const CXXConstructorDecl *D, CXXCtorType Type) |
| 47 | : Context(C), Out(Res), Structor(D), StructorType(Type) { } |
| 48 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 49 | const CXXDestructorDecl *D, CXXDtorType Type) |
| 50 | : Context(C), Out(Res), Structor(D), StructorType(Type) { } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 51 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 52 | llvm::raw_svector_ostream &getStream() { return Out; } |
| 53 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 54 | bool mangle(const NamedDecl *D); |
| 55 | void mangleCalloffset(int64_t nv, int64_t v); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 56 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 57 | void mangleName(const NamedDecl *ND); |
| 58 | void mangleType(QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 60 | private: |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 61 | bool mangleSubstitution(const NamedDecl *ND); |
| 62 | bool mangleSubstitution(QualType T); |
| 63 | bool mangleSubstitution(uintptr_t Ptr); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 65 | bool mangleStandardSubstitution(const NamedDecl *ND); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 66 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 67 | void addSubstitution(const NamedDecl *ND) { |
| 68 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
Anders Carlsson | 433d137 | 2009-11-07 04:26:04 +0000 | [diff] [blame] | 69 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 70 | addSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 71 | } |
| 72 | void addSubstitution(QualType T); |
| 73 | void addSubstitution(uintptr_t Ptr); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 75 | bool mangleFunctionDecl(const FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 77 | void mangleName(const TemplateDecl *TD, |
| 78 | const TemplateArgument *TemplateArgs, |
| 79 | unsigned NumTemplateArgs); |
| 80 | void mangleUnqualifiedName(const NamedDecl *ND); |
| 81 | void mangleUnscopedName(const NamedDecl *ND); |
| 82 | void mangleUnscopedTemplateName(const TemplateDecl *ND); |
| 83 | void mangleSourceName(const IdentifierInfo *II); |
| 84 | void mangleLocalName(const NamedDecl *ND); |
| 85 | void mangleNestedName(const NamedDecl *ND); |
| 86 | void mangleNestedName(const TemplateDecl *TD, |
| 87 | const TemplateArgument *TemplateArgs, |
| 88 | unsigned NumTemplateArgs); |
| 89 | void manglePrefix(const DeclContext *DC); |
| 90 | void mangleTemplatePrefix(const TemplateDecl *ND); |
| 91 | void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); |
| 92 | void mangleQualifiers(Qualifiers Quals); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 93 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 94 | // Declare manglers for every type class. |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 95 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 96 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 97 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); |
| 98 | #include "clang/AST/TypeNodes.def" |
| 99 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 100 | void mangleType(const TagType*); |
| 101 | void mangleBareFunctionType(const FunctionType *T, |
| 102 | bool MangleReturnType); |
| 103 | void mangleExpression(const Expr *E); |
| 104 | void mangleCXXCtorType(CXXCtorType T); |
| 105 | void mangleCXXDtorType(CXXDtorType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 107 | void mangleTemplateArgs(const TemplateArgument *TemplateArgs, |
| 108 | unsigned NumTemplateArgs); |
| 109 | void mangleTemplateArgumentList(const TemplateArgumentList &L); |
| 110 | void mangleTemplateArgument(const TemplateArgument &A); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 111 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 112 | void mangleTemplateParameter(unsigned Index); |
| 113 | }; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 116 | static bool isInCLinkageSpecification(const Decl *D) { |
Douglas Gregor | 457e281 | 2009-10-28 16:31:34 +0000 | [diff] [blame] | 117 | D = D->getCanonicalDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | for (const DeclContext *DC = D->getDeclContext(); |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 119 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 121 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 128 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
Anders Carlsson | a1e1622 | 2009-11-07 07:15:03 +0000 | [diff] [blame] | 129 | // (always) as does passing a C++ member function and a function |
| 130 | // whose name is not a simple identifier. |
| 131 | if (!FD->hasAttr<OverloadableAttr>() && !isa<CXXMethodDecl>(FD) && |
| 132 | FD->getDeclName().isIdentifier()) { |
Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 133 | // C functions are not mangled, and "main" is never mangled. |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 134 | if (!Context.getASTContext().getLangOptions().CPlusPlus || FD->isMain()) |
Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 135 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
| 137 | // No mangling in an "implicit extern C" header. |
Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 138 | if (FD->getLocation().isValid() && |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 139 | Context.getASTContext().getSourceManager(). |
| 140 | isInExternCSystemHeader(FD->getLocation())) |
Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 141 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 143 | // No name mangling in a C linkage specification. |
Anders Carlsson | a1e1622 | 2009-11-07 07:15:03 +0000 | [diff] [blame] | 144 | if (isInCLinkageSpecification(FD)) |
Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 147 | |
| 148 | // If we get here, mangle the decl name! |
| 149 | Out << "_Z"; |
| 150 | mangleFunctionEncoding(FD); |
| 151 | return true; |
| 152 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 153 | |
| 154 | bool CXXNameMangler::mangle(const NamedDecl *D) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 155 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 156 | // over all other naming in the .o file. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 157 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 158 | // If we have an asm name, then we use it as the mangling. |
| 159 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 160 | Out << ALA->getLabel(); |
| 161 | return true; |
| 162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 164 | // <mangled-name> ::= _Z <encoding> |
| 165 | // ::= <data name> |
| 166 | // ::= <special-name> |
| 167 | |
| 168 | // FIXME: Actually use a visitor to decode these? |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 169 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 170 | return mangleFunctionDecl(FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | c02ab4c | 2009-11-21 09:14:44 +0000 | [diff] [blame^] | 172 | const VarDecl *VD = cast<VarDecl>(D); |
| 173 | if (!Context.getASTContext().getLangOptions().CPlusPlus || |
| 174 | isInCLinkageSpecification(D) || |
| 175 | D->getDeclContext()->isTranslationUnit()) |
| 176 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Daniel Dunbar | c02ab4c | 2009-11-21 09:14:44 +0000 | [diff] [blame^] | 178 | Out << "_Z"; |
| 179 | mangleName(VD); |
| 180 | return true; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 184 | // <encoding> ::= <function name> <bare-function-type> |
| 185 | mangleName(FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 187 | // Whether the mangling of a function type includes the return type depends on |
| 188 | // the context and the nature of the function. The rules for deciding whether |
| 189 | // the return type is included are: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | // |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 191 | // 1. Template functions (names or types) have return types encoded, with |
| 192 | // the exceptions listed below. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | // 2. Function types not appearing as part of a function name mangling, |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 194 | // e.g. parameters, pointer types, etc., have return type encoded, with the |
| 195 | // exceptions listed below. |
| 196 | // 3. Non-template function names do not have return types encoded. |
| 197 | // |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 198 | // The exceptions mentioned in (1) and (2) above, for which the return type is |
| 199 | // never included, are |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 200 | // 1. Constructors. |
| 201 | // 2. Destructors. |
| 202 | // 3. Conversion operator functions, e.g. operator int. |
| 203 | bool MangleReturnType = false; |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 204 | if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { |
| 205 | if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || |
| 206 | isa<CXXConversionDecl>(FD))) |
| 207 | MangleReturnType = true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 208 | |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 209 | // Mangle the type of the primary template. |
| 210 | FD = PrimaryTemplate->getTemplatedDecl(); |
| 211 | } |
| 212 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 213 | // Do the canonicalization out here because parameter types can |
| 214 | // undergo additional canonicalization (e.g. array decay). |
| 215 | FunctionType *FT = cast<FunctionType>(Context.getASTContext() |
| 216 | .getCanonicalType(FD->getType())); |
| 217 | |
| 218 | mangleBareFunctionType(FT, MangleReturnType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 221 | static bool isStdNamespace(const NamespaceDecl *NS) { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 222 | const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); |
| 223 | return II && II->isStr("std"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 226 | static bool isStdNamespace(const DeclContext *DC) { |
| 227 | return DC->isNamespace() && DC->getParent()->isTranslationUnit() && |
| 228 | isStdNamespace(cast<NamespaceDecl>(DC)); |
| 229 | } |
| 230 | |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 231 | static const TemplateDecl * |
| 232 | isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 233 | // Check if we have a function template. |
| 234 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 235 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 236 | TemplateArgs = FD->getTemplateSpecializationArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 237 | return TD; |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 241 | // Check if we have a class template. |
| 242 | if (const ClassTemplateSpecializationDecl *Spec = |
| 243 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 244 | TemplateArgs = &Spec->getTemplateArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 245 | return Spec->getSpecializedTemplate(); |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 246 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 247 | |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 251 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 252 | // <name> ::= <nested-name> |
| 253 | // ::= <unscoped-name> |
| 254 | // ::= <unscoped-template-name> <template-args> |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 255 | // ::= <local-name> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 256 | // |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 257 | const DeclContext *DC = ND->getDeclContext(); |
Anders Carlsson | 5cc58c6 | 2009-09-22 17:23:30 +0000 | [diff] [blame] | 258 | while (isa<LinkageSpecDecl>(DC)) |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 259 | DC = DC->getParent(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 260 | |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 261 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 262 | // Check if we have a template. |
| 263 | const TemplateArgumentList *TemplateArgs = 0; |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 264 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 265 | mangleUnscopedTemplateName(TD); |
| 266 | mangleTemplateArgumentList(*TemplateArgs); |
| 267 | return; |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 268 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 269 | |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 270 | mangleUnscopedName(ND); |
| 271 | return; |
| 272 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 273 | |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 274 | if (isa<FunctionDecl>(DC)) { |
| 275 | mangleLocalName(ND); |
| 276 | return; |
| 277 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 278 | |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 279 | mangleNestedName(ND); |
| 280 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 281 | void CXXNameMangler::mangleName(const TemplateDecl *TD, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 282 | const TemplateArgument *TemplateArgs, |
| 283 | unsigned NumTemplateArgs) { |
| 284 | const DeclContext *DC = TD->getDeclContext(); |
| 285 | while (isa<LinkageSpecDecl>(DC)) { |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 286 | assert(cast<LinkageSpecDecl>(DC)->getLanguage() == |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 287 | LinkageSpecDecl::lang_cxx && "Unexpected linkage decl!"); |
| 288 | DC = DC->getParent(); |
| 289 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 290 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 291 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 292 | mangleUnscopedTemplateName(TD); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 293 | mangleTemplateArgs(TemplateArgs, NumTemplateArgs); |
| 294 | } else { |
| 295 | mangleNestedName(TD, TemplateArgs, NumTemplateArgs); |
| 296 | } |
| 297 | } |
| 298 | |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 299 | void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) { |
| 300 | // <unscoped-name> ::= <unqualified-name> |
| 301 | // ::= St <unqualified-name> # ::std:: |
| 302 | if (isStdNamespace(ND->getDeclContext())) |
| 303 | Out << "St"; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 304 | |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 305 | mangleUnqualifiedName(ND); |
| 306 | } |
| 307 | |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 308 | void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) { |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 309 | // <unscoped-template-name> ::= <unscoped-name> |
| 310 | // ::= <substitution> |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 311 | if (mangleSubstitution(ND)) |
Anders Carlsson | 03c9d53 | 2009-09-17 04:02:31 +0000 | [diff] [blame] | 312 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 313 | |
Anders Carlsson | 1668f20 | 2009-09-26 20:13:56 +0000 | [diff] [blame] | 314 | mangleUnscopedName(ND->getTemplatedDecl()); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 315 | addSubstitution(ND); |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 318 | void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 319 | // <call-offset> ::= h <nv-offset> _ |
| 320 | // ::= v <v-offset> _ |
| 321 | // <nv-offset> ::= <offset number> # non-virtual base override |
| 322 | // <v-offset> ::= <offset nubmer> _ <virtual offset number> |
| 323 | // # virtual base override, with vcall offset |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 324 | if (v == 0) { |
Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 325 | Out << "h"; |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 326 | if (nv < 0) { |
| 327 | Out << "n"; |
| 328 | nv = -nv; |
| 329 | } |
| 330 | Out << nv; |
| 331 | } else { |
Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 332 | Out << "v"; |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 333 | if (nv < 0) { |
| 334 | Out << "n"; |
| 335 | nv = -nv; |
| 336 | } |
| 337 | Out << nv; |
| 338 | Out << "_"; |
| 339 | if (v < 0) { |
| 340 | Out << "n"; |
| 341 | v = -v; |
| 342 | } |
| 343 | Out << v; |
| 344 | } |
| 345 | Out << "_"; |
Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 348 | void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) { |
| 349 | // <unqualified-name> ::= <operator-name> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | // ::= <ctor-dtor-name> |
| 351 | // ::= <source-name> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 352 | DeclarationName Name = ND->getDeclName(); |
| 353 | switch (Name.getNameKind()) { |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 354 | case DeclarationName::Identifier: { |
| 355 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 356 | if (NS->isAnonymousNamespace()) { |
| 357 | // This is how gcc mangles these names. It's apparently |
| 358 | // always '1', no matter how many different anonymous |
| 359 | // namespaces appear in a context. |
| 360 | Out << "12_GLOBAL__N_1"; |
| 361 | break; |
| 362 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 363 | } |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 364 | |
| 365 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
| 366 | mangleSourceName(II); |
| 367 | break; |
| 368 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 369 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 370 | // We must have an anonymous struct. |
| 371 | const TagDecl *TD = cast<TagDecl>(ND); |
| 372 | if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { |
| 373 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 374 | "Typedef should not be in another decl context!"); |
| 375 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 376 | "Typedef was not named!"); |
| 377 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 378 | break; |
| 379 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 380 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 381 | // Get a unique id for the anonymous struct. |
| 382 | uint64_t AnonStructId = Context.getAnonymousStructId(TD); |
| 383 | |
| 384 | // Mangle it as a source name in the form |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 385 | // [n] $_<id> |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 386 | // where n is the length of the string. |
| 387 | llvm::SmallString<8> Str; |
| 388 | Str += "$_"; |
| 389 | Str += llvm::utostr(AnonStructId); |
| 390 | |
| 391 | Out << Str.size(); |
| 392 | Out << Str.str(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 393 | break; |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 394 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 395 | |
| 396 | case DeclarationName::ObjCZeroArgSelector: |
| 397 | case DeclarationName::ObjCOneArgSelector: |
| 398 | case DeclarationName::ObjCMultiArgSelector: |
| 399 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 400 | break; |
| 401 | |
| 402 | case DeclarationName::CXXConstructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 403 | if (ND == Structor) |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 404 | // If the named decl is the C++ constructor we're mangling, use the type |
| 405 | // we were given. |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 406 | mangleCXXCtorType(static_cast<CXXCtorType>(StructorType)); |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 407 | else |
| 408 | // Otherwise, use the complete constructor name. This is relevant if a |
| 409 | // class with a constructor is declared within a constructor. |
| 410 | mangleCXXCtorType(Ctor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 411 | break; |
| 412 | |
| 413 | case DeclarationName::CXXDestructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 414 | if (ND == Structor) |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 415 | // If the named decl is the C++ destructor we're mangling, use the type we |
| 416 | // were given. |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 417 | mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); |
| 418 | else |
| 419 | // Otherwise, use the complete destructor name. This is relevant if a |
| 420 | // class with a destructor is declared within a destructor. |
| 421 | mangleCXXDtorType(Dtor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 422 | break; |
| 423 | |
| 424 | case DeclarationName::CXXConversionFunctionName: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | // <operator-name> ::= cv <type> # (cast) |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 426 | Out << "cv"; |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 427 | mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType())); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 428 | break; |
| 429 | |
| 430 | case DeclarationName::CXXOperatorName: |
| 431 | mangleOperatorName(Name.getCXXOverloadedOperator(), |
| 432 | cast<FunctionDecl>(ND)->getNumParams()); |
| 433 | break; |
| 434 | |
| 435 | case DeclarationName::CXXUsingDirective: |
| 436 | assert(false && "Can't mangle a using directive name!"); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 437 | break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 442 | // <source-name> ::= <positive length number> <identifier> |
| 443 | // <number> ::= [n] <non-negative decimal integer> |
| 444 | // <identifier> ::= <unqualified source code identifier> |
| 445 | Out << II->getLength() << II->getName(); |
| 446 | } |
| 447 | |
| 448 | void CXXNameMangler::mangleNestedName(const NamedDecl *ND) { |
| 449 | // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
| 450 | // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 451 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 452 | Out << 'N'; |
| 453 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 454 | mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 455 | |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 456 | // Check if we have a template. |
| 457 | const TemplateArgumentList *TemplateArgs = 0; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 458 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 459 | mangleTemplatePrefix(TD); |
| 460 | mangleTemplateArgumentList(*TemplateArgs); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 461 | } else { |
| 462 | manglePrefix(ND->getDeclContext()); |
| 463 | mangleUnqualifiedName(ND); |
| 464 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 466 | Out << 'E'; |
| 467 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 468 | void CXXNameMangler::mangleNestedName(const TemplateDecl *TD, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 469 | const TemplateArgument *TemplateArgs, |
| 470 | unsigned NumTemplateArgs) { |
Anders Carlsson | e45117b | 2009-09-27 19:53:49 +0000 | [diff] [blame] | 471 | // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 472 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 473 | Out << 'N'; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 474 | |
Anders Carlsson | e45117b | 2009-09-27 19:53:49 +0000 | [diff] [blame] | 475 | mangleTemplatePrefix(TD); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 476 | mangleTemplateArgs(TemplateArgs, NumTemplateArgs); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 477 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 478 | Out << 'E'; |
| 479 | } |
| 480 | |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 481 | void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { |
| 482 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 483 | // := Z <function encoding> E s [<discriminator>] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | // <discriminator> := _ <non-negative number> |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 485 | Out << 'Z'; |
| 486 | mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext())); |
| 487 | Out << 'E'; |
| 488 | mangleSourceName(ND->getIdentifier()); |
| 489 | } |
| 490 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 491 | void CXXNameMangler::manglePrefix(const DeclContext *DC) { |
| 492 | // <prefix> ::= <prefix> <unqualified-name> |
| 493 | // ::= <template-prefix> <template-args> |
| 494 | // ::= <template-param> |
| 495 | // ::= # empty |
| 496 | // ::= <substitution> |
| 497 | // FIXME: We only handle mangling of namespaces and classes at the moment. |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 498 | |
Anders Carlsson | add2882 | 2009-09-22 20:33:31 +0000 | [diff] [blame] | 499 | while (isa<LinkageSpecDecl>(DC)) |
| 500 | DC = DC->getParent(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 501 | |
Anders Carlsson | 9263e91 | 2009-09-18 18:39:58 +0000 | [diff] [blame] | 502 | if (DC->isTranslationUnit()) |
| 503 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 504 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 505 | if (mangleSubstitution(cast<NamedDecl>(DC))) |
| 506 | return; |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 507 | |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 508 | // Check if we have a template. |
| 509 | const TemplateArgumentList *TemplateArgs = 0; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 510 | if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) { |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 511 | mangleTemplatePrefix(TD); |
| 512 | mangleTemplateArgumentList(*TemplateArgs); |
| 513 | } else { |
| 514 | manglePrefix(DC->getParent()); |
| 515 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 516 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 517 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 518 | addSubstitution(cast<NamedDecl>(DC)); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 521 | void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) { |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 522 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 523 | // ::= <template-param> |
| 524 | // ::= <substitution> |
| 525 | |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 526 | if (mangleSubstitution(ND)) |
| 527 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 528 | |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 529 | // FIXME: <template-param> |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 530 | |
Anders Carlsson | aa73ab1 | 2009-09-18 18:47:07 +0000 | [diff] [blame] | 531 | manglePrefix(ND->getDeclContext()); |
Anders Carlsson | 1668f20 | 2009-09-26 20:13:56 +0000 | [diff] [blame] | 532 | mangleUnqualifiedName(ND->getTemplatedDecl()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 533 | |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 534 | addSubstitution(ND); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | void |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 538 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 539 | switch (OO) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 540 | // <operator-name> ::= nw # new |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 541 | case OO_New: Out << "nw"; break; |
| 542 | // ::= na # new[] |
| 543 | case OO_Array_New: Out << "na"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 544 | // ::= dl # delete |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 545 | case OO_Delete: Out << "dl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 546 | // ::= da # delete[] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 547 | case OO_Array_Delete: Out << "da"; break; |
| 548 | // ::= ps # + (unary) |
| 549 | // ::= pl # + |
| 550 | case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 551 | // ::= ng # - (unary) |
| 552 | // ::= mi # - |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 553 | case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 554 | // ::= ad # & (unary) |
| 555 | // ::= an # & |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 556 | case OO_Amp: Out << (Arity == 1? "ad" : "an"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 557 | // ::= de # * (unary) |
| 558 | // ::= ml # * |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 559 | case OO_Star: Out << (Arity == 1? "de" : "ml"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 560 | // ::= co # ~ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 561 | case OO_Tilde: Out << "co"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 562 | // ::= dv # / |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 563 | case OO_Slash: Out << "dv"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 564 | // ::= rm # % |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 565 | case OO_Percent: Out << "rm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 566 | // ::= or # | |
| 567 | case OO_Pipe: Out << "or"; break; |
| 568 | // ::= eo # ^ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 569 | case OO_Caret: Out << "eo"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 570 | // ::= aS # = |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 571 | case OO_Equal: Out << "aS"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 572 | // ::= pL # += |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 573 | case OO_PlusEqual: Out << "pL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 574 | // ::= mI # -= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 575 | case OO_MinusEqual: Out << "mI"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 576 | // ::= mL # *= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 577 | case OO_StarEqual: Out << "mL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 578 | // ::= dV # /= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 579 | case OO_SlashEqual: Out << "dV"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 580 | // ::= rM # %= |
| 581 | case OO_PercentEqual: Out << "rM"; break; |
| 582 | // ::= aN # &= |
| 583 | case OO_AmpEqual: Out << "aN"; break; |
| 584 | // ::= oR # |= |
| 585 | case OO_PipeEqual: Out << "oR"; break; |
| 586 | // ::= eO # ^= |
| 587 | case OO_CaretEqual: Out << "eO"; break; |
| 588 | // ::= ls # << |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 589 | case OO_LessLess: Out << "ls"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 590 | // ::= rs # >> |
| 591 | case OO_GreaterGreater: Out << "rs"; break; |
| 592 | // ::= lS # <<= |
| 593 | case OO_LessLessEqual: Out << "lS"; break; |
| 594 | // ::= rS # >>= |
| 595 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 596 | // ::= eq # == |
| 597 | case OO_EqualEqual: Out << "eq"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 598 | // ::= ne # != |
| 599 | case OO_ExclaimEqual: Out << "ne"; break; |
| 600 | // ::= lt # < |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 601 | case OO_Less: Out << "lt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 602 | // ::= gt # > |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 603 | case OO_Greater: Out << "gt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 604 | // ::= le # <= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 605 | case OO_LessEqual: Out << "le"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 606 | // ::= ge # >= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 607 | case OO_GreaterEqual: Out << "ge"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 608 | // ::= nt # ! |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 609 | case OO_Exclaim: Out << "nt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 610 | // ::= aa # && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 611 | case OO_AmpAmp: Out << "aa"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 612 | // ::= oo # || |
| 613 | case OO_PipePipe: Out << "oo"; break; |
| 614 | // ::= pp # ++ |
| 615 | case OO_PlusPlus: Out << "pp"; break; |
| 616 | // ::= mm # -- |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 617 | case OO_MinusMinus: Out << "mm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 618 | // ::= cm # , |
| 619 | case OO_Comma: Out << "cm"; break; |
| 620 | // ::= pm # ->* |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 621 | case OO_ArrowStar: Out << "pm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 622 | // ::= pt # -> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 623 | case OO_Arrow: Out << "pt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 624 | // ::= cl # () |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 625 | case OO_Call: Out << "cl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 626 | // ::= ix # [] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 627 | case OO_Subscript: Out << "ix"; break; |
| 628 | // UNSUPPORTED: ::= qu # ? |
| 629 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 630 | case OO_None: |
| 631 | case OO_Conditional: |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 632 | case NUM_OVERLOADED_OPERATORS: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | assert(false && "Not an overloaded operator"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | } |
| 637 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 638 | void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 640 | if (Quals.hasRestrict()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 641 | Out << 'r'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 642 | if (Quals.hasVolatile()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 643 | Out << 'V'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 644 | if (Quals.hasConst()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 645 | Out << 'K'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 646 | |
| 647 | // FIXME: For now, just drop all extension qualifiers on the floor. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void CXXNameMangler::mangleType(QualType T) { |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 651 | // Only operate on the canonical type! |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 652 | T = Context.getASTContext().getCanonicalType(T); |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 653 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 654 | bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 655 | if (IsSubstitutable && mangleSubstitution(T)) |
| 656 | return; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 658 | if (Qualifiers Quals = T.getLocalQualifiers()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 659 | mangleQualifiers(Quals); |
| 660 | // Recurse: even if the qualified type isn't yet substitutable, |
| 661 | // the unqualified type might be. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 662 | mangleType(T.getLocalUnqualifiedType()); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 663 | } else { |
| 664 | switch (T->getTypeClass()) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 665 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 666 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 667 | case Type::CLASS: \ |
| 668 | llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
| 669 | return; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 670 | #define TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 671 | case Type::CLASS: \ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 672 | mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 673 | break; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 674 | #include "clang/AST/TypeNodes.def" |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 675 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 676 | } |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 677 | |
| 678 | // Add the substitution. |
| 679 | if (IsSubstitutable) |
| 680 | addSubstitution(T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 684 | // <type> ::= <builtin-type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 685 | // <builtin-type> ::= v # void |
| 686 | // ::= w # wchar_t |
| 687 | // ::= b # bool |
| 688 | // ::= c # char |
| 689 | // ::= a # signed char |
| 690 | // ::= h # unsigned char |
| 691 | // ::= s # short |
| 692 | // ::= t # unsigned short |
| 693 | // ::= i # int |
| 694 | // ::= j # unsigned int |
| 695 | // ::= l # long |
| 696 | // ::= m # unsigned long |
| 697 | // ::= x # long long, __int64 |
| 698 | // ::= y # unsigned long long, __int64 |
| 699 | // ::= n # __int128 |
| 700 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 701 | // ::= f # float |
| 702 | // ::= d # double |
| 703 | // ::= e # long double, __float80 |
| 704 | // UNSUPPORTED: ::= g # __float128 |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 705 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 706 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 707 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 708 | // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 709 | // ::= Di # char32_t |
| 710 | // ::= Ds # char16_t |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 711 | // ::= u <source-name> # vendor extended type |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 712 | // From our point of view, std::nullptr_t is a builtin, but as far as mangling |
| 713 | // is concerned, it's a type called std::nullptr_t. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 714 | switch (T->getKind()) { |
| 715 | case BuiltinType::Void: Out << 'v'; break; |
| 716 | case BuiltinType::Bool: Out << 'b'; break; |
| 717 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 718 | case BuiltinType::UChar: Out << 'h'; break; |
| 719 | case BuiltinType::UShort: Out << 't'; break; |
| 720 | case BuiltinType::UInt: Out << 'j'; break; |
| 721 | case BuiltinType::ULong: Out << 'm'; break; |
| 722 | case BuiltinType::ULongLong: Out << 'y'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 723 | case BuiltinType::UInt128: Out << 'o'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 724 | case BuiltinType::SChar: Out << 'a'; break; |
| 725 | case BuiltinType::WChar: Out << 'w'; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 726 | case BuiltinType::Char16: Out << "Ds"; break; |
| 727 | case BuiltinType::Char32: Out << "Di"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 728 | case BuiltinType::Short: Out << 's'; break; |
| 729 | case BuiltinType::Int: Out << 'i'; break; |
| 730 | case BuiltinType::Long: Out << 'l'; break; |
| 731 | case BuiltinType::LongLong: Out << 'x'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 732 | case BuiltinType::Int128: Out << 'n'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 733 | case BuiltinType::Float: Out << 'f'; break; |
| 734 | case BuiltinType::Double: Out << 'd'; break; |
| 735 | case BuiltinType::LongDouble: Out << 'e'; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 736 | case BuiltinType::NullPtr: Out << "St9nullptr_t"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 737 | |
| 738 | case BuiltinType::Overload: |
| 739 | case BuiltinType::Dependent: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | assert(false && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 741 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 742 | break; |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 743 | case BuiltinType::UndeducedAuto: |
| 744 | assert(0 && "Should not see undeduced auto here"); |
| 745 | break; |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 746 | case BuiltinType::ObjCId: Out << "11objc_object"; break; |
| 747 | case BuiltinType::ObjCClass: Out << "10objc_class"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 751 | // <type> ::= <function-type> |
| 752 | // <function-type> ::= F [Y] <bare-function-type> E |
| 753 | void CXXNameMangler::mangleType(const FunctionProtoType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 754 | Out << 'F'; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 755 | // FIXME: We don't have enough information in the AST to produce the 'Y' |
| 756 | // encoding for extern "C" function types. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 757 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 758 | Out << 'E'; |
| 759 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 760 | void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
| 761 | llvm::llvm_unreachable("Can't mangle K&R function prototypes"); |
| 762 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 763 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 764 | bool MangleReturnType) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 765 | // We should never be mangling something without a prototype. |
| 766 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 767 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 768 | // <bare-function-type> ::= <signature type>+ |
| 769 | if (MangleReturnType) |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 770 | mangleType(Proto->getResultType()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 771 | |
Anders Carlsson | c6c91bc | 2009-04-01 00:15:23 +0000 | [diff] [blame] | 772 | if (Proto->getNumArgs() == 0) { |
| 773 | Out << 'v'; |
| 774 | return; |
| 775 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 777 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | ArgEnd = Proto->arg_type_end(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 779 | Arg != ArgEnd; ++Arg) |
| 780 | mangleType(*Arg); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 781 | |
| 782 | // <builtin-type> ::= z # ellipsis |
| 783 | if (Proto->isVariadic()) |
| 784 | Out << 'z'; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 785 | } |
| 786 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 787 | // <type> ::= <class-enum-type> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | // <class-enum-type> ::= <name> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 789 | void CXXNameMangler::mangleType(const EnumType *T) { |
| 790 | mangleType(static_cast<const TagType*>(T)); |
| 791 | } |
| 792 | void CXXNameMangler::mangleType(const RecordType *T) { |
| 793 | mangleType(static_cast<const TagType*>(T)); |
| 794 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 795 | void CXXNameMangler::mangleType(const TagType *T) { |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 796 | if (!T->getDecl()->getIdentifier()) |
| 797 | mangleName(T->getDecl()->getTypedefForAnonDecl()); |
| 798 | else |
| 799 | mangleName(T->getDecl()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 800 | } |
| 801 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 802 | // <type> ::= <array-type> |
| 803 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 804 | // ::= A [<dimension expression>] _ <element type> |
| 805 | void CXXNameMangler::mangleType(const ConstantArrayType *T) { |
| 806 | Out << 'A' << T->getSize() << '_'; |
| 807 | mangleType(T->getElementType()); |
| 808 | } |
| 809 | void CXXNameMangler::mangleType(const VariableArrayType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 810 | Out << 'A'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 811 | mangleExpression(T->getSizeExpr()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 812 | Out << '_'; |
| 813 | mangleType(T->getElementType()); |
| 814 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 815 | void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { |
| 816 | Out << 'A'; |
| 817 | mangleExpression(T->getSizeExpr()); |
| 818 | Out << '_'; |
| 819 | mangleType(T->getElementType()); |
| 820 | } |
| 821 | void CXXNameMangler::mangleType(const IncompleteArrayType *T) { |
| 822 | Out << 'A' << '_'; |
| 823 | mangleType(T->getElementType()); |
| 824 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 825 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 826 | // <type> ::= <pointer-to-member-type> |
| 827 | // <pointer-to-member-type> ::= M <class type> <member type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 828 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 829 | Out << 'M'; |
| 830 | mangleType(QualType(T->getClass(), 0)); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 831 | QualType PointeeType = T->getPointeeType(); |
| 832 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 833 | mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals())); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 834 | mangleType(FPT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | } else |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 836 | mangleType(PointeeType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 837 | } |
| 838 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 839 | // <type> ::= <template-param> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 840 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 841 | mangleTemplateParameter(T->getIndex()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 842 | } |
| 843 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 844 | // FIXME: <type> ::= <template-template-param> <template-args> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 845 | |
| 846 | // <type> ::= P <type> # pointer-to |
| 847 | void CXXNameMangler::mangleType(const PointerType *T) { |
| 848 | Out << 'P'; |
| 849 | mangleType(T->getPointeeType()); |
| 850 | } |
| 851 | void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { |
| 852 | Out << 'P'; |
| 853 | mangleType(T->getPointeeType()); |
| 854 | } |
| 855 | |
| 856 | // <type> ::= R <type> # reference-to |
| 857 | void CXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 858 | Out << 'R'; |
| 859 | mangleType(T->getPointeeType()); |
| 860 | } |
| 861 | |
| 862 | // <type> ::= O <type> # rvalue reference-to (C++0x) |
| 863 | void CXXNameMangler::mangleType(const RValueReferenceType *T) { |
| 864 | Out << 'O'; |
| 865 | mangleType(T->getPointeeType()); |
| 866 | } |
| 867 | |
| 868 | // <type> ::= C <type> # complex pair (C 2000) |
| 869 | void CXXNameMangler::mangleType(const ComplexType *T) { |
| 870 | Out << 'C'; |
| 871 | mangleType(T->getElementType()); |
| 872 | } |
| 873 | |
| 874 | // GNU extension: vector types |
| 875 | void CXXNameMangler::mangleType(const VectorType *T) { |
| 876 | Out << "U8__vector"; |
| 877 | mangleType(T->getElementType()); |
| 878 | } |
| 879 | void CXXNameMangler::mangleType(const ExtVectorType *T) { |
| 880 | mangleType(static_cast<const VectorType*>(T)); |
| 881 | } |
| 882 | void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { |
| 883 | Out << "U8__vector"; |
| 884 | mangleType(T->getElementType()); |
| 885 | } |
| 886 | |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 887 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 888 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 889 | } |
| 890 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 891 | void CXXNameMangler::mangleType(const BlockPointerType *T) { |
| 892 | assert(false && "can't mangle block pointer types yet"); |
| 893 | } |
| 894 | |
| 895 | void CXXNameMangler::mangleType(const FixedWidthIntType *T) { |
| 896 | assert(false && "can't mangle arbitary-precision integer type yet"); |
| 897 | } |
| 898 | |
| 899 | void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 900 | TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl(); |
| 901 | assert(TD && "FIXME: Support dependent template names!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 902 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 903 | mangleName(TD, T->getArgs(), T->getNumArgs()); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | void CXXNameMangler::mangleType(const TypenameType *T) { |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 907 | // Typename types are always nested |
| 908 | Out << 'N'; |
| 909 | |
| 910 | const Type *QTy = T->getQualifier()->getAsType(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 911 | if (const TemplateSpecializationType *TST = |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 912 | dyn_cast<TemplateSpecializationType>(QTy)) { |
Anders Carlsson | 8859917 | 2009-09-27 01:06:07 +0000 | [diff] [blame] | 913 | if (!mangleSubstitution(QualType(TST, 0))) { |
| 914 | TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl(); |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 915 | |
Anders Carlsson | 8859917 | 2009-09-27 01:06:07 +0000 | [diff] [blame] | 916 | mangleTemplatePrefix(TD); |
| 917 | mangleTemplateArgs(TST->getArgs(), TST->getNumArgs()); |
| 918 | addSubstitution(QualType(TST, 0)); |
| 919 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 920 | } else if (const TemplateTypeParmType *TTPT = |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 921 | dyn_cast<TemplateTypeParmType>(QTy)) { |
| 922 | // We use the QualType mangle type variant here because it handles |
| 923 | // substitutions. |
| 924 | mangleType(QualType(TTPT, 0)); |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 925 | } else |
| 926 | assert(false && "Unhandled type!"); |
| 927 | |
| 928 | mangleSourceName(T->getIdentifier()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 929 | |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 930 | Out << 'E'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 933 | void CXXNameMangler::mangleExpression(const Expr *E) { |
| 934 | // <expression> ::= <unary operator-name> <expression> |
| 935 | // ::= <binary operator-name> <expression> <expression> |
| 936 | // ::= <trinary operator-name> <expression> <expression> <expression> |
| 937 | // ::= cl <expression>* E # call |
| 938 | // ::= cv <type> expression # conversion with one argument |
| 939 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
| 940 | // ::= st <type> # sizeof (a type) |
| 941 | // ::= at <type> # alignof (a type) |
| 942 | // ::= <template-param> |
| 943 | // ::= <function-param> |
| 944 | // ::= sr <type> <unqualified-name> # dependent name |
| 945 | // ::= sr <type> <unqualified-name> <template-args> # dependent template-id |
| 946 | // ::= sZ <template-param> # size of a parameter pack |
| 947 | // ::= <expr-primary> |
| 948 | switch (E->getStmtClass()) { |
| 949 | default: assert(false && "Unhandled expression kind!"); |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 950 | |
| 951 | case Expr::ParenExprClass: |
| 952 | mangleExpression(cast<ParenExpr>(E)->getSubExpr()); |
| 953 | break; |
| 954 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 955 | case Expr::DeclRefExprClass: { |
| 956 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 957 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 958 | switch (D->getKind()) { |
| 959 | default: assert(false && "Unhandled decl kind!"); |
| 960 | case Decl::NonTypeTemplateParm: { |
| 961 | const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D); |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 962 | mangleTemplateParameter(PD->getIndex()); |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 963 | break; |
| 964 | } |
| 965 | |
| 966 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 967 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 968 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 969 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 970 | |
John McCall | 865d447 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 971 | case Expr::DependentScopeDeclRefExprClass: { |
| 972 | const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E); |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 973 | const Type *QTy = DRE->getQualifier()->getAsType(); |
| 974 | assert(QTy && "Qualifier was not type!"); |
| 975 | |
| 976 | // ::= sr <type> <unqualified-name> # dependent name |
| 977 | Out << "sr"; |
| 978 | mangleType(QualType(QTy, 0)); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 979 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 980 | assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier && |
| 981 | "Unhandled decl name kind!"); |
| 982 | mangleSourceName(DRE->getDeclName().getAsIdentifierInfo()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 983 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 984 | break; |
| 985 | } |
| 986 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 987 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 988 | } |
| 989 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 990 | // FIXME: <type> ::= G <type> # imaginary (C 2000) |
| 991 | // FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier |
| 992 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 993 | void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { |
| 994 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 995 | // ::= C2 # base object constructor |
| 996 | // ::= C3 # complete object allocating constructor |
| 997 | // |
| 998 | switch (T) { |
| 999 | case Ctor_Complete: |
| 1000 | Out << "C1"; |
| 1001 | break; |
| 1002 | case Ctor_Base: |
| 1003 | Out << "C2"; |
| 1004 | break; |
| 1005 | case Ctor_CompleteAllocating: |
| 1006 | Out << "C3"; |
| 1007 | break; |
| 1008 | } |
| 1009 | } |
| 1010 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 1011 | void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
| 1012 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 1013 | // ::= D1 # complete object destructor |
| 1014 | // ::= D2 # base object destructor |
| 1015 | // |
| 1016 | switch (T) { |
| 1017 | case Dtor_Deleting: |
| 1018 | Out << "D0"; |
| 1019 | break; |
| 1020 | case Dtor_Complete: |
| 1021 | Out << "D1"; |
| 1022 | break; |
| 1023 | case Dtor_Base: |
| 1024 | Out << "D2"; |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | |
Anders Carlsson | 068f347 | 2009-09-17 05:31:47 +0000 | [diff] [blame] | 1029 | void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) { |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1030 | // <template-args> ::= I <template-arg>+ E |
| 1031 | Out << "I"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1033 | for (unsigned i = 0, e = L.size(); i != e; ++i) { |
| 1034 | const TemplateArgument &A = L[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | |
Anders Carlsson | 068f347 | 2009-09-17 05:31:47 +0000 | [diff] [blame] | 1036 | mangleTemplateArgument(A); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1037 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1039 | Out << "E"; |
| 1040 | } |
| 1041 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1042 | void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs, |
| 1043 | unsigned NumTemplateArgs) { |
| 1044 | // <template-args> ::= I <template-arg>+ E |
| 1045 | Out << "I"; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1046 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1047 | for (unsigned i = 0; i != NumTemplateArgs; ++i) { |
| 1048 | mangleTemplateArgument(TemplateArgs[i]); |
| 1049 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1050 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1051 | Out << "E"; |
| 1052 | } |
| 1053 | |
Anders Carlsson | 068f347 | 2009-09-17 05:31:47 +0000 | [diff] [blame] | 1054 | void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | // <template-arg> ::= <type> # type or template |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1056 | // ::= X <expression> E # expression |
| 1057 | // ::= <expr-primary> # simple expressions |
| 1058 | // ::= I <template-arg>* E # argument pack |
| 1059 | // ::= sp <expression> # pack expansion of (C++0x) |
| 1060 | switch (A.getKind()) { |
| 1061 | default: |
| 1062 | assert(0 && "Unknown template argument kind!"); |
| 1063 | case TemplateArgument::Type: |
| 1064 | mangleType(A.getAsType()); |
| 1065 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1066 | case TemplateArgument::Expression: |
| 1067 | Out << 'X'; |
| 1068 | mangleExpression(A.getAsExpr()); |
| 1069 | Out << 'E'; |
| 1070 | break; |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1071 | case TemplateArgument::Integral: |
| 1072 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1073 | |
| 1074 | Out << 'L'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1076 | mangleType(A.getIntegralType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1078 | const llvm::APSInt *Integral = A.getAsIntegral(); |
| 1079 | if (A.getIntegralType()->isBooleanType()) { |
| 1080 | // Boolean values are encoded as 0/1. |
| 1081 | Out << (Integral->getBoolValue() ? '1' : '0'); |
| 1082 | } else { |
| 1083 | if (Integral->isNegative()) |
| 1084 | Out << 'n'; |
| 1085 | Integral->abs().print(Out, false); |
| 1086 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1088 | Out << 'E'; |
| 1089 | break; |
| 1090 | } |
| 1091 | } |
| 1092 | |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1093 | void CXXNameMangler::mangleTemplateParameter(unsigned Index) { |
| 1094 | // <template-param> ::= T_ # first template parameter |
| 1095 | // ::= T <parameter-2 non-negative number> _ |
| 1096 | if (Index == 0) |
| 1097 | Out << "T_"; |
| 1098 | else |
| 1099 | Out << 'T' << (Index - 1) << '_'; |
| 1100 | } |
| 1101 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1102 | // <substitution> ::= S <seq-id> _ |
| 1103 | // ::= S_ |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 1104 | bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) { |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1105 | // Try one of the standard substitutions first. |
| 1106 | if (mangleStandardSubstitution(ND)) |
| 1107 | return true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1108 | |
Anders Carlsson | 433d137 | 2009-11-07 04:26:04 +0000 | [diff] [blame] | 1109 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 1110 | return mangleSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 1111 | } |
| 1112 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1113 | bool CXXNameMangler::mangleSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 1114 | if (!T.getCVRQualifiers()) { |
| 1115 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 1116 | return mangleSubstitution(RT->getDecl()); |
| 1117 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1118 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1119 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 1120 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1121 | return mangleSubstitution(TypePtr); |
| 1122 | } |
| 1123 | |
| 1124 | bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) { |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1125 | llvm::DenseMap<uintptr_t, unsigned>::iterator I = |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1126 | Substitutions.find(Ptr); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1127 | if (I == Substitutions.end()) |
| 1128 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1129 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1130 | unsigned SeqID = I->second; |
| 1131 | if (SeqID == 0) |
| 1132 | Out << "S_"; |
| 1133 | else { |
| 1134 | SeqID--; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1135 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1136 | // <seq-id> is encoded in base-36, using digits and upper case letters. |
| 1137 | char Buffer[10]; |
| 1138 | char *BufferPtr = Buffer + 9; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1139 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1140 | *BufferPtr = 0; |
| 1141 | if (SeqID == 0) *--BufferPtr = '0'; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1142 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1143 | while (SeqID) { |
| 1144 | assert(BufferPtr > Buffer && "Buffer overflow!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1145 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1146 | unsigned char c = static_cast<unsigned char>(SeqID) % 36; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1147 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1148 | *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10); |
| 1149 | SeqID /= 36; |
| 1150 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1151 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1152 | Out << 'S' << BufferPtr << '_'; |
| 1153 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1154 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1155 | return true; |
| 1156 | } |
| 1157 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1158 | static bool isCharType(QualType T) { |
| 1159 | if (T.isNull()) |
| 1160 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1161 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1162 | return T->isSpecificBuiltinType(BuiltinType::Char_S) || |
| 1163 | T->isSpecificBuiltinType(BuiltinType::Char_U); |
| 1164 | } |
| 1165 | |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1166 | /// isCharSpecialization - Returns whether a given type is a template |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1167 | /// specialization of a given name with a single argument of type char. |
| 1168 | static bool isCharSpecialization(QualType T, const char *Name) { |
| 1169 | if (T.isNull()) |
| 1170 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1171 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1172 | const RecordType *RT = T->getAs<RecordType>(); |
| 1173 | if (!RT) |
| 1174 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1175 | |
| 1176 | const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1177 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 1178 | if (!SD) |
| 1179 | return false; |
| 1180 | |
| 1181 | if (!isStdNamespace(SD->getDeclContext())) |
| 1182 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1183 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1184 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 1185 | if (TemplateArgs.size() != 1) |
| 1186 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1187 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1188 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1189 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1190 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1191 | return SD->getIdentifier()->getName() == Name; |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1194 | bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) { |
| 1195 | // <substitution> ::= St # ::std:: |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1196 | // FIXME: type_info == comes out as __ZNK3std9type_infoeqERKS0_ instead of |
| 1197 | // __ZNKSt9type_infoeqERKS_ |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1198 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 1199 | if (isStdNamespace(NS)) { |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1200 | Out << "St"; |
| 1201 | return true; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) { |
| 1206 | if (!isStdNamespace(TD->getDeclContext())) |
| 1207 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1208 | |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1209 | // <substitution> ::= Sa # ::std::allocator |
| 1210 | if (TD->getIdentifier()->isStr("allocator")) { |
| 1211 | Out << "Sa"; |
| 1212 | return true; |
| 1213 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1214 | |
Anders Carlsson | 189d59c | 2009-09-26 23:14:39 +0000 | [diff] [blame] | 1215 | // <<substitution> ::= Sb # ::std::basic_string |
| 1216 | if (TD->getIdentifier()->isStr("basic_string")) { |
| 1217 | Out << "Sb"; |
| 1218 | return true; |
| 1219 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1220 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1221 | |
| 1222 | if (const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1223 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 1224 | // <substitution> ::= Ss # ::std::basic_string<char, |
| 1225 | // ::std::char_traits<char>, |
| 1226 | // ::std::allocator<char> > |
| 1227 | if (SD->getIdentifier()->isStr("basic_string")) { |
| 1228 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1229 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1230 | if (TemplateArgs.size() != 3) |
| 1231 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1232 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1233 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1234 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1235 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1236 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 1237 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1238 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1239 | if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator")) |
| 1240 | return false; |
| 1241 | |
| 1242 | Out << "Ss"; |
| 1243 | return true; |
| 1244 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1245 | |
| 1246 | // <substitution> ::= So # ::std::basic_ostream<char, |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1247 | // ::std::char_traits<char> > |
| 1248 | if (SD->getIdentifier()->isStr("basic_ostream")) { |
| 1249 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1250 | |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1251 | if (TemplateArgs.size() != 2) |
| 1252 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1253 | |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1254 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1255 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1256 | |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1257 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 1258 | return false; |
| 1259 | |
| 1260 | Out << "So"; |
| 1261 | return true; |
| 1262 | } |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1263 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1264 | return false; |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1267 | void CXXNameMangler::addSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 1268 | if (!T.getCVRQualifiers()) { |
| 1269 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1270 | addSubstitution(RT->getDecl()); |
| 1271 | return; |
| 1272 | } |
| 1273 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1274 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1275 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1276 | addSubstitution(TypePtr); |
| 1277 | } |
| 1278 | |
| 1279 | void CXXNameMangler::addSubstitution(uintptr_t Ptr) { |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1280 | unsigned SeqID = Substitutions.size(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1281 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1282 | assert(!Substitutions.count(Ptr) && "Substitution already exists!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1283 | Substitutions[Ptr] = SeqID; |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1286 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1288 | /// \brief Mangles the name of the declaration D and emits that name to the |
| 1289 | /// given output stream. |
| 1290 | /// |
| 1291 | /// If the declaration D requires a mangled name, this routine will emit that |
| 1292 | /// mangled name to \p os and return true. Otherwise, \p os will be unchanged |
| 1293 | /// and this routine will return false. In this case, the caller should just |
| 1294 | /// emit the identifier of the declaration (\c D->getIdentifier()) as its |
| 1295 | /// name. |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1296 | bool MangleContext::mangleName(const NamedDecl *D, |
| 1297 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c02ab4c | 2009-11-21 09:14:44 +0000 | [diff] [blame^] | 1298 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 1299 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 1300 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 1301 | "Invalid mangleName() call on 'structor decl!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1302 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1303 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 1304 | getASTContext().getSourceManager(), |
| 1305 | "Mangling declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1307 | CXXNameMangler Mangler(*this, Res); |
| 1308 | return Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1311 | void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1312 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 1313 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 1314 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1317 | void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1318 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 1319 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 1320 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1321 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1322 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1323 | /// \brief Mangles the a thunk with the offset n for the declaration D and |
| 1324 | /// emits that name to the given output stream. |
| 1325 | void MangleContext::mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v, |
| 1326 | llvm::SmallVectorImpl<char> &Res) { |
| 1327 | // FIXME: Hum, we might have to thunk these, fix. |
| 1328 | assert(!isa<CXXDestructorDecl>(FD) && |
| 1329 | "Use mangleCXXDtor for destructor decls!"); |
| 1330 | |
| 1331 | // <special-name> ::= T <call-offset> <base encoding> |
| 1332 | // # base is the nominal target function of thunk |
| 1333 | CXXNameMangler Mangler(*this, Res); |
| 1334 | Mangler.getStream() << "_ZT"; |
| 1335 | Mangler.mangleCalloffset(nv, v); |
| 1336 | Mangler.mangleFunctionEncoding(FD); |
| 1337 | } |
| 1338 | |
| 1339 | /// \brief Mangles the a covariant thunk for the declaration D and emits that |
| 1340 | /// name to the given output stream. |
| 1341 | void MangleContext::mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, |
| 1342 | int64_t v_t, int64_t nv_r, int64_t v_r, |
| 1343 | llvm::SmallVectorImpl<char> &Res) { |
| 1344 | // FIXME: Hum, we might have to thunk these, fix. |
| 1345 | assert(!isa<CXXDestructorDecl>(FD) && |
| 1346 | "Use mangleCXXDtor for destructor decls!"); |
| 1347 | |
| 1348 | // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> |
| 1349 | // # base is the nominal target function of thunk |
| 1350 | // # first call-offset is 'this' adjustment |
| 1351 | // # second call-offset is result adjustment |
| 1352 | CXXNameMangler Mangler(*this, Res); |
| 1353 | Mangler.getStream() << "_ZTc"; |
| 1354 | Mangler.mangleCalloffset(nv_t, v_t); |
| 1355 | Mangler.mangleCalloffset(nv_r, v_r); |
| 1356 | Mangler.mangleFunctionEncoding(FD); |
| 1357 | } |
| 1358 | |
| 1359 | /// mangleGuardVariable - Returns the mangled name for a guard variable |
| 1360 | /// for the passed in VarDecl. |
| 1361 | void MangleContext::mangleGuardVariable(const VarDecl *D, |
| 1362 | llvm::SmallVectorImpl<char> &Res) { |
| 1363 | // <special-name> ::= GV <object name> # Guard variable for one-time |
| 1364 | // # initialization |
| 1365 | CXXNameMangler Mangler(*this, Res); |
| 1366 | Mangler.getStream() << "_ZGV"; |
| 1367 | Mangler.mangleName(D); |
| 1368 | } |
| 1369 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1370 | void MangleContext::mangleCXXVtable(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1371 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1372 | // <special-name> ::= TV <type> # virtual table |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1373 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1374 | Mangler.getStream() << "_ZTV"; |
| 1375 | Mangler.mangleName(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1376 | } |
Mike Stump | 82d75b0 | 2009-11-10 01:58:37 +0000 | [diff] [blame] | 1377 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1378 | void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1379 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1380 | // <special-name> ::= TT <type> # VTT structure |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1381 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1382 | Mangler.getStream() << "_ZTT"; |
| 1383 | Mangler.mangleName(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1384 | } |
Mike Stump | ab3f7e9 | 2009-11-10 01:41:59 +0000 | [diff] [blame] | 1385 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1386 | void MangleContext::mangleCXXCtorVtable(const CXXRecordDecl *RD, int64_t Offset, |
| 1387 | const CXXRecordDecl *Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1388 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1389 | // <special-name> ::= TC <type> <offset number> _ <base type> |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1390 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1391 | Mangler.getStream() << "_ZTC"; |
| 1392 | Mangler.mangleName(RD); |
| 1393 | Mangler.getStream() << Offset; |
| 1394 | Mangler.getStream() << "_"; |
| 1395 | Mangler.mangleName(Type); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1396 | } |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 1397 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1398 | void MangleContext::mangleCXXRtti(QualType Ty, |
| 1399 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1400 | // <special-name> ::= TI <type> # typeinfo structure |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1401 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1402 | Mangler.getStream() << "_ZTI"; |
| 1403 | Mangler.mangleType(Ty); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1404 | } |
Mike Stump | 6779598 | 2009-11-14 00:14:13 +0000 | [diff] [blame] | 1405 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1406 | void MangleContext::mangleCXXRttiName(QualType Ty, |
| 1407 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1408 | // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1409 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1410 | Mangler.getStream() << "_ZTS"; |
| 1411 | Mangler.mangleType(Ty); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1412 | } |