clang 20.0.0git
MicrosoftMangle.cpp
Go to the documentation of this file.
1//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
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// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/AST/Attr.h"
16#include "clang/AST/CharUnits.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
25#include "clang/AST/Mangle.h"
27#include "clang/Basic/ABI.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/StringExtras.h"
33#include "llvm/Support/CRC.h"
34#include "llvm/Support/MD5.h"
35#include "llvm/Support/StringSaver.h"
36#include "llvm/Support/xxhash.h"
37#include <functional>
38#include <optional>
39
40using namespace clang;
41
42namespace {
43
44// Get GlobalDecl of DeclContext of local entities.
45static GlobalDecl getGlobalDeclAsDeclContext(const DeclContext *DC) {
46 GlobalDecl GD;
47 if (auto *CD = dyn_cast<CXXConstructorDecl>(DC))
48 GD = GlobalDecl(CD, Ctor_Complete);
49 else if (auto *DD = dyn_cast<CXXDestructorDecl>(DC))
50 GD = GlobalDecl(DD, Dtor_Complete);
51 else
52 GD = GlobalDecl(cast<FunctionDecl>(DC));
53 return GD;
54}
55
56struct msvc_hashing_ostream : public llvm::raw_svector_ostream {
57 raw_ostream &OS;
59
60 msvc_hashing_ostream(raw_ostream &OS)
61 : llvm::raw_svector_ostream(Buffer), OS(OS) {}
62 ~msvc_hashing_ostream() override {
63 StringRef MangledName = str();
64 bool StartsWithEscape = MangledName.starts_with("\01");
65 if (StartsWithEscape)
66 MangledName = MangledName.drop_front(1);
67 if (MangledName.size() < 4096) {
68 OS << str();
69 return;
70 }
71
72 llvm::MD5 Hasher;
73 llvm::MD5::MD5Result Hash;
74 Hasher.update(MangledName);
75 Hasher.final(Hash);
76
77 SmallString<32> HexString;
78 llvm::MD5::stringifyResult(Hash, HexString);
79
80 if (StartsWithEscape)
81 OS << '\01';
82 OS << "??@" << HexString << '@';
83 }
84};
85
86static const DeclContext *
87getLambdaDefaultArgumentDeclContext(const Decl *D) {
88 if (const auto *RD = dyn_cast<CXXRecordDecl>(D))
89 if (RD->isLambda())
90 if (const auto *Parm =
91 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
92 return Parm->getDeclContext();
93 return nullptr;
94}
95
96/// Retrieve the declaration context that should be used when mangling
97/// the given declaration.
98static const DeclContext *getEffectiveDeclContext(const Decl *D) {
99 // The ABI assumes that lambda closure types that occur within
100 // default arguments live in the context of the function. However, due to
101 // the way in which Clang parses and creates function declarations, this is
102 // not the case: the lambda closure type ends up living in the context
103 // where the function itself resides, because the function declaration itself
104 // had not yet been created. Fix the context here.
105 if (const auto *LDADC = getLambdaDefaultArgumentDeclContext(D))
106 return LDADC;
107
108 // Perform the same check for block literals.
109 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
110 if (ParmVarDecl *ContextParam =
111 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
112 return ContextParam->getDeclContext();
113 }
114
115 const DeclContext *DC = D->getDeclContext();
116 if (isa<CapturedDecl>(DC) || isa<OMPDeclareReductionDecl>(DC) ||
117 isa<OMPDeclareMapperDecl>(DC)) {
118 return getEffectiveDeclContext(cast<Decl>(DC));
119 }
120
121 return DC->getRedeclContext();
122}
123
124static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
125 return getEffectiveDeclContext(cast<Decl>(DC));
126}
127
128static const FunctionDecl *getStructor(const NamedDecl *ND) {
129 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND))
130 return FTD->getTemplatedDecl()->getCanonicalDecl();
131
132 const auto *FD = cast<FunctionDecl>(ND);
133 if (const auto *FTD = FD->getPrimaryTemplate())
134 return FTD->getTemplatedDecl()->getCanonicalDecl();
135
136 return FD->getCanonicalDecl();
137}
138
139/// MicrosoftMangleContextImpl - Overrides the default MangleContext for the
140/// Microsoft Visual C++ ABI.
141class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
142 typedef std::pair<const DeclContext *, IdentifierInfo *> DiscriminatorKeyTy;
143 llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator;
144 llvm::DenseMap<const NamedDecl *, unsigned> Uniquifier;
145 llvm::DenseMap<const CXXRecordDecl *, unsigned> LambdaIds;
146 llvm::DenseMap<GlobalDecl, unsigned> SEHFilterIds;
147 llvm::DenseMap<GlobalDecl, unsigned> SEHFinallyIds;
148 SmallString<16> AnonymousNamespaceHash;
149
150public:
151 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags,
152 bool IsAux = false);
153 bool shouldMangleCXXName(const NamedDecl *D) override;
154 bool shouldMangleStringLiteral(const StringLiteral *SL) override;
155 void mangleCXXName(GlobalDecl GD, raw_ostream &Out) override;
157 const MethodVFTableLocation &ML,
158 raw_ostream &Out) override;
159 void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk,
160 bool ElideOverrideInfo, raw_ostream &) override;
162 const ThunkInfo &Thunk, bool ElideOverrideInfo,
163 raw_ostream &) override;
164 void mangleCXXVFTable(const CXXRecordDecl *Derived,
166 raw_ostream &Out) override;
167 void mangleCXXVBTable(const CXXRecordDecl *Derived,
169 raw_ostream &Out) override;
170
171 void mangleCXXVTable(const CXXRecordDecl *, raw_ostream &) override;
173 const CXXRecordDecl *DstRD,
174 raw_ostream &Out) override;
175 void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile,
176 bool IsUnaligned, uint32_t NumEntries,
177 raw_ostream &Out) override;
178 void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries,
179 raw_ostream &Out) override;
181 CXXCtorType CT, uint32_t Size, uint32_t NVOffset,
182 int32_t VBPtrOffset, uint32_t VBIndex,
183 raw_ostream &Out) override;
184 void mangleCXXRTTI(QualType T, raw_ostream &Out) override;
185 void mangleCXXRTTIName(QualType T, raw_ostream &Out,
186 bool NormalizeIntegers) override;
188 uint32_t NVOffset, int32_t VBPtrOffset,
189 uint32_t VBTableOffset, uint32_t Flags,
190 raw_ostream &Out) override;
191 void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived,
192 raw_ostream &Out) override;
194 raw_ostream &Out) override;
195 void
198 raw_ostream &Out) override;
199 void mangleCanonicalTypeName(QualType T, raw_ostream &,
200 bool NormalizeIntegers) override;
201 void mangleReferenceTemporary(const VarDecl *, unsigned ManglingNumber,
202 raw_ostream &) override;
203 void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out) override;
204 void mangleThreadSafeStaticGuardVariable(const VarDecl *D, unsigned GuardNum,
205 raw_ostream &Out) override;
206 void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override;
208 raw_ostream &Out) override;
209 void mangleSEHFilterExpression(GlobalDecl EnclosingDecl,
210 raw_ostream &Out) override;
211 void mangleSEHFinallyBlock(GlobalDecl EnclosingDecl,
212 raw_ostream &Out) override;
213 void mangleStringLiteral(const StringLiteral *SL, raw_ostream &Out) override;
214 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
215 const DeclContext *DC = getEffectiveDeclContext(ND);
216 if (!DC->isFunctionOrMethod())
217 return false;
218
219 // Lambda closure types are already numbered, give out a phony number so
220 // that they demangle nicely.
221 if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
222 if (RD->isLambda()) {
223 disc = 1;
224 return true;
225 }
226 }
227
228 // Use the canonical number for externally visible decls.
229 if (ND->isExternallyVisible()) {
230 disc = getASTContext().getManglingNumber(ND, isAux());
231 return true;
232 }
233
234 // Anonymous tags are already numbered.
235 if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) {
236 if (!Tag->hasNameForLinkage() &&
237 !getASTContext().getDeclaratorForUnnamedTagDecl(Tag) &&
238 !getASTContext().getTypedefNameForUnnamedTagDecl(Tag))
239 return false;
240 }
241
242 // Make up a reasonable number for internal decls.
243 unsigned &discriminator = Uniquifier[ND];
244 if (!discriminator)
245 discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())];
246 disc = discriminator + 1;
247 return true;
248 }
249
250 std::string getLambdaString(const CXXRecordDecl *Lambda) override {
251 assert(Lambda->isLambda() && "RD must be a lambda!");
252 std::string Name("<lambda_");
253
254 Decl *LambdaContextDecl = Lambda->getLambdaContextDecl();
255 unsigned LambdaManglingNumber = Lambda->getLambdaManglingNumber();
256 unsigned LambdaId;
257 const ParmVarDecl *Parm = dyn_cast_or_null<ParmVarDecl>(LambdaContextDecl);
258 const FunctionDecl *Func =
259 Parm ? dyn_cast<FunctionDecl>(Parm->getDeclContext()) : nullptr;
260
261 if (Func) {
262 unsigned DefaultArgNo =
263 Func->getNumParams() - Parm->getFunctionScopeIndex();
264 Name += llvm::utostr(DefaultArgNo);
265 Name += "_";
266 }
267
268 if (LambdaManglingNumber)
269 LambdaId = LambdaManglingNumber;
270 else
271 LambdaId = getLambdaIdForDebugInfo(Lambda);
272
273 Name += llvm::utostr(LambdaId);
274 Name += ">";
275 return Name;
276 }
277
278 unsigned getLambdaId(const CXXRecordDecl *RD) {
279 assert(RD->isLambda() && "RD must be a lambda!");
280 assert(!RD->isExternallyVisible() && "RD must not be visible!");
281 assert(RD->getLambdaManglingNumber() == 0 &&
282 "RD must not have a mangling number!");
283 std::pair<llvm::DenseMap<const CXXRecordDecl *, unsigned>::iterator, bool>
284 Result = LambdaIds.insert(std::make_pair(RD, LambdaIds.size()));
285 return Result.first->second;
286 }
287
288 unsigned getLambdaIdForDebugInfo(const CXXRecordDecl *RD) {
289 assert(RD->isLambda() && "RD must be a lambda!");
290 assert(!RD->isExternallyVisible() && "RD must not be visible!");
291 assert(RD->getLambdaManglingNumber() == 0 &&
292 "RD must not have a mangling number!");
293 // The lambda should exist, but return 0 in case it doesn't.
294 return LambdaIds.lookup(RD);
295 }
296
297 /// Return a character sequence that is (somewhat) unique to the TU suitable
298 /// for mangling anonymous namespaces.
299 StringRef getAnonymousNamespaceHash() const {
300 return AnonymousNamespaceHash;
301 }
302
303private:
304 void mangleInitFiniStub(const VarDecl *D, char CharCode, raw_ostream &Out);
305};
306
307/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
308/// Microsoft Visual C++ ABI.
309class MicrosoftCXXNameMangler {
310 MicrosoftMangleContextImpl &Context;
311 raw_ostream &Out;
312
313 /// The "structor" is the top-level declaration being mangled, if
314 /// that's not a template specialization; otherwise it's the pattern
315 /// for that specialization.
316 const NamedDecl *Structor;
317 unsigned StructorType;
318
319 typedef llvm::SmallVector<std::string, 10> BackRefVec;
320 BackRefVec NameBackReferences;
321
322 typedef llvm::DenseMap<const void *, unsigned> ArgBackRefMap;
323 ArgBackRefMap FunArgBackReferences;
324 ArgBackRefMap TemplateArgBackReferences;
325
326 typedef llvm::DenseMap<const void *, StringRef> TemplateArgStringMap;
327 TemplateArgStringMap TemplateArgStrings;
328 llvm::BumpPtrAllocator TemplateArgStringStorageAlloc;
329 llvm::StringSaver TemplateArgStringStorage;
330
331 typedef std::set<std::pair<int, bool>> PassObjectSizeArgsSet;
332 PassObjectSizeArgsSet PassObjectSizeArgs;
333
334 ASTContext &getASTContext() const { return Context.getASTContext(); }
335
336 const bool PointersAre64Bit;
337
338 DiagnosticBuilder Error(SourceLocation, StringRef, StringRef);
340 DiagnosticBuilder Error(StringRef);
341
342public:
343 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
344 enum class TplArgKind { ClassNTTP, StructuralValue };
345
346 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_)
347 : Context(C), Out(Out_), Structor(nullptr), StructorType(-1),
348 TemplateArgStringStorage(TemplateArgStringStorageAlloc),
349 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(
350 LangAS::Default) == 64) {}
351
352 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_,
354 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
355 TemplateArgStringStorage(TemplateArgStringStorageAlloc),
356 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(
357 LangAS::Default) == 64) {}
358
359 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_,
361 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
362 TemplateArgStringStorage(TemplateArgStringStorageAlloc),
363 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(
364 LangAS::Default) == 64) {}
365
366 raw_ostream &getStream() const { return Out; }
367
368 void mangle(GlobalDecl GD, StringRef Prefix = "?");
369 void mangleName(GlobalDecl GD);
370 void mangleFunctionEncoding(GlobalDecl GD, bool ShouldMangle);
371 void mangleVariableEncoding(const VarDecl *VD);
372 void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD,
373 const NonTypeTemplateParmDecl *PD,
374 QualType TemplateArgType,
375 StringRef Prefix = "$");
376 void mangleMemberDataPointerInClassNTTP(const CXXRecordDecl *,
377 const ValueDecl *);
378 void mangleMemberFunctionPointer(const CXXRecordDecl *RD,
379 const CXXMethodDecl *MD,
380 const NonTypeTemplateParmDecl *PD,
381 QualType TemplateArgType,
382 StringRef Prefix = "$");
383 void mangleFunctionPointer(const FunctionDecl *FD,
384 const NonTypeTemplateParmDecl *PD,
385 QualType TemplateArgType);
386 void mangleVarDecl(const VarDecl *VD, const NonTypeTemplateParmDecl *PD,
387 QualType TemplateArgType);
388 void mangleMemberFunctionPointerInClassNTTP(const CXXRecordDecl *RD,
389 const CXXMethodDecl *MD);
390 void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
391 const MethodVFTableLocation &ML);
392 void mangleNumber(int64_t Number);
393 void mangleNumber(llvm::APSInt Number);
394 void mangleFloat(llvm::APFloat Number);
395 void mangleBits(llvm::APInt Number);
396 void mangleTagTypeKind(TagTypeKind TK);
397 void mangleArtificialTagType(TagTypeKind TK, StringRef UnqualifiedName,
398 ArrayRef<StringRef> NestedNames = {});
399 void mangleAddressSpaceType(QualType T, Qualifiers Quals, SourceRange Range);
400 void mangleType(QualType T, SourceRange Range,
401 QualifierMangleMode QMM = QMM_Mangle);
402 void mangleFunctionType(const FunctionType *T,
403 const FunctionDecl *D = nullptr,
404 bool ForceThisQuals = false,
405 bool MangleExceptionSpec = true);
406 void mangleSourceName(StringRef Name);
407 void mangleNestedName(GlobalDecl GD);
408
409 void mangleAutoReturnType(QualType T, QualifierMangleMode QMM);
410
411private:
412 bool isStructorDecl(const NamedDecl *ND) const {
413 return ND == Structor || getStructor(ND) == Structor;
414 }
415
416 bool is64BitPointer(Qualifiers Quals) const {
417 LangAS AddrSpace = Quals.getAddressSpace();
418 return AddrSpace == LangAS::ptr64 ||
419 (PointersAre64Bit && !(AddrSpace == LangAS::ptr32_sptr ||
420 AddrSpace == LangAS::ptr32_uptr));
421 }
422
423 void mangleUnqualifiedName(GlobalDecl GD) {
424 mangleUnqualifiedName(GD, cast<NamedDecl>(GD.getDecl())->getDeclName());
425 }
426 void mangleUnqualifiedName(GlobalDecl GD, DeclarationName Name);
427 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
428 void mangleCXXDtorType(CXXDtorType T);
429 void mangleQualifiers(Qualifiers Quals, bool IsMember);
430 void mangleRefQualifier(RefQualifierKind RefQualifier);
431 void manglePointerCVQualifiers(Qualifiers Quals);
432 void manglePointerExtQualifiers(Qualifiers Quals, QualType PointeeType);
433
434 void mangleUnscopedTemplateName(GlobalDecl GD);
435 void
436 mangleTemplateInstantiationName(GlobalDecl GD,
437 const TemplateArgumentList &TemplateArgs);
438 void mangleObjCMethodName(const ObjCMethodDecl *MD);
439
440 void mangleFunctionArgumentType(QualType T, SourceRange Range);
441 void manglePassObjectSizeArg(const PassObjectSizeAttr *POSA);
442
443 bool isArtificialTagType(QualType T) const;
444
445 // Declare manglers for every type class.
446#define ABSTRACT_TYPE(CLASS, PARENT)
447#define NON_CANONICAL_TYPE(CLASS, PARENT)
448#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
449 Qualifiers Quals, \
450 SourceRange Range);
451#include "clang/AST/TypeNodes.inc"
452#undef ABSTRACT_TYPE
453#undef NON_CANONICAL_TYPE
454#undef TYPE
455
456 void mangleType(const TagDecl *TD);
457 void mangleDecayedArrayType(const ArrayType *T);
458 void mangleArrayType(const ArrayType *T);
459 void mangleFunctionClass(const FunctionDecl *FD);
460 void mangleCallingConvention(CallingConv CC, SourceRange Range);
461 void mangleCallingConvention(const FunctionType *T, SourceRange Range);
462 void mangleIntegerLiteral(const llvm::APSInt &Number,
463 const NonTypeTemplateParmDecl *PD = nullptr,
464 QualType TemplateArgType = QualType());
465 void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD);
466 void mangleThrowSpecification(const FunctionProtoType *T);
467
468 void mangleTemplateArgs(const TemplateDecl *TD,
469 const TemplateArgumentList &TemplateArgs);
470 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA,
471 const NamedDecl *Parm);
472 void mangleTemplateArgValue(QualType T, const APValue &V, TplArgKind,
473 bool WithScalarType = false);
474
475 void mangleObjCProtocol(const ObjCProtocolDecl *PD);
476 void mangleObjCLifetime(const QualType T, Qualifiers Quals,
478 void mangleObjCKindOfType(const ObjCObjectType *T, Qualifiers Quals,
480
481 void mangleAutoReturnType(const MemberPointerType *T, Qualifiers Quals);
482 void mangleAutoReturnType(const PointerType *T, Qualifiers Quals);
483 void mangleAutoReturnType(const LValueReferenceType *T, Qualifiers Quals);
484 void mangleAutoReturnType(const RValueReferenceType *T, Qualifiers Quals);
485};
486}
487
488MicrosoftMangleContextImpl::MicrosoftMangleContextImpl(ASTContext &Context,
489 DiagnosticsEngine &Diags,
490 bool IsAux)
491 : MicrosoftMangleContext(Context, Diags, IsAux) {
492 // To mangle anonymous namespaces, hash the path to the main source file. The
493 // path should be whatever (probably relative) path was passed on the command
494 // line. The goal is for the compiler to produce the same output regardless of
495 // working directory, so use the uncanonicalized relative path.
496 //
497 // It's important to make the mangled names unique because, when CodeView
498 // debug info is in use, the debugger uses mangled type names to distinguish
499 // between otherwise identically named types in anonymous namespaces.
500 //
501 // These symbols are always internal, so there is no need for the hash to
502 // match what MSVC produces. For the same reason, clang is free to change the
503 // hash at any time without breaking compatibility with old versions of clang.
504 // The generated names are intended to look similar to what MSVC generates,
505 // which are something like "?A0x01234567@".
506 SourceManager &SM = Context.getSourceManager();
507 if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID())) {
508 // Truncate the hash so we get 8 characters of hexadecimal.
509 uint32_t TruncatedHash = uint32_t(xxh3_64bits(FE->getName()));
510 AnonymousNamespaceHash = llvm::utohexstr(TruncatedHash);
511 } else {
512 // If we don't have a path to the main file, we'll just use 0.
513 AnonymousNamespaceHash = "0";
514 }
515}
516
517bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
518 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
519 LanguageLinkage L = FD->getLanguageLinkage();
520 // Overloadable functions need mangling.
521 if (FD->hasAttr<OverloadableAttr>())
522 return true;
523
524 // The ABI expects that we would never mangle "typical" user-defined entry
525 // points regardless of visibility or freestanding-ness.
526 //
527 // N.B. This is distinct from asking about "main". "main" has a lot of
528 // special rules associated with it in the standard while these
529 // user-defined entry points are outside of the purview of the standard.
530 // For example, there can be only one definition for "main" in a standards
531 // compliant program; however nothing forbids the existence of wmain and
532 // WinMain in the same translation unit.
533 if (FD->isMSVCRTEntryPoint())
534 return false;
535
536 // C++ functions and those whose names are not a simple identifier need
537 // mangling.
538 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
539 return true;
540
541 // C functions are not mangled.
542 if (L == CLanguageLinkage)
543 return false;
544 }
545
546 // Otherwise, no mangling is done outside C++ mode.
547 if (!getASTContext().getLangOpts().CPlusPlus)
548 return false;
549
550 const VarDecl *VD = dyn_cast<VarDecl>(D);
551 if (VD && !isa<DecompositionDecl>(D)) {
552 // C variables are not mangled.
553 if (VD->isExternC())
554 return false;
555
556 // Variables at global scope with internal linkage are not mangled.
557 const DeclContext *DC = getEffectiveDeclContext(D);
558 // Check for extern variable declared locally.
559 if (DC->isFunctionOrMethod() && D->hasLinkage())
560 while (!DC->isNamespace() && !DC->isTranslationUnit())
561 DC = getEffectiveParentContext(DC);
562
563 if (DC->isTranslationUnit() && D->getFormalLinkage() == Linkage::Internal &&
564 !isa<VarTemplateSpecializationDecl>(D) && D->getIdentifier() != nullptr)
565 return false;
566 }
567
568 return true;
569}
570
571bool
572MicrosoftMangleContextImpl::shouldMangleStringLiteral(const StringLiteral *SL) {
573 return true;
574}
575
576DiagnosticBuilder MicrosoftCXXNameMangler::Error(SourceLocation loc,
577 StringRef thing1,
578 StringRef thing2) {
579 DiagnosticsEngine &Diags = Context.getDiags();
580 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
581 "cannot mangle this %0 %1 yet");
582 return Diags.Report(loc, DiagID) << thing1 << thing2;
583}
584
585DiagnosticBuilder MicrosoftCXXNameMangler::Error(SourceLocation loc,
586 StringRef thingy) {
587 DiagnosticsEngine &Diags = Context.getDiags();
588 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
589 "cannot mangle this %0 yet");
590 return Diags.Report(loc, DiagID) << thingy;
591}
592
593DiagnosticBuilder MicrosoftCXXNameMangler::Error(StringRef thingy) {
594 DiagnosticsEngine &Diags = Context.getDiags();
595 // extra placeholders are ignored quietly when not used
596 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
597 "cannot mangle this %0 yet");
598 return Diags.Report(DiagID) << thingy;
599}
600
601void MicrosoftCXXNameMangler::mangle(GlobalDecl GD, StringRef Prefix) {
602 const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
603 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
604 // Therefore it's really important that we don't decorate the
605 // name with leading underscores or leading/trailing at signs. So, by
606 // default, we emit an asm marker at the start so we get the name right.
607 // Callers can override this with a custom prefix.
608
609 // <mangled-name> ::= ? <name> <type-encoding>
610 Out << Prefix;
611 mangleName(GD);
612 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
613 mangleFunctionEncoding(GD, Context.shouldMangleDeclName(FD));
614 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
615 mangleVariableEncoding(VD);
616 else if (isa<MSGuidDecl>(D))
617 // MSVC appears to mangle GUIDs as if they were variables of type
618 // 'const struct __s_GUID'.
619 Out << "3U__s_GUID@@B";
620 else if (isa<TemplateParamObjectDecl>(D)) {
621 // Template parameter objects don't get a <type-encoding>; their type is
622 // specified as part of their value.
623 } else
624 llvm_unreachable("Tried to mangle unexpected NamedDecl!");
625}
626
627void MicrosoftCXXNameMangler::mangleFunctionEncoding(GlobalDecl GD,
628 bool ShouldMangle) {
629 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
630 // <type-encoding> ::= <function-class> <function-type>
631
632 // Since MSVC operates on the type as written and not the canonical type, it
633 // actually matters which decl we have here. MSVC appears to choose the
634 // first, since it is most likely to be the declaration in a header file.
635 FD = FD->getFirstDecl();
636
637 // We should never ever see a FunctionNoProtoType at this point.
638 // We don't even know how to mangle their types anyway :).
640
641 // extern "C" functions can hold entities that must be mangled.
642 // As it stands, these functions still need to get expressed in the full
643 // external name. They have their class and type omitted, replaced with '9'.
644 if (ShouldMangle) {
645 // We would like to mangle all extern "C" functions using this additional
646 // component but this would break compatibility with MSVC's behavior.
647 // Instead, do this when we know that compatibility isn't important (in
648 // other words, when it is an overloaded extern "C" function).
649 if (FD->isExternC() && FD->hasAttr<OverloadableAttr>())
650 Out << "$$J0";
651
652 mangleFunctionClass(FD);
653
654 mangleFunctionType(FT, FD, false, false);
655 } else {
656 Out << '9';
657 }
658}
659
660void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
661 // <type-encoding> ::= <storage-class> <variable-type>
662 // <storage-class> ::= 0 # private static member
663 // ::= 1 # protected static member
664 // ::= 2 # public static member
665 // ::= 3 # global
666 // ::= 4 # static local
667
668 // The first character in the encoding (after the name) is the storage class.
669 if (VD->isStaticDataMember()) {
670 // If it's a static member, it also encodes the access level.
671 switch (VD->getAccess()) {
672 default:
673 case AS_private: Out << '0'; break;
674 case AS_protected: Out << '1'; break;
675 case AS_public: Out << '2'; break;
676 }
677 }
678 else if (!VD->isStaticLocal())
679 Out << '3';
680 else
681 Out << '4';
682 // Now mangle the type.
683 // <variable-type> ::= <type> <cvr-qualifiers>
684 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
685 // Pointers and references are odd. The type of 'int * const foo;' gets
686 // mangled as 'QAHA' instead of 'PAHB', for example.
687 SourceRange SR = VD->getSourceRange();
688 QualType Ty = VD->getType();
689 if (Ty->isPointerType() || Ty->isReferenceType() ||
690 Ty->isMemberPointerType()) {
691 mangleType(Ty, SR, QMM_Drop);
692 manglePointerExtQualifiers(
693 Ty.getDesugaredType(getASTContext()).getLocalQualifiers(), QualType());
694 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
695 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
696 // Member pointers are suffixed with a back reference to the member
697 // pointer's class name.
698 mangleName(MPT->getClass()->getAsCXXRecordDecl());
699 } else
700 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
701 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
702 // Global arrays are funny, too.
703 mangleDecayedArrayType(AT);
704 if (AT->getElementType()->isArrayType())
705 Out << 'A';
706 else
707 mangleQualifiers(Ty.getQualifiers(), false);
708 } else {
709 mangleType(Ty, SR, QMM_Drop);
710 mangleQualifiers(Ty.getQualifiers(), false);
711 }
712}
713
714void MicrosoftCXXNameMangler::mangleMemberDataPointer(
715 const CXXRecordDecl *RD, const ValueDecl *VD,
716 const NonTypeTemplateParmDecl *PD, QualType TemplateArgType,
717 StringRef Prefix) {
718 // <member-data-pointer> ::= <integer-literal>
719 // ::= $F <number> <number>
720 // ::= $G <number> <number> <number>
721 //
722 // <auto-nttp> ::= $ M <type> <integer-literal>
723 // <auto-nttp> ::= $ M <type> F <name> <number>
724 // <auto-nttp> ::= $ M <type> G <name> <number> <number>
725
726 int64_t FieldOffset;
727 int64_t VBTableOffset;
729 if (VD) {
730 FieldOffset = getASTContext().getFieldOffset(VD);
731 assert(FieldOffset % getASTContext().getCharWidth() == 0 &&
732 "cannot take address of bitfield");
733 FieldOffset /= getASTContext().getCharWidth();
734
735 VBTableOffset = 0;
736
737 if (IM == MSInheritanceModel::Virtual)
738 FieldOffset -= getASTContext().getOffsetOfBaseWithVBPtr(RD).getQuantity();
739 } else {
740 FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1;
741
742 VBTableOffset = -1;
743 }
744
745 char Code = '\0';
746 switch (IM) {
747 case MSInheritanceModel::Single: Code = '0'; break;
748 case MSInheritanceModel::Multiple: Code = '0'; break;
749 case MSInheritanceModel::Virtual: Code = 'F'; break;
750 case MSInheritanceModel::Unspecified: Code = 'G'; break;
751 }
752
753 Out << Prefix;
754
755 if (VD &&
756 getASTContext().getLangOpts().isCompatibleWithMSVC(
757 LangOptions::MSVC2019) &&
758 PD && PD->getType()->getTypeClass() == Type::Auto &&
759 !TemplateArgType.isNull()) {
760 Out << "M";
761 mangleType(TemplateArgType, SourceRange(), QMM_Drop);
762 }
763
764 Out << Code;
765
766 mangleNumber(FieldOffset);
767
768 // The C++ standard doesn't allow base-to-derived member pointer conversions
769 // in template parameter contexts, so the vbptr offset of data member pointers
770 // is always zero.
772 mangleNumber(0);
774 mangleNumber(VBTableOffset);
775}
776
777void MicrosoftCXXNameMangler::mangleMemberDataPointerInClassNTTP(
778 const CXXRecordDecl *RD, const ValueDecl *VD) {
780 // <nttp-class-member-data-pointer> ::= <member-data-pointer>
781 // ::= N
782 // ::= 8 <postfix> @ <unqualified-name> @
783
784 if (IM != MSInheritanceModel::Single && IM != MSInheritanceModel::Multiple)
785 return mangleMemberDataPointer(RD, VD, nullptr, QualType(), "");
786
787 if (!VD) {
788 Out << 'N';
789 return;
790 }
791
792 Out << '8';
793 mangleNestedName(VD);
794 Out << '@';
795 mangleUnqualifiedName(VD);
796 Out << '@';
797}
798
799void MicrosoftCXXNameMangler::mangleMemberFunctionPointer(
800 const CXXRecordDecl *RD, const CXXMethodDecl *MD,
801 const NonTypeTemplateParmDecl *PD, QualType TemplateArgType,
802 StringRef Prefix) {
803 // <member-function-pointer> ::= $1? <name>
804 // ::= $H? <name> <number>
805 // ::= $I? <name> <number> <number>
806 // ::= $J? <name> <number> <number> <number>
807 //
808 // <auto-nttp> ::= $ M <type> 1? <name>
809 // <auto-nttp> ::= $ M <type> H? <name> <number>
810 // <auto-nttp> ::= $ M <type> I? <name> <number> <number>
811 // <auto-nttp> ::= $ M <type> J? <name> <number> <number> <number>
812
814
815 char Code = '\0';
816 switch (IM) {
817 case MSInheritanceModel::Single: Code = '1'; break;
818 case MSInheritanceModel::Multiple: Code = 'H'; break;
819 case MSInheritanceModel::Virtual: Code = 'I'; break;
820 case MSInheritanceModel::Unspecified: Code = 'J'; break;
821 }
822
823 // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr
824 // thunk.
825 uint64_t NVOffset = 0;
826 uint64_t VBTableOffset = 0;
827 uint64_t VBPtrOffset = 0;
828 if (MD) {
829 Out << Prefix;
830
831 if (getASTContext().getLangOpts().isCompatibleWithMSVC(
832 LangOptions::MSVC2019) &&
833 PD && PD->getType()->getTypeClass() == Type::Auto &&
834 !TemplateArgType.isNull()) {
835 Out << "M";
836 mangleType(TemplateArgType, SourceRange(), QMM_Drop);
837 }
838
839 Out << Code << '?';
840 if (MD->isVirtual()) {
841 MicrosoftVTableContext *VTContext =
842 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
844 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
845 mangleVirtualMemPtrThunk(MD, ML);
846 NVOffset = ML.VFPtrOffset.getQuantity();
847 VBTableOffset = ML.VBTableIndex * 4;
848 if (ML.VBase) {
849 const ASTRecordLayout &Layout = getASTContext().getASTRecordLayout(RD);
850 VBPtrOffset = Layout.getVBPtrOffset().getQuantity();
851 }
852 } else {
853 mangleName(MD);
854 mangleFunctionEncoding(MD, /*ShouldMangle=*/true);
855 }
856
857 if (VBTableOffset == 0 && IM == MSInheritanceModel::Virtual)
858 NVOffset -= getASTContext().getOffsetOfBaseWithVBPtr(RD).getQuantity();
859 } else {
860 // Null single inheritance member functions are encoded as a simple nullptr.
861 if (IM == MSInheritanceModel::Single) {
862 Out << Prefix << "0A@";
863 return;
864 }
865 if (IM == MSInheritanceModel::Unspecified)
866 VBTableOffset = -1;
867 Out << Prefix << Code;
868 }
869
870 if (inheritanceModelHasNVOffsetField(/*IsMemberFunction=*/true, IM))
871 mangleNumber(static_cast<uint32_t>(NVOffset));
873 mangleNumber(VBPtrOffset);
875 mangleNumber(VBTableOffset);
876}
877
878void MicrosoftCXXNameMangler::mangleFunctionPointer(
879 const FunctionDecl *FD, const NonTypeTemplateParmDecl *PD,
880 QualType TemplateArgType) {
881 // <func-ptr> ::= $1? <mangled-name>
882 // <func-ptr> ::= <auto-nttp>
883 //
884 // <auto-nttp> ::= $ M <type> 1? <mangled-name>
885 Out << '$';
886
887 if (getASTContext().getLangOpts().isCompatibleWithMSVC(
888 LangOptions::MSVC2019) &&
889 PD && PD->getType()->getTypeClass() == Type::Auto &&
890 !TemplateArgType.isNull()) {
891 Out << "M";
892 mangleType(TemplateArgType, SourceRange(), QMM_Drop);
893 }
894
895 Out << "1?";
896 mangleName(FD);
897 mangleFunctionEncoding(FD, /*ShouldMangle=*/true);
898}
899
900void MicrosoftCXXNameMangler::mangleVarDecl(const VarDecl *VD,
901 const NonTypeTemplateParmDecl *PD,
902 QualType TemplateArgType) {
903 // <var-ptr> ::= $1? <mangled-name>
904 // <var-ptr> ::= <auto-nttp>
905 //
906 // <auto-nttp> ::= $ M <type> 1? <mangled-name>
907 Out << '$';
908
909 if (getASTContext().getLangOpts().isCompatibleWithMSVC(
910 LangOptions::MSVC2019) &&
911 PD && PD->getType()->getTypeClass() == Type::Auto &&
912 !TemplateArgType.isNull()) {
913 Out << "M";
914 mangleType(TemplateArgType, SourceRange(), QMM_Drop);
915 }
916
917 Out << "1?";
918 mangleName(VD);
919 mangleVariableEncoding(VD);
920}
921
922void MicrosoftCXXNameMangler::mangleMemberFunctionPointerInClassNTTP(
923 const CXXRecordDecl *RD, const CXXMethodDecl *MD) {
924 // <nttp-class-member-function-pointer> ::= <member-function-pointer>
925 // ::= N
926 // ::= E? <virtual-mem-ptr-thunk>
927 // ::= E? <mangled-name> <type-encoding>
928
929 if (!MD) {
930 if (RD->getMSInheritanceModel() != MSInheritanceModel::Single)
931 return mangleMemberFunctionPointer(RD, MD, nullptr, QualType(), "");
932
933 Out << 'N';
934 return;
935 }
936
937 Out << "E?";
938 if (MD->isVirtual()) {
939 MicrosoftVTableContext *VTContext =
940 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
942 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
943 mangleVirtualMemPtrThunk(MD, ML);
944 } else {
945 mangleName(MD);
946 mangleFunctionEncoding(MD, /*ShouldMangle=*/true);
947 }
948}
949
950void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk(
951 const CXXMethodDecl *MD, const MethodVFTableLocation &ML) {
952 // Get the vftable offset.
953 CharUnits PointerWidth = getASTContext().toCharUnitsFromBits(
954 getASTContext().getTargetInfo().getPointerWidth(LangAS::Default));
955 uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity();
956
957 Out << "?_9";
958 mangleName(MD->getParent());
959 Out << "$B";
960 mangleNumber(OffsetInVFTable);
961 Out << 'A';
962 mangleCallingConvention(MD->getType()->castAs<FunctionProtoType>(),
963 MD->getSourceRange());
964}
965
966void MicrosoftCXXNameMangler::mangleName(GlobalDecl GD) {
967 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
968
969 // Always start with the unqualified name.
970 mangleUnqualifiedName(GD);
971
972 mangleNestedName(GD);
973
974 // Terminate the whole name with an '@'.
975 Out << '@';
976}
977
978void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
979 mangleNumber(llvm::APSInt(llvm::APInt(64, Number), /*IsUnsigned*/false));
980}
981
982void MicrosoftCXXNameMangler::mangleNumber(llvm::APSInt Number) {
983 // MSVC never mangles any integer wider than 64 bits. In general it appears
984 // to convert every integer to signed 64 bit before mangling (including
985 // unsigned 64 bit values). Do the same, but preserve bits beyond the bottom
986 // 64.
987 unsigned Width = std::max(Number.getBitWidth(), 64U);
988 llvm::APInt Value = Number.extend(Width);
989
990 // <non-negative integer> ::= A@ # when Number == 0
991 // ::= <decimal digit> # when 1 <= Number <= 10
992 // ::= <hex digit>+ @ # when Number >= 10
993 //
994 // <number> ::= [?] <non-negative integer>
995
996 if (Value.isNegative()) {
997 Value = -Value;
998 Out << '?';
999 }
1000 mangleBits(Value);
1001}
1002
1003void MicrosoftCXXNameMangler::mangleFloat(llvm::APFloat Number) {
1004 using llvm::APFloat;
1005
1006 switch (APFloat::SemanticsToEnum(Number.getSemantics())) {
1007 case APFloat::S_IEEEsingle: Out << 'A'; break;
1008 case APFloat::S_IEEEdouble: Out << 'B'; break;
1009
1010 // The following are all Clang extensions. We try to pick manglings that are
1011 // unlikely to conflict with MSVC's scheme.
1012 case APFloat::S_IEEEhalf: Out << 'V'; break;
1013 case APFloat::S_BFloat: Out << 'W'; break;
1014 case APFloat::S_x87DoubleExtended: Out << 'X'; break;
1015 case APFloat::S_IEEEquad: Out << 'Y'; break;
1016 case APFloat::S_PPCDoubleDouble: Out << 'Z'; break;
1017 case APFloat::S_PPCDoubleDoubleLegacy:
1018 case APFloat::S_Float8E5M2:
1019 case APFloat::S_Float8E4M3:
1020 case APFloat::S_Float8E4M3FN:
1021 case APFloat::S_Float8E5M2FNUZ:
1022 case APFloat::S_Float8E4M3FNUZ:
1023 case APFloat::S_Float8E4M3B11FNUZ:
1024 case APFloat::S_Float8E3M4:
1025 case APFloat::S_FloatTF32:
1026 case APFloat::S_Float8E8M0FNU:
1027 case APFloat::S_Float6E3M2FN:
1028 case APFloat::S_Float6E2M3FN:
1029 case APFloat::S_Float4E2M1FN:
1030 llvm_unreachable("Tried to mangle unexpected APFloat semantics");
1031 }
1032
1033 mangleBits(Number.bitcastToAPInt());
1034}
1035
1036void MicrosoftCXXNameMangler::mangleBits(llvm::APInt Value) {
1037 if (Value == 0)
1038 Out << "A@";
1039 else if (Value.uge(1) && Value.ule(10))
1040 Out << (Value - 1);
1041 else {
1042 // Numbers that are not encoded as decimal digits are represented as nibbles
1043 // in the range of ASCII characters 'A' to 'P'.
1044 // The number 0x123450 would be encoded as 'BCDEFA'
1045 llvm::SmallString<32> EncodedNumberBuffer;
1046 for (; Value != 0; Value.lshrInPlace(4))
1047 EncodedNumberBuffer.push_back('A' + (Value & 0xf).getZExtValue());
1048 std::reverse(EncodedNumberBuffer.begin(), EncodedNumberBuffer.end());
1049 Out.write(EncodedNumberBuffer.data(), EncodedNumberBuffer.size());
1050 Out << '@';
1051 }
1052}
1053
1055 const TemplateArgumentList *&TemplateArgs) {
1056 const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
1057 // Check if we have a function template.
1058 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1059 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
1060 TemplateArgs = FD->getTemplateSpecializationArgs();
1061 return GD.getWithDecl(TD);
1062 }
1063 }
1064
1065 // Check if we have a class template.
1066 if (const ClassTemplateSpecializationDecl *Spec =
1067 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
1068 TemplateArgs = &Spec->getTemplateArgs();
1069 return GD.getWithDecl(Spec->getSpecializedTemplate());
1070 }
1071
1072 // Check if we have a variable template.
1073 if (const VarTemplateSpecializationDecl *Spec =
1074 dyn_cast<VarTemplateSpecializationDecl>(ND)) {
1075 TemplateArgs = &Spec->getTemplateArgs();
1076 return GD.getWithDecl(Spec->getSpecializedTemplate());
1077 }
1078
1079 return GlobalDecl();
1080}
1081
1082void MicrosoftCXXNameMangler::mangleUnqualifiedName(GlobalDecl GD,
1083 DeclarationName Name) {
1084 const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
1085 // <unqualified-name> ::= <operator-name>
1086 // ::= <ctor-dtor-name>
1087 // ::= <source-name>
1088 // ::= <template-name>
1089
1090 // Check if we have a template.
1091 const TemplateArgumentList *TemplateArgs = nullptr;
1092 if (GlobalDecl TD = isTemplate(GD, TemplateArgs)) {
1093 // Function templates aren't considered for name back referencing. This
1094 // makes sense since function templates aren't likely to occur multiple
1095 // times in a symbol.
1096 if (isa<FunctionTemplateDecl>(TD.getDecl())) {
1097 mangleTemplateInstantiationName(TD, *TemplateArgs);
1098 Out << '@';
1099 return;
1100 }
1101
1102 // Here comes the tricky thing: if we need to mangle something like
1103 // void foo(A::X<Y>, B::X<Y>),
1104 // the X<Y> part is aliased. However, if you need to mangle
1105 // void foo(A::X<A::Y>, A::X<B::Y>),
1106 // the A::X<> part is not aliased.
1107 // That is, from the mangler's perspective we have a structure like this:
1108 // namespace[s] -> type[ -> template-parameters]
1109 // but from the Clang perspective we have
1110 // type [ -> template-parameters]
1111 // \-> namespace[s]
1112 // What we do is we create a new mangler, mangle the same type (without
1113 // a namespace suffix) to a string using the extra mangler and then use
1114 // the mangled type name as a key to check the mangling of different types
1115 // for aliasing.
1116
1117 // It's important to key cache reads off ND, not TD -- the same TD can
1118 // be used with different TemplateArgs, but ND uniquely identifies
1119 // TD / TemplateArg pairs.
1120 ArgBackRefMap::iterator Found = TemplateArgBackReferences.find(ND);
1121 if (Found == TemplateArgBackReferences.end()) {
1122
1123 TemplateArgStringMap::iterator Found = TemplateArgStrings.find(ND);
1124 if (Found == TemplateArgStrings.end()) {
1125 // Mangle full template name into temporary buffer.
1126 llvm::SmallString<64> TemplateMangling;
1127 llvm::raw_svector_ostream Stream(TemplateMangling);
1128 MicrosoftCXXNameMangler Extra(Context, Stream);
1129 Extra.mangleTemplateInstantiationName(TD, *TemplateArgs);
1130
1131 // Use the string backref vector to possibly get a back reference.
1132 mangleSourceName(TemplateMangling);
1133
1134 // Memoize back reference for this type if one exist, else memoize
1135 // the mangling itself.
1136 BackRefVec::iterator StringFound =
1137 llvm::find(NameBackReferences, TemplateMangling);
1138 if (StringFound != NameBackReferences.end()) {
1139 TemplateArgBackReferences[ND] =
1140 StringFound - NameBackReferences.begin();
1141 } else {
1142 TemplateArgStrings[ND] =
1143 TemplateArgStringStorage.save(TemplateMangling.str());
1144 }
1145 } else {
1146 Out << Found->second << '@'; // Outputs a StringRef.
1147 }
1148 } else {
1149 Out << Found->second; // Outputs a back reference (an int).
1150 }
1151 return;
1152 }
1153
1154 switch (Name.getNameKind()) {
1156 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
1157 bool IsDeviceStub =
1158 ND &&
1159 ((isa<FunctionDecl>(ND) && ND->hasAttr<CUDAGlobalAttr>()) ||
1160 (isa<FunctionTemplateDecl>(ND) &&
1161 cast<FunctionTemplateDecl>(ND)
1162 ->getTemplatedDecl()
1163 ->hasAttr<CUDAGlobalAttr>())) &&
1164 GD.getKernelReferenceKind() == KernelReferenceKind::Stub;
1165 if (IsDeviceStub)
1166 mangleSourceName(
1167 (llvm::Twine("__device_stub__") + II->getName()).str());
1168 else
1169 mangleSourceName(II->getName());
1170 break;
1171 }
1172
1173 // Otherwise, an anonymous entity. We must have a declaration.
1174 assert(ND && "mangling empty name without declaration");
1175
1176 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1177 if (NS->isAnonymousNamespace()) {
1178 Out << "?A0x" << Context.getAnonymousNamespaceHash() << '@';
1179 break;
1180 }
1181 }
1182
1183 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(ND)) {
1184 // Decomposition declarations are considered anonymous, and get
1185 // numbered with a $S prefix.
1186 llvm::SmallString<64> Name("$S");
1187 // Get a unique id for the anonymous struct.
1188 Name += llvm::utostr(Context.getAnonymousStructId(DD) + 1);
1189 mangleSourceName(Name);
1190 break;
1191 }
1192
1193 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1194 // We must have an anonymous union or struct declaration.
1195 const CXXRecordDecl *RD = VD->getType()->getAsCXXRecordDecl();
1196 assert(RD && "expected variable decl to have a record type");
1197 // Anonymous types with no tag or typedef get the name of their
1198 // declarator mangled in. If they have no declarator, number them with
1199 // a $S prefix.
1200 llvm::SmallString<64> Name("$S");
1201 // Get a unique id for the anonymous struct.
1202 Name += llvm::utostr(Context.getAnonymousStructId(RD) + 1);
1203 mangleSourceName(Name.str());
1204 break;
1205 }
1206
1207 if (const MSGuidDecl *GD = dyn_cast<MSGuidDecl>(ND)) {
1208 // Mangle a GUID object as if it were a variable with the corresponding
1209 // mangled name.
1210 SmallString<sizeof("_GUID_12345678_1234_1234_1234_1234567890ab")> GUID;
1211 llvm::raw_svector_ostream GUIDOS(GUID);
1212 Context.mangleMSGuidDecl(GD, GUIDOS);
1213 mangleSourceName(GUID);
1214 break;
1215 }
1216
1217 if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
1218 Out << "?__N";
1219 mangleTemplateArgValue(TPO->getType().getUnqualifiedType(),
1220 TPO->getValue(), TplArgKind::ClassNTTP);
1221 break;
1222 }
1223
1224 // We must have an anonymous struct.
1225 const TagDecl *TD = cast<TagDecl>(ND);
1226 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
1227 assert(TD->getDeclContext() == D->getDeclContext() &&
1228 "Typedef should not be in another decl context!");
1229 assert(D->getDeclName().getAsIdentifierInfo() &&
1230 "Typedef was not named!");
1231 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName());
1232 break;
1233 }
1234
1235 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
1236 if (Record->isLambda()) {
1237 llvm::SmallString<10> Name("<lambda_");
1238
1239 Decl *LambdaContextDecl = Record->getLambdaContextDecl();
1240 unsigned LambdaManglingNumber = Record->getLambdaManglingNumber();
1241 unsigned LambdaId;
1242 const ParmVarDecl *Parm =
1243 dyn_cast_or_null<ParmVarDecl>(LambdaContextDecl);
1244 const FunctionDecl *Func =
1245 Parm ? dyn_cast<FunctionDecl>(Parm->getDeclContext()) : nullptr;
1246
1247 if (Func) {
1248 unsigned DefaultArgNo =
1249 Func->getNumParams() - Parm->getFunctionScopeIndex();
1250 Name += llvm::utostr(DefaultArgNo);
1251 Name += "_";
1252 }
1253
1254 if (LambdaManglingNumber)
1255 LambdaId = LambdaManglingNumber;
1256 else
1257 LambdaId = Context.getLambdaId(Record);
1258
1259 Name += llvm::utostr(LambdaId);
1260 Name += ">";
1261
1262 mangleSourceName(Name);
1263
1264 // If the context is a variable or a class member and not a parameter,
1265 // it is encoded in a qualified name.
1266 if (LambdaManglingNumber && LambdaContextDecl) {
1267 if ((isa<VarDecl>(LambdaContextDecl) ||
1268 isa<FieldDecl>(LambdaContextDecl)) &&
1269 !isa<ParmVarDecl>(LambdaContextDecl)) {
1270 mangleUnqualifiedName(cast<NamedDecl>(LambdaContextDecl));
1271 }
1272 }
1273 break;
1274 }
1275 }
1276
1278 if (DeclaratorDecl *DD =
1279 Context.getASTContext().getDeclaratorForUnnamedTagDecl(TD)) {
1280 // Anonymous types without a name for linkage purposes have their
1281 // declarator mangled in if they have one.
1282 Name += "<unnamed-type-";
1283 Name += DD->getName();
1284 } else if (TypedefNameDecl *TND =
1285 Context.getASTContext().getTypedefNameForUnnamedTagDecl(
1286 TD)) {
1287 // Anonymous types without a name for linkage purposes have their
1288 // associate typedef mangled in if they have one.
1289 Name += "<unnamed-type-";
1290 Name += TND->getName();
1291 } else if (isa<EnumDecl>(TD) &&
1292 cast<EnumDecl>(TD)->enumerator_begin() !=
1293 cast<EnumDecl>(TD)->enumerator_end()) {
1294 // Anonymous non-empty enums mangle in the first enumerator.
1295 auto *ED = cast<EnumDecl>(TD);
1296 Name += "<unnamed-enum-";
1297 Name += ED->enumerator_begin()->getName();
1298 } else {
1299 // Otherwise, number the types using a $S prefix.
1300 Name += "<unnamed-type-$S";
1301 Name += llvm::utostr(Context.getAnonymousStructId(TD) + 1);
1302 }
1303 Name += ">";
1304 mangleSourceName(Name.str());
1305 break;
1306 }
1307
1311 // This is reachable only when constructing an outlined SEH finally
1312 // block. Nothing depends on this mangling and it's used only with
1313 // functinos with internal linkage.
1315 mangleSourceName(Name.str());
1316 break;
1317 }
1318
1320 if (isStructorDecl(ND)) {
1321 if (StructorType == Ctor_CopyingClosure) {
1322 Out << "?_O";
1323 return;
1324 }
1325 if (StructorType == Ctor_DefaultClosure) {
1326 Out << "?_F";
1327 return;
1328 }
1329 }
1330 Out << "?0";
1331 return;
1332
1334 if (isStructorDecl(ND))
1335 // If the named decl is the C++ destructor we're mangling,
1336 // use the type we were given.
1337 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1338 else
1339 // Otherwise, use the base destructor name. This is relevant if a
1340 // class with a destructor is declared within a destructor.
1341 mangleCXXDtorType(Dtor_Base);
1342 break;
1343
1345 // <operator-name> ::= ?B # (cast)
1346 // The target type is encoded as the return type.
1347 Out << "?B";
1348 break;
1349
1351 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
1352 break;
1353
1355 Out << "?__K";
1356 mangleSourceName(Name.getCXXLiteralIdentifier()->getName());
1357 break;
1358 }
1359
1361 llvm_unreachable("Can't mangle a deduction guide name!");
1362
1364 llvm_unreachable("Can't mangle a using directive name!");
1365 }
1366}
1367
1368// <postfix> ::= <unqualified-name> [<postfix>]
1369// ::= <substitution> [<postfix>]
1370void MicrosoftCXXNameMangler::mangleNestedName(GlobalDecl GD) {
1371 const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
1372
1373 if (const auto *ID = dyn_cast<IndirectFieldDecl>(ND))
1374 for (unsigned I = 1, IE = ID->getChainingSize(); I < IE; ++I)
1375 mangleSourceName("<unnamed-tag>");
1376
1377 const DeclContext *DC = getEffectiveDeclContext(ND);
1378 while (!DC->isTranslationUnit()) {
1379 if (isa<TagDecl>(ND) || isa<VarDecl>(ND)) {
1380 unsigned Disc;
1381 if (Context.getNextDiscriminator(ND, Disc)) {
1382 Out << '?';
1383 mangleNumber(Disc);
1384 Out << '?';
1385 }
1386 }
1387
1388 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
1389 auto Discriminate =
1390 [](StringRef Name, const unsigned Discriminator,
1391 const unsigned ParameterDiscriminator) -> std::string {
1392 std::string Buffer;
1393 llvm::raw_string_ostream Stream(Buffer);
1394 Stream << Name;
1395 if (Discriminator)
1396 Stream << '_' << Discriminator;
1397 if (ParameterDiscriminator)
1398 Stream << '_' << ParameterDiscriminator;
1399 return Buffer;
1400 };
1401
1402 unsigned Discriminator = BD->getBlockManglingNumber();
1403 if (!Discriminator)
1404 Discriminator = Context.getBlockId(BD, /*Local=*/false);
1405
1406 // Mangle the parameter position as a discriminator to deal with unnamed
1407 // parameters. Rather than mangling the unqualified parameter name,
1408 // always use the position to give a uniform mangling.
1409 unsigned ParameterDiscriminator = 0;
1410 if (const auto *MC = BD->getBlockManglingContextDecl())
1411 if (const auto *P = dyn_cast<ParmVarDecl>(MC))
1412 if (const auto *F = dyn_cast<FunctionDecl>(P->getDeclContext()))
1413 ParameterDiscriminator =
1414 F->getNumParams() - P->getFunctionScopeIndex();
1415
1416 DC = getEffectiveDeclContext(BD);
1417
1418 Out << '?';
1419 mangleSourceName(Discriminate("_block_invoke", Discriminator,
1420 ParameterDiscriminator));
1421 // If we have a block mangling context, encode that now. This allows us
1422 // to discriminate between named static data initializers in the same
1423 // scope. This is handled differently from parameters, which use
1424 // positions to discriminate between multiple instances.
1425 if (const auto *MC = BD->getBlockManglingContextDecl())
1426 if (!isa<ParmVarDecl>(MC))
1427 if (const auto *ND = dyn_cast<NamedDecl>(MC))
1428 mangleUnqualifiedName(ND);
1429 // MS ABI and Itanium manglings are in inverted scopes. In the case of a
1430 // RecordDecl, mangle the entire scope hierarchy at this point rather than
1431 // just the unqualified name to get the ordering correct.
1432 if (const auto *RD = dyn_cast<RecordDecl>(DC))
1433 mangleName(RD);
1434 else
1435 Out << '@';
1436 // void __cdecl
1437 Out << "YAX";
1438 // struct __block_literal *
1439 Out << 'P';
1440 // __ptr64
1441 if (PointersAre64Bit)
1442 Out << 'E';
1443 Out << 'A';
1444 mangleArtificialTagType(TagTypeKind::Struct,
1445 Discriminate("__block_literal", Discriminator,
1446 ParameterDiscriminator));
1447 Out << "@Z";
1448
1449 // If the effective context was a Record, we have fully mangled the
1450 // qualified name and do not need to continue.
1451 if (isa<RecordDecl>(DC))
1452 break;
1453 continue;
1454 } else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
1455 mangleObjCMethodName(Method);
1456 } else if (isa<NamedDecl>(DC)) {
1457 ND = cast<NamedDecl>(DC);
1458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1459 mangle(getGlobalDeclAsDeclContext(FD), "?");
1460 break;
1461 } else {
1462 mangleUnqualifiedName(ND);
1463 // Lambdas in default arguments conceptually belong to the function the
1464 // parameter corresponds to.
1465 if (const auto *LDADC = getLambdaDefaultArgumentDeclContext(ND)) {
1466 DC = LDADC;
1467 continue;
1468 }
1469 }
1470 }
1471 DC = DC->getParent();
1472 }
1473}
1474
1475void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
1476 // Microsoft uses the names on the case labels for these dtor variants. Clang
1477 // uses the Itanium terminology internally. Everything in this ABI delegates
1478 // towards the base dtor.
1479 switch (T) {
1480 // <operator-name> ::= ?1 # destructor
1481 case Dtor_Base: Out << "?1"; return;
1482 // <operator-name> ::= ?_D # vbase destructor
1483 case Dtor_Complete: Out << "?_D"; return;
1484 // <operator-name> ::= ?_G # scalar deleting destructor
1485 case Dtor_Deleting: Out << "?_G"; return;
1486 // <operator-name> ::= ?_E # vector deleting destructor
1487 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
1488 // it.
1489 case Dtor_Comdat:
1490 llvm_unreachable("not expecting a COMDAT");
1491 }
1492 llvm_unreachable("Unsupported dtor type?");
1493}
1494
1495void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
1497 switch (OO) {
1498 // ?0 # constructor
1499 // ?1 # destructor
1500 // <operator-name> ::= ?2 # new
1501 case OO_New: Out << "?2"; break;
1502 // <operator-name> ::= ?3 # delete
1503 case OO_Delete: Out << "?3"; break;
1504 // <operator-name> ::= ?4 # =
1505 case OO_Equal: Out << "?4"; break;
1506 // <operator-name> ::= ?5 # >>
1507 case OO_GreaterGreater: Out << "?5"; break;
1508 // <operator-name> ::= ?6 # <<
1509 case OO_LessLess: Out << "?6"; break;
1510 // <operator-name> ::= ?7 # !
1511 case OO_Exclaim: Out << "?7"; break;
1512 // <operator-name> ::= ?8 # ==
1513 case OO_EqualEqual: Out << "?8"; break;
1514 // <operator-name> ::= ?9 # !=
1515 case OO_ExclaimEqual: Out << "?9"; break;
1516 // <operator-name> ::= ?A # []
1517 case OO_Subscript: Out << "?A"; break;
1518 // ?B # conversion
1519 // <operator-name> ::= ?C # ->
1520 case OO_Arrow: Out << "?C"; break;
1521 // <operator-name> ::= ?D # *
1522 case OO_Star: Out << "?D"; break;
1523 // <operator-name> ::= ?E # ++
1524 case OO_PlusPlus: Out << "?E"; break;
1525 // <operator-name> ::= ?F # --
1526 case OO_MinusMinus: Out << "?F"; break;
1527 // <operator-name> ::= ?G # -
1528 case OO_Minus: Out << "?G"; break;
1529 // <operator-name> ::= ?H # +
1530 case OO_Plus: Out << "?H"; break;
1531 // <operator-name> ::= ?I # &
1532 case OO_Amp: Out << "?I"; break;
1533 // <operator-name> ::= ?J # ->*
1534 case OO_ArrowStar: Out << "?J"; break;
1535 // <operator-name> ::= ?K # /
1536 case OO_Slash: Out << "?K"; break;
1537 // <operator-name> ::= ?L # %
1538 case OO_Percent: Out << "?L"; break;
1539 // <operator-name> ::= ?M # <
1540 case OO_Less: Out << "?M"; break;
1541 // <operator-name> ::= ?N # <=
1542 case OO_LessEqual: Out << "?N"; break;
1543 // <operator-name> ::= ?O # >
1544 case OO_Greater: Out << "?O"; break;
1545 // <operator-name> ::= ?P # >=
1546 case OO_GreaterEqual: Out << "?P"; break;
1547 // <operator-name> ::= ?Q # ,
1548 case OO_Comma: Out << "?Q"; break;
1549 // <operator-name> ::= ?R # ()
1550 case OO_Call: Out << "?R"; break;
1551 // <operator-name> ::= ?S # ~
1552 case OO_Tilde: Out << "?S"; break;
1553 // <operator-name> ::= ?T # ^
1554 case OO_Caret: Out << "?T"; break;
1555 // <operator-name> ::= ?U # |
1556 case OO_Pipe: Out << "?U"; break;
1557 // <operator-name> ::= ?V # &&
1558 case OO_AmpAmp: Out << "?V"; break;
1559 // <operator-name> ::= ?W # ||
1560 case OO_PipePipe: Out << "?W"; break;
1561 // <operator-name> ::= ?X # *=
1562 case OO_StarEqual: Out << "?X"; break;
1563 // <operator-name> ::= ?Y # +=
1564 case OO_PlusEqual: Out << "?Y"; break;
1565 // <operator-name> ::= ?Z # -=
1566 case OO_MinusEqual: Out << "?Z"; break;
1567 // <operator-name> ::= ?_0 # /=
1568 case OO_SlashEqual: Out << "?_0"; break;
1569 // <operator-name> ::= ?_1 # %=
1570 case OO_PercentEqual: Out << "?_1"; break;
1571 // <operator-name> ::= ?_2 # >>=
1572 case OO_GreaterGreaterEqual: Out << "?_2"; break;
1573 // <operator-name> ::= ?_3 # <<=
1574 case OO_LessLessEqual: Out << "?_3"; break;
1575 // <operator-name> ::= ?_4 # &=
1576 case OO_AmpEqual: Out << "?_4"; break;
1577 // <operator-name> ::= ?_5 # |=
1578 case OO_PipeEqual: Out << "?_5"; break;
1579 // <operator-name> ::= ?_6 # ^=
1580 case OO_CaretEqual: Out << "?_6"; break;
1581 // ?_7 # vftable
1582 // ?_8 # vbtable
1583 // ?_9 # vcall
1584 // ?_A # typeof
1585 // ?_B # local static guard
1586 // ?_C # string
1587 // ?_D # vbase destructor
1588 // ?_E # vector deleting destructor
1589 // ?_F # default constructor closure
1590 // ?_G # scalar deleting destructor
1591 // ?_H # vector constructor iterator
1592 // ?_I # vector destructor iterator
1593 // ?_J # vector vbase constructor iterator
1594 // ?_K # virtual displacement map
1595 // ?_L # eh vector constructor iterator
1596 // ?_M # eh vector destructor iterator
1597 // ?_N # eh vector vbase constructor iterator
1598 // ?_O # copy constructor closure
1599 // ?_P<name> # udt returning <name>
1600 // ?_Q # <unknown>
1601 // ?_R0 # RTTI Type Descriptor
1602 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
1603 // ?_R2 # RTTI Base Class Array
1604 // ?_R3 # RTTI Class Hierarchy Descriptor
1605 // ?_R4 # RTTI Complete Object Locator
1606 // ?_S # local vftable
1607 // ?_T # local vftable constructor closure
1608 // <operator-name> ::= ?_U # new[]
1609 case OO_Array_New: Out << "?_U"; break;
1610 // <operator-name> ::= ?_V # delete[]
1611 case OO_Array_Delete: Out << "?_V"; break;
1612 // <operator-name> ::= ?__L # co_await
1613 case OO_Coawait: Out << "?__L"; break;
1614 // <operator-name> ::= ?__M # <=>
1615 case OO_Spaceship: Out << "?__M"; break;
1616
1617 case OO_Conditional: {
1618 Error(Loc, "conditional operator");
1619 break;
1620 }
1621
1622 case OO_None:
1624 llvm_unreachable("Not an overloaded operator");
1625 }
1626}
1627
1628void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) {
1629 // <source name> ::= <identifier> @
1630 BackRefVec::iterator Found = llvm::find(NameBackReferences, Name);
1631 if (Found == NameBackReferences.end()) {
1632 if (NameBackReferences.size() < 10)
1633 NameBackReferences.push_back(std::string(Name));
1634 Out << Name << '@';
1635 } else {
1636 Out << (Found - NameBackReferences.begin());
1637 }
1638}
1639
1640void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
1641 Context.mangleObjCMethodNameAsSourceName(MD, Out);
1642}
1643
1644void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
1645 GlobalDecl GD, const TemplateArgumentList &TemplateArgs) {
1646 // <template-name> ::= <unscoped-template-name> <template-args>
1647 // ::= <substitution>
1648 // Always start with the unqualified name.
1649
1650 // Templates have their own context for back references.
1651 ArgBackRefMap OuterFunArgsContext;
1652 ArgBackRefMap OuterTemplateArgsContext;
1653 BackRefVec OuterTemplateContext;
1654 PassObjectSizeArgsSet OuterPassObjectSizeArgs;
1655 NameBackReferences.swap(OuterTemplateContext);
1656 FunArgBackReferences.swap(OuterFunArgsContext);
1657 TemplateArgBackReferences.swap(OuterTemplateArgsContext);
1658 PassObjectSizeArgs.swap(OuterPassObjectSizeArgs);
1659
1660 mangleUnscopedTemplateName(GD);
1661 mangleTemplateArgs(cast<TemplateDecl>(GD.getDecl()), TemplateArgs);
1662
1663 // Restore the previous back reference contexts.
1664 NameBackReferences.swap(OuterTemplateContext);
1665 FunArgBackReferences.swap(OuterFunArgsContext);
1666 TemplateArgBackReferences.swap(OuterTemplateArgsContext);
1667 PassObjectSizeArgs.swap(OuterPassObjectSizeArgs);
1668}
1669
1670void MicrosoftCXXNameMangler::mangleUnscopedTemplateName(GlobalDecl GD) {
1671 // <unscoped-template-name> ::= ?$ <unqualified-name>
1672 Out << "?$";
1673 mangleUnqualifiedName(GD);
1674}
1675
1676void MicrosoftCXXNameMangler::mangleIntegerLiteral(
1677 const llvm::APSInt &Value, const NonTypeTemplateParmDecl *PD,
1678 QualType TemplateArgType) {
1679 // <integer-literal> ::= $0 <number>
1680 // <integer-literal> ::= <auto-nttp>
1681 //
1682 // <auto-nttp> ::= $ M <type> 0 <number>
1683 Out << "$";
1684
1685 // Since MSVC 2019, add 'M[<type>]' after '$' for auto template parameter when
1686 // argument is integer.
1687 if (getASTContext().getLangOpts().isCompatibleWithMSVC(
1688 LangOptions::MSVC2019) &&
1689 PD && PD->getType()->getTypeClass() == Type::Auto &&
1690 !TemplateArgType.isNull()) {
1691 Out << "M";
1692 mangleType(TemplateArgType, SourceRange(), QMM_Drop);
1693 }
1694
1695 Out << "0";
1696
1697 mangleNumber(Value);
1698}
1699
1700void MicrosoftCXXNameMangler::mangleExpression(
1701 const Expr *E, const NonTypeTemplateParmDecl *PD) {
1702 // See if this is a constant expression.
1703 if (std::optional<llvm::APSInt> Value =
1704 E->getIntegerConstantExpr(Context.getASTContext())) {
1705 mangleIntegerLiteral(*Value, PD, E->getType());
1706 return;
1707 }
1708
1709 // As bad as this diagnostic is, it's better than crashing.
1710 Error(E->getExprLoc(), "expression type: ", E->getStmtClassName())
1711 << E->getSourceRange();
1712}
1713
1714void MicrosoftCXXNameMangler::mangleTemplateArgs(
1715 const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) {
1716 // <template-args> ::= <template-arg>+
1718 assert(TPL->size() == TemplateArgs.size() &&
1719 "size mismatch between args and parms!");
1720
1721 for (size_t i = 0; i < TemplateArgs.size(); ++i) {
1722 const TemplateArgument &TA = TemplateArgs[i];
1723
1724 // Separate consecutive packs by $$Z.
1725 if (i > 0 && TA.getKind() == TemplateArgument::Pack &&
1726 TemplateArgs[i - 1].getKind() == TemplateArgument::Pack)
1727 Out << "$$Z";
1728
1729 mangleTemplateArg(TD, TA, TPL->getParam(i));
1730 }
1731}
1732
1733/// If value V (with type T) represents a decayed pointer to the first element
1734/// of an array, return that array.
1736 // Must be a pointer...
1737 if (!T->isPointerType() || !V.isLValue() || !V.hasLValuePath() ||
1738 !V.getLValueBase())
1739 return nullptr;
1740 // ... to element 0 of an array.
1741 QualType BaseT = V.getLValueBase().getType();
1742 if (!BaseT->isArrayType() || V.getLValuePath().size() != 1 ||
1743 V.getLValuePath()[0].getAsArrayIndex() != 0)
1744 return nullptr;
1745 return const_cast<ValueDecl *>(
1746 V.getLValueBase().dyn_cast<const ValueDecl *>());
1747}
1748
1749void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
1750 const TemplateArgument &TA,
1751 const NamedDecl *Parm) {
1752 // <template-arg> ::= <type>
1753 // ::= <integer-literal>
1754 // ::= <member-data-pointer>
1755 // ::= <member-function-pointer>
1756 // ::= $ <constant-value>
1757 // ::= $ <auto-nttp-constant-value>
1758 // ::= <template-args>
1759 //
1760 // <auto-nttp-constant-value> ::= M <type> <constant-value>
1761 //
1762 // <constant-value> ::= 0 <number> # integer
1763 // ::= 1 <mangled-name> # address of D
1764 // ::= 2 <type> <typed-constant-value>* @ # struct
1765 // ::= 3 <type> <constant-value>* @ # array
1766 // ::= 4 ??? # string
1767 // ::= 5 <constant-value> @ # address of subobject
1768 // ::= 6 <constant-value> <unqualified-name> @ # a.b
1769 // ::= 7 <type> [<unqualified-name> <constant-value>] @
1770 // # union, with or without an active member
1771 // # pointer to member, symbolically
1772 // ::= 8 <class> <unqualified-name> @
1773 // ::= A <type> <non-negative integer> # float
1774 // ::= B <type> <non-negative integer> # double
1775 // # pointer to member, by component value
1776 // ::= F <number> <number>
1777 // ::= G <number> <number> <number>
1778 // ::= H <mangled-name> <number>
1779 // ::= I <mangled-name> <number> <number>
1780 // ::= J <mangled-name> <number> <number> <number>
1781 //
1782 // <typed-constant-value> ::= [<type>] <constant-value>
1783 //
1784 // The <type> appears to be included in a <typed-constant-value> only in the
1785 // '0', '1', '8', 'A', 'B', and 'E' cases.
1786
1787 switch (TA.getKind()) {
1789 llvm_unreachable("Can't mangle null template arguments!");
1791 llvm_unreachable("Can't mangle template expansion arguments!");
1793 QualType T = TA.getAsType();
1794 mangleType(T, SourceRange(), QMM_Escape);
1795 break;
1796 }
1798 const NamedDecl *ND = TA.getAsDecl();
1799 if (isa<FieldDecl>(ND) || isa<IndirectFieldDecl>(ND)) {
1800 mangleMemberDataPointer(cast<CXXRecordDecl>(ND->getDeclContext())
1801 ->getMostRecentNonInjectedDecl(),
1802 cast<ValueDecl>(ND),
1803 cast<NonTypeTemplateParmDecl>(Parm),
1804 TA.getParamTypeForDecl());
1805 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1806 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
1807 if (MD && MD->isInstance()) {
1808 mangleMemberFunctionPointer(
1810 cast<NonTypeTemplateParmDecl>(Parm), TA.getParamTypeForDecl());
1811 } else {
1812 mangleFunctionPointer(FD, cast<NonTypeTemplateParmDecl>(Parm),
1813 TA.getParamTypeForDecl());
1814 }
1815 } else if (TA.getParamTypeForDecl()->isRecordType()) {
1816 Out << "$";
1817 auto *TPO = cast<TemplateParamObjectDecl>(ND);
1818 mangleTemplateArgValue(TPO->getType().getUnqualifiedType(),
1819 TPO->getValue(), TplArgKind::ClassNTTP);
1820 } else if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1821 mangleVarDecl(VD, cast<NonTypeTemplateParmDecl>(Parm),
1822 TA.getParamTypeForDecl());
1823 } else {
1824 mangle(ND, "$1?");
1825 }
1826 break;
1827 }
1829 QualType T = TA.getIntegralType();
1830 mangleIntegerLiteral(TA.getAsIntegral(),
1831 cast<NonTypeTemplateParmDecl>(Parm), T);
1832 break;
1833 }
1835 QualType T = TA.getNullPtrType();
1836 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
1837 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
1838 if (MPT->isMemberFunctionPointerType() &&
1839 !isa<FunctionTemplateDecl>(TD)) {
1840 mangleMemberFunctionPointer(RD, nullptr, nullptr, QualType());
1841 return;
1842 }
1843 if (MPT->isMemberDataPointer()) {
1844 if (!isa<FunctionTemplateDecl>(TD)) {
1845 mangleMemberDataPointer(RD, nullptr, nullptr, QualType());
1846 return;
1847 }
1848 // nullptr data pointers are always represented with a single field
1849 // which is initialized with either 0 or -1. Why -1? Well, we need to
1850 // distinguish the case where the data member is at offset zero in the
1851 // record.
1852 // However, we are free to use 0 *if* we would use multiple fields for
1853 // non-nullptr member pointers.
1854 if (!RD->nullFieldOffsetIsZero()) {
1855 mangleIntegerLiteral(llvm::APSInt::get(-1),
1856 cast<NonTypeTemplateParmDecl>(Parm), T);
1857 return;
1858 }
1859 }
1860 }
1861 mangleIntegerLiteral(llvm::APSInt::getUnsigned(0),
1862 cast<NonTypeTemplateParmDecl>(Parm), T);
1863 break;
1864 }
1868 // Mangle the result of array-to-pointer decay as if it were a reference
1869 // to the original declaration, to match MSVC's behavior. This can result
1870 // in mangling collisions in some cases!
1871 return mangleTemplateArg(
1872 TD, TemplateArgument(D, TA.getStructuralValueType()), Parm);
1873 }
1874 Out << "$";
1875 if (cast<NonTypeTemplateParmDecl>(Parm)
1876 ->getType()
1877 ->getContainedDeducedType()) {
1878 Out << "M";
1879 mangleType(TA.getNonTypeTemplateArgumentType(), SourceRange(), QMM_Drop);
1880 }
1881 mangleTemplateArgValue(TA.getStructuralValueType(),
1883 TplArgKind::StructuralValue,
1884 /*WithScalarType=*/false);
1885 break;
1887 mangleExpression(TA.getAsExpr(), cast<NonTypeTemplateParmDecl>(Parm));
1888 break;
1890 ArrayRef<TemplateArgument> TemplateArgs = TA.getPackAsArray();
1891 if (TemplateArgs.empty()) {
1892 if (isa<TemplateTypeParmDecl>(Parm) ||
1893 isa<TemplateTemplateParmDecl>(Parm))
1894 // MSVC 2015 changed the mangling for empty expanded template packs,
1895 // use the old mangling for link compatibility for old versions.
1896 Out << (Context.getASTContext().getLangOpts().isCompatibleWithMSVC(
1897 LangOptions::MSVC2015)
1898 ? "$$V"
1899 : "$$$V");
1900 else if (isa<NonTypeTemplateParmDecl>(Parm))
1901 Out << "$S";
1902 else
1903 llvm_unreachable("unexpected template parameter decl!");
1904 } else {
1905 for (const TemplateArgument &PA : TemplateArgs)
1906 mangleTemplateArg(TD, PA, Parm);
1907 }
1908 break;
1909 }
1911 const NamedDecl *ND =
1913 if (const auto *TD = dyn_cast<TagDecl>(ND)) {
1914 mangleType(TD);
1915 } else if (isa<TypeAliasDecl>(ND)) {
1916 Out << "$$Y";
1917 mangleName(ND);
1918 } else {
1919 llvm_unreachable("unexpected template template NamedDecl!");
1920 }
1921 break;
1922 }
1923 }
1924}
1925
1926void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
1927 const APValue &V,
1928 TplArgKind TAK,
1929 bool WithScalarType) {
1930 switch (V.getKind()) {
1931 case APValue::None:
1933 // FIXME: MSVC doesn't allow this, so we can't be sure how it should be
1934 // mangled.
1935 if (WithScalarType)
1936 mangleType(T, SourceRange(), QMM_Escape);
1937 Out << '@';
1938 return;
1939
1940 case APValue::Int:
1941 if (WithScalarType)
1942 mangleType(T, SourceRange(), QMM_Escape);
1943 Out << '0';
1944 mangleNumber(V.getInt());
1945 return;
1946
1947 case APValue::Float:
1948 if (WithScalarType)
1949 mangleType(T, SourceRange(), QMM_Escape);
1950 mangleFloat(V.getFloat());
1951 return;
1952
1953 case APValue::LValue: {
1954 if (WithScalarType)
1955 mangleType(T, SourceRange(), QMM_Escape);
1956
1957 APValue::LValueBase Base = V.getLValueBase();
1958
1959 // this might not cover every case but did cover issue 97756
1960 // see test CodeGen/ms_mangler_templatearg_opte
1961 if (V.isLValueOnePastTheEnd()) {
1962 Out << "5E";
1963 auto *VD = Base.dyn_cast<const ValueDecl *>();
1964 if (VD)
1965 mangle(VD);
1966 Out << "@";
1967 return;
1968 }
1969
1970 if (!V.hasLValuePath() || V.getLValuePath().empty()) {
1971 // Taking the address of a complete object has a special-case mangling.
1972 if (Base.isNull()) {
1973 // MSVC emits 0A@ for null pointers. Generalize this for arbitrary
1974 // integers cast to pointers.
1975 // FIXME: This mangles 0 cast to a pointer the same as a null pointer,
1976 // even in cases where the two are different values.
1977 Out << "0";
1978 mangleNumber(V.getLValueOffset().getQuantity());
1979 } else if (!V.hasLValuePath()) {
1980 // FIXME: This can only happen as an extension. Invent a mangling.
1981 Error("template argument (extension not comaptible with ms mangler)");
1982 return;
1983 } else if (auto *VD = Base.dyn_cast<const ValueDecl*>()) {
1984 Out << "E";
1985 mangle(VD);
1986 } else {
1987 Error("template argument (undeclared base)");
1988 return;
1989 }
1990 } else {
1991 if (TAK == TplArgKind::ClassNTTP && T->isPointerType())
1992 Out << "5";
1993
1994 SmallVector<char, 2> EntryTypes;
1995 SmallVector<std::function<void()>, 2> EntryManglers;
1996 QualType ET = Base.getType();
1997 for (APValue::LValuePathEntry E : V.getLValuePath()) {
1998 if (auto *AT = ET->getAsArrayTypeUnsafe()) {
1999 EntryTypes.push_back('C');
2000 EntryManglers.push_back([this, I = E.getAsArrayIndex()] {
2001 Out << '0';
2002 mangleNumber(I);
2003 Out << '@';
2004 });
2005 ET = AT->getElementType();
2006 continue;
2007 }
2008
2009 const Decl *D = E.getAsBaseOrMember().getPointer();
2010 if (auto *FD = dyn_cast<FieldDecl>(D)) {
2011 ET = FD->getType();
2012 if (const auto *RD = ET->getAsRecordDecl())
2013 if (RD->isAnonymousStructOrUnion())
2014 continue;
2015 } else {
2016 ET = getASTContext().getRecordType(cast<CXXRecordDecl>(D));
2017 // Bug in MSVC: fully qualified name of base class should be used for
2018 // mangling to prevent collisions e.g. on base classes with same names
2019 // in different namespaces.
2020 }
2021
2022 EntryTypes.push_back('6');
2023 EntryManglers.push_back([this, D] {
2024 mangleUnqualifiedName(cast<NamedDecl>(D));
2025 Out << '@';
2026 });
2027 }
2028
2029 for (auto I = EntryTypes.rbegin(), E = EntryTypes.rend(); I != E; ++I)
2030 Out << *I;
2031
2032 auto *VD = Base.dyn_cast<const ValueDecl*>();
2033 if (!VD) {
2034 Error("template argument (null value decl)");
2035 return;
2036 }
2037 Out << (TAK == TplArgKind::ClassNTTP ? 'E' : '1');
2038 mangle(VD);
2039
2040 for (const std::function<void()> &Mangler : EntryManglers)
2041 Mangler();
2042 if (TAK == TplArgKind::ClassNTTP && T->isPointerType())
2043 Out << '@';
2044 }
2045
2046 return;
2047 }
2048
2050 if (WithScalarType)
2051 mangleType(T, SourceRange(), QMM_Escape);
2052
2053 const CXXRecordDecl *RD =
2054 T->castAs<MemberPointerType>()->getMostRecentCXXRecordDecl();
2055 const ValueDecl *D = V.getMemberPointerDecl();
2056 if (TAK == TplArgKind::ClassNTTP) {
2058 mangleMemberDataPointerInClassNTTP(RD, D);
2059 else
2060 mangleMemberFunctionPointerInClassNTTP(RD,
2061 cast_or_null<CXXMethodDecl>(D));
2062 } else {
2064 mangleMemberDataPointer(RD, D, nullptr, QualType(), "");
2065 else
2066 mangleMemberFunctionPointer(RD, cast_or_null<CXXMethodDecl>(D), nullptr,
2067 QualType(), "");
2068 }
2069 return;
2070 }
2071
2072 case APValue::Struct: {
2073 Out << '2';
2074 mangleType(T, SourceRange(), QMM_Escape);
2075 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
2076 assert(RD && "unexpected type for record value");
2077
2078 unsigned BaseIndex = 0;
2079 for (const CXXBaseSpecifier &B : RD->bases())
2080 mangleTemplateArgValue(B.getType(), V.getStructBase(BaseIndex++), TAK);
2081 for (const FieldDecl *FD : RD->fields())
2082 if (!FD->isUnnamedBitField())
2083 mangleTemplateArgValue(FD->getType(),
2084 V.getStructField(FD->getFieldIndex()), TAK,
2085 /*WithScalarType*/ true);
2086 Out << '@';
2087 return;
2088 }
2089
2090 case APValue::Union:
2091 Out << '7';
2092 mangleType(T, SourceRange(), QMM_Escape);
2093 if (const FieldDecl *FD = V.getUnionField()) {
2094 mangleUnqualifiedName(FD);
2095 mangleTemplateArgValue(FD->getType(), V.getUnionValue(), TAK);
2096 }
2097 Out << '@';
2098 return;
2099
2101 // We mangle complex types as structs, so mangle the value as a struct too.
2102 Out << '2';
2103 mangleType(T, SourceRange(), QMM_Escape);
2104 Out << '0';
2105 mangleNumber(V.getComplexIntReal());
2106 Out << '0';
2107 mangleNumber(V.getComplexIntImag());
2108 Out << '@';
2109 return;
2110
2112 Out << '2';
2113 mangleType(T, SourceRange(), QMM_Escape);
2114 mangleFloat(V.getComplexFloatReal());
2115 mangleFloat(V.getComplexFloatImag());
2116 Out << '@';
2117 return;
2118
2119 case APValue::Array: {
2120 Out << '3';
2121 QualType ElemT = getASTContext().getAsArrayType(T)->getElementType();
2122 mangleType(ElemT, SourceRange(), QMM_Escape);
2123 for (unsigned I = 0, N = V.getArraySize(); I != N; ++I) {
2124 const APValue &ElemV = I < V.getArrayInitializedElts()
2125 ? V.getArrayInitializedElt(I)
2126 : V.getArrayFiller();
2127 mangleTemplateArgValue(ElemT, ElemV, TAK);
2128 Out << '@';
2129 }
2130 Out << '@';
2131 return;
2132 }
2133
2134 case APValue::Vector: {
2135 // __m128 is mangled as a struct containing an array. We follow this
2136 // approach for all vector types.
2137 Out << '2';
2138 mangleType(T, SourceRange(), QMM_Escape);
2139 Out << '3';
2140 QualType ElemT = T->castAs<VectorType>()->getElementType();
2141 mangleType(ElemT, SourceRange(), QMM_Escape);
2142 for (unsigned I = 0, N = V.getVectorLength(); I != N; ++I) {
2143 const APValue &ElemV = V.getVectorElt(I);
2144 mangleTemplateArgValue(ElemT, ElemV, TAK);
2145 Out << '@';
2146 }
2147 Out << "@@";
2148 return;
2149 }
2150
2152 Error("template argument (value type: address label diff)");
2153 return;
2154 }
2155
2156 case APValue::FixedPoint: {
2157 Error("template argument (value type: fixed point)");
2158 return;
2159 }
2160 }
2161}
2162
2163void MicrosoftCXXNameMangler::mangleObjCProtocol(const ObjCProtocolDecl *PD) {
2164 llvm::SmallString<64> TemplateMangling;
2165 llvm::raw_svector_ostream Stream(TemplateMangling);
2166 MicrosoftCXXNameMangler Extra(Context, Stream);
2167
2168 Stream << "?$";
2169 Extra.mangleSourceName("Protocol");
2170 Extra.mangleArtificialTagType(TagTypeKind::Struct, PD->getName());
2171
2172 mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__ObjC"});
2173}
2174
2175void MicrosoftCXXNameMangler::mangleObjCLifetime(const QualType Type,
2176 Qualifiers Quals,
2178 llvm::SmallString<64> TemplateMangling;
2179 llvm::raw_svector_ostream Stream(TemplateMangling);
2180 MicrosoftCXXNameMangler Extra(Context, Stream);
2181
2182 Stream << "?$";
2183 switch (Quals.getObjCLifetime()) {
2186 break;
2188 Extra.mangleSourceName("Autoreleasing");
2189 break;
2191 Extra.mangleSourceName("Strong");
2192 break;
2194 Extra.mangleSourceName("Weak");
2195 break;
2196 }
2197 Extra.manglePointerCVQualifiers(Quals);
2198 Extra.manglePointerExtQualifiers(Quals, Type);
2199 Extra.mangleType(Type, Range);
2200
2201 mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__ObjC"});
2202}
2203
2204void MicrosoftCXXNameMangler::mangleObjCKindOfType(const ObjCObjectType *T,
2205 Qualifiers Quals,
2207 llvm::SmallString<64> TemplateMangling;
2208 llvm::raw_svector_ostream Stream(TemplateMangling);
2209 MicrosoftCXXNameMangler Extra(Context, Stream);
2210
2211 Stream << "?$";
2212 Extra.mangleSourceName("KindOf");
2213 Extra.mangleType(QualType(T, 0)
2214 .stripObjCKindOfType(getASTContext())
2215 ->castAs<ObjCObjectType>(),
2216 Quals, Range);
2217
2218 mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__ObjC"});
2219}
2220
2221void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
2222 bool IsMember) {
2223 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
2224 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
2225 // 'I' means __restrict (32/64-bit).
2226 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
2227 // keyword!
2228 // <base-cvr-qualifiers> ::= A # near
2229 // ::= B # near const
2230 // ::= C # near volatile
2231 // ::= D # near const volatile
2232 // ::= E # far (16-bit)
2233 // ::= F # far const (16-bit)
2234 // ::= G # far volatile (16-bit)
2235 // ::= H # far const volatile (16-bit)
2236 // ::= I # huge (16-bit)
2237 // ::= J # huge const (16-bit)
2238 // ::= K # huge volatile (16-bit)
2239 // ::= L # huge const volatile (16-bit)
2240 // ::= M <basis> # based
2241 // ::= N <basis> # based const
2242 // ::= O <basis> # based volatile
2243 // ::= P <basis> # based const volatile
2244 // ::= Q # near member
2245 // ::= R # near const member
2246 // ::= S # near volatile member
2247 // ::= T # near const volatile member
2248 // ::= U # far member (16-bit)
2249 // ::= V # far const member (16-bit)
2250 // ::= W # far volatile member (16-bit)
2251 // ::= X # far const volatile member (16-bit)
2252 // ::= Y # huge member (16-bit)
2253 // ::= Z # huge const member (16-bit)
2254 // ::= 0 # huge volatile member (16-bit)
2255 // ::= 1 # huge const volatile member (16-bit)
2256 // ::= 2 <basis> # based member
2257 // ::= 3 <basis> # based const member
2258 // ::= 4 <basis> # based volatile member
2259 // ::= 5 <basis> # based const volatile member
2260 // ::= 6 # near function (pointers only)
2261 // ::= 7 # far function (pointers only)
2262 // ::= 8 # near method (pointers only)
2263 // ::= 9 # far method (pointers only)
2264 // ::= _A <basis> # based function (pointers only)
2265 // ::= _B <basis> # based function (far?) (pointers only)
2266 // ::= _C <basis> # based method (pointers only)
2267 // ::= _D <basis> # based method (far?) (pointers only)
2268 // ::= _E # block (Clang)
2269 // <basis> ::= 0 # __based(void)
2270 // ::= 1 # __based(segment)?
2271 // ::= 2 <name> # __based(name)
2272 // ::= 3 # ?
2273 // ::= 4 # ?
2274 // ::= 5 # not really based
2275 bool HasConst = Quals.hasConst(),
2276 HasVolatile = Quals.hasVolatile();
2277
2278 if (!IsMember) {
2279 if (HasConst && HasVolatile) {
2280 Out << 'D';
2281 } else if (HasVolatile) {
2282 Out << 'C';
2283 } else if (HasConst) {
2284 Out << 'B';
2285 } else {
2286 Out << 'A';
2287 }
2288 } else {
2289 if (HasConst && HasVolatile) {
2290 Out << 'T';
2291 } else if (HasVolatile) {
2292 Out << 'S';
2293 } else if (HasConst) {
2294 Out << 'R';
2295 } else {
2296 Out << 'Q';
2297 }
2298 }
2299
2300 // FIXME: For now, just drop all extension qualifiers on the floor.
2301}
2302
2303void
2304MicrosoftCXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
2305 // <ref-qualifier> ::= G # lvalue reference
2306 // ::= H # rvalue-reference
2307 switch (RefQualifier) {
2308 case RQ_None:
2309 break;
2310
2311 case RQ_LValue:
2312 Out << 'G';
2313 break;
2314
2315 case RQ_RValue:
2316 Out << 'H';
2317 break;
2318 }
2319}
2320
2321void MicrosoftCXXNameMangler::manglePointerExtQualifiers(Qualifiers Quals,
2322 QualType PointeeType) {
2323 // Check if this is a default 64-bit pointer or has __ptr64 qualifier.
2324 bool is64Bit = PointeeType.isNull() ? PointersAre64Bit :
2325 is64BitPointer(PointeeType.getQualifiers());
2326 if (is64Bit && (PointeeType.isNull() || !PointeeType->isFunctionType()))
2327 Out << 'E';
2328
2329 if (Quals.hasRestrict())
2330 Out << 'I';
2331
2332 if (Quals.hasUnaligned() ||
2333 (!PointeeType.isNull() && PointeeType.getLocalQualifiers().hasUnaligned()))
2334 Out << 'F';
2335}
2336
2337void MicrosoftCXXNameMangler::manglePointerCVQualifiers(Qualifiers Quals) {
2338 // <pointer-cv-qualifiers> ::= P # no qualifiers
2339 // ::= Q # const
2340 // ::= R # volatile
2341 // ::= S # const volatile
2342 bool HasConst = Quals.hasConst(),
2343 HasVolatile = Quals.hasVolatile();
2344
2345 if (HasConst && HasVolatile) {
2346 Out << 'S';
2347 } else if (HasVolatile) {
2348 Out << 'R';
2349 } else if (HasConst) {
2350 Out << 'Q';
2351 } else {
2352 Out << 'P';
2353 }
2354}
2355
2356void MicrosoftCXXNameMangler::mangleFunctionArgumentType(QualType T,
2358 // MSVC will backreference two canonically equivalent types that have slightly
2359 // different manglings when mangled alone.
2360
2361 // Decayed types do not match up with non-decayed versions of the same type.
2362 //
2363 // e.g.
2364 // void (*x)(void) will not form a backreference with void x(void)
2365 void *TypePtr;
2366 if (const auto *DT = T->getAs<DecayedType>()) {
2367 QualType OriginalType = DT->getOriginalType();
2368 // All decayed ArrayTypes should be treated identically; as-if they were
2369 // a decayed IncompleteArrayType.
2370 if (const auto *AT = getASTContext().getAsArrayType(OriginalType))
2371 OriginalType = getASTContext().getIncompleteArrayType(
2372 AT->getElementType(), AT->getSizeModifier(),
2373 AT->getIndexTypeCVRQualifiers());
2374
2375 TypePtr = OriginalType.getCanonicalType().getAsOpaquePtr();
2376 // If the original parameter was textually written as an array,
2377 // instead treat the decayed parameter like it's const.
2378 //
2379 // e.g.
2380 // int [] -> int * const
2381 if (OriginalType->isArrayType())
2382 T = T.withConst();
2383 } else {
2384 TypePtr = T.getCanonicalType().getAsOpaquePtr();
2385 }
2386
2387 ArgBackRefMap::iterator Found = FunArgBackReferences.find(TypePtr);
2388
2389 if (Found == FunArgBackReferences.end()) {
2390 size_t OutSizeBefore = Out.tell();
2391
2392 mangleType(T, Range, QMM_Drop);
2393
2394 // See if it's worth creating a back reference.
2395 // Only types longer than 1 character are considered
2396 // and only 10 back references slots are available:
2397 bool LongerThanOneChar = (Out.tell() - OutSizeBefore > 1);
2398 if (LongerThanOneChar && FunArgBackReferences.size() < 10) {
2399 size_t Size = FunArgBackReferences.size();
2400 FunArgBackReferences[TypePtr] = Size;
2401 }
2402 } else {
2403 Out << Found->second;
2404 }
2405}
2406
2407void MicrosoftCXXNameMangler::manglePassObjectSizeArg(
2408 const PassObjectSizeAttr *POSA) {
2409 int Type = POSA->getType();
2410 bool Dynamic = POSA->isDynamic();
2411
2412 auto Iter = PassObjectSizeArgs.insert({Type, Dynamic}).first;
2413 auto *TypePtr = (const void *)&*Iter;
2414 ArgBackRefMap::iterator Found = FunArgBackReferences.find(TypePtr);
2415
2416 if (Found == FunArgBackReferences.end()) {
2417 std::string Name =
2418 Dynamic ? "__pass_dynamic_object_size" : "__pass_object_size";
2419 mangleArtificialTagType(TagTypeKind::Enum, Name + llvm::utostr(Type),
2420 {"__clang"});
2421
2422 if (FunArgBackReferences.size() < 10) {
2423 size_t Size = FunArgBackReferences.size();
2424 FunArgBackReferences[TypePtr] = Size;
2425 }
2426 } else {
2427 Out << Found->second;
2428 }
2429}
2430
2431void MicrosoftCXXNameMangler::mangleAddressSpaceType(QualType T,
2432 Qualifiers Quals,
2434 // Address space is mangled as an unqualified templated type in the __clang
2435 // namespace. The demangled version of this is:
2436 // In the case of a language specific address space:
2437 // __clang::struct _AS[language_addr_space]<Type>
2438 // where:
2439 // <language_addr_space> ::= <OpenCL-addrspace> | <CUDA-addrspace>
2440 // <OpenCL-addrspace> ::= "CL" [ "global" | "local" | "constant" |
2441 // "private"| "generic" | "device" | "host" ]
2442 // <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ]
2443 // Note that the above were chosen to match the Itanium mangling for this.
2444 //
2445 // In the case of a non-language specific address space:
2446 // __clang::struct _AS<TargetAS, Type>
2447 assert(Quals.hasAddressSpace() && "Not valid without address space");
2448 llvm::SmallString<32> ASMangling;
2449 llvm::raw_svector_ostream Stream(ASMangling);
2450 MicrosoftCXXNameMangler Extra(Context, Stream);
2451 Stream << "?$";
2452
2453 LangAS AS = Quals.getAddressSpace();
2454 if (Context.getASTContext().addressSpaceMapManglingFor(AS)) {
2455 unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
2456 Extra.mangleSourceName("_AS");
2457 Extra.mangleIntegerLiteral(llvm::APSInt::getUnsigned(TargetAS));
2458 } else {
2459 switch (AS) {
2460 default:
2461 llvm_unreachable("Not a language specific address space");
2462 case LangAS::opencl_global:
2463 Extra.mangleSourceName("_ASCLglobal");
2464 break;
2465 case LangAS::opencl_global_device:
2466 Extra.mangleSourceName("_ASCLdevice");
2467 break;
2468 case LangAS::opencl_global_host:
2469 Extra.mangleSourceName("_ASCLhost");
2470 break;
2471 case LangAS::opencl_local:
2472 Extra.mangleSourceName("_ASCLlocal");
2473 break;
2474 case LangAS::opencl_constant:
2475 Extra.mangleSourceName("_ASCLconstant");
2476 break;
2477 case LangAS::opencl_private:
2478 Extra.mangleSourceName("_ASCLprivate");
2479 break;
2480 case LangAS::opencl_generic:
2481 Extra.mangleSourceName("_ASCLgeneric");
2482 break;
2483 case LangAS::cuda_device:
2484 Extra.mangleSourceName("_ASCUdevice");
2485 break;
2486 case LangAS::cuda_constant:
2487 Extra.mangleSourceName("_ASCUconstant");
2488 break;
2489 case LangAS::cuda_shared:
2490 Extra.mangleSourceName("_ASCUshared");
2491 break;
2492 case LangAS::ptr32_sptr:
2493 case LangAS::ptr32_uptr:
2494 case LangAS::ptr64:
2495 llvm_unreachable("don't mangle ptr address spaces with _AS");
2496 }
2497 }
2498
2499 Extra.mangleType(T, Range, QMM_Escape);
2500 mangleQualifiers(Qualifiers(), false);
2501 mangleArtificialTagType(TagTypeKind::Struct, ASMangling, {"__clang"});
2502}
2503
2504void MicrosoftCXXNameMangler::mangleAutoReturnType(QualType T,
2505 QualifierMangleMode QMM) {
2506 assert(getASTContext().getLangOpts().isCompatibleWithMSVC(
2507 LangOptions::MSVC2019) &&
2508 "Cannot mangle MSVC 2017 auto return types!");
2509
2510 if (isa<AutoType>(T)) {
2511 const auto *AT = T->getContainedAutoType();
2512 Qualifiers Quals = T.getLocalQualifiers();
2513
2514 if (QMM == QMM_Result)
2515 Out << '?';
2516 if (QMM != QMM_Drop)
2517 mangleQualifiers(Quals, false);
2518 Out << (AT->isDecltypeAuto() ? "_T" : "_P");
2519 return;
2520 }
2521
2522 T = T.getDesugaredType(getASTContext());
2523 Qualifiers Quals = T.getLocalQualifiers();
2524
2525 switch (QMM) {
2526 case QMM_Drop:
2527 case QMM_Result:
2528 break;
2529 case QMM_Mangle:
2530 mangleQualifiers(Quals, false);
2531 break;
2532 default:
2533 llvm_unreachable("QMM_Escape unexpected");
2534 }
2535
2536 const Type *ty = T.getTypePtr();
2537 switch (ty->getTypeClass()) {
2538 case Type::MemberPointer:
2539 mangleAutoReturnType(cast<MemberPointerType>(ty), Quals);
2540 break;
2541 case Type::Pointer:
2542 mangleAutoReturnType(cast<PointerType>(ty), Quals);
2543 break;
2544 case Type::LValueReference:
2545 mangleAutoReturnType(cast<LValueReferenceType>(ty), Quals);
2546 break;
2547 case Type::RValueReference:
2548 mangleAutoReturnType(cast<RValueReferenceType>(ty), Quals);
2549 break;
2550 default:
2551 llvm_unreachable("Invalid type expected");
2552 }
2553}
2554
2555void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
2556 QualifierMangleMode QMM) {
2557 // Don't use the canonical types. MSVC includes things like 'const' on
2558 // pointer arguments to function pointers that canonicalization strips away.
2559 T = T.getDesugaredType(getASTContext());
2560 Qualifiers Quals = T.getLocalQualifiers();
2561
2562 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
2563 // If there were any Quals, getAsArrayType() pushed them onto the array
2564 // element type.
2565 if (QMM == QMM_Mangle)
2566 Out << 'A';
2567 else if (QMM == QMM_Escape || QMM == QMM_Result)
2568 Out << "$$B";
2569 mangleArrayType(AT);
2570 return;
2571 }
2572
2573 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
2575
2576 switch (QMM) {
2577 case QMM_Drop:
2578 if (Quals.hasObjCLifetime())
2579 Quals = Quals.withoutObjCLifetime();
2580 break;
2581 case QMM_Mangle:
2582 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
2583 Out << '6';
2584 mangleFunctionType(FT);
2585 return;
2586 }
2587 mangleQualifiers(Quals, false);
2588 break;
2589 case QMM_Escape:
2590 if (!IsPointer && Quals) {
2591 Out << "$$C";
2592 mangleQualifiers(Quals, false);
2593 }
2594 break;
2595 case QMM_Result:
2596 // Presence of __unaligned qualifier shouldn't affect mangling here.
2597 Quals.removeUnaligned();
2598 if (Quals.hasObjCLifetime())
2599 Quals = Quals.withoutObjCLifetime();
2600 if ((!IsPointer && Quals) || isa<TagType>(T) || isArtificialTagType(T)) {
2601 Out << '?';
2602 mangleQualifiers(Quals, false);
2603 }
2604 break;
2605 }
2606
2607 const Type *ty = T.getTypePtr();
2608
2609 switch (ty->getTypeClass()) {
2610#define ABSTRACT_TYPE(CLASS, PARENT)
2611#define NON_CANONICAL_TYPE(CLASS, PARENT) \
2612 case Type::CLASS: \
2613 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
2614 return;
2615#define TYPE(CLASS, PARENT) \
2616 case Type::CLASS: \
2617 mangleType(cast<CLASS##Type>(ty), Quals, Range); \
2618 break;
2619#include "clang/AST/TypeNodes.inc"
2620#undef ABSTRACT_TYPE
2621#undef NON_CANONICAL_TYPE
2622#undef TYPE
2623 }
2624}
2625
2626void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, Qualifiers,
2628 // <type> ::= <builtin-type>
2629 // <builtin-type> ::= X # void
2630 // ::= C # signed char
2631 // ::= D # char
2632 // ::= E # unsigned char
2633 // ::= F # short
2634 // ::= G # unsigned short (or wchar_t if it's not a builtin)
2635 // ::= H # int
2636 // ::= I # unsigned int
2637 // ::= J # long
2638 // ::= K # unsigned long
2639 // L # <none>
2640 // ::= M # float
2641 // ::= N # double
2642 // ::= O # long double (__float80 is mangled differently)
2643 // ::= _J # long long, __int64
2644 // ::= _K # unsigned long long, __int64
2645 // ::= _L # __int128
2646 // ::= _M # unsigned __int128
2647 // ::= _N # bool
2648 // _O # <array in parameter>
2649 // ::= _Q # char8_t
2650 // ::= _S # char16_t
2651 // ::= _T # __float80 (Intel)
2652 // ::= _U # char32_t
2653 // ::= _W # wchar_t
2654 // ::= _Z # __float80 (Digital Mars)
2655 switch (T->getKind()) {
2656 case BuiltinType::Void:
2657 Out << 'X';
2658 break;
2659 case BuiltinType::SChar:
2660 Out << 'C';
2661 break;
2662 case BuiltinType::Char_U:
2663 case BuiltinType::Char_S:
2664 Out << 'D';
2665 break;
2666 case BuiltinType::UChar:
2667 Out << 'E';
2668 break;
2669 case BuiltinType::Short:
2670 Out << 'F';
2671 break;
2672 case BuiltinType::UShort:
2673 Out << 'G';
2674 break;
2675 case BuiltinType::Int:
2676 Out << 'H';
2677 break;
2678 case BuiltinType::UInt:
2679 Out << 'I';
2680 break;
2681 case BuiltinType::Long:
2682 Out << 'J';
2683 break;
2684 case BuiltinType::ULong:
2685 Out << 'K';
2686 break;
2687 case BuiltinType::Float:
2688 Out << 'M';
2689 break;
2690 case BuiltinType::Double:
2691 Out << 'N';
2692 break;
2693 // TODO: Determine size and mangle accordingly
2694 case BuiltinType::LongDouble:
2695 Out << 'O';
2696 break;
2697 case BuiltinType::LongLong:
2698 Out << "_J";
2699 break;
2700 case BuiltinType::ULongLong:
2701 Out << "_K";
2702 break;
2703 case BuiltinType::Int128:
2704 Out << "_L";
2705 break;
2706 case BuiltinType::UInt128:
2707 Out << "_M";
2708 break;
2709 case BuiltinType::Bool:
2710 Out << "_N";
2711 break;
2712 case BuiltinType::Char8:
2713 Out << "_Q";
2714 break;
2715 case BuiltinType::Char16:
2716 Out << "_S";
2717 break;
2718 case BuiltinType::Char32:
2719 Out << "_U";
2720 break;
2721 case BuiltinType::WChar_S:
2722 case BuiltinType::WChar_U:
2723 Out << "_W";
2724 break;
2725
2726#define BUILTIN_TYPE(Id, SingletonId)
2727#define PLACEHOLDER_TYPE(Id, SingletonId) \
2728 case BuiltinType::Id:
2729#include "clang/AST/BuiltinTypes.def"
2730 case BuiltinType::Dependent:
2731 llvm_unreachable("placeholder types shouldn't get to name mangling");
2732
2733 case BuiltinType::ObjCId:
2734 mangleArtificialTagType(TagTypeKind::Struct, "objc_object");
2735 break;
2736 case BuiltinType::ObjCClass:
2737 mangleArtificialTagType(TagTypeKind::Struct, "objc_class");
2738 break;
2739 case BuiltinType::ObjCSel:
2740 mangleArtificialTagType(TagTypeKind::Struct, "objc_selector");
2741 break;
2742
2743#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2744 case BuiltinType::Id: \
2745 Out << "PAUocl_" #ImgType "_" #Suffix "@@"; \
2746 break;
2747#include "clang/Basic/OpenCLImageTypes.def"
2748 case BuiltinType::OCLSampler:
2749 Out << "PA";
2750 mangleArtificialTagType(TagTypeKind::Struct, "ocl_sampler");
2751 break;
2752 case BuiltinType::OCLEvent:
2753 Out << "PA";
2754 mangleArtificialTagType(TagTypeKind::Struct, "ocl_event");
2755 break;
2756 case BuiltinType::OCLClkEvent:
2757 Out << "PA";
2758 mangleArtificialTagType(TagTypeKind::Struct, "ocl_clkevent");
2759 break;
2760 case BuiltinType::OCLQueue:
2761 Out << "PA";
2762 mangleArtificialTagType(TagTypeKind::Struct, "ocl_queue");
2763 break;
2764 case BuiltinType::OCLReserveID:
2765 Out << "PA";
2766 mangleArtificialTagType(TagTypeKind::Struct, "ocl_reserveid");
2767 break;
2768#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2769 case BuiltinType::Id: \
2770 mangleArtificialTagType(TagTypeKind::Struct, "ocl_" #ExtType); \
2771 break;
2772#include "clang/Basic/OpenCLExtensionTypes.def"
2773
2774 case BuiltinType::NullPtr:
2775 Out << "$$T";
2776 break;
2777
2778 case BuiltinType::Float16:
2779 mangleArtificialTagType(TagTypeKind::Struct, "_Float16", {"__clang"});
2780 break;
2781
2782 case BuiltinType::Half:
2783 if (!getASTContext().getLangOpts().HLSL)
2784 mangleArtificialTagType(TagTypeKind::Struct, "_Half", {"__clang"});
2785 else if (getASTContext().getLangOpts().NativeHalfType)
2786 Out << "$f16@";
2787 else
2788 Out << "$halff@";
2789 break;
2790
2791 case BuiltinType::BFloat16:
2792 mangleArtificialTagType(TagTypeKind::Struct, "__bf16", {"__clang"});
2793 break;
2794
2795#define WASM_REF_TYPE(InternalName, MangledName, Id, SingletonId, AS) \
2796 case BuiltinType::Id: \
2797 mangleArtificialTagType(TagTypeKind::Struct, MangledName); \
2798 mangleArtificialTagType(TagTypeKind::Struct, MangledName, {"__clang"}); \
2799 break;
2800
2801#include "clang/Basic/WebAssemblyReferenceTypes.def"
2802
2803#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
2804 case BuiltinType::Id: \
2805 mangleArtificialTagType(TagTypeKind::Struct, #Name); \
2806 break;
2807#include "clang/Basic/HLSLIntangibleTypes.def"
2808
2809#define SVE_TYPE(Name, Id, SingletonId) \
2810 case BuiltinType::Id:
2811#include "clang/Basic/AArch64SVEACLETypes.def"
2812#define PPC_VECTOR_TYPE(Name, Id, Size) \
2813 case BuiltinType::Id:
2814#include "clang/Basic/PPCTypes.def"
2815#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
2816#include "clang/Basic/RISCVVTypes.def"
2817#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
2818#include "clang/Basic/AMDGPUTypes.def"
2819 case BuiltinType::ShortAccum:
2820 case BuiltinType::Accum:
2821 case BuiltinType::LongAccum:
2822 case BuiltinType::UShortAccum:
2823 case BuiltinType::UAccum:
2824 case BuiltinType::ULongAccum:
2825 case BuiltinType::ShortFract:
2826 case BuiltinType::Fract:
2827 case BuiltinType::LongFract:
2828 case BuiltinType::UShortFract:
2829 case BuiltinType::UFract:
2830 case BuiltinType::ULongFract:
2831 case BuiltinType::SatShortAccum:
2832 case BuiltinType::SatAccum:
2833 case BuiltinType::SatLongAccum:
2834 case BuiltinType::SatUShortAccum:
2835 case BuiltinType::SatUAccum:
2836 case BuiltinType::SatULongAccum:
2837 case BuiltinType::SatShortFract:
2838 case BuiltinType::SatFract:
2839 case BuiltinType::SatLongFract:
2840 case BuiltinType::SatUShortFract:
2841 case BuiltinType::SatUFract:
2842 case BuiltinType::SatULongFract:
2843 case BuiltinType::Ibm128:
2844 case BuiltinType::Float128: {
2845 Error(Range.getBegin(), "built-in type: ",
2846 T->getName(Context.getASTContext().getPrintingPolicy()))
2847 << Range;
2848 break;
2849 }
2850 }
2851}
2852
2853// <type> ::= <function-type>
2854void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T, Qualifiers,
2855 SourceRange) {
2856 // Structors only appear in decls, so at this point we know it's not a
2857 // structor type.
2858 // FIXME: This may not be lambda-friendly.
2859 if (T->getMethodQuals() || T->getRefQualifier() != RQ_None) {
2860 Out << "$$A8@@";
2861 mangleFunctionType(T, /*D=*/nullptr, /*ForceThisQuals=*/true);
2862 } else {
2863 Out << "$$A6";
2864 mangleFunctionType(T);
2865 }
2866}
2867void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
2869 Out << "$$A6";
2870 mangleFunctionType(T);
2871}
2872
2873void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
2874 const FunctionDecl *D,
2875 bool ForceThisQuals,
2876 bool MangleExceptionSpec) {
2877 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
2878 // <return-type> <argument-list> <throw-spec>
2879 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(T);
2880
2882 if (D) Range = D->getSourceRange();
2883
2884 bool IsInLambda = false;
2885 bool IsStructor = false, HasThisQuals = ForceThisQuals, IsCtorClosure = false;
2886 CallingConv CC = T->getCallConv();
2887 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
2888 if (MD->getParent()->isLambda())
2889 IsInLambda = true;
2891 HasThisQuals = true;
2892 if (isa<CXXDestructorDecl>(MD)) {
2893 IsStructor = true;
2894 } else if (isa<CXXConstructorDecl>(MD)) {
2895 IsStructor = true;
2896 IsCtorClosure = (StructorType == Ctor_CopyingClosure ||
2897 StructorType == Ctor_DefaultClosure) &&
2898 isStructorDecl(MD);
2899 if (IsCtorClosure)
2900 CC = getASTContext().getDefaultCallingConvention(
2901 /*IsVariadic=*/false, /*IsCXXMethod=*/true);
2902 }
2903 }
2904
2905 // If this is a C++ instance method, mangle the CVR qualifiers for the
2906 // this pointer.
2907 if (HasThisQuals) {
2908 Qualifiers Quals = Proto->getMethodQuals();
2909 manglePointerExtQualifiers(Quals, /*PointeeType=*/QualType());
2910 mangleRefQualifier(Proto->getRefQualifier());
2911 mangleQualifiers(Quals, /*IsMember=*/false);
2912 }
2913
2914 mangleCallingConvention(CC, Range);
2915
2916 // <return-type> ::= <type>
2917 // ::= @ # structors (they have no declared return type)
2918 if (IsStructor) {
2919 if (isa<CXXDestructorDecl>(D) && isStructorDecl(D)) {
2920 // The scalar deleting destructor takes an extra int argument which is not
2921 // reflected in the AST.
2922 if (StructorType == Dtor_Deleting) {
2923 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
2924 return;
2925 }
2926 // The vbase destructor returns void which is not reflected in the AST.
2927 if (StructorType == Dtor_Complete) {
2928 Out << "XXZ";
2929 return;
2930 }
2931 }
2932 if (IsCtorClosure) {
2933 // Default constructor closure and copy constructor closure both return
2934 // void.
2935 Out << 'X';
2936
2937 if (StructorType == Ctor_DefaultClosure) {
2938 // Default constructor closure always has no arguments.
2939 Out << 'X';
2940 } else if (StructorType == Ctor_CopyingClosure) {
2941 // Copy constructor closure always takes an unqualified reference.
2942 mangleFunctionArgumentType(getASTContext().getLValueReferenceType(
2943 Proto->getParamType(0)
2945 ->getPointeeType(),
2946 /*SpelledAsLValue=*/true),
2947 Range);
2948 Out << '@';
2949 } else {
2950 llvm_unreachable("unexpected constructor closure!");
2951 }
2952 Out << 'Z';
2953 return;
2954 }
2955 Out << '@';
2956 } else if (IsInLambda && isa_and_nonnull<CXXConversionDecl>(D)) {
2957 // The only lambda conversion operators are to function pointers, which
2958 // can differ by their calling convention and are typically deduced. So
2959 // we make sure that this type gets mangled properly.
2960 mangleType(T->getReturnType(), Range, QMM_Result);
2961 } else {
2962 QualType ResultType = T->getReturnType();
2963 if (IsInLambda && isa<CXXConversionDecl>(D)) {
2964 // The only lambda conversion operators are to function pointers, which
2965 // can differ by their calling convention and are typically deduced. So
2966 // we make sure that this type gets mangled properly.
2967 mangleType(ResultType, Range, QMM_Result);
2968 } else if (IsInLambda) {
2969 if (const auto *AT = ResultType->getContainedAutoType()) {
2970 assert(AT->getKeyword() != AutoTypeKeyword::GNUAutoType &&
2971 "shouldn't need to mangle __auto_type!");
2972 Out << '?';
2973 mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false);
2974 Out << '?';
2975 mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>");
2976 Out << '@';
2977 } else {
2978 Out << '@';
2979 }
2980 } else if (const auto *AT = ResultType->getContainedAutoType()) {
2981 assert(AT->getKeyword() != AutoTypeKeyword::GNUAutoType &&
2982 "shouldn't need to mangle __auto_type!");
2983
2984 // If we have any pointer types with the clang address space extension
2985 // then defer to the custom clang mangling to keep backwards
2986 // compatibility. See `mangleType(const PointerType *T, Qualifiers Quals,
2987 // SourceRange Range)` for details.
2988 auto UseClangMangling = [](QualType ResultType) {
2989 QualType T = ResultType;
2990 while (isa<PointerType>(T.getTypePtr())) {
2991 T = T->getPointeeType();
2992 if (T.getQualifiers().hasAddressSpace())
2993 return true;
2994 }
2995 return false;
2996 };
2997
2998 if (getASTContext().getLangOpts().isCompatibleWithMSVC(
2999 LangOptions::MSVC2019) &&
3000 !UseClangMangling(ResultType)) {
3001 if (D && !D->getPrimaryTemplate()) {
3002 Out << '@';
3003 } else {
3004 if (D && D->getPrimaryTemplate()) {
3005 const FunctionProtoType *FPT = D->getPrimaryTemplate()
3006 ->getTemplatedDecl()
3007 ->getFirstDecl()
3008 ->getType()
3009 ->castAs<FunctionProtoType>();
3010 ResultType = FPT->getReturnType();
3011 }
3012 mangleAutoReturnType(ResultType, QMM_Result);
3013 }
3014 } else {
3015 Out << '?';
3016 mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false);
3017 Out << '?';
3018 mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>");
3019 Out << '@';
3020 }
3021 } else {
3022 if (ResultType->isVoidType())
3023 ResultType = ResultType.getUnqualifiedType();
3024 mangleType(ResultType, Range, QMM_Result);
3025 }
3026 }
3027
3028 // <argument-list> ::= X # void
3029 // ::= <type>+ @
3030 // ::= <type>* Z # varargs
3031 if (!Proto) {
3032 // Function types without prototypes can arise when mangling a function type
3033 // within an overloadable function in C. We mangle these as the absence of
3034 // any parameter types (not even an empty parameter list).
3035 Out << '@';
3036 } else if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
3037 Out << 'X';
3038 } else {
3039 // Happens for function pointer type arguments for example.
3040 for (unsigned I = 0, E = Proto->getNumParams(); I != E; ++I) {
3041 // Explicit object parameters are prefixed by "_V".
3042 if (I == 0 && D && D->getParamDecl(I)->isExplicitObjectParameter())
3043 Out << "_V";
3044
3045 mangleFunctionArgumentType(Proto->getParamType(I), Range);
3046 // Mangle each pass_object_size parameter as if it's a parameter of enum
3047 // type passed directly after the parameter with the pass_object_size
3048 // attribute. The aforementioned enum's name is __pass_object_size, and we
3049 // pretend it resides in a top-level namespace called __clang.
3050 //
3051 // FIXME: Is there a defined extension notation for the MS ABI, or is it
3052 // necessary to just cross our fingers and hope this type+namespace
3053 // combination doesn't conflict with anything?
3054 if (D)
3055 if (const auto *P = D->getParamDecl(I)->getAttr<PassObjectSizeAttr>())
3056 manglePassObjectSizeArg(P);
3057 }
3058 // <builtin-type> ::= Z # ellipsis
3059 if (Proto->isVariadic())
3060 Out << 'Z';
3061 else
3062 Out << '@';
3063 }
3064
3065 if (MangleExceptionSpec && getASTContext().getLangOpts().CPlusPlus17 &&
3066 getASTContext().getLangOpts().isCompatibleWithMSVC(
3067 LangOptions::MSVC2017_5))
3068 mangleThrowSpecification(Proto);
3069 else
3070 Out << 'Z';
3071}
3072
3073void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
3074 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
3075 // # pointer. in 64-bit mode *all*
3076 // # 'this' pointers are 64-bit.
3077 // ::= <global-function>
3078 // <member-function> ::= A # private: near
3079 // ::= B # private: far
3080 // ::= C # private: static near
3081 // ::= D # private: static far
3082 // ::= E # private: virtual near
3083 // ::= F # private: virtual far
3084 // ::= I # protected: near
3085 // ::= J # protected: far
3086 // ::= K # protected: static near
3087 // ::= L # protected: static far
3088 // ::= M # protected: virtual near
3089 // ::= N # protected: virtual far
3090 // ::= Q # public: near
3091 // ::= R # public: far
3092 // ::= S # public: static near
3093 // ::= T # public: static far
3094 // ::= U # public: virtual near
3095 // ::= V # public: virtual far
3096 // <global-function> ::= Y # global near
3097 // ::= Z # global far
3098 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
3099 bool IsVirtual = MD->isVirtual();
3100 // When mangling vbase destructor variants, ignore whether or not the
3101 // underlying destructor was defined to be virtual.
3102 if (isa<CXXDestructorDecl>(MD) && isStructorDecl(MD) &&
3103 StructorType == Dtor_Complete) {
3104 IsVirtual = false;
3105 }
3106 switch (MD->getAccess()) {
3107 case AS_none:
3108 llvm_unreachable("Unsupported access specifier");
3109 case AS_private:
3111 Out << 'C';
3112 else if (IsVirtual)
3113 Out << 'E';
3114 else
3115 Out << 'A';
3116 break;
3117 case AS_protected:
3119 Out << 'K';
3120 else if (IsVirtual)
3121 Out << 'M';
3122 else
3123 Out << 'I';
3124 break;
3125 case AS_public:
3127 Out << 'S';
3128 else if (IsVirtual)
3129 Out << 'U';
3130 else
3131 Out << 'Q';
3132 }
3133 } else {
3134 Out << 'Y';
3135 }
3136}
3137void MicrosoftCXXNameMangler::mangleCallingConvention(CallingConv CC,
3139 // <calling-convention> ::= A # __cdecl
3140 // ::= B # __export __cdecl
3141 // ::= C # __pascal
3142 // ::= D # __export __pascal
3143 // ::= E # __thiscall
3144 // ::= F # __export __thiscall
3145 // ::= G # __stdcall
3146 // ::= H # __export __stdcall
3147 // ::= I # __fastcall
3148 // ::= J # __export __fastcall
3149 // ::= Q # __vectorcall
3150 // ::= S # __attribute__((__swiftcall__)) // Clang-only
3151 // ::= W # __attribute__((__swiftasynccall__))
3152 // ::= U # __attribute__((__preserve_most__))
3153 // ::= V # __attribute__((__preserve_none__)) //
3154 // Clang-only
3155 // // Clang-only
3156 // ::= w # __regcall
3157 // ::= x # __regcall4
3158 // The 'export' calling conventions are from a bygone era
3159 // (*cough*Win16*cough*) when functions were declared for export with
3160 // that keyword. (It didn't actually export them, it just made them so
3161 // that they could be in a DLL and somebody from another module could call
3162 // them.)
3163
3164 switch (CC) {
3165 default:
3166 break;
3167 case CC_Win64:
3168 case CC_X86_64SysV:
3169 case CC_C:
3170 Out << 'A';
3171 return;
3172 case CC_X86Pascal:
3173 Out << 'C';
3174 return;
3175 case CC_X86ThisCall:
3176 Out << 'E';
3177 return;
3178 case CC_X86StdCall:
3179 Out << 'G';
3180 return;
3181 case CC_X86FastCall:
3182 Out << 'I';
3183 return;
3184 case CC_X86VectorCall:
3185 Out << 'Q';
3186 return;
3187 case CC_Swift:
3188 Out << 'S';
3189 return;
3190 case CC_SwiftAsync:
3191 Out << 'W';
3192 return;
3193 case CC_PreserveMost:
3194 Out << 'U';
3195 return;
3196 case CC_PreserveNone:
3197 Out << 'V';
3198 return;
3199 case CC_X86RegCall:
3200 if (getASTContext().getLangOpts().RegCall4)
3201 Out << "x";
3202 else
3203 Out << "w";
3204 return;
3205 }
3206
3207 Error(Range.getBegin(), "calling convention") << Range;
3208}
3209void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
3211 mangleCallingConvention(T->getCallConv(), Range);
3212}
3213
3214void MicrosoftCXXNameMangler::mangleThrowSpecification(
3215 const FunctionProtoType *FT) {
3216 // <throw-spec> ::= Z # (default)
3217 // ::= _E # noexcept
3218 if (FT->canThrow())
3219 Out << 'Z';
3220 else
3221 Out << "_E";
3222}
3223
3224void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
3226 // Probably should be mangled as a template instantiation; need to see what
3227 // VC does first.
3228 Error(Range.getBegin(), "unresolved dependent type") << Range;
3229}
3230
3231// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
3232// <union-type> ::= T <name>
3233// <struct-type> ::= U <name>
3234// <class-type> ::= V <name>
3235// <enum-type> ::= W4 <name>
3236void MicrosoftCXXNameMangler::mangleTagTypeKind(TagTypeKind TTK) {
3237 switch (TTK) {
3238 case TagTypeKind::Union:
3239 Out << 'T';
3240 break;
3241 case TagTypeKind::Struct:
3242 case TagTypeKind::Interface:
3243 Out << 'U';
3244 break;
3245 case TagTypeKind::Class:
3246 Out << 'V';
3247 break;
3248 case TagTypeKind::Enum:
3249 Out << "W4";
3250 break;
3251 }
3252}
3253void MicrosoftCXXNameMangler::mangleType(const EnumType *T, Qualifiers,
3254 SourceRange) {
3255 mangleType(cast<TagType>(T)->getDecl());
3256}
3257void MicrosoftCXXNameMangler::mangleType(const RecordType *T, Qualifiers,
3258 SourceRange) {
3259 mangleType(cast<TagType>(T)->getDecl());
3260}
3261void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
3262 mangleTagTypeKind(TD->getTagKind());
3263 mangleName(TD);
3264}
3265
3266// If you add a call to this, consider updating isArtificialTagType() too.
3267void MicrosoftCXXNameMangler::mangleArtificialTagType(
3268 TagTypeKind TK, StringRef UnqualifiedName,
3269 ArrayRef<StringRef> NestedNames) {
3270 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
3271 mangleTagTypeKind(TK);
3272
3273 // Always start with the unqualified name.
3274 mangleSourceName(UnqualifiedName);
3275
3276 for (StringRef N : llvm::reverse(NestedNames))
3277 mangleSourceName(N);
3278
3279 // Terminate the whole name with an '@'.
3280 Out << '@';
3281}
3282
3283// <type> ::= <array-type>
3284// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
3285// [Y <dimension-count> <dimension>+]
3286// <element-type> # as global, E is never required
3287// It's supposed to be the other way around, but for some strange reason, it
3288// isn't. Today this behavior is retained for the sole purpose of backwards
3289// compatibility.
3290void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
3291 // This isn't a recursive mangling, so now we have to do it all in this
3292 // one call.
3293 manglePointerCVQualifiers(T->getElementType().getQualifiers());
3294 mangleType(T->getElementType(), SourceRange());
3295}
3296void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T, Qualifiers,
3297 SourceRange) {
3298 llvm_unreachable("Should have been special cased");
3299}
3300void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T, Qualifiers,
3301 SourceRange) {
3302 llvm_unreachable("Should have been special cased");
3303}
3304void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
3306 llvm_unreachable("Should have been special cased");
3307}
3308void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
3310 llvm_unreachable("Should have been special cased");
3311}
3312void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
3313 QualType ElementTy(T, 0);
3314 SmallVector<llvm::APInt, 3> Dimensions;
3315 for (;;) {
3316 if (ElementTy->isConstantArrayType()) {
3317 const ConstantArrayType *CAT =
3318 getASTContext().getAsConstantArrayType(ElementTy);
3319 Dimensions.push_back(CAT->getSize());
3320 ElementTy = CAT->getElementType();
3321 } else if (ElementTy->isIncompleteArrayType()) {
3322 const IncompleteArrayType *IAT =
3323 getASTContext().getAsIncompleteArrayType(ElementTy);
3324 Dimensions.push_back(llvm::APInt(32, 0));
3325 ElementTy = IAT->getElementType();
3326 } else if (ElementTy->isVariableArrayType()) {
3327 const VariableArrayType *VAT =
3328 getASTContext().getAsVariableArrayType(ElementTy);
3329 Dimensions.push_back(llvm::APInt(32, 0));
3330 ElementTy = VAT->getElementType();
3331 } else if (ElementTy->isDependentSizedArrayType()) {
3332 // The dependent expression has to be folded into a constant (TODO).
3333 const DependentSizedArrayType *DSAT =
3334 getASTContext().getAsDependentSizedArrayType(ElementTy);
3335 Error(DSAT->getSizeExpr()->getExprLoc(), "dependent-length")
3336 << DSAT->getBracketsRange();
3337 return;
3338 } else {
3339 break;
3340 }
3341 }
3342 Out << 'Y';
3343 // <dimension-count> ::= <number> # number of extra dimensions
3344 mangleNumber(Dimensions.size());
3345 for (const llvm::APInt &Dimension : Dimensions)
3346 mangleNumber(Dimension.getLimitedValue());
3347 mangleType(ElementTy, SourceRange(), QMM_Escape);
3348}
3349
3350void MicrosoftCXXNameMangler::mangleType(const ArrayParameterType *T,
3352 mangleArrayType(cast<ConstantArrayType>(T));
3353}
3354
3355// <type> ::= <pointer-to-member-type>
3356// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
3357// <class name> <type>
3358void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
3359 Qualifiers Quals, SourceRange Range) {
3360 QualType PointeeType = T->getPointeeType();
3361 manglePointerCVQualifiers(Quals);
3362 manglePointerExtQualifiers(Quals, PointeeType);
3363 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
3364 Out << '8';
3365 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
3366 mangleFunctionType(FPT, nullptr, true);
3367 } else {
3368 mangleQualifiers(PointeeType.getQualifiers(), true);
3369 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
3370 mangleType(PointeeType, Range, QMM_Drop);
3371 }
3372}
3373
3374void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
3376 Out << '?';
3377
3379 Name += "<TTPT_";
3380 Name += llvm::utostr(T->getDepth());
3381 Name += "_";
3382 Name += llvm::utostr(T->getIndex());
3383 Name += ">";
3384 mangleSourceName(Name);
3385}
3386
3387void MicrosoftCXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T,
3389 Error(Range.getBegin(), "substituted parameter pack") << Range;
3390}
3391
3392// <type> ::= <pointer-type>
3393// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
3394// # the E is required for 64-bit non-static pointers
3395void MicrosoftCXXNameMangler::mangleType(const PointerType *T, Qualifiers Quals,
3397 QualType PointeeType = T->getPointeeType();
3398 manglePointerCVQualifiers(Quals);
3399 manglePointerExtQualifiers(Quals, PointeeType);
3400
3401 // For pointer size address spaces, go down the same type mangling path as
3402 // non address space types.
3403 LangAS AddrSpace = PointeeType.getQualifiers().getAddressSpace();
3404 if (isPtrSizeAddressSpace(AddrSpace) || AddrSpace == LangAS::Default)
3405 mangleType(PointeeType, Range);
3406 else
3407 mangleAddressSpaceType(PointeeType, PointeeType.getQualifiers(), Range);
3408}
3409
3410void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
3411 Qualifiers Quals, SourceRange Range) {
3412 QualType PointeeType = T->getPointeeType();
3413 switch (Quals.getObjCLifetime()) {
3416 break;
3420 return mangleObjCLifetime(PointeeType, Quals, Range);
3421 }
3422 manglePointerCVQualifiers(Quals);
3423 manglePointerExtQualifiers(Quals, PointeeType);
3424 mangleType(PointeeType, Range);
3425}
3426
3427// <type> ::= <reference-type>
3428// <reference-type> ::= A E? <cvr-qualifiers> <type>
3429// # the E is required for 64-bit non-static lvalue references
3430void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
3431 Qualifiers Quals, SourceRange Range) {
3432 QualType PointeeType = T->getPointeeType();
3433 assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!");
3434 Out << 'A';
3435 manglePointerExtQualifiers(Quals, PointeeType);
3436 mangleType(PointeeType, Range);
3437}
3438
3439// <type> ::= <r-value-reference-type>
3440// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
3441// # the E is required for 64-bit non-static rvalue references
3442void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
3443 Qualifiers Quals, SourceRange Range) {
3444 QualType PointeeType = T->getPointeeType();
3445 assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!");
3446 Out << "$$Q";
3447 manglePointerExtQualifiers(Quals, PointeeType);
3448 mangleType(PointeeType, Range);
3449}
3450
3451void MicrosoftCXXNameMangler::mangleType(const ComplexType *T, Qualifiers,
3453 QualType ElementType = T->getElementType();
3454
3455 llvm::SmallString<64> TemplateMangling;
3456 llvm::raw_svector_ostream Stream(TemplateMangling);
3457 MicrosoftCXXNameMangler Extra(Context, Stream);
3458 Stream << "?$";
3459 Extra.mangleSourceName("_Complex");
3460 Extra.mangleType(ElementType, Range, QMM_Escape);
3461
3462 mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__clang"});
3463}
3464
3465// Returns true for types that mangleArtificialTagType() gets called for with
3466// TagTypeKind Union, Struct, Class and where compatibility with MSVC's
3467// mangling matters.
3468// (It doesn't matter for Objective-C types and the like that cl.exe doesn't
3469// support.)
3470bool MicrosoftCXXNameMangler::isArtificialTagType(QualType T) const {
3471 const Type *ty = T.getTypePtr();
3472 switch (ty->getTypeClass()) {
3473 default:
3474 return false;
3475
3476 case Type::Vector: {
3477 // For ABI compatibility only __m64, __m128(id), and __m256(id) matter,
3478 // but since mangleType(VectorType*) always calls mangleArtificialTagType()
3479 // just always return true (the other vector types are clang-only).
3480 return true;
3481 }
3482 }
3483}
3484
3485void MicrosoftCXXNameMangler::mangleType(const VectorType *T, Qualifiers Quals,
3487 QualType EltTy = T->getElementType();
3488 const BuiltinType *ET = EltTy->getAs<BuiltinType>();
3489 const BitIntType *BitIntTy = EltTy->getAs<BitIntType>();
3490 assert((ET || BitIntTy) &&
3491 "vectors with non-builtin/_BitInt elements are unsupported");
3492 uint64_t Width = getASTContext().getTypeSize(T);
3493 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
3494 // doesn't match the Intel types uses a custom mangling below.
3495 size_t OutSizeBefore = Out.tell();
3496 if (!isa<ExtVectorType>(T)) {
3497 if (getASTContext().getTargetInfo().getTriple().isX86() && ET) {
3498 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
3499 mangleArtificialTagType(TagTypeKind::Union, "__m64");
3500 } else if (Width >= 128) {
3501 if (ET->getKind() == BuiltinType::Float)
3502 mangleArtificialTagType(TagTypeKind::Union,
3503 "__m" + llvm::utostr(Width));
3504 else if (ET->getKind() == BuiltinType::LongLong)
3505 mangleArtificialTagType(TagTypeKind::Union,
3506 "__m" + llvm::utostr(Width) + 'i');
3507 else if (ET->getKind() == BuiltinType::Double)
3508 mangleArtificialTagType(TagTypeKind::Struct,
3509 "__m" + llvm::utostr(Width) + 'd');
3510 }
3511 }
3512 }
3513
3514 bool IsBuiltin = Out.tell() != OutSizeBefore;
3515 if (!IsBuiltin) {
3516 // The MS ABI doesn't have a special mangling for vector types, so we define
3517 // our own mangling to handle uses of __vector_size__ on user-specified
3518 // types, and for extensions like __v4sf.
3519
3520 llvm::SmallString<64> TemplateMangling;
3521 llvm::raw_svector_ostream Stream(TemplateMangling);
3522 MicrosoftCXXNameMangler Extra(Context, Stream);
3523 Stream << "?$";
3524 Extra.mangleSourceName("__vector");
3525 Extra.mangleType(QualType(ET ? static_cast<const Type *>(ET) : BitIntTy, 0),
3526 Range, QMM_Escape);
3527 Extra.mangleIntegerLiteral(llvm::APSInt::getUnsigned(T->getNumElements()));
3528
3529 mangleArtificialTagType(TagTypeKind::Union, TemplateMangling, {"__clang"});
3530 }
3531}
3532
3533void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
3534 Qualifiers Quals, SourceRange Range) {
3535 mangleType(static_cast<const VectorType *>(T), Quals, Range);
3536}
3537
3538void MicrosoftCXXNameMangler::mangleType(const DependentVectorType *T,
3540 Error(Range.getBegin(), "dependent-sized vector type") << Range;
3541}
3542
3543void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
3545 Error(Range.getBegin(), "dependent-sized extended vector type") << Range;
3546}
3547
3548void MicrosoftCXXNameMangler::mangleType(const ConstantMatrixType *T,
3549 Qualifiers quals, SourceRange Range) {
3550 Error(Range.getBegin(), "matrix type") << Range;
3551}
3552
3553void MicrosoftCXXNameMangler::mangleType(const DependentSizedMatrixType *T,
3554 Qualifiers quals, SourceRange Range) {
3555 Error(Range.getBegin(), "dependent-sized matrix type") << Range;
3556}
3557
3558void MicrosoftCXXNameMangler::mangleType(const DependentAddressSpaceType *T,
3560 Error(Range.getBegin(), "dependent address space type") << Range;
3561}
3562
3563void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, Qualifiers,
3564 SourceRange) {
3565 // ObjC interfaces have structs underlying them.
3566 mangleTagTypeKind(TagTypeKind::Struct);
3567 mangleName(T->getDecl());
3568}
3569
3570void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
3571 Qualifiers Quals, SourceRange Range) {
3572 if (T->isKindOfType())
3573 return mangleObjCKindOfType(T, Quals, Range);
3574
3575 if (T->qual_empty() && !T->isSpecialized())
3576 return mangleType(T->getBaseType(), Range, QMM_Drop);
3577
3578 ArgBackRefMap OuterFunArgsContext;
3579 ArgBackRefMap OuterTemplateArgsContext;
3580 BackRefVec OuterTemplateContext;
3581
3582 FunArgBackReferences.swap(OuterFunArgsContext);
3583 TemplateArgBackReferences.swap(OuterTemplateArgsContext);
3584 NameBackReferences.swap(OuterTemplateContext);
3585
3586 mangleTagTypeKind(TagTypeKind::Struct);
3587
3588 Out << "?$";
3589 if (T->isObjCId())
3590 mangleSourceName("objc_object");
3591 else if (T->isObjCClass())
3592 mangleSourceName("objc_class");
3593 else
3594 mangleSourceName(T->getInterface()->getName());
3595
3596 for (const auto &Q : T->quals())
3597 mangleObjCProtocol(Q);
3598
3599 if (T->isSpecialized())
3600 for (const auto &TA : T->getTypeArgs())
3601 mangleType(TA, Range, QMM_Drop);
3602
3603 Out << '@';
3604
3605 Out << '@';
3606
3607 FunArgBackReferences.swap(OuterFunArgsContext);
3608 TemplateArgBackReferences.swap(OuterTemplateArgsContext);
3609 NameBackReferences.swap(OuterTemplateContext);
3610}
3611
3612void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
3613 Qualifiers Quals, SourceRange Range) {
3614 QualType PointeeType = T->getPointeeType();
3615 manglePointerCVQualifiers(Quals);
3616 manglePointerExtQualifiers(Quals, PointeeType);
3617
3618 Out << "_E";
3619
3620 mangleFunctionType(PointeeType->castAs<FunctionProtoType>());
3621}
3622
3623void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
3625 llvm_unreachable("Cannot mangle injected class name type.");
3626}
3627
3628void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
3630 Error(Range.getBegin(), "template specialization type") << Range;
3631}
3632
3633void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, Qualifiers,
3635 Error(Range.getBegin(), "dependent name type") << Range;
3636}
3637
3638void MicrosoftCXXNameMangler::mangleType(
3641 Error(Range.getBegin(), "dependent template specialization type") << Range;
3642}
3643
3644void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, Qualifiers,
3646 Error(Range.getBegin(), "pack expansion") << Range;
3647}
3648
3649void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T,
3650 Qualifiers Quals, SourceRange Range) {
3651 manglePointerCVQualifiers(Quals);
3652 mangleType(T->getSelectedType(), Range);
3653}
3654
3655void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, Qualifiers,
3657 Error(Range.getBegin(), "typeof(type)") << Range;
3658}
3659
3660void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, Qualifiers,
3662 Error(Range.getBegin(), "typeof(expression)") << Range;
3663}
3664
3665void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, Qualifiers,
3667 Error(Range.getBegin(), "decltype()") << Range;
3668}
3669
3670void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
3672 Error(Range.getBegin(), "unary transform type") << Range;
3673}
3674
3675void MicrosoftCXXNameMangler::mangleType(const AutoType *T, Qualifiers,
3677 assert(T->getDeducedType().isNull() && "expecting a dependent type!");
3678
3679 Error(Range.getBegin(), "'auto' type") << Range;
3680}
3681
3682void MicrosoftCXXNameMangler::mangleType(
3684 assert(T->getDeducedType().isNull() && "expecting a dependent type!");
3685
3686 Error(Range.getBegin(), "deduced class template specialization type")
3687 << Range;
3688}
3689
3690void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, Qualifiers,
3692 QualType ValueType = T->getValueType();
3693
3694 llvm::SmallString<64> TemplateMangling;
3695 llvm::raw_svector_ostream Stream(TemplateMangling);
3696 MicrosoftCXXNameMangler Extra(Context, Stream);
3697 Stream << "?$";
3698 Extra.mangleSourceName("_Atomic");
3699 Extra.mangleType(ValueType, Range, QMM_Escape);
3700
3701 mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__clang"});
3702}
3703
3704void MicrosoftCXXNameMangler::mangleType(const PipeType *T, Qualifiers,
3706 QualType ElementType = T->getElementType();
3707
3708 llvm::SmallString<64> TemplateMangling;
3709 llvm::raw_svector_ostream Stream(TemplateMangling);
3710 MicrosoftCXXNameMangler Extra(Context, Stream);
3711 Stream << "?$";
3712 Extra.mangleSourceName("ocl_pipe");
3713 Extra.mangleType(ElementType, Range, QMM_Escape);
3714 Extra.mangleIntegerLiteral(llvm::APSInt::get(T->isReadOnly()));
3715
3716 mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__clang"});
3717}
3718
3719void MicrosoftMangleContextImpl::mangleCXXName(GlobalDecl GD,
3720 raw_ostream &Out) {
3721 const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
3723 getASTContext().getSourceManager(),
3724 "Mangling declaration");
3725
3726 msvc_hashing_ostream MHO(Out);
3727
3728 if (auto *CD = dyn_cast<CXXConstructorDecl>(D)) {
3729 auto Type = GD.getCtorType();
3730 MicrosoftCXXNameMangler mangler(*this, MHO, CD, Type);
3731 return mangler.mangle(GD);
3732 }
3733
3734 if (auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
3735 auto Type = GD.getDtorType();
3736 MicrosoftCXXNameMangler mangler(*this, MHO, DD, Type);
3737 return mangler.mangle(GD);
3738 }
3739
3740 MicrosoftCXXNameMangler Mangler(*this, MHO);
3741 return Mangler.mangle(GD);
3742}
3743
3744void MicrosoftCXXNameMangler::mangleType(const BitIntType *T, Qualifiers,
3746 llvm::SmallString<64> TemplateMangling;
3747 llvm::raw_svector_ostream Stream(TemplateMangling);
3748 MicrosoftCXXNameMangler Extra(Context, Stream);
3749 Stream << "?$";
3750 if (T->isUnsigned())
3751 Extra.mangleSourceName("_UBitInt");
3752 else
3753 Extra.mangleSourceName("_BitInt");
3754 Extra.mangleIntegerLiteral(llvm::APSInt::getUnsigned(T->getNumBits()));
3755
3756 mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__clang"});
3757}
3758
3759void MicrosoftCXXNameMangler::mangleType(const DependentBitIntType *T,
3761 Error(Range.getBegin(), "DependentBitInt type") << Range;
3762}
3763
3764void MicrosoftCXXNameMangler::mangleType(const HLSLAttributedResourceType *T,
3766 llvm_unreachable("HLSL uses Itanium name mangling");
3767}
3768
3769// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
3770// <virtual-adjustment>
3771// <no-adjustment> ::= A # private near
3772// ::= B # private far
3773// ::= I # protected near
3774// ::= J # protected far
3775// ::= Q # public near
3776// ::= R # public far
3777// <static-adjustment> ::= G <static-offset> # private near
3778// ::= H <static-offset> # private far
3779// ::= O <static-offset> # protected near
3780// ::= P <static-offset> # protected far
3781// ::= W <static-offset> # public near
3782// ::= X <static-offset> # public far
3783// <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near
3784// ::= $1 <virtual-shift> <static-offset> # private far
3785// ::= $2 <virtual-shift> <static-offset> # protected near
3786// ::= $3 <virtual-shift> <static-offset> # protected far
3787// ::= $4 <virtual-shift> <static-offset> # public near
3788// ::= $5 <virtual-shift> <static-offset> # public far
3789// <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift>
3790// <vtordisp-shift> ::= <offset-to-vtordisp>
3791// <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset>
3792// <offset-to-vtordisp>
3794 const ThisAdjustment &Adjustment,
3795 MicrosoftCXXNameMangler &Mangler,
3796 raw_ostream &Out) {
3797 if (!Adjustment.Virtual.isEmpty()) {
3798 Out << '$';
3799 char AccessSpec;
3800 switch (AS) {
3801 case AS_none:
3802 llvm_unreachable("Unsupported access specifier");
3803 case AS_private:
3804 AccessSpec = '0';
3805 break;
3806 case AS_protected:
3807 AccessSpec = '2';
3808 break;
3809 case AS_public:
3810 AccessSpec = '4';
3811 }
3812 if (Adjustment.Virtual.Microsoft.VBPtrOffset) {
3813 Out << 'R' << AccessSpec;
3814 Mangler.mangleNumber(
3815 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset));
3816 Mangler.mangleNumber(
3817 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset));
3818 Mangler.mangleNumber(
3819 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
3820 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual));
3821 } else {
3822 Out << AccessSpec;
3823 Mangler.mangleNumber(
3824 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
3825 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
3826 }
3827 } else if (Adjustment.NonVirtual != 0) {
3828 switch (AS) {
3829 case AS_none:
3830 llvm_unreachable("Unsupported access specifier");
3831 case AS_private:
3832 Out << 'G';
3833 break;
3834 case AS_protected:
3835 Out << 'O';
3836 break;
3837 case AS_public:
3838 Out << 'W';
3839 }
3840 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
3841 } else {
3842 switch (AS) {
3843 case AS_none:
3844 llvm_unreachable("Unsupported access specifier");
3845 case AS_private:
3846 Out << 'A';
3847 break;
3848 case AS_protected:
3849 Out << 'I';
3850 break;
3851 case AS_public:
3852 Out << 'Q';
3853 }
3854 }
3855}
3856
3857void MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(
3858 const CXXMethodDecl *MD, const MethodVFTableLocation &ML,
3859 raw_ostream &Out) {
3860 msvc_hashing_ostream MHO(Out);
3861 MicrosoftCXXNameMangler Mangler(*this, MHO);
3862 Mangler.getStream() << '?';
3863 Mangler.mangleVirtualMemPtrThunk(MD, ML);
3864}
3865
3866void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
3867 const ThunkInfo &Thunk,
3868 bool /*ElideOverrideInfo*/,
3869 raw_ostream &Out) {
3870 msvc_hashing_ostream MHO(Out);
3871 MicrosoftCXXNameMangler Mangler(*this, MHO);
3872 Mangler.getStream() << '?';
3873 Mangler.mangleName(MD);
3874
3875 // Usually the thunk uses the access specifier of the new method, but if this
3876 // is a covariant return thunk, then MSVC always uses the public access
3877 // specifier, and we do the same.
3878 AccessSpecifier AS = Thunk.Return.isEmpty() ? MD->getAccess() : AS_public;
3879 mangleThunkThisAdjustment(AS, Thunk.This, Mangler, MHO);
3880
3881 if (!Thunk.Return.isEmpty())
3882 assert(Thunk.Method != nullptr &&
3883 "Thunk info should hold the overridee decl");
3884
3885 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD;
3886 Mangler.mangleFunctionType(
3887 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD);
3888}
3889
3890void MicrosoftMangleContextImpl::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3892 const ThunkInfo &Thunk,
3893 bool /*ElideOverrideInfo*/,
3894 raw_ostream &Out) {
3895 // FIXME: Actually, the dtor thunk should be emitted for vector deleting
3896 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor
3897 // mangling manually until we support both deleting dtor types.
3898 assert(Type == Dtor_Deleting);
3899 msvc_hashing_ostream MHO(Out);
3900 MicrosoftCXXNameMangler Mangler(*this, MHO, DD, Type);
3901 Mangler.getStream() << "??_E";
3902 Mangler.mangleName(DD->getParent());
3903 auto &Adjustment = Thunk.This;
3904 mangleThunkThisAdjustment(DD->getAccess(), Adjustment, Mangler, MHO);
3905 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD);
3906}
3907
3908void MicrosoftMangleContextImpl::mangleCXXVFTable(
3909 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
3910 raw_ostream &Out) {
3911 // <mangled-name> ::= ?_7 <class-name> <storage-class>
3912 // <cvr-qualifiers> [<name>] @
3913 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
3914 // is always '6' for vftables.
3915 msvc_hashing_ostream MHO(Out);
3916 MicrosoftCXXNameMangler Mangler(*this, MHO);
3917 if (Derived->hasAttr<DLLImportAttr>())
3918 Mangler.getStream() << "??_S";
3919 else
3920 Mangler.getStream() << "??_7";
3921 Mangler.mangleName(Derived);
3922 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
3923 for (const CXXRecordDecl *RD : BasePath)
3924 Mangler.mangleName(RD);
3925 Mangler.getStream() << '@';
3926}
3927
3928void MicrosoftMangleContextImpl::mangleCXXVTable(const CXXRecordDecl *Derived,
3929 raw_ostream &Out) {
3930 // TODO: Determine appropriate mangling for MSABI
3931 mangleCXXVFTable(Derived, {}, Out);
3932}
3933
3934void MicrosoftMangleContextImpl::mangleCXXVBTable(
3935 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
3936 raw_ostream &Out) {
3937 // <mangled-name> ::= ?_8 <class-name> <storage-class>
3938 // <cvr-qualifiers> [<name>] @
3939 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
3940 // is always '7' for vbtables.
3941 msvc_hashing_ostream MHO(Out);
3942 MicrosoftCXXNameMangler Mangler(*this, MHO);
3943 Mangler.getStream() << "??_8";
3944 Mangler.mangleName(Derived);
3945 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
3946 for (const CXXRecordDecl *RD : BasePath)
3947 Mangler.mangleName(RD);
3948 Mangler.getStream() << '@';
3949}
3950
3951void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &Out) {
3952 msvc_hashing_ostream MHO(Out);
3953 MicrosoftCXXNameMangler Mangler(*this, MHO);
3954 Mangler.getStream() << "??_R0";
3955 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result);
3956 Mangler.getStream() << "@8";
3957}
3958
3959void MicrosoftMangleContextImpl::mangleCXXRTTIName(
3960 QualType T, raw_ostream &Out, bool NormalizeIntegers = false) {
3961 MicrosoftCXXNameMangler Mangler(*this, Out);
3962 Mangler.getStream() << '.';
3963 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result);
3964}
3965
3966void MicrosoftMangleContextImpl::mangleCXXVirtualDisplacementMap(
3967 const CXXRecordDecl *SrcRD, const CXXRecordDecl *DstRD, raw_ostream &Out) {
3968 msvc_hashing_ostream MHO(Out);
3969 MicrosoftCXXNameMangler Mangler(*this, MHO);
3970 Mangler.getStream() << "??_K";
3971 Mangler.mangleName(SrcRD);
3972 Mangler.getStream() << "$C";
3973 Mangler.mangleName(DstRD);
3974}
3975
3976void MicrosoftMangleContextImpl::mangleCXXThrowInfo(QualType T, bool IsConst,
3977 bool IsVolatile,
3978 bool IsUnaligned,
3979 uint32_t NumEntries,
3980 raw_ostream &Out) {
3981 msvc_hashing_ostream MHO(Out);
3982 MicrosoftCXXNameMangler Mangler(*this, MHO);
3983 Mangler.getStream() << "_TI";
3984 if (IsConst)
3985 Mangler.getStream() << 'C';
3986 if (IsVolatile)
3987 Mangler.getStream() << 'V';
3988 if (IsUnaligned)
3989 Mangler.getStream() << 'U';
3990 Mangler.getStream() << NumEntries;
3991 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result);
3992}
3993
3994void MicrosoftMangleContextImpl::mangleCXXCatchableTypeArray(
3995 QualType T, uint32_t NumEntries, raw_ostream &Out) {
3996 msvc_hashing_ostream MHO(Out);
3997 MicrosoftCXXNameMangler Mangler(*this, MHO);
3998 Mangler.getStream() << "_CTA";
3999 Mangler.getStream() << NumEntries;
4000 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result);
4001}
4002
4003void MicrosoftMangleContextImpl::mangleCXXCatchableType(
4004 QualType T, const CXXConstructorDecl *CD, CXXCtorType CT, uint32_t Size,
4005 uint32_t NVOffset, int32_t VBPtrOffset, uint32_t VBIndex,
4006 raw_ostream &Out) {
4007 MicrosoftCXXNameMangler Mangler(*this, Out);
4008 Mangler.getStream() << "_CT";
4009
4010 llvm::SmallString<64> RTTIMangling;
4011 {
4012 llvm::raw_svector_ostream Stream(RTTIMangling);
4013 msvc_hashing_ostream MHO(Stream);
4014 mangleCXXRTTI(T, MHO);
4015 }
4016 Mangler.getStream() << RTTIMangling;
4017
4018 // VS2015 and VS2017.1 omit the copy-constructor in the mangled name but
4019 // both older and newer versions include it.
4020 // FIXME: It is known that the Ctor is present in 2013, and in 2017.7
4021 // (_MSC_VER 1914) and newer, and that it's omitted in 2015 and 2017.4
4022 // (_MSC_VER 1911), but it's unknown when exactly it reappeared (1914?
4023 // Or 1912, 1913 already?).
4024 bool OmitCopyCtor = getASTContext().getLangOpts().isCompatibleWithMSVC(
4025 LangOptions::MSVC2015) &&
4026 !getASTContext().getLangOpts().isCompatibleWithMSVC(
4027 LangOptions::MSVC2017_7);
4028 llvm::SmallString<64> CopyCtorMangling;
4029 if (!OmitCopyCtor && CD) {
4030 llvm::raw_svector_ostream Stream(CopyCtorMangling);
4031 msvc_hashing_ostream MHO(Stream);
4032 mangleCXXName(GlobalDecl(CD, CT), MHO);
4033 }
4034 Mangler.getStream() << CopyCtorMangling;
4035
4036 Mangler.getStream() << Size;
4037 if (VBPtrOffset == -1) {
4038 if (NVOffset) {
4039 Mangler.getStream() << NVOffset;
4040 }
4041 } else {
4042 Mangler.getStream() << NVOffset;
4043 Mangler.getStream() << VBPtrOffset;
4044 Mangler.getStream() << VBIndex;
4045 }
4046}
4047
4048void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassDescriptor(
4049 const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset,
4050 uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) {
4051 msvc_hashing_ostream MHO(Out);
4052 MicrosoftCXXNameMangler Mangler(*this, MHO);
4053 Mangler.getStream() << "??_R1";
4054 Mangler.mangleNumber(NVOffset);
4055 Mangler.mangleNumber(VBPtrOffset);
4056 Mangler.mangleNumber(VBTableOffset);
4057 Mangler.mangleNumber(Flags);
4058 Mangler.mangleName(Derived);
4059 Mangler.getStream() << "8";
4060}
4061
4062void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassArray(
4063 const CXXRecordDecl *Derived, raw_ostream &Out) {
4064 msvc_hashing_ostream MHO(Out);
4065 MicrosoftCXXNameMangler Mangler(*this, MHO);
4066 Mangler.getStream() << "??_R2";
4067 Mangler.mangleName(Derived);
4068 Mangler.getStream() << "8";
4069}
4070
4071void MicrosoftMangleContextImpl::mangleCXXRTTIClassHierarchyDescriptor(
4072 const CXXRecordDecl *Derived, raw_ostream &Out) {
4073 msvc_hashing_ostream MHO(Out);
4074 MicrosoftCXXNameMangler Mangler(*this, MHO);
4075 Mangler.getStream() << "??_R3";
4076 Mangler.mangleName(Derived);
4077 Mangler.getStream() << "8";
4078}
4079
4080void MicrosoftMangleContextImpl::mangleCXXRTTICompleteObjectLocator(
4081 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
4082 raw_ostream &Out) {
4083 // <mangled-name> ::= ?_R4 <class-name> <storage-class>
4084 // <cvr-qualifiers> [<name>] @
4085 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
4086 // is always '6' for vftables.
4087 llvm::SmallString<64> VFTableMangling;
4088 llvm::raw_svector_ostream Stream(VFTableMangling);
4089 mangleCXXVFTable(Derived, BasePath, Stream);
4090
4091 if (VFTableMangling.starts_with("??@")) {
4092 assert(VFTableMangling.ends_with("@"));
4093 Out << VFTableMangling << "??_R4@";
4094 return;
4095 }
4096
4097 assert(VFTableMangling.starts_with("??_7") ||
4098 VFTableMangling.starts_with("??_S"));
4099
4100 Out << "??_R4" << VFTableMangling.str().drop_front(4);
4101}
4102
4103void MicrosoftMangleContextImpl::mangleSEHFilterExpression(
4104 GlobalDecl EnclosingDecl, raw_ostream &Out) {
4105 msvc_hashing_ostream MHO(Out);
4106 MicrosoftCXXNameMangler Mangler(*this, MHO);
4107 // The function body is in the same comdat as the function with the handler,
4108 // so the numbering here doesn't have to be the same across TUs.
4109 //
4110 // <mangled-name> ::= ?filt$ <filter-number> @0
4111 Mangler.getStream() << "?filt$" << SEHFilterIds[EnclosingDecl]++ << "@0@";
4112 Mangler.mangleName(EnclosingDecl);
4113}
4114
4115void MicrosoftMangleContextImpl::mangleSEHFinallyBlock(
4116 GlobalDecl EnclosingDecl, raw_ostream &Out) {
4117 msvc_hashing_ostream MHO(Out);
4118 MicrosoftCXXNameMangler Mangler(*this, MHO);
4119 // The function body is in the same comdat as the function with the handler,
4120 // so the numbering here doesn't have to be the same across TUs.
4121 //
4122 // <mangled-name> ::= ?fin$ <filter-number> @0
4123 Mangler.getStream() << "?fin$" << SEHFinallyIds[EnclosingDecl]++ << "@0@";
4124 Mangler.mangleName(EnclosingDecl);
4125}
4126
4127void MicrosoftMangleContextImpl::mangleCanonicalTypeName(
4128 QualType T, raw_ostream &Out, bool NormalizeIntegers = false) {
4129 // This is just a made up unique string for the purposes of tbaa. undname
4130 // does *not* know how to demangle it.
4131 MicrosoftCXXNameMangler Mangler(*this, Out);
4132 Mangler.getStream() << '?';
4133 Mangler.mangleType(T.getCanonicalType(), SourceRange());
4134}
4135
4136void MicrosoftMangleContextImpl::mangleReferenceTemporary(
4137 const VarDecl *VD, unsigned ManglingNumber, raw_ostream &Out) {
4138 msvc_hashing_ostream MHO(Out);
4139 MicrosoftCXXNameMangler Mangler(*this, MHO);
4140
4141 Mangler.getStream() << "?";
4142 Mangler.mangleSourceName("$RT" + llvm::utostr(ManglingNumber));
4143 Mangler.mangle(VD, "");
4144}
4145
4146void MicrosoftMangleContextImpl::mangleThreadSafeStaticGuardVariable(
4147 const VarDecl *VD, unsigned GuardNum, raw_ostream &Out) {
4148 msvc_hashing_ostream MHO(Out);
4149 MicrosoftCXXNameMangler Mangler(*this, MHO);
4150
4151 Mangler.getStream() << "?";
4152 Mangler.mangleSourceName("$TSS" + llvm::utostr(GuardNum));
4153 Mangler.mangleNestedName(VD);
4154 Mangler.getStream() << "@4HA";
4155}
4156
4157void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
4158 raw_ostream &Out) {
4159 // <guard-name> ::= ?_B <postfix> @5 <scope-depth>
4160 // ::= ?__J <postfix> @5 <scope-depth>
4161 // ::= ?$S <guard-num> @ <postfix> @4IA
4162
4163 // The first mangling is what MSVC uses to guard static locals in inline
4164 // functions. It uses a different mangling in external functions to support
4165 // guarding more than 32 variables. MSVC rejects inline functions with more
4166 // than 32 static locals. We don't fully implement the second mangling
4167 // because those guards are not externally visible, and instead use LLVM's
4168 // default renaming when creating a new guard variable.
4169 msvc_hashing_ostream MHO(Out);
4170 MicrosoftCXXNameMangler Mangler(*this, MHO);
4171
4172 bool Visible = VD->isExternallyVisible();
4173 if (Visible) {
4174 Mangler.getStream() << (VD->getTLSKind() ? "??__J" : "??_B");
4175 } else {
4176 Mangler.getStream() << "?$S1@";
4177 }
4178 unsigned ScopeDepth = 0;
4179 if (Visible && !getNextDiscriminator(VD, ScopeDepth))
4180 // If we do not have a discriminator and are emitting a guard variable for
4181 // use at global scope, then mangling the nested name will not be enough to
4182 // remove ambiguities.
4183 Mangler.mangle(VD, "");
4184 else
4185 Mangler.mangleNestedName(VD);
4186 Mangler.getStream() << (Visible ? "@5" : "@4IA");
4187 if (ScopeDepth)
4188 Mangler.mangleNumber(ScopeDepth);
4189}
4190
4191void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
4192 char CharCode,
4193 raw_ostream &Out) {
4194 msvc_hashing_ostream MHO(Out);
4195 MicrosoftCXXNameMangler Mangler(*this, MHO);
4196 Mangler.getStream() << "??__" << CharCode;
4197 if (D->isStaticDataMember()) {
4198 Mangler.getStream() << '?';
4199 Mangler.mangleName(D);
4200 Mangler.mangleVariableEncoding(D);
4201 Mangler.getStream() << "@@";
4202 } else {
4203 Mangler.mangleName(D);
4204 }
4205 // This is the function class mangling. These stubs are global, non-variadic,
4206 // cdecl functions that return void and take no args.
4207 Mangler.getStream() << "YAXXZ";
4208}
4209
4210void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
4211 raw_ostream &Out) {
4212 // <initializer-name> ::= ?__E <name> YAXXZ
4213 mangleInitFiniStub(D, 'E', Out);
4214}
4215
4216void
4217MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
4218 raw_ostream &Out) {
4219 // <destructor-name> ::= ?__F <name> YAXXZ
4220 mangleInitFiniStub(D, 'F', Out);
4221}
4222
4223void MicrosoftMangleContextImpl::mangleStringLiteral(const StringLiteral *SL,
4224 raw_ostream &Out) {
4225 // <char-type> ::= 0 # char, char16_t, char32_t
4226 // # (little endian char data in mangling)
4227 // ::= 1 # wchar_t (big endian char data in mangling)
4228 //
4229 // <literal-length> ::= <non-negative integer> # the length of the literal
4230 //
4231 // <encoded-crc> ::= <hex digit>+ @ # crc of the literal including
4232 // # trailing null bytes
4233 //
4234 // <encoded-string> ::= <simple character> # uninteresting character
4235 // ::= '?$' <hex digit> <hex digit> # these two nibbles
4236 // # encode the byte for the
4237 // # character
4238 // ::= '?' [a-z] # \xe1 - \xfa
4239 // ::= '?' [A-Z] # \xc1 - \xda
4240 // ::= '?' [0-9] # [,/\:. \n\t'-]
4241 //
4242 // <literal> ::= '??_C@_' <char-type> <literal-length> <encoded-crc>
4243 // <encoded-string> '@'
4244 MicrosoftCXXNameMangler Mangler(*this, Out);
4245 Mangler.getStream() << "??_C@_";
4246
4247 // The actual string length might be different from that of the string literal
4248 // in cases like:
4249 // char foo[3] = "foobar";
4250 // char bar[42] = "foobar";
4251 // Where it is truncated or zero-padded to fit the array. This is the length
4252 // used for mangling, and any trailing null-bytes also need to be mangled.
4253 unsigned StringLength =
4254 getASTContext().getAsConstantArrayType(SL->getType())->getZExtSize();
4255 unsigned StringByteLength = StringLength * SL->getCharByteWidth();
4256
4257 // <char-type>: The "kind" of string literal is encoded into the mangled name.
4258 if (SL->isWide())
4259 Mangler.getStream() << '1';
4260 else
4261 Mangler.getStream() << '0';
4262
4263 // <literal-length>: The next part of the mangled name consists of the length
4264 // of the string in bytes.
4265 Mangler.mangleNumber(StringByteLength);
4266
4267 auto GetLittleEndianByte = [&SL](unsigned Index) {
4268 unsigned CharByteWidth = SL->getCharByteWidth();
4269 if (Index / CharByteWidth >= SL->getLength())
4270 return static_cast<char>(0);
4271 uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);
4272 unsigned OffsetInCodeUnit = Index % CharByteWidth;
4273 return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff);
4274 };
4275
4276 auto GetBigEndianByte = [&SL](unsigned Index) {
4277 unsigned CharByteWidth = SL->getCharByteWidth();
4278 if (Index / CharByteWidth >= SL->getLength())
4279 return static_cast<char>(0);
4280 uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);
4281 unsigned OffsetInCodeUnit = (CharByteWidth - 1) - (Index % CharByteWidth);
4282 return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff);
4283 };
4284
4285 // CRC all the bytes of the StringLiteral.
4286 llvm::JamCRC JC;
4287 for (unsigned I = 0, E = StringByteLength; I != E; ++I)
4288 JC.update(GetLittleEndianByte(I));
4289
4290 // <encoded-crc>: The CRC is encoded utilizing the standard number mangling
4291 // scheme.
4292 Mangler.mangleNumber(JC.getCRC());
4293
4294 // <encoded-string>: The mangled name also contains the first 32 bytes
4295 // (including null-terminator bytes) of the encoded StringLiteral.
4296 // Each character is encoded by splitting them into bytes and then encoding
4297 // the constituent bytes.
4298 auto MangleByte = [&Mangler](char Byte) {
4299 // There are five different manglings for characters:
4300 // - [a-zA-Z0-9_$]: A one-to-one mapping.
4301 // - ?[a-z]: The range from \xe1 to \xfa.
4302 // - ?[A-Z]: The range from \xc1 to \xda.
4303 // - ?[0-9]: The set of [,/\:. \n\t'-].
4304 // - ?$XX: A fallback which maps nibbles.
4305 if (isAsciiIdentifierContinue(Byte, /*AllowDollar=*/true)) {
4306 Mangler.getStream() << Byte;
4307 } else if (isLetter(Byte & 0x7f)) {
4308 Mangler.getStream() << '?' << static_cast<char>(Byte & 0x7f);
4309 } else {
4310 const char SpecialChars[] = {',', '/', '\\', ':', '.',
4311 ' ', '\n', '\t', '\'', '-'};
4312 const char *Pos = llvm::find(SpecialChars, Byte);
4313 if (Pos != std::end(SpecialChars)) {
4314 Mangler.getStream() << '?' << (Pos - std::begin(SpecialChars));
4315 } else {
4316 Mangler.getStream() << "?$";
4317 Mangler.getStream() << static_cast<char>('A' + ((Byte >> 4) & 0xf));
4318 Mangler.getStream() << static_cast<char>('A' + (Byte & 0xf));
4319 }
4320 }
4321 };
4322
4323 // Enforce our 32 bytes max, except wchar_t which gets 32 chars instead.
4324 unsigned MaxBytesToMangle = SL->isWide() ? 64U : 32U;
4325 unsigned NumBytesToMangle = std::min(MaxBytesToMangle, StringByteLength);
4326 for (unsigned I = 0; I != NumBytesToMangle; ++I) {
4327 if (SL->isWide())
4328 MangleByte(GetBigEndianByte(I));
4329 else
4330 MangleByte(GetLittleEndianByte(I));
4331 }
4332
4333 Mangler.getStream() << '@';
4334}
4335
4336void MicrosoftCXXNameMangler::mangleAutoReturnType(const MemberPointerType *T,
4337 Qualifiers Quals) {
4338 QualType PointeeType = T->getPointeeType();
4339 manglePointerCVQualifiers(Quals);
4340 manglePointerExtQualifiers(Quals, PointeeType);
4341 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
4342 Out << '8';
4343 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
4344 mangleFunctionType(FPT, nullptr, true);
4345 } else {
4346 mangleQualifiers(PointeeType.getQualifiers(), true);
4347 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
4348 mangleAutoReturnType(PointeeType, QMM_Drop);
4349 }
4350}
4351
4352void MicrosoftCXXNameMangler::mangleAutoReturnType(const PointerType *T,
4353 Qualifiers Quals) {
4354 QualType PointeeType = T->getPointeeType();
4355 assert(!PointeeType.getQualifiers().hasAddressSpace() &&
4356 "Unexpected address space mangling required");
4357
4358 manglePointerCVQualifiers(Quals);
4359 manglePointerExtQualifiers(Quals, PointeeType);
4360
4361 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
4362 Out << '6';
4363 mangleFunctionType(FPT);
4364 } else {
4365 mangleAutoReturnType(PointeeType, QMM_Mangle);
4366 }
4367}
4368
4369void MicrosoftCXXNameMangler::mangleAutoReturnType(const LValueReferenceType *T,
4370 Qualifiers Quals) {
4371 QualType PointeeType = T->getPointeeType();
4372 assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!");
4373 Out << 'A';
4374 manglePointerExtQualifiers(Quals, PointeeType);
4375 mangleAutoReturnType(PointeeType, QMM_Mangle);
4376}
4377
4378void MicrosoftCXXNameMangler::mangleAutoReturnType(const RValueReferenceType *T,
4379 Qualifiers Quals) {
4380 QualType PointeeType = T->getPointeeType();
4381 assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!");
4382 Out << "$$Q";
4383 manglePointerExtQualifiers(Quals, PointeeType);
4384 mangleAutoReturnType(PointeeType, QMM_Mangle);
4385}
4386
4388 DiagnosticsEngine &Diags,
4389 bool IsAux) {
4390 return new MicrosoftMangleContextImpl(Context, Diags, IsAux);
4391}
Enums/classes describing ABI related information about constructors, destructors and thunks.
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3443
StringRef P
#define SM(sm)
Definition: Cuda.cpp:84
const Decl * D
Expr * E
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
This file defines OpenMP nodes for declarative directives.
Defines the C++ template declaration subclasses.
Defines the clang::Expr interface and subclasses for C++ expressions.
unsigned Iter
Definition: HTMLLogger.cpp:153
static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target)
llvm::MachO::Record Record
Definition: MachO.h:31
static GlobalDecl isTemplate(GlobalDecl GD, const TemplateArgumentList *&TemplateArgs)
static ValueDecl * getAsArrayToPointerDecayedDecl(QualType T, const APValue &V)
If value V (with type T) represents a decayed pointer to the first element of an array,...
static void mangleThunkThisAdjustment(AccessSpecifier AS, const ThisAdjustment &Adjustment, MicrosoftCXXNameMangler &Mangler, raw_ostream &Out)
SourceRange Range
Definition: SemaObjC.cpp:758
SourceLocation Loc
Definition: SemaObjC.cpp:759
Defines the SourceManager interface.
A non-discriminated union of a base, field, or array index.
Definition: APValue.h:206
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
@ Indeterminate
This object has an indeterminate value (C++ [basic.indet]).
Definition: APValue.h:131
@ None
There is no such object (it's outside its lifetime).
Definition: APValue.h:129
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
SourceManager & getSourceManager()
Definition: ASTContext.h:741
unsigned getManglingNumber(const NamedDecl *ND, bool ForAuxTarget=false) const
TypedefNameDecl * getTypedefNameForUnnamedTagDecl(const TagDecl *TD)
const LangOptions & getLangOpts() const
Definition: ASTContext.h:834
bool addressSpaceMapManglingFor(LangAS AS) const
Definition: ASTContext.h:3010
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:733
DeclaratorDecl * getDeclaratorForUnnamedTagDecl(const TagDecl *TD)
unsigned getTargetAddressSpace(LangAS AS) const
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
CharUnits getVBPtrOffset() const
getVBPtrOffset - Get the offset for virtual base table pointer.
Definition: RecordLayout.h:324
Represents a constant array type that does not decay to a pointer when used as a function parameter.
Definition: Type.h:3747
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3577
QualType getElementType() const
Definition: Type.h:3589
Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained by a type-constraint.
Definition: Type.h:6556
A fixed int type of a specified bitwidth.
Definition: Type.h:7814
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4474
Pointer to a block type.
Definition: Type.h:3408
This class is used for builtin types like 'int'.
Definition: Type.h:3034
Kind getKind() const
Definition: Type.h:3082
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2553
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2817
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2078
bool isImplicitObjectMemberFunction() const
[C++2b][dcl.fct]/p7 An implicit object member function is a non-static member function without an exp...
Definition: DeclCXX.cpp:2556
bool isVirtual() const
Definition: DeclCXX.h:2133
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2204
bool isInstance() const
Definition: DeclCXX.h:2105
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
Decl * getLambdaContextDecl() const
Retrieve the declaration that provides additional context for a lambda, when the normal declaration c...
Definition: DeclCXX.cpp:1779
base_class_range bases()
Definition: DeclCXX.h:620
bool isLambda() const
Determine whether this class describes a lambda function object.
Definition: DeclCXX.h:1030
unsigned getLambdaManglingNumber() const
If this is the closure type of a lambda expression, retrieve the number to be used for name mangling ...
Definition: DeclCXX.h:1782
CXXRecordDecl * getMostRecentNonInjectedDecl()
Definition: DeclCXX.h:550
MSInheritanceModel getMSInheritanceModel() const
Returns the inheritance model used for this record.
bool nullFieldOffsetIsZero() const
In the Microsoft C++ ABI, use zero for the field offset of a null data member pointer if we can guara...
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
Represents a class template specialization, which refers to a class template with a given set of temp...
Complex values, per C99 6.2.5p11.
Definition: Type.h:3145
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:3615
llvm::APInt getSize() const
Return the constant array size as an APInt.
Definition: Type.h:3671
Represents a concrete matrix type with constant number of rows and columns.
Definition: Type.h:4232
Represents a pointer type decayed from an array or function type.
Definition: Type.h:3391
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2089
bool isNamespace() const
Definition: DeclBase.h:2178
bool isTranslationUnit() const
Definition: DeclBase.h:2165
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1990
bool isFunctionOrMethod() const
Definition: DeclBase.h:2141
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
T * getAttr() const
Definition: DeclBase.h:576
SourceLocation getLocation() const
Definition: DeclBase.h:442
DeclContext * getDeclContext()
Definition: DeclBase.h:451
AccessSpecifier getAccess() const
Definition: DeclBase.h:510
bool hasAttr() const
Definition: DeclBase.h:580
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition: DeclBase.h:430
The name of a declaration.
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:735
Represents the type decltype(expr) (C++11).
Definition: Type.h:5874
A decomposition declaration.
Definition: DeclCXX.h:4184
Represents a C++17 deduced template specialization type.
Definition: Type.h:6604
Represents an extended address space qualifier where the input address space value is dependent.
Definition: Type.h:3920
Represents a qualified type name for which the type name is dependent.
Definition: Type.h:7024
Represents an array type in C++ whose size is a value-dependent expression.
Definition: Type.h:3862
SourceRange getBracketsRange() const
Definition: Type.h:3888
Expr * getSizeExpr() const
Definition: Type.h:3882
Represents an extended vector type where either the type or size is dependent.
Definition: Type.h:3960
Represents a matrix type where the type and the number of rows and columns is dependent on a template...
Definition: Type.h:4291
Represents a template specialization type whose template cannot be resolved, e.g.
Definition: Type.h:7076
Represents a vector type where either the type or size is dependent.
Definition: Type.h:4086
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1220
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1493
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Definition: Diagnostic.h:896
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:6098
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc=nullptr) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
QualType getType() const
Definition: Expr.h:142
ExtVectorType - Extended vector type.
Definition: Type.h:4126
Represents a member of a struct/union/class.
Definition: Decl.h:3033
Represents a function declaration or definition.
Definition: Decl.h:1935
FunctionTemplateDecl * getPrimaryTemplate() const
Retrieve the primary template that this function template specialization either specializes or was in...
Definition: Decl.cpp:4172
FunctionDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: Decl.cpp:3623
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition: Decl.cpp:4188
bool isExternC() const
Determines whether this function is a function with external, C linkage.
Definition: Decl.cpp:3498
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: Decl.cpp:4405
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition: Type.h:4681
Represents a prototype with parameter type info, e.g.
Definition: Type.h:5102
unsigned getNumParams() const
Definition: Type.h:5355
Qualifiers getMethodQuals() const
Definition: Type.h:5497
QualType getParamType(unsigned i) const
Definition: Type.h:5357
CanThrowResult canThrow() const
Determine whether this function type has a non-throwing exception specification.
Definition: Type.cpp:3758
bool isVariadic() const
Whether this function prototype is variadic.
Definition: Type.h:5479
RefQualifierKind getRefQualifier() const
Retrieve the ref-qualifier associated with this function type.
Definition: Type.h:5505
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:4321
CallingConv getCallConv() const
Definition: Type.h:4654
QualType getReturnType() const
Definition: Type.h:4643
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:105
KernelReferenceKind getKernelReferenceKind() const
Definition: GlobalDecl.h:132
GlobalDecl getWithDecl(const Decl *D)
Definition: GlobalDecl.h:167
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:110
const Decl * getDecl() const
Definition: GlobalDecl.h:103
One of these records is kept for each identifier that is lexed.
Represents a C array with an unspecified size.
Definition: Type.h:3764
The injected class name of a C++ class template or class template partial specialization.
Definition: Type.h:6793
An lvalue reference type, per C++11 [dcl.ref].
Definition: Type.h:3483
bool isCompatibleWithMSVC(MSVCMajorVersion MajorVersion) const
Definition: LangOptions.h:672
A global _GUID constant.
Definition: DeclCXX.h:4307
virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, const ThunkInfo &Thunk, bool ElideOverrideInfo, raw_ostream &)=0
virtual void mangleCXXRTTI(QualType T, raw_ostream &)=0
bool isAux() const
Definition: Mangle.h:70
virtual std::string getLambdaString(const CXXRecordDecl *Lambda)=0
virtual bool shouldMangleStringLiteral(const StringLiteral *SL)=0
ASTContext & getASTContext() const
Definition: Mangle.h:78
virtual void mangleDynamicAtExitDestructor(const VarDecl *D, raw_ostream &)=0
virtual void mangleSEHFilterExpression(GlobalDecl EnclosingDecl, raw_ostream &Out)=0
virtual void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, bool ElideOverrideInfo, raw_ostream &)=0
virtual void mangleCanonicalTypeName(QualType T, raw_ostream &, bool NormalizeIntegers=false)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing.
virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &)=0
virtual void mangleCXXName(GlobalDecl GD, raw_ostream &)=0
virtual void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber, raw_ostream &)=0
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
virtual void mangleCXXRTTIName(QualType T, raw_ostream &, bool NormalizeIntegers=false)=0
virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &)=0
virtual void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &)=0
virtual void mangleSEHFinallyBlock(GlobalDecl EnclosingDecl, raw_ostream &Out)=0
virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &)=0
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:3519
virtual void mangleCXXVFTable(const CXXRecordDecl *Derived, ArrayRef< const CXXRecordDecl * > BasePath, raw_ostream &Out)=0
Mangle vftable symbols.
virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, const MethodVFTableLocation &ML, raw_ostream &Out)=0
virtual void mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived, ArrayRef< const CXXRecordDecl * > BasePath, raw_ostream &Out)=0
virtual void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile, bool IsUnaligned, uint32_t NumEntries, raw_ostream &Out)=0
virtual void mangleCXXVBTable(const CXXRecordDecl *Derived, ArrayRef< const CXXRecordDecl * > BasePath, raw_ostream &Out)=0
Mangle vbtable symbols.
virtual void mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived, raw_ostream &Out)=0
virtual void mangleThreadSafeStaticGuardVariable(const VarDecl *VD, unsigned GuardNum, raw_ostream &Out)=0
static MicrosoftMangleContext * create(ASTContext &Context, DiagnosticsEngine &Diags, bool IsAux=false)
virtual void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries, raw_ostream &Out)=0
virtual void mangleCXXRTTIBaseClassDescriptor(const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset, uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out)=0
virtual void mangleCXXCatchableType(QualType T, const CXXConstructorDecl *CD, CXXCtorType CT, uint32_t Size, uint32_t NVOffset, int32_t VBPtrOffset, uint32_t VBIndex, raw_ostream &Out)=0
virtual void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived, raw_ostream &Out)=0
virtual void mangleCXXVirtualDisplacementMap(const CXXRecordDecl *SrcRD, const CXXRecordDecl *DstRD, raw_ostream &Out)=0
MethodVFTableLocation getMethodVFTableLocation(GlobalDecl GD)
This represents a decl that may have a name.
Definition: Decl.h:253
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:274
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:280
bool isExternallyVisible() const
Definition: Decl.h:412
Represent a C++ namespace.
Definition: Decl.h:551
NonTypeTemplateParmDecl - Declares a non-type template parameter, e.g., "Size" in.
Interfaces are the core concept in Objective-C for object oriented design.
Definition: Type.h:7524
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
Represents a pointer to an Objective C object.
Definition: Type.h:7580
Represents a class type in Objective C.
Definition: Type.h:7326
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2083
Represents a pack expansion of types.
Definition: Type.h:7141
Represents a parameter to a function.
Definition: Decl.h:1725
unsigned getFunctionScopeIndex() const
Returns the index of this parameter in its prototype or method scope.
Definition: Decl.h:1785
PipeType - OpenCL20.
Definition: Type.h:7780
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:3198
PrettyStackTraceDecl - If a crash occurs, indicate that it happened when doing something to a specifi...
Definition: DeclBase.h:1286
A (possibly-)qualified type.
Definition: Type.h:929
QualType getDesugaredType(const ASTContext &Context) const
Return the specified type with any "sugar" removed from the type.
Definition: Type.h:1291
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:996
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:7971
QualType getCanonicalType() const
Definition: Type.h:7983
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:8025
void * getAsOpaquePtr() const
Definition: Type.h:976
Qualifiers getLocalQualifiers() const
Retrieve the set of qualifiers local to this particular QualType instance, not including any qualifie...
Definition: Type.h:7963
The collection of all-type qualifiers we support.
Definition: Type.h:324
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition: Type.h:354
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition: Type.h:347
@ OCL_None
There is no lifetime qualification on this type.
Definition: Type.h:343
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition: Type.h:357
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition: Type.h:360
bool hasConst() const
Definition: Type.h:450
bool hasUnaligned() const
Definition: Type.h:504
bool hasAddressSpace() const
Definition: Type.h:563
bool hasRestrict() const
Definition: Type.h:470
void removeUnaligned()
Definition: Type.h:508
bool hasVolatile() const
Definition: Type.h:460
bool hasObjCLifetime() const
Definition: Type.h:537
ObjCLifetime getObjCLifetime() const
Definition: Type.h:538
Qualifiers withoutObjCLifetime() const
Definition: Type.h:526
LangAS getAddressSpace() const
Definition: Type.h:564
An rvalue reference type, per C++11 [dcl.ref].
Definition: Type.h:3501
field_range fields() const
Definition: Decl.h:4354
bool isAnonymousStructOrUnion() const
Whether this is an anonymous struct or union.
Definition: Decl.h:4200
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:6072
RecordDecl * getDecl() const
Definition: Type.h:6082
decl_type * getFirstDecl()
Return the first declaration of this declaration or itself if this is the only declaration.
Definition: Redeclarable.h:215
QualType getPointeeType() const
Definition: Type.h:3457
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:324
const char * getStmtClassName() const
Definition: Stmt.cpp:77
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1778
bool isWide() const
Definition: Expr.h:1903
unsigned getLength() const
Definition: Expr.h:1895
uint32_t getCodeUnit(size_t i) const
Definition: Expr.h:1870
unsigned getCharByteWidth() const
Definition: Expr.h:1896
Represents the result of substituting a set of types for a template type parameter pack.
Definition: Type.h:6464
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3564
TypedefNameDecl * getTypedefNameForAnonDecl() const
Definition: Decl.h:3792
TagKind getTagKind() const
Definition: Decl.h:3759
A template argument list.
Definition: DeclTemplate.h:250
unsigned size() const
Retrieve the number of template arguments in this template argument list.
Definition: DeclTemplate.h:286
Represents a template argument.
Definition: TemplateBase.h:61
ArrayRef< TemplateArgument > getPackAsArray() const
Return the array of arguments in this template argument pack.
Definition: TemplateBase.h:444
QualType getStructuralValueType() const
Get the type of a StructuralValue.
Definition: TemplateBase.h:399
QualType getParamTypeForDecl() const
Definition: TemplateBase.h:331
Expr * getAsExpr() const
Retrieve the template argument as an expression.
Definition: TemplateBase.h:408
QualType getNonTypeTemplateArgumentType() const
If this is a non-type template argument, get its type.
QualType getAsType() const
Retrieve the type for a type template argument.
Definition: TemplateBase.h:319
llvm::APSInt getAsIntegral() const
Retrieve the template argument as an integral value.
Definition: TemplateBase.h:363
QualType getNullPtrType() const
Retrieve the type for null non-type template argument.
Definition: TemplateBase.h:337
TemplateName getAsTemplate() const
Retrieve the template name for a template name argument.
Definition: TemplateBase.h:343
QualType getIntegralType() const
Retrieve the type of the integral value.
Definition: TemplateBase.h:377
ValueDecl * getAsDecl() const
Retrieve the declaration for a declaration non-type template argument.
Definition: TemplateBase.h:326
@ Declaration
The template argument is a declaration that was provided for a pointer, reference,...
Definition: TemplateBase.h:74
@ Template
The template argument is a template name that was provided for a template template parameter.
Definition: TemplateBase.h:93
@ StructuralValue
The template argument is a non-type template argument that can't be represented by the special-case D...
Definition: TemplateBase.h:89
@ Pack
The template argument is actually a parameter pack.
Definition: TemplateBase.h:107
@ TemplateExpansion
The template argument is a pack expansion of a template name that was provided for a template templat...
Definition: TemplateBase.h:97
@ NullPtr
The template argument is a null pointer or null pointer to member that was provided for a non-type te...
Definition: TemplateBase.h:78
@ Type
The template argument is a type.
Definition: TemplateBase.h:70
@ Null
Represents an empty template argument, e.g., one that has not been deduced.
Definition: TemplateBase.h:67
@ Integral
The template argument is an integral value stored in an llvm::APSInt that was provided for an integra...
Definition: TemplateBase.h:82
@ Expression
The template argument is an expression, and we've not resolved it to one of the other forms yet,...
Definition: TemplateBase.h:103
ArgKind getKind() const
Return the kind of stored template argument.
Definition: TemplateBase.h:295
const APValue & getAsStructuralValue() const
Get the value of a StructuralValue.
Definition: TemplateBase.h:396
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:399
NamedDecl * getTemplatedDecl() const
Get the underlying, templated declaration.
Definition: DeclTemplate.h:431
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Definition: DeclTemplate.h:418
TemplateDecl * getAsTemplateDecl(bool IgnoreDeduced=false) const
Retrieve the underlying template declaration that this template name refers to, if known.
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:73
NamedDecl * getParam(unsigned Idx)
Definition: DeclTemplate.h:147
Represents a type template specialization; the template must be a class template, a type alias templa...
Definition: Type.h:6661
Represents a typeof (or typeof) expression (a C23 feature and GCC extension) or a typeof_unqual expre...
Definition: Type.h:5797
Represents typeof(type), a C23 feature and GCC extension, or `typeof_unqual(type),...
Definition: Type.h:5847
The base class of the type hierarchy.
Definition: Type.h:1828
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1916
bool isBlockPointerType() const
Definition: Type.h:8200
bool isVoidType() const
Definition: Type.h:8510
bool isArrayType() const
Definition: Type.h:8258
bool isPointerType() const
Definition: Type.h:8186
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8800
bool isReferenceType() const
Definition: Type.h:8204
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:738
AutoType * getContainedAutoType() const
Get the AutoType whose type will be deduced for a variable with an initializer of this type.
Definition: Type.h:2811
bool isMemberDataPointerType() const
Definition: Type.h:8251
bool isMemberPointerType() const
Definition: Type.h:8240
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition: Type.h:8786
bool isFunctionType() const
Definition: Type.h:8182
bool isAnyPointerType() const
Definition: Type.h:8194
TypeClass getTypeClass() const
Definition: Type.h:2341
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8731
bool isRecordType() const
Definition: Type.h:8286
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.cpp:1920
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3413
A unary type transform, which is a type constructed from another.
Definition: Type.h:5989
Represents the dependent type named by a dependently-scoped typename using declaration,...
Definition: Type.h:5667
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:671
QualType getType() const
Definition: Decl.h:682
Represents a variable declaration or definition.
Definition: Decl.h:882
TLSKind getTLSKind() const
Definition: Decl.cpp:2157
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: Decl.cpp:2179
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition: Decl.h:1234
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition: Decl.h:1159
bool isExternC() const
Determines whether this variable is a variable with external, C linkage.
Definition: Decl.cpp:2234
Represents a variable template specialization, which refers to a variable template with a given set o...
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3808
Represents a GCC generic vector type.
Definition: Type.h:4034
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:21
@ OO_None
Not an overloaded operator.
Definition: OperatorKinds.h:22
@ NUM_OVERLOADED_OPERATORS
Definition: OperatorKinds.h:26
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
@ Ctor_DefaultClosure
Default closure variant of a ctor.
Definition: ABI.h:29
@ Ctor_CopyingClosure
Copying closure variant of a ctor.
Definition: ABI.h:28
@ Ctor_Complete
Complete object ctor.
Definition: ABI.h:25
@ CPlusPlus
Definition: LangStandard.h:55
@ CPlusPlus17
Definition: LangStandard.h:58
LLVM_READONLY bool isAsciiIdentifierContinue(unsigned char c)
Definition: CharInfo.h:61
RefQualifierKind
The kind of C++11 ref-qualifier associated with a function type.
Definition: Type.h:1766
@ RQ_None
No ref-qualifier was provided.
Definition: Type.h:1768
@ RQ_LValue
An lvalue ref-qualifier was provided (&).
Definition: Type.h:1771
@ RQ_RValue
An rvalue ref-qualifier was provided (&&).
Definition: Type.h:1774
bool inheritanceModelHasNVOffsetField(bool IsMemberFunction, MSInheritanceModel Inheritance)
LanguageLinkage
Describes the different kinds of language linkage (C++ [dcl.link]) that an entity may have.
Definition: Linkage.h:63
@ CLanguageLinkage
Definition: Linkage.h:64
@ CXXLanguageLinkage
Definition: Linkage.h:65
bool inheritanceModelHasVBPtrOffsetField(MSInheritanceModel Inheritance)
LLVM_READONLY bool isLetter(unsigned char c)
Return true if this character is an ASCII letter: [a-zA-Z].
Definition: CharInfo.h:132
bool inheritanceModelHasVBTableOffsetField(MSInheritanceModel Inheritance)
CXXDtorType
C++ destructor types.
Definition: ABI.h:33
@ Dtor_Comdat
The COMDAT used for dtors.
Definition: ABI.h:37
@ Dtor_Base
Base object dtor.
Definition: ABI.h:36
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
@ Dtor_Deleting
Deleting dtor.
Definition: ABI.h:34
TagTypeKind
The kind of a tag type.
Definition: Type.h:6871
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
bool isPtrSizeAddressSpace(LangAS AS)
Definition: AddressSpaces.h:91
const FunctionProtoType * T
MSInheritanceModel
Assigned inheritance model for a class in the MS C++ ABI.
Definition: Specifiers.h:398
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:278
@ CC_X86Pascal
Definition: Specifiers.h:284
@ CC_Swift
Definition: Specifiers.h:293
@ CC_PreserveMost
Definition: Specifiers.h:295
@ CC_Win64
Definition: Specifiers.h:285
@ CC_X86ThisCall
Definition: Specifiers.h:282
@ CC_PreserveNone
Definition: Specifiers.h:301
@ CC_C
Definition: Specifiers.h:279
@ CC_SwiftAsync
Definition: Specifiers.h:294
@ CC_X86RegCall
Definition: Specifiers.h:287
@ CC_X86VectorCall
Definition: Specifiers.h:283
@ CC_X86StdCall
Definition: Specifiers.h:280
@ CC_X86_64SysV
Definition: Specifiers.h:286
@ CC_X86FastCall
Definition: Specifiers.h:281
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:123
@ AS_public
Definition: Specifiers.h:124
@ AS_protected
Definition: Specifiers.h:125
@ AS_none
Definition: Specifiers.h:127
@ AS_private
Definition: Specifiers.h:126
unsigned long uint64_t
long int64_t
unsigned int uint32_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
const CXXRecordDecl * VBase
If nonnull, holds the last vbase which contains the vfptr that the method definition is adjusted to.
CharUnits VFPtrOffset
This is the offset of the vfptr from the start of the last vbase, or the complete type if there are n...
uint64_t VBTableIndex
If nonzero, holds the vbtable index of the virtual base with the vfptr.
uint64_t Index
Method's index in the vftable.
bool isEmpty() const
Definition: Thunk.h:70
A this pointer adjustment.
Definition: Thunk.h:92
union clang::ThisAdjustment::VirtualAdjustment Virtual
int64_t NonVirtual
The non-virtual adjustment from the derived object to its nearest virtual base.
Definition: Thunk.h:95
The this pointer adjustment as well as an optional return adjustment for a thunk.
Definition: Thunk.h:157
ThisAdjustment This
The this pointer adjustment.
Definition: Thunk.h:159
const CXXMethodDecl * Method
Holds a pointer to the overridden method this thunk is for, if needed by the ABI to distinguish diffe...
Definition: Thunk.h:172
ReturnAdjustment Return
The return adjustment.
Definition: Thunk.h:162
struct clang::ThisAdjustment::VirtualAdjustment::@193 Microsoft
int32_t VtordispOffset
The offset of the vtordisp (in bytes), relative to the ECX.
Definition: Thunk.h:109
int32_t VBOffsetOffset
The offset (in bytes) of the vbase offset in the vbtable.
Definition: Thunk.h:116
int32_t VBPtrOffset
The offset of the vbptr of the derived class (in bytes), relative to the ECX after vtordisp adjustmen...
Definition: Thunk.h:113