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

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