Coverage Report

Created: 2022-05-14 11:35

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/Mangle.h
Line
Count
Source (jump to first uncovered line)
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
  /// For aux target. If true, uses mangling number for aux target from
58
  /// ASTContext.
59
  bool IsAux = false;
60
61
  llvm::DenseMap<const BlockDecl*, unsigned> GlobalBlockIds;
62
  llvm::DenseMap<const BlockDecl*, unsigned> LocalBlockIds;
63
  llvm::DenseMap<const NamedDecl*, uint64_t> AnonStructIds;
64
65
public:
66
0
  ManglerKind getKind() const { return Kind; }
67
68
4.87k
  bool isAux() const { return IsAux; }
69
70
  explicit MangleContext(ASTContext &Context, DiagnosticsEngine &Diags,
71
                         ManglerKind Kind, bool IsAux = false)
72
105k
      : Context(Context), Diags(Diags), Kind(Kind), IsAux(IsAux) {}
73
74
102k
  virtual ~MangleContext() { }
75
76
55.4M
  ASTContext &getASTContext() const { return Context; }
77
78
21
  DiagnosticsEngine &getDiags() const { return Diags; }
79
80
289k
  virtual void startNewFunction() { LocalBlockIds.clear(); }
81
82
1.25k
  unsigned getBlockId(const BlockDecl *BD, bool Local) {
83
1.25k
    llvm::DenseMap<const BlockDecl *, unsigned> &BlockIds
84
1.25k
      = Local? 
LocalBlockIds1.09k
:
GlobalBlockIds163
;
85
1.25k
    std::pair<llvm::DenseMap<const BlockDecl *, unsigned>::iterator, bool>
86
1.25k
      Result = BlockIds.insert(std::make_pair(BD, BlockIds.size()));
87
1.25k
    return Result.first->second;
88
1.25k
  }
89
90
4.50k
  uint64_t getAnonymousStructId(const NamedDecl *D) {
91
4.50k
    std::pair<llvm::DenseMap<const NamedDecl *, uint64_t>::iterator, bool>
92
4.50k
        Result = AnonStructIds.insert(std::make_pair(D, AnonStructIds.size()));
93
4.50k
    return Result.first->second;
94
4.50k
  }
95
96
0
  uint64_t getAnonymousStructIdForDebugInfo(const NamedDecl *D) {
97
0
    llvm::DenseMap<const NamedDecl *, uint64_t>::iterator Result =
98
0
        AnonStructIds.find(D);
99
    // The decl should already be inserted, but return 0 in case it is not.
100
0
    if (Result == AnonStructIds.end())
101
0
      return 0;
102
0
    return Result->second;
103
0
  }
104
105
  virtual std::string getLambdaString(const CXXRecordDecl *Lambda) = 0;
106
107
  /// @name Mangler Entry Points
108
  /// @{
109
110
  bool shouldMangleDeclName(const NamedDecl *D);
111
  virtual bool shouldMangleCXXName(const NamedDecl *D) = 0;
112
  virtual bool shouldMangleStringLiteral(const StringLiteral *SL) = 0;
113
114
56.2k
  virtual bool isUniqueInternalLinkageDecl(const NamedDecl *ND) {
115
56.2k
    return false;
116
56.2k
  }
117
118
0
  virtual void needsUniqueInternalLinkageNames() { }
119
120
  // FIXME: consider replacing raw_ostream & with something like SmallString &.
121
  void mangleName(GlobalDecl GD, raw_ostream &);
122
  virtual void mangleCXXName(GlobalDecl GD, raw_ostream &) = 0;
123
  virtual void mangleThunk(const CXXMethodDecl *MD,
124
                          const ThunkInfo &Thunk,
125
                          raw_ostream &) = 0;
126
  virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
127
                                  const ThisAdjustment &ThisAdjustment,
128
                                  raw_ostream &) = 0;
129
  virtual void mangleReferenceTemporary(const VarDecl *D,
130
                                        unsigned ManglingNumber,
131
                                        raw_ostream &) = 0;
132
  virtual void mangleCXXRTTI(QualType T, raw_ostream &) = 0;
133
  virtual void mangleCXXRTTIName(QualType T, raw_ostream &) = 0;
134
  virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &) = 0;
135
  virtual void mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream&);
136
137
  void mangleGlobalBlock(const BlockDecl *BD,
138
                         const NamedDecl *ID,
139
                         raw_ostream &Out);
140
  void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT,
141
                       const BlockDecl *BD, raw_ostream &Out);
142
  void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT,
143
                       const BlockDecl *BD, raw_ostream &Out);
144
  void mangleBlock(const DeclContext *DC, const BlockDecl *BD,
145
                   raw_ostream &Out);
146
147
  void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &OS,
148
                            bool includePrefixByte = true,
149
                            bool includeCategoryNamespace = true);
150
  void mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
151
                                        raw_ostream &);
152
153
  virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) = 0;
154
155
  virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &) = 0;
156
157
  virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
158
                                             raw_ostream &) = 0;
159
160
  virtual void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl,
161
                                         raw_ostream &Out) = 0;
162
163
  virtual void mangleSEHFinallyBlock(const NamedDecl *EnclosingDecl,
164
                                     raw_ostream &Out) = 0;
165
166
  /// Generates a unique string for an externally visible type for use with TBAA
167
  /// or type uniquing.
168
  /// TODO: Extend this to internal types by generating names that are unique
169
  /// across translation units so it can be used with LTO.
170
  virtual void mangleTypeName(QualType T, raw_ostream &) = 0;
171
172
  /// @}
173
};
174
175
class ItaniumMangleContext : public MangleContext {
176
public:
177
  using DiscriminatorOverrideTy =
178
      llvm::Optional<unsigned> (*)(ASTContext &, const NamedDecl *);
179
  explicit ItaniumMangleContext(ASTContext &C, DiagnosticsEngine &D,
180
                                bool IsAux = false)
181
104k
      : MangleContext(C, D, MK_Itanium, IsAux) {}
182
183
  virtual void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) = 0;
184
  virtual void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) = 0;
185
  virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
186
                                   const CXXRecordDecl *Type,
187
                                   raw_ostream &) = 0;
188
  virtual void mangleItaniumThreadLocalInit(const VarDecl *D,
189
                                            raw_ostream &) = 0;
190
  virtual void mangleItaniumThreadLocalWrapper(const VarDecl *D,
191
                                               raw_ostream &) = 0;
192
193
  virtual void mangleCXXCtorComdat(const CXXConstructorDecl *D,
194
                                   raw_ostream &) = 0;
195
  virtual void mangleCXXDtorComdat(const CXXDestructorDecl *D,
196
                                   raw_ostream &) = 0;
197
198
  virtual void mangleLambdaSig(const CXXRecordDecl *Lambda, raw_ostream &) = 0;
199
200
  virtual void mangleDynamicStermFinalizer(const VarDecl *D, raw_ostream &) = 0;
201
202
  // This has to live here, otherwise the CXXNameMangler won't have access to
203
  // it.
204
  virtual DiscriminatorOverrideTy getDiscriminatorOverride() const = 0;
205
0
  static bool classof(const MangleContext *C) {
206
0
    return C->getKind() == MK_Itanium;
207
0
  }
208
209
  static ItaniumMangleContext *
210
  create(ASTContext &Context, DiagnosticsEngine &Diags, bool IsAux = false);
211
  static ItaniumMangleContext *create(ASTContext &Context,
212
                                      DiagnosticsEngine &Diags,
213
                                      DiscriminatorOverrideTy Discriminator,
214
                                      bool IsAux = false);
215
};
216
217
class MicrosoftMangleContext : public MangleContext {
218
public:
219
  explicit MicrosoftMangleContext(ASTContext &C, DiagnosticsEngine &D,
220
                                  bool IsAux = false)
221
868
      : MangleContext(C, D, MK_Microsoft, IsAux) {}
222
223
  /// Mangle vftable symbols.  Only a subset of the bases along the path
224
  /// to the vftable are included in the name.  It's up to the caller to pick
225
  /// them correctly.
226
  virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
227
                                ArrayRef<const CXXRecordDecl *> BasePath,
228
                                raw_ostream &Out) = 0;
229
230
  /// Mangle vbtable symbols.  Only a subset of the bases along the path
231
  /// to the vbtable are included in the name.  It's up to the caller to pick
232
  /// them correctly.
233
  virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
234
                                ArrayRef<const CXXRecordDecl *> BasePath,
235
                                raw_ostream &Out) = 0;
236
237
  virtual void mangleThreadSafeStaticGuardVariable(const VarDecl *VD,
238
                                                   unsigned GuardNum,
239
                                                   raw_ostream &Out) = 0;
240
241
  virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
242
                                        const MethodVFTableLocation &ML,
243
                                        raw_ostream &Out) = 0;
244
245
  virtual void mangleCXXVirtualDisplacementMap(const CXXRecordDecl *SrcRD,
246
                                               const CXXRecordDecl *DstRD,
247
                                               raw_ostream &Out) = 0;
248
249
  virtual void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile,
250
                                  bool IsUnaligned, uint32_t NumEntries,
251
                                  raw_ostream &Out) = 0;
252
253
  virtual void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries,
254
                                           raw_ostream &Out) = 0;
255
256
  virtual void mangleCXXCatchableType(QualType T, const CXXConstructorDecl *CD,
257
                                      CXXCtorType CT, uint32_t Size,
258
                                      uint32_t NVOffset, int32_t VBPtrOffset,
259
                                      uint32_t VBIndex, raw_ostream &Out) = 0;
260
261
  virtual void mangleCXXRTTIBaseClassDescriptor(
262
      const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset,
263
      uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) = 0;
264
265
  virtual void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived,
266
                                           raw_ostream &Out) = 0;
267
  virtual void
268
  mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived,
269
                                        raw_ostream &Out) = 0;
270
271
  virtual void
272
  mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived,
273
                                     ArrayRef<const CXXRecordDecl *> BasePath,
274
                                     raw_ostream &Out) = 0;
275
276
0
  static bool classof(const MangleContext *C) {
277
0
    return C->getKind() == MK_Microsoft;
278
0
  }
279
280
  static MicrosoftMangleContext *
281
  create(ASTContext &Context, DiagnosticsEngine &Diags, bool IsAux = false);
282
};
283
284
class ASTNameGenerator {
285
public:
286
  explicit ASTNameGenerator(ASTContext &Ctx);
287
  ~ASTNameGenerator();
288
289
  /// Writes name for \p D to \p OS.
290
  /// \returns true on failure, false on success.
291
  bool writeName(const Decl *D, raw_ostream &OS);
292
293
  /// \returns name for \p D
294
  std::string getName(const Decl *D);
295
296
  /// \returns all applicable mangled names.
297
  /// For example C++ constructors/destructors can have multiple.
298
  std::vector<std::string> getAllManglings(const Decl *D);
299
300
private:
301
  class Implementation;
302
  std::unique_ptr<Implementation> Impl;
303
};
304
}
305
306
#endif