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