Mangle.cpp revision f7ccbad5d9949e7ddd1cbef43d482553b811e026
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
32using 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
37namespace {
38
39static 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
46static 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
62void MangleContext::anchor() { }
63
64void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
65                                      raw_ostream &Out) {
66  Out << "__block_global_" << getBlockId(BD, false);
67}
68
69void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
70                                    CXXCtorType CT, const BlockDecl *BD,
71                                    raw_ostream &ResStream) {
72  checkMangleDC(CD, BD);
73  SmallString<64> Buffer;
74  llvm::raw_svector_ostream Out(Buffer);
75  mangleCXXCtor(CD, CT, Out);
76  Out.flush();
77  mangleFunctionBlock(*this, Buffer, BD, ResStream);
78}
79
80void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
81                                    CXXDtorType DT, const BlockDecl *BD,
82                                    raw_ostream &ResStream) {
83  checkMangleDC(DD, BD);
84  SmallString<64> Buffer;
85  llvm::raw_svector_ostream Out(Buffer);
86  mangleCXXDtor(DD, DT, Out);
87  Out.flush();
88  mangleFunctionBlock(*this, Buffer, BD, ResStream);
89}
90
91void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
92                                raw_ostream &Out) {
93  assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
94  checkMangleDC(DC, BD);
95
96  SmallString<64> Buffer;
97  llvm::raw_svector_ostream Stream(Buffer);
98  if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
99    mangleObjCMethodName(Method, Stream);
100  } else {
101    const NamedDecl *ND = cast<NamedDecl>(DC);
102    if (IdentifierInfo *II = ND->getIdentifier())
103      Stream << II->getName();
104    else {
105      // FIXME: We were doing a mangleUnqualifiedName() before, but that's
106      // a private member of a class that will soon itself be private to the
107      // Itanium C++ ABI object. What should we do now? Right now, I'm just
108      // calling the mangleName() method on the MangleContext; is there a
109      // better way?
110      mangleName(ND, Stream);
111    }
112  }
113  Stream.flush();
114  mangleFunctionBlock(*this, Buffer, BD, Out);
115}
116
117void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
118                                         raw_ostream &Out) {
119  SmallString<64> Name;
120  llvm::raw_svector_ostream OS(Name);
121
122  const ObjCContainerDecl *CD =
123  dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
124  assert (CD && "Missing container decl in GetNameForMethod");
125  OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
126  if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
127    OS << '(' << CID << ')';
128  OS << ' ' << MD->getSelector().getAsString() << ']';
129
130  Out << OS.str().size() << OS.str();
131}
132
133void MangleContext::mangleBlock(const BlockDecl *BD,
134                                raw_ostream &Out) {
135  const DeclContext *DC = BD->getDeclContext();
136  while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
137    DC = DC->getParent();
138  if (DC->isFunctionOrMethod())
139    mangleBlock(DC, BD, Out);
140  else
141    mangleGlobalBlock(BD, Out);
142}
143