Coverage Report

Created: 2024-01-17 10:31

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