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

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