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