clang 23.0.0git
Mangle.cpp
Go to the documentation of this file.
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"
14#include "clang/AST/Attr.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclObjC.h"
19#include "clang/AST/ExprCXX.h"
21#include "clang/Basic/ABI.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
59 StringRef Outer,
60 const BlockDecl *BD,
61 raw_ostream &Out) {
62 unsigned discriminator = Context.getBlockId(BD, 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
79
80static bool isExternC(const NamedDecl *ND) {
81 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
82 return FD->isExternC();
83 if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
84 return VD->isExternC();
85 return false;
86}
87
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>(ND))
97 if (FD->isMain() && FD->getNumParams() == 2)
99
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>(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
129
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.
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 &&
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>(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
177
178 if (auto *Ctor = llvm::dyn_cast<clang::CXXConstructorDecl>(GD.getDecl())) {
179 Out << "C";
180 if (Ctor->getInheritedConstructor().getConstructor())
181 Out << "I";
182 Out << GD.getCtorType();
183 } else if (llvm::isa<clang::CXXDestructorDecl>(GD.getDecl())) {
184 Out << "D" << GD.getDtorType();
185 }
186
187 Out << label.substr(LLDBManglingABI::FunctionLabelPrefix.size());
188}
189
190void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
192 const NamedDecl *D = cast<NamedDecl>(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("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 =
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(LLDBManglingABI::FunctionLabelPrefix))
224 emitLLDBAsmLabel(ALA->getLabel(), GD, Out);
225 else
226 Out << ALA->getLabel();
227
228 return;
229 }
230
231 if (auto *GD = dyn_cast<MSGuidDecl>(D))
232 return mangleMSGuidDecl(GD, Out);
233
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>(D))
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>(D))
267 else
268 mangleCXXName(GD, Out);
269
270 const FunctionDecl *FD = cast<FunctionDecl>(D);
271 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
272 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(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>(FD))
283 if (MD->isImplicitObjectMemberFunction())
284 ++ArgWords;
285 uint64_t DefaultPtrWidth = TI.getPointerWidth(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(ASTContext.getTypeSize(AT), DefaultPtrWidth) /
294 DefaultPtrWidth;
295 }
296 Out << ((DefaultPtrWidth / 8) * ArgWords);
297}
298
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("_GUID_%08" PRIx32 "_%04" PRIx32 "_%04" PRIx32 "_",
305 P.Part1, P.Part2, P.Part3);
306 unsigned I = 0;
307 for (uint8_t C : P.Part4And5) {
308 Out << llvm::format("%02" PRIx8, C);
309 if (++I == 2)
310 Out << "_";
311 }
312}
313
315 const NamedDecl *ID,
316 raw_ostream &Out) {
317 unsigned discriminator = getBlockId(BD, false);
318 if (ID) {
319 if (shouldMangleDeclName(ID))
320 mangleName(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
332 CXXCtorType CT, const BlockDecl *BD,
333 raw_ostream &ResStream) {
334 SmallString<64> Buffer;
335 llvm::raw_svector_ostream Out(Buffer);
336 mangleName(GlobalDecl(CD, CT), Out);
337 mangleFunctionBlock(*this, Buffer, BD, ResStream);
338}
339
341 CXXDtorType DT, const BlockDecl *BD,
342 raw_ostream &ResStream) {
343 SmallString<64> Buffer;
344 llvm::raw_svector_ostream Out(Buffer);
345 mangleName(GlobalDecl(DD, DT), Out);
346 mangleFunctionBlock(*this, Buffer, BD, ResStream);
347}
348
350 raw_ostream &Out) {
352
353 SmallString<64> Buffer;
354 llvm::raw_svector_ostream Stream(Buffer);
355 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
357 } else {
358 assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
359 "expected a NamedDecl or BlockDecl");
360 for (; isa_and_nonnull<BlockDecl>(DC); DC = DC->getParent())
361 (void)getBlockId(cast<BlockDecl>(DC), true);
362 assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
363 "expected a TranslationUnitDecl or a NamedDecl");
364 if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
365 mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
366 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
367 mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
368 else if (auto ND = dyn_cast<NamedDecl>(DC)) {
369 if (!shouldMangleDeclName(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(ND, Stream);
378 }
379 }
380 }
381 mangleFunctionBlock(*this, Buffer, BD, Out);
382}
383
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(numArgs, 1U);
407 slotIndex != slotEnd; ++slotIndex) {
408 if (auto name = selector.getIdentifierInfoForSlot(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>(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(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 && !useDirectABI,
448 MD->isInstanceMethod(), ClassName, CategoryName,
449 MethodName, useDirectABI);
450}
451
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
463 std::unique_ptr<MangleContext> MC;
464 llvm::DataLayout DL;
465
466public:
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>(D)) {
476 if (FD->isDependentContext())
477 return true;
478 if (writeFuncOrVarName(FD, FrontendBufOS))
479 return true;
480 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
481 if (writeFuncOrVarName(VD, FrontendBufOS))
482 return true;
483 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
484 MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
485 /*includeCategoryNamespace=*/true);
486 return false;
487 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
488 writeObjCClassName(ID, FrontendBufOS);
489 } else {
490 return true;
491 }
492
493 // Now apply backend mangling.
494 llvm::Mangler::getNameWithPrefix(OS, 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
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>(OCD))
522 ClassName = OID->getObjCRuntimeNameAsString();
523 else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(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, OCD->getASTContext());
532 llvm::Mangler::getNameWithPrefix(Mangled, 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>(D))
544 return getAllManglings(OCD);
545
547 return {};
548
549 const NamedDecl *ND = cast<NamedDecl>(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>(ND)) {
564 Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
565
567 if (!CD->getParent()->isAbstract())
568 Manglings.emplace_back(getMangledStructor(CD, 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(getMangledStructor(CD, Ctor_DefaultClosure));
574 } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
575 Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
577 Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
578 if (DD->isVirtual())
579 Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
580 }
581 } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
582 Manglings.emplace_back(getName(ND));
583 if (MD->isVirtual()) {
584 if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(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(MD, ContextualizedName))
590 ThunkName = getMangledThunk(MD, T, /* ElideOverrideInfo */ true);
591 else
592 ThunkName = ContextualizedName;
593 Manglings.emplace_back(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>(D))
607 GD = GlobalDecl(CtorD, Ctor_Complete);
608 else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
609 GD = GlobalDecl(DtorD, Dtor_Complete);
610 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
611 GD = FD->isReferenceableKernel() ? GlobalDecl(FD) : GlobalDecl(D);
612 } else
613 GD = GlobalDecl(D);
614 MC->mangleName(GD, 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) {
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>(ND))
636 GD = GlobalDecl(CD, static_cast<CXXCtorType>(StructorType));
637 else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
638 GD = GlobalDecl(DD, static_cast<CXXDtorType>(StructorType));
639 MC->mangleName(GD, FOS);
640
641 std::string BackendBuf;
642 llvm::raw_string_ostream BOS(BackendBuf);
643
644 llvm::Mangler::getNameWithPrefix(BOS, 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, T, ElideOverrideInfo, FOS);
655
656 std::string BackendBuf;
657 llvm::raw_string_ostream BOS(BackendBuf);
658
659 llvm::Mangler::getNameWithPrefix(BOS, FrontendBuf, DL);
660
661 return BackendBuf;
662 }
663};
664
666 : Impl(std::make_unique<Implementation>(Ctx)) {}
667
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}
Enums/classes describing ABI related information about constructors, destructors and thunks.
Defines the clang::ASTContext interface.
static bool hasDefaultCXXMethodCC(ASTContext &Context, const CXXMethodDecl *MD)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Defines the clang::Expr interface and subclasses for C++ expressions.
CCMangling
Definition Mangle.cpp:71
@ CCM_Fast
Definition Mangle.cpp:73
@ CCM_Vector
Definition Mangle.cpp:75
@ CCM_Std
Definition Mangle.cpp:76
@ CCM_WasmMainArgcArgv
Definition Mangle.cpp:77
@ CCM_RegCall
Definition Mangle.cpp:74
@ CCM_Other
Definition Mangle.cpp:72
static void emitLLDBAsmLabel(llvm::StringRef label, GlobalDecl GD, llvm::raw_ostream &Out)
Given an LLDB function call label, this function prints the label into Out, together with the structo...
Definition Mangle.cpp:172
static void mangleFunctionBlock(MangleContext &Context, StringRef Outer, const BlockDecl *BD, raw_ostream &Out)
Definition Mangle.cpp:58
static bool isExternC(const NamedDecl *ND)
Definition Mangle.cpp:80
static CCMangling getCallingConvMangling(const ASTContext &Context, const NamedDecl *ND)
Definition Mangle.cpp:88
std::string getName(const Decl *D)
Definition Mangle.cpp:498
std::vector< std::string > getAllManglings(const Decl *D)
Definition Mangle.cpp:542
static StringRef getClassSymbolPrefix(ObjCKind Kind, const ASTContext &Context)
Definition Mangle.cpp:512
bool writeName(const Decl *D, raw_ostream &OS)
Definition Mangle.cpp:471
std::vector< std::string > getAllManglings(const ObjCContainerDecl *OCD)
Definition Mangle.cpp:519
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
bool useAbbreviatedThunkName(GlobalDecl VirtualMethodDecl, StringRef MangledName)
VTableContextBase * getVTableContext()
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:917
std::string getName(const Decl *D)
Definition Mangle.cpp:674
ASTNameGenerator(ASTContext &Ctx)
Definition Mangle.cpp:665
bool writeName(const Decl *D, raw_ostream &OS)
Writes name for D to OS.
Definition Mangle.cpp:670
std::vector< std::string > getAllManglings(const Decl *D)
Definition Mangle.cpp:678
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4689
Represents a C++ constructor within a class.
Definition DeclCXX.h:2611
Represents a C++ destructor within a class.
Definition DeclCXX.h:2876
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2109
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:573
bool hasAttrs() const
Definition DeclBase.h:518
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:546
DeclContext * getDeclContext()
Definition DeclBase.h:448
Module * getOwningModuleForLinkage() const
Get the module that owns this declaration for linkage purposes.
Definition Decl.cpp:1636
bool hasAttr() const
Definition DeclBase.h:577
Represents a function declaration or definition.
Definition Decl.h:2015
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5357
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4553
CallingConv getCallConv() const
Definition TypeBase.h:4908
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
CXXCtorType getCtorType() const
Definition GlobalDecl.h:108
CXXDtorType getDtorType() const
Definition GlobalDecl.h:113
const Decl * getDecl() const
Definition GlobalDecl.h:106
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
A global _GUID constant.
Definition DeclCXX.h:4401
Parts getParts() const
Get the decomposed parts of this declaration.
Definition DeclCXX.h:4431
MSGuidDeclParts Parts
Definition DeclCXX.h:4403
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition Mangle.h:56
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
Definition Mangle.cpp:349
unsigned getBlockId(const BlockDecl *BD, bool Local)
Definition Mangle.h:92
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
Definition Mangle.cpp:331
ASTContext & getASTContext() const
Definition Mangle.h:86
void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID, raw_ostream &Out)
Definition Mangle.cpp:314
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &OS, bool includePrefixByte=true, bool includeCategoryNamespace=true, bool useDirectABI=false) const
Definition Mangle.cpp:384
virtual bool isUniqueInternalLinkageDecl(const NamedDecl *ND)
Definition Mangle.h:132
bool shouldMangleDeclName(const NamedDecl *D)
Definition Mangle.cpp:127
virtual void mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &) const
Definition Mangle.cpp:299
void mangleName(GlobalDecl GD, raw_ostream &)
Definition Mangle.cpp:190
virtual void mangleCXXName(GlobalDecl GD, raw_ostream &)=0
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
Definition Mangle.cpp:340
void mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD, raw_ostream &) const
Definition Mangle.cpp:452
This represents a decl that may have a name.
Definition Decl.h:274
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
bool hasExternalFormalLinkage() const
True if this decl has external linkage.
Definition Decl.h:429
ObjCContainerDecl - Represents a container for method declarations.
Definition DeclObjC.h:948
StringRef getObjCRuntimeNameAsString() const
Produce a name to be used for class's metadata.
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
Selector getSelector() const
Definition DeclObjC.h:327
bool isInstanceMethod() const
Definition DeclObjC.h:426
ObjCCategoryDecl * getCategory()
If this method is declared or implemented in a category, return that category.
bool isClassMethod() const
Definition DeclObjC.h:434
ObjCInterfaceDecl * getClassInterface()
The basic abstraction for the target Objective-C runtime.
Definition ObjCRuntime.h:28
bool isGNUFamily() const
Is this runtime basically of the GNU family of runtimes?
A (possibly-)qualified type.
Definition TypeBase.h:937
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
bool isItaniumFamily() const
Does this ABI generally fall into the Itanium family of ABIs?
Exposes information about the current target.
Definition TargetInfo.h:227
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition TargetInfo.h:490
const char * getUserLabelPrefix() const
Returns the default value of the USER_LABEL_PREFIX macro, which is the prefix given to user symbols b...
Definition TargetInfo.h:930
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
bool shouldUseMicrosoftCCforMangling() const
Should the Microsoft mangling scheme be used for C Calling Convention.
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9328
virtual const ThunkInfoVectorTy * getThunkInfo(GlobalDecl GD)
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
Defines the clang::TargetInfo interface.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
The JSON file list parser is used to communicate input to InstallAPI.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
@ Ctor_Base
Base object ctor.
Definition ABI.h:26
@ Ctor_DefaultClosure
Default closure variant of a ctor.
Definition ABI.h:29
@ Ctor_Complete
Complete object ctor.
Definition ABI.h:25
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
bool isInstanceMethod(const Decl *D)
Definition Attr.h:120
void mangleObjCMethodName(raw_ostream &OS, bool includePrefixByte, bool isInstanceMethod, StringRef ClassName, std::optional< StringRef > CategoryName, StringRef MethodName, bool useDirectABI)
Extract mangling function name from MangleContext such that swift can call it to prepare for ObjCDire...
Definition Mangle.cpp:32
CXXDtorType
C++ destructor types.
Definition ABI.h:34
@ Dtor_Base
Base object dtor.
Definition ABI.h:37
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
@ Dtor_Deleting
Deleting dtor.
Definition ABI.h:35
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:278
@ CC_X86VectorCall
Definition Specifiers.h:283
@ CC_X86StdCall
Definition Specifiers.h:280
@ CC_X86FastCall
Definition Specifiers.h:281
U cast(CodeGen::Address addr)
Definition Address.h:327
static constexpr llvm::StringLiteral FunctionLabelPrefix
Definition Mangle.h:328
uint16_t Part2
...-89ab-...
Definition DeclCXX.h:4380
uint32_t Part1
{01234567-...
Definition DeclCXX.h:4378
uint16_t Part3
...-cdef-...
Definition DeclCXX.h:4382
uint8_t Part4And5[8]
...-0123-456789abcdef}
Definition DeclCXX.h:4384