blob: 73c9f5778f41e078ddede302661a1d2ac9d344f8 [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- 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,
Chris Lattner5f9e2722011-07-23 10:55:15 +000040 StringRef Outer,
Peter Collingbourne14110472011-01-13 18:57:25 +000041 const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000042 raw_ostream &Out) {
Peter Collingbourne14110472011-01-13 18:57:25 +000043 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();
Richard Smith7a614d82011-06-11 17:19:42 +000051 // 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();
Peter Collingbourne14110472011-01-13 18:57:25 +000056 assert(DC == ExpectedDC && "Given decl context did not match expected!");
57#endif
58}
59
60}
61
David Blaikie99ba9e32011-12-20 02:48:34 +000062void MangleContext::anchor() { }
63
Peter Collingbourne14110472011-01-13 18:57:25 +000064void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000065 raw_ostream &Out) {
Peter Collingbourne14110472011-01-13 18:57:25 +000066 Out << "__block_global_" << getBlockId(BD, false);
67}
68
69void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
70 CXXCtorType CT, const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000071 raw_ostream &ResStream) {
Peter Collingbourne14110472011-01-13 18:57:25 +000072 checkMangleDC(CD, BD);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000073 SmallString<64> Buffer;
Rafael Espindolac4850c22011-02-10 23:59:36 +000074 llvm::raw_svector_ostream Out(Buffer);
75 mangleCXXCtor(CD, CT, Out);
76 Out.flush();
77 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne14110472011-01-13 18:57:25 +000078}
79
80void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
81 CXXDtorType DT, const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 raw_ostream &ResStream) {
Peter Collingbourne14110472011-01-13 18:57:25 +000083 checkMangleDC(DD, BD);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000084 SmallString<64> Buffer;
Rafael Espindolac4850c22011-02-10 23:59:36 +000085 llvm::raw_svector_ostream Out(Buffer);
86 mangleCXXDtor(DD, DT, Out);
87 Out.flush();
88 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne14110472011-01-13 18:57:25 +000089}
90
91void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000092 raw_ostream &Out) {
Peter Collingbourne14110472011-01-13 18:57:25 +000093 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
94 checkMangleDC(DC, BD);
95
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000096 SmallString<64> Buffer;
Rafael Espindolaf0be9792011-02-11 02:52:17 +000097 llvm::raw_svector_ostream Stream(Buffer);
Peter Collingbourne14110472011-01-13 18:57:25 +000098 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +000099 mangleObjCMethodName(Method, Stream);
Peter Collingbourne14110472011-01-13 18:57:25 +0000100 } else {
101 const NamedDecl *ND = cast<NamedDecl>(DC);
102 if (IdentifierInfo *II = ND->getIdentifier())
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000103 Stream << II->getName();
Peter Collingbourne14110472011-01-13 18:57:25 +0000104 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?
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000110 mangleName(ND, Stream);
Peter Collingbourne14110472011-01-13 18:57:25 +0000111 }
112 }
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000113 Stream.flush();
Rafael Espindolac4850c22011-02-10 23:59:36 +0000114 mangleFunctionBlock(*this, Buffer, BD, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +0000115}
116
117void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000118 raw_ostream &Out) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000119 SmallString<64> Name;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000120 llvm::raw_svector_ostream OS(Name);
Peter Collingbourne14110472011-01-13 18:57:25 +0000121
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))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000127 OS << '(' << *CID << ')';
Peter Collingbourne14110472011-01-13 18:57:25 +0000128 OS << ' ' << MD->getSelector().getAsString() << ']';
129
130 Out << OS.str().size() << OS.str();
131}
132
133void MangleContext::mangleBlock(const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 raw_ostream &Out) {
Peter Collingbourne14110472011-01-13 18:57:25 +0000135 const DeclContext *DC = BD->getDeclContext();
136 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
137 DC = DC->getParent();
138 if (DC->isFunctionOrMethod())
Rafael Espindolac4850c22011-02-10 23:59:36 +0000139 mangleBlock(DC, BD, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +0000140 else
Rafael Espindolac4850c22011-02-10 23:59:36 +0000141 mangleGlobalBlock(BD, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +0000142}