1 | //===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===// |
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 | // This provides C++ name mangling targeting the Microsoft Visual C++ ABI. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "clang/AST/ASTContext.h" |
14 | #include "clang/AST/Attr.h" |
15 | #include "clang/AST/CXXInheritance.h" |
16 | #include "clang/AST/CharUnits.h" |
17 | #include "clang/AST/Decl.h" |
18 | #include "clang/AST/DeclCXX.h" |
19 | #include "clang/AST/DeclObjC.h" |
20 | #include "clang/AST/DeclOpenMP.h" |
21 | #include "clang/AST/DeclTemplate.h" |
22 | #include "clang/AST/Expr.h" |
23 | #include "clang/AST/ExprCXX.h" |
24 | #include "clang/AST/GlobalDecl.h" |
25 | #include "clang/AST/Mangle.h" |
26 | #include "clang/AST/VTableBuilder.h" |
27 | #include "clang/Basic/ABI.h" |
28 | #include "clang/Basic/DiagnosticOptions.h" |
29 | #include "clang/Basic/SourceManager.h" |
30 | #include "clang/Basic/TargetInfo.h" |
31 | #include "llvm/ADT/SmallVector.h" |
32 | #include "llvm/ADT/StringExtras.h" |
33 | #include "llvm/Support/CRC.h" |
34 | #include "llvm/Support/MD5.h" |
35 | #include "llvm/Support/StringSaver.h" |
36 | #include "llvm/Support/xxhash.h" |
37 | #include <functional> |
38 | #include <optional> |
39 | |
40 | using namespace clang; |
41 | |
42 | namespace { |
43 | |
44 | // Get GlobalDecl of DeclContext of local entities. |
45 | static GlobalDecl getGlobalDeclAsDeclContext(const DeclContext *DC) { |
46 | GlobalDecl GD; |
47 | if (auto *CD = dyn_cast<CXXConstructorDecl>(Val: DC)) |
48 | GD = GlobalDecl(CD, Ctor_Complete); |
49 | else if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: DC)) |
50 | GD = GlobalDecl(DD, Dtor_Complete); |
51 | else |
52 | GD = GlobalDecl(cast<FunctionDecl>(Val: DC)); |
53 | return GD; |
54 | } |
55 | |
56 | struct msvc_hashing_ostream : public llvm::raw_svector_ostream { |
57 | raw_ostream &OS; |
58 | llvm::SmallString<64> Buffer; |
59 | |
60 | msvc_hashing_ostream(raw_ostream &OS) |
61 | : llvm::raw_svector_ostream(Buffer), OS(OS) {} |
62 | ~msvc_hashing_ostream() override { |
63 | StringRef MangledName = str(); |
64 | bool StartsWithEscape = MangledName.starts_with(Prefix: "\01" ); |
65 | if (StartsWithEscape) |
66 | MangledName = MangledName.drop_front(N: 1); |
67 | if (MangledName.size() < 4096) { |
68 | OS << str(); |
69 | return; |
70 | } |
71 | |
72 | llvm::MD5 Hasher; |
73 | llvm::MD5::MD5Result Hash; |
74 | Hasher.update(Str: MangledName); |
75 | Hasher.final(Result&: Hash); |
76 | |
77 | SmallString<32> HexString; |
78 | llvm::MD5::stringifyResult(Result&: Hash, Str&: HexString); |
79 | |
80 | if (StartsWithEscape) |
81 | OS << '\01'; |
82 | OS << "??@" << HexString << '@'; |
83 | } |
84 | }; |
85 | |
86 | static const DeclContext * |
87 | getLambdaDefaultArgumentDeclContext(const Decl *D) { |
88 | if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) |
89 | if (RD->isLambda()) |
90 | if (const auto *Parm = |
91 | dyn_cast_or_null<ParmVarDecl>(Val: RD->getLambdaContextDecl())) |
92 | return Parm->getDeclContext(); |
93 | return nullptr; |
94 | } |
95 | |
96 | /// Retrieve the declaration context that should be used when mangling |
97 | /// the given declaration. |
98 | static const DeclContext *getEffectiveDeclContext(const Decl *D) { |
99 | // The ABI assumes that lambda closure types that occur within |
100 | // default arguments live in the context of the function. However, due to |
101 | // the way in which Clang parses and creates function declarations, this is |
102 | // not the case: the lambda closure type ends up living in the context |
103 | // where the function itself resides, because the function declaration itself |
104 | // had not yet been created. Fix the context here. |
105 | if (const auto *LDADC = getLambdaDefaultArgumentDeclContext(D)) |
106 | return LDADC; |
107 | |
108 | // Perform the same check for block literals. |
109 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(Val: D)) { |
110 | if (ParmVarDecl *ContextParam = |
111 | dyn_cast_or_null<ParmVarDecl>(Val: BD->getBlockManglingContextDecl())) |
112 | return ContextParam->getDeclContext(); |
113 | } |
114 | |
115 | const DeclContext *DC = D->getDeclContext(); |
116 | if (isa<CapturedDecl>(Val: DC) || isa<OMPDeclareReductionDecl>(Val: DC) || |
117 | isa<OMPDeclareMapperDecl>(Val: DC)) { |
118 | return getEffectiveDeclContext(D: cast<Decl>(Val: DC)); |
119 | } |
120 | |
121 | return DC->getRedeclContext(); |
122 | } |
123 | |
124 | static const DeclContext *getEffectiveParentContext(const DeclContext *DC) { |
125 | return getEffectiveDeclContext(D: cast<Decl>(Val: DC)); |
126 | } |
127 | |
128 | static const FunctionDecl *getStructor(const NamedDecl *ND) { |
129 | if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ND)) |
130 | return FTD->getTemplatedDecl()->getCanonicalDecl(); |
131 | |
132 | const auto *FD = cast<FunctionDecl>(Val: ND); |
133 | if (const auto *FTD = FD->getPrimaryTemplate()) |
134 | return FTD->getTemplatedDecl()->getCanonicalDecl(); |
135 | |
136 | return FD->getCanonicalDecl(); |
137 | } |
138 | |
139 | /// MicrosoftMangleContextImpl - Overrides the default MangleContext for the |
140 | /// Microsoft Visual C++ ABI. |
141 | class MicrosoftMangleContextImpl : public MicrosoftMangleContext { |
142 | typedef std::pair<const DeclContext *, IdentifierInfo *> DiscriminatorKeyTy; |
143 | llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator; |
144 | llvm::DenseMap<const NamedDecl *, unsigned> Uniquifier; |
145 | llvm::DenseMap<const CXXRecordDecl *, unsigned> LambdaIds; |
146 | llvm::DenseMap<GlobalDecl, unsigned> SEHFilterIds; |
147 | llvm::DenseMap<GlobalDecl, unsigned> SEHFinallyIds; |
148 | SmallString<16> AnonymousNamespaceHash; |
149 | |
150 | public: |
151 | MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags, |
152 | bool IsAux = false); |
153 | bool shouldMangleCXXName(const NamedDecl *D) override; |
154 | bool shouldMangleStringLiteral(const StringLiteral *SL) override; |
155 | void mangleCXXName(GlobalDecl GD, raw_ostream &Out) override; |
156 | void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, |
157 | const MethodVFTableLocation &ML, |
158 | raw_ostream &Out) override; |
159 | void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, |
160 | bool ElideOverrideInfo, raw_ostream &) override; |
161 | void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
162 | const ThunkInfo &Thunk, bool ElideOverrideInfo, |
163 | raw_ostream &) override; |
164 | void mangleCXXVFTable(const CXXRecordDecl *Derived, |
165 | ArrayRef<const CXXRecordDecl *> BasePath, |
166 | raw_ostream &Out) override; |
167 | void mangleCXXVBTable(const CXXRecordDecl *Derived, |
168 | ArrayRef<const CXXRecordDecl *> BasePath, |
169 | raw_ostream &Out) override; |
170 | |
171 | void mangleCXXVTable(const CXXRecordDecl *, raw_ostream &) override; |
172 | void mangleCXXVirtualDisplacementMap(const CXXRecordDecl *SrcRD, |
173 | const CXXRecordDecl *DstRD, |
174 | raw_ostream &Out) override; |
175 | void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile, |
176 | bool IsUnaligned, uint32_t NumEntries, |
177 | raw_ostream &Out) override; |
178 | void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries, |
179 | raw_ostream &Out) override; |
180 | void mangleCXXCatchableType(QualType T, const CXXConstructorDecl *CD, |
181 | CXXCtorType CT, uint32_t Size, uint32_t NVOffset, |
182 | int32_t VBPtrOffset, uint32_t VBIndex, |
183 | raw_ostream &Out) override; |
184 | void mangleCXXRTTI(QualType T, raw_ostream &Out) override; |
185 | void mangleCXXRTTIName(QualType T, raw_ostream &Out, |
186 | bool NormalizeIntegers) override; |
187 | void mangleCXXRTTIBaseClassDescriptor(const CXXRecordDecl *Derived, |
188 | uint32_t NVOffset, int32_t VBPtrOffset, |
189 | uint32_t VBTableOffset, uint32_t Flags, |
190 | raw_ostream &Out) override; |
191 | void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived, |
192 | raw_ostream &Out) override; |
193 | void mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived, |
194 | raw_ostream &Out) override; |
195 | void |
196 | mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived, |
197 | ArrayRef<const CXXRecordDecl *> BasePath, |
198 | raw_ostream &Out) override; |
199 | void mangleCanonicalTypeName(QualType T, raw_ostream &, |
200 | bool NormalizeIntegers) override; |
201 | void mangleReferenceTemporary(const VarDecl *, unsigned ManglingNumber, |
202 | raw_ostream &) override; |
203 | void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out) override; |
204 | void mangleThreadSafeStaticGuardVariable(const VarDecl *D, unsigned GuardNum, |
205 | raw_ostream &Out) override; |
206 | void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override; |
207 | void mangleDynamicAtExitDestructor(const VarDecl *D, |
208 | raw_ostream &Out) override; |
209 | void mangleSEHFilterExpression(GlobalDecl EnclosingDecl, |
210 | raw_ostream &Out) override; |
211 | void mangleSEHFinallyBlock(GlobalDecl EnclosingDecl, |
212 | raw_ostream &Out) override; |
213 | void mangleStringLiteral(const StringLiteral *SL, raw_ostream &Out) override; |
214 | bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { |
215 | const DeclContext *DC = getEffectiveDeclContext(D: ND); |
216 | if (!DC->isFunctionOrMethod()) |
217 | return false; |
218 | |
219 | // Lambda closure types are already numbered, give out a phony number so |
220 | // that they demangle nicely. |
221 | if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: ND)) { |
222 | if (RD->isLambda()) { |
223 | disc = 1; |
224 | return true; |
225 | } |
226 | } |
227 | |
228 | // Use the canonical number for externally visible decls. |
229 | if (ND->isExternallyVisible()) { |
230 | disc = getASTContext().getManglingNumber(ND, ForAuxTarget: isAux()); |
231 | return true; |
232 | } |
233 | |
234 | // Anonymous tags are already numbered. |
235 | if (const TagDecl *Tag = dyn_cast<TagDecl>(Val: ND)) { |
236 | if (!Tag->hasNameForLinkage() && |
237 | !getASTContext().getDeclaratorForUnnamedTagDecl(TD: Tag) && |
238 | !getASTContext().getTypedefNameForUnnamedTagDecl(TD: Tag)) |
239 | return false; |
240 | } |
241 | |
242 | // Make up a reasonable number for internal decls. |
243 | unsigned &discriminator = Uniquifier[ND]; |
244 | if (!discriminator) |
245 | discriminator = ++Discriminator[std::make_pair(x&: DC, y: ND->getIdentifier())]; |
246 | disc = discriminator + 1; |
247 | return true; |
248 | } |
249 | |
250 | std::string getLambdaString(const CXXRecordDecl *Lambda) override { |
251 | assert(Lambda->isLambda() && "RD must be a lambda!" ); |
252 | std::string Name("<lambda_" ); |
253 | |
254 | Decl *LambdaContextDecl = Lambda->getLambdaContextDecl(); |
255 | unsigned LambdaManglingNumber = Lambda->getLambdaManglingNumber(); |
256 | unsigned LambdaId; |
257 | const ParmVarDecl *Parm = dyn_cast_or_null<ParmVarDecl>(Val: LambdaContextDecl); |
258 | const FunctionDecl *Func = |
259 | Parm ? dyn_cast<FunctionDecl>(Val: Parm->getDeclContext()) : nullptr; |
260 | |
261 | if (Func) { |
262 | unsigned DefaultArgNo = |
263 | Func->getNumParams() - Parm->getFunctionScopeIndex(); |
264 | Name += llvm::utostr(X: DefaultArgNo); |
265 | Name += "_" ; |
266 | } |
267 | |
268 | if (LambdaManglingNumber) |
269 | LambdaId = LambdaManglingNumber; |
270 | else |
271 | LambdaId = getLambdaIdForDebugInfo(RD: Lambda); |
272 | |
273 | Name += llvm::utostr(X: LambdaId); |
274 | Name += ">" ; |
275 | return Name; |
276 | } |
277 | |
278 | unsigned getLambdaId(const CXXRecordDecl *RD) { |
279 | assert(RD->isLambda() && "RD must be a lambda!" ); |
280 | assert(!RD->isExternallyVisible() && "RD must not be visible!" ); |
281 | assert(RD->getLambdaManglingNumber() == 0 && |
282 | "RD must not have a mangling number!" ); |
283 | std::pair<llvm::DenseMap<const CXXRecordDecl *, unsigned>::iterator, bool> |
284 | Result = LambdaIds.insert(KV: std::make_pair(x&: RD, y: LambdaIds.size())); |
285 | return Result.first->second; |
286 | } |
287 | |
288 | unsigned getLambdaIdForDebugInfo(const CXXRecordDecl *RD) { |
289 | assert(RD->isLambda() && "RD must be a lambda!" ); |
290 | assert(!RD->isExternallyVisible() && "RD must not be visible!" ); |
291 | assert(RD->getLambdaManglingNumber() == 0 && |
292 | "RD must not have a mangling number!" ); |
293 | // The lambda should exist, but return 0 in case it doesn't. |
294 | return LambdaIds.lookup(Val: RD); |
295 | } |
296 | |
297 | /// Return a character sequence that is (somewhat) unique to the TU suitable |
298 | /// for mangling anonymous namespaces. |
299 | StringRef getAnonymousNamespaceHash() const { |
300 | return AnonymousNamespaceHash; |
301 | } |
302 | |
303 | private: |
304 | void mangleInitFiniStub(const VarDecl *D, char CharCode, raw_ostream &Out); |
305 | }; |
306 | |
307 | /// MicrosoftCXXNameMangler - Manage the mangling of a single name for the |
308 | /// Microsoft Visual C++ ABI. |
309 | class MicrosoftCXXNameMangler { |
310 | MicrosoftMangleContextImpl &Context; |
311 | raw_ostream &Out; |
312 | |
313 | /// The "structor" is the top-level declaration being mangled, if |
314 | /// that's not a template specialization; otherwise it's the pattern |
315 | /// for that specialization. |
316 | const NamedDecl *Structor; |
317 | unsigned StructorType; |
318 | |
319 | typedef llvm::SmallVector<std::string, 10> BackRefVec; |
320 | BackRefVec NameBackReferences; |
321 | |
322 | typedef llvm::DenseMap<const void *, unsigned> ArgBackRefMap; |
323 | ArgBackRefMap FunArgBackReferences; |
324 | ArgBackRefMap TemplateArgBackReferences; |
325 | |
326 | typedef llvm::DenseMap<const void *, StringRef> TemplateArgStringMap; |
327 | TemplateArgStringMap TemplateArgStrings; |
328 | llvm::BumpPtrAllocator TemplateArgStringStorageAlloc; |
329 | llvm::StringSaver TemplateArgStringStorage; |
330 | |
331 | typedef std::set<std::pair<int, bool>> PassObjectSizeArgsSet; |
332 | PassObjectSizeArgsSet PassObjectSizeArgs; |
333 | |
334 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
335 | |
336 | const bool PointersAre64Bit; |
337 | |
338 | DiagnosticBuilder Error(SourceLocation, StringRef, StringRef); |
339 | DiagnosticBuilder Error(SourceLocation, StringRef); |
340 | DiagnosticBuilder Error(StringRef); |
341 | |
342 | public: |
343 | enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result }; |
344 | enum class TplArgKind { ClassNTTP, StructuralValue }; |
345 | |
346 | MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_) |
347 | : Context(C), Out(Out_), Structor(nullptr), StructorType(-1), |
348 | TemplateArgStringStorage(TemplateArgStringStorageAlloc), |
349 | PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth( |
350 | AddrSpace: LangAS::Default) == 64) {} |
351 | |
352 | MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_, |
353 | const CXXConstructorDecl *D, CXXCtorType Type) |
354 | : Context(C), Out(Out_), Structor(getStructor(ND: D)), StructorType(Type), |
355 | TemplateArgStringStorage(TemplateArgStringStorageAlloc), |
356 | PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth( |
357 | AddrSpace: LangAS::Default) == 64) {} |
358 | |
359 | MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_, |
360 | const CXXDestructorDecl *D, CXXDtorType Type) |
361 | : Context(C), Out(Out_), Structor(getStructor(ND: D)), StructorType(Type), |
362 | TemplateArgStringStorage(TemplateArgStringStorageAlloc), |
363 | PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth( |
364 | AddrSpace: LangAS::Default) == 64) {} |
365 | |
366 | raw_ostream &getStream() const { return Out; } |
367 | |
368 | void mangle(GlobalDecl GD, StringRef Prefix = "?" ); |
369 | void mangleName(GlobalDecl GD); |
370 | void mangleFunctionEncoding(GlobalDecl GD, bool ShouldMangle); |
371 | void mangleVariableEncoding(const VarDecl *VD); |
372 | void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD, |
373 | const NonTypeTemplateParmDecl *PD, |
374 | QualType TemplateArgType, |
375 | StringRef Prefix = "$" ); |
376 | void mangleMemberDataPointerInClassNTTP(const CXXRecordDecl *, |
377 | const ValueDecl *); |
378 | void mangleMemberFunctionPointer(const CXXRecordDecl *RD, |
379 | const CXXMethodDecl *MD, |
380 | const NonTypeTemplateParmDecl *PD, |
381 | QualType TemplateArgType, |
382 | StringRef Prefix = "$" ); |
383 | void mangleFunctionPointer(const FunctionDecl *FD, |
384 | const NonTypeTemplateParmDecl *PD, |
385 | QualType TemplateArgType); |
386 | void mangleVarDecl(const VarDecl *VD, const NonTypeTemplateParmDecl *PD, |
387 | QualType TemplateArgType); |
388 | void mangleMemberFunctionPointerInClassNTTP(const CXXRecordDecl *RD, |
389 | const CXXMethodDecl *MD); |
390 | void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, |
391 | const MethodVFTableLocation &ML); |
392 | void mangleNumber(int64_t Number); |
393 | void mangleNumber(llvm::APSInt Number); |
394 | void mangleFloat(llvm::APFloat Number); |
395 | void mangleBits(llvm::APInt Number); |
396 | void mangleTagTypeKind(TagTypeKind TK); |
397 | void mangleArtificialTagType(TagTypeKind TK, StringRef UnqualifiedName, |
398 | ArrayRef<StringRef> NestedNames = {}); |
399 | void mangleAddressSpaceType(QualType T, Qualifiers Quals, SourceRange Range); |
400 | void mangleType(QualType T, SourceRange Range, |
401 | QualifierMangleMode QMM = QMM_Mangle); |
402 | void mangleFunctionType(const FunctionType *T, |
403 | const FunctionDecl *D = nullptr, |
404 | bool ForceThisQuals = false, |
405 | bool MangleExceptionSpec = true); |
406 | void mangleSourceName(StringRef Name); |
407 | void mangleNestedName(GlobalDecl GD); |
408 | |
409 | void mangleAutoReturnType(QualType T, QualifierMangleMode QMM); |
410 | |
411 | private: |
412 | bool isStructorDecl(const NamedDecl *ND) const { |
413 | return ND == Structor || getStructor(ND) == Structor; |
414 | } |
415 | |
416 | bool is64BitPointer(Qualifiers Quals) const { |
417 | LangAS AddrSpace = Quals.getAddressSpace(); |
418 | return AddrSpace == LangAS::ptr64 || |
419 | (PointersAre64Bit && !(AddrSpace == LangAS::ptr32_sptr || |
420 | AddrSpace == LangAS::ptr32_uptr)); |
421 | } |
422 | |
423 | void mangleUnqualifiedName(GlobalDecl GD) { |
424 | mangleUnqualifiedName(GD, Name: cast<NamedDecl>(Val: GD.getDecl())->getDeclName()); |
425 | } |
426 | void mangleUnqualifiedName(GlobalDecl GD, DeclarationName Name); |
427 | void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc); |
428 | void mangleCXXDtorType(CXXDtorType T); |
429 | void mangleQualifiers(Qualifiers Quals, bool IsMember); |
430 | void mangleRefQualifier(RefQualifierKind RefQualifier); |
431 | void manglePointerCVQualifiers(Qualifiers Quals); |
432 | void manglePointerExtQualifiers(Qualifiers Quals, QualType PointeeType); |
433 | void manglePointerAuthQualifier(Qualifiers Quals); |
434 | |
435 | void mangleUnscopedTemplateName(GlobalDecl GD); |
436 | void |
437 | mangleTemplateInstantiationName(GlobalDecl GD, |
438 | const TemplateArgumentList &TemplateArgs); |
439 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
440 | |
441 | void mangleFunctionArgumentType(QualType T, SourceRange Range); |
442 | void manglePassObjectSizeArg(const PassObjectSizeAttr *POSA); |
443 | |
444 | bool isArtificialTagType(QualType T) const; |
445 | |
446 | // Declare manglers for every type class. |
447 | #define ABSTRACT_TYPE(CLASS, PARENT) |
448 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
449 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \ |
450 | Qualifiers Quals, \ |
451 | SourceRange Range); |
452 | #include "clang/AST/TypeNodes.inc" |
453 | #undef ABSTRACT_TYPE |
454 | #undef NON_CANONICAL_TYPE |
455 | #undef TYPE |
456 | |
457 | void mangleType(const TagDecl *TD); |
458 | void mangleDecayedArrayType(const ArrayType *T); |
459 | void mangleArrayType(const ArrayType *T); |
460 | void mangleFunctionClass(const FunctionDecl *FD); |
461 | void mangleCallingConvention(CallingConv CC, SourceRange Range); |
462 | void mangleCallingConvention(const FunctionType *T, SourceRange Range); |
463 | void mangleIntegerLiteral(const llvm::APSInt &Number, |
464 | const NonTypeTemplateParmDecl *PD = nullptr, |
465 | QualType TemplateArgType = QualType()); |
466 | void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD); |
467 | void mangleThrowSpecification(const FunctionProtoType *T); |
468 | |
469 | void mangleTemplateArgs(const TemplateDecl *TD, |
470 | const TemplateArgumentList &TemplateArgs); |
471 | void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA, |
472 | const NamedDecl *Parm); |
473 | void mangleTemplateArgValue(QualType T, const APValue &V, TplArgKind, |
474 | bool WithScalarType = false); |
475 | |
476 | void mangleObjCProtocol(const ObjCProtocolDecl *PD); |
477 | void mangleObjCLifetime(const QualType T, Qualifiers Quals, |
478 | SourceRange Range); |
479 | void mangleObjCKindOfType(const ObjCObjectType *T, Qualifiers Quals, |
480 | SourceRange Range); |
481 | |
482 | void mangleAutoReturnType(const MemberPointerType *T, Qualifiers Quals); |
483 | void mangleAutoReturnType(const PointerType *T, Qualifiers Quals); |
484 | void mangleAutoReturnType(const LValueReferenceType *T, Qualifiers Quals); |
485 | void mangleAutoReturnType(const RValueReferenceType *T, Qualifiers Quals); |
486 | }; |
487 | } |
488 | |
489 | MicrosoftMangleContextImpl::MicrosoftMangleContextImpl(ASTContext &Context, |
490 | DiagnosticsEngine &Diags, |
491 | bool IsAux) |
492 | : MicrosoftMangleContext(Context, Diags, IsAux) { |
493 | // To mangle anonymous namespaces, hash the path to the main source file. The |
494 | // path should be whatever (probably relative) path was passed on the command |
495 | // line. The goal is for the compiler to produce the same output regardless of |
496 | // working directory, so use the uncanonicalized relative path. |
497 | // |
498 | // It's important to make the mangled names unique because, when CodeView |
499 | // debug info is in use, the debugger uses mangled type names to distinguish |
500 | // between otherwise identically named types in anonymous namespaces. |
501 | // |
502 | // These symbols are always internal, so there is no need for the hash to |
503 | // match what MSVC produces. For the same reason, clang is free to change the |
504 | // hash at any time without breaking compatibility with old versions of clang. |
505 | // The generated names are intended to look similar to what MSVC generates, |
506 | // which are something like "?A0x01234567@". |
507 | SourceManager &SM = Context.getSourceManager(); |
508 | if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID: SM.getMainFileID())) { |
509 | // Truncate the hash so we get 8 characters of hexadecimal. |
510 | uint32_t TruncatedHash = uint32_t(xxh3_64bits(data: FE->getName())); |
511 | AnonymousNamespaceHash = llvm::utohexstr(X: TruncatedHash); |
512 | } else { |
513 | // If we don't have a path to the main file, we'll just use 0. |
514 | AnonymousNamespaceHash = "0" ; |
515 | } |
516 | } |
517 | |
518 | bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) { |
519 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: D)) { |
520 | LanguageLinkage L = FD->getLanguageLinkage(); |
521 | // Overloadable functions need mangling. |
522 | if (FD->hasAttr<OverloadableAttr>()) |
523 | return true; |
524 | |
525 | // The ABI expects that we would never mangle "typical" user-defined entry |
526 | // points regardless of visibility or freestanding-ness. |
527 | // |
528 | // N.B. This is distinct from asking about "main". "main" has a lot of |
529 | // special rules associated with it in the standard while these |
530 | // user-defined entry points are outside of the purview of the standard. |
531 | // For example, there can be only one definition for "main" in a standards |
532 | // compliant program; however nothing forbids the existence of wmain and |
533 | // WinMain in the same translation unit. |
534 | if (FD->isMSVCRTEntryPoint()) |
535 | return false; |
536 | |
537 | // C++ functions and those whose names are not a simple identifier need |
538 | // mangling. |
539 | if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage) |
540 | return true; |
541 | |
542 | // C functions are not mangled. |
543 | if (L == CLanguageLinkage) |
544 | return false; |
545 | } |
546 | |
547 | // Otherwise, no mangling is done outside C++ mode. |
548 | if (!getASTContext().getLangOpts().CPlusPlus) |
549 | return false; |
550 | |
551 | const VarDecl *VD = dyn_cast<VarDecl>(Val: D); |
552 | if (VD && !isa<DecompositionDecl>(Val: D)) { |
553 | // C variables are not mangled. |
554 | if (VD->isExternC()) |
555 | return false; |
556 | |
557 | // Variables at global scope with internal linkage are not mangled. |
558 | const DeclContext *DC = getEffectiveDeclContext(D); |
559 | // Check for extern variable declared locally. |
560 | if (DC->isFunctionOrMethod() && D->hasLinkage()) |
561 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
562 | DC = getEffectiveParentContext(DC); |
563 | |
564 | if (DC->isTranslationUnit() && D->getFormalLinkage() == Linkage::Internal && |
565 | !isa<VarTemplateSpecializationDecl>(Val: D) && D->getIdentifier() != nullptr) |
566 | return false; |
567 | } |
568 | |
569 | return true; |
570 | } |
571 | |
572 | bool |
573 | MicrosoftMangleContextImpl::shouldMangleStringLiteral(const StringLiteral *SL) { |
574 | return true; |
575 | } |
576 | |
577 | DiagnosticBuilder MicrosoftCXXNameMangler::Error(SourceLocation loc, |
578 | StringRef thing1, |
579 | StringRef thing2) { |
580 | DiagnosticsEngine &Diags = Context.getDiags(); |
581 | unsigned DiagID = Diags.getCustomDiagID(L: DiagnosticsEngine::Error, |
582 | FormatString: "cannot mangle this %0 %1 yet" ); |
583 | return Diags.Report(Loc: loc, DiagID) << thing1 << thing2; |
584 | } |
585 | |
586 | DiagnosticBuilder MicrosoftCXXNameMangler::Error(SourceLocation loc, |
587 | StringRef thingy) { |
588 | DiagnosticsEngine &Diags = Context.getDiags(); |
589 | unsigned DiagID = Diags.getCustomDiagID(L: DiagnosticsEngine::Error, |
590 | FormatString: "cannot mangle this %0 yet" ); |
591 | return Diags.Report(Loc: loc, DiagID) << thingy; |
592 | } |
593 | |
594 | DiagnosticBuilder MicrosoftCXXNameMangler::Error(StringRef thingy) { |
595 | DiagnosticsEngine &Diags = Context.getDiags(); |
596 | // extra placeholders are ignored quietly when not used |
597 | unsigned DiagID = Diags.getCustomDiagID(L: DiagnosticsEngine::Error, |
598 | FormatString: "cannot mangle this %0 yet" ); |
599 | return Diags.Report(DiagID) << thingy; |
600 | } |
601 | |
602 | void MicrosoftCXXNameMangler::mangle(GlobalDecl GD, StringRef Prefix) { |
603 | const NamedDecl *D = cast<NamedDecl>(Val: GD.getDecl()); |
604 | // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. |
605 | // Therefore it's really important that we don't decorate the |
606 | // name with leading underscores or leading/trailing at signs. So, by |
607 | // default, we emit an asm marker at the start so we get the name right. |
608 | // Callers can override this with a custom prefix. |
609 | |
610 | // <mangled-name> ::= ? <name> <type-encoding> |
611 | Out << Prefix; |
612 | mangleName(GD); |
613 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: D)) |
614 | mangleFunctionEncoding(GD, ShouldMangle: Context.shouldMangleDeclName(D: FD)); |
615 | else if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
616 | mangleVariableEncoding(VD); |
617 | else if (isa<MSGuidDecl>(Val: D)) |
618 | // MSVC appears to mangle GUIDs as if they were variables of type |
619 | // 'const struct __s_GUID'. |
620 | Out << "3U__s_GUID@@B" ; |
621 | else if (isa<TemplateParamObjectDecl>(Val: D)) { |
622 | // Template parameter objects don't get a <type-encoding>; their type is |
623 | // specified as part of their value. |
624 | } else |
625 | llvm_unreachable("Tried to mangle unexpected NamedDecl!" ); |
626 | } |
627 | |
628 | void MicrosoftCXXNameMangler::mangleFunctionEncoding(GlobalDecl GD, |
629 | bool ShouldMangle) { |
630 | const FunctionDecl *FD = cast<FunctionDecl>(Val: GD.getDecl()); |
631 | // <type-encoding> ::= <function-class> <function-type> |
632 | |
633 | // Since MSVC operates on the type as written and not the canonical type, it |
634 | // actually matters which decl we have here. MSVC appears to choose the |
635 | // first, since it is most likely to be the declaration in a header file. |
636 | FD = FD->getFirstDecl(); |
637 | |
638 | // We should never ever see a FunctionNoProtoType at this point. |
639 | // We don't even know how to mangle their types anyway :). |
640 | const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>(); |
641 | |
642 | // extern "C" functions can hold entities that must be mangled. |
643 | // As it stands, these functions still need to get expressed in the full |
644 | // external name. They have their class and type omitted, replaced with '9'. |
645 | if (ShouldMangle) { |
646 | // We would like to mangle all extern "C" functions using this additional |
647 | // component but this would break compatibility with MSVC's behavior. |
648 | // Instead, do this when we know that compatibility isn't important (in |
649 | // other words, when it is an overloaded extern "C" function). |
650 | if (FD->isExternC() && FD->hasAttr<OverloadableAttr>()) |
651 | Out << "$$J0" ; |
652 | |
653 | mangleFunctionClass(FD); |
654 | |
655 | mangleFunctionType(T: FT, D: FD, ForceThisQuals: false, MangleExceptionSpec: false); |
656 | } else { |
657 | Out << '9'; |
658 | } |
659 | } |
660 | |
661 | void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { |
662 | // <type-encoding> ::= <storage-class> <variable-type> |
663 | // <storage-class> ::= 0 # private static member |
664 | // ::= 1 # protected static member |
665 | // ::= 2 # public static member |
666 | // ::= 3 # global |
667 | // ::= 4 # static local |
668 | |
669 | // The first character in the encoding (after the name) is the storage class. |
670 | if (VD->isStaticDataMember()) { |
671 | // If it's a static member, it also encodes the access level. |
672 | switch (VD->getAccess()) { |
673 | default: |
674 | case AS_private: Out << '0'; break; |
675 | case AS_protected: Out << '1'; break; |
676 | case AS_public: Out << '2'; break; |
677 | } |
678 | } |
679 | else if (!VD->isStaticLocal()) |
680 | Out << '3'; |
681 | else |
682 | Out << '4'; |
683 | // Now mangle the type. |
684 | // <variable-type> ::= <type> <cvr-qualifiers> |
685 | // ::= <type> <pointee-cvr-qualifiers> # pointers, references |
686 | // Pointers and references are odd. The type of 'int * const foo;' gets |
687 | // mangled as 'QAHA' instead of 'PAHB', for example. |
688 | SourceRange SR = VD->getSourceRange(); |
689 | QualType Ty = VD->getType(); |
690 | if (Ty->isPointerType() || Ty->isReferenceType() || |
691 | Ty->isMemberPointerType()) { |
692 | mangleType(T: Ty, Range: SR, QMM: QMM_Drop); |
693 | manglePointerExtQualifiers( |
694 | Quals: Ty.getDesugaredType(Context: getASTContext()).getLocalQualifiers(), PointeeType: QualType()); |
695 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) { |
696 | mangleQualifiers(Quals: MPT->getPointeeType().getQualifiers(), IsMember: true); |
697 | // Member pointers are suffixed with a back reference to the member |
698 | // pointer's class name. |
699 | mangleName(GD: MPT->getMostRecentCXXRecordDecl()); |
700 | } else |
701 | mangleQualifiers(Quals: Ty->getPointeeType().getQualifiers(), IsMember: false); |
702 | } else if (const ArrayType *AT = getASTContext().getAsArrayType(T: Ty)) { |
703 | // Global arrays are funny, too. |
704 | mangleDecayedArrayType(T: AT); |
705 | if (AT->getElementType()->isArrayType()) |
706 | Out << 'A'; |
707 | else |
708 | mangleQualifiers(Quals: Ty.getQualifiers(), IsMember: false); |
709 | } else { |
710 | mangleType(T: Ty, Range: SR, QMM: QMM_Drop); |
711 | mangleQualifiers(Quals: Ty.getQualifiers(), IsMember: false); |
712 | } |
713 | } |
714 | |
715 | void MicrosoftCXXNameMangler::mangleMemberDataPointer( |
716 | const CXXRecordDecl *RD, const ValueDecl *VD, |
717 | const NonTypeTemplateParmDecl *PD, QualType TemplateArgType, |
718 | StringRef Prefix) { |
719 | // <member-data-pointer> ::= <integer-literal> |
720 | // ::= $F <number> <number> |
721 | // ::= $G <number> <number> <number> |
722 | // |
723 | // <auto-nttp> ::= $ M <type> <integer-literal> |
724 | // <auto-nttp> ::= $ M <type> F <name> <number> |
725 | // <auto-nttp> ::= $ M <type> G <name> <number> <number> |
726 | |
727 | int64_t FieldOffset; |
728 | int64_t VBTableOffset; |
729 | MSInheritanceModel IM = RD->getMSInheritanceModel(); |
730 | if (VD) { |
731 | FieldOffset = getASTContext().getFieldOffset(FD: VD); |
732 | assert(FieldOffset % getASTContext().getCharWidth() == 0 && |
733 | "cannot take address of bitfield" ); |
734 | FieldOffset /= getASTContext().getCharWidth(); |
735 | |
736 | VBTableOffset = 0; |
737 | |
738 | if (IM == MSInheritanceModel::Virtual) |
739 | FieldOffset -= getASTContext().getOffsetOfBaseWithVBPtr(RD).getQuantity(); |
740 | } else { |
741 | FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1; |
742 | |
743 | VBTableOffset = -1; |
744 | } |
745 | |
746 | char Code = '\0'; |
747 | switch (IM) { |
748 | case MSInheritanceModel::Single: Code = '0'; break; |
749 | case MSInheritanceModel::Multiple: Code = '0'; break; |
750 | case MSInheritanceModel::Virtual: Code = 'F'; break; |
751 | case MSInheritanceModel::Unspecified: Code = 'G'; break; |
752 | } |
753 | |
754 | Out << Prefix; |
755 | |
756 | if (VD && |
757 | getASTContext().getLangOpts().isCompatibleWithMSVC( |
758 | MajorVersion: LangOptions::MSVC2019) && |
759 | PD && PD->getType()->getTypeClass() == Type::Auto && |
760 | !TemplateArgType.isNull()) { |
761 | Out << "M" ; |
762 | mangleType(T: TemplateArgType, Range: SourceRange(), QMM: QMM_Drop); |
763 | } |
764 | |
765 | Out << Code; |
766 | |
767 | mangleNumber(Number: FieldOffset); |
768 | |
769 | // The C++ standard doesn't allow base-to-derived member pointer conversions |
770 | // in template parameter contexts, so the vbptr offset of data member pointers |
771 | // is always zero. |
772 | if (inheritanceModelHasVBPtrOffsetField(Inheritance: IM)) |
773 | mangleNumber(Number: 0); |
774 | if (inheritanceModelHasVBTableOffsetField(Inheritance: IM)) |
775 | mangleNumber(Number: VBTableOffset); |
776 | } |
777 | |
778 | void MicrosoftCXXNameMangler::mangleMemberDataPointerInClassNTTP( |
779 | const CXXRecordDecl *RD, const ValueDecl *VD) { |
780 | MSInheritanceModel IM = RD->getMSInheritanceModel(); |
781 | // <nttp-class-member-data-pointer> ::= <member-data-pointer> |
782 | // ::= N |
783 | // ::= 8 <postfix> @ <unqualified-name> @ |
784 | |
785 | if (IM != MSInheritanceModel::Single && IM != MSInheritanceModel::Multiple) |
786 | return mangleMemberDataPointer(RD, VD, PD: nullptr, TemplateArgType: QualType(), Prefix: "" ); |
787 | |
788 | if (!VD) { |
789 | Out << 'N'; |
790 | return; |
791 | } |
792 | |
793 | Out << '8'; |
794 | mangleNestedName(GD: VD); |
795 | Out << '@'; |
796 | mangleUnqualifiedName(GD: VD); |
797 | Out << '@'; |
798 | } |
799 | |
800 | void MicrosoftCXXNameMangler::mangleMemberFunctionPointer( |
801 | const CXXRecordDecl *RD, const CXXMethodDecl *MD, |
802 | const NonTypeTemplateParmDecl *PD, QualType TemplateArgType, |
803 | StringRef Prefix) { |
804 | // <member-function-pointer> ::= $1? <name> |
805 | // ::= $H? <name> <number> |
806 | // ::= $I? <name> <number> <number> |
807 | // ::= $J? <name> <number> <number> <number> |
808 | // |
809 | // <auto-nttp> ::= $ M <type> 1? <name> |
810 | // <auto-nttp> ::= $ M <type> H? <name> <number> |
811 | // <auto-nttp> ::= $ M <type> I? <name> <number> <number> |
812 | // <auto-nttp> ::= $ M <type> J? <name> <number> <number> <number> |
813 | |
814 | MSInheritanceModel IM = RD->getMSInheritanceModel(); |
815 | |
816 | char Code = '\0'; |
817 | switch (IM) { |
818 | case MSInheritanceModel::Single: Code = '1'; break; |
819 | case MSInheritanceModel::Multiple: Code = 'H'; break; |
820 | case MSInheritanceModel::Virtual: Code = 'I'; break; |
821 | case MSInheritanceModel::Unspecified: Code = 'J'; break; |
822 | } |
823 | |
824 | // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr |
825 | // thunk. |
826 | uint64_t NVOffset = 0; |
827 | uint64_t VBTableOffset = 0; |
828 | uint64_t VBPtrOffset = 0; |
829 | if (MD) { |
830 | Out << Prefix; |
831 | |
832 | if (getASTContext().getLangOpts().isCompatibleWithMSVC( |
833 | MajorVersion: LangOptions::MSVC2019) && |
834 | PD && PD->getType()->getTypeClass() == Type::Auto && |
835 | !TemplateArgType.isNull()) { |
836 | Out << "M" ; |
837 | mangleType(T: TemplateArgType, Range: SourceRange(), QMM: QMM_Drop); |
838 | } |
839 | |
840 | Out << Code << '?'; |
841 | if (MD->isVirtual()) { |
842 | MicrosoftVTableContext *VTContext = |
843 | cast<MicrosoftVTableContext>(Val: getASTContext().getVTableContext()); |
844 | MethodVFTableLocation ML = |
845 | VTContext->getMethodVFTableLocation(GD: GlobalDecl(MD)); |
846 | mangleVirtualMemPtrThunk(MD, ML); |
847 | NVOffset = ML.VFPtrOffset.getQuantity(); |
848 | VBTableOffset = ML.VBTableIndex * 4; |
849 | if (ML.VBase) { |
850 | const ASTRecordLayout &Layout = getASTContext().getASTRecordLayout(D: RD); |
851 | VBPtrOffset = Layout.getVBPtrOffset().getQuantity(); |
852 | } |
853 | } else { |
854 | mangleName(GD: MD); |
855 | mangleFunctionEncoding(GD: MD, /*ShouldMangle=*/true); |
856 | } |
857 | |
858 | if (VBTableOffset == 0 && IM == MSInheritanceModel::Virtual) |
859 | NVOffset -= getASTContext().getOffsetOfBaseWithVBPtr(RD).getQuantity(); |
860 | } else { |
861 | // Null single inheritance member functions are encoded as a simple nullptr. |
862 | if (IM == MSInheritanceModel::Single) { |
863 | Out << Prefix << "0A@" ; |
864 | return; |
865 | } |
866 | if (IM == MSInheritanceModel::Unspecified) |
867 | VBTableOffset = -1; |
868 | Out << Prefix << Code; |
869 | } |
870 | |
871 | if (inheritanceModelHasNVOffsetField(/*IsMemberFunction=*/true, Inheritance: IM)) |
872 | mangleNumber(Number: static_cast<uint32_t>(NVOffset)); |
873 | if (inheritanceModelHasVBPtrOffsetField(Inheritance: IM)) |
874 | mangleNumber(Number: VBPtrOffset); |
875 | if (inheritanceModelHasVBTableOffsetField(Inheritance: IM)) |
876 | mangleNumber(Number: VBTableOffset); |
877 | } |
878 | |
879 | void MicrosoftCXXNameMangler::mangleFunctionPointer( |
880 | const FunctionDecl *FD, const NonTypeTemplateParmDecl *PD, |
881 | QualType TemplateArgType) { |
882 | // <func-ptr> ::= $1? <mangled-name> |
883 | // <func-ptr> ::= <auto-nttp> |
884 | // |
885 | // <auto-nttp> ::= $ M <type> 1? <mangled-name> |
886 | Out << '$'; |
887 | |
888 | if (getASTContext().getLangOpts().isCompatibleWithMSVC( |
889 | MajorVersion: LangOptions::MSVC2019) && |
890 | PD && PD->getType()->getTypeClass() == Type::Auto && |
891 | !TemplateArgType.isNull()) { |
892 | Out << "M" ; |
893 | mangleType(T: TemplateArgType, Range: SourceRange(), QMM: QMM_Drop); |
894 | } |
895 | |
896 | Out << "1?" ; |
897 | mangleName(GD: FD); |
898 | mangleFunctionEncoding(GD: FD, /*ShouldMangle=*/true); |
899 | } |
900 | |
901 | void MicrosoftCXXNameMangler::mangleVarDecl(const VarDecl *VD, |
902 | const NonTypeTemplateParmDecl *PD, |
903 | QualType TemplateArgType) { |
904 | // <var-ptr> ::= $1? <mangled-name> |
905 | // <var-ptr> ::= <auto-nttp> |
906 | // |
907 | // <auto-nttp> ::= $ M <type> 1? <mangled-name> |
908 | Out << '$'; |
909 | |
910 | if (getASTContext().getLangOpts().isCompatibleWithMSVC( |
911 | MajorVersion: LangOptions::MSVC2019) && |
912 | PD && PD->getType()->getTypeClass() == Type::Auto && |
913 | !TemplateArgType.isNull()) { |
914 | Out << "M" ; |
915 | mangleType(T: TemplateArgType, Range: SourceRange(), QMM: QMM_Drop); |
916 | } |
917 | |
918 | Out << "1?" ; |
919 | mangleName(GD: VD); |
920 | mangleVariableEncoding(VD); |
921 | } |
922 | |
923 | void MicrosoftCXXNameMangler::mangleMemberFunctionPointerInClassNTTP( |
924 | const CXXRecordDecl *RD, const CXXMethodDecl *MD) { |
925 | // <nttp-class-member-function-pointer> ::= <member-function-pointer> |
926 | // ::= N |
927 | // ::= E? <virtual-mem-ptr-thunk> |
928 | // ::= E? <mangled-name> <type-encoding> |
929 | |
930 | if (!MD) { |
931 | if (RD->getMSInheritanceModel() != MSInheritanceModel::Single) |
932 | return mangleMemberFunctionPointer(RD, MD, PD: nullptr, TemplateArgType: QualType(), Prefix: "" ); |
933 | |
934 | Out << 'N'; |
935 | return; |
936 | } |
937 | |
938 | Out << "E?" ; |
939 | if (MD->isVirtual()) { |
940 | MicrosoftVTableContext *VTContext = |
941 | cast<MicrosoftVTableContext>(Val: getASTContext().getVTableContext()); |
942 | MethodVFTableLocation ML = |
943 | VTContext->getMethodVFTableLocation(GD: GlobalDecl(MD)); |
944 | mangleVirtualMemPtrThunk(MD, ML); |
945 | } else { |
946 | mangleName(GD: MD); |
947 | mangleFunctionEncoding(GD: MD, /*ShouldMangle=*/true); |
948 | } |
949 | } |
950 | |
951 | void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk( |
952 | const CXXMethodDecl *MD, const MethodVFTableLocation &ML) { |
953 | // Get the vftable offset. |
954 | CharUnits PointerWidth = getASTContext().toCharUnitsFromBits( |
955 | BitSize: getASTContext().getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default)); |
956 | uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity(); |
957 | |
958 | Out << "?_9" ; |
959 | mangleName(GD: MD->getParent()); |
960 | Out << "$B" ; |
961 | mangleNumber(Number: OffsetInVFTable); |
962 | Out << 'A'; |
963 | mangleCallingConvention(T: MD->getType()->castAs<FunctionProtoType>(), |
964 | Range: MD->getSourceRange()); |
965 | } |
966 | |
967 | void MicrosoftCXXNameMangler::mangleName(GlobalDecl GD) { |
968 | // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ |
969 | |
970 | // Always start with the unqualified name. |
971 | mangleUnqualifiedName(GD); |
972 | |
973 | mangleNestedName(GD); |
974 | |
975 | // Terminate the whole name with an '@'. |
976 | Out << '@'; |
977 | } |
978 | |
979 | void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { |
980 | mangleNumber(Number: llvm::APSInt(llvm::APInt(64, Number), /*IsUnsigned*/false)); |
981 | } |
982 | |
983 | void MicrosoftCXXNameMangler::mangleNumber(llvm::APSInt Number) { |
984 | // MSVC never mangles any integer wider than 64 bits. In general it appears |
985 | // to convert every integer to signed 64 bit before mangling (including |
986 | // unsigned 64 bit values). Do the same, but preserve bits beyond the bottom |
987 | // 64. |
988 | unsigned Width = std::max(a: Number.getBitWidth(), b: 64U); |
989 | llvm::APInt Value = Number.extend(width: Width); |
990 | |
991 | // <non-negative integer> ::= A@ # when Number == 0 |
992 | // ::= <decimal digit> # when 1 <= Number <= 10 |
993 | // ::= <hex digit>+ @ # when Number >= 10 |
994 | // |
995 | // <number> ::= [?] <non-negative integer> |
996 | |
997 | if (Value.isNegative()) { |
998 | Value = -Value; |
999 | Out << '?'; |
1000 | } |
1001 | mangleBits(Number: Value); |
1002 | } |
1003 | |
1004 | void MicrosoftCXXNameMangler::mangleFloat(llvm::APFloat Number) { |
1005 | using llvm::APFloat; |
1006 | |
1007 | switch (APFloat::SemanticsToEnum(Sem: Number.getSemantics())) { |
1008 | case APFloat::S_IEEEsingle: Out << 'A'; break; |
1009 | case APFloat::S_IEEEdouble: Out << 'B'; break; |
1010 | |
1011 | // The following are all Clang extensions. We try to pick manglings that are |
1012 | // unlikely to conflict with MSVC's scheme. |
1013 | case APFloat::S_IEEEhalf: Out << 'V'; break; |
1014 | case APFloat::S_BFloat: Out << 'W'; break; |
1015 | case APFloat::S_x87DoubleExtended: Out << 'X'; break; |
1016 | case APFloat::S_IEEEquad: Out << 'Y'; break; |
1017 | case APFloat::S_PPCDoubleDouble: Out << 'Z'; break; |
1018 | case APFloat::S_PPCDoubleDoubleLegacy: |
1019 | case APFloat::S_Float8E5M2: |
1020 | case APFloat::S_Float8E4M3: |
1021 | case APFloat::S_Float8E4M3FN: |
1022 | case APFloat::S_Float8E5M2FNUZ: |
1023 | case APFloat::S_Float8E4M3FNUZ: |
1024 | case APFloat::S_Float8E4M3B11FNUZ: |
1025 | case APFloat::S_Float8E3M4: |
1026 | case APFloat::S_FloatTF32: |
1027 | case APFloat::S_Float8E8M0FNU: |
1028 | case APFloat::S_Float6E3M2FN: |
1029 | case APFloat::S_Float6E2M3FN: |
1030 | case APFloat::S_Float4E2M1FN: |
1031 | llvm_unreachable("Tried to mangle unexpected APFloat semantics" ); |
1032 | } |
1033 | |
1034 | mangleBits(Number: Number.bitcastToAPInt()); |
1035 | } |
1036 | |
1037 | void MicrosoftCXXNameMangler::mangleBits(llvm::APInt Value) { |
1038 | if (Value == 0) |
1039 | Out << "A@" ; |
1040 | else if (Value.uge(RHS: 1) && Value.ule(RHS: 10)) |
1041 | Out << (Value - 1); |
1042 | else { |
1043 | // Numbers that are not encoded as decimal digits are represented as nibbles |
1044 | // in the range of ASCII characters 'A' to 'P'. |
1045 | // The number 0x123450 would be encoded as 'BCDEFA' |
1046 | llvm::SmallString<32> EncodedNumberBuffer; |
1047 | for (; Value != 0; Value.lshrInPlace(ShiftAmt: 4)) |
1048 | EncodedNumberBuffer.push_back(Elt: 'A' + (Value & 0xf).getZExtValue()); |
1049 | std::reverse(first: EncodedNumberBuffer.begin(), last: EncodedNumberBuffer.end()); |
1050 | Out.write(Ptr: EncodedNumberBuffer.data(), Size: EncodedNumberBuffer.size()); |
1051 | Out << '@'; |
1052 | } |
1053 | } |
1054 | |
1055 | static GlobalDecl isTemplate(GlobalDecl GD, |
1056 | const TemplateArgumentList *&TemplateArgs) { |
1057 | const NamedDecl *ND = cast<NamedDecl>(Val: GD.getDecl()); |
1058 | // Check if we have a function template. |
1059 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: ND)) { |
1060 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
1061 | TemplateArgs = FD->getTemplateSpecializationArgs(); |
1062 | return GD.getWithDecl(D: TD); |
1063 | } |
1064 | } |
1065 | |
1066 | // Check if we have a class template. |
1067 | if (const ClassTemplateSpecializationDecl *Spec = |
1068 | dyn_cast<ClassTemplateSpecializationDecl>(Val: ND)) { |
1069 | TemplateArgs = &Spec->getTemplateArgs(); |
1070 | return GD.getWithDecl(D: Spec->getSpecializedTemplate()); |
1071 | } |
1072 | |
1073 | // Check if we have a variable template. |
1074 | if (const VarTemplateSpecializationDecl *Spec = |
1075 | dyn_cast<VarTemplateSpecializationDecl>(Val: ND)) { |
1076 | TemplateArgs = &Spec->getTemplateArgs(); |
1077 | return GD.getWithDecl(D: Spec->getSpecializedTemplate()); |
1078 | } |
1079 | |
1080 | return GlobalDecl(); |
1081 | } |
1082 | |
1083 | void MicrosoftCXXNameMangler::mangleUnqualifiedName(GlobalDecl GD, |
1084 | DeclarationName Name) { |
1085 | const NamedDecl *ND = cast<NamedDecl>(Val: GD.getDecl()); |
1086 | // <unqualified-name> ::= <operator-name> |
1087 | // ::= <ctor-dtor-name> |
1088 | // ::= <source-name> |
1089 | // ::= <template-name> |
1090 | |
1091 | // Check if we have a template. |
1092 | const TemplateArgumentList *TemplateArgs = nullptr; |
1093 | if (GlobalDecl TD = isTemplate(GD, TemplateArgs)) { |
1094 | // Function templates aren't considered for name back referencing. This |
1095 | // makes sense since function templates aren't likely to occur multiple |
1096 | // times in a symbol. |
1097 | if (isa<FunctionTemplateDecl>(Val: TD.getDecl())) { |
1098 | mangleTemplateInstantiationName(GD: TD, TemplateArgs: *TemplateArgs); |
1099 | Out << '@'; |
1100 | return; |
1101 | } |
1102 | |
1103 | // Here comes the tricky thing: if we need to mangle something like |
1104 | // void foo(A::X<Y>, B::X<Y>), |
1105 | // the X<Y> part is aliased. However, if you need to mangle |
1106 | // void foo(A::X<A::Y>, A::X<B::Y>), |
1107 | // the A::X<> part is not aliased. |
1108 | // That is, from the mangler's perspective we have a structure like this: |
1109 | // namespace[s] -> type[ -> template-parameters] |
1110 | // but from the Clang perspective we have |
1111 | // type [ -> template-parameters] |
1112 | // \-> namespace[s] |
1113 | // What we do is we create a new mangler, mangle the same type (without |
1114 | // a namespace suffix) to a string using the extra mangler and then use |
1115 | // the mangled type name as a key to check the mangling of different types |
1116 | // for aliasing. |
1117 | |
1118 | // It's important to key cache reads off ND, not TD -- the same TD can |
1119 | // be used with different TemplateArgs, but ND uniquely identifies |
1120 | // TD / TemplateArg pairs. |
1121 | ArgBackRefMap::iterator Found = TemplateArgBackReferences.find(Val: ND); |
1122 | if (Found == TemplateArgBackReferences.end()) { |
1123 | |
1124 | TemplateArgStringMap::iterator Found = TemplateArgStrings.find(Val: ND); |
1125 | if (Found == TemplateArgStrings.end()) { |
1126 | // Mangle full template name into temporary buffer. |
1127 | llvm::SmallString<64> TemplateMangling; |
1128 | llvm::raw_svector_ostream Stream(TemplateMangling); |
1129 | MicrosoftCXXNameMangler (Context, Stream); |
1130 | Extra.mangleTemplateInstantiationName(GD: TD, TemplateArgs: *TemplateArgs); |
1131 | |
1132 | // Use the string backref vector to possibly get a back reference. |
1133 | mangleSourceName(Name: TemplateMangling); |
1134 | |
1135 | // Memoize back reference for this type if one exist, else memoize |
1136 | // the mangling itself. |
1137 | BackRefVec::iterator StringFound = |
1138 | llvm::find(Range&: NameBackReferences, Val: TemplateMangling); |
1139 | if (StringFound != NameBackReferences.end()) { |
1140 | TemplateArgBackReferences[ND] = |
1141 | StringFound - NameBackReferences.begin(); |
1142 | } else { |
1143 | TemplateArgStrings[ND] = |
1144 | TemplateArgStringStorage.save(S: TemplateMangling.str()); |
1145 | } |
1146 | } else { |
1147 | Out << Found->second << '@'; // Outputs a StringRef. |
1148 | } |
1149 | } else { |
1150 | Out << Found->second; // Outputs a back reference (an int). |
1151 | } |
1152 | return; |
1153 | } |
1154 | |
1155 | switch (Name.getNameKind()) { |
1156 | case DeclarationName::Identifier: { |
1157 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
1158 | bool IsDeviceStub = |
1159 | ND && |
1160 | ((isa<FunctionDecl>(Val: ND) && ND->hasAttr<CUDAGlobalAttr>()) || |
1161 | (isa<FunctionTemplateDecl>(Val: ND) && |
1162 | cast<FunctionTemplateDecl>(Val: ND) |
1163 | ->getTemplatedDecl() |
1164 | ->hasAttr<CUDAGlobalAttr>())) && |
1165 | GD.getKernelReferenceKind() == KernelReferenceKind::Stub; |
1166 | bool IsOCLDeviceStub = |
1167 | ND && isa<FunctionDecl>(Val: ND) && |
1168 | DeviceKernelAttr::isOpenCLSpelling( |
1169 | A: ND->getAttr<DeviceKernelAttr>()) && |
1170 | GD.getKernelReferenceKind() == KernelReferenceKind::Stub; |
1171 | if (IsDeviceStub) |
1172 | mangleSourceName( |
1173 | Name: (llvm::Twine("__device_stub__" ) + II->getName()).str()); |
1174 | else if (IsOCLDeviceStub) |
1175 | mangleSourceName( |
1176 | Name: (llvm::Twine("__clang_ocl_kern_imp_" ) + II->getName()).str()); |
1177 | else |
1178 | mangleSourceName(Name: II->getName()); |
1179 | break; |
1180 | } |
1181 | |
1182 | // Otherwise, an anonymous entity. We must have a declaration. |
1183 | assert(ND && "mangling empty name without declaration" ); |
1184 | |
1185 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Val: ND)) { |
1186 | if (NS->isAnonymousNamespace()) { |
1187 | Out << "?A0x" << Context.getAnonymousNamespaceHash() << '@'; |
1188 | break; |
1189 | } |
1190 | } |
1191 | |
1192 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: ND)) { |
1193 | // Decomposition declarations are considered anonymous, and get |
1194 | // numbered with a $S prefix. |
1195 | llvm::SmallString<64> Name("$S" ); |
1196 | // Get a unique id for the anonymous struct. |
1197 | Name += llvm::utostr(X: Context.getAnonymousStructId(D: DD) + 1); |
1198 | mangleSourceName(Name); |
1199 | break; |
1200 | } |
1201 | |
1202 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: ND)) { |
1203 | // We must have an anonymous union or struct declaration. |
1204 | const CXXRecordDecl *RD = VD->getType()->getAsCXXRecordDecl(); |
1205 | assert(RD && "expected variable decl to have a record type" ); |
1206 | // Anonymous types with no tag or typedef get the name of their |
1207 | // declarator mangled in. If they have no declarator, number them with |
1208 | // a $S prefix. |
1209 | llvm::SmallString<64> Name("$S" ); |
1210 | // Get a unique id for the anonymous struct. |
1211 | Name += llvm::utostr(X: Context.getAnonymousStructId(D: RD) + 1); |
1212 | mangleSourceName(Name: Name.str()); |
1213 | break; |
1214 | } |
1215 | |
1216 | if (const MSGuidDecl *GD = dyn_cast<MSGuidDecl>(Val: ND)) { |
1217 | // Mangle a GUID object as if it were a variable with the corresponding |
1218 | // mangled name. |
1219 | SmallString<sizeof("_GUID_12345678_1234_1234_1234_1234567890ab" )> GUID; |
1220 | llvm::raw_svector_ostream GUIDOS(GUID); |
1221 | Context.mangleMSGuidDecl(GD, GUIDOS); |
1222 | mangleSourceName(Name: GUID); |
1223 | break; |
1224 | } |
1225 | |
1226 | if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: ND)) { |
1227 | Out << "?__N" ; |
1228 | mangleTemplateArgValue(T: TPO->getType().getUnqualifiedType(), |
1229 | V: TPO->getValue(), TplArgKind::ClassNTTP); |
1230 | break; |
1231 | } |
1232 | |
1233 | // We must have an anonymous struct. |
1234 | const TagDecl *TD = cast<TagDecl>(Val: ND); |
1235 | if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) { |
1236 | assert(TD->getDeclContext() == D->getDeclContext() && |
1237 | "Typedef should not be in another decl context!" ); |
1238 | assert(D->getDeclName().getAsIdentifierInfo() && |
1239 | "Typedef was not named!" ); |
1240 | mangleSourceName(Name: D->getDeclName().getAsIdentifierInfo()->getName()); |
1241 | break; |
1242 | } |
1243 | |
1244 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Val: TD)) { |
1245 | if (Record->isLambda()) { |
1246 | llvm::SmallString<10> Name("<lambda_" ); |
1247 | |
1248 | Decl *LambdaContextDecl = Record->getLambdaContextDecl(); |
1249 | unsigned LambdaManglingNumber = Record->getLambdaManglingNumber(); |
1250 | unsigned LambdaId; |
1251 | const ParmVarDecl *Parm = |
1252 | dyn_cast_or_null<ParmVarDecl>(Val: LambdaContextDecl); |
1253 | const FunctionDecl *Func = |
1254 | Parm ? dyn_cast<FunctionDecl>(Val: Parm->getDeclContext()) : nullptr; |
1255 | |
1256 | if (Func) { |
1257 | unsigned DefaultArgNo = |
1258 | Func->getNumParams() - Parm->getFunctionScopeIndex(); |
1259 | Name += llvm::utostr(X: DefaultArgNo); |
1260 | Name += "_" ; |
1261 | } |
1262 | |
1263 | if (LambdaManglingNumber) |
1264 | LambdaId = LambdaManglingNumber; |
1265 | else |
1266 | LambdaId = Context.getLambdaId(RD: Record); |
1267 | |
1268 | Name += llvm::utostr(X: LambdaId); |
1269 | Name += ">" ; |
1270 | |
1271 | mangleSourceName(Name); |
1272 | |
1273 | // If the context is a variable or a class member and not a parameter, |
1274 | // it is encoded in a qualified name. |
1275 | if (LambdaManglingNumber && LambdaContextDecl) { |
1276 | if ((isa<VarDecl>(Val: LambdaContextDecl) || |
1277 | isa<FieldDecl>(Val: LambdaContextDecl)) && |
1278 | !isa<ParmVarDecl>(Val: LambdaContextDecl)) { |
1279 | mangleUnqualifiedName(GD: cast<NamedDecl>(Val: LambdaContextDecl)); |
1280 | } |
1281 | } |
1282 | break; |
1283 | } |
1284 | } |
1285 | |
1286 | llvm::SmallString<64> Name; |
1287 | if (DeclaratorDecl *DD = |
1288 | Context.getASTContext().getDeclaratorForUnnamedTagDecl(TD)) { |
1289 | // Anonymous types without a name for linkage purposes have their |
1290 | // declarator mangled in if they have one. |
1291 | Name += "<unnamed-type-" ; |
1292 | Name += DD->getName(); |
1293 | } else if (TypedefNameDecl *TND = |
1294 | Context.getASTContext().getTypedefNameForUnnamedTagDecl( |
1295 | TD)) { |
1296 | // Anonymous types without a name for linkage purposes have their |
1297 | // associate typedef mangled in if they have one. |
1298 | Name += "<unnamed-type-" ; |
1299 | Name += TND->getName(); |
1300 | } else if (isa<EnumDecl>(Val: TD) && |
1301 | cast<EnumDecl>(Val: TD)->enumerator_begin() != |
1302 | cast<EnumDecl>(Val: TD)->enumerator_end()) { |
1303 | // Anonymous non-empty enums mangle in the first enumerator. |
1304 | auto *ED = cast<EnumDecl>(Val: TD); |
1305 | Name += "<unnamed-enum-" ; |
1306 | Name += ED->enumerator_begin()->getName(); |
1307 | } else { |
1308 | // Otherwise, number the types using a $S prefix. |
1309 | Name += "<unnamed-type-$S" ; |
1310 | Name += llvm::utostr(X: Context.getAnonymousStructId(D: TD) + 1); |
1311 | } |
1312 | Name += ">" ; |
1313 | mangleSourceName(Name: Name.str()); |
1314 | break; |
1315 | } |
1316 | |
1317 | case DeclarationName::ObjCZeroArgSelector: |
1318 | case DeclarationName::ObjCOneArgSelector: |
1319 | case DeclarationName::ObjCMultiArgSelector: { |
1320 | // This is reachable only when constructing an outlined SEH finally |
1321 | // block. Nothing depends on this mangling and it's used only with |
1322 | // functinos with internal linkage. |
1323 | llvm::SmallString<64> Name; |
1324 | mangleSourceName(Name: Name.str()); |
1325 | break; |
1326 | } |
1327 | |
1328 | case DeclarationName::CXXConstructorName: |
1329 | if (isStructorDecl(ND)) { |
1330 | if (StructorType == Ctor_CopyingClosure) { |
1331 | Out << "?_O" ; |
1332 | return; |
1333 | } |
1334 | if (StructorType == Ctor_DefaultClosure) { |
1335 | Out << "?_F" ; |
1336 | return; |
1337 | } |
1338 | } |
1339 | Out << "?0" ; |
1340 | return; |
1341 | |
1342 | case DeclarationName::CXXDestructorName: |
1343 | if (isStructorDecl(ND)) |
1344 | // If the named decl is the C++ destructor we're mangling, |
1345 | // use the type we were given. |
1346 | mangleCXXDtorType(T: static_cast<CXXDtorType>(StructorType)); |
1347 | else |
1348 | // Otherwise, use the base destructor name. This is relevant if a |
1349 | // class with a destructor is declared within a destructor. |
1350 | mangleCXXDtorType(T: Dtor_Base); |
1351 | break; |
1352 | |
1353 | case DeclarationName::CXXConversionFunctionName: |
1354 | // <operator-name> ::= ?B # (cast) |
1355 | // The target type is encoded as the return type. |
1356 | Out << "?B" ; |
1357 | break; |
1358 | |
1359 | case DeclarationName::CXXOperatorName: |
1360 | mangleOperatorName(OO: Name.getCXXOverloadedOperator(), Loc: ND->getLocation()); |
1361 | break; |
1362 | |
1363 | case DeclarationName::CXXLiteralOperatorName: { |
1364 | Out << "?__K" ; |
1365 | mangleSourceName(Name: Name.getCXXLiteralIdentifier()->getName()); |
1366 | break; |
1367 | } |
1368 | |
1369 | case DeclarationName::CXXDeductionGuideName: |
1370 | llvm_unreachable("Can't mangle a deduction guide name!" ); |
1371 | |
1372 | case DeclarationName::CXXUsingDirective: |
1373 | llvm_unreachable("Can't mangle a using directive name!" ); |
1374 | } |
1375 | } |
1376 | |
1377 | // <postfix> ::= <unqualified-name> [<postfix>] |
1378 | // ::= <substitution> [<postfix>] |
1379 | void MicrosoftCXXNameMangler::mangleNestedName(GlobalDecl GD) { |
1380 | const NamedDecl *ND = cast<NamedDecl>(Val: GD.getDecl()); |
1381 | |
1382 | if (const auto *ID = dyn_cast<IndirectFieldDecl>(Val: ND)) |
1383 | for (unsigned I = 1, IE = ID->getChainingSize(); I < IE; ++I) |
1384 | mangleSourceName(Name: "<unnamed-tag>" ); |
1385 | |
1386 | const DeclContext *DC = getEffectiveDeclContext(D: ND); |
1387 | while (!DC->isTranslationUnit()) { |
1388 | if (isa<TagDecl>(Val: ND) || isa<VarDecl>(Val: ND)) { |
1389 | unsigned Disc; |
1390 | if (Context.getNextDiscriminator(ND, disc&: Disc)) { |
1391 | Out << '?'; |
1392 | mangleNumber(Number: Disc); |
1393 | Out << '?'; |
1394 | } |
1395 | } |
1396 | |
1397 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(Val: DC)) { |
1398 | auto Discriminate = |
1399 | [](StringRef Name, const unsigned Discriminator, |
1400 | const unsigned ParameterDiscriminator) -> std::string { |
1401 | std::string Buffer; |
1402 | llvm::raw_string_ostream Stream(Buffer); |
1403 | Stream << Name; |
1404 | if (Discriminator) |
1405 | Stream << '_' << Discriminator; |
1406 | if (ParameterDiscriminator) |
1407 | Stream << '_' << ParameterDiscriminator; |
1408 | return Buffer; |
1409 | }; |
1410 | |
1411 | unsigned Discriminator = BD->getBlockManglingNumber(); |
1412 | if (!Discriminator) |
1413 | Discriminator = Context.getBlockId(BD, /*Local=*/false); |
1414 | |
1415 | // Mangle the parameter position as a discriminator to deal with unnamed |
1416 | // parameters. Rather than mangling the unqualified parameter name, |
1417 | // always use the position to give a uniform mangling. |
1418 | unsigned ParameterDiscriminator = 0; |
1419 | if (const auto *MC = BD->getBlockManglingContextDecl()) |
1420 | if (const auto *P = dyn_cast<ParmVarDecl>(Val: MC)) |
1421 | if (const auto *F = dyn_cast<FunctionDecl>(Val: P->getDeclContext())) |
1422 | ParameterDiscriminator = |
1423 | F->getNumParams() - P->getFunctionScopeIndex(); |
1424 | |
1425 | DC = getEffectiveDeclContext(D: BD); |
1426 | |
1427 | Out << '?'; |
1428 | mangleSourceName(Name: Discriminate("_block_invoke" , Discriminator, |
1429 | ParameterDiscriminator)); |
1430 | // If we have a block mangling context, encode that now. This allows us |
1431 | // to discriminate between named static data initializers in the same |
1432 | // scope. This is handled differently from parameters, which use |
1433 | // positions to discriminate between multiple instances. |
1434 | if (const auto *MC = BD->getBlockManglingContextDecl()) |
1435 | if (!isa<ParmVarDecl>(Val: MC)) |
1436 | if (const auto *ND = dyn_cast<NamedDecl>(Val: MC)) |
1437 | mangleUnqualifiedName(GD: ND); |
1438 | // MS ABI and Itanium manglings are in inverted scopes. In the case of a |
1439 | // RecordDecl, mangle the entire scope hierarchy at this point rather than |
1440 | // just the unqualified name to get the ordering correct. |
1441 | if (const auto *RD = dyn_cast<RecordDecl>(Val: DC)) |
1442 | mangleName(GD: RD); |
1443 | else |
1444 | Out << '@'; |
1445 | // void __cdecl |
1446 | Out << "YAX" ; |
1447 | // struct __block_literal * |
1448 | Out << 'P'; |
1449 | // __ptr64 |
1450 | if (PointersAre64Bit) |
1451 | Out << 'E'; |
1452 | Out << 'A'; |
1453 | mangleArtificialTagType(TK: TagTypeKind::Struct, |
1454 | UnqualifiedName: Discriminate("__block_literal" , Discriminator, |
1455 | ParameterDiscriminator)); |
1456 | Out << "@Z" ; |
1457 | |
1458 | // If the effective context was a Record, we have fully mangled the |
1459 | // qualified name and do not need to continue. |
1460 | if (isa<RecordDecl>(Val: DC)) |
1461 | break; |
1462 | continue; |
1463 | } else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: DC)) { |
1464 | mangleObjCMethodName(MD: Method); |
1465 | } else if (isa<NamedDecl>(Val: DC)) { |
1466 | ND = cast<NamedDecl>(Val: DC); |
1467 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: ND)) { |
1468 | mangle(GD: getGlobalDeclAsDeclContext(DC: FD), Prefix: "?" ); |
1469 | break; |
1470 | } else { |
1471 | mangleUnqualifiedName(GD: ND); |
1472 | // Lambdas in default arguments conceptually belong to the function the |
1473 | // parameter corresponds to. |
1474 | if (const auto *LDADC = getLambdaDefaultArgumentDeclContext(D: ND)) { |
1475 | DC = LDADC; |
1476 | continue; |
1477 | } |
1478 | } |
1479 | } |
1480 | DC = DC->getParent(); |
1481 | } |
1482 | } |
1483 | |
1484 | void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
1485 | // Microsoft uses the names on the case labels for these dtor variants. Clang |
1486 | // uses the Itanium terminology internally. Everything in this ABI delegates |
1487 | // towards the base dtor. |
1488 | switch (T) { |
1489 | // <operator-name> ::= ?1 # destructor |
1490 | case Dtor_Base: Out << "?1" ; return; |
1491 | // <operator-name> ::= ?_D # vbase destructor |
1492 | case Dtor_Complete: Out << "?_D" ; return; |
1493 | // <operator-name> ::= ?_G # scalar deleting destructor |
1494 | case Dtor_Deleting: Out << "?_G" ; return; |
1495 | // <operator-name> ::= ?_E # vector deleting destructor |
1496 | // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need |
1497 | // it. |
1498 | case Dtor_Comdat: |
1499 | llvm_unreachable("not expecting a COMDAT" ); |
1500 | } |
1501 | llvm_unreachable("Unsupported dtor type?" ); |
1502 | } |
1503 | |
1504 | void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, |
1505 | SourceLocation Loc) { |
1506 | switch (OO) { |
1507 | // ?0 # constructor |
1508 | // ?1 # destructor |
1509 | // <operator-name> ::= ?2 # new |
1510 | case OO_New: Out << "?2" ; break; |
1511 | // <operator-name> ::= ?3 # delete |
1512 | case OO_Delete: Out << "?3" ; break; |
1513 | // <operator-name> ::= ?4 # = |
1514 | case OO_Equal: Out << "?4" ; break; |
1515 | // <operator-name> ::= ?5 # >> |
1516 | case OO_GreaterGreater: Out << "?5" ; break; |
1517 | // <operator-name> ::= ?6 # << |
1518 | case OO_LessLess: Out << "?6" ; break; |
1519 | // <operator-name> ::= ?7 # ! |
1520 | case OO_Exclaim: Out << "?7" ; break; |
1521 | // <operator-name> ::= ?8 # == |
1522 | case OO_EqualEqual: Out << "?8" ; break; |
1523 | // <operator-name> ::= ?9 # != |
1524 | case OO_ExclaimEqual: Out << "?9" ; break; |
1525 | // <operator-name> ::= ?A # [] |
1526 | case OO_Subscript: Out << "?A" ; break; |
1527 | // ?B # conversion |
1528 | // <operator-name> ::= ?C # -> |
1529 | case OO_Arrow: Out << "?C" ; break; |
1530 | // <operator-name> ::= ?D # * |
1531 | case OO_Star: Out << "?D" ; break; |
1532 | // <operator-name> ::= ?E # ++ |
1533 | case OO_PlusPlus: Out << "?E" ; break; |
1534 | // <operator-name> ::= ?F # -- |
1535 | case OO_MinusMinus: Out << "?F" ; break; |
1536 | // <operator-name> ::= ?G # - |
1537 | case OO_Minus: Out << "?G" ; break; |
1538 | // <operator-name> ::= ?H # + |
1539 | case OO_Plus: Out << "?H" ; break; |
1540 | // <operator-name> ::= ?I # & |
1541 | case OO_Amp: Out << "?I" ; break; |
1542 | // <operator-name> ::= ?J # ->* |
1543 | case OO_ArrowStar: Out << "?J" ; break; |
1544 | // <operator-name> ::= ?K # / |
1545 | case OO_Slash: Out << "?K" ; break; |
1546 | // <operator-name> ::= ?L # % |
1547 | case OO_Percent: Out << "?L" ; break; |
1548 | // <operator-name> ::= ?M # < |
1549 | case OO_Less: Out << "?M" ; break; |
1550 | // <operator-name> ::= ?N # <= |
1551 | case OO_LessEqual: Out << "?N" ; break; |
1552 | // <operator-name> ::= ?O # > |
1553 | case OO_Greater: Out << "?O" ; break; |
1554 | // <operator-name> ::= ?P # >= |
1555 | case OO_GreaterEqual: Out << "?P" ; break; |
1556 | // <operator-name> ::= ?Q # , |
1557 | case OO_Comma: Out << "?Q" ; break; |
1558 | // <operator-name> ::= ?R # () |
1559 | case OO_Call: Out << "?R" ; break; |
1560 | // <operator-name> ::= ?S # ~ |
1561 | case OO_Tilde: Out << "?S" ; break; |
1562 | // <operator-name> ::= ?T # ^ |
1563 | case OO_Caret: Out << "?T" ; break; |
1564 | // <operator-name> ::= ?U # | |
1565 | case OO_Pipe: Out << "?U" ; break; |
1566 | // <operator-name> ::= ?V # && |
1567 | case OO_AmpAmp: Out << "?V" ; break; |
1568 | // <operator-name> ::= ?W # || |
1569 | case OO_PipePipe: Out << "?W" ; break; |
1570 | // <operator-name> ::= ?X # *= |
1571 | case OO_StarEqual: Out << "?X" ; break; |
1572 | // <operator-name> ::= ?Y # += |
1573 | case OO_PlusEqual: Out << "?Y" ; break; |
1574 | // <operator-name> ::= ?Z # -= |
1575 | case OO_MinusEqual: Out << "?Z" ; break; |
1576 | // <operator-name> ::= ?_0 # /= |
1577 | case OO_SlashEqual: Out << "?_0" ; break; |
1578 | // <operator-name> ::= ?_1 # %= |
1579 | case OO_PercentEqual: Out << "?_1" ; break; |
1580 | // <operator-name> ::= ?_2 # >>= |
1581 | case OO_GreaterGreaterEqual: Out << "?_2" ; break; |
1582 | // <operator-name> ::= ?_3 # <<= |
1583 | case OO_LessLessEqual: Out << "?_3" ; break; |
1584 | // <operator-name> ::= ?_4 # &= |
1585 | case OO_AmpEqual: Out << "?_4" ; break; |
1586 | // <operator-name> ::= ?_5 # |= |
1587 | case OO_PipeEqual: Out << "?_5" ; break; |
1588 | // <operator-name> ::= ?_6 # ^= |
1589 | case OO_CaretEqual: Out << "?_6" ; break; |
1590 | // ?_7 # vftable |
1591 | // ?_8 # vbtable |
1592 | // ?_9 # vcall |
1593 | // ?_A # typeof |
1594 | // ?_B # local static guard |
1595 | // ?_C # string |
1596 | // ?_D # vbase destructor |
1597 | // ?_E # vector deleting destructor |
1598 | // ?_F # default constructor closure |
1599 | // ?_G # scalar deleting destructor |
1600 | // ?_H # vector constructor iterator |
1601 | // ?_I # vector destructor iterator |
1602 | // ?_J # vector vbase constructor iterator |
1603 | // ?_K # virtual displacement map |
1604 | // ?_L # eh vector constructor iterator |
1605 | // ?_M # eh vector destructor iterator |
1606 | // ?_N # eh vector vbase constructor iterator |
1607 | // ?_O # copy constructor closure |
1608 | // ?_P<name> # udt returning <name> |
1609 | // ?_Q # <unknown> |
1610 | // ?_R0 # RTTI Type Descriptor |
1611 | // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) |
1612 | // ?_R2 # RTTI Base Class Array |
1613 | // ?_R3 # RTTI Class Hierarchy Descriptor |
1614 | // ?_R4 # RTTI Complete Object Locator |
1615 | // ?_S # local vftable |
1616 | // ?_T # local vftable constructor closure |
1617 | // <operator-name> ::= ?_U # new[] |
1618 | case OO_Array_New: Out << "?_U" ; break; |
1619 | // <operator-name> ::= ?_V # delete[] |
1620 | case OO_Array_Delete: Out << "?_V" ; break; |
1621 | // <operator-name> ::= ?__L # co_await |
1622 | case OO_Coawait: Out << "?__L" ; break; |
1623 | // <operator-name> ::= ?__M # <=> |
1624 | case OO_Spaceship: Out << "?__M" ; break; |
1625 | |
1626 | case OO_Conditional: { |
1627 | Error(loc: Loc, thingy: "conditional operator" ); |
1628 | break; |
1629 | } |
1630 | |
1631 | case OO_None: |
1632 | case NUM_OVERLOADED_OPERATORS: |
1633 | llvm_unreachable("Not an overloaded operator" ); |
1634 | } |
1635 | } |
1636 | |
1637 | void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) { |
1638 | // <source name> ::= <identifier> @ |
1639 | BackRefVec::iterator Found = llvm::find(Range&: NameBackReferences, Val: Name); |
1640 | if (Found == NameBackReferences.end()) { |
1641 | if (NameBackReferences.size() < 10) |
1642 | NameBackReferences.push_back(Elt: std::string(Name)); |
1643 | Out << Name << '@'; |
1644 | } else { |
1645 | Out << (Found - NameBackReferences.begin()); |
1646 | } |
1647 | } |
1648 | |
1649 | void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
1650 | Context.mangleObjCMethodNameAsSourceName(MD, Out); |
1651 | } |
1652 | |
1653 | void MicrosoftCXXNameMangler::mangleTemplateInstantiationName( |
1654 | GlobalDecl GD, const TemplateArgumentList &TemplateArgs) { |
1655 | // <template-name> ::= <unscoped-template-name> <template-args> |
1656 | // ::= <substitution> |
1657 | // Always start with the unqualified name. |
1658 | |
1659 | // Templates have their own context for back references. |
1660 | ArgBackRefMap OuterFunArgsContext; |
1661 | ArgBackRefMap OuterTemplateArgsContext; |
1662 | BackRefVec OuterTemplateContext; |
1663 | PassObjectSizeArgsSet OuterPassObjectSizeArgs; |
1664 | NameBackReferences.swap(RHS&: OuterTemplateContext); |
1665 | FunArgBackReferences.swap(RHS&: OuterFunArgsContext); |
1666 | TemplateArgBackReferences.swap(RHS&: OuterTemplateArgsContext); |
1667 | PassObjectSizeArgs.swap(x&: OuterPassObjectSizeArgs); |
1668 | |
1669 | mangleUnscopedTemplateName(GD); |
1670 | mangleTemplateArgs(TD: cast<TemplateDecl>(Val: GD.getDecl()), TemplateArgs); |
1671 | |
1672 | // Restore the previous back reference contexts. |
1673 | NameBackReferences.swap(RHS&: OuterTemplateContext); |
1674 | FunArgBackReferences.swap(RHS&: OuterFunArgsContext); |
1675 | TemplateArgBackReferences.swap(RHS&: OuterTemplateArgsContext); |
1676 | PassObjectSizeArgs.swap(x&: OuterPassObjectSizeArgs); |
1677 | } |
1678 | |
1679 | void MicrosoftCXXNameMangler::mangleUnscopedTemplateName(GlobalDecl GD) { |
1680 | // <unscoped-template-name> ::= ?$ <unqualified-name> |
1681 | Out << "?$" ; |
1682 | mangleUnqualifiedName(GD); |
1683 | } |
1684 | |
1685 | void MicrosoftCXXNameMangler::mangleIntegerLiteral( |
1686 | const llvm::APSInt &Value, const NonTypeTemplateParmDecl *PD, |
1687 | QualType TemplateArgType) { |
1688 | // <integer-literal> ::= $0 <number> |
1689 | // <integer-literal> ::= <auto-nttp> |
1690 | // |
1691 | // <auto-nttp> ::= $ M <type> 0 <number> |
1692 | Out << "$" ; |
1693 | |
1694 | // Since MSVC 2019, add 'M[<type>]' after '$' for auto template parameter when |
1695 | // argument is integer. |
1696 | if (getASTContext().getLangOpts().isCompatibleWithMSVC( |
1697 | MajorVersion: LangOptions::MSVC2019) && |
1698 | PD && PD->getType()->getTypeClass() == Type::Auto && |
1699 | !TemplateArgType.isNull()) { |
1700 | Out << "M" ; |
1701 | mangleType(T: TemplateArgType, Range: SourceRange(), QMM: QMM_Drop); |
1702 | } |
1703 | |
1704 | Out << "0" ; |
1705 | |
1706 | mangleNumber(Number: Value); |
1707 | } |
1708 | |
1709 | void MicrosoftCXXNameMangler::mangleExpression( |
1710 | const Expr *E, const NonTypeTemplateParmDecl *PD) { |
1711 | // See if this is a constant expression. |
1712 | if (std::optional<llvm::APSInt> Value = |
1713 | E->getIntegerConstantExpr(Ctx: Context.getASTContext())) { |
1714 | mangleIntegerLiteral(Value: *Value, PD, TemplateArgType: E->getType()); |
1715 | return; |
1716 | } |
1717 | |
1718 | // As bad as this diagnostic is, it's better than crashing. |
1719 | Error(loc: E->getExprLoc(), thing1: "expression type: " , thing2: E->getStmtClassName()) |
1720 | << E->getSourceRange(); |
1721 | } |
1722 | |
1723 | void MicrosoftCXXNameMangler::mangleTemplateArgs( |
1724 | const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) { |
1725 | // <template-args> ::= <template-arg>+ |
1726 | const TemplateParameterList *TPL = TD->getTemplateParameters(); |
1727 | assert(TPL->size() == TemplateArgs.size() && |
1728 | "size mismatch between args and parms!" ); |
1729 | |
1730 | for (size_t i = 0; i < TemplateArgs.size(); ++i) { |
1731 | const TemplateArgument &TA = TemplateArgs[i]; |
1732 | |
1733 | // Separate consecutive packs by $$Z. |
1734 | if (i > 0 && TA.getKind() == TemplateArgument::Pack && |
1735 | TemplateArgs[i - 1].getKind() == TemplateArgument::Pack) |
1736 | Out << "$$Z" ; |
1737 | |
1738 | mangleTemplateArg(TD, TA, Parm: TPL->getParam(Idx: i)); |
1739 | } |
1740 | } |
1741 | |
1742 | /// If value V (with type T) represents a decayed pointer to the first element |
1743 | /// of an array, return that array. |
1744 | static ValueDecl *getAsArrayToPointerDecayedDecl(QualType T, const APValue &V) { |
1745 | // Must be a pointer... |
1746 | if (!T->isPointerType() || !V.isLValue() || !V.hasLValuePath() || |
1747 | !V.getLValueBase()) |
1748 | return nullptr; |
1749 | // ... to element 0 of an array. |
1750 | QualType BaseT = V.getLValueBase().getType(); |
1751 | if (!BaseT->isArrayType() || V.getLValuePath().size() != 1 || |
1752 | V.getLValuePath()[0].getAsArrayIndex() != 0) |
1753 | return nullptr; |
1754 | return const_cast<ValueDecl *>( |
1755 | V.getLValueBase().dyn_cast<const ValueDecl *>()); |
1756 | } |
1757 | |
1758 | void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD, |
1759 | const TemplateArgument &TA, |
1760 | const NamedDecl *Parm) { |
1761 | // <template-arg> ::= <type> |
1762 | // ::= <integer-literal> |
1763 | // ::= <member-data-pointer> |
1764 | // ::= <member-function-pointer> |
1765 | // ::= $ <constant-value> |
1766 | // ::= $ <auto-nttp-constant-value> |
1767 | // ::= <template-args> |
1768 | // |
1769 | // <auto-nttp-constant-value> ::= M <type> <constant-value> |
1770 | // |
1771 | // <constant-value> ::= 0 <number> # integer |
1772 | // ::= 1 <mangled-name> # address of D |
1773 | // ::= 2 <type> <typed-constant-value>* @ # struct |
1774 | // ::= 3 <type> <constant-value>* @ # array |
1775 | // ::= 4 ??? # string |
1776 | // ::= 5 <constant-value> @ # address of subobject |
1777 | // ::= 6 <constant-value> <unqualified-name> @ # a.b |
1778 | // ::= 7 <type> [<unqualified-name> <constant-value>] @ |
1779 | // # union, with or without an active member |
1780 | // # pointer to member, symbolically |
1781 | // ::= 8 <class> <unqualified-name> @ |
1782 | // ::= A <type> <non-negative integer> # float |
1783 | // ::= B <type> <non-negative integer> # double |
1784 | // # pointer to member, by component value |
1785 | // ::= F <number> <number> |
1786 | // ::= G <number> <number> <number> |
1787 | // ::= H <mangled-name> <number> |
1788 | // ::= I <mangled-name> <number> <number> |
1789 | // ::= J <mangled-name> <number> <number> <number> |
1790 | // |
1791 | // <typed-constant-value> ::= [<type>] <constant-value> |
1792 | // |
1793 | // The <type> appears to be included in a <typed-constant-value> only in the |
1794 | // '0', '1', '8', 'A', 'B', and 'E' cases. |
1795 | |
1796 | switch (TA.getKind()) { |
1797 | case TemplateArgument::Null: |
1798 | llvm_unreachable("Can't mangle null template arguments!" ); |
1799 | case TemplateArgument::TemplateExpansion: |
1800 | llvm_unreachable("Can't mangle template expansion arguments!" ); |
1801 | case TemplateArgument::Type: { |
1802 | QualType T = TA.getAsType(); |
1803 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
1804 | break; |
1805 | } |
1806 | case TemplateArgument::Declaration: { |
1807 | const NamedDecl *ND = TA.getAsDecl(); |
1808 | if (isa<FieldDecl>(Val: ND) || isa<IndirectFieldDecl>(Val: ND)) { |
1809 | mangleMemberDataPointer(RD: cast<CXXRecordDecl>(Val: ND->getDeclContext()) |
1810 | ->getMostRecentNonInjectedDecl(), |
1811 | VD: cast<ValueDecl>(Val: ND), |
1812 | PD: cast<NonTypeTemplateParmDecl>(Val: Parm), |
1813 | TemplateArgType: TA.getParamTypeForDecl()); |
1814 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: ND)) { |
1815 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD); |
1816 | if (MD && MD->isInstance()) { |
1817 | mangleMemberFunctionPointer( |
1818 | RD: MD->getParent()->getMostRecentNonInjectedDecl(), MD, |
1819 | PD: cast<NonTypeTemplateParmDecl>(Val: Parm), TemplateArgType: TA.getParamTypeForDecl()); |
1820 | } else { |
1821 | mangleFunctionPointer(FD, PD: cast<NonTypeTemplateParmDecl>(Val: Parm), |
1822 | TemplateArgType: TA.getParamTypeForDecl()); |
1823 | } |
1824 | } else if (TA.getParamTypeForDecl()->isRecordType()) { |
1825 | Out << "$" ; |
1826 | auto *TPO = cast<TemplateParamObjectDecl>(Val: ND); |
1827 | mangleTemplateArgValue(T: TPO->getType().getUnqualifiedType(), |
1828 | V: TPO->getValue(), TplArgKind::ClassNTTP); |
1829 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(Val: ND)) { |
1830 | mangleVarDecl(VD, PD: cast<NonTypeTemplateParmDecl>(Val: Parm), |
1831 | TemplateArgType: TA.getParamTypeForDecl()); |
1832 | } else { |
1833 | mangle(GD: ND, Prefix: "$1?" ); |
1834 | } |
1835 | break; |
1836 | } |
1837 | case TemplateArgument::Integral: { |
1838 | QualType T = TA.getIntegralType(); |
1839 | mangleIntegerLiteral(Value: TA.getAsIntegral(), |
1840 | PD: cast<NonTypeTemplateParmDecl>(Val: Parm), TemplateArgType: T); |
1841 | break; |
1842 | } |
1843 | case TemplateArgument::NullPtr: { |
1844 | QualType T = TA.getNullPtrType(); |
1845 | if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) { |
1846 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
1847 | if (MPT->isMemberFunctionPointerType() && |
1848 | !isa<FunctionTemplateDecl>(Val: TD)) { |
1849 | mangleMemberFunctionPointer(RD, MD: nullptr, PD: nullptr, TemplateArgType: QualType()); |
1850 | return; |
1851 | } |
1852 | if (MPT->isMemberDataPointer()) { |
1853 | if (!isa<FunctionTemplateDecl>(Val: TD)) { |
1854 | mangleMemberDataPointer(RD, VD: nullptr, PD: nullptr, TemplateArgType: QualType()); |
1855 | return; |
1856 | } |
1857 | // nullptr data pointers are always represented with a single field |
1858 | // which is initialized with either 0 or -1. Why -1? Well, we need to |
1859 | // distinguish the case where the data member is at offset zero in the |
1860 | // record. |
1861 | // However, we are free to use 0 *if* we would use multiple fields for |
1862 | // non-nullptr member pointers. |
1863 | if (!RD->nullFieldOffsetIsZero()) { |
1864 | mangleIntegerLiteral(Value: llvm::APSInt::get(X: -1), |
1865 | PD: cast<NonTypeTemplateParmDecl>(Val: Parm), TemplateArgType: T); |
1866 | return; |
1867 | } |
1868 | } |
1869 | } |
1870 | mangleIntegerLiteral(Value: llvm::APSInt::getUnsigned(X: 0), |
1871 | PD: cast<NonTypeTemplateParmDecl>(Val: Parm), TemplateArgType: T); |
1872 | break; |
1873 | } |
1874 | case TemplateArgument::StructuralValue: |
1875 | if (ValueDecl *D = getAsArrayToPointerDecayedDecl( |
1876 | T: TA.getStructuralValueType(), V: TA.getAsStructuralValue())) { |
1877 | // Mangle the result of array-to-pointer decay as if it were a reference |
1878 | // to the original declaration, to match MSVC's behavior. This can result |
1879 | // in mangling collisions in some cases! |
1880 | return mangleTemplateArg( |
1881 | TD, TA: TemplateArgument(D, TA.getStructuralValueType()), Parm); |
1882 | } |
1883 | Out << "$" ; |
1884 | if (cast<NonTypeTemplateParmDecl>(Val: Parm) |
1885 | ->getType() |
1886 | ->getContainedDeducedType()) { |
1887 | Out << "M" ; |
1888 | mangleType(T: TA.getNonTypeTemplateArgumentType(), Range: SourceRange(), QMM: QMM_Drop); |
1889 | } |
1890 | mangleTemplateArgValue(T: TA.getStructuralValueType(), |
1891 | V: TA.getAsStructuralValue(), |
1892 | TplArgKind::StructuralValue, |
1893 | /*WithScalarType=*/false); |
1894 | break; |
1895 | case TemplateArgument::Expression: |
1896 | mangleExpression(E: TA.getAsExpr(), PD: cast<NonTypeTemplateParmDecl>(Val: Parm)); |
1897 | break; |
1898 | case TemplateArgument::Pack: { |
1899 | ArrayRef<TemplateArgument> TemplateArgs = TA.getPackAsArray(); |
1900 | if (TemplateArgs.empty()) { |
1901 | if (isa<TemplateTypeParmDecl>(Val: Parm) || |
1902 | isa<TemplateTemplateParmDecl>(Val: Parm)) |
1903 | // MSVC 2015 changed the mangling for empty expanded template packs, |
1904 | // use the old mangling for link compatibility for old versions. |
1905 | Out << (Context.getASTContext().getLangOpts().isCompatibleWithMSVC( |
1906 | MajorVersion: LangOptions::MSVC2015) |
1907 | ? "$$V" |
1908 | : "$$$V" ); |
1909 | else if (isa<NonTypeTemplateParmDecl>(Val: Parm)) |
1910 | Out << "$S" ; |
1911 | else |
1912 | llvm_unreachable("unexpected template parameter decl!" ); |
1913 | } else { |
1914 | for (const TemplateArgument &PA : TemplateArgs) |
1915 | mangleTemplateArg(TD, TA: PA, Parm); |
1916 | } |
1917 | break; |
1918 | } |
1919 | case TemplateArgument::Template: { |
1920 | const NamedDecl *ND = |
1921 | TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl(); |
1922 | if (const auto *TD = dyn_cast<TagDecl>(Val: ND)) { |
1923 | mangleType(TD); |
1924 | } else if (isa<TypeAliasDecl>(Val: ND)) { |
1925 | Out << "$$Y" ; |
1926 | mangleName(GD: ND); |
1927 | } else { |
1928 | llvm_unreachable("unexpected template template NamedDecl!" ); |
1929 | } |
1930 | break; |
1931 | } |
1932 | } |
1933 | } |
1934 | |
1935 | void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T, |
1936 | const APValue &V, |
1937 | TplArgKind TAK, |
1938 | bool WithScalarType) { |
1939 | switch (V.getKind()) { |
1940 | case APValue::None: |
1941 | case APValue::Indeterminate: |
1942 | // FIXME: MSVC doesn't allow this, so we can't be sure how it should be |
1943 | // mangled. |
1944 | if (WithScalarType) |
1945 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
1946 | Out << '@'; |
1947 | return; |
1948 | |
1949 | case APValue::Int: |
1950 | if (WithScalarType) |
1951 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
1952 | Out << '0'; |
1953 | mangleNumber(Number: V.getInt()); |
1954 | return; |
1955 | |
1956 | case APValue::Float: |
1957 | if (WithScalarType) |
1958 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
1959 | mangleFloat(Number: V.getFloat()); |
1960 | return; |
1961 | |
1962 | case APValue::LValue: { |
1963 | if (WithScalarType) |
1964 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
1965 | |
1966 | APValue::LValueBase Base = V.getLValueBase(); |
1967 | |
1968 | // this might not cover every case but did cover issue 97756 |
1969 | // see test CodeGen/ms_mangler_templatearg_opte |
1970 | if (V.isLValueOnePastTheEnd()) { |
1971 | Out << "5E" ; |
1972 | auto *VD = Base.dyn_cast<const ValueDecl *>(); |
1973 | if (VD) |
1974 | mangle(GD: VD); |
1975 | Out << "@" ; |
1976 | return; |
1977 | } |
1978 | |
1979 | if (!V.hasLValuePath() || V.getLValuePath().empty()) { |
1980 | // Taking the address of a complete object has a special-case mangling. |
1981 | if (Base.isNull()) { |
1982 | // MSVC emits 0A@ for null pointers. Generalize this for arbitrary |
1983 | // integers cast to pointers. |
1984 | // FIXME: This mangles 0 cast to a pointer the same as a null pointer, |
1985 | // even in cases where the two are different values. |
1986 | Out << "0" ; |
1987 | mangleNumber(Number: V.getLValueOffset().getQuantity()); |
1988 | } else if (!V.hasLValuePath()) { |
1989 | // FIXME: This can only happen as an extension. Invent a mangling. |
1990 | Error(thingy: "template argument (extension not comaptible with ms mangler)" ); |
1991 | return; |
1992 | } else if (auto *VD = Base.dyn_cast<const ValueDecl*>()) { |
1993 | Out << "E" ; |
1994 | mangle(GD: VD); |
1995 | } else { |
1996 | Error(thingy: "template argument (undeclared base)" ); |
1997 | return; |
1998 | } |
1999 | } else { |
2000 | if (TAK == TplArgKind::ClassNTTP && T->isPointerType()) |
2001 | Out << "5" ; |
2002 | |
2003 | SmallVector<char, 2> EntryTypes; |
2004 | SmallVector<std::function<void()>, 2> EntryManglers; |
2005 | QualType ET = Base.getType(); |
2006 | for (APValue::LValuePathEntry E : V.getLValuePath()) { |
2007 | if (auto *AT = ET->getAsArrayTypeUnsafe()) { |
2008 | EntryTypes.push_back(Elt: 'C'); |
2009 | EntryManglers.push_back(Elt: [this, I = E.getAsArrayIndex()] { |
2010 | Out << '0'; |
2011 | mangleNumber(Number: I); |
2012 | Out << '@'; |
2013 | }); |
2014 | ET = AT->getElementType(); |
2015 | continue; |
2016 | } |
2017 | |
2018 | const Decl *D = E.getAsBaseOrMember().getPointer(); |
2019 | if (auto *FD = dyn_cast<FieldDecl>(Val: D)) { |
2020 | ET = FD->getType(); |
2021 | if (const auto *RD = ET->getAsRecordDecl()) |
2022 | if (RD->isAnonymousStructOrUnion()) |
2023 | continue; |
2024 | } else { |
2025 | ET = getASTContext().getRecordType(Decl: cast<CXXRecordDecl>(Val: D)); |
2026 | // Bug in MSVC: fully qualified name of base class should be used for |
2027 | // mangling to prevent collisions e.g. on base classes with same names |
2028 | // in different namespaces. |
2029 | } |
2030 | |
2031 | EntryTypes.push_back(Elt: '6'); |
2032 | EntryManglers.push_back(Elt: [this, D] { |
2033 | mangleUnqualifiedName(GD: cast<NamedDecl>(Val: D)); |
2034 | Out << '@'; |
2035 | }); |
2036 | } |
2037 | |
2038 | for (auto I = EntryTypes.rbegin(), E = EntryTypes.rend(); I != E; ++I) |
2039 | Out << *I; |
2040 | |
2041 | auto *VD = Base.dyn_cast<const ValueDecl*>(); |
2042 | if (!VD) { |
2043 | Error(thingy: "template argument (null value decl)" ); |
2044 | return; |
2045 | } |
2046 | Out << (TAK == TplArgKind::ClassNTTP ? 'E' : '1'); |
2047 | mangle(GD: VD); |
2048 | |
2049 | for (const std::function<void()> &Mangler : EntryManglers) |
2050 | Mangler(); |
2051 | if (TAK == TplArgKind::ClassNTTP && T->isPointerType()) |
2052 | Out << '@'; |
2053 | } |
2054 | |
2055 | return; |
2056 | } |
2057 | |
2058 | case APValue::MemberPointer: { |
2059 | if (WithScalarType) |
2060 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
2061 | |
2062 | const CXXRecordDecl *RD = |
2063 | T->castAs<MemberPointerType>()->getMostRecentCXXRecordDecl(); |
2064 | const ValueDecl *D = V.getMemberPointerDecl(); |
2065 | if (TAK == TplArgKind::ClassNTTP) { |
2066 | if (T->isMemberDataPointerType()) |
2067 | mangleMemberDataPointerInClassNTTP(RD, VD: D); |
2068 | else |
2069 | mangleMemberFunctionPointerInClassNTTP(RD, |
2070 | MD: cast_or_null<CXXMethodDecl>(Val: D)); |
2071 | } else { |
2072 | if (T->isMemberDataPointerType()) |
2073 | mangleMemberDataPointer(RD, VD: D, PD: nullptr, TemplateArgType: QualType(), Prefix: "" ); |
2074 | else |
2075 | mangleMemberFunctionPointer(RD, MD: cast_or_null<CXXMethodDecl>(Val: D), PD: nullptr, |
2076 | TemplateArgType: QualType(), Prefix: "" ); |
2077 | } |
2078 | return; |
2079 | } |
2080 | |
2081 | case APValue::Struct: { |
2082 | Out << '2'; |
2083 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
2084 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
2085 | assert(RD && "unexpected type for record value" ); |
2086 | |
2087 | unsigned BaseIndex = 0; |
2088 | for (const CXXBaseSpecifier &B : RD->bases()) |
2089 | mangleTemplateArgValue(T: B.getType(), V: V.getStructBase(i: BaseIndex++), TAK); |
2090 | for (const FieldDecl *FD : RD->fields()) |
2091 | if (!FD->isUnnamedBitField()) |
2092 | mangleTemplateArgValue(T: FD->getType(), |
2093 | V: V.getStructField(i: FD->getFieldIndex()), TAK, |
2094 | /*WithScalarType*/ true); |
2095 | Out << '@'; |
2096 | return; |
2097 | } |
2098 | |
2099 | case APValue::Union: |
2100 | Out << '7'; |
2101 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
2102 | if (const FieldDecl *FD = V.getUnionField()) { |
2103 | mangleUnqualifiedName(GD: FD); |
2104 | mangleTemplateArgValue(T: FD->getType(), V: V.getUnionValue(), TAK); |
2105 | } |
2106 | Out << '@'; |
2107 | return; |
2108 | |
2109 | case APValue::ComplexInt: |
2110 | // We mangle complex types as structs, so mangle the value as a struct too. |
2111 | Out << '2'; |
2112 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
2113 | Out << '0'; |
2114 | mangleNumber(Number: V.getComplexIntReal()); |
2115 | Out << '0'; |
2116 | mangleNumber(Number: V.getComplexIntImag()); |
2117 | Out << '@'; |
2118 | return; |
2119 | |
2120 | case APValue::ComplexFloat: |
2121 | Out << '2'; |
2122 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
2123 | mangleFloat(Number: V.getComplexFloatReal()); |
2124 | mangleFloat(Number: V.getComplexFloatImag()); |
2125 | Out << '@'; |
2126 | return; |
2127 | |
2128 | case APValue::Array: { |
2129 | Out << '3'; |
2130 | QualType ElemT = getASTContext().getAsArrayType(T)->getElementType(); |
2131 | mangleType(T: ElemT, Range: SourceRange(), QMM: QMM_Escape); |
2132 | for (unsigned I = 0, N = V.getArraySize(); I != N; ++I) { |
2133 | const APValue &ElemV = I < V.getArrayInitializedElts() |
2134 | ? V.getArrayInitializedElt(I) |
2135 | : V.getArrayFiller(); |
2136 | mangleTemplateArgValue(T: ElemT, V: ElemV, TAK); |
2137 | Out << '@'; |
2138 | } |
2139 | Out << '@'; |
2140 | return; |
2141 | } |
2142 | |
2143 | case APValue::Vector: { |
2144 | // __m128 is mangled as a struct containing an array. We follow this |
2145 | // approach for all vector types. |
2146 | Out << '2'; |
2147 | mangleType(T, Range: SourceRange(), QMM: QMM_Escape); |
2148 | Out << '3'; |
2149 | QualType ElemT = T->castAs<VectorType>()->getElementType(); |
2150 | mangleType(T: ElemT, Range: SourceRange(), QMM: QMM_Escape); |
2151 | for (unsigned I = 0, N = V.getVectorLength(); I != N; ++I) { |
2152 | const APValue &ElemV = V.getVectorElt(I); |
2153 | mangleTemplateArgValue(T: ElemT, V: ElemV, TAK); |
2154 | Out << '@'; |
2155 | } |
2156 | Out << "@@" ; |
2157 | return; |
2158 | } |
2159 | |
2160 | case APValue::AddrLabelDiff: { |
2161 | Error(thingy: "template argument (value type: address label diff)" ); |
2162 | return; |
2163 | } |
2164 | |
2165 | case APValue::FixedPoint: { |
2166 | Error(thingy: "template argument (value type: fixed point)" ); |
2167 | return; |
2168 | } |
2169 | } |
2170 | } |
2171 | |
2172 | void MicrosoftCXXNameMangler::mangleObjCProtocol(const ObjCProtocolDecl *PD) { |
2173 | llvm::SmallString<64> TemplateMangling; |
2174 | llvm::raw_svector_ostream Stream(TemplateMangling); |
2175 | MicrosoftCXXNameMangler (Context, Stream); |
2176 | |
2177 | Stream << "?$" ; |
2178 | Extra.mangleSourceName(Name: "Protocol" ); |
2179 | Extra.mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: PD->getName()); |
2180 | |
2181 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__ObjC" }); |
2182 | } |
2183 | |
2184 | void MicrosoftCXXNameMangler::mangleObjCLifetime(const QualType Type, |
2185 | Qualifiers Quals, |
2186 | SourceRange Range) { |
2187 | llvm::SmallString<64> TemplateMangling; |
2188 | llvm::raw_svector_ostream Stream(TemplateMangling); |
2189 | MicrosoftCXXNameMangler (Context, Stream); |
2190 | |
2191 | Stream << "?$" ; |
2192 | switch (Quals.getObjCLifetime()) { |
2193 | case Qualifiers::OCL_None: |
2194 | case Qualifiers::OCL_ExplicitNone: |
2195 | break; |
2196 | case Qualifiers::OCL_Autoreleasing: |
2197 | Extra.mangleSourceName(Name: "Autoreleasing" ); |
2198 | break; |
2199 | case Qualifiers::OCL_Strong: |
2200 | Extra.mangleSourceName(Name: "Strong" ); |
2201 | break; |
2202 | case Qualifiers::OCL_Weak: |
2203 | Extra.mangleSourceName(Name: "Weak" ); |
2204 | break; |
2205 | } |
2206 | Extra.manglePointerCVQualifiers(Quals); |
2207 | Extra.manglePointerExtQualifiers(Quals, PointeeType: Type); |
2208 | Extra.mangleType(T: Type, Range); |
2209 | |
2210 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__ObjC" }); |
2211 | } |
2212 | |
2213 | void MicrosoftCXXNameMangler::mangleObjCKindOfType(const ObjCObjectType *T, |
2214 | Qualifiers Quals, |
2215 | SourceRange Range) { |
2216 | llvm::SmallString<64> TemplateMangling; |
2217 | llvm::raw_svector_ostream Stream(TemplateMangling); |
2218 | MicrosoftCXXNameMangler (Context, Stream); |
2219 | |
2220 | Stream << "?$" ; |
2221 | Extra.mangleSourceName(Name: "KindOf" ); |
2222 | Extra.mangleType(T: QualType(T, 0) |
2223 | .stripObjCKindOfType(ctx: getASTContext()) |
2224 | ->castAs<ObjCObjectType>(), |
2225 | Quals, Range); |
2226 | |
2227 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__ObjC" }); |
2228 | } |
2229 | |
2230 | void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, |
2231 | bool IsMember) { |
2232 | // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> |
2233 | // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); |
2234 | // 'I' means __restrict (32/64-bit). |
2235 | // Note that the MSVC __restrict keyword isn't the same as the C99 restrict |
2236 | // keyword! |
2237 | // <base-cvr-qualifiers> ::= A # near |
2238 | // ::= B # near const |
2239 | // ::= C # near volatile |
2240 | // ::= D # near const volatile |
2241 | // ::= E # far (16-bit) |
2242 | // ::= F # far const (16-bit) |
2243 | // ::= G # far volatile (16-bit) |
2244 | // ::= H # far const volatile (16-bit) |
2245 | // ::= I # huge (16-bit) |
2246 | // ::= J # huge const (16-bit) |
2247 | // ::= K # huge volatile (16-bit) |
2248 | // ::= L # huge const volatile (16-bit) |
2249 | // ::= M <basis> # based |
2250 | // ::= N <basis> # based const |
2251 | // ::= O <basis> # based volatile |
2252 | // ::= P <basis> # based const volatile |
2253 | // ::= Q # near member |
2254 | // ::= R # near const member |
2255 | // ::= S # near volatile member |
2256 | // ::= T # near const volatile member |
2257 | // ::= U # far member (16-bit) |
2258 | // ::= V # far const member (16-bit) |
2259 | // ::= W # far volatile member (16-bit) |
2260 | // ::= X # far const volatile member (16-bit) |
2261 | // ::= Y # huge member (16-bit) |
2262 | // ::= Z # huge const member (16-bit) |
2263 | // ::= 0 # huge volatile member (16-bit) |
2264 | // ::= 1 # huge const volatile member (16-bit) |
2265 | // ::= 2 <basis> # based member |
2266 | // ::= 3 <basis> # based const member |
2267 | // ::= 4 <basis> # based volatile member |
2268 | // ::= 5 <basis> # based const volatile member |
2269 | // ::= 6 # near function (pointers only) |
2270 | // ::= 7 # far function (pointers only) |
2271 | // ::= 8 # near method (pointers only) |
2272 | // ::= 9 # far method (pointers only) |
2273 | // ::= _A <basis> # based function (pointers only) |
2274 | // ::= _B <basis> # based function (far?) (pointers only) |
2275 | // ::= _C <basis> # based method (pointers only) |
2276 | // ::= _D <basis> # based method (far?) (pointers only) |
2277 | // ::= _E # block (Clang) |
2278 | // <basis> ::= 0 # __based(void) |
2279 | // ::= 1 # __based(segment)? |
2280 | // ::= 2 <name> # __based(name) |
2281 | // ::= 3 # ? |
2282 | // ::= 4 # ? |
2283 | // ::= 5 # not really based |
2284 | bool HasConst = Quals.hasConst(), |
2285 | HasVolatile = Quals.hasVolatile(); |
2286 | |
2287 | if (!IsMember) { |
2288 | if (HasConst && HasVolatile) { |
2289 | Out << 'D'; |
2290 | } else if (HasVolatile) { |
2291 | Out << 'C'; |
2292 | } else if (HasConst) { |
2293 | Out << 'B'; |
2294 | } else { |
2295 | Out << 'A'; |
2296 | } |
2297 | } else { |
2298 | if (HasConst && HasVolatile) { |
2299 | Out << 'T'; |
2300 | } else if (HasVolatile) { |
2301 | Out << 'S'; |
2302 | } else if (HasConst) { |
2303 | Out << 'R'; |
2304 | } else { |
2305 | Out << 'Q'; |
2306 | } |
2307 | } |
2308 | |
2309 | // FIXME: For now, just drop all extension qualifiers on the floor. |
2310 | } |
2311 | |
2312 | void |
2313 | MicrosoftCXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) { |
2314 | // <ref-qualifier> ::= G # lvalue reference |
2315 | // ::= H # rvalue-reference |
2316 | switch (RefQualifier) { |
2317 | case RQ_None: |
2318 | break; |
2319 | |
2320 | case RQ_LValue: |
2321 | Out << 'G'; |
2322 | break; |
2323 | |
2324 | case RQ_RValue: |
2325 | Out << 'H'; |
2326 | break; |
2327 | } |
2328 | } |
2329 | |
2330 | void MicrosoftCXXNameMangler::manglePointerExtQualifiers(Qualifiers Quals, |
2331 | QualType PointeeType) { |
2332 | // Check if this is a default 64-bit pointer or has __ptr64 qualifier. |
2333 | bool is64Bit = PointeeType.isNull() ? PointersAre64Bit : |
2334 | is64BitPointer(Quals: PointeeType.getQualifiers()); |
2335 | if (is64Bit && (PointeeType.isNull() || !PointeeType->isFunctionType())) |
2336 | Out << 'E'; |
2337 | |
2338 | if (Quals.hasRestrict()) |
2339 | Out << 'I'; |
2340 | |
2341 | if (Quals.hasUnaligned() || |
2342 | (!PointeeType.isNull() && PointeeType.getLocalQualifiers().hasUnaligned())) |
2343 | Out << 'F'; |
2344 | } |
2345 | |
2346 | void MicrosoftCXXNameMangler::manglePointerAuthQualifier(Qualifiers Quals) { |
2347 | PointerAuthQualifier PointerAuth = Quals.getPointerAuth(); |
2348 | if (!PointerAuth) |
2349 | return; |
2350 | |
2351 | Out << "__ptrauth" ; |
2352 | mangleNumber(Number: PointerAuth.getKey()); |
2353 | mangleNumber(Number: PointerAuth.isAddressDiscriminated()); |
2354 | mangleNumber(Number: PointerAuth.getExtraDiscriminator()); |
2355 | } |
2356 | |
2357 | void MicrosoftCXXNameMangler::manglePointerCVQualifiers(Qualifiers Quals) { |
2358 | // <pointer-cv-qualifiers> ::= P # no qualifiers |
2359 | // ::= Q # const |
2360 | // ::= R # volatile |
2361 | // ::= S # const volatile |
2362 | bool HasConst = Quals.hasConst(), |
2363 | HasVolatile = Quals.hasVolatile(); |
2364 | |
2365 | if (HasConst && HasVolatile) { |
2366 | Out << 'S'; |
2367 | } else if (HasVolatile) { |
2368 | Out << 'R'; |
2369 | } else if (HasConst) { |
2370 | Out << 'Q'; |
2371 | } else { |
2372 | Out << 'P'; |
2373 | } |
2374 | } |
2375 | |
2376 | void MicrosoftCXXNameMangler::mangleFunctionArgumentType(QualType T, |
2377 | SourceRange Range) { |
2378 | // MSVC will backreference two canonically equivalent types that have slightly |
2379 | // different manglings when mangled alone. |
2380 | |
2381 | // Decayed types do not match up with non-decayed versions of the same type. |
2382 | // |
2383 | // e.g. |
2384 | // void (*x)(void) will not form a backreference with void x(void) |
2385 | void *TypePtr; |
2386 | if (const auto *DT = T->getAs<DecayedType>()) { |
2387 | QualType OriginalType = DT->getOriginalType(); |
2388 | // All decayed ArrayTypes should be treated identically; as-if they were |
2389 | // a decayed IncompleteArrayType. |
2390 | if (const auto *AT = getASTContext().getAsArrayType(T: OriginalType)) |
2391 | OriginalType = getASTContext().getIncompleteArrayType( |
2392 | EltTy: AT->getElementType(), ASM: AT->getSizeModifier(), |
2393 | IndexTypeQuals: AT->getIndexTypeCVRQualifiers()); |
2394 | |
2395 | TypePtr = OriginalType.getCanonicalType().getAsOpaquePtr(); |
2396 | // If the original parameter was textually written as an array, |
2397 | // instead treat the decayed parameter like it's const. |
2398 | // |
2399 | // e.g. |
2400 | // int [] -> int * const |
2401 | if (OriginalType->isArrayType()) |
2402 | T = T.withConst(); |
2403 | } else { |
2404 | TypePtr = T.getCanonicalType().getAsOpaquePtr(); |
2405 | } |
2406 | |
2407 | ArgBackRefMap::iterator Found = FunArgBackReferences.find(Val: TypePtr); |
2408 | |
2409 | if (Found == FunArgBackReferences.end()) { |
2410 | size_t OutSizeBefore = Out.tell(); |
2411 | |
2412 | mangleType(T, Range, QMM: QMM_Drop); |
2413 | |
2414 | // See if it's worth creating a back reference. |
2415 | // Only types longer than 1 character are considered |
2416 | // and only 10 back references slots are available: |
2417 | bool LongerThanOneChar = (Out.tell() - OutSizeBefore > 1); |
2418 | if (LongerThanOneChar && FunArgBackReferences.size() < 10) { |
2419 | size_t Size = FunArgBackReferences.size(); |
2420 | FunArgBackReferences[TypePtr] = Size; |
2421 | } |
2422 | } else { |
2423 | Out << Found->second; |
2424 | } |
2425 | } |
2426 | |
2427 | void MicrosoftCXXNameMangler::manglePassObjectSizeArg( |
2428 | const PassObjectSizeAttr *POSA) { |
2429 | int Type = POSA->getType(); |
2430 | bool Dynamic = POSA->isDynamic(); |
2431 | |
2432 | auto Iter = PassObjectSizeArgs.insert(x: {Type, Dynamic}).first; |
2433 | auto *TypePtr = (const void *)&*Iter; |
2434 | ArgBackRefMap::iterator Found = FunArgBackReferences.find(Val: TypePtr); |
2435 | |
2436 | if (Found == FunArgBackReferences.end()) { |
2437 | std::string Name = |
2438 | Dynamic ? "__pass_dynamic_object_size" : "__pass_object_size" ; |
2439 | mangleArtificialTagType(TK: TagTypeKind::Enum, UnqualifiedName: Name + llvm::utostr(X: Type), |
2440 | NestedNames: {"__clang" }); |
2441 | |
2442 | if (FunArgBackReferences.size() < 10) { |
2443 | size_t Size = FunArgBackReferences.size(); |
2444 | FunArgBackReferences[TypePtr] = Size; |
2445 | } |
2446 | } else { |
2447 | Out << Found->second; |
2448 | } |
2449 | } |
2450 | |
2451 | void MicrosoftCXXNameMangler::mangleAddressSpaceType(QualType T, |
2452 | Qualifiers Quals, |
2453 | SourceRange Range) { |
2454 | // Address space is mangled as an unqualified templated type in the __clang |
2455 | // namespace. The demangled version of this is: |
2456 | // In the case of a language specific address space: |
2457 | // __clang::struct _AS[language_addr_space]<Type> |
2458 | // where: |
2459 | // <language_addr_space> ::= <OpenCL-addrspace> | <CUDA-addrspace> |
2460 | // <OpenCL-addrspace> ::= "CL" [ "global" | "local" | "constant" | |
2461 | // "private"| "generic" | "device" | "host" ] |
2462 | // <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ] |
2463 | // Note that the above were chosen to match the Itanium mangling for this. |
2464 | // |
2465 | // In the case of a non-language specific address space: |
2466 | // __clang::struct _AS<TargetAS, Type> |
2467 | assert(Quals.hasAddressSpace() && "Not valid without address space" ); |
2468 | llvm::SmallString<32> ASMangling; |
2469 | llvm::raw_svector_ostream Stream(ASMangling); |
2470 | MicrosoftCXXNameMangler (Context, Stream); |
2471 | Stream << "?$" ; |
2472 | |
2473 | LangAS AS = Quals.getAddressSpace(); |
2474 | if (Context.getASTContext().addressSpaceMapManglingFor(AS)) { |
2475 | unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS); |
2476 | Extra.mangleSourceName(Name: "_AS" ); |
2477 | Extra.mangleIntegerLiteral(Value: llvm::APSInt::getUnsigned(X: TargetAS)); |
2478 | } else { |
2479 | switch (AS) { |
2480 | default: |
2481 | llvm_unreachable("Not a language specific address space" ); |
2482 | case LangAS::opencl_global: |
2483 | Extra.mangleSourceName(Name: "_ASCLglobal" ); |
2484 | break; |
2485 | case LangAS::opencl_global_device: |
2486 | Extra.mangleSourceName(Name: "_ASCLdevice" ); |
2487 | break; |
2488 | case LangAS::opencl_global_host: |
2489 | Extra.mangleSourceName(Name: "_ASCLhost" ); |
2490 | break; |
2491 | case LangAS::opencl_local: |
2492 | Extra.mangleSourceName(Name: "_ASCLlocal" ); |
2493 | break; |
2494 | case LangAS::opencl_constant: |
2495 | Extra.mangleSourceName(Name: "_ASCLconstant" ); |
2496 | break; |
2497 | case LangAS::opencl_private: |
2498 | Extra.mangleSourceName(Name: "_ASCLprivate" ); |
2499 | break; |
2500 | case LangAS::opencl_generic: |
2501 | Extra.mangleSourceName(Name: "_ASCLgeneric" ); |
2502 | break; |
2503 | case LangAS::cuda_device: |
2504 | Extra.mangleSourceName(Name: "_ASCUdevice" ); |
2505 | break; |
2506 | case LangAS::cuda_constant: |
2507 | Extra.mangleSourceName(Name: "_ASCUconstant" ); |
2508 | break; |
2509 | case LangAS::cuda_shared: |
2510 | Extra.mangleSourceName(Name: "_ASCUshared" ); |
2511 | break; |
2512 | case LangAS::ptr32_sptr: |
2513 | case LangAS::ptr32_uptr: |
2514 | case LangAS::ptr64: |
2515 | llvm_unreachable("don't mangle ptr address spaces with _AS" ); |
2516 | } |
2517 | } |
2518 | |
2519 | Extra.mangleType(T, Range, QMM: QMM_Escape); |
2520 | mangleQualifiers(Quals: Qualifiers(), IsMember: false); |
2521 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: ASMangling, NestedNames: {"__clang" }); |
2522 | } |
2523 | |
2524 | void MicrosoftCXXNameMangler::mangleAutoReturnType(QualType T, |
2525 | QualifierMangleMode QMM) { |
2526 | assert(getASTContext().getLangOpts().isCompatibleWithMSVC( |
2527 | LangOptions::MSVC2019) && |
2528 | "Cannot mangle MSVC 2017 auto return types!" ); |
2529 | |
2530 | if (isa<AutoType>(Val: T)) { |
2531 | const auto *AT = T->getContainedAutoType(); |
2532 | Qualifiers Quals = T.getLocalQualifiers(); |
2533 | |
2534 | if (QMM == QMM_Result) |
2535 | Out << '?'; |
2536 | if (QMM != QMM_Drop) |
2537 | mangleQualifiers(Quals, IsMember: false); |
2538 | Out << (AT->isDecltypeAuto() ? "_T" : "_P" ); |
2539 | return; |
2540 | } |
2541 | |
2542 | T = T.getDesugaredType(Context: getASTContext()); |
2543 | Qualifiers Quals = T.getLocalQualifiers(); |
2544 | |
2545 | switch (QMM) { |
2546 | case QMM_Drop: |
2547 | case QMM_Result: |
2548 | break; |
2549 | case QMM_Mangle: |
2550 | mangleQualifiers(Quals, IsMember: false); |
2551 | break; |
2552 | default: |
2553 | llvm_unreachable("QMM_Escape unexpected" ); |
2554 | } |
2555 | |
2556 | const Type *ty = T.getTypePtr(); |
2557 | switch (ty->getTypeClass()) { |
2558 | case Type::MemberPointer: |
2559 | mangleAutoReturnType(T: cast<MemberPointerType>(Val: ty), Quals); |
2560 | break; |
2561 | case Type::Pointer: |
2562 | mangleAutoReturnType(T: cast<PointerType>(Val: ty), Quals); |
2563 | break; |
2564 | case Type::LValueReference: |
2565 | mangleAutoReturnType(T: cast<LValueReferenceType>(Val: ty), Quals); |
2566 | break; |
2567 | case Type::RValueReference: |
2568 | mangleAutoReturnType(T: cast<RValueReferenceType>(Val: ty), Quals); |
2569 | break; |
2570 | default: |
2571 | llvm_unreachable("Invalid type expected" ); |
2572 | } |
2573 | } |
2574 | |
2575 | void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range, |
2576 | QualifierMangleMode QMM) { |
2577 | // Don't use the canonical types. MSVC includes things like 'const' on |
2578 | // pointer arguments to function pointers that canonicalization strips away. |
2579 | T = T.getDesugaredType(Context: getASTContext()); |
2580 | Qualifiers Quals = T.getLocalQualifiers(); |
2581 | |
2582 | if (const ArrayType *AT = getASTContext().getAsArrayType(T)) { |
2583 | // If there were any Quals, getAsArrayType() pushed them onto the array |
2584 | // element type. |
2585 | if (QMM == QMM_Mangle) |
2586 | Out << 'A'; |
2587 | else if (QMM == QMM_Escape || QMM == QMM_Result) |
2588 | Out << "$$B" ; |
2589 | mangleArrayType(T: AT); |
2590 | return; |
2591 | } |
2592 | |
2593 | bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() || |
2594 | T->isReferenceType() || T->isBlockPointerType(); |
2595 | |
2596 | switch (QMM) { |
2597 | case QMM_Drop: |
2598 | if (Quals.hasObjCLifetime()) |
2599 | Quals = Quals.withoutObjCLifetime(); |
2600 | break; |
2601 | case QMM_Mangle: |
2602 | if (const FunctionType *FT = dyn_cast<FunctionType>(Val&: T)) { |
2603 | Out << '6'; |
2604 | mangleFunctionType(T: FT); |
2605 | return; |
2606 | } |
2607 | mangleQualifiers(Quals, IsMember: false); |
2608 | break; |
2609 | case QMM_Escape: |
2610 | if (!IsPointer && Quals) { |
2611 | Out << "$$C" ; |
2612 | mangleQualifiers(Quals, IsMember: false); |
2613 | } |
2614 | break; |
2615 | case QMM_Result: |
2616 | // Presence of __unaligned qualifier shouldn't affect mangling here. |
2617 | Quals.removeUnaligned(); |
2618 | if (Quals.hasObjCLifetime()) |
2619 | Quals = Quals.withoutObjCLifetime(); |
2620 | if ((!IsPointer && Quals) || isa<TagType>(Val: T) || isArtificialTagType(T)) { |
2621 | Out << '?'; |
2622 | mangleQualifiers(Quals, IsMember: false); |
2623 | } |
2624 | break; |
2625 | } |
2626 | |
2627 | const Type *ty = T.getTypePtr(); |
2628 | |
2629 | switch (ty->getTypeClass()) { |
2630 | #define ABSTRACT_TYPE(CLASS, PARENT) |
2631 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
2632 | case Type::CLASS: \ |
2633 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
2634 | return; |
2635 | #define TYPE(CLASS, PARENT) \ |
2636 | case Type::CLASS: \ |
2637 | mangleType(cast<CLASS##Type>(ty), Quals, Range); \ |
2638 | break; |
2639 | #include "clang/AST/TypeNodes.inc" |
2640 | #undef ABSTRACT_TYPE |
2641 | #undef NON_CANONICAL_TYPE |
2642 | #undef TYPE |
2643 | } |
2644 | } |
2645 | |
2646 | void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, Qualifiers, |
2647 | SourceRange Range) { |
2648 | // <type> ::= <builtin-type> |
2649 | // <builtin-type> ::= X # void |
2650 | // ::= C # signed char |
2651 | // ::= D # char |
2652 | // ::= E # unsigned char |
2653 | // ::= F # short |
2654 | // ::= G # unsigned short (or wchar_t if it's not a builtin) |
2655 | // ::= H # int |
2656 | // ::= I # unsigned int |
2657 | // ::= J # long |
2658 | // ::= K # unsigned long |
2659 | // L # <none> |
2660 | // ::= M # float |
2661 | // ::= N # double |
2662 | // ::= O # long double (__float80 is mangled differently) |
2663 | // ::= _J # long long, __int64 |
2664 | // ::= _K # unsigned long long, __int64 |
2665 | // ::= _L # __int128 |
2666 | // ::= _M # unsigned __int128 |
2667 | // ::= _N # bool |
2668 | // _O # <array in parameter> |
2669 | // ::= _Q # char8_t |
2670 | // ::= _S # char16_t |
2671 | // ::= _T # __float80 (Intel) |
2672 | // ::= _U # char32_t |
2673 | // ::= _W # wchar_t |
2674 | // ::= _Z # __float80 (Digital Mars) |
2675 | switch (T->getKind()) { |
2676 | case BuiltinType::Void: |
2677 | Out << 'X'; |
2678 | break; |
2679 | case BuiltinType::SChar: |
2680 | Out << 'C'; |
2681 | break; |
2682 | case BuiltinType::Char_U: |
2683 | case BuiltinType::Char_S: |
2684 | Out << 'D'; |
2685 | break; |
2686 | case BuiltinType::UChar: |
2687 | Out << 'E'; |
2688 | break; |
2689 | case BuiltinType::Short: |
2690 | Out << 'F'; |
2691 | break; |
2692 | case BuiltinType::UShort: |
2693 | Out << 'G'; |
2694 | break; |
2695 | case BuiltinType::Int: |
2696 | Out << 'H'; |
2697 | break; |
2698 | case BuiltinType::UInt: |
2699 | Out << 'I'; |
2700 | break; |
2701 | case BuiltinType::Long: |
2702 | Out << 'J'; |
2703 | break; |
2704 | case BuiltinType::ULong: |
2705 | Out << 'K'; |
2706 | break; |
2707 | case BuiltinType::Float: |
2708 | Out << 'M'; |
2709 | break; |
2710 | case BuiltinType::Double: |
2711 | Out << 'N'; |
2712 | break; |
2713 | // TODO: Determine size and mangle accordingly |
2714 | case BuiltinType::LongDouble: |
2715 | Out << 'O'; |
2716 | break; |
2717 | case BuiltinType::LongLong: |
2718 | Out << "_J" ; |
2719 | break; |
2720 | case BuiltinType::ULongLong: |
2721 | Out << "_K" ; |
2722 | break; |
2723 | case BuiltinType::Int128: |
2724 | Out << "_L" ; |
2725 | break; |
2726 | case BuiltinType::UInt128: |
2727 | Out << "_M" ; |
2728 | break; |
2729 | case BuiltinType::Bool: |
2730 | Out << "_N" ; |
2731 | break; |
2732 | case BuiltinType::Char8: |
2733 | Out << "_Q" ; |
2734 | break; |
2735 | case BuiltinType::Char16: |
2736 | Out << "_S" ; |
2737 | break; |
2738 | case BuiltinType::Char32: |
2739 | Out << "_U" ; |
2740 | break; |
2741 | case BuiltinType::WChar_S: |
2742 | case BuiltinType::WChar_U: |
2743 | Out << "_W" ; |
2744 | break; |
2745 | |
2746 | #define BUILTIN_TYPE(Id, SingletonId) |
2747 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
2748 | case BuiltinType::Id: |
2749 | #include "clang/AST/BuiltinTypes.def" |
2750 | case BuiltinType::Dependent: |
2751 | llvm_unreachable("placeholder types shouldn't get to name mangling" ); |
2752 | |
2753 | case BuiltinType::ObjCId: |
2754 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "objc_object" ); |
2755 | break; |
2756 | case BuiltinType::ObjCClass: |
2757 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "objc_class" ); |
2758 | break; |
2759 | case BuiltinType::ObjCSel: |
2760 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "objc_selector" ); |
2761 | break; |
2762 | |
2763 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2764 | case BuiltinType::Id: \ |
2765 | Out << "PAUocl_" #ImgType "_" #Suffix "@@"; \ |
2766 | break; |
2767 | #include "clang/Basic/OpenCLImageTypes.def" |
2768 | case BuiltinType::OCLSampler: |
2769 | Out << "PA" ; |
2770 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "ocl_sampler" ); |
2771 | break; |
2772 | case BuiltinType::OCLEvent: |
2773 | Out << "PA" ; |
2774 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "ocl_event" ); |
2775 | break; |
2776 | case BuiltinType::OCLClkEvent: |
2777 | Out << "PA" ; |
2778 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "ocl_clkevent" ); |
2779 | break; |
2780 | case BuiltinType::OCLQueue: |
2781 | Out << "PA" ; |
2782 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "ocl_queue" ); |
2783 | break; |
2784 | case BuiltinType::OCLReserveID: |
2785 | Out << "PA" ; |
2786 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "ocl_reserveid" ); |
2787 | break; |
2788 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2789 | case BuiltinType::Id: \ |
2790 | mangleArtificialTagType(TagTypeKind::Struct, "ocl_" #ExtType); \ |
2791 | break; |
2792 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2793 | |
2794 | case BuiltinType::NullPtr: |
2795 | Out << "$$T" ; |
2796 | break; |
2797 | |
2798 | case BuiltinType::Float16: |
2799 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "_Float16" , NestedNames: {"__clang" }); |
2800 | break; |
2801 | |
2802 | case BuiltinType::Half: |
2803 | if (!getASTContext().getLangOpts().HLSL) |
2804 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "_Half" , NestedNames: {"__clang" }); |
2805 | else if (getASTContext().getLangOpts().NativeHalfType) |
2806 | Out << "$f16@" ; |
2807 | else |
2808 | Out << "$halff@" ; |
2809 | break; |
2810 | |
2811 | case BuiltinType::BFloat16: |
2812 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "__bf16" , NestedNames: {"__clang" }); |
2813 | break; |
2814 | |
2815 | case BuiltinType::MFloat8: |
2816 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: "__mfp8" , NestedNames: {"__clang" }); |
2817 | break; |
2818 | |
2819 | #define WASM_REF_TYPE(InternalName, MangledName, Id, SingletonId, AS) \ |
2820 | case BuiltinType::Id: \ |
2821 | mangleArtificialTagType(TagTypeKind::Struct, MangledName); \ |
2822 | mangleArtificialTagType(TagTypeKind::Struct, MangledName, {"__clang"}); \ |
2823 | break; |
2824 | |
2825 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
2826 | |
2827 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ |
2828 | case BuiltinType::Id: \ |
2829 | mangleArtificialTagType(TagTypeKind::Struct, #Name); \ |
2830 | break; |
2831 | #include "clang/Basic/HLSLIntangibleTypes.def" |
2832 | |
2833 | #define SVE_TYPE(Name, Id, SingletonId) \ |
2834 | case BuiltinType::Id: \ |
2835 | mangleArtificialTagType(TagTypeKind::Struct, #Name, {"__clang"}); \ |
2836 | break; |
2837 | #define SVE_SCALAR_TYPE(Name, MangledName, Id, SingletonId, Bits) |
2838 | #include "clang/Basic/AArch64ACLETypes.def" |
2839 | |
2840 | // Issue an error for any type not explicitly handled. |
2841 | default: |
2842 | Error(loc: Range.getBegin(), thing1: "built-in type: " , |
2843 | thing2: T->getName(Policy: Context.getASTContext().getPrintingPolicy())) |
2844 | << Range; |
2845 | break; |
2846 | } |
2847 | } |
2848 | |
2849 | // <type> ::= <function-type> |
2850 | void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T, Qualifiers, |
2851 | SourceRange) { |
2852 | // Structors only appear in decls, so at this point we know it's not a |
2853 | // structor type. |
2854 | // FIXME: This may not be lambda-friendly. |
2855 | if (T->getMethodQuals() || T->getRefQualifier() != RQ_None) { |
2856 | Out << "$$A8@@" ; |
2857 | mangleFunctionType(T, /*D=*/nullptr, /*ForceThisQuals=*/true); |
2858 | } else { |
2859 | Out << "$$A6" ; |
2860 | mangleFunctionType(T); |
2861 | } |
2862 | } |
2863 | void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T, |
2864 | Qualifiers, SourceRange) { |
2865 | Out << "$$A6" ; |
2866 | mangleFunctionType(T); |
2867 | } |
2868 | |
2869 | void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T, |
2870 | const FunctionDecl *D, |
2871 | bool ForceThisQuals, |
2872 | bool MangleExceptionSpec) { |
2873 | // <function-type> ::= <this-cvr-qualifiers> <calling-convention> |
2874 | // <return-type> <argument-list> <throw-spec> |
2875 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(Val: T); |
2876 | |
2877 | SourceRange Range; |
2878 | if (D) Range = D->getSourceRange(); |
2879 | |
2880 | bool IsInLambda = false; |
2881 | bool IsStructor = false, HasThisQuals = ForceThisQuals, IsCtorClosure = false; |
2882 | CallingConv CC = T->getCallConv(); |
2883 | if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Val: D)) { |
2884 | if (MD->getParent()->isLambda()) |
2885 | IsInLambda = true; |
2886 | if (MD->isImplicitObjectMemberFunction()) |
2887 | HasThisQuals = true; |
2888 | if (isa<CXXDestructorDecl>(Val: MD)) { |
2889 | IsStructor = true; |
2890 | } else if (isa<CXXConstructorDecl>(Val: MD)) { |
2891 | IsStructor = true; |
2892 | IsCtorClosure = (StructorType == Ctor_CopyingClosure || |
2893 | StructorType == Ctor_DefaultClosure) && |
2894 | isStructorDecl(ND: MD); |
2895 | if (IsCtorClosure) |
2896 | CC = getASTContext().getDefaultCallingConvention( |
2897 | /*IsVariadic=*/false, /*IsCXXMethod=*/true); |
2898 | } |
2899 | } |
2900 | |
2901 | // If this is a C++ instance method, mangle the CVR qualifiers for the |
2902 | // this pointer. |
2903 | if (HasThisQuals) { |
2904 | Qualifiers Quals = Proto->getMethodQuals(); |
2905 | manglePointerExtQualifiers(Quals, /*PointeeType=*/QualType()); |
2906 | mangleRefQualifier(RefQualifier: Proto->getRefQualifier()); |
2907 | mangleQualifiers(Quals, /*IsMember=*/false); |
2908 | } |
2909 | |
2910 | mangleCallingConvention(CC, Range); |
2911 | |
2912 | // <return-type> ::= <type> |
2913 | // ::= @ # structors (they have no declared return type) |
2914 | if (IsStructor) { |
2915 | if (isa<CXXDestructorDecl>(Val: D) && isStructorDecl(ND: D)) { |
2916 | // The scalar deleting destructor takes an extra int argument which is not |
2917 | // reflected in the AST. |
2918 | if (StructorType == Dtor_Deleting) { |
2919 | Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z" ); |
2920 | return; |
2921 | } |
2922 | // The vbase destructor returns void which is not reflected in the AST. |
2923 | if (StructorType == Dtor_Complete) { |
2924 | Out << "XXZ" ; |
2925 | return; |
2926 | } |
2927 | } |
2928 | if (IsCtorClosure) { |
2929 | // Default constructor closure and copy constructor closure both return |
2930 | // void. |
2931 | Out << 'X'; |
2932 | |
2933 | if (StructorType == Ctor_DefaultClosure) { |
2934 | // Default constructor closure always has no arguments. |
2935 | Out << 'X'; |
2936 | } else if (StructorType == Ctor_CopyingClosure) { |
2937 | // Copy constructor closure always takes an unqualified reference. |
2938 | mangleFunctionArgumentType(T: getASTContext().getLValueReferenceType( |
2939 | T: Proto->getParamType(i: 0) |
2940 | ->castAs<LValueReferenceType>() |
2941 | ->getPointeeType(), |
2942 | /*SpelledAsLValue=*/true), |
2943 | Range); |
2944 | Out << '@'; |
2945 | } else { |
2946 | llvm_unreachable("unexpected constructor closure!" ); |
2947 | } |
2948 | Out << 'Z'; |
2949 | return; |
2950 | } |
2951 | Out << '@'; |
2952 | } else if (IsInLambda && isa_and_nonnull<CXXConversionDecl>(Val: D)) { |
2953 | // The only lambda conversion operators are to function pointers, which |
2954 | // can differ by their calling convention and are typically deduced. So |
2955 | // we make sure that this type gets mangled properly. |
2956 | mangleType(T: T->getReturnType(), Range, QMM: QMM_Result); |
2957 | } else { |
2958 | QualType ResultType = T->getReturnType(); |
2959 | if (IsInLambda && isa<CXXConversionDecl>(Val: D)) { |
2960 | // The only lambda conversion operators are to function pointers, which |
2961 | // can differ by their calling convention and are typically deduced. So |
2962 | // we make sure that this type gets mangled properly. |
2963 | mangleType(T: ResultType, Range, QMM: QMM_Result); |
2964 | } else if (IsInLambda) { |
2965 | if (const auto *AT = ResultType->getContainedAutoType()) { |
2966 | assert(AT->getKeyword() != AutoTypeKeyword::GNUAutoType && |
2967 | "shouldn't need to mangle __auto_type!" ); |
2968 | Out << '?'; |
2969 | mangleQualifiers(Quals: ResultType.getLocalQualifiers(), /*IsMember=*/false); |
2970 | Out << '?'; |
2971 | mangleSourceName(Name: AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>" ); |
2972 | Out << '@'; |
2973 | } else { |
2974 | Out << '@'; |
2975 | } |
2976 | } else if (const auto *AT = ResultType->getContainedAutoType()) { |
2977 | assert(AT->getKeyword() != AutoTypeKeyword::GNUAutoType && |
2978 | "shouldn't need to mangle __auto_type!" ); |
2979 | |
2980 | // If we have any pointer types with the clang address space extension |
2981 | // then defer to the custom clang mangling to keep backwards |
2982 | // compatibility. See `mangleType(const PointerType *T, Qualifiers Quals, |
2983 | // SourceRange Range)` for details. |
2984 | auto UseClangMangling = [](QualType ResultType) { |
2985 | QualType T = ResultType; |
2986 | while (isa<PointerType>(Val: T.getTypePtr())) { |
2987 | T = T->getPointeeType(); |
2988 | if (T.getQualifiers().hasAddressSpace()) |
2989 | return true; |
2990 | } |
2991 | return false; |
2992 | }; |
2993 | |
2994 | if (getASTContext().getLangOpts().isCompatibleWithMSVC( |
2995 | MajorVersion: LangOptions::MSVC2019) && |
2996 | !UseClangMangling(ResultType)) { |
2997 | if (D && !D->getPrimaryTemplate()) { |
2998 | Out << '@'; |
2999 | } else { |
3000 | if (D && D->getPrimaryTemplate()) { |
3001 | const FunctionProtoType *FPT = D->getPrimaryTemplate() |
3002 | ->getTemplatedDecl() |
3003 | ->getFirstDecl() |
3004 | ->getType() |
3005 | ->castAs<FunctionProtoType>(); |
3006 | ResultType = FPT->getReturnType(); |
3007 | } |
3008 | mangleAutoReturnType(T: ResultType, QMM: QMM_Result); |
3009 | } |
3010 | } else { |
3011 | Out << '?'; |
3012 | mangleQualifiers(Quals: ResultType.getLocalQualifiers(), /*IsMember=*/false); |
3013 | Out << '?'; |
3014 | mangleSourceName(Name: AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>" ); |
3015 | Out << '@'; |
3016 | } |
3017 | } else { |
3018 | if (ResultType->isVoidType()) |
3019 | ResultType = ResultType.getUnqualifiedType(); |
3020 | mangleType(T: ResultType, Range, QMM: QMM_Result); |
3021 | } |
3022 | } |
3023 | |
3024 | // <argument-list> ::= X # void |
3025 | // ::= <type>+ @ |
3026 | // ::= <type>* Z # varargs |
3027 | if (!Proto) { |
3028 | // Function types without prototypes can arise when mangling a function type |
3029 | // within an overloadable function in C. We mangle these as the absence of |
3030 | // any parameter types (not even an empty parameter list). |
3031 | Out << '@'; |
3032 | } else if (Proto->getNumParams() == 0 && !Proto->isVariadic()) { |
3033 | Out << 'X'; |
3034 | } else { |
3035 | // Happens for function pointer type arguments for example. |
3036 | for (unsigned I = 0, E = Proto->getNumParams(); I != E; ++I) { |
3037 | // Explicit object parameters are prefixed by "_V". |
3038 | if (I == 0 && D && D->getParamDecl(i: I)->isExplicitObjectParameter()) |
3039 | Out << "_V" ; |
3040 | |
3041 | mangleFunctionArgumentType(T: Proto->getParamType(i: I), Range); |
3042 | // Mangle each pass_object_size parameter as if it's a parameter of enum |
3043 | // type passed directly after the parameter with the pass_object_size |
3044 | // attribute. The aforementioned enum's name is __pass_object_size, and we |
3045 | // pretend it resides in a top-level namespace called __clang. |
3046 | // |
3047 | // FIXME: Is there a defined extension notation for the MS ABI, or is it |
3048 | // necessary to just cross our fingers and hope this type+namespace |
3049 | // combination doesn't conflict with anything? |
3050 | if (D) |
3051 | if (const auto *P = D->getParamDecl(i: I)->getAttr<PassObjectSizeAttr>()) |
3052 | manglePassObjectSizeArg(POSA: P); |
3053 | } |
3054 | // <builtin-type> ::= Z # ellipsis |
3055 | if (Proto->isVariadic()) |
3056 | Out << 'Z'; |
3057 | else |
3058 | Out << '@'; |
3059 | } |
3060 | |
3061 | if (MangleExceptionSpec && getASTContext().getLangOpts().CPlusPlus17 && |
3062 | getASTContext().getLangOpts().isCompatibleWithMSVC( |
3063 | MajorVersion: LangOptions::MSVC2017_5)) |
3064 | mangleThrowSpecification(T: Proto); |
3065 | else |
3066 | Out << 'Z'; |
3067 | } |
3068 | |
3069 | void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { |
3070 | // <function-class> ::= <member-function> E? # E designates a 64-bit 'this' |
3071 | // # pointer. in 64-bit mode *all* |
3072 | // # 'this' pointers are 64-bit. |
3073 | // ::= <global-function> |
3074 | // <member-function> ::= A # private: near |
3075 | // ::= B # private: far |
3076 | // ::= C # private: static near |
3077 | // ::= D # private: static far |
3078 | // ::= E # private: virtual near |
3079 | // ::= F # private: virtual far |
3080 | // ::= I # protected: near |
3081 | // ::= J # protected: far |
3082 | // ::= K # protected: static near |
3083 | // ::= L # protected: static far |
3084 | // ::= M # protected: virtual near |
3085 | // ::= N # protected: virtual far |
3086 | // ::= Q # public: near |
3087 | // ::= R # public: far |
3088 | // ::= S # public: static near |
3089 | // ::= T # public: static far |
3090 | // ::= U # public: virtual near |
3091 | // ::= V # public: virtual far |
3092 | // <global-function> ::= Y # global near |
3093 | // ::= Z # global far |
3094 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD)) { |
3095 | bool IsVirtual = MD->isVirtual(); |
3096 | // When mangling vbase destructor variants, ignore whether or not the |
3097 | // underlying destructor was defined to be virtual. |
3098 | if (isa<CXXDestructorDecl>(Val: MD) && isStructorDecl(ND: MD) && |
3099 | StructorType == Dtor_Complete) { |
3100 | IsVirtual = false; |
3101 | } |
3102 | switch (MD->getAccess()) { |
3103 | case AS_none: |
3104 | llvm_unreachable("Unsupported access specifier" ); |
3105 | case AS_private: |
3106 | if (!MD->isImplicitObjectMemberFunction()) |
3107 | Out << 'C'; |
3108 | else if (IsVirtual) |
3109 | Out << 'E'; |
3110 | else |
3111 | Out << 'A'; |
3112 | break; |
3113 | case AS_protected: |
3114 | if (!MD->isImplicitObjectMemberFunction()) |
3115 | Out << 'K'; |
3116 | else if (IsVirtual) |
3117 | Out << 'M'; |
3118 | else |
3119 | Out << 'I'; |
3120 | break; |
3121 | case AS_public: |
3122 | if (!MD->isImplicitObjectMemberFunction()) |
3123 | Out << 'S'; |
3124 | else if (IsVirtual) |
3125 | Out << 'U'; |
3126 | else |
3127 | Out << 'Q'; |
3128 | } |
3129 | } else { |
3130 | Out << 'Y'; |
3131 | } |
3132 | } |
3133 | void MicrosoftCXXNameMangler::mangleCallingConvention(CallingConv CC, |
3134 | SourceRange Range) { |
3135 | // <calling-convention> ::= A # __cdecl |
3136 | // ::= B # __export __cdecl |
3137 | // ::= C # __pascal |
3138 | // ::= D # __export __pascal |
3139 | // ::= E # __thiscall |
3140 | // ::= F # __export __thiscall |
3141 | // ::= G # __stdcall |
3142 | // ::= H # __export __stdcall |
3143 | // ::= I # __fastcall |
3144 | // ::= J # __export __fastcall |
3145 | // ::= Q # __vectorcall |
3146 | // ::= S # __attribute__((__swiftcall__)) // Clang-only |
3147 | // ::= W # __attribute__((__swiftasynccall__)) |
3148 | // ::= U # __attribute__((__preserve_most__)) |
3149 | // ::= V # __attribute__((__preserve_none__)) // |
3150 | // Clang-only |
3151 | // // Clang-only |
3152 | // ::= w # __regcall |
3153 | // ::= x # __regcall4 |
3154 | // The 'export' calling conventions are from a bygone era |
3155 | // (*cough*Win16*cough*) when functions were declared for export with |
3156 | // that keyword. (It didn't actually export them, it just made them so |
3157 | // that they could be in a DLL and somebody from another module could call |
3158 | // them.) |
3159 | |
3160 | switch (CC) { |
3161 | default: |
3162 | break; |
3163 | case CC_Win64: |
3164 | case CC_X86_64SysV: |
3165 | case CC_C: |
3166 | Out << 'A'; |
3167 | return; |
3168 | case CC_X86Pascal: |
3169 | Out << 'C'; |
3170 | return; |
3171 | case CC_X86ThisCall: |
3172 | Out << 'E'; |
3173 | return; |
3174 | case CC_X86StdCall: |
3175 | Out << 'G'; |
3176 | return; |
3177 | case CC_X86FastCall: |
3178 | Out << 'I'; |
3179 | return; |
3180 | case CC_X86VectorCall: |
3181 | Out << 'Q'; |
3182 | return; |
3183 | case CC_Swift: |
3184 | Out << 'S'; |
3185 | return; |
3186 | case CC_SwiftAsync: |
3187 | Out << 'W'; |
3188 | return; |
3189 | case CC_PreserveMost: |
3190 | Out << 'U'; |
3191 | return; |
3192 | case CC_PreserveNone: |
3193 | Out << 'V'; |
3194 | return; |
3195 | case CC_X86RegCall: |
3196 | if (getASTContext().getLangOpts().RegCall4) |
3197 | Out << "x" ; |
3198 | else |
3199 | Out << "w" ; |
3200 | return; |
3201 | } |
3202 | |
3203 | Error(loc: Range.getBegin(), thingy: "calling convention" ) << Range; |
3204 | } |
3205 | void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T, |
3206 | SourceRange Range) { |
3207 | mangleCallingConvention(CC: T->getCallConv(), Range); |
3208 | } |
3209 | |
3210 | void MicrosoftCXXNameMangler::mangleThrowSpecification( |
3211 | const FunctionProtoType *FT) { |
3212 | // <throw-spec> ::= Z # (default) |
3213 | // ::= _E # noexcept |
3214 | if (FT->canThrow()) |
3215 | Out << 'Z'; |
3216 | else |
3217 | Out << "_E" ; |
3218 | } |
3219 | |
3220 | void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T, |
3221 | Qualifiers, SourceRange Range) { |
3222 | // Probably should be mangled as a template instantiation; need to see what |
3223 | // VC does first. |
3224 | Error(loc: Range.getBegin(), thingy: "unresolved dependent type" ) << Range; |
3225 | } |
3226 | |
3227 | // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> |
3228 | // <union-type> ::= T <name> |
3229 | // <struct-type> ::= U <name> |
3230 | // <class-type> ::= V <name> |
3231 | // <enum-type> ::= W4 <name> |
3232 | void MicrosoftCXXNameMangler::mangleTagTypeKind(TagTypeKind TTK) { |
3233 | switch (TTK) { |
3234 | case TagTypeKind::Union: |
3235 | Out << 'T'; |
3236 | break; |
3237 | case TagTypeKind::Struct: |
3238 | case TagTypeKind::Interface: |
3239 | Out << 'U'; |
3240 | break; |
3241 | case TagTypeKind::Class: |
3242 | Out << 'V'; |
3243 | break; |
3244 | case TagTypeKind::Enum: |
3245 | Out << "W4" ; |
3246 | break; |
3247 | } |
3248 | } |
3249 | void MicrosoftCXXNameMangler::mangleType(const EnumType *T, Qualifiers, |
3250 | SourceRange) { |
3251 | mangleType(TD: cast<TagType>(Val: T)->getDecl()); |
3252 | } |
3253 | void MicrosoftCXXNameMangler::mangleType(const RecordType *T, Qualifiers, |
3254 | SourceRange) { |
3255 | mangleType(TD: cast<TagType>(Val: T)->getDecl()); |
3256 | } |
3257 | void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) { |
3258 | mangleTagTypeKind(TTK: TD->getTagKind()); |
3259 | mangleName(GD: TD); |
3260 | } |
3261 | |
3262 | // If you add a call to this, consider updating isArtificialTagType() too. |
3263 | void MicrosoftCXXNameMangler::mangleArtificialTagType( |
3264 | TagTypeKind TK, StringRef UnqualifiedName, |
3265 | ArrayRef<StringRef> NestedNames) { |
3266 | // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ |
3267 | mangleTagTypeKind(TTK: TK); |
3268 | |
3269 | // Always start with the unqualified name. |
3270 | mangleSourceName(Name: UnqualifiedName); |
3271 | |
3272 | for (StringRef N : llvm::reverse(C&: NestedNames)) |
3273 | mangleSourceName(Name: N); |
3274 | |
3275 | // Terminate the whole name with an '@'. |
3276 | Out << '@'; |
3277 | } |
3278 | |
3279 | // <type> ::= <array-type> |
3280 | // <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> |
3281 | // [Y <dimension-count> <dimension>+] |
3282 | // <element-type> # as global, E is never required |
3283 | // It's supposed to be the other way around, but for some strange reason, it |
3284 | // isn't. Today this behavior is retained for the sole purpose of backwards |
3285 | // compatibility. |
3286 | void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) { |
3287 | // This isn't a recursive mangling, so now we have to do it all in this |
3288 | // one call. |
3289 | manglePointerCVQualifiers(Quals: T->getElementType().getQualifiers()); |
3290 | mangleType(T: T->getElementType(), Range: SourceRange()); |
3291 | } |
3292 | void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T, Qualifiers, |
3293 | SourceRange) { |
3294 | llvm_unreachable("Should have been special cased" ); |
3295 | } |
3296 | void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T, Qualifiers, |
3297 | SourceRange) { |
3298 | llvm_unreachable("Should have been special cased" ); |
3299 | } |
3300 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T, |
3301 | Qualifiers, SourceRange) { |
3302 | llvm_unreachable("Should have been special cased" ); |
3303 | } |
3304 | void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T, |
3305 | Qualifiers, SourceRange) { |
3306 | llvm_unreachable("Should have been special cased" ); |
3307 | } |
3308 | void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) { |
3309 | QualType ElementTy(T, 0); |
3310 | SmallVector<llvm::APInt, 3> Dimensions; |
3311 | for (;;) { |
3312 | if (ElementTy->isConstantArrayType()) { |
3313 | const ConstantArrayType *CAT = |
3314 | getASTContext().getAsConstantArrayType(T: ElementTy); |
3315 | Dimensions.push_back(Elt: CAT->getSize()); |
3316 | ElementTy = CAT->getElementType(); |
3317 | } else if (ElementTy->isIncompleteArrayType()) { |
3318 | const IncompleteArrayType *IAT = |
3319 | getASTContext().getAsIncompleteArrayType(T: ElementTy); |
3320 | Dimensions.push_back(Elt: llvm::APInt(32, 0)); |
3321 | ElementTy = IAT->getElementType(); |
3322 | } else if (ElementTy->isVariableArrayType()) { |
3323 | const VariableArrayType *VAT = |
3324 | getASTContext().getAsVariableArrayType(T: ElementTy); |
3325 | Dimensions.push_back(Elt: llvm::APInt(32, 0)); |
3326 | ElementTy = VAT->getElementType(); |
3327 | } else if (ElementTy->isDependentSizedArrayType()) { |
3328 | // The dependent expression has to be folded into a constant (TODO). |
3329 | const DependentSizedArrayType *DSAT = |
3330 | getASTContext().getAsDependentSizedArrayType(T: ElementTy); |
3331 | Error(loc: DSAT->getSizeExpr()->getExprLoc(), thingy: "dependent-length" ) |
3332 | << DSAT->getSizeExpr()->getSourceRange(); |
3333 | return; |
3334 | } else { |
3335 | break; |
3336 | } |
3337 | } |
3338 | Out << 'Y'; |
3339 | // <dimension-count> ::= <number> # number of extra dimensions |
3340 | mangleNumber(Number: Dimensions.size()); |
3341 | for (const llvm::APInt &Dimension : Dimensions) |
3342 | mangleNumber(Number: Dimension.getLimitedValue()); |
3343 | mangleType(T: ElementTy, Range: SourceRange(), QMM: QMM_Escape); |
3344 | } |
3345 | |
3346 | void MicrosoftCXXNameMangler::mangleType(const ArrayParameterType *T, |
3347 | Qualifiers, SourceRange) { |
3348 | mangleArrayType(T: cast<ConstantArrayType>(Val: T)); |
3349 | } |
3350 | |
3351 | // <type> ::= <pointer-to-member-type> |
3352 | // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> |
3353 | // <class name> <type> |
3354 | void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T, |
3355 | Qualifiers Quals, SourceRange Range) { |
3356 | QualType PointeeType = T->getPointeeType(); |
3357 | manglePointerCVQualifiers(Quals); |
3358 | manglePointerExtQualifiers(Quals, PointeeType); |
3359 | if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { |
3360 | Out << '8'; |
3361 | mangleName(GD: T->getMostRecentCXXRecordDecl()); |
3362 | mangleFunctionType(T: FPT, D: nullptr, ForceThisQuals: true); |
3363 | } else { |
3364 | mangleQualifiers(Quals: PointeeType.getQualifiers(), IsMember: true); |
3365 | mangleName(GD: T->getMostRecentCXXRecordDecl()); |
3366 | mangleType(T: PointeeType, Range, QMM: QMM_Drop); |
3367 | } |
3368 | } |
3369 | |
3370 | void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T, |
3371 | Qualifiers, SourceRange Range) { |
3372 | Out << '?'; |
3373 | |
3374 | llvm::SmallString<64> Name; |
3375 | Name += "<TTPT_" ; |
3376 | Name += llvm::utostr(X: T->getDepth()); |
3377 | Name += "_" ; |
3378 | Name += llvm::utostr(X: T->getIndex()); |
3379 | Name += ">" ; |
3380 | mangleSourceName(Name); |
3381 | } |
3382 | |
3383 | void MicrosoftCXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T, |
3384 | Qualifiers, SourceRange Range) { |
3385 | Error(loc: Range.getBegin(), thingy: "substituted parameter pack" ) << Range; |
3386 | } |
3387 | |
3388 | // <type> ::= <pointer-type> |
3389 | // <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type> |
3390 | // # the E is required for 64-bit non-static pointers |
3391 | void MicrosoftCXXNameMangler::mangleType(const PointerType *T, Qualifiers Quals, |
3392 | SourceRange Range) { |
3393 | QualType PointeeType = T->getPointeeType(); |
3394 | manglePointerCVQualifiers(Quals); |
3395 | manglePointerExtQualifiers(Quals, PointeeType); |
3396 | manglePointerAuthQualifier(Quals); |
3397 | |
3398 | // For pointer size address spaces, go down the same type mangling path as |
3399 | // non address space types. |
3400 | LangAS AddrSpace = PointeeType.getQualifiers().getAddressSpace(); |
3401 | if (isPtrSizeAddressSpace(AS: AddrSpace) || AddrSpace == LangAS::Default) |
3402 | mangleType(T: PointeeType, Range); |
3403 | else |
3404 | mangleAddressSpaceType(T: PointeeType, Quals: PointeeType.getQualifiers(), Range); |
3405 | } |
3406 | |
3407 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T, |
3408 | Qualifiers Quals, SourceRange Range) { |
3409 | QualType PointeeType = T->getPointeeType(); |
3410 | switch (Quals.getObjCLifetime()) { |
3411 | case Qualifiers::OCL_None: |
3412 | case Qualifiers::OCL_ExplicitNone: |
3413 | break; |
3414 | case Qualifiers::OCL_Autoreleasing: |
3415 | case Qualifiers::OCL_Strong: |
3416 | case Qualifiers::OCL_Weak: |
3417 | return mangleObjCLifetime(Type: PointeeType, Quals, Range); |
3418 | } |
3419 | manglePointerCVQualifiers(Quals); |
3420 | manglePointerExtQualifiers(Quals, PointeeType); |
3421 | mangleType(T: PointeeType, Range); |
3422 | } |
3423 | |
3424 | // <type> ::= <reference-type> |
3425 | // <reference-type> ::= A E? <cvr-qualifiers> <type> |
3426 | // # the E is required for 64-bit non-static lvalue references |
3427 | void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T, |
3428 | Qualifiers Quals, SourceRange Range) { |
3429 | QualType PointeeType = T->getPointeeType(); |
3430 | assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!" ); |
3431 | Out << 'A'; |
3432 | manglePointerExtQualifiers(Quals, PointeeType); |
3433 | mangleType(T: PointeeType, Range); |
3434 | } |
3435 | |
3436 | // <type> ::= <r-value-reference-type> |
3437 | // <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type> |
3438 | // # the E is required for 64-bit non-static rvalue references |
3439 | void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T, |
3440 | Qualifiers Quals, SourceRange Range) { |
3441 | QualType PointeeType = T->getPointeeType(); |
3442 | assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!" ); |
3443 | Out << "$$Q" ; |
3444 | manglePointerExtQualifiers(Quals, PointeeType); |
3445 | mangleType(T: PointeeType, Range); |
3446 | } |
3447 | |
3448 | void MicrosoftCXXNameMangler::mangleType(const ComplexType *T, Qualifiers, |
3449 | SourceRange Range) { |
3450 | QualType ElementType = T->getElementType(); |
3451 | |
3452 | llvm::SmallString<64> TemplateMangling; |
3453 | llvm::raw_svector_ostream Stream(TemplateMangling); |
3454 | MicrosoftCXXNameMangler (Context, Stream); |
3455 | Stream << "?$" ; |
3456 | Extra.mangleSourceName(Name: "_Complex" ); |
3457 | Extra.mangleType(T: ElementType, Range, QMM: QMM_Escape); |
3458 | |
3459 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__clang" }); |
3460 | } |
3461 | |
3462 | // Returns true for types that mangleArtificialTagType() gets called for with |
3463 | // TagTypeKind Union, Struct, Class and where compatibility with MSVC's |
3464 | // mangling matters. |
3465 | // (It doesn't matter for Objective-C types and the like that cl.exe doesn't |
3466 | // support.) |
3467 | bool MicrosoftCXXNameMangler::isArtificialTagType(QualType T) const { |
3468 | const Type *ty = T.getTypePtr(); |
3469 | switch (ty->getTypeClass()) { |
3470 | default: |
3471 | return false; |
3472 | |
3473 | case Type::Vector: { |
3474 | // For ABI compatibility only __m64, __m128(id), and __m256(id) matter, |
3475 | // but since mangleType(VectorType*) always calls mangleArtificialTagType() |
3476 | // just always return true (the other vector types are clang-only). |
3477 | return true; |
3478 | } |
3479 | } |
3480 | } |
3481 | |
3482 | void MicrosoftCXXNameMangler::mangleType(const VectorType *T, Qualifiers Quals, |
3483 | SourceRange Range) { |
3484 | QualType EltTy = T->getElementType(); |
3485 | const BuiltinType *ET = EltTy->getAs<BuiltinType>(); |
3486 | const BitIntType *BitIntTy = EltTy->getAs<BitIntType>(); |
3487 | assert((ET || BitIntTy) && |
3488 | "vectors with non-builtin/_BitInt elements are unsupported" ); |
3489 | uint64_t Width = getASTContext().getTypeSize(T); |
3490 | // Pattern match exactly the typedefs in our intrinsic headers. Anything that |
3491 | // doesn't match the Intel types uses a custom mangling below. |
3492 | size_t OutSizeBefore = Out.tell(); |
3493 | if (!isa<ExtVectorType>(Val: T)) { |
3494 | if (getASTContext().getTargetInfo().getTriple().isX86() && ET) { |
3495 | if (Width == 64 && ET->getKind() == BuiltinType::LongLong) { |
3496 | mangleArtificialTagType(TK: TagTypeKind::Union, UnqualifiedName: "__m64" ); |
3497 | } else if (Width >= 128) { |
3498 | if (ET->getKind() == BuiltinType::Float) |
3499 | mangleArtificialTagType(TK: TagTypeKind::Union, |
3500 | UnqualifiedName: "__m" + llvm::utostr(X: Width)); |
3501 | else if (ET->getKind() == BuiltinType::LongLong) |
3502 | mangleArtificialTagType(TK: TagTypeKind::Union, |
3503 | UnqualifiedName: "__m" + llvm::utostr(X: Width) + 'i'); |
3504 | else if (ET->getKind() == BuiltinType::Double) |
3505 | mangleArtificialTagType(TK: TagTypeKind::Struct, |
3506 | UnqualifiedName: "__m" + llvm::utostr(X: Width) + 'd'); |
3507 | } |
3508 | } |
3509 | } |
3510 | |
3511 | bool IsBuiltin = Out.tell() != OutSizeBefore; |
3512 | if (!IsBuiltin) { |
3513 | // The MS ABI doesn't have a special mangling for vector types, so we define |
3514 | // our own mangling to handle uses of __vector_size__ on user-specified |
3515 | // types, and for extensions like __v4sf. |
3516 | |
3517 | llvm::SmallString<64> TemplateMangling; |
3518 | llvm::raw_svector_ostream Stream(TemplateMangling); |
3519 | MicrosoftCXXNameMangler (Context, Stream); |
3520 | Stream << "?$" ; |
3521 | Extra.mangleSourceName(Name: "__vector" ); |
3522 | Extra.mangleType(T: QualType(ET ? static_cast<const Type *>(ET) : BitIntTy, 0), |
3523 | Range, QMM: QMM_Escape); |
3524 | Extra.mangleIntegerLiteral(Value: llvm::APSInt::getUnsigned(X: T->getNumElements())); |
3525 | |
3526 | mangleArtificialTagType(TK: TagTypeKind::Union, UnqualifiedName: TemplateMangling, NestedNames: {"__clang" }); |
3527 | } |
3528 | } |
3529 | |
3530 | void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T, |
3531 | Qualifiers Quals, SourceRange Range) { |
3532 | mangleType(T: static_cast<const VectorType *>(T), Quals, Range); |
3533 | } |
3534 | |
3535 | void MicrosoftCXXNameMangler::mangleType(const DependentVectorType *T, |
3536 | Qualifiers, SourceRange Range) { |
3537 | Error(loc: Range.getBegin(), thingy: "dependent-sized vector type" ) << Range; |
3538 | } |
3539 | |
3540 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T, |
3541 | Qualifiers, SourceRange Range) { |
3542 | Error(loc: Range.getBegin(), thingy: "dependent-sized extended vector type" ) << Range; |
3543 | } |
3544 | |
3545 | void MicrosoftCXXNameMangler::mangleType(const ConstantMatrixType *T, |
3546 | Qualifiers quals, SourceRange Range) { |
3547 | QualType EltTy = T->getElementType(); |
3548 | |
3549 | llvm::SmallString<64> TemplateMangling; |
3550 | llvm::raw_svector_ostream Stream(TemplateMangling); |
3551 | MicrosoftCXXNameMangler (Context, Stream); |
3552 | |
3553 | Stream << "?$" ; |
3554 | |
3555 | Extra.mangleSourceName(Name: "__matrix" ); |
3556 | Extra.mangleType(T: EltTy, Range, QMM: QMM_Escape); |
3557 | |
3558 | Extra.mangleIntegerLiteral(Value: llvm::APSInt::getUnsigned(X: T->getNumRows())); |
3559 | Extra.mangleIntegerLiteral(Value: llvm::APSInt::getUnsigned(X: T->getNumColumns())); |
3560 | |
3561 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__clang" }); |
3562 | } |
3563 | |
3564 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedMatrixType *T, |
3565 | Qualifiers quals, SourceRange Range) { |
3566 | Error(loc: Range.getBegin(), thingy: "dependent-sized matrix type" ) << Range; |
3567 | } |
3568 | |
3569 | void MicrosoftCXXNameMangler::mangleType(const DependentAddressSpaceType *T, |
3570 | Qualifiers, SourceRange Range) { |
3571 | Error(loc: Range.getBegin(), thingy: "dependent address space type" ) << Range; |
3572 | } |
3573 | |
3574 | void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, Qualifiers, |
3575 | SourceRange) { |
3576 | // ObjC interfaces have structs underlying them. |
3577 | mangleTagTypeKind(TTK: TagTypeKind::Struct); |
3578 | mangleName(GD: T->getDecl()); |
3579 | } |
3580 | |
3581 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T, |
3582 | Qualifiers Quals, SourceRange Range) { |
3583 | if (T->isKindOfType()) |
3584 | return mangleObjCKindOfType(T, Quals, Range); |
3585 | |
3586 | if (T->qual_empty() && !T->isSpecialized()) |
3587 | return mangleType(T: T->getBaseType(), Range, QMM: QMM_Drop); |
3588 | |
3589 | ArgBackRefMap OuterFunArgsContext; |
3590 | ArgBackRefMap OuterTemplateArgsContext; |
3591 | BackRefVec OuterTemplateContext; |
3592 | |
3593 | FunArgBackReferences.swap(RHS&: OuterFunArgsContext); |
3594 | TemplateArgBackReferences.swap(RHS&: OuterTemplateArgsContext); |
3595 | NameBackReferences.swap(RHS&: OuterTemplateContext); |
3596 | |
3597 | mangleTagTypeKind(TTK: TagTypeKind::Struct); |
3598 | |
3599 | Out << "?$" ; |
3600 | if (T->isObjCId()) |
3601 | mangleSourceName(Name: "objc_object" ); |
3602 | else if (T->isObjCClass()) |
3603 | mangleSourceName(Name: "objc_class" ); |
3604 | else |
3605 | mangleSourceName(Name: T->getInterface()->getName()); |
3606 | |
3607 | for (const auto &Q : T->quals()) |
3608 | mangleObjCProtocol(PD: Q); |
3609 | |
3610 | if (T->isSpecialized()) |
3611 | for (const auto &TA : T->getTypeArgs()) |
3612 | mangleType(T: TA, Range, QMM: QMM_Drop); |
3613 | |
3614 | Out << '@'; |
3615 | |
3616 | Out << '@'; |
3617 | |
3618 | FunArgBackReferences.swap(RHS&: OuterFunArgsContext); |
3619 | TemplateArgBackReferences.swap(RHS&: OuterTemplateArgsContext); |
3620 | NameBackReferences.swap(RHS&: OuterTemplateContext); |
3621 | } |
3622 | |
3623 | void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T, |
3624 | Qualifiers Quals, SourceRange Range) { |
3625 | QualType PointeeType = T->getPointeeType(); |
3626 | manglePointerCVQualifiers(Quals); |
3627 | manglePointerExtQualifiers(Quals, PointeeType); |
3628 | |
3629 | Out << "_E" ; |
3630 | |
3631 | mangleFunctionType(T: PointeeType->castAs<FunctionProtoType>()); |
3632 | } |
3633 | |
3634 | void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *, |
3635 | Qualifiers, SourceRange) { |
3636 | llvm_unreachable("Cannot mangle injected class name type." ); |
3637 | } |
3638 | |
3639 | void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T, |
3640 | Qualifiers, SourceRange Range) { |
3641 | Error(loc: Range.getBegin(), thingy: "template specialization type" ) << Range; |
3642 | } |
3643 | |
3644 | void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, Qualifiers, |
3645 | SourceRange Range) { |
3646 | Error(loc: Range.getBegin(), thingy: "dependent name type" ) << Range; |
3647 | } |
3648 | |
3649 | void MicrosoftCXXNameMangler::mangleType( |
3650 | const DependentTemplateSpecializationType *T, Qualifiers, |
3651 | SourceRange Range) { |
3652 | Error(loc: Range.getBegin(), thingy: "dependent template specialization type" ) << Range; |
3653 | } |
3654 | |
3655 | void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, Qualifiers, |
3656 | SourceRange Range) { |
3657 | Error(loc: Range.getBegin(), thingy: "pack expansion" ) << Range; |
3658 | } |
3659 | |
3660 | void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T, |
3661 | Qualifiers Quals, SourceRange Range) { |
3662 | manglePointerCVQualifiers(Quals); |
3663 | mangleType(T: T->getSelectedType(), Range); |
3664 | } |
3665 | |
3666 | void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, Qualifiers, |
3667 | SourceRange Range) { |
3668 | Error(loc: Range.getBegin(), thingy: "typeof(type)" ) << Range; |
3669 | } |
3670 | |
3671 | void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, Qualifiers, |
3672 | SourceRange Range) { |
3673 | Error(loc: Range.getBegin(), thingy: "typeof(expression)" ) << Range; |
3674 | } |
3675 | |
3676 | void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, Qualifiers, |
3677 | SourceRange Range) { |
3678 | Error(loc: Range.getBegin(), thingy: "decltype()" ) << Range; |
3679 | } |
3680 | |
3681 | void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T, |
3682 | Qualifiers, SourceRange Range) { |
3683 | Error(loc: Range.getBegin(), thingy: "unary transform type" ) << Range; |
3684 | } |
3685 | |
3686 | void MicrosoftCXXNameMangler::mangleType(const AutoType *T, Qualifiers, |
3687 | SourceRange Range) { |
3688 | assert(T->getDeducedType().isNull() && "expecting a dependent type!" ); |
3689 | |
3690 | Error(loc: Range.getBegin(), thingy: "'auto' type" ) << Range; |
3691 | } |
3692 | |
3693 | void MicrosoftCXXNameMangler::mangleType( |
3694 | const DeducedTemplateSpecializationType *T, Qualifiers, SourceRange Range) { |
3695 | assert(T->getDeducedType().isNull() && "expecting a dependent type!" ); |
3696 | |
3697 | Error(loc: Range.getBegin(), thingy: "deduced class template specialization type" ) |
3698 | << Range; |
3699 | } |
3700 | |
3701 | void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, Qualifiers, |
3702 | SourceRange Range) { |
3703 | QualType ValueType = T->getValueType(); |
3704 | |
3705 | llvm::SmallString<64> TemplateMangling; |
3706 | llvm::raw_svector_ostream Stream(TemplateMangling); |
3707 | MicrosoftCXXNameMangler (Context, Stream); |
3708 | Stream << "?$" ; |
3709 | Extra.mangleSourceName(Name: "_Atomic" ); |
3710 | Extra.mangleType(T: ValueType, Range, QMM: QMM_Escape); |
3711 | |
3712 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__clang" }); |
3713 | } |
3714 | |
3715 | void MicrosoftCXXNameMangler::mangleType(const PipeType *T, Qualifiers, |
3716 | SourceRange Range) { |
3717 | QualType ElementType = T->getElementType(); |
3718 | |
3719 | llvm::SmallString<64> TemplateMangling; |
3720 | llvm::raw_svector_ostream Stream(TemplateMangling); |
3721 | MicrosoftCXXNameMangler (Context, Stream); |
3722 | Stream << "?$" ; |
3723 | Extra.mangleSourceName(Name: "ocl_pipe" ); |
3724 | Extra.mangleType(T: ElementType, Range, QMM: QMM_Escape); |
3725 | Extra.mangleIntegerLiteral(Value: llvm::APSInt::get(X: T->isReadOnly())); |
3726 | |
3727 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__clang" }); |
3728 | } |
3729 | |
3730 | void MicrosoftMangleContextImpl::mangleCXXName(GlobalDecl GD, |
3731 | raw_ostream &Out) { |
3732 | const NamedDecl *D = cast<NamedDecl>(Val: GD.getDecl()); |
3733 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
3734 | getASTContext().getSourceManager(), |
3735 | "Mangling declaration" ); |
3736 | |
3737 | msvc_hashing_ostream MHO(Out); |
3738 | |
3739 | if (auto *CD = dyn_cast<CXXConstructorDecl>(Val: D)) { |
3740 | auto Type = GD.getCtorType(); |
3741 | MicrosoftCXXNameMangler mangler(*this, MHO, CD, Type); |
3742 | return mangler.mangle(GD); |
3743 | } |
3744 | |
3745 | if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: D)) { |
3746 | auto Type = GD.getDtorType(); |
3747 | MicrosoftCXXNameMangler mangler(*this, MHO, DD, Type); |
3748 | return mangler.mangle(GD); |
3749 | } |
3750 | |
3751 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3752 | return Mangler.mangle(GD); |
3753 | } |
3754 | |
3755 | void MicrosoftCXXNameMangler::mangleType(const BitIntType *T, Qualifiers, |
3756 | SourceRange Range) { |
3757 | llvm::SmallString<64> TemplateMangling; |
3758 | llvm::raw_svector_ostream Stream(TemplateMangling); |
3759 | MicrosoftCXXNameMangler (Context, Stream); |
3760 | Stream << "?$" ; |
3761 | if (T->isUnsigned()) |
3762 | Extra.mangleSourceName(Name: "_UBitInt" ); |
3763 | else |
3764 | Extra.mangleSourceName(Name: "_BitInt" ); |
3765 | Extra.mangleIntegerLiteral(Value: llvm::APSInt::getUnsigned(X: T->getNumBits())); |
3766 | |
3767 | mangleArtificialTagType(TK: TagTypeKind::Struct, UnqualifiedName: TemplateMangling, NestedNames: {"__clang" }); |
3768 | } |
3769 | |
3770 | void MicrosoftCXXNameMangler::mangleType(const DependentBitIntType *T, |
3771 | Qualifiers, SourceRange Range) { |
3772 | Error(loc: Range.getBegin(), thingy: "DependentBitInt type" ) << Range; |
3773 | } |
3774 | |
3775 | void MicrosoftCXXNameMangler::mangleType(const HLSLAttributedResourceType *T, |
3776 | Qualifiers, SourceRange Range) { |
3777 | llvm_unreachable("HLSL uses Itanium name mangling" ); |
3778 | } |
3779 | |
3780 | void MicrosoftCXXNameMangler::mangleType(const HLSLInlineSpirvType *T, |
3781 | Qualifiers, SourceRange Range) { |
3782 | llvm_unreachable("HLSL uses Itanium name mangling" ); |
3783 | } |
3784 | |
3785 | // <this-adjustment> ::= <no-adjustment> | <static-adjustment> | |
3786 | // <virtual-adjustment> |
3787 | // <no-adjustment> ::= A # private near |
3788 | // ::= B # private far |
3789 | // ::= I # protected near |
3790 | // ::= J # protected far |
3791 | // ::= Q # public near |
3792 | // ::= R # public far |
3793 | // <static-adjustment> ::= G <static-offset> # private near |
3794 | // ::= H <static-offset> # private far |
3795 | // ::= O <static-offset> # protected near |
3796 | // ::= P <static-offset> # protected far |
3797 | // ::= W <static-offset> # public near |
3798 | // ::= X <static-offset> # public far |
3799 | // <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near |
3800 | // ::= $1 <virtual-shift> <static-offset> # private far |
3801 | // ::= $2 <virtual-shift> <static-offset> # protected near |
3802 | // ::= $3 <virtual-shift> <static-offset> # protected far |
3803 | // ::= $4 <virtual-shift> <static-offset> # public near |
3804 | // ::= $5 <virtual-shift> <static-offset> # public far |
3805 | // <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift> |
3806 | // <vtordisp-shift> ::= <offset-to-vtordisp> |
3807 | // <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset> |
3808 | // <offset-to-vtordisp> |
3809 | static void mangleThunkThisAdjustment(AccessSpecifier AS, |
3810 | const ThisAdjustment &Adjustment, |
3811 | MicrosoftCXXNameMangler &Mangler, |
3812 | raw_ostream &Out) { |
3813 | if (!Adjustment.Virtual.isEmpty()) { |
3814 | Out << '$'; |
3815 | char AccessSpec; |
3816 | switch (AS) { |
3817 | case AS_none: |
3818 | llvm_unreachable("Unsupported access specifier" ); |
3819 | case AS_private: |
3820 | AccessSpec = '0'; |
3821 | break; |
3822 | case AS_protected: |
3823 | AccessSpec = '2'; |
3824 | break; |
3825 | case AS_public: |
3826 | AccessSpec = '4'; |
3827 | } |
3828 | if (Adjustment.Virtual.Microsoft.VBPtrOffset) { |
3829 | Out << 'R' << AccessSpec; |
3830 | Mangler.mangleNumber( |
3831 | Number: static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset)); |
3832 | Mangler.mangleNumber( |
3833 | Number: static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset)); |
3834 | Mangler.mangleNumber( |
3835 | Number: static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); |
3836 | Mangler.mangleNumber(Number: static_cast<uint32_t>(Adjustment.NonVirtual)); |
3837 | } else { |
3838 | Out << AccessSpec; |
3839 | Mangler.mangleNumber( |
3840 | Number: static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); |
3841 | Mangler.mangleNumber(Number: -static_cast<uint32_t>(Adjustment.NonVirtual)); |
3842 | } |
3843 | } else if (Adjustment.NonVirtual != 0) { |
3844 | switch (AS) { |
3845 | case AS_none: |
3846 | llvm_unreachable("Unsupported access specifier" ); |
3847 | case AS_private: |
3848 | Out << 'G'; |
3849 | break; |
3850 | case AS_protected: |
3851 | Out << 'O'; |
3852 | break; |
3853 | case AS_public: |
3854 | Out << 'W'; |
3855 | } |
3856 | Mangler.mangleNumber(Number: -static_cast<uint32_t>(Adjustment.NonVirtual)); |
3857 | } else { |
3858 | switch (AS) { |
3859 | case AS_none: |
3860 | llvm_unreachable("Unsupported access specifier" ); |
3861 | case AS_private: |
3862 | Out << 'A'; |
3863 | break; |
3864 | case AS_protected: |
3865 | Out << 'I'; |
3866 | break; |
3867 | case AS_public: |
3868 | Out << 'Q'; |
3869 | } |
3870 | } |
3871 | } |
3872 | |
3873 | void MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk( |
3874 | const CXXMethodDecl *MD, const MethodVFTableLocation &ML, |
3875 | raw_ostream &Out) { |
3876 | msvc_hashing_ostream MHO(Out); |
3877 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3878 | Mangler.getStream() << '?'; |
3879 | Mangler.mangleVirtualMemPtrThunk(MD, ML); |
3880 | } |
3881 | |
3882 | void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD, |
3883 | const ThunkInfo &Thunk, |
3884 | bool /*ElideOverrideInfo*/, |
3885 | raw_ostream &Out) { |
3886 | msvc_hashing_ostream MHO(Out); |
3887 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3888 | Mangler.getStream() << '?'; |
3889 | Mangler.mangleName(GD: MD); |
3890 | |
3891 | // Usually the thunk uses the access specifier of the new method, but if this |
3892 | // is a covariant return thunk, then MSVC always uses the public access |
3893 | // specifier, and we do the same. |
3894 | AccessSpecifier AS = Thunk.Return.isEmpty() ? MD->getAccess() : AS_public; |
3895 | mangleThunkThisAdjustment(AS, Adjustment: Thunk.This, Mangler, Out&: MHO); |
3896 | |
3897 | if (!Thunk.Return.isEmpty()) |
3898 | assert(Thunk.Method != nullptr && |
3899 | "Thunk info should hold the overridee decl" ); |
3900 | |
3901 | const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD; |
3902 | Mangler.mangleFunctionType( |
3903 | T: DeclForFPT->getType()->castAs<FunctionProtoType>(), D: MD); |
3904 | } |
3905 | |
3906 | void MicrosoftMangleContextImpl::mangleCXXDtorThunk(const CXXDestructorDecl *DD, |
3907 | CXXDtorType Type, |
3908 | const ThunkInfo &Thunk, |
3909 | bool /*ElideOverrideInfo*/, |
3910 | raw_ostream &Out) { |
3911 | // FIXME: Actually, the dtor thunk should be emitted for vector deleting |
3912 | // dtors rather than scalar deleting dtors. Just use the vector deleting dtor |
3913 | // mangling manually until we support both deleting dtor types. |
3914 | assert(Type == Dtor_Deleting); |
3915 | msvc_hashing_ostream MHO(Out); |
3916 | MicrosoftCXXNameMangler Mangler(*this, MHO, DD, Type); |
3917 | Mangler.getStream() << "??_E" ; |
3918 | Mangler.mangleName(GD: DD->getParent()); |
3919 | auto &Adjustment = Thunk.This; |
3920 | mangleThunkThisAdjustment(AS: DD->getAccess(), Adjustment, Mangler, Out&: MHO); |
3921 | Mangler.mangleFunctionType(T: DD->getType()->castAs<FunctionProtoType>(), D: DD); |
3922 | } |
3923 | |
3924 | void MicrosoftMangleContextImpl::mangleCXXVFTable( |
3925 | const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, |
3926 | raw_ostream &Out) { |
3927 | // <mangled-name> ::= ?_7 <class-name> <storage-class> |
3928 | // <cvr-qualifiers> [<name>] @ |
3929 | // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> |
3930 | // is always '6' for vftables. |
3931 | msvc_hashing_ostream MHO(Out); |
3932 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3933 | if (Derived->hasAttr<DLLImportAttr>()) |
3934 | Mangler.getStream() << "??_S" ; |
3935 | else |
3936 | Mangler.getStream() << "??_7" ; |
3937 | Mangler.mangleName(GD: Derived); |
3938 | Mangler.getStream() << "6B" ; // '6' for vftable, 'B' for const. |
3939 | for (const CXXRecordDecl *RD : BasePath) |
3940 | Mangler.mangleName(GD: RD); |
3941 | Mangler.getStream() << '@'; |
3942 | } |
3943 | |
3944 | void MicrosoftMangleContextImpl::mangleCXXVTable(const CXXRecordDecl *Derived, |
3945 | raw_ostream &Out) { |
3946 | // TODO: Determine appropriate mangling for MSABI |
3947 | mangleCXXVFTable(Derived, BasePath: {}, Out); |
3948 | } |
3949 | |
3950 | void MicrosoftMangleContextImpl::mangleCXXVBTable( |
3951 | const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, |
3952 | raw_ostream &Out) { |
3953 | // <mangled-name> ::= ?_8 <class-name> <storage-class> |
3954 | // <cvr-qualifiers> [<name>] @ |
3955 | // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> |
3956 | // is always '7' for vbtables. |
3957 | msvc_hashing_ostream MHO(Out); |
3958 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3959 | Mangler.getStream() << "??_8" ; |
3960 | Mangler.mangleName(GD: Derived); |
3961 | Mangler.getStream() << "7B" ; // '7' for vbtable, 'B' for const. |
3962 | for (const CXXRecordDecl *RD : BasePath) |
3963 | Mangler.mangleName(GD: RD); |
3964 | Mangler.getStream() << '@'; |
3965 | } |
3966 | |
3967 | void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &Out) { |
3968 | msvc_hashing_ostream MHO(Out); |
3969 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3970 | Mangler.getStream() << "??_R0" ; |
3971 | Mangler.mangleType(T, Range: SourceRange(), QMM: MicrosoftCXXNameMangler::QMM_Result); |
3972 | Mangler.getStream() << "@8" ; |
3973 | } |
3974 | |
3975 | void MicrosoftMangleContextImpl::mangleCXXRTTIName( |
3976 | QualType T, raw_ostream &Out, bool NormalizeIntegers = false) { |
3977 | MicrosoftCXXNameMangler Mangler(*this, Out); |
3978 | Mangler.getStream() << '.'; |
3979 | Mangler.mangleType(T, Range: SourceRange(), QMM: MicrosoftCXXNameMangler::QMM_Result); |
3980 | } |
3981 | |
3982 | void MicrosoftMangleContextImpl::mangleCXXVirtualDisplacementMap( |
3983 | const CXXRecordDecl *SrcRD, const CXXRecordDecl *DstRD, raw_ostream &Out) { |
3984 | msvc_hashing_ostream MHO(Out); |
3985 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3986 | Mangler.getStream() << "??_K" ; |
3987 | Mangler.mangleName(GD: SrcRD); |
3988 | Mangler.getStream() << "$C" ; |
3989 | Mangler.mangleName(GD: DstRD); |
3990 | } |
3991 | |
3992 | void MicrosoftMangleContextImpl::mangleCXXThrowInfo(QualType T, bool IsConst, |
3993 | bool IsVolatile, |
3994 | bool IsUnaligned, |
3995 | uint32_t NumEntries, |
3996 | raw_ostream &Out) { |
3997 | msvc_hashing_ostream MHO(Out); |
3998 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
3999 | Mangler.getStream() << "_TI" ; |
4000 | if (IsConst) |
4001 | Mangler.getStream() << 'C'; |
4002 | if (IsVolatile) |
4003 | Mangler.getStream() << 'V'; |
4004 | if (IsUnaligned) |
4005 | Mangler.getStream() << 'U'; |
4006 | Mangler.getStream() << NumEntries; |
4007 | Mangler.mangleType(T, Range: SourceRange(), QMM: MicrosoftCXXNameMangler::QMM_Result); |
4008 | } |
4009 | |
4010 | void MicrosoftMangleContextImpl::mangleCXXCatchableTypeArray( |
4011 | QualType T, uint32_t NumEntries, raw_ostream &Out) { |
4012 | msvc_hashing_ostream MHO(Out); |
4013 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4014 | Mangler.getStream() << "_CTA" ; |
4015 | Mangler.getStream() << NumEntries; |
4016 | Mangler.mangleType(T, Range: SourceRange(), QMM: MicrosoftCXXNameMangler::QMM_Result); |
4017 | } |
4018 | |
4019 | void MicrosoftMangleContextImpl::mangleCXXCatchableType( |
4020 | QualType T, const CXXConstructorDecl *CD, CXXCtorType CT, uint32_t Size, |
4021 | uint32_t NVOffset, int32_t VBPtrOffset, uint32_t VBIndex, |
4022 | raw_ostream &Out) { |
4023 | MicrosoftCXXNameMangler Mangler(*this, Out); |
4024 | Mangler.getStream() << "_CT" ; |
4025 | |
4026 | llvm::SmallString<64> RTTIMangling; |
4027 | { |
4028 | llvm::raw_svector_ostream Stream(RTTIMangling); |
4029 | msvc_hashing_ostream MHO(Stream); |
4030 | mangleCXXRTTI(T, Out&: MHO); |
4031 | } |
4032 | Mangler.getStream() << RTTIMangling; |
4033 | |
4034 | // VS2015 and VS2017.1 omit the copy-constructor in the mangled name but |
4035 | // both older and newer versions include it. |
4036 | // FIXME: It is known that the Ctor is present in 2013, and in 2017.7 |
4037 | // (_MSC_VER 1914) and newer, and that it's omitted in 2015 and 2017.4 |
4038 | // (_MSC_VER 1911), but it's unknown when exactly it reappeared (1914? |
4039 | // Or 1912, 1913 already?). |
4040 | bool OmitCopyCtor = getASTContext().getLangOpts().isCompatibleWithMSVC( |
4041 | MajorVersion: LangOptions::MSVC2015) && |
4042 | !getASTContext().getLangOpts().isCompatibleWithMSVC( |
4043 | MajorVersion: LangOptions::MSVC2017_7); |
4044 | llvm::SmallString<64> CopyCtorMangling; |
4045 | if (!OmitCopyCtor && CD) { |
4046 | llvm::raw_svector_ostream Stream(CopyCtorMangling); |
4047 | msvc_hashing_ostream MHO(Stream); |
4048 | mangleCXXName(GD: GlobalDecl(CD, CT), Out&: MHO); |
4049 | } |
4050 | Mangler.getStream() << CopyCtorMangling; |
4051 | |
4052 | Mangler.getStream() << Size; |
4053 | if (VBPtrOffset == -1) { |
4054 | if (NVOffset) { |
4055 | Mangler.getStream() << NVOffset; |
4056 | } |
4057 | } else { |
4058 | Mangler.getStream() << NVOffset; |
4059 | Mangler.getStream() << VBPtrOffset; |
4060 | Mangler.getStream() << VBIndex; |
4061 | } |
4062 | } |
4063 | |
4064 | void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassDescriptor( |
4065 | const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset, |
4066 | uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) { |
4067 | msvc_hashing_ostream MHO(Out); |
4068 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4069 | Mangler.getStream() << "??_R1" ; |
4070 | Mangler.mangleNumber(Number: NVOffset); |
4071 | Mangler.mangleNumber(Number: VBPtrOffset); |
4072 | Mangler.mangleNumber(Number: VBTableOffset); |
4073 | Mangler.mangleNumber(Number: Flags); |
4074 | Mangler.mangleName(GD: Derived); |
4075 | Mangler.getStream() << "8" ; |
4076 | } |
4077 | |
4078 | void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassArray( |
4079 | const CXXRecordDecl *Derived, raw_ostream &Out) { |
4080 | msvc_hashing_ostream MHO(Out); |
4081 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4082 | Mangler.getStream() << "??_R2" ; |
4083 | Mangler.mangleName(GD: Derived); |
4084 | Mangler.getStream() << "8" ; |
4085 | } |
4086 | |
4087 | void MicrosoftMangleContextImpl::mangleCXXRTTIClassHierarchyDescriptor( |
4088 | const CXXRecordDecl *Derived, raw_ostream &Out) { |
4089 | msvc_hashing_ostream MHO(Out); |
4090 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4091 | Mangler.getStream() << "??_R3" ; |
4092 | Mangler.mangleName(GD: Derived); |
4093 | Mangler.getStream() << "8" ; |
4094 | } |
4095 | |
4096 | void MicrosoftMangleContextImpl::mangleCXXRTTICompleteObjectLocator( |
4097 | const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, |
4098 | raw_ostream &Out) { |
4099 | // <mangled-name> ::= ?_R4 <class-name> <storage-class> |
4100 | // <cvr-qualifiers> [<name>] @ |
4101 | // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> |
4102 | // is always '6' for vftables. |
4103 | llvm::SmallString<64> VFTableMangling; |
4104 | llvm::raw_svector_ostream Stream(VFTableMangling); |
4105 | mangleCXXVFTable(Derived, BasePath, Out&: Stream); |
4106 | |
4107 | if (VFTableMangling.starts_with(Prefix: "??@" )) { |
4108 | assert(VFTableMangling.ends_with("@" )); |
4109 | Out << VFTableMangling << "??_R4@" ; |
4110 | return; |
4111 | } |
4112 | |
4113 | assert(VFTableMangling.starts_with("??_7" ) || |
4114 | VFTableMangling.starts_with("??_S" )); |
4115 | |
4116 | Out << "??_R4" << VFTableMangling.str().drop_front(N: 4); |
4117 | } |
4118 | |
4119 | void MicrosoftMangleContextImpl::mangleSEHFilterExpression( |
4120 | GlobalDecl EnclosingDecl, raw_ostream &Out) { |
4121 | msvc_hashing_ostream MHO(Out); |
4122 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4123 | // The function body is in the same comdat as the function with the handler, |
4124 | // so the numbering here doesn't have to be the same across TUs. |
4125 | // |
4126 | // <mangled-name> ::= ?filt$ <filter-number> @0 |
4127 | Mangler.getStream() << "?filt$" << SEHFilterIds[EnclosingDecl]++ << "@0@" ; |
4128 | Mangler.mangleName(GD: EnclosingDecl); |
4129 | } |
4130 | |
4131 | void MicrosoftMangleContextImpl::mangleSEHFinallyBlock( |
4132 | GlobalDecl EnclosingDecl, raw_ostream &Out) { |
4133 | msvc_hashing_ostream MHO(Out); |
4134 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4135 | // The function body is in the same comdat as the function with the handler, |
4136 | // so the numbering here doesn't have to be the same across TUs. |
4137 | // |
4138 | // <mangled-name> ::= ?fin$ <filter-number> @0 |
4139 | Mangler.getStream() << "?fin$" << SEHFinallyIds[EnclosingDecl]++ << "@0@" ; |
4140 | Mangler.mangleName(GD: EnclosingDecl); |
4141 | } |
4142 | |
4143 | void MicrosoftMangleContextImpl::mangleCanonicalTypeName( |
4144 | QualType T, raw_ostream &Out, bool NormalizeIntegers = false) { |
4145 | // This is just a made up unique string for the purposes of tbaa. undname |
4146 | // does *not* know how to demangle it. |
4147 | MicrosoftCXXNameMangler Mangler(*this, Out); |
4148 | Mangler.getStream() << '?'; |
4149 | Mangler.mangleType(T: T.getCanonicalType(), Range: SourceRange()); |
4150 | } |
4151 | |
4152 | void MicrosoftMangleContextImpl::mangleReferenceTemporary( |
4153 | const VarDecl *VD, unsigned ManglingNumber, raw_ostream &Out) { |
4154 | msvc_hashing_ostream MHO(Out); |
4155 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4156 | |
4157 | Mangler.getStream() << "?" ; |
4158 | Mangler.mangleSourceName(Name: "$RT" + llvm::utostr(X: ManglingNumber)); |
4159 | Mangler.mangle(GD: VD, Prefix: "" ); |
4160 | } |
4161 | |
4162 | void MicrosoftMangleContextImpl::mangleThreadSafeStaticGuardVariable( |
4163 | const VarDecl *VD, unsigned GuardNum, raw_ostream &Out) { |
4164 | msvc_hashing_ostream MHO(Out); |
4165 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4166 | |
4167 | Mangler.getStream() << "?" ; |
4168 | Mangler.mangleSourceName(Name: "$TSS" + llvm::utostr(X: GuardNum)); |
4169 | Mangler.mangleNestedName(GD: VD); |
4170 | Mangler.getStream() << "@4HA" ; |
4171 | } |
4172 | |
4173 | void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD, |
4174 | raw_ostream &Out) { |
4175 | // <guard-name> ::= ?_B <postfix> @5 <scope-depth> |
4176 | // ::= ?__J <postfix> @5 <scope-depth> |
4177 | // ::= ?$S <guard-num> @ <postfix> @4IA |
4178 | |
4179 | // The first mangling is what MSVC uses to guard static locals in inline |
4180 | // functions. It uses a different mangling in external functions to support |
4181 | // guarding more than 32 variables. MSVC rejects inline functions with more |
4182 | // than 32 static locals. We don't fully implement the second mangling |
4183 | // because those guards are not externally visible, and instead use LLVM's |
4184 | // default renaming when creating a new guard variable. |
4185 | msvc_hashing_ostream MHO(Out); |
4186 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4187 | |
4188 | bool Visible = VD->isExternallyVisible(); |
4189 | if (Visible) { |
4190 | Mangler.getStream() << (VD->getTLSKind() ? "??__J" : "??_B" ); |
4191 | } else { |
4192 | Mangler.getStream() << "?$S1@" ; |
4193 | } |
4194 | unsigned ScopeDepth = 0; |
4195 | if (Visible && !getNextDiscriminator(ND: VD, disc&: ScopeDepth)) |
4196 | // If we do not have a discriminator and are emitting a guard variable for |
4197 | // use at global scope, then mangling the nested name will not be enough to |
4198 | // remove ambiguities. |
4199 | Mangler.mangle(GD: VD, Prefix: "" ); |
4200 | else |
4201 | Mangler.mangleNestedName(GD: VD); |
4202 | Mangler.getStream() << (Visible ? "@5" : "@4IA" ); |
4203 | if (ScopeDepth) |
4204 | Mangler.mangleNumber(Number: ScopeDepth); |
4205 | } |
4206 | |
4207 | void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D, |
4208 | char CharCode, |
4209 | raw_ostream &Out) { |
4210 | msvc_hashing_ostream MHO(Out); |
4211 | MicrosoftCXXNameMangler Mangler(*this, MHO); |
4212 | Mangler.getStream() << "??__" << CharCode; |
4213 | if (D->isStaticDataMember()) { |
4214 | Mangler.getStream() << '?'; |
4215 | Mangler.mangleName(GD: D); |
4216 | Mangler.mangleVariableEncoding(VD: D); |
4217 | Mangler.getStream() << "@@" ; |
4218 | } else { |
4219 | Mangler.mangleName(GD: D); |
4220 | } |
4221 | // This is the function class mangling. These stubs are global, non-variadic, |
4222 | // cdecl functions that return void and take no args. |
4223 | Mangler.getStream() << "YAXXZ" ; |
4224 | } |
4225 | |
4226 | void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D, |
4227 | raw_ostream &Out) { |
4228 | // <initializer-name> ::= ?__E <name> YAXXZ |
4229 | mangleInitFiniStub(D, CharCode: 'E', Out); |
4230 | } |
4231 | |
4232 | void |
4233 | MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D, |
4234 | raw_ostream &Out) { |
4235 | // <destructor-name> ::= ?__F <name> YAXXZ |
4236 | mangleInitFiniStub(D, CharCode: 'F', Out); |
4237 | } |
4238 | |
4239 | void MicrosoftMangleContextImpl::mangleStringLiteral(const StringLiteral *SL, |
4240 | raw_ostream &Out) { |
4241 | // <char-type> ::= 0 # char, char16_t, char32_t |
4242 | // # (little endian char data in mangling) |
4243 | // ::= 1 # wchar_t (big endian char data in mangling) |
4244 | // |
4245 | // <literal-length> ::= <non-negative integer> # the length of the literal |
4246 | // |
4247 | // <encoded-crc> ::= <hex digit>+ @ # crc of the literal including |
4248 | // # trailing null bytes |
4249 | // |
4250 | // <encoded-string> ::= <simple character> # uninteresting character |
4251 | // ::= '?$' <hex digit> <hex digit> # these two nibbles |
4252 | // # encode the byte for the |
4253 | // # character |
4254 | // ::= '?' [a-z] # \xe1 - \xfa |
4255 | // ::= '?' [A-Z] # \xc1 - \xda |
4256 | // ::= '?' [0-9] # [,/\:. \n\t'-] |
4257 | // |
4258 | // <literal> ::= '??_C@_' <char-type> <literal-length> <encoded-crc> |
4259 | // <encoded-string> '@' |
4260 | MicrosoftCXXNameMangler Mangler(*this, Out); |
4261 | Mangler.getStream() << "??_C@_" ; |
4262 | |
4263 | // The actual string length might be different from that of the string literal |
4264 | // in cases like: |
4265 | // char foo[3] = "foobar"; |
4266 | // char bar[42] = "foobar"; |
4267 | // Where it is truncated or zero-padded to fit the array. This is the length |
4268 | // used for mangling, and any trailing null-bytes also need to be mangled. |
4269 | unsigned StringLength = |
4270 | getASTContext().getAsConstantArrayType(T: SL->getType())->getZExtSize(); |
4271 | unsigned StringByteLength = StringLength * SL->getCharByteWidth(); |
4272 | |
4273 | // <char-type>: The "kind" of string literal is encoded into the mangled name. |
4274 | if (SL->isWide()) |
4275 | Mangler.getStream() << '1'; |
4276 | else |
4277 | Mangler.getStream() << '0'; |
4278 | |
4279 | // <literal-length>: The next part of the mangled name consists of the length |
4280 | // of the string in bytes. |
4281 | Mangler.mangleNumber(Number: StringByteLength); |
4282 | |
4283 | auto GetLittleEndianByte = [&SL](unsigned Index) { |
4284 | unsigned CharByteWidth = SL->getCharByteWidth(); |
4285 | if (Index / CharByteWidth >= SL->getLength()) |
4286 | return static_cast<char>(0); |
4287 | uint32_t CodeUnit = SL->getCodeUnit(i: Index / CharByteWidth); |
4288 | unsigned OffsetInCodeUnit = Index % CharByteWidth; |
4289 | return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff); |
4290 | }; |
4291 | |
4292 | auto GetBigEndianByte = [&SL](unsigned Index) { |
4293 | unsigned CharByteWidth = SL->getCharByteWidth(); |
4294 | if (Index / CharByteWidth >= SL->getLength()) |
4295 | return static_cast<char>(0); |
4296 | uint32_t CodeUnit = SL->getCodeUnit(i: Index / CharByteWidth); |
4297 | unsigned OffsetInCodeUnit = (CharByteWidth - 1) - (Index % CharByteWidth); |
4298 | return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff); |
4299 | }; |
4300 | |
4301 | // CRC all the bytes of the StringLiteral. |
4302 | llvm::JamCRC JC; |
4303 | for (unsigned I = 0, E = StringByteLength; I != E; ++I) |
4304 | JC.update(Data: GetLittleEndianByte(I)); |
4305 | |
4306 | // <encoded-crc>: The CRC is encoded utilizing the standard number mangling |
4307 | // scheme. |
4308 | Mangler.mangleNumber(Number: JC.getCRC()); |
4309 | |
4310 | // <encoded-string>: The mangled name also contains the first 32 bytes |
4311 | // (including null-terminator bytes) of the encoded StringLiteral. |
4312 | // Each character is encoded by splitting them into bytes and then encoding |
4313 | // the constituent bytes. |
4314 | auto MangleByte = [&Mangler](char Byte) { |
4315 | // There are five different manglings for characters: |
4316 | // - [a-zA-Z0-9_$]: A one-to-one mapping. |
4317 | // - ?[a-z]: The range from \xe1 to \xfa. |
4318 | // - ?[A-Z]: The range from \xc1 to \xda. |
4319 | // - ?[0-9]: The set of [,/\:. \n\t'-]. |
4320 | // - ?$XX: A fallback which maps nibbles. |
4321 | if (isAsciiIdentifierContinue(c: Byte, /*AllowDollar=*/true)) { |
4322 | Mangler.getStream() << Byte; |
4323 | } else if (isLetter(c: Byte & 0x7f)) { |
4324 | Mangler.getStream() << '?' << static_cast<char>(Byte & 0x7f); |
4325 | } else { |
4326 | const char SpecialChars[] = {',', '/', '\\', ':', '.', |
4327 | ' ', '\n', '\t', '\'', '-'}; |
4328 | const char *Pos = llvm::find(Range: SpecialChars, Val: Byte); |
4329 | if (Pos != std::end(arr: SpecialChars)) { |
4330 | Mangler.getStream() << '?' << (Pos - std::begin(arr: SpecialChars)); |
4331 | } else { |
4332 | Mangler.getStream() << "?$" ; |
4333 | Mangler.getStream() << static_cast<char>('A' + ((Byte >> 4) & 0xf)); |
4334 | Mangler.getStream() << static_cast<char>('A' + (Byte & 0xf)); |
4335 | } |
4336 | } |
4337 | }; |
4338 | |
4339 | // Enforce our 32 bytes max, except wchar_t which gets 32 chars instead. |
4340 | unsigned MaxBytesToMangle = SL->isWide() ? 64U : 32U; |
4341 | unsigned NumBytesToMangle = std::min(a: MaxBytesToMangle, b: StringByteLength); |
4342 | for (unsigned I = 0; I != NumBytesToMangle; ++I) { |
4343 | if (SL->isWide()) |
4344 | MangleByte(GetBigEndianByte(I)); |
4345 | else |
4346 | MangleByte(GetLittleEndianByte(I)); |
4347 | } |
4348 | |
4349 | Mangler.getStream() << '@'; |
4350 | } |
4351 | |
4352 | void MicrosoftCXXNameMangler::mangleAutoReturnType(const MemberPointerType *T, |
4353 | Qualifiers Quals) { |
4354 | QualType PointeeType = T->getPointeeType(); |
4355 | manglePointerCVQualifiers(Quals); |
4356 | manglePointerExtQualifiers(Quals, PointeeType); |
4357 | if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { |
4358 | Out << '8'; |
4359 | mangleName(GD: T->getMostRecentCXXRecordDecl()); |
4360 | mangleFunctionType(T: FPT, D: nullptr, ForceThisQuals: true); |
4361 | } else { |
4362 | mangleQualifiers(Quals: PointeeType.getQualifiers(), IsMember: true); |
4363 | mangleName(GD: T->getMostRecentCXXRecordDecl()); |
4364 | mangleAutoReturnType(T: PointeeType, QMM: QMM_Drop); |
4365 | } |
4366 | } |
4367 | |
4368 | void MicrosoftCXXNameMangler::mangleAutoReturnType(const PointerType *T, |
4369 | Qualifiers Quals) { |
4370 | QualType PointeeType = T->getPointeeType(); |
4371 | assert(!PointeeType.getQualifiers().hasAddressSpace() && |
4372 | "Unexpected address space mangling required" ); |
4373 | |
4374 | manglePointerCVQualifiers(Quals); |
4375 | manglePointerExtQualifiers(Quals, PointeeType); |
4376 | |
4377 | if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { |
4378 | Out << '6'; |
4379 | mangleFunctionType(T: FPT); |
4380 | } else { |
4381 | mangleAutoReturnType(T: PointeeType, QMM: QMM_Mangle); |
4382 | } |
4383 | } |
4384 | |
4385 | void MicrosoftCXXNameMangler::mangleAutoReturnType(const LValueReferenceType *T, |
4386 | Qualifiers Quals) { |
4387 | QualType PointeeType = T->getPointeeType(); |
4388 | assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!" ); |
4389 | Out << 'A'; |
4390 | manglePointerExtQualifiers(Quals, PointeeType); |
4391 | mangleAutoReturnType(T: PointeeType, QMM: QMM_Mangle); |
4392 | } |
4393 | |
4394 | void MicrosoftCXXNameMangler::mangleAutoReturnType(const RValueReferenceType *T, |
4395 | Qualifiers Quals) { |
4396 | QualType PointeeType = T->getPointeeType(); |
4397 | assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!" ); |
4398 | Out << "$$Q" ; |
4399 | manglePointerExtQualifiers(Quals, PointeeType); |
4400 | mangleAutoReturnType(T: PointeeType, QMM: QMM_Mangle); |
4401 | } |
4402 | |
4403 | MicrosoftMangleContext *MicrosoftMangleContext::create(ASTContext &Context, |
4404 | DiagnosticsEngine &Diags, |
4405 | bool IsAux) { |
4406 | return new MicrosoftMangleContextImpl(Context, Diags, IsAux); |
4407 | } |
4408 | |