]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/lib/AST/Mangle.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / lib / AST / Mangle.cpp
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/raw_ostream.h"
24 #include "llvm/Support/ErrorHandling.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 namespace {
38
39 static void mangleFunctionBlock(MangleContext &Context,
40                                 StringRef Outer,
41                                 const BlockDecl *BD,
42                                 raw_ostream &Out) {
43   Out << "__" << Outer << "_block_invoke_" << Context.getBlockId(BD, true);
44 }
45
46 static void checkMangleDC(const DeclContext *DC, const BlockDecl *BD) {
47 #ifndef NDEBUG
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)
55     DC = DC->getParent();
56   assert(DC == ExpectedDC && "Given decl context did not match expected!");
57 #endif
58 }
59
60 }
61
62 void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
63                                       raw_ostream &Out) {
64   Out << "__block_global_" << getBlockId(BD, false);
65 }
66
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);
74   Out.flush();
75   mangleFunctionBlock(*this, Buffer, BD, ResStream);
76 }
77
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);
85   Out.flush();
86   mangleFunctionBlock(*this, Buffer, BD, ResStream);
87 }
88
89 void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
90                                 raw_ostream &Out) {
91   assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
92   checkMangleDC(DC, BD);
93
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);
98   } else {
99     const NamedDecl *ND = cast<NamedDecl>(DC);
100     if (IdentifierInfo *II = ND->getIdentifier())
101       Stream << II->getName();
102     else {
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
107       // better way?
108       mangleName(ND, Stream);
109     }
110   }
111   Stream.flush();
112   mangleFunctionBlock(*this, Buffer, BD, Out);
113 }
114
115 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
116                                          raw_ostream &Out) {
117   llvm::SmallString<64> Name;
118   llvm::raw_svector_ostream OS(Name);
119   
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() << ']';
127   
128   Out << OS.str().size() << OS.str();
129 }
130
131 void MangleContext::mangleBlock(const BlockDecl *BD,
132                                 raw_ostream &Out) {
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);
138   else
139     mangleGlobalBlock(BD, Out);
140 }