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