Coverage Report

Created: 2021-01-26 06:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/Mangle.h
Line
Count
Source
1
//===--- Mangle.h - Mangle C++ Names ----------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Defines the C++ name mangling interface.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_AST_MANGLE_H
14
#define LLVM_CLANG_AST_MANGLE_H
15
16
#include "clang/AST/Decl.h"
17
#include "clang/AST/GlobalDecl.h"
18
#include "clang/AST/Type.h"
19
#include "clang/Basic/ABI.h"
20
#include "llvm/ADT/DenseMap.h"
21
#include "llvm/Support/Casting.h"
22
23
namespace llvm {
24
  class raw_ostream;
25
}
26
27
namespace clang {
28
  class ASTContext;
29
  class BlockDecl;
30
  class CXXConstructorDecl;
31
  class CXXDestructorDecl;
32
  class CXXMethodDecl;
33
  class FunctionDecl;
34
  struct MethodVFTableLocation;
35
  class NamedDecl;
36
  class ObjCMethodDecl;
37
  class StringLiteral;
38
  struct ThisAdjustment;
39
  struct ThunkInfo;
40
  class VarDecl;
41
42
/// MangleContext - Context for tracking state which persists across multiple
43
/// calls to the C++ name mangler.
44
class MangleContext {
45
public:
46
  enum ManglerKind {
47
    MK_Itanium,
48
    MK_Microsoft
49
  };
50
51
private:
52
  virtual void anchor();
53
54
  ASTContext &Context;
55
  DiagnosticsEngine &Diags;
56
  const ManglerKind Kind;
57
58
  llvm::DenseMap<const BlockDecl*, unsigned> GlobalBlockIds;
59
  llvm::DenseMap<const BlockDecl*, unsigned> LocalBlockIds;
60
  llvm::DenseMap<const NamedDecl*, uint64_t> AnonStructIds;
61
62
public:
63
46.2k
  ManglerKind getKind() const { return Kind; }
64
65
  explicit MangleContext(ASTContext &Context,
66
                         DiagnosticsEngine &Diags,
67
                         ManglerKind Kind)
68
94.1k
      : Context(Context), Diags(Diags), Kind(Kind) {}
69
70
91.6k
  virtual ~MangleContext() { }
71
72
24.0M
  ASTContext &getASTContext() const { return Context; }
73
74
3
  DiagnosticsEngine &getDiags() const { return Diags; }
75
76
271k
  virtual void startNewFunction() { LocalBlockIds.clear(); }
77
78
1.13k
  unsigned getBlockId(const BlockDecl *BD, bool Local) {
79
1.13k
    llvm::DenseMap<const BlockDecl *, unsigned> &BlockIds
80
974
      = Local? LocalBlockIds : 
GlobalBlockIds156
;
81
1.13k
    std::pair<llvm::DenseMap<const BlockDecl *, unsigned>::iterator, bool>
82
1.13k
      Result = BlockIds.insert(std::make_pair(BD, BlockIds.size()));
83
1.13k
    return Result.first->second;
84
1.13k
  }
85
86
8.11k
  uint64_t getAnonymousStructId(const NamedDecl *D) {
87
8.11k
    std::pair<llvm::DenseMap<const NamedDecl *, uint64_t>::iterator, bool>
88
8.11k
        Result = AnonStructIds.insert(std::make_pair(D, AnonStructIds.size()));
89
8.11k
    return Result.first->second;
90
8.11k
  }
91
92
  /// @name Mangler Entry Points
93
  /// @{
94
95
  bool shouldMangleDeclName(const NamedDecl *D);
96
  virtual bool shouldMangleCXXName(const NamedDecl *D) = 0;
97
  virtual bool shouldMangleStringLiteral(const StringLiteral *SL) = 0;
98
99
  // FIXME: consider replacing raw_ostream & with something like SmallString &.
100
  void mangleName(GlobalDecl GD, raw_ostream &);
101
  virtual void mangleCXXName(GlobalDecl GD, raw_ostream &) = 0;
102
  virtual void mangleThunk(const CXXMethodDecl *MD,
103
                          const ThunkInfo &Thunk,
104
                          raw_ostream &) = 0;
105
  virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
106
                                  const ThisAdjustment &ThisAdjustment,
107
                                  raw_ostream &) = 0;
108
  virtual void mangleReferenceTemporary(const VarDecl *D,
109
                                        unsigned ManglingNumber,
110
                                        raw_ostream &) = 0;
111
  virtual void mangleCXXRTTI(QualType T, raw_ostream &) = 0;
112
  virtual void mangleCXXRTTIName(QualType T, raw_ostream &) = 0;
113
  virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &) = 0;
114
  virtual void mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream&);
115
116
  void mangleGlobalBlock(const BlockDecl *BD,
117
                         const NamedDecl *ID,
118
                         raw_ostream &Out);
119
  void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT,
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 &OS,
127
                            bool includePrefixByte = true,
128
                            bool includeCategoryNamespace = true);
129
  void mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
130
                                        raw_ostream &);
131
132
  virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) = 0;
133
134
  virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &) = 0;
135
136
  virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
137
                                             raw_ostream &) = 0;
138
139
  virtual void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl,
140
                                         raw_ostream &Out) = 0;
141
142
  virtual void mangleSEHFinallyBlock(const NamedDecl *EnclosingDecl,
143
                                     raw_ostream &Out) = 0;
