1//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implements C++ name mangling according to the Itanium C++ ABI,
10// which is used in GCC 3.2 and newer (and many compilers that are
11// ABI-compatible with GCC):
12//
13// http://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
14//
15//===----------------------------------------------------------------------===//
16
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/Attr.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/DeclOpenMP.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/ExprCXX.h"
26#include "clang/AST/ExprConcepts.h"
27#include "clang/AST/ExprObjC.h"
28#include "clang/AST/Mangle.h"
29#include "clang/AST/TypeLoc.h"
30#include "clang/Basic/ABI.h"
31#include "clang/Basic/DiagnosticAST.h"
32#include "clang/Basic/Module.h"
33#include "clang/Basic/SourceManager.h"
34#include "clang/Basic/TargetInfo.h"
35#include "clang/Basic/Thunk.h"
36#include "llvm/ADT/StringExtras.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/TargetParser/RISCVTargetParser.h"
40#include <optional>
41
42using namespace clang;
43
44namespace {
45
46static bool isLocalContainerContext(const DeclContext *DC) {
47 return isa<FunctionDecl>(Val: DC) || isa<ObjCMethodDecl>(Val: DC) || isa<BlockDecl>(Val: DC);
48}
49
50static const FunctionDecl *getStructor(const FunctionDecl *fn) {
51 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
52 return ftd->getTemplatedDecl();
53
54 return fn;
55}
56
57static const NamedDecl *getStructor(const NamedDecl *decl) {
58 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(Val: decl);
59 return (fn ? getStructor(fn) : decl);
60}
61
62static bool isLambda(const NamedDecl *ND) {
63 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Val: ND);
64 if (!Record)
65 return false;
66
67 return Record->isLambda();
68}
69
70static const unsigned UnknownArity = ~0U;
71
72class ItaniumMangleContextImpl : public ItaniumMangleContext {
73 typedef std::pair<const DeclContext*, IdentifierInfo*> DiscriminatorKeyTy;
74 llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator;
75 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
76 const DiscriminatorOverrideTy DiscriminatorOverride = nullptr;
77 NamespaceDecl *StdNamespace = nullptr;
78
79 bool NeedsUniqueInternalLinkageNames = false;
80
81public:
82 explicit ItaniumMangleContextImpl(
83 ASTContext &Context, DiagnosticsEngine &Diags,
84 DiscriminatorOverrideTy DiscriminatorOverride, bool IsAux = false)
85 : ItaniumMangleContext(Context, Diags, IsAux),
86 DiscriminatorOverride(DiscriminatorOverride) {}
87
88 /// @name Mangler Entry Points
89 /// @{
90
91 bool shouldMangleCXXName(const NamedDecl *D) override;
92 bool shouldMangleStringLiteral(const StringLiteral *) override {
93 return false;
94 }
95
96 bool isUniqueInternalLinkageDecl(const NamedDecl *ND) override;
97 void needsUniqueInternalLinkageNames() override {
98 NeedsUniqueInternalLinkageNames = true;
99 }
100
101 void mangleCXXName(GlobalDecl GD, raw_ostream &) override;
102 void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk,
103 raw_ostream &) override;
104 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
105 const ThisAdjustment &ThisAdjustment,
106 raw_ostream &) override;
107 void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber,
108 raw_ostream &) override;
109 void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) override;
110 void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) override;
111 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
112 const CXXRecordDecl *Type, raw_ostream &) override;
113 void mangleCXXRTTI(QualType T, raw_ostream &) override;
114 void mangleCXXRTTIName(QualType T, raw_ostream &,
115 bool NormalizeIntegers) override;
116 void mangleCanonicalTypeName(QualType T, raw_ostream &,
117 bool NormalizeIntegers) override;
118
119 void mangleCXXCtorComdat(const CXXConstructorDecl *D, raw_ostream &) override;
120 void mangleCXXDtorComdat(const CXXDestructorDecl *D, raw_ostream &) override;
121 void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) override;
122 void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override;
123 void mangleDynamicAtExitDestructor(const VarDecl *D,
124 raw_ostream &Out) override;
125 void mangleDynamicStermFinalizer(const VarDecl *D, raw_ostream &Out) override;
126 void mangleSEHFilterExpression(GlobalDecl EnclosingDecl,
127 raw_ostream &Out) override;
128 void mangleSEHFinallyBlock(GlobalDecl EnclosingDecl,
129 raw_ostream &Out) override;
130 void mangleItaniumThreadLocalInit(const VarDecl *D, raw_ostream &) override;
131 void mangleItaniumThreadLocalWrapper(const VarDecl *D,
132 raw_ostream &) override;
133
134 void mangleStringLiteral(const StringLiteral *, raw_ostream &) override;
135
136 void mangleLambdaSig(const CXXRecordDecl *Lambda, raw_ostream &) override;
137
138 void mangleModuleInitializer(const Module *Module, raw_ostream &) override;
139
140 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
141 // Lambda closure types are already numbered.
142 if (isLambda(ND))
143 return false;
144
145 // Anonymous tags are already numbered.
146 if (const TagDecl *Tag = dyn_cast<TagDecl>(Val: ND)) {
147 if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl())
148 return false;
149 }
150
151 // Use the canonical number for externally visible decls.
152 if (ND->isExternallyVisible()) {
153 unsigned discriminator = getASTContext().getManglingNumber(ND, ForAuxTarget: isAux());
154 if (discriminator == 1)
155 return false;
156 disc = discriminator - 2;
157 return true;
158 }
159
160 // Make up a reasonable number for internal decls.
161 unsigned &discriminator = Uniquifier[ND];
162 if (!discriminator) {
163 const DeclContext *DC = getEffectiveDeclContext(ND);
164 discriminator = ++Discriminator[std::make_pair(x&: DC, y: ND->getIdentifier())];
165 }
166 if (discriminator == 1)
167 return false;
168 disc = discriminator-2;
169 return true;
170 }
171
172 std::string getLambdaString(const CXXRecordDecl *Lambda) override {
173 // This function matches the one in MicrosoftMangle, which returns
174 // the string that is used in lambda mangled names.
175 assert(Lambda->isLambda() && "RD must be a lambda!");
176 std::string Name("<lambda");
177 Decl *LambdaContextDecl = Lambda->getLambdaContextDecl();
178 unsigned LambdaManglingNumber = Lambda->getLambdaManglingNumber();
179 unsigned LambdaId;
180 const ParmVarDecl *Parm = dyn_cast_or_null<ParmVarDecl>(Val: LambdaContextDecl);
181 const FunctionDecl *Func =
182 Parm ? dyn_cast<FunctionDecl>(Parm->getDeclContext()) : nullptr;
183
184 if (Func) {
185 unsigned DefaultArgNo =
186 Func->getNumParams() - Parm->getFunctionScopeIndex();
187 Name += llvm::utostr(X: DefaultArgNo);
188 Name += "_";
189 }
190
191 if (LambdaManglingNumber)
192 LambdaId = LambdaManglingNumber;
193 else
194 LambdaId = getAnonymousStructIdForDebugInfo(Lambda);
195
196 Name += llvm::utostr(X: LambdaId);
197 Name += '>';
198 return Name;
199 }
200
201 DiscriminatorOverrideTy getDiscriminatorOverride() const override {
202 return DiscriminatorOverride;
203 }
204
205 NamespaceDecl *getStdNamespace();
206
207 const DeclContext *getEffectiveDeclContext(const Decl *D);
208 const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
209 return getEffectiveDeclContext(D: cast<Decl>(Val: DC));
210 }
211
212 bool isInternalLinkageDecl(const NamedDecl *ND);
213
214 /// @}
215};
216
217/// Manage the mangling of a single name.
218class CXXNameMangler {
219 ItaniumMangleContextImpl &Context;
220 raw_ostream &Out;
221 /// Normalize integer types for cross-language CFI support with other
222 /// languages that can't represent and encode C/C++ integer types.
223 bool NormalizeIntegers = false;
224
225 bool NullOut = false;
226 /// In the "DisableDerivedAbiTags" mode derived ABI tags are not calculated.
227 /// This mode is used when mangler creates another mangler recursively to
228 /// calculate ABI tags for the function return value or the variable type.
229 /// Also it is required to avoid infinite recursion in some cases.
230 bool DisableDerivedAbiTags = false;
231
232 /// The "structor" is the top-level declaration being mangled, if
233 /// that's not a template specialization; otherwise it's the pattern
234 /// for that specialization.
235 const NamedDecl *Structor;
236 unsigned StructorType = 0;
237
238 // An offset to add to all template parameter depths while mangling. Used
239 // when mangling a template parameter list to see if it matches a template
240 // template parameter exactly.
241 unsigned TemplateDepthOffset = 0;
242
243 /// The next substitution sequence number.
244 unsigned SeqID = 0;
245
246 class FunctionTypeDepthState {
247 unsigned Bits = 0;
248
249 enum { InResultTypeMask = 1 };
250
251 public:
252 FunctionTypeDepthState() = default;
253
254 /// The number of function types we're inside.
255 unsigned getDepth() const {
256 return Bits >> 1;
257 }
258
259 /// True if we're in the return type of the innermost function type.
260 bool isInResultType() const {
261 return Bits & InResultTypeMask;
262 }
263
264 FunctionTypeDepthState push() {
265 FunctionTypeDepthState tmp = *this;
266 Bits = (Bits & ~InResultTypeMask) + 2;
267 return tmp;
268 }
269
270 void enterResultType() {
271 Bits |= InResultTypeMask;
272 }
273
274 void leaveResultType() {
275 Bits &= ~InResultTypeMask;
276 }
277
278 void pop(FunctionTypeDepthState saved) {
279 assert(getDepth() == saved.getDepth() + 1);
280 Bits = saved.Bits;
281 }
282
283 } FunctionTypeDepth;
284
285 // abi_tag is a gcc attribute, taking one or more strings called "tags".
286 // The goal is to annotate against which version of a library an object was
287 // built and to be able to provide backwards compatibility ("dual abi").
288 // For more information see docs/ItaniumMangleAbiTags.rst.
289 typedef SmallVector<StringRef, 4> AbiTagList;
290
291 // State to gather all implicit and explicit tags used in a mangled name.
292 // Must always have an instance of this while emitting any name to keep
293 // track.
294 class AbiTagState final {
295 public:
296 explicit AbiTagState(AbiTagState *&Head) : LinkHead(Head) {
297 Parent = LinkHead;
298 LinkHead = this;
299 }
300
301 // No copy, no move.
302 AbiTagState(const AbiTagState &) = delete;
303 AbiTagState &operator=(const AbiTagState &) = delete;
304
305 ~AbiTagState() { pop(); }
306
307 void write(raw_ostream &Out, const NamedDecl *ND,
308 const AbiTagList *AdditionalAbiTags) {
309 ND = cast<NamedDecl>(ND->getCanonicalDecl());
310 if (!isa<FunctionDecl>(ND) && !isa<VarDecl>(ND)) {
311 assert(
312 !AdditionalAbiTags &&
313 "only function and variables need a list of additional abi tags");
314 if (const auto *NS = dyn_cast<NamespaceDecl>(ND)) {
315 if (const auto *AbiTag = NS->getAttr<AbiTagAttr>()) {
316 UsedAbiTags.insert(UsedAbiTags.end(), AbiTag->tags().begin(),
317 AbiTag->tags().end());
318 }
319 // Don't emit abi tags for namespaces.
320 return;
321 }
322 }
323
324 AbiTagList TagList;
325 if (const auto *AbiTag = ND->getAttr<AbiTagAttr>()) {
326 UsedAbiTags.insert(UsedAbiTags.end(), AbiTag->tags().begin(),
327 AbiTag->tags().end());
328 TagList.insert(TagList.end(), AbiTag->tags().begin(),
329 AbiTag->tags().end());
330 }
331
332 if (AdditionalAbiTags) {
333 UsedAbiTags.insert(UsedAbiTags.end(), AdditionalAbiTags->begin(),
334 AdditionalAbiTags->end());
335 TagList.insert(TagList.end(), AdditionalAbiTags->begin(),
336 AdditionalAbiTags->end());
337 }
338
339 llvm::sort(TagList);
340 TagList.erase(std::unique(TagList.begin(), TagList.end()), TagList.end());
341
342 writeSortedUniqueAbiTags(Out, AbiTags: TagList);
343 }
344
345 const AbiTagList &getUsedAbiTags() const { return UsedAbiTags; }
346 void setUsedAbiTags(const AbiTagList &AbiTags) {
347 UsedAbiTags = AbiTags;
348 }
349
350 const AbiTagList &getEmittedAbiTags() const {
351 return EmittedAbiTags;
352 }
353
354 const AbiTagList &getSortedUniqueUsedAbiTags() {
355 llvm::sort(UsedAbiTags);
356 UsedAbiTags.erase(std::unique(UsedAbiTags.begin(), UsedAbiTags.end()),
357 UsedAbiTags.end());
358 return UsedAbiTags;
359 }
360
361 private:
362 //! All abi tags used implicitly or explicitly.
363 AbiTagList UsedAbiTags;
364 //! All explicit abi tags (i.e. not from namespace).
365 AbiTagList EmittedAbiTags;
366
367 AbiTagState *&LinkHead;
368 AbiTagState *Parent = nullptr;
369
370 void pop() {
371 assert(LinkHead == this &&
372 "abi tag link head must point to us on destruction");
373 if (Parent) {
374 Parent->UsedAbiTags.insert(Parent->UsedAbiTags.end(),
375 UsedAbiTags.begin(), UsedAbiTags.end());
376 Parent->EmittedAbiTags.insert(Parent->EmittedAbiTags.end(),
377 EmittedAbiTags.begin(),
378 EmittedAbiTags.end());
379 }
380 LinkHead = Parent;
381 }
382
383 void writeSortedUniqueAbiTags(raw_ostream &Out, const AbiTagList &AbiTags) {
384 for (const auto &Tag : AbiTags) {
385 EmittedAbiTags.push_back(Elt: Tag);
386 Out << "B";
387 Out << Tag.size();
388 Out << Tag;
389 }
390 }
391 };
392
393 AbiTagState *AbiTags = nullptr;
394 AbiTagState AbiTagsRoot;
395
396 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
397 llvm::DenseMap<StringRef, unsigned> ModuleSubstitutions;
398
399 ASTContext &getASTContext() const { return Context.getASTContext(); }
400
401 bool isCompatibleWith(LangOptions::ClangABI Ver) {
402 return Context.getASTContext().getLangOpts().getClangABICompat() <= Ver;
403 }
404
405 bool isStd(const NamespaceDecl *NS);
406 bool isStdNamespace(const DeclContext *DC);
407
408 const RecordDecl *GetLocalClassDecl(const Decl *D);
409 bool isSpecializedAs(QualType S, llvm::StringRef Name, QualType A);
410 bool isStdCharSpecialization(const ClassTemplateSpecializationDecl *SD,
411 llvm::StringRef Name, bool HasAllocator);
412
413public:
414 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
415 const NamedDecl *D = nullptr, bool NullOut_ = false)
416 : Context(C), Out(Out_), NullOut(NullOut_), Structor(getStructor(decl: D)),
417 AbiTagsRoot(AbiTags) {
418 // These can't be mangled without a ctor type or dtor type.
419 assert(!D || (!isa<CXXDestructorDecl>(D) &&
420 !isa<CXXConstructorDecl>(D)));
421 }
422 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
423 const CXXConstructorDecl *D, CXXCtorType Type)
424 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
425 AbiTagsRoot(AbiTags) {}
426 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
427 const CXXDestructorDecl *D, CXXDtorType Type)
428 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
429 AbiTagsRoot(AbiTags) {}
430
431 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
432 bool NormalizeIntegers_)
433 : Context(C), Out(Out_), NormalizeIntegers(NormalizeIntegers_),
434 NullOut(false), Structor(nullptr), AbiTagsRoot(AbiTags) {}
435 CXXNameMangler(CXXNameMangler &Outer, raw_ostream &Out_)
436 : Context(Outer.Context), Out(Out_), Structor(Outer.Structor),
437 StructorType(Outer.StructorType), SeqID(Outer.SeqID),
438 FunctionTypeDepth(Outer.FunctionTypeDepth), AbiTagsRoot(AbiTags),
439 Substitutions(Outer.Substitutions),
440 ModuleSubstitutions(Outer.ModuleSubstitutions) {}
441
442 CXXNameMangler(CXXNameMangler &Outer, llvm::raw_null_ostream &Out_)
443 : CXXNameMangler(Outer, (raw_ostream &)Out_) {
444 NullOut = true;
445 }
446
447 struct WithTemplateDepthOffset { unsigned Offset; };
448 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out,
449 WithTemplateDepthOffset Offset)
450 : CXXNameMangler(C, Out) {
451 TemplateDepthOffset = Offset.Offset;
452 }
453
454 raw_ostream &getStream() { return Out; }
455
456 void disableDerivedAbiTags() { DisableDerivedAbiTags = true; }
457 static bool shouldHaveAbiTags(ItaniumMangleContextImpl &C, const VarDecl *VD);
458
459 void mangle(GlobalDecl GD);
460 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
461 void mangleNumber(const llvm::APSInt &I);
462 void mangleNumber(int64_t Number);
463 void mangleFloat(const llvm::APFloat &F);
464 void mangleFunctionEncoding(GlobalDecl GD);
465 void mangleSeqID(unsigned SeqID);
466 void mangleName(GlobalDecl GD);
467 void mangleType(QualType T);
468 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
469 void mangleLambdaSig(const CXXRecordDecl *Lambda);
470 void mangleModuleNamePrefix(StringRef Name, bool IsPartition = false);
471
472private:
473
474 bool mangleSubstitution(const NamedDecl *ND);
475 bool mangleSubstitution(NestedNameSpecifier *NNS);
476 bool mangleSubstitution(QualType T);
477 bool mangleSubstitution(TemplateName Template);
478 bool mangleSubstitution(uintptr_t Ptr);
479
480 void mangleExistingSubstitution(TemplateName name);
481
482 bool mangleStandardSubstitution(const NamedDecl *ND);
483
484 void addSubstitution(const NamedDecl *ND) {
485 ND = cast<NamedDecl>(ND->getCanonicalDecl());
486
487 addSubstitution(Ptr: reinterpret_cast<uintptr_t>(ND));
488 }
489 void addSubstitution(NestedNameSpecifier *NNS) {
490 NNS = Context.getASTContext().getCanonicalNestedNameSpecifier(NNS);
491
492 addSubstitution(Ptr: reinterpret_cast<uintptr_t>(NNS));
493 }
494 void addSubstitution(QualType T);
495 void addSubstitution(TemplateName Template);
496 void addSubstitution(uintptr_t Ptr);
497 // Destructive copy substitutions from other mangler.
498 void extendSubstitutions(CXXNameMangler* Other);
499
500 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
501 bool recursive = false);
502 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
503 DeclarationName name,
504 const TemplateArgumentLoc *TemplateArgs,
505 unsigned NumTemplateArgs,
506 unsigned KnownArity = UnknownArity);
507
508 void mangleFunctionEncodingBareType(const FunctionDecl *FD);
509
510 void mangleNameWithAbiTags(GlobalDecl GD,
511 const AbiTagList *AdditionalAbiTags);
512 void mangleModuleName(const NamedDecl *ND);
513 void mangleTemplateName(const TemplateDecl *TD,
514 ArrayRef<TemplateArgument> Args);
515 void mangleUnqualifiedName(GlobalDecl GD, const DeclContext *DC,
516 const AbiTagList *AdditionalAbiTags) {
517 mangleUnqualifiedName(GD, cast<NamedDecl>(GD.getDecl())->getDeclName(), DC,
518 UnknownArity, AdditionalAbiTags);
519 }
520 void mangleUnqualifiedName(GlobalDecl GD, DeclarationName Name,
521 const DeclContext *DC, unsigned KnownArity,
522 const AbiTagList *AdditionalAbiTags);
523 void mangleUnscopedName(GlobalDecl GD, const DeclContext *DC,
524 const AbiTagList *AdditionalAbiTags);
525 void mangleUnscopedTemplateName(GlobalDecl GD, const DeclContext *DC,
526 const AbiTagList *AdditionalAbiTags);
527 void mangleSourceName(const IdentifierInfo *II);
528 void mangleRegCallName(const IdentifierInfo *II);
529 void mangleDeviceStubName(const IdentifierInfo *II);
530 void mangleSourceNameWithAbiTags(
531 const NamedDecl *ND, const AbiTagList *AdditionalAbiTags = nullptr);
532 void mangleLocalName(GlobalDecl GD,
533 const AbiTagList *AdditionalAbiTags);
534 void mangleBlockForPrefix(const BlockDecl *Block);
535 void mangleUnqualifiedBlock(const BlockDecl *Block);
536 void mangleTemplateParamDecl(const NamedDecl *Decl);
537 void mangleTemplateParameterList(const TemplateParameterList *Params);
538 void mangleTypeConstraint(const ConceptDecl *Concept,
539 ArrayRef<TemplateArgument> Arguments);
540 void mangleTypeConstraint(const TypeConstraint *Constraint);
541 void mangleRequiresClause(const Expr *RequiresClause);
542 void mangleLambda(const CXXRecordDecl *Lambda);
543 void mangleNestedName(GlobalDecl GD, const DeclContext *DC,
544 const AbiTagList *AdditionalAbiTags,
545 bool NoFunction=false);
546 void mangleNestedName(const TemplateDecl *TD,
547 ArrayRef<TemplateArgument> Args);
548 void mangleNestedNameWithClosurePrefix(GlobalDecl GD,
549 const NamedDecl *PrefixND,
550 const AbiTagList *AdditionalAbiTags);
551 void manglePrefix(NestedNameSpecifier *qualifier);
552 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
553 void manglePrefix(QualType type);
554 void mangleTemplatePrefix(GlobalDecl GD, bool NoFunction=false);
555 void mangleTemplatePrefix(TemplateName Template);
556 const NamedDecl *getClosurePrefix(const Decl *ND);
557 void mangleClosurePrefix(const NamedDecl *ND, bool NoFunction = false);
558 bool mangleUnresolvedTypeOrSimpleId(QualType DestroyedType,
559 StringRef Prefix = "");
560 void mangleOperatorName(DeclarationName Name, unsigned Arity);
561 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
562 void mangleVendorQualifier(StringRef qualifier);
563 void mangleQualifiers(Qualifiers Quals, const DependentAddressSpaceType *DAST = nullptr);
564 void mangleRefQualifier(RefQualifierKind RefQualifier);
565
566 void mangleObjCMethodName(const ObjCMethodDecl *MD);
567
568 // Declare manglers for every type class.
569#define ABSTRACT_TYPE(CLASS, PARENT)
570#define NON_CANONICAL_TYPE(CLASS, PARENT)
571#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
572#include "clang/AST/TypeNodes.inc"
573
574 void mangleType(const TagType*);
575 void mangleType(TemplateName);
576 static StringRef getCallingConvQualifierName(CallingConv CC);
577 void mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo info);
578 void mangleExtFunctionInfo(const FunctionType *T);
579 void mangleBareFunctionType(const FunctionProtoType *T, bool MangleReturnType,
580 const FunctionDecl *FD = nullptr);
581 void mangleNeonVectorType(const VectorType *T);
582 void mangleNeonVectorType(const DependentVectorType *T);
583 void mangleAArch64NeonVectorType(const VectorType *T);
584 void mangleAArch64NeonVectorType(const DependentVectorType *T);
585 void mangleAArch64FixedSveVectorType(const VectorType *T);
586 void mangleAArch64FixedSveVectorType(const DependentVectorType *T);
587 void mangleRISCVFixedRVVVectorType(const VectorType *T);
588 void mangleRISCVFixedRVVVectorType(const DependentVectorType *T);
589
590 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
591 void mangleFloatLiteral(QualType T, const llvm::APFloat &V);
592 void mangleFixedPointLiteral();
593 void mangleNullPointer(QualType T);
594
595 void mangleMemberExprBase(const Expr *base, bool isArrow);
596 void mangleMemberExpr(const Expr *base, bool isArrow,
597 NestedNameSpecifier *qualifier,
598 NamedDecl *firstQualifierLookup,
599 DeclarationName name,
600 const TemplateArgumentLoc *TemplateArgs,
601 unsigned NumTemplateArgs,
602 unsigned knownArity);
603 void mangleCastExpression(const Expr *E, StringRef CastEncoding);
604 void mangleInitListElements(const InitListExpr *InitList);
605 void mangleRequirement(SourceLocation RequiresExprLoc,
606 const concepts::Requirement *Req);
607 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity,
608 bool AsTemplateArg = false);
609 void mangleCXXCtorType(CXXCtorType T, const CXXRecordDecl *InheritedFrom);
610 void mangleCXXDtorType(CXXDtorType T);
611
612 struct TemplateArgManglingInfo;
613 void mangleTemplateArgs(TemplateName TN,
614 const TemplateArgumentLoc *TemplateArgs,
615 unsigned NumTemplateArgs);
616 void mangleTemplateArgs(TemplateName TN, ArrayRef<TemplateArgument> Args);
617 void mangleTemplateArgs(TemplateName TN, const TemplateArgumentList &AL);
618 void mangleTemplateArg(TemplateArgManglingInfo &Info, unsigned Index,
619 TemplateArgument A);
620 void mangleTemplateArg(TemplateArgument A, bool NeedExactType);
621 void mangleTemplateArgExpr(const Expr *E);
622 void mangleValueInTemplateArg(QualType T, const APValue &V, bool TopLevel,
623 bool NeedExactType = false);
624
625 void mangleTemplateParameter(unsigned Depth, unsigned Index);
626
627 void mangleFunctionParam(const ParmVarDecl *parm);
628
629 void writeAbiTags(const NamedDecl *ND,
630 const AbiTagList *AdditionalAbiTags);
631
632 // Returns sorted unique list of ABI tags.
633 AbiTagList makeFunctionReturnTypeTags(const FunctionDecl *FD);
634 // Returns sorted unique list of ABI tags.
635 AbiTagList makeVariableTypeTags(const VarDecl *VD);
636};
637
638}
639
640NamespaceDecl *ItaniumMangleContextImpl::getStdNamespace() {
641 if (!StdNamespace) {
642 StdNamespace = NamespaceDecl::Create(
643 getASTContext(), getASTContext().getTranslationUnitDecl(),
644 /*Inline=*/false, SourceLocation(), SourceLocation(),
645 &getASTContext().Idents.get(Name: "std"),
646 /*PrevDecl=*/nullptr, /*Nested=*/false);
647 StdNamespace->setImplicit();
648 }
649 return StdNamespace;
650}
651
652/// Retrieve the declaration context that should be used when mangling the given
653/// declaration.
654const DeclContext *
655ItaniumMangleContextImpl::getEffectiveDeclContext(const Decl *D) {
656 // The ABI assumes that lambda closure types that occur within
657 // default arguments live in the context of the function. However, due to
658 // the way in which Clang parses and creates function declarations, this is
659 // not the case: the lambda closure type ends up living in the context
660 // where the function itself resides, because the function declaration itself
661 // had not yet been created. Fix the context here.
662 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Val: D)) {
663 if (RD->isLambda())
664 if (ParmVarDecl *ContextParam =
665 dyn_cast_or_null<ParmVarDecl>(Val: RD->getLambdaContextDecl()))
666 return ContextParam->getDeclContext();
667 }
668
669 // Perform the same check for block literals.
670 if (const BlockDecl *BD = dyn_cast<BlockDecl>(Val: D)) {
671 if (ParmVarDecl *ContextParam =
672 dyn_cast_or_null<ParmVarDecl>(Val: BD->getBlockManglingContextDecl()))
673 return ContextParam->getDeclContext();
674 }
675
676 // On ARM and AArch64, the va_list tag is always mangled as if in the std
677 // namespace. We do not represent va_list as actually being in the std
678 // namespace in C because this would result in incorrect debug info in C,
679 // among other things. It is important for both languages to have the same
680 // mangling in order for -fsanitize=cfi-icall to work.
681 if (D == getASTContext().getVaListTagDecl()) {
682 const llvm::Triple &T = getASTContext().getTargetInfo().getTriple();
683 if (T.isARM() || T.isThumb() || T.isAArch64())
684 return getStdNamespace();
685 }
686
687 const DeclContext *DC = D->getDeclContext();
688 if (isa<CapturedDecl>(Val: DC) || isa<OMPDeclareReductionDecl>(Val: DC) ||
689 isa<OMPDeclareMapperDecl>(Val: DC)) {
690 return getEffectiveDeclContext(D: cast<Decl>(Val: DC));
691 }
692
693 if (const auto *VD = dyn_cast<VarDecl>(Val: D))
694 if (VD->isExternC())
695 return getASTContext().getTranslationUnitDecl();
696
697 if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
698 if (FD->isExternC())
699 return getASTContext().getTranslationUnitDecl();
700 // Member-like constrained friends are mangled as if they were members of
701 // the enclosing class.
702 if (FD->isMemberLikeConstrainedFriend() &&
703 getASTContext().getLangOpts().getClangABICompat() >
704 LangOptions::ClangABI::Ver17)
705 return D->getLexicalDeclContext()->getRedeclContext();
706 }
707
708 return DC->getRedeclContext();
709}
710
711bool ItaniumMangleContextImpl::isInternalLinkageDecl(const NamedDecl *ND) {
712 if (ND && ND->getFormalLinkage() == Linkage::Internal &&
713 !ND->isExternallyVisible() &&
714 getEffectiveDeclContext(ND)->isFileContext() &&
715 !ND->isInAnonymousNamespace())
716 return true;
717 return false;
718}
719
720// Check if this Function Decl needs a unique internal linkage name.
721bool ItaniumMangleContextImpl::isUniqueInternalLinkageDecl(
722 const NamedDecl *ND) {
723 if (!NeedsUniqueInternalLinkageNames || !ND)
724 return false;
725
726 const auto *FD = dyn_cast<FunctionDecl>(Val: ND);
727 if (!FD)
728 return false;
729
730 // For C functions without prototypes, return false as their
731 // names should not be mangled.
732 if (!FD->getType()->getAs<FunctionProtoType>())
733 return false;
734
735 if (isInternalLinkageDecl(ND))
736 return true;
737
738 return false;
739}
740
741bool ItaniumMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
742 if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
743 LanguageLinkage L = FD->getLanguageLinkage();
744 // Overloadable functions need mangling.
745 if (FD->hasAttr<OverloadableAttr>())
746 return true;
747
748 // "main" is not mangled.
749 if (FD->isMain())
750 return false;
751
752 // The Windows ABI expects that we would never mangle "typical"
753 // user-defined entry points regardless of visibility or freestanding-ness.
754 //
755 // N.B. This is distinct from asking about "main". "main" has a lot of
756 // special rules associated with it in the standard while these
757 // user-defined entry points are outside of the purview of the standard.
758 // For example, there can be only one definition for "main" in a standards
759 // compliant program; however nothing forbids the existence of wmain and
760 // WinMain in the same translation unit.
761 if (FD->isMSVCRTEntryPoint())
762 return false;
763
764 // C++ functions and those whose names are not a simple identifier need
765 // mangling.
766 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
767 return true;
768
769 // C functions are not mangled.
770 if (L == CLanguageLinkage)
771 return false;
772 }
773
774 // Otherwise, no mangling is done outside C++ mode.
775 if (!getASTContext().getLangOpts().CPlusPlus)
776 return false;
777
778 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
779 // Decompositions are mangled.
780 if (isa<DecompositionDecl>(Val: VD))
781 return true;
782
783 // C variables are not mangled.
784 if (VD->isExternC())
785 return false;
786
787 // Variables at global scope are not mangled unless they have internal
788 // linkage or are specializations or are attached to a named module.
789 const DeclContext *DC = getEffectiveDeclContext(D);
790 // Check for extern variable declared locally.
791 if (DC->isFunctionOrMethod() && D->hasLinkage())
792 while (!DC->isFileContext())
793 DC = getEffectiveParentContext(DC);
794 if (DC->isTranslationUnit() && D->getFormalLinkage() != Linkage::Internal &&
795 !CXXNameMangler::shouldHaveAbiTags(C&: *this, VD) &&
796 !isa<VarTemplateSpecializationDecl>(Val: VD) &&
797 !VD->getOwningModuleForLinkage())
798 return false;
799 }
800
801 return true;
802}
803
804void CXXNameMangler::writeAbiTags(const NamedDecl *ND,
805 const AbiTagList *AdditionalAbiTags) {
806 assert(AbiTags && "require AbiTagState");
807 AbiTags->write(Out, ND, AdditionalAbiTags: DisableDerivedAbiTags ? nullptr : AdditionalAbiTags);
808}
809
810void CXXNameMangler::mangleSourceNameWithAbiTags(
811 const NamedDecl *ND, const AbiTagList *AdditionalAbiTags) {
812 mangleSourceName(II: ND->getIdentifier());
813 writeAbiTags(ND, AdditionalAbiTags);
814}
815
816void CXXNameMangler::mangle(GlobalDecl GD) {
817 // <mangled-name> ::= _Z <encoding>
818 // ::= <data name>
819 // ::= <special-name>
820 Out << "_Z";
821 if (isa<FunctionDecl>(Val: GD.getDecl()))
822 mangleFunctionEncoding(GD);
823 else if (isa<VarDecl, FieldDecl, MSGuidDecl, TemplateParamObjectDecl,
824 BindingDecl>(Val: GD.getDecl()))
825 mangleName(GD);
826 else if (const IndirectFieldDecl *IFD =
827 dyn_cast<IndirectFieldDecl>(Val: GD.getDecl()))
828 mangleName(IFD->getAnonField());
829 else
830 llvm_unreachable("unexpected kind of global decl");
831}
832
833void CXXNameMangler::mangleFunctionEncoding(GlobalDecl GD) {
834 const FunctionDecl *FD = cast<FunctionDecl>(Val: GD.getDecl());
835 // <encoding> ::= <function name> <bare-function-type>
836
837 // Don't mangle in the type if this isn't a decl we should typically mangle.
838 if (!Context.shouldMangleDeclName(FD)) {
839 mangleName(GD);
840 return;
841 }
842
843 AbiTagList ReturnTypeAbiTags = makeFunctionReturnTypeTags(FD);
844 if (ReturnTypeAbiTags.empty()) {
845 // There are no tags for return type, the simplest case. Enter the function
846 // parameter scope before mangling the name, because a template using
847 // constrained `auto` can have references to its parameters within its
848 // template argument list:
849 //
850 // template<typename T> void f(T x, C<decltype(x)> auto)
851 // ... is mangled as ...
852 // template<typename T, C<decltype(param 1)> U> void f(T, U)
853 FunctionTypeDepthState Saved = FunctionTypeDepth.push();
854 mangleName(GD);
855 FunctionTypeDepth.pop(saved: Saved);
856 mangleFunctionEncodingBareType(FD);
857 return;
858 }
859
860 // Mangle function name and encoding to temporary buffer.
861 // We have to output name and encoding to the same mangler to get the same
862 // substitution as it will be in final mangling.
863 SmallString<256> FunctionEncodingBuf;
864 llvm::raw_svector_ostream FunctionEncodingStream(FunctionEncodingBuf);
865 CXXNameMangler FunctionEncodingMangler(*this, FunctionEncodingStream);
866 // Output name of the function.
867 FunctionEncodingMangler.disableDerivedAbiTags();
868
869 FunctionTypeDepthState Saved = FunctionTypeDepth.push();
870 FunctionEncodingMangler.mangleNameWithAbiTags(GD: FD, AdditionalAbiTags: nullptr);
871 FunctionTypeDepth.pop(saved: Saved);
872
873 // Remember length of the function name in the buffer.
874 size_t EncodingPositionStart = FunctionEncodingStream.str().size();
875 FunctionEncodingMangler.mangleFunctionEncodingBareType(FD);
876
877 // Get tags from return type that are not present in function name or
878 // encoding.
879 const AbiTagList &UsedAbiTags =
880 FunctionEncodingMangler.AbiTagsRoot.getSortedUniqueUsedAbiTags();
881 AbiTagList AdditionalAbiTags(ReturnTypeAbiTags.size());
882 AdditionalAbiTags.erase(
883 CS: std::set_difference(first1: ReturnTypeAbiTags.begin(), last1: ReturnTypeAbiTags.end(),
884 first2: UsedAbiTags.begin(), last2: UsedAbiTags.end(),
885 result: AdditionalAbiTags.begin()),
886 CE: AdditionalAbiTags.end());
887
888 // Output name with implicit tags and function encoding from temporary buffer.
889 Saved = FunctionTypeDepth.push();
890 mangleNameWithAbiTags(GD: FD, AdditionalAbiTags: &AdditionalAbiTags);
891 FunctionTypeDepth.pop(saved: Saved);
892 Out << FunctionEncodingStream.str().substr(Start: EncodingPositionStart);
893
894 // Function encoding could create new substitutions so we have to add
895 // temp mangled substitutions to main mangler.
896 extendSubstitutions(Other: &FunctionEncodingMangler);
897}
898
899void CXXNameMangler::mangleFunctionEncodingBareType(const FunctionDecl *FD) {
900 if (FD->hasAttr<EnableIfAttr>()) {
901 FunctionTypeDepthState Saved = FunctionTypeDepth.push();
902 Out << "Ua9enable_ifI";
903 for (AttrVec::const_iterator I = FD->getAttrs().begin(),
904 E = FD->getAttrs().end();
905 I != E; ++I) {
906 EnableIfAttr *EIA = dyn_cast<EnableIfAttr>(*I);
907 if (!EIA)
908 continue;
909 if (isCompatibleWith(Ver: LangOptions::ClangABI::Ver11)) {
910 // Prior to Clang 12, we hardcoded the X/E around enable-if's argument,
911 // even though <template-arg> should not include an X/E around
912 // <expr-primary>.
913 Out << 'X';
914 mangleExpression(E: EIA->getCond());
915 Out << 'E';
916 } else {
917 mangleTemplateArgExpr(E: EIA->getCond());
918 }
919 }
920 Out << 'E';
921 FunctionTypeDepth.pop(saved: Saved);
922 }
923
924 // When mangling an inheriting constructor, the bare function type used is
925 // that of the inherited constructor.
926 if (auto *CD = dyn_cast<CXXConstructorDecl>(Val: FD))
927 if (auto Inherited = CD->getInheritedConstructor())
928 FD = Inherited.getConstructor();
929
930 // Whether the mangling of a function type includes the return type depends on
931 // the context and the nature of the function. The rules for deciding whether
932 // the return type is included are:
933 //
934 // 1. Template functions (names or types) have return types encoded, with
935 // the exceptions listed below.
936 // 2. Function types not appearing as part of a function name mangling,
937 // e.g. parameters, pointer types, etc., have return type encoded, with the
938 // exceptions listed below.
939 // 3. Non-template function names do not have return types encoded.
940 //
941 // The exceptions mentioned in (1) and (2) above, for which the return type is
942 // never included, are
943 // 1. Constructors.
944 // 2. Destructors.
945 // 3. Conversion operator functions, e.g. operator int.
946 bool MangleReturnType = false;
947 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
948 if (!(isa<CXXConstructorDecl>(Val: FD) || isa<CXXDestructorDecl>(Val: FD) ||
949 isa<CXXConversionDecl>(Val: FD)))
950 MangleReturnType = true;
951
952 // Mangle the type of the primary template.
953 FD = PrimaryTemplate->getTemplatedDecl();
954 }
955
956 mangleBareFunctionType(T: FD->getType()->castAs<FunctionProtoType>(),
957 MangleReturnType, FD);
958}
959
960/// Return whether a given namespace is the 'std' namespace.
961bool CXXNameMangler::isStd(const NamespaceDecl *NS) {
962 if (!Context.getEffectiveParentContext(NS)->isTranslationUnit())
963 return false;
964
965 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
966 return II && II->isStr(Str: "std");
967}
968
969// isStdNamespace - Return whether a given decl context is a toplevel 'std'
970// namespace.
971bool CXXNameMangler::isStdNamespace(const DeclContext *DC) {
972 if (!DC->isNamespace())
973 return false;
974
975 return isStd(NS: cast<NamespaceDecl>(Val: DC));
976}
977
978static const GlobalDecl
979isTemplate(GlobalDecl GD, const TemplateArgumentList *&TemplateArgs) {
980 const NamedDecl *ND = cast<NamedDecl>(Val: GD.getDecl());
981 // Check if we have a function template.
982 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: ND)) {
983 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
984 TemplateArgs = FD->getTemplateSpecializationArgs();
985 return GD.getWithDecl(TD);
986 }
987 }
988
989 // Check if we have a class template.
990 if (const ClassTemplateSpecializationDecl *Spec =
991 dyn_cast<ClassTemplateSpecializationDecl>(Val: ND)) {
992 TemplateArgs = &Spec->getTemplateArgs();
993 return GD.getWithDecl(Spec->getSpecializedTemplate());
994 }
995
996 // Check if we have a variable template.
997 if (const VarTemplateSpecializationDecl *Spec =
998 dyn_cast<VarTemplateSpecializationDecl>(Val: ND)) {
999 TemplateArgs = &Spec->getTemplateArgs();
1000 return GD.getWithDecl(Spec->getSpecializedTemplate());
1001 }
1002
1003 return GlobalDecl();
1004}
1005
1006static TemplateName asTemplateName(GlobalDecl GD) {
1007 const TemplateDecl *TD = dyn_cast_or_null<TemplateDecl>(Val: GD.getDecl());
1008 return TemplateName(const_cast<TemplateDecl*>(TD));
1009}
1010
1011void CXXNameMangler::mangleName(GlobalDecl GD) {
1012 const NamedDecl *ND = cast<NamedDecl>(Val: GD.getDecl());
1013 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: ND)) {
1014 // Variables should have implicit tags from its type.
1015 AbiTagList VariableTypeAbiTags = makeVariableTypeTags(VD);
1016 if (VariableTypeAbiTags.empty()) {
1017 // Simple case no variable type tags.
1018 mangleNameWithAbiTags(GD: VD, AdditionalAbiTags: nullptr);
1019 return;
1020 }
1021
1022 // Mangle variable name to null stream to collect tags.
1023 llvm::raw_null_ostream NullOutStream;
1024 CXXNameMangler VariableNameMangler(*this, NullOutStream);
1025 VariableNameMangler.disableDerivedAbiTags();
1026 VariableNameMangler.mangleNameWithAbiTags(GD: VD, AdditionalAbiTags: nullptr);
1027
1028 // Get tags from variable type that are not present in its name.
1029 const AbiTagList &UsedAbiTags =
1030 VariableNameMangler.AbiTagsRoot.getSortedUniqueUsedAbiTags();
1031 AbiTagList AdditionalAbiTags(VariableTypeAbiTags.size());
1032 AdditionalAbiTags.erase(
1033 CS: std::set_difference(first1: VariableTypeAbiTags.begin(),
1034 last1: VariableTypeAbiTags.end(), first2: UsedAbiTags.begin(),
1035 last2: UsedAbiTags.end(), result: AdditionalAbiTags.begin()),
1036 CE: AdditionalAbiTags.end());
1037
1038 // Output name with implicit tags.
1039 mangleNameWithAbiTags(GD: VD, AdditionalAbiTags: &AdditionalAbiTags);
1040 } else {
1041 mangleNameWithAbiTags(GD, AdditionalAbiTags: nullptr);
1042 }
1043}
1044
1045const RecordDecl *CXXNameMangler::GetLocalClassDecl(const Decl *D) {
1046 const DeclContext *DC = Context.getEffectiveDeclContext(D);
1047 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
1048 if (isLocalContainerContext(DC))
1049 return dyn_cast<RecordDecl>(Val: D);
1050 D = cast<Decl>(Val: DC);
1051 DC = Context.getEffectiveDeclContext(D);
1052 }
1053 return nullptr;
1054}
1055
1056void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
1057 const AbiTagList *AdditionalAbiTags) {
1058 const NamedDecl *ND = cast<NamedDecl>(Val: GD.getDecl());
1059 // <name> ::= [<module-name>] <nested-name>
1060 // ::= [<module-name>] <unscoped-name>
1061 // ::= [<module-name>] <unscoped-template-name> <template-args>
1062 // ::= <local-name>
1063 //
1064 const DeclContext *DC = Context.getEffectiveDeclContext(ND);
1065 bool IsLambda = isLambda(ND);
1066
1067 // If this is an extern variable declared locally, the relevant DeclContext
1068 // is that of the containing namespace, or the translation unit.
1069 // FIXME: This is a hack; extern variables declared locally should have
1070 // a proper semantic declaration context!
1071 if (isLocalContainerContext(DC) && ND->hasLinkage() && !IsLambda)
1072 while (!DC->isNamespace() && !DC->isTranslationUnit())
1073 DC = Context.getEffectiveParentContext(DC);
1074 else if (GetLocalClassDecl(ND) &&
1075 (!IsLambda || isCompatibleWith(Ver: LangOptions::ClangABI::Ver18))) {
1076 mangleLocalName(GD, AdditionalAbiTags);
1077 return;
1078 }
1079
1080 assert(!isa<LinkageSpecDecl>(DC) && "context cannot be LinkageSpecDecl");
1081
1082 // Closures can require a nested-name mangling even if they're semantically
1083 // in the global namespace.
1084 if (const NamedDecl *PrefixND = getClosurePrefix(ND)) {
1085 mangleNestedNameWithClosurePrefix(GD, PrefixND, AdditionalAbiTags);
1086 return;
1087 }
1088
1089 if (isLocalContainerContext(DC)) {
1090 mangleLocalName(GD, AdditionalAbiTags);
1091 return;
1092 }
1093
1094 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
1095 // Check if we have a template.
1096 const TemplateArgumentList *TemplateArgs = nullptr;
1097 if (GlobalDecl TD = isTemplate(GD, TemplateArgs)) {
1098 mangleUnscopedTemplateName(GD: TD, DC, AdditionalAbiTags);
1099 mangleTemplateArgs(TN: asTemplateName(GD: TD), AL: *TemplateArgs);
1100 return;
1101 }
1102
1103 mangleUnscopedName(GD, DC, AdditionalAbiTags);
1104 return;
1105 }
1106
1107 mangleNestedName(GD, DC, AdditionalAbiTags);
1108}
1109
1110void CXXNameMangler::mangleModuleName(const NamedDecl *ND) {
1111 if (ND->isExternallyVisible())
1112 if (Module *M = ND->getOwningModuleForLinkage())
1113 mangleModuleNamePrefix(Name: M->getPrimaryModuleInterfaceName());
1114}
1115
1116// <module-name> ::= <module-subname>
1117// ::= <module-name> <module-subname>
1118// ::= <substitution>
1119// <module-subname> ::= W <source-name>
1120// ::= W P <source-name>
1121void CXXNameMangler::mangleModuleNamePrefix(StringRef Name, bool IsPartition) {
1122 // <substitution> ::= S <seq-id> _
1123 auto It = ModuleSubstitutions.find(Val: Name);
1124 if (It != ModuleSubstitutions.end()) {
1125 Out << 'S';
1126 mangleSeqID(SeqID: It->second);
1127 return;
1128 }
1129
1130 // FIXME: Preserve hierarchy in module names rather than flattening
1131 // them to strings; use Module*s as substitution keys.
1132 auto Parts = Name.rsplit(Separator: '.');
1133 if (Parts.second.empty())
1134 Parts.second = Parts.first;
1135 else {
1136 mangleModuleNamePrefix(Name: Parts.first, IsPartition);
1137 IsPartition = false;
1138 }
1139
1140 Out << 'W';
1141 if (IsPartition)
1142 Out << 'P';
1143 Out << Parts.second.size() << Parts.second;
1144 ModuleSubstitutions.insert(KV: {Name, SeqID++});
1145}
1146
1147void CXXNameMangler::mangleTemplateName(const TemplateDecl *TD,
1148 ArrayRef<TemplateArgument> Args) {
1149 const DeclContext *DC = Context.getEffectiveDeclContext(TD);
1150
1151 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
1152 mangleUnscopedTemplateName(TD, DC, nullptr);
1153 mangleTemplateArgs(TN: asTemplateName(TD), Args);
1154 } else {
1155 mangleNestedName(TD, Args);
1156 }
1157}
1158
1159void CXXNameMangler::mangleUnscopedName(GlobalDecl GD, const DeclContext *DC,
1160 const AbiTagList *AdditionalAbiTags) {
1161 // <unscoped-name> ::= <unqualified-name>
1162 // ::= St <unqualified-name> # ::std::
1163
1164 assert(!isa<LinkageSpecDecl>(DC) && "unskipped LinkageSpecDecl");
1165 if (isStdNamespace(DC))
1166 Out << "St";
1167
1168 mangleUnqualifiedName(GD, DC, AdditionalAbiTags);
1169}
1170
1171void CXXNameMangler::mangleUnscopedTemplateName(
1172 GlobalDecl GD, const DeclContext *DC, const AbiTagList *AdditionalAbiTags) {
1173 const TemplateDecl *ND = cast<TemplateDecl>(Val: GD.getDecl());
1174 // <unscoped-template-name> ::= <unscoped-name>
1175 // ::= <substitution>
1176 if (mangleSubstitution(ND))
1177 return;
1178
1179 // <template-template-param> ::= <template-param>
1180 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: ND)) {
1181 assert(!AdditionalAbiTags &&
1182 "template template param cannot have abi tags");
1183 mangleTemplateParameter(Depth: TTP->getDepth(), Index: TTP->getIndex());
1184 } else if (isa<BuiltinTemplateDecl>(Val: ND) || isa<ConceptDecl>(Val: ND)) {
1185 mangleUnscopedName(GD, DC, AdditionalAbiTags);
1186 } else {
1187 mangleUnscopedName(GD: GD.getWithDecl(ND->getTemplatedDecl()), DC,
1188 AdditionalAbiTags);
1189 }
1190
1191 addSubstitution(ND);
1192}
1193
1194void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
1195 // ABI:
1196 // Floating-point literals are encoded using a fixed-length
1197 // lowercase hexadecimal string corresponding to the internal
1198 // representation (IEEE on Itanium), high-order bytes first,
1199 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
1200 // on Itanium.
1201 // The 'without leading zeroes' thing seems to be an editorial
1202 // mistake; see the discussion on cxx-abi-dev beginning on
1203 // 2012-01-16.
1204
1205 // Our requirements here are just barely weird enough to justify
1206 // using a custom algorithm instead of post-processing APInt::toString().
1207
1208 llvm::APInt valueBits = f.bitcastToAPInt();
1209 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
1210 assert(numCharacters != 0);
1211
1212 // Allocate a buffer of the right number of characters.
1213 SmallVector<char, 20> buffer(numCharacters);
1214
1215 // Fill the buffer left-to-right.
1216 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
1217 // The bit-index of the next hex digit.
1218 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
1219
1220 // Project out 4 bits starting at 'digitIndex'.
1221 uint64_t hexDigit = valueBits.getRawData()[digitBitIndex / 64];
1222 hexDigit >>= (digitBitIndex % 64);
1223 hexDigit &= 0xF;
1224
1225 // Map that over to a lowercase hex digit.
1226 static const char charForHex[16] = {
1227 '0', '1', '2', '3', '4', '5', '6', '7',
1228 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1229 };
1230 buffer[stringIndex] = charForHex[hexDigit];
1231 }
1232
1233 Out.write(Ptr: buffer.data(), Size: numCharacters);
1234}
1235
1236void CXXNameMangler::mangleFloatLiteral(QualType T, const llvm::APFloat &V) {
1237 Out << 'L';
1238 mangleType(T);
1239 mangleFloat(f: V);
1240 Out << 'E';
1241}
1242
1243void CXXNameMangler::mangleFixedPointLiteral() {
1244 DiagnosticsEngine &Diags = Context.getDiags();
1245 unsigned DiagID = Diags.getCustomDiagID(
1246 L: DiagnosticsEngine::Error, FormatString: "cannot mangle fixed point literals yet");
1247 Diags.Report(DiagID);
1248}
1249
1250void CXXNameMangler::mangleNullPointer(QualType T) {
1251 // <expr-primary> ::= L <type> 0 E
1252 Out << 'L';
1253 mangleType(T);
1254 Out << "0E";
1255}
1256
1257void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
1258 if (Value.isSigned() && Value.isNegative()) {
1259 Out << 'n';
1260 Value.abs().print(OS&: Out, /*signed*/ isSigned: false);
1261 } else {
1262 Value.print(OS&: Out, /*signed*/ isSigned: false);
1263 }
1264}
1265
1266void CXXNameMangler::mangleNumber(int64_t Number) {
1267 // <number> ::= [n] <non-negative decimal integer>
1268 if (Number < 0) {
1269 Out << 'n';
1270 Number = -Number;
1271 }
1272
1273 Out << Number;
1274}
1275
1276void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
1277 // <call-offset> ::= h <nv-offset> _
1278 // ::= v <v-offset> _
1279 // <nv-offset> ::= <offset number> # non-virtual base override
1280 // <v-offset> ::= <offset number> _ <virtual offset number>
1281 // # virtual base override, with vcall offset
1282 if (!Virtual) {
1283 Out << 'h';
1284 mangleNumber(Number: NonVirtual);
1285 Out << '_';
1286 return;
1287 }
1288
1289 Out << 'v';
1290 mangleNumber(Number: NonVirtual);
1291 Out << '_';
1292 mangleNumber(Number: Virtual);
1293 Out << '_';
1294}
1295
1296void CXXNameMangler::manglePrefix(QualType type) {
1297 if (const auto *TST = type->getAs<TemplateSpecializationType>()) {
1298 if (!mangleSubstitution(T: QualType(TST, 0))) {
1299 mangleTemplatePrefix(Template: TST->getTemplateName());
1300
1301 // FIXME: GCC does not appear to mangle the template arguments when
1302 // the template in question is a dependent template name. Should we
1303 // emulate that badness?
1304 mangleTemplateArgs(TN: TST->getTemplateName(), Args: TST->template_arguments());
1305 addSubstitution(T: QualType(TST, 0));
1306 }
1307 } else if (const auto *DTST =
1308 type->getAs<DependentTemplateSpecializationType>()) {
1309 if (!mangleSubstitution(T: QualType(DTST, 0))) {
1310 TemplateName Template = getASTContext().getDependentTemplateName(
1311 NNS: DTST->getQualifier(), Name: DTST->getIdentifier());
1312 mangleTemplatePrefix(Template);
1313
1314 // FIXME: GCC does not appear to mangle the template arguments when
1315 // the template in question is a dependent template name. Should we
1316 // emulate that badness?
1317 mangleTemplateArgs(TN: Template, Args: DTST->template_arguments());
1318 addSubstitution(T: QualType(DTST, 0));
1319 }
1320 } else {
1321 // We use the QualType mangle type variant here because it handles
1322 // substitutions.
1323 mangleType(T: type);
1324 }
1325}
1326
1327/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
1328///
1329/// \param recursive - true if this is being called recursively,
1330/// i.e. if there is more prefix "to the right".
1331void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
1332 bool recursive) {
1333
1334 // x, ::x
1335 // <unresolved-name> ::= [gs] <base-unresolved-name>
1336
1337 // T::x / decltype(p)::x
1338 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
1339
1340 // T::N::x /decltype(p)::N::x
1341 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
1342 // <base-unresolved-name>
1343
1344 // A::x, N::y, A<T>::z; "gs" means leading "::"
1345 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
1346 // <base-unresolved-name>
1347
1348 switch (qualifier->getKind()) {
1349 case NestedNameSpecifier::Global:
1350 Out << "gs";
1351
1352 // We want an 'sr' unless this is the entire NNS.
1353 if (recursive)
1354 Out << "sr";
1355
1356 // We never want an 'E' here.
1357 return;
1358
1359 case NestedNameSpecifier::Super:
1360 llvm_unreachable("Can't mangle __super specifier");
1361
1362 case NestedNameSpecifier::Namespace:
1363 if (qualifier->getPrefix())
1364 mangleUnresolvedPrefix(qualifier: qualifier->getPrefix(),
1365 /*recursive*/ true);
1366 else
1367 Out << "sr";
1368 mangleSourceNameWithAbiTags(qualifier->getAsNamespace());
1369 break;
1370 case NestedNameSpecifier::NamespaceAlias:
1371 if (qualifier->getPrefix())
1372 mangleUnresolvedPrefix(qualifier: qualifier->getPrefix(),
1373 /*recursive*/ true);
1374 else
1375 Out << "sr";
1376 mangleSourceNameWithAbiTags(qualifier->getAsNamespaceAlias());
1377 break;
1378
1379 case NestedNameSpecifier::TypeSpec:
1380 case NestedNameSpecifier::TypeSpecWithTemplate: {
1381 const Type *type = qualifier->getAsType();
1382
1383 // We only want to use an unresolved-type encoding if this is one of:
1384 // - a decltype
1385 // - a template type parameter
1386 // - a template template parameter with arguments
1387 // In all of these cases, we should have no prefix.
1388 if (qualifier->getPrefix()) {
1389 mangleUnresolvedPrefix(qualifier: qualifier->getPrefix(),
1390 /*recursive*/ true);
1391 } else {
1392 // Otherwise, all the cases want this.
1393 Out << "sr";
1394 }
1395
1396 if (mangleUnresolvedTypeOrSimpleId(DestroyedType: QualType(type, 0), Prefix: recursive ? "N" : ""))
1397 return;
1398
1399 break;
1400 }
1401
1402 case NestedNameSpecifier::Identifier:
1403 // Member expressions can have these without prefixes.
1404 if (qualifier->getPrefix())
1405 mangleUnresolvedPrefix(qualifier: qualifier->getPrefix(),
1406 /*recursive*/ true);
1407 else
1408 Out << "sr";
1409
1410 mangleSourceName(II: qualifier->getAsIdentifier());
1411 // An Identifier has no type information, so we can't emit abi tags for it.
1412 break;
1413 }
1414
1415 // If this was the innermost part of the NNS, and we fell out to
1416 // here, append an 'E'.
1417 if (!recursive)
1418 Out << 'E';
1419}
1420
1421/// Mangle an unresolved-name, which is generally used for names which
1422/// weren't resolved to specific entities.
1423void CXXNameMangler::mangleUnresolvedName(
1424 NestedNameSpecifier *qualifier, DeclarationName name,
1425 const TemplateArgumentLoc *TemplateArgs, unsigned NumTemplateArgs,
1426 unsigned knownArity) {
1427 if (qualifier) mangleUnresolvedPrefix(qualifier);
1428 switch (name.getNameKind()) {
1429 // <base-unresolved-name> ::= <simple-id>
1430 case DeclarationName::Identifier:
1431 mangleSourceName(II: name.getAsIdentifierInfo());
1432 break;
1433 // <base-unresolved-name> ::= dn <destructor-name>
1434 case DeclarationName::CXXDestructorName:
1435 Out << "dn";
1436 mangleUnresolvedTypeOrSimpleId(DestroyedType: name.getCXXNameType());
1437 break;
1438 // <base-unresolved-name> ::= on <operator-name>
1439 case DeclarationName::CXXConversionFunctionName:
1440 case DeclarationName::CXXLiteralOperatorName:
1441 case DeclarationName::CXXOperatorName:
1442 Out << "on";
1443 mangleOperatorName(Name: name, Arity: knownArity);
1444 break;
1445 case DeclarationName::CXXConstructorName:
1446 llvm_unreachable("Can't mangle a constructor name!");
1447 case DeclarationName::CXXUsingDirective:
1448 llvm_unreachable("Can't mangle a using directive name!");
1449 case DeclarationName::CXXDeductionGuideName:
1450 llvm_unreachable("Can't mangle a deduction guide name!");
1451 case DeclarationName::ObjCMultiArgSelector:
1452 case DeclarationName::ObjCOneArgSelector:
1453 case DeclarationName::ObjCZeroArgSelector:
1454 llvm_unreachable("Can't mangle Objective-C selector names here!");
1455 }
1456
1457 // The <simple-id> and on <operator-name> productions end in an optional
1458 // <template-args>.
1459 if (TemplateArgs)
1460 mangleTemplateArgs(TN: TemplateName(), TemplateArgs, NumTemplateArgs);
1461}
1462
1463void CXXNameMangler::mangleUnqualifiedName(
1464 GlobalDecl GD, DeclarationName Name, const DeclContext *DC,
1465 unsigned KnownArity, const AbiTagList *AdditionalAbiTags) {
1466 const NamedDecl *ND = cast_or_null<NamedDecl>(Val: GD.getDecl());
1467 // <unqualified-name> ::= [<module-name>] [F] <operator-name>
1468 // ::= <ctor-dtor-name>
1469 // ::= [<module-name>] [F] <source-name>
1470 // ::= [<module-name>] DC <source-name>* E
1471
1472 if (ND && DC && DC->isFileContext())
1473 mangleModuleName(ND);
1474
1475 // A member-like constrained friend is mangled with a leading 'F'.
1476 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/24.
1477 auto *FD = dyn_cast<FunctionDecl>(Val: ND);
1478 auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ND);
1479 if ((FD && FD->isMemberLikeConstrainedFriend()) ||
1480 (FTD && FTD->getTemplatedDecl()->isMemberLikeConstrainedFriend())) {
1481 if (!isCompatibleWith(Ver: LangOptions::ClangABI::Ver17))
1482 Out << 'F';
1483 }
1484
1485 unsigned Arity = KnownArity;
1486 switch (Name.getNameKind()) {
1487 case DeclarationName::Identifier: {
1488 const IdentifierInfo *II = Name.getAsIdentifierInfo();
1489
1490 // We mangle decomposition declarations as the names of their bindings.
1491 if (auto *DD = dyn_cast<DecompositionDecl>(Val: ND)) {
1492 // FIXME: Non-standard mangling for decomposition declarations:
1493 //
1494 // <unqualified-name> ::= DC <source-name>* E
1495 //
1496 // Proposed on cxx-abi-dev on 2016-08-12
1497 Out << "DC";
1498 for (auto *BD : DD->bindings())
1499 mangleSourceName(II: BD->getDeclName().getAsIdentifierInfo());
1500 Out << 'E';
1501 writeAbiTags(ND, AdditionalAbiTags);
1502 break;
1503 }
1504
1505 if (auto *GD = dyn_cast<MSGuidDecl>(Val: ND)) {
1506 // We follow MSVC in mangling GUID declarations as if they were variables
1507 // with a particular reserved name. Continue the pretense here.
1508 SmallString<sizeof("_GUID_12345678_1234_1234_1234_1234567890ab")> GUID;
1509 llvm::raw_svector_ostream GUIDOS(GUID);
1510 Context.mangleMSGuidDecl(GD, GUIDOS);
1511 Out << GUID.size() << GUID;
1512 break;
1513 }
1514
1515 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: ND)) {
1516 // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/63.
1517 Out << "TA";
1518 mangleValueInTemplateArg(T: TPO->getType().getUnqualifiedType(),
1519 V: TPO->getValue(), /*TopLevel=*/true);
1520 break;
1521 }
1522
1523 if (II) {
1524 // Match GCC's naming convention for internal linkage symbols, for
1525 // symbols that are not actually visible outside of this TU. GCC
1526 // distinguishes between internal and external linkage symbols in
1527 // its mangling, to support cases like this that were valid C++ prior
1528 // to DR426:
1529 //
1530 // void test() { extern void foo(); }
1531 // static void foo();
1532 //
1533 // Don't bother with the L marker for names in anonymous namespaces; the
1534 // 12_GLOBAL__N_1 mangling is quite sufficient there, and this better
1535 // matches GCC anyway, because GCC does not treat anonymous namespaces as
1536 // implying internal linkage.
1537 if (Context.isInternalLinkageDecl(ND))
1538 Out << 'L';
1539
1540 bool IsRegCall = FD &&
1541 FD->getType()->castAs<FunctionType>()->getCallConv() ==
1542 clang::CC_X86RegCall;
1543 bool IsDeviceStub =
1544 FD && FD->hasAttr<CUDAGlobalAttr>() &&
1545 GD.getKernelReferenceKind() == KernelReferenceKind::Stub;
1546 if (IsDeviceStub)
1547 mangleDeviceStubName(II);
1548 else if (IsRegCall)
1549 mangleRegCallName(II);
1550 else
1551 mangleSourceName(II);
1552
1553 writeAbiTags(ND, AdditionalAbiTags);
1554 break;
1555 }
1556
1557 // Otherwise, an anonymous entity. We must have a declaration.
1558 assert(ND && "mangling empty name without declaration");
1559
1560 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Val: ND)) {
1561 if (NS->isAnonymousNamespace()) {
1562 // This is how gcc mangles these names.
1563 Out << "12_GLOBAL__N_1";
1564 break;
1565 }
1566 }
1567
1568 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: ND)) {
1569 // We must have an anonymous union or struct declaration.
1570 const RecordDecl *RD = VD->getType()->castAs<RecordType>()->getDecl();
1571
1572 // Itanium C++ ABI 5.1.2:
1573 //
1574 // For the purposes of mangling, the name of an anonymous union is
1575 // considered to be the name of the first named data member found by a
1576 // pre-order, depth-first, declaration-order walk of the data members of
1577 // the anonymous union. If there is no such data member (i.e., if all of
1578 // the data members in the union are unnamed), then there is no way for
1579 // a program to refer to the anonymous union, and there is therefore no
1580 // need to mangle its name.
1581 assert(RD->isAnonymousStructOrUnion()
1582 && "Expected anonymous struct or union!");
1583 const FieldDecl *FD = RD->findFirstNamedDataMember();
1584
1585 // It's actually possible for various reasons for us to get here
1586 // with an empty anonymous struct / union. Fortunately, it
1587 // doesn't really matter what name we generate.
1588 if (!FD) break;
1589 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1590
1591 mangleSourceName(II: FD->getIdentifier());
1592 // Not emitting abi tags: internal name anyway.
1593 break;
1594 }
1595
1596 // Class extensions have no name as a category, and it's possible
1597 // for them to be the semantic parent of certain declarations
1598 // (primarily, tag decls defined within declarations). Such
1599 // declarations will always have internal linkage, so the name
1600 // doesn't really matter, but we shouldn't crash on them. For
1601 // safety, just handle all ObjC containers here.
1602 if (isa<ObjCContainerDecl>(Val: ND))
1603 break;
1604
1605 // We must have an anonymous struct.
1606 const TagDecl *TD = cast<TagDecl>(Val: ND);
1607 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
1608 assert(TD->getDeclContext() == D->getDeclContext() &&
1609 "Typedef should not be in another decl context!");
1610 assert(D->getDeclName().getAsIdentifierInfo() &&
1611 "Typedef was not named!");
1612 mangleSourceName(II: D->getDeclName().getAsIdentifierInfo());
1613 assert(!AdditionalAbiTags && "Type cannot have additional abi tags");
1614 // Explicit abi tags are still possible; take from underlying type, not
1615 // from typedef.
1616 writeAbiTags(TD, nullptr);
1617 break;
1618 }
1619
1620 // <unnamed-type-name> ::= <closure-type-name>
1621 //
1622 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1623 // <lambda-sig> ::= <template-param-decl>* <parameter-type>+
1624 // # Parameter types or 'v' for 'void'.
1625 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Val: TD)) {
1626 std::optional<unsigned> DeviceNumber =
1627 Context.getDiscriminatorOverride()(Context.getASTContext(), Record);
1628
1629 // If we have a device-number via the discriminator, use that to mangle
1630 // the lambda, otherwise use the typical lambda-mangling-number. In either
1631 // case, a '0' should be mangled as a normal unnamed class instead of as a
1632 // lambda.
1633 if (Record->isLambda() &&
1634 ((DeviceNumber && *DeviceNumber > 0) ||
1635 (!DeviceNumber && Record->getLambdaManglingNumber() > 0))) {
1636 assert(!AdditionalAbiTags &&
1637 "Lambda type cannot have additional abi tags");
1638 mangleLambda(Lambda: Record);
1639 break;
1640 }
1641 }
1642
1643 if (TD->isExternallyVisible()) {
1644 unsigned UnnamedMangle =
1645 getASTContext().getManglingNumber(TD, Context.isAux());
1646 Out << "Ut";
1647 if (UnnamedMangle > 1)
1648 Out << UnnamedMangle - 2;
1649 Out << '_';
1650 writeAbiTags(TD, AdditionalAbiTags);
1651 break;
1652 }
1653
1654 // Get a unique id for the anonymous struct. If it is not a real output
1655 // ID doesn't matter so use fake one.
1656 unsigned AnonStructId =
1657 NullOut ? 0
1658 : Context.getAnonymousStructId(TD, dyn_cast<FunctionDecl>(Val: DC));
1659
1660 // Mangle it as a source name in the form
1661 // [n] $_<id>
1662 // where n is the length of the string.
1663 SmallString<8> Str;
1664 Str += "$_";
1665 Str += llvm::utostr(X: AnonStructId);
1666
1667 Out << Str.size();
1668 Out << Str;
1669 break;
1670 }
1671
1672 case DeclarationName::ObjCZeroArgSelector:
1673 case DeclarationName::ObjCOneArgSelector:
1674 case DeclarationName::ObjCMultiArgSelector:
1675 llvm_unreachable("Can't mangle Objective-C selector names here!");
1676
1677 case DeclarationName::CXXConstructorName: {
1678 const CXXRecordDecl *InheritedFrom = nullptr;
1679 TemplateName InheritedTemplateName;
1680 const TemplateArgumentList *InheritedTemplateArgs = nullptr;
1681 if (auto Inherited =
1682 cast<CXXConstructorDecl>(Val: ND)->getInheritedConstructor()) {
1683 InheritedFrom = Inherited.getConstructor()->getParent();
1684 InheritedTemplateName =
1685 TemplateName(Inherited.getConstructor()->getPrimaryTemplate());
1686 InheritedTemplateArgs =
1687 Inherited.getConstructor()->getTemplateSpecializationArgs();
1688 }
1689
1690 if (ND == Structor)
1691 // If the named decl is the C++ constructor we're mangling, use the type
1692 // we were given.
1693 mangleCXXCtorType(T: static_cast<CXXCtorType>(StructorType), InheritedFrom);
1694 else
1695 // Otherwise, use the complete constructor name. This is relevant if a
1696 // class with a constructor is declared within a constructor.
1697 mangleCXXCtorType(T: Ctor_Complete, InheritedFrom);
1698
1699 // FIXME: The template arguments are part of the enclosing prefix or
1700 // nested-name, but it's more convenient to mangle them here.
1701 if (InheritedTemplateArgs)
1702 mangleTemplateArgs(TN: InheritedTemplateName, AL: *InheritedTemplateArgs);
1703
1704 writeAbiTags(ND, AdditionalAbiTags);
1705 break;
1706 }
1707
1708 case DeclarationName::CXXDestructorName:
1709 if (ND == Structor)
1710 // If the named decl is the C++ destructor we're mangling, use the type we
1711 // were given.
1712 mangleCXXDtorType(T: static_cast<CXXDtorType>(StructorType));
1713 else
1714 // Otherwise, use the complete destructor name. This is relevant if a
1715 // class with a destructor is declared within a destructor.
1716 mangleCXXDtorType(T: Dtor_Complete);
1717 assert(ND);
1718 writeAbiTags(ND, AdditionalAbiTags);
1719 break;
1720
1721 case DeclarationName::CXXOperatorName:
1722 if (ND && Arity == UnknownArity) {
1723 Arity = cast<FunctionDecl>(Val: ND)->getNumParams();
1724
1725 // If we have a member function, we need to include the 'this' pointer.
1726 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: ND))
1727 if (MD->isImplicitObjectMemberFunction())
1728 Arity++;
1729 }
1730 [[fallthrough]];
1731 case DeclarationName::CXXConversionFunctionName:
1732 case DeclarationName::CXXLiteralOperatorName:
1733 mangleOperatorName(Name, Arity);
1734 writeAbiTags(ND, AdditionalAbiTags);
1735 break;
1736
1737 case DeclarationName::CXXDeductionGuideName:
1738 llvm_unreachable("Can't mangle a deduction guide name!");
1739
1740 case DeclarationName::CXXUsingDirective:
1741 llvm_unreachable("Can't mangle a using directive name!");
1742 }
1743}
1744
1745void CXXNameMangler::mangleRegCallName(const IdentifierInfo *II) {
1746 // <source-name> ::= <positive length number> __regcall3__ <identifier>
1747 // <number> ::= [n] <non-negative decimal integer>
1748 // <identifier> ::= <unqualified source code identifier>
1749 if (getASTContext().getLangOpts().RegCall4)
1750 Out << II->getLength() + sizeof("__regcall4__") - 1 << "__regcall4__"
1751 << II->getName();
1752 else
1753 Out << II->getLength() + sizeof("__regcall3__") - 1 << "__regcall3__"
1754 << II->getName();
1755}
1756
1757void CXXNameMangler::mangleDeviceStubName(const IdentifierInfo *II) {
1758 // <source-name> ::= <positive length number> __device_stub__ <identifier>
1759 // <number> ::= [n] <non-negative decimal integer>
1760 // <identifier> ::= <unqualified source code identifier>
1761 Out << II->getLength() + sizeof("__device_stub__") - 1 << "__device_stub__"
1762 << II->getName();
1763}
1764
1765void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1766 // <source-name> ::= <positive length number> <identifier>
1767 // <number> ::= [n] <non-negative decimal integer>
1768 // <identifier> ::= <unqualified source code identifier>
1769 Out << II->getLength() << II->getName();
1770}
1771
1772void CXXNameMangler::mangleNestedName(GlobalDecl GD,
1773 const DeclContext *DC,
1774 const AbiTagList *AdditionalAbiTags,
1775 bool NoFunction) {
1776 const NamedDecl *ND = cast<NamedDecl>(Val: GD.getDecl());
1777 // <nested-name>
1778 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1779 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1780 // <template-args> E
1781
1782 Out << 'N';
1783 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Val: ND)) {
1784 Qualifiers MethodQuals = Method->getMethodQualifiers();
1785 // We do not consider restrict a distinguishing attribute for overloading
1786 // purposes so we must not mangle it.
1787 if (Method->isExplicitObjectMemberFunction())
1788 Out << 'H';
1789 MethodQuals.removeRestrict();
1790 mangleQualifiers(Quals: MethodQuals);
1791 mangleRefQualifier(RefQualifier: Method->getRefQualifier());
1792 }
1793
1794 // Check if we have a template.
1795 const TemplateArgumentList *TemplateArgs = nullptr;
1796 if (GlobalDecl TD = isTemplate(GD, TemplateArgs)) {
1797 mangleTemplatePrefix(GD: TD, NoFunction);
1798 mangleTemplateArgs(TN: asTemplateName(GD: TD), AL: *TemplateArgs);
1799 } else {
1800 manglePrefix(DC, NoFunction);
1801 mangleUnqualifiedName(GD, DC, AdditionalAbiTags);
1802 }
1803
1804 Out << 'E';
1805}
1806void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
1807 ArrayRef<TemplateArgument> Args) {
1808 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1809
1810 Out << 'N';
1811
1812 mangleTemplatePrefix(TD);
1813 mangleTemplateArgs(TN: asTemplateName(TD), Args);
1814
1815 Out << 'E';
1816}
1817
1818void CXXNameMangler::mangleNestedNameWithClosurePrefix(
1819 GlobalDecl GD, const NamedDecl *PrefixND,
1820 const AbiTagList *AdditionalAbiTags) {
1821 // A <closure-prefix> represents a variable or field, not a regular
1822 // DeclContext, so needs special handling. In this case we're mangling a
1823 // limited form of <nested-name>:
1824 //
1825 // <nested-name> ::= N <closure-prefix> <closure-type-name> E
1826
1827 Out << 'N';
1828
1829 mangleClosurePrefix(ND: PrefixND);
1830 mangleUnqualifiedName(GD, DC: nullptr, AdditionalAbiTags);
1831
1832 Out << 'E';
1833}
1834
1835static GlobalDecl getParentOfLocalEntity(const DeclContext *DC) {
1836 GlobalDecl GD;
1837 // The Itanium spec says:
1838 // For entities in constructors and destructors, the mangling of the
1839 // complete object constructor or destructor is used as the base function
1840 // name, i.e. the C1 or D1 version.
1841 if (auto *CD = dyn_cast<CXXConstructorDecl>(Val: DC))
1842 GD = GlobalDecl(CD, Ctor_Complete);
1843 else if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: DC))
1844 GD = GlobalDecl(DD, Dtor_Complete);
1845 else
1846 GD = GlobalDecl(cast<FunctionDecl>(Val: DC));
1847 return GD;
1848}
1849
1850void CXXNameMangler::mangleLocalName(GlobalDecl GD,
1851 const AbiTagList *AdditionalAbiTags) {
1852 const Decl *D = GD.getDecl();
1853 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1854 // := Z <function encoding> E s [<discriminator>]
1855 // <local-name> := Z <function encoding> E d [ <parameter number> ]
1856 // _ <entity name>
1857 // <discriminator> := _ <non-negative number>
1858 assert(isa<NamedDecl>(D) || isa<BlockDecl>(D));
1859 const RecordDecl *RD = GetLocalClassDecl(D);
1860 const DeclContext *DC = Context.getEffectiveDeclContext(D: RD ? RD : D);
1861
1862 Out << 'Z';
1863
1864 {
1865 AbiTagState LocalAbiTags(AbiTags);
1866
1867 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Val: DC))
1868 mangleObjCMethodName(MD);
1869 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(Val: DC))
1870 mangleBlockForPrefix(Block: BD);
1871 else
1872 mangleFunctionEncoding(GD: getParentOfLocalEntity(DC));
1873
1874 // Implicit ABI tags (from namespace) are not available in the following
1875 // entity; reset to actually emitted tags, which are available.
1876 LocalAbiTags.setUsedAbiTags(LocalAbiTags.getEmittedAbiTags());
1877 }
1878
1879 Out << 'E';
1880
1881 // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to
1882 // be a bug that is fixed in trunk.
1883
1884 if (RD) {
1885 // The parameter number is omitted for the last parameter, 0 for the
1886 // second-to-last parameter, 1 for the third-to-last parameter, etc. The
1887 // <entity name> will of course contain a <closure-type-name>: Its
1888 // numbering will be local to the particular argument in which it appears
1889 // -- other default arguments do not affect its encoding.
1890 const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
1891 if (CXXRD && CXXRD->isLambda()) {
1892 if (const ParmVarDecl *Parm
1893 = dyn_cast_or_null<ParmVarDecl>(Val: CXXRD->getLambdaContextDecl())) {
1894 if (const FunctionDecl *Func
1895 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1896 Out << 'd';
1897 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1898 if (Num > 1)
1899 mangleNumber(Number: Num - 2);
1900 Out << '_';
1901 }
1902 }
1903 }
1904
1905 // Mangle the name relative to the closest enclosing function.
1906 // equality ok because RD derived from ND above
1907 if (D == RD) {
1908 mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
1909 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(Val: D)) {
1910 if (const NamedDecl *PrefixND = getClosurePrefix(BD))
1911 mangleClosurePrefix(ND: PrefixND, NoFunction: true /*NoFunction*/);
1912 else
1913 manglePrefix(DC: Context.getEffectiveDeclContext(BD), NoFunction: true /*NoFunction*/);
1914 assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1915 mangleUnqualifiedBlock(Block: BD);
1916 } else {
1917 const NamedDecl *ND = cast<NamedDecl>(Val: D);
1918 mangleNestedName(GD, DC: Context.getEffectiveDeclContext(ND),
1919 AdditionalAbiTags, NoFunction: true /*NoFunction*/);
1920 }
1921 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(Val: D)) {
1922 // Mangle a block in a default parameter; see above explanation for
1923 // lambdas.
1924 if (const ParmVarDecl *Parm
1925 = dyn_cast_or_null<ParmVarDecl>(Val: BD->getBlockManglingContextDecl())) {
1926 if (const FunctionDecl *Func
1927 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1928 Out << 'd';
1929 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1930 if (Num > 1)
1931 mangleNumber(Number: Num - 2);
1932 Out << '_';
1933 }
1934 }
1935
1936 assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1937 mangleUnqualifiedBlock(Block: BD);
1938 } else {
1939 mangleUnqualifiedName(GD, DC, AdditionalAbiTags);
1940 }
1941
1942 if (const NamedDecl *ND = dyn_cast<NamedDecl>(RD ? RD : D)) {
1943 unsigned disc;
1944 if (Context.getNextDiscriminator(ND, disc)) {
1945 if (disc < 10)
1946 Out << '_' << disc;
1947 else
1948 Out << "__" << disc << '_';
1949 }
1950 }
1951}
1952
1953void CXXNameMangler::mangleBlockForPrefix(const BlockDecl *Block) {
1954 if (GetLocalClassDecl(Block)) {
1955 mangleLocalName(GD: Block, /* AdditionalAbiTags */ nullptr);
1956 return;
1957 }
1958 const DeclContext *DC = Context.getEffectiveDeclContext(Block);
1959 if (isLocalContainerContext(DC)) {
1960 mangleLocalName(GD: Block, /* AdditionalAbiTags */ nullptr);
1961 return;
1962 }
1963 if (const NamedDecl *PrefixND = getClosurePrefix(Block))
1964 mangleClosurePrefix(ND: PrefixND);
1965 else
1966 manglePrefix(DC);
1967 mangleUnqualifiedBlock(Block);
1968}
1969
1970void CXXNameMangler::mangleUnqualifiedBlock(const BlockDecl *Block) {
1971 // When trying to be ABI-compatibility with clang 12 and before, mangle a
1972 // <data-member-prefix> now, with no substitutions and no <template-args>.
1973 if (Decl *Context = Block->getBlockManglingContextDecl()) {
1974 if (isCompatibleWith(Ver: LangOptions::ClangABI::Ver12) &&
1975 (isa<VarDecl>(Val: Context) || isa<FieldDecl>(Val: Context)) &&
1976 Context->getDeclContext()->isRecord()) {
1977 const auto *ND = cast<NamedDecl>(Val: Context);
1978 if (ND->getIdentifier()) {
1979 mangleSourceNameWithAbiTags(ND);
1980 Out << 'M';
1981 }
1982 }
1983 }
1984
1985 // If we have a block mangling number, use it.
1986 unsigned Number = Block->getBlockManglingNumber();
1987 // Otherwise, just make up a number. It doesn't matter what it is because
1988 // the symbol in question isn't externally visible.
1989 if (!Number)
1990 Number = Context.getBlockId(BD: Block, Local: false);
1991 else {
1992 // Stored mangling numbers are 1-based.
1993 --Number;
1994 }
1995 Out << "Ub";
1996 if (Number > 0)
1997 Out << Number - 1;
1998 Out << '_';
1999}
2000
2001// <template-param-decl>
2002// ::= Ty # template type parameter
2003// ::= Tk <concept name> [<template-args>] # constrained type parameter
2004// ::= Tn <type> # template non-type parameter
2005// ::= Tt <template-param-decl>* E [Q <requires-clause expr>]
2006// # template template parameter
2007// ::= Tp <template-param-decl> # template parameter pack
2008void CXXNameMangler::mangleTemplateParamDecl(const NamedDecl *Decl) {
2009 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/47.
2010 if (auto *Ty = dyn_cast<TemplateTypeParmDecl>(Val: Decl)) {
2011 if (Ty->isParameterPack())
2012 Out << "Tp";
2013 const TypeConstraint *Constraint = Ty->getTypeConstraint();
2014 if (Constraint && !isCompatibleWith(Ver: LangOptions::ClangABI::Ver17)) {
2015 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/24.
2016 Out << "Tk";
2017 mangleTypeConstraint(Constraint);
2018 } else {
2019 Out << "Ty";
2020 }
2021 } else if (auto *Tn = dyn_cast<NonTypeTemplateParmDecl>(Val: Decl)) {
2022 if (Tn->isExpandedParameterPack()) {
2023 for (unsigned I = 0, N = Tn->getNumExpansionTypes(); I != N; ++I) {
2024 Out << "Tn";
2025 mangleType(T: Tn->getExpansionType(I));
2026 }
2027 } else {
2028 QualType T = Tn->getType();
2029 if (Tn->isParameterPack()) {
2030 Out << "Tp";
2031 if (auto *PackExpansion = T->getAs<PackExpansionType>())
2032 T = PackExpansion->getPattern();
2033 }
2034 Out << "Tn";
2035 mangleType(T);
2036 }
2037 } else if (auto *Tt = dyn_cast<TemplateTemplateParmDecl>(Val: Decl)) {
2038 if (Tt->isExpandedParameterPack()) {
2039 for (unsigned I = 0, N = Tt->getNumExpansionTemplateParameters(); I != N;
2040 ++I)
2041 mangleTemplateParameterList(Params: Tt->getExpansionTemplateParameters(I));
2042 } else {
2043 if (Tt->isParameterPack())
2044 Out << "Tp";
2045 mangleTemplateParameterList(Params: Tt->getTemplateParameters());
2046 }
2047 }
2048}
2049
2050void CXXNameMangler::mangleTemplateParameterList(
2051 const TemplateParameterList *Params) {
2052 Out << "Tt";
2053 for (auto *Param : *Params)
2054 mangleTemplateParamDecl(Decl: Param);
2055 mangleRequiresClause(RequiresClause: Params->getRequiresClause());
2056 Out << "E";
2057}
2058
2059void CXXNameMangler::mangleTypeConstraint(
2060 const ConceptDecl *Concept, ArrayRef<TemplateArgument> Arguments) {
2061 const DeclContext *DC = Context.getEffectiveDeclContext(Concept);
2062 if (!Arguments.empty())
2063 mangleTemplateName(Concept, Arguments);
2064 else if (DC->isTranslationUnit() || isStdNamespace(DC))
2065 mangleUnscopedName(Concept, DC, nullptr);
2066 else
2067 mangleNestedName(Concept, DC, nullptr);
2068}
2069
2070void CXXNameMangler::mangleTypeConstraint(const TypeConstraint *Constraint) {
2071 llvm::SmallVector<TemplateArgument, 8> Args;
2072 if (Constraint->getTemplateArgsAsWritten()) {
2073 for (const TemplateArgumentLoc &ArgLoc :
2074 Constraint->getTemplateArgsAsWritten()->arguments())
2075 Args.push_back(Elt: ArgLoc.getArgument());
2076 }
2077 return mangleTypeConstraint(Concept: Constraint->getNamedConcept(), Arguments: Args);
2078}
2079
2080void CXXNameMangler::mangleRequiresClause(const Expr *RequiresClause) {
2081 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/24.
2082 if (RequiresClause && !isCompatibleWith(Ver: LangOptions::ClangABI::Ver17)) {
2083 Out << 'Q';
2084 mangleExpression(E: RequiresClause);
2085 }
2086}
2087
2088void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) {
2089 // When trying to be ABI-compatibility with clang 12 and before, mangle a
2090 // <data-member-prefix> now, with no substitutions.
2091 if (Decl *Context = Lambda->getLambdaContextDecl()) {
2092 if (isCompatibleWith(Ver: LangOptions::ClangABI::Ver12) &&
2093 (isa<VarDecl>(Val: Context) || isa<FieldDecl>(Val: Context)) &&
2094 !isa<ParmVarDecl>(Val: Context)) {
2095 if (const IdentifierInfo *Name
2096 = cast<NamedDecl>(Val: Context)->getIdentifier()) {
2097 mangleSourceName(II: Name);
2098 const TemplateArgumentList *TemplateArgs = nullptr;
2099 if (GlobalDecl TD = isTemplate(GD: cast<NamedDecl>(Val: Context), TemplateArgs))
2100 mangleTemplateArgs(TN: asTemplateName(GD: TD), AL: *TemplateArgs);
2101 Out << 'M';
2102 }
2103 }
2104 }
2105
2106 Out << "Ul";
2107 mangleLambdaSig(Lambda);
2108 Out << "E";
2109
2110 // The number is omitted for the first closure type with a given
2111 // <lambda-sig> in a given context; it is n-2 for the nth closure type
2112 // (in lexical order) with that same <lambda-sig> and context.
2113 //
2114 // The AST keeps track of the number for us.
2115 //
2116 // In CUDA/HIP, to ensure the consistent lamba numbering between the device-
2117 // and host-side compilations, an extra device mangle context may be created
2118 // if the host-side CXX ABI has different numbering for lambda. In such case,
2119 // if the mangle context is that device-side one, use the device-side lambda
2120 // mangling number for this lambda.
2121 std::optional<unsigned> DeviceNumber =
2122 Context.getDiscriminatorOverride()(Context.getASTContext(), Lambda);
2123 unsigned Number =
2124 DeviceNumber ? *DeviceNumber : Lambda->getLambdaManglingNumber();
2125
2126 assert(Number > 0 && "Lambda should be mangled as an unnamed class");
2127 if (Number > 1)
2128 mangleNumber(Number: Number - 2);
2129 Out << '_';
2130}
2131
2132void CXXNameMangler::mangleLambdaSig(const CXXRecordDecl *Lambda) {
2133 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/31.
2134 for (auto *D : Lambda->getLambdaExplicitTemplateParameters())
2135 mangleTemplateParamDecl(Decl: D);
2136
2137 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/24.
2138 if (auto *TPL = Lambda->getGenericLambdaTemplateParameterList())
2139 mangleRequiresClause(RequiresClause: TPL->getRequiresClause());
2140
2141 auto *Proto =
2142 Lambda->getLambdaTypeInfo()->getType()->castAs<FunctionProtoType>();
2143 mangleBareFunctionType(Proto, /*MangleReturnType=*/false,
2144 Lambda->getLambdaStaticInvoker());
2145}
2146
2147void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
2148 switch (qualifier->getKind()) {
2149 case NestedNameSpecifier::Global:
2150 // nothing
2151 return;
2152
2153 case NestedNameSpecifier::Super:
2154 llvm_unreachable("Can't mangle __super specifier");
2155
2156 case NestedNameSpecifier::Namespace:
2157 mangleName(qualifier->getAsNamespace());
2158 return;
2159
2160 case NestedNameSpecifier::NamespaceAlias:
2161 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
2162 return;
2163
2164 case NestedNameSpecifier::TypeSpec:
2165 case NestedNameSpecifier::TypeSpecWithTemplate:
2166 manglePrefix(type: QualType(qualifier->getAsType(), 0));
2167 return;
2168
2169 case NestedNameSpecifier::Identifier:
2170 // Clang 14 and before did not consider this substitutable.
2171 bool Clang14Compat = isCompatibleWith(Ver: LangOptions::ClangABI::Ver14);
2172 if (!Clang14Compat && mangleSubstitution(NNS: qualifier))
2173 return;
2174
2175 // Member expressions can have these without prefixes, but that
2176 // should end up in mangleUnresolvedPrefix instead.
2177 assert(qualifier->getPrefix());
2178 manglePrefix(qualifier: qualifier->getPrefix());
2179
2180 mangleSourceName(II: qualifier->getAsIdentifier());
2181
2182 if (!Clang14Compat)
2183 addSubstitution(NNS: qualifier);
2184 return;
2185 }
2186
2187 llvm_unreachable("unexpected nested name specifier");
2188}
2189
2190void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
2191 // <prefix> ::= <prefix> <unqualified-name>
2192 // ::= <template-prefix> <template-args>
2193 // ::= <closure-prefix>
2194 // ::= <template-param>
2195 // ::= # empty
2196 // ::= <substitution>
2197
2198 assert(!isa<LinkageSpecDecl>(DC) && "prefix cannot be LinkageSpecDecl");
2199
2200 if (DC->isTranslationUnit())
2201 return;
2202
2203 if (NoFunction && isLocalContainerContext(DC))
2204 return;
2205
2206 const NamedDecl *ND = cast<NamedDecl>(Val: DC);
2207 if (mangleSubstitution(ND))
2208 return;
2209
2210 // Check if we have a template-prefix or a closure-prefix.
2211 const TemplateArgumentList *TemplateArgs = nullptr;
2212 if (GlobalDecl TD = isTemplate(GD: ND, TemplateArgs)) {
2213 mangleTemplatePrefix(GD: TD);
2214 mangleTemplateArgs(TN: asTemplateName(GD: TD), AL: *TemplateArgs);
2215 } else if (const NamedDecl *PrefixND = getClosurePrefix(ND)) {
2216 mangleClosurePrefix(ND: PrefixND, NoFunction);
2217 mangleUnqualifiedName(GD: ND, DC: nullptr, AdditionalAbiTags: nullptr);
2218 } else {
2219 const DeclContext *DC = Context.getEffectiveDeclContext(ND);
2220 manglePrefix(DC, NoFunction);
2221 mangleUnqualifiedName(GD: ND, DC, AdditionalAbiTags: nullptr);
2222 }
2223
2224 addSubstitution(ND);
2225}
2226
2227void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
2228 // <template-prefix> ::= <prefix> <template unqualified-name>
2229 // ::= <template-param>
2230 // ::= <substitution>
2231 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2232 return mangleTemplatePrefix(TD);
2233
2234 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
2235 assert(Dependent && "unexpected template name kind");
2236
2237 // Clang 11 and before mangled the substitution for a dependent template name
2238 // after already having emitted (a substitution for) the prefix.
2239 bool Clang11Compat = isCompatibleWith(Ver: LangOptions::ClangABI::Ver11);
2240 if (!Clang11Compat && mangleSubstitution(Template))
2241 return;
2242
2243 if (NestedNameSpecifier *Qualifier = Dependent->getQualifier())
2244 manglePrefix(qualifier: Qualifier);
2245
2246 if (Clang11Compat && mangleSubstitution(Template))
2247 return;
2248
2249 if (const IdentifierInfo *Id = Dependent->getIdentifier())
2250 mangleSourceName(II: Id);
2251 else
2252 mangleOperatorName(OO: Dependent->getOperator(), Arity: UnknownArity);
2253
2254 addSubstitution(Template);
2255}
2256
2257void CXXNameMangler::mangleTemplatePrefix(GlobalDecl GD,
2258 bool NoFunction) {
2259 const TemplateDecl *ND = cast<TemplateDecl>(Val: GD.getDecl());
2260 // <template-prefix> ::= <prefix> <template unqualified-name>
2261 // ::= <template-param>
2262 // ::= <substitution>
2263 // <template-template-param> ::= <template-param>
2264 // <substitution>
2265
2266 if (mangleSubstitution(ND))
2267 return;
2268
2269 // <template-template-param> ::= <template-param>
2270 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: ND)) {
2271 mangleTemplateParameter(Depth: TTP->getDepth(), Index: TTP->getIndex());
2272 } else {
2273 const DeclContext *DC = Context.getEffectiveDeclContext(ND);
2274 manglePrefix(DC, NoFunction);
2275 if (isa<BuiltinTemplateDecl>(Val: ND) || isa<ConceptDecl>(Val: ND))
2276 mangleUnqualifiedName(GD, DC, AdditionalAbiTags: nullptr);
2277 else
2278 mangleUnqualifiedName(GD: GD.getWithDecl(ND->getTemplatedDecl()), DC,
2279 AdditionalAbiTags: nullptr);
2280 }
2281
2282 addSubstitution(ND);
2283}
2284
2285const NamedDecl *CXXNameMangler::getClosurePrefix(const Decl *ND) {
2286 if (isCompatibleWith(Ver: LangOptions::ClangABI::Ver12))
2287 return nullptr;
2288
2289 const NamedDecl *Context = nullptr;
2290 if (auto *Block = dyn_cast<BlockDecl>(Val: ND)) {
2291 Context = dyn_cast_or_null<NamedDecl>(Val: Block->getBlockManglingContextDecl());
2292 } else if (auto *RD = dyn_cast<CXXRecordDecl>(Val: ND)) {
2293 if (RD->isLambda())
2294 Context = dyn_cast_or_null<NamedDecl>(Val: RD->getLambdaContextDecl());
2295 }
2296 if (!Context)
2297 return nullptr;
2298
2299 // Only lambdas within the initializer of a non-local variable or non-static
2300 // data member get a <closure-prefix>.
2301 if ((isa<VarDecl>(Val: Context) && cast<VarDecl>(Val: Context)->hasGlobalStorage()) ||
2302 isa<FieldDecl>(Val: Context))
2303 return Context;
2304
2305 return nullptr;
2306}
2307
2308void CXXNameMangler::mangleClosurePrefix(const NamedDecl *ND, bool NoFunction) {
2309 // <closure-prefix> ::= [ <prefix> ] <unqualified-name> M
2310 // ::= <template-prefix> <template-args> M
2311 if (mangleSubstitution(ND))
2312 return;
2313
2314 const TemplateArgumentList *TemplateArgs = nullptr;
2315 if (GlobalDecl TD = isTemplate(GD: ND, TemplateArgs)) {
2316 mangleTemplatePrefix(GD: TD, NoFunction);
2317 mangleTemplateArgs(TN: asTemplateName(GD: TD), AL: *TemplateArgs);
2318 } else {
2319 const auto *DC = Context.getEffectiveDeclContext(ND);
2320 manglePrefix(DC, NoFunction);
2321 mangleUnqualifiedName(ND, DC, nullptr);
2322 }
2323
2324 Out << 'M';
2325
2326 addSubstitution(ND);
2327}
2328
2329/// Mangles a template name under the production <type>. Required for
2330/// template template arguments.
2331/// <type> ::= <class-enum-type>
2332/// ::= <template-param>
2333/// ::= <substitution>
2334void CXXNameMangler::mangleType(TemplateName TN) {
2335 if (mangleSubstitution(Template: TN))
2336 return;
2337
2338 TemplateDecl *TD = nullptr;
2339
2340 switch (TN.getKind()) {
2341 case TemplateName::QualifiedTemplate:
2342 case TemplateName::UsingTemplate:
2343 case TemplateName::Template:
2344 TD = TN.getAsTemplateDecl();
2345 goto HaveDecl;
2346
2347 HaveDecl:
2348 if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: TD))
2349 mangleTemplateParameter(Depth: TTP->getDepth(), Index: TTP->getIndex());
2350 else
2351 mangleName(TD);
2352 break;
2353
2354 case TemplateName::OverloadedTemplate:
2355 case TemplateName::AssumedTemplate:
2356 llvm_unreachable("can't mangle an overloaded template name as a <type>");
2357
2358 case TemplateName::DependentTemplate: {
2359 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
2360 assert(Dependent->isIdentifier());
2361
2362 // <class-enum-type> ::= <name>
2363 // <name> ::= <nested-name>
2364 mangleUnresolvedPrefix(qualifier: Dependent->getQualifier());
2365 mangleSourceName(II: Dependent->getIdentifier());
2366 break;
2367 }
2368
2369 case TemplateName::SubstTemplateTemplateParm: {
2370 // Substituted template parameters are mangled as the substituted
2371 // template. This will check for the substitution twice, which is
2372 // fine, but we have to return early so that we don't try to *add*
2373 // the substitution twice.
2374 SubstTemplateTemplateParmStorage *subst
2375 = TN.getAsSubstTemplateTemplateParm();
2376 mangleType(TN: subst->getReplacement());
2377 return;
2378 }
2379
2380 case TemplateName::SubstTemplateTemplateParmPack: {
2381 // FIXME: not clear how to mangle this!
2382 // template <template <class> class T...> class A {
2383 // template <template <class> class U...> void foo(B<T,U> x...);
2384 // };
2385 Out << "_SUBSTPACK_";
2386 break;
2387 }
2388 }
2389
2390 addSubstitution(Template: TN);
2391}
2392
2393bool CXXNameMangler::mangleUnresolvedTypeOrSimpleId(QualType Ty,
2394 StringRef Prefix) {
2395 // Only certain other types are valid as prefixes; enumerate them.
2396 switch (Ty->getTypeClass()) {
2397 case Type::Builtin:
2398 case Type::Complex:
2399 case Type::Adjusted:
2400 case Type::Decayed:
2401 case Type::ArrayParameter:
2402 case Type::Pointer:
2403 case Type::BlockPointer:
2404 case Type::LValueReference:
2405 case Type::RValueReference:
2406 case Type::MemberPointer:
2407 case Type::ConstantArray:
2408 case Type::IncompleteArray:
2409 case Type::VariableArray:
2410 case Type::DependentSizedArray:
2411 case Type::DependentAddressSpace:
2412 case Type::DependentVector:
2413 case Type::DependentSizedExtVector:
2414 case Type::Vector:
2415 case Type::ExtVector:
2416 case Type::ConstantMatrix:
2417 case Type::DependentSizedMatrix:
2418 case Type::FunctionProto:
2419 case Type::FunctionNoProto:
2420 case Type::Paren:
2421 case Type::Attributed:
2422 case Type::BTFTagAttributed:
2423 case Type::Auto:
2424 case Type::DeducedTemplateSpecialization:
2425 case Type::PackExpansion:
2426 case Type::ObjCObject:
2427 case Type::ObjCInterface:
2428 case Type::ObjCObjectPointer:
2429 case Type::ObjCTypeParam:
2430 case Type::Atomic:
2431 case Type::Pipe:
2432 case Type::MacroQualified:
2433 case Type::BitInt:
2434 case Type::DependentBitInt:
2435 case Type::CountAttributed:
2436 llvm_unreachable("type is illegal as a nested name specifier");
2437
2438 case Type::SubstTemplateTypeParmPack:
2439 // FIXME: not clear how to mangle this!
2440 // template <class T...> class A {
2441 // template <class U...> void foo(decltype(T::foo(U())) x...);
2442 // };
2443 Out << "_SUBSTPACK_";
2444 break;
2445
2446 // <unresolved-type> ::= <template-param>
2447 // ::= <decltype>
2448 // ::= <template-template-param> <template-args>
2449 // (this last is not official yet)
2450 case Type::TypeOfExpr:
2451 case Type::TypeOf:
2452 case Type::Decltype:
2453 case Type::PackIndexing:
2454 case Type::TemplateTypeParm:
2455 case Type::UnaryTransform:
2456 case Type::SubstTemplateTypeParm:
2457 unresolvedType:
2458 // Some callers want a prefix before the mangled type.
2459 Out << Prefix;
2460
2461 // This seems to do everything we want. It's not really
2462 // sanctioned for a substituted template parameter, though.
2463 mangleType(T: Ty);
2464
2465 // We never want to print 'E' directly after an unresolved-type,
2466 // so we return directly.
2467 return true;
2468
2469 case Type::Typedef:
2470 mangleSourceNameWithAbiTags(cast<TypedefType>(Val&: Ty)->getDecl());
2471 break;
2472
2473 case Type::UnresolvedUsing:
2474 mangleSourceNameWithAbiTags(
2475 cast<UnresolvedUsingType>(Val&: Ty)->getDecl());
2476 break;
2477
2478 case Type::Enum:
2479 case Type::Record:
2480 mangleSourceNameWithAbiTags(cast<TagType>(Val&: Ty)->getDecl());
2481 break;
2482
2483 case Type::TemplateSpecialization: {
2484 const TemplateSpecializationType *TST =
2485 cast<TemplateSpecializationType>(Val&: Ty);
2486 TemplateName TN = TST->getTemplateName();
2487 switch (TN.getKind()) {
2488 case TemplateName::Template:
2489 case TemplateName::QualifiedTemplate: {
2490 TemplateDecl *TD = TN.getAsTemplateDecl();
2491
2492 // If the base is a template template parameter, this is an
2493 // unresolved type.
2494 assert(TD && "no template for template specialization type");
2495 if (isa<TemplateTemplateParmDecl>(Val: TD))
2496 goto unresolvedType;
2497
2498 mangleSourceNameWithAbiTags(TD);
2499 break;
2500 }
2501
2502 case TemplateName::OverloadedTemplate:
2503 case TemplateName::AssumedTemplate:
2504 case TemplateName::DependentTemplate:
2505 llvm_unreachable("invalid base for a template specialization type");
2506
2507 case TemplateName::SubstTemplateTemplateParm: {
2508 SubstTemplateTemplateParmStorage *subst =
2509 TN.getAsSubstTemplateTemplateParm();
2510 mangleExistingSubstitution(name: subst->getReplacement());
2511 break;
2512 }
2513
2514 case TemplateName::SubstTemplateTemplateParmPack: {
2515 // FIXME: not clear how to mangle this!
2516 // template <template <class U> class T...> class A {
2517 // template <class U...> void foo(decltype(T<U>::foo) x...);
2518 // };
2519 Out << "_SUBSTPACK_";
2520 break;
2521 }
2522 case TemplateName::UsingTemplate: {
2523 TemplateDecl *TD = TN.getAsTemplateDecl();
2524 assert(TD && !isa<TemplateTemplateParmDecl>(TD));
2525 mangleSourceNameWithAbiTags(TD);
2526 break;
2527 }
2528 }
2529
2530 // Note: we don't pass in the template name here. We are mangling the
2531 // original source-level template arguments, so we shouldn't consider
2532 // conversions to the corresponding template parameter.
2533 // FIXME: Other compilers mangle partially-resolved template arguments in
2534 // unresolved-qualifier-levels.
2535 mangleTemplateArgs(TN: TemplateName(), Args: TST->template_arguments());
2536 break;
2537 }
2538
2539 case Type::InjectedClassName:
2540 mangleSourceNameWithAbiTags(
2541 cast<InjectedClassNameType>(Val&: Ty)->getDecl());
2542 break;
2543
2544 case Type::DependentName:
2545 mangleSourceName(II: cast<DependentNameType>(Val&: Ty)->getIdentifier());
2546 break;
2547
2548 case Type::DependentTemplateSpecialization: {
2549 const DependentTemplateSpecializationType *DTST =
2550 cast<DependentTemplateSpecializationType>(Val&: Ty);
2551 TemplateName Template = getASTContext().getDependentTemplateName(
2552 NNS: DTST->getQualifier(), Name: DTST->getIdentifier());
2553 mangleSourceName(II: DTST->getIdentifier());
2554 mangleTemplateArgs(TN: Template, Args: DTST->template_arguments());
2555 break;
2556 }
2557
2558 case Type::Using:
2559 return mangleUnresolvedTypeOrSimpleId(Ty: cast<UsingType>(Val&: Ty)->desugar(),
2560 Prefix);
2561 case Type::Elaborated:
2562 return mangleUnresolvedTypeOrSimpleId(
2563 Ty: cast<ElaboratedType>(Val&: Ty)->getNamedType(), Prefix);
2564 }
2565
2566 return false;
2567}
2568
2569void CXXNameMangler::mangleOperatorName(DeclarationName Name, unsigned Arity) {
2570 switch (Name.getNameKind()) {
2571 case DeclarationName::CXXConstructorName:
2572 case DeclarationName::CXXDestructorName:
2573 case DeclarationName::CXXDeductionGuideName:
2574 case DeclarationName::CXXUsingDirective:
2575 case DeclarationName::Identifier:
2576 case DeclarationName::ObjCMultiArgSelector:
2577 case DeclarationName::ObjCOneArgSelector:
2578 case DeclarationName::ObjCZeroArgSelector:
2579 llvm_unreachable("Not an operator name");
2580
2581 case DeclarationName::CXXConversionFunctionName:
2582 // <operator-name> ::= cv <type> # (cast)
2583 Out << "cv";
2584 mangleType(T: Name.getCXXNameType());
2585 break;
2586
2587 case DeclarationName::CXXLiteralOperatorName:
2588 Out << "li";
2589 mangleSourceName(II: Name.getCXXLiteralIdentifier());
2590 return;
2591
2592 case DeclarationName::CXXOperatorName:
2593 mangleOperatorName(OO: Name.getCXXOverloadedOperator(), Arity);
2594 break;
2595 }
2596}
2597
2598void
2599CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
2600 switch (OO) {
2601 // <operator-name> ::= nw # new
2602 case OO_New: Out << "nw"; break;
2603 // ::= na # new[]
2604 case OO_Array_New: Out << "na"; break;
2605 // ::= dl # delete
2606 case OO_Delete: Out << "dl"; break;
2607 // ::= da # delete[]
2608 case OO_Array_Delete: Out << "da"; break;
2609 // ::= ps # + (unary)
2610 // ::= pl # + (binary or unknown)
2611 case OO_Plus:
2612 Out << (Arity == 1? "ps" : "pl"); break;
2613 // ::= ng # - (unary)
2614 // ::= mi # - (binary or unknown)
2615 case OO_Minus:
2616 Out << (Arity == 1? "ng" : "mi"); break;
2617 // ::= ad # & (unary)
2618 // ::= an # & (binary or unknown)
2619 case OO_Amp:
2620 Out << (Arity == 1? "ad" : "an"); break;
2621 // ::= de # * (unary)
2622 // ::= ml # * (binary or unknown)
2623 case OO_Star:
2624 // Use binary when unknown.
2625 Out << (Arity == 1? "de" : "ml"); break;
2626 // ::= co # ~
2627 case OO_Tilde: Out << "co"; break;
2628 // ::= dv # /
2629 case OO_Slash: Out << "dv"; break;
2630 // ::= rm # %
2631 case OO_Percent: Out << "rm"; break;
2632 // ::= or # |
2633 case OO_Pipe: Out << "or"; break;
2634 // ::= eo # ^
2635 case OO_Caret: Out << "eo"; break;
2636 // ::= aS # =
2637 case OO_Equal: Out << "aS"; break;
2638 // ::= pL # +=
2639 case OO_PlusEqual: Out << "pL"; break;
2640 // ::= mI # -=
2641 case OO_MinusEqual: Out << "mI"; break;
2642 // ::= mL # *=
2643 case OO_StarEqual: Out << "mL"; break;
2644 // ::= dV # /=
2645 case OO_SlashEqual: Out << "dV"; break;
2646 // ::= rM # %=
2647 case OO_PercentEqual: Out << "rM"; break;
2648 // ::= aN # &=
2649 case OO_AmpEqual: Out << "aN"; break;
2650 // ::= oR # |=
2651 case OO_PipeEqual: Out << "oR"; break;
2652 // ::= eO # ^=
2653 case OO_CaretEqual: Out << "eO"; break;
2654 // ::= ls # <<
2655 case OO_LessLess: Out << "ls"; break;
2656 // ::= rs # >>
2657 case OO_GreaterGreater: Out << "rs"; break;
2658 // ::= lS # <<=
2659 case OO_LessLessEqual: Out << "lS"; break;
2660 // ::= rS # >>=
2661 case OO_GreaterGreaterEqual: Out << "rS"; break;
2662 // ::= eq # ==
2663 case OO_EqualEqual: Out << "eq"; break;
2664 // ::= ne # !=
2665 case OO_ExclaimEqual: Out << "ne"; break;
2666 // ::= lt # <
2667 case OO_Less: Out << "lt"; break;
2668 // ::= gt # >
2669 case OO_Greater: Out << "gt"; break;
2670 // ::= le # <=
2671 case OO_LessEqual: Out << "le"; break;
2672 // ::= ge # >=
2673 case OO_GreaterEqual: Out << "ge"; break;
2674 // ::= nt # !
2675 case OO_Exclaim: Out << "nt"; break;
2676 // ::= aa # &&
2677 case OO_AmpAmp: Out << "aa"; break;
2678 // ::= oo # ||
2679 case OO_PipePipe: Out << "oo"; break;
2680 // ::= pp # ++
2681 case OO_PlusPlus: Out << "pp"; break;
2682 // ::= mm # --
2683 case OO_MinusMinus: Out << "mm"; break;
2684 // ::= cm # ,
2685 case OO_Comma: Out << "cm"; break;
2686 // ::= pm # ->*
2687 case OO_ArrowStar: Out << "pm"; break;
2688 // ::= pt # ->
2689 case OO_Arrow: Out << "pt"; break;
2690 // ::= cl # ()
2691 case OO_Call: Out << "cl"; break;
2692 // ::= ix # []
2693 case OO_Subscript: Out << "ix"; break;
2694
2695 // ::= qu # ?
2696 // The conditional operator can't be overloaded, but we still handle it when
2697 // mangling expressions.
2698 case OO_Conditional: Out << "qu"; break;
2699 // Proposal on cxx-abi-dev, 2015-10-21.
2700 // ::= aw # co_await
2701 case OO_Coawait: Out << "aw"; break;
2702 // Proposed in cxx-abi github issue 43.
2703 // ::= ss # <=>
2704 case OO_Spaceship: Out << "ss"; break;
2705
2706 case OO_None:
2707 case NUM_OVERLOADED_OPERATORS:
2708 llvm_unreachable("Not an overloaded operator");
2709 }
2710}
2711
2712void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSpaceType *DAST) {
2713 // Vendor qualifiers come first and if they are order-insensitive they must
2714 // be emitted in reversed alphabetical order, see Itanium ABI 5.1.5.
2715
2716 // <type> ::= U <addrspace-expr>
2717 if (DAST) {
2718 Out << "U2ASI";
2719 mangleExpression(E: DAST->getAddrSpaceExpr());
2720 Out << "E";
2721 }
2722
2723 // Address space qualifiers start with an ordinary letter.
2724 if (Quals.hasAddressSpace()) {
2725 // Address space extension:
2726 //
2727 // <type> ::= U <target-addrspace>
2728 // <type> ::= U <OpenCL-addrspace>
2729 // <type> ::= U <CUDA-addrspace>
2730
2731 SmallString<64> ASString;
2732 LangAS AS = Quals.getAddressSpace();
2733
2734 if (Context.getASTContext().addressSpaceMapManglingFor(AS)) {
2735 // <target-addrspace> ::= "AS" <address-space-number>
2736 unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
2737 if (TargetAS != 0 ||
2738 Context.getASTContext().getTargetAddressSpace(AS: LangAS::Default) != 0)
2739 ASString = "AS" + llvm::utostr(X: TargetAS);
2740 } else {
2741 switch (AS) {
2742 default: llvm_unreachable("Not a language specific address space");
2743 // <OpenCL-addrspace> ::= "CL" [ "global" | "local" | "constant" |
2744 // "private"| "generic" | "device" |
2745 // "host" ]
2746 case LangAS::opencl_global:
2747 ASString = "CLglobal";
2748 break;
2749 case LangAS::opencl_global_device:
2750 ASString = "CLdevice";
2751 break;
2752 case LangAS::opencl_global_host:
2753 ASString = "CLhost";
2754 break;
2755 case LangAS::opencl_local:
2756 ASString = "CLlocal";
2757 break;
2758 case LangAS::opencl_constant:
2759 ASString = "CLconstant";
2760 break;
2761 case LangAS::opencl_private:
2762 ASString = "CLprivate";
2763 break;
2764 case LangAS::opencl_generic:
2765 ASString = "CLgeneric";
2766 break;
2767 // <SYCL-addrspace> ::= "SY" [ "global" | "local" | "private" |
2768 // "device" | "host" ]
2769 case LangAS::sycl_global:
2770 ASString = "SYglobal";
2771 break;
2772 case LangAS::sycl_global_device:
2773 ASString = "SYdevice";
2774 break;
2775 case LangAS::sycl_global_host:
2776 ASString = "SYhost";
2777 break;
2778 case LangAS::sycl_local:
2779 ASString = "SYlocal";
2780 break;
2781 case LangAS::sycl_private:
2782 ASString = "SYprivate";
2783 break;
2784 // <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ]
2785 case LangAS::cuda_device:
2786 ASString = "CUdevice";
2787 break;
2788 case LangAS::cuda_constant:
2789 ASString = "CUconstant";
2790 break;
2791 case LangAS::cuda_shared:
2792 ASString = "CUshared";
2793 break;
2794 // <ptrsize-addrspace> ::= [ "ptr32_sptr" | "ptr32_uptr" | "ptr64" ]
2795 case LangAS::ptr32_sptr:
2796 ASString = "ptr32_sptr";
2797 break;
2798 case LangAS::ptr32_uptr:
2799 ASString = "ptr32_uptr";
2800 break;
2801 case LangAS::ptr64:
2802 ASString = "ptr64";
2803 break;
2804 }
2805 }
2806 if (!ASString.empty())
2807 mangleVendorQualifier(qualifier: ASString);
2808 }
2809
2810 // The ARC ownership qualifiers start with underscores.
2811 // Objective-C ARC Extension:
2812 //
2813 // <type> ::= U "__strong"
2814 // <type> ::= U "__weak"
2815 // <type> ::= U "__autoreleasing"
2816 //
2817 // Note: we emit __weak first to preserve the order as
2818 // required by the Itanium ABI.
2819 if (Quals.getObjCLifetime() == Qualifiers::OCL_Weak)
2820 mangleVendorQualifier(qualifier: "__weak");
2821
2822 // __unaligned (from -fms-extensions)
2823 if (Quals.hasUnaligned())
2824 mangleVendorQualifier(qualifier: "__unaligned");
2825
2826 // Remaining ARC ownership qualifiers.
2827 switch (Quals.getObjCLifetime()) {
2828 case Qualifiers::OCL_None:
2829 break;
2830
2831 case Qualifiers::OCL_Weak:
2832 // Do nothing as we already handled this case above.
2833 break;
2834
2835 case Qualifiers::OCL_Strong:
2836 mangleVendorQualifier(qualifier: "__strong");
2837 break;
2838
2839 case Qualifiers::OCL_Autoreleasing:
2840 mangleVendorQualifier(qualifier: "__autoreleasing");
2841 break;
2842
2843 case Qualifiers::OCL_ExplicitNone:
2844 // The __unsafe_unretained qualifier is *not* mangled, so that
2845 // __unsafe_unretained types in ARC produce the same manglings as the
2846 // equivalent (but, naturally, unqualified) types in non-ARC, providing
2847 // better ABI compatibility.
2848 //
2849 // It's safe to do this because unqualified 'id' won't show up
2850 // in any type signatures that need to be mangled.
2851 break;
2852 }
2853
2854 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
2855 if (Quals.hasRestrict())
2856 Out << 'r';
2857 if (Quals.hasVolatile())
2858 Out << 'V';
2859 if (Quals.hasConst())
2860 Out << 'K';
2861}
2862
2863void CXXNameMangler::mangleVendorQualifier(StringRef name) {
2864 Out << 'U' << name.size() << name;
2865}
2866
2867void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
2868 // <ref-qualifier> ::= R # lvalue reference
2869 // ::= O # rvalue-reference
2870 switch (RefQualifier) {
2871 case RQ_None:
2872 break;
2873
2874 case RQ_LValue:
2875 Out << 'R';
2876 break;
2877
2878 case RQ_RValue:
2879 Out << 'O';
2880 break;
2881 }
2882}
2883
2884void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
2885 Context.mangleObjCMethodNameAsSourceName(MD, Out);
2886}
2887
2888static bool isTypeSubstitutable(Qualifiers Quals, const Type *Ty,
2889 ASTContext &Ctx) {
2890 if (Quals)
2891 return true;
2892 if (Ty->isSpecificBuiltinType(K: BuiltinType::ObjCSel))
2893 return true;
2894 if (Ty->isOpenCLSpecificType())
2895 return true;
2896 // From Clang 18.0 we correctly treat SVE types as substitution candidates.
2897 if (Ty->isSVESizelessBuiltinType() &&
2898 Ctx.getLangOpts().getClangABICompat() > LangOptions::ClangABI::Ver17)
2899 return true;
2900 if (Ty->isBuiltinType())
2901 return false;
2902 // Through to Clang 6.0, we accidentally treated undeduced auto types as
2903 // substitution candidates.
2904 if (Ctx.getLangOpts().getClangABICompat() > LangOptions::ClangABI::Ver6 &&
2905 isa<AutoType>(Val: Ty))
2906 return false;
2907 // A placeholder type for class template deduction is substitutable with
2908 // its corresponding template name; this is handled specially when mangling
2909 // the type.
2910 if (auto *DeducedTST = Ty->getAs<DeducedTemplateSpecializationType>())
2911 if (DeducedTST->getDeducedType().isNull())
2912 return false;
2913 return true;
2914}
2915
2916void CXXNameMangler::mangleType(QualType T) {
2917 // If our type is instantiation-dependent but not dependent, we mangle
2918 // it as it was written in the source, removing any top-level sugar.
2919 // Otherwise, use the canonical type.
2920 //
2921 // FIXME: This is an approximation of the instantiation-dependent name
2922 // mangling rules, since we should really be using the type as written and
2923 // augmented via semantic analysis (i.e., with implicit conversions and
2924 // default template arguments) for any instantiation-dependent type.
2925 // Unfortunately, that requires several changes to our AST:
2926 // - Instantiation-dependent TemplateSpecializationTypes will need to be
2927 // uniqued, so that we can handle substitutions properly
2928 // - Default template arguments will need to be represented in the
2929 // TemplateSpecializationType, since they need to be mangled even though
2930 // they aren't written.
2931 // - Conversions on non-type template arguments need to be expressed, since
2932 // they can affect the mangling of sizeof/alignof.
2933 //
2934 // FIXME: This is wrong when mapping to the canonical type for a dependent
2935 // type discards instantiation-dependent portions of the type, such as for:
2936 //
2937 // template<typename T, int N> void f(T (&)[sizeof(N)]);
2938 // template<typename T> void f(T() throw(typename T::type)); (pre-C++17)
2939 //
2940 // It's also wrong in the opposite direction when instantiation-dependent,
2941 // canonically-equivalent types differ in some irrelevant portion of inner
2942 // type sugar. In such cases, we fail to form correct substitutions, eg:
2943 //
2944 // template<int N> void f(A<sizeof(N)> *, A<sizeof(N)> (*));
2945 //
2946 // We should instead canonicalize the non-instantiation-dependent parts,
2947 // regardless of whether the type as a whole is dependent or instantiation
2948 // dependent.
2949 if (!T->isInstantiationDependentType() || T->isDependentType())
2950 T = T.getCanonicalType();
2951 else {
2952 // Desugar any types that are purely sugar.
2953 do {
2954 // Don't desugar through template specialization types that aren't
2955 // type aliases. We need to mangle the template arguments as written.
2956 if (const TemplateSpecializationType *TST
2957 = dyn_cast<TemplateSpecializationType>(Val&: T))
2958 if (!TST->isTypeAlias())
2959 break;
2960
2961 // FIXME: We presumably shouldn't strip off ElaboratedTypes with
2962 // instantation-dependent qualifiers. See
2963 // https://github.com/itanium-cxx-abi/cxx-abi/issues/114.
2964
2965 QualType Desugared
2966 = T.getSingleStepDesugaredType(Context: Context.getASTContext());
2967 if (Desugared == T)
2968 break;
2969
2970 T = Desugared;
2971 } while (true);
2972 }
2973 SplitQualType split = T.split();
2974 Qualifiers quals = split.Quals;
2975 const Type *ty = split.Ty;
2976
2977 bool isSubstitutable =
2978 isTypeSubstitutable(Quals: quals, Ty: ty, Ctx&: Context.getASTContext());
2979 if (isSubstitutable && mangleSubstitution(T))
2980 return;
2981
2982 // If we're mangling a qualified array type, push the qualifiers to
2983 // the element type.
2984 if (quals && isa<ArrayType>(Val: T)) {
2985 ty = Context.getASTContext().getAsArrayType(T);
2986 quals = Qualifiers();
2987
2988 // Note that we don't update T: we want to add the
2989 // substitution at the original type.
2990 }
2991
2992 if (quals || ty->isDependentAddressSpaceType()) {
2993 if (const DependentAddressSpaceType *DAST =
2994 dyn_cast<DependentAddressSpaceType>(Val: ty)) {
2995 SplitQualType splitDAST = DAST->getPointeeType().split();
2996 mangleQualifiers(Quals: splitDAST.Quals, DAST);
2997 mangleType(T: QualType(splitDAST.Ty, 0));
2998 } else {
2999 mangleQualifiers(Quals: quals);
3000
3001 // Recurse: even if the qualified type isn't yet substitutable,
3002 // the unqualified type might be.
3003 mangleType(T: QualType(ty, 0));
3004 }
3005 } else {
3006 switch (ty->getTypeClass()) {
3007#define ABSTRACT_TYPE(CLASS, PARENT)
3008#define NON_CANONICAL_TYPE(CLASS, PARENT) \
3009 case Type::CLASS: \
3010 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
3011 return;
3012#define TYPE(CLASS, PARENT) \
3013 case Type::CLASS: \
3014 mangleType(static_cast<const CLASS##Type*>(ty)); \
3015 break;
3016#include "clang/AST/TypeNodes.inc"
3017 }
3018 }
3019
3020 // Add the substitution.
3021 if (isSubstitutable)
3022 addSubstitution(T);
3023}
3024
3025void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
3026 if (!mangleStandardSubstitution(ND))
3027 mangleName(GD: ND);
3028}
3029
3030void CXXNameMangler::mangleType(const BuiltinType *T) {
3031 // <type> ::= <builtin-type>
3032 // <builtin-type> ::= v # void
3033 // ::= w # wchar_t
3034 // ::= b # bool
3035 // ::= c # char
3036 // ::= a # signed char
3037 // ::= h # unsigned char
3038 // ::= s # short
3039 // ::= t # unsigned short
3040 // ::= i # int
3041 // ::= j # unsigned int
3042 // ::= l # long
3043 // ::= m # unsigned long
3044 // ::= x # long long, __int64
3045 // ::= y # unsigned long long, __int64
3046 // ::= n # __int128
3047 // ::= o # unsigned __int128
3048 // ::= f # float
3049 // ::= d # double
3050 // ::= e # long double, __float80
3051 // ::= g # __float128
3052 // ::= g # __ibm128
3053 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
3054 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
3055 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
3056 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3057 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point type _FloatN (N bits);
3058 // ::= Di # char32_t
3059 // ::= Ds # char16_t
3060 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3061 // ::= [DS] DA # N1169 fixed-point [_Sat] T _Accum
3062 // ::= [DS] DR # N1169 fixed-point [_Sat] T _Fract
3063 // ::= u <source-name> # vendor extended type
3064 //
3065 // <fixed-point-size>
3066 // ::= s # short
3067 // ::= t # unsigned short
3068 // ::= i # plain
3069 // ::= j # unsigned
3070 // ::= l # long
3071 // ::= m # unsigned long
3072 std::string type_name;
3073 // Normalize integer types as vendor extended types:
3074 // u<length>i<type size>
3075 // u<length>u<type size>
3076 if (NormalizeIntegers && T->isInteger()) {
3077 if (T->isSignedInteger()) {
3078 switch (getASTContext().getTypeSize(T)) {
3079 case 8:
3080 // Pick a representative for each integer size in the substitution
3081 // dictionary. (Its actual defined size is not relevant.)
3082 if (mangleSubstitution(Ptr: BuiltinType::SChar))
3083 break;
3084 Out << "u2i8";
3085 addSubstitution(Ptr: BuiltinType::SChar);
3086 break;
3087 case 16:
3088 if (mangleSubstitution(Ptr: BuiltinType::Short))
3089 break;
3090 Out << "u3i16";
3091 addSubstitution(Ptr: BuiltinType::Short);
3092 break;
3093 case 32:
3094 if (mangleSubstitution(Ptr: BuiltinType::Int))
3095 break;
3096 Out << "u3i32";
3097 addSubstitution(Ptr: BuiltinType::Int);
3098 break;
3099 case 64:
3100 if (mangleSubstitution(Ptr: BuiltinType::Long))
3101 break;
3102 Out << "u3i64";
3103 addSubstitution(Ptr: BuiltinType::Long);
3104 break;
3105 case 128:
3106 if (mangleSubstitution(Ptr: BuiltinType::Int128))
3107 break;
3108 Out << "u4i128";
3109 addSubstitution(Ptr: BuiltinType::Int128);
3110 break;
3111 default:
3112 llvm_unreachable("Unknown integer size for normalization");
3113 }
3114 } else {
3115 switch (getASTContext().getTypeSize(T)) {
3116 case 8:
3117 if (mangleSubstitution(Ptr: BuiltinType::UChar))
3118 break;
3119 Out << "u2u8";
3120 addSubstitution(Ptr: BuiltinType::UChar);
3121 break;
3122 case 16:
3123 if (mangleSubstitution(Ptr: BuiltinType::UShort))
3124 break;
3125 Out << "u3u16";
3126 addSubstitution(Ptr: BuiltinType::UShort);
3127 break;
3128 case 32:
3129 if (mangleSubstitution(Ptr: BuiltinType::UInt))
3130 break;
3131 Out << "u3u32";
3132 addSubstitution(Ptr: BuiltinType::UInt);
3133 break;
3134 case 64:
3135 if (mangleSubstitution(Ptr: BuiltinType::ULong))
3136 break;
3137 Out << "u3u64";
3138 addSubstitution(Ptr: BuiltinType::ULong);
3139 break;
3140 case 128:
3141 if (mangleSubstitution(Ptr: BuiltinType::UInt128))
3142 break;
3143 Out << "u4u128";
3144 addSubstitution(Ptr: BuiltinType::UInt128);
3145 break;
3146 default:
3147 llvm_unreachable("Unknown integer size for normalization");
3148 }
3149 }
3150 return;
3151 }
3152 switch (T->getKind()) {
3153 case BuiltinType::Void:
3154 Out << 'v';
3155 break;
3156 case BuiltinType::Bool:
3157 Out << 'b';
3158 break;
3159 case BuiltinType::Char_U:
3160 case BuiltinType::Char_S:
3161 Out << 'c';
3162 break;
3163 case BuiltinType::UChar:
3164 Out << 'h';
3165 break;
3166 case BuiltinType::UShort:
3167 Out << 't';
3168 break;
3169 case BuiltinType::UInt:
3170 Out << 'j';
3171 break;
3172 case BuiltinType::ULong:
3173 Out << 'm';
3174 break;
3175 case BuiltinType::ULongLong:
3176 Out << 'y';
3177 break;
3178 case BuiltinType::UInt128:
3179 Out << 'o';
3180 break;
3181 case BuiltinType::SChar:
3182 Out << 'a';
3183 break;
3184 case BuiltinType::WChar_S:
3185 case BuiltinType::WChar_U:
3186 Out << 'w';
3187 break;
3188 case BuiltinType::Char8:
3189 Out << "Du";
3190 break;
3191 case BuiltinType::Char16:
3192 Out << "Ds";
3193 break;
3194 case BuiltinType::Char32:
3195 Out << "Di";
3196 break;
3197 case BuiltinType::Short:
3198 Out << 's';
3199 break;
3200 case BuiltinType::Int:
3201 Out << 'i';
3202 break;
3203 case BuiltinType::Long:
3204 Out << 'l';
3205 break;
3206 case BuiltinType::LongLong:
3207 Out << 'x';
3208 break;
3209 case BuiltinType::Int128:
3210 Out << 'n';
3211 break;
3212 case BuiltinType::Float16:
3213 Out << "DF16_";
3214 break;
3215 case BuiltinType::ShortAccum:
3216 Out << "DAs";
3217 break;
3218 case BuiltinType::Accum:
3219 Out << "DAi";
3220 break;
3221 case BuiltinType::LongAccum:
3222 Out << "DAl";
3223 break;
3224 case BuiltinType::UShortAccum:
3225 Out << "DAt";
3226 break;
3227 case BuiltinType::UAccum:
3228 Out << "DAj";
3229 break;
3230 case BuiltinType::ULongAccum:
3231 Out << "DAm";
3232 break;
3233 case BuiltinType::ShortFract:
3234 Out << "DRs";
3235 break;
3236 case BuiltinType::Fract:
3237 Out << "DRi";
3238 break;
3239 case BuiltinType::LongFract:
3240 Out << "DRl";
3241 break;
3242 case BuiltinType::UShortFract:
3243 Out << "DRt";
3244 break;
3245 case BuiltinType::UFract:
3246 Out << "DRj";
3247 break;
3248 case BuiltinType::ULongFract:
3249 Out << "DRm";
3250 break;
3251 case BuiltinType::SatShortAccum:
3252 Out << "DSDAs";
3253 break;
3254 case BuiltinType::SatAccum:
3255 Out << "DSDAi";
3256 break;
3257 case BuiltinType::SatLongAccum:
3258 Out << "DSDAl";
3259 break;
3260 case BuiltinType::SatUShortAccum:
3261 Out << "DSDAt";
3262 break;
3263 case BuiltinType::SatUAccum:
3264 Out << "DSDAj";
3265 break;
3266 case BuiltinType::SatULongAccum:
3267 Out << "DSDAm";
3268 break;
3269 case BuiltinType::SatShortFract:
3270 Out << "DSDRs";
3271 break;
3272 case BuiltinType::SatFract:
3273 Out << "DSDRi";
3274 break;
3275 case BuiltinType::SatLongFract:
3276 Out << "DSDRl";
3277 break;
3278 case BuiltinType::SatUShortFract:
3279 Out << "DSDRt";
3280 break;
3281 case BuiltinType::SatUFract:
3282 Out << "DSDRj";
3283 break;
3284 case BuiltinType::SatULongFract:
3285 Out << "DSDRm";
3286 break;
3287 case BuiltinType::Half:
3288 Out << "Dh";
3289 break;
3290 case BuiltinType::Float:
3291 Out << 'f';
3292 break;
3293 case BuiltinType::Double:
3294 Out << 'd';
3295 break;
3296 case BuiltinType::LongDouble: {
3297 const TargetInfo *TI =
3298 getASTContext().getLangOpts().OpenMP &&
3299 getASTContext().getLangOpts().OpenMPIsTargetDevice
3300 ? getASTContext().getAuxTargetInfo()
3301 : &getASTContext().getTargetInfo();
3302 Out << TI->getLongDoubleMangling();
3303 break;
3304 }
3305 case BuiltinType::Float128: {
3306 const TargetInfo *TI =
3307 getASTContext().getLangOpts().OpenMP &&
3308 getASTContext().getLangOpts().OpenMPIsTargetDevice
3309 ? getASTContext().getAuxTargetInfo()
3310 : &getASTContext().getTargetInfo();
3311 Out << TI->getFloat128Mangling();
3312 break;
3313 }
3314 case BuiltinType::BFloat16: {
3315 const TargetInfo *TI =
3316 ((getASTContext().getLangOpts().OpenMP &&
3317 getASTContext().getLangOpts().OpenMPIsTargetDevice) ||
3318 getASTContext().getLangOpts().SYCLIsDevice)
3319 ? getASTContext().getAuxTargetInfo()
3320 : &getASTContext().getTargetInfo();
3321 Out << TI->getBFloat16Mangling();
3322 break;
3323 }
3324 case BuiltinType::Ibm128: {
3325 const TargetInfo *TI = &getASTContext().getTargetInfo();
3326 Out << TI->getIbm128Mangling();
3327 break;
3328 }
3329 case BuiltinType::NullPtr:
3330 Out << "Dn";
3331 break;
3332
3333#define BUILTIN_TYPE(Id, SingletonId)
3334#define PLACEHOLDER_TYPE(Id, SingletonId) \
3335 case BuiltinType::Id:
3336#include "clang/AST/BuiltinTypes.def"
3337 case BuiltinType::Dependent:
3338 if (!NullOut)
3339 llvm_unreachable("mangling a placeholder type");
3340 break;
3341 case BuiltinType::ObjCId:
3342 Out << "11objc_object";
3343 break;
3344 case BuiltinType::ObjCClass:
3345 Out << "10objc_class";
3346 break;
3347 case BuiltinType::ObjCSel:
3348 Out << "13objc_selector";
3349 break;
3350#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
3351 case BuiltinType::Id: \
3352 type_name = "ocl_" #ImgType "_" #Suffix; \
3353 Out << type_name.size() << type_name; \
3354 break;
3355#include "clang/Basic/OpenCLImageTypes.def"
3356 case BuiltinType::OCLSampler:
3357 Out << "11ocl_sampler";
3358 break;
3359 case BuiltinType::OCLEvent:
3360 Out << "9ocl_event";
3361 break;
3362 case BuiltinType::OCLClkEvent:
3363 Out << "12ocl_clkevent";
3364 break;
3365 case BuiltinType::OCLQueue:
3366 Out << "9ocl_queue";
3367 break;
3368 case BuiltinType::OCLReserveID:
3369 Out << "13ocl_reserveid";
3370 break;
3371#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
3372 case BuiltinType::Id: \
3373 type_name = "ocl_" #ExtType; \
3374 Out << type_name.size() << type_name; \
3375 break;
3376#include "clang/Basic/OpenCLExtensionTypes.def"
3377 // The SVE types are effectively target-specific. The mangling scheme
3378 // is defined in the appendices to the Procedure Call Standard for the
3379 // Arm Architecture.
3380#define SVE_VECTOR_TYPE(InternalName, MangledName, Id, SingletonId, NumEls, \
3381 ElBits, IsSigned, IsFP, IsBF) \
3382 case BuiltinType::Id: \
3383 if (T->getKind() == BuiltinType::SveBFloat16 && \
3384 isCompatibleWith(LangOptions::ClangABI::Ver17)) { \
3385 /* Prior to Clang 18.0 we used this incorrect mangled name */ \
3386 type_name = "__SVBFloat16_t"; \
3387 Out << "u" << type_name.size() << type_name; \
3388 } else { \
3389 type_name = MangledName; \
3390 Out << (type_name == InternalName ? "u" : "") << type_name.size() \
3391 << type_name; \
3392 } \
3393 break;
3394#define SVE_PREDICATE_TYPE(InternalName, MangledName, Id, SingletonId, NumEls) \
3395 case BuiltinType::Id: \
3396 type_name = MangledName; \
3397 Out << (type_name == InternalName ? "u" : "") << type_name.size() \
3398 << type_name; \
3399 break;
3400#define SVE_OPAQUE_TYPE(InternalName, MangledName, Id, SingletonId) \
3401 case BuiltinType::Id: \
3402 type_name = MangledName; \
3403 Out << (type_name == InternalName ? "u" : "") << type_name.size() \
3404 << type_name; \
3405 break;
3406#include "clang/Basic/AArch64SVEACLETypes.def"
3407#define PPC_VECTOR_TYPE(Name, Id, Size) \
3408 case BuiltinType::Id: \
3409 type_name = #Name; \
3410 Out << 'u' << type_name.size() << type_name; \
3411 break;
3412#include "clang/Basic/PPCTypes.def"
3413 // TODO: Check the mangling scheme for RISC-V V.
3414#define RVV_TYPE(Name, Id, SingletonId) \
3415 case BuiltinType::Id: \
3416 type_name = Name; \
3417 Out << 'u' << type_name.size() << type_name; \
3418 break;
3419#include "clang/Basic/RISCVVTypes.def"
3420#define WASM_REF_TYPE(InternalName, MangledName, Id, SingletonId, AS) \
3421 case BuiltinType::Id: \
3422 type_name = MangledName; \
3423 Out << 'u' << type_name.size() << type_name; \
3424 break;
3425#include "clang/Basic/WebAssemblyReferenceTypes.def"
3426 }
3427}
3428
3429StringRef CXXNameMangler::getCallingConvQualifierName(CallingConv CC) {
3430 switch (CC) {
3431 case CC_C:
3432 return "";
3433
3434 case CC_X86VectorCall:
3435 case CC_X86Pascal:
3436 case CC_X86RegCall:
3437 case CC_AAPCS:
3438 case CC_AAPCS_VFP:
3439 case CC_AArch64VectorCall:
3440 case CC_AArch64SVEPCS:
3441 case CC_AMDGPUKernelCall:
3442 case CC_IntelOclBicc:
3443 case CC_SpirFunction:
3444 case CC_OpenCLKernel:
3445 case CC_PreserveMost:
3446 case CC_PreserveAll:
3447 case CC_M68kRTD:
3448 case CC_PreserveNone:
3449 case CC_RISCVVectorCall:
3450 // FIXME: we should be mangling all of the above.
3451 return "";
3452
3453 case CC_X86ThisCall:
3454 // FIXME: To match mingw GCC, thiscall should only be mangled in when it is
3455 // used explicitly. At this point, we don't have that much information in
3456 // the AST, since clang tends to bake the convention into the canonical
3457 // function type. thiscall only rarely used explicitly, so don't mangle it
3458 // for now.
3459 return "";
3460
3461 case CC_X86StdCall:
3462 return "stdcall";
3463 case CC_X86FastCall:
3464 return "fastcall";
3465 case CC_X86_64SysV:
3466 return "sysv_abi";
3467 case CC_Win64:
3468 return "ms_abi";
3469 case CC_Swift:
3470 return "swiftcall";
3471 case CC_SwiftAsync:
3472 return "swiftasynccall";
3473 }
3474 llvm_unreachable("bad calling convention");
3475}
3476
3477void CXXNameMangler::mangleExtFunctionInfo(const FunctionType *T) {
3478 // Fast path.
3479 if (T->getExtInfo() == FunctionType::ExtInfo())
3480 return;
3481
3482 // Vendor-specific qualifiers are emitted in reverse alphabetical order.
3483 // This will get more complicated in the future if we mangle other
3484 // things here; but for now, since we mangle ns_returns_retained as
3485 // a qualifier on the result type, we can get away with this:
3486 StringRef CCQualifier = getCallingConvQualifierName(CC: T->getExtInfo().getCC());
3487 if (!CCQualifier.empty())
3488 mangleVendorQualifier(name: CCQualifier);
3489
3490 // FIXME: regparm
3491 // FIXME: noreturn
3492}
3493
3494void
3495CXXNameMangler::mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo PI) {
3496 // Vendor-specific qualifiers are emitted in reverse alphabetical order.
3497
3498 // Note that these are *not* substitution candidates. Demanglers might
3499 // have trouble with this if the parameter type is fully substituted.
3500
3501 switch (PI.getABI()) {
3502 case ParameterABI::Ordinary:
3503 break;
3504
3505 // All of these start with "swift", so they come before "ns_consumed".
3506 case ParameterABI::SwiftContext:
3507 case ParameterABI::SwiftAsyncContext:
3508 case ParameterABI::SwiftErrorResult:
3509 case ParameterABI::SwiftIndirectResult:
3510 mangleVendorQualifier(name: getParameterABISpelling(kind: PI.getABI()));
3511 break;
3512 }
3513
3514 if (PI.isConsumed())
3515 mangleVendorQualifier(name: "ns_consumed");
3516
3517 if (PI.isNoEscape())
3518 mangleVendorQualifier(name: "noescape");
3519}
3520
3521// <type> ::= <function-type>
3522// <function-type> ::= [<CV-qualifiers>] F [Y]
3523// <bare-function-type> [<ref-qualifier>] E
3524void CXXNameMangler::mangleType(const FunctionProtoType *T) {
3525 mangleExtFunctionInfo(T);
3526
3527 // Mangle CV-qualifiers, if present. These are 'this' qualifiers,
3528 // e.g. "const" in "int (A::*)() const".
3529 mangleQualifiers(Quals: T->getMethodQuals());
3530
3531 // Mangle instantiation-dependent exception-specification, if present,
3532 // per cxx-abi-dev proposal on 2016-10-11.
3533 if (T->hasInstantiationDependentExceptionSpec()) {
3534 if (isComputedNoexcept(ESpecType: T->getExceptionSpecType())) {
3535 Out << "DO";
3536 mangleExpression(E: T->getNoexceptExpr());
3537 Out << "E";
3538 } else {
3539 assert(T->getExceptionSpecType() == EST_Dynamic);
3540 Out << "Dw";
3541 for (auto ExceptTy : T->exceptions())
3542 mangleType(T: ExceptTy);
3543 Out << "E";
3544 }
3545 } else if (T->isNothrow()) {
3546 Out << "Do";
3547 }
3548
3549 Out << 'F';
3550
3551 // FIXME: We don't have enough information in the AST to produce the 'Y'
3552 // encoding for extern "C" function types.
3553 mangleBareFunctionType(T, /*MangleReturnType=*/true);
3554
3555 // Mangle the ref-qualifier, if present.
3556 mangleRefQualifier(RefQualifier: T->getRefQualifier());
3557
3558 Out << 'E';
3559}
3560
3561void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
3562 // Function types without prototypes can arise when mangling a function type
3563 // within an overloadable function in C. We mangle these as the absence of any
3564 // parameter types (not even an empty parameter list).
3565 Out << 'F';
3566
3567 FunctionTypeDepthState saved = FunctionTypeDepth.push();
3568
3569 FunctionTypeDepth.enterResultType();
3570 mangleType(T->getReturnType());
3571 FunctionTypeDepth.leaveResultType();
3572
3573 FunctionTypeDepth.pop(saved);
3574 Out << 'E';
3575}
3576
3577void CXXNameMangler::mangleBareFunctionType(const FunctionProtoType *Proto,
3578 bool MangleReturnType,
3579 const FunctionDecl *FD) {
3580 // Record that we're in a function type. See mangleFunctionParam
3581 // for details on what we're trying to achieve here.
3582 FunctionTypeDepthState saved = FunctionTypeDepth.push();
3583
3584 // <bare-function-type> ::= <signature type>+
3585 if (MangleReturnType) {
3586 FunctionTypeDepth.enterResultType();
3587
3588 // Mangle ns_returns_retained as an order-sensitive qualifier here.
3589 if (Proto->getExtInfo().getProducesResult() && FD == nullptr)
3590 mangleVendorQualifier(name: "ns_returns_retained");
3591
3592 // Mangle the return type without any direct ARC ownership qualifiers.
3593 QualType ReturnTy = Proto->getReturnType();
3594 if (ReturnTy.getObjCLifetime()) {
3595 auto SplitReturnTy = ReturnTy.split();
3596 SplitReturnTy.Quals.removeObjCLifetime();
3597 ReturnTy = getASTContext().getQualifiedType(SplitReturnTy);
3598 }
3599 mangleType(T: ReturnTy);
3600
3601 FunctionTypeDepth.leaveResultType();
3602 }
3603
3604 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
3605 // <builtin-type> ::= v # void
3606 Out << 'v';
3607 } else {
3608 assert(!FD || FD->getNumParams() == Proto->getNumParams());
3609 for (unsigned I = 0, E = Proto->getNumParams(); I != E; ++I) {
3610 // Mangle extended parameter info as order-sensitive qualifiers here.
3611 if (Proto->hasExtParameterInfos() && FD == nullptr) {
3612 mangleExtParameterInfo(PI: Proto->getExtParameterInfo(I));
3613 }
3614
3615 // Mangle the type.
3616 QualType ParamTy = Proto->getParamType(i: I);
3617 mangleType(T: Context.getASTContext().getSignatureParameterType(T: ParamTy));
3618
3619 if (FD) {
3620 if (auto *Attr = FD->getParamDecl(I)->getAttr<PassObjectSizeAttr>()) {
3621 // Attr can only take 1 character, so we can hardcode the length
3622 // below.
3623 assert(Attr->getType() <= 9 && Attr->getType() >= 0);
3624 if (Attr->isDynamic())
3625 Out << "U25pass_dynamic_object_size" << Attr->getType();
3626 else
3627 Out << "U17pass_object_size" << Attr->getType();
3628 }
3629 }
3630 }
3631
3632 // <builtin-type> ::= z # ellipsis
3633 if (Proto->isVariadic())
3634 Out << 'z';
3635 }
3636
3637 if (FD) {
3638 FunctionTypeDepth.enterResultType();
3639 mangleRequiresClause(RequiresClause: FD->getTrailingRequiresClause());
3640 }
3641
3642 FunctionTypeDepth.pop(saved);
3643}
3644
3645// <type> ::= <class-enum-type>
3646// <class-enum-type> ::= <name>
3647void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
3648 mangleName(T->getDecl());
3649}
3650
3651// <type> ::= <class-enum-type>
3652// <class-enum-type> ::= <name>
3653void CXXNameMangler::mangleType(const EnumType *T) {
3654 mangleType(static_cast<const TagType*>(T));
3655}
3656void CXXNameMangler::mangleType(const RecordType *T) {
3657 mangleType(static_cast<const TagType*>(T));
3658}
3659void CXXNameMangler::mangleType(const TagType *T) {
3660 mangleName(T->getDecl());
3661}
3662
3663// <type> ::= <array-type>
3664// <array-type> ::= A <positive dimension number> _ <element type>
3665// ::= A [<dimension expression>] _ <element type>
3666void CXXNameMangler::mangleType(const ConstantArrayType *T) {
3667 Out << 'A' << T->getSize() << '_';
3668 mangleType(T->getElementType());
3669}
3670void CXXNameMangler::mangleType(const VariableArrayType *T) {
3671 Out << 'A';
3672 // decayed vla types (size 0) will just be skipped.
3673 if (T->getSizeExpr())
3674 mangleExpression(E: T->getSizeExpr());
3675 Out << '_';
3676 mangleType(T->getElementType());
3677}
3678void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
3679 Out << 'A';
3680 // A DependentSizedArrayType might not have size expression as below
3681 //
3682 // template<int ...N> int arr[] = {N...};
3683 if (T->getSizeExpr())
3684 mangleExpression(E: T->getSizeExpr());
3685 Out << '_';
3686 mangleType(T->getElementType());
3687}
3688void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
3689 Out << "A_";
3690 mangleType(T->getElementType());
3691}
3692
3693// <type> ::= <pointer-to-member-type>
3694// <pointer-to-member-type> ::= M <class type> <member type>
3695void CXXNameMangler::mangleType(const MemberPointerType *T) {
3696 Out << 'M';
3697 mangleType(T: QualType(T->getClass(), 0));
3698 QualType PointeeType = T->getPointeeType();
3699 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Val&: PointeeType)) {
3700 mangleType(FPT);
3701
3702 // Itanium C++ ABI 5.1.8:
3703 //
3704 // The type of a non-static member function is considered to be different,
3705 // for the purposes of substitution, from the type of a namespace-scope or
3706 // static member function whose type appears similar. The types of two
3707 // non-static member functions are considered to be different, for the
3708 // purposes of substitution, if the functions are members of different
3709 // classes. In other words, for the purposes of substitution, the class of
3710 // which the function is a member is considered part of the type of
3711 // function.
3712
3713 // Given that we already substitute member function pointers as a
3714 // whole, the net effect of this rule is just to unconditionally
3715 // suppress substitution on the function type in a member pointer.
3716 // We increment the SeqID here to emulate adding an entry to the
3717 // substitution table.
3718 ++SeqID;
3719 } else
3720 mangleType(T: PointeeType);
3721}
3722
3723// <type> ::= <template-param>
3724void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
3725 mangleTemplateParameter(Depth: T->getDepth(), Index: T->getIndex());
3726}
3727
3728// <type> ::= <template-param>
3729void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
3730 // FIXME: not clear how to mangle this!
3731 // template <class T...> class A {
3732 // template <class U...> void foo(T(*)(U) x...);
3733 // };
3734 Out << "_SUBSTPACK_";
3735}
3736
3737// <type> ::= P <type> # pointer-to
3738void CXXNameMangler::mangleType(const PointerType *T) {
3739 Out << 'P';
3740 mangleType(T: T->getPointeeType());
3741}
3742void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
3743 Out << 'P';
3744 mangleType(T: T->getPointeeType());
3745}
3746
3747// <type> ::= R <type> # reference-to
3748void CXXNameMangler::mangleType(const LValueReferenceType *T) {
3749 Out << 'R';
3750 mangleType(T->getPointeeType());
3751}
3752
3753// <type> ::= O <type> # rvalue reference-to (C++0x)
3754void CXXNameMangler::mangleType(const RValueReferenceType *T) {
3755 Out << 'O';
3756 mangleType(T->getPointeeType());
3757}
3758
3759// <type> ::= C <type> # complex pair (C 2000)
3760void CXXNameMangler::mangleType(const ComplexType *T) {
3761 Out << 'C';
3762 mangleType(T: T->getElementType());
3763}
3764
3765// ARM's ABI for Neon vector types specifies that they should be mangled as
3766// if they are structs (to match ARM's initial implementation). The
3767// vector type must be one of the special types predefined by ARM.
3768void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
3769 QualType EltType = T->getElementType();
3770 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
3771 const char *EltName = nullptr;
3772 if (T->getVectorKind() == VectorKind::NeonPoly) {
3773 switch (cast<BuiltinType>(Val&: EltType)->getKind()) {
3774 case BuiltinType::SChar:
3775 case BuiltinType::UChar:
3776 EltName = "poly8_t";
3777 break;
3778 case BuiltinType::Short:
3779 case BuiltinType::UShort:
3780 EltName = "poly16_t";
3781 break;
3782 case BuiltinType::LongLong:
3783 case BuiltinType::ULongLong:
3784 EltName = "poly64_t";
3785 break;
3786 default: llvm_unreachable("unexpected Neon polynomial vector element type");
3787 }
3788 } else {
3789 switch (cast<BuiltinType>(Val&: EltType)->getKind()) {
3790 case BuiltinType::SChar: EltName = "int8_t"; break;
3791 case BuiltinType::UChar: EltName = "uint8_t"; break;
3792 case BuiltinType::Short: EltName = "int16_t"; break;
3793 case BuiltinType::UShort: EltName = "uint16_t"; break;
3794 case BuiltinType::Int: EltName = "int32_t"; break;
3795 case BuiltinType::UInt: EltName = "uint32_t"; break;
3796 case BuiltinType::LongLong: EltName = "int64_t"; break;
3797 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
3798 case BuiltinType::Double: EltName = "float64_t"; break;
3799 case BuiltinType::Float: EltName = "float32_t"; break;
3800 case BuiltinType::Half: EltName = "float16_t"; break;
3801 case BuiltinType::BFloat16: EltName = "bfloat16_t"; break;
3802 default:
3803 llvm_unreachable("unexpected Neon vector element type");
3804 }
3805 }
3806 const char *BaseName = nullptr;
3807 unsigned BitSize = (T->getNumElements() *
3808 getASTContext().getTypeSize(T: EltType));
3809 if (BitSize == 64)
3810 BaseName = "__simd64_";
3811 else {
3812 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
3813 BaseName = "__simd128_";
3814 }
3815 Out << strlen(s: BaseName) + strlen(s: EltName);
3816 Out << BaseName << EltName;
3817}
3818
3819void CXXNameMangler::mangleNeonVectorType(const DependentVectorType *T) {
3820 DiagnosticsEngine &Diags = Context.getDiags();
3821 unsigned DiagID = Diags.getCustomDiagID(
3822 L: DiagnosticsEngine::Error,
3823 FormatString: "cannot mangle this dependent neon vector type yet");
3824 Diags.Report(Loc: T->getAttributeLoc(), DiagID);
3825}
3826
3827static StringRef mangleAArch64VectorBase(const BuiltinType *EltType) {
3828 switch (EltType->getKind()) {
3829 case BuiltinType::SChar:
3830 return "Int8";
3831 case BuiltinType::Short:
3832 return "Int16";
3833 case BuiltinType::Int:
3834 return "Int32";
3835 case BuiltinType::Long:
3836 case BuiltinType::LongLong:
3837 return "Int64";
3838 case BuiltinType::UChar:
3839 return "Uint8";
3840 case BuiltinType::UShort:
3841 return "Uint16";
3842 case BuiltinType::UInt:
3843 return "Uint32";
3844 case BuiltinType::ULong:
3845 case BuiltinType::ULongLong:
3846 return "Uint64";
3847 case BuiltinType::Half:
3848 return "Float16";
3849 case BuiltinType::Float:
3850 return "Float32";
3851 case BuiltinType::Double:
3852 return "Float64";
3853 case BuiltinType::BFloat16:
3854 return "Bfloat16";
3855 default:
3856 llvm_unreachable("Unexpected vector element base type");
3857 }
3858}
3859
3860// AArch64's ABI for Neon vector types specifies that they should be mangled as
3861// the equivalent internal name. The vector type must be one of the special
3862// types predefined by ARM.
3863void CXXNameMangler::mangleAArch64NeonVectorType(const VectorType *T) {
3864 QualType EltType = T->getElementType();
3865 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
3866 unsigned BitSize =
3867 (T->getNumElements() * getASTContext().getTypeSize(T: EltType));
3868 (void)BitSize; // Silence warning.
3869
3870 assert((BitSize == 64 || BitSize == 128) &&
3871 "Neon vector type not 64 or 128 bits");
3872
3873 StringRef EltName;
3874 if (T->getVectorKind() == VectorKind::NeonPoly) {
3875 switch (cast<BuiltinType>(Val&: EltType)->getKind()) {
3876 case BuiltinType::UChar:
3877 EltName = "Poly8";
3878 break;
3879 case BuiltinType::UShort:
3880 EltName = "Poly16";
3881 break;
3882 case BuiltinType::ULong:
3883 case BuiltinType::ULongLong:
3884 EltName = "Poly64";
3885 break;
3886 default:
3887 llvm_unreachable("unexpected Neon polynomial vector element type");
3888 }
3889 } else
3890 EltName = mangleAArch64VectorBase(EltType: cast<BuiltinType>(Val&: EltType));
3891
3892 std::string TypeName =
3893 ("__" + EltName + "x" + Twine(T->getNumElements()) + "_t").str();
3894 Out << TypeName.length() << TypeName;
3895}
3896void CXXNameMangler::mangleAArch64NeonVectorType(const DependentVectorType *T) {
3897 DiagnosticsEngine &Diags = Context.getDiags();
3898 unsigned DiagID = Diags.getCustomDiagID(
3899 L: DiagnosticsEngine::Error,
3900 FormatString: "cannot mangle this dependent neon vector type yet");
3901 Diags.Report(Loc: T->getAttributeLoc(), DiagID);
3902}
3903
3904// The AArch64 ACLE specifies that fixed-length SVE vector and predicate types
3905// defined with the 'arm_sve_vector_bits' attribute map to the same AAPCS64
3906// type as the sizeless variants.
3907//
3908// The mangling scheme for VLS types is implemented as a "pseudo" template:
3909//
3910// '__SVE_VLS<<type>, <vector length>>'
3911//
3912// Combining the existing SVE type and a specific vector length (in bits).
3913// For example:
3914//
3915// typedef __SVInt32_t foo __attribute__((arm_sve_vector_bits(512)));
3916//
3917// is described as '__SVE_VLS<__SVInt32_t, 512u>' and mangled as:
3918//
3919// "9__SVE_VLSI" + base type mangling + "Lj" + __ARM_FEATURE_SVE_BITS + "EE"
3920//
3921// i.e. 9__SVE_VLSIu11__SVInt32_tLj512EE
3922//
3923// The latest ACLE specification (00bet5) does not contain details of this
3924// mangling scheme, it will be specified in the next revision. The mangling
3925// scheme is otherwise defined in the appendices to the Procedure Call Standard
3926// for the Arm Architecture, see
3927// https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#appendix-c-mangling
3928void CXXNameMangler::mangleAArch64FixedSveVectorType(const VectorType *T) {
3929 assert((T->getVectorKind() == VectorKind::SveFixedLengthData ||
3930 T->getVectorKind() == VectorKind::SveFixedLengthPredicate) &&
3931 "expected fixed-length SVE vector!");
3932
3933 QualType EltType = T->getElementType();
3934 assert(EltType->isBuiltinType() &&
3935 "expected builtin type for fixed-length SVE vector!");
3936
3937 StringRef TypeName;
3938 switch (cast<BuiltinType>(Val&: EltType)->getKind()) {
3939 case BuiltinType::SChar:
3940 TypeName = "__SVInt8_t";
3941 break;
3942 case BuiltinType::UChar: {
3943 if (T->getVectorKind() == VectorKind::SveFixedLengthData)
3944 TypeName = "__SVUint8_t";
3945 else
3946 TypeName = "__SVBool_t";
3947 break;
3948 }
3949 case BuiltinType::Short:
3950 TypeName = "__SVInt16_t";
3951 break;
3952 case BuiltinType::UShort:
3953 TypeName = "__SVUint16_t";
3954 break;
3955 case BuiltinType::Int:
3956 TypeName = "__SVInt32_t";
3957 break;
3958 case BuiltinType::UInt:
3959 TypeName = "__SVUint32_t";
3960 break;
3961 case BuiltinType::Long:
3962 TypeName = "__SVInt64_t";
3963 break;
3964 case BuiltinType::ULong:
3965 TypeName = "__SVUint64_t";
3966 break;
3967 case BuiltinType::Half:
3968 TypeName = "__SVFloat16_t";
3969 break;
3970 case BuiltinType::Float:
3971 TypeName = "__SVFloat32_t";
3972 break;
3973 case BuiltinType::Double:
3974 TypeName = "__SVFloat64_t";
3975 break;
3976 case BuiltinType::BFloat16:
3977 TypeName = "__SVBfloat16_t";
3978 break;
3979 default:
3980 llvm_unreachable("unexpected element type for fixed-length SVE vector!");
3981 }
3982
3983 unsigned VecSizeInBits = getASTContext().getTypeInfo(T).Width;
3984
3985 if (T->getVectorKind() == VectorKind::SveFixedLengthPredicate)
3986 VecSizeInBits *= 8;
3987
3988 Out << "9__SVE_VLSI" << 'u' << TypeName.size() << TypeName << "Lj"
3989 << VecSizeInBits << "EE";
3990}
3991
3992void CXXNameMangler::mangleAArch64FixedSveVectorType(
3993 const DependentVectorType *T) {
3994 DiagnosticsEngine &Diags = Context.getDiags();
3995 unsigned DiagID = Diags.getCustomDiagID(
3996 L: DiagnosticsEngine::Error,
3997 FormatString: "cannot mangle this dependent fixed-length SVE vector type yet");
3998 Diags.Report(Loc: T->getAttributeLoc(), DiagID);
3999}
4000
4001void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
4002 assert((T->getVectorKind() == VectorKind::RVVFixedLengthData ||
4003 T->getVectorKind() == VectorKind::RVVFixedLengthMask) &&
4004 "expected fixed-length RVV vector!");
4005
4006 QualType EltType = T->getElementType();
4007 assert(EltType->isBuiltinType() &&
4008 "expected builtin type for fixed-length RVV vector!");
4009
4010 SmallString<20> TypeNameStr;
4011 llvm::raw_svector_ostream TypeNameOS(TypeNameStr);
4012 TypeNameOS << "__rvv_";
4013 switch (cast<BuiltinType>(Val&: EltType)->getKind()) {
4014 case BuiltinType::SChar:
4015 TypeNameOS << "int8";
4016 break;
4017 case BuiltinType::UChar:
4018 if (T->getVectorKind() == VectorKind::RVVFixedLengthData)
4019 TypeNameOS << "uint8";
4020 else
4021 TypeNameOS << "bool";
4022 break;
4023 case BuiltinType::Short:
4024 TypeNameOS << "int16";
4025 break;
4026 case BuiltinType::UShort:
4027 TypeNameOS << "uint16";
4028 break;
4029 case BuiltinType::Int:
4030 TypeNameOS << "int32";
4031 break;
4032 case BuiltinType::UInt:
4033 TypeNameOS << "uint32";
4034 break;
4035 case BuiltinType::Long:
4036 TypeNameOS << "int64";
4037 break;
4038 case BuiltinType::ULong:
4039 TypeNameOS << "uint64";
4040 break;
4041 case BuiltinType::Float16:
4042 TypeNameOS << "float16";
4043 break;
4044 case BuiltinType::Float:
4045 TypeNameOS << "float32";
4046 break;
4047 case BuiltinType::Double:
4048 TypeNameOS << "float64";
4049 break;
4050 default:
4051 llvm_unreachable("unexpected element type for fixed-length RVV vector!");
4052 }
4053
4054 unsigned VecSizeInBits = getASTContext().getTypeInfo(T).Width;
4055
4056 // Apend the LMUL suffix.
4057 auto VScale = getASTContext().getTargetInfo().getVScaleRange(
4058 LangOpts: getASTContext().getLangOpts());
4059 unsigned VLen = VScale->first * llvm::RISCV::RVVBitsPerBlock;
4060
4061 if (T->getVectorKind() == VectorKind::RVVFixedLengthData) {
4062 TypeNameOS << 'm';
4063 if (VecSizeInBits >= VLen)
4064 TypeNameOS << (VecSizeInBits / VLen);
4065 else
4066 TypeNameOS << 'f' << (VLen / VecSizeInBits);
4067 } else {
4068 TypeNameOS << (VLen / VecSizeInBits);
4069 }
4070 TypeNameOS << "_t";
4071
4072 Out << "9__RVV_VLSI" << 'u' << TypeNameStr.size() << TypeNameStr << "Lj"
4073 << VecSizeInBits << "EE";
4074}
4075
4076void CXXNameMangler::mangleRISCVFixedRVVVectorType(
4077 const DependentVectorType *T) {
4078 DiagnosticsEngine &Diags = Context.getDiags();
4079 unsigned DiagID = Diags.getCustomDiagID(
4080 L: DiagnosticsEngine::Error,
4081 FormatString: "cannot mangle this dependent fixed-length RVV vector type yet");
4082 Diags.Report(Loc: T->getAttributeLoc(), DiagID);
4083}
4084
4085// GNU extension: vector types
4086// <type> ::= <vector-type>
4087// <vector-type> ::= Dv <positive dimension number> _
4088// <extended element type>
4089// ::= Dv [<dimension expression>] _ <element type>
4090// <extended element type> ::= <element type>
4091// ::= p # AltiVec vector pixel
4092// ::= b # Altivec vector bool
4093void CXXNameMangler::mangleType(const VectorType *T) {
4094 if ((T->getVectorKind() == VectorKind::Neon ||
4095 T->getVectorKind() == VectorKind::NeonPoly)) {
4096 llvm::Triple Target = getASTContext().getTargetInfo().getTriple();
4097 llvm::Triple::ArchType Arch =
4098 getASTContext().getTargetInfo().getTriple().getArch();
4099 if ((Arch == llvm::Triple::aarch64 ||
4100 Arch == llvm::Triple::aarch64_be) && !Target.isOSDarwin())
4101 mangleAArch64NeonVectorType(T);
4102 else
4103 mangleNeonVectorType(T);
4104 return;
4105 } else if (T->getVectorKind() == VectorKind::SveFixedLengthData ||
4106 T->getVectorKind() == VectorKind::SveFixedLengthPredicate) {
4107 mangleAArch64FixedSveVectorType(T);
4108 return;
4109 } else if (T->getVectorKind() == VectorKind::RVVFixedLengthData ||
4110 T->getVectorKind() == VectorKind::RVVFixedLengthMask) {
4111 mangleRISCVFixedRVVVectorType(T);
4112 return;
4113 }
4114 Out << "Dv" << T->getNumElements() << '_';
4115 if (T->getVectorKind() == VectorKind::AltiVecPixel)
4116 Out << 'p';
4117 else if (T->getVectorKind() == VectorKind::AltiVecBool)
4118 Out << 'b';
4119 else
4120 mangleType(T: T->getElementType());
4121}
4122
4123void CXXNameMangler::mangleType(const DependentVectorType *T) {
4124 if ((T->getVectorKind() == VectorKind::Neon ||
4125 T->getVectorKind() == VectorKind::NeonPoly)) {
4126 llvm::Triple Target = getASTContext().getTargetInfo().getTriple();
4127 llvm::Triple::ArchType Arch =
4128 getASTContext().getTargetInfo().getTriple().getArch();
4129 if ((Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_be) &&
4130 !Target.isOSDarwin())
4131 mangleAArch64NeonVectorType(T);
4132 else
4133 mangleNeonVectorType(T);
4134 return;
4135 } else if (T->getVectorKind() == VectorKind::SveFixedLengthData ||
4136 T->getVectorKind() == VectorKind::SveFixedLengthPredicate) {
4137 mangleAArch64FixedSveVectorType(T);
4138 return;
4139 } else if (T->getVectorKind() == VectorKind::RVVFixedLengthData) {
4140 mangleRISCVFixedRVVVectorType(T);
4141 return;
4142 }
4143
4144 Out << "Dv";
4145 mangleExpression(E: T->getSizeExpr());
4146 Out << '_';
4147 if (T->getVectorKind() == VectorKind::AltiVecPixel)
4148 Out << 'p';
4149 else if (T->getVectorKind() == VectorKind::AltiVecBool)
4150 Out << 'b';
4151 else
4152 mangleType(T: T->getElementType());
4153}
4154
4155void CXXNameMangler::mangleType(const ExtVectorType *T) {
4156 mangleType(static_cast<const VectorType*>(T));
4157}
4158void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
4159 Out << "Dv";
4160 mangleExpression(E: T->getSizeExpr());
4161 Out << '_';
4162 mangleType(T: T->getElementType());
4163}
4164
4165void CXXNameMangler::mangleType(const ConstantMatrixType *T) {
4166 // Mangle matrix types as a vendor extended type:
4167 // u<Len>matrix_typeI<Rows><Columns><element type>E
4168
4169 StringRef VendorQualifier = "matrix_type";
4170 Out << "u" << VendorQualifier.size() << VendorQualifier;
4171
4172 Out << "I";
4173 auto &ASTCtx = getASTContext();
4174 unsigned BitWidth = ASTCtx.getTypeSize(T: ASTCtx.getSizeType());
4175 llvm::APSInt Rows(BitWidth);
4176 Rows = T->getNumRows();
4177 mangleIntegerLiteral(T: ASTCtx.getSizeType(), Value: Rows);
4178 llvm::APSInt Columns(BitWidth);
4179 Columns = T->getNumColumns();
4180 mangleIntegerLiteral(T: ASTCtx.getSizeType(), Value: Columns);
4181 mangleType(T->getElementType());
4182 Out << "E";
4183}
4184
4185void CXXNameMangler::mangleType(const DependentSizedMatrixType *T) {
4186 // Mangle matrix types as a vendor extended type:
4187 // u<Len>matrix_typeI<row expr><column expr><element type>E
4188 StringRef VendorQualifier = "matrix_type";
4189 Out << "u" << VendorQualifier.size() << VendorQualifier;
4190
4191 Out << "I";
4192 mangleTemplateArgExpr(E: T->getRowExpr());
4193 mangleTemplateArgExpr(E: T->getColumnExpr());
4194 mangleType(T->getElementType());
4195 Out << "E";
4196}
4197
4198void CXXNameMangler::mangleType(const DependentAddressSpaceType *T) {
4199 SplitQualType split = T->getPointeeType().split();
4200 mangleQualifiers(Quals: split.Quals, DAST: T);
4201 mangleType(T: QualType(split.Ty, 0));
4202}
4203
4204void CXXNameMangler::mangleType(const PackExpansionType *T) {
4205 // <type> ::= Dp <type> # pack expansion (C++0x)
4206 Out << "Dp";
4207 mangleType(T: T->getPattern());
4208}
4209
4210void CXXNameMangler::mangleType(const PackIndexingType *T) {
4211 if (!T->hasSelectedType())
4212 mangleType(T: T->getPattern());
4213 else
4214 mangleType(T: T->getSelectedType());
4215}
4216
4217void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
4218 mangleSourceName(II: T->getDecl()->getIdentifier());
4219}
4220
4221void CXXNameMangler::mangleType(const ObjCObjectType *T) {
4222 // Treat __kindof as a vendor extended type qualifier.
4223 if (T->isKindOfType())
4224 Out << "U8__kindof";
4225
4226 if (!T->qual_empty()) {
4227 // Mangle protocol qualifiers.
4228 SmallString<64> QualStr;
4229 llvm::raw_svector_ostream QualOS(QualStr);
4230 QualOS << "objcproto";
4231 for (const auto *I : T->quals()) {
4232 StringRef name = I->getName();
4233 QualOS << name.size() << name;
4234 }
4235 Out << 'U' << QualStr.size() << QualStr;
4236 }
4237
4238 mangleType(T: T->getBaseType());
4239
4240 if (T->isSpecialized()) {
4241 // Mangle type arguments as I <type>+ E
4242 Out << 'I';
4243 for (auto typeArg : T->getTypeArgs())
4244 mangleType(T: typeArg);
4245 Out << 'E';
4246 }
4247}
4248
4249void CXXNameMangler::mangleType(const BlockPointerType *T) {
4250 Out << "U13block_pointer";
4251 mangleType(T: T->getPointeeType());
4252}
4253
4254void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
4255 // Mangle injected class name types as if the user had written the
4256 // specialization out fully. It may not actually be possible to see
4257 // this mangling, though.
4258 mangleType(T: T->getInjectedSpecializationType());
4259}
4260
4261void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
4262 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
4263 mangleTemplateName(TD, Args: T->template_arguments());
4264 } else {
4265 if (mangleSubstitution(T: QualType(T, 0)))
4266 return;
4267
4268 mangleTemplatePrefix(Template: T->getTemplateName());
4269
4270 // FIXME: GCC does not appear to mangle the template arguments when
4271 // the template in question is a dependent template name. Should we
4272 // emulate that badness?
4273 mangleTemplateArgs(TN: T->getTemplateName(), Args: T->template_arguments());
4274 addSubstitution(T: QualType(T, 0));
4275 }
4276}
4277
4278void CXXNameMangler::mangleType(const DependentNameType *T) {
4279 // Proposal by cxx-abi-dev, 2014-03-26
4280 // <class-enum-type> ::= <name> # non-dependent or dependent type name or
4281 // # dependent elaborated type specifier using
4282 // # 'typename'
4283 // ::= Ts <name> # dependent elaborated type specifier using
4284 // # 'struct' or 'class'
4285 // ::= Tu <name> # dependent elaborated type specifier using
4286 // # 'union'
4287 // ::= Te <name> # dependent elaborated type specifier using
4288 // # 'enum'
4289 switch (T->getKeyword()) {
4290 case ElaboratedTypeKeyword::None:
4291 case ElaboratedTypeKeyword::Typename:
4292 break;
4293 case ElaboratedTypeKeyword::Struct:
4294 case ElaboratedTypeKeyword::Class:
4295 case ElaboratedTypeKeyword::Interface:
4296 Out << "Ts";
4297 break;
4298 case ElaboratedTypeKeyword::Union:
4299 Out << "Tu";
4300 break;
4301 case ElaboratedTypeKeyword::Enum:
4302 Out << "Te";
4303 break;
4304 }
4305 // Typename types are always nested
4306 Out << 'N';
4307 manglePrefix(qualifier: T->getQualifier());
4308 mangleSourceName(II: T->getIdentifier());
4309 Out << 'E';
4310}
4311
4312void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
4313 // Dependently-scoped template types are nested if they have a prefix.
4314 Out << 'N';
4315
4316 // TODO: avoid making this TemplateName.
4317 TemplateName Prefix =
4318 getASTContext().getDependentTemplateName(NNS: T->getQualifier(),
4319 Name: T->getIdentifier());
4320 mangleTemplatePrefix(Template: Prefix);
4321
4322 // FIXME: GCC does not appear to mangle the template arguments when
4323 // the template in question is a dependent template name. Should we
4324 // emulate that badness?
4325 mangleTemplateArgs(TN: Prefix, Args: T->template_arguments());
4326 Out << 'E';
4327}
4328
4329void CXXNameMangler::mangleType(const TypeOfType *T) {
4330 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
4331 // "extension with parameters" mangling.
4332 Out << "u6typeof";
4333}
4334
4335void CXXNameMangler::mangleType(const TypeOfExprType *T) {
4336 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
4337 // "extension with parameters" mangling.
4338 Out << "u6typeof";
4339}
4340
4341void CXXNameMangler::mangleType(const DecltypeType *T) {
4342 Expr *E = T->getUnderlyingExpr();
4343
4344 // type ::= Dt <expression> E # decltype of an id-expression
4345 // # or class member access
4346 // ::= DT <expression> E # decltype of an expression
4347
4348 // This purports to be an exhaustive list of id-expressions and
4349 // class member accesses. Note that we do not ignore parentheses;
4350 // parentheses change the semantics of decltype for these
4351 // expressions (and cause the mangler to use the other form).
4352 if (isa<DeclRefExpr>(Val: E) ||
4353 isa<MemberExpr>(Val: E) ||
4354 isa<UnresolvedLookupExpr>(Val: E) ||
4355 isa<DependentScopeDeclRefExpr>(Val: E) ||
4356 isa<CXXDependentScopeMemberExpr>(Val: E) ||
4357 isa<UnresolvedMemberExpr>(Val: E))
4358 Out << "Dt";
4359 else
4360 Out << "DT";
4361 mangleExpression(E);
4362 Out << 'E';
4363}
4364
4365void CXXNameMangler::mangleType(const UnaryTransformType *T) {
4366 // If this is dependent, we need to record that. If not, we simply
4367 // mangle it as the underlying type since they are equivalent.
4368 if (T->isDependentType()) {
4369 Out << "u";
4370
4371 StringRef BuiltinName;
4372 switch (T->getUTTKind()) {
4373#define TRANSFORM_TYPE_TRAIT_DEF(Enum, Trait) \
4374 case UnaryTransformType::Enum: \
4375 BuiltinName = "__" #Trait; \
4376 break;
4377#include "clang/Basic/TransformTypeTraits.def"
4378 }
4379 Out << BuiltinName.size() << BuiltinName;
4380 }
4381
4382 Out << "I";
4383 mangleType(T: T->getBaseType());
4384 Out << "E";
4385}
4386
4387void CXXNameMangler::mangleType(const AutoType *T) {
4388 assert(T->getDeducedType().isNull() &&
4389 "Deduced AutoType shouldn't be handled here!");
4390 assert(T->getKeyword() != AutoTypeKeyword::GNUAutoType &&
4391 "shouldn't need to mangle __auto_type!");
4392 // <builtin-type> ::= Da # auto
4393 // ::= Dc # decltype(auto)
4394 // ::= Dk # constrained auto
4395 // ::= DK # constrained decltype(auto)
4396 if (T->isConstrained() && !isCompatibleWith(Ver: LangOptions::ClangABI::Ver17)) {
4397 Out << (T->isDecltypeAuto() ? "DK" : "Dk");
4398 mangleTypeConstraint(Concept: T->getTypeConstraintConcept(),
4399 Arguments: T->getTypeConstraintArguments());
4400 } else {
4401 Out << (T->isDecltypeAuto() ? "Dc" : "Da");
4402 }
4403}
4404
4405void CXXNameMangler::mangleType(const DeducedTemplateSpecializationType *T) {
4406 QualType Deduced = T->getDeducedType();
4407 if (!Deduced.isNull())
4408 return mangleType(T: Deduced);
4409
4410 TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl();
4411 assert(TD && "shouldn't form deduced TST unless we know we have a template");
4412
4413 if (mangleSubstitution(TD))
4414 return;
4415
4416 mangleName(GD: GlobalDecl(TD));
4417 addSubstitution(TD);
4418}
4419
4420void CXXNameMangler::mangleType(const AtomicType *T) {
4421 // <type> ::= U <source-name> <type> # vendor extended type qualifier
4422 // (Until there's a standardized mangling...)
4423 Out << "U7_Atomic";
4424 mangleType(T: T->getValueType());
4425}
4426
4427void CXXNameMangler::mangleType(const PipeType *T) {
4428 // Pipe type mangling rules are described in SPIR 2.0 specification
4429 // A.1 Data types and A.3 Summary of changes
4430 // <type> ::= 8ocl_pipe
4431 Out << "8ocl_pipe";
4432}
4433
4434void CXXNameMangler::mangleType(const BitIntType *T) {
4435 // 5.1.5.2 Builtin types
4436 // <type> ::= DB <number | instantiation-dependent expression> _
4437 // ::= DU <number | instantiation-dependent expression> _
4438 Out << "D" << (T->isUnsigned() ? "U" : "B") << T->getNumBits() << "_";
4439}
4440
4441void CXXNameMangler::mangleType(const DependentBitIntType *T) {
4442 // 5.1.5.2 Builtin types
4443 // <type> ::= DB <number | instantiation-dependent expression> _
4444 // ::= DU <number | instantiation-dependent expression> _
4445 Out << "D" << (T->isUnsigned() ? "U" : "B");
4446 mangleExpression(E: T->getNumBitsExpr());
4447 Out << "_";
4448}
4449
4450void CXXNameMangler::mangleType(const ArrayParameterType *T) {
4451 mangleType(cast<ConstantArrayType>(Val: T));
4452}
4453
4454void CXXNameMangler::mangleIntegerLiteral(QualType T,
4455 const llvm::APSInt &Value) {
4456 // <expr-primary> ::= L <type> <value number> E # integer literal
4457 Out << 'L';
4458
4459 mangleType(T);
4460 if (T->isBooleanType()) {
4461 // Boolean values are encoded as 0/1.
4462 Out << (Value.getBoolValue() ? '1' : '0');
4463 } else {
4464 mangleNumber(Value);
4465 }
4466 Out << 'E';
4467
4468}
4469
4470void CXXNameMangler::mangleMemberExprBase(const Expr *Base, bool IsArrow) {
4471 // Ignore member expressions involving anonymous unions.
4472 while (const auto *RT = Base->getType()->getAs<RecordType>()) {
4473 if (!RT->getDecl()->isAnonymousStructOrUnion())
4474 break;
4475 const auto *ME = dyn_cast<MemberExpr>(Val: Base);
4476 if (!ME)
4477 break;
4478 Base = ME->getBase();
4479 IsArrow = ME->isArrow();
4480 }
4481
4482 if (Base->isImplicitCXXThis()) {
4483 // Note: GCC mangles member expressions to the implicit 'this' as
4484 // *this., whereas we represent them as this->. The Itanium C++ ABI
4485 // does not specify anything here, so we follow GCC.
4486 Out << "dtdefpT";
4487 } else {
4488 Out << (IsArrow ? "pt" : "dt");
4489 mangleExpression(E: Base);
4490 }
4491}
4492
4493/// Mangles a member expression.
4494void CXXNameMangler::mangleMemberExpr(const Expr *base,
4495 bool isArrow,
4496 NestedNameSpecifier *qualifier,
4497 NamedDecl *firstQualifierLookup,
4498 DeclarationName member,
4499 const TemplateArgumentLoc *TemplateArgs,
4500 unsigned NumTemplateArgs,
4501 unsigned arity) {
4502 // <expression> ::= dt <expression> <unresolved-name>
4503 // ::= pt <expression> <unresolved-name>
4504 if (base)
4505 mangleMemberExprBase(Base: base, IsArrow: isArrow);
4506 mangleUnresolvedName(qualifier, name: member, TemplateArgs, NumTemplateArgs, knownArity: arity);
4507}
4508
4509/// Look at the callee of the given call expression and determine if
4510/// it's a parenthesized id-expression which would have triggered ADL
4511/// otherwise.
4512static bool isParenthesizedADLCallee(const CallExpr *call) {
4513 const Expr *callee = call->getCallee();
4514 const Expr *fn = callee->IgnoreParens();
4515
4516 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
4517 // too, but for those to appear in the callee, it would have to be
4518 // parenthesized.
4519 if (callee == fn) return false;
4520
4521 // Must be an unresolved lookup.
4522 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(Val: fn);
4523 if (!lookup) return false;
4524
4525 assert(!lookup->requiresADL());
4526
4527 // Must be an unqualified lookup.
4528 if (lookup->getQualifier()) return false;
4529
4530 // Must not have found a class member. Note that if one is a class
4531 // member, they're all class members.
4532 if (lookup->getNumDecls() > 0 &&
4533 (*lookup->decls_begin())->isCXXClassMember())
4534 return false;
4535
4536 // Otherwise, ADL would have been triggered.
4537 return true;
4538}
4539
4540void CXXNameMangler::mangleCastExpression(const Expr *E, StringRef CastEncoding) {
4541 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(Val: E);
4542 Out << CastEncoding;
4543 mangleType(ECE->getType());
4544 mangleExpression(E: ECE->getSubExpr());
4545}
4546
4547void CXXNameMangler::mangleInitListElements(const InitListExpr *InitList) {
4548 if (auto *Syntactic = InitList->getSyntacticForm())
4549 InitList = Syntactic;
4550 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
4551 mangleExpression(E: InitList->getInit(Init: i));
4552}
4553
4554void CXXNameMangler::mangleRequirement(SourceLocation RequiresExprLoc,
4555 const concepts::Requirement *Req) {
4556 using concepts::Requirement;
4557
4558 // TODO: We can't mangle the result of a failed substitution. It's not clear
4559 // whether we should be mangling the original form prior to any substitution
4560 // instead. See https://lists.isocpp.org/core/2023/04/14118.php
4561 auto HandleSubstitutionFailure =
4562 [&](SourceLocation Loc) {
4563 DiagnosticsEngine &Diags = Context.getDiags();
4564 unsigned DiagID = Diags.getCustomDiagID(
4565 L: DiagnosticsEngine::Error, FormatString: "cannot mangle this requires-expression "
4566 "containing a substitution failure");
4567 Diags.Report(Loc, DiagID);
4568 Out << 'F';
4569 };
4570
4571 switch (Req->getKind()) {
4572 case Requirement::RK_Type: {
4573 const auto *TR = cast<concepts::TypeRequirement>(Val: Req);
4574 if (TR->isSubstitutionFailure())
4575 return HandleSubstitutionFailure(
4576 TR->getSubstitutionDiagnostic()->DiagLoc);
4577
4578 Out << 'T';
4579 mangleType(T: TR->getType()->getType());
4580 break;
4581 }
4582
4583 case Requirement::RK_Simple:
4584 case Requirement::RK_Compound: {
4585 const auto *ER = cast<concepts::ExprRequirement>(Val: Req);
4586 if (ER->isExprSubstitutionFailure())
4587 return HandleSubstitutionFailure(
4588 ER->getExprSubstitutionDiagnostic()->DiagLoc);
4589
4590 Out << 'X';
4591 mangleExpression(E: ER->getExpr());
4592
4593 if (ER->hasNoexceptRequirement())
4594 Out << 'N';
4595
4596 if (!ER->getReturnTypeRequirement().isEmpty()) {
4597 if (ER->getReturnTypeRequirement().isSubstitutionFailure())
4598 return HandleSubstitutionFailure(ER->getReturnTypeRequirement()
4599 .getSubstitutionDiagnostic()
4600 ->DiagLoc);
4601
4602 Out << 'R';
4603 mangleTypeConstraint(Constraint: ER->getReturnTypeRequirement().getTypeConstraint());
4604 }
4605 break;
4606 }
4607
4608 case Requirement::RK_Nested:
4609 const auto *NR = cast<concepts::NestedRequirement>(Val: Req);
4610 if (NR->hasInvalidConstraint()) {
4611 // FIXME: NestedRequirement should track the location of its requires
4612 // keyword.
4613 return HandleSubstitutionFailure(RequiresExprLoc);
4614 }
4615
4616 Out << 'Q';
4617 mangleExpression(E: NR->getConstraintExpr());
4618 break;
4619 }
4620}
4621
4622void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity,
4623 bool AsTemplateArg) {
4624 // <expression> ::= <unary operator-name> <expression>
4625 // ::= <binary operator-name> <expression> <expression>
4626 // ::= <trinary operator-name> <expression> <expression> <expression>
4627 // ::= cv <type> expression # conversion with one argument
4628 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4629 // ::= dc <type> <expression> # dynamic_cast<type> (expression)
4630 // ::= sc <type> <expression> # static_cast<type> (expression)
4631 // ::= cc <type> <expression> # const_cast<type> (expression)
4632 // ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4633 // ::= st <type> # sizeof (a type)
4634 // ::= at <type> # alignof (a type)
4635 // ::= <template-param>
4636 // ::= <function-param>
4637 // ::= fpT # 'this' expression (part of <function-param>)
4638 // ::= sr <type> <unqualified-name> # dependent name
4639 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
4640 // ::= ds <expression> <expression> # expr.*expr
4641 // ::= sZ <template-param> # size of a parameter pack
4642 // ::= sZ <function-param> # size of a function parameter pack
4643 // ::= u <source-name> <template-arg>* E # vendor extended expression
4644 // ::= <expr-primary>
4645 // <expr-primary> ::= L <type> <value number> E # integer literal
4646 // ::= L <type> <value float> E # floating literal
4647 // ::= L <type> <string type> E # string literal
4648 // ::= L <nullptr type> E # nullptr literal "LDnE"
4649 // ::= L <pointer type> 0 E # null pointer template argument
4650 // ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C99); not used by clang
4651 // ::= L <mangled-name> E # external name
4652 QualType ImplicitlyConvertedToType;
4653
4654 // A top-level expression that's not <expr-primary> needs to be wrapped in
4655 // X...E in a template arg.
4656 bool IsPrimaryExpr = true;
4657 auto NotPrimaryExpr = [&] {
4658 if (AsTemplateArg && IsPrimaryExpr)
4659 Out << 'X';
4660 IsPrimaryExpr = false;
4661 };
4662
4663 auto MangleDeclRefExpr = [&](const NamedDecl *D) {
4664 switch (D->getKind()) {
4665 default:
4666 // <expr-primary> ::= L <mangled-name> E # external name
4667 Out << 'L';
4668 mangle(GD: D);
4669 Out << 'E';
4670 break;
4671
4672 case Decl::ParmVar:
4673 NotPrimaryExpr();
4674 mangleFunctionParam(parm: cast<ParmVarDecl>(Val: D));
4675 break;
4676
4677 case Decl::EnumConstant: {
4678 // <expr-primary>
4679 const EnumConstantDecl *ED = cast<EnumConstantDecl>(Val: D);
4680 mangleIntegerLiteral(T: ED->getType(), Value: ED->getInitVal());
4681 break;
4682 }
4683
4684 case Decl::NonTypeTemplateParm:
4685 NotPrimaryExpr();
4686 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(Val: D);
4687 mangleTemplateParameter(Depth: PD->getDepth(), Index: PD->getIndex());
4688 break;
4689 }
4690 };
4691
4692 // 'goto recurse' is used when handling a simple "unwrapping" node which
4693 // produces no output, where ImplicitlyConvertedToType and AsTemplateArg need
4694 // to be preserved.
4695recurse:
4696 switch (E->getStmtClass()) {
4697 case Expr::NoStmtClass:
4698#define ABSTRACT_STMT(Type)
4699#define EXPR(Type, Base)
4700#define STMT(Type, Base) \
4701 case Expr::Type##Class:
4702#include "clang/AST/StmtNodes.inc"
4703 // fallthrough
4704
4705 // These all can only appear in local or variable-initialization
4706 // contexts and so should never appear in a mangling.
4707 case Expr::AddrLabelExprClass:
4708 case Expr::DesignatedInitUpdateExprClass:
4709 case Expr::ImplicitValueInitExprClass:
4710 case Expr::ArrayInitLoopExprClass:
4711 case Expr::ArrayInitIndexExprClass:
4712 case Expr::NoInitExprClass:
4713 case Expr::ParenListExprClass:
4714 case Expr::MSPropertyRefExprClass:
4715 case Expr::MSPropertySubscriptExprClass:
4716 case Expr::TypoExprClass: // This should no longer exist in the AST by now.
4717 case Expr::RecoveryExprClass:
4718 case Expr::OMPArraySectionExprClass:
4719 case Expr::OMPArrayShapingExprClass:
4720 case Expr::OMPIteratorExprClass:
4721 case Expr::CXXInheritedCtorInitExprClass:
4722 case Expr::CXXParenListInitExprClass:
4723 case Expr::PackIndexingExprClass:
4724 llvm_unreachable("unexpected statement kind");
4725
4726 case Expr::ConstantExprClass:
4727 E = cast<ConstantExpr>(E)->getSubExpr();
4728 goto recurse;
4729
4730 // FIXME: invent manglings for all these.
4731 case Expr::BlockExprClass:
4732 case Expr::ChooseExprClass:
4733 case Expr::CompoundLiteralExprClass:
4734 case Expr::ExtVectorElementExprClass:
4735 case Expr::GenericSelectionExprClass:
4736 case Expr::ObjCEncodeExprClass:
4737 case Expr::ObjCIsaExprClass:
4738 case Expr::ObjCIvarRefExprClass:
4739 case Expr::ObjCMessageExprClass:
4740 case Expr::ObjCPropertyRefExprClass:
4741 case Expr::ObjCProtocolExprClass:
4742 case Expr::ObjCSelectorExprClass:
4743 case Expr::ObjCStringLiteralClass:
4744 case Expr::ObjCBoxedExprClass:
4745 case Expr::ObjCArrayLiteralClass:
4746 case Expr::ObjCDictionaryLiteralClass:
4747 case Expr::ObjCSubscriptRefExprClass:
4748 case Expr::ObjCIndirectCopyRestoreExprClass:
4749 case Expr::ObjCAvailabilityCheckExprClass:
4750 case Expr::OffsetOfExprClass:
4751 case Expr::PredefinedExprClass:
4752 case Expr::ShuffleVectorExprClass:
4753 case Expr::ConvertVectorExprClass:
4754 case Expr::StmtExprClass:
4755 case Expr::ArrayTypeTraitExprClass:
4756 case Expr::ExpressionTraitExprClass:
4757 case Expr::VAArgExprClass:
4758 case Expr::CUDAKernelCallExprClass:
4759 case Expr::AsTypeExprClass:
4760 case Expr::PseudoObjectExprClass:
4761 case Expr::AtomicExprClass:
4762 case Expr::SourceLocExprClass:
4763 case Expr::BuiltinBitCastExprClass:
4764 {
4765 NotPrimaryExpr();
4766 if (!NullOut) {
4767 // As bad as this diagnostic is, it's better than crashing.
4768 DiagnosticsEngine &Diags = Context.getDiags();
4769 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
4770 "cannot yet mangle expression type %0");
4771 Diags.Report(Loc: E->getExprLoc(), DiagID)
4772 << E->getStmtClassName() << E->getSourceRange();
4773 return;
4774 }
4775 break;
4776 }
4777
4778 case Expr::CXXUuidofExprClass: {
4779 NotPrimaryExpr();
4780 const CXXUuidofExpr *UE = cast<CXXUuidofExpr>(E);
4781 // As of clang 12, uuidof uses the vendor extended expression
4782 // mangling. Previously, it used a special-cased nonstandard extension.
4783 if (!isCompatibleWith(Ver: LangOptions::ClangABI::Ver11)) {
4784 Out << "u8__uuidof";
4785 if (UE->isTypeOperand())
4786 mangleType(T: UE->getTypeOperand(Context&: Context.getASTContext()));
4787 else
4788 mangleTemplateArgExpr(E: UE->getExprOperand());
4789 Out << 'E';
4790 } else {
4791 if (UE->isTypeOperand()) {
4792 QualType UuidT = UE->getTypeOperand(Context&: Context.getASTContext());
4793 Out << "u8__uuidoft";
4794 mangleType(T: UuidT);
4795 } else {
4796 Expr *UuidExp = UE->getExprOperand();
4797 Out << "u8__uuidofz";
4798 mangleExpression(E: UuidExp);
4799 }
4800 }
4801 break;
4802 }
4803
4804 // Even gcc-4.5 doesn't mangle this.
4805 case Expr::BinaryConditionalOperatorClass: {
4806 NotPrimaryExpr();
4807 DiagnosticsEngine &Diags = Context.getDiags();
4808 unsigned DiagID =
4809 Diags.getCustomDiagID(DiagnosticsEngine::Error,
4810 "?: operator with omitted middle operand cannot be mangled");
4811 Diags.Report(Loc: E->getExprLoc(), DiagID)
4812 << E->getStmtClassName() << E->getSourceRange();
4813 return;
4814 }
4815
4816 // These are used for internal purposes and cannot be meaningfully mangled.
4817 case Expr::OpaqueValueExprClass:
4818 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
4819
4820 case Expr::InitListExprClass: {
4821 NotPrimaryExpr();
4822 Out << "il";
4823 mangleInitListElements(InitList: cast<InitListExpr>(E));
4824 Out << "E";
4825 break;
4826 }
4827
4828 case Expr::DesignatedInitExprClass: {
4829 NotPrimaryExpr();
4830 auto *DIE = cast<DesignatedInitExpr>(E);
4831 for (const auto &Designator : DIE->designators()) {
4832 if (Designator.isFieldDesignator()) {
4833 Out << "di";
4834 mangleSourceName(Designator.getFieldName());
4835 } else if (Designator.isArrayDesignator()) {
4836 Out << "dx";
4837 mangleExpression(DIE->getArrayIndex(Designator));
4838 } else {
4839 assert(Designator.isArrayRangeDesignator() &&
4840 "unknown designator kind");
4841 Out << "dX";
4842 mangleExpression(DIE->getArrayRangeStart(Designator));
4843 mangleExpression(DIE->getArrayRangeEnd(Designator));
4844 }
4845 }
4846 mangleExpression(E: DIE->getInit());
4847 break;
4848 }
4849
4850 case Expr::CXXDefaultArgExprClass:
4851 E = cast<CXXDefaultArgExpr>(E)->getExpr();
4852 goto recurse;
4853
4854 case Expr::CXXDefaultInitExprClass:
4855 E = cast<CXXDefaultInitExpr>(E)->getExpr();
4856 goto recurse;
4857
4858 case Expr::CXXStdInitializerListExprClass:
4859 E = cast<CXXStdInitializerListExpr>(E)->getSubExpr();
4860 goto recurse;
4861
4862 case Expr::SubstNonTypeTemplateParmExprClass: {
4863 // Mangle a substituted parameter the same way we mangle the template
4864 // argument.
4865 auto *SNTTPE = cast<SubstNonTypeTemplateParmExpr>(E);
4866 if (auto *CE = dyn_cast<ConstantExpr>(SNTTPE->getReplacement())) {
4867 // Pull out the constant value and mangle it as a template argument.
4868 QualType ParamType = SNTTPE->getParameterType(Context.getASTContext());
4869 assert(CE->hasAPValueResult() && "expected the NTTP to have an APValue");
4870 mangleValueInTemplateArg(T: ParamType, V: CE->getAPValueResult(), TopLevel: false,
4871 /*NeedExactType=*/true);
4872 break;
4873 }
4874 // The remaining cases all happen to be substituted with expressions that
4875 // mangle the same as a corresponding template argument anyway.
4876 E = cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement();
4877 goto recurse;
4878 }
4879
4880 case Expr::UserDefinedLiteralClass:
4881 // We follow g++'s approach of mangling a UDL as a call to the literal
4882 // operator.
4883 case Expr::CXXMemberCallExprClass: // fallthrough
4884 case Expr::CallExprClass: {
4885 NotPrimaryExpr();
4886 const CallExpr *CE = cast<CallExpr>(E);
4887
4888 // <expression> ::= cp <simple-id> <expression>* E
4889 // We use this mangling only when the call would use ADL except
4890 // for being parenthesized. Per discussion with David
4891 // Vandervoorde, 2011.04.25.
4892 if (isParenthesizedADLCallee(call: CE)) {
4893 Out << "cp";
4894 // The callee here is a parenthesized UnresolvedLookupExpr with
4895 // no qualifier and should always get mangled as a <simple-id>
4896 // anyway.
4897
4898 // <expression> ::= cl <expression>* E
4899 } else {
4900 Out << "cl";
4901 }
4902
4903 unsigned CallArity = CE->getNumArgs();
4904 for (const Expr *Arg : CE->arguments())
4905 if (isa<PackExpansionExpr>(Arg))
4906 CallArity = UnknownArity;
4907
4908 mangleExpression(E: CE->getCallee(), Arity: CallArity);
4909 for (const Expr *Arg : CE->arguments())
4910 mangleExpression(Arg);
4911 Out << 'E';
4912 break;
4913 }
4914
4915 case Expr::CXXNewExprClass: {
4916 NotPrimaryExpr();
4917 const CXXNewExpr *New = cast<CXXNewExpr>(E);
4918 if (New->isGlobalNew()) Out << "gs";
4919 Out << (New->isArray() ? "na" : "nw");
4920 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
4921 E = New->placement_arg_end(); I != E; ++I)
4922 mangleExpression(*I);
4923 Out << '_';
4924 mangleType(T: New->getAllocatedType());
4925 if (New->hasInitializer()) {
4926 if (New->getInitializationStyle() == CXXNewInitializationStyle::Braces)
4927 Out << "il";
4928 else
4929 Out << "pi";
4930 const Expr *Init = New->getInitializer();
4931 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
4932 // Directly inline the initializers.
4933 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
4934 E = CCE->arg_end();
4935 I != E; ++I)
4936 mangleExpression(*I);
4937 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
4938 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
4939 mangleExpression(E: PLE->getExpr(Init: i));
4940 } else if (New->getInitializationStyle() ==
4941 CXXNewInitializationStyle::Braces &&
4942 isa<InitListExpr>(Init)) {
4943 // Only take InitListExprs apart for list-initialization.
4944 mangleInitListElements(InitList: cast<InitListExpr>(Init));
4945 } else
4946 mangleExpression(E: Init);
4947 }
4948 Out << 'E';
4949 break;
4950 }
4951
4952 case Expr::CXXPseudoDestructorExprClass: {
4953 NotPrimaryExpr();
4954 const auto *PDE = cast<CXXPseudoDestructorExpr>(E);
4955 if (const Expr *Base = PDE->getBase())
4956 mangleMemberExprBase(Base, IsArrow: PDE->isArrow());
4957 NestedNameSpecifier *Qualifier = PDE->getQualifier();
4958 if (TypeSourceInfo *ScopeInfo = PDE->getScopeTypeInfo()) {
4959 if (Qualifier) {
4960 mangleUnresolvedPrefix(qualifier: Qualifier,
4961 /*recursive=*/true);
4962 mangleUnresolvedTypeOrSimpleId(Ty: ScopeInfo->getType());
4963 Out << 'E';
4964 } else {
4965 Out << "sr";
4966 if (!mangleUnresolvedTypeOrSimpleId(Ty: ScopeInfo->getType()))
4967 Out << 'E';
4968 }
4969 } else if (Qualifier) {
4970 mangleUnresolvedPrefix(qualifier: Qualifier);
4971 }
4972 // <base-unresolved-name> ::= dn <destructor-name>
4973 Out << "dn";
4974 QualType DestroyedType = PDE->getDestroyedType();
4975 mangleUnresolvedTypeOrSimpleId(Ty: DestroyedType);
4976 break;
4977 }
4978
4979 case Expr::MemberExprClass: {
4980 NotPrimaryExpr();
4981 const MemberExpr *ME = cast<MemberExpr>(E);
4982 mangleMemberExpr(base: ME->getBase(), isArrow: ME->isArrow(),
4983 qualifier: ME->getQualifier(), firstQualifierLookup: nullptr,
4984 member: ME->getMemberDecl()->getDeclName(),
4985 TemplateArgs: ME->getTemplateArgs(), NumTemplateArgs: ME->getNumTemplateArgs(),
4986 arity: Arity);
4987 break;
4988 }
4989
4990 case Expr::UnresolvedMemberExprClass: {
4991 NotPrimaryExpr();
4992 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
4993 mangleMemberExpr(base: ME->isImplicitAccess() ? nullptr : ME->getBase(),
4994 isArrow: ME->isArrow(), qualifier: ME->getQualifier(), firstQualifierLookup: nullptr,
4995 member: ME->getMemberName(),
4996 TemplateArgs: ME->getTemplateArgs(), NumTemplateArgs: ME->getNumTemplateArgs(),
4997 arity: Arity);
4998 break;
4999 }
5000
5001 case Expr::CXXDependentScopeMemberExprClass: {
5002 NotPrimaryExpr();
5003 const CXXDependentScopeMemberExpr *ME
5004 = cast<CXXDependentScopeMemberExpr>(E);
5005 mangleMemberExpr(base: ME->isImplicitAccess() ? nullptr : ME->getBase(),
5006 isArrow: ME->isArrow(), qualifier: ME->getQualifier(),
5007 firstQualifierLookup: ME->getFirstQualifierFoundInScope(),
5008 member: ME->getMember(),
5009 TemplateArgs: ME->getTemplateArgs(), NumTemplateArgs: ME->getNumTemplateArgs(),
5010 arity: Arity);
5011 break;
5012 }
5013
5014 case Expr::UnresolvedLookupExprClass: {
5015 NotPrimaryExpr();
5016 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
5017 mangleUnresolvedName(qualifier: ULE->getQualifier(), name: ULE->getName(),
5018 TemplateArgs: ULE->getTemplateArgs(), NumTemplateArgs: ULE->getNumTemplateArgs(),
5019 knownArity: Arity);
5020 break;
5021 }
5022
5023 case Expr::CXXUnresolvedConstructExprClass: {
5024 NotPrimaryExpr();
5025 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
5026 unsigned N = CE->getNumArgs();
5027
5028 if (CE->isListInitialization()) {
5029 assert(N == 1 && "unexpected form for list initialization");
5030 auto *IL = cast<InitListExpr>(CE->getArg(I: 0));
5031 Out << "tl";
5032 mangleType(CE->getType());
5033 mangleInitListElements(InitList: IL);
5034 Out << "E";
5035 break;
5036 }
5037
5038 Out << "cv";
5039 mangleType(CE->getType());
5040 if (N != 1) Out << '_';
5041 for (unsigned I = 0; I != N; ++I) mangleExpression(E: CE->getArg(I));
5042 if (N != 1) Out << 'E';
5043 break;
5044 }
5045
5046 case Expr::CXXConstructExprClass: {
5047 // An implicit cast is silent, thus may contain <expr-primary>.
5048 const auto *CE = cast<CXXConstructExpr>(E);
5049 if (!CE->isListInitialization() || CE->isStdInitListInitialization()) {
5050 assert(
5051 CE->getNumArgs() >= 1 &&
5052 (CE->getNumArgs() == 1 || isa<CXXDefaultArgExpr>(CE->getArg(1))) &&
5053 "implicit CXXConstructExpr must have one argument");
5054 E = cast<CXXConstructExpr>(E)->getArg(0);
5055 goto recurse;
5056 }
5057 NotPrimaryExpr();
5058 Out << "il";
5059 for (auto *E : CE->arguments())
5060 mangleExpression(E);
5061 Out << "E";
5062 break;
5063 }
5064
5065 case Expr::CXXTemporaryObjectExprClass: {
5066 NotPrimaryExpr();
5067 const auto *CE = cast<CXXTemporaryObjectExpr>(E);
5068 unsigned N = CE->getNumArgs();
5069 bool List = CE->isListInitialization();
5070
5071 if (List)
5072 Out << "tl";
5073 else
5074 Out << "cv";
5075 mangleType(CE->getType());
5076 if (!List && N != 1)
5077 Out << '_';
5078 if (CE->isStdInitListInitialization()) {
5079 // We implicitly created a std::initializer_list<T> for the first argument
5080 // of a constructor of type U in an expression of the form U{a, b, c}.
5081 // Strip all the semantic gunk off the initializer list.
5082 auto *SILE =
5083 cast<CXXStdInitializerListExpr>(CE->getArg(0)->IgnoreImplicit());
5084 auto *ILE = cast<InitListExpr>(SILE->getSubExpr()->IgnoreImplicit());
5085 mangleInitListElements(InitList: ILE);
5086 } else {
5087 for (auto *E : CE->arguments())
5088 mangleExpression(E);
5089 }
5090 if (List || N != 1)
5091 Out << 'E';
5092 break;
5093 }
5094
5095 case Expr::CXXScalarValueInitExprClass:
5096 NotPrimaryExpr();
5097 Out << "cv";
5098 mangleType(T: E->getType());
5099 Out << "_E";
5100 break;
5101
5102 case Expr::CXXNoexceptExprClass:
5103 NotPrimaryExpr();
5104 Out << "nx";
5105 mangleExpression(E: cast<CXXNoexceptExpr>(E)->getOperand());
5106 break;
5107
5108 case Expr::UnaryExprOrTypeTraitExprClass: {
5109 // Non-instantiation-dependent traits are an <expr-primary> integer literal.
5110 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
5111
5112 if (!SAE->isInstantiationDependent()) {
5113 // Itanium C++ ABI:
5114 // If the operand of a sizeof or alignof operator is not
5115 // instantiation-dependent it is encoded as an integer literal
5116 // reflecting the result of the operator.
5117 //
5118 // If the result of the operator is implicitly converted to a known
5119 // integer type, that type is used for the literal; otherwise, the type
5120 // of std::size_t or std::ptrdiff_t is used.
5121 //
5122 // FIXME: We still include the operand in the profile in this case. This
5123 // can lead to mangling collisions between function templates that we
5124 // consider to be different.
5125 QualType T = (ImplicitlyConvertedToType.isNull() ||
5126 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
5127 : ImplicitlyConvertedToType;
5128 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
5129 mangleIntegerLiteral(T, Value: V);
5130 break;
5131 }
5132
5133 NotPrimaryExpr(); // But otherwise, they are not.
5134
5135 auto MangleAlignofSizeofArg = [&] {
5136 if (SAE->isArgumentType()) {
5137 Out << 't';
5138 mangleType(T: SAE->getArgumentType());
5139 } else {
5140 Out << 'z';
5141 mangleExpression(E: SAE->getArgumentExpr());
5142 }
5143 };
5144
5145 switch(SAE->getKind()) {
5146 case UETT_SizeOf:
5147 Out << 's';
5148 MangleAlignofSizeofArg();
5149 break;
5150 case UETT_PreferredAlignOf:
5151 // As of clang 12, we mangle __alignof__ differently than alignof. (They
5152 // have acted differently since Clang 8, but were previously mangled the
5153 // same.)
5154 if (!isCompatibleWith(Ver: LangOptions::ClangABI::Ver11)) {
5155 Out << "u11__alignof__";
5156 if (SAE->isArgumentType())
5157 mangleType(T: SAE->getArgumentType());
5158 else
5159 mangleTemplateArgExpr(E: SAE->getArgumentExpr());
5160 Out << 'E';
5161 break;
5162 }
5163 [[fallthrough]];
5164 case UETT_AlignOf:
5165 Out << 'a';
5166 MangleAlignofSizeofArg();
5167 break;
5168 case UETT_DataSizeOf: {
5169 DiagnosticsEngine &Diags = Context.getDiags();
5170 unsigned DiagID =
5171 Diags.getCustomDiagID(DiagnosticsEngine::Error,
5172 "cannot yet mangle __datasizeof expression");
5173 Diags.Report(DiagID);
5174 return;
5175 }
5176 case UETT_VecStep: {
5177 DiagnosticsEngine &Diags = Context.getDiags();
5178 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
5179 "cannot yet mangle vec_step expression");
5180 Diags.Report(DiagID);
5181 return;
5182 }
5183 case UETT_OpenMPRequiredSimdAlign: {
5184 DiagnosticsEngine &Diags = Context.getDiags();
5185 unsigned DiagID = Diags.getCustomDiagID(
5186 DiagnosticsEngine::Error,
5187 "cannot yet mangle __builtin_omp_required_simd_align expression");
5188 Diags.Report(DiagID);
5189 return;
5190 }
5191 case UETT_VectorElements: {
5192 DiagnosticsEngine &Diags = Context.getDiags();
5193 unsigned DiagID = Diags.getCustomDiagID(
5194 DiagnosticsEngine::Error,
5195 "cannot yet mangle __builtin_vectorelements expression");
5196 Diags.Report(DiagID);
5197 return;
5198 }
5199 }
5200 break;
5201 }
5202
5203 case Expr::TypeTraitExprClass: {
5204 // <expression> ::= u <source-name> <template-arg>* E # vendor extension
5205 const TypeTraitExpr *TTE = cast<TypeTraitExpr>(E);
5206 NotPrimaryExpr();
5207 Out << 'u';
5208 llvm::StringRef Spelling = getTraitSpelling(T: TTE->getTrait());
5209 Out << Spelling.size() << Spelling;
5210 for (TypeSourceInfo *TSI : TTE->getArgs()) {
5211 mangleType(TSI->getType());
5212 }
5213 Out << 'E';
5214 break;
5215 }
5216
5217 case Expr::CXXThrowExprClass: {
5218 NotPrimaryExpr();
5219 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
5220 // <expression> ::= tw <expression> # throw expression
5221 // ::= tr # rethrow
5222 if (TE->getSubExpr()) {
5223 Out << "tw";
5224 mangleExpression(E: TE->getSubExpr());
5225 } else {
5226 Out << "tr";
5227 }
5228 break;
5229 }
5230
5231 case Expr::CXXTypeidExprClass: {
5232 NotPrimaryExpr();
5233 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
5234 // <expression> ::= ti <type> # typeid (type)
5235 // ::= te <expression> # typeid (expression)
5236 if (TIE->isTypeOperand()) {
5237 Out << "ti";
5238 mangleType(T: TIE->getTypeOperand(Context&: Context.getASTContext()));
5239 } else {
5240 Out << "te";
5241 mangleExpression(E: TIE->getExprOperand());
5242 }
5243 break;
5244 }
5245
5246 case Expr::CXXDeleteExprClass: {
5247 NotPrimaryExpr();
5248 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
5249 // <expression> ::= [gs] dl <expression> # [::] delete expr
5250 // ::= [gs] da <expression> # [::] delete [] expr
5251 if (DE->isGlobalDelete()) Out << "gs";
5252 Out << (DE->isArrayForm() ? "da" : "dl");
5253 mangleExpression(E: DE->getArgument());
5254 break;
5255 }
5256
5257 case Expr::UnaryOperatorClass: {
5258 NotPrimaryExpr();
5259 const UnaryOperator *UO = cast<UnaryOperator>(E);
5260 mangleOperatorName(OO: UnaryOperator::getOverloadedOperator(Opc: UO->getOpcode()),
5261 /*Arity=*/1);
5262 mangleExpression(E: UO->getSubExpr());
5263 break;
5264 }
5265
5266 case Expr::ArraySubscriptExprClass: {
5267 NotPrimaryExpr();
5268 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
5269
5270 // Array subscript is treated as a syntactically weird form of
5271 // binary operator.
5272 Out << "ix";
5273 mangleExpression(E: AE->getLHS());
5274 mangleExpression(E: AE->getRHS());
5275 break;
5276 }
5277
5278 case Expr::MatrixSubscriptExprClass: {
5279 NotPrimaryExpr();
5280 const MatrixSubscriptExpr *ME = cast<MatrixSubscriptExpr>(E);
5281 Out << "ixix";
5282 mangleExpression(E: ME->getBase());
5283 mangleExpression(E: ME->getRowIdx());
5284 mangleExpression(E: ME->getColumnIdx());
5285 break;
5286 }
5287
5288 case Expr::CompoundAssignOperatorClass: // fallthrough
5289 case Expr::BinaryOperatorClass: {
5290 NotPrimaryExpr();
5291 const BinaryOperator *BO = cast<BinaryOperator>(E);
5292 if (BO->getOpcode() == BO_PtrMemD)
5293 Out << "ds";
5294 else
5295 mangleOperatorName(OO: BinaryOperator::getOverloadedOperator(Opc: BO->getOpcode()),
5296 /*Arity=*/2);
5297 mangleExpression(E: BO->getLHS());
5298 mangleExpression(E: BO->getRHS());
5299 break;
5300 }
5301
5302 case Expr::CXXRewrittenBinaryOperatorClass: {
5303 NotPrimaryExpr();
5304 // The mangled form represents the original syntax.
5305 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
5306 cast<CXXRewrittenBinaryOperator>(E)->getDecomposedForm();
5307 mangleOperatorName(OO: BinaryOperator::getOverloadedOperator(Opc: Decomposed.Opcode),
5308 /*Arity=*/2);
5309 mangleExpression(E: Decomposed.LHS);
5310 mangleExpression(E: Decomposed.RHS);
5311 break;
5312 }
5313
5314 case Expr::ConditionalOperatorClass: {
5315 NotPrimaryExpr();
5316 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
5317 mangleOperatorName(OO: OO_Conditional, /*Arity=*/3);
5318 mangleExpression(E: CO->getCond());
5319 mangleExpression(E: CO->getLHS(), Arity);
5320 mangleExpression(E: CO->getRHS(), Arity);
5321 break;
5322 }
5323
5324 case Expr::ImplicitCastExprClass: {
5325 ImplicitlyConvertedToType = E->getType();
5326 E = cast<ImplicitCastExpr>(E)->getSubExpr();
5327 goto recurse;
5328 }
5329
5330 case Expr::ObjCBridgedCastExprClass: {
5331 NotPrimaryExpr();
5332 // Mangle ownership casts as a vendor extended operator __bridge,
5333 // __bridge_transfer, or __bridge_retain.
5334 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
5335 Out << "v1U" << Kind.size() << Kind;
5336 mangleCastExpression(E, CastEncoding: "cv");
5337 break;
5338 }
5339
5340 case Expr::CStyleCastExprClass:
5341 NotPrimaryExpr();
5342 mangleCastExpression(E, CastEncoding: "cv");
5343 break;
5344
5345 case Expr::CXXFunctionalCastExprClass: {
5346 NotPrimaryExpr();
5347 auto *Sub = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreImplicit();
5348 // FIXME: Add isImplicit to CXXConstructExpr.
5349 if (auto *CCE = dyn_cast<CXXConstructExpr>(Sub))
5350 if (CCE->getParenOrBraceRange().isInvalid())
5351 Sub = CCE->getArg(0)->IgnoreImplicit();
5352 if (auto *StdInitList = dyn_cast<CXXStdInitializerListExpr>(Sub))
5353 Sub = StdInitList->getSubExpr()->IgnoreImplicit();
5354 if (auto *IL = dyn_cast<InitListExpr>(Sub)) {
5355 Out << "tl";
5356 mangleType(T: E->getType());
5357 mangleInitListElements(InitList: IL);
5358 Out << "E";
5359 } else {
5360 mangleCastExpression(E, CastEncoding: "cv");
5361 }
5362 break;
5363 }
5364
5365 case Expr::CXXStaticCastExprClass:
5366 NotPrimaryExpr();
5367 mangleCastExpression(E, CastEncoding: "sc");
5368 break;
5369 case Expr::CXXDynamicCastExprClass:
5370 NotPrimaryExpr();
5371 mangleCastExpression(E, CastEncoding: "dc");
5372 break;
5373 case Expr::CXXReinterpretCastExprClass:
5374 NotPrimaryExpr();
5375 mangleCastExpression(E, CastEncoding: "rc");
5376 break;
5377 case Expr::CXXConstCastExprClass:
5378 NotPrimaryExpr();
5379 mangleCastExpression(E, CastEncoding: "cc");
5380 break;
5381 case Expr::CXXAddrspaceCastExprClass:
5382 NotPrimaryExpr();
5383 mangleCastExpression(E, CastEncoding: "ac");
5384 break;
5385
5386 case Expr::CXXOperatorCallExprClass: {
5387 NotPrimaryExpr();
5388 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
5389 unsigned NumArgs = CE->getNumArgs();
5390 // A CXXOperatorCallExpr for OO_Arrow models only semantics, not syntax
5391 // (the enclosing MemberExpr covers the syntactic portion).
5392 if (CE->getOperator() != OO_Arrow)
5393 mangleOperatorName(OO: CE->getOperator(), /*Arity=*/NumArgs);
5394 // Mangle the arguments.
5395 for (unsigned i = 0; i != NumArgs; ++i)
5396 mangleExpression(E: CE->getArg(i));
5397 break;
5398 }
5399
5400 case Expr::ParenExprClass:
5401 E = cast<ParenExpr>(E)->getSubExpr();
5402 goto recurse;
5403
5404 case Expr::ConceptSpecializationExprClass: {
5405 auto *CSE = cast<ConceptSpecializationExpr>(E);
5406 if (isCompatibleWith(Ver: LangOptions::ClangABI::Ver17)) {
5407 // Clang 17 and before mangled concept-ids as if they resolved to an
5408 // entity, meaning that references to enclosing template arguments don't
5409 // work.
5410 Out << "L_Z";
5411 mangleTemplateName(TD: CSE->getNamedConcept(), Args: CSE->getTemplateArguments());
5412 Out << 'E';
5413 break;
5414 }
5415 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/24.
5416 NotPrimaryExpr();
5417 mangleUnresolvedName(
5418 qualifier: CSE->getNestedNameSpecifierLoc().getNestedNameSpecifier(),
5419 name: CSE->getConceptNameInfo().getName(),
5420 TemplateArgs: CSE->getTemplateArgsAsWritten()->getTemplateArgs(),
5421 NumTemplateArgs: CSE->getTemplateArgsAsWritten()->getNumTemplateArgs());
5422 break;
5423 }
5424
5425 case Expr::RequiresExprClass: {
5426 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/24.
5427 auto *RE = cast<RequiresExpr>(E);
5428 // This is a primary-expression in the C++ grammar, but does not have an
5429 // <expr-primary> mangling (starting with 'L').
5430 NotPrimaryExpr();
5431 if (RE->getLParenLoc().isValid()) {
5432 Out << "rQ";
5433 FunctionTypeDepthState saved = FunctionTypeDepth.push();
5434 if (RE->getLocalParameters().empty()) {
5435 Out << 'v';
5436 } else {
5437 for (ParmVarDecl *Param : RE->getLocalParameters()) {
5438 mangleType(Context.getASTContext().getSignatureParameterType(
5439 Param->getType()));
5440 }
5441 }
5442 Out << '_';
5443
5444 // The rest of the mangling is in the immediate scope of the parameters.
5445 FunctionTypeDepth.enterResultType();
5446 for (const concepts::Requirement *Req : RE->getRequirements())
5447 mangleRequirement(RE->getExprLoc(), Req);
5448 FunctionTypeDepth.pop(saved);
5449 Out << 'E';
5450 } else {
5451 Out << "rq";
5452 for (const concepts::Requirement *Req : RE->getRequirements())
5453 mangleRequirement(RE->getExprLoc(), Req);
5454 Out << 'E';
5455 }
5456 break;
5457 }
5458
5459 case Expr::DeclRefExprClass:
5460 // MangleDeclRefExpr helper handles primary-vs-nonprimary
5461 MangleDeclRefExpr(cast<DeclRefExpr>(E)->getDecl());
5462 break;
5463
5464 case Expr::SubstNonTypeTemplateParmPackExprClass:
5465 NotPrimaryExpr();
5466 // FIXME: not clear how to mangle this!
5467 // template <unsigned N...> class A {
5468 // template <class U...> void foo(U (&x)[N]...);
5469 // };
5470 Out << "_SUBSTPACK_";
5471 break;
5472
5473 case Expr::FunctionParmPackExprClass: {
5474 NotPrimaryExpr();
5475 // FIXME: not clear how to mangle this!
5476 const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E);
5477 Out << "v110_SUBSTPACK";
5478 MangleDeclRefExpr(FPPE->getParameterPack());
5479 break;
5480 }
5481
5482 case Expr::DependentScopeDeclRefExprClass: {
5483 NotPrimaryExpr();
5484 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
5485 mangleUnresolvedName(qualifier: DRE->getQualifier(), name: DRE->getDeclName(),
5486 TemplateArgs: DRE->getTemplateArgs(), NumTemplateArgs: DRE->getNumTemplateArgs(),
5487 knownArity: Arity);
5488 break;
5489 }
5490
5491 case Expr::CXXBindTemporaryExprClass:
5492 E = cast<CXXBindTemporaryExpr>(E)->getSubExpr();
5493 goto recurse;
5494
5495 case Expr::ExprWithCleanupsClass:
5496 E = cast<ExprWithCleanups>(E)->getSubExpr();
5497 goto recurse;
5498
5499 case Expr::FloatingLiteralClass: {
5500 // <expr-primary>
5501 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
5502 mangleFloatLiteral(T: FL->getType(), V: FL->getValue());
5503 break;
5504 }
5505
5506 case Expr::FixedPointLiteralClass:
5507 // Currently unimplemented -- might be <expr-primary> in future?
5508 mangleFixedPointLiteral();
5509 break;
5510
5511 case Expr::CharacterLiteralClass:
5512 // <expr-primary>
5513 Out << 'L';
5514 mangleType(T: E->getType());
5515 Out << cast<CharacterLiteral>(E)->getValue();
5516 Out << 'E';
5517 break;
5518
5519 // FIXME. __objc_yes/__objc_no are mangled same as true/false
5520 case Expr::ObjCBoolLiteralExprClass:
5521 // <expr-primary>
5522 Out << "Lb";
5523 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
5524 Out << 'E';
5525 break;
5526
5527 case Expr::CXXBoolLiteralExprClass:
5528 // <expr-primary>
5529 Out << "Lb";
5530 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
5531 Out << 'E';
5532 break;
5533
5534 case Expr::IntegerLiteralClass: {
5535 // <expr-primary>
5536 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
5537 if (E->getType()->isSignedIntegerType())
5538 Value.setIsSigned(true);
5539 mangleIntegerLiteral(T: E->getType(), Value);
5540 break;
5541 }
5542
5543 case Expr::ImaginaryLiteralClass: {
5544 // <expr-primary>
5545 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
5546 // Mangle as if a complex literal.
5547 // Proposal from David Vandevoorde, 2010.06.30.
5548 Out << 'L';
5549 mangleType(T: E->getType());
5550 if (const FloatingLiteral *Imag =
5551 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
5552 // Mangle a floating-point zero of the appropriate type.
5553 mangleFloat(f: llvm::APFloat(Imag->getValue().getSemantics()));
5554 Out << '_';
5555 mangleFloat(f: Imag->getValue());
5556 } else {
5557 Out << "0_";
5558 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
5559 if (IE->getSubExpr()->getType()->isSignedIntegerType())
5560 Value.setIsSigned(true);
5561 mangleNumber(Value);
5562 }
5563 Out << 'E';
5564 break;
5565 }
5566
5567 case Expr::StringLiteralClass: {
5568 // <expr-primary>
5569 // Revised proposal from David Vandervoorde, 2010.07.15.
5570 Out << 'L';
5571 assert(isa<ConstantArrayType>(E->getType()));
5572 mangleType(T: E->getType());
5573 Out << 'E';
5574 break;
5575 }
5576
5577 case Expr::GNUNullExprClass:
5578 // <expr-primary>
5579 // Mangle as if an integer literal 0.
5580 mangleIntegerLiteral(T: E->getType(), Value: llvm::APSInt(32));
5581 break;
5582
5583 case Expr::CXXNullPtrLiteralExprClass: {
5584 // <expr-primary>
5585 Out << "LDnE";
5586 break;
5587 }
5588
5589 case Expr::LambdaExprClass: {
5590 // A lambda-expression can't appear in the signature of an
5591 // externally-visible declaration, so there's no standard mangling for
5592 // this, but mangling as a literal of the closure type seems reasonable.
5593 Out << "L";
5594 mangleType(Context.getASTContext().getRecordType(Decl: cast<LambdaExpr>(E)->getLambdaClass()));
5595 Out << "E";
5596 break;
5597 }
5598
5599 case Expr::PackExpansionExprClass:
5600 NotPrimaryExpr();
5601 Out << "sp";
5602 mangleExpression(E: cast<PackExpansionExpr>(E)->getPattern());
5603 break;
5604
5605 case Expr::SizeOfPackExprClass: {
5606 NotPrimaryExpr();
5607 auto *SPE = cast<SizeOfPackExpr>(E);
5608 if (SPE->isPartiallySubstituted()) {
5609 Out << "sP";
5610 for (const auto &A : SPE->getPartialArguments())
5611 mangleTemplateArg(A, false);
5612 Out << "E";
5613 break;
5614 }
5615
5616 Out << "sZ";
5617 const NamedDecl *Pack = SPE->getPack();
5618 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
5619 mangleTemplateParameter(Depth: TTP->getDepth(), Index: TTP->getIndex());
5620 else if (const NonTypeTemplateParmDecl *NTTP
5621 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
5622 mangleTemplateParameter(Depth: NTTP->getDepth(), Index: NTTP->getIndex());
5623 else if (const TemplateTemplateParmDecl *TempTP
5624 = dyn_cast<TemplateTemplateParmDecl>(Pack))
5625 mangleTemplateParameter(Depth: TempTP->getDepth(), Index: TempTP->getIndex());
5626 else
5627 mangleFunctionParam(parm: cast<ParmVarDecl>(Pack));
5628 break;
5629 }
5630
5631 case Expr::MaterializeTemporaryExprClass:
5632 E = cast<MaterializeTemporaryExpr>(E)->getSubExpr();
5633 goto recurse;
5634
5635 case Expr::CXXFoldExprClass: {
5636 NotPrimaryExpr();
5637 auto *FE = cast<CXXFoldExpr>(E);
5638 if (FE->isLeftFold())
5639 Out << (FE->getInit() ? "fL" : "fl");
5640 else
5641 Out << (FE->getInit() ? "fR" : "fr");
5642
5643 if (FE->getOperator() == BO_PtrMemD)
5644 Out << "ds";
5645 else
5646 mangleOperatorName(
5647 BinaryOperator::getOverloadedOperator(Opc: FE->getOperator()),
5648 /*Arity=*/2);
5649
5650 if (FE->getLHS())
5651 mangleExpression(E: FE->getLHS());
5652 if (FE->getRHS())
5653 mangleExpression(E: FE->getRHS());
5654 break;
5655 }
5656
5657 case Expr::CXXThisExprClass:
5658 NotPrimaryExpr();
5659 Out << "fpT";
5660 break;
5661
5662 case Expr::CoawaitExprClass:
5663 // FIXME: Propose a non-vendor mangling.
5664 NotPrimaryExpr();
5665 Out << "v18co_await";
5666 mangleExpression(E: cast<CoawaitExpr>(E)->getOperand());
5667 break;
5668
5669 case Expr::DependentCoawaitExprClass:
5670 // FIXME: Propose a non-vendor mangling.
5671 NotPrimaryExpr();
5672 Out << "v18co_await";
5673 mangleExpression(E: cast<DependentCoawaitExpr>(E)->getOperand());
5674 break;
5675
5676 case Expr::CoyieldExprClass:
5677 // FIXME: Propose a non-vendor mangling.
5678 NotPrimaryExpr();
5679 Out << "v18co_yield";
5680 mangleExpression(E: cast<CoawaitExpr>(E)->getOperand());
5681 break;
5682 case Expr::SYCLUniqueStableNameExprClass: {
5683 const auto *USN = cast<SYCLUniqueStableNameExpr>(E);
5684 NotPrimaryExpr();
5685
5686 Out << "u33__builtin_sycl_unique_stable_name";
5687 mangleType(USN->getTypeSourceInfo()->getType());
5688
5689 Out << "E";
5690 break;
5691 }
5692 }
5693
5694 if (AsTemplateArg && !IsPrimaryExpr)
5695 Out << 'E';
5696}
5697
5698/// Mangle an expression which refers to a parameter variable.
5699///
5700/// <expression> ::= <function-param>
5701/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
5702/// <function-param> ::= fp <top-level CV-qualifiers>
5703/// <parameter-2 non-negative number> _ # L == 0, I > 0
5704/// <function-param> ::= fL <L-1 non-negative number>
5705/// p <top-level CV-qualifiers> _ # L > 0, I == 0
5706/// <function-param> ::= fL <L-1 non-negative number>
5707/// p <top-level CV-qualifiers>
5708/// <I-1 non-negative number> _ # L > 0, I > 0
5709///
5710/// L is the nesting depth of the parameter, defined as 1 if the
5711/// parameter comes from the innermost function prototype scope
5712/// enclosing the current context, 2 if from the next enclosing
5713/// function prototype scope, and so on, with one special case: if
5714/// we've processed the full parameter clause for the innermost
5715/// function type, then L is one less. This definition conveniently
5716/// makes it irrelevant whether a function's result type was written
5717/// trailing or leading, but is otherwise overly complicated; the
5718/// numbering was first designed without considering references to
5719/// parameter in locations other than return types, and then the
5720/// mangling had to be generalized without changing the existing
5721/// manglings.
5722///
5723/// I is the zero-based index of the parameter within its parameter
5724/// declaration clause. Note that the original ABI document describes
5725/// this using 1-based ordinals.
5726void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
5727 unsigned parmDepth = parm->getFunctionScopeDepth();
5728 unsigned parmIndex = parm->getFunctionScopeIndex();
5729
5730 // Compute 'L'.
5731 // parmDepth does not include the declaring function prototype.
5732 // FunctionTypeDepth does account for that.
5733 assert(parmDepth < FunctionTypeDepth.getDepth());
5734 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
5735 if (FunctionTypeDepth.isInResultType())
5736 nestingDepth--;
5737
5738 if (nestingDepth == 0) {
5739 Out << "fp";
5740 } else {
5741 Out << "fL" << (nestingDepth - 1) << 'p';
5742 }
5743
5744 // Top-level qualifiers. We don't have to worry about arrays here,
5745 // because parameters declared as arrays should already have been
5746 // transformed to have pointer type. FIXME: apparently these don't
5747 // get mangled if used as an rvalue of a known non-class type?
5748 assert(!parm->getType()->isArrayType()
5749 && "parameter's type is still an array type?");
5750
5751 if (const DependentAddressSpaceType *DAST =
5752 dyn_cast<DependentAddressSpaceType>(parm->getType())) {
5753 mangleQualifiers(Quals: DAST->getPointeeType().getQualifiers(), DAST);
5754 } else {
5755 mangleQualifiers(Quals: parm->getType().getQualifiers());
5756 }
5757
5758 // Parameter index.
5759 if (parmIndex != 0) {
5760 Out << (parmIndex - 1);
5761 }
5762 Out << '_';
5763}
5764
5765void CXXNameMangler::mangleCXXCtorType(CXXCtorType T,
5766 const CXXRecordDecl *InheritedFrom) {
5767 // <ctor-dtor-name> ::= C1 # complete object constructor
5768 // ::= C2 # base object constructor
5769 // ::= CI1 <type> # complete inheriting constructor
5770 // ::= CI2 <type> # base inheriting constructor
5771 //
5772 // In addition, C5 is a comdat name with C1 and C2 in it.
5773 Out << 'C';
5774 if (InheritedFrom)
5775 Out << 'I';
5776 switch (T) {
5777 case Ctor_Complete:
5778 Out << '1';
5779 break;
5780 case Ctor_Base:
5781 Out << '2';
5782 break;
5783 case Ctor_Comdat:
5784 Out << '5';
5785 break;
5786 case Ctor_DefaultClosure:
5787 case Ctor_CopyingClosure:
5788 llvm_unreachable("closure constructors don't exist for the Itanium ABI!");
5789 }
5790 if (InheritedFrom)
5791 mangleName(InheritedFrom);
5792}
5793
5794void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
5795 // <ctor-dtor-name> ::= D0 # deleting destructor
5796 // ::= D1 # complete object destructor
5797 // ::= D2 # base object destructor
5798 //
5799 // In addition, D5 is a comdat name with D1, D2 and, if virtual, D0 in it.
5800 switch (T) {
5801 case Dtor_Deleting:
5802 Out << "D0";
5803 break;
5804 case Dtor_Complete:
5805 Out << "D1";
5806 break;
5807 case Dtor_Base:
5808 Out << "D2";
5809 break;
5810 case Dtor_Comdat:
5811 Out << "D5";
5812 break;
5813 }
5814}
5815
5816// Helper to provide ancillary information on a template used to mangle its
5817// arguments.
5818struct CXXNameMangler::TemplateArgManglingInfo {
5819 const CXXNameMangler &Mangler;
5820 TemplateDecl *ResolvedTemplate = nullptr;
5821 bool SeenPackExpansionIntoNonPack = false;
5822 const NamedDecl *UnresolvedExpandedPack = nullptr;
5823
5824 TemplateArgManglingInfo(const CXXNameMangler &Mangler, TemplateName TN)
5825 : Mangler(Mangler) {
5826 if (TemplateDecl *TD = TN.getAsTemplateDecl())
5827 ResolvedTemplate = TD;
5828 }
5829
5830 /// Information about how to mangle a template argument.
5831 struct Info {
5832 /// Do we need to mangle the template argument with an exactly correct type?
5833 bool NeedExactType;
5834 /// If we need to prefix the mangling with a mangling of the template
5835 /// parameter, the corresponding parameter.
5836 const NamedDecl *TemplateParameterToMangle;
5837 };
5838
5839 /// Determine whether the resolved template might be overloaded on its
5840 /// template parameter list. If so, the mangling needs to include enough
5841 /// information to reconstruct the template parameter list.
5842 bool isOverloadable() {
5843 // Function templates are generally overloadable. As a special case, a
5844 // member function template of a generic lambda is not overloadable.
5845 if (auto *FTD = dyn_cast_or_null<FunctionTemplateDecl>(Val: ResolvedTemplate)) {
5846 auto *RD = dyn_cast<CXXRecordDecl>(FTD->getDeclContext());
5847 if (!RD || !RD->isGenericLambda())
5848 return true;
5849 }
5850
5851 // All other templates are not overloadable. Partial specializations would
5852 // be, but we never mangle them.
5853 return false;
5854 }
5855
5856 /// Determine whether we need to prefix this <template-arg> mangling with a
5857 /// <template-param-decl>. This happens if the natural template parameter for
5858 /// the argument mangling is not the same as the actual template parameter.
5859 bool needToMangleTemplateParam(const NamedDecl *Param,
5860 const TemplateArgument &Arg) {
5861 // For a template type parameter, the natural parameter is 'typename T'.
5862 // The actual parameter might be constrained.
5863 if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(Val: Param))
5864 return TTP->hasTypeConstraint();
5865
5866 if (Arg.getKind() == TemplateArgument::Pack) {
5867 // For an empty pack, the natural parameter is `typename...`.
5868 if (Arg.pack_size() == 0)
5869 return true;
5870
5871 // For any other pack, we use the first argument to determine the natural
5872 // template parameter.
5873 return needToMangleTemplateParam(Param, Arg: *Arg.pack_begin());
5874 }
5875
5876 // For a non-type template parameter, the natural parameter is `T V` (for a
5877 // prvalue argument) or `T &V` (for a glvalue argument), where `T` is the
5878 // type of the argument, which we require to exactly match. If the actual
5879 // parameter has a deduced or instantiation-dependent type, it is not
5880 // equivalent to the natural parameter.
5881 if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: Param))
5882 return NTTP->getType()->isInstantiationDependentType() ||
5883 NTTP->getType()->getContainedDeducedType();
5884
5885 // For a template template parameter, the template-head might differ from
5886 // that of the template.
5887 auto *TTP = cast<TemplateTemplateParmDecl>(Val: Param);
5888 TemplateName ArgTemplateName = Arg.getAsTemplateOrTemplatePattern();
5889 const TemplateDecl *ArgTemplate = ArgTemplateName.getAsTemplateDecl();
5890 if (!ArgTemplate)
5891 return true;
5892
5893 // Mangle the template parameter list of the parameter and argument to see
5894 // if they are the same. We can't use Profile for this, because it can't
5895 // model the depth difference between parameter and argument and might not
5896 // necessarily have the same definition of "identical" that we use here --
5897 // that is, same mangling.
5898 auto MangleTemplateParamListToString =
5899 [&](SmallVectorImpl<char> &Buffer, const TemplateParameterList *Params,
5900 unsigned DepthOffset) {
5901 llvm::raw_svector_ostream Stream(Buffer);
5902 CXXNameMangler(Mangler.Context, Stream,
5903 WithTemplateDepthOffset{.Offset: DepthOffset})
5904 .mangleTemplateParameterList(Params);
5905 };
5906 llvm::SmallString<128> ParamTemplateHead, ArgTemplateHead;
5907 MangleTemplateParamListToString(ParamTemplateHead,
5908 TTP->getTemplateParameters(), 0);
5909 // Add the depth of the parameter's template parameter list to all
5910 // parameters appearing in the argument to make the indexes line up
5911 // properly.
5912 MangleTemplateParamListToString(ArgTemplateHead,
5913 ArgTemplate->getTemplateParameters(),
5914 TTP->getTemplateParameters()->getDepth());
5915 return ParamTemplateHead != ArgTemplateHead;
5916 }
5917
5918 /// Determine information about how this template argument should be mangled.
5919 /// This should be called exactly once for each parameter / argument pair, in
5920 /// order.
5921 Info getArgInfo(unsigned ParamIdx, const TemplateArgument &Arg) {
5922 // We need correct types when the template-name is unresolved or when it
5923 // names a template that is able to be overloaded.
5924 if (!ResolvedTemplate || SeenPackExpansionIntoNonPack)
5925 return {.NeedExactType: true, .TemplateParameterToMangle: nullptr};
5926
5927 // Move to the next parameter.
5928 const NamedDecl *Param = UnresolvedExpandedPack;
5929 if (!Param) {
5930 assert(ParamIdx < ResolvedTemplate->getTemplateParameters()->size() &&
5931 "no parameter for argument");
5932 Param = ResolvedTemplate->getTemplateParameters()->getParam(Idx: ParamIdx);
5933
5934 // If we reach a parameter pack whose argument isn't in pack form, that
5935 // means Sema couldn't or didn't figure out which arguments belonged to
5936 // it, because it contains a pack expansion or because Sema bailed out of
5937 // computing parameter / argument correspondence before this point. Track
5938 // the pack as the corresponding parameter for all further template
5939 // arguments until we hit a pack expansion, at which point we don't know
5940 // the correspondence between parameters and arguments at all.
5941 if (Param->isParameterPack() && Arg.getKind() != TemplateArgument::Pack) {
5942 UnresolvedExpandedPack = Param;
5943 }
5944 }
5945
5946 // If we encounter a pack argument that is expanded into a non-pack
5947 // parameter, we can no longer track parameter / argument correspondence,
5948 // and need to use exact types from this point onwards.
5949 if (Arg.isPackExpansion() &&
5950 (!Param->isParameterPack() || UnresolvedExpandedPack)) {
5951 SeenPackExpansionIntoNonPack = true;
5952 return {.NeedExactType: true, .TemplateParameterToMangle: nullptr};
5953 }
5954
5955 // We need exact types for arguments of a template that might be overloaded
5956 // on template parameter type.
5957 if (isOverloadable())
5958 return {.NeedExactType: true, .TemplateParameterToMangle: needToMangleTemplateParam(Param, Arg) ? Param : nullptr};
5959
5960 // Otherwise, we only need a correct type if the parameter has a deduced
5961 // type.
5962 //
5963 // Note: for an expanded parameter pack, getType() returns the type prior
5964 // to expansion. We could ask for the expanded type with getExpansionType(),
5965 // but it doesn't matter because substitution and expansion don't affect
5966 // whether a deduced type appears in the type.
5967 auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: Param);
5968 bool NeedExactType = NTTP && NTTP->getType()->getContainedDeducedType();
5969 return {.NeedExactType: NeedExactType, .TemplateParameterToMangle: nullptr};
5970 }
5971
5972 /// Determine if we should mangle a requires-clause after the template
5973 /// argument list. If so, returns the expression to mangle.
5974 const Expr *getTrailingRequiresClauseToMangle() {
5975 if (!isOverloadable())
5976 return nullptr;
5977 return ResolvedTemplate->getTemplateParameters()->getRequiresClause();
5978 }
5979};
5980
5981void CXXNameMangler::mangleTemplateArgs(TemplateName TN,
5982 const TemplateArgumentLoc *TemplateArgs,
5983 unsigned NumTemplateArgs) {
5984 // <template-args> ::= I <template-arg>+ [Q <requires-clause expr>] E
5985 Out << 'I';
5986 TemplateArgManglingInfo Info(*this, TN);
5987 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
5988 mangleTemplateArg(Info, Index: i, A: TemplateArgs[i].getArgument());
5989 }
5990 mangleRequiresClause(RequiresClause: Info.getTrailingRequiresClauseToMangle());
5991 Out << 'E';
5992}
5993
5994void CXXNameMangler::mangleTemplateArgs(TemplateName TN,
5995 const TemplateArgumentList &AL) {
5996 // <template-args> ::= I <template-arg>+ [Q <requires-clause expr>] E
5997 Out << 'I';
5998 TemplateArgManglingInfo Info(*this, TN);
5999 for (unsigned i = 0, e = AL.size(); i != e; ++i) {
6000 mangleTemplateArg(Info, Index: i, A: AL[i]);
6001 }
6002 mangleRequiresClause(RequiresClause: Info.getTrailingRequiresClauseToMangle());
6003 Out << 'E';
6004}
6005
6006void CXXNameMangler::mangleTemplateArgs(TemplateName TN,
6007 ArrayRef<TemplateArgument> Args) {
6008 // <template-args> ::= I <template-arg>+ [Q <requires-clause expr>] E
6009 Out << 'I';
6010 TemplateArgManglingInfo Info(*this, TN);
6011 for (unsigned i = 0; i != Args.size(); ++i) {
6012 mangleTemplateArg(Info, Index: i, A: Args[i]);
6013 }
6014 mangleRequiresClause(RequiresClause: Info.getTrailingRequiresClauseToMangle());
6015 Out << 'E';
6016}
6017
6018void CXXNameMangler::mangleTemplateArg(TemplateArgManglingInfo &Info,
6019 unsigned Index, TemplateArgument A) {
6020 TemplateArgManglingInfo::Info ArgInfo = Info.getArgInfo(ParamIdx: Index, Arg: A);
6021
6022 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/47.
6023 if (ArgInfo.TemplateParameterToMangle &&
6024 !isCompatibleWith(Ver: LangOptions::ClangABI::Ver17)) {
6025 // The template parameter is mangled if the mangling would otherwise be
6026 // ambiguous.
6027 //
6028 // <template-arg> ::= <template-param-decl> <template-arg>
6029 //
6030 // Clang 17 and before did not do this.
6031 mangleTemplateParamDecl(Decl: ArgInfo.TemplateParameterToMangle);
6032 }
6033
6034 mangleTemplateArg(A, NeedExactType: ArgInfo.NeedExactType);
6035}
6036
6037void CXXNameMangler::mangleTemplateArg(TemplateArgument A, bool NeedExactType) {
6038 // <template-arg> ::= <type> # type or template
6039 // ::= X <expression> E # expression
6040 // ::= <expr-primary> # simple expressions
6041 // ::= J <template-arg>* E # argument pack
6042 if (!A.isInstantiationDependent() || A.isDependent())
6043 A = Context.getASTContext().getCanonicalTemplateArgument(Arg: A);
6044
6045 switch (A.getKind()) {
6046 case TemplateArgument::Null:
6047 llvm_unreachable("Cannot mangle NULL template argument");
6048
6049 case TemplateArgument::Type:
6050 mangleType(T: A.getAsType());
6051 break;
6052 case TemplateArgument::Template:
6053 // This is mangled as <type>.
6054 mangleType(TN: A.getAsTemplate());
6055 break;
6056 case TemplateArgument::TemplateExpansion:
6057 // <type> ::= Dp <type> # pack expansion (C++0x)
6058 Out << "Dp";
6059 mangleType(TN: A.getAsTemplateOrTemplatePattern());
6060 break;
6061 case TemplateArgument::Expression:
6062 mangleTemplateArgExpr(E: A.getAsExpr());
6063 break;
6064 case TemplateArgument::Integral:
6065 mangleIntegerLiteral(T: A.getIntegralType(), Value: A.getAsIntegral());
6066 break;
6067 case TemplateArgument::Declaration: {
6068 // <expr-primary> ::= L <mangled-name> E # external name
6069 ValueDecl *D = A.getAsDecl();
6070
6071 // Template parameter objects are modeled by reproducing a source form
6072 // produced as if by aggregate initialization.
6073 if (A.getParamTypeForDecl()->isRecordType()) {
6074 auto *TPO = cast<TemplateParamObjectDecl>(Val: D);
6075 mangleValueInTemplateArg(T: TPO->getType().getUnqualifiedType(),
6076 V: TPO->getValue(), /*TopLevel=*/true,
6077 NeedExactType);
6078 break;
6079 }
6080
6081 ASTContext &Ctx = Context.getASTContext();
6082 APValue Value;
6083 if (D->isCXXInstanceMember())
6084 // Simple pointer-to-member with no conversion.
6085 Value = APValue(D, /*IsDerivedMember=*/false, /*Path=*/{});
6086 else if (D->getType()->isArrayType() &&
6087 Ctx.hasSimilarType(T1: Ctx.getDecayedType(T: D->getType()),
6088 T2: A.getParamTypeForDecl()) &&
6089 !isCompatibleWith(Ver: LangOptions::ClangABI::Ver11))
6090 // Build a value corresponding to this implicit array-to-pointer decay.
6091 Value = APValue(APValue::LValueBase(D), CharUnits::Zero(),
6092 {APValue::LValuePathEntry::ArrayIndex(Index: 0)},
6093 /*OnePastTheEnd=*/false);
6094 else
6095 // Regular pointer or reference to a declaration.
6096 Value = APValue(APValue::LValueBase(D), CharUnits::Zero(),
6097 ArrayRef<APValue::LValuePathEntry>(),
6098 /*OnePastTheEnd=*/false);
6099 mangleValueInTemplateArg(T: A.getParamTypeForDecl(), V: Value, /*TopLevel=*/true,
6100 NeedExactType);
6101 break;
6102 }
6103 case TemplateArgument::NullPtr: {
6104 mangleNullPointer(T: A.getNullPtrType());
6105 break;
6106 }
6107 case TemplateArgument::StructuralValue:
6108 mangleValueInTemplateArg(T: A.getStructuralValueType(),
6109 V: A.getAsStructuralValue(),
6110 /*TopLevel=*/true, NeedExactType);
6111 break;
6112 case TemplateArgument::Pack: {
6113 // <template-arg> ::= J <template-arg>* E
6114 Out << 'J';
6115 for (const auto &P : A.pack_elements())
6116 mangleTemplateArg(A: P, NeedExactType);
6117 Out << 'E';
6118 }
6119 }
6120}
6121
6122void CXXNameMangler::mangleTemplateArgExpr(const Expr *E) {
6123 if (!isCompatibleWith(Ver: LangOptions::ClangABI::Ver11)) {
6124 mangleExpression(E, Arity: UnknownArity, /*AsTemplateArg=*/true);
6125 return;
6126 }
6127
6128 // Prior to Clang 12, we didn't omit the X .. E around <expr-primary>
6129 // correctly in cases where the template argument was
6130 // constructed from an expression rather than an already-evaluated
6131 // literal. In such a case, we would then e.g. emit 'XLi0EE' instead of
6132 // 'Li0E'.
6133 //
6134 // We did special-case DeclRefExpr to attempt to DTRT for that one
6135 // expression-kind, but while doing so, unfortunately handled ParmVarDecl
6136 // (subtype of VarDecl) _incorrectly_, and emitted 'L_Z .. E' instead of
6137 // the proper 'Xfp_E'.
6138 E = E->IgnoreParenImpCasts();
6139 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
6140 const ValueDecl *D = DRE->getDecl();
6141 if (isa<VarDecl>(Val: D) || isa<FunctionDecl>(Val: D)) {
6142 Out << 'L';
6143 mangle(D);
6144 Out << 'E';
6145 return;
6146 }
6147 }
6148 Out << 'X';
6149 mangleExpression(E);
6150 Out << 'E';
6151}
6152
6153/// Determine whether a given value is equivalent to zero-initialization for
6154/// the purpose of discarding a trailing portion of a 'tl' mangling.
6155///
6156/// Note that this is not in general equivalent to determining whether the
6157/// value has an all-zeroes bit pattern.
6158static bool isZeroInitialized(QualType T, const APValue &V) {
6159 // FIXME: mangleValueInTemplateArg has quadratic time complexity in
6160 // pathological cases due to using this, but it's a little awkward
6161 // to do this in linear time in general.
6162 switch (V.getKind()) {
6163 case APValue::None:
6164 case APValue::Indeterminate:
6165 case APValue::AddrLabelDiff:
6166 return false;
6167
6168 case APValue::Struct: {
6169 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
6170 assert(RD && "unexpected type for record value");
6171 unsigned I = 0;
6172 for (const CXXBaseSpecifier &BS : RD->bases()) {
6173 if (!isZeroInitialized(T: BS.getType(), V: V.getStructBase(i: I)))
6174 return false;
6175 ++I;
6176 }
6177 I = 0;
6178 for (const FieldDecl *FD : RD->fields()) {
6179 if (!FD->isUnnamedBitField() &&
6180 !isZeroInitialized(FD->getType(), V.getStructField(I)))
6181 return false;
6182 ++I;
6183 }
6184 return true;
6185 }
6186
6187 case APValue::Union: {
6188 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
6189 assert(RD && "unexpected type for union value");
6190 // Zero-initialization zeroes the first non-unnamed-bitfield field, if any.
6191 for (const FieldDecl *FD : RD->fields()) {
6192 if (!FD->isUnnamedBitField())
6193 return V.getUnionField() && declaresSameEntity(FD, V.getUnionField()) &&
6194 isZeroInitialized(FD->getType(), V.getUnionValue());
6195 }
6196 // If there are no fields (other than unnamed bitfields), the value is
6197 // necessarily zero-initialized.
6198 return true;
6199 }
6200
6201 case APValue::Array: {
6202 QualType ElemT(T->getArrayElementTypeNoTypeQual(), 0);
6203 for (unsigned I = 0, N = V.getArrayInitializedElts(); I != N; ++I)
6204 if (!isZeroInitialized(T: ElemT, V: V.getArrayInitializedElt(I)))
6205 return false;
6206 return !V.hasArrayFiller() || isZeroInitialized(T: ElemT, V: V.getArrayFiller());
6207 }
6208
6209 case APValue::Vector: {
6210 const VectorType *VT = T->castAs<VectorType>();
6211 for (unsigned I = 0, N = V.getVectorLength(); I != N; ++I)
6212 if (!isZeroInitialized(T: VT->getElementType(), V: V.getVectorElt(I)))
6213 return false;
6214 return true;
6215 }
6216
6217 case APValue::Int:
6218 return !V.getInt();
6219
6220 case APValue::Float:
6221 return V.getFloat().isPosZero();
6222
6223 case APValue::FixedPoint:
6224 return !V.getFixedPoint().getValue();
6225
6226 case APValue::ComplexFloat:
6227 return V.getComplexFloatReal().isPosZero() &&
6228 V.getComplexFloatImag().isPosZero();
6229
6230 case APValue::ComplexInt:
6231 return !V.getComplexIntReal() && !V.getComplexIntImag();
6232
6233 case APValue::LValue:
6234 return V.isNullPointer();
6235
6236 case APValue::MemberPointer:
6237 return !V.getMemberPointerDecl();
6238 }
6239
6240 llvm_unreachable("Unhandled APValue::ValueKind enum");
6241}
6242
6243static QualType getLValueType(ASTContext &Ctx, const APValue &LV) {
6244 QualType T = LV.getLValueBase().getType();
6245 for (APValue::LValuePathEntry E : LV.getLValuePath()) {
6246 if (const ArrayType *AT = Ctx.getAsArrayType(T))
6247 T = AT->getElementType();
6248 else if (const FieldDecl *FD =
6249 dyn_cast<FieldDecl>(Val: E.getAsBaseOrMember().getPointer()))
6250 T = FD->getType();
6251 else
6252 T = Ctx.getRecordType(
6253 cast<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer()));
6254 }
6255 return T;
6256}
6257
6258static IdentifierInfo *getUnionInitName(SourceLocation UnionLoc,
6259 DiagnosticsEngine &Diags,
6260 const FieldDecl *FD) {
6261 // According to:
6262 // http://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling.anonymous
6263 // For the purposes of mangling, the name of an anonymous union is considered
6264 // to be the name of the first named data member found by a pre-order,
6265 // depth-first, declaration-order walk of the data members of the anonymous
6266 // union.
6267
6268 if (FD->getIdentifier())
6269 return FD->getIdentifier();
6270
6271 // The only cases where the identifer of a FieldDecl would be blank is if the
6272 // field represents an anonymous record type or if it is an unnamed bitfield.
6273 // There is no type to descend into in the case of a bitfield, so we can just
6274 // return nullptr in that case.
6275 if (FD->isBitField())
6276 return nullptr;
6277 const CXXRecordDecl *RD = FD->getType()->getAsCXXRecordDecl();
6278
6279 // Consider only the fields in declaration order, searched depth-first. We
6280 // don't care about the active member of the union, as all we are doing is
6281 // looking for a valid name. We also don't check bases, due to guidance from
6282 // the Itanium ABI folks.
6283 for (const FieldDecl *RDField : RD->fields()) {
6284 if (IdentifierInfo *II = getUnionInitName(UnionLoc, Diags, RDField))
6285 return II;
6286 }
6287
6288 // According to the Itanium ABI: If there is no such data member (i.e., if all
6289 // of the data members in the union are unnamed), then there is no way for a
6290 // program to refer to the anonymous union, and there is therefore no need to
6291 // mangle its name. However, we should diagnose this anyway.
6292 unsigned DiagID = Diags.getCustomDiagID(
6293 L: DiagnosticsEngine::Error, FormatString: "cannot mangle this unnamed union NTTP yet");
6294 Diags.Report(Loc: UnionLoc, DiagID);
6295
6296 return nullptr;
6297}
6298
6299void CXXNameMangler::mangleValueInTemplateArg(QualType T, const APValue &V,
6300 bool TopLevel,
6301 bool NeedExactType) {
6302 // Ignore all top-level cv-qualifiers, to match GCC.
6303 Qualifiers Quals;
6304 T = getASTContext().getUnqualifiedArrayType(T, Quals);
6305
6306 // A top-level expression that's not a primary expression is wrapped in X...E.
6307 bool IsPrimaryExpr = true;
6308 auto NotPrimaryExpr = [&] {
6309 if (TopLevel && IsPrimaryExpr)
6310 Out << 'X';
6311 IsPrimaryExpr = false;
6312 };
6313
6314 // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/63.
6315 switch (V.getKind()) {
6316 case APValue::None:
6317 case APValue::Indeterminate:
6318 Out << 'L';
6319 mangleType(T);
6320 Out << 'E';
6321 break;
6322
6323 case APValue::AddrLabelDiff:
6324 llvm_unreachable("unexpected value kind in template argument");
6325
6326 case APValue::Struct: {
6327 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
6328 assert(RD && "unexpected type for record value");
6329
6330 // Drop trailing zero-initialized elements.
6331 llvm::SmallVector<const FieldDecl *, 16> Fields(RD->fields());
6332 while (
6333 !Fields.empty() &&
6334 (Fields.back()->isUnnamedBitField() ||
6335 isZeroInitialized(Fields.back()->getType(),
6336 V.getStructField(i: Fields.back()->getFieldIndex())))) {
6337 Fields.pop_back();
6338 }
6339 llvm::ArrayRef<CXXBaseSpecifier> Bases(RD->bases_begin(), RD->bases_end());
6340 if (Fields.empty()) {
6341 while (!Bases.empty() &&
6342 isZeroInitialized(T: Bases.back().getType(),
6343 V: V.getStructBase(i: Bases.size() - 1)))
6344 Bases = Bases.drop_back();
6345 }
6346
6347 // <expression> ::= tl <type> <braced-expression>* E
6348 NotPrimaryExpr();
6349 Out << "tl";
6350 mangleType(T);
6351 for (unsigned I = 0, N = Bases.size(); I != N; ++I)
6352 mangleValueInTemplateArg(T: Bases[I].getType(), V: V.getStructBase(i: I), TopLevel: false);
6353 for (unsigned I = 0, N = Fields.size(); I != N; ++I) {
6354 if (Fields[I]->isUnnamedBitField())
6355 continue;
6356 mangleValueInTemplateArg(T: Fields[I]->getType(),
6357 V: V.getStructField(i: Fields[I]->getFieldIndex()),
6358 TopLevel: false);
6359 }
6360 Out << 'E';
6361 break;
6362 }
6363
6364 case APValue::Union: {
6365 assert(T->getAsCXXRecordDecl() && "unexpected type for union value");
6366 const FieldDecl *FD = V.getUnionField();
6367
6368 if (!FD) {
6369 Out << 'L';
6370 mangleType(T);
6371 Out << 'E';
6372 break;
6373 }
6374
6375 // <braced-expression> ::= di <field source-name> <braced-expression>
6376 NotPrimaryExpr();
6377 Out << "tl";
6378 mangleType(T);
6379 if (!isZeroInitialized(T, V)) {
6380 Out << "di";
6381 IdentifierInfo *II = (getUnionInitName(
6382 T->getAsCXXRecordDecl()->getLocation(), Context.getDiags(), FD));
6383 if (II)
6384 mangleSourceName(II);
6385 mangleValueInTemplateArg(T: FD->getType(), V: V.getUnionValue(), TopLevel: false);
6386 }
6387 Out << 'E';
6388 break;
6389 }
6390
6391 case APValue::Array: {
6392 QualType ElemT(T->getArrayElementTypeNoTypeQual(), 0);
6393
6394 NotPrimaryExpr();
6395 Out << "tl";
6396 mangleType(T);
6397
6398 // Drop trailing zero-initialized elements.
6399 unsigned N = V.getArraySize();
6400 if (!V.hasArrayFiller() || isZeroInitialized(T: ElemT, V: V.getArrayFiller())) {
6401 N = V.getArrayInitializedElts();
6402 while (N && isZeroInitialized(T: ElemT, V: V.getArrayInitializedElt(I: N - 1)))
6403 --N;
6404 }
6405
6406 for (unsigned I = 0; I != N; ++I) {
6407 const APValue &Elem = I < V.getArrayInitializedElts()
6408 ? V.getArrayInitializedElt(I)
6409 : V.getArrayFiller();
6410 mangleValueInTemplateArg(T: ElemT, V: Elem, TopLevel: false);
6411 }
6412 Out << 'E';
6413 break;
6414 }
6415
6416 case APValue::Vector: {
6417 const VectorType *VT = T->castAs<VectorType>();
6418
6419 NotPrimaryExpr();
6420 Out << "tl";
6421 mangleType(T);
6422 unsigned N = V.getVectorLength();
6423 while (N && isZeroInitialized(T: VT->getElementType(), V: V.getVectorElt(I: N - 1)))
6424 --N;
6425 for (unsigned I = 0; I != N; ++I)
6426 mangleValueInTemplateArg(T: VT->getElementType(), V: V.getVectorElt(I), TopLevel: false);
6427 Out << 'E';
6428 break;
6429 }
6430
6431 case APValue::Int:
6432 mangleIntegerLiteral(T, Value: V.getInt());
6433 break;
6434
6435 case APValue::Float:
6436 mangleFloatLiteral(T, V: V.getFloat());
6437 break;
6438
6439 case APValue::FixedPoint:
6440 mangleFixedPointLiteral();
6441 break;
6442
6443 case APValue::ComplexFloat: {
6444 const ComplexType *CT = T->castAs<ComplexType>();
6445 NotPrimaryExpr();
6446 Out << "tl";
6447 mangleType(T);
6448 if (!V.getComplexFloatReal().isPosZero() ||
6449 !V.getComplexFloatImag().isPosZero())
6450 mangleFloatLiteral(T: CT->getElementType(), V: V.getComplexFloatReal());
6451 if (!V.getComplexFloatImag().isPosZero())
6452 mangleFloatLiteral(T: CT->getElementType(), V: V.getComplexFloatImag());
6453 Out << 'E';
6454 break;
6455 }
6456
6457 case APValue::ComplexInt: {
6458 const ComplexType *CT = T->castAs<ComplexType>();
6459 NotPrimaryExpr();
6460 Out << "tl";
6461 mangleType(T);
6462 if (V.getComplexIntReal().getBoolValue() ||
6463 V.getComplexIntImag().getBoolValue())
6464 mangleIntegerLiteral(T: CT->getElementType(), Value: V.getComplexIntReal());
6465 if (V.getComplexIntImag().getBoolValue())
6466 mangleIntegerLiteral(T: CT->getElementType(), Value: V.getComplexIntImag());
6467 Out << 'E';
6468 break;
6469 }
6470
6471 case APValue::LValue: {
6472 // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/47.
6473 assert((T->isPointerType() || T->isReferenceType()) &&
6474 "unexpected type for LValue template arg");
6475
6476 if (V.isNullPointer()) {
6477 mangleNullPointer(T);
6478 break;
6479 }
6480
6481 APValue::LValueBase B = V.getLValueBase();
6482 if (!B) {
6483 // Non-standard mangling for integer cast to a pointer; this can only
6484 // occur as an extension.
6485 CharUnits Offset = V.getLValueOffset();
6486 if (Offset.isZero()) {
6487 // This is reinterpret_cast<T*>(0), not a null pointer. Mangle this as
6488 // a cast, because L <type> 0 E means something else.
6489 NotPrimaryExpr();
6490 Out << "rc";
6491 mangleType(T);
6492 Out << "Li0E";
6493 if (TopLevel)
6494 Out << 'E';
6495 } else {
6496 Out << "L";
6497 mangleType(T);
6498 Out << Offset.getQuantity() << 'E';
6499 }
6500 break;
6501 }
6502
6503 ASTContext &Ctx = Context.getASTContext();
6504
6505 enum { Base, Offset, Path } Kind;
6506 if (!V.hasLValuePath()) {
6507 // Mangle as (T*)((char*)&base + N).
6508 if (T->isReferenceType()) {
6509 NotPrimaryExpr();
6510 Out << "decvP";
6511 mangleType(T: T->getPointeeType());
6512 } else {
6513 NotPrimaryExpr();
6514 Out << "cv";
6515 mangleType(T);
6516 }
6517 Out << "plcvPcad";
6518 Kind = Offset;
6519 } else {
6520 // Clang 11 and before mangled an array subject to array-to-pointer decay
6521 // as if it were the declaration itself.
6522 bool IsArrayToPointerDecayMangledAsDecl = false;
6523 if (TopLevel && Ctx.getLangOpts().getClangABICompat() <=
6524 LangOptions::ClangABI::Ver11) {
6525 QualType BType = B.getType();
6526 IsArrayToPointerDecayMangledAsDecl =
6527 BType->isArrayType() && V.getLValuePath().size() == 1 &&
6528 V.getLValuePath()[0].getAsArrayIndex() == 0 &&
6529 Ctx.hasSimilarType(T1: T, T2: Ctx.getDecayedType(T: BType));
6530 }
6531
6532 if ((!V.getLValuePath().empty() || V.isLValueOnePastTheEnd()) &&
6533 !IsArrayToPointerDecayMangledAsDecl) {
6534 NotPrimaryExpr();
6535 // A final conversion to the template parameter's type is usually
6536 // folded into the 'so' mangling, but we can't do that for 'void*'
6537 // parameters without introducing collisions.
6538 if (NeedExactType && T->isVoidPointerType()) {
6539 Out << "cv";
6540 mangleType(T);
6541 }
6542 if (T->isPointerType())
6543 Out << "ad";
6544 Out << "so";
6545 mangleType(T: T->isVoidPointerType()
6546 ? getLValueType(Ctx, LV: V).getUnqualifiedType()
6547 : T->getPointeeType());
6548 Kind = Path;
6549 } else {
6550 if (NeedExactType &&
6551 !Ctx.hasSameType(T1: T->getPointeeType(), T2: getLValueType(Ctx, LV: V)) &&
6552 !isCompatibleWith(Ver: LangOptions::ClangABI::Ver11)) {
6553 NotPrimaryExpr();
6554 Out << "cv";
6555 mangleType(T);
6556 }
6557 if (T->isPointerType()) {
6558 NotPrimaryExpr();
6559 Out << "ad";
6560 }
6561 Kind = Base;
6562 }
6563 }
6564
6565 QualType TypeSoFar = B.getType();
6566 if (auto *VD = B.dyn_cast<const ValueDecl*>()) {
6567 Out << 'L';
6568 mangle(VD);
6569 Out << 'E';
6570 } else if (auto *E = B.dyn_cast<const Expr*>()) {
6571 NotPrimaryExpr();
6572 mangleExpression(E);
6573 } else if (auto TI = B.dyn_cast<TypeInfoLValue>()) {
6574 NotPrimaryExpr();
6575 Out << "ti";
6576 mangleType(T: QualType(TI.getType(), 0));
6577 } else {
6578 // We should never see dynamic allocations here.
6579 llvm_unreachable("unexpected lvalue base kind in template argument");
6580 }
6581
6582 switch (Kind) {
6583 case Base:
6584 break;
6585
6586 case Offset:
6587 Out << 'L';
6588 mangleType(T: Ctx.getPointerDiffType());
6589 mangleNumber(Number: V.getLValueOffset().getQuantity());
6590 Out << 'E';
6591 break;
6592
6593 case Path:
6594 // <expression> ::= so <referent type> <expr> [<offset number>]
6595 // <union-selector>* [p] E
6596 if (!V.getLValueOffset().isZero())
6597 mangleNumber(Number: V.getLValueOffset().getQuantity());
6598
6599 // We model a past-the-end array pointer as array indexing with index N,
6600 // not with the "past the end" flag. Compensate for that.
6601 bool OnePastTheEnd = V.isLValueOnePastTheEnd();
6602
6603 for (APValue::LValuePathEntry E : V.getLValuePath()) {
6604 if (auto *AT = TypeSoFar->getAsArrayTypeUnsafe()) {
6605 if (auto *CAT = dyn_cast<ConstantArrayType>(AT))
6606 OnePastTheEnd |= CAT->getSize() == E.getAsArrayIndex();
6607 TypeSoFar = AT->getElementType();
6608 } else {
6609 const Decl *D = E.getAsBaseOrMember().getPointer();
6610 if (auto *FD = dyn_cast<FieldDecl>(Val: D)) {
6611 // <union-selector> ::= _ <number>
6612 if (FD->getParent()->isUnion()) {
6613 Out << '_';
6614 if (FD->getFieldIndex())
6615 Out << (FD->getFieldIndex() - 1);
6616 }
6617 TypeSoFar = FD->getType();
6618 } else {
6619 TypeSoFar = Ctx.getRecordType(cast<CXXRecordDecl>(Val: D));
6620 }
6621 }
6622 }
6623
6624 if (OnePastTheEnd)
6625 Out << 'p';
6626 Out << 'E';
6627 break;
6628 }
6629
6630 break;
6631 }
6632
6633 case APValue::MemberPointer:
6634 // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/47.
6635 if (!V.getMemberPointerDecl()) {
6636 mangleNullPointer(T);
6637 break;
6638 }
6639
6640 ASTContext &Ctx = Context.getASTContext();
6641
6642 NotPrimaryExpr();
6643 if (!V.getMemberPointerPath().empty()) {
6644 Out << "mc";
6645 mangleType(T);
6646 } else if (NeedExactType &&
6647 !Ctx.hasSameType(
6648 T1: T->castAs<MemberPointerType>()->getPointeeType(),
6649 T2: V.getMemberPointerDecl()->getType()) &&
6650 !isCompatibleWith(Ver: LangOptions::ClangABI::Ver11)) {
6651 Out << "cv";
6652 mangleType(T);
6653 }
6654 Out << "adL";
6655 mangle(V.getMemberPointerDecl());
6656 Out << 'E';
6657 if (!V.getMemberPointerPath().empty()) {
6658 CharUnits Offset =
6659 Context.getASTContext().getMemberPointerPathAdjustment(MP: V);
6660 if (!Offset.isZero())
6661 mangleNumber(Number: Offset.getQuantity());
6662 Out << 'E';
6663 }
6664 break;
6665 }
6666
6667 if (TopLevel && !IsPrimaryExpr)
6668 Out << 'E';
6669}
6670
6671void CXXNameMangler::mangleTemplateParameter(unsigned Depth, unsigned Index) {
6672 // <template-param> ::= T_ # first template parameter
6673 // ::= T <parameter-2 non-negative number> _
6674 // ::= TL <L-1 non-negative number> __
6675 // ::= TL <L-1 non-negative number> _
6676 // <parameter-2 non-negative number> _
6677 //
6678 // The latter two manglings are from a proposal here:
6679 // https://github.com/itanium-cxx-abi/cxx-abi/issues/31#issuecomment-528122117
6680 Out << 'T';
6681 Depth += TemplateDepthOffset;
6682 if (Depth != 0)
6683 Out << 'L' << (Depth - 1) << '_';
6684 if (Index != 0)
6685 Out << (Index - 1);
6686 Out << '_';
6687}
6688
6689void CXXNameMangler::mangleSeqID(unsigned SeqID) {
6690 if (SeqID == 0) {
6691 // Nothing.
6692 } else if (SeqID == 1) {
6693 Out << '0';
6694 } else {
6695 SeqID--;
6696
6697 // <seq-id> is encoded in base-36, using digits and upper case letters.
6698 char Buffer[7]; // log(2**32) / log(36) ~= 7
6699 MutableArrayRef<char> BufferRef(Buffer);
6700 MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin();
6701
6702 for (; SeqID != 0; SeqID /= 36) {
6703 unsigned C = SeqID % 36;
6704 *I++ = (C < 10 ? '0' + C : 'A' + C - 10);
6705 }
6706
6707 Out.write(Ptr: I.base(), Size: I - BufferRef.rbegin());
6708 }
6709 Out << '_';
6710}
6711
6712void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
6713 bool result = mangleSubstitution(Template: tname);
6714 assert(result && "no existing substitution for template name");
6715 (void) result;
6716}
6717
6718// <substitution> ::= S <seq-id> _
6719// ::= S_
6720bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
6721 // Try one of the standard substitutions first.
6722 if (mangleStandardSubstitution(ND))
6723 return true;
6724
6725 ND = cast<NamedDecl>(ND->getCanonicalDecl());
6726 return mangleSubstitution(Ptr: reinterpret_cast<uintptr_t>(ND));
6727}
6728
6729bool CXXNameMangler::mangleSubstitution(NestedNameSpecifier *NNS) {
6730 assert(NNS->getKind() == NestedNameSpecifier::Identifier &&
6731 "mangleSubstitution(NestedNameSpecifier *) is only used for "
6732 "identifier nested name specifiers.");
6733 NNS = Context.getASTContext().getCanonicalNestedNameSpecifier(NNS);
6734 return mangleSubstitution(Ptr: reinterpret_cast<uintptr_t>(NNS));
6735}
6736
6737/// Determine whether the given type has any qualifiers that are relevant for
6738/// substitutions.
6739static bool hasMangledSubstitutionQualifiers(QualType T) {
6740 Qualifiers Qs = T.getQualifiers();
6741 return Qs.getCVRQualifiers() || Qs.hasAddressSpace() || Qs.hasUnaligned();
6742}
6743
6744bool CXXNameMangler::mangleSubstitution(QualType T) {
6745 if (!hasMangledSubstitutionQualifiers(T)) {
6746 if (const RecordType *RT = T->getAs<RecordType>())
6747 return mangleSubstitution(RT->getDecl());
6748 }
6749
6750 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
6751
6752 return mangleSubstitution(Ptr: TypePtr);
6753}
6754
6755bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
6756 if (TemplateDecl *TD = Template.getAsTemplateDecl())
6757 return mangleSubstitution(TD);
6758
6759 Template = Context.getASTContext().getCanonicalTemplateName(Name: Template);
6760 return mangleSubstitution(
6761 Ptr: reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
6762}
6763
6764bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
6765 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Val: Ptr);
6766 if (I == Substitutions.end())
6767 return false;
6768
6769 unsigned SeqID = I->second;
6770 Out << 'S';
6771 mangleSeqID(SeqID);
6772
6773 return true;
6774}
6775
6776/// Returns whether S is a template specialization of std::Name with a single
6777/// argument of type A.
6778bool CXXNameMangler::isSpecializedAs(QualType S, llvm::StringRef Name,
6779 QualType A) {
6780 if (S.isNull())
6781 return false;
6782
6783 const RecordType *RT = S->getAs<RecordType>();
6784 if (!RT)
6785 return false;
6786
6787 const ClassTemplateSpecializationDecl *SD =
6788 dyn_cast<ClassTemplateSpecializationDecl>(Val: RT->getDecl());
6789 if (!SD || !SD->getIdentifier()->isStr(Name))
6790 return false;
6791
6792 if (!isStdNamespace(DC: Context.getEffectiveDeclContext(SD)))
6793 return false;
6794
6795 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
6796 if (TemplateArgs.size() != 1)
6797 return false;
6798
6799 if (TemplateArgs[0].getAsType() != A)
6800 return false;
6801
6802 if (SD->getSpecializedTemplate()->getOwningModuleForLinkage())
6803 return false;
6804
6805 return true;
6806}
6807
6808/// Returns whether SD is a template specialization std::Name<char,
6809/// std::char_traits<char> [, std::allocator<char>]>
6810/// HasAllocator controls whether the 3rd template argument is needed.
6811bool CXXNameMangler::isStdCharSpecialization(
6812 const ClassTemplateSpecializationDecl *SD, llvm::StringRef Name,
6813 bool HasAllocator) {
6814 if (!SD->getIdentifier()->isStr(Name))
6815 return false;
6816
6817 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
6818 if (TemplateArgs.size() != (HasAllocator ? 3 : 2))
6819 return false;
6820
6821 QualType A = TemplateArgs[0].getAsType();
6822 if (A.isNull())
6823 return false;
6824 // Plain 'char' is named Char_S or Char_U depending on the target ABI.
6825 if (!A->isSpecificBuiltinType(K: BuiltinType::Char_S) &&
6826 !A->isSpecificBuiltinType(K: BuiltinType::Char_U))
6827 return false;
6828
6829 if (!isSpecializedAs(S: TemplateArgs[1].getAsType(), Name: "char_traits", A))
6830 return false;
6831
6832 if (HasAllocator &&
6833 !isSpecializedAs(S: TemplateArgs[2].getAsType(), Name: "allocator", A))
6834 return false;
6835
6836 if (SD->getSpecializedTemplate()->getOwningModuleForLinkage())
6837 return false;
6838
6839 return true;
6840}
6841
6842bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
6843 // <substitution> ::= St # ::std::
6844 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Val: ND)) {
6845 if (isStd(NS)) {
6846 Out << "St";
6847 return true;
6848 }
6849 return false;
6850 }
6851
6852 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(Val: ND)) {
6853 if (!isStdNamespace(DC: Context.getEffectiveDeclContext(TD)))
6854 return false;
6855
6856 if (TD->getOwningModuleForLinkage())
6857 return false;
6858
6859 // <substitution> ::= Sa # ::std::allocator
6860 if (TD->getIdentifier()->isStr("allocator")) {
6861 Out << "Sa";
6862 return true;
6863 }
6864
6865 // <<substitution> ::= Sb # ::std::basic_string
6866 if (TD->getIdentifier()->isStr("basic_string")) {
6867 Out << "Sb";
6868 return true;
6869 }
6870 return false;
6871 }
6872
6873 if (const ClassTemplateSpecializationDecl *SD =
6874 dyn_cast<ClassTemplateSpecializationDecl>(Val: ND)) {
6875 if (!isStdNamespace(DC: Context.getEffectiveDeclContext(SD)))
6876 return false;
6877
6878 if (SD->getSpecializedTemplate()->getOwningModuleForLinkage())
6879 return false;
6880
6881 // <substitution> ::= Ss # ::std::basic_string<char,
6882 // ::std::char_traits<char>,
6883 // ::std::allocator<char> >
6884 if (isStdCharSpecialization(SD, Name: "basic_string", /*HasAllocator=*/true)) {
6885 Out << "Ss";
6886 return true;
6887 }
6888
6889 // <substitution> ::= Si # ::std::basic_istream<char,
6890 // ::std::char_traits<char> >
6891 if (isStdCharSpecialization(SD, Name: "basic_istream", /*HasAllocator=*/false)) {
6892 Out << "Si";
6893 return true;
6894 }
6895
6896 // <substitution> ::= So # ::std::basic_ostream<char,
6897 // ::std::char_traits<char> >
6898 if (isStdCharSpecialization(SD, Name: "basic_ostream", /*HasAllocator=*/false)) {
6899 Out << "So";
6900 return true;
6901 }
6902
6903 // <substitution> ::= Sd # ::std::basic_iostream<char,
6904 // ::std::char_traits<char> >
6905 if (isStdCharSpecialization(SD, Name: "basic_iostream", /*HasAllocator=*/false)) {
6906 Out << "Sd";
6907 return true;
6908 }
6909 return false;
6910 }
6911
6912 return false;
6913}
6914
6915void CXXNameMangler::addSubstitution(QualType T) {
6916 if (!hasMangledSubstitutionQualifiers(T)) {
6917 if (const RecordType *RT = T->getAs<RecordType>()) {
6918 addSubstitution(RT->getDecl());
6919 return;
6920 }
6921 }
6922
6923 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
6924 addSubstitution(Ptr: TypePtr);
6925}
6926
6927void CXXNameMangler::addSubstitution(TemplateName Template) {
6928 if (TemplateDecl *TD = Template.getAsTemplateDecl())
6929 return addSubstitution(TD);
6930
6931 Template = Context.getASTContext().getCanonicalTemplateName(Name: Template);
6932 addSubstitution(Ptr: reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
6933}
6934
6935void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
6936 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
6937 Substitutions[Ptr] = SeqID++;
6938}
6939
6940void CXXNameMangler::extendSubstitutions(CXXNameMangler* Other) {
6941 assert(Other->SeqID >= SeqID && "Must be superset of substitutions!");
6942 if (Other->SeqID > SeqID) {
6943 Substitutions.swap(RHS&: Other->Substitutions);
6944 SeqID = Other->SeqID;
6945 }
6946}
6947
6948CXXNameMangler::AbiTagList
6949CXXNameMangler::makeFunctionReturnTypeTags(const FunctionDecl *FD) {
6950 // When derived abi tags are disabled there is no need to make any list.
6951 if (DisableDerivedAbiTags)
6952 return AbiTagList();
6953
6954 llvm::raw_null_ostream NullOutStream;
6955 CXXNameMangler TrackReturnTypeTags(*this, NullOutStream);
6956 TrackReturnTypeTags.disableDerivedAbiTags();
6957
6958 const FunctionProtoType *Proto =
6959 cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
6960 FunctionTypeDepthState saved = TrackReturnTypeTags.FunctionTypeDepth.push();
6961 TrackReturnTypeTags.FunctionTypeDepth.enterResultType();
6962 TrackReturnTypeTags.mangleType(Proto->getReturnType());
6963 TrackReturnTypeTags.FunctionTypeDepth.leaveResultType();
6964 TrackReturnTypeTags.FunctionTypeDepth.pop(saved);
6965
6966 return TrackReturnTypeTags.AbiTagsRoot.getSortedUniqueUsedAbiTags();
6967}
6968
6969CXXNameMangler::AbiTagList
6970CXXNameMangler::makeVariableTypeTags(const VarDecl *VD) {
6971 // When derived abi tags are disabled there is no need to make any list.
6972 if (DisableDerivedAbiTags)
6973 return AbiTagList();
6974
6975 llvm::raw_null_ostream NullOutStream;
6976 CXXNameMangler TrackVariableType(*this, NullOutStream);
6977 TrackVariableType.disableDerivedAbiTags();
6978
6979 TrackVariableType.mangleType(VD->getType());
6980
6981 return TrackVariableType.AbiTagsRoot.getSortedUniqueUsedAbiTags();
6982}
6983
6984bool CXXNameMangler::shouldHaveAbiTags(ItaniumMangleContextImpl &C,
6985 const VarDecl *VD) {
6986 llvm::raw_null_ostream NullOutStream;
6987 CXXNameMangler TrackAbiTags(C, NullOutStream, nullptr, true);
6988 TrackAbiTags.mangle(GD: VD);
6989 return TrackAbiTags.AbiTagsRoot.getUsedAbiTags().size();
6990}
6991
6992//
6993
6994/// Mangles the name of the declaration D and emits that name to the given
6995/// output stream.
6996///
6997/// If the declaration D requires a mangled name, this routine will emit that
6998/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
6999/// and this routine will return false. In this case, the caller should just
7000/// emit the identifier of the declaration (\c D->getIdentifier()) as its
7001/// name.
7002void ItaniumMangleContextImpl::mangleCXXName(GlobalDecl GD,
7003 raw_ostream &Out) {
7004 const NamedDecl *D = cast<NamedDecl>(Val: GD.getDecl());
7005 assert((isa<FunctionDecl, VarDecl, TemplateParamObjectDecl>(D)) &&
7006 "Invalid mangleName() call, argument is not a variable or function!");
7007
7008 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
7009 getASTContext().getSourceManager(),
7010 "Mangling declaration");
7011
7012 if (auto *CD = dyn_cast<CXXConstructorDecl>(Val: D)) {
7013 auto Type = GD.getCtorType();
7014 CXXNameMangler Mangler(*this, Out, CD, Type);
7015 return Mangler.mangle(GD: GlobalDecl(CD, Type));
7016 }
7017
7018 if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: D)) {
7019 auto Type = GD.getDtorType();
7020 CXXNameMangler Mangler(*this, Out, DD, Type);
7021 return Mangler.mangle(GD: GlobalDecl(DD, Type));
7022 }
7023
7024 CXXNameMangler Mangler(*this, Out, D);
7025 Mangler.mangle(GD);
7026}
7027
7028void ItaniumMangleContextImpl::mangleCXXCtorComdat(const CXXConstructorDecl *D,
7029 raw_ostream &Out) {
7030 CXXNameMangler Mangler(*this, Out, D, Ctor_Comdat);
7031 Mangler.mangle(GD: GlobalDecl(D, Ctor_Comdat));
7032}
7033
7034void ItaniumMangleContextImpl::mangleCXXDtorComdat(const CXXDestructorDecl *D,
7035 raw_ostream &Out) {
7036 CXXNameMangler Mangler(*this, Out, D, Dtor_Comdat);
7037 Mangler.mangle(GD: GlobalDecl(D, Dtor_Comdat));
7038}
7039
7040void ItaniumMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
7041 const ThunkInfo &Thunk,
7042 raw_ostream &Out) {
7043 // <special-name> ::= T <call-offset> <base encoding>
7044 // # base is the nominal target function of thunk
7045 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
7046 // # base is the nominal target function of thunk
7047 // # first call-offset is 'this' adjustment
7048 // # second call-offset is result adjustment
7049
7050 assert(!isa<CXXDestructorDecl>(MD) &&
7051 "Use mangleCXXDtor for destructor decls!");
7052 CXXNameMangler Mangler(*this, Out);
7053 Mangler.getStream() << "_ZT";
7054 if (!Thunk.Return.isEmpty())
7055 Mangler.getStream() << 'c';
7056
7057 // Mangle the 'this' pointer adjustment.
7058 Mangler.mangleCallOffset(NonVirtual: Thunk.This.NonVirtual,
7059 Virtual: Thunk.This.Virtual.Itanium.VCallOffsetOffset);
7060
7061 // Mangle the return pointer adjustment if there is one.
7062 if (!Thunk.Return.isEmpty())
7063 Mangler.mangleCallOffset(NonVirtual: Thunk.Return.NonVirtual,
7064 Virtual: Thunk.Return.Virtual.Itanium.VBaseOffsetOffset);
7065
7066 Mangler.mangleFunctionEncoding(MD);
7067}
7068
7069void ItaniumMangleContextImpl::mangleCXXDtorThunk(
7070 const CXXDestructorDecl *DD, CXXDtorType Type,
7071 const ThisAdjustment &ThisAdjustment, raw_ostream &Out) {
7072 // <special-name> ::= T <call-offset> <base encoding>
7073 // # base is the nominal target function of thunk
7074 CXXNameMangler Mangler(*this, Out, DD, Type);
7075 Mangler.getStream() << "_ZT";
7076
7077 // Mangle the 'this' pointer adjustment.
7078 Mangler.mangleCallOffset(NonVirtual: ThisAdjustment.NonVirtual,
7079 Virtual: ThisAdjustment.Virtual.Itanium.VCallOffsetOffset);
7080
7081 Mangler.mangleFunctionEncoding(GD: GlobalDecl(DD, Type));
7082}
7083
7084/// Returns the mangled name for a guard variable for the passed in VarDecl.
7085void ItaniumMangleContextImpl::mangleStaticGuardVariable(const VarDecl *D,
7086 raw_ostream &Out) {
7087 // <special-name> ::= GV <object name> # Guard variable for one-time
7088 // # initialization
7089 CXXNameMangler Mangler(*this, Out);
7090 // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to
7091 // be a bug that is fixed in trunk.
7092 Mangler.getStream() << "_ZGV";
7093 Mangler.mangleName(GD: D);
7094}
7095
7096void ItaniumMangleContextImpl::mangleDynamicInitializer(const VarDecl *MD,
7097 raw_ostream &Out) {
7098 // These symbols are internal in the Itanium ABI, so the names don't matter.
7099 // Clang has traditionally used this symbol and allowed LLVM to adjust it to
7100 // avoid duplicate symbols.
7101 Out << "__cxx_global_var_init";
7102}
7103
7104void ItaniumMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
7105 raw_ostream &Out) {
7106 // Prefix the mangling of D with __dtor_.
7107 CXXNameMangler Mangler(*this, Out);
7108 Mangler.getStream() << "__dtor_";
7109 if (shouldMangleDeclName(D))
7110 Mangler.mangle(GD: D);
7111 else
7112 Mangler.getStream() << D->getName();
7113}
7114
7115void ItaniumMangleContextImpl::mangleDynamicStermFinalizer(const VarDecl *D,
7116 raw_ostream &Out) {
7117 // Clang generates these internal-linkage functions as part of its
7118 // implementation of the XL ABI.
7119 CXXNameMangler Mangler(*this, Out);
7120 Mangler.getStream() << "__finalize_";
7121 if (shouldMangleDeclName(D))
7122 Mangler.mangle(GD: D);
7123 else
7124 Mangler.getStream() << D->getName();
7125}
7126
7127void ItaniumMangleContextImpl::mangleSEHFilterExpression(
7128 GlobalDecl EnclosingDecl, raw_ostream &Out) {
7129 CXXNameMangler Mangler(*this, Out);
7130 Mangler.getStream() << "__filt_";
7131 auto *EnclosingFD = cast<FunctionDecl>(Val: EnclosingDecl.getDecl());
7132 if (shouldMangleDeclName(EnclosingFD))
7133 Mangler.mangle(GD: EnclosingDecl);
7134 else
7135 Mangler.getStream() << EnclosingFD->getName();
7136}
7137
7138void ItaniumMangleContextImpl::mangleSEHFinallyBlock(
7139 GlobalDecl EnclosingDecl, raw_ostream &Out) {
7140 CXXNameMangler Mangler(*this, Out);
7141 Mangler.getStream() << "__fin_";
7142 auto *EnclosingFD = cast<FunctionDecl>(Val: EnclosingDecl.getDecl());
7143 if (shouldMangleDeclName(EnclosingFD))
7144 Mangler.mangle(GD: EnclosingDecl);
7145 else
7146 Mangler.getStream() << EnclosingFD->getName();
7147}
7148
7149void ItaniumMangleContextImpl::mangleItaniumThreadLocalInit(const VarDecl *D,
7150 raw_ostream &Out) {
7151 // <special-name> ::= TH <object name>
7152 CXXNameMangler Mangler(*this, Out);
7153 Mangler.getStream() << "_ZTH";
7154 Mangler.mangleName(GD: D);
7155}
7156
7157void
7158ItaniumMangleContextImpl::mangleItaniumThreadLocalWrapper(const VarDecl *D,
7159 raw_ostream &Out) {
7160 // <special-name> ::= TW <object name>
7161 CXXNameMangler Mangler(*this, Out);
7162 Mangler.getStream() << "_ZTW";
7163 Mangler.mangleName(GD: D);
7164}
7165
7166void ItaniumMangleContextImpl::mangleReferenceTemporary(const VarDecl *D,
7167 unsigned ManglingNumber,
7168 raw_ostream &Out) {
7169 // We match the GCC mangling here.
7170 // <special-name> ::= GR <object name>
7171 CXXNameMangler Mangler(*this, Out);
7172 Mangler.getStream() << "_ZGR";
7173 Mangler.mangleName(GD: D);
7174 assert(ManglingNumber > 0 && "Reference temporary mangling number is zero!");
7175 Mangler.mangleSeqID(SeqID: ManglingNumber - 1);
7176}
7177
7178void ItaniumMangleContextImpl::mangleCXXVTable(const CXXRecordDecl *RD,
7179 raw_ostream &Out) {
7180 // <special-name> ::= TV <type> # virtual table
7181 CXXNameMangler Mangler(*this, Out);
7182 Mangler.getStream() << "_ZTV";
7183 Mangler.mangleNameOrStandardSubstitution(RD);
7184}
7185
7186void ItaniumMangleContextImpl::mangleCXXVTT(const CXXRecordDecl *RD,
7187 raw_ostream &Out) {
7188 // <special-name> ::= TT <type> # VTT structure
7189 CXXNameMangler Mangler(*this, Out);
7190 Mangler.getStream() << "_ZTT";
7191 Mangler.mangleNameOrStandardSubstitution(RD);
7192}
7193
7194void ItaniumMangleContextImpl::mangleCXXCtorVTable(const CXXRecordDecl *RD,
7195 int64_t Offset,
7196 const CXXRecordDecl *Type,
7197 raw_ostream &Out) {
7198 // <special-name> ::= TC <type> <offset number> _ <base type>
7199 CXXNameMangler Mangler(*this, Out);
7200 Mangler.getStream() << "_ZTC";
7201 Mangler.mangleNameOrStandardSubstitution(RD);
7202 Mangler.getStream() << Offset;
7203 Mangler.getStream() << '_';
7204 Mangler.mangleNameOrStandardSubstitution(Type);
7205}
7206
7207void ItaniumMangleContextImpl::mangleCXXRTTI(QualType Ty, raw_ostream &Out) {
7208 // <special-name> ::= TI <type> # typeinfo structure
7209 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
7210 CXXNameMangler Mangler(*this, Out);
7211 Mangler.getStream() << "_ZTI";
7212 Mangler.mangleType(T: Ty);
7213}
7214
7215void ItaniumMangleContextImpl::mangleCXXRTTIName(
7216 QualType Ty, raw_ostream &Out, bool NormalizeIntegers = false) {
7217 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
7218 CXXNameMangler Mangler(*this, Out, NormalizeIntegers);
7219 Mangler.getStream() << "_ZTS";
7220 Mangler.mangleType(T: Ty);
7221}
7222
7223void ItaniumMangleContextImpl::mangleCanonicalTypeName(
7224 QualType Ty, raw_ostream &Out, bool NormalizeIntegers = false) {
7225 mangleCXXRTTIName(Ty, Out, NormalizeIntegers);
7226}
7227
7228void ItaniumMangleContextImpl::mangleStringLiteral(const StringLiteral *, raw_ostream &) {
7229 llvm_unreachable("Can't mangle string literals");
7230}
7231
7232void ItaniumMangleContextImpl::mangleLambdaSig(const CXXRecordDecl *Lambda,
7233 raw_ostream &Out) {
7234 CXXNameMangler Mangler(*this, Out);
7235 Mangler.mangleLambdaSig(Lambda);
7236}
7237
7238void ItaniumMangleContextImpl::mangleModuleInitializer(const Module *M,
7239 raw_ostream &Out) {
7240 // <special-name> ::= GI <module-name> # module initializer function
7241 CXXNameMangler Mangler(*this, Out);
7242 Mangler.getStream() << "_ZGI";
7243 Mangler.mangleModuleNamePrefix(Name: M->getPrimaryModuleInterfaceName());
7244 if (M->isModulePartition()) {
7245 // The partition needs including, as partitions can have them too.
7246 auto Partition = M->Name.find(c: ':');
7247 Mangler.mangleModuleNamePrefix(
7248 Name: StringRef(&M->Name[Partition + 1], M->Name.size() - Partition - 1),
7249 /*IsPartition*/ true);
7250 }
7251}
7252
7253ItaniumMangleContext *ItaniumMangleContext::create(ASTContext &Context,
7254 DiagnosticsEngine &Diags,
7255 bool IsAux) {
7256 return new ItaniumMangleContextImpl(
7257 Context, Diags,
7258 [](ASTContext &, const NamedDecl *) -> std::optional<unsigned> {
7259 return std::nullopt;
7260 },
7261 IsAux);
7262}
7263
7264ItaniumMangleContext *
7265ItaniumMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags,
7266 DiscriminatorOverrideTy DiscriminatorOverride,
7267 bool IsAux) {
7268 return new ItaniumMangleContextImpl(Context, Diags, DiscriminatorOverride,
7269 IsAux);
7270}
7271

source code of clang/lib/AST/ItaniumMangle.cpp