1 //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements generic name mangling support for blocks and Objective-C.
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/raw_ostream.h"
24 #include "llvm/Support/ErrorHandling.h"
26 #define MANGLE_CHECKER 0
32 using namespace clang;
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.
39 static void mangleFunctionBlock(MangleContext &Context,
43 Out << "__" << Outer << "_block_invoke_" << Context.getBlockId(BD, true);
46 static void checkMangleDC(const DeclContext *DC, const BlockDecl *BD) {
48 const DeclContext *ExpectedDC = BD->getDeclContext();
49 while (isa<BlockDecl>(ExpectedDC) || isa<EnumDecl>(ExpectedDC))
50 ExpectedDC = ExpectedDC->getParent();
51 // In-class initializers for non-static data members are lexically defined
52 // within the class, but are mangled as if they were specified as constructor
53 // member initializers.
54 if (isa<CXXRecordDecl>(ExpectedDC) && DC != ExpectedDC)
56 assert(DC == ExpectedDC && "Given decl context did not match expected!");
62 void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
64 Out << "__block_global_" << getBlockId(BD, false);
67 void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
68 CXXCtorType CT, const BlockDecl *BD,
69 raw_ostream &ResStream) {
70 checkMangleDC(CD, BD);
71 llvm::SmallString<64> Buffer;
72 llvm::raw_svector_ostream Out(Buffer);
73 mangleCXXCtor(CD, CT, Out);
75 mangleFunctionBlock(*this, Buffer, BD, ResStream);
78 void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
79 CXXDtorType DT, const BlockDecl *BD,
80 raw_ostream &ResStream) {
81 checkMangleDC(DD, BD);
82 llvm::SmallString<64> Buffer;
83 llvm::raw_svector_ostream Out(Buffer);
84 mangleCXXDtor(DD, DT, Out);
86 mangleFunctionBlock(*this, Buffer, BD, ResStream);
89 void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
91 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
92 checkMangleDC(DC, BD);
94 llvm::SmallString<64> Buffer;
95 llvm::raw_svector_ostream Stream(Buffer);
96 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
97 mangleObjCMethodName(Method, Stream);
99 const NamedDecl *ND = cast<NamedDecl>(DC);
100 if (IdentifierInfo *II = ND->getIdentifier())
101 Stream << II->getName();
103 // FIXME: We were doing a mangleUnqualifiedName() before, but that's
104 // a private member of a class that will soon itself be private to the
105 // Itanium C++ ABI object. What should we do now? Right now, I'm just
106 // calling the mangleName() method on the MangleContext; is there a
108 mangleName(ND, Stream);
112 mangleFunctionBlock(*this, Buffer, BD, Out);
115 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
117 llvm::SmallString<64> Name;
118 llvm::raw_svector_ostream OS(Name);
120 const ObjCContainerDecl *CD =
121 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
122 assert (CD && "Missing container decl in GetNameForMethod");
123 OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
124 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
125 OS << '(' << CID << ')';
126 OS << ' ' << MD->getSelector().getAsString() << ']';
128 Out << OS.str().size() << OS.str();
131 void MangleContext::mangleBlock(const BlockDecl *BD,
133 const DeclContext *DC = BD->getDeclContext();
134 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
135 DC = DC->getParent();
136 if (DC->isFunctionOrMethod())
137 mangleBlock(DC, BD, Out);
139 mangleGlobalBlock(BD, Out);