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