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