Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Mangle.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Mangle.cpp - 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
// Implements generic name mangling support for blocks and Objective-C.
10
//
11
//===----------------------------------------------------------------------===//
12
#include "clang/AST/Attr.h"
13
#include "clang/AST/ASTContext.h"
14
#include "clang/AST/Decl.h"
15
#include "clang/AST/DeclCXX.h"
16
#include "clang/AST/DeclObjC.h"
17
#include "clang/AST/DeclTemplate.h"
18
#include "clang/AST/ExprCXX.h"
19
#include "clang/AST/Mangle.h"
20
#include "clang/AST/VTableBuilder.h"
21
#include "clang/Basic/ABI.h"
22
#include "clang/Basic/SourceManager.h"
23
#include "clang/Basic/TargetInfo.h"
24
#include "llvm/ADT/StringExtras.h"
25
#include "llvm/IR/DataLayout.h"
26
#include "llvm/IR/Mangler.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include "llvm/Support/Format.h"
29
#include "llvm/Support/raw_ostream.h"
30
31
using namespace clang;
32
33
// FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
34
// much to be desired. Come up with a better mangling scheme.
35
36
static void mangleFunctionBlock(MangleContext &Context,
37
                                StringRef Outer,
38
                                const BlockDecl *BD,
39
1.01k
                                raw_ostream &Out) {
40
1.01k
  unsigned discriminator = Context.getBlockId(BD, true);
41
1.01k
  if (discriminator == 0)
42
819
    Out << "__" << Outer << "_block_invoke";
43
193
  else
44
193
    Out << "__" << Outer << "_block_invoke_" << discriminator+1;
45
1.01k
}
46
47
0
void MangleContext::anchor() { }
48
49
enum CCMangling {
50
  CCM_Other,
51
  CCM_Fast,
52
  CCM_RegCall,
53
  CCM_Vector,
54
  CCM_Std,
55
  CCM_WasmMainArgcArgv
56
};
57
58
85.6k
static bool isExternC(const NamedDecl *ND) {
59
85.6k
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
60
80.2k
    return FD->isExternC();
61
5.43k
  if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
62
5.42k
    return VD->isExternC();
63
9
  return false;
64
5.43k
}
65
66
static CCMangling getCallingConvMangling(const ASTContext &Context,
67
5.48M
                                         const NamedDecl *ND) {
68
5.48M
  const TargetInfo &TI = Context.getTargetInfo();
69
5.48M
  const llvm::Triple &Triple = TI.getTriple();
70
71
  // On wasm, the argc/argv form of "main" is renamed so that the startup code
72
  // can call it with the correct function signature.
73
  // On Emscripten, users may be exporting "main" and expecting to call it
74
  // themselves, so we can't mangle it.
75
5.48M
  if (Triple.isWasm() && 
!Triple.isOSEmscripten()912
)
76
891
    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
77
799
      if (FD->isMain() && 
FD->hasPrototype()5
&&
FD->param_size() == 25
)
78
4
        return CCM_WasmMainArgcArgv;
79
80
5.48M
  if (!Triple.isOSWindows() || 
!Triple.isX86()139k
)
81
5.34M
    return CCM_Other;
82
83
138k
  if (Context.getLangOpts().CPlusPlus && 
!isExternC(ND)85.6k
&&
84
138k
      
TI.getCXXABI() == TargetCXXABI::Microsoft85.4k
)
85
75.3k
    return CCM_Other;
86
87
62.8k
  const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
88
62.8k
  if (!FD)
89
990
    return CCM_Other;
90
61.8k
  QualType T = FD->getType();
91
92
61.8k
  const FunctionType *FT = T->castAs<FunctionType>();
93
94
61.8k
  CallingConv CC = FT->getCallConv();
95
61.8k
  switch (CC) {
96
61.4k
  default:
97
61.4k
    return CCM_Other;
98
87
  case CC_X86FastCall:
99
87
    return CCM_Fast;
100
54
  case CC_X86StdCall:
101
54
    return CCM_Std;
102
187
  case CC_X86VectorCall:
103
187
    return CCM_Vector;
104
61.8k
  }
105
61.8k
}
106
107
4.19M
bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
108
4.19M
  const ASTContext &ASTContext = getASTContext();
