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