clang API Documentation
00001 //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Implements generic name mangling support for blocks and Objective-C. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 #include "clang/AST/Mangle.h" 00014 #include "clang/AST/ASTContext.h" 00015 #include "clang/AST/Decl.h" 00016 #include "clang/AST/DeclCXX.h" 00017 #include "clang/AST/DeclObjC.h" 00018 #include "clang/AST/DeclTemplate.h" 00019 #include "clang/AST/ExprCXX.h" 00020 #include "clang/Basic/ABI.h" 00021 #include "clang/Basic/SourceManager.h" 00022 #include "llvm/ADT/StringExtras.h" 00023 #include "llvm/Support/raw_ostream.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 00026 #define MANGLE_CHECKER 0 00027 00028 #if MANGLE_CHECKER 00029 #include <cxxabi.h> 00030 #endif 00031 00032 using namespace clang; 00033 00034 // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves 00035 // much to be desired. Come up with a better mangling scheme. 00036 00037 namespace { 00038 00039 static void mangleFunctionBlock(MangleContext &Context, 00040 StringRef Outer, 00041 const BlockDecl *BD, 00042 raw_ostream &Out) { 00043 Out << "__" << Outer << "_block_invoke_" << Context.getBlockId(BD, true); 00044 } 00045 00046 static void checkMangleDC(const DeclContext *DC, const BlockDecl *BD) { 00047 #ifndef NDEBUG 00048 const DeclContext *ExpectedDC = BD->getDeclContext(); 00049 while (isa<BlockDecl>(ExpectedDC) || isa<EnumDecl>(ExpectedDC)) 00050 ExpectedDC = ExpectedDC->getParent(); 00051 // In-class initializers for non-static data members are lexically defined 00052 // within the class, but are mangled as if they were specified as constructor 00053 // member initializers. 00054 if (isa<CXXRecordDecl>(ExpectedDC) && DC != ExpectedDC) 00055 DC = DC->getParent(); 00056 assert(DC == ExpectedDC && "Given decl context did not match expected!"); 00057 #endif 00058 } 00059 00060 } 00061 00062 void MangleContext::anchor() { } 00063 00064 void MangleContext::mangleGlobalBlock(const BlockDecl *BD, 00065 raw_ostream &Out) { 00066 Out << "__block_global_" << getBlockId(BD, false); 00067 } 00068 00069 void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD, 00070 CXXCtorType CT, const BlockDecl *BD, 00071 raw_ostream &ResStream) { 00072 checkMangleDC(CD, BD); 00073 SmallString<64> Buffer; 00074 llvm::raw_svector_ostream Out(Buffer); 00075 mangleCXXCtor(CD, CT, Out); 00076 Out.flush(); 00077 mangleFunctionBlock(*this, Buffer, BD, ResStream); 00078 } 00079 00080 void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD, 00081 CXXDtorType DT, const BlockDecl *BD, 00082 raw_ostream &ResStream) { 00083 checkMangleDC(DD, BD); 00084 SmallString<64> Buffer; 00085 llvm::raw_svector_ostream Out(Buffer); 00086 mangleCXXDtor(DD, DT, Out); 00087 Out.flush(); 00088 mangleFunctionBlock(*this, Buffer, BD, ResStream); 00089 } 00090 00091 void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD, 00092 raw_ostream &Out) { 00093 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC)); 00094 checkMangleDC(DC, BD); 00095 00096 SmallString<64> Buffer; 00097 llvm::raw_svector_ostream Stream(Buffer); 00098 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) { 00099 mangleObjCMethodName(Method, Stream); 00100 } else { 00101 const NamedDecl *ND = cast<NamedDecl>(DC); 00102 if (IdentifierInfo *II = ND->getIdentifier()) 00103 Stream << II->getName(); 00104 else { 00105 // FIXME: We were doing a mangleUnqualifiedName() before, but that's 00106 // a private member of a class that will soon itself be private to the 00107 // Itanium C++ ABI object. What should we do now? Right now, I'm just 00108 // calling the mangleName() method on the MangleContext; is there a 00109 // better way? 00110 mangleName(ND, Stream); 00111 } 00112 } 00113 Stream.flush(); 00114 mangleFunctionBlock(*this, Buffer, BD, Out); 00115 } 00116 00117 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD, 00118 raw_ostream &Out) { 00119 SmallString<64> Name; 00120 llvm::raw_svector_ostream OS(Name); 00121 00122 const ObjCContainerDecl *CD = 00123 dyn_cast<ObjCContainerDecl>(MD->getDeclContext()); 00124 assert (CD && "Missing container decl in GetNameForMethod"); 00125 OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName(); 00126 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) 00127 OS << '(' << *CID << ')'; 00128 OS << ' ' << MD->getSelector().getAsString() << ']'; 00129 00130 Out << OS.str().size() << OS.str(); 00131 } 00132 00133 void MangleContext::mangleBlock(const BlockDecl *BD, 00134 raw_ostream &Out) { 00135 const DeclContext *DC = BD->getDeclContext(); 00136 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) 00137 DC = DC->getParent(); 00138 if (DC->isFunctionOrMethod()) 00139 mangleBlock(DC, BD, Out); 00140 else 00141 mangleGlobalBlock(BD, Out); 00142 }