144
145
  /// Generates a unique string for an externally visible type for use with TBAA
146
  /// or type uniquing.
147
  /// TODO: Extend this to internal types by generating names that are unique
148
  /// across translation units so it can be used with LTO.
149
  virtual void mangleTypeName(QualType T, raw_ostream &) = 0;
150
151
  /// @}
152
};
153
154
class ItaniumMangleContext : public MangleContext {
155
public:
156
  explicit ItaniumMangleContext(ASTContext &C, DiagnosticsEngine &D)
157
93.3k
      : MangleContext(C, D, MK_Itanium) {}
158
159
  virtual void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) = 0;
160
  virtual void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) = 0;
161
  virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
162
                                   const CXXRecordDecl *Type,
163
                                   raw_ostream &) = 0;
164
  virtual void mangleItaniumThreadLocalInit(const VarDecl *D,
165
                                            raw_ostream &) = 0;
166
  virtual void mangleItaniumThreadLocalWrapper(const VarDecl *D,
167
                                               raw_ostream &) = 0;
168
169
  virtual void mangleCXXCtorComdat(const CXXConstructorDecl *D,
170
                                   raw_ostream &) = 0;
171
  virtual void mangleCXXDtorComdat(const CXXDestructorDecl *D,
172
                                   raw_ostream &) = 0;
173
174
  virtual void mangleLambdaSig(const CXXRecordDecl *Lambda, raw_ostream &) = 0;
175
176
  virtual void mangleDynamicStermFinalizer(const VarDecl *D, raw_ostream &) = 0;
177
178
40.9k
  static bool classof(const MangleContext *C) {
179
40.9k
    return C->getKind() == MK_Itanium;
180
40.9k
  }
181
182
  static ItaniumMangleContext *create(ASTContext &Context,
183
                                      DiagnosticsEngine &Diags);
184
};
185
186
class MicrosoftMangleContext : public MangleContext {
187
public:
188
  explicit MicrosoftMangleContext(ASTContext &C, DiagnosticsEngine &D)
189
795
      : MangleContext(C, D, MK_Microsoft) {}
190
191
  /// Mangle vftable symbols.  Only a subset of the bases along the path
192
  /// to the vftable are included in the name.  It's up to the caller to pick
193
  /// them correctly.
194
  virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
195
                                ArrayRef<const CXXRecordDecl *> BasePath,
196
                                raw_ostream &Out) = 0;
197
198
  /// Mangle vbtable symbols.  Only a subset of the bases along the path
199
  /// to the vbtable are included in the name.  It's up to the caller to pick
200
  /// them correctly.
201
  virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
202
                                ArrayRef<const CXXRecordDecl *> BasePath,
203
                                raw_ostream &Out) = 0;
204
205
  virtual void mangleThreadSafeStaticGuardVariable(const VarDecl *VD,
206
                                                   unsigned GuardNum,
207
                                                   raw_ostream &Out) = 0;
208
209
  virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
210
                                        const MethodVFTableLocation &ML,
211
                                        raw_ostream &Out) = 0;
212
213
  virtual void mangleCXXVirtualDisplacementMap(const CXXRecordDecl *SrcRD,
214
                                               const CXXRecordDecl *DstRD,
215
                                               raw_ostream &Out) = 0;
216
217
  virtual void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile,
218
                                  bool IsUnaligned, uint32_t NumEntries,
219
                                  raw_ostream &Out) = 0;
220
221
  virtual void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries,
222
                                           raw_ostream &Out) = 0;
223
224
  virtual void mangleCXXCatchableType(QualType T, const CXXConstructorDecl *CD,
225
                                      CXXCtorType CT, uint32_t Size,
226
                                      uint32_t NVOffset, int32_t VBPtrOffset,
227
                                      uint32_t VBIndex, raw_ostream &Out) = 0;
228
229
  virtual void mangleCXXRTTIBaseClassDescriptor(
230
      const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset,
231
      uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) = 0;
232
233
  virtual void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived,
234
                                           raw_ostream &Out) = 0;
235
  virtual void
236
  mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived,
237
                                        raw_ostream &Out) = 0;
238
239
  virtual void
240
  mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived,
241
                                     ArrayRef<const CXXRecordDecl *> BasePath,
242
                                     raw_ostream &Out) = 0;
243
244
5.24k
  static bool classof(const MangleContext *C) {
245
5.24k
    return C->getKind() == MK_Microsoft;
246
5.24k
  }
247
248
  static MicrosoftMangleContext *create(ASTContext &Context,
249
                                        DiagnosticsEngine &Diags);
250
};
251
252
class ASTNameGenerator {
253
public:
254
  explicit ASTNameGenerator(ASTContext &Ctx);
255
  ~ASTNameGenerator();
256
257
  /// Writes name for \p D to \p OS.
258
  /// \returns true on failure, false on success.
259
  bool writeName(const Decl *D, raw_ostream &OS);
260
261
  /// \returns name for \p D
262
  std::string getName(const Decl *D);
263
264
  /// \returns all applicable mangled names.
265
  /// For example C++ constructors/destructors can have multiple.
266
  std::vector<std::string> getAllManglings(const Decl *D);
267
268
private:
269
  class Implementation;
270
  std::unique_ptr<Implementation> Impl;
271
};
272
}
273
274
#endif