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" |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | using namespace clang; |
| 27 | |
| 28 | namespace { |
| 29 | class VISIBILITY_HIDDEN CXXNameMangler { |
| 30 | ASTContext &Context; |
| 31 | llvm::raw_ostream &Out; |
| 32 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 33 | const CXXMethodDecl *Structor; |
| 34 | unsigned StructorType; |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 35 | CXXCtorType CtorType; |
| 36 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 37 | public: |
| 38 | CXXNameMangler(ASTContext &C, llvm::raw_ostream &os) |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 39 | : Context(C), Out(os), Structor(0), StructorType(0) { } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 40 | |
| 41 | bool mangle(const NamedDecl *D); |
Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 42 | void mangleGuardVariable(const VarDecl *D); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 43 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame^] | 44 | void mangleCXXVtable(QualType Type); |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 45 | void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 46 | void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type); |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 48 | private: |
| 49 | bool mangleFunctionDecl(const FunctionDecl *FD); |
| 50 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 51 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 52 | void mangleName(const NamedDecl *ND); |
| 53 | void mangleUnqualifiedName(const NamedDecl *ND); |
| 54 | void mangleSourceName(const IdentifierInfo *II); |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 55 | void mangleLocalName(const NamedDecl *ND); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 56 | void mangleNestedName(const NamedDecl *ND); |
| 57 | void manglePrefix(const DeclContext *DC); |
| 58 | void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); |
| 59 | void mangleCVQualifiers(unsigned Quals); |
| 60 | void mangleType(QualType T); |
| 61 | void mangleType(const BuiltinType *T); |
| 62 | void mangleType(const FunctionType *T); |
| 63 | void mangleBareFunctionType(const FunctionType *T, bool MangleReturnType); |
| 64 | void mangleType(const TagType *T); |
| 65 | void mangleType(const ArrayType *T); |
| 66 | void mangleType(const MemberPointerType *T); |
| 67 | void mangleType(const TemplateTypeParmType *T); |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 68 | void mangleType(const ObjCInterfaceType *T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 69 | void mangleExpression(Expr *E); |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 70 | void mangleCXXCtorType(CXXCtorType T); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 71 | void mangleCXXDtorType(CXXDtorType T); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 72 | |
| 73 | void mangleTemplateArgumentList(const TemplateArgumentList &L); |
| 74 | void mangleTemplateArgument(const TemplateArgument &A); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 75 | }; |
| 76 | } |
| 77 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 78 | static bool isInCLinkageSpecification(const Decl *D) { |
| 79 | for (const DeclContext *DC = D->getDeclContext(); |
| 80 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 81 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
| 82 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) { |
| 89 | // Clang's "overloadable" attribute extension to C/C++ implies |
| 90 | // name mangling (always). |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 91 | if (!FD->hasAttr<OverloadableAttr>()) { |
Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 92 | // C functions are not mangled, and "main" is never mangled. |
| 93 | if (!Context.getLangOptions().CPlusPlus || FD->isMain()) |
| 94 | return false; |
| 95 | |
| 96 | // No mangling in an "implicit extern C" header. |
| 97 | if (FD->getLocation().isValid() && |
| 98 | Context.getSourceManager().isInExternCSystemHeader(FD->getLocation())) |
| 99 | return false; |
| 100 | |
| 101 | // No name mangling in a C linkage specification. |
| 102 | if (isInCLinkageSpecification(FD)) |
| 103 | return false; |
| 104 | } |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 105 | |
| 106 | // If we get here, mangle the decl name! |
| 107 | Out << "_Z"; |
| 108 | mangleFunctionEncoding(FD); |
| 109 | return true; |
| 110 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 111 | |
| 112 | bool CXXNameMangler::mangle(const NamedDecl *D) { |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 113 | // Any decl can be declared with __asm("foo") on it, and this takes |
| 114 | // precedence over all other naming in the .o file. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 115 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 116 | // If we have an asm name, then we use it as the mangling. |
| 117 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 118 | Out << ALA->getLabel(); |
| 119 | return true; |
| 120 | } |
| 121 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 122 | // <mangled-name> ::= _Z <encoding> |
| 123 | // ::= <data name> |
| 124 | // ::= <special-name> |
| 125 | |
| 126 | // FIXME: Actually use a visitor to decode these? |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 127 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 128 | return mangleFunctionDecl(FD); |
Chris Lattner | bc7a029 | 2009-03-21 06:19:20 +0000 | [diff] [blame] | 129 | |
Anders Carlsson | 329749c | 2009-04-02 16:05:20 +0000 | [diff] [blame] | 130 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 131 | if (!Context.getLangOptions().CPlusPlus || |
Anders Carlsson | 9ccb065 | 2009-04-11 01:19:45 +0000 | [diff] [blame] | 132 | isInCLinkageSpecification(D) || |
| 133 | D->getDeclContext()->isTranslationUnit()) |
Anders Carlsson | 329749c | 2009-04-02 16:05:20 +0000 | [diff] [blame] | 134 | return false; |
| 135 | |
| 136 | Out << "_Z"; |
| 137 | mangleName(VD); |
| 138 | return true; |
| 139 | } |
| 140 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 141 | return false; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 144 | void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D, |
| 145 | CXXCtorType Type) { |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 146 | assert(!Structor && "Structor already set!"); |
| 147 | Structor = D; |
| 148 | StructorType = Type; |
| 149 | |
| 150 | mangle(D); |
| 151 | } |
| 152 | |
| 153 | void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D, |
| 154 | CXXDtorType Type) { |
| 155 | assert(!Structor && "Structor already set!"); |
| 156 | Structor = D; |
| 157 | StructorType = Type; |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 158 | |
| 159 | mangle(D); |
| 160 | } |
| 161 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame^] | 162 | void CXXNameMangler::mangleCXXVtable(QualType T) { |
| 163 | // <special-name> ::= TV <type> # virtual table |
| 164 | Out << "_ZTV"; |
| 165 | mangleType(T); |
| 166 | } |
| 167 | |
Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 168 | void CXXNameMangler::mangleGuardVariable(const VarDecl *D) |
| 169 | { |
| 170 | // <special-name> ::= GV <object name> # Guard variable for one-time |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame^] | 171 | // # initialization |
Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 172 | |
| 173 | Out << "_ZGV"; |
| 174 | mangleName(D); |
| 175 | } |
| 176 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 177 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 178 | // <encoding> ::= <function name> <bare-function-type> |
| 179 | mangleName(FD); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 180 | |
| 181 | // Whether the mangling of a function type includes the return type depends |
| 182 | // on the context and the nature of the function. The rules for deciding |
| 183 | // whether the return type is included are: |
| 184 | // |
| 185 | // 1. Template functions (names or types) have return types encoded, with |
| 186 | // the exceptions listed below. |
| 187 | // 2. Function types not appearing as part of a function name mangling, |
| 188 | // e.g. parameters, pointer types, etc., have return type encoded, with the |
| 189 | // exceptions listed below. |
| 190 | // 3. Non-template function names do not have return types encoded. |
| 191 | // |
| 192 | // The exceptions mentioned in (1) and (2) above, for which the return |
| 193 | // type is never included, are |
| 194 | // 1. Constructors. |
| 195 | // 2. Destructors. |
| 196 | // 3. Conversion operator functions, e.g. operator int. |
| 197 | bool MangleReturnType = false; |
| 198 | if (FD->getPrimaryTemplate() && |
| 199 | !(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || |
| 200 | isa<CXXConversionDecl>(FD))) |
| 201 | MangleReturnType = true; |
| 202 | mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static bool isStdNamespace(const DeclContext *DC) { |
| 206 | if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit()) |
| 207 | return false; |
| 208 | |
| 209 | const NamespaceDecl *NS = cast<NamespaceDecl>(DC); |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 210 | return NS->getOriginalNamespace()->getIdentifier()->isStr("std"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 214 | // <name> ::= <nested-name> |
| 215 | // ::= <unscoped-name> |
| 216 | // ::= <unscoped-template-name> <template-args> |
| 217 | // ::= <local-name> # See Scope Encoding below |
| 218 | // |
| 219 | // <unscoped-name> ::= <unqualified-name> |
| 220 | // ::= St <unqualified-name> # ::std:: |
| 221 | if (ND->getDeclContext()->isTranslationUnit()) |
| 222 | mangleUnqualifiedName(ND); |
| 223 | else if (isStdNamespace(ND->getDeclContext())) { |
| 224 | Out << "St"; |
| 225 | mangleUnqualifiedName(ND); |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 226 | } else if (isa<FunctionDecl>(ND->getDeclContext())) |
| 227 | mangleLocalName(ND); |
| 228 | else |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 229 | mangleNestedName(ND); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) { |
| 233 | // <unqualified-name> ::= <operator-name> |
| 234 | // ::= <ctor-dtor-name> |
| 235 | // ::= <source-name> |
| 236 | DeclarationName Name = ND->getDeclName(); |
| 237 | switch (Name.getNameKind()) { |
| 238 | case DeclarationName::Identifier: |
| 239 | mangleSourceName(Name.getAsIdentifierInfo()); |
| 240 | break; |
| 241 | |
| 242 | case DeclarationName::ObjCZeroArgSelector: |
| 243 | case DeclarationName::ObjCOneArgSelector: |
| 244 | case DeclarationName::ObjCMultiArgSelector: |
| 245 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 246 | break; |
| 247 | |
| 248 | case DeclarationName::CXXConstructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 249 | if (ND == Structor) |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 250 | // If the named decl is the C++ constructor we're mangling, use the |
| 251 | // type we were given. |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 252 | mangleCXXCtorType(static_cast<CXXCtorType>(StructorType)); |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 253 | else |
| 254 | // Otherwise, use the complete constructor name. This is relevant if a |
| 255 | // class with a constructor is declared within a constructor. |
| 256 | mangleCXXCtorType(Ctor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case DeclarationName::CXXDestructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 260 | if (ND == Structor) |
| 261 | // If the named decl is the C++ destructor we're mangling, use the |
| 262 | // type we were given. |
| 263 | mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); |
| 264 | else |
| 265 | // Otherwise, use the complete destructor name. This is relevant if a |
| 266 | // class with a destructor is declared within a destructor. |
| 267 | mangleCXXDtorType(Dtor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 268 | break; |
| 269 | |
| 270 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 271 | // <operator-name> ::= cv <type> # (cast) |
| 272 | Out << "cv"; |
| 273 | mangleType(Context.getCanonicalType(Name.getCXXNameType())); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 274 | break; |
| 275 | |
| 276 | case DeclarationName::CXXOperatorName: |
| 277 | mangleOperatorName(Name.getCXXOverloadedOperator(), |
| 278 | cast<FunctionDecl>(ND)->getNumParams()); |
| 279 | break; |
| 280 | |
| 281 | case DeclarationName::CXXUsingDirective: |
| 282 | assert(false && "Can't mangle a using directive name!"); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 283 | break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 284 | } |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 285 | |
| 286 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
| 287 | if (const TemplateArgumentList *TemplateArgs |
| 288 | = Function->getTemplateSpecializationArgs()) |
| 289 | mangleTemplateArgumentList(*TemplateArgs); |
| 290 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 294 | // <source-name> ::= <positive length number> <identifier> |
| 295 | // <number> ::= [n] <non-negative decimal integer> |
| 296 | // <identifier> ::= <unqualified source code identifier> |
| 297 | Out << II->getLength() << II->getName(); |
| 298 | } |
| 299 | |
| 300 | void CXXNameMangler::mangleNestedName(const NamedDecl *ND) { |
| 301 | // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
| 302 | // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 303 | // FIXME: no template support |
| 304 | Out << 'N'; |
| 305 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) |
| 306 | mangleCVQualifiers(Method->getTypeQualifiers()); |
| 307 | manglePrefix(ND->getDeclContext()); |
| 308 | mangleUnqualifiedName(ND); |
| 309 | Out << 'E'; |
| 310 | } |
| 311 | |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 312 | void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { |
| 313 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 314 | // := Z <function encoding> E s [<discriminator>] |
| 315 | // <discriminator> := _ <non-negative number> |
| 316 | Out << 'Z'; |
| 317 | mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext())); |
| 318 | Out << 'E'; |
| 319 | mangleSourceName(ND->getIdentifier()); |
| 320 | } |
| 321 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 322 | void CXXNameMangler::manglePrefix(const DeclContext *DC) { |
| 323 | // <prefix> ::= <prefix> <unqualified-name> |
| 324 | // ::= <template-prefix> <template-args> |
| 325 | // ::= <template-param> |
| 326 | // ::= # empty |
| 327 | // ::= <substitution> |
| 328 | // FIXME: We only handle mangling of namespaces and classes at the moment. |
Anders Carlsson | c8dee9c | 2009-04-01 00:42:16 +0000 | [diff] [blame] | 329 | if (!DC->getParent()->isTranslationUnit()) |
| 330 | manglePrefix(DC->getParent()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 331 | |
| 332 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC)) |
| 333 | mangleSourceName(Namespace->getIdentifier()); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 334 | else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) { |
| 335 | if (const ClassTemplateSpecializationDecl *D = |
| 336 | dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 337 | mangleType(QualType(D->getTypeForDecl(), 0)); |
| 338 | } else |
| 339 | mangleSourceName(Record->getIdentifier()); |
| 340 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void |
| 344 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 345 | switch (OO) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 346 | // <operator-name> ::= nw # new |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 347 | case OO_New: Out << "nw"; break; |
| 348 | // ::= na # new[] |
| 349 | case OO_Array_New: Out << "na"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 350 | // ::= dl # delete |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 351 | case OO_Delete: Out << "dl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 352 | // ::= da # delete[] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 353 | case OO_Array_Delete: Out << "da"; break; |
| 354 | // ::= ps # + (unary) |
| 355 | // ::= pl # + |
| 356 | case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 357 | // ::= ng # - (unary) |
| 358 | // ::= mi # - |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 359 | case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 360 | // ::= ad # & (unary) |
| 361 | // ::= an # & |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 362 | case OO_Amp: Out << (Arity == 1? "ad" : "an"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 363 | // ::= de # * (unary) |
| 364 | // ::= ml # * |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 365 | case OO_Star: Out << (Arity == 1? "de" : "ml"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 366 | // ::= co # ~ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 367 | case OO_Tilde: Out << "co"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 368 | // ::= dv # / |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 369 | case OO_Slash: Out << "dv"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 370 | // ::= rm # % |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 371 | case OO_Percent: Out << "rm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 372 | // ::= or # | |
| 373 | case OO_Pipe: Out << "or"; break; |
| 374 | // ::= eo # ^ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 375 | case OO_Caret: Out << "eo"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 376 | // ::= aS # = |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 377 | case OO_Equal: Out << "aS"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 378 | // ::= pL # += |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 379 | case OO_PlusEqual: Out << "pL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 380 | // ::= mI # -= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 381 | case OO_MinusEqual: Out << "mI"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 382 | // ::= mL # *= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 383 | case OO_StarEqual: Out << "mL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 384 | // ::= dV # /= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 385 | case OO_SlashEqual: Out << "dV"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 386 | // ::= rM # %= |
| 387 | case OO_PercentEqual: Out << "rM"; break; |
| 388 | // ::= aN # &= |
| 389 | case OO_AmpEqual: Out << "aN"; break; |
| 390 | // ::= oR # |= |
| 391 | case OO_PipeEqual: Out << "oR"; break; |
| 392 | // ::= eO # ^= |
| 393 | case OO_CaretEqual: Out << "eO"; break; |
| 394 | // ::= ls # << |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 395 | case OO_LessLess: Out << "ls"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 396 | // ::= rs # >> |
| 397 | case OO_GreaterGreater: Out << "rs"; break; |
| 398 | // ::= lS # <<= |
| 399 | case OO_LessLessEqual: Out << "lS"; break; |
| 400 | // ::= rS # >>= |
| 401 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 402 | // ::= eq # == |
| 403 | case OO_EqualEqual: Out << "eq"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 404 | // ::= ne # != |
| 405 | case OO_ExclaimEqual: Out << "ne"; break; |
| 406 | // ::= lt # < |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 407 | case OO_Less: Out << "lt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 408 | // ::= gt # > |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 409 | case OO_Greater: Out << "gt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 410 | // ::= le # <= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 411 | case OO_LessEqual: Out << "le"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 412 | // ::= ge # >= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 413 | case OO_GreaterEqual: Out << "ge"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 414 | // ::= nt # ! |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 415 | case OO_Exclaim: Out << "nt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 416 | // ::= aa # && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 417 | case OO_AmpAmp: Out << "aa"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 418 | // ::= oo # || |
| 419 | case OO_PipePipe: Out << "oo"; break; |
| 420 | // ::= pp # ++ |
| 421 | case OO_PlusPlus: Out << "pp"; break; |
| 422 | // ::= mm # -- |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 423 | case OO_MinusMinus: Out << "mm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 424 | // ::= cm # , |
| 425 | case OO_Comma: Out << "cm"; break; |
| 426 | // ::= pm # ->* |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 427 | case OO_ArrowStar: Out << "pm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 428 | // ::= pt # -> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 429 | case OO_Arrow: Out << "pt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 430 | // ::= cl # () |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 431 | case OO_Call: Out << "cl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 432 | // ::= ix # [] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 433 | case OO_Subscript: Out << "ix"; break; |
| 434 | // UNSUPPORTED: ::= qu # ? |
| 435 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 436 | case OO_None: |
| 437 | case OO_Conditional: |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 438 | case NUM_OVERLOADED_OPERATORS: |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 439 | assert(false && "Not an overloaded operator"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void CXXNameMangler::mangleCVQualifiers(unsigned Quals) { |
| 445 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
| 446 | if (Quals & QualType::Restrict) |
| 447 | Out << 'r'; |
| 448 | if (Quals & QualType::Volatile) |
| 449 | Out << 'V'; |
| 450 | if (Quals & QualType::Const) |
| 451 | Out << 'K'; |
| 452 | } |
| 453 | |
| 454 | void CXXNameMangler::mangleType(QualType T) { |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 455 | // Only operate on the canonical type! |
| 456 | T = Context.getCanonicalType(T); |
| 457 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 458 | // FIXME: Should we have a TypeNodes.def to make this easier? (YES!) |
| 459 | |
| 460 | // <type> ::= <CV-qualifiers> <type> |
| 461 | mangleCVQualifiers(T.getCVRQualifiers()); |
| 462 | |
| 463 | // ::= <builtin-type> |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 464 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 465 | mangleType(BT); |
| 466 | // ::= <function-type> |
| 467 | else if (const FunctionType *FT = dyn_cast<FunctionType>(T.getTypePtr())) |
| 468 | mangleType(FT); |
| 469 | // ::= <class-enum-type> |
| 470 | else if (const TagType *TT = dyn_cast<TagType>(T.getTypePtr())) |
| 471 | mangleType(TT); |
| 472 | // ::= <array-type> |
| 473 | else if (const ArrayType *AT = dyn_cast<ArrayType>(T.getTypePtr())) |
| 474 | mangleType(AT); |
| 475 | // ::= <pointer-to-member-type> |
| 476 | else if (const MemberPointerType *MPT |
| 477 | = dyn_cast<MemberPointerType>(T.getTypePtr())) |
| 478 | mangleType(MPT); |
| 479 | // ::= <template-param> |
| 480 | else if (const TemplateTypeParmType *TypeParm |
| 481 | = dyn_cast<TemplateTypeParmType>(T.getTypePtr())) |
| 482 | mangleType(TypeParm); |
| 483 | // FIXME: ::= <template-template-param> <template-args> |
| 484 | // FIXME: ::= <substitution> # See Compression below |
| 485 | // ::= P <type> # pointer-to |
| 486 | else if (const PointerType *PT = dyn_cast<PointerType>(T.getTypePtr())) { |
| 487 | Out << 'P'; |
| 488 | mangleType(PT->getPointeeType()); |
| 489 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 490 | else if (const ObjCObjectPointerType *PT = |
| 491 | dyn_cast<ObjCObjectPointerType>(T.getTypePtr())) { |
| 492 | Out << 'P'; |
| 493 | mangleType(PT->getPointeeType()); |
| 494 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 495 | // ::= R <type> # reference-to |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 496 | else if (const LValueReferenceType *RT = |
| 497 | dyn_cast<LValueReferenceType>(T.getTypePtr())) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 498 | Out << 'R'; |
| 499 | mangleType(RT->getPointeeType()); |
| 500 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 501 | // ::= O <type> # rvalue reference-to (C++0x) |
| 502 | else if (const RValueReferenceType *RT = |
| 503 | dyn_cast<RValueReferenceType>(T.getTypePtr())) { |
| 504 | Out << 'O'; |
| 505 | mangleType(RT->getPointeeType()); |
| 506 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 507 | // ::= C <type> # complex pair (C 2000) |
| 508 | else if (const ComplexType *CT = dyn_cast<ComplexType>(T.getTypePtr())) { |
| 509 | Out << 'C'; |
| 510 | mangleType(CT->getElementType()); |
| 511 | } else if (const VectorType *VT = dyn_cast<VectorType>(T.getTypePtr())) { |
| 512 | // GNU extension: vector types |
| 513 | Out << "U8__vector"; |
| 514 | mangleType(VT->getElementType()); |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 515 | } else if (const ObjCInterfaceType *IT = |
| 516 | dyn_cast<ObjCInterfaceType>(T.getTypePtr())) { |
| 517 | mangleType(IT); |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 518 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 519 | // FIXME: ::= G <type> # imaginary (C 2000) |
| 520 | // FIXME: ::= U <source-name> <type> # vendor extended type qualifier |
| 521 | else |
| 522 | assert(false && "Cannot mangle unknown type"); |
| 523 | } |
| 524 | |
| 525 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
| 526 | // <builtin-type> ::= v # void |
| 527 | // ::= w # wchar_t |
| 528 | // ::= b # bool |
| 529 | // ::= c # char |
| 530 | // ::= a # signed char |
| 531 | // ::= h # unsigned char |
| 532 | // ::= s # short |
| 533 | // ::= t # unsigned short |
| 534 | // ::= i # int |
| 535 | // ::= j # unsigned int |
| 536 | // ::= l # long |
| 537 | // ::= m # unsigned long |
| 538 | // ::= x # long long, __int64 |
| 539 | // ::= y # unsigned long long, __int64 |
| 540 | // ::= n # __int128 |
| 541 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 542 | // ::= f # float |
| 543 | // ::= d # double |
| 544 | // ::= e # long double, __float80 |
| 545 | // UNSUPPORTED: ::= g # __float128 |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 546 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 547 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 548 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 549 | // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 550 | // ::= Di # char32_t |
| 551 | // ::= Ds # char16_t |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 552 | // ::= u <source-name> # vendor extended type |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 553 | // From our point of view, std::nullptr_t is a builtin, but as far as mangling |
| 554 | // is concerned, it's a type called std::nullptr_t. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 555 | switch (T->getKind()) { |
| 556 | case BuiltinType::Void: Out << 'v'; break; |
| 557 | case BuiltinType::Bool: Out << 'b'; break; |
| 558 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 559 | case BuiltinType::UChar: Out << 'h'; break; |
| 560 | case BuiltinType::UShort: Out << 't'; break; |
| 561 | case BuiltinType::UInt: Out << 'j'; break; |
| 562 | case BuiltinType::ULong: Out << 'm'; break; |
| 563 | case BuiltinType::ULongLong: Out << 'y'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 564 | case BuiltinType::UInt128: Out << 'o'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 565 | case BuiltinType::SChar: Out << 'a'; break; |
| 566 | case BuiltinType::WChar: Out << 'w'; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 567 | case BuiltinType::Char16: Out << "Ds"; break; |
| 568 | case BuiltinType::Char32: Out << "Di"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 569 | case BuiltinType::Short: Out << 's'; break; |
| 570 | case BuiltinType::Int: Out << 'i'; break; |
| 571 | case BuiltinType::Long: Out << 'l'; break; |
| 572 | case BuiltinType::LongLong: Out << 'x'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 573 | case BuiltinType::Int128: Out << 'n'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 574 | case BuiltinType::Float: Out << 'f'; break; |
| 575 | case BuiltinType::Double: Out << 'd'; break; |
| 576 | case BuiltinType::LongDouble: Out << 'e'; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 577 | case BuiltinType::NullPtr: Out << "St9nullptr_t"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 578 | |
| 579 | case BuiltinType::Overload: |
| 580 | case BuiltinType::Dependent: |
| 581 | assert(false && |
| 582 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 583 | break; |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 584 | case BuiltinType::UndeducedAuto: |
| 585 | assert(0 && "Should not see undeduced auto here"); |
| 586 | break; |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 587 | case BuiltinType::ObjCId: Out << "11objc_object"; break; |
| 588 | case BuiltinType::ObjCClass: Out << "10objc_class"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | void CXXNameMangler::mangleType(const FunctionType *T) { |
| 593 | // <function-type> ::= F [Y] <bare-function-type> E |
| 594 | Out << 'F'; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 595 | // FIXME: We don't have enough information in the AST to produce the 'Y' |
| 596 | // encoding for extern "C" function types. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 597 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 598 | Out << 'E'; |
| 599 | } |
| 600 | |
| 601 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 602 | bool MangleReturnType) { |
| 603 | // <bare-function-type> ::= <signature type>+ |
| 604 | if (MangleReturnType) |
| 605 | mangleType(T->getResultType()); |
| 606 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 607 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 608 | assert(Proto && "Can't mangle K&R function prototypes"); |
| 609 | |
Anders Carlsson | c6c91bc | 2009-04-01 00:15:23 +0000 | [diff] [blame] | 610 | if (Proto->getNumArgs() == 0) { |
| 611 | Out << 'v'; |
| 612 | return; |
| 613 | } |
| 614 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 615 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 616 | ArgEnd = Proto->arg_type_end(); |
| 617 | Arg != ArgEnd; ++Arg) |
| 618 | mangleType(*Arg); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 619 | |
| 620 | // <builtin-type> ::= z # ellipsis |
| 621 | if (Proto->isVariadic()) |
| 622 | Out << 'z'; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void CXXNameMangler::mangleType(const TagType *T) { |
| 626 | // <class-enum-type> ::= <name> |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 627 | |
| 628 | if (!T->getDecl()->getIdentifier()) |
| 629 | mangleName(T->getDecl()->getTypedefForAnonDecl()); |
| 630 | else |
| 631 | mangleName(T->getDecl()); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 632 | |
| 633 | // If this is a class template specialization, mangle the template |
| 634 | // arguments. |
| 635 | if (ClassTemplateSpecializationDecl *Spec |
| 636 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) |
| 637 | mangleTemplateArgumentList(Spec->getTemplateArgs()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | void CXXNameMangler::mangleType(const ArrayType *T) { |
| 641 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 642 | // ::= A [<dimension expression>] _ <element type> |
| 643 | Out << 'A'; |
| 644 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) |
| 645 | Out << CAT->getSize(); |
| 646 | else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(T)) |
| 647 | mangleExpression(VAT->getSizeExpr()); |
| 648 | else if (const DependentSizedArrayType *DSAT |
| 649 | = dyn_cast<DependentSizedArrayType>(T)) |
| 650 | mangleExpression(DSAT->getSizeExpr()); |
| 651 | |
| 652 | Out << '_'; |
| 653 | mangleType(T->getElementType()); |
| 654 | } |
| 655 | |
| 656 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
| 657 | // <pointer-to-member-type> ::= M <class type> <member type> |
| 658 | Out << 'M'; |
| 659 | mangleType(QualType(T->getClass(), 0)); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 660 | QualType PointeeType = T->getPointeeType(); |
| 661 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
| 662 | mangleCVQualifiers(FPT->getTypeQuals()); |
| 663 | mangleType(FPT); |
| 664 | } else |
| 665 | mangleType(PointeeType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
| 669 | // <template-param> ::= T_ # first template parameter |
| 670 | // ::= T <parameter-2 non-negative number> _ |
| 671 | if (T->getIndex() == 0) |
| 672 | Out << "T_"; |
| 673 | else |
| 674 | Out << 'T' << (T->getIndex() - 1) << '_'; |
| 675 | } |
| 676 | |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 677 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 678 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 679 | } |
| 680 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 681 | void CXXNameMangler::mangleExpression(Expr *E) { |
| 682 | assert(false && "Cannot mangle expressions yet"); |
| 683 | } |
| 684 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 685 | void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { |
| 686 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 687 | // ::= C2 # base object constructor |
| 688 | // ::= C3 # complete object allocating constructor |
| 689 | // |
| 690 | switch (T) { |
| 691 | case Ctor_Complete: |
| 692 | Out << "C1"; |
| 693 | break; |
| 694 | case Ctor_Base: |
| 695 | Out << "C2"; |
| 696 | break; |
| 697 | case Ctor_CompleteAllocating: |
| 698 | Out << "C3"; |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 703 | void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
| 704 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 705 | // ::= D1 # complete object destructor |
| 706 | // ::= D2 # base object destructor |
| 707 | // |
| 708 | switch (T) { |
| 709 | case Dtor_Deleting: |
| 710 | Out << "D0"; |
| 711 | break; |
| 712 | case Dtor_Complete: |
| 713 | Out << "D1"; |
| 714 | break; |
| 715 | case Dtor_Base: |
| 716 | Out << "D2"; |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 721 | void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) { |
| 722 | // <template-args> ::= I <template-arg>+ E |
| 723 | Out << "I"; |
| 724 | |
| 725 | for (unsigned i = 0, e = L.size(); i != e; ++i) { |
| 726 | const TemplateArgument &A = L[i]; |
| 727 | |
| 728 | mangleTemplateArgument(A); |
| 729 | } |
| 730 | |
| 731 | Out << "E"; |
| 732 | } |
| 733 | |
| 734 | void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) { |
| 735 | // <template-arg> ::= <type> # type or template |
| 736 | // ::= X <expression> E # expression |
| 737 | // ::= <expr-primary> # simple expressions |
| 738 | // ::= I <template-arg>* E # argument pack |
| 739 | // ::= sp <expression> # pack expansion of (C++0x) |
| 740 | switch (A.getKind()) { |
| 741 | default: |
| 742 | assert(0 && "Unknown template argument kind!"); |
| 743 | case TemplateArgument::Type: |
| 744 | mangleType(A.getAsType()); |
| 745 | break; |
| 746 | case TemplateArgument::Integral: |
| 747 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 748 | |
| 749 | Out << 'L'; |
| 750 | |
| 751 | mangleType(A.getIntegralType()); |
| 752 | |
| 753 | const llvm::APSInt *Integral = A.getAsIntegral(); |
| 754 | if (A.getIntegralType()->isBooleanType()) { |
| 755 | // Boolean values are encoded as 0/1. |
| 756 | Out << (Integral->getBoolValue() ? '1' : '0'); |
| 757 | } else { |
| 758 | if (Integral->isNegative()) |
| 759 | Out << 'n'; |
| 760 | Integral->abs().print(Out, false); |
| 761 | } |
| 762 | |
| 763 | Out << 'E'; |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 768 | namespace clang { |
| 769 | /// \brief Mangles the name of the declaration D and emits that name |
| 770 | /// to the given output stream. |
| 771 | /// |
| 772 | /// If the declaration D requires a mangled name, this routine will |
| 773 | /// emit that mangled name to \p os and return true. Otherwise, \p |
| 774 | /// os will be unchanged and this routine will return false. In this |
| 775 | /// case, the caller should just emit the identifier of the declaration |
| 776 | /// (\c D->getIdentifier()) as its name. |
| 777 | bool mangleName(const NamedDecl *D, ASTContext &Context, |
| 778 | llvm::raw_ostream &os) { |
Anders Carlsson | 578aa64 | 2009-05-03 16:51:04 +0000 | [diff] [blame] | 779 | assert(!isa<CXXConstructorDecl>(D) && |
| 780 | "Use mangleCXXCtor for constructor decls!"); |
| 781 | assert(!isa<CXXDestructorDecl>(D) && |
| 782 | "Use mangleCXXDtor for destructor decls!"); |
| 783 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 784 | CXXNameMangler Mangler(Context, os); |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 785 | if (!Mangler.mangle(D)) |
| 786 | return false; |
| 787 | |
| 788 | os.flush(); |
| 789 | return true; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 790 | } |
Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 791 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 792 | /// mangleGuardVariable - Returns the mangled name for a guard variable |
| 793 | /// for the passed in VarDecl. |
Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 794 | void mangleGuardVariable(const VarDecl *D, ASTContext &Context, |
| 795 | llvm::raw_ostream &os) { |
| 796 | CXXNameMangler Mangler(Context, os); |
| 797 | Mangler.mangleGuardVariable(D); |
| 798 | |
| 799 | os.flush(); |
| 800 | } |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 801 | |
| 802 | void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
| 803 | ASTContext &Context, llvm::raw_ostream &os) { |
| 804 | CXXNameMangler Mangler(Context, os); |
| 805 | Mangler.mangleCXXCtor(D, Type); |
| 806 | |
| 807 | os.flush(); |
| 808 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 809 | |
| 810 | void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
| 811 | ASTContext &Context, llvm::raw_ostream &os) { |
| 812 | CXXNameMangler Mangler(Context, os); |
| 813 | Mangler.mangleCXXDtor(D, Type); |
| 814 | |
| 815 | os.flush(); |
| 816 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 817 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame^] | 818 | void mangleCXXVtable(QualType Type, ASTContext &Context, |
| 819 | llvm::raw_ostream &os) { |
| 820 | CXXNameMangler Mangler(Context, os); |
| 821 | Mangler.mangleCXXVtable(Type); |
| 822 | |
| 823 | os.flush(); |
| 824 | } |
| 825 | } |