Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===// |
| 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 "clang/AST/Mangle.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Attr.h" |
| 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclCXX.h" |
| 22 | #include "clang/AST/DeclObjC.h" |
| 23 | #include "clang/AST/DeclTemplate.h" |
| 24 | #include "clang/AST/ExprCXX.h" |
| 25 | #include "clang/AST/ExprObjC.h" |
| 26 | #include "clang/AST/TypeLoc.h" |
| 27 | #include "clang/Basic/ABI.h" |
| 28 | #include "clang/Basic/SourceManager.h" |
| 29 | #include "clang/Basic/TargetInfo.h" |
| 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | |
| 34 | #define MANGLE_CHECKER 0 |
| 35 | |
| 36 | #if MANGLE_CHECKER |
| 37 | #include <cxxabi.h> |
| 38 | #endif |
| 39 | |
| 40 | using namespace clang; |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | /// \brief Retrieve the declaration context that should be used when mangling |
| 45 | /// the given declaration. |
| 46 | static const DeclContext *getEffectiveDeclContext(const Decl *D) { |
| 47 | // The ABI assumes that lambda closure types that occur within |
| 48 | // default arguments live in the context of the function. However, due to |
| 49 | // the way in which Clang parses and creates function declarations, this is |
| 50 | // not the case: the lambda closure type ends up living in the context |
| 51 | // where the function itself resides, because the function declaration itself |
| 52 | // had not yet been created. Fix the context here. |
| 53 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 54 | if (RD->isLambda()) |
| 55 | if (ParmVarDecl *ContextParam |
| 56 | = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) |
| 57 | return ContextParam->getDeclContext(); |
| 58 | } |
| 59 | |
| 60 | return D->getDeclContext(); |
| 61 | } |
| 62 | |
| 63 | static const DeclContext *getEffectiveParentContext(const DeclContext *DC) { |
| 64 | return getEffectiveDeclContext(cast<Decl>(DC)); |
| 65 | } |
| 66 | |
| 67 | static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) { |
| 68 | const DeclContext *DC = dyn_cast<DeclContext>(ND); |
| 69 | if (!DC) |
| 70 | DC = getEffectiveDeclContext(ND); |
| 71 | while (!DC->isNamespace() && !DC->isTranslationUnit()) { |
| 72 | const DeclContext *Parent = getEffectiveDeclContext(cast<Decl>(DC)); |
| 73 | if (isa<FunctionDecl>(Parent)) |
| 74 | return dyn_cast<CXXRecordDecl>(DC); |
| 75 | DC = Parent; |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static const FunctionDecl *getStructor(const FunctionDecl *fn) { |
| 81 | if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate()) |
| 82 | return ftd->getTemplatedDecl(); |
| 83 | |
| 84 | return fn; |
| 85 | } |
| 86 | |
| 87 | static const NamedDecl *getStructor(const NamedDecl *decl) { |
| 88 | const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl); |
| 89 | return (fn ? getStructor(fn) : decl); |
| 90 | } |
| 91 | |
| 92 | static const unsigned UnknownArity = ~0U; |
| 93 | |
| 94 | class ItaniumMangleContext : public MangleContext { |
| 95 | llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds; |
| 96 | unsigned Discriminator; |
| 97 | llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier; |
| 98 | |
| 99 | public: |
| 100 | explicit ItaniumMangleContext(ASTContext &Context, |
| 101 | DiagnosticsEngine &Diags) |
| 102 | : MangleContext(Context, Diags) { } |
| 103 | |
| 104 | uint64_t getAnonymousStructId(const TagDecl *TD) { |
| 105 | std::pair<llvm::DenseMap<const TagDecl *, |
| 106 | uint64_t>::iterator, bool> Result = |
| 107 | AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size())); |
| 108 | return Result.first->second; |
| 109 | } |
| 110 | |
| 111 | void startNewFunction() { |
| 112 | MangleContext::startNewFunction(); |
| 113 | mangleInitDiscriminator(); |
| 114 | } |
| 115 | |
| 116 | /// @name Mangler Entry Points |
| 117 | /// @{ |
| 118 | |
| 119 | bool shouldMangleDeclName(const NamedDecl *D); |
| 120 | void mangleName(const NamedDecl *D, raw_ostream &); |
| 121 | void mangleThunk(const CXXMethodDecl *MD, |
| 122 | const ThunkInfo &Thunk, |
| 123 | raw_ostream &); |
| 124 | void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 125 | const ThisAdjustment &ThisAdjustment, |
| 126 | raw_ostream &); |
| 127 | void mangleReferenceTemporary(const VarDecl *D, |
| 128 | raw_ostream &); |
| 129 | void mangleCXXVTable(const CXXRecordDecl *RD, |
| 130 | raw_ostream &); |
| 131 | void mangleCXXVTT(const CXXRecordDecl *RD, |
| 132 | raw_ostream &); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 133 | void mangleCXXVBTable(const CXXRecordDecl *Derived, |
| 134 | ArrayRef<const CXXRecordDecl *> BasePath, |
| 135 | raw_ostream &Out); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 136 | void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, |
| 137 | const CXXRecordDecl *Type, |
| 138 | raw_ostream &); |
| 139 | void mangleCXXRTTI(QualType T, raw_ostream &); |
| 140 | void mangleCXXRTTIName(QualType T, raw_ostream &); |
| 141 | void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
| 142 | raw_ostream &); |
| 143 | void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
| 144 | raw_ostream &); |
| 145 | |
| 146 | void mangleItaniumGuardVariable(const VarDecl *D, raw_ostream &); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 147 | void mangleItaniumThreadLocalInit(const VarDecl *D, raw_ostream &); |
| 148 | void mangleItaniumThreadLocalWrapper(const VarDecl *D, raw_ostream &); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 149 | |
| 150 | void mangleInitDiscriminator() { |
| 151 | Discriminator = 0; |
| 152 | } |
| 153 | |
| 154 | bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { |
| 155 | // Lambda closure types with external linkage (indicated by a |
| 156 | // non-zero lambda mangling number) have their own numbering scheme, so |
| 157 | // they do not need a discriminator. |
| 158 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 159 | if (RD->isLambda() && RD->getLambdaManglingNumber() > 0) |
| 160 | return false; |
| 161 | |
| 162 | unsigned &discriminator = Uniquifier[ND]; |
| 163 | if (!discriminator) |
| 164 | discriminator = ++Discriminator; |
| 165 | if (discriminator == 1) |
| 166 | return false; |
| 167 | disc = discriminator-2; |
| 168 | return true; |
| 169 | } |
| 170 | /// @} |
| 171 | }; |
| 172 | |
| 173 | /// CXXNameMangler - Manage the mangling of a single name. |
| 174 | class CXXNameMangler { |
| 175 | ItaniumMangleContext &Context; |
| 176 | raw_ostream &Out; |
| 177 | |
| 178 | /// The "structor" is the top-level declaration being mangled, if |
| 179 | /// that's not a template specialization; otherwise it's the pattern |
| 180 | /// for that specialization. |
| 181 | const NamedDecl *Structor; |
| 182 | unsigned StructorType; |
| 183 | |
| 184 | /// SeqID - The next subsitution sequence number. |
| 185 | unsigned SeqID; |
| 186 | |
| 187 | class FunctionTypeDepthState { |
| 188 | unsigned Bits; |
| 189 | |
| 190 | enum { InResultTypeMask = 1 }; |
| 191 | |
| 192 | public: |
| 193 | FunctionTypeDepthState() : Bits(0) {} |
| 194 | |
| 195 | /// The number of function types we're inside. |
| 196 | unsigned getDepth() const { |
| 197 | return Bits >> 1; |
| 198 | } |
| 199 | |
| 200 | /// True if we're in the return type of the innermost function type. |
| 201 | bool isInResultType() const { |
| 202 | return Bits & InResultTypeMask; |
| 203 | } |
| 204 | |
| 205 | FunctionTypeDepthState push() { |
| 206 | FunctionTypeDepthState tmp = *this; |
| 207 | Bits = (Bits & ~InResultTypeMask) + 2; |
| 208 | return tmp; |
| 209 | } |
| 210 | |
| 211 | void enterResultType() { |
| 212 | Bits |= InResultTypeMask; |
| 213 | } |
| 214 | |
| 215 | void leaveResultType() { |
| 216 | Bits &= ~InResultTypeMask; |
| 217 | } |
| 218 | |
| 219 | void pop(FunctionTypeDepthState saved) { |
| 220 | assert(getDepth() == saved.getDepth() + 1); |
| 221 | Bits = saved.Bits; |
| 222 | } |
| 223 | |
| 224 | } FunctionTypeDepth; |
| 225 | |
| 226 | llvm::DenseMap<uintptr_t, unsigned> Substitutions; |
| 227 | |
| 228 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 229 | |
| 230 | public: |
| 231 | CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_, |
| 232 | const NamedDecl *D = 0) |
| 233 | : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0), |
| 234 | SeqID(0) { |
| 235 | // These can't be mangled without a ctor type or dtor type. |
| 236 | assert(!D || (!isa<CXXDestructorDecl>(D) && |
| 237 | !isa<CXXConstructorDecl>(D))); |
| 238 | } |
| 239 | CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_, |
| 240 | const CXXConstructorDecl *D, CXXCtorType Type) |
| 241 | : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), |
| 242 | SeqID(0) { } |
| 243 | CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_, |
| 244 | const CXXDestructorDecl *D, CXXDtorType Type) |
| 245 | : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), |
| 246 | SeqID(0) { } |
| 247 | |
| 248 | #if MANGLE_CHECKER |
| 249 | ~CXXNameMangler() { |
| 250 | if (Out.str()[0] == '\01') |
| 251 | return; |
| 252 | |
| 253 | int status = 0; |
| 254 | char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status); |
| 255 | assert(status == 0 && "Could not demangle mangled name!"); |
| 256 | free(result); |
| 257 | } |
| 258 | #endif |
| 259 | raw_ostream &getStream() { return Out; } |
| 260 | |
| 261 | void mangle(const NamedDecl *D, StringRef Prefix = "_Z"); |
| 262 | void mangleCallOffset(int64_t NonVirtual, int64_t Virtual); |
| 263 | void mangleNumber(const llvm::APSInt &I); |
| 264 | void mangleNumber(int64_t Number); |
| 265 | void mangleFloat(const llvm::APFloat &F); |
| 266 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 267 | void mangleName(const NamedDecl *ND); |
| 268 | void mangleType(QualType T); |
| 269 | void mangleNameOrStandardSubstitution(const NamedDecl *ND); |
| 270 | |
| 271 | private: |
| 272 | bool mangleSubstitution(const NamedDecl *ND); |
| 273 | bool mangleSubstitution(QualType T); |
| 274 | bool mangleSubstitution(TemplateName Template); |
| 275 | bool mangleSubstitution(uintptr_t Ptr); |
| 276 | |
| 277 | void mangleExistingSubstitution(QualType type); |
| 278 | void mangleExistingSubstitution(TemplateName name); |
| 279 | |
| 280 | bool mangleStandardSubstitution(const NamedDecl *ND); |
| 281 | |
| 282 | void addSubstitution(const NamedDecl *ND) { |
| 283 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
| 284 | |
| 285 | addSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 286 | } |
| 287 | void addSubstitution(QualType T); |
| 288 | void addSubstitution(TemplateName Template); |
| 289 | void addSubstitution(uintptr_t Ptr); |
| 290 | |
| 291 | void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier, |
| 292 | NamedDecl *firstQualifierLookup, |
| 293 | bool recursive = false); |
| 294 | void mangleUnresolvedName(NestedNameSpecifier *qualifier, |
| 295 | NamedDecl *firstQualifierLookup, |
| 296 | DeclarationName name, |
| 297 | unsigned KnownArity = UnknownArity); |
| 298 | |
| 299 | void mangleName(const TemplateDecl *TD, |
| 300 | const TemplateArgument *TemplateArgs, |
| 301 | unsigned NumTemplateArgs); |
| 302 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 303 | mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity); |
| 304 | } |
| 305 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name, |
| 306 | unsigned KnownArity); |
| 307 | void mangleUnscopedName(const NamedDecl *ND); |
| 308 | void mangleUnscopedTemplateName(const TemplateDecl *ND); |
| 309 | void mangleUnscopedTemplateName(TemplateName); |
| 310 | void mangleSourceName(const IdentifierInfo *II); |
| 311 | void mangleLocalName(const NamedDecl *ND); |
| 312 | void mangleLambda(const CXXRecordDecl *Lambda); |
| 313 | void mangleNestedName(const NamedDecl *ND, const DeclContext *DC, |
| 314 | bool NoFunction=false); |
| 315 | void mangleNestedName(const TemplateDecl *TD, |
| 316 | const TemplateArgument *TemplateArgs, |
| 317 | unsigned NumTemplateArgs); |
| 318 | void manglePrefix(NestedNameSpecifier *qualifier); |
| 319 | void manglePrefix(const DeclContext *DC, bool NoFunction=false); |
| 320 | void manglePrefix(QualType type); |
| 321 | void mangleTemplatePrefix(const TemplateDecl *ND); |
| 322 | void mangleTemplatePrefix(TemplateName Template); |
| 323 | void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); |
| 324 | void mangleQualifiers(Qualifiers Quals); |
| 325 | void mangleRefQualifier(RefQualifierKind RefQualifier); |
| 326 | |
| 327 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
| 328 | |
| 329 | // Declare manglers for every type class. |
| 330 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 331 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 332 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); |
| 333 | #include "clang/AST/TypeNodes.def" |
| 334 | |
| 335 | void mangleType(const TagType*); |
| 336 | void mangleType(TemplateName); |
| 337 | void mangleBareFunctionType(const FunctionType *T, |
| 338 | bool MangleReturnType); |
| 339 | void mangleNeonVectorType(const VectorType *T); |
| 340 | |
| 341 | void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value); |
| 342 | void mangleMemberExpr(const Expr *base, bool isArrow, |
| 343 | NestedNameSpecifier *qualifier, |
| 344 | NamedDecl *firstQualifierLookup, |
| 345 | DeclarationName name, |
| 346 | unsigned knownArity); |
| 347 | void mangleExpression(const Expr *E, unsigned Arity = UnknownArity); |
| 348 | void mangleCXXCtorType(CXXCtorType T); |
| 349 | void mangleCXXDtorType(CXXDtorType T); |
| 350 | |
| 351 | void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs); |
| 352 | void mangleTemplateArgs(const TemplateArgument *TemplateArgs, |
| 353 | unsigned NumTemplateArgs); |
| 354 | void mangleTemplateArgs(const TemplateArgumentList &AL); |
| 355 | void mangleTemplateArg(TemplateArgument A); |
| 356 | |
| 357 | void mangleTemplateParameter(unsigned Index); |
| 358 | |
| 359 | void mangleFunctionParam(const ParmVarDecl *parm); |
| 360 | }; |
| 361 | |
| 362 | } |
| 363 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 364 | bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 365 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 366 | if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs()) |
| 367 | return false; |
| 368 | |
| 369 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 370 | // over all other naming in the .o file. |
| 371 | if (D->hasAttr<AsmLabelAttr>()) |
| 372 | return true; |
| 373 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 374 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 375 | if (FD) { |
| 376 | LanguageLinkage L = FD->getLanguageLinkage(); |
| 377 | // Overloadable functions need mangling. |
| 378 | if (FD->hasAttr<OverloadableAttr>()) |
| 379 | return true; |
| 380 | |
Rafael Espindola | 83dece5 | 2013-02-14 15:38:59 +0000 | [diff] [blame] | 381 | // "main" is not mangled. |
| 382 | if (FD->isMain()) |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 383 | return false; |
| 384 | |
| 385 | // C++ functions and those whose names are not a simple identifier need |
| 386 | // mangling. |
| 387 | if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage) |
| 388 | return true; |
Rafael Espindola | 747836e | 2013-02-14 03:31:26 +0000 | [diff] [blame] | 389 | |
Rafael Espindola | 83dece5 | 2013-02-14 15:38:59 +0000 | [diff] [blame] | 390 | // C functions are not mangled. |
| 391 | if (L == CLanguageLinkage) |
| 392 | return false; |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 393 | } |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 394 | |
| 395 | // Otherwise, no mangling is done outside C++ mode. |
| 396 | if (!getASTContext().getLangOpts().CPlusPlus) |
| 397 | return false; |
| 398 | |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 399 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 400 | if (VD) { |
| 401 | // C variables are not mangled. |
| 402 | if (VD->isExternC()) |
| 403 | return false; |
| 404 | |
| 405 | // Variables at global scope with non-internal linkage are not mangled |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 406 | const DeclContext *DC = getEffectiveDeclContext(D); |
| 407 | // Check for extern variable declared locally. |
| 408 | if (DC->isFunctionOrMethod() && D->hasLinkage()) |
| 409 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 410 | DC = getEffectiveParentContext(DC); |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 411 | if (DC->isTranslationUnit() && D->getFormalLinkage() != InternalLinkage) |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 412 | return false; |
| 413 | } |
| 414 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | |
| 418 | void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) { |
| 419 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 420 | // over all other naming in the .o file. |
| 421 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 422 | // If we have an asm name, then we use it as the mangling. |
| 423 | |
| 424 | // Adding the prefix can cause problems when one file has a "foo" and |
| 425 | // another has a "\01foo". That is known to happen on ELF with the |
| 426 | // tricks normally used for producing aliases (PR9177). Fortunately the |
| 427 | // llvm mangler on ELF is a nop, so we can just avoid adding the \01 |
| 428 | // marker. We also avoid adding the marker if this is an alias for an |
| 429 | // LLVM intrinsic. |
| 430 | StringRef UserLabelPrefix = |
| 431 | getASTContext().getTargetInfo().getUserLabelPrefix(); |
| 432 | if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm.")) |
| 433 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 434 | |
| 435 | Out << ALA->getLabel(); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | // <mangled-name> ::= _Z <encoding> |
| 440 | // ::= <data name> |
| 441 | // ::= <special-name> |
| 442 | Out << Prefix; |
| 443 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 444 | mangleFunctionEncoding(FD); |
| 445 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 446 | mangleName(VD); |
| 447 | else |
| 448 | mangleName(cast<FieldDecl>(D)); |
| 449 | } |
| 450 | |
| 451 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 452 | // <encoding> ::= <function name> <bare-function-type> |
| 453 | mangleName(FD); |
| 454 | |
| 455 | // Don't mangle in the type if this isn't a decl we should typically mangle. |
| 456 | if (!Context.shouldMangleDeclName(FD)) |
| 457 | return; |
| 458 | |
| 459 | // Whether the mangling of a function type includes the return type depends on |
| 460 | // the context and the nature of the function. The rules for deciding whether |
| 461 | // the return type is included are: |
| 462 | // |
| 463 | // 1. Template functions (names or types) have return types encoded, with |
| 464 | // the exceptions listed below. |
| 465 | // 2. Function types not appearing as part of a function name mangling, |
| 466 | // e.g. parameters, pointer types, etc., have return type encoded, with the |
| 467 | // exceptions listed below. |
| 468 | // 3. Non-template function names do not have return types encoded. |
| 469 | // |
| 470 | // The exceptions mentioned in (1) and (2) above, for which the return type is |
| 471 | // never included, are |
| 472 | // 1. Constructors. |
| 473 | // 2. Destructors. |
| 474 | // 3. Conversion operator functions, e.g. operator int. |
| 475 | bool MangleReturnType = false; |
| 476 | if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { |
| 477 | if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || |
| 478 | isa<CXXConversionDecl>(FD))) |
| 479 | MangleReturnType = true; |
| 480 | |
| 481 | // Mangle the type of the primary template. |
| 482 | FD = PrimaryTemplate->getTemplatedDecl(); |
| 483 | } |
| 484 | |
| 485 | mangleBareFunctionType(FD->getType()->getAs<FunctionType>(), |
| 486 | MangleReturnType); |
| 487 | } |
| 488 | |
| 489 | static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) { |
| 490 | while (isa<LinkageSpecDecl>(DC)) { |
| 491 | DC = getEffectiveParentContext(DC); |
| 492 | } |
| 493 | |
| 494 | return DC; |
| 495 | } |
| 496 | |
| 497 | /// isStd - Return whether a given namespace is the 'std' namespace. |
| 498 | static bool isStd(const NamespaceDecl *NS) { |
| 499 | if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS)) |
| 500 | ->isTranslationUnit()) |
| 501 | return false; |
| 502 | |
| 503 | const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); |
| 504 | return II && II->isStr("std"); |
| 505 | } |
| 506 | |
| 507 | // isStdNamespace - Return whether a given decl context is a toplevel 'std' |
| 508 | // namespace. |
| 509 | static bool isStdNamespace(const DeclContext *DC) { |
| 510 | if (!DC->isNamespace()) |
| 511 | return false; |
| 512 | |
| 513 | return isStd(cast<NamespaceDecl>(DC)); |
| 514 | } |
| 515 | |
| 516 | static const TemplateDecl * |
| 517 | isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { |
| 518 | // Check if we have a function template. |
| 519 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ |
| 520 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
| 521 | TemplateArgs = FD->getTemplateSpecializationArgs(); |
| 522 | return TD; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // Check if we have a class template. |
| 527 | if (const ClassTemplateSpecializationDecl *Spec = |
| 528 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 529 | TemplateArgs = &Spec->getTemplateArgs(); |
| 530 | return Spec->getSpecializedTemplate(); |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static bool isLambda(const NamedDecl *ND) { |
| 537 | const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND); |
| 538 | if (!Record) |
| 539 | return false; |
| 540 | |
| 541 | return Record->isLambda(); |
| 542 | } |
| 543 | |
| 544 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 545 | // <name> ::= <nested-name> |
| 546 | // ::= <unscoped-name> |
| 547 | // ::= <unscoped-template-name> <template-args> |
| 548 | // ::= <local-name> |
| 549 | // |
| 550 | const DeclContext *DC = getEffectiveDeclContext(ND); |
| 551 | |
| 552 | // If this is an extern variable declared locally, the relevant DeclContext |
| 553 | // is that of the containing namespace, or the translation unit. |
| 554 | // FIXME: This is a hack; extern variables declared locally should have |
| 555 | // a proper semantic declaration context! |
| 556 | if (isa<FunctionDecl>(DC) && ND->hasLinkage() && !isLambda(ND)) |
| 557 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 558 | DC = getEffectiveParentContext(DC); |
| 559 | else if (GetLocalClassDecl(ND)) { |
| 560 | mangleLocalName(ND); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | DC = IgnoreLinkageSpecDecls(DC); |
| 565 | |
| 566 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
| 567 | // Check if we have a template. |
| 568 | const TemplateArgumentList *TemplateArgs = 0; |
| 569 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
| 570 | mangleUnscopedTemplateName(TD); |
| 571 | mangleTemplateArgs(*TemplateArgs); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | mangleUnscopedName(ND); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) { |
| 580 | mangleLocalName(ND); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | mangleNestedName(ND, DC); |
| 585 | } |
| 586 | void CXXNameMangler::mangleName(const TemplateDecl *TD, |
| 587 | const TemplateArgument *TemplateArgs, |
| 588 | unsigned NumTemplateArgs) { |
| 589 | const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD)); |
| 590 | |
| 591 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
| 592 | mangleUnscopedTemplateName(TD); |
| 593 | mangleTemplateArgs(TemplateArgs, NumTemplateArgs); |
| 594 | } else { |
| 595 | mangleNestedName(TD, TemplateArgs, NumTemplateArgs); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) { |
| 600 | // <unscoped-name> ::= <unqualified-name> |
| 601 | // ::= St <unqualified-name> # ::std:: |
| 602 | |
| 603 | if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND)))) |
| 604 | Out << "St"; |
| 605 | |
| 606 | mangleUnqualifiedName(ND); |
| 607 | } |
| 608 | |
| 609 | void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) { |
| 610 | // <unscoped-template-name> ::= <unscoped-name> |
| 611 | // ::= <substitution> |
| 612 | if (mangleSubstitution(ND)) |
| 613 | return; |
| 614 | |
| 615 | // <template-template-param> ::= <template-param> |
| 616 | if (const TemplateTemplateParmDecl *TTP |
| 617 | = dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 618 | mangleTemplateParameter(TTP->getIndex()); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | mangleUnscopedName(ND->getTemplatedDecl()); |
| 623 | addSubstitution(ND); |
| 624 | } |
| 625 | |
| 626 | void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) { |
| 627 | // <unscoped-template-name> ::= <unscoped-name> |
| 628 | // ::= <substitution> |
| 629 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 630 | return mangleUnscopedTemplateName(TD); |
| 631 | |
| 632 | if (mangleSubstitution(Template)) |
| 633 | return; |
| 634 | |
| 635 | DependentTemplateName *Dependent = Template.getAsDependentTemplateName(); |
| 636 | assert(Dependent && "Not a dependent template name?"); |
| 637 | if (const IdentifierInfo *Id = Dependent->getIdentifier()) |
| 638 | mangleSourceName(Id); |
| 639 | else |
| 640 | mangleOperatorName(Dependent->getOperator(), UnknownArity); |
| 641 | |
| 642 | addSubstitution(Template); |
| 643 | } |
| 644 | |
| 645 | void CXXNameMangler::mangleFloat(const llvm::APFloat &f) { |
| 646 | // ABI: |
| 647 | // Floating-point literals are encoded using a fixed-length |
| 648 | // lowercase hexadecimal string corresponding to the internal |
| 649 | // representation (IEEE on Itanium), high-order bytes first, |
| 650 | // without leading zeroes. For example: "Lf bf800000 E" is -1.0f |
| 651 | // on Itanium. |
| 652 | // The 'without leading zeroes' thing seems to be an editorial |
| 653 | // mistake; see the discussion on cxx-abi-dev beginning on |
| 654 | // 2012-01-16. |
| 655 | |
| 656 | // Our requirements here are just barely weird enough to justify |
| 657 | // using a custom algorithm instead of post-processing APInt::toString(). |
| 658 | |
| 659 | llvm::APInt valueBits = f.bitcastToAPInt(); |
| 660 | unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4; |
| 661 | assert(numCharacters != 0); |
| 662 | |
| 663 | // Allocate a buffer of the right number of characters. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 664 | SmallVector<char, 20> buffer; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 665 | buffer.set_size(numCharacters); |
| 666 | |
| 667 | // Fill the buffer left-to-right. |
| 668 | for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) { |
| 669 | // The bit-index of the next hex digit. |
| 670 | unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1); |
| 671 | |
| 672 | // Project out 4 bits starting at 'digitIndex'. |
| 673 | llvm::integerPart hexDigit |
| 674 | = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth]; |
| 675 | hexDigit >>= (digitBitIndex % llvm::integerPartWidth); |
| 676 | hexDigit &= 0xF; |
| 677 | |
| 678 | // Map that over to a lowercase hex digit. |
| 679 | static const char charForHex[16] = { |
| 680 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 681 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 682 | }; |
| 683 | buffer[stringIndex] = charForHex[hexDigit]; |
| 684 | } |
| 685 | |
| 686 | Out.write(buffer.data(), numCharacters); |
| 687 | } |
| 688 | |
| 689 | void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) { |
| 690 | if (Value.isSigned() && Value.isNegative()) { |
| 691 | Out << 'n'; |
| 692 | Value.abs().print(Out, /*signed*/ false); |
| 693 | } else { |
| 694 | Value.print(Out, /*signed*/ false); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | void CXXNameMangler::mangleNumber(int64_t Number) { |
| 699 | // <number> ::= [n] <non-negative decimal integer> |
| 700 | if (Number < 0) { |
| 701 | Out << 'n'; |
| 702 | Number = -Number; |
| 703 | } |
| 704 | |
| 705 | Out << Number; |
| 706 | } |
| 707 | |
| 708 | void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) { |
| 709 | // <call-offset> ::= h <nv-offset> _ |
| 710 | // ::= v <v-offset> _ |
| 711 | // <nv-offset> ::= <offset number> # non-virtual base override |
| 712 | // <v-offset> ::= <offset number> _ <virtual offset number> |
| 713 | // # virtual base override, with vcall offset |
| 714 | if (!Virtual) { |
| 715 | Out << 'h'; |
| 716 | mangleNumber(NonVirtual); |
| 717 | Out << '_'; |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | Out << 'v'; |
| 722 | mangleNumber(NonVirtual); |
| 723 | Out << '_'; |
| 724 | mangleNumber(Virtual); |
| 725 | Out << '_'; |
| 726 | } |
| 727 | |
| 728 | void CXXNameMangler::manglePrefix(QualType type) { |
| 729 | if (const TemplateSpecializationType *TST = |
| 730 | type->getAs<TemplateSpecializationType>()) { |
| 731 | if (!mangleSubstitution(QualType(TST, 0))) { |
| 732 | mangleTemplatePrefix(TST->getTemplateName()); |
| 733 | |
| 734 | // FIXME: GCC does not appear to mangle the template arguments when |
| 735 | // the template in question is a dependent template name. Should we |
| 736 | // emulate that badness? |
| 737 | mangleTemplateArgs(TST->getArgs(), TST->getNumArgs()); |
| 738 | addSubstitution(QualType(TST, 0)); |
| 739 | } |
| 740 | } else if (const DependentTemplateSpecializationType *DTST |
| 741 | = type->getAs<DependentTemplateSpecializationType>()) { |
| 742 | TemplateName Template |
| 743 | = getASTContext().getDependentTemplateName(DTST->getQualifier(), |
| 744 | DTST->getIdentifier()); |
| 745 | mangleTemplatePrefix(Template); |
| 746 | |
| 747 | // FIXME: GCC does not appear to mangle the template arguments when |
| 748 | // the template in question is a dependent template name. Should we |
| 749 | // emulate that badness? |
| 750 | mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs()); |
| 751 | } else { |
| 752 | // We use the QualType mangle type variant here because it handles |
| 753 | // substitutions. |
| 754 | mangleType(type); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /// Mangle everything prior to the base-unresolved-name in an unresolved-name. |
| 759 | /// |
| 760 | /// \param firstQualifierLookup - the entity found by unqualified lookup |
| 761 | /// for the first name in the qualifier, if this is for a member expression |
| 762 | /// \param recursive - true if this is being called recursively, |
| 763 | /// i.e. if there is more prefix "to the right". |
| 764 | void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier, |
| 765 | NamedDecl *firstQualifierLookup, |
| 766 | bool recursive) { |
| 767 | |
| 768 | // x, ::x |
| 769 | // <unresolved-name> ::= [gs] <base-unresolved-name> |
| 770 | |
| 771 | // T::x / decltype(p)::x |
| 772 | // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name> |
| 773 | |
| 774 | // T::N::x /decltype(p)::N::x |
| 775 | // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E |
| 776 | // <base-unresolved-name> |
| 777 | |
| 778 | // A::x, N::y, A<T>::z; "gs" means leading "::" |
| 779 | // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E |
| 780 | // <base-unresolved-name> |
| 781 | |
| 782 | switch (qualifier->getKind()) { |
| 783 | case NestedNameSpecifier::Global: |
| 784 | Out << "gs"; |
| 785 | |
| 786 | // We want an 'sr' unless this is the entire NNS. |
| 787 | if (recursive) |
| 788 | Out << "sr"; |
| 789 | |
| 790 | // We never want an 'E' here. |
| 791 | return; |
| 792 | |
| 793 | case NestedNameSpecifier::Namespace: |
| 794 | if (qualifier->getPrefix()) |
| 795 | mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup, |
| 796 | /*recursive*/ true); |
| 797 | else |
| 798 | Out << "sr"; |
| 799 | mangleSourceName(qualifier->getAsNamespace()->getIdentifier()); |
| 800 | break; |
| 801 | case NestedNameSpecifier::NamespaceAlias: |
| 802 | if (qualifier->getPrefix()) |
| 803 | mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup, |
| 804 | /*recursive*/ true); |
| 805 | else |
| 806 | Out << "sr"; |
| 807 | mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier()); |
| 808 | break; |
| 809 | |
| 810 | case NestedNameSpecifier::TypeSpec: |
| 811 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 812 | const Type *type = qualifier->getAsType(); |
| 813 | |
| 814 | // We only want to use an unresolved-type encoding if this is one of: |
| 815 | // - a decltype |
| 816 | // - a template type parameter |
| 817 | // - a template template parameter with arguments |
| 818 | // In all of these cases, we should have no prefix. |
| 819 | if (qualifier->getPrefix()) { |
| 820 | mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup, |
| 821 | /*recursive*/ true); |
| 822 | } else { |
| 823 | // Otherwise, all the cases want this. |
| 824 | Out << "sr"; |
| 825 | } |
| 826 | |
| 827 | // Only certain other types are valid as prefixes; enumerate them. |
| 828 | switch (type->getTypeClass()) { |
| 829 | case Type::Builtin: |
| 830 | case Type::Complex: |
Reid Kleckner | 12df246 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 831 | case Type::Decayed: |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 832 | case Type::Pointer: |
| 833 | case Type::BlockPointer: |
| 834 | case Type::LValueReference: |
| 835 | case Type::RValueReference: |
| 836 | case Type::MemberPointer: |
| 837 | case Type::ConstantArray: |
| 838 | case Type::IncompleteArray: |
| 839 | case Type::VariableArray: |
| 840 | case Type::DependentSizedArray: |
| 841 | case Type::DependentSizedExtVector: |
| 842 | case Type::Vector: |
| 843 | case Type::ExtVector: |
| 844 | case Type::FunctionProto: |
| 845 | case Type::FunctionNoProto: |
| 846 | case Type::Enum: |
| 847 | case Type::Paren: |
| 848 | case Type::Elaborated: |
| 849 | case Type::Attributed: |
| 850 | case Type::Auto: |
| 851 | case Type::PackExpansion: |
| 852 | case Type::ObjCObject: |
| 853 | case Type::ObjCInterface: |
| 854 | case Type::ObjCObjectPointer: |
| 855 | case Type::Atomic: |
| 856 | llvm_unreachable("type is illegal as a nested name specifier"); |
| 857 | |
| 858 | case Type::SubstTemplateTypeParmPack: |
| 859 | // FIXME: not clear how to mangle this! |
| 860 | // template <class T...> class A { |
| 861 | // template <class U...> void foo(decltype(T::foo(U())) x...); |
| 862 | // }; |
| 863 | Out << "_SUBSTPACK_"; |
| 864 | break; |
| 865 | |
| 866 | // <unresolved-type> ::= <template-param> |
| 867 | // ::= <decltype> |
| 868 | // ::= <template-template-param> <template-args> |
| 869 | // (this last is not official yet) |
| 870 | case Type::TypeOfExpr: |
| 871 | case Type::TypeOf: |
| 872 | case Type::Decltype: |
| 873 | case Type::TemplateTypeParm: |
| 874 | case Type::UnaryTransform: |
| 875 | case Type::SubstTemplateTypeParm: |
| 876 | unresolvedType: |
| 877 | assert(!qualifier->getPrefix()); |
| 878 | |
| 879 | // We only get here recursively if we're followed by identifiers. |
| 880 | if (recursive) Out << 'N'; |
| 881 | |
| 882 | // This seems to do everything we want. It's not really |
| 883 | // sanctioned for a substituted template parameter, though. |
| 884 | mangleType(QualType(type, 0)); |
| 885 | |
| 886 | // We never want to print 'E' directly after an unresolved-type, |
| 887 | // so we return directly. |
| 888 | return; |
| 889 | |
| 890 | case Type::Typedef: |
| 891 | mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier()); |
| 892 | break; |
| 893 | |
| 894 | case Type::UnresolvedUsing: |
| 895 | mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl() |
| 896 | ->getIdentifier()); |
| 897 | break; |
| 898 | |
| 899 | case Type::Record: |
| 900 | mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier()); |
| 901 | break; |
| 902 | |
| 903 | case Type::TemplateSpecialization: { |
| 904 | const TemplateSpecializationType *tst |
| 905 | = cast<TemplateSpecializationType>(type); |
| 906 | TemplateName name = tst->getTemplateName(); |
| 907 | switch (name.getKind()) { |
| 908 | case TemplateName::Template: |
| 909 | case TemplateName::QualifiedTemplate: { |
| 910 | TemplateDecl *temp = name.getAsTemplateDecl(); |
| 911 | |
| 912 | // If the base is a template template parameter, this is an |
| 913 | // unresolved type. |
| 914 | assert(temp && "no template for template specialization type"); |
| 915 | if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType; |
| 916 | |
| 917 | mangleSourceName(temp->getIdentifier()); |
| 918 | break; |
| 919 | } |
| 920 | |
| 921 | case TemplateName::OverloadedTemplate: |
| 922 | case TemplateName::DependentTemplate: |
| 923 | llvm_unreachable("invalid base for a template specialization type"); |
| 924 | |
| 925 | case TemplateName::SubstTemplateTemplateParm: { |
| 926 | SubstTemplateTemplateParmStorage *subst |
| 927 | = name.getAsSubstTemplateTemplateParm(); |
| 928 | mangleExistingSubstitution(subst->getReplacement()); |
| 929 | break; |
| 930 | } |
| 931 | |
| 932 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 933 | // FIXME: not clear how to mangle this! |
| 934 | // template <template <class U> class T...> class A { |
| 935 | // template <class U...> void foo(decltype(T<U>::foo) x...); |
| 936 | // }; |
| 937 | Out << "_SUBSTPACK_"; |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | mangleTemplateArgs(tst->getArgs(), tst->getNumArgs()); |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | case Type::InjectedClassName: |
| 947 | mangleSourceName(cast<InjectedClassNameType>(type)->getDecl() |
| 948 | ->getIdentifier()); |
| 949 | break; |
| 950 | |
| 951 | case Type::DependentName: |
| 952 | mangleSourceName(cast<DependentNameType>(type)->getIdentifier()); |
| 953 | break; |
| 954 | |
| 955 | case Type::DependentTemplateSpecialization: { |
| 956 | const DependentTemplateSpecializationType *tst |
| 957 | = cast<DependentTemplateSpecializationType>(type); |
| 958 | mangleSourceName(tst->getIdentifier()); |
| 959 | mangleTemplateArgs(tst->getArgs(), tst->getNumArgs()); |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | case NestedNameSpecifier::Identifier: |
| 967 | // Member expressions can have these without prefixes. |
| 968 | if (qualifier->getPrefix()) { |
| 969 | mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup, |
| 970 | /*recursive*/ true); |
| 971 | } else if (firstQualifierLookup) { |
| 972 | |
| 973 | // Try to make a proper qualifier out of the lookup result, and |
| 974 | // then just recurse on that. |
| 975 | NestedNameSpecifier *newQualifier; |
| 976 | if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) { |
| 977 | QualType type = getASTContext().getTypeDeclType(typeDecl); |
| 978 | |
| 979 | // Pretend we had a different nested name specifier. |
| 980 | newQualifier = NestedNameSpecifier::Create(getASTContext(), |
| 981 | /*prefix*/ 0, |
| 982 | /*template*/ false, |
| 983 | type.getTypePtr()); |
| 984 | } else if (NamespaceDecl *nspace = |
| 985 | dyn_cast<NamespaceDecl>(firstQualifierLookup)) { |
| 986 | newQualifier = NestedNameSpecifier::Create(getASTContext(), |
| 987 | /*prefix*/ 0, |
| 988 | nspace); |
| 989 | } else if (NamespaceAliasDecl *alias = |
| 990 | dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) { |
| 991 | newQualifier = NestedNameSpecifier::Create(getASTContext(), |
| 992 | /*prefix*/ 0, |
| 993 | alias); |
| 994 | } else { |
| 995 | // No sensible mangling to do here. |
| 996 | newQualifier = 0; |
| 997 | } |
| 998 | |
| 999 | if (newQualifier) |
| 1000 | return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive); |
| 1001 | |
| 1002 | } else { |
| 1003 | Out << "sr"; |
| 1004 | } |
| 1005 | |
| 1006 | mangleSourceName(qualifier->getAsIdentifier()); |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | // If this was the innermost part of the NNS, and we fell out to |
| 1011 | // here, append an 'E'. |
| 1012 | if (!recursive) |
| 1013 | Out << 'E'; |
| 1014 | } |
| 1015 | |
| 1016 | /// Mangle an unresolved-name, which is generally used for names which |
| 1017 | /// weren't resolved to specific entities. |
| 1018 | void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier, |
| 1019 | NamedDecl *firstQualifierLookup, |
| 1020 | DeclarationName name, |
| 1021 | unsigned knownArity) { |
| 1022 | if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup); |
| 1023 | mangleUnqualifiedName(0, name, knownArity); |
| 1024 | } |
| 1025 | |
| 1026 | static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) { |
| 1027 | assert(RD->isAnonymousStructOrUnion() && |
| 1028 | "Expected anonymous struct or union!"); |
| 1029 | |
| 1030 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 1031 | I != E; ++I) { |
| 1032 | if (I->getIdentifier()) |
| 1033 | return *I; |
| 1034 | |
| 1035 | if (const RecordType *RT = I->getType()->getAs<RecordType>()) |
| 1036 | if (const FieldDecl *NamedDataMember = |
| 1037 | FindFirstNamedDataMember(RT->getDecl())) |
| 1038 | return NamedDataMember; |
| 1039 | } |
| 1040 | |
| 1041 | // We didn't find a named data member. |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 1046 | DeclarationName Name, |
| 1047 | unsigned KnownArity) { |
| 1048 | // <unqualified-name> ::= <operator-name> |
| 1049 | // ::= <ctor-dtor-name> |
| 1050 | // ::= <source-name> |
| 1051 | switch (Name.getNameKind()) { |
| 1052 | case DeclarationName::Identifier: { |
| 1053 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
| 1054 | // We must avoid conflicts between internally- and externally- |
| 1055 | // linked variable and function declaration names in the same TU: |
| 1056 | // void test() { extern void foo(); } |
| 1057 | // static void foo(); |
| 1058 | // This naming convention is the same as that followed by GCC, |
| 1059 | // though it shouldn't actually matter. |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1060 | if (ND && ND->getFormalLinkage() == InternalLinkage && |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1061 | getEffectiveDeclContext(ND)->isFileContext()) |
| 1062 | Out << 'L'; |
| 1063 | |
| 1064 | mangleSourceName(II); |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | // Otherwise, an anonymous entity. We must have a declaration. |
| 1069 | assert(ND && "mangling empty name without declaration"); |
| 1070 | |
| 1071 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 1072 | if (NS->isAnonymousNamespace()) { |
| 1073 | // This is how gcc mangles these names. |
| 1074 | Out << "12_GLOBAL__N_1"; |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 1080 | // We must have an anonymous union or struct declaration. |
| 1081 | const RecordDecl *RD = |
| 1082 | cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl()); |
| 1083 | |
| 1084 | // Itanium C++ ABI 5.1.2: |
| 1085 | // |
| 1086 | // For the purposes of mangling, the name of an anonymous union is |
| 1087 | // considered to be the name of the first named data member found by a |
| 1088 | // pre-order, depth-first, declaration-order walk of the data members of |
| 1089 | // the anonymous union. If there is no such data member (i.e., if all of |
| 1090 | // the data members in the union are unnamed), then there is no way for |
| 1091 | // a program to refer to the anonymous union, and there is therefore no |
| 1092 | // need to mangle its name. |
| 1093 | const FieldDecl *FD = FindFirstNamedDataMember(RD); |
| 1094 | |
| 1095 | // It's actually possible for various reasons for us to get here |
| 1096 | // with an empty anonymous struct / union. Fortunately, it |
| 1097 | // doesn't really matter what name we generate. |
| 1098 | if (!FD) break; |
| 1099 | assert(FD->getIdentifier() && "Data member name isn't an identifier!"); |
| 1100 | |
| 1101 | mangleSourceName(FD->getIdentifier()); |
| 1102 | break; |
| 1103 | } |
John McCall | 0baaabb | 2013-04-10 06:08:21 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Class extensions have no name as a category, and it's possible |
| 1106 | // for them to be the semantic parent of certain declarations |
| 1107 | // (primarily, tag decls defined within declarations). Such |
| 1108 | // declarations will always have internal linkage, so the name |
| 1109 | // doesn't really matter, but we shouldn't crash on them. For |
| 1110 | // safety, just handle all ObjC containers here. |
| 1111 | if (isa<ObjCContainerDecl>(ND)) |
| 1112 | break; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1113 | |
| 1114 | // We must have an anonymous struct. |
| 1115 | const TagDecl *TD = cast<TagDecl>(ND); |
| 1116 | if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) { |
| 1117 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 1118 | "Typedef should not be in another decl context!"); |
| 1119 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 1120 | "Typedef was not named!"); |
| 1121 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | // <unnamed-type-name> ::= <closure-type-name> |
| 1126 | // |
| 1127 | // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ |
| 1128 | // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'. |
| 1129 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) { |
| 1130 | if (Record->isLambda() && Record->getLambdaManglingNumber()) { |
| 1131 | mangleLambda(Record); |
| 1132 | break; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | int UnnamedMangle = Context.getASTContext().getUnnamedTagManglingNumber(TD); |
| 1137 | if (UnnamedMangle != -1) { |
| 1138 | Out << "Ut"; |
| 1139 | if (UnnamedMangle != 0) |
| 1140 | Out << llvm::utostr(UnnamedMangle - 1); |
| 1141 | Out << '_'; |
| 1142 | break; |
| 1143 | } |
| 1144 | |
| 1145 | // Get a unique id for the anonymous struct. |
| 1146 | uint64_t AnonStructId = Context.getAnonymousStructId(TD); |
| 1147 | |
| 1148 | // Mangle it as a source name in the form |
| 1149 | // [n] $_<id> |
| 1150 | // where n is the length of the string. |
| 1151 | SmallString<8> Str; |
| 1152 | Str += "$_"; |
| 1153 | Str += llvm::utostr(AnonStructId); |
| 1154 | |
| 1155 | Out << Str.size(); |
| 1156 | Out << Str.str(); |
| 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | case DeclarationName::ObjCZeroArgSelector: |
| 1161 | case DeclarationName::ObjCOneArgSelector: |
| 1162 | case DeclarationName::ObjCMultiArgSelector: |
| 1163 | llvm_unreachable("Can't mangle Objective-C selector names here!"); |
| 1164 | |
| 1165 | case DeclarationName::CXXConstructorName: |
| 1166 | if (ND == Structor) |
| 1167 | // If the named decl is the C++ constructor we're mangling, use the type |
| 1168 | // we were given. |
| 1169 | mangleCXXCtorType(static_cast<CXXCtorType>(StructorType)); |
| 1170 | else |
| 1171 | // Otherwise, use the complete constructor name. This is relevant if a |
| 1172 | // class with a constructor is declared within a constructor. |
| 1173 | mangleCXXCtorType(Ctor_Complete); |
| 1174 | break; |
| 1175 | |
| 1176 | case DeclarationName::CXXDestructorName: |
| 1177 | if (ND == Structor) |
| 1178 | // If the named decl is the C++ destructor we're mangling, use the type we |
| 1179 | // were given. |
| 1180 | mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); |
| 1181 | else |
| 1182 | // Otherwise, use the complete destructor name. This is relevant if a |
| 1183 | // class with a destructor is declared within a destructor. |
| 1184 | mangleCXXDtorType(Dtor_Complete); |
| 1185 | break; |
| 1186 | |
| 1187 | case DeclarationName::CXXConversionFunctionName: |
| 1188 | // <operator-name> ::= cv <type> # (cast) |
| 1189 | Out << "cv"; |
| 1190 | mangleType(Name.getCXXNameType()); |
| 1191 | break; |
| 1192 | |
| 1193 | case DeclarationName::CXXOperatorName: { |
| 1194 | unsigned Arity; |
| 1195 | if (ND) { |
| 1196 | Arity = cast<FunctionDecl>(ND)->getNumParams(); |
| 1197 | |
| 1198 | // If we have a C++ member function, we need to include the 'this' pointer. |
| 1199 | // FIXME: This does not make sense for operators that are static, but their |
| 1200 | // names stay the same regardless of the arity (operator new for instance). |
| 1201 | if (isa<CXXMethodDecl>(ND)) |
| 1202 | Arity++; |
| 1203 | } else |
| 1204 | Arity = KnownArity; |
| 1205 | |
| 1206 | mangleOperatorName(Name.getCXXOverloadedOperator(), Arity); |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | case DeclarationName::CXXLiteralOperatorName: |
| 1211 | // FIXME: This mangling is not yet official. |
| 1212 | Out << "li"; |
| 1213 | mangleSourceName(Name.getCXXLiteralIdentifier()); |
| 1214 | break; |
| 1215 | |
| 1216 | case DeclarationName::CXXUsingDirective: |
| 1217 | llvm_unreachable("Can't mangle a using directive name!"); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 1222 | // <source-name> ::= <positive length number> <identifier> |
| 1223 | // <number> ::= [n] <non-negative decimal integer> |
| 1224 | // <identifier> ::= <unqualified source code identifier> |
| 1225 | Out << II->getLength() << II->getName(); |
| 1226 | } |
| 1227 | |
| 1228 | void CXXNameMangler::mangleNestedName(const NamedDecl *ND, |
| 1229 | const DeclContext *DC, |
| 1230 | bool NoFunction) { |
| 1231 | // <nested-name> |
| 1232 | // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E |
| 1233 | // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> |
| 1234 | // <template-args> E |
| 1235 | |
| 1236 | Out << 'N'; |
| 1237 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) { |
| 1238 | mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
| 1239 | mangleRefQualifier(Method->getRefQualifier()); |
| 1240 | } |
| 1241 | |
| 1242 | // Check if we have a template. |
| 1243 | const TemplateArgumentList *TemplateArgs = 0; |
| 1244 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
| 1245 | mangleTemplatePrefix(TD); |
| 1246 | mangleTemplateArgs(*TemplateArgs); |
| 1247 | } |
| 1248 | else { |
| 1249 | manglePrefix(DC, NoFunction); |
| 1250 | mangleUnqualifiedName(ND); |
| 1251 | } |
| 1252 | |
| 1253 | Out << 'E'; |
| 1254 | } |
| 1255 | void CXXNameMangler::mangleNestedName(const TemplateDecl *TD, |
| 1256 | const TemplateArgument *TemplateArgs, |
| 1257 | unsigned NumTemplateArgs) { |
| 1258 | // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 1259 | |
| 1260 | Out << 'N'; |
| 1261 | |
| 1262 | mangleTemplatePrefix(TD); |
| 1263 | mangleTemplateArgs(TemplateArgs, NumTemplateArgs); |
| 1264 | |
| 1265 | Out << 'E'; |
| 1266 | } |
| 1267 | |
| 1268 | void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { |
| 1269 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 1270 | // := Z <function encoding> E s [<discriminator>] |
| 1271 | // <local-name> := Z <function encoding> E d [ <parameter number> ] |
| 1272 | // _ <entity name> |
| 1273 | // <discriminator> := _ <non-negative number> |
| 1274 | const DeclContext *DC = getEffectiveDeclContext(ND); |
| 1275 | if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) { |
| 1276 | // Don't add objc method name mangling to locally declared function |
| 1277 | mangleUnqualifiedName(ND); |
| 1278 | return; |
| 1279 | } |
| 1280 | |
| 1281 | Out << 'Z'; |
| 1282 | |
| 1283 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) { |
| 1284 | mangleObjCMethodName(MD); |
| 1285 | } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) { |
| 1286 | mangleFunctionEncoding(cast<FunctionDecl>(getEffectiveDeclContext(RD))); |
| 1287 | Out << 'E'; |
| 1288 | |
| 1289 | // The parameter number is omitted for the last parameter, 0 for the |
| 1290 | // second-to-last parameter, 1 for the third-to-last parameter, etc. The |
| 1291 | // <entity name> will of course contain a <closure-type-name>: Its |
| 1292 | // numbering will be local to the particular argument in which it appears |
| 1293 | // -- other default arguments do not affect its encoding. |
| 1294 | bool SkipDiscriminator = false; |
| 1295 | if (RD->isLambda()) { |
| 1296 | if (const ParmVarDecl *Parm |
| 1297 | = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) { |
| 1298 | if (const FunctionDecl *Func |
| 1299 | = dyn_cast<FunctionDecl>(Parm->getDeclContext())) { |
| 1300 | Out << 'd'; |
| 1301 | unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex(); |
| 1302 | if (Num > 1) |
| 1303 | mangleNumber(Num - 2); |
| 1304 | Out << '_'; |
| 1305 | SkipDiscriminator = true; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | // Mangle the name relative to the closest enclosing function. |
| 1311 | if (ND == RD) // equality ok because RD derived from ND above |
| 1312 | mangleUnqualifiedName(ND); |
| 1313 | else |
| 1314 | mangleNestedName(ND, DC, true /*NoFunction*/); |
| 1315 | |
| 1316 | if (!SkipDiscriminator) { |
| 1317 | unsigned disc; |
| 1318 | if (Context.getNextDiscriminator(RD, disc)) { |
| 1319 | if (disc < 10) |
| 1320 | Out << '_' << disc; |
| 1321 | else |
| 1322 | Out << "__" << disc << '_'; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | return; |
| 1327 | } |
| 1328 | else |
| 1329 | mangleFunctionEncoding(cast<FunctionDecl>(DC)); |
| 1330 | |
| 1331 | Out << 'E'; |
| 1332 | mangleUnqualifiedName(ND); |
| 1333 | } |
| 1334 | |
| 1335 | void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) { |
| 1336 | // If the context of a closure type is an initializer for a class member |
| 1337 | // (static or nonstatic), it is encoded in a qualified name with a final |
| 1338 | // <prefix> of the form: |
| 1339 | // |
| 1340 | // <data-member-prefix> := <member source-name> M |
| 1341 | // |
| 1342 | // Technically, the data-member-prefix is part of the <prefix>. However, |
| 1343 | // since a closure type will always be mangled with a prefix, it's easier |
| 1344 | // to emit that last part of the prefix here. |
| 1345 | if (Decl *Context = Lambda->getLambdaContextDecl()) { |
| 1346 | if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) && |
| 1347 | Context->getDeclContext()->isRecord()) { |
| 1348 | if (const IdentifierInfo *Name |
| 1349 | = cast<NamedDecl>(Context)->getIdentifier()) { |
| 1350 | mangleSourceName(Name); |
| 1351 | Out << 'M'; |
| 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | Out << "Ul"; |
| 1357 | const FunctionProtoType *Proto = Lambda->getLambdaTypeInfo()->getType()-> |
| 1358 | getAs<FunctionProtoType>(); |
| 1359 | mangleBareFunctionType(Proto, /*MangleReturnType=*/false); |
| 1360 | Out << "E"; |
| 1361 | |
| 1362 | // The number is omitted for the first closure type with a given |
| 1363 | // <lambda-sig> in a given context; it is n-2 for the nth closure type |
| 1364 | // (in lexical order) with that same <lambda-sig> and context. |
| 1365 | // |
| 1366 | // The AST keeps track of the number for us. |
| 1367 | unsigned Number = Lambda->getLambdaManglingNumber(); |
| 1368 | assert(Number > 0 && "Lambda should be mangled as an unnamed class"); |
| 1369 | if (Number > 1) |
| 1370 | mangleNumber(Number - 2); |
| 1371 | Out << '_'; |
| 1372 | } |
| 1373 | |
| 1374 | void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) { |
| 1375 | switch (qualifier->getKind()) { |
| 1376 | case NestedNameSpecifier::Global: |
| 1377 | // nothing |
| 1378 | return; |
| 1379 | |
| 1380 | case NestedNameSpecifier::Namespace: |
| 1381 | mangleName(qualifier->getAsNamespace()); |
| 1382 | return; |
| 1383 | |
| 1384 | case NestedNameSpecifier::NamespaceAlias: |
| 1385 | mangleName(qualifier->getAsNamespaceAlias()->getNamespace()); |
| 1386 | return; |
| 1387 | |
| 1388 | case NestedNameSpecifier::TypeSpec: |
| 1389 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1390 | manglePrefix(QualType(qualifier->getAsType(), 0)); |
| 1391 | return; |
| 1392 | |
| 1393 | case NestedNameSpecifier::Identifier: |
| 1394 | // Member expressions can have these without prefixes, but that |
| 1395 | // should end up in mangleUnresolvedPrefix instead. |
| 1396 | assert(qualifier->getPrefix()); |
| 1397 | manglePrefix(qualifier->getPrefix()); |
| 1398 | |
| 1399 | mangleSourceName(qualifier->getAsIdentifier()); |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | llvm_unreachable("unexpected nested name specifier"); |
| 1404 | } |
| 1405 | |
| 1406 | void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) { |
| 1407 | // <prefix> ::= <prefix> <unqualified-name> |
| 1408 | // ::= <template-prefix> <template-args> |
| 1409 | // ::= <template-param> |
| 1410 | // ::= # empty |
| 1411 | // ::= <substitution> |
| 1412 | |
| 1413 | DC = IgnoreLinkageSpecDecls(DC); |
| 1414 | |
| 1415 | if (DC->isTranslationUnit()) |
| 1416 | return; |
| 1417 | |
| 1418 | if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) { |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame^] | 1419 | // Reflect the lambda mangling rules, except that we don't have an |
| 1420 | // actual function declaration. |
| 1421 | if (NoFunction) |
| 1422 | return; |
| 1423 | |
| 1424 | manglePrefix(getEffectiveParentContext(DC), NoFunction); |
| 1425 | // If we have a block mangling number, use it. |
| 1426 | unsigned Number = Block->getBlockManglingNumber(); |
| 1427 | // Otherwise, just make up a number. It doesn't matter what it is because |
| 1428 | // the symbol in question isn't externally visible. |
| 1429 | if (!Number) |
| 1430 | Number = Context.getBlockId(Block, false); |
| 1431 | Out << "Ub"; |
Eli Friedman | bace10c | 2013-06-24 20:24:19 +0000 | [diff] [blame] | 1432 | if (Number > 1) |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame^] | 1433 | Out << Number - 2; |
| 1434 | Out << '_'; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1435 | return; |
Ben Langmuir | 524387a | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 1436 | } else if (isa<CapturedDecl>(DC)) { |
| 1437 | // Skip CapturedDecl context. |
| 1438 | manglePrefix(getEffectiveParentContext(DC), NoFunction); |
| 1439 | return; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | const NamedDecl *ND = cast<NamedDecl>(DC); |
| 1443 | if (mangleSubstitution(ND)) |
| 1444 | return; |
| 1445 | |
| 1446 | // Check if we have a template. |
| 1447 | const TemplateArgumentList *TemplateArgs = 0; |
| 1448 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
| 1449 | mangleTemplatePrefix(TD); |
| 1450 | mangleTemplateArgs(*TemplateArgs); |
| 1451 | } |
| 1452 | else if(NoFunction && (isa<FunctionDecl>(ND) || isa<ObjCMethodDecl>(ND))) |
| 1453 | return; |
| 1454 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| 1455 | mangleObjCMethodName(Method); |
| 1456 | else { |
| 1457 | manglePrefix(getEffectiveDeclContext(ND), NoFunction); |
| 1458 | mangleUnqualifiedName(ND); |
| 1459 | } |
| 1460 | |
| 1461 | addSubstitution(ND); |
| 1462 | } |
| 1463 | |
| 1464 | void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) { |
| 1465 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 1466 | // ::= <template-param> |
| 1467 | // ::= <substitution> |
| 1468 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 1469 | return mangleTemplatePrefix(TD); |
| 1470 | |
| 1471 | if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName()) |
| 1472 | manglePrefix(Qualified->getQualifier()); |
| 1473 | |
| 1474 | if (OverloadedTemplateStorage *Overloaded |
| 1475 | = Template.getAsOverloadedTemplate()) { |
| 1476 | mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(), |
| 1477 | UnknownArity); |
| 1478 | return; |
| 1479 | } |
| 1480 | |
| 1481 | DependentTemplateName *Dependent = Template.getAsDependentTemplateName(); |
| 1482 | assert(Dependent && "Unknown template name kind?"); |
| 1483 | manglePrefix(Dependent->getQualifier()); |
| 1484 | mangleUnscopedTemplateName(Template); |
| 1485 | } |
| 1486 | |
| 1487 | void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) { |
| 1488 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 1489 | // ::= <template-param> |
| 1490 | // ::= <substitution> |
| 1491 | // <template-template-param> ::= <template-param> |
| 1492 | // <substitution> |
| 1493 | |
| 1494 | if (mangleSubstitution(ND)) |
| 1495 | return; |
| 1496 | |
| 1497 | // <template-template-param> ::= <template-param> |
| 1498 | if (const TemplateTemplateParmDecl *TTP |
| 1499 | = dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 1500 | mangleTemplateParameter(TTP->getIndex()); |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | manglePrefix(getEffectiveDeclContext(ND)); |
| 1505 | mangleUnqualifiedName(ND->getTemplatedDecl()); |
| 1506 | addSubstitution(ND); |
| 1507 | } |
| 1508 | |
| 1509 | /// Mangles a template name under the production <type>. Required for |
| 1510 | /// template template arguments. |
| 1511 | /// <type> ::= <class-enum-type> |
| 1512 | /// ::= <template-param> |
| 1513 | /// ::= <substitution> |
| 1514 | void CXXNameMangler::mangleType(TemplateName TN) { |
| 1515 | if (mangleSubstitution(TN)) |
| 1516 | return; |
| 1517 | |
| 1518 | TemplateDecl *TD = 0; |
| 1519 | |
| 1520 | switch (TN.getKind()) { |
| 1521 | case TemplateName::QualifiedTemplate: |
| 1522 | TD = TN.getAsQualifiedTemplateName()->getTemplateDecl(); |
| 1523 | goto HaveDecl; |
| 1524 | |
| 1525 | case TemplateName::Template: |
| 1526 | TD = TN.getAsTemplateDecl(); |
| 1527 | goto HaveDecl; |
| 1528 | |
| 1529 | HaveDecl: |
| 1530 | if (isa<TemplateTemplateParmDecl>(TD)) |
| 1531 | mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex()); |
| 1532 | else |
| 1533 | mangleName(TD); |
| 1534 | break; |
| 1535 | |
| 1536 | case TemplateName::OverloadedTemplate: |
| 1537 | llvm_unreachable("can't mangle an overloaded template name as a <type>"); |
| 1538 | |
| 1539 | case TemplateName::DependentTemplate: { |
| 1540 | const DependentTemplateName *Dependent = TN.getAsDependentTemplateName(); |
| 1541 | assert(Dependent->isIdentifier()); |
| 1542 | |
| 1543 | // <class-enum-type> ::= <name> |
| 1544 | // <name> ::= <nested-name> |
| 1545 | mangleUnresolvedPrefix(Dependent->getQualifier(), 0); |
| 1546 | mangleSourceName(Dependent->getIdentifier()); |
| 1547 | break; |
| 1548 | } |
| 1549 | |
| 1550 | case TemplateName::SubstTemplateTemplateParm: { |
| 1551 | // Substituted template parameters are mangled as the substituted |
| 1552 | // template. This will check for the substitution twice, which is |
| 1553 | // fine, but we have to return early so that we don't try to *add* |
| 1554 | // the substitution twice. |
| 1555 | SubstTemplateTemplateParmStorage *subst |
| 1556 | = TN.getAsSubstTemplateTemplateParm(); |
| 1557 | mangleType(subst->getReplacement()); |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 1562 | // FIXME: not clear how to mangle this! |
| 1563 | // template <template <class> class T...> class A { |
| 1564 | // template <template <class> class U...> void foo(B<T,U> x...); |
| 1565 | // }; |
| 1566 | Out << "_SUBSTPACK_"; |
| 1567 | break; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | addSubstitution(TN); |
| 1572 | } |
| 1573 | |
| 1574 | void |
| 1575 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 1576 | switch (OO) { |
| 1577 | // <operator-name> ::= nw # new |
| 1578 | case OO_New: Out << "nw"; break; |
| 1579 | // ::= na # new[] |
| 1580 | case OO_Array_New: Out << "na"; break; |
| 1581 | // ::= dl # delete |
| 1582 | case OO_Delete: Out << "dl"; break; |
| 1583 | // ::= da # delete[] |
| 1584 | case OO_Array_Delete: Out << "da"; break; |
| 1585 | // ::= ps # + (unary) |
| 1586 | // ::= pl # + (binary or unknown) |
| 1587 | case OO_Plus: |
| 1588 | Out << (Arity == 1? "ps" : "pl"); break; |
| 1589 | // ::= ng # - (unary) |
| 1590 | // ::= mi # - (binary or unknown) |
| 1591 | case OO_Minus: |
| 1592 | Out << (Arity == 1? "ng" : "mi"); break; |
| 1593 | // ::= ad # & (unary) |
| 1594 | // ::= an # & (binary or unknown) |
| 1595 | case OO_Amp: |
| 1596 | Out << (Arity == 1? "ad" : "an"); break; |
| 1597 | // ::= de # * (unary) |
| 1598 | // ::= ml # * (binary or unknown) |
| 1599 | case OO_Star: |
| 1600 | // Use binary when unknown. |
| 1601 | Out << (Arity == 1? "de" : "ml"); break; |
| 1602 | // ::= co # ~ |
| 1603 | case OO_Tilde: Out << "co"; break; |
| 1604 | // ::= dv # / |
| 1605 | case OO_Slash: Out << "dv"; break; |
| 1606 | // ::= rm # % |
| 1607 | case OO_Percent: Out << "rm"; break; |
| 1608 | // ::= or # | |
| 1609 | case OO_Pipe: Out << "or"; break; |
| 1610 | // ::= eo # ^ |
| 1611 | case OO_Caret: Out << "eo"; break; |
| 1612 | // ::= aS # = |
| 1613 | case OO_Equal: Out << "aS"; break; |
| 1614 | // ::= pL # += |
| 1615 | case OO_PlusEqual: Out << "pL"; break; |
| 1616 | // ::= mI # -= |
| 1617 | case OO_MinusEqual: Out << "mI"; break; |
| 1618 | // ::= mL # *= |
| 1619 | case OO_StarEqual: Out << "mL"; break; |
| 1620 | // ::= dV # /= |
| 1621 | case OO_SlashEqual: Out << "dV"; break; |
| 1622 | // ::= rM # %= |
| 1623 | case OO_PercentEqual: Out << "rM"; break; |
| 1624 | // ::= aN # &= |
| 1625 | case OO_AmpEqual: Out << "aN"; break; |
| 1626 | // ::= oR # |= |
| 1627 | case OO_PipeEqual: Out << "oR"; break; |
| 1628 | // ::= eO # ^= |
| 1629 | case OO_CaretEqual: Out << "eO"; break; |
| 1630 | // ::= ls # << |
| 1631 | case OO_LessLess: Out << "ls"; break; |
| 1632 | // ::= rs # >> |
| 1633 | case OO_GreaterGreater: Out << "rs"; break; |
| 1634 | // ::= lS # <<= |
| 1635 | case OO_LessLessEqual: Out << "lS"; break; |
| 1636 | // ::= rS # >>= |
| 1637 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
| 1638 | // ::= eq # == |
| 1639 | case OO_EqualEqual: Out << "eq"; break; |
| 1640 | // ::= ne # != |
| 1641 | case OO_ExclaimEqual: Out << "ne"; break; |
| 1642 | // ::= lt # < |
| 1643 | case OO_Less: Out << "lt"; break; |
| 1644 | // ::= gt # > |
| 1645 | case OO_Greater: Out << "gt"; break; |
| 1646 | // ::= le # <= |
| 1647 | case OO_LessEqual: Out << "le"; break; |
| 1648 | // ::= ge # >= |
| 1649 | case OO_GreaterEqual: Out << "ge"; break; |
| 1650 | // ::= nt # ! |
| 1651 | case OO_Exclaim: Out << "nt"; break; |
| 1652 | // ::= aa # && |
| 1653 | case OO_AmpAmp: Out << "aa"; break; |
| 1654 | // ::= oo # || |
| 1655 | case OO_PipePipe: Out << "oo"; break; |
| 1656 | // ::= pp # ++ |
| 1657 | case OO_PlusPlus: Out << "pp"; break; |
| 1658 | // ::= mm # -- |
| 1659 | case OO_MinusMinus: Out << "mm"; break; |
| 1660 | // ::= cm # , |
| 1661 | case OO_Comma: Out << "cm"; break; |
| 1662 | // ::= pm # ->* |
| 1663 | case OO_ArrowStar: Out << "pm"; break; |
| 1664 | // ::= pt # -> |
| 1665 | case OO_Arrow: Out << "pt"; break; |
| 1666 | // ::= cl # () |
| 1667 | case OO_Call: Out << "cl"; break; |
| 1668 | // ::= ix # [] |
| 1669 | case OO_Subscript: Out << "ix"; break; |
| 1670 | |
| 1671 | // ::= qu # ? |
| 1672 | // The conditional operator can't be overloaded, but we still handle it when |
| 1673 | // mangling expressions. |
| 1674 | case OO_Conditional: Out << "qu"; break; |
| 1675 | |
| 1676 | case OO_None: |
| 1677 | case NUM_OVERLOADED_OPERATORS: |
| 1678 | llvm_unreachable("Not an overloaded operator"); |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { |
| 1683 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
| 1684 | if (Quals.hasRestrict()) |
| 1685 | Out << 'r'; |
| 1686 | if (Quals.hasVolatile()) |
| 1687 | Out << 'V'; |
| 1688 | if (Quals.hasConst()) |
| 1689 | Out << 'K'; |
| 1690 | |
| 1691 | if (Quals.hasAddressSpace()) { |
| 1692 | // Extension: |
| 1693 | // |
| 1694 | // <type> ::= U <address-space-number> |
| 1695 | // |
| 1696 | // where <address-space-number> is a source name consisting of 'AS' |
| 1697 | // followed by the address space <number>. |
| 1698 | SmallString<64> ASString; |
Tanya Lattner | f21107b | 2013-02-08 01:07:32 +0000 | [diff] [blame] | 1699 | ASString = "AS" + llvm::utostr_32( |
| 1700 | Context.getASTContext().getTargetAddressSpace(Quals.getAddressSpace())); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1701 | Out << 'U' << ASString.size() << ASString; |
| 1702 | } |
| 1703 | |
| 1704 | StringRef LifetimeName; |
| 1705 | switch (Quals.getObjCLifetime()) { |
| 1706 | // Objective-C ARC Extension: |
| 1707 | // |
| 1708 | // <type> ::= U "__strong" |
| 1709 | // <type> ::= U "__weak" |
| 1710 | // <type> ::= U "__autoreleasing" |
| 1711 | case Qualifiers::OCL_None: |
| 1712 | break; |
| 1713 | |
| 1714 | case Qualifiers::OCL_Weak: |
| 1715 | LifetimeName = "__weak"; |
| 1716 | break; |
| 1717 | |
| 1718 | case Qualifiers::OCL_Strong: |
| 1719 | LifetimeName = "__strong"; |
| 1720 | break; |
| 1721 | |
| 1722 | case Qualifiers::OCL_Autoreleasing: |
| 1723 | LifetimeName = "__autoreleasing"; |
| 1724 | break; |
| 1725 | |
| 1726 | case Qualifiers::OCL_ExplicitNone: |
| 1727 | // The __unsafe_unretained qualifier is *not* mangled, so that |
| 1728 | // __unsafe_unretained types in ARC produce the same manglings as the |
| 1729 | // equivalent (but, naturally, unqualified) types in non-ARC, providing |
| 1730 | // better ABI compatibility. |
| 1731 | // |
| 1732 | // It's safe to do this because unqualified 'id' won't show up |
| 1733 | // in any type signatures that need to be mangled. |
| 1734 | break; |
| 1735 | } |
| 1736 | if (!LifetimeName.empty()) |
| 1737 | Out << 'U' << LifetimeName.size() << LifetimeName; |
| 1738 | } |
| 1739 | |
| 1740 | void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) { |
| 1741 | // <ref-qualifier> ::= R # lvalue reference |
| 1742 | // ::= O # rvalue-reference |
| 1743 | // Proposal to Itanium C++ ABI list on 1/26/11 |
| 1744 | switch (RefQualifier) { |
| 1745 | case RQ_None: |
| 1746 | break; |
| 1747 | |
| 1748 | case RQ_LValue: |
| 1749 | Out << 'R'; |
| 1750 | break; |
| 1751 | |
| 1752 | case RQ_RValue: |
| 1753 | Out << 'O'; |
| 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 1759 | Context.mangleObjCMethodName(MD, Out); |
| 1760 | } |
| 1761 | |
| 1762 | void CXXNameMangler::mangleType(QualType T) { |
| 1763 | // If our type is instantiation-dependent but not dependent, we mangle |
| 1764 | // it as it was written in the source, removing any top-level sugar. |
| 1765 | // Otherwise, use the canonical type. |
| 1766 | // |
| 1767 | // FIXME: This is an approximation of the instantiation-dependent name |
| 1768 | // mangling rules, since we should really be using the type as written and |
| 1769 | // augmented via semantic analysis (i.e., with implicit conversions and |
| 1770 | // default template arguments) for any instantiation-dependent type. |
| 1771 | // Unfortunately, that requires several changes to our AST: |
| 1772 | // - Instantiation-dependent TemplateSpecializationTypes will need to be |
| 1773 | // uniqued, so that we can handle substitutions properly |
| 1774 | // - Default template arguments will need to be represented in the |
| 1775 | // TemplateSpecializationType, since they need to be mangled even though |
| 1776 | // they aren't written. |
| 1777 | // - Conversions on non-type template arguments need to be expressed, since |
| 1778 | // they can affect the mangling of sizeof/alignof. |
| 1779 | if (!T->isInstantiationDependentType() || T->isDependentType()) |
| 1780 | T = T.getCanonicalType(); |
| 1781 | else { |
| 1782 | // Desugar any types that are purely sugar. |
| 1783 | do { |
| 1784 | // Don't desugar through template specialization types that aren't |
| 1785 | // type aliases. We need to mangle the template arguments as written. |
| 1786 | if (const TemplateSpecializationType *TST |
| 1787 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1788 | if (!TST->isTypeAlias()) |
| 1789 | break; |
| 1790 | |
| 1791 | QualType Desugared |
| 1792 | = T.getSingleStepDesugaredType(Context.getASTContext()); |
| 1793 | if (Desugared == T) |
| 1794 | break; |
| 1795 | |
| 1796 | T = Desugared; |
| 1797 | } while (true); |
| 1798 | } |
| 1799 | SplitQualType split = T.split(); |
| 1800 | Qualifiers quals = split.Quals; |
| 1801 | const Type *ty = split.Ty; |
| 1802 | |
| 1803 | bool isSubstitutable = quals || !isa<BuiltinType>(T); |
| 1804 | if (isSubstitutable && mangleSubstitution(T)) |
| 1805 | return; |
| 1806 | |
| 1807 | // If we're mangling a qualified array type, push the qualifiers to |
| 1808 | // the element type. |
| 1809 | if (quals && isa<ArrayType>(T)) { |
| 1810 | ty = Context.getASTContext().getAsArrayType(T); |
| 1811 | quals = Qualifiers(); |
| 1812 | |
| 1813 | // Note that we don't update T: we want to add the |
| 1814 | // substitution at the original type. |
| 1815 | } |
| 1816 | |
| 1817 | if (quals) { |
| 1818 | mangleQualifiers(quals); |
| 1819 | // Recurse: even if the qualified type isn't yet substitutable, |
| 1820 | // the unqualified type might be. |
| 1821 | mangleType(QualType(ty, 0)); |
| 1822 | } else { |
| 1823 | switch (ty->getTypeClass()) { |
| 1824 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 1825 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
| 1826 | case Type::CLASS: \ |
| 1827 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
| 1828 | return; |
| 1829 | #define TYPE(CLASS, PARENT) \ |
| 1830 | case Type::CLASS: \ |
| 1831 | mangleType(static_cast<const CLASS##Type*>(ty)); \ |
| 1832 | break; |
| 1833 | #include "clang/AST/TypeNodes.def" |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | // Add the substitution. |
| 1838 | if (isSubstitutable) |
| 1839 | addSubstitution(T); |
| 1840 | } |
| 1841 | |
| 1842 | void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) { |
| 1843 | if (!mangleStandardSubstitution(ND)) |
| 1844 | mangleName(ND); |
| 1845 | } |
| 1846 | |
| 1847 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
| 1848 | // <type> ::= <builtin-type> |
| 1849 | // <builtin-type> ::= v # void |
| 1850 | // ::= w # wchar_t |
| 1851 | // ::= b # bool |
| 1852 | // ::= c # char |
| 1853 | // ::= a # signed char |
| 1854 | // ::= h # unsigned char |
| 1855 | // ::= s # short |
| 1856 | // ::= t # unsigned short |
| 1857 | // ::= i # int |
| 1858 | // ::= j # unsigned int |
| 1859 | // ::= l # long |
| 1860 | // ::= m # unsigned long |
| 1861 | // ::= x # long long, __int64 |
| 1862 | // ::= y # unsigned long long, __int64 |
| 1863 | // ::= n # __int128 |
| 1864 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 1865 | // ::= f # float |
| 1866 | // ::= d # double |
| 1867 | // ::= e # long double, __float80 |
| 1868 | // UNSUPPORTED: ::= g # __float128 |
| 1869 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 1870 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 1871 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 1872 | // ::= Dh # IEEE 754r half-precision floating point (16 bits) |
| 1873 | // ::= Di # char32_t |
| 1874 | // ::= Ds # char16_t |
| 1875 | // ::= Dn # std::nullptr_t (i.e., decltype(nullptr)) |
| 1876 | // ::= u <source-name> # vendor extended type |
| 1877 | switch (T->getKind()) { |
| 1878 | case BuiltinType::Void: Out << 'v'; break; |
| 1879 | case BuiltinType::Bool: Out << 'b'; break; |
| 1880 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 1881 | case BuiltinType::UChar: Out << 'h'; break; |
| 1882 | case BuiltinType::UShort: Out << 't'; break; |
| 1883 | case BuiltinType::UInt: Out << 'j'; break; |
| 1884 | case BuiltinType::ULong: Out << 'm'; break; |
| 1885 | case BuiltinType::ULongLong: Out << 'y'; break; |
| 1886 | case BuiltinType::UInt128: Out << 'o'; break; |
| 1887 | case BuiltinType::SChar: Out << 'a'; break; |
| 1888 | case BuiltinType::WChar_S: |
| 1889 | case BuiltinType::WChar_U: Out << 'w'; break; |
| 1890 | case BuiltinType::Char16: Out << "Ds"; break; |
| 1891 | case BuiltinType::Char32: Out << "Di"; break; |
| 1892 | case BuiltinType::Short: Out << 's'; break; |
| 1893 | case BuiltinType::Int: Out << 'i'; break; |
| 1894 | case BuiltinType::Long: Out << 'l'; break; |
| 1895 | case BuiltinType::LongLong: Out << 'x'; break; |
| 1896 | case BuiltinType::Int128: Out << 'n'; break; |
| 1897 | case BuiltinType::Half: Out << "Dh"; break; |
| 1898 | case BuiltinType::Float: Out << 'f'; break; |
| 1899 | case BuiltinType::Double: Out << 'd'; break; |
| 1900 | case BuiltinType::LongDouble: Out << 'e'; break; |
| 1901 | case BuiltinType::NullPtr: Out << "Dn"; break; |
| 1902 | |
| 1903 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1904 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 1905 | case BuiltinType::Id: |
| 1906 | #include "clang/AST/BuiltinTypes.def" |
| 1907 | case BuiltinType::Dependent: |
| 1908 | llvm_unreachable("mangling a placeholder type"); |
| 1909 | case BuiltinType::ObjCId: Out << "11objc_object"; break; |
| 1910 | case BuiltinType::ObjCClass: Out << "10objc_class"; break; |
| 1911 | case BuiltinType::ObjCSel: Out << "13objc_selector"; break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1912 | case BuiltinType::OCLImage1d: Out << "11ocl_image1d"; break; |
| 1913 | case BuiltinType::OCLImage1dArray: Out << "16ocl_image1darray"; break; |
| 1914 | case BuiltinType::OCLImage1dBuffer: Out << "17ocl_image1dbuffer"; break; |
| 1915 | case BuiltinType::OCLImage2d: Out << "11ocl_image2d"; break; |
| 1916 | case BuiltinType::OCLImage2dArray: Out << "16ocl_image2darray"; break; |
| 1917 | case BuiltinType::OCLImage3d: Out << "11ocl_image3d"; break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1918 | case BuiltinType::OCLSampler: Out << "11ocl_sampler"; break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1919 | case BuiltinType::OCLEvent: Out << "9ocl_event"; break; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | // <type> ::= <function-type> |
| 1924 | // <function-type> ::= [<CV-qualifiers>] F [Y] |
| 1925 | // <bare-function-type> [<ref-qualifier>] E |
| 1926 | // (Proposal to cxx-abi-dev, 2012-05-11) |
| 1927 | void CXXNameMangler::mangleType(const FunctionProtoType *T) { |
| 1928 | // Mangle CV-qualifiers, if present. These are 'this' qualifiers, |
| 1929 | // e.g. "const" in "int (A::*)() const". |
| 1930 | mangleQualifiers(Qualifiers::fromCVRMask(T->getTypeQuals())); |
| 1931 | |
| 1932 | Out << 'F'; |
| 1933 | |
| 1934 | // FIXME: We don't have enough information in the AST to produce the 'Y' |
| 1935 | // encoding for extern "C" function types. |
| 1936 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 1937 | |
| 1938 | // Mangle the ref-qualifier, if present. |
| 1939 | mangleRefQualifier(T->getRefQualifier()); |
| 1940 | |
| 1941 | Out << 'E'; |
| 1942 | } |
| 1943 | void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
| 1944 | llvm_unreachable("Can't mangle K&R function prototypes"); |
| 1945 | } |
| 1946 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 1947 | bool MangleReturnType) { |
| 1948 | // We should never be mangling something without a prototype. |
| 1949 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 1950 | |
| 1951 | // Record that we're in a function type. See mangleFunctionParam |
| 1952 | // for details on what we're trying to achieve here. |
| 1953 | FunctionTypeDepthState saved = FunctionTypeDepth.push(); |
| 1954 | |
| 1955 | // <bare-function-type> ::= <signature type>+ |
| 1956 | if (MangleReturnType) { |
| 1957 | FunctionTypeDepth.enterResultType(); |
| 1958 | mangleType(Proto->getResultType()); |
| 1959 | FunctionTypeDepth.leaveResultType(); |
| 1960 | } |
| 1961 | |
| 1962 | if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { |
| 1963 | // <builtin-type> ::= v # void |
| 1964 | Out << 'v'; |
| 1965 | |
| 1966 | FunctionTypeDepth.pop(saved); |
| 1967 | return; |
| 1968 | } |
| 1969 | |
| 1970 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
| 1971 | ArgEnd = Proto->arg_type_end(); |
| 1972 | Arg != ArgEnd; ++Arg) |
| 1973 | mangleType(Context.getASTContext().getSignatureParameterType(*Arg)); |
| 1974 | |
| 1975 | FunctionTypeDepth.pop(saved); |
| 1976 | |
| 1977 | // <builtin-type> ::= z # ellipsis |
| 1978 | if (Proto->isVariadic()) |
| 1979 | Out << 'z'; |
| 1980 | } |
| 1981 | |
| 1982 | // <type> ::= <class-enum-type> |
| 1983 | // <class-enum-type> ::= <name> |
| 1984 | void CXXNameMangler::mangleType(const UnresolvedUsingType *T) { |
| 1985 | mangleName(T->getDecl()); |
| 1986 | } |
| 1987 | |
| 1988 | // <type> ::= <class-enum-type> |
| 1989 | // <class-enum-type> ::= <name> |
| 1990 | void CXXNameMangler::mangleType(const EnumType *T) { |
| 1991 | mangleType(static_cast<const TagType*>(T)); |
| 1992 | } |
| 1993 | void CXXNameMangler::mangleType(const RecordType *T) { |
| 1994 | mangleType(static_cast<const TagType*>(T)); |
| 1995 | } |
| 1996 | void CXXNameMangler::mangleType(const TagType *T) { |
| 1997 | mangleName(T->getDecl()); |
| 1998 | } |
| 1999 | |
| 2000 | // <type> ::= <array-type> |
| 2001 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 2002 | // ::= A [<dimension expression>] _ <element type> |
| 2003 | void CXXNameMangler::mangleType(const ConstantArrayType *T) { |
| 2004 | Out << 'A' << T->getSize() << '_'; |
| 2005 | mangleType(T->getElementType()); |
| 2006 | } |
| 2007 | void CXXNameMangler::mangleType(const VariableArrayType *T) { |
| 2008 | Out << 'A'; |
| 2009 | // decayed vla types (size 0) will just be skipped. |
| 2010 | if (T->getSizeExpr()) |
| 2011 | mangleExpression(T->getSizeExpr()); |
| 2012 | Out << '_'; |
| 2013 | mangleType(T->getElementType()); |
| 2014 | } |
| 2015 | void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { |
| 2016 | Out << 'A'; |
| 2017 | mangleExpression(T->getSizeExpr()); |
| 2018 | Out << '_'; |
| 2019 | mangleType(T->getElementType()); |
| 2020 | } |
| 2021 | void CXXNameMangler::mangleType(const IncompleteArrayType *T) { |
| 2022 | Out << "A_"; |
| 2023 | mangleType(T->getElementType()); |
| 2024 | } |
| 2025 | |
| 2026 | // <type> ::= <pointer-to-member-type> |
| 2027 | // <pointer-to-member-type> ::= M <class type> <member type> |
| 2028 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
| 2029 | Out << 'M'; |
| 2030 | mangleType(QualType(T->getClass(), 0)); |
| 2031 | QualType PointeeType = T->getPointeeType(); |
| 2032 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
| 2033 | mangleType(FPT); |
| 2034 | |
| 2035 | // Itanium C++ ABI 5.1.8: |
| 2036 | // |
| 2037 | // The type of a non-static member function is considered to be different, |
| 2038 | // for the purposes of substitution, from the type of a namespace-scope or |
| 2039 | // static member function whose type appears similar. The types of two |
| 2040 | // non-static member functions are considered to be different, for the |
| 2041 | // purposes of substitution, if the functions are members of different |
| 2042 | // classes. In other words, for the purposes of substitution, the class of |
| 2043 | // which the function is a member is considered part of the type of |
| 2044 | // function. |
| 2045 | |
| 2046 | // Given that we already substitute member function pointers as a |
| 2047 | // whole, the net effect of this rule is just to unconditionally |
| 2048 | // suppress substitution on the function type in a member pointer. |
| 2049 | // We increment the SeqID here to emulate adding an entry to the |
| 2050 | // substitution table. |
| 2051 | ++SeqID; |
| 2052 | } else |
| 2053 | mangleType(PointeeType); |
| 2054 | } |
| 2055 | |
| 2056 | // <type> ::= <template-param> |
| 2057 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
| 2058 | mangleTemplateParameter(T->getIndex()); |
| 2059 | } |
| 2060 | |
| 2061 | // <type> ::= <template-param> |
| 2062 | void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) { |
| 2063 | // FIXME: not clear how to mangle this! |
| 2064 | // template <class T...> class A { |
| 2065 | // template <class U...> void foo(T(*)(U) x...); |
| 2066 | // }; |
| 2067 | Out << "_SUBSTPACK_"; |
| 2068 | } |
| 2069 | |
| 2070 | // <type> ::= P <type> # pointer-to |
| 2071 | void CXXNameMangler::mangleType(const PointerType *T) { |
| 2072 | Out << 'P'; |
| 2073 | mangleType(T->getPointeeType()); |
| 2074 | } |
| 2075 | void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { |
| 2076 | Out << 'P'; |
| 2077 | mangleType(T->getPointeeType()); |
| 2078 | } |
| 2079 | |
| 2080 | // <type> ::= R <type> # reference-to |
| 2081 | void CXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 2082 | Out << 'R'; |
| 2083 | mangleType(T->getPointeeType()); |
| 2084 | } |
| 2085 | |
| 2086 | // <type> ::= O <type> # rvalue reference-to (C++0x) |
| 2087 | void CXXNameMangler::mangleType(const RValueReferenceType *T) { |
| 2088 | Out << 'O'; |
| 2089 | mangleType(T->getPointeeType()); |
| 2090 | } |
| 2091 | |
| 2092 | // <type> ::= C <type> # complex pair (C 2000) |
| 2093 | void CXXNameMangler::mangleType(const ComplexType *T) { |
| 2094 | Out << 'C'; |
| 2095 | mangleType(T->getElementType()); |
| 2096 | } |
| 2097 | |
| 2098 | // ARM's ABI for Neon vector types specifies that they should be mangled as |
| 2099 | // if they are structs (to match ARM's initial implementation). The |
| 2100 | // vector type must be one of the special types predefined by ARM. |
| 2101 | void CXXNameMangler::mangleNeonVectorType(const VectorType *T) { |
| 2102 | QualType EltType = T->getElementType(); |
| 2103 | assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType"); |
| 2104 | const char *EltName = 0; |
| 2105 | if (T->getVectorKind() == VectorType::NeonPolyVector) { |
| 2106 | switch (cast<BuiltinType>(EltType)->getKind()) { |
| 2107 | case BuiltinType::SChar: EltName = "poly8_t"; break; |
| 2108 | case BuiltinType::Short: EltName = "poly16_t"; break; |
| 2109 | default: llvm_unreachable("unexpected Neon polynomial vector element type"); |
| 2110 | } |
| 2111 | } else { |
| 2112 | switch (cast<BuiltinType>(EltType)->getKind()) { |
| 2113 | case BuiltinType::SChar: EltName = "int8_t"; break; |
| 2114 | case BuiltinType::UChar: EltName = "uint8_t"; break; |
| 2115 | case BuiltinType::Short: EltName = "int16_t"; break; |
| 2116 | case BuiltinType::UShort: EltName = "uint16_t"; break; |
| 2117 | case BuiltinType::Int: EltName = "int32_t"; break; |
| 2118 | case BuiltinType::UInt: EltName = "uint32_t"; break; |
| 2119 | case BuiltinType::LongLong: EltName = "int64_t"; break; |
| 2120 | case BuiltinType::ULongLong: EltName = "uint64_t"; break; |
| 2121 | case BuiltinType::Float: EltName = "float32_t"; break; |
| 2122 | default: llvm_unreachable("unexpected Neon vector element type"); |
| 2123 | } |
| 2124 | } |
| 2125 | const char *BaseName = 0; |
| 2126 | unsigned BitSize = (T->getNumElements() * |
| 2127 | getASTContext().getTypeSize(EltType)); |
| 2128 | if (BitSize == 64) |
| 2129 | BaseName = "__simd64_"; |
| 2130 | else { |
| 2131 | assert(BitSize == 128 && "Neon vector type not 64 or 128 bits"); |
| 2132 | BaseName = "__simd128_"; |
| 2133 | } |
| 2134 | Out << strlen(BaseName) + strlen(EltName); |
| 2135 | Out << BaseName << EltName; |
| 2136 | } |
| 2137 | |
| 2138 | // GNU extension: vector types |
| 2139 | // <type> ::= <vector-type> |
| 2140 | // <vector-type> ::= Dv <positive dimension number> _ |
| 2141 | // <extended element type> |
| 2142 | // ::= Dv [<dimension expression>] _ <element type> |
| 2143 | // <extended element type> ::= <element type> |
| 2144 | // ::= p # AltiVec vector pixel |
| 2145 | // ::= b # Altivec vector bool |
| 2146 | void CXXNameMangler::mangleType(const VectorType *T) { |
| 2147 | if ((T->getVectorKind() == VectorType::NeonVector || |
| 2148 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 2149 | mangleNeonVectorType(T); |
| 2150 | return; |
| 2151 | } |
| 2152 | Out << "Dv" << T->getNumElements() << '_'; |
| 2153 | if (T->getVectorKind() == VectorType::AltiVecPixel) |
| 2154 | Out << 'p'; |
| 2155 | else if (T->getVectorKind() == VectorType::AltiVecBool) |
| 2156 | Out << 'b'; |
| 2157 | else |
| 2158 | mangleType(T->getElementType()); |
| 2159 | } |
| 2160 | void CXXNameMangler::mangleType(const ExtVectorType *T) { |
| 2161 | mangleType(static_cast<const VectorType*>(T)); |
| 2162 | } |
| 2163 | void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { |
| 2164 | Out << "Dv"; |
| 2165 | mangleExpression(T->getSizeExpr()); |
| 2166 | Out << '_'; |
| 2167 | mangleType(T->getElementType()); |
| 2168 | } |
| 2169 | |
| 2170 | void CXXNameMangler::mangleType(const PackExpansionType *T) { |
| 2171 | // <type> ::= Dp <type> # pack expansion (C++0x) |
| 2172 | Out << "Dp"; |
| 2173 | mangleType(T->getPattern()); |
| 2174 | } |
| 2175 | |
| 2176 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 2177 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 2178 | } |
| 2179 | |
| 2180 | void CXXNameMangler::mangleType(const ObjCObjectType *T) { |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2181 | if (!T->qual_empty()) { |
| 2182 | // Mangle protocol qualifiers. |
| 2183 | SmallString<64> QualStr; |
| 2184 | llvm::raw_svector_ostream QualOS(QualStr); |
| 2185 | QualOS << "objcproto"; |
| 2186 | ObjCObjectType::qual_iterator i = T->qual_begin(), e = T->qual_end(); |
| 2187 | for ( ; i != e; ++i) { |
| 2188 | StringRef name = (*i)->getName(); |
| 2189 | QualOS << name.size() << name; |
| 2190 | } |
| 2191 | QualOS.flush(); |
| 2192 | Out << 'U' << QualStr.size() << QualStr; |
| 2193 | } |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2194 | mangleType(T->getBaseType()); |
| 2195 | } |
| 2196 | |
| 2197 | void CXXNameMangler::mangleType(const BlockPointerType *T) { |
| 2198 | Out << "U13block_pointer"; |
| 2199 | mangleType(T->getPointeeType()); |
| 2200 | } |
| 2201 | |
| 2202 | void CXXNameMangler::mangleType(const InjectedClassNameType *T) { |
| 2203 | // Mangle injected class name types as if the user had written the |
| 2204 | // specialization out fully. It may not actually be possible to see |
| 2205 | // this mangling, though. |
| 2206 | mangleType(T->getInjectedSpecializationType()); |
| 2207 | } |
| 2208 | |
| 2209 | void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { |
| 2210 | if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) { |
| 2211 | mangleName(TD, T->getArgs(), T->getNumArgs()); |
| 2212 | } else { |
| 2213 | if (mangleSubstitution(QualType(T, 0))) |
| 2214 | return; |
| 2215 | |
| 2216 | mangleTemplatePrefix(T->getTemplateName()); |
| 2217 | |
| 2218 | // FIXME: GCC does not appear to mangle the template arguments when |
| 2219 | // the template in question is a dependent template name. Should we |
| 2220 | // emulate that badness? |
| 2221 | mangleTemplateArgs(T->getArgs(), T->getNumArgs()); |
| 2222 | addSubstitution(QualType(T, 0)); |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | void CXXNameMangler::mangleType(const DependentNameType *T) { |
| 2227 | // Typename types are always nested |
| 2228 | Out << 'N'; |
| 2229 | manglePrefix(T->getQualifier()); |
| 2230 | mangleSourceName(T->getIdentifier()); |
| 2231 | Out << 'E'; |
| 2232 | } |
| 2233 | |
| 2234 | void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) { |
| 2235 | // Dependently-scoped template types are nested if they have a prefix. |
| 2236 | Out << 'N'; |
| 2237 | |
| 2238 | // TODO: avoid making this TemplateName. |
| 2239 | TemplateName Prefix = |
| 2240 | getASTContext().getDependentTemplateName(T->getQualifier(), |
| 2241 | T->getIdentifier()); |
| 2242 | mangleTemplatePrefix(Prefix); |
| 2243 | |
| 2244 | // FIXME: GCC does not appear to mangle the template arguments when |
| 2245 | // the template in question is a dependent template name. Should we |
| 2246 | // emulate that badness? |
| 2247 | mangleTemplateArgs(T->getArgs(), T->getNumArgs()); |
| 2248 | Out << 'E'; |
| 2249 | } |
| 2250 | |
| 2251 | void CXXNameMangler::mangleType(const TypeOfType *T) { |
| 2252 | // FIXME: this is pretty unsatisfactory, but there isn't an obvious |
| 2253 | // "extension with parameters" mangling. |
| 2254 | Out << "u6typeof"; |
| 2255 | } |
| 2256 | |
| 2257 | void CXXNameMangler::mangleType(const TypeOfExprType *T) { |
| 2258 | // FIXME: this is pretty unsatisfactory, but there isn't an obvious |
| 2259 | // "extension with parameters" mangling. |
| 2260 | Out << "u6typeof"; |
| 2261 | } |
| 2262 | |
| 2263 | void CXXNameMangler::mangleType(const DecltypeType *T) { |
| 2264 | Expr *E = T->getUnderlyingExpr(); |
| 2265 | |
| 2266 | // type ::= Dt <expression> E # decltype of an id-expression |
| 2267 | // # or class member access |
| 2268 | // ::= DT <expression> E # decltype of an expression |
| 2269 | |
| 2270 | // This purports to be an exhaustive list of id-expressions and |
| 2271 | // class member accesses. Note that we do not ignore parentheses; |
| 2272 | // parentheses change the semantics of decltype for these |
| 2273 | // expressions (and cause the mangler to use the other form). |
| 2274 | if (isa<DeclRefExpr>(E) || |
| 2275 | isa<MemberExpr>(E) || |
| 2276 | isa<UnresolvedLookupExpr>(E) || |
| 2277 | isa<DependentScopeDeclRefExpr>(E) || |
| 2278 | isa<CXXDependentScopeMemberExpr>(E) || |
| 2279 | isa<UnresolvedMemberExpr>(E)) |
| 2280 | Out << "Dt"; |
| 2281 | else |
| 2282 | Out << "DT"; |
| 2283 | mangleExpression(E); |
| 2284 | Out << 'E'; |
| 2285 | } |
| 2286 | |
| 2287 | void CXXNameMangler::mangleType(const UnaryTransformType *T) { |
| 2288 | // If this is dependent, we need to record that. If not, we simply |
| 2289 | // mangle it as the underlying type since they are equivalent. |
| 2290 | if (T->isDependentType()) { |
| 2291 | Out << 'U'; |
| 2292 | |
| 2293 | switch (T->getUTTKind()) { |
| 2294 | case UnaryTransformType::EnumUnderlyingType: |
| 2295 | Out << "3eut"; |
| 2296 | break; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | mangleType(T->getUnderlyingType()); |
| 2301 | } |
| 2302 | |
| 2303 | void CXXNameMangler::mangleType(const AutoType *T) { |
| 2304 | QualType D = T->getDeducedType(); |
| 2305 | // <builtin-type> ::= Da # dependent auto |
| 2306 | if (D.isNull()) |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 2307 | Out << (T->isDecltypeAuto() ? "Dc" : "Da"); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2308 | else |
| 2309 | mangleType(D); |
| 2310 | } |
| 2311 | |
| 2312 | void CXXNameMangler::mangleType(const AtomicType *T) { |
| 2313 | // <type> ::= U <source-name> <type> # vendor extended type qualifier |
| 2314 | // (Until there's a standardized mangling...) |
| 2315 | Out << "U7_Atomic"; |
| 2316 | mangleType(T->getValueType()); |
| 2317 | } |
| 2318 | |
| 2319 | void CXXNameMangler::mangleIntegerLiteral(QualType T, |
| 2320 | const llvm::APSInt &Value) { |
| 2321 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 2322 | Out << 'L'; |
| 2323 | |
| 2324 | mangleType(T); |
| 2325 | if (T->isBooleanType()) { |
| 2326 | // Boolean values are encoded as 0/1. |
| 2327 | Out << (Value.getBoolValue() ? '1' : '0'); |
| 2328 | } else { |
| 2329 | mangleNumber(Value); |
| 2330 | } |
| 2331 | Out << 'E'; |
| 2332 | |
| 2333 | } |
| 2334 | |
| 2335 | /// Mangles a member expression. |
| 2336 | void CXXNameMangler::mangleMemberExpr(const Expr *base, |
| 2337 | bool isArrow, |
| 2338 | NestedNameSpecifier *qualifier, |
| 2339 | NamedDecl *firstQualifierLookup, |
| 2340 | DeclarationName member, |
| 2341 | unsigned arity) { |
| 2342 | // <expression> ::= dt <expression> <unresolved-name> |
| 2343 | // ::= pt <expression> <unresolved-name> |
| 2344 | if (base) { |
| 2345 | if (base->isImplicitCXXThis()) { |
| 2346 | // Note: GCC mangles member expressions to the implicit 'this' as |
| 2347 | // *this., whereas we represent them as this->. The Itanium C++ ABI |
| 2348 | // does not specify anything here, so we follow GCC. |
| 2349 | Out << "dtdefpT"; |
| 2350 | } else { |
| 2351 | Out << (isArrow ? "pt" : "dt"); |
| 2352 | mangleExpression(base); |
| 2353 | } |
| 2354 | } |
| 2355 | mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity); |
| 2356 | } |
| 2357 | |
| 2358 | /// Look at the callee of the given call expression and determine if |
| 2359 | /// it's a parenthesized id-expression which would have triggered ADL |
| 2360 | /// otherwise. |
| 2361 | static bool isParenthesizedADLCallee(const CallExpr *call) { |
| 2362 | const Expr *callee = call->getCallee(); |
| 2363 | const Expr *fn = callee->IgnoreParens(); |
| 2364 | |
| 2365 | // Must be parenthesized. IgnoreParens() skips __extension__ nodes, |
| 2366 | // too, but for those to appear in the callee, it would have to be |
| 2367 | // parenthesized. |
| 2368 | if (callee == fn) return false; |
| 2369 | |
| 2370 | // Must be an unresolved lookup. |
| 2371 | const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn); |
| 2372 | if (!lookup) return false; |
| 2373 | |
| 2374 | assert(!lookup->requiresADL()); |
| 2375 | |
| 2376 | // Must be an unqualified lookup. |
| 2377 | if (lookup->getQualifier()) return false; |
| 2378 | |
| 2379 | // Must not have found a class member. Note that if one is a class |
| 2380 | // member, they're all class members. |
| 2381 | if (lookup->getNumDecls() > 0 && |
| 2382 | (*lookup->decls_begin())->isCXXClassMember()) |
| 2383 | return false; |
| 2384 | |
| 2385 | // Otherwise, ADL would have been triggered. |
| 2386 | return true; |
| 2387 | } |
| 2388 | |
| 2389 | void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) { |
| 2390 | // <expression> ::= <unary operator-name> <expression> |
| 2391 | // ::= <binary operator-name> <expression> <expression> |
| 2392 | // ::= <trinary operator-name> <expression> <expression> <expression> |
| 2393 | // ::= cv <type> expression # conversion with one argument |
| 2394 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
| 2395 | // ::= st <type> # sizeof (a type) |
| 2396 | // ::= at <type> # alignof (a type) |
| 2397 | // ::= <template-param> |
| 2398 | // ::= <function-param> |
| 2399 | // ::= sr <type> <unqualified-name> # dependent name |
| 2400 | // ::= sr <type> <unqualified-name> <template-args> # dependent template-id |
| 2401 | // ::= ds <expression> <expression> # expr.*expr |
| 2402 | // ::= sZ <template-param> # size of a parameter pack |
| 2403 | // ::= sZ <function-param> # size of a function parameter pack |
| 2404 | // ::= <expr-primary> |
| 2405 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 2406 | // ::= L <type <value float> E # floating literal |
| 2407 | // ::= L <mangled-name> E # external name |
| 2408 | // ::= fpT # 'this' expression |
| 2409 | QualType ImplicitlyConvertedToType; |
| 2410 | |
| 2411 | recurse: |
| 2412 | switch (E->getStmtClass()) { |
| 2413 | case Expr::NoStmtClass: |
| 2414 | #define ABSTRACT_STMT(Type) |
| 2415 | #define EXPR(Type, Base) |
| 2416 | #define STMT(Type, Base) \ |
| 2417 | case Expr::Type##Class: |
| 2418 | #include "clang/AST/StmtNodes.inc" |
| 2419 | // fallthrough |
| 2420 | |
| 2421 | // These all can only appear in local or variable-initialization |
| 2422 | // contexts and so should never appear in a mangling. |
| 2423 | case Expr::AddrLabelExprClass: |
| 2424 | case Expr::DesignatedInitExprClass: |
| 2425 | case Expr::ImplicitValueInitExprClass: |
| 2426 | case Expr::ParenListExprClass: |
| 2427 | case Expr::LambdaExprClass: |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2428 | case Expr::MSPropertyRefExprClass: |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2429 | llvm_unreachable("unexpected statement kind"); |
| 2430 | |
| 2431 | // FIXME: invent manglings for all these. |
| 2432 | case Expr::BlockExprClass: |
| 2433 | case Expr::CXXPseudoDestructorExprClass: |
| 2434 | case Expr::ChooseExprClass: |
| 2435 | case Expr::CompoundLiteralExprClass: |
| 2436 | case Expr::ExtVectorElementExprClass: |
| 2437 | case Expr::GenericSelectionExprClass: |
| 2438 | case Expr::ObjCEncodeExprClass: |
| 2439 | case Expr::ObjCIsaExprClass: |
| 2440 | case Expr::ObjCIvarRefExprClass: |
| 2441 | case Expr::ObjCMessageExprClass: |
| 2442 | case Expr::ObjCPropertyRefExprClass: |
| 2443 | case Expr::ObjCProtocolExprClass: |
| 2444 | case Expr::ObjCSelectorExprClass: |
| 2445 | case Expr::ObjCStringLiteralClass: |
| 2446 | case Expr::ObjCBoxedExprClass: |
| 2447 | case Expr::ObjCArrayLiteralClass: |
| 2448 | case Expr::ObjCDictionaryLiteralClass: |
| 2449 | case Expr::ObjCSubscriptRefExprClass: |
| 2450 | case Expr::ObjCIndirectCopyRestoreExprClass: |
| 2451 | case Expr::OffsetOfExprClass: |
| 2452 | case Expr::PredefinedExprClass: |
| 2453 | case Expr::ShuffleVectorExprClass: |
| 2454 | case Expr::StmtExprClass: |
| 2455 | case Expr::UnaryTypeTraitExprClass: |
| 2456 | case Expr::BinaryTypeTraitExprClass: |
| 2457 | case Expr::TypeTraitExprClass: |
| 2458 | case Expr::ArrayTypeTraitExprClass: |
| 2459 | case Expr::ExpressionTraitExprClass: |
| 2460 | case Expr::VAArgExprClass: |
| 2461 | case Expr::CXXUuidofExprClass: |
| 2462 | case Expr::CUDAKernelCallExprClass: |
| 2463 | case Expr::AsTypeExprClass: |
| 2464 | case Expr::PseudoObjectExprClass: |
| 2465 | case Expr::AtomicExprClass: |
| 2466 | { |
| 2467 | // As bad as this diagnostic is, it's better than crashing. |
| 2468 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2469 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2470 | "cannot yet mangle expression type %0"); |
| 2471 | Diags.Report(E->getExprLoc(), DiagID) |
| 2472 | << E->getStmtClassName() << E->getSourceRange(); |
| 2473 | break; |
| 2474 | } |
| 2475 | |
| 2476 | // Even gcc-4.5 doesn't mangle this. |
| 2477 | case Expr::BinaryConditionalOperatorClass: { |
| 2478 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2479 | unsigned DiagID = |
| 2480 | Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2481 | "?: operator with omitted middle operand cannot be mangled"); |
| 2482 | Diags.Report(E->getExprLoc(), DiagID) |
| 2483 | << E->getStmtClassName() << E->getSourceRange(); |
| 2484 | break; |
| 2485 | } |
| 2486 | |
| 2487 | // These are used for internal purposes and cannot be meaningfully mangled. |
| 2488 | case Expr::OpaqueValueExprClass: |
| 2489 | llvm_unreachable("cannot mangle opaque value; mangling wrong thing?"); |
| 2490 | |
| 2491 | case Expr::InitListExprClass: { |
| 2492 | // Proposal by Jason Merrill, 2012-01-03 |
| 2493 | Out << "il"; |
| 2494 | const InitListExpr *InitList = cast<InitListExpr>(E); |
| 2495 | for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i) |
| 2496 | mangleExpression(InitList->getInit(i)); |
| 2497 | Out << "E"; |
| 2498 | break; |
| 2499 | } |
| 2500 | |
| 2501 | case Expr::CXXDefaultArgExprClass: |
| 2502 | mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity); |
| 2503 | break; |
| 2504 | |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2505 | case Expr::CXXDefaultInitExprClass: |
| 2506 | mangleExpression(cast<CXXDefaultInitExpr>(E)->getExpr(), Arity); |
| 2507 | break; |
| 2508 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2509 | case Expr::CXXStdInitializerListExprClass: |
| 2510 | mangleExpression(cast<CXXStdInitializerListExpr>(E)->getSubExpr(), Arity); |
| 2511 | break; |
| 2512 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2513 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 2514 | mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), |
| 2515 | Arity); |
| 2516 | break; |
| 2517 | |
| 2518 | case Expr::UserDefinedLiteralClass: |
| 2519 | // We follow g++'s approach of mangling a UDL as a call to the literal |
| 2520 | // operator. |
| 2521 | case Expr::CXXMemberCallExprClass: // fallthrough |
| 2522 | case Expr::CallExprClass: { |
| 2523 | const CallExpr *CE = cast<CallExpr>(E); |
| 2524 | |
| 2525 | // <expression> ::= cp <simple-id> <expression>* E |
| 2526 | // We use this mangling only when the call would use ADL except |
| 2527 | // for being parenthesized. Per discussion with David |
| 2528 | // Vandervoorde, 2011.04.25. |
| 2529 | if (isParenthesizedADLCallee(CE)) { |
| 2530 | Out << "cp"; |
| 2531 | // The callee here is a parenthesized UnresolvedLookupExpr with |
| 2532 | // no qualifier and should always get mangled as a <simple-id> |
| 2533 | // anyway. |
| 2534 | |
| 2535 | // <expression> ::= cl <expression>* E |
| 2536 | } else { |
| 2537 | Out << "cl"; |
| 2538 | } |
| 2539 | |
| 2540 | mangleExpression(CE->getCallee(), CE->getNumArgs()); |
| 2541 | for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I) |
| 2542 | mangleExpression(CE->getArg(I)); |
| 2543 | Out << 'E'; |
| 2544 | break; |
| 2545 | } |
| 2546 | |
| 2547 | case Expr::CXXNewExprClass: { |
| 2548 | const CXXNewExpr *New = cast<CXXNewExpr>(E); |
| 2549 | if (New->isGlobalNew()) Out << "gs"; |
| 2550 | Out << (New->isArray() ? "na" : "nw"); |
| 2551 | for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(), |
| 2552 | E = New->placement_arg_end(); I != E; ++I) |
| 2553 | mangleExpression(*I); |
| 2554 | Out << '_'; |
| 2555 | mangleType(New->getAllocatedType()); |
| 2556 | if (New->hasInitializer()) { |
| 2557 | // Proposal by Jason Merrill, 2012-01-03 |
| 2558 | if (New->getInitializationStyle() == CXXNewExpr::ListInit) |
| 2559 | Out << "il"; |
| 2560 | else |
| 2561 | Out << "pi"; |
| 2562 | const Expr *Init = New->getInitializer(); |
| 2563 | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
| 2564 | // Directly inline the initializers. |
| 2565 | for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(), |
| 2566 | E = CCE->arg_end(); |
| 2567 | I != E; ++I) |
| 2568 | mangleExpression(*I); |
| 2569 | } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) { |
| 2570 | for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i) |
| 2571 | mangleExpression(PLE->getExpr(i)); |
| 2572 | } else if (New->getInitializationStyle() == CXXNewExpr::ListInit && |
| 2573 | isa<InitListExpr>(Init)) { |
| 2574 | // Only take InitListExprs apart for list-initialization. |
| 2575 | const InitListExpr *InitList = cast<InitListExpr>(Init); |
| 2576 | for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i) |
| 2577 | mangleExpression(InitList->getInit(i)); |
| 2578 | } else |
| 2579 | mangleExpression(Init); |
| 2580 | } |
| 2581 | Out << 'E'; |
| 2582 | break; |
| 2583 | } |
| 2584 | |
| 2585 | case Expr::MemberExprClass: { |
| 2586 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 2587 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 2588 | ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(), |
| 2589 | Arity); |
| 2590 | break; |
| 2591 | } |
| 2592 | |
| 2593 | case Expr::UnresolvedMemberExprClass: { |
| 2594 | const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E); |
| 2595 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 2596 | ME->getQualifier(), 0, ME->getMemberName(), |
| 2597 | Arity); |
| 2598 | if (ME->hasExplicitTemplateArgs()) |
| 2599 | mangleTemplateArgs(ME->getExplicitTemplateArgs()); |
| 2600 | break; |
| 2601 | } |
| 2602 | |
| 2603 | case Expr::CXXDependentScopeMemberExprClass: { |
| 2604 | const CXXDependentScopeMemberExpr *ME |
| 2605 | = cast<CXXDependentScopeMemberExpr>(E); |
| 2606 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 2607 | ME->getQualifier(), ME->getFirstQualifierFoundInScope(), |
| 2608 | ME->getMember(), Arity); |
| 2609 | if (ME->hasExplicitTemplateArgs()) |
| 2610 | mangleTemplateArgs(ME->getExplicitTemplateArgs()); |
| 2611 | break; |
| 2612 | } |
| 2613 | |
| 2614 | case Expr::UnresolvedLookupExprClass: { |
| 2615 | const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E); |
| 2616 | mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity); |
| 2617 | |
| 2618 | // All the <unresolved-name> productions end in a |
| 2619 | // base-unresolved-name, where <template-args> are just tacked |
| 2620 | // onto the end. |
| 2621 | if (ULE->hasExplicitTemplateArgs()) |
| 2622 | mangleTemplateArgs(ULE->getExplicitTemplateArgs()); |
| 2623 | break; |
| 2624 | } |
| 2625 | |
| 2626 | case Expr::CXXUnresolvedConstructExprClass: { |
| 2627 | const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E); |
| 2628 | unsigned N = CE->arg_size(); |
| 2629 | |
| 2630 | Out << "cv"; |
| 2631 | mangleType(CE->getType()); |
| 2632 | if (N != 1) Out << '_'; |
| 2633 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
| 2634 | if (N != 1) Out << 'E'; |
| 2635 | break; |
| 2636 | } |
| 2637 | |
| 2638 | case Expr::CXXTemporaryObjectExprClass: |
| 2639 | case Expr::CXXConstructExprClass: { |
| 2640 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(E); |
| 2641 | unsigned N = CE->getNumArgs(); |
| 2642 | |
| 2643 | // Proposal by Jason Merrill, 2012-01-03 |
| 2644 | if (CE->isListInitialization()) |
| 2645 | Out << "tl"; |
| 2646 | else |
| 2647 | Out << "cv"; |
| 2648 | mangleType(CE->getType()); |
| 2649 | if (N != 1) Out << '_'; |
| 2650 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
| 2651 | if (N != 1) Out << 'E'; |
| 2652 | break; |
| 2653 | } |
| 2654 | |
| 2655 | case Expr::CXXScalarValueInitExprClass: |
| 2656 | Out <<"cv"; |
| 2657 | mangleType(E->getType()); |
| 2658 | Out <<"_E"; |
| 2659 | break; |
| 2660 | |
| 2661 | case Expr::CXXNoexceptExprClass: |
| 2662 | Out << "nx"; |
| 2663 | mangleExpression(cast<CXXNoexceptExpr>(E)->getOperand()); |
| 2664 | break; |
| 2665 | |
| 2666 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 2667 | const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E); |
| 2668 | |
| 2669 | if (!SAE->isInstantiationDependent()) { |
| 2670 | // Itanium C++ ABI: |
| 2671 | // If the operand of a sizeof or alignof operator is not |
| 2672 | // instantiation-dependent it is encoded as an integer literal |
| 2673 | // reflecting the result of the operator. |
| 2674 | // |
| 2675 | // If the result of the operator is implicitly converted to a known |
| 2676 | // integer type, that type is used for the literal; otherwise, the type |
| 2677 | // of std::size_t or std::ptrdiff_t is used. |
| 2678 | QualType T = (ImplicitlyConvertedToType.isNull() || |
| 2679 | !ImplicitlyConvertedToType->isIntegerType())? SAE->getType() |
| 2680 | : ImplicitlyConvertedToType; |
| 2681 | llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext()); |
| 2682 | mangleIntegerLiteral(T, V); |
| 2683 | break; |
| 2684 | } |
| 2685 | |
| 2686 | switch(SAE->getKind()) { |
| 2687 | case UETT_SizeOf: |
| 2688 | Out << 's'; |
| 2689 | break; |
| 2690 | case UETT_AlignOf: |
| 2691 | Out << 'a'; |
| 2692 | break; |
| 2693 | case UETT_VecStep: |
| 2694 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2695 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2696 | "cannot yet mangle vec_step expression"); |
| 2697 | Diags.Report(DiagID); |
| 2698 | return; |
| 2699 | } |
| 2700 | if (SAE->isArgumentType()) { |
| 2701 | Out << 't'; |
| 2702 | mangleType(SAE->getArgumentType()); |
| 2703 | } else { |
| 2704 | Out << 'z'; |
| 2705 | mangleExpression(SAE->getArgumentExpr()); |
| 2706 | } |
| 2707 | break; |
| 2708 | } |
| 2709 | |
| 2710 | case Expr::CXXThrowExprClass: { |
| 2711 | const CXXThrowExpr *TE = cast<CXXThrowExpr>(E); |
| 2712 | |
| 2713 | // Proposal from David Vandervoorde, 2010.06.30 |
| 2714 | if (TE->getSubExpr()) { |
| 2715 | Out << "tw"; |
| 2716 | mangleExpression(TE->getSubExpr()); |
| 2717 | } else { |
| 2718 | Out << "tr"; |
| 2719 | } |
| 2720 | break; |
| 2721 | } |
| 2722 | |
| 2723 | case Expr::CXXTypeidExprClass: { |
| 2724 | const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E); |
| 2725 | |
| 2726 | // Proposal from David Vandervoorde, 2010.06.30 |
| 2727 | if (TIE->isTypeOperand()) { |
| 2728 | Out << "ti"; |
| 2729 | mangleType(TIE->getTypeOperand()); |
| 2730 | } else { |
| 2731 | Out << "te"; |
| 2732 | mangleExpression(TIE->getExprOperand()); |
| 2733 | } |
| 2734 | break; |
| 2735 | } |
| 2736 | |
| 2737 | case Expr::CXXDeleteExprClass: { |
| 2738 | const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E); |
| 2739 | |
| 2740 | // Proposal from David Vandervoorde, 2010.06.30 |
| 2741 | if (DE->isGlobalDelete()) Out << "gs"; |
| 2742 | Out << (DE->isArrayForm() ? "da" : "dl"); |
| 2743 | mangleExpression(DE->getArgument()); |
| 2744 | break; |
| 2745 | } |
| 2746 | |
| 2747 | case Expr::UnaryOperatorClass: { |
| 2748 | const UnaryOperator *UO = cast<UnaryOperator>(E); |
| 2749 | mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()), |
| 2750 | /*Arity=*/1); |
| 2751 | mangleExpression(UO->getSubExpr()); |
| 2752 | break; |
| 2753 | } |
| 2754 | |
| 2755 | case Expr::ArraySubscriptExprClass: { |
| 2756 | const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E); |
| 2757 | |
| 2758 | // Array subscript is treated as a syntactically weird form of |
| 2759 | // binary operator. |
| 2760 | Out << "ix"; |
| 2761 | mangleExpression(AE->getLHS()); |
| 2762 | mangleExpression(AE->getRHS()); |
| 2763 | break; |
| 2764 | } |
| 2765 | |
| 2766 | case Expr::CompoundAssignOperatorClass: // fallthrough |
| 2767 | case Expr::BinaryOperatorClass: { |
| 2768 | const BinaryOperator *BO = cast<BinaryOperator>(E); |
| 2769 | if (BO->getOpcode() == BO_PtrMemD) |
| 2770 | Out << "ds"; |
| 2771 | else |
| 2772 | mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()), |
| 2773 | /*Arity=*/2); |
| 2774 | mangleExpression(BO->getLHS()); |
| 2775 | mangleExpression(BO->getRHS()); |
| 2776 | break; |
| 2777 | } |
| 2778 | |
| 2779 | case Expr::ConditionalOperatorClass: { |
| 2780 | const ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 2781 | mangleOperatorName(OO_Conditional, /*Arity=*/3); |
| 2782 | mangleExpression(CO->getCond()); |
| 2783 | mangleExpression(CO->getLHS(), Arity); |
| 2784 | mangleExpression(CO->getRHS(), Arity); |
| 2785 | break; |
| 2786 | } |
| 2787 | |
| 2788 | case Expr::ImplicitCastExprClass: { |
| 2789 | ImplicitlyConvertedToType = E->getType(); |
| 2790 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 2791 | goto recurse; |
| 2792 | } |
| 2793 | |
| 2794 | case Expr::ObjCBridgedCastExprClass: { |
| 2795 | // Mangle ownership casts as a vendor extended operator __bridge, |
| 2796 | // __bridge_transfer, or __bridge_retain. |
| 2797 | StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName(); |
| 2798 | Out << "v1U" << Kind.size() << Kind; |
| 2799 | } |
| 2800 | // Fall through to mangle the cast itself. |
| 2801 | |
| 2802 | case Expr::CStyleCastExprClass: |
| 2803 | case Expr::CXXStaticCastExprClass: |
| 2804 | case Expr::CXXDynamicCastExprClass: |
| 2805 | case Expr::CXXReinterpretCastExprClass: |
| 2806 | case Expr::CXXConstCastExprClass: |
| 2807 | case Expr::CXXFunctionalCastExprClass: { |
| 2808 | const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E); |
| 2809 | Out << "cv"; |
| 2810 | mangleType(ECE->getType()); |
| 2811 | mangleExpression(ECE->getSubExpr()); |
| 2812 | break; |
| 2813 | } |
| 2814 | |
| 2815 | case Expr::CXXOperatorCallExprClass: { |
| 2816 | const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E); |
| 2817 | unsigned NumArgs = CE->getNumArgs(); |
| 2818 | mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs); |
| 2819 | // Mangle the arguments. |
| 2820 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2821 | mangleExpression(CE->getArg(i)); |
| 2822 | break; |
| 2823 | } |
| 2824 | |
| 2825 | case Expr::ParenExprClass: |
| 2826 | mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity); |
| 2827 | break; |
| 2828 | |
| 2829 | case Expr::DeclRefExprClass: { |
| 2830 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 2831 | |
| 2832 | switch (D->getKind()) { |
| 2833 | default: |
| 2834 | // <expr-primary> ::= L <mangled-name> E # external name |
| 2835 | Out << 'L'; |
| 2836 | mangle(D, "_Z"); |
| 2837 | Out << 'E'; |
| 2838 | break; |
| 2839 | |
| 2840 | case Decl::ParmVar: |
| 2841 | mangleFunctionParam(cast<ParmVarDecl>(D)); |
| 2842 | break; |
| 2843 | |
| 2844 | case Decl::EnumConstant: { |
| 2845 | const EnumConstantDecl *ED = cast<EnumConstantDecl>(D); |
| 2846 | mangleIntegerLiteral(ED->getType(), ED->getInitVal()); |
| 2847 | break; |
| 2848 | } |
| 2849 | |
| 2850 | case Decl::NonTypeTemplateParm: { |
| 2851 | const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D); |
| 2852 | mangleTemplateParameter(PD->getIndex()); |
| 2853 | break; |
| 2854 | } |
| 2855 | |
| 2856 | } |
| 2857 | |
| 2858 | break; |
| 2859 | } |
| 2860 | |
| 2861 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
| 2862 | // FIXME: not clear how to mangle this! |
| 2863 | // template <unsigned N...> class A { |
| 2864 | // template <class U...> void foo(U (&x)[N]...); |
| 2865 | // }; |
| 2866 | Out << "_SUBSTPACK_"; |
| 2867 | break; |
| 2868 | |
| 2869 | case Expr::FunctionParmPackExprClass: { |
| 2870 | // FIXME: not clear how to mangle this! |
| 2871 | const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E); |
| 2872 | Out << "v110_SUBSTPACK"; |
| 2873 | mangleFunctionParam(FPPE->getParameterPack()); |
| 2874 | break; |
| 2875 | } |
| 2876 | |
| 2877 | case Expr::DependentScopeDeclRefExprClass: { |
| 2878 | const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E); |
| 2879 | mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity); |
| 2880 | |
| 2881 | // All the <unresolved-name> productions end in a |
| 2882 | // base-unresolved-name, where <template-args> are just tacked |
| 2883 | // onto the end. |
| 2884 | if (DRE->hasExplicitTemplateArgs()) |
| 2885 | mangleTemplateArgs(DRE->getExplicitTemplateArgs()); |
| 2886 | break; |
| 2887 | } |
| 2888 | |
| 2889 | case Expr::CXXBindTemporaryExprClass: |
| 2890 | mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr()); |
| 2891 | break; |
| 2892 | |
| 2893 | case Expr::ExprWithCleanupsClass: |
| 2894 | mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity); |
| 2895 | break; |
| 2896 | |
| 2897 | case Expr::FloatingLiteralClass: { |
| 2898 | const FloatingLiteral *FL = cast<FloatingLiteral>(E); |
| 2899 | Out << 'L'; |
| 2900 | mangleType(FL->getType()); |
| 2901 | mangleFloat(FL->getValue()); |
| 2902 | Out << 'E'; |
| 2903 | break; |
| 2904 | } |
| 2905 | |
| 2906 | case Expr::CharacterLiteralClass: |
| 2907 | Out << 'L'; |
| 2908 | mangleType(E->getType()); |
| 2909 | Out << cast<CharacterLiteral>(E)->getValue(); |
| 2910 | Out << 'E'; |
| 2911 | break; |
| 2912 | |
| 2913 | // FIXME. __objc_yes/__objc_no are mangled same as true/false |
| 2914 | case Expr::ObjCBoolLiteralExprClass: |
| 2915 | Out << "Lb"; |
| 2916 | Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0'); |
| 2917 | Out << 'E'; |
| 2918 | break; |
| 2919 | |
| 2920 | case Expr::CXXBoolLiteralExprClass: |
| 2921 | Out << "Lb"; |
| 2922 | Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0'); |
| 2923 | Out << 'E'; |
| 2924 | break; |
| 2925 | |
| 2926 | case Expr::IntegerLiteralClass: { |
| 2927 | llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue()); |
| 2928 | if (E->getType()->isSignedIntegerType()) |
| 2929 | Value.setIsSigned(true); |
| 2930 | mangleIntegerLiteral(E->getType(), Value); |
| 2931 | break; |
| 2932 | } |
| 2933 | |
| 2934 | case Expr::ImaginaryLiteralClass: { |
| 2935 | const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E); |
| 2936 | // Mangle as if a complex literal. |
| 2937 | // Proposal from David Vandevoorde, 2010.06.30. |
| 2938 | Out << 'L'; |
| 2939 | mangleType(E->getType()); |
| 2940 | if (const FloatingLiteral *Imag = |
| 2941 | dyn_cast<FloatingLiteral>(IE->getSubExpr())) { |
| 2942 | // Mangle a floating-point zero of the appropriate type. |
| 2943 | mangleFloat(llvm::APFloat(Imag->getValue().getSemantics())); |
| 2944 | Out << '_'; |
| 2945 | mangleFloat(Imag->getValue()); |
| 2946 | } else { |
| 2947 | Out << "0_"; |
| 2948 | llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue()); |
| 2949 | if (IE->getSubExpr()->getType()->isSignedIntegerType()) |
| 2950 | Value.setIsSigned(true); |
| 2951 | mangleNumber(Value); |
| 2952 | } |
| 2953 | Out << 'E'; |
| 2954 | break; |
| 2955 | } |
| 2956 | |
| 2957 | case Expr::StringLiteralClass: { |
| 2958 | // Revised proposal from David Vandervoorde, 2010.07.15. |
| 2959 | Out << 'L'; |
| 2960 | assert(isa<ConstantArrayType>(E->getType())); |
| 2961 | mangleType(E->getType()); |
| 2962 | Out << 'E'; |
| 2963 | break; |
| 2964 | } |
| 2965 | |
| 2966 | case Expr::GNUNullExprClass: |
| 2967 | // FIXME: should this really be mangled the same as nullptr? |
| 2968 | // fallthrough |
| 2969 | |
| 2970 | case Expr::CXXNullPtrLiteralExprClass: { |
| 2971 | // Proposal from David Vandervoorde, 2010.06.30, as |
| 2972 | // modified by ABI list discussion. |
| 2973 | Out << "LDnE"; |
| 2974 | break; |
| 2975 | } |
| 2976 | |
| 2977 | case Expr::PackExpansionExprClass: |
| 2978 | Out << "sp"; |
| 2979 | mangleExpression(cast<PackExpansionExpr>(E)->getPattern()); |
| 2980 | break; |
| 2981 | |
| 2982 | case Expr::SizeOfPackExprClass: { |
| 2983 | Out << "sZ"; |
| 2984 | const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack(); |
| 2985 | if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack)) |
| 2986 | mangleTemplateParameter(TTP->getIndex()); |
| 2987 | else if (const NonTypeTemplateParmDecl *NTTP |
| 2988 | = dyn_cast<NonTypeTemplateParmDecl>(Pack)) |
| 2989 | mangleTemplateParameter(NTTP->getIndex()); |
| 2990 | else if (const TemplateTemplateParmDecl *TempTP |
| 2991 | = dyn_cast<TemplateTemplateParmDecl>(Pack)) |
| 2992 | mangleTemplateParameter(TempTP->getIndex()); |
| 2993 | else |
| 2994 | mangleFunctionParam(cast<ParmVarDecl>(Pack)); |
| 2995 | break; |
| 2996 | } |
| 2997 | |
| 2998 | case Expr::MaterializeTemporaryExprClass: { |
| 2999 | mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()); |
| 3000 | break; |
| 3001 | } |
| 3002 | |
| 3003 | case Expr::CXXThisExprClass: |
| 3004 | Out << "fpT"; |
| 3005 | break; |
| 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | /// Mangle an expression which refers to a parameter variable. |
| 3010 | /// |
| 3011 | /// <expression> ::= <function-param> |
| 3012 | /// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0 |
| 3013 | /// <function-param> ::= fp <top-level CV-qualifiers> |
| 3014 | /// <parameter-2 non-negative number> _ # L == 0, I > 0 |
| 3015 | /// <function-param> ::= fL <L-1 non-negative number> |
| 3016 | /// p <top-level CV-qualifiers> _ # L > 0, I == 0 |
| 3017 | /// <function-param> ::= fL <L-1 non-negative number> |
| 3018 | /// p <top-level CV-qualifiers> |
| 3019 | /// <I-1 non-negative number> _ # L > 0, I > 0 |
| 3020 | /// |
| 3021 | /// L is the nesting depth of the parameter, defined as 1 if the |
| 3022 | /// parameter comes from the innermost function prototype scope |
| 3023 | /// enclosing the current context, 2 if from the next enclosing |
| 3024 | /// function prototype scope, and so on, with one special case: if |
| 3025 | /// we've processed the full parameter clause for the innermost |
| 3026 | /// function type, then L is one less. This definition conveniently |
| 3027 | /// makes it irrelevant whether a function's result type was written |
| 3028 | /// trailing or leading, but is otherwise overly complicated; the |
| 3029 | /// numbering was first designed without considering references to |
| 3030 | /// parameter in locations other than return types, and then the |
| 3031 | /// mangling had to be generalized without changing the existing |
| 3032 | /// manglings. |
| 3033 | /// |
| 3034 | /// I is the zero-based index of the parameter within its parameter |
| 3035 | /// declaration clause. Note that the original ABI document describes |
| 3036 | /// this using 1-based ordinals. |
| 3037 | void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) { |
| 3038 | unsigned parmDepth = parm->getFunctionScopeDepth(); |
| 3039 | unsigned parmIndex = parm->getFunctionScopeIndex(); |
| 3040 | |
| 3041 | // Compute 'L'. |
| 3042 | // parmDepth does not include the declaring function prototype. |
| 3043 | // FunctionTypeDepth does account for that. |
| 3044 | assert(parmDepth < FunctionTypeDepth.getDepth()); |
| 3045 | unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth; |
| 3046 | if (FunctionTypeDepth.isInResultType()) |
| 3047 | nestingDepth--; |
| 3048 | |
| 3049 | if (nestingDepth == 0) { |
| 3050 | Out << "fp"; |
| 3051 | } else { |
| 3052 | Out << "fL" << (nestingDepth - 1) << 'p'; |
| 3053 | } |
| 3054 | |
| 3055 | // Top-level qualifiers. We don't have to worry about arrays here, |
| 3056 | // because parameters declared as arrays should already have been |
| 3057 | // transformed to have pointer type. FIXME: apparently these don't |
| 3058 | // get mangled if used as an rvalue of a known non-class type? |
| 3059 | assert(!parm->getType()->isArrayType() |
| 3060 | && "parameter's type is still an array type?"); |
| 3061 | mangleQualifiers(parm->getType().getQualifiers()); |
| 3062 | |
| 3063 | // Parameter index. |
| 3064 | if (parmIndex != 0) { |
| 3065 | Out << (parmIndex - 1); |
| 3066 | } |
| 3067 | Out << '_'; |
| 3068 | } |
| 3069 | |
| 3070 | void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { |
| 3071 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 3072 | // ::= C2 # base object constructor |
| 3073 | // ::= C3 # complete object allocating constructor |
| 3074 | // |
| 3075 | switch (T) { |
| 3076 | case Ctor_Complete: |
| 3077 | Out << "C1"; |
| 3078 | break; |
| 3079 | case Ctor_Base: |
| 3080 | Out << "C2"; |
| 3081 | break; |
| 3082 | case Ctor_CompleteAllocating: |
| 3083 | Out << "C3"; |
| 3084 | break; |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
| 3089 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 3090 | // ::= D1 # complete object destructor |
| 3091 | // ::= D2 # base object destructor |
| 3092 | // |
| 3093 | switch (T) { |
| 3094 | case Dtor_Deleting: |
| 3095 | Out << "D0"; |
| 3096 | break; |
| 3097 | case Dtor_Complete: |
| 3098 | Out << "D1"; |
| 3099 | break; |
| 3100 | case Dtor_Base: |
| 3101 | Out << "D2"; |
| 3102 | break; |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | void CXXNameMangler::mangleTemplateArgs( |
| 3107 | const ASTTemplateArgumentListInfo &TemplateArgs) { |
| 3108 | // <template-args> ::= I <template-arg>+ E |
| 3109 | Out << 'I'; |
| 3110 | for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i) |
| 3111 | mangleTemplateArg(TemplateArgs.getTemplateArgs()[i].getArgument()); |
| 3112 | Out << 'E'; |
| 3113 | } |
| 3114 | |
| 3115 | void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &AL) { |
| 3116 | // <template-args> ::= I <template-arg>+ E |
| 3117 | Out << 'I'; |
| 3118 | for (unsigned i = 0, e = AL.size(); i != e; ++i) |
| 3119 | mangleTemplateArg(AL[i]); |
| 3120 | Out << 'E'; |
| 3121 | } |
| 3122 | |
| 3123 | void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs, |
| 3124 | unsigned NumTemplateArgs) { |
| 3125 | // <template-args> ::= I <template-arg>+ E |
| 3126 | Out << 'I'; |
| 3127 | for (unsigned i = 0; i != NumTemplateArgs; ++i) |
| 3128 | mangleTemplateArg(TemplateArgs[i]); |
| 3129 | Out << 'E'; |
| 3130 | } |
| 3131 | |
| 3132 | void CXXNameMangler::mangleTemplateArg(TemplateArgument A) { |
| 3133 | // <template-arg> ::= <type> # type or template |
| 3134 | // ::= X <expression> E # expression |
| 3135 | // ::= <expr-primary> # simple expressions |
| 3136 | // ::= J <template-arg>* E # argument pack |
| 3137 | // ::= sp <expression> # pack expansion of (C++0x) |
| 3138 | if (!A.isInstantiationDependent() || A.isDependent()) |
| 3139 | A = Context.getASTContext().getCanonicalTemplateArgument(A); |
| 3140 | |
| 3141 | switch (A.getKind()) { |
| 3142 | case TemplateArgument::Null: |
| 3143 | llvm_unreachable("Cannot mangle NULL template argument"); |
| 3144 | |
| 3145 | case TemplateArgument::Type: |
| 3146 | mangleType(A.getAsType()); |
| 3147 | break; |
| 3148 | case TemplateArgument::Template: |
| 3149 | // This is mangled as <type>. |
| 3150 | mangleType(A.getAsTemplate()); |
| 3151 | break; |
| 3152 | case TemplateArgument::TemplateExpansion: |
| 3153 | // <type> ::= Dp <type> # pack expansion (C++0x) |
| 3154 | Out << "Dp"; |
| 3155 | mangleType(A.getAsTemplateOrTemplatePattern()); |
| 3156 | break; |
| 3157 | case TemplateArgument::Expression: { |
| 3158 | // It's possible to end up with a DeclRefExpr here in certain |
| 3159 | // dependent cases, in which case we should mangle as a |
| 3160 | // declaration. |
| 3161 | const Expr *E = A.getAsExpr()->IgnoreParens(); |
| 3162 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 3163 | const ValueDecl *D = DRE->getDecl(); |
| 3164 | if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) { |
| 3165 | Out << "L"; |
| 3166 | mangle(D, "_Z"); |
| 3167 | Out << 'E'; |
| 3168 | break; |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | Out << 'X'; |
| 3173 | mangleExpression(E); |
| 3174 | Out << 'E'; |
| 3175 | break; |
| 3176 | } |
| 3177 | case TemplateArgument::Integral: |
| 3178 | mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral()); |
| 3179 | break; |
| 3180 | case TemplateArgument::Declaration: { |
| 3181 | // <expr-primary> ::= L <mangled-name> E # external name |
| 3182 | // Clang produces AST's where pointer-to-member-function expressions |
| 3183 | // and pointer-to-function expressions are represented as a declaration not |
| 3184 | // an expression. We compensate for it here to produce the correct mangling. |
| 3185 | ValueDecl *D = A.getAsDecl(); |
| 3186 | bool compensateMangling = !A.isDeclForReferenceParam(); |
| 3187 | if (compensateMangling) { |
| 3188 | Out << 'X'; |
| 3189 | mangleOperatorName(OO_Amp, 1); |
| 3190 | } |
| 3191 | |
| 3192 | Out << 'L'; |
| 3193 | // References to external entities use the mangled name; if the name would |
| 3194 | // not normally be manged then mangle it as unqualified. |
| 3195 | // |
| 3196 | // FIXME: The ABI specifies that external names here should have _Z, but |
| 3197 | // gcc leaves this off. |
| 3198 | if (compensateMangling) |
| 3199 | mangle(D, "_Z"); |
| 3200 | else |
| 3201 | mangle(D, "Z"); |
| 3202 | Out << 'E'; |
| 3203 | |
| 3204 | if (compensateMangling) |
| 3205 | Out << 'E'; |
| 3206 | |
| 3207 | break; |
| 3208 | } |
| 3209 | case TemplateArgument::NullPtr: { |
| 3210 | // <expr-primary> ::= L <type> 0 E |
| 3211 | Out << 'L'; |
| 3212 | mangleType(A.getNullPtrType()); |
| 3213 | Out << "0E"; |
| 3214 | break; |
| 3215 | } |
| 3216 | case TemplateArgument::Pack: { |
| 3217 | // Note: proposal by Mike Herrick on 12/20/10 |
| 3218 | Out << 'J'; |
| 3219 | for (TemplateArgument::pack_iterator PA = A.pack_begin(), |
| 3220 | PAEnd = A.pack_end(); |
| 3221 | PA != PAEnd; ++PA) |
| 3222 | mangleTemplateArg(*PA); |
| 3223 | Out << 'E'; |
| 3224 | } |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | void CXXNameMangler::mangleTemplateParameter(unsigned Index) { |
| 3229 | // <template-param> ::= T_ # first template parameter |
| 3230 | // ::= T <parameter-2 non-negative number> _ |
| 3231 | if (Index == 0) |
| 3232 | Out << "T_"; |
| 3233 | else |
| 3234 | Out << 'T' << (Index - 1) << '_'; |
| 3235 | } |
| 3236 | |
| 3237 | void CXXNameMangler::mangleExistingSubstitution(QualType type) { |
| 3238 | bool result = mangleSubstitution(type); |
| 3239 | assert(result && "no existing substitution for type"); |
| 3240 | (void) result; |
| 3241 | } |
| 3242 | |
| 3243 | void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) { |
| 3244 | bool result = mangleSubstitution(tname); |
| 3245 | assert(result && "no existing substitution for template name"); |
| 3246 | (void) result; |
| 3247 | } |
| 3248 | |
| 3249 | // <substitution> ::= S <seq-id> _ |
| 3250 | // ::= S_ |
| 3251 | bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) { |
| 3252 | // Try one of the standard substitutions first. |
| 3253 | if (mangleStandardSubstitution(ND)) |
| 3254 | return true; |
| 3255 | |
| 3256 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
| 3257 | return mangleSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 3258 | } |
| 3259 | |
| 3260 | /// \brief Determine whether the given type has any qualifiers that are |
| 3261 | /// relevant for substitutions. |
| 3262 | static bool hasMangledSubstitutionQualifiers(QualType T) { |
| 3263 | Qualifiers Qs = T.getQualifiers(); |
| 3264 | return Qs.getCVRQualifiers() || Qs.hasAddressSpace(); |
| 3265 | } |
| 3266 | |
| 3267 | bool CXXNameMangler::mangleSubstitution(QualType T) { |
| 3268 | if (!hasMangledSubstitutionQualifiers(T)) { |
| 3269 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 3270 | return mangleSubstitution(RT->getDecl()); |
| 3271 | } |
| 3272 | |
| 3273 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 3274 | |
| 3275 | return mangleSubstitution(TypePtr); |
| 3276 | } |
| 3277 | |
| 3278 | bool CXXNameMangler::mangleSubstitution(TemplateName Template) { |
| 3279 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 3280 | return mangleSubstitution(TD); |
| 3281 | |
| 3282 | Template = Context.getASTContext().getCanonicalTemplateName(Template); |
| 3283 | return mangleSubstitution( |
| 3284 | reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); |
| 3285 | } |
| 3286 | |
| 3287 | bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) { |
| 3288 | llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr); |
| 3289 | if (I == Substitutions.end()) |
| 3290 | return false; |
| 3291 | |
| 3292 | unsigned SeqID = I->second; |
| 3293 | if (SeqID == 0) |
| 3294 | Out << "S_"; |
| 3295 | else { |
| 3296 | SeqID--; |
| 3297 | |
| 3298 | // <seq-id> is encoded in base-36, using digits and upper case letters. |
| 3299 | char Buffer[10]; |
| 3300 | char *BufferPtr = llvm::array_endof(Buffer); |
| 3301 | |
| 3302 | if (SeqID == 0) *--BufferPtr = '0'; |
| 3303 | |
| 3304 | while (SeqID) { |
| 3305 | assert(BufferPtr > Buffer && "Buffer overflow!"); |
| 3306 | |
| 3307 | char c = static_cast<char>(SeqID % 36); |
| 3308 | |
| 3309 | *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10); |
| 3310 | SeqID /= 36; |
| 3311 | } |
| 3312 | |
| 3313 | Out << 'S' |
| 3314 | << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr) |
| 3315 | << '_'; |
| 3316 | } |
| 3317 | |
| 3318 | return true; |
| 3319 | } |
| 3320 | |
| 3321 | static bool isCharType(QualType T) { |
| 3322 | if (T.isNull()) |
| 3323 | return false; |
| 3324 | |
| 3325 | return T->isSpecificBuiltinType(BuiltinType::Char_S) || |
| 3326 | T->isSpecificBuiltinType(BuiltinType::Char_U); |
| 3327 | } |
| 3328 | |
| 3329 | /// isCharSpecialization - Returns whether a given type is a template |
| 3330 | /// specialization of a given name with a single argument of type char. |
| 3331 | static bool isCharSpecialization(QualType T, const char *Name) { |
| 3332 | if (T.isNull()) |
| 3333 | return false; |
| 3334 | |
| 3335 | const RecordType *RT = T->getAs<RecordType>(); |
| 3336 | if (!RT) |
| 3337 | return false; |
| 3338 | |
| 3339 | const ClassTemplateSpecializationDecl *SD = |
| 3340 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 3341 | if (!SD) |
| 3342 | return false; |
| 3343 | |
| 3344 | if (!isStdNamespace(getEffectiveDeclContext(SD))) |
| 3345 | return false; |
| 3346 | |
| 3347 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 3348 | if (TemplateArgs.size() != 1) |
| 3349 | return false; |
| 3350 | |
| 3351 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 3352 | return false; |
| 3353 | |
| 3354 | return SD->getIdentifier()->getName() == Name; |
| 3355 | } |
| 3356 | |
| 3357 | template <std::size_t StrLen> |
| 3358 | static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD, |
| 3359 | const char (&Str)[StrLen]) { |
| 3360 | if (!SD->getIdentifier()->isStr(Str)) |
| 3361 | return false; |
| 3362 | |
| 3363 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 3364 | if (TemplateArgs.size() != 2) |
| 3365 | return false; |
| 3366 | |
| 3367 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 3368 | return false; |
| 3369 | |
| 3370 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 3371 | return false; |
| 3372 | |
| 3373 | return true; |
| 3374 | } |
| 3375 | |
| 3376 | bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) { |
| 3377 | // <substitution> ::= St # ::std:: |
| 3378 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 3379 | if (isStd(NS)) { |
| 3380 | Out << "St"; |
| 3381 | return true; |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) { |
| 3386 | if (!isStdNamespace(getEffectiveDeclContext(TD))) |
| 3387 | return false; |
| 3388 | |
| 3389 | // <substitution> ::= Sa # ::std::allocator |
| 3390 | if (TD->getIdentifier()->isStr("allocator")) { |
| 3391 | Out << "Sa"; |
| 3392 | return true; |
| 3393 | } |
| 3394 | |
| 3395 | // <<substitution> ::= Sb # ::std::basic_string |
| 3396 | if (TD->getIdentifier()->isStr("basic_string")) { |
| 3397 | Out << "Sb"; |
| 3398 | return true; |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | if (const ClassTemplateSpecializationDecl *SD = |
| 3403 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 3404 | if (!isStdNamespace(getEffectiveDeclContext(SD))) |
| 3405 | return false; |
| 3406 | |
| 3407 | // <substitution> ::= Ss # ::std::basic_string<char, |
| 3408 | // ::std::char_traits<char>, |
| 3409 | // ::std::allocator<char> > |
| 3410 | if (SD->getIdentifier()->isStr("basic_string")) { |
| 3411 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 3412 | |
| 3413 | if (TemplateArgs.size() != 3) |
| 3414 | return false; |
| 3415 | |
| 3416 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 3417 | return false; |
| 3418 | |
| 3419 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 3420 | return false; |
| 3421 | |
| 3422 | if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator")) |
| 3423 | return false; |
| 3424 | |
| 3425 | Out << "Ss"; |
| 3426 | return true; |
| 3427 | } |
| 3428 | |
| 3429 | // <substitution> ::= Si # ::std::basic_istream<char, |
| 3430 | // ::std::char_traits<char> > |
| 3431 | if (isStreamCharSpecialization(SD, "basic_istream")) { |
| 3432 | Out << "Si"; |
| 3433 | return true; |
| 3434 | } |
| 3435 | |
| 3436 | // <substitution> ::= So # ::std::basic_ostream<char, |
| 3437 | // ::std::char_traits<char> > |
| 3438 | if (isStreamCharSpecialization(SD, "basic_ostream")) { |
| 3439 | Out << "So"; |
| 3440 | return true; |
| 3441 | } |
| 3442 | |
| 3443 | // <substitution> ::= Sd # ::std::basic_iostream<char, |
| 3444 | // ::std::char_traits<char> > |
| 3445 | if (isStreamCharSpecialization(SD, "basic_iostream")) { |
| 3446 | Out << "Sd"; |
| 3447 | return true; |
| 3448 | } |
| 3449 | } |
| 3450 | return false; |
| 3451 | } |
| 3452 | |
| 3453 | void CXXNameMangler::addSubstitution(QualType T) { |
| 3454 | if (!hasMangledSubstitutionQualifiers(T)) { |
| 3455 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3456 | addSubstitution(RT->getDecl()); |
| 3457 | return; |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 3462 | addSubstitution(TypePtr); |
| 3463 | } |
| 3464 | |
| 3465 | void CXXNameMangler::addSubstitution(TemplateName Template) { |
| 3466 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 3467 | return addSubstitution(TD); |
| 3468 | |
| 3469 | Template = Context.getASTContext().getCanonicalTemplateName(Template); |
| 3470 | addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); |
| 3471 | } |
| 3472 | |
| 3473 | void CXXNameMangler::addSubstitution(uintptr_t Ptr) { |
| 3474 | assert(!Substitutions.count(Ptr) && "Substitution already exists!"); |
| 3475 | Substitutions[Ptr] = SeqID++; |
| 3476 | } |
| 3477 | |
| 3478 | // |
| 3479 | |
| 3480 | /// \brief Mangles the name of the declaration D and emits that name to the |
| 3481 | /// given output stream. |
| 3482 | /// |
| 3483 | /// If the declaration D requires a mangled name, this routine will emit that |
| 3484 | /// mangled name to \p os and return true. Otherwise, \p os will be unchanged |
| 3485 | /// and this routine will return false. In this case, the caller should just |
| 3486 | /// emit the identifier of the declaration (\c D->getIdentifier()) as its |
| 3487 | /// name. |
| 3488 | void ItaniumMangleContext::mangleName(const NamedDecl *D, |
| 3489 | raw_ostream &Out) { |
| 3490 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 3491 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 3492 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 3493 | "Invalid mangleName() call on 'structor decl!"); |
| 3494 | |
| 3495 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 3496 | getASTContext().getSourceManager(), |
| 3497 | "Mangling declaration"); |
| 3498 | |
| 3499 | CXXNameMangler Mangler(*this, Out, D); |
| 3500 | return Mangler.mangle(D); |
| 3501 | } |
| 3502 | |
| 3503 | void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, |
| 3504 | CXXCtorType Type, |
| 3505 | raw_ostream &Out) { |
| 3506 | CXXNameMangler Mangler(*this, Out, D, Type); |
| 3507 | Mangler.mangle(D); |
| 3508 | } |
| 3509 | |
| 3510 | void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, |
| 3511 | CXXDtorType Type, |
| 3512 | raw_ostream &Out) { |
| 3513 | CXXNameMangler Mangler(*this, Out, D, Type); |
| 3514 | Mangler.mangle(D); |
| 3515 | } |
| 3516 | |
| 3517 | void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD, |
| 3518 | const ThunkInfo &Thunk, |
| 3519 | raw_ostream &Out) { |
| 3520 | // <special-name> ::= T <call-offset> <base encoding> |
| 3521 | // # base is the nominal target function of thunk |
| 3522 | // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> |
| 3523 | // # base is the nominal target function of thunk |
| 3524 | // # first call-offset is 'this' adjustment |
| 3525 | // # second call-offset is result adjustment |
| 3526 | |
| 3527 | assert(!isa<CXXDestructorDecl>(MD) && |
| 3528 | "Use mangleCXXDtor for destructor decls!"); |
| 3529 | CXXNameMangler Mangler(*this, Out); |
| 3530 | Mangler.getStream() << "_ZT"; |
| 3531 | if (!Thunk.Return.isEmpty()) |
| 3532 | Mangler.getStream() << 'c'; |
| 3533 | |
| 3534 | // Mangle the 'this' pointer adjustment. |
| 3535 | Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset); |
| 3536 | |
| 3537 | // Mangle the return pointer adjustment if there is one. |
| 3538 | if (!Thunk.Return.isEmpty()) |
| 3539 | Mangler.mangleCallOffset(Thunk.Return.NonVirtual, |
| 3540 | Thunk.Return.VBaseOffsetOffset); |
| 3541 | |
| 3542 | Mangler.mangleFunctionEncoding(MD); |
| 3543 | } |
| 3544 | |
| 3545 | void |
| 3546 | ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, |
| 3547 | CXXDtorType Type, |
| 3548 | const ThisAdjustment &ThisAdjustment, |
| 3549 | raw_ostream &Out) { |
| 3550 | // <special-name> ::= T <call-offset> <base encoding> |
| 3551 | // # base is the nominal target function of thunk |
| 3552 | CXXNameMangler Mangler(*this, Out, DD, Type); |
| 3553 | Mangler.getStream() << "_ZT"; |
| 3554 | |
| 3555 | // Mangle the 'this' pointer adjustment. |
| 3556 | Mangler.mangleCallOffset(ThisAdjustment.NonVirtual, |
| 3557 | ThisAdjustment.VCallOffsetOffset); |
| 3558 | |
| 3559 | Mangler.mangleFunctionEncoding(DD); |
| 3560 | } |
| 3561 | |
| 3562 | /// mangleGuardVariable - Returns the mangled name for a guard variable |
| 3563 | /// for the passed in VarDecl. |
| 3564 | void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D, |
| 3565 | raw_ostream &Out) { |
| 3566 | // <special-name> ::= GV <object name> # Guard variable for one-time |
| 3567 | // # initialization |
| 3568 | CXXNameMangler Mangler(*this, Out); |
| 3569 | Mangler.getStream() << "_ZGV"; |
| 3570 | Mangler.mangleName(D); |
| 3571 | } |
| 3572 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 3573 | void ItaniumMangleContext::mangleItaniumThreadLocalInit(const VarDecl *D, |
| 3574 | raw_ostream &Out) { |
| 3575 | // <special-name> ::= TH <object name> |
| 3576 | CXXNameMangler Mangler(*this, Out); |
| 3577 | Mangler.getStream() << "_ZTH"; |
| 3578 | Mangler.mangleName(D); |
| 3579 | } |
| 3580 | |
| 3581 | void ItaniumMangleContext::mangleItaniumThreadLocalWrapper(const VarDecl *D, |
| 3582 | raw_ostream &Out) { |
| 3583 | // <special-name> ::= TW <object name> |
| 3584 | CXXNameMangler Mangler(*this, Out); |
| 3585 | Mangler.getStream() << "_ZTW"; |
| 3586 | Mangler.mangleName(D); |
| 3587 | } |
| 3588 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3589 | void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D, |
| 3590 | raw_ostream &Out) { |
| 3591 | // We match the GCC mangling here. |
| 3592 | // <special-name> ::= GR <object name> |
| 3593 | CXXNameMangler Mangler(*this, Out); |
| 3594 | Mangler.getStream() << "_ZGR"; |
| 3595 | Mangler.mangleName(D); |
| 3596 | } |
| 3597 | |
| 3598 | void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, |
| 3599 | raw_ostream &Out) { |
| 3600 | // <special-name> ::= TV <type> # virtual table |
| 3601 | CXXNameMangler Mangler(*this, Out); |
| 3602 | Mangler.getStream() << "_ZTV"; |
| 3603 | Mangler.mangleNameOrStandardSubstitution(RD); |
| 3604 | } |
| 3605 | |
| 3606 | void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
| 3607 | raw_ostream &Out) { |
| 3608 | // <special-name> ::= TT <type> # VTT structure |
| 3609 | CXXNameMangler Mangler(*this, Out); |
| 3610 | Mangler.getStream() << "_ZTT"; |
| 3611 | Mangler.mangleNameOrStandardSubstitution(RD); |
| 3612 | } |
| 3613 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 3614 | void |
| 3615 | ItaniumMangleContext::mangleCXXVBTable(const CXXRecordDecl *Derived, |
| 3616 | ArrayRef<const CXXRecordDecl *> BasePath, |
| 3617 | raw_ostream &Out) { |
| 3618 | llvm_unreachable("The Itanium C++ ABI does not have virtual base tables!"); |
| 3619 | } |
| 3620 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3621 | void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, |
| 3622 | int64_t Offset, |
| 3623 | const CXXRecordDecl *Type, |
| 3624 | raw_ostream &Out) { |
| 3625 | // <special-name> ::= TC <type> <offset number> _ <base type> |
| 3626 | CXXNameMangler Mangler(*this, Out); |
| 3627 | Mangler.getStream() << "_ZTC"; |
| 3628 | Mangler.mangleNameOrStandardSubstitution(RD); |
| 3629 | Mangler.getStream() << Offset; |
| 3630 | Mangler.getStream() << '_'; |
| 3631 | Mangler.mangleNameOrStandardSubstitution(Type); |
| 3632 | } |
| 3633 | |
| 3634 | void ItaniumMangleContext::mangleCXXRTTI(QualType Ty, |
| 3635 | raw_ostream &Out) { |
| 3636 | // <special-name> ::= TI <type> # typeinfo structure |
| 3637 | assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); |
| 3638 | CXXNameMangler Mangler(*this, Out); |
| 3639 | Mangler.getStream() << "_ZTI"; |
| 3640 | Mangler.mangleType(Ty); |
| 3641 | } |
| 3642 | |
| 3643 | void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty, |
| 3644 | raw_ostream &Out) { |
| 3645 | // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) |
| 3646 | CXXNameMangler Mangler(*this, Out); |
| 3647 | Mangler.getStream() << "_ZTS"; |
| 3648 | Mangler.mangleType(Ty); |
| 3649 | } |
| 3650 | |
| 3651 | MangleContext *clang::createItaniumMangleContext(ASTContext &Context, |
| 3652 | DiagnosticsEngine &Diags) { |
| 3653 | return new ItaniumMangleContext(Context, Diags); |
| 3654 | } |