Home | History | Annotate | Download | only in AST
      1 //===--- Mangle.cpp - Mangle C++ Names --------------------------*- 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 generic name mangling support for blocks and Objective-C.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #include "clang/AST/Mangle.h"
     14 #include "clang/AST/ASTContext.h"
     15 #include "clang/AST/Decl.h"
     16 #include "clang/AST/DeclCXX.h"
     17 #include "clang/AST/DeclObjC.h"
     18 #include "clang/AST/DeclTemplate.h"
     19 #include "clang/AST/ExprCXX.h"
     20 #include "clang/Basic/ABI.h"
     21 #include "clang/Basic/SourceManager.h"
     22 #include "llvm/ADT/StringExtras.h"
     23 #include "llvm/Support/ErrorHandling.h"
     24 #include "llvm/Support/raw_ostream.h"
     25 
     26 #define MANGLE_CHECKER 0
     27 
     28 #if MANGLE_CHECKER
     29 #include <cxxabi.h>
     30 #endif
     31 
     32 using namespace clang;
     33 
     34 // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
     35 // much to be desired. Come up with a better mangling scheme.
     36 
     37 static void mangleFunctionBlock(MangleContext &Context,
     38                                 StringRef Outer,
     39                                 const BlockDecl *BD,
     40                                 raw_ostream &Out) {
     41   unsigned discriminator = Context.getBlockId(BD, true);
     42   if (discriminator == 0)
     43     Out << "__" << Outer << "_block_invoke";
     44   else
     45     Out << "__" << Outer << "_block_invoke_" << discriminator+1;
     46 }
     47 
     48 void MangleContext::anchor() { }
     49 
     50 void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
     51                                       const NamedDecl *ID,
     52                                       raw_ostream &Out) {
     53   unsigned discriminator = getBlockId(BD, false);
     54   if (ID) {
     55     if (shouldMangleDeclName(ID))
     56       mangleName(ID, Out);
     57     else {
     58       Out << ID->getIdentifier()->getName();
     59     }
     60   }
     61   if (discriminator == 0)
     62     Out << "_block_invoke";
     63   else
     64     Out << "_block_invoke_" << discriminator+1;
     65 }
     66 
     67 void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
     68                                     CXXCtorType CT, const BlockDecl *BD,
     69                                     raw_ostream &ResStream) {
     70   SmallString<64> Buffer;
     71   llvm::raw_svector_ostream Out(Buffer);
     72   mangleCXXCtor(CD, CT, Out);
     73   Out.flush();
     74   mangleFunctionBlock(*this, Buffer, BD, ResStream);
     75 }
     76 
     77 void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
     78                                     CXXDtorType DT, const BlockDecl *BD,
     79                                     raw_ostream &ResStream) {
     80   SmallString<64> Buffer;
     81   llvm::raw_svector_ostream Out(Buffer);
     82   mangleCXXDtor(DD, DT, Out);
     83   Out.flush();
     84   mangleFunctionBlock(*this, Buffer, BD, ResStream);
     85 }
     86 
     87 void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
     88                                 raw_ostream &Out) {
     89   assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
     90 
     91   SmallString<64> Buffer;
     92   llvm::raw_svector_ostream Stream(Buffer);
     93   if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
     94     mangleObjCMethodName(Method, Stream);
     95   } else {
     96     const NamedDecl *ND = cast<NamedDecl>(DC);
     97     if (!shouldMangleDeclName(ND) && ND->getIdentifier())
     98       Stream << ND->getIdentifier()->getName();
     99     else {
    100       // FIXME: We were doing a mangleUnqualifiedName() before, but that's
    101       // a private member of a class that will soon itself be private to the
    102       // Itanium C++ ABI object. What should we do now? Right now, I'm just
    103       // calling the mangleName() method on the MangleContext; is there a
    104       // better way?
    105       mangleName(ND, Stream);
    106     }
    107   }
    108   Stream.flush();
    109   mangleFunctionBlock(*this, Buffer, BD, Out);
    110 }
    111 
    112 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
    113                                          raw_ostream &Out) {
    114   SmallString<64> Name;
    115   llvm::raw_svector_ostream OS(Name);
    116 
    117   const ObjCContainerDecl *CD =
    118   dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
    119   assert (CD && "Missing container decl in GetNameForMethod");
    120   OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
    121   if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
    122     OS << '(' << *CID << ')';
    123   OS << ' ' << MD->getSelector().getAsString() << ']';
    124 
    125   Out << OS.str().size() << OS.str();
    126 }
    127