clang 19.0.0git
ItaniumMangle.cpp
Go to the documentation of this file.
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
18#include "clang/AST/Attr.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/ExprCXX.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"
32#include "clang/Basic/Module.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>(DC) || isa<ObjCMethodDecl>(DC) || isa<BlockDecl>(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>(decl);
59 return (fn ? getStructor(fn) : decl);
60}
61
62static bool isLambda(const NamedDecl *ND) {
63 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(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;
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;
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;
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>(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, 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(DC, 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>(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(DefaultArgNo);
188 Name += "_";
189 }
190
191 if (LambdaManglingNumber)
192 LambdaId = LambdaManglingNumber;
193 else
194 LambdaId = getAnonymousStructIdForDebugInfo(Lambda);
195
196 Name += llvm::utostr(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(cast<Decl>(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, 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(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(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_,
424 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
425 AbiTagsRoot(AbiTags) {}
426 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
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(reinterpret_cast<uintptr_t>(ND));
488 }
489 void addSubstitution(NestedNameSpecifier *NNS) {
490 NNS = Context.getASTContext().getCanonicalNestedNameSpecifier(NNS);
491
492 addSubstitution(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,
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,
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,
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,
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("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>(D)) {
663 if (RD->isLambda())
664 if (ParmVarDecl *ContextParam =
665 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
666 return ContextParam->getDeclContext();
667 }
668
669 // Perform the same check for block literals.
670 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
671 if (ParmVarDecl *ContextParam =
672 dyn_cast_or_null<ParmVarDecl>(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>(DC) || isa<OMPDeclareReductionDecl>(DC) ||
689 isa<OMPDeclareMapperDecl>(DC)) {
690 return getEffectiveDeclContext(cast<Decl>(DC));
691 }
692
693 if (const auto *VD = dyn_cast<VarDecl>(D))
694 if (VD->isExternC())
695 return getASTContext().getTranslationUnitDecl();
696
697 if (const auto *FD = dyn_cast<FunctionDecl>(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)
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() &&
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>(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>(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>(D)) {
779 // Decompositions are mangled.
780 if (isa<DecompositionDecl>(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(*this, VD) &&
796 !isa<VarTemplateSpecializationDecl>(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, DisableDerivedAbiTags ? nullptr : AdditionalAbiTags);
808}
809
810void CXXNameMangler::mangleSourceNameWithAbiTags(
811 const NamedDecl *ND, const AbiTagList *AdditionalAbiTags) {
812 mangleSourceName(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>(GD.getDecl()))
822 mangleFunctionEncoding(GD);
824 BindingDecl>(GD.getDecl()))
825 mangleName(GD);
826 else if (const IndirectFieldDecl *IFD =
827 dyn_cast<IndirectFieldDecl>(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>(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);
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(FD, nullptr);
871 FunctionTypeDepth.pop(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 std::set_difference(ReturnTypeAbiTags.begin(), ReturnTypeAbiTags.end(),
884 UsedAbiTags.begin(), UsedAbiTags.end(),
885 AdditionalAbiTags.begin()),
886 AdditionalAbiTags.end());
887
888 // Output name with implicit tags and function encoding from temporary buffer.
889 Saved = FunctionTypeDepth.push();
890 mangleNameWithAbiTags(FD, &AdditionalAbiTags);
891 FunctionTypeDepth.pop(Saved);
892 Out << FunctionEncodingStream.str().substr(EncodingPositionStart);
893
894 // Function encoding could create new substitutions so we have to add
895 // temp mangled substitutions to main mangler.
896 extendSubstitutions(&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(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(EIA->getCond());
915 Out << 'E';
916 } else {
917 mangleTemplateArgExpr(EIA->getCond());
918 }
919 }
920 Out << 'E';
921 FunctionTypeDepth.pop(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>(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>(FD) || isa<CXXDestructorDecl>(FD) ||
949 isa<CXXConversionDecl>(FD)))
950 MangleReturnType = true;
951
952 // Mangle the type of the primary template.
953 FD = PrimaryTemplate->getTemplatedDecl();
954 }
955
956 mangleBareFunctionType(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
966 return II && II->isStr("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(cast<NamespaceDecl>(DC));
976}
977
978static const GlobalDecl
979isTemplate(GlobalDecl GD, const TemplateArgumentList *&TemplateArgs) {
980 const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
981 // Check if we have a function template.
982 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(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>(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>(ND)) {
999 TemplateArgs = &Spec->getTemplateArgs();
1000 return GD.getWithDecl(Spec->getSpecializedTemplate());
1001 }
1002
1003 return GlobalDecl();
1004}
1005
1007 const TemplateDecl *TD = dyn_cast_or_null<TemplateDecl>(GD.getDecl());
1008 return TemplateName(const_cast<TemplateDecl*>(TD));
1009}
1010
1011void CXXNameMangler::mangleName(GlobalDecl GD) {
1012 const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
1013 if (const VarDecl *VD = dyn_cast<VarDecl>(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(VD, 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(VD, 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 std::set_difference(VariableTypeAbiTags.begin(),
1034 VariableTypeAbiTags.end(), UsedAbiTags.begin(),
1035 UsedAbiTags.end(), AdditionalAbiTags.begin()),
1036 AdditionalAbiTags.end());
1037
1038 // Output name with implicit tags.
1039 mangleNameWithAbiTags(VD, &AdditionalAbiTags);
1040 } else {
1041 mangleNameWithAbiTags(GD, 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>(D);
1050 D = cast<Decl>(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>(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(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(TD, DC, AdditionalAbiTags);
1099 mangleTemplateArgs(asTemplateName(TD), *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(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(Name);
1124 if (It != ModuleSubstitutions.end()) {
1125 Out << 'S';
1126 mangleSeqID(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('.');
1133 if (Parts.second.empty())
1134 Parts.second = Parts.first;
1135 else {
1136 mangleModuleNamePrefix(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({Name, SeqID++});
1145}
1146
1147void CXXNameMangler::mangleTemplateName(const TemplateDecl *TD,
1149 const DeclContext *DC = Context.getEffectiveDeclContext(TD);
1150
1151 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
1152 mangleUnscopedTemplateName(TD, DC, nullptr);
1153 mangleTemplateArgs(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>(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>(ND)) {
1181 assert(!AdditionalAbiTags &&
1182 "template template param cannot have abi tags");
1183 mangleTemplateParameter(TTP->getDepth(), TTP->getIndex());
1184 } else if (isa<BuiltinTemplateDecl>(ND) || isa<ConceptDecl>(ND)) {
1185 mangleUnscopedName(GD, DC, AdditionalAbiTags);
1186 } else {
1187 mangleUnscopedName(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(buffer.data(), numCharacters);
1234}
1235
1236void CXXNameMangler::mangleFloatLiteral(QualType T, const llvm::APFloat &V) {
1237 Out << 'L';
1238 mangleType(T);
1239 mangleFloat(V);
1240 Out << 'E';
1241}
1242
1243void CXXNameMangler::mangleFixedPointLiteral() {
1244 DiagnosticsEngine &Diags = Context.getDiags();
1245 unsigned DiagID = Diags.getCustomDiagID(
1246 DiagnosticsEngine::Error, "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(Out, /*signed*/ false);
1261 } else {
1262 Value.print(Out, /*signed*/ 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(NonVirtual);
1285 Out << '_';
1286 return;
1287 }
1288
1289 Out << 'v';
1290 mangleNumber(NonVirtual);
1291 Out << '_';
1292 mangleNumber(Virtual);
1293 Out << '_';
1294}
1295
1296void CXXNameMangler::manglePrefix(QualType type) {
1297 if (const auto *TST = type->getAs<TemplateSpecializationType>()) {
1298 if (!mangleSubstitution(QualType(TST, 0))) {
1299 mangleTemplatePrefix(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(TST->getTemplateName(), TST->template_arguments());
1305 addSubstitution(QualType(TST, 0));
1306 }
1307 } else if (const auto *DTST =
1309 if (!mangleSubstitution(QualType(DTST, 0))) {
1310 TemplateName Template = getASTContext().getDependentTemplateName(
1311 DTST->getQualifier(), 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(Template, DTST->template_arguments());
1318 addSubstitution(QualType(DTST, 0));
1319 }
1320 } else {
1321 // We use the QualType mangle type variant here because it handles
1322 // substitutions.
1323 mangleType(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()) {
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
1360 llvm_unreachable("Can't mangle __super specifier");
1361
1363 if (qualifier->getPrefix())
1364 mangleUnresolvedPrefix(qualifier->getPrefix(),
1365 /*recursive*/ true);
1366 else
1367 Out << "sr";
1368 mangleSourceNameWithAbiTags(qualifier->getAsNamespace());
1369 break;
1371 if (qualifier->getPrefix())
1372 mangleUnresolvedPrefix(qualifier->getPrefix(),
1373 /*recursive*/ true);
1374 else
1375 Out << "sr";
1376 mangleSourceNameWithAbiTags(qualifier->getAsNamespaceAlias());
1377 break;
1378
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->getPrefix(),
1390 /*recursive*/ true);
1391 } else {
1392 // Otherwise, all the cases want this.
1393 Out << "sr";
1394 }
1395
1396 if (mangleUnresolvedTypeOrSimpleId(QualType(type, 0), recursive ? "N" : ""))
1397 return;
1398
1399 break;
1400 }
1401
1403 // Member expressions can have these without prefixes.
1404 if (qualifier->getPrefix())
1405 mangleUnresolvedPrefix(qualifier->getPrefix(),
1406 /*recursive*/ true);
1407 else
1408 Out << "sr";
1409
1410 mangleSourceName(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>
1431 mangleSourceName(name.getAsIdentifierInfo());
1432 break;
1433 // <base-unresolved-name> ::= dn <destructor-name>
1435 Out << "dn";
1436 mangleUnresolvedTypeOrSimpleId(name.getCXXNameType());
1437 break;
1438 // <base-unresolved-name> ::= on <operator-name>
1442 Out << "on";
1443 mangleOperatorName(name, knownArity);
1444 break;
1446 llvm_unreachable("Can't mangle a constructor name!");
1448 llvm_unreachable("Can't mangle a using directive name!");
1450 llvm_unreachable("Can't mangle a deduction guide name!");
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(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>(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>(ND);
1478 auto *FTD = dyn_cast<FunctionTemplateDecl>(ND);
1479 if ((FD && FD->isMemberLikeConstrainedFriend()) ||
1480 (FTD && FTD->getTemplatedDecl()->isMemberLikeConstrainedFriend())) {
1481 if (!isCompatibleWith(LangOptions::ClangABI::Ver17))
1482 Out << 'F';
1483 }
1484
1485 unsigned Arity = KnownArity;
1486 switch (Name.getNameKind()) {
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>(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(BD->getDeclName().getAsIdentifierInfo());
1500 Out << 'E';
1501 writeAbiTags(ND, AdditionalAbiTags);
1502 break;
1503 }
1504
1505 if (auto *GD = dyn_cast<MSGuidDecl>(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>(ND)) {
1516 // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/63.
1517 Out << "TA";
1518 mangleValueInTemplateArg(TPO->getType().getUnqualifiedType(),
1519 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() ==
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>(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>(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(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>(ND))
1603 break;
1604
1605 // We must have an anonymous struct.
1606 const TagDecl *TD = cast<TagDecl>(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(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>(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(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>(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(AnonStructId);
1666
1667 Out << Str.size();
1668 Out << Str;
1669 break;
1670 }
1671
1675 llvm_unreachable("Can't mangle Objective-C selector names here!");
1676
1678 const CXXRecordDecl *InheritedFrom = nullptr;
1679 TemplateName InheritedTemplateName;
1680 const TemplateArgumentList *InheritedTemplateArgs = nullptr;
1681 if (auto Inherited =
1682 cast<CXXConstructorDecl>(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(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(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(InheritedTemplateName, *InheritedTemplateArgs);
1703
1704 writeAbiTags(ND, AdditionalAbiTags);
1705 break;
1706 }
1707
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(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(Dtor_Complete);
1717 assert(ND);
1718 writeAbiTags(ND, AdditionalAbiTags);
1719 break;
1720
1722 if (ND && Arity == UnknownArity) {
1723 Arity = cast<FunctionDecl>(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>(ND))
1727 if (MD->isImplicitObjectMemberFunction())
1728 Arity++;
1729 }
1730 [[fallthrough]];
1733 mangleOperatorName(Name, Arity);
1734 writeAbiTags(ND, AdditionalAbiTags);
1735 break;
1736
1738 llvm_unreachable("Can't mangle a deduction guide name!");
1739
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>(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>(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(MethodQuals);
1791 mangleRefQualifier(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(TD, NoFunction);
1798 mangleTemplateArgs(asTemplateName(TD), *TemplateArgs);
1799 } else {
1800 manglePrefix(DC, NoFunction);
1801 mangleUnqualifiedName(GD, DC, AdditionalAbiTags);
1802 }
1803
1804 Out << 'E';
1805}
1806void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
1808 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1809
1810 Out << 'N';
1811
1812 mangleTemplatePrefix(TD);
1813 mangleTemplateArgs(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(PrefixND);
1830 mangleUnqualifiedName(GD, nullptr, AdditionalAbiTags);
1831
1832 Out << 'E';
1833}
1834
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>(DC))
1842 GD = GlobalDecl(CD, Ctor_Complete);
1843 else if (auto *DD = dyn_cast<CXXDestructorDecl>(DC))
1844 GD = GlobalDecl(DD, Dtor_Complete);
1845 else
1846 GD = GlobalDecl(cast<FunctionDecl>(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(RD ? RD : D);
1861
1862 Out << 'Z';
1863
1864 {
1865 AbiTagState LocalAbiTags(AbiTags);
1866
1867 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
1868 mangleObjCMethodName(MD);
1869 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC))
1870 mangleBlockForPrefix(BD);
1871 else
1872 mangleFunctionEncoding(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>(RD);
1891 if (CXXRD && CXXRD->isLambda()) {
1892 if (const ParmVarDecl *Parm
1893 = dyn_cast_or_null<ParmVarDecl>(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(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>(D)) {
1910 if (const NamedDecl *PrefixND = getClosurePrefix(BD))
1911 mangleClosurePrefix(PrefixND, true /*NoFunction*/);
1912 else
1913 manglePrefix(Context.getEffectiveDeclContext(BD), true /*NoFunction*/);
1914 assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1915 mangleUnqualifiedBlock(BD);
1916 } else {
1917 const NamedDecl *ND = cast<NamedDecl>(D);
1918 mangleNestedName(GD, Context.getEffectiveDeclContext(ND),
1919 AdditionalAbiTags, true /*NoFunction*/);
1920 }
1921 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(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>(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(Num - 2);
1932 Out << '_';
1933 }
1934 }
1935
1936 assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1937 mangleUnqualifiedBlock(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(Block, /* AdditionalAbiTags */ nullptr);
1956 return;
1957 }
1958 const DeclContext *DC = Context.getEffectiveDeclContext(Block);
1959 if (isLocalContainerContext(DC)) {
1960 mangleLocalName(Block, /* AdditionalAbiTags */ nullptr);
1961 return;
1962 }
1963 if (const NamedDecl *PrefixND = getClosurePrefix(Block))
1964 mangleClosurePrefix(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(LangOptions::ClangABI::Ver12) &&
1975 (isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1976 Context->getDeclContext()->isRecord()) {
1977 const auto *ND = cast<NamedDecl>(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(Block, 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>(Decl)) {
2011 if (Ty->isParameterPack())
2012 Out << "Tp";
2013 const TypeConstraint *Constraint = Ty->getTypeConstraint();
2014 if (Constraint && !isCompatibleWith(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>(Decl)) {
2022 if (Tn->isExpandedParameterPack()) {
2023 for (unsigned I = 0, N = Tn->getNumExpansionTypes(); I != N; ++I) {
2024 Out << "Tn";
2025 mangleType(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>(Decl)) {
2038 if (Tt->isExpandedParameterPack()) {
2039 for (unsigned I = 0, N = Tt->getNumExpansionTemplateParameters(); I != N;
2040 ++I)
2041 mangleTemplateParameterList(Tt->getExpansionTemplateParameters(I));
2042 } else {
2043 if (Tt->isParameterPack())
2044 Out << "Tp";
2045 mangleTemplateParameterList(Tt->getTemplateParameters());
2046 }
2047 }
2048}
2049
2050void CXXNameMangler::mangleTemplateParameterList(
2051 const TemplateParameterList *Params) {
2052 Out << "Tt";
2053 for (auto *Param : *Params)
2054 mangleTemplateParamDecl(Param);
2055 mangleRequiresClause(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) {
2072 if (Constraint->getTemplateArgsAsWritten()) {
2073 for (const TemplateArgumentLoc &ArgLoc :
2074 Constraint->getTemplateArgsAsWritten()->arguments())
2075 Args.push_back(ArgLoc.getArgument());
2076 }
2077 return mangleTypeConstraint(Constraint->getNamedConcept(), 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(LangOptions::ClangABI::Ver17)) {
2083 Out << 'Q';
2084 mangleExpression(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(LangOptions::ClangABI::Ver12) &&
2093 (isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
2094 !isa<ParmVarDecl>(Context)) {
2095 if (const IdentifierInfo *Name
2096 = cast<NamedDecl>(Context)->getIdentifier()) {
2097 mangleSourceName(Name);
2098 const TemplateArgumentList *TemplateArgs = nullptr;
2099 if (GlobalDecl TD = isTemplate(cast<NamedDecl>(Context), TemplateArgs))
2100 mangleTemplateArgs(asTemplateName(TD), *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 - 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(D);
2136
2137 // Proposed on https://github.com/itanium-cxx-abi/cxx-abi/issues/24.
2138 if (auto *TPL = Lambda->getGenericLambdaTemplateParameterList())
2139 mangleRequiresClause(TPL->getRequiresClause());
2140
2141 auto *Proto =
2143 mangleBareFunctionType(Proto, /*MangleReturnType=*/false,
2144 Lambda->getLambdaStaticInvoker());
2145}
2146
2147void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
2148 switch (qualifier->getKind()) {
2150 // nothing
2151 return;
2152
2154 llvm_unreachable("Can't mangle __super specifier");
2155
2157 mangleName(qualifier->getAsNamespace());
2158 return;
2159
2161 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
2162 return;
2163
2166 manglePrefix(QualType(qualifier->getAsType(), 0));
2167 return;
2168
2170 // Clang 14 and before did not consider this substitutable.
2171 bool Clang14Compat = isCompatibleWith(LangOptions::ClangABI::Ver14);
2172 if (!Clang14Compat && mangleSubstitution(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->getPrefix());
2179
2180 mangleSourceName(qualifier->getAsIdentifier());
2181
2182 if (!Clang14Compat)
2183 addSubstitution(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>(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(ND, TemplateArgs)) {
2213 mangleTemplatePrefix(TD);
2214 mangleTemplateArgs(asTemplateName(TD), *TemplateArgs);
2215 } else if (const NamedDecl *PrefixND = getClosurePrefix(ND)) {
2216 mangleClosurePrefix(PrefixND, NoFunction);
2217 mangleUnqualifiedName(ND, nullptr, nullptr);
2218 } else {
2219 const DeclContext *DC = Context.getEffectiveDeclContext(ND);
2220 manglePrefix(DC, NoFunction);
2221 mangleUnqualifiedName(ND, DC, 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(LangOptions::ClangABI::Ver11);
2240 if (!Clang11Compat && mangleSubstitution(Template))
2241 return;
2242
2243 if (NestedNameSpecifier *Qualifier = Dependent->getQualifier())
2244 manglePrefix(Qualifier);
2245
2246 if (Clang11Compat && mangleSubstitution(Template))
2247 return;
2248
2249 if (const IdentifierInfo *Id = Dependent->getIdentifier())
2250 mangleSourceName(Id);
2251 else
2252 mangleOperatorName(Dependent->getOperator(), UnknownArity);
2253
2254 addSubstitution(Template);
2255}
2256
2257void CXXNameMangler::mangleTemplatePrefix(GlobalDecl GD,
2258 bool NoFunction) {
2259 const TemplateDecl *ND = cast<TemplateDecl>(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>(ND)) {
2271 mangleTemplateParameter(TTP->getDepth(), TTP->getIndex());
2272 } else {
2273 const DeclContext *DC = Context.getEffectiveDeclContext(ND);
2274 manglePrefix(DC, NoFunction);
2275 if (isa<BuiltinTemplateDecl>(ND) || isa<ConceptDecl>(ND))
2276 mangleUnqualifiedName(GD, DC, nullptr);
2277 else
2278 mangleUnqualifiedName(GD.getWithDecl(ND->getTemplatedDecl()), DC,
2279 nullptr);
2280 }
2281
2282 addSubstitution(ND);
2283}
2284
2285const NamedDecl *CXXNameMangler::getClosurePrefix(const Decl *ND) {
2286 if (isCompatibleWith(LangOptions::ClangABI::Ver12))
2287 return nullptr;
2288
2289 const NamedDecl *Context = nullptr;
2290 if (auto *Block = dyn_cast<BlockDecl>(ND)) {
2291 Context = dyn_cast_or_null<NamedDecl>(Block->getBlockManglingContextDecl());
2292 } else if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
2293 if (RD->isLambda())
2294 Context = dyn_cast_or_null<NamedDecl>(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>(Context) && cast<VarDecl>(Context)->hasGlobalStorage()) ||
2302 isa<FieldDecl>(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(ND, TemplateArgs)) {
2316 mangleTemplatePrefix(TD, NoFunction);
2317 mangleTemplateArgs(asTemplateName(TD), *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(TN))
2336 return;
2337
2338 TemplateDecl *TD = nullptr;
2339
2340 switch (TN.getKind()) {
2344 TD = TN.getAsTemplateDecl();
2345 goto HaveDecl;
2346
2347 HaveDecl:
2348 if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TD))
2349 mangleTemplateParameter(TTP->getDepth(), TTP->getIndex());
2350 else
2351 mangleName(TD);
2352 break;
2353
2356 llvm_unreachable("can't mangle an overloaded template name as a <type>");
2357
2359 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
2360 assert(Dependent->isIdentifier());
2361
2362 // <class-enum-type> ::= <name>
2363 // <name> ::= <nested-name>
2364 mangleUnresolvedPrefix(Dependent->getQualifier());
2365 mangleSourceName(Dependent->getIdentifier());
2366 break;
2367 }
2368
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.
2376 mangleType(subst->getReplacement());
2377 return;
2378 }
2379
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(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(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>(Ty)->getDecl());
2471 break;
2472
2473 case Type::UnresolvedUsing:
2474 mangleSourceNameWithAbiTags(
2475 cast<UnresolvedUsingType>(Ty)->getDecl());
2476 break;
2477
2478 case Type::Enum:
2479 case Type::Record:
2480 mangleSourceNameWithAbiTags(cast<TagType>(Ty)->getDecl());
2481 break;
2482
2483 case Type::TemplateSpecialization: {
2484 const TemplateSpecializationType *TST =
2485 cast<TemplateSpecializationType>(Ty);
2486 TemplateName TN = TST->getTemplateName();
2487 switch (TN.getKind()) {
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>(TD))
2496 goto unresolvedType;
2497
2498 mangleSourceNameWithAbiTags(TD);
2499 break;
2500 }
2501
2505 llvm_unreachable("invalid base for a template specialization type");
2506
2510 mangleExistingSubstitution(subst->getReplacement());
2511 break;
2512 }
2513
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 }
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(TemplateName(), TST->template_arguments());
2536 break;
2537 }
2538
2539 case Type::InjectedClassName:
2540 mangleSourceNameWithAbiTags(
2541 cast<InjectedClassNameType>(Ty)->getDecl());
2542 break;
2543
2544 case Type::DependentName:
2545 mangleSourceName(cast<DependentNameType>(Ty)->getIdentifier());
2546 break;
2547
2548 case Type::DependentTemplateSpecialization: {
2550 cast<DependentTemplateSpecializationType>(Ty);
2551 TemplateName Template = getASTContext().getDependentTemplateName(
2552 DTST->getQualifier(), DTST->getIdentifier());
2553 mangleSourceName(DTST->getIdentifier());
2554 mangleTemplateArgs(Template, DTST->template_arguments());
2555 break;
2556 }
2557
2558 case Type::Using:
2559 return mangleUnresolvedTypeOrSimpleId(cast<UsingType>(Ty)->desugar(),
2560 Prefix);
2561 case Type::Elaborated:
2562 return mangleUnresolvedTypeOrSimpleId(
2563 cast<ElaboratedType>(Ty)->getNamedType(), Prefix);
2564 }
2565
2566 return false;
2567}
2568
2569void CXXNameMangler::mangleOperatorName(DeclarationName Name, unsigned Arity) {
2570 switch (Name.getNameKind()) {
2579 llvm_unreachable("Not an operator name");
2580
2582 // <operator-name> ::= cv <type> # (cast)
2583 Out << "cv";
2584 mangleType(Name.getCXXNameType());
2585 break;
2586
2588 Out << "li";
2589 mangleSourceName(Name.getCXXLiteralIdentifier());
2590 return;
2591
2593 mangleOperatorName(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:
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(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(LangAS::Default) != 0)
2739 ASString = "AS" + llvm::utostr(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(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.
2820 mangleVendorQualifier("__weak");
2821
2822 // __unaligned (from -fms-extensions)
2823 if (Quals.hasUnaligned())
2824 mangleVendorQualifier("__unaligned");
2825
2826 // Remaining ARC ownership qualifiers.
2827 switch (Quals.getObjCLifetime()) {
2829 break;
2830
2832 // Do nothing as we already handled this case above.
2833 break;
2834
2836 mangleVendorQualifier("__strong");
2837 break;
2838
2840 mangleVendorQualifier("__autoreleasing");
2841 break;
2842
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(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>(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.
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>(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.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, ty, 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>(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>(ty)) {
2995 SplitQualType splitDAST = DAST->getPointeeType().split();
2996 mangleQualifiers(splitDAST.Quals, DAST);
2997 mangleType(QualType(splitDAST.Ty, 0));
2998 } else {
2999 mangleQualifiers(quals);
3000
3001 // Recurse: even if the qualified type isn't yet substitutable,
3002 // the unqualified type might be.
3003 mangleType(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(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(BuiltinType::SChar))
3083 break;
3084 Out << "u2i8";
3085 addSubstitution(BuiltinType::SChar);
3086 break;
3087 case 16:
3088 if (mangleSubstitution(BuiltinType::Short))
3089 break;
3090 Out << "u3i16";
3091 addSubstitution(BuiltinType::Short);
3092 break;
3093 case 32:
3094 if (mangleSubstitution(BuiltinType::Int))
3095 break;
3096 Out << "u3i32";
3097 addSubstitution(BuiltinType::Int);
3098 break;
3099 case 64:
3100 if (mangleSubstitution(BuiltinType::Long))
3101 break;
3102 Out << "u3i64";
3103 addSubstitution(BuiltinType::Long);
3104 break;
3105 case 128:
3106 if (mangleSubstitution(BuiltinType::Int128))
3107 break;
3108 Out << "u4i128";
3109 addSubstitution(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(BuiltinType::UChar))
3118 break;
3119 Out << "u2u8";
3120 addSubstitution(BuiltinType::UChar);
3121 break;
3122 case 16:
3123 if (mangleSubstitution(BuiltinType::UShort))
3124 break;
3125 Out << "u3u16";
3126 addSubstitution(BuiltinType::UShort);
3127 break;
3128 case 32:
3129 if (mangleSubstitution(BuiltinType::UInt))
3130 break;
3131 Out << "u3u32";
3132 addSubstitution(BuiltinType::UInt);
3133 break;
3134 case 64:
3135 if (mangleSubstitution(BuiltinType::ULong))
3136 break;
3137 Out << "u3u64";
3138 addSubstitution(BuiltinType::ULong);
3139 break;
3140 case 128:
3141 if (mangleSubstitution(BuiltinType::UInt128))
3142 break;
3143 Out << "u4u128";
3144 addSubstitution(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:
3440 case CC_AArch64SVEPCS:
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.
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(T->getExtInfo().getCC());
3487 if (!CCQualifier.empty())
3488 mangleVendorQualifier(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(getParameterABISpelling(PI.getABI()));
3511 break;
3512 }
3513
3514 if (PI.isConsumed())
3515 mangleVendorQualifier("ns_consumed");
3516
3517 if (PI.isNoEscape())
3518 mangleVendorQualifier("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(T->getMethodQuals());
3530
3531 // Mangle instantiation-dependent exception-specification, if present,
3532 // per cxx-abi-dev proposal on 2016-10-11.
3535 Out << "DO";
3536 mangleExpression(T->getNoexceptExpr());
3537 Out << "E";
3538 } else {
3539 assert(T->getExceptionSpecType() == EST_Dynamic);
3540 Out << "Dw";
3541 for (auto ExceptTy : T->exceptions())
3542 mangleType(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(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("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(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(Proto->getExtParameterInfo(I));
3613 }
3614
3615 // Mangle the type.
3616 QualType ParamTy = Proto->getParamType(I);
3617 mangleType(Context.getASTContext().getSignatureParameterType(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(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(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(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(QualType(T->getClass(), 0));
3698 QualType PointeeType = T->getPointeeType();
3699 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(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(PointeeType);
3721}
3722
3723// <type> ::= <template-param>
3724void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
3725 mangleTemplateParameter(T->getDepth(), 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->getPointeeType());
3741}
3742void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
3743 Out << 'P';
3744 mangleType(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->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>(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>(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(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(BaseName) + strlen(EltName);
3816 Out << BaseName << EltName;
3817}
3818
3819void CXXNameMangler::mangleNeonVectorType(const DependentVectorType *T) {
3820 DiagnosticsEngine &Diags = Context.getDiags();
3821 unsigned DiagID = Diags.getCustomDiagID(
3823 "cannot mangle this dependent neon vector type yet");
3824 Diags.Report(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(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>(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(cast<BuiltinType>(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(
3900 "cannot mangle this dependent neon vector type yet");
3901 Diags.Report(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>(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(
3997 "cannot mangle this dependent fixed-length SVE vector type yet");
3998 Diags.Report(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>(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 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(
4081 "cannot mangle this dependent fixed-length RVV vector type yet");
4082 Diags.Report(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->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(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->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(T->getSizeExpr());
4161 Out << '_';
4162 mangleType(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(ASTCtx.getSizeType());
4175 llvm::APSInt Rows(BitWidth);
4176 Rows = T->getNumRows();
4177 mangleIntegerLiteral(ASTCtx.getSizeType(), Rows);
4178 llvm::APSInt Columns(BitWidth);
4179 Columns = T->getNumColumns();
4180 mangleIntegerLiteral(ASTCtx.getSizeType(), 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(T->getRowExpr());
4193 mangleTemplateArgExpr(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(split.Quals, T);
4201 mangleType(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->getPattern());
4208}
4209
4210void CXXNameMangler::mangleType(const PackIndexingType *T) {
4211 if (!T->hasSelectedType())
4212 mangleType(T->getPattern());
4213 else
4214 mangleType(T->getSelectedType());
4215}
4216
4217void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
4218 mangleSourceName(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->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(typeArg);
4245 Out << 'E';
4246 }
4247}
4248
4249void CXXNameMangler::mangleType(const BlockPointerType *T) {
4250 Out << "U13block_pointer";
4251 mangleType(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->getInjectedSpecializationType());
4259}
4260
4261void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
4262 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
4263 mangleTemplateName(TD, T->template_arguments());
4264 } else {
4265 if (mangleSubstitution(QualType(T, 0)))
4266 return;
4267
4268 mangleTemplatePrefix(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(T->getTemplateName(), T->template_arguments());
4274 addSubstitution(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(T->getQualifier());
4308 mangleSourceName(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(T->getQualifier(),
4319 T->getIdentifier());
4320 mangleTemplatePrefix(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(Prefix, 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>(E) ||
4353 isa<MemberExpr>(E) ||
4354 isa<UnresolvedLookupExpr>(E) ||
4355 isa<DependentScopeDeclRefExpr>(E) ||
4356 isa<CXXDependentScopeMemberExpr>(E) ||
4357 isa<UnresolvedMemberExpr>(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->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(LangOptions::ClangABI::Ver17)) {
4397 Out << (T->isDecltypeAuto() ? "DK" : "Dk");
4398 mangleTypeConstraint(T->getTypeConstraintConcept(),
4399 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(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(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->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(T->getNumBitsExpr());
4447 Out << "_";
4448}
4449
4450void CXXNameMangler::mangleType(const ArrayParameterType *T) {
4451 mangleType(cast<ConstantArrayType>(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>(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(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, isArrow);
4506 mangleUnresolvedName(qualifier, member, TemplateArgs, NumTemplateArgs, 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>(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>(E);
4542 Out << CastEncoding;
4543 mangleType(ECE->getType());
4544 mangleExpression(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(InitList->getInit(i));
4552}
4553
4554void CXXNameMangler::mangleRequirement(SourceLocation RequiresExprLoc,
4555 const concepts::Requirement *Req) {
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 DiagnosticsEngine::Error, "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>(Req);
4574 if (TR->isSubstitutionFailure())
4575 return HandleSubstitutionFailure(
4576 TR->getSubstitutionDiagnostic()->DiagLoc);
4577
4578 Out << 'T';
4579 mangleType(TR->getType()->getType());
4580 break;
4581 }
4582
4583 case Requirement::RK_Simple:
4584 case Requirement::RK_Compound: {
4585 const auto *ER = cast<concepts::ExprRequirement>(Req);
4586 if (ER->isExprSubstitutionFailure())
4587 return HandleSubstitutionFailure(
4588 ER->getExprSubstitutionDiagnostic()->DiagLoc);
4589
4590 Out << 'X';
4591 mangleExpression(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(ER->getReturnTypeRequirement().getTypeConstraint());
4604 }
4605 break;
4606 }
4607
4608 case Requirement::RK_Nested:
4609 const auto *NR = cast<concepts::NestedRequirement>(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(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(D);
4669 Out << 'E';
4670 break;
4671
4672 case Decl::ParmVar:
4673 NotPrimaryExpr();
4674 mangleFunctionParam(cast<ParmVarDecl>(D));
4675 break;
4676
4677 case Decl::EnumConstant: {
4678 // <expr-primary>
4679 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
4680 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
4681 break;
4682 }
4683
4684 case Decl::NonTypeTemplateParm:
4685 NotPrimaryExpr();
4686 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
4687 mangleTemplateParameter(PD->getDepth(), 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::ArraySectionExprClass:
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(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(LangOptions::ClangABI::Ver11)) {
4784 Out << "u8__uuidof";
4785 if (UE->isTypeOperand())
4786 mangleType(UE->getTypeOperand(Context.getASTContext()));
4787 else
4788 mangleTemplateArgExpr(UE->getExprOperand());
4789 Out << 'E';
4790 } else {
4791 if (UE->isTypeOperand()) {
4792 QualType UuidT = UE->getTypeOperand(Context.getASTContext());
4793 Out << "u8__uuidoft";
4794 mangleType(UuidT);
4795 } else {
4796 Expr *UuidExp = UE->getExprOperand();
4797 Out << "u8__uuidofz";
4798 mangleExpression(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 =
4810 "?: operator with omitted middle operand cannot be mangled");
4811 Diags.Report(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(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()) {
4833 Out << "di";
4834 mangleSourceName(Designator.getFieldName());
4835 } else if (Designator.isArrayDesignator()) {
4836 Out << "dx";
4837 mangleExpression(DIE->getArrayIndex(Designator));
4838 } else {
4840 "unknown designator kind");
4841 Out << "dX";
4842 mangleExpression(DIE->getArrayRangeStart(Designator));
4843 mangleExpression(DIE->getArrayRangeEnd(Designator));
4844 }
4845 }
4846 mangleExpression(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(ParamType, CE->getAPValueResult(), 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(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(CE->getCallee(), 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");
4921 E = New->placement_arg_end(); I != E; ++I)
4922 mangleExpression(*I);
4923 Out << '_';
4924 mangleType(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(PLE->getExpr(i));
4940 } else if (New->getInitializationStyle() ==
4941 CXXNewInitializationStyle::Braces &&
4942 isa<InitListExpr>(Init)) {
4943 // Only take InitListExprs apart for list-initialization.
4944 mangleInitListElements(cast<InitListExpr>(Init));
4945 } else
4946 mangleExpression(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, PDE->isArrow());
4957 NestedNameSpecifier *Qualifier = PDE->getQualifier();
4958 if (TypeSourceInfo *ScopeInfo = PDE->getScopeTypeInfo()) {
4959 if (Qualifier) {
4960 mangleUnresolvedPrefix(Qualifier,
4961 /*recursive=*/true);
4962 mangleUnresolvedTypeOrSimpleId(ScopeInfo->getType());
4963 Out << 'E';
4964 } else {
4965 Out << "sr";
4966 if (!mangleUnresolvedTypeOrSimpleId(ScopeInfo->getType()))
4967 Out << 'E';
4968 }
4969 } else if (Qualifier) {
4970 mangleUnresolvedPrefix(Qualifier);
4971 }
4972 // <base-unresolved-name> ::= dn <destructor-name>
4973 Out << "dn";
4974 QualType DestroyedType = PDE->getDestroyedType();
4975 mangleUnresolvedTypeOrSimpleId(DestroyedType);
4976 break;
4977 }
4978
4979 case Expr::MemberExprClass: {
4980 NotPrimaryExpr();
4981 const MemberExpr *ME = cast<MemberExpr>(E);
4982 mangleMemberExpr(ME->getBase(), ME->isArrow(),
4983 ME->getQualifier(), nullptr,
4984 ME->getMemberDecl()->getDeclName(),
4986 Arity);
4987 break;
4988 }
4989
4990 case Expr::UnresolvedMemberExprClass: {
4991 NotPrimaryExpr();
4992 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
4993 mangleMemberExpr(ME->isImplicitAccess() ? nullptr : ME->getBase(),
4994 ME->isArrow(), ME->getQualifier(), nullptr,
4995 ME->getMemberName(),
4997 Arity);
4998 break;
4999 }
5000
5001 case Expr::CXXDependentScopeMemberExprClass: {
5002 NotPrimaryExpr();
5004 = cast<CXXDependentScopeMemberExpr>(E);
5005 mangleMemberExpr(ME->isImplicitAccess() ? nullptr : ME->getBase(),
5006 ME->isArrow(), ME->getQualifier(),
5008 ME->getMember(),
5010 Arity);
5011 break;
5012 }
5013
5014 case Expr::UnresolvedLookupExprClass: {
5015 NotPrimaryExpr();
5016 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
5017 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(),
5018 ULE->getTemplateArgs(), ULE->getNumTemplateArgs(),
5019 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(0));
5031 Out << "tl";
5032 mangleType(CE->getType());
5033 mangleInitListElements(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(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(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(E->getType());
5099 Out << "_E";
5100 break;
5101
5102 case Expr::CXXNoexceptExprClass:
5103 NotPrimaryExpr();
5104 Out << "nx";
5105 mangleExpression(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, 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(SAE->getArgumentType());
5139 } else {
5140 Out << 'z';
5141 mangleExpression(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(LangOptions::ClangABI::Ver11)) {
5155 Out << "u11__alignof__";
5156 if (SAE->isArgumentType())
5157 mangleType(SAE->getArgumentType());
5158 else
5159 mangleTemplateArgExpr(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 =
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(
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(
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(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(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(TIE->getTypeOperand(Context.getASTContext()));
5239 } else {
5240 Out << "te";
5241 mangleExpression(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(DE->getArgument());
5254 break;
5255 }
5256
5257 case Expr::UnaryOperatorClass: {
5258 NotPrimaryExpr();
5259 const UnaryOperator *UO = cast<UnaryOperator>(E);
5260 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
5261 /*Arity=*/1);
5262 mangleExpression(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(AE->getLHS());
5274 mangleExpression(AE->getRHS());
5275 break;
5276 }
5277
5278 case Expr::MatrixSubscriptExprClass: {
5279 NotPrimaryExpr();
5280 const MatrixSubscriptExpr *ME = cast<MatrixSubscriptExpr>(E);
5281 Out << "ixix";
5282 mangleExpression(ME->getBase());
5283 mangleExpression(ME->getRowIdx());
5284 mangleExpression(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(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
5296 /*Arity=*/2);
5297 mangleExpression(BO->getLHS());
5298 mangleExpression(BO->getRHS());
5299 break;
5300 }
5301
5302 case Expr::CXXRewrittenBinaryOperatorClass: {
5303 NotPrimaryExpr();
5304 // The mangled form represents the original syntax.
5306 cast<CXXRewrittenBinaryOperator>(E)->getDecomposedForm();
5307 mangleOperatorName(BinaryOperator::getOverloadedOperator(Decomposed.Opcode),
5308 /*Arity=*/2);
5309 mangleExpression(Decomposed.LHS);
5310 mangleExpression(Decomposed.RHS);
5311 break;
5312 }
5313
5314 case Expr::ConditionalOperatorClass: {
5315 NotPrimaryExpr();
5316 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
5317 mangleOperatorName(OO_Conditional, /*Arity=*/3);
5318 mangleExpression(CO->getCond());
5319 mangleExpression(CO->getLHS(), Arity);
5320 mangleExpression(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, "cv");
5337 break;
5338 }
5339
5340 case Expr::CStyleCastExprClass:
5341 NotPrimaryExpr();
5342 mangleCastExpression(E, "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(E->getType());
5357 mangleInitListElements(IL);
5358 Out << "E";
5359 } else {
5360 mangleCastExpression(E, "cv");
5361 }
5362 break;
5363 }
5364
5365 case Expr::CXXStaticCastExprClass:
5366 NotPrimaryExpr();
5367 mangleCastExpression(E, "sc");
5368 break;
5369 case Expr::CXXDynamicCastExprClass:
5370 NotPrimaryExpr();
5371 mangleCastExpression(E, "dc");
5372 break;
5373 case Expr::CXXReinterpretCastExprClass:
5374 NotPrimaryExpr();
5375 mangleCastExpression(E, "rc");
5376 break;
5377 case Expr::CXXConstCastExprClass:
5378 NotPrimaryExpr();
5379 mangleCastExpression(E, "cc");
5380 break;
5381 case Expr::CXXAddrspaceCastExprClass:
5382 NotPrimaryExpr();
5383 mangleCastExpression(E, "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(CE->getOperator(), /*Arity=*/NumArgs);
5394 // Mangle the arguments.
5395 for (unsigned i = 0; i != NumArgs; ++i)
5396 mangleExpression(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(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(CSE->getNamedConcept(), 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 CSE->getNestedNameSpecifierLoc().getNestedNameSpecifier(),
5419 CSE->getConceptNameInfo().getName(),
5420 CSE->getTemplateArgsAsWritten()->getTemplateArgs(),
5421 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(DRE->getQualifier(), DRE->getDeclName(),
5486 DRE->getTemplateArgs(), DRE->getNumTemplateArgs(),
5487 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(FL->getType(), 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(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(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(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(llvm::APFloat(Imag->getValue().getSemantics()));
5554 Out << '_';
5555 mangleFloat(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(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(E->getType(), 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(cast<LambdaExpr>(E)->getLambdaClass()));
5595 Out << "E";
5596 break;
5597 }
5598
5599 case Expr::PackExpansionExprClass:
5600 NotPrimaryExpr();
5601 Out << "sp";
5602 mangleExpression(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(TTP->getDepth(), TTP->getIndex());
5620 else if (const NonTypeTemplateParmDecl *NTTP
5621 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
5622 mangleTemplateParameter(NTTP->getDepth(), NTTP->getIndex());
5623 else if (const TemplateTemplateParmDecl *TempTP
5624 = dyn_cast<TemplateTemplateParmDecl>(Pack))
5625 mangleTemplateParameter(TempTP->getDepth(), TempTP->getIndex());
5626 else
5627 mangleFunctionParam(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(FE->getOperator()),
5648 /*Arity=*/2);
5649
5650 if (FE->getLHS())
5651 mangleExpression(FE->getLHS());
5652 if (FE->getRHS())
5653 mangleExpression(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(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(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(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(DAST->getPointeeType().getQualifiers(), DAST);
5754 } else {
5755 mangleQualifiers(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;
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.
5819 const CXXNameMangler &Mangler;