109
110
4.19M
  CCMangling CC = getCallingConvMangling(ASTContext, D);
111
4.19M
  if (CC != CCM_Other)
112
180
    return true;
113
114
  // If the declaration has an owning module for linkage purposes that needs to
115
  // be mangled, we must mangle its name.
116
4.19M
  if (!D->hasExternalFormalLinkage() && 
D->getOwningModuleForLinkage()2.05M
)
117
54
    return true;
118
119
  // C functions with internal linkage have to be mangled with option
120
  // -funique-internal-linkage-names.
121
4.19M
  if (!getASTContext().getLangOpts().CPlusPlus &&
122
4.19M
      
isUniqueInternalLinkageDecl(D)1.68M
)
123
8
    return true;
124
125
  // In C, functions with no attributes never need to be mangled. Fastpath them.
126
4.19M
  if (!getASTContext().getLangOpts().CPlusPlus && 
!D->hasAttrs()1.68M
)
127
106k
    return false;
128
129
  // Any decl can be declared with __asm("foo") on it, and this takes precedence
130
  // over all other naming in the .o file.
131
4.08M
  if (D->hasAttr<AsmLabelAttr>())
132
9.33k
    return true;
133
134
  // Declarations that don't have identifier names always need to be mangled.
135
4.07M
  if (isa<MSGuidDecl>(D))
136
32
    return true;
137
138
4.07M
  return shouldMangleCXXName(D);
139
4.07M
}
140
141
1.30M
void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
142
1.30M
  const ASTContext &ASTContext = getASTContext();
143
1.30M
  const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
144
145
  // Any decl can be declared with __asm("foo") on it, and this takes precedence
146
  // over all other naming in the .o file.
147
1.30M
  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
148
    // If we have an asm name, then we use it as the mangling.
149
150
    // If the label isn't literal, or if this is an alias for an LLVM intrinsic,
151
    // do not add a "\01" prefix.
152
9.36k
    if (!ALA->getIsLiteralLabel() || 
ALA->getLabel().startswith("llvm.")9.05k
) {
153
313
      Out << ALA->getLabel();
154
313
      return;
155
313
    }
156
157
    // Adding the prefix can cause problems when one file has a "foo" and
158
    // another has a "\01foo". That is known to happen on ELF with the
159
    // tricks normally used for producing aliases (PR9177). Fortunately the
160
    // llvm mangler on ELF is a nop, so we can just avoid adding the \01
161
    // marker.
162
9.05k
    StringRef UserLabelPrefix =
163
9.05k
        getASTContext().getTargetInfo().getUserLabelPrefix();
164
9.05k
#ifndef NDEBUG
165
9.05k
    char GlobalPrefix =
166
9.05k
        llvm::DataLayout(getASTContext().getTargetInfo().getDataLayoutString())
167
9.05k
            .getGlobalPrefix();
168
9.05k
    assert((UserLabelPrefix.empty() && !GlobalPrefix) ||
169
9.05k
           (UserLabelPrefix.size() == 1 && UserLabelPrefix[0] == GlobalPrefix));
170
0
#endif
171
9.05k
    if (!UserLabelPrefix.empty())
172
9.02k
      Out << '\01'; // LLVM IR Marker for __asm("foo")
173
174
9.05k
    Out << ALA->getLabel();
175
9.05k
    return;
176
9.36k
  }
177
178
1.29M
  if (auto *GD = dyn_cast<MSGuidDecl>(D))
179
32
    return mangleMSGuidDecl(GD, Out);
180
181
1.29M
  CCMangling CC = getCallingConvMangling(ASTContext, D);
182
183
1.29M
  if (CC == CCM_WasmMainArgcArgv) {
184
2
    Out << "__main_argc_argv";
185
2
    return;
186
2
  }
187
188
1.29M
  bool MCXX = shouldMangleCXXName(D);
189
1.29M
  const TargetInfo &TI = Context.getTargetInfo();
190
1.29M
  if (CC == CCM_Other || 
(150
MCXX150
&&
TI.getCXXABI() == TargetCXXABI::Microsoft28
)) {
191
1.29M
    if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
192
0
      mangleObjCMethodNameAsSourceName(OMD, Out);
193
1.29M
    else
194
1.29M
      mangleCXXName(GD, Out);
195
1.29M
    return;
196
1.29M
  }
197
198
149
  Out << '\01';
199
149
  if (CC == CCM_Std)
200
21
    Out << '_';
201
128
  else if (CC == CCM_Fast)
202
38
    Out << '@';
203
90
  else if (CC == CCM_RegCall)
204
0
    Out << "__regcall3__";
205
206
149
  if (!MCXX)
207
122
    Out << D->getIdentifier()->getName();
208
27
  else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
209
0
    mangleObjCMethodNameAsSourceName(OMD, Out);
210
27
  else
211
27
    mangleCXXName(GD, Out);
212
213
149
  const FunctionDecl *FD = cast<FunctionDecl>(D);
214
149
  const FunctionType *FT = FD->getType()->castAs<FunctionType>();
215
149
  const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
216
149
  if (CC == CCM_Vector)
217
90
    Out << '@';
218
149
  Out << '@';
219
149
  if (!Proto) {
220
5
    Out << '0';
221
5
    return;
222
5
  }
223
144
  assert(!Proto->isVariadic());
224
0
  unsigned ArgWords = 0;
225
144
  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
226
5
    if (!MD->isStatic())
227
4
      ++ArgWords;
228
251
  for (const auto &AT : Proto->param_types()) {
229
    // If an argument type is incomplete there is no way to get its size to
230
    // correctly encode into the mangling scheme.
231
    // Follow GCCs behaviour by simply breaking out of the loop.
232
251
    if (AT->isIncompleteType())
233
1
      break;
234
    // Size should be aligned to pointer size.
235
250
    ArgWords +=
236
250
        llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
237
250
        TI.getPointerWidth(0);
238
250
  }
239
144
  Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
240
144
}
241
242
67
void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &Out) {
243
  // For now, follow the MSVC naming convention for GUID objects on all
244
  // targets.
245
67
  MSGuidDecl::Parts P = GD->getParts();
246
67
  Out << llvm::format("_GUID_%08" PRIx32 "_%04" PRIx32 "_%04" PRIx32 "_",
247
67
                      P.Part1, P.Part2, P.Part3);
248
67
  unsigned I = 0;
249
536
  for (uint8_t C : P.Part4And5) {
250
536
    Out << llvm::format("%02" PRIx8, C);
251
536
    if (++I == 2)
252
67
      Out << "_";
253
536
  }
254
67
}
255
256
void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
257
                                      const NamedDecl *ID,
258
107
                                      raw_ostream &Out) {
259
107
  unsigned discriminator = getBlockId(BD, false);
260
107
  if (ID) {
261
77
    if (shouldMangleDeclName(ID))
262
12
      mangleName(ID, Out);
263
65
    else {
264
65
      Out << ID->getIdentifier()->getName();
265
65
    }
266
77
  }
267
107
  if (discriminator == 0)
268
74
    Out << "_block_invoke";
269
33
  else
270
33
    Out << "_block_invoke_" << discriminator+1;
271
107
}
272
273
void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
274
                                    CXXCtorType CT, const BlockDecl *BD,
275
32
                                    raw_ostream &ResStream) {
276
32
  SmallString<64> Buffer;
277
32
  llvm::raw_svector_ostream Out(Buffer);
278
32
  mangleName(GlobalDecl(CD, CT), Out);
279
32
  mangleFunctionBlock(*this, Buffer, BD, ResStream);
280
32
}
281
282
void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
283
                                    CXXDtorType DT, const BlockDecl *BD,
284
10
                                    raw_ostream &ResStream) {
285
10
  SmallString<64> Buffer;
286
10
  llvm::raw_svector_ostream Out(Buffer);
287
10
  mangleName(GlobalDecl(DD, DT), Out);
288
10
  mangleFunctionBlock(*this, Buffer, BD, ResStream);
289
10
}
290
291
void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
292
970
                                raw_ostream &Out) {
293
970
  assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
294
295
0
  SmallString<64> Buffer;
296
970
  llvm::raw_svector_ostream Stream(Buffer);
297
970
  if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
298
41
    mangleObjCMethodNameAsSourceName(Method, Stream);
299
929
  } else {
300
929
    assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
301
929
           "expected a NamedDecl or BlockDecl");
302
929
    if (isa<BlockDecl>(DC))
303
0
      for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
304
0
        (void) getBlockId(cast<BlockDecl>(DC), true);
305
929
    assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
306
929
           "expected a TranslationUnitDecl or a NamedDecl");
307
929
    if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
308
0
      mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
309
929
    else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
310
0
      mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
311
929
    else if (auto ND = dyn_cast<NamedDecl>(DC)) {
312
929
      if (!shouldMangleDeclName(ND) && 
ND->getIdentifier()697
)
313
697
        Stream << ND->getIdentifier()->getName();
314
232
      else {
315
        // FIXME: We were doing a mangleUnqualifiedName() before, but that's
316
        // a private member of a class that will soon itself be private to the
317
        // Itanium C++ ABI object. What should we do now? Right now, I'm just
318
        // calling the mangleName() method on the MangleContext; is there a
319
        // better way?
320
232
        mangleName(ND, Stream);
321
232
      }
322
929
    }
323
929
  }
324
0
  mangleFunctionBlock(*this, Buffer, BD, Out);
325
970
}
326
327
void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
328
                                         raw_ostream &OS,
329
                                         bool includePrefixByte,
330
3.17k
                                         bool includeCategoryNamespace) {
331
3.17k
  if (getASTContext().getLangOpts().ObjCRuntime.isGNUFamily()) {
332
    // This is the mangling we've always used on the GNU runtimes, but it
333
    // has obvious collisions in the face of underscores within class
334
    // names, category names, and selectors; maybe we should improve it.
335
336
88
    OS << (MD->isClassMethod() ? 
"_c_"8
:
"_i_"80
)
337
88
       << MD->getClassInterface()->getName() << '_';
338
339
88
    if (includeCategoryNamespace) {
340
88
      if (auto category = MD->getCategory())
341
2
        OS << category->getName();
342
88
    }
343
88
    OS << '_';
344
345
88
    auto selector = MD->getSelector();
346
88
    for (unsigned slotIndex = 0,
347
88
                  numArgs = selector.getNumArgs(),
348
88
                  slotEnd = std::max(numArgs, 1U);
349
178
           slotIndex != slotEnd; 
++slotIndex90
) {
350
90
      if (auto name = selector.getIdentifierInfoForSlot(slotIndex))
351
90
        OS << name->getName();
352
353
      // Replace all the positions that would've been ':' with '_'.
354
      // That's after each slot except that a unary selector doesn't
355
      // end in ':'.
356
90
      if (numArgs)
357
34
        OS << '_';
358
90
    }
359
360
88
    return;
361
88
  }
362
363
  // \01+[ContainerName(CategoryName) SelectorName]
364
3.09k
  if (includePrefixByte) {
365
2.58k
    OS << '\01';
366
2.58k
  }
367
3.09k
  OS << (MD->isInstanceMethod() ? 
'-'2.79k
:
'+'292
) << '[';
368
3.09k
  if (const auto *CID = MD->getCategory()) {
369
433
    OS << CID->getClassInterface()->getName();
370
433
    if (includeCategoryNamespace) {
371
427
      OS << '(' << *CID << ')';
372
427
    }
373
2.65k
  } else if (const auto *CD =
374
2.65k
                 dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
375
2.65k
    OS << CD->getName();
376
2.65k
  } else {
377
0
    llvm_unreachable("Unexpected ObjC method decl context");
378
0
  }
379
3.09k
  OS << ' ';
380
3.09k
  MD->getSelector().print(OS);
381
3.09k
  OS << ']';
382
3.09k
}
383
384
void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
385
311
                                                     raw_ostream &Out) {
386
311
  SmallString<64> Name;
387
311
  llvm::raw_svector_ostream OS(Name);
388
389
311
  mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
390
311
                       /*includeCategoryNamespace=*/true);
391
311
  Out << OS.str().size() << OS.str();
392
311
}
393
394
class ASTNameGenerator::Implementation {
395
  std::unique_ptr<MangleContext> MC;
396
  llvm::DataLayout DL;
397
398
public:
399
  explicit Implementation(ASTContext &Ctx)
400
      : MC(Ctx.createMangleContext()),
401
415
        DL(Ctx.getTargetInfo().getDataLayoutString()) {}
402
403
2.80k
  bool writeName(const Decl *D, raw_ostream &OS) {
404
    // First apply frontend mangling.
405
2.80k
    SmallString<128> FrontendBuf;
406
2.80k
    llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
407
2.80k
    if (auto *FD = dyn_cast<FunctionDecl>(D)) {
408
734
      if (FD->isDependentContext())
409
141
        return true;
410
593
      if (writeFuncOrVarName(FD, FrontendBufOS))
411
0
        return true;
412
2.07k
    } else if (auto *VD = dyn_cast<VarDecl>(D)) {
413
209
      if (writeFuncOrVarName(VD, FrontendBufOS))
414
0
        return true;
415
1.86k
    } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
416
199
      MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
417
199
                               /*includeCategoryNamespace=*/true);
418
199
      return false;
419
1.66k
    } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
420
117
      writeObjCClassName(ID, FrontendBufOS);
421
1.55k
    } else {
422
1.55k
      return true;
423
1.55k
    }
424
425
    // Now apply backend mangling.
426
919
    llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
427
919
    return false;
428
2.80k
  }
429
430
1.43k
  std::string getName(const Decl *D) {
431
1.43k
    std::string Name;
432
1.43k
    {
433
1.43k
      llvm::raw_string_ostream OS(Name);
434
1.43k
      writeName(D, OS);
435
1.43k
    }
436
1.43k
    return Name;
437
1.43k
  }
438
439
  enum ObjCKind {
440
    ObjCClass,
441
    ObjCMetaclass,
442
  };
443
444
  static StringRef getClassSymbolPrefix(ObjCKind Kind,
445
125
                                        const ASTContext &Context) {
446
125
    if (Context.getLangOpts().ObjCRuntime.isGNUFamily())
447
4
      return Kind == ObjCMetaclass ? 
"_OBJC_METACLASS_"2
:
"_OBJC_CLASS_"2
;
448
121
    return Kind == ObjCMetaclass ? 
"OBJC_METACLASS_$_"2
:
"OBJC_CLASS_$_"119
;
449
125
  }
450
451
4
  std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) {
452
4
    StringRef ClassName;
453
4
    if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
454
2
      ClassName = OID->getObjCRuntimeNameAsString();
455
2
    else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
456
2
      ClassName = OID->getObjCRuntimeNameAsString();
457
458
4
    if (ClassName.empty())
459
0
      return {};
460
461
8
    
auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string 4
{
462
8
      SmallString<40> Mangled;
463
8
      auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext());
464
8
      llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
465
8
      return std::string(Mangled.str());
466
8
    };
467
468
4
    return {
469
4
        Mangle(ObjCClass, ClassName),
470
4
        Mangle(ObjCMetaclass, ClassName),
471
4
    };
472
4
  }
473
474
192
  std::vector<std::string> getAllManglings(const Decl *D) {
475
192
    if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D))
476
4
      return getAllManglings(OCD);
477
478
188
    if (!(isa<CXXRecordDecl>(D) || 
isa<CXXMethodDecl>(D)167
))
479
104
      return {};
480
481
84
    const NamedDecl *ND = cast<NamedDecl>(D);
482
483
84
    ASTContext &Ctx = ND->getASTContext();
484
84
    std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
485
486
84
    std::vector<std::string> Manglings;
487
488
84
    auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
489
1
      auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
490
1
                                                     /*IsCXXMethod=*/true);
491
1
      auto CC = MD->getType()->castAs<FunctionProtoType>()->getCallConv();
492
1
      return CC == DefaultCC;
493
1
    };
494
495
84
    if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
496
22
      Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
497
498
22
      if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
499
18
        if (!CD->getParent()->isAbstract())
500
16
          Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete));
501
502
22
      if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
503
4
        if (CD->hasAttr<DLLExportAttr>() && 
CD->isDefaultConstructor()1
)
504
1
          if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
505
1
            Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure));
506
62
    } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
507
15
      Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
508
15
      if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
509
12
        Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
510
12
        if (DD->isVirtual())
511
10
          Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
512
12
      }
513
47
    } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
514
26
      Manglings.emplace_back(getName(ND));
515
26
      if (MD->isVirtual())
516
20
        if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
517
3
          for (const auto &T : *TIV)
518
3
            Manglings.emplace_back(getMangledThunk(MD, T));
519
26
    }
520
521
84
    return Manglings;
522
188
  }
523
524
private:
525
802
  bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) {
526
802
    if (MC->shouldMangleDeclName(D)) {
527
473
      GlobalDecl GD;
528
473
      if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
529
67
        GD = GlobalDecl(CtorD, Ctor_Complete);
530
406
      else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
531
42
        GD = GlobalDecl(DtorD, Dtor_Complete);
532
364
      else if (D->hasAttr<CUDAGlobalAttr>())
533
0
        GD = GlobalDecl(cast<FunctionDecl>(D));
534
364
      else
535
364
        GD = GlobalDecl(D);
536
473
      MC->mangleName(GD, OS);
537
473
      return false;
538
473
    } else {
539
329
      IdentifierInfo *II = D->getIdentifier();
540
329
      if (!II)
541
0
        return true;
542
329
      OS << II->getName();
543
329
      return false;
544
329
    }
545
802
  }
546
547
117
  void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) {
548
117
    OS << getClassSymbolPrefix(ObjCClass, D->getASTContext());
549
117
    OS << D->getObjCRuntimeNameAsString();
550
117
  }
551
552
76
  std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) {
553
76
    std::string FrontendBuf;
554
76
    llvm::raw_string_ostream FOS(FrontendBuf);
555
556
76
    GlobalDecl GD;
557
76
    if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
558
39
      GD = GlobalDecl(CD, static_cast<CXXCtorType>(StructorType));
559
37
    else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
560
37
      GD = GlobalDecl(DD, static_cast<CXXDtorType>(StructorType));
561
76
    MC->mangleName(GD, FOS);
562
563
76
    std::string BackendBuf;
564
76
    llvm::raw_string_ostream BOS(BackendBuf);
565
566
76
    llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
567
568
76
    return BOS.str();
569
76
  }
570
571
3
  std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) {
572
3
    std::string FrontendBuf;
573
3
    llvm::raw_string_ostream FOS(FrontendBuf);
574
575
3
    MC->mangleThunk(MD, T, FOS);
576
577
3
    std::string BackendBuf;
578
3
    llvm::raw_string_ostream BOS(BackendBuf);
579
580
3
    llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
581
582
3
    return BOS.str();
583
3
  }
584
};
585
586
ASTNameGenerator::ASTNameGenerator(ASTContext &Ctx)
587
415
    : Impl(std::make_unique<Implementation>(Ctx)) {}
588
589
415
ASTNameGenerator::~ASTNameGenerator() {}
590
591
1.37k
bool ASTNameGenerator::writeName(const Decl *D, raw_ostream &OS) {
592
1.37k
  return Impl->writeName(D, OS);
593
1.37k
}
594
595
1.41k
std::string ASTNameGenerator::getName(const Decl *D) {
596
1.41k
  return Impl->getName(D);
597
1.41k
}
598
599
192
std::vector<std::string> ASTNameGenerator::getAllManglings(const Decl *D) {
600
192
  return Impl->getAllManglings(D);
601
192
}