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/Decl.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclObjC.h" 22 #include "clang/AST/DeclTemplate.h" 23 #include "clang/AST/ExprCXX.h" 24 #include "clang/Basic/ABI.h" 25 #include "clang/Basic/SourceManager.h" 26 #include "clang/Basic/TargetInfo.h" 27 #include "llvm/ADT/StringExtras.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include "llvm/Support/ErrorHandling.h" 30 31 #define MANGLE_CHECKER 0 32 33 #if MANGLE_CHECKER 34 #include <cxxabi.h> 35 #endif 36 37 using namespace clang; 38 39 namespace { 40 41 static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) { 42 const DeclContext *DC = dyn_cast<DeclContext>(ND); 43 if (!DC) 44 DC = ND->getDeclContext(); 45 while (!DC->isNamespace() && !DC->isTranslationUnit()) { 46 if (isa<FunctionDecl>(DC->getParent())) 47 return dyn_cast<CXXRecordDecl>(DC); 48 DC = DC->getParent(); 49 } 50 return 0; 51 } 52 53 static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) { 54 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && 55 "Passed in decl is not a ctor or dtor!"); 56 57 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) { 58 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl()); 59 60 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && 61 "Templated decl is not a ctor or dtor!"); 62 } 63 64 return MD; 65 } 66 67 static const unsigned UnknownArity = ~0U; 68 69 class ItaniumMangleContext : public MangleContext { 70 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds; 71 unsigned Discriminator; 72 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier; 73 74 public: 75 explicit ItaniumMangleContext(ASTContext &Context, 76 Diagnostic &Diags) 77 : MangleContext(Context, Diags) { } 78 79 uint64_t getAnonymousStructId(const TagDecl *TD) { 80 std::pair<llvm::DenseMap<const TagDecl *, 81 uint64_t>::iterator, bool> Result = 82 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size())); 83 return Result.first->second; 84 } 85 86 void startNewFunction() { 87 MangleContext::startNewFunction(); 88 mangleInitDiscriminator(); 89 } 90 91 /// @name Mangler Entry Points 92 /// @{ 93 94 bool shouldMangleDeclName(const NamedDecl *D); 95 void mangleName(const NamedDecl *D, llvm::raw_ostream &); 96 void mangleThunk(const CXXMethodDecl *MD, 97 const ThunkInfo &Thunk, 98 llvm::raw_ostream &); 99 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, 100 const ThisAdjustment &ThisAdjustment, 101 llvm::raw_ostream &); 102 void mangleReferenceTemporary(const VarDecl *D, 103 llvm::raw_ostream &); 104 void mangleCXXVTable(const CXXRecordDecl *RD, 105 llvm::raw_ostream &); 106 void mangleCXXVTT(const CXXRecordDecl *RD, 107 llvm::raw_ostream &); 108 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, 109 const CXXRecordDecl *Type, 110 llvm::raw_ostream &); 111 void mangleCXXRTTI(QualType T, llvm::raw_ostream &); 112 void mangleCXXRTTIName(QualType T, llvm::raw_ostream &); 113 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, 114 llvm::raw_ostream &); 115 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, 116 llvm::raw_ostream &); 117 118 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &); 119 120 void mangleInitDiscriminator() { 121 Discriminator = 0; 122 } 123 124 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { 125 unsigned &discriminator = Uniquifier[ND]; 126 if (!discriminator) 127 discriminator = ++Discriminator; 128 if (discriminator == 1) 129 return false; 130 disc = discriminator-2; 131 return true; 132 } 133 /// @} 134 }; 135 136 /// CXXNameMangler - Manage the mangling of a single name. 137 class CXXNameMangler { 138 ItaniumMangleContext &Context; 139 llvm::raw_ostream &Out; 140 141 const CXXMethodDecl *Structor; 142 unsigned StructorType; 143 144 /// SeqID - The next subsitution sequence number. 145 unsigned SeqID; 146 147 llvm::DenseMap<uintptr_t, unsigned> Substitutions; 148 149 ASTContext &getASTContext() const { return Context.getASTContext(); } 150 151 public: 152 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_) 153 : Context(C), Out(Out_), Structor(0), StructorType(0), SeqID(0) { } 154 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_, 155 const CXXConstructorDecl *D, CXXCtorType Type) 156 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), 157 SeqID(0) { } 158 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_, 159 const CXXDestructorDecl *D, CXXDtorType Type) 160 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), 161 SeqID(0) { } 162 163 #if MANGLE_CHECKER 164 ~CXXNameMangler() { 165 if (Out.str()[0] == '\01') 166 return; 167 168 int status = 0; 169 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status); 170 assert(status == 0 && "Could not demangle mangled name!"); 171 free(result); 172 } 173 #endif 174 llvm::raw_ostream &getStream() { return Out; } 175 176 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z"); 177 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual); 178 void mangleNumber(const llvm::APSInt &I); 179 void mangleNumber(int64_t Number); 180 void mangleFloat(const llvm::APFloat &F); 181 void mangleFunctionEncoding(const FunctionDecl *FD); 182 void mangleName(const NamedDecl *ND); 183 void mangleType(QualType T); 184 void mangleNameOrStandardSubstitution(const NamedDecl *ND); 185 186 private: 187 bool mangleSubstitution(const NamedDecl *ND); 188 bool mangleSubstitution(QualType T); 189 bool mangleSubstitution(TemplateName Template); 190 bool mangleSubstitution(uintptr_t Ptr); 191 192 bool mangleStandardSubstitution(const NamedDecl *ND); 193 194 void addSubstitution(const NamedDecl *ND) { 195 ND = cast<NamedDecl>(ND->getCanonicalDecl()); 196 197 addSubstitution(reinterpret_cast<uintptr_t>(ND)); 198 } 199 void addSubstitution(QualType T); 200 void addSubstitution(TemplateName Template); 201 void addSubstitution(uintptr_t Ptr); 202 203 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier); 204 void mangleUnresolvedName(NestedNameSpecifier *Qualifier, 205 DeclarationName Name, 206 unsigned KnownArity = UnknownArity); 207 208 void mangleName(const TemplateDecl *TD, 209 const TemplateArgument *TemplateArgs, 210 unsigned NumTemplateArgs); 211 void mangleUnqualifiedName(const NamedDecl *ND) { 212 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity); 213 } 214 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name, 215 unsigned KnownArity); 216 void mangleUnscopedName(const NamedDecl *ND); 217 void mangleUnscopedTemplateName(const TemplateDecl *ND); 218 void mangleUnscopedTemplateName(TemplateName); 219 void mangleSourceName(const IdentifierInfo *II); 220 void mangleLocalName(const NamedDecl *ND); 221 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC, 222 bool NoFunction=false); 223 void mangleNestedName(const TemplateDecl *TD, 224 const TemplateArgument *TemplateArgs, 225 unsigned NumTemplateArgs); 226 void manglePrefix(const DeclContext *DC, bool NoFunction=false); 227 void mangleTemplatePrefix(const TemplateDecl *ND); 228 void mangleTemplatePrefix(TemplateName Template); 229 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); 230 void mangleQualifiers(Qualifiers Quals); 231 void mangleRefQualifier(RefQualifierKind RefQualifier); 232 233 void mangleObjCMethodName(const ObjCMethodDecl *MD); 234 235 // Declare manglers for every type class. 236 #define ABSTRACT_TYPE(CLASS, PARENT) 237 #define NON_CANONICAL_TYPE(CLASS, PARENT) 238 #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); 239 #include "clang/AST/TypeNodes.def" 240 241 void mangleType(const TagType*); 242 void mangleType(TemplateName); 243 void mangleBareFunctionType(const FunctionType *T, 244 bool MangleReturnType); 245 void mangleNeonVectorType(const VectorType *T); 246 247 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value); 248 void mangleMemberExpr(const Expr *Base, bool IsArrow, 249 NestedNameSpecifier *Qualifier, 250 DeclarationName Name, 251 unsigned KnownArity); 252 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity); 253 void mangleCXXCtorType(CXXCtorType T); 254 void mangleCXXDtorType(CXXDtorType T); 255 256 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs); 257 void mangleTemplateArgs(TemplateName Template, 258 const TemplateArgument *TemplateArgs, 259 unsigned NumTemplateArgs); 260 void mangleTemplateArgs(const TemplateParameterList &PL, 261 const TemplateArgument *TemplateArgs, 262 unsigned NumTemplateArgs); 263 void mangleTemplateArgs(const TemplateParameterList &PL, 264 const TemplateArgumentList &AL); 265 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A); 266 267 void mangleTemplateParameter(unsigned Index); 268 }; 269 270 } 271 272 static bool isInCLinkageSpecification(const Decl *D) { 273 D = D->getCanonicalDecl(); 274 for (const DeclContext *DC = D->getDeclContext(); 275 !DC->isTranslationUnit(); DC = DC->getParent()) { 276 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) 277 return Linkage->getLanguage() == LinkageSpecDecl::lang_c; 278 } 279 280 return false; 281 } 282 283 bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) { 284 // In C, functions with no attributes never need to be mangled. Fastpath them. 285 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) 286 return false; 287 288 // Any decl can be declared with __asm("foo") on it, and this takes precedence 289 // over all other naming in the .o file. 290 if (D->hasAttr<AsmLabelAttr>()) 291 return true; 292 293 // Clang's "overloadable" attribute extension to C/C++ implies name mangling 294 // (always) as does passing a C++ member function and a function 295 // whose name is not a simple identifier. 296 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 297 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || 298 !FD->getDeclName().isIdentifier())) 299 return true; 300 301 // Otherwise, no mangling is done outside C++ mode. 302 if (!getASTContext().getLangOptions().CPlusPlus) 303 return false; 304 305 // Variables at global scope with non-internal linkage are not mangled 306 if (!FD) { 307 const DeclContext *DC = D->getDeclContext(); 308 // Check for extern variable declared locally. 309 if (DC->isFunctionOrMethod() && D->hasLinkage()) 310 while (!DC->isNamespace() && !DC->isTranslationUnit()) 311 DC = DC->getParent(); 312 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage) 313 return false; 314 } 315 316 // Class members are always mangled. 317 if (D->getDeclContext()->isRecord()) 318 return true; 319 320 // C functions and "main" are not mangled. 321 if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) 322 return false; 323 324 return true; 325 } 326 327 void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) { 328 // Any decl can be declared with __asm("foo") on it, and this takes precedence 329 // over all other naming in the .o file. 330 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 331 // If we have an asm name, then we use it as the mangling. 332 333 // Adding the prefix can cause problems when one file has a "foo" and 334 // another has a "\01foo". That is known to happen on ELF with the 335 // tricks normally used for producing aliases (PR9177). Fortunately the 336 // llvm mangler on ELF is a nop, so we can just avoid adding the \01 337 // marker. We also avoid adding the marker if this is an alias for an 338 // LLVM intrinsic. 339 llvm::StringRef UserLabelPrefix = 340 getASTContext().Target.getUserLabelPrefix(); 341 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm.")) 342 Out << '\01'; // LLVM IR Marker for __asm("foo") 343 344 Out << ALA->getLabel(); 345 return; 346 } 347 348 // <mangled-name> ::= _Z <encoding> 349 // ::= <data name> 350 // ::= <special-name> 351 Out << Prefix; 352 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 353 mangleFunctionEncoding(FD); 354 else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 355 mangleName(VD); 356 else 357 mangleName(cast<FieldDecl>(D)); 358 } 359 360 void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { 361 // <encoding> ::= <function name> <bare-function-type> 362 mangleName(FD); 363 364 // Don't mangle in the type if this isn't a decl we should typically mangle. 365 if (!Context.shouldMangleDeclName(FD)) 366 return; 367 368 // Whether the mangling of a function type includes the return type depends on 369 // the context and the nature of the function. The rules for deciding whether 370 // the return type is included are: 371 // 372 // 1. Template functions (names or types) have return types encoded, with 373 // the exceptions listed below. 374 // 2. Function types not appearing as part of a function name mangling, 375 // e.g. parameters, pointer types, etc., have return type encoded, with the 376 // exceptions listed below. 377 // 3. Non-template function names do not have return types encoded. 378 // 379 // The exceptions mentioned in (1) and (2) above, for which the return type is 380 // never included, are 381 // 1. Constructors. 382 // 2. Destructors. 383 // 3. Conversion operator functions, e.g. operator int. 384 bool MangleReturnType = false; 385 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { 386 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || 387 isa<CXXConversionDecl>(FD))) 388 MangleReturnType = true; 389 390 // Mangle the type of the primary template. 391 FD = PrimaryTemplate->getTemplatedDecl(); 392 } 393 394 // Do the canonicalization out here because parameter types can 395 // undergo additional canonicalization (e.g. array decay). 396 const FunctionType *FT 397 = cast<FunctionType>(Context.getASTContext() 398 .getCanonicalType(FD->getType())); 399 400 mangleBareFunctionType(FT, MangleReturnType); 401 } 402 403 static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) { 404 while (isa<LinkageSpecDecl>(DC)) { 405 DC = DC->getParent(); 406 } 407 408 return DC; 409 } 410 411 /// isStd - Return whether a given namespace is the 'std' namespace. 412 static bool isStd(const NamespaceDecl *NS) { 413 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit()) 414 return false; 415 416 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); 417 return II && II->isStr("std"); 418 } 419 420 // isStdNamespace - Return whether a given decl context is a toplevel 'std' 421 // namespace. 422 static bool isStdNamespace(const DeclContext *DC) { 423 if (!DC->isNamespace()) 424 return false; 425 426 return isStd(cast<NamespaceDecl>(DC)); 427 } 428 429 static const TemplateDecl * 430 isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { 431 // Check if we have a function template. 432 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ 433 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { 434 TemplateArgs = FD->getTemplateSpecializationArgs(); 435 return TD; 436 } 437 } 438 439 // Check if we have a class template. 440 if (const ClassTemplateSpecializationDecl *Spec = 441 dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 442 TemplateArgs = &Spec->getTemplateArgs(); 443 return Spec->getSpecializedTemplate(); 444 } 445 446 return 0; 447 } 448 449 void CXXNameMangler::mangleName(const NamedDecl *ND) { 450 // <name> ::= <nested-name> 451 // ::= <unscoped-name> 452 // ::= <unscoped-template-name> <template-args> 453 // ::= <local-name> 454 // 455 const DeclContext *DC = ND->getDeclContext(); 456 457 // If this is an extern variable declared locally, the relevant DeclContext 458 // is that of the containing namespace, or the translation unit. 459 if (isa<FunctionDecl>(DC) && ND->hasLinkage()) 460 while (!DC->isNamespace() && !DC->isTranslationUnit()) 461 DC = DC->getParent(); 462 else if (GetLocalClassDecl(ND)) { 463 mangleLocalName(ND); 464 return; 465 } 466 467 while (isa<LinkageSpecDecl>(DC)) 468 DC = DC->getParent(); 469 470 if (DC->isTranslationUnit() || isStdNamespace(DC)) { 471 // Check if we have a template. 472 const TemplateArgumentList *TemplateArgs = 0; 473 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { 474 mangleUnscopedTemplateName(TD); 475 TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); 476 mangleTemplateArgs(*TemplateParameters, *TemplateArgs); 477 return; 478 } 479 480 mangleUnscopedName(ND); 481 return; 482 } 483 484 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) { 485 mangleLocalName(ND); 486 return; 487 } 488 489 mangleNestedName(ND, DC); 490 } 491 void CXXNameMangler::mangleName(const TemplateDecl *TD, 492 const TemplateArgument *TemplateArgs, 493 unsigned NumTemplateArgs) { 494 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext()); 495 496 if (DC->isTranslationUnit() || isStdNamespace(DC)) { 497 mangleUnscopedTemplateName(TD); 498 TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); 499 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs); 500 } else { 501 mangleNestedName(TD, TemplateArgs, NumTemplateArgs); 502 } 503 } 504 505 void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) { 506 // <unscoped-name> ::= <unqualified-name> 507 // ::= St <unqualified-name> # ::std:: 508 if (isStdNamespace(ND->getDeclContext())) 509 Out << "St"; 510 511 mangleUnqualifiedName(ND); 512 } 513 514 void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) { 515 // <unscoped-template-name> ::= <unscoped-name> 516 // ::= <substitution> 517 if (mangleSubstitution(ND)) 518 return; 519 520 // <template-template-param> ::= <template-param> 521 if (const TemplateTemplateParmDecl *TTP 522 = dyn_cast<TemplateTemplateParmDecl>(ND)) { 523 mangleTemplateParameter(TTP->getIndex()); 524 return; 525 } 526 527 mangleUnscopedName(ND->getTemplatedDecl()); 528 addSubstitution(ND); 529 } 530 531 void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) { 532 // <unscoped-template-name> ::= <unscoped-name> 533 // ::= <substitution> 534 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 535 return mangleUnscopedTemplateName(TD); 536 537 if (mangleSubstitution(Template)) 538 return; 539 540 // FIXME: How to cope with operators here? 541 DependentTemplateName *Dependent = Template.getAsDependentTemplateName(); 542 assert(Dependent && "Not a dependent template name?"); 543 if (!Dependent->isIdentifier()) { 544 // FIXME: We can't possibly know the arity of the operator here! 545 Diagnostic &Diags = Context.getDiags(); 546 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, 547 "cannot mangle dependent operator name"); 548 Diags.Report(DiagID); 549 return; 550 } 551 552 mangleSourceName(Dependent->getIdentifier()); 553 addSubstitution(Template); 554 } 555 556 void CXXNameMangler::mangleFloat(const llvm::APFloat &F) { 557 // TODO: avoid this copy with careful stream management. 558 llvm::SmallString<20> Buffer; 559 F.bitcastToAPInt().toString(Buffer, 16, false); 560 Out.write(Buffer.data(), Buffer.size()); 561 } 562 563 void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) { 564 if (Value.isSigned() && Value.isNegative()) { 565 Out << 'n'; 566 Value.abs().print(Out, true); 567 } else 568 Value.print(Out, Value.isSigned()); 569 } 570 571 void CXXNameMangler::mangleNumber(int64_t Number) { 572 // <number> ::= [n] <non-negative decimal integer> 573 if (Number < 0) { 574 Out << 'n'; 575 Number = -Number; 576 } 577 578 Out << Number; 579 } 580 581 void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) { 582 // <call-offset> ::= h <nv-offset> _ 583 // ::= v <v-offset> _ 584 // <nv-offset> ::= <offset number> # non-virtual base override 585 // <v-offset> ::= <offset number> _ <virtual offset number> 586 // # virtual base override, with vcall offset 587 if (!Virtual) { 588 Out << 'h'; 589 mangleNumber(NonVirtual); 590 Out << '_'; 591 return; 592 } 593 594 Out << 'v'; 595 mangleNumber(NonVirtual); 596 Out << '_'; 597 mangleNumber(Virtual); 598 Out << '_'; 599 } 600 601 void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) { 602 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier); 603 switch (Qualifier->getKind()) { 604 case NestedNameSpecifier::Global: 605 // nothing 606 break; 607 case NestedNameSpecifier::Namespace: 608 mangleName(Qualifier->getAsNamespace()); 609 break; 610 case NestedNameSpecifier::NamespaceAlias: 611 mangleName(Qualifier->getAsNamespaceAlias()->getNamespace()); 612 break; 613 case NestedNameSpecifier::TypeSpec: 614 case NestedNameSpecifier::TypeSpecWithTemplate: { 615 const Type *QTy = Qualifier->getAsType(); 616 617 if (const TemplateSpecializationType *TST = 618 dyn_cast<TemplateSpecializationType>(QTy)) { 619 if (!mangleSubstitution(QualType(TST, 0))) { 620 mangleTemplatePrefix(TST->getTemplateName()); 621 622 // FIXME: GCC does not appear to mangle the template arguments when 623 // the template in question is a dependent template name. Should we 624 // emulate that badness? 625 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(), 626 TST->getNumArgs()); 627 addSubstitution(QualType(TST, 0)); 628 } 629 } else if (const DependentTemplateSpecializationType *DTST 630 = dyn_cast<DependentTemplateSpecializationType>(QTy)) { 631 TemplateName Template 632 = getASTContext().getDependentTemplateName(DTST->getQualifier(), 633 DTST->getIdentifier()); 634 mangleTemplatePrefix(Template); 635 636 // FIXME: GCC does not appear to mangle the template arguments when 637 // the template in question is a dependent template name. Should we 638 // emulate that badness? 639 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs()); 640 } else { 641 // We use the QualType mangle type variant here because it handles 642 // substitutions. 643 mangleType(QualType(QTy, 0)); 644 } 645 } 646 break; 647 case NestedNameSpecifier::Identifier: 648 // Member expressions can have these without prefixes. 649 if (Qualifier->getPrefix()) 650 mangleUnresolvedScope(Qualifier->getPrefix()); 651 mangleSourceName(Qualifier->getAsIdentifier()); 652 break; 653 } 654 } 655 656 /// Mangles a name which was not resolved to a specific entity. 657 void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier, 658 DeclarationName Name, 659 unsigned KnownArity) { 660 if (Qualifier) 661 mangleUnresolvedScope(Qualifier); 662 // FIXME: ambiguity of unqualified lookup with :: 663 664 mangleUnqualifiedName(0, Name, KnownArity); 665 } 666 667 static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) { 668 assert(RD->isAnonymousStructOrUnion() && 669 "Expected anonymous struct or union!"); 670 671 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); 672 I != E; ++I) { 673 const FieldDecl *FD = *I; 674 675 if (FD->getIdentifier()) 676 return FD; 677 678 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) { 679 if (const FieldDecl *NamedDataMember = 680 FindFirstNamedDataMember(RT->getDecl())) 681 return NamedDataMember; 682 } 683 } 684 685 // We didn't find a named data member. 686 return 0; 687 } 688 689 void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, 690 DeclarationName Name, 691 unsigned KnownArity) { 692 // <unqualified-name> ::= <operator-name> 693 // ::= <ctor-dtor-name> 694 // ::= <source-name> 695 switch (Name.getNameKind()) { 696 case DeclarationName::Identifier: { 697 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { 698 // We must avoid conflicts between internally- and externally- 699 // linked variable and function declaration names in the same TU: 700 // void test() { extern void foo(); } 701 // static void foo(); 702 // This naming convention is the same as that followed by GCC, 703 // though it shouldn't actually matter. 704 if (ND && ND->getLinkage() == InternalLinkage && 705 ND->getDeclContext()->isFileContext()) 706 Out << 'L'; 707 708 mangleSourceName(II); 709 break; 710 } 711 712 // Otherwise, an anonymous entity. We must have a declaration. 713 assert(ND && "mangling empty name without declaration"); 714 715 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { 716 if (NS->isAnonymousNamespace()) { 717 // This is how gcc mangles these names. 718 Out << "12_GLOBAL__N_1"; 719 break; 720 } 721 } 722 723 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { 724 // We must have an anonymous union or struct declaration. 725 const RecordDecl *RD = 726 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl()); 727 728 // Itanium C++ ABI 5.1.2: 729 // 730 // For the purposes of mangling, the name of an anonymous union is 731 // considered to be the name of the first named data member found by a 732 // pre-order, depth-first, declaration-order walk of the data members of 733 // the anonymous union. If there is no such data member (i.e., if all of 734 // the data members in the union are unnamed), then there is no way for 735 // a program to refer to the anonymous union, and there is therefore no 736 // need to mangle its name. 737 const FieldDecl *FD = FindFirstNamedDataMember(RD); 738 739 // It's actually possible for various reasons for us to get here 740 // with an empty anonymous struct / union. Fortunately, it 741 // doesn't really matter what name we generate. 742 if (!FD) break; 743 assert(FD->getIdentifier() && "Data member name isn't an identifier!"); 744 745 mangleSourceName(FD->getIdentifier()); 746 break; 747 } 748 749 // We must have an anonymous struct. 750 const TagDecl *TD = cast<TagDecl>(ND); 751 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { 752 assert(TD->getDeclContext() == D->getDeclContext() && 753 "Typedef should not be in another decl context!"); 754 assert(D->getDeclName().getAsIdentifierInfo() && 755 "Typedef was not named!"); 756 mangleSourceName(D->getDeclName().getAsIdentifierInfo()); 757 break; 758 } 759 760 // Get a unique id for the anonymous struct. 761 uint64_t AnonStructId = Context.getAnonymousStructId(TD); 762 763 // Mangle it as a source name in the form 764 // [n] $_<id> 765 // where n is the length of the string. 766 llvm::SmallString<8> Str; 767 Str += "$_"; 768 Str += llvm::utostr(AnonStructId); 769 770 Out << Str.size(); 771 Out << Str.str(); 772 break; 773 } 774 775 case DeclarationName::ObjCZeroArgSelector: 776 case DeclarationName::ObjCOneArgSelector: 777 case DeclarationName::ObjCMultiArgSelector: 778 assert(false && "Can't mangle Objective-C selector names here!"); 779 break; 780 781 case DeclarationName::CXXConstructorName: 782 if (ND == Structor) 783 // If the named decl is the C++ constructor we're mangling, use the type 784 // we were given. 785 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType)); 786 else 787 // Otherwise, use the complete constructor name. This is relevant if a 788 // class with a constructor is declared within a constructor. 789 mangleCXXCtorType(Ctor_Complete); 790 break; 791 792 case DeclarationName::CXXDestructorName: 793 if (ND == Structor) 794 // If the named decl is the C++ destructor we're mangling, use the type we 795 // were given. 796 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); 797 else 798 // Otherwise, use the complete destructor name. This is relevant if a 799 // class with a destructor is declared within a destructor. 800 mangleCXXDtorType(Dtor_Complete); 801 break; 802 803 case DeclarationName::CXXConversionFunctionName: 804 // <operator-name> ::= cv <type> # (cast) 805 Out << "cv"; 806 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType())); 807 break; 808 809 case DeclarationName::CXXOperatorName: { 810 unsigned Arity; 811 if (ND) { 812 Arity = cast<FunctionDecl>(ND)->getNumParams(); 813 814 // If we have a C++ member function, we need to include the 'this' pointer. 815 // FIXME: This does not make sense for operators that are static, but their 816 // names stay the same regardless of the arity (operator new for instance). 817 if (isa<CXXMethodDecl>(ND)) 818 Arity++; 819 } else 820 Arity = KnownArity; 821 822 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity); 823 break; 824 } 825 826 case DeclarationName::CXXLiteralOperatorName: 827 // FIXME: This mangling is not yet official. 828 Out << "li"; 829 mangleSourceName(Name.getCXXLiteralIdentifier()); 830 break; 831 832 case DeclarationName::CXXUsingDirective: 833 assert(false && "Can't mangle a using directive name!"); 834 break; 835 } 836 } 837 838 void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { 839 // <source-name> ::= <positive length number> <identifier> 840 // <number> ::= [n] <non-negative decimal integer> 841 // <identifier> ::= <unqualified source code identifier> 842 Out << II->getLength() << II->getName(); 843 } 844 845 void CXXNameMangler::mangleNestedName(const NamedDecl *ND, 846 const DeclContext *DC, 847 bool NoFunction) { 848 // <nested-name> 849 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E 850 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> 851 // <template-args> E 852 853 Out << 'N'; 854 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) { 855 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers())); 856 mangleRefQualifier(Method->getRefQualifier()); 857 } 858 859 // Check if we have a template. 860 const TemplateArgumentList *TemplateArgs = 0; 861 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { 862 mangleTemplatePrefix(TD); 863 TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); 864 mangleTemplateArgs(*TemplateParameters, *TemplateArgs); 865 } 866 else { 867 manglePrefix(DC, NoFunction); 868 mangleUnqualifiedName(ND); 869 } 870 871 Out << 'E'; 872 } 873 void CXXNameMangler::mangleNestedName(const TemplateDecl *TD, 874 const TemplateArgument *TemplateArgs, 875 unsigned NumTemplateArgs) { 876 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E 877 878 Out << 'N'; 879 880 mangleTemplatePrefix(TD); 881 TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); 882 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs); 883 884 Out << 'E'; 885 } 886 887 void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { 888 // <local-name> := Z <function encoding> E <entity name> [<discriminator>] 889 // := Z <function encoding> E s [<discriminator>] 890 // <discriminator> := _ <non-negative number> 891 const DeclContext *DC = ND->getDeclContext(); 892 Out << 'Z'; 893 894 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) { 895 mangleObjCMethodName(MD); 896 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) { 897 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext())); 898 Out << 'E'; 899 900 // Mangle the name relative to the closest enclosing function. 901 if (ND == RD) // equality ok because RD derived from ND above 902 mangleUnqualifiedName(ND); 903 else 904 mangleNestedName(ND, DC, true /*NoFunction*/); 905 906 unsigned disc; 907 if (Context.getNextDiscriminator(RD, disc)) { 908 if (disc < 10) 909 Out << '_' << disc; 910 else 911 Out << "__" << disc << '_'; 912 } 913 914 return; 915 } 916 else 917 mangleFunctionEncoding(cast<FunctionDecl>(DC)); 918 919 Out << 'E'; 920 mangleUnqualifiedName(ND); 921 } 922 923 void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) { 924 // <prefix> ::= <prefix> <unqualified-name> 925 // ::= <template-prefix> <template-args> 926 // ::= <template-param> 927 // ::= # empty 928 // ::= <substitution> 929 930 while (isa<LinkageSpecDecl>(DC)) 931 DC = DC->getParent(); 932 933 if (DC->isTranslationUnit()) 934 return; 935 936 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) { 937 manglePrefix(DC->getParent(), NoFunction); 938 llvm::SmallString<64> Name; 939 llvm::raw_svector_ostream NameStream(Name); 940 Context.mangleBlock(Block, NameStream); 941 NameStream.flush(); 942 Out << Name.size() << Name; 943 return; 944 } 945 946 if (mangleSubstitution(cast<NamedDecl>(DC))) 947 return; 948 949 // Check if we have a template. 950 const TemplateArgumentList *TemplateArgs = 0; 951 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) { 952 mangleTemplatePrefix(TD); 953 TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); 954 mangleTemplateArgs(*TemplateParameters, *TemplateArgs); 955 } 956 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) 957 return; 958 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) 959 mangleObjCMethodName(Method); 960 else { 961 manglePrefix(DC->getParent(), NoFunction); 962 mangleUnqualifiedName(cast<NamedDecl>(DC)); 963 } 964 965 addSubstitution(cast<NamedDecl>(DC)); 966 } 967 968 void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) { 969 // <template-prefix> ::= <prefix> <template unqualified-name> 970 // ::= <template-param> 971 // ::= <substitution> 972 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 973 return mangleTemplatePrefix(TD); 974 975 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName()) 976 mangleUnresolvedScope(Qualified->getQualifier()); 977 978 if (OverloadedTemplateStorage *Overloaded 979 = Template.getAsOverloadedTemplate()) { 980 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(), 981 UnknownArity); 982 return; 983 } 984 985 DependentTemplateName *Dependent = Template.getAsDependentTemplateName(); 986 assert(Dependent && "Unknown template name kind?"); 987 mangleUnresolvedScope(Dependent->getQualifier()); 988 mangleUnscopedTemplateName(Template); 989 } 990 991 void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) { 992 // <template-prefix> ::= <prefix> <template unqualified-name> 993 // ::= <template-param> 994 // ::= <substitution> 995 // <template-template-param> ::= <template-param> 996 // <substitution> 997 998 if (mangleSubstitution(ND)) 999 return; 1000 1001 // <template-template-param> ::= <template-param> 1002 if (const TemplateTemplateParmDecl *TTP 1003 = dyn_cast<TemplateTemplateParmDecl>(ND)) { 1004 mangleTemplateParameter(TTP->getIndex()); 1005 return; 1006 } 1007 1008 manglePrefix(ND->getDeclContext()); 1009 mangleUnqualifiedName(ND->getTemplatedDecl()); 1010 addSubstitution(ND); 1011 } 1012 1013 /// Mangles a template name under the production <type>. Required for 1014 /// template template arguments. 1015 /// <type> ::= <class-enum-type> 1016 /// ::= <template-param> 1017 /// ::= <substitution> 1018 void CXXNameMangler::mangleType(TemplateName TN) { 1019 if (mangleSubstitution(TN)) 1020 return; 1021 1022 TemplateDecl *TD = 0; 1023 1024 switch (TN.getKind()) { 1025 case TemplateName::QualifiedTemplate: 1026 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl(); 1027 goto HaveDecl; 1028 1029 case TemplateName::Template: 1030 TD = TN.getAsTemplateDecl(); 1031 goto HaveDecl; 1032 1033 HaveDecl: 1034 if (isa<TemplateTemplateParmDecl>(TD)) 1035 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex()); 1036 else 1037 mangleName(TD); 1038 break; 1039 1040 case TemplateName::OverloadedTemplate: 1041 llvm_unreachable("can't mangle an overloaded template name as a <type>"); 1042 break; 1043 1044 case TemplateName::DependentTemplate: { 1045 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName(); 1046 assert(Dependent->isIdentifier()); 1047 1048 // <class-enum-type> ::= <name> 1049 // <name> ::= <nested-name> 1050 mangleUnresolvedScope(Dependent->getQualifier()); 1051 mangleSourceName(Dependent->getIdentifier()); 1052 break; 1053 } 1054 1055 case TemplateName::SubstTemplateTemplateParmPack: { 1056 SubstTemplateTemplateParmPackStorage *SubstPack 1057 = TN.getAsSubstTemplateTemplateParmPack(); 1058 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex()); 1059 break; 1060 } 1061 } 1062 1063 addSubstitution(TN); 1064 } 1065 1066 void 1067 CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { 1068 switch (OO) { 1069 // <operator-name> ::= nw # new 1070 case OO_New: Out << "nw"; break; 1071 // ::= na # new[] 1072 case OO_Array_New: Out << "na"; break; 1073 // ::= dl # delete 1074 case OO_Delete: Out << "dl"; break; 1075 // ::= da # delete[] 1076 case OO_Array_Delete: Out << "da"; break; 1077 // ::= ps # + (unary) 1078 // ::= pl # + (binary or unknown) 1079 case OO_Plus: 1080 Out << (Arity == 1? "ps" : "pl"); break; 1081 // ::= ng # - (unary) 1082 // ::= mi # - (binary or unknown) 1083 case OO_Minus: 1084 Out << (Arity == 1? "ng" : "mi"); break; 1085 // ::= ad # & (unary) 1086 // ::= an # & (binary or unknown) 1087 case OO_Amp: 1088 Out << (Arity == 1? "ad" : "an"); break; 1089 // ::= de # * (unary) 1090 // ::= ml # * (binary or unknown) 1091 case OO_Star: 1092 // Use binary when unknown. 1093 Out << (Arity == 1? "de" : "ml"); break; 1094 // ::= co # ~ 1095 case OO_Tilde: Out << "co"; break; 1096 // ::= dv # / 1097 case OO_Slash: Out << "dv"; break; 1098 // ::= rm # % 1099 case OO_Percent: Out << "rm"; break; 1100 // ::= or # | 1101 case OO_Pipe: Out << "or"; break; 1102 // ::= eo # ^ 1103 case OO_Caret: Out << "eo"; break; 1104 // ::= aS # = 1105 case OO_Equal: Out << "aS"; break; 1106 // ::= pL # += 1107 case OO_PlusEqual: Out << "pL"; break; 1108 // ::= mI # -= 1109 case OO_MinusEqual: Out << "mI"; break; 1110 // ::= mL # *= 1111 case OO_StarEqual: Out << "mL"; break; 1112 // ::= dV # /= 1113 case OO_SlashEqual: Out << "dV"; break; 1114 // ::= rM # %= 1115 case OO_PercentEqual: Out << "rM"; break; 1116 // ::= aN # &= 1117 case OO_AmpEqual: Out << "aN"; break; 1118 // ::= oR # |= 1119 case OO_PipeEqual: Out << "oR"; break; 1120 // ::= eO # ^= 1121 case OO_CaretEqual: Out << "eO"; break; 1122 // ::= ls # << 1123 case OO_LessLess: Out << "ls"; break; 1124 // ::= rs # >> 1125 case OO_GreaterGreater: Out << "rs"; break; 1126 // ::= lS # <<= 1127 case OO_LessLessEqual: Out << "lS"; break; 1128 // ::= rS # >>= 1129 case OO_GreaterGreaterEqual: Out << "rS"; break; 1130 // ::= eq # == 1131 case OO_EqualEqual: Out << "eq"; break; 1132 // ::= ne # != 1133 case OO_ExclaimEqual: Out << "ne"; break; 1134 // ::= lt # < 1135 case OO_Less: Out << "lt"; break; 1136 // ::= gt # > 1137 case OO_Greater: Out << "gt"; break; 1138 // ::= le # <= 1139 case OO_LessEqual: Out << "le"; break; 1140 // ::= ge # >= 1141 case OO_GreaterEqual: Out << "ge"; break; 1142 // ::= nt # ! 1143 case OO_Exclaim: Out << "nt"; break; 1144 // ::= aa # && 1145 case OO_AmpAmp: Out << "aa"; break; 1146 // ::= oo # || 1147 case OO_PipePipe: Out << "oo"; break; 1148 // ::= pp # ++ 1149 case OO_PlusPlus: Out << "pp"; break; 1150 // ::= mm # -- 1151 case OO_MinusMinus: Out << "mm"; break; 1152 // ::= cm # , 1153 case OO_Comma: Out << "cm"; break; 1154 // ::= pm # ->* 1155 case OO_ArrowStar: Out << "pm"; break; 1156 // ::= pt # -> 1157 case OO_Arrow: Out << "pt"; break; 1158 // ::= cl # () 1159 case OO_Call: Out << "cl"; break; 1160 // ::= ix # [] 1161 case OO_Subscript: Out << "ix"; break; 1162 1163 // ::= qu # ? 1164 // The conditional operator can't be overloaded, but we still handle it when 1165 // mangling expressions. 1166 case OO_Conditional: Out << "qu"; break; 1167 1168 case OO_None: 1169 case NUM_OVERLOADED_OPERATORS: 1170 assert(false && "Not an overloaded operator"); 1171 break; 1172 } 1173 } 1174 1175 void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { 1176 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const 1177 if (Quals.hasRestrict()) 1178 Out << 'r'; 1179 if (Quals.hasVolatile()) 1180 Out << 'V'; 1181 if (Quals.hasConst()) 1182 Out << 'K'; 1183 1184 if (Quals.hasAddressSpace()) { 1185 // Extension: 1186 // 1187 // <type> ::= U <address-space-number> 1188 // 1189 // where <address-space-number> is a source name consisting of 'AS' 1190 // followed by the address space <number>. 1191 llvm::SmallString<64> ASString; 1192 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace()); 1193 Out << 'U' << ASString.size() << ASString; 1194 } 1195 1196 // FIXME: For now, just drop all extension qualifiers on the floor. 1197 } 1198 1199 void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) { 1200 // <ref-qualifier> ::= R # lvalue reference 1201 // ::= O # rvalue-reference 1202 // Proposal to Itanium C++ ABI list on 1/26/11 1203 switch (RefQualifier) { 1204 case RQ_None: 1205 break; 1206 1207 case RQ_LValue: 1208 Out << 'R'; 1209 break; 1210 1211 case RQ_RValue: 1212 Out << 'O'; 1213 break; 1214 } 1215 } 1216 1217 void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { 1218 Context.mangleObjCMethodName(MD, Out); 1219 } 1220 1221 void CXXNameMangler::mangleType(QualType nonCanon) { 1222 // Only operate on the canonical type! 1223 QualType canon = nonCanon.getCanonicalType(); 1224 1225 SplitQualType split = canon.split(); 1226 Qualifiers quals = split.second; 1227 const Type *ty = split.first; 1228 1229 bool isSubstitutable = quals || !isa<BuiltinType>(ty); 1230 if (isSubstitutable && mangleSubstitution(canon)) 1231 return; 1232 1233 // If we're mangling a qualified array type, push the qualifiers to 1234 // the element type. 1235 if (quals && isa<ArrayType>(ty)) { 1236 ty = Context.getASTContext().getAsArrayType(canon); 1237 quals = Qualifiers(); 1238 1239 // Note that we don't update canon: we want to add the 1240 // substitution at the canonical type. 1241 } 1242 1243 if (quals) { 1244 mangleQualifiers(quals); 1245 // Recurse: even if the qualified type isn't yet substitutable, 1246 // the unqualified type might be. 1247 mangleType(QualType(ty, 0)); 1248 } else { 1249 switch (ty->getTypeClass()) { 1250 #define ABSTRACT_TYPE(CLASS, PARENT) 1251 #define NON_CANONICAL_TYPE(CLASS, PARENT) \ 1252 case Type::CLASS: \ 1253 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ 1254 return; 1255 #define TYPE(CLASS, PARENT) \ 1256 case Type::CLASS: \ 1257 mangleType(static_cast<const CLASS##Type*>(ty)); \ 1258 break; 1259 #include "clang/AST/TypeNodes.def" 1260 } 1261 } 1262 1263 // Add the substitution. 1264 if (isSubstitutable) 1265 addSubstitution(canon); 1266 } 1267 1268 void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) { 1269 if (!mangleStandardSubstitution(ND)) 1270 mangleName(ND); 1271 } 1272 1273 void CXXNameMangler::mangleType(const BuiltinType *T) { 1274 // <type> ::= <builtin-type> 1275 // <builtin-type> ::= v # void 1276 // ::= w # wchar_t 1277 // ::= b # bool 1278 // ::= c # char 1279 // ::= a # signed char 1280 // ::= h # unsigned char 1281 // ::= s # short 1282 // ::= t # unsigned short 1283 // ::= i # int 1284 // ::= j # unsigned int 1285 // ::= l # long 1286 // ::= m # unsigned long 1287 // ::= x # long long, __int64 1288 // ::= y # unsigned long long, __int64 1289 // ::= n # __int128 1290 // UNSUPPORTED: ::= o # unsigned __int128 1291 // ::= f # float 1292 // ::= d # double 1293 // ::= e # long double, __float80 1294 // UNSUPPORTED: ::= g # __float128 1295 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) 1296 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) 1297 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) 1298 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) 1299 // ::= Di # char32_t 1300 // ::= Ds # char16_t 1301 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr)) 1302 // ::= u <source-name> # vendor extended type 1303 switch (T->getKind()) { 1304 case BuiltinType::Void: Out << 'v'; break; 1305 case BuiltinType::Bool: Out << 'b'; break; 1306 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; 1307 case BuiltinType::UChar: Out << 'h'; break; 1308 case BuiltinType::UShort: Out << 't'; break; 1309 case BuiltinType::UInt: Out << 'j'; break; 1310 case BuiltinType::ULong: Out << 'm'; break; 1311 case BuiltinType::ULongLong: Out << 'y'; break; 1312 case BuiltinType::UInt128: Out << 'o'; break; 1313 case BuiltinType::SChar: Out << 'a'; break; 1314 case BuiltinType::WChar_S: 1315 case BuiltinType::WChar_U: Out << 'w'; break; 1316 case BuiltinType::Char16: Out << "Ds"; break; 1317 case BuiltinType::Char32: Out << "Di"; break; 1318 case BuiltinType::Short: Out << 's'; break; 1319 case BuiltinType::Int: Out << 'i'; break; 1320 case BuiltinType::Long: Out << 'l'; break; 1321 case BuiltinType::LongLong: Out << 'x'; break; 1322 case BuiltinType::Int128: Out << 'n'; break; 1323 case BuiltinType::Float: Out << 'f'; break; 1324 case BuiltinType::Double: Out << 'd'; break; 1325 case BuiltinType::LongDouble: Out << 'e'; break; 1326 case BuiltinType::NullPtr: Out << "Dn"; break; 1327 1328 case BuiltinType::Overload: 1329 case BuiltinType::Dependent: 1330 case BuiltinType::UnknownAny: 1331 assert(false && 1332 "Overloaded and dependent types shouldn't get to name mangling"); 1333 break; 1334 case BuiltinType::ObjCId: Out << "11objc_object"; break; 1335 case BuiltinType::ObjCClass: Out << "10objc_class"; break; 1336 case BuiltinType::ObjCSel: Out << "13objc_selector"; break; 1337 } 1338 } 1339 1340 // <type> ::= <function-type> 1341 // <function-type> ::= F [Y] <bare-function-type> E 1342 void CXXNameMangler::mangleType(const FunctionProtoType *T) { 1343 Out << 'F'; 1344 // FIXME: We don't have enough information in the AST to produce the 'Y' 1345 // encoding for extern "C" function types. 1346 mangleBareFunctionType(T, /*MangleReturnType=*/true); 1347 Out << 'E'; 1348 } 1349 void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { 1350 llvm_unreachable("Can't mangle K&R function prototypes"); 1351 } 1352 void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, 1353 bool MangleReturnType) { 1354 // We should never be mangling something without a prototype. 1355 const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 1356 1357 // <bare-function-type> ::= <signature type>+ 1358 if (MangleReturnType) 1359 mangleType(Proto->getResultType()); 1360 1361 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { 1362 // <builtin-type> ::= v # void 1363 Out << 'v'; 1364 return; 1365 } 1366 1367 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), 1368 ArgEnd = Proto->arg_type_end(); 1369 Arg != ArgEnd; ++Arg) 1370 mangleType(*Arg); 1371 1372 // <builtin-type> ::= z # ellipsis 1373 if (Proto->isVariadic()) 1374 Out << 'z'; 1375 } 1376 1377 // <type> ::= <class-enum-type> 1378 // <class-enum-type> ::= <name> 1379 void CXXNameMangler::mangleType(const UnresolvedUsingType *T) { 1380 mangleName(T->getDecl()); 1381 } 1382 1383 // <type> ::= <class-enum-type> 1384 // <class-enum-type> ::= <name> 1385 void CXXNameMangler::mangleType(const EnumType *T) { 1386 mangleType(static_cast<const TagType*>(T)); 1387 } 1388 void CXXNameMangler::mangleType(const RecordType *T) { 1389 mangleType(static_cast<const TagType*>(T)); 1390 } 1391 void CXXNameMangler::mangleType(const TagType *T) { 1392 mangleName(T->getDecl()); 1393 } 1394 1395 // <type> ::= <array-type> 1396 // <array-type> ::= A <positive dimension number> _ <element type> 1397 // ::= A [<dimension expression>] _ <element type> 1398 void CXXNameMangler::mangleType(const ConstantArrayType *T) { 1399 Out << 'A' << T->getSize() << '_'; 1400 mangleType(T->getElementType()); 1401 } 1402 void CXXNameMangler::mangleType(const VariableArrayType *T) { 1403 Out << 'A'; 1404 // decayed vla types (size 0) will just be skipped. 1405 if (T->getSizeExpr()) 1406 mangleExpression(T->getSizeExpr()); 1407 Out << '_'; 1408 mangleType(T->getElementType()); 1409 } 1410 void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { 1411 Out << 'A'; 1412 mangleExpression(T->getSizeExpr()); 1413 Out << '_'; 1414 mangleType(T->getElementType()); 1415 } 1416 void CXXNameMangler::mangleType(const IncompleteArrayType *T) { 1417 Out << "A_"; 1418 mangleType(T->getElementType()); 1419 } 1420 1421 // <type> ::= <pointer-to-member-type> 1422 // <pointer-to-member-type> ::= M <class type> <member type> 1423 void CXXNameMangler::mangleType(const MemberPointerType *T) { 1424 Out << 'M'; 1425 mangleType(QualType(T->getClass(), 0)); 1426 QualType PointeeType = T->getPointeeType(); 1427 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { 1428 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals())); 1429 mangleRefQualifier(FPT->getRefQualifier()); 1430 mangleType(FPT); 1431 1432 // Itanium C++ ABI 5.1.8: 1433 // 1434 // The type of a non-static member function is considered to be different, 1435 // for the purposes of substitution, from the type of a namespace-scope or 1436 // static member function whose type appears similar. The types of two 1437 // non-static member functions are considered to be different, for the 1438 // purposes of substitution, if the functions are members of different 1439 // classes. In other words, for the purposes of substitution, the class of 1440 // which the function is a member is considered part of the type of 1441 // function. 1442 1443 // We increment the SeqID here to emulate adding an entry to the 1444 // substitution table. We can't actually add it because we don't want this 1445 // particular function type to be substituted. 1446 ++SeqID; 1447 } else 1448 mangleType(PointeeType); 1449 } 1450 1451 // <type> ::= <template-param> 1452 void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { 1453 mangleTemplateParameter(T->getIndex()); 1454 } 1455 1456 // <type> ::= <template-param> 1457 void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) { 1458 mangleTemplateParameter(T->getReplacedParameter()->getIndex()); 1459 } 1460 1461 // <type> ::= P <type> # pointer-to 1462 void CXXNameMangler::mangleType(const PointerType *T) { 1463 Out << 'P'; 1464 mangleType(T->getPointeeType()); 1465 } 1466 void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { 1467 Out << 'P'; 1468 mangleType(T->getPointeeType()); 1469 } 1470 1471 // <type> ::= R <type> # reference-to 1472 void CXXNameMangler::mangleType(const LValueReferenceType *T) { 1473 Out << 'R'; 1474 mangleType(T->getPointeeType()); 1475 } 1476 1477 // <type> ::= O <type> # rvalue reference-to (C++0x) 1478 void CXXNameMangler::mangleType(const RValueReferenceType *T) { 1479 Out << 'O'; 1480 mangleType(T->getPointeeType()); 1481 } 1482 1483 // <type> ::= C <type> # complex pair (C 2000) 1484 void CXXNameMangler::mangleType(const ComplexType *T) { 1485 Out << 'C'; 1486 mangleType(T->getElementType()); 1487 } 1488 1489 // ARM's ABI for Neon vector types specifies that they should be mangled as 1490 // if they are structs (to match ARM's initial implementation). The 1491 // vector type must be one of the special types predefined by ARM. 1492 void CXXNameMangler::mangleNeonVectorType(const VectorType *T) { 1493 QualType EltType = T->getElementType(); 1494 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType"); 1495 const char *EltName = 0; 1496 if (T->getVectorKind() == VectorType::NeonPolyVector) { 1497 switch (cast<BuiltinType>(EltType)->getKind()) { 1498 case BuiltinType::SChar: EltName = "poly8_t"; break; 1499 case BuiltinType::Short: EltName = "poly16_t"; break; 1500 default: llvm_unreachable("unexpected Neon polynomial vector element type"); 1501 } 1502 } else { 1503 switch (cast<BuiltinType>(EltType)->getKind()) { 1504 case BuiltinType::SChar: EltName = "int8_t"; break; 1505 case BuiltinType::UChar: EltName = "uint8_t"; break; 1506 case BuiltinType::Short: EltName = "int16_t"; break; 1507 case BuiltinType::UShort: EltName = "uint16_t"; break; 1508 case BuiltinType::Int: EltName = "int32_t"; break; 1509 case BuiltinType::UInt: EltName = "uint32_t"; break; 1510 case BuiltinType::LongLong: EltName = "int64_t"; break; 1511 case BuiltinType::ULongLong: EltName = "uint64_t"; break; 1512 case BuiltinType::Float: EltName = "float32_t"; break; 1513 default: llvm_unreachable("unexpected Neon vector element type"); 1514 } 1515 } 1516 const char *BaseName = 0; 1517 unsigned BitSize = (T->getNumElements() * 1518 getASTContext().getTypeSize(EltType)); 1519 if (BitSize == 64) 1520 BaseName = "__simd64_"; 1521 else { 1522 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits"); 1523 BaseName = "__simd128_"; 1524 } 1525 Out << strlen(BaseName) + strlen(EltName); 1526 Out << BaseName << EltName; 1527 } 1528 1529 // GNU extension: vector types 1530 // <type> ::= <vector-type> 1531 // <vector-type> ::= Dv <positive dimension number> _ 1532 // <extended element type> 1533 // ::= Dv [<dimension expression>] _ <element type> 1534 // <extended element type> ::= <element type> 1535 // ::= p # AltiVec vector pixel 1536 void CXXNameMangler::mangleType(const VectorType *T) { 1537 if ((T->getVectorKind() == VectorType::NeonVector || 1538 T->getVectorKind() == VectorType::NeonPolyVector)) { 1539 mangleNeonVectorType(T); 1540 return; 1541 } 1542 Out << "Dv" << T->getNumElements() << '_'; 1543 if (T->getVectorKind() == VectorType::AltiVecPixel) 1544 Out << 'p'; 1545 else if (T->getVectorKind() == VectorType::AltiVecBool) 1546 Out << 'b'; 1547 else 1548 mangleType(T->getElementType()); 1549 } 1550 void CXXNameMangler::mangleType(const ExtVectorType *T) { 1551 mangleType(static_cast<const VectorType*>(T)); 1552 } 1553 void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { 1554 Out << "Dv"; 1555 mangleExpression(T->getSizeExpr()); 1556 Out << '_'; 1557 mangleType(T->getElementType()); 1558 } 1559 1560 void CXXNameMangler::mangleType(const PackExpansionType *T) { 1561 // <type> ::= Dp <type> # pack expansion (C++0x) 1562 Out << "Dp"; 1563 mangleType(T->getPattern()); 1564 } 1565 1566 void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { 1567 mangleSourceName(T->getDecl()->getIdentifier()); 1568 } 1569 1570 void CXXNameMangler::mangleType(const ObjCObjectType *T) { 1571 // We don't allow overloading by different protocol qualification, 1572 // so mangling them isn't necessary. 1573 mangleType(T->getBaseType()); 1574 } 1575 1576 void CXXNameMangler::mangleType(const BlockPointerType *T) { 1577 Out << "U13block_pointer"; 1578 mangleType(T->getPointeeType()); 1579 } 1580 1581 void CXXNameMangler::mangleType(const InjectedClassNameType *T) { 1582 // Mangle injected class name types as if the user had written the 1583 // specialization out fully. It may not actually be possible to see 1584 // this mangling, though. 1585 mangleType(T->getInjectedSpecializationType()); 1586 } 1587 1588 void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { 1589 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) { 1590 mangleName(TD, T->getArgs(), T->getNumArgs()); 1591 } else { 1592 if (mangleSubstitution(QualType(T, 0))) 1593 return; 1594 1595 mangleTemplatePrefix(T->getTemplateName()); 1596 1597 // FIXME: GCC does not appear to mangle the template arguments when 1598 // the template in question is a dependent template name. Should we 1599 // emulate that badness? 1600 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs()); 1601 addSubstitution(QualType(T, 0)); 1602 } 1603 } 1604 1605 void CXXNameMangler::mangleType(const DependentNameType *T) { 1606 // Typename types are always nested 1607 Out << 'N'; 1608 mangleUnresolvedScope(T->getQualifier()); 1609 mangleSourceName(T->getIdentifier()); 1610 Out << 'E'; 1611 } 1612 1613 void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) { 1614 // Dependently-scoped template types are nested if they have a prefix. 1615 Out << 'N'; 1616 1617 // TODO: avoid making this TemplateName. 1618 TemplateName Prefix = 1619 getASTContext().getDependentTemplateName(T->getQualifier(), 1620 T->getIdentifier()); 1621 mangleTemplatePrefix(Prefix); 1622 1623 // FIXME: GCC does not appear to mangle the template arguments when 1624 // the template in question is a dependent template name. Should we 1625 // emulate that badness? 1626 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs()); 1627 Out << 'E'; 1628 } 1629 1630 void CXXNameMangler::mangleType(const TypeOfType *T) { 1631 // FIXME: this is pretty unsatisfactory, but there isn't an obvious 1632 // "extension with parameters" mangling. 1633 Out << "u6typeof"; 1634 } 1635 1636 void CXXNameMangler::mangleType(const TypeOfExprType *T) { 1637 // FIXME: this is pretty unsatisfactory, but there isn't an obvious 1638 // "extension with parameters" mangling. 1639 Out << "u6typeof"; 1640 } 1641 1642 void CXXNameMangler::mangleType(const DecltypeType *T) { 1643 Expr *E = T->getUnderlyingExpr(); 1644 1645 // type ::= Dt <expression> E # decltype of an id-expression 1646 // # or class member access 1647 // ::= DT <expression> E # decltype of an expression 1648 1649 // This purports to be an exhaustive list of id-expressions and 1650 // class member accesses. Note that we do not ignore parentheses; 1651 // parentheses change the semantics of decltype for these 1652 // expressions (and cause the mangler to use the other form). 1653 if (isa<DeclRefExpr>(E) || 1654 isa<MemberExpr>(E) || 1655 isa<UnresolvedLookupExpr>(E) || 1656 isa<DependentScopeDeclRefExpr>(E) || 1657 isa<CXXDependentScopeMemberExpr>(E) || 1658 isa<UnresolvedMemberExpr>(E)) 1659 Out << "Dt"; 1660 else 1661 Out << "DT"; 1662 mangleExpression(E); 1663 Out << 'E'; 1664 } 1665 1666 void CXXNameMangler::mangleType(const AutoType *T) { 1667 QualType D = T->getDeducedType(); 1668 // <builtin-type> ::= Da # dependent auto 1669 if (D.isNull()) 1670 Out << "Da"; 1671 else 1672 mangleType(D); 1673 } 1674 1675 void CXXNameMangler::mangleIntegerLiteral(QualType T, 1676 const llvm::APSInt &Value) { 1677 // <expr-primary> ::= L <type> <value number> E # integer literal 1678 Out << 'L'; 1679 1680 mangleType(T); 1681 if (T->isBooleanType()) { 1682 // Boolean values are encoded as 0/1. 1683 Out << (Value.getBoolValue() ? '1' : '0'); 1684 } else { 1685 mangleNumber(Value); 1686 } 1687 Out << 'E'; 1688 1689 } 1690 1691 /// Mangles a member expression. Implicit accesses are not handled, 1692 /// but that should be okay, because you shouldn't be able to 1693 /// make an implicit access in a function template declaration. 1694 void CXXNameMangler::mangleMemberExpr(const Expr *Base, 1695 bool IsArrow, 1696 NestedNameSpecifier *Qualifier, 1697 DeclarationName Member, 1698 unsigned Arity) { 1699 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable. 1700 // OTOH, gcc also mangles the name as an expression. 1701 Out << (IsArrow ? "pt" : "dt"); 1702 mangleExpression(Base); 1703 mangleUnresolvedName(Qualifier, Member, Arity); 1704 } 1705 1706 void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) { 1707 // <expression> ::= <unary operator-name> <expression> 1708 // ::= <binary operator-name> <expression> <expression> 1709 // ::= <trinary operator-name> <expression> <expression> <expression> 1710 // ::= cl <expression>* E # call 1711 // ::= cv <type> expression # conversion with one argument 1712 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments 1713 // ::= st <type> # sizeof (a type) 1714 // ::= at <type> # alignof (a type) 1715 // ::= <template-param> 1716 // ::= <function-param> 1717 // ::= sr <type> <unqualified-name> # dependent name 1718 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id 1719 // ::= sZ <template-param> # size of a parameter pack 1720 // ::= sZ <function-param> # size of a function parameter pack 1721 // ::= <expr-primary> 1722 // <expr-primary> ::= L <type> <value number> E # integer literal 1723 // ::= L <type <value float> E # floating literal 1724 // ::= L <mangled-name> E # external name 1725 switch (E->getStmtClass()) { 1726 case Expr::NoStmtClass: 1727 #define ABSTRACT_STMT(Type) 1728 #define EXPR(Type, Base) 1729 #define STMT(Type, Base) \ 1730 case Expr::Type##Class: 1731 #include "clang/AST/StmtNodes.inc" 1732 // fallthrough 1733 1734 // These all can only appear in local or variable-initialization 1735 // contexts and so should never appear in a mangling. 1736 case Expr::AddrLabelExprClass: 1737 case Expr::BlockDeclRefExprClass: 1738 case Expr::CXXThisExprClass: 1739 case Expr::DesignatedInitExprClass: 1740 case Expr::ImplicitValueInitExprClass: 1741 case Expr::InitListExprClass: 1742 case Expr::ParenListExprClass: 1743 case Expr::CXXScalarValueInitExprClass: 1744 llvm_unreachable("unexpected statement kind"); 1745 break; 1746 1747 // FIXME: invent manglings for all these. 1748 case Expr::BlockExprClass: 1749 case Expr::CXXPseudoDestructorExprClass: 1750 case Expr::ChooseExprClass: 1751 case Expr::CompoundLiteralExprClass: 1752 case Expr::ExtVectorElementExprClass: 1753 case Expr::GenericSelectionExprClass: 1754 case Expr::ObjCEncodeExprClass: 1755 case Expr::ObjCIsaExprClass: 1756 case Expr::ObjCIvarRefExprClass: 1757 case Expr::ObjCMessageExprClass: 1758 case Expr::ObjCPropertyRefExprClass: 1759 case Expr::ObjCProtocolExprClass: 1760 case Expr::ObjCSelectorExprClass: 1761 case Expr::ObjCStringLiteralClass: 1762 case Expr::OffsetOfExprClass: 1763 case Expr::PredefinedExprClass: 1764 case Expr::ShuffleVectorExprClass: 1765 case Expr::StmtExprClass: 1766 case Expr::UnaryTypeTraitExprClass: 1767 case Expr::BinaryTypeTraitExprClass: 1768 case Expr::VAArgExprClass: 1769 case Expr::CXXUuidofExprClass: 1770 case Expr::CXXNoexceptExprClass: 1771 case Expr::CUDAKernelCallExprClass: { 1772 // As bad as this diagnostic is, it's better than crashing. 1773 Diagnostic &Diags = Context.getDiags(); 1774 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, 1775 "cannot yet mangle expression type %0"); 1776 Diags.Report(E->getExprLoc(), DiagID) 1777 << E->getStmtClassName() << E->getSourceRange(); 1778 break; 1779 } 1780 1781 // Even gcc-4.5 doesn't mangle this. 1782 case Expr::BinaryConditionalOperatorClass: { 1783 Diagnostic &Diags = Context.getDiags(); 1784 unsigned DiagID = 1785 Diags.getCustomDiagID(Diagnostic::Error, 1786 "?: operator with omitted middle operand cannot be mangled"); 1787 Diags.Report(E->getExprLoc(), DiagID) 1788 << E->getStmtClassName() << E->getSourceRange(); 1789 break; 1790 } 1791 1792 // These are used for internal purposes and cannot be meaningfully mangled. 1793 case Expr::OpaqueValueExprClass: 1794 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?"); 1795 1796 case Expr::CXXDefaultArgExprClass: 1797 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity); 1798 break; 1799 1800 case Expr::CXXMemberCallExprClass: // fallthrough 1801 case Expr::CallExprClass: { 1802 const CallExpr *CE = cast<CallExpr>(E); 1803 Out << "cl"; 1804 mangleExpression(CE->getCallee(), CE->getNumArgs()); 1805 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I) 1806 mangleExpression(CE->getArg(I)); 1807 Out << 'E'; 1808 break; 1809 } 1810 1811 case Expr::CXXNewExprClass: { 1812 // Proposal from David Vandervoorde, 2010.06.30 1813 const CXXNewExpr *New = cast<CXXNewExpr>(E); 1814 if (New->isGlobalNew()) Out << "gs"; 1815 Out << (New->isArray() ? "na" : "nw"); 1816 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(), 1817 E = New->placement_arg_end(); I != E; ++I) 1818 mangleExpression(*I); 1819 Out << '_'; 1820 mangleType(New->getAllocatedType()); 1821 if (New->hasInitializer()) { 1822 Out << "pi"; 1823 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(), 1824 E = New->constructor_arg_end(); I != E; ++I) 1825 mangleExpression(*I); 1826 } 1827 Out << 'E'; 1828 break; 1829 } 1830 1831 case Expr::MemberExprClass: { 1832 const MemberExpr *ME = cast<MemberExpr>(E); 1833 mangleMemberExpr(ME->getBase(), ME->isArrow(), 1834 ME->getQualifier(), ME->getMemberDecl()->getDeclName(), 1835 Arity); 1836 break; 1837 } 1838 1839 case Expr::UnresolvedMemberExprClass: { 1840 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E); 1841 mangleMemberExpr(ME->getBase(), ME->isArrow(), 1842 ME->getQualifier(), ME->getMemberName(), 1843 Arity); 1844 if (ME->hasExplicitTemplateArgs()) 1845 mangleTemplateArgs(ME->getExplicitTemplateArgs()); 1846 break; 1847 } 1848 1849 case Expr::CXXDependentScopeMemberExprClass: { 1850 const CXXDependentScopeMemberExpr *ME 1851 = cast<CXXDependentScopeMemberExpr>(E); 1852 mangleMemberExpr(ME->getBase(), ME->isArrow(), 1853 ME->getQualifier(), ME->getMember(), 1854 Arity); 1855 if (ME->hasExplicitTemplateArgs()) 1856 mangleTemplateArgs(ME->getExplicitTemplateArgs()); 1857 break; 1858 } 1859 1860 case Expr::UnresolvedLookupExprClass: { 1861 // The ABI doesn't cover how to mangle overload sets, so we mangle 1862 // using something as close as possible to the original lookup 1863 // expression. 1864 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E); 1865 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity); 1866 if (ULE->hasExplicitTemplateArgs()) 1867 mangleTemplateArgs(ULE->getExplicitTemplateArgs()); 1868 break; 1869 } 1870 1871 case Expr::CXXUnresolvedConstructExprClass: { 1872 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E); 1873 unsigned N = CE->arg_size(); 1874 1875 Out << "cv"; 1876 mangleType(CE->getType()); 1877 if (N != 1) Out << '_'; 1878 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); 1879 if (N != 1) Out << 'E'; 1880 break; 1881 } 1882 1883 case Expr::CXXTemporaryObjectExprClass: 1884 case Expr::CXXConstructExprClass: { 1885 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E); 1886 unsigned N = CE->getNumArgs(); 1887 1888 Out << "cv"; 1889 mangleType(CE->getType()); 1890 if (N != 1) Out << '_'; 1891 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); 1892 if (N != 1) Out << 'E'; 1893 break; 1894 } 1895 1896 case Expr::UnaryExprOrTypeTraitExprClass: { 1897 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E); 1898 switch(SAE->getKind()) { 1899 case UETT_SizeOf: 1900 Out << 's'; 1901 break; 1902 case UETT_AlignOf: 1903 Out << 'a'; 1904 break; 1905 case UETT_VecStep: 1906 Diagnostic &Diags = Context.getDiags(); 1907 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, 1908 "cannot yet mangle vec_step expression"); 1909 Diags.Report(DiagID); 1910 return; 1911 } 1912 if (SAE->isArgumentType()) { 1913 Out << 't'; 1914 mangleType(SAE->getArgumentType()); 1915 } else { 1916 Out << 'z'; 1917 mangleExpression(SAE->getArgumentExpr()); 1918 } 1919 break; 1920 } 1921 1922 case Expr::CXXThrowExprClass: { 1923 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E); 1924 1925 // Proposal from David Vandervoorde, 2010.06.30 1926 if (TE->getSubExpr()) { 1927 Out << "tw"; 1928 mangleExpression(TE->getSubExpr()); 1929 } else { 1930 Out << "tr"; 1931 } 1932 break; 1933 } 1934 1935 case Expr::CXXTypeidExprClass: { 1936 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E); 1937 1938 // Proposal from David Vandervoorde, 2010.06.30 1939 if (TIE->isTypeOperand()) { 1940 Out << "ti"; 1941 mangleType(TIE->getTypeOperand()); 1942 } else { 1943 Out << "te"; 1944 mangleExpression(TIE->getExprOperand()); 1945 } 1946 break; 1947 } 1948 1949 case Expr::CXXDeleteExprClass: { 1950 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E); 1951 1952 // Proposal from David Vandervoorde, 2010.06.30 1953 if (DE->isGlobalDelete()) Out << "gs"; 1954 Out << (DE->isArrayForm() ? "da" : "dl"); 1955 mangleExpression(DE->getArgument()); 1956 break; 1957 } 1958 1959 case Expr::UnaryOperatorClass: { 1960 const UnaryOperator *UO = cast<UnaryOperator>(E); 1961 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()), 1962 /*Arity=*/1); 1963 mangleExpression(UO->getSubExpr()); 1964 break; 1965 } 1966 1967 case Expr::ArraySubscriptExprClass: { 1968 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E); 1969 1970 // Array subscript is treated as a syntactically wierd form of 1971 // binary operator. 1972 Out << "ix"; 1973 mangleExpression(AE->getLHS()); 1974 mangleExpression(AE->getRHS()); 1975 break; 1976 } 1977 1978 case Expr::CompoundAssignOperatorClass: // fallthrough 1979 case Expr::BinaryOperatorClass: { 1980 const BinaryOperator *BO = cast<BinaryOperator>(E); 1981 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()), 1982 /*Arity=*/2); 1983 mangleExpression(BO->getLHS()); 1984 mangleExpression(BO->getRHS()); 1985 break; 1986 } 1987 1988 case Expr::ConditionalOperatorClass: { 1989 const ConditionalOperator *CO = cast<ConditionalOperator>(E); 1990 mangleOperatorName(OO_Conditional, /*Arity=*/3); 1991 mangleExpression(CO->getCond()); 1992 mangleExpression(CO->getLHS(), Arity); 1993 mangleExpression(CO->getRHS(), Arity); 1994 break; 1995 } 1996 1997 case Expr::ImplicitCastExprClass: { 1998 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity); 1999 break; 2000 } 2001 2002 case Expr::CStyleCastExprClass: 2003 case Expr::CXXStaticCastExprClass: 2004 case Expr::CXXDynamicCastExprClass: 2005 case Expr::CXXReinterpretCastExprClass: 2006 case Expr::CXXConstCastExprClass: 2007 case Expr::CXXFunctionalCastExprClass: { 2008 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E); 2009 Out << "cv"; 2010 mangleType(ECE->getType()); 2011 mangleExpression(ECE->getSubExpr()); 2012 break; 2013 } 2014 2015 case Expr::CXXOperatorCallExprClass: { 2016 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E); 2017 unsigned NumArgs = CE->getNumArgs(); 2018 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs); 2019 // Mangle the arguments. 2020 for (unsigned i = 0; i != NumArgs; ++i) 2021 mangleExpression(CE->getArg(i)); 2022 break; 2023 } 2024 2025 case Expr::ParenExprClass: 2026 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity); 2027 break; 2028 2029 case Expr::DeclRefExprClass: { 2030 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); 2031 2032 switch (D->getKind()) { 2033 default: 2034 // <expr-primary> ::= L <mangled-name> E # external name 2035 Out << 'L'; 2036 mangle(D, "_Z"); 2037 Out << 'E'; 2038 break; 2039 2040 case Decl::EnumConstant: { 2041 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D); 2042 mangleIntegerLiteral(ED->getType(), ED->getInitVal()); 2043 break; 2044 } 2045 2046 case Decl::NonTypeTemplateParm: { 2047 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D); 2048 mangleTemplateParameter(PD->getIndex()); 2049 break; 2050 } 2051 2052 } 2053 2054 break; 2055 } 2056 2057 case Expr::SubstNonTypeTemplateParmPackExprClass: 2058 mangleTemplateParameter( 2059 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex()); 2060 break; 2061 2062 case Expr::DependentScopeDeclRefExprClass: { 2063 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E); 2064 NestedNameSpecifier *NNS = DRE->getQualifier(); 2065 const Type *QTy = NNS->getAsType(); 2066 2067 // When we're dealing with a nested-name-specifier that has just a 2068 // dependent identifier in it, mangle that as a typename. FIXME: 2069 // It isn't clear that we ever actually want to have such a 2070 // nested-name-specifier; why not just represent it as a typename type? 2071 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) { 2072 QTy = getASTContext().getDependentNameType(ETK_Typename, 2073 NNS->getPrefix(), 2074 NNS->getAsIdentifier()) 2075 .getTypePtr(); 2076 } 2077 assert(QTy && "Qualifier was not type!"); 2078 2079 // ::= sr <type> <unqualified-name> # dependent name 2080 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id 2081 Out << "sr"; 2082 mangleType(QualType(QTy, 0)); 2083 mangleUnqualifiedName(0, DRE->getDeclName(), Arity); 2084 if (DRE->hasExplicitTemplateArgs()) 2085 mangleTemplateArgs(DRE->getExplicitTemplateArgs()); 2086 2087 break; 2088 } 2089 2090 case Expr::CXXBindTemporaryExprClass: 2091 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr()); 2092 break; 2093 2094 case Expr::ExprWithCleanupsClass: 2095 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity); 2096 break; 2097 2098 case Expr::FloatingLiteralClass: { 2099 const FloatingLiteral *FL = cast<FloatingLiteral>(E); 2100 Out << 'L'; 2101 mangleType(FL->getType()); 2102 mangleFloat(FL->getValue()); 2103 Out << 'E'; 2104 break; 2105 } 2106 2107 case Expr::CharacterLiteralClass: 2108 Out << 'L'; 2109 mangleType(E->getType()); 2110 Out << cast<CharacterLiteral>(E)->getValue(); 2111 Out << 'E'; 2112 break; 2113 2114 case Expr::CXXBoolLiteralExprClass: 2115 Out << "Lb"; 2116 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0'); 2117 Out << 'E'; 2118 break; 2119 2120 case Expr::IntegerLiteralClass: { 2121 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue()); 2122 if (E->getType()->isSignedIntegerType()) 2123 Value.setIsSigned(true); 2124 mangleIntegerLiteral(E->getType(), Value); 2125 break; 2126 } 2127 2128 case Expr::ImaginaryLiteralClass: { 2129 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E); 2130 // Mangle as if a complex literal. 2131 // Proposal from David Vandevoorde, 2010.06.30. 2132 Out << 'L'; 2133 mangleType(E->getType()); 2134 if (const FloatingLiteral *Imag = 2135 dyn_cast<FloatingLiteral>(IE->getSubExpr())) { 2136 // Mangle a floating-point zero of the appropriate type. 2137 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics())); 2138 Out << '_'; 2139 mangleFloat(Imag->getValue()); 2140 } else { 2141 Out << "0_"; 2142 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue()); 2143 if (IE->getSubExpr()->getType()->isSignedIntegerType()) 2144 Value.setIsSigned(true); 2145 mangleNumber(Value); 2146 } 2147 Out << 'E'; 2148 break; 2149 } 2150 2151 case Expr::StringLiteralClass: { 2152 // Revised proposal from David Vandervoorde, 2010.07.15. 2153 Out << 'L'; 2154 assert(isa<ConstantArrayType>(E->getType())); 2155 mangleType(E->getType()); 2156 Out << 'E'; 2157 break; 2158 } 2159 2160 case Expr::GNUNullExprClass: 2161 // FIXME: should this really be mangled the same as nullptr? 2162 // fallthrough 2163 2164 case Expr::CXXNullPtrLiteralExprClass: { 2165 // Proposal from David Vandervoorde, 2010.06.30, as 2166 // modified by ABI list discussion. 2167 Out << "LDnE"; 2168 break; 2169 } 2170 2171 case Expr::PackExpansionExprClass: 2172 Out << "sp"; 2173 mangleExpression(cast<PackExpansionExpr>(E)->getPattern()); 2174 break; 2175 2176 case Expr::SizeOfPackExprClass: { 2177 Out << "sZ"; 2178 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack(); 2179 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack)) 2180 mangleTemplateParameter(TTP->getIndex()); 2181 else if (const NonTypeTemplateParmDecl *NTTP 2182 = dyn_cast<NonTypeTemplateParmDecl>(Pack)) 2183 mangleTemplateParameter(NTTP->getIndex()); 2184 else if (const TemplateTemplateParmDecl *TempTP 2185 = dyn_cast<TemplateTemplateParmDecl>(Pack)) 2186 mangleTemplateParameter(TempTP->getIndex()); 2187 else { 2188 // Note: proposed by Mike Herrick on 11/30/10 2189 // <expression> ::= sZ <function-param> # size of function parameter pack 2190 Diagnostic &Diags = Context.getDiags(); 2191 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, 2192 "cannot mangle sizeof...(function parameter pack)"); 2193 Diags.Report(DiagID); 2194 return; 2195 } 2196 break; 2197 } 2198 } 2199 } 2200 2201 void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { 2202 // <ctor-dtor-name> ::= C1 # complete object constructor 2203 // ::= C2 # base object constructor 2204 // ::= C3 # complete object allocating constructor 2205 // 2206 switch (T) { 2207 case Ctor_Complete: 2208 Out << "C1"; 2209 break; 2210 case Ctor_Base: 2211 Out << "C2"; 2212 break; 2213 case Ctor_CompleteAllocating: 2214 Out << "C3"; 2215 break; 2216 } 2217 } 2218 2219 void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { 2220 // <ctor-dtor-name> ::= D0 # deleting destructor 2221 // ::= D1 # complete object destructor 2222 // ::= D2 # base object destructor 2223 // 2224 switch (T) { 2225 case Dtor_Deleting: 2226 Out << "D0"; 2227 break; 2228 case Dtor_Complete: 2229 Out << "D1"; 2230 break; 2231 case Dtor_Base: 2232 Out << "D2"; 2233 break; 2234 } 2235 } 2236 2237 void CXXNameMangler::mangleTemplateArgs( 2238 const ExplicitTemplateArgumentList &TemplateArgs) { 2239 // <template-args> ::= I <template-arg>+ E 2240 Out << 'I'; 2241 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I) 2242 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument()); 2243 Out << 'E'; 2244 } 2245 2246 void CXXNameMangler::mangleTemplateArgs(TemplateName Template, 2247 const TemplateArgument *TemplateArgs, 2248 unsigned NumTemplateArgs) { 2249 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 2250 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs, 2251 NumTemplateArgs); 2252 2253 // <template-args> ::= I <template-arg>+ E 2254 Out << 'I'; 2255 for (unsigned i = 0; i != NumTemplateArgs; ++i) 2256 mangleTemplateArg(0, TemplateArgs[i]); 2257 Out << 'E'; 2258 } 2259 2260 void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL, 2261 const TemplateArgumentList &AL) { 2262 // <template-args> ::= I <template-arg>+ E 2263 Out << 'I'; 2264 for (unsigned i = 0, e = AL.size(); i != e; ++i) 2265 mangleTemplateArg(PL.getParam(i), AL[i]); 2266 Out << 'E'; 2267 } 2268 2269 void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL, 2270 const TemplateArgument *TemplateArgs, 2271 unsigned NumTemplateArgs) { 2272 // <template-args> ::= I <template-arg>+ E 2273 Out << 'I'; 2274 for (unsigned i = 0; i != NumTemplateArgs; ++i) 2275 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]); 2276 Out << 'E'; 2277 } 2278 2279 void CXXNameMangler::mangleTemplateArg(const NamedDecl *P, 2280 const TemplateArgument &A) { 2281 // <template-arg> ::= <type> # type or template 2282 // ::= X <expression> E # expression 2283 // ::= <expr-primary> # simple expressions 2284 // ::= J <template-arg>* E # argument pack 2285 // ::= sp <expression> # pack expansion of (C++0x) 2286 switch (A.getKind()) { 2287 case TemplateArgument::Null: 2288 llvm_unreachable("Cannot mangle NULL template argument"); 2289 2290 case TemplateArgument::Type: 2291 mangleType(A.getAsType()); 2292 break; 2293 case TemplateArgument::Template: 2294 // This is mangled as <type>. 2295 mangleType(A.getAsTemplate()); 2296 break; 2297 case TemplateArgument::TemplateExpansion: 2298 // <type> ::= Dp <type> # pack expansion (C++0x) 2299 Out << "Dp"; 2300 mangleType(A.getAsTemplateOrTemplatePattern()); 2301 break; 2302 case TemplateArgument::Expression: 2303 Out << 'X'; 2304 mangleExpression(A.getAsExpr()); 2305 Out << 'E'; 2306 break; 2307 case TemplateArgument::Integral: 2308 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral()); 2309 break; 2310 case TemplateArgument::Declaration: { 2311 assert(P && "Missing template parameter for declaration argument"); 2312 // <expr-primary> ::= L <mangled-name> E # external name 2313 2314 // Clang produces AST's where pointer-to-member-function expressions 2315 // and pointer-to-function expressions are represented as a declaration not 2316 // an expression. We compensate for it here to produce the correct mangling. 2317 NamedDecl *D = cast<NamedDecl>(A.getAsDecl()); 2318 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P); 2319 bool compensateMangling = D->isCXXClassMember() && 2320 !Parameter->getType()->isReferenceType(); 2321 if (compensateMangling) { 2322 Out << 'X'; 2323 mangleOperatorName(OO_Amp, 1); 2324 } 2325 2326 Out << 'L'; 2327 // References to external entities use the mangled name; if the name would 2328 // not normally be manged then mangle it as unqualified. 2329 // 2330 // FIXME: The ABI specifies that external names here should have _Z, but 2331 // gcc leaves this off. 2332 if (compensateMangling) 2333 mangle(D, "_Z"); 2334 else 2335 mangle(D, "Z"); 2336 Out << 'E'; 2337 2338 if (compensateMangling) 2339 Out << 'E'; 2340 2341 break; 2342 } 2343 2344 case TemplateArgument::Pack: { 2345 // Note: proposal by Mike Herrick on 12/20/10 2346 Out << 'J'; 2347 for (TemplateArgument::pack_iterator PA = A.pack_begin(), 2348 PAEnd = A.pack_end(); 2349 PA != PAEnd; ++PA) 2350 mangleTemplateArg(P, *PA); 2351 Out << 'E'; 2352 } 2353 } 2354 } 2355 2356 void CXXNameMangler::mangleTemplateParameter(unsigned Index) { 2357 // <template-param> ::= T_ # first template parameter 2358 // ::= T <parameter-2 non-negative number> _ 2359 if (Index == 0) 2360 Out << "T_"; 2361 else 2362 Out << 'T' << (Index - 1) << '_'; 2363 } 2364 2365 // <substitution> ::= S <seq-id> _ 2366 // ::= S_ 2367 bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) { 2368 // Try one of the standard substitutions first. 2369 if (mangleStandardSubstitution(ND)) 2370 return true; 2371 2372 ND = cast<NamedDecl>(ND->getCanonicalDecl()); 2373 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND)); 2374 } 2375 2376 bool CXXNameMangler::mangleSubstitution(QualType T) { 2377 if (!T.getCVRQualifiers()) { 2378 if (const RecordType *RT = T->getAs<RecordType>()) 2379 return mangleSubstitution(RT->getDecl()); 2380 } 2381 2382 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); 2383 2384 return mangleSubstitution(TypePtr); 2385 } 2386 2387 bool CXXNameMangler::mangleSubstitution(TemplateName Template) { 2388 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 2389 return mangleSubstitution(TD); 2390 2391 Template = Context.getASTContext().getCanonicalTemplateName(Template); 2392 return mangleSubstitution( 2393 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); 2394 } 2395 2396 bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) { 2397 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr); 2398 if (I == Substitutions.end()) 2399 return false; 2400 2401 unsigned SeqID = I->second; 2402 if (SeqID == 0) 2403 Out << "S_"; 2404 else { 2405 SeqID--; 2406 2407 // <seq-id> is encoded in base-36, using digits and upper case letters. 2408 char Buffer[10]; 2409 char *BufferPtr = llvm::array_endof(Buffer); 2410 2411 if (SeqID == 0) *--BufferPtr = '0'; 2412 2413 while (SeqID) { 2414 assert(BufferPtr > Buffer && "Buffer overflow!"); 2415 2416 char c = static_cast<char>(SeqID % 36); 2417 2418 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10); 2419 SeqID /= 36; 2420 } 2421 2422 Out << 'S' 2423 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr) 2424 << '_'; 2425 } 2426 2427 return true; 2428 } 2429 2430 static bool isCharType(QualType T) { 2431 if (T.isNull()) 2432 return false; 2433 2434 return T->isSpecificBuiltinType(BuiltinType::Char_S) || 2435 T->isSpecificBuiltinType(BuiltinType::Char_U); 2436 } 2437 2438 /// isCharSpecialization - Returns whether a given type is a template 2439 /// specialization of a given name with a single argument of type char. 2440 static bool isCharSpecialization(QualType T, const char *Name) { 2441 if (T.isNull()) 2442 return false; 2443 2444 const RecordType *RT = T->getAs<RecordType>(); 2445 if (!RT) 2446 return false; 2447 2448 const ClassTemplateSpecializationDecl *SD = 2449 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); 2450 if (!SD) 2451 return false; 2452 2453 if (!isStdNamespace(SD->getDeclContext())) 2454 return false; 2455 2456 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); 2457 if (TemplateArgs.size() != 1) 2458 return false; 2459 2460 if (!isCharType(TemplateArgs[0].getAsType())) 2461 return false; 2462 2463 return SD->getIdentifier()->getName() == Name; 2464 } 2465 2466 template <std::size_t StrLen> 2467 static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD, 2468 const char (&Str)[StrLen]) { 2469 if (!SD->getIdentifier()->isStr(Str)) 2470 return false; 2471 2472 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); 2473 if (TemplateArgs.size() != 2) 2474 return false; 2475 2476 if (!isCharType(TemplateArgs[0].getAsType())) 2477 return false; 2478 2479 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) 2480 return false; 2481 2482 return true; 2483 } 2484 2485 bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) { 2486 // <substitution> ::= St # ::std:: 2487 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { 2488 if (isStd(NS)) { 2489 Out << "St"; 2490 return true; 2491 } 2492 } 2493 2494 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) { 2495 if (!isStdNamespace(TD->getDeclContext())) 2496 return false; 2497 2498 // <substitution> ::= Sa # ::std::allocator 2499 if (TD->getIdentifier()->isStr("allocator")) { 2500 Out << "Sa"; 2501 return true; 2502 } 2503 2504 // <<substitution> ::= Sb # ::std::basic_string 2505 if (TD->getIdentifier()->isStr("basic_string")) { 2506 Out << "Sb"; 2507 return true; 2508 } 2509 } 2510 2511 if (const ClassTemplateSpecializationDecl *SD = 2512 dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 2513 if (!isStdNamespace(SD->getDeclContext())) 2514 return false; 2515 2516 // <substitution> ::= Ss # ::std::basic_string<char, 2517 // ::std::char_traits<char>, 2518 // ::std::allocator<char> > 2519 if (SD->getIdentifier()->isStr("basic_string")) { 2520 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); 2521 2522 if (TemplateArgs.size() != 3) 2523 return false; 2524 2525 if (!isCharType(TemplateArgs[0].getAsType())) 2526 return false; 2527 2528 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) 2529 return false; 2530 2531 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator")) 2532 return false; 2533 2534 Out << "Ss"; 2535 return true; 2536 } 2537 2538 // <substitution> ::= Si # ::std::basic_istream<char, 2539 // ::std::char_traits<char> > 2540 if (isStreamCharSpecialization(SD, "basic_istream")) { 2541 Out << "Si"; 2542 return true; 2543 } 2544 2545 // <substitution> ::= So # ::std::basic_ostream<char, 2546 // ::std::char_traits<char> > 2547 if (isStreamCharSpecialization(SD, "basic_ostream")) { 2548 Out << "So"; 2549 return true; 2550 } 2551 2552 // <substitution> ::= Sd # ::std::basic_iostream<char, 2553 // ::std::char_traits<char> > 2554 if (isStreamCharSpecialization(SD, "basic_iostream")) { 2555 Out << "Sd"; 2556 return true; 2557 } 2558 } 2559 return false; 2560 } 2561 2562 void CXXNameMangler::addSubstitution(QualType T) { 2563 if (!T.getCVRQualifiers()) { 2564 if (const RecordType *RT = T->getAs<RecordType>()) { 2565 addSubstitution(RT->getDecl()); 2566 return; 2567 } 2568 } 2569 2570 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); 2571 addSubstitution(TypePtr); 2572 } 2573 2574 void CXXNameMangler::addSubstitution(TemplateName Template) { 2575 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 2576 return addSubstitution(TD); 2577 2578 Template = Context.getASTContext().getCanonicalTemplateName(Template); 2579 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); 2580 } 2581 2582 void CXXNameMangler::addSubstitution(uintptr_t Ptr) { 2583 assert(!Substitutions.count(Ptr) && "Substitution already exists!"); 2584 Substitutions[Ptr] = SeqID++; 2585 } 2586 2587 // 2588 2589 /// \brief Mangles the name of the declaration D and emits that name to the 2590 /// given output stream. 2591 /// 2592 /// If the declaration D requires a mangled name, this routine will emit that 2593 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged 2594 /// and this routine will return false. In this case, the caller should just 2595 /// emit the identifier of the declaration (\c D->getIdentifier()) as its 2596 /// name. 2597 void ItaniumMangleContext::mangleName(const NamedDecl *D, 2598 llvm::raw_ostream &Out) { 2599 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && 2600 "Invalid mangleName() call, argument is not a variable or function!"); 2601 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && 2602 "Invalid mangleName() call on 'structor decl!"); 2603 2604 PrettyStackTraceDecl CrashInfo(D, SourceLocation(), 2605 getASTContext().getSourceManager(), 2606 "Mangling declaration"); 2607 2608 CXXNameMangler Mangler(*this, Out); 2609 return Mangler.mangle(D); 2610 } 2611 2612 void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, 2613 CXXCtorType Type, 2614 llvm::raw_ostream &Out) { 2615 CXXNameMangler Mangler(*this, Out, D, Type); 2616 Mangler.mangle(D); 2617 } 2618 2619 void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, 2620 CXXDtorType Type, 2621 llvm::raw_ostream &Out) { 2622 CXXNameMangler Mangler(*this, Out, D, Type); 2623 Mangler.mangle(D); 2624 } 2625 2626 void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD, 2627 const ThunkInfo &Thunk, 2628 llvm::raw_ostream &Out) { 2629 // <special-name> ::= T <call-offset> <base encoding> 2630 // # base is the nominal target function of thunk 2631 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> 2632 // # base is the nominal target function of thunk 2633 // # first call-offset is 'this' adjustment 2634 // # second call-offset is result adjustment 2635 2636 assert(!isa<CXXDestructorDecl>(MD) && 2637 "Use mangleCXXDtor for destructor decls!"); 2638 CXXNameMangler Mangler(*this, Out); 2639 Mangler.getStream() << "_ZT"; 2640 if (!Thunk.Return.isEmpty()) 2641 Mangler.getStream() << 'c'; 2642 2643 // Mangle the 'this' pointer adjustment. 2644 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset); 2645 2646 // Mangle the return pointer adjustment if there is one. 2647 if (!Thunk.Return.isEmpty()) 2648 Mangler.mangleCallOffset(Thunk.Return.NonVirtual, 2649 Thunk.Return.VBaseOffsetOffset); 2650 2651 Mangler.mangleFunctionEncoding(MD); 2652 } 2653 2654 void 2655 ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, 2656 CXXDtorType Type, 2657 const ThisAdjustment &ThisAdjustment, 2658 llvm::raw_ostream &Out) { 2659 // <special-name> ::= T <call-offset> <base encoding> 2660 // # base is the nominal target function of thunk 2661 CXXNameMangler Mangler(*this, Out, DD, Type); 2662 Mangler.getStream() << "_ZT"; 2663 2664 // Mangle the 'this' pointer adjustment. 2665 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual, 2666 ThisAdjustment.VCallOffsetOffset); 2667 2668 Mangler.mangleFunctionEncoding(DD); 2669 } 2670 2671 /// mangleGuardVariable - Returns the mangled name for a guard variable 2672 /// for the passed in VarDecl. 2673 void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D, 2674 llvm::raw_ostream &Out) { 2675 // <special-name> ::= GV <object name> # Guard variable for one-time 2676 // # initialization 2677 CXXNameMangler Mangler(*this, Out); 2678 Mangler.getStream() << "_ZGV"; 2679 Mangler.mangleName(D); 2680 } 2681 2682 void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D, 2683 llvm::raw_ostream &Out) { 2684 // We match the GCC mangling here. 2685 // <special-name> ::= GR <object name> 2686 CXXNameMangler Mangler(*this, Out); 2687 Mangler.getStream() << "_ZGR"; 2688 Mangler.mangleName(D); 2689 } 2690 2691 void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, 2692 llvm::raw_ostream &Out) { 2693 // <special-name> ::= TV <type> # virtual table 2694 CXXNameMangler Mangler(*this, Out); 2695 Mangler.getStream() << "_ZTV"; 2696 Mangler.mangleNameOrStandardSubstitution(RD); 2697 } 2698 2699 void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, 2700 llvm::raw_ostream &Out) { 2701 // <special-name> ::= TT <type> # VTT structure 2702 CXXNameMangler Mangler(*this, Out); 2703 Mangler.getStream() << "_ZTT"; 2704 Mangler.mangleNameOrStandardSubstitution(RD); 2705 } 2706 2707 void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, 2708 int64_t Offset, 2709 const CXXRecordDecl *Type, 2710 llvm::raw_ostream &Out) { 2711 // <special-name> ::= TC <type> <offset number> _ <base type> 2712 CXXNameMangler Mangler(*this, Out); 2713 Mangler.getStream() << "_ZTC"; 2714 Mangler.mangleNameOrStandardSubstitution(RD); 2715 Mangler.getStream() << Offset; 2716 Mangler.getStream() << '_'; 2717 Mangler.mangleNameOrStandardSubstitution(Type); 2718 } 2719 2720 void ItaniumMangleContext::mangleCXXRTTI(QualType Ty, 2721 llvm::raw_ostream &Out) { 2722 // <special-name> ::= TI <type> # typeinfo structure 2723 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); 2724 CXXNameMangler Mangler(*this, Out); 2725 Mangler.getStream() << "_ZTI"; 2726 Mangler.mangleType(Ty); 2727 } 2728 2729 void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty, 2730 llvm::raw_ostream &Out) { 2731 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) 2732 CXXNameMangler Mangler(*this, Out); 2733 Mangler.getStream() << "_ZTS"; 2734 Mangler.mangleType(Ty); 2735 } 2736 2737 MangleContext *clang::createItaniumMangleContext(ASTContext &Context, 2738 Diagnostic &Diags) { 2739 return new ItaniumMangleContext(Context, Diags); 2740 } 2741