clang  3.8.0
Mangle.h
Go to the documentation of this file.
1 //===--- Mangle.h - 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 // Defines the C++ name mangling interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_MANGLE_H
15 #define LLVM_CLANG_AST_MANGLE_H
16 
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/Casting.h"
23 #include "llvm/Support/raw_ostream.h"
24 
25 namespace clang {
26  class ASTContext;
27  class BlockDecl;
28  class CXXConstructorDecl;
29  class CXXDestructorDecl;
30  class CXXMethodDecl;
31  class FunctionDecl;
32  class NamedDecl;
33  class ObjCMethodDecl;
34  class StringLiteral;
35  struct ThisAdjustment;
36  struct ThunkInfo;
37  class VarDecl;
38 
39 /// MangleContext - Context for tracking state which persists across multiple
40 /// calls to the C++ name mangler.
42 public:
43  enum ManglerKind {
46  };
47 
48 private:
49  virtual void anchor();
50 
51  ASTContext &Context;
52  DiagnosticsEngine &Diags;
53  const ManglerKind Kind;
54 
55  llvm::DenseMap<const BlockDecl*, unsigned> GlobalBlockIds;
56  llvm::DenseMap<const BlockDecl*, unsigned> LocalBlockIds;
57  llvm::DenseMap<const TagDecl*, uint64_t> AnonStructIds;
58 
59 public:
60  ManglerKind getKind() const { return Kind; }
61 
62  explicit MangleContext(ASTContext &Context,
63  DiagnosticsEngine &Diags,
65  : Context(Context), Diags(Diags), Kind(Kind) {}
66 
67  virtual ~MangleContext() { }
68 
69  ASTContext &getASTContext() const { return Context; }
70 
71  DiagnosticsEngine &getDiags() const { return Diags; }
72 
73  virtual void startNewFunction() { LocalBlockIds.clear(); }
74 
75  unsigned getBlockId(const BlockDecl *BD, bool Local) {
76  llvm::DenseMap<const BlockDecl *, unsigned> &BlockIds
77  = Local? LocalBlockIds : GlobalBlockIds;
79  Result = BlockIds.insert(std::make_pair(BD, BlockIds.size()));
80  return Result.first->second;
81  }
82 
83  uint64_t getAnonymousStructId(const TagDecl *TD) {
85  Result = AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
86  return Result.first->second;
87  }
88 
89  /// @name Mangler Entry Points
90  /// @{
91 
92  bool shouldMangleDeclName(const NamedDecl *D);
93  virtual bool shouldMangleCXXName(const NamedDecl *D) = 0;
94  virtual bool shouldMangleStringLiteral(const StringLiteral *SL) = 0;
95 
96  // FIXME: consider replacing raw_ostream & with something like SmallString &.
97  void mangleName(const NamedDecl *D, raw_ostream &);
98  virtual void mangleCXXName(const NamedDecl *D, raw_ostream &) = 0;
99  virtual void mangleThunk(const CXXMethodDecl *MD,
100  const ThunkInfo &Thunk,
101  raw_ostream &) = 0;
102  virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
104  raw_ostream &) = 0;
105  virtual void mangleReferenceTemporary(const VarDecl *D,
106  unsigned ManglingNumber,
107  raw_ostream &) = 0;
108  virtual void mangleCXXRTTI(QualType T, raw_ostream &) = 0;
109  virtual void mangleCXXRTTIName(QualType T, raw_ostream &) = 0;
110  virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
111  raw_ostream &) = 0;
112  virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
113  raw_ostream &) = 0;
114  virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &) = 0;
115 
116  void mangleGlobalBlock(const BlockDecl *BD,
117  const NamedDecl *ID,
118  raw_ostream &Out);
120  const BlockDecl *BD, raw_ostream &Out);
121  void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT,
122  const BlockDecl *BD, raw_ostream &Out);
123  void mangleBlock(const DeclContext *DC, const BlockDecl *BD,
124  raw_ostream &Out);
125 
126  void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &);
127 
128  virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) = 0;
129 
130  virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &) = 0;
131 
132  virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
133  raw_ostream &) = 0;
134 
135  virtual void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl,
136  raw_ostream &Out) = 0;
137 
138  virtual void mangleSEHFinallyBlock(const NamedDecl *EnclosingDecl,
139  raw_ostream &Out) = 0;
140 
141  /// Generates a unique string for an externally visible type for use with TBAA
142  /// or type uniquing.
143  /// TODO: Extend this to internal types by generating names that are unique
144  /// across translation units so it can be used with LTO.
145  virtual void mangleTypeName(QualType T, raw_ostream &) = 0;
146 
147  /// @}
148 };
149 
151 public:
153  : MangleContext(C, D, MK_Itanium) {}
154 
155  virtual void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) = 0;
156  virtual void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) = 0;
157  virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
158  const CXXRecordDecl *Type,
159  raw_ostream &) = 0;
160  virtual void mangleItaniumThreadLocalInit(const VarDecl *D,
161  raw_ostream &) = 0;
162  virtual void mangleItaniumThreadLocalWrapper(const VarDecl *D,
163  raw_ostream &) = 0;
164 
165  virtual void mangleCXXCtorComdat(const CXXConstructorDecl *D,
166  raw_ostream &) = 0;
167  virtual void mangleCXXDtorComdat(const CXXDestructorDecl *D,
168  raw_ostream &) = 0;
169 
170  static bool classof(const MangleContext *C) {
171  return C->getKind() == MK_Itanium;
172  }
173 
174  static ItaniumMangleContext *create(ASTContext &Context,
175  DiagnosticsEngine &Diags);
176 };
177 
179 public:
181  : MangleContext(C, D, MK_Microsoft) {}
182 
183  /// \brief Mangle vftable symbols. Only a subset of the bases along the path
184  /// to the vftable are included in the name. It's up to the caller to pick
185  /// them correctly.
186  virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
188  raw_ostream &Out) = 0;
189 
190  /// \brief Mangle vbtable symbols. Only a subset of the bases along the path
191  /// to the vbtable are included in the name. It's up to the caller to pick
192  /// them correctly.
193  virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
195  raw_ostream &Out) = 0;
196 
197  virtual void mangleThreadSafeStaticGuardVariable(const VarDecl *VD,
198  unsigned GuardNum,
199  raw_ostream &Out) = 0;
200 
201  virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
202  raw_ostream &) = 0;
203 
204  virtual void mangleCXXVirtualDisplacementMap(const CXXRecordDecl *SrcRD,
205  const CXXRecordDecl *DstRD,
206  raw_ostream &Out) = 0;
207 
208  virtual void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile,
209  uint32_t NumEntries, raw_ostream &Out) = 0;
210 
211  virtual void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries,
212  raw_ostream &Out) = 0;
213 
214  virtual void mangleCXXCatchableType(QualType T, const CXXConstructorDecl *CD,
215  CXXCtorType CT, uint32_t Size,
216  uint32_t NVOffset, int32_t VBPtrOffset,
217  uint32_t VBIndex, raw_ostream &Out) = 0;
218 
220  const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset,
221  uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) = 0;
222 
223  virtual void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived,
224  raw_ostream &Out) = 0;
225  virtual void
227  raw_ostream &Out) = 0;
228 
229  virtual void
232  raw_ostream &Out) = 0;
233 
234  static bool classof(const MangleContext *C) {
235  return C->getKind() == MK_Microsoft;
236  }
237 
238  static MicrosoftMangleContext *create(ASTContext &Context,
239  DiagnosticsEngine &Diags);
240 };
241 }
242 
243 #endif
virtual void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl, raw_ostream &Out)=0
A (possibly-)qualified type.
Definition: Type.h:575
static MicrosoftMangleContext * create(ASTContext &Context, DiagnosticsEngine &Diags)
virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, raw_ostream &)=0
virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &)=0
C Language Family Type Representation.
virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, raw_ostream &)=0
virtual void mangleSEHFinallyBlock(const NamedDecl *EnclosingDecl, raw_ostream &Out)=0
ASTContext & getASTContext() const
Definition: Mangle.h:69
The base class of the type hierarchy.
Definition: Type.h:1249
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2134
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:699
A this pointer adjustment.
Definition: ABI.h:108
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
virtual void mangleCXXDtorComdat(const CXXDestructorDecl *D, raw_ostream &)=0
virtual void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile, uint32_t NumEntries, raw_ostream &Out)=0
virtual void mangleItaniumThreadLocalWrapper(const VarDecl *D, raw_ostream &)=0
virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, const CXXRecordDecl *Type, raw_ostream &)=0
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:91
virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, raw_ostream &)=0
virtual void mangleCXXRTTIBaseClassDescriptor(const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset, uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out)=0
The this pointer adjustment as well as an optional return adjustment for a thunk. ...
Definition: ABI.h:179
bool shouldMangleDeclName(const NamedDecl *D)
Definition: Mangle.cpp:98
static ItaniumMangleContext * create(ASTContext &Context, DiagnosticsEngine &Diags)
static bool classof(const MangleContext *C)
Definition: Mangle.h:234
uint32_t Offset
Definition: CacheTokens.cpp:44
virtual void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber, raw_ostream &)=0
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
virtual void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &)=0
Enums/classes describing ABI related information about constructors, destructors and thunks...
virtual void mangleCXXRTTI(QualType T, raw_ostream &)=0
void mangleName(const NamedDecl *D, raw_ostream &)
Definition: Mangle.cpp:117
virtual void mangleDynamicAtExitDestructor(const VarDecl *D, raw_ostream &)=0
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
MangleContext(ASTContext &Context, DiagnosticsEngine &Diags, ManglerKind Kind)
Definition: Mangle.h:62
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
friend class ASTContext
Definition: Type.h:4012
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3369
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2345
virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &)=0
The result type of a method or function.
virtual void startNewFunction()
Definition: Mangle.h:73
virtual void mangleItaniumThreadLocalInit(const VarDecl *D, raw_ostream &)=0
virtual void mangleThreadSafeStaticGuardVariable(const VarDecl *VD, unsigned GuardNum, raw_ostream &Out)=0
virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, const ThisAdjustment &ThisAdjustment, raw_ostream &)=0
Kind
virtual void mangleCXXRTTIName(QualType T, raw_ostream &)=0
virtual void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries, raw_ostream &Out)=0
virtual bool shouldMangleStringLiteral(const StringLiteral *SL)=0
const TemplateArgument * iterator
Definition: Type.h:4070
unsigned getBlockId(const BlockDecl *BD, bool Local)
Definition: Mangle.h:75
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2644
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:212
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1701
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:41
uint64_t getAnonymousStructId(const TagDecl *TD)
Definition: Mangle.h:83
virtual void mangleCXXName(const NamedDecl *D, raw_ostream &)=0
virtual void mangleCXXVBTable(const CXXRecordDecl *Derived, ArrayRef< const CXXRecordDecl * > BasePath, raw_ostream &Out)=0
Mangle vbtable symbols.
virtual void mangleCXXVFTable(const CXXRecordDecl *Derived, ArrayRef< const CXXRecordDecl * > BasePath, raw_ostream &Out)=0
Mangle vftable symbols.
virtual void mangleTypeName(QualType T, raw_ostream &)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing...
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
virtual ~MangleContext()
Definition: Mangle.h:67
ManglerKind getKind() const
Definition: Mangle.h:60
virtual void mangleCXXCatchableType(QualType T, const CXXConstructorDecl *CD, CXXCtorType CT, uint32_t Size, uint32_t NVOffset, int32_t VBPtrOffset, uint32_t VBIndex, raw_ostream &Out)=0
MicrosoftMangleContext(ASTContext &C, DiagnosticsEngine &D)
Definition: Mangle.h:180
void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID, raw_ostream &Out)
Definition: Mangle.cpp:186
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:221
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1121
static bool classof(const MangleContext *C)
Definition: Mangle.h:170
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &)
Definition: Mangle.cpp:257
virtual void mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived, raw_ostream &Out)=0
virtual void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived, raw_ostream &Out)=0
ItaniumMangleContext(ASTContext &C, DiagnosticsEngine &D)
Definition: Mangle.h:152
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
virtual void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, raw_ostream &)=0
virtual void mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived, ArrayRef< const CXXRecordDecl * > BasePath, raw_ostream &Out)=0
virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &)=0
virtual void mangleCXXVirtualDisplacementMap(const CXXRecordDecl *SrcRD, const CXXRecordDecl *DstRD, raw_ostream &Out)=0
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1452
virtual void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &)=0
NamedDecl - This represents a decl with a name.
Definition: Decl.h:145
DiagnosticsEngine & getDiags() const
Definition: Mangle.h:71
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:203
virtual void mangleCXXCtorComdat(const CXXConstructorDecl *D, raw_ostream &)=0