1 //===--- Mangle.h - 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 // Defines the C++ name mangling interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_MANGLE_H
15 #define LLVM_CLANG_AST_MANGLE_H
17 #include "clang/AST/Type.h"
18 #include "clang/Basic/ABI.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/raw_ostream.h"
27 class CXXConstructorDecl;
28 class CXXDestructorDecl;
34 struct ThisAdjustment;
37 /// MangleBuffer - a convenient class for storing a name which is
38 /// either the result of a mangling or is a constant string with
39 /// external memory ownership.
42 void setString(StringRef Ref) {
46 SmallVectorImpl<char> &getBuffer() {
50 StringRef getString() const {
51 if (!String.empty()) return String;
55 operator StringRef() const {
61 SmallString<256> Buffer;
64 /// MangleContext - Context for tracking state which persists across multiple
65 /// calls to the C++ name mangler.
67 virtual void anchor();
70 DiagnosticsEngine &Diags;
72 llvm::DenseMap<const BlockDecl*, unsigned> GlobalBlockIds;
73 llvm::DenseMap<const BlockDecl*, unsigned> LocalBlockIds;
76 explicit MangleContext(ASTContext &Context,
77 DiagnosticsEngine &Diags)
78 : Context(Context), Diags(Diags) { }
80 virtual ~MangleContext() { }
82 ASTContext &getASTContext() const { return Context; }
84 DiagnosticsEngine &getDiags() const { return Diags; }
86 virtual void startNewFunction() { LocalBlockIds.clear(); }
88 unsigned getBlockId(const BlockDecl *BD, bool Local) {
89 llvm::DenseMap<const BlockDecl *, unsigned> &BlockIds
90 = Local? LocalBlockIds : GlobalBlockIds;
91 std::pair<llvm::DenseMap<const BlockDecl *, unsigned>::iterator, bool>
92 Result = BlockIds.insert(std::make_pair(BD, BlockIds.size()));
93 return Result.first->second;
96 /// @name Mangler Entry Points
99 virtual bool shouldMangleDeclName(const NamedDecl *D) = 0;
100 virtual void mangleName(const NamedDecl *D, raw_ostream &)=0;
101 virtual void mangleThunk(const CXXMethodDecl *MD,
102 const ThunkInfo &Thunk,
104 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
105 const ThisAdjustment &ThisAdjustment,
107 virtual void mangleReferenceTemporary(const VarDecl *D,
109 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
111 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
113 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
114 const CXXRecordDecl *Type,
116 virtual void mangleCXXRTTI(QualType T, raw_ostream &) = 0;
117 virtual void mangleCXXRTTIName(QualType T, raw_ostream &) = 0;
118 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
120 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
123 void mangleGlobalBlock(const BlockDecl *BD,
126 void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT,
127 const BlockDecl *BD, raw_ostream &Out);
128 void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT,
129 const BlockDecl *BD, raw_ostream &Out);
130 void mangleBlock(const DeclContext *DC, const BlockDecl *BD,
132 // Do the right thing.
133 void mangleBlock(const BlockDecl *BD, raw_ostream &Out,
134 const NamedDecl *ID=0);
136 void mangleObjCMethodName(const ObjCMethodDecl *MD,
139 // This is pretty lame.
140 virtual void mangleItaniumGuardVariable(const VarDecl *D,
142 llvm_unreachable("Target does not support mangling guard variables");
144 // FIXME: Revisit this once we know what we need to do for MSVC compatibility.
145 virtual void mangleItaniumThreadLocalInit(const VarDecl *D,
147 llvm_unreachable("Target does not support mangling thread_local variables");
149 virtual void mangleItaniumThreadLocalWrapper(const VarDecl *D,
151 llvm_unreachable("Target does not support mangling thread_local variables");
157 MangleContext *createItaniumMangleContext(ASTContext &Context,
158 DiagnosticsEngine &Diags);
159 MangleContext *createMicrosoftMangleContext(ASTContext &Context,
160 DiagnosticsEngine &Diags);