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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

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