blob: 2cc1e5f46c60fd80421c43536ed71aa048465cda [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Mangle.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000018#include "clang/AST/CharUnits.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/DeclTemplate.h"
David Majnemer58e5bee2014-03-24 21:43:36 +000023#include "clang/AST/Expr.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000024#include "clang/AST/ExprCXX.h"
Reid Kleckner96f8f932014-02-05 17:27:08 +000025#include "clang/AST/VTableBuilder.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000026#include "clang/Basic/ABI.h"
27#include "clang/Basic/DiagnosticOptions.h"
Reid Kleckner369f3162013-05-14 20:30:42 +000028#include "clang/Basic/TargetInfo.h"
David Majnemer2206bf52014-03-05 08:57:59 +000029#include "llvm/ADT/StringExtras.h"
Reid Kleckner7dafb232013-05-22 17:16:39 +000030#include "llvm/ADT/StringMap.h"
David Majnemer58e5bee2014-03-24 21:43:36 +000031#include "llvm/Support/MathExtras.h"
David Majnemer3843a052014-03-23 17:47:16 +000032
Guy Benyei11169dd2012-12-18 14:30:41 +000033using namespace clang;
34
35namespace {
36
David Majnemerd5a42b82013-09-13 09:03:14 +000037/// \brief Retrieve the declaration context that should be used when mangling
38/// the given declaration.
39static const DeclContext *getEffectiveDeclContext(const Decl *D) {
40 // The ABI assumes that lambda closure types that occur within
41 // default arguments live in the context of the function. However, due to
42 // the way in which Clang parses and creates function declarations, this is
43 // not the case: the lambda closure type ends up living in the context
44 // where the function itself resides, because the function declaration itself
45 // had not yet been created. Fix the context here.
46 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
47 if (RD->isLambda())
48 if (ParmVarDecl *ContextParam =
49 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
50 return ContextParam->getDeclContext();
51 }
52
53 // Perform the same check for block literals.
54 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
55 if (ParmVarDecl *ContextParam =
56 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
57 return ContextParam->getDeclContext();
58 }
59
60 const DeclContext *DC = D->getDeclContext();
61 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC))
62 return getEffectiveDeclContext(CD);
63
64 return DC;
65}
66
67static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
68 return getEffectiveDeclContext(cast<Decl>(DC));
69}
70
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000071static const FunctionDecl *getStructor(const FunctionDecl *fn) {
72 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
73 return ftd->getTemplatedDecl();
74
75 return fn;
76}
77
David Majnemer2206bf52014-03-05 08:57:59 +000078static bool isLambda(const NamedDecl *ND) {
79 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
80 if (!Record)
81 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +000082
David Majnemer2206bf52014-03-05 08:57:59 +000083 return Record->isLambda();
84}
Guy Benyei11169dd2012-12-18 14:30:41 +000085
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000086/// MicrosoftMangleContextImpl - Overrides the default MangleContext for the
Guy Benyei11169dd2012-12-18 14:30:41 +000087/// Microsoft Visual C++ ABI.
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000088class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
David Majnemer2206bf52014-03-05 08:57:59 +000089 typedef std::pair<const DeclContext *, IdentifierInfo *> DiscriminatorKeyTy;
90 llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator;
David Majnemer02e9af42014-04-23 05:16:51 +000091 llvm::DenseMap<const NamedDecl *, unsigned> Uniquifier;
David Majnemerf017ec32014-03-05 10:35:06 +000092 llvm::DenseMap<const CXXRecordDecl *, unsigned> LambdaIds;
David Majnemer2206bf52014-03-05 08:57:59 +000093
Guy Benyei11169dd2012-12-18 14:30:41 +000094public:
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000095 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags)
96 : MicrosoftMangleContext(Context, Diags) {}
Craig Toppercbce6e92014-03-11 06:22:39 +000097 bool shouldMangleCXXName(const NamedDecl *D) override;
David Majnemer58e5bee2014-03-24 21:43:36 +000098 bool shouldMangleStringLiteral(const StringLiteral *SL) override;
Craig Toppercbce6e92014-03-11 06:22:39 +000099 void mangleCXXName(const NamedDecl *D, raw_ostream &Out) override;
David Majnemer02e9af42014-04-23 05:16:51 +0000100 void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
101 raw_ostream &) override;
Craig Toppercbce6e92014-03-11 06:22:39 +0000102 void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk,
103 raw_ostream &) override;
104 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
105 const ThisAdjustment &ThisAdjustment,
106 raw_ostream &) override;
107 void mangleCXXVFTable(const CXXRecordDecl *Derived,
108 ArrayRef<const CXXRecordDecl *> BasePath,
109 raw_ostream &Out) override;
110 void mangleCXXVBTable(const CXXRecordDecl *Derived,
111 ArrayRef<const CXXRecordDecl *> BasePath,
112 raw_ostream &Out) override;
David Majnemera96b7ee2014-05-13 00:44:44 +0000113 void mangleCXXRTTI(QualType T, raw_ostream &Out) override;
114 void mangleCXXRTTIName(QualType T, raw_ostream &Out) override;
David Majnemeraeb55c92014-05-13 06:57:43 +0000115 void mangleCXXRTTIBaseClassDescriptor(const CXXRecordDecl *Derived,
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000116 uint32_t NVOffset, int32_t VBPtrOffset,
David Majnemeraeb55c92014-05-13 06:57:43 +0000117 uint32_t VBTableOffset, uint32_t Flags,
118 raw_ostream &Out) override;
David Majnemera96b7ee2014-05-13 00:44:44 +0000119 void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived,
120 raw_ostream &Out) override;
121 void mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived,
122 raw_ostream &Out) override;
David Majnemeraeb55c92014-05-13 06:57:43 +0000123 void
124 mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived,
125 ArrayRef<const CXXRecordDecl *> BasePath,
126 raw_ostream &Out) override;
Craig Toppercbce6e92014-03-11 06:22:39 +0000127 void mangleTypeName(QualType T, raw_ostream &) override;
128 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
129 raw_ostream &) override;
130 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
131 raw_ostream &) override;
David Majnemerdaff3702014-05-01 17:50:17 +0000132 void mangleReferenceTemporary(const VarDecl *, unsigned ManglingNumber,
133 raw_ostream &) override;
Craig Toppercbce6e92014-03-11 06:22:39 +0000134 void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out) override;
135 void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override;
136 void mangleDynamicAtExitDestructor(const VarDecl *D,
137 raw_ostream &Out) override;
David Majnemer58e5bee2014-03-24 21:43:36 +0000138 void mangleStringLiteral(const StringLiteral *SL, raw_ostream &Out) override;
David Majnemer2206bf52014-03-05 08:57:59 +0000139 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
140 // Lambda closure types are already numbered.
141 if (isLambda(ND))
142 return false;
143
144 const DeclContext *DC = getEffectiveDeclContext(ND);
145 if (!DC->isFunctionOrMethod())
146 return false;
147
148 // Use the canonical number for externally visible decls.
149 if (ND->isExternallyVisible()) {
150 disc = getASTContext().getManglingNumber(ND);
151 return true;
152 }
153
154 // Anonymous tags are already numbered.
155 if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) {
156 if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl())
157 return false;
158 }
159
160 // Make up a reasonable number for internal decls.
161 unsigned &discriminator = Uniquifier[ND];
162 if (!discriminator)
163 discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())];
164 disc = discriminator;
165 return true;
166 }
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000167
David Majnemerf017ec32014-03-05 10:35:06 +0000168 unsigned getLambdaId(const CXXRecordDecl *RD) {
169 assert(RD->isLambda() && "RD must be a lambda!");
170 assert(!RD->isExternallyVisible() && "RD must not be visible!");
171 assert(RD->getLambdaManglingNumber() == 0 &&
172 "RD must not have a mangling number!");
173 std::pair<llvm::DenseMap<const CXXRecordDecl *, unsigned>::iterator, bool>
174 Result = LambdaIds.insert(std::make_pair(RD, LambdaIds.size()));
175 return Result.first->second;
176 }
177
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000178private:
179 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei11169dd2012-12-18 14:30:41 +0000180};
181
David Majnemer2206bf52014-03-05 08:57:59 +0000182/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
183/// Microsoft Visual C++ ABI.
184class MicrosoftCXXNameMangler {
185 MicrosoftMangleContextImpl &Context;
186 raw_ostream &Out;
187
188 /// The "structor" is the top-level declaration being mangled, if
189 /// that's not a template specialization; otherwise it's the pattern
190 /// for that specialization.
191 const NamedDecl *Structor;
192 unsigned StructorType;
193
194 typedef llvm::StringMap<unsigned> BackRefMap;
195 BackRefMap NameBackReferences;
196 bool UseNameBackReferences;
197
David Majnemer02e9af42014-04-23 05:16:51 +0000198 typedef llvm::DenseMap<void *, unsigned> ArgBackRefMap;
David Majnemer2206bf52014-03-05 08:57:59 +0000199 ArgBackRefMap TypeBackReferences;
200
201 ASTContext &getASTContext() const { return Context.getASTContext(); }
202
203 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
204 // this check into mangleQualifiers().
205 const bool PointersAre64Bit;
206
207public:
208 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
209
210 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_)
Craig Topper36250ad2014-05-12 05:36:57 +0000211 : Context(C), Out(Out_), Structor(nullptr), StructorType(-1),
David Majnemer02e9af42014-04-23 05:16:51 +0000212 UseNameBackReferences(true),
213 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
214 64) {}
David Majnemer2206bf52014-03-05 08:57:59 +0000215
216 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_,
217 const CXXDestructorDecl *D, CXXDtorType Type)
David Majnemer02e9af42014-04-23 05:16:51 +0000218 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
219 UseNameBackReferences(true),
220 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
221 64) {}
David Majnemer2206bf52014-03-05 08:57:59 +0000222
223 raw_ostream &getStream() const { return Out; }
224
225 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
226 void mangleName(const NamedDecl *ND);
David Majnemer2206bf52014-03-05 08:57:59 +0000227 void mangleFunctionEncoding(const FunctionDecl *FD);
228 void mangleVariableEncoding(const VarDecl *VD);
229 void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD);
230 void mangleMemberFunctionPointer(const CXXRecordDecl *RD,
231 const CXXMethodDecl *MD);
232 void mangleVirtualMemPtrThunk(
233 const CXXMethodDecl *MD,
234 const MicrosoftVTableContext::MethodVFTableLocation &ML);
235 void mangleNumber(int64_t Number);
236 void mangleType(QualType T, SourceRange Range,
237 QualifierMangleMode QMM = QMM_Mangle);
Craig Topper36250ad2014-05-12 05:36:57 +0000238 void mangleFunctionType(const FunctionType *T,
239 const FunctionDecl *D = nullptr,
David Majnemer2206bf52014-03-05 08:57:59 +0000240 bool ForceInstMethod = false);
241 void mangleNestedName(const NamedDecl *ND);
242
243private:
244 void disableBackReferences() { UseNameBackReferences = false; }
245 void mangleUnqualifiedName(const NamedDecl *ND) {
246 mangleUnqualifiedName(ND, ND->getDeclName());
247 }
248 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
249 void mangleSourceName(StringRef Name);
250 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
251 void mangleCXXDtorType(CXXDtorType T);
252 void mangleQualifiers(Qualifiers Quals, bool IsMember);
David Majnemere3785bb2014-04-23 05:16:56 +0000253 void mangleRefQualifier(RefQualifierKind RefQualifier);
David Majnemer2206bf52014-03-05 08:57:59 +0000254 void manglePointerCVQualifiers(Qualifiers Quals);
255 void manglePointerExtQualifiers(Qualifiers Quals, const Type *PointeeType);
256
257 void mangleUnscopedTemplateName(const TemplateDecl *ND);
David Majnemer02e9af42014-04-23 05:16:51 +0000258 void
259 mangleTemplateInstantiationName(const TemplateDecl *TD,
260 const TemplateArgumentList &TemplateArgs);
David Majnemer2206bf52014-03-05 08:57:59 +0000261 void mangleObjCMethodName(const ObjCMethodDecl *MD);
262
263 void mangleArgumentType(QualType T, SourceRange Range);
264
265 // Declare manglers for every type class.
266#define ABSTRACT_TYPE(CLASS, PARENT)
267#define NON_CANONICAL_TYPE(CLASS, PARENT)
268#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
269 SourceRange Range);
270#include "clang/AST/TypeNodes.def"
271#undef ABSTRACT_TYPE
272#undef NON_CANONICAL_TYPE
273#undef TYPE
274
275 void mangleType(const TagDecl *TD);
276 void mangleDecayedArrayType(const ArrayType *T);
277 void mangleArrayType(const ArrayType *T);
278 void mangleFunctionClass(const FunctionDecl *FD);
279 void mangleCallingConvention(const FunctionType *T);
280 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
281 void mangleExpression(const Expr *E);
282 void mangleThrowSpecification(const FunctionProtoType *T);
283
284 void mangleTemplateArgs(const TemplateDecl *TD,
285 const TemplateArgumentList &TemplateArgs);
286 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
287};
Guy Benyei11169dd2012-12-18 14:30:41 +0000288}
289
Rafael Espindola002667c2013-10-16 01:40:34 +0000290bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
David Majnemerd5a42b82013-09-13 09:03:14 +0000291 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
292 LanguageLinkage L = FD->getLanguageLinkage();
293 // Overloadable functions need mangling.
294 if (FD->hasAttr<OverloadableAttr>())
295 return true;
296
David Majnemerc729b0b2013-09-16 22:44:20 +0000297 // The ABI expects that we would never mangle "typical" user-defined entry
298 // points regardless of visibility or freestanding-ness.
299 //
300 // N.B. This is distinct from asking about "main". "main" has a lot of
301 // special rules associated with it in the standard while these
302 // user-defined entry points are outside of the purview of the standard.
303 // For example, there can be only one definition for "main" in a standards
304 // compliant program; however nothing forbids the existence of wmain and
305 // WinMain in the same translation unit.
306 if (FD->isMSVCRTEntryPoint())
David Majnemerd5a42b82013-09-13 09:03:14 +0000307 return false;
308
309 // C++ functions and those whose names are not a simple identifier need
310 // mangling.
311 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
312 return true;
313
314 // C functions are not mangled.
315 if (L == CLanguageLinkage)
316 return false;
317 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000318
319 // Otherwise, no mangling is done outside C++ mode.
320 if (!getASTContext().getLangOpts().CPlusPlus)
321 return false;
322
David Majnemerd5a42b82013-09-13 09:03:14 +0000323 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
324 // C variables are not mangled.
325 if (VD->isExternC())
326 return false;
327
328 // Variables at global scope with non-internal linkage are not mangled.
329 const DeclContext *DC = getEffectiveDeclContext(D);
330 // Check for extern variable declared locally.
331 if (DC->isFunctionOrMethod() && D->hasLinkage())
332 while (!DC->isNamespace() && !DC->isTranslationUnit())
333 DC = getEffectiveParentContext(DC);
334
335 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
336 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +0000337 return false;
338 }
339
Guy Benyei11169dd2012-12-18 14:30:41 +0000340 return true;
341}
342
David Majnemer58e5bee2014-03-24 21:43:36 +0000343bool
344MicrosoftMangleContextImpl::shouldMangleStringLiteral(const StringLiteral *SL) {
345 return SL->isAscii() || SL->isWide();
346 // TODO: This needs to be updated when MSVC gains support for Unicode
347 // literals.
348}
349
David Majnemer02e9af42014-04-23 05:16:51 +0000350void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000351 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
352 // Therefore it's really important that we don't decorate the
353 // name with leading underscores or leading/trailing at signs. So, by
354 // default, we emit an asm marker at the start so we get the name right.
355 // Callers can override this with a custom prefix.
356
Guy Benyei11169dd2012-12-18 14:30:41 +0000357 // <mangled-name> ::= ? <name> <type-encoding>
358 Out << Prefix;
359 mangleName(D);
360 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
361 mangleFunctionEncoding(FD);
362 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
363 mangleVariableEncoding(VD);
364 else {
365 // TODO: Fields? Can MSVC even mangle them?
366 // Issue a diagnostic for now.
367 DiagnosticsEngine &Diags = Context.getDiags();
David Majnemer02e9af42014-04-23 05:16:51 +0000368 unsigned DiagID = Diags.getCustomDiagID(
369 DiagnosticsEngine::Error, "cannot mangle this declaration yet");
370 Diags.Report(D->getLocation(), DiagID) << D->getSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +0000371 }
372}
373
374void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
375 // <type-encoding> ::= <function-class> <function-type>
376
Reid Kleckner18da98e2013-06-24 19:21:52 +0000377 // Since MSVC operates on the type as written and not the canonical type, it
378 // actually matters which decl we have here. MSVC appears to choose the
379 // first, since it is most likely to be the declaration in a header file.
Rafael Espindola8db352d2013-10-17 15:37:26 +0000380 FD = FD->getFirstDecl();
Reid Kleckner18da98e2013-06-24 19:21:52 +0000381
Guy Benyei11169dd2012-12-18 14:30:41 +0000382 // We should never ever see a FunctionNoProtoType at this point.
383 // We don't even know how to mangle their types anyway :).
Reid Kleckner9a7f3e62013-10-08 00:58:57 +0000384 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
Guy Benyei11169dd2012-12-18 14:30:41 +0000385
David Majnemerd5a42b82013-09-13 09:03:14 +0000386 // extern "C" functions can hold entities that must be mangled.
387 // As it stands, these functions still need to get expressed in the full
388 // external name. They have their class and type omitted, replaced with '9'.
389 if (Context.shouldMangleDeclName(FD)) {
390 // First, the function class.
391 mangleFunctionClass(FD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000392
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +0000393 mangleFunctionType(FT, FD);
David Majnemerd5a42b82013-09-13 09:03:14 +0000394 } else
395 Out << '9';
Guy Benyei11169dd2012-12-18 14:30:41 +0000396}
397
398void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
399 // <type-encoding> ::= <storage-class> <variable-type>
400 // <storage-class> ::= 0 # private static member
401 // ::= 1 # protected static member
402 // ::= 2 # public static member
403 // ::= 3 # global
404 // ::= 4 # static local
David Majnemerb4119f72013-12-13 01:06:04 +0000405
Guy Benyei11169dd2012-12-18 14:30:41 +0000406 // The first character in the encoding (after the name) is the storage class.
407 if (VD->isStaticDataMember()) {
408 // If it's a static member, it also encodes the access level.
409 switch (VD->getAccess()) {
410 default:
411 case AS_private: Out << '0'; break;
412 case AS_protected: Out << '1'; break;
413 case AS_public: Out << '2'; break;
414 }
415 }
416 else if (!VD->isStaticLocal())
417 Out << '3';
418 else
419 Out << '4';
420 // Now mangle the type.
421 // <variable-type> ::= <type> <cvr-qualifiers>
422 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
423 // Pointers and references are odd. The type of 'int * const foo;' gets
424 // mangled as 'QAHA' instead of 'PAHB', for example.
425 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
David Majnemerb9a5f2d2014-01-21 20:33:36 +0000426 QualType Ty = VD->getType();
David Majnemer6dda7bb2013-08-15 08:13:23 +0000427 if (Ty->isPointerType() || Ty->isReferenceType() ||
428 Ty->isMemberPointerType()) {
David Majnemera2724ae2013-08-09 05:56:24 +0000429 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer8eec58f2014-02-18 14:20:10 +0000430 manglePointerExtQualifiers(
Craig Topper36250ad2014-05-12 05:36:57 +0000431 Ty.getDesugaredType(getASTContext()).getLocalQualifiers(), nullptr);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000432 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
433 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
434 // Member pointers are suffixed with a back reference to the member
435 // pointer's class name.
436 mangleName(MPT->getClass()->getAsCXXRecordDecl());
437 } else
438 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemera2724ae2013-08-09 05:56:24 +0000439 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 // Global arrays are funny, too.
David Majnemer5a1b2042013-09-11 04:44:30 +0000441 mangleDecayedArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +0000442 if (AT->getElementType()->isArrayType())
443 Out << 'A';
444 else
445 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000446 } else {
Peter Collingbourne2816c022013-04-25 04:25:40 +0000447 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000448 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000449 }
450}
451
Reid Kleckner96f8f932014-02-05 17:27:08 +0000452void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD,
David Majnemer1e378e42014-02-06 12:46:52 +0000453 const ValueDecl *VD) {
Reid Kleckner96f8f932014-02-05 17:27:08 +0000454 // <member-data-pointer> ::= <integer-literal>
455 // ::= $F <number> <number>
456 // ::= $G <number> <number> <number>
457
David Majnemer763584d2014-02-06 10:59:19 +0000458 int64_t FieldOffset;
459 int64_t VBTableOffset;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000460 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
David Majnemer1e378e42014-02-06 12:46:52 +0000461 if (VD) {
462 FieldOffset = getASTContext().getFieldOffset(VD);
David Majnemer763584d2014-02-06 10:59:19 +0000463 assert(FieldOffset % getASTContext().getCharWidth() == 0 &&
Reid Kleckner96f8f932014-02-05 17:27:08 +0000464 "cannot take address of bitfield");
David Majnemer763584d2014-02-06 10:59:19 +0000465 FieldOffset /= getASTContext().getCharWidth();
466
467 VBTableOffset = 0;
468 } else {
469 FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1;
470
471 VBTableOffset = -1;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000472 }
473
David Majnemer763584d2014-02-06 10:59:19 +0000474 char Code = '\0';
Reid Kleckner96f8f932014-02-05 17:27:08 +0000475 switch (IM) {
David Majnemer763584d2014-02-06 10:59:19 +0000476 case MSInheritanceAttr::Keyword_single_inheritance: Code = '0'; break;
477 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = '0'; break;
478 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'F'; break;
479 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'G'; break;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000480 }
481
David Majnemer763584d2014-02-06 10:59:19 +0000482 Out << '$' << Code;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000483
David Majnemer763584d2014-02-06 10:59:19 +0000484 mangleNumber(FieldOffset);
485
Reid Klecknerbf94e6e2014-04-07 18:07:03 +0000486 // The C++ standard doesn't allow base-to-derived member pointer conversions
487 // in template parameter contexts, so the vbptr offset of data member pointers
488 // is always zero.
David Majnemer763584d2014-02-06 10:59:19 +0000489 if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
Reid Kleckner96f8f932014-02-05 17:27:08 +0000490 mangleNumber(0);
David Majnemer763584d2014-02-06 10:59:19 +0000491 if (MSInheritanceAttr::hasVBTableOffsetField(IM))
492 mangleNumber(VBTableOffset);
Reid Kleckner96f8f932014-02-05 17:27:08 +0000493}
494
495void
496MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD,
497 const CXXMethodDecl *MD) {
498 // <member-function-pointer> ::= $1? <name>
499 // ::= $H? <name> <number>
500 // ::= $I? <name> <number> <number>
501 // ::= $J? <name> <number> <number> <number>
502 // ::= $0A@
503
504 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
505
506 // The null member function pointer is $0A@ in function templates and crashes
507 // MSVC when used in class templates, so we don't know what they really look
508 // like.
509 if (!MD) {
510 Out << "$0A@";
511 return;
512 }
513
514 char Code = '\0';
515 switch (IM) {
516 case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break;
517 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break;
518 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break;
519 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break;
520 }
521
522 Out << '$' << Code << '?';
523
524 // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr
525 // thunk.
526 uint64_t NVOffset = 0;
527 uint64_t VBTableOffset = 0;
Reid Klecknerbf94e6e2014-04-07 18:07:03 +0000528 uint64_t VBPtrOffset = 0;
David Majnemer763584d2014-02-06 10:59:19 +0000529 if (MD->isVirtual()) {
Reid Kleckner96f8f932014-02-05 17:27:08 +0000530 MicrosoftVTableContext *VTContext =
531 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
532 const MicrosoftVTableContext::MethodVFTableLocation &ML =
533 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
534 mangleVirtualMemPtrThunk(MD, ML);
535 NVOffset = ML.VFPtrOffset.getQuantity();
536 VBTableOffset = ML.VBTableIndex * 4;
537 if (ML.VBase) {
Reid Klecknerbf94e6e2014-04-07 18:07:03 +0000538 const ASTRecordLayout &Layout = getASTContext().getASTRecordLayout(RD);
539 VBPtrOffset = Layout.getVBPtrOffset().getQuantity();
Reid Kleckner96f8f932014-02-05 17:27:08 +0000540 }
541 } else {
542 mangleName(MD);
543 mangleFunctionEncoding(MD);
544 }
545
546 if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM))
547 mangleNumber(NVOffset);
548 if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
Reid Klecknerbf94e6e2014-04-07 18:07:03 +0000549 mangleNumber(VBPtrOffset);
Reid Kleckner96f8f932014-02-05 17:27:08 +0000550 if (MSInheritanceAttr::hasVBTableOffsetField(IM))
551 mangleNumber(VBTableOffset);
552}
553
554void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk(
555 const CXXMethodDecl *MD,
556 const MicrosoftVTableContext::MethodVFTableLocation &ML) {
557 // Get the vftable offset.
558 CharUnits PointerWidth = getASTContext().toCharUnitsFromBits(
559 getASTContext().getTargetInfo().getPointerWidth(0));
560 uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity();
561
562 Out << "?_9";
563 mangleName(MD->getParent());
564 Out << "$B";
565 mangleNumber(OffsetInVFTable);
566 Out << 'A';
567 Out << (PointersAre64Bit ? 'A' : 'E');
568}
569
Guy Benyei11169dd2012-12-18 14:30:41 +0000570void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
571 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
Guy Benyei11169dd2012-12-18 14:30:41 +0000572
573 // Always start with the unqualified name.
David Majnemerb4119f72013-12-13 01:06:04 +0000574 mangleUnqualifiedName(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000575
David Majnemer2206bf52014-03-05 08:57:59 +0000576 mangleNestedName(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000577
578 // Terminate the whole name with an '@'.
579 Out << '@';
580}
581
David Majnemer2a816452013-12-09 10:44:32 +0000582void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
583 // <non-negative integer> ::= A@ # when Number == 0
584 // ::= <decimal digit> # when 1 <= Number <= 10
585 // ::= <hex digit>+ @ # when Number >= 10
586 //
587 // <number> ::= [?] <non-negative integer>
Guy Benyei11169dd2012-12-18 14:30:41 +0000588
David Majnemer2a816452013-12-09 10:44:32 +0000589 uint64_t Value = static_cast<uint64_t>(Number);
590 if (Number < 0) {
591 Value = -Value;
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 Out << '?';
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 }
David Majnemer2a816452013-12-09 10:44:32 +0000594
595 if (Value == 0)
596 Out << "A@";
597 else if (Value >= 1 && Value <= 10)
598 Out << (Value - 1);
599 else {
600 // Numbers that are not encoded as decimal digits are represented as nibbles
601 // in the range of ASCII characters 'A' to 'P'.
602 // The number 0x123450 would be encoded as 'BCDEFA'
603 char EncodedNumberBuffer[sizeof(uint64_t) * 2];
604 llvm::MutableArrayRef<char> BufferRef(EncodedNumberBuffer);
605 llvm::MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin();
606 for (; Value != 0; Value >>= 4)
607 *I++ = 'A' + (Value & 0xf);
608 Out.write(I.base(), I - BufferRef.rbegin());
Guy Benyei11169dd2012-12-18 14:30:41 +0000609 Out << '@';
610 }
611}
612
613static const TemplateDecl *
Reid Kleckner52518862013-03-20 01:40:23 +0000614isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000615 // Check if we have a function template.
David Majnemer02e9af42014-04-23 05:16:51 +0000616 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000617 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000618 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000619 return TD;
620 }
621 }
622
623 // Check if we have a class template.
624 if (const ClassTemplateSpecializationDecl *Spec =
David Majnemer02e9af42014-04-23 05:16:51 +0000625 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Reid Kleckner52518862013-03-20 01:40:23 +0000626 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000627 return Spec->getSpecializedTemplate();
628 }
629
David Majnemer8f774532014-03-04 05:38:05 +0000630 // Check if we have a variable template.
631 if (const VarTemplateSpecializationDecl *Spec =
632 dyn_cast<VarTemplateSpecializationDecl>(ND)) {
633 TemplateArgs = &Spec->getTemplateArgs();
634 return Spec->getSpecializedTemplate();
635 }
636
Craig Topper36250ad2014-05-12 05:36:57 +0000637 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000638}
639
David Majnemer02e9af42014-04-23 05:16:51 +0000640void MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
641 DeclarationName Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000642 // <unqualified-name> ::= <operator-name>
643 // ::= <ctor-dtor-name>
644 // ::= <source-name>
645 // ::= <template-name>
Reid Kleckner52518862013-03-20 01:40:23 +0000646
Guy Benyei11169dd2012-12-18 14:30:41 +0000647 // Check if we have a template.
Craig Topper36250ad2014-05-12 05:36:57 +0000648 const TemplateArgumentList *TemplateArgs = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000649 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Klecknerc16c4472013-07-13 00:43:39 +0000650 // Function templates aren't considered for name back referencing. This
651 // makes sense since function templates aren't likely to occur multiple
652 // times in a symbol.
653 // FIXME: Test alias template mangling with MSVC 2013.
654 if (!isa<ClassTemplateDecl>(TD)) {
655 mangleTemplateInstantiationName(TD, *TemplateArgs);
656 return;
657 }
658
Guy Benyei11169dd2012-12-18 14:30:41 +0000659 // Here comes the tricky thing: if we need to mangle something like
660 // void foo(A::X<Y>, B::X<Y>),
661 // the X<Y> part is aliased. However, if you need to mangle
662 // void foo(A::X<A::Y>, A::X<B::Y>),
663 // the A::X<> part is not aliased.
664 // That said, from the mangler's perspective we have a structure like this:
665 // namespace[s] -> type[ -> template-parameters]
666 // but from the Clang perspective we have
667 // type [ -> template-parameters]
668 // \-> namespace[s]
669 // What we do is we create a new mangler, mangle the same type (without
670 // a namespace suffix) using the extra mangler with back references
671 // disabled (to avoid infinite recursion) and then use the mangled type
672 // name as a key to check the mangling of different types for aliasing.
673
674 std::string BackReferenceKey;
675 BackRefMap::iterator Found;
676 if (UseNameBackReferences) {
677 llvm::raw_string_ostream Stream(BackReferenceKey);
678 MicrosoftCXXNameMangler Extra(Context, Stream);
679 Extra.disableBackReferences();
680 Extra.mangleUnqualifiedName(ND, Name);
681 Stream.flush();
682
683 Found = NameBackReferences.find(BackReferenceKey);
684 }
685 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000686 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000687 if (UseNameBackReferences && NameBackReferences.size() < 10) {
688 size_t Size = NameBackReferences.size();
689 NameBackReferences[BackReferenceKey] = Size;
690 }
691 } else {
692 Out << Found->second;
693 }
694 return;
695 }
696
697 switch (Name.getNameKind()) {
698 case DeclarationName::Identifier: {
699 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
David Majnemer956bc112013-11-25 17:50:19 +0000700 mangleSourceName(II->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000701 break;
702 }
David Majnemerb4119f72013-12-13 01:06:04 +0000703
Guy Benyei11169dd2012-12-18 14:30:41 +0000704 // Otherwise, an anonymous entity. We must have a declaration.
705 assert(ND && "mangling empty name without declaration");
David Majnemerb4119f72013-12-13 01:06:04 +0000706
Guy Benyei11169dd2012-12-18 14:30:41 +0000707 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
708 if (NS->isAnonymousNamespace()) {
709 Out << "?A@";
710 break;
711 }
712 }
David Majnemerb4119f72013-12-13 01:06:04 +0000713
David Majnemer2206bf52014-03-05 08:57:59 +0000714 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
715 // We must have an anonymous union or struct declaration.
716 const CXXRecordDecl *RD = VD->getType()->getAsCXXRecordDecl();
717 assert(RD && "expected variable decl to have a record type");
718 // Anonymous types with no tag or typedef get the name of their
719 // declarator mangled in. If they have no declarator, number them with
720 // a $S prefix.
721 llvm::SmallString<64> Name("$S");
722 // Get a unique id for the anonymous struct.
723 Name += llvm::utostr(Context.getAnonymousStructId(RD) + 1);
724 mangleSourceName(Name.str());
725 break;
726 }
727
Guy Benyei11169dd2012-12-18 14:30:41 +0000728 // We must have an anonymous struct.
729 const TagDecl *TD = cast<TagDecl>(ND);
730 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
731 assert(TD->getDeclContext() == D->getDeclContext() &&
732 "Typedef should not be in another decl context!");
733 assert(D->getDeclName().getAsIdentifierInfo() &&
734 "Typedef was not named!");
David Majnemer956bc112013-11-25 17:50:19 +0000735 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000736 break;
737 }
738
David Majnemerf017ec32014-03-05 10:35:06 +0000739 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
740 if (Record->isLambda()) {
741 llvm::SmallString<10> Name("<lambda_");
742 unsigned LambdaId;
743 if (Record->getLambdaManglingNumber())
744 LambdaId = Record->getLambdaManglingNumber();
745 else
746 LambdaId = Context.getLambdaId(Record);
747
748 Name += llvm::utostr(LambdaId);
749 Name += ">";
750
751 mangleSourceName(Name);
752 break;
753 }
754 }
755
David Majnemer2206bf52014-03-05 08:57:59 +0000756 llvm::SmallString<64> Name("<unnamed-type-");
David Majnemer956bc112013-11-25 17:50:19 +0000757 if (TD->hasDeclaratorForAnonDecl()) {
David Majnemer50ce8352013-09-17 23:57:10 +0000758 // Anonymous types with no tag or typedef get the name of their
David Majnemer2206bf52014-03-05 08:57:59 +0000759 // declarator mangled in if they have one.
David Majnemer956bc112013-11-25 17:50:19 +0000760 Name += TD->getDeclaratorForAnonDecl()->getName();
David Majnemer956bc112013-11-25 17:50:19 +0000761 } else {
David Majnemer2206bf52014-03-05 08:57:59 +0000762 // Otherwise, number the types using a $S prefix.
763 Name += "$S";
David Majnemerf017ec32014-03-05 10:35:06 +0000764 Name += llvm::utostr(Context.getAnonymousStructId(TD));
David Majnemer956bc112013-11-25 17:50:19 +0000765 }
David Majnemer2206bf52014-03-05 08:57:59 +0000766 Name += ">";
767 mangleSourceName(Name.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 break;
769 }
David Majnemerb4119f72013-12-13 01:06:04 +0000770
Guy Benyei11169dd2012-12-18 14:30:41 +0000771 case DeclarationName::ObjCZeroArgSelector:
772 case DeclarationName::ObjCOneArgSelector:
773 case DeclarationName::ObjCMultiArgSelector:
774 llvm_unreachable("Can't mangle Objective-C selector names here!");
David Majnemerb4119f72013-12-13 01:06:04 +0000775
Guy Benyei11169dd2012-12-18 14:30:41 +0000776 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000777 if (ND == Structor) {
778 assert(StructorType == Ctor_Complete &&
779 "Should never be asked to mangle a ctor other than complete");
780 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000781 Out << "?0";
782 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000783
Guy Benyei11169dd2012-12-18 14:30:41 +0000784 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000785 if (ND == Structor)
786 // If the named decl is the C++ destructor we're mangling,
787 // use the type we were given.
788 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
789 else
Reid Klecknere7de47e2013-07-22 13:51:44 +0000790 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000791 // class with a destructor is declared within a destructor.
Reid Klecknere7de47e2013-07-22 13:51:44 +0000792 mangleCXXDtorType(Dtor_Base);
Guy Benyei11169dd2012-12-18 14:30:41 +0000793 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000794
Guy Benyei11169dd2012-12-18 14:30:41 +0000795 case DeclarationName::CXXConversionFunctionName:
796 // <operator-name> ::= ?B # (cast)
797 // The target type is encoded as the return type.
798 Out << "?B";
799 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000800
Guy Benyei11169dd2012-12-18 14:30:41 +0000801 case DeclarationName::CXXOperatorName:
802 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
803 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000804
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 case DeclarationName::CXXLiteralOperatorName: {
David Majnemere31a3ed2014-06-04 16:46:26 +0000806 Out << "?__K";
807 mangleSourceName(Name.getCXXLiteralIdentifier()->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 break;
809 }
David Majnemerb4119f72013-12-13 01:06:04 +0000810
Guy Benyei11169dd2012-12-18 14:30:41 +0000811 case DeclarationName::CXXUsingDirective:
812 llvm_unreachable("Can't mangle a using directive name!");
813 }
814}
815
David Majnemer2206bf52014-03-05 08:57:59 +0000816void MicrosoftCXXNameMangler::mangleNestedName(const NamedDecl *ND) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000817 // <postfix> ::= <unqualified-name> [<postfix>]
818 // ::= <substitution> [<postfix>]
David Majnemerf017ec32014-03-05 10:35:06 +0000819 if (isLambda(ND))
820 return;
821
David Majnemer2206bf52014-03-05 08:57:59 +0000822 const DeclContext *DC = ND->getDeclContext();
Guy Benyei11169dd2012-12-18 14:30:41 +0000823
David Majnemer2206bf52014-03-05 08:57:59 +0000824 while (!DC->isTranslationUnit()) {
825 if (isa<TagDecl>(ND) || isa<VarDecl>(ND)) {
826 unsigned Disc;
827 if (Context.getNextDiscriminator(ND, Disc)) {
828 Out << '?';
829 mangleNumber(Disc);
830 Out << '?';
831 }
832 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000833
David Majnemer2206bf52014-03-05 08:57:59 +0000834 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Alp Tokerc7dc0622014-05-11 22:10:52 +0000835 DiagnosticsEngine &Diags = Context.getDiags();
David Majnemer2206bf52014-03-05 08:57:59 +0000836 unsigned DiagID =
837 Diags.getCustomDiagID(DiagnosticsEngine::Error,
838 "cannot mangle a local inside this block yet");
839 Diags.Report(BD->getLocation(), DiagID);
840
841 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
842 // for how this should be done.
843 Out << "__block_invoke" << Context.getBlockId(BD, false);
844 Out << '@';
845 continue;
846 } else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
847 mangleObjCMethodName(Method);
848 } else if (isa<NamedDecl>(DC)) {
849 ND = cast<NamedDecl>(DC);
850 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
851 mangle(FD, "?");
852 break;
853 } else
854 mangleUnqualifiedName(ND);
855 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000856 DC = DC->getParent();
Guy Benyei11169dd2012-12-18 14:30:41 +0000857 }
858}
859
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000860void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000861 // Microsoft uses the names on the case labels for these dtor variants. Clang
862 // uses the Itanium terminology internally. Everything in this ABI delegates
863 // towards the base dtor.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000864 switch (T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000865 // <operator-name> ::= ?1 # destructor
866 case Dtor_Base: Out << "?1"; return;
867 // <operator-name> ::= ?_D # vbase destructor
868 case Dtor_Complete: Out << "?_D"; return;
869 // <operator-name> ::= ?_G # scalar deleting destructor
870 case Dtor_Deleting: Out << "?_G"; return;
871 // <operator-name> ::= ?_E # vector deleting destructor
872 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
873 // it.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000874 }
875 llvm_unreachable("Unsupported dtor type?");
876}
877
Guy Benyei11169dd2012-12-18 14:30:41 +0000878void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
879 SourceLocation Loc) {
880 switch (OO) {
881 // ?0 # constructor
882 // ?1 # destructor
883 // <operator-name> ::= ?2 # new
884 case OO_New: Out << "?2"; break;
885 // <operator-name> ::= ?3 # delete
886 case OO_Delete: Out << "?3"; break;
887 // <operator-name> ::= ?4 # =
888 case OO_Equal: Out << "?4"; break;
889 // <operator-name> ::= ?5 # >>
890 case OO_GreaterGreater: Out << "?5"; break;
891 // <operator-name> ::= ?6 # <<
892 case OO_LessLess: Out << "?6"; break;
893 // <operator-name> ::= ?7 # !
894 case OO_Exclaim: Out << "?7"; break;
895 // <operator-name> ::= ?8 # ==
896 case OO_EqualEqual: Out << "?8"; break;
897 // <operator-name> ::= ?9 # !=
898 case OO_ExclaimEqual: Out << "?9"; break;
899 // <operator-name> ::= ?A # []
900 case OO_Subscript: Out << "?A"; break;
901 // ?B # conversion
902 // <operator-name> ::= ?C # ->
903 case OO_Arrow: Out << "?C"; break;
904 // <operator-name> ::= ?D # *
905 case OO_Star: Out << "?D"; break;
906 // <operator-name> ::= ?E # ++
907 case OO_PlusPlus: Out << "?E"; break;
908 // <operator-name> ::= ?F # --
909 case OO_MinusMinus: Out << "?F"; break;
910 // <operator-name> ::= ?G # -
911 case OO_Minus: Out << "?G"; break;
912 // <operator-name> ::= ?H # +
913 case OO_Plus: Out << "?H"; break;
914 // <operator-name> ::= ?I # &
915 case OO_Amp: Out << "?I"; break;
916 // <operator-name> ::= ?J # ->*
917 case OO_ArrowStar: Out << "?J"; break;
918 // <operator-name> ::= ?K # /
919 case OO_Slash: Out << "?K"; break;
920 // <operator-name> ::= ?L # %
921 case OO_Percent: Out << "?L"; break;
922 // <operator-name> ::= ?M # <
923 case OO_Less: Out << "?M"; break;
924 // <operator-name> ::= ?N # <=
925 case OO_LessEqual: Out << "?N"; break;
926 // <operator-name> ::= ?O # >
927 case OO_Greater: Out << "?O"; break;
928 // <operator-name> ::= ?P # >=
929 case OO_GreaterEqual: Out << "?P"; break;
930 // <operator-name> ::= ?Q # ,
931 case OO_Comma: Out << "?Q"; break;
932 // <operator-name> ::= ?R # ()
933 case OO_Call: Out << "?R"; break;
934 // <operator-name> ::= ?S # ~
935 case OO_Tilde: Out << "?S"; break;
936 // <operator-name> ::= ?T # ^
937 case OO_Caret: Out << "?T"; break;
938 // <operator-name> ::= ?U # |
939 case OO_Pipe: Out << "?U"; break;
940 // <operator-name> ::= ?V # &&
941 case OO_AmpAmp: Out << "?V"; break;
942 // <operator-name> ::= ?W # ||
943 case OO_PipePipe: Out << "?W"; break;
944 // <operator-name> ::= ?X # *=
945 case OO_StarEqual: Out << "?X"; break;
946 // <operator-name> ::= ?Y # +=
947 case OO_PlusEqual: Out << "?Y"; break;
948 // <operator-name> ::= ?Z # -=
949 case OO_MinusEqual: Out << "?Z"; break;
950 // <operator-name> ::= ?_0 # /=
951 case OO_SlashEqual: Out << "?_0"; break;
952 // <operator-name> ::= ?_1 # %=
953 case OO_PercentEqual: Out << "?_1"; break;
954 // <operator-name> ::= ?_2 # >>=
955 case OO_GreaterGreaterEqual: Out << "?_2"; break;
956 // <operator-name> ::= ?_3 # <<=
957 case OO_LessLessEqual: Out << "?_3"; break;
958 // <operator-name> ::= ?_4 # &=
959 case OO_AmpEqual: Out << "?_4"; break;
960 // <operator-name> ::= ?_5 # |=
961 case OO_PipeEqual: Out << "?_5"; break;
962 // <operator-name> ::= ?_6 # ^=
963 case OO_CaretEqual: Out << "?_6"; break;
964 // ?_7 # vftable
965 // ?_8 # vbtable
966 // ?_9 # vcall
967 // ?_A # typeof
968 // ?_B # local static guard
969 // ?_C # string
970 // ?_D # vbase destructor
971 // ?_E # vector deleting destructor
972 // ?_F # default constructor closure
973 // ?_G # scalar deleting destructor
974 // ?_H # vector constructor iterator
975 // ?_I # vector destructor iterator
976 // ?_J # vector vbase constructor iterator
977 // ?_K # virtual displacement map
978 // ?_L # eh vector constructor iterator
979 // ?_M # eh vector destructor iterator
980 // ?_N # eh vector vbase constructor iterator
981 // ?_O # copy constructor closure
982 // ?_P<name> # udt returning <name>
983 // ?_Q # <unknown>
984 // ?_R0 # RTTI Type Descriptor
985 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
986 // ?_R2 # RTTI Base Class Array
987 // ?_R3 # RTTI Class Hierarchy Descriptor
988 // ?_R4 # RTTI Complete Object Locator
989 // ?_S # local vftable
990 // ?_T # local vftable constructor closure
991 // <operator-name> ::= ?_U # new[]
992 case OO_Array_New: Out << "?_U"; break;
993 // <operator-name> ::= ?_V # delete[]
994 case OO_Array_Delete: Out << "?_V"; break;
David Majnemerb4119f72013-12-13 01:06:04 +0000995
Guy Benyei11169dd2012-12-18 14:30:41 +0000996 case OO_Conditional: {
997 DiagnosticsEngine &Diags = Context.getDiags();
998 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
999 "cannot mangle this conditional operator yet");
1000 Diags.Report(Loc, DiagID);
1001 break;
1002 }
David Majnemerb4119f72013-12-13 01:06:04 +00001003
Guy Benyei11169dd2012-12-18 14:30:41 +00001004 case OO_None:
1005 case NUM_OVERLOADED_OPERATORS:
1006 llvm_unreachable("Not an overloaded operator");
1007 }
1008}
1009
David Majnemer956bc112013-11-25 17:50:19 +00001010void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001011 // <source name> ::= <identifier> @
Guy Benyei11169dd2012-12-18 14:30:41 +00001012 BackRefMap::iterator Found;
1013 if (UseNameBackReferences)
David Majnemer956bc112013-11-25 17:50:19 +00001014 Found = NameBackReferences.find(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +00001015 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
David Majnemer956bc112013-11-25 17:50:19 +00001016 Out << Name << '@';
Guy Benyei11169dd2012-12-18 14:30:41 +00001017 if (UseNameBackReferences && NameBackReferences.size() < 10) {
1018 size_t Size = NameBackReferences.size();
David Majnemer956bc112013-11-25 17:50:19 +00001019 NameBackReferences[Name] = Size;
Guy Benyei11169dd2012-12-18 14:30:41 +00001020 }
1021 } else {
1022 Out << Found->second;
1023 }
1024}
1025
1026void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
1027 Context.mangleObjCMethodName(MD, Out);
1028}
1029
Guy Benyei11169dd2012-12-18 14:30:41 +00001030void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
David Majnemer02e9af42014-04-23 05:16:51 +00001031 const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001032 // <template-name> ::= <unscoped-template-name> <template-args>
1033 // ::= <substitution>
1034 // Always start with the unqualified name.
1035
1036 // Templates have their own context for back references.
1037 ArgBackRefMap OuterArgsContext;
1038 BackRefMap OuterTemplateContext;
1039 NameBackReferences.swap(OuterTemplateContext);
1040 TypeBackReferences.swap(OuterArgsContext);
1041
1042 mangleUnscopedTemplateName(TD);
Reid Kleckner52518862013-03-20 01:40:23 +00001043 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +00001044
1045 // Restore the previous back reference contexts.
1046 NameBackReferences.swap(OuterTemplateContext);
1047 TypeBackReferences.swap(OuterArgsContext);
1048}
1049
1050void
1051MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
1052 // <unscoped-template-name> ::= ?$ <unqualified-name>
1053 Out << "?$";
1054 mangleUnqualifiedName(TD);
1055}
1056
David Majnemer02e9af42014-04-23 05:16:51 +00001057void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
1058 bool IsBoolean) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001059 // <integer-literal> ::= $0 <number>
1060 Out << "$0";
1061 // Make sure booleans are encoded as 0/1.
1062 if (IsBoolean && Value.getBoolValue())
1063 mangleNumber(1);
1064 else
David Majnemer2a816452013-12-09 10:44:32 +00001065 mangleNumber(Value.getSExtValue());
Guy Benyei11169dd2012-12-18 14:30:41 +00001066}
1067
David Majnemer02e9af42014-04-23 05:16:51 +00001068void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001069 // See if this is a constant expression.
1070 llvm::APSInt Value;
1071 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
1072 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
1073 return;
1074 }
1075
Craig Topper36250ad2014-05-12 05:36:57 +00001076 const CXXUuidofExpr *UE = nullptr;
David Majnemer8eaab6f2013-08-13 06:32:20 +00001077 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1078 if (UO->getOpcode() == UO_AddrOf)
1079 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
1080 } else
1081 UE = dyn_cast<CXXUuidofExpr>(E);
1082
1083 if (UE) {
1084 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
1085 // const __s_GUID _GUID_{lower case UUID with underscores}
1086 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
1087 std::string Name = "_GUID_" + Uuid.lower();
1088 std::replace(Name.begin(), Name.end(), '-', '_');
1089
David Majnemere9cab2f2013-08-13 09:17:25 +00001090 // If we had to peek through an address-of operator, treat this like we are
David Majnemer8eaab6f2013-08-13 06:32:20 +00001091 // dealing with a pointer type. Otherwise, treat it like a const reference.
1092 //
1093 // N.B. This matches up with the handling of TemplateArgument::Declaration
1094 // in mangleTemplateArg
1095 if (UE == E)
1096 Out << "$E?";
1097 else
1098 Out << "$1?";
1099 Out << Name << "@@3U__s_GUID@@B";
1100 return;
1101 }
1102
Guy Benyei11169dd2012-12-18 14:30:41 +00001103 // As bad as this diagnostic is, it's better than crashing.
1104 DiagnosticsEngine &Diags = Context.getDiags();
David Majnemer02e9af42014-04-23 05:16:51 +00001105 unsigned DiagID = Diags.getCustomDiagID(
1106 DiagnosticsEngine::Error, "cannot yet mangle expression type %0");
1107 Diags.Report(E->getExprLoc(), DiagID) << E->getStmtClassName()
1108 << E->getSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00001109}
1110
David Majnemer8265dec2014-03-30 16:30:54 +00001111void MicrosoftCXXNameMangler::mangleTemplateArgs(
1112 const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001113 // <template-args> ::= <template-arg>+ @
David Majnemer8265dec2014-03-30 16:30:54 +00001114 for (const TemplateArgument &TA : TemplateArgs.asArray())
David Majnemer08177c52013-08-27 08:21:25 +00001115 mangleTemplateArg(TD, TA);
Guy Benyei11169dd2012-12-18 14:30:41 +00001116 Out << '@';
1117}
1118
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001119void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer08177c52013-08-27 08:21:25 +00001120 const TemplateArgument &TA) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001121 // <template-arg> ::= <type>
1122 // ::= <integer-literal>
1123 // ::= <member-data-pointer>
1124 // ::= <member-function-pointer>
1125 // ::= $E? <name> <type-encoding>
1126 // ::= $1? <name> <type-encoding>
1127 // ::= $0A@
1128 // ::= <template-args>
1129
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001130 switch (TA.getKind()) {
1131 case TemplateArgument::Null:
1132 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer08177c52013-08-27 08:21:25 +00001133 case TemplateArgument::TemplateExpansion:
1134 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001135 case TemplateArgument::Type: {
1136 QualType T = TA.getAsType();
1137 mangleType(T, SourceRange(), QMM_Escape);
1138 break;
1139 }
David Majnemere8fdc062013-08-13 01:25:35 +00001140 case TemplateArgument::Declaration: {
1141 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
David Majnemer1e378e42014-02-06 12:46:52 +00001142 if (isa<FieldDecl>(ND) || isa<IndirectFieldDecl>(ND)) {
Reid Klecknere253b092014-02-08 01:15:37 +00001143 mangleMemberDataPointer(
1144 cast<CXXRecordDecl>(ND->getDeclContext())->getMostRecentDecl(),
1145 cast<ValueDecl>(ND));
Reid Kleckner09b47d12014-02-05 18:59:38 +00001146 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Nick Lewycky1f529662014-02-05 23:53:29 +00001147 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
Reid Kleckner09b47d12014-02-05 18:59:38 +00001148 if (MD && MD->isInstance())
Reid Klecknere253b092014-02-08 01:15:37 +00001149 mangleMemberFunctionPointer(MD->getParent()->getMostRecentDecl(), MD);
Reid Kleckner09b47d12014-02-05 18:59:38 +00001150 else
Nick Lewycky1f529662014-02-05 23:53:29 +00001151 mangle(FD, "$1?");
Reid Kleckner09b47d12014-02-05 18:59:38 +00001152 } else {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001153 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner09b47d12014-02-05 18:59:38 +00001154 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001155 break;
David Majnemere8fdc062013-08-13 01:25:35 +00001156 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001157 case TemplateArgument::Integral:
1158 mangleIntegerLiteral(TA.getAsIntegral(),
1159 TA.getIntegralType()->isBooleanType());
1160 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001161 case TemplateArgument::NullPtr: {
1162 QualType T = TA.getNullPtrType();
1163 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
David Majnemer763584d2014-02-06 10:59:19 +00001164 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
Reid Kleckner96f8f932014-02-05 17:27:08 +00001165 if (MPT->isMemberFunctionPointerType())
Craig Topper36250ad2014-05-12 05:36:57 +00001166 mangleMemberFunctionPointer(RD, nullptr);
Reid Kleckner96f8f932014-02-05 17:27:08 +00001167 else
Craig Topper36250ad2014-05-12 05:36:57 +00001168 mangleMemberDataPointer(RD, nullptr);
Reid Kleckner96f8f932014-02-05 17:27:08 +00001169 } else {
1170 Out << "$0A@";
1171 }
David Majnemerae465ef2013-08-05 21:33:59 +00001172 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001173 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001174 case TemplateArgument::Expression:
1175 mangleExpression(TA.getAsExpr());
1176 break;
David Majnemer06fa05a2014-06-04 16:46:32 +00001177 case TemplateArgument::Pack: {
1178 llvm::ArrayRef<TemplateArgument> TemplateArgs = TA.getPackAsArray();
1179 if (TemplateArgs.empty()) {
1180 Out << "$S";
1181 } else {
1182 for (const TemplateArgument &PA : TemplateArgs)
1183 mangleTemplateArg(TD, PA);
1184 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001185 break;
David Majnemer06fa05a2014-06-04 16:46:32 +00001186 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001187 case TemplateArgument::Template:
David Majnemer0db0ca42013-08-05 22:26:46 +00001188 mangleType(cast<TagDecl>(
1189 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1190 break;
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001191 }
1192}
1193
Guy Benyei11169dd2012-12-18 14:30:41 +00001194void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1195 bool IsMember) {
1196 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1197 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1198 // 'I' means __restrict (32/64-bit).
1199 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1200 // keyword!
1201 // <base-cvr-qualifiers> ::= A # near
1202 // ::= B # near const
1203 // ::= C # near volatile
1204 // ::= D # near const volatile
1205 // ::= E # far (16-bit)
1206 // ::= F # far const (16-bit)
1207 // ::= G # far volatile (16-bit)
1208 // ::= H # far const volatile (16-bit)
1209 // ::= I # huge (16-bit)
1210 // ::= J # huge const (16-bit)
1211 // ::= K # huge volatile (16-bit)
1212 // ::= L # huge const volatile (16-bit)
1213 // ::= M <basis> # based
1214 // ::= N <basis> # based const
1215 // ::= O <basis> # based volatile
1216 // ::= P <basis> # based const volatile
1217 // ::= Q # near member
1218 // ::= R # near const member
1219 // ::= S # near volatile member
1220 // ::= T # near const volatile member
1221 // ::= U # far member (16-bit)
1222 // ::= V # far const member (16-bit)
1223 // ::= W # far volatile member (16-bit)
1224 // ::= X # far const volatile member (16-bit)
1225 // ::= Y # huge member (16-bit)
1226 // ::= Z # huge const member (16-bit)
1227 // ::= 0 # huge volatile member (16-bit)
1228 // ::= 1 # huge const volatile member (16-bit)
1229 // ::= 2 <basis> # based member
1230 // ::= 3 <basis> # based const member
1231 // ::= 4 <basis> # based volatile member
1232 // ::= 5 <basis> # based const volatile member
1233 // ::= 6 # near function (pointers only)
1234 // ::= 7 # far function (pointers only)
1235 // ::= 8 # near method (pointers only)
1236 // ::= 9 # far method (pointers only)
1237 // ::= _A <basis> # based function (pointers only)
1238 // ::= _B <basis> # based function (far?) (pointers only)
1239 // ::= _C <basis> # based method (pointers only)
1240 // ::= _D <basis> # based method (far?) (pointers only)
1241 // ::= _E # block (Clang)
1242 // <basis> ::= 0 # __based(void)
1243 // ::= 1 # __based(segment)?
1244 // ::= 2 <name> # __based(name)
1245 // ::= 3 # ?
1246 // ::= 4 # ?
1247 // ::= 5 # not really based
1248 bool HasConst = Quals.hasConst(),
1249 HasVolatile = Quals.hasVolatile();
David Majnemer89594f32013-08-05 22:43:06 +00001250
Guy Benyei11169dd2012-12-18 14:30:41 +00001251 if (!IsMember) {
1252 if (HasConst && HasVolatile) {
1253 Out << 'D';
1254 } else if (HasVolatile) {
1255 Out << 'C';
1256 } else if (HasConst) {
1257 Out << 'B';
1258 } else {
1259 Out << 'A';
1260 }
1261 } else {
1262 if (HasConst && HasVolatile) {
1263 Out << 'T';
1264 } else if (HasVolatile) {
1265 Out << 'S';
1266 } else if (HasConst) {
1267 Out << 'R';
1268 } else {
1269 Out << 'Q';
1270 }
1271 }
1272
1273 // FIXME: For now, just drop all extension qualifiers on the floor.
1274}
1275
David Majnemer8eec58f2014-02-18 14:20:10 +00001276void
David Majnemere3785bb2014-04-23 05:16:56 +00001277MicrosoftCXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1278 // <ref-qualifier> ::= G # lvalue reference
1279 // ::= H # rvalue-reference
1280 switch (RefQualifier) {
1281 case RQ_None:
1282 break;
1283
1284 case RQ_LValue:
1285 Out << 'G';
1286 break;
1287
1288 case RQ_RValue:
1289 Out << 'H';
1290 break;
1291 }
1292}
1293
1294void
David Majnemer8eec58f2014-02-18 14:20:10 +00001295MicrosoftCXXNameMangler::manglePointerExtQualifiers(Qualifiers Quals,
1296 const Type *PointeeType) {
1297 bool HasRestrict = Quals.hasRestrict();
1298 if (PointersAre64Bit && (!PointeeType || !PointeeType->isFunctionType()))
1299 Out << 'E';
1300
1301 if (HasRestrict)
1302 Out << 'I';
1303}
1304
1305void MicrosoftCXXNameMangler::manglePointerCVQualifiers(Qualifiers Quals) {
1306 // <pointer-cv-qualifiers> ::= P # no qualifiers
1307 // ::= Q # const
1308 // ::= R # volatile
1309 // ::= S # const volatile
Guy Benyei11169dd2012-12-18 14:30:41 +00001310 bool HasConst = Quals.hasConst(),
David Majnemer8eec58f2014-02-18 14:20:10 +00001311 HasVolatile = Quals.hasVolatile();
David Majnemer0b6bf8a2014-02-18 12:58:35 +00001312
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 if (HasConst && HasVolatile) {
1314 Out << 'S';
1315 } else if (HasVolatile) {
1316 Out << 'R';
1317 } else if (HasConst) {
1318 Out << 'Q';
1319 } else {
1320 Out << 'P';
1321 }
1322}
1323
1324void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1325 SourceRange Range) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001326 // MSVC will backreference two canonically equivalent types that have slightly
1327 // different manglings when mangled alone.
David Majnemer5a1b2042013-09-11 04:44:30 +00001328
1329 // Decayed types do not match up with non-decayed versions of the same type.
1330 //
1331 // e.g.
1332 // void (*x)(void) will not form a backreference with void x(void)
1333 void *TypePtr;
1334 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1335 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1336 // If the original parameter was textually written as an array,
1337 // instead treat the decayed parameter like it's const.
1338 //
1339 // e.g.
1340 // int [] -> int * const
1341 if (DT->getOriginalType()->isArrayType())
1342 T = T.withConst();
1343 } else
1344 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1345
Guy Benyei11169dd2012-12-18 14:30:41 +00001346 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1347
1348 if (Found == TypeBackReferences.end()) {
1349 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1350
David Majnemer5a1b2042013-09-11 04:44:30 +00001351 mangleType(T, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001352
1353 // See if it's worth creating a back reference.
1354 // Only types longer than 1 character are considered
1355 // and only 10 back references slots are available:
1356 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1357 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1358 size_t Size = TypeBackReferences.size();
1359 TypeBackReferences[TypePtr] = Size;
1360 }
1361 } else {
1362 Out << Found->second;
1363 }
1364}
1365
1366void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourne2816c022013-04-25 04:25:40 +00001367 QualifierMangleMode QMM) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001368 // Don't use the canonical types. MSVC includes things like 'const' on
1369 // pointer arguments to function pointers that canonicalization strips away.
1370 T = T.getDesugaredType(getASTContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001371 Qualifiers Quals = T.getLocalQualifiers();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001372 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1373 // If there were any Quals, getAsArrayType() pushed them onto the array
1374 // element type.
Peter Collingbourne2816c022013-04-25 04:25:40 +00001375 if (QMM == QMM_Mangle)
1376 Out << 'A';
1377 else if (QMM == QMM_Escape || QMM == QMM_Result)
1378 Out << "$$B";
Reid Kleckner18da98e2013-06-24 19:21:52 +00001379 mangleArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001380 return;
1381 }
1382
1383 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1384 T->isBlockPointerType();
1385
1386 switch (QMM) {
1387 case QMM_Drop:
1388 break;
1389 case QMM_Mangle:
1390 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1391 Out << '6';
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001392 mangleFunctionType(FT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001393 return;
1394 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 mangleQualifiers(Quals, false);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001396 break;
1397 case QMM_Escape:
1398 if (!IsPointer && Quals) {
1399 Out << "$$C";
1400 mangleQualifiers(Quals, false);
1401 }
1402 break;
1403 case QMM_Result:
1404 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1405 Out << '?';
1406 mangleQualifiers(Quals, false);
1407 }
1408 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 }
1410
Peter Collingbourne2816c022013-04-25 04:25:40 +00001411 // We have to mangle these now, while we still have enough information.
David Majnemer8eec58f2014-02-18 14:20:10 +00001412 if (IsPointer) {
1413 manglePointerCVQualifiers(Quals);
1414 manglePointerExtQualifiers(Quals, T->getPointeeType().getTypePtr());
1415 }
Peter Collingbourne2816c022013-04-25 04:25:40 +00001416 const Type *ty = T.getTypePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001417
1418 switch (ty->getTypeClass()) {
1419#define ABSTRACT_TYPE(CLASS, PARENT)
1420#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1421 case Type::CLASS: \
1422 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1423 return;
1424#define TYPE(CLASS, PARENT) \
1425 case Type::CLASS: \
1426 mangleType(cast<CLASS##Type>(ty), Range); \
1427 break;
1428#include "clang/AST/TypeNodes.def"
1429#undef ABSTRACT_TYPE
1430#undef NON_CANONICAL_TYPE
1431#undef TYPE
1432 }
1433}
1434
1435void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1436 SourceRange Range) {
1437 // <type> ::= <builtin-type>
1438 // <builtin-type> ::= X # void
1439 // ::= C # signed char
1440 // ::= D # char
1441 // ::= E # unsigned char
1442 // ::= F # short
1443 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1444 // ::= H # int
1445 // ::= I # unsigned int
1446 // ::= J # long
1447 // ::= K # unsigned long
1448 // L # <none>
1449 // ::= M # float
1450 // ::= N # double
1451 // ::= O # long double (__float80 is mangled differently)
1452 // ::= _J # long long, __int64
1453 // ::= _K # unsigned long long, __int64
1454 // ::= _L # __int128
1455 // ::= _M # unsigned __int128
1456 // ::= _N # bool
1457 // _O # <array in parameter>
1458 // ::= _T # __float80 (Intel)
1459 // ::= _W # wchar_t
1460 // ::= _Z # __float80 (Digital Mars)
1461 switch (T->getKind()) {
1462 case BuiltinType::Void: Out << 'X'; break;
1463 case BuiltinType::SChar: Out << 'C'; break;
1464 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1465 case BuiltinType::UChar: Out << 'E'; break;
1466 case BuiltinType::Short: Out << 'F'; break;
1467 case BuiltinType::UShort: Out << 'G'; break;
1468 case BuiltinType::Int: Out << 'H'; break;
1469 case BuiltinType::UInt: Out << 'I'; break;
1470 case BuiltinType::Long: Out << 'J'; break;
1471 case BuiltinType::ULong: Out << 'K'; break;
1472 case BuiltinType::Float: Out << 'M'; break;
1473 case BuiltinType::Double: Out << 'N'; break;
1474 // TODO: Determine size and mangle accordingly
1475 case BuiltinType::LongDouble: Out << 'O'; break;
1476 case BuiltinType::LongLong: Out << "_J"; break;
1477 case BuiltinType::ULongLong: Out << "_K"; break;
1478 case BuiltinType::Int128: Out << "_L"; break;
1479 case BuiltinType::UInt128: Out << "_M"; break;
1480 case BuiltinType::Bool: Out << "_N"; break;
1481 case BuiltinType::WChar_S:
1482 case BuiltinType::WChar_U: Out << "_W"; break;
1483
1484#define BUILTIN_TYPE(Id, SingletonId)
1485#define PLACEHOLDER_TYPE(Id, SingletonId) \
1486 case BuiltinType::Id:
1487#include "clang/AST/BuiltinTypes.def"
1488 case BuiltinType::Dependent:
1489 llvm_unreachable("placeholder types shouldn't get to name mangling");
1490
1491 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1492 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1493 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00001494
1495 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1496 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1497 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1498 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1499 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1500 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei61054192013-02-07 10:55:47 +00001501 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001502 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
David Majnemerb4119f72013-12-13 01:06:04 +00001503
Guy Benyei11169dd2012-12-18 14:30:41 +00001504 case BuiltinType::NullPtr: Out << "$$T"; break;
1505
1506 case BuiltinType::Char16:
1507 case BuiltinType::Char32:
1508 case BuiltinType::Half: {
1509 DiagnosticsEngine &Diags = Context.getDiags();
1510 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1511 "cannot mangle this built-in %0 type yet");
1512 Diags.Report(Range.getBegin(), DiagID)
1513 << T->getName(Context.getASTContext().getPrintingPolicy())
1514 << Range;
1515 break;
1516 }
1517 }
1518}
1519
1520// <type> ::= <function-type>
1521void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1522 SourceRange) {
1523 // Structors only appear in decls, so at this point we know it's not a
1524 // structor type.
1525 // FIXME: This may not be lambda-friendly.
1526 Out << "$$A6";
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001527 mangleFunctionType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001528}
1529void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1530 SourceRange) {
1531 llvm_unreachable("Can't mangle K&R function prototypes");
1532}
1533
Peter Collingbourne2816c022013-04-25 04:25:40 +00001534void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1535 const FunctionDecl *D,
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001536 bool ForceInstMethod) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001537 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1538 // <return-type> <argument-list> <throw-spec>
1539 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1540
Reid Kleckner18da98e2013-06-24 19:21:52 +00001541 SourceRange Range;
1542 if (D) Range = D->getSourceRange();
1543
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001544 bool IsStructor = false, IsInstMethod = ForceInstMethod;
1545 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1546 if (MD->isInstance())
1547 IsInstMethod = true;
1548 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
1549 IsStructor = true;
1550 }
1551
Guy Benyei11169dd2012-12-18 14:30:41 +00001552 // If this is a C++ instance method, mangle the CVR qualifiers for the
1553 // this pointer.
David Majnemer6dda7bb2013-08-15 08:13:23 +00001554 if (IsInstMethod) {
David Majnemer8eec58f2014-02-18 14:20:10 +00001555 Qualifiers Quals = Qualifiers::fromCVRMask(Proto->getTypeQuals());
Craig Topper36250ad2014-05-12 05:36:57 +00001556 manglePointerExtQualifiers(Quals, nullptr);
David Majnemere3785bb2014-04-23 05:16:56 +00001557 mangleRefQualifier(Proto->getRefQualifier());
David Majnemer8eec58f2014-02-18 14:20:10 +00001558 mangleQualifiers(Quals, false);
David Majnemer6dda7bb2013-08-15 08:13:23 +00001559 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001560
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001561 mangleCallingConvention(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001562
1563 // <return-type> ::= <type>
1564 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001565 if (IsStructor) {
1566 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1567 StructorType == Dtor_Deleting) {
1568 // The scalar deleting destructor takes an extra int argument.
1569 // However, the FunctionType generated has 0 arguments.
1570 // FIXME: This is a temporary hack.
1571 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanovf46993e2013-08-26 10:32:04 +00001572 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001573 return;
1574 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001575 Out << '@';
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001576 } else {
Alp Toker314cc812014-01-25 16:55:45 +00001577 QualType ResultType = Proto->getReturnType();
David Majnemer2e1e04912014-04-01 05:29:46 +00001578 if (const auto *AT =
1579 dyn_cast_or_null<AutoType>(ResultType->getContainedAutoType())) {
1580 Out << '?';
1581 mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false);
1582 Out << '?';
1583 mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>");
1584 Out << '@';
1585 } else {
1586 if (ResultType->isVoidType())
1587 ResultType = ResultType.getUnqualifiedType();
1588 mangleType(ResultType, Range, QMM_Result);
1589 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 }
1591
1592 // <argument-list> ::= X # void
1593 // ::= <type>+ @
1594 // ::= <type>* Z # varargs
Alp Toker9cacbab2014-01-20 20:26:09 +00001595 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001596 Out << 'X';
1597 } else {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001598 // Happens for function pointer type arguments for example.
David Majnemerb926dbc2014-04-23 05:16:53 +00001599 for (const QualType Arg : Proto->param_types())
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001600 mangleArgumentType(Arg, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001601 // <builtin-type> ::= Z # ellipsis
1602 if (Proto->isVariadic())
1603 Out << 'Z';
1604 else
1605 Out << '@';
1606 }
1607
1608 mangleThrowSpecification(Proto);
1609}
1610
1611void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Kleckner369f3162013-05-14 20:30:42 +00001612 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1613 // # pointer. in 64-bit mode *all*
1614 // # 'this' pointers are 64-bit.
1615 // ::= <global-function>
1616 // <member-function> ::= A # private: near
1617 // ::= B # private: far
1618 // ::= C # private: static near
1619 // ::= D # private: static far
1620 // ::= E # private: virtual near
1621 // ::= F # private: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001622 // ::= I # protected: near
1623 // ::= J # protected: far
1624 // ::= K # protected: static near
1625 // ::= L # protected: static far
1626 // ::= M # protected: virtual near
1627 // ::= N # protected: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001628 // ::= Q # public: near
1629 // ::= R # public: far
1630 // ::= S # public: static near
1631 // ::= T # public: static far
1632 // ::= U # public: virtual near
1633 // ::= V # public: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001634 // <global-function> ::= Y # global near
1635 // ::= Z # global far
Guy Benyei11169dd2012-12-18 14:30:41 +00001636 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1637 switch (MD->getAccess()) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001638 case AS_none:
1639 llvm_unreachable("Unsupported access specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00001640 case AS_private:
1641 if (MD->isStatic())
1642 Out << 'C';
1643 else if (MD->isVirtual())
1644 Out << 'E';
1645 else
1646 Out << 'A';
1647 break;
1648 case AS_protected:
1649 if (MD->isStatic())
1650 Out << 'K';
1651 else if (MD->isVirtual())
1652 Out << 'M';
1653 else
1654 Out << 'I';
1655 break;
1656 case AS_public:
1657 if (MD->isStatic())
1658 Out << 'S';
1659 else if (MD->isVirtual())
1660 Out << 'U';
1661 else
1662 Out << 'Q';
1663 }
1664 } else
1665 Out << 'Y';
1666}
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001667void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001668 // <calling-convention> ::= A # __cdecl
1669 // ::= B # __export __cdecl
1670 // ::= C # __pascal
1671 // ::= D # __export __pascal
1672 // ::= E # __thiscall
1673 // ::= F # __export __thiscall
1674 // ::= G # __stdcall
1675 // ::= H # __export __stdcall
1676 // ::= I # __fastcall
1677 // ::= J # __export __fastcall
1678 // The 'export' calling conventions are from a bygone era
1679 // (*cough*Win16*cough*) when functions were declared for export with
1680 // that keyword. (It didn't actually export them, it just made them so
1681 // that they could be in a DLL and somebody from another module could call
1682 // them.)
1683 CallingConv CC = T->getCallConv();
Guy Benyei11169dd2012-12-18 14:30:41 +00001684 switch (CC) {
1685 default:
1686 llvm_unreachable("Unsupported CC for mangling");
Charles Davisb5a214e2013-08-30 04:39:01 +00001687 case CC_X86_64Win64:
1688 case CC_X86_64SysV:
Guy Benyei11169dd2012-12-18 14:30:41 +00001689 case CC_C: Out << 'A'; break;
1690 case CC_X86Pascal: Out << 'C'; break;
1691 case CC_X86ThisCall: Out << 'E'; break;
1692 case CC_X86StdCall: Out << 'G'; break;
1693 case CC_X86FastCall: Out << 'I'; break;
1694 }
1695}
1696void MicrosoftCXXNameMangler::mangleThrowSpecification(
1697 const FunctionProtoType *FT) {
1698 // <throw-spec> ::= Z # throw(...) (default)
1699 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1700 // ::= <type>+
1701 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1702 // all actually mangled as 'Z'. (They're ignored because their associated
1703 // functionality isn't implemented, and probably never will be.)
1704 Out << 'Z';
1705}
1706
1707void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1708 SourceRange Range) {
1709 // Probably should be mangled as a template instantiation; need to see what
1710 // VC does first.
1711 DiagnosticsEngine &Diags = Context.getDiags();
1712 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1713 "cannot mangle this unresolved dependent type yet");
1714 Diags.Report(Range.getBegin(), DiagID)
1715 << Range;
1716}
1717
1718// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1719// <union-type> ::= T <name>
1720// <struct-type> ::= U <name>
1721// <class-type> ::= V <name>
David Majnemer048f90c2013-12-09 04:28:34 +00001722// <enum-type> ::= W4 <name>
Guy Benyei11169dd2012-12-18 14:30:41 +00001723void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001724 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001725}
1726void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001727 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001728}
David Majnemer0db0ca42013-08-05 22:26:46 +00001729void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1730 switch (TD->getTagKind()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001731 case TTK_Union:
1732 Out << 'T';
1733 break;
1734 case TTK_Struct:
1735 case TTK_Interface:
1736 Out << 'U';
1737 break;
1738 case TTK_Class:
1739 Out << 'V';
1740 break;
1741 case TTK_Enum:
David Majnemer048f90c2013-12-09 04:28:34 +00001742 Out << "W4";
Guy Benyei11169dd2012-12-18 14:30:41 +00001743 break;
1744 }
David Majnemer0db0ca42013-08-05 22:26:46 +00001745 mangleName(TD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001746}
1747
1748// <type> ::= <array-type>
1749// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1750// [Y <dimension-count> <dimension>+]
Reid Kleckner369f3162013-05-14 20:30:42 +00001751// <element-type> # as global, E is never required
Guy Benyei11169dd2012-12-18 14:30:41 +00001752// It's supposed to be the other way around, but for some strange reason, it
1753// isn't. Today this behavior is retained for the sole purpose of backwards
1754// compatibility.
David Majnemer5a1b2042013-09-11 04:44:30 +00001755void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001756 // This isn't a recursive mangling, so now we have to do it all in this
1757 // one call.
David Majnemer8eec58f2014-02-18 14:20:10 +00001758 manglePointerCVQualifiers(T->getElementType().getQualifiers());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001759 mangleType(T->getElementType(), SourceRange());
Guy Benyei11169dd2012-12-18 14:30:41 +00001760}
1761void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1762 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001763 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001764}
1765void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1766 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001767 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001768}
1769void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1770 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001771 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001772}
1773void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1774 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001775 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001776}
Reid Kleckner18da98e2013-06-24 19:21:52 +00001777void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001778 QualType ElementTy(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 SmallVector<llvm::APInt, 3> Dimensions;
1780 for (;;) {
1781 if (const ConstantArrayType *CAT =
David Majnemer02e9af42014-04-23 05:16:51 +00001782 getASTContext().getAsConstantArrayType(ElementTy)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001783 Dimensions.push_back(CAT->getSize());
1784 ElementTy = CAT->getElementType();
1785 } else if (ElementTy->isVariableArrayType()) {
1786 const VariableArrayType *VAT =
1787 getASTContext().getAsVariableArrayType(ElementTy);
1788 DiagnosticsEngine &Diags = Context.getDiags();
1789 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1790 "cannot mangle this variable-length array yet");
1791 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1792 << VAT->getBracketsRange();
1793 return;
1794 } else if (ElementTy->isDependentSizedArrayType()) {
1795 // The dependent expression has to be folded into a constant (TODO).
1796 const DependentSizedArrayType *DSAT =
1797 getASTContext().getAsDependentSizedArrayType(ElementTy);
1798 DiagnosticsEngine &Diags = Context.getDiags();
1799 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1800 "cannot mangle this dependent-length array yet");
1801 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1802 << DSAT->getBracketsRange();
1803 return;
Peter Collingbourne2816c022013-04-25 04:25:40 +00001804 } else if (const IncompleteArrayType *IAT =
David Majnemer02e9af42014-04-23 05:16:51 +00001805 getASTContext().getAsIncompleteArrayType(ElementTy)) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001806 Dimensions.push_back(llvm::APInt(32, 0));
1807 ElementTy = IAT->getElementType();
1808 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001809 else break;
1810 }
Peter Collingbourne2816c022013-04-25 04:25:40 +00001811 Out << 'Y';
1812 // <dimension-count> ::= <number> # number of extra dimensions
1813 mangleNumber(Dimensions.size());
David Majnemerb926dbc2014-04-23 05:16:53 +00001814 for (const llvm::APInt &Dimension : Dimensions)
1815 mangleNumber(Dimension.getLimitedValue());
Reid Kleckner18da98e2013-06-24 19:21:52 +00001816 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei11169dd2012-12-18 14:30:41 +00001817}
1818
1819// <type> ::= <pointer-to-member-type>
1820// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1821// <class name> <type>
1822void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1823 SourceRange Range) {
1824 QualType PointeeType = T->getPointeeType();
1825 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1826 Out << '8';
1827 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Craig Topper36250ad2014-05-12 05:36:57 +00001828 mangleFunctionType(FPT, nullptr, true);
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 } else {
1830 mangleQualifiers(PointeeType.getQualifiers(), true);
1831 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001832 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001833 }
1834}
1835
1836void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1837 SourceRange Range) {
1838 DiagnosticsEngine &Diags = Context.getDiags();
1839 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1840 "cannot mangle this template type parameter type yet");
1841 Diags.Report(Range.getBegin(), DiagID)
1842 << Range;
1843}
1844
1845void MicrosoftCXXNameMangler::mangleType(
1846 const SubstTemplateTypeParmPackType *T,
1847 SourceRange Range) {
1848 DiagnosticsEngine &Diags = Context.getDiags();
1849 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1850 "cannot mangle this substituted parameter pack yet");
1851 Diags.Report(Range.getBegin(), DiagID)
1852 << Range;
1853}
1854
1855// <type> ::= <pointer-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001856// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001857// # the E is required for 64-bit non-static pointers
Guy Benyei11169dd2012-12-18 14:30:41 +00001858void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1859 SourceRange Range) {
1860 QualType PointeeTy = T->getPointeeType();
Peter Collingbourne2816c022013-04-25 04:25:40 +00001861 mangleType(PointeeTy, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001862}
1863void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1864 SourceRange Range) {
1865 // Object pointers never have qualifiers.
1866 Out << 'A';
David Majnemer8eec58f2014-02-18 14:20:10 +00001867 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 mangleType(T->getPointeeType(), Range);
1869}
1870
1871// <type> ::= <reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001872// <reference-type> ::= A E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001873// # the E is required for 64-bit non-static lvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001874void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1875 SourceRange Range) {
1876 Out << 'A';
David Majnemer8eec58f2014-02-18 14:20:10 +00001877 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001878 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001879}
1880
1881// <type> ::= <r-value-reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001882// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001883// # the E is required for 64-bit non-static rvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001884void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1885 SourceRange Range) {
1886 Out << "$$Q";
David Majnemer8eec58f2014-02-18 14:20:10 +00001887 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001888 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001889}
1890
1891void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1892 SourceRange Range) {
1893 DiagnosticsEngine &Diags = Context.getDiags();
1894 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1895 "cannot mangle this complex number type yet");
1896 Diags.Report(Range.getBegin(), DiagID)
1897 << Range;
1898}
1899
1900void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1901 SourceRange Range) {
Reid Klecknere7e64d82013-03-26 16:56:59 +00001902 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1903 assert(ET && "vectors with non-builtin elements are unsupported");
1904 uint64_t Width = getASTContext().getTypeSize(T);
1905 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1906 // doesn't match the Intel types uses a custom mangling below.
1907 bool IntelVector = true;
1908 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1909 Out << "T__m64";
1910 } else if (Width == 128 || Width == 256) {
1911 if (ET->getKind() == BuiltinType::Float)
1912 Out << "T__m" << Width;
1913 else if (ET->getKind() == BuiltinType::LongLong)
1914 Out << "T__m" << Width << 'i';
1915 else if (ET->getKind() == BuiltinType::Double)
1916 Out << "U__m" << Width << 'd';
1917 else
1918 IntelVector = false;
1919 } else {
1920 IntelVector = false;
1921 }
1922
1923 if (!IntelVector) {
1924 // The MS ABI doesn't have a special mangling for vector types, so we define
1925 // our own mangling to handle uses of __vector_size__ on user-specified
1926 // types, and for extensions like __v4sf.
1927 Out << "T__clang_vec" << T->getNumElements() << '_';
1928 mangleType(ET, Range);
1929 }
1930
1931 Out << "@@";
Guy Benyei11169dd2012-12-18 14:30:41 +00001932}
Reid Klecknere7e64d82013-03-26 16:56:59 +00001933
Guy Benyei11169dd2012-12-18 14:30:41 +00001934void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1935 SourceRange Range) {
1936 DiagnosticsEngine &Diags = Context.getDiags();
1937 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1938 "cannot mangle this extended vector type yet");
1939 Diags.Report(Range.getBegin(), DiagID)
1940 << Range;
1941}
1942void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1943 SourceRange Range) {
1944 DiagnosticsEngine &Diags = Context.getDiags();
1945 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1946 "cannot mangle this dependent-sized extended vector type yet");
1947 Diags.Report(Range.getBegin(), DiagID)
1948 << Range;
1949}
1950
1951void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1952 SourceRange) {
1953 // ObjC interfaces have structs underlying them.
1954 Out << 'U';
1955 mangleName(T->getDecl());
1956}
1957
1958void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1959 SourceRange Range) {
1960 // We don't allow overloading by different protocol qualification,
1961 // so mangling them isn't necessary.
1962 mangleType(T->getBaseType(), Range);
1963}
1964
1965void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1966 SourceRange Range) {
1967 Out << "_E";
1968
1969 QualType pointee = T->getPointeeType();
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001970 mangleFunctionType(pointee->castAs<FunctionProtoType>());
Guy Benyei11169dd2012-12-18 14:30:41 +00001971}
1972
David Majnemerf0a84f22013-08-16 08:29:13 +00001973void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1974 SourceRange) {
1975 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei11169dd2012-12-18 14:30:41 +00001976}
1977
1978void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1979 SourceRange Range) {
1980 DiagnosticsEngine &Diags = Context.getDiags();
1981 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1982 "cannot mangle this template specialization type yet");
1983 Diags.Report(Range.getBegin(), DiagID)
1984 << Range;
1985}
1986
1987void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1988 SourceRange Range) {
1989 DiagnosticsEngine &Diags = Context.getDiags();
1990 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1991 "cannot mangle this dependent name type yet");
1992 Diags.Report(Range.getBegin(), DiagID)
1993 << Range;
1994}
1995
1996void MicrosoftCXXNameMangler::mangleType(
1997 const DependentTemplateSpecializationType *T,
1998 SourceRange Range) {
1999 DiagnosticsEngine &Diags = Context.getDiags();
2000 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2001 "cannot mangle this dependent template specialization type yet");
2002 Diags.Report(Range.getBegin(), DiagID)
2003 << Range;
2004}
2005
2006void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
2007 SourceRange Range) {
2008 DiagnosticsEngine &Diags = Context.getDiags();
2009 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2010 "cannot mangle this pack expansion yet");
2011 Diags.Report(Range.getBegin(), DiagID)
2012 << Range;
2013}
2014
2015void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
2016 SourceRange Range) {
2017 DiagnosticsEngine &Diags = Context.getDiags();
2018 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2019 "cannot mangle this typeof(type) yet");
2020 Diags.Report(Range.getBegin(), DiagID)
2021 << Range;
2022}
2023
2024void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
2025 SourceRange Range) {
2026 DiagnosticsEngine &Diags = Context.getDiags();
2027 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2028 "cannot mangle this typeof(expression) yet");
2029 Diags.Report(Range.getBegin(), DiagID)
2030 << Range;
2031}
2032
2033void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
2034 SourceRange Range) {
2035 DiagnosticsEngine &Diags = Context.getDiags();
2036 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2037 "cannot mangle this decltype() yet");
2038 Diags.Report(Range.getBegin(), DiagID)
2039 << Range;
2040}
2041
2042void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
2043 SourceRange Range) {
2044 DiagnosticsEngine &Diags = Context.getDiags();
2045 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2046 "cannot mangle this unary transform type yet");
2047 Diags.Report(Range.getBegin(), DiagID)
2048 << Range;
2049}
2050
2051void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
David Majnemerb9a5f2d2014-01-21 20:33:36 +00002052 assert(T->getDeducedType().isNull() && "expecting a dependent type!");
2053
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 DiagnosticsEngine &Diags = Context.getDiags();
2055 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2056 "cannot mangle this 'auto' type yet");
2057 Diags.Report(Range.getBegin(), DiagID)
2058 << Range;
2059}
2060
2061void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
2062 SourceRange Range) {
2063 DiagnosticsEngine &Diags = Context.getDiags();
2064 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2065 "cannot mangle this C11 atomic type yet");
2066 Diags.Report(Range.getBegin(), DiagID)
2067 << Range;
2068}
2069
Rafael Espindola002667c2013-10-16 01:40:34 +00002070void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D,
2071 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002072 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2073 "Invalid mangleName() call, argument is not a variable or function!");
2074 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2075 "Invalid mangleName() call on 'structor decl!");
2076
2077 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2078 getASTContext().getSourceManager(),
2079 "Mangling declaration");
2080
2081 MicrosoftCXXNameMangler Mangler(*this, Out);
2082 return Mangler.mangle(D);
2083}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002084
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002085// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
2086// <virtual-adjustment>
2087// <no-adjustment> ::= A # private near
2088// ::= B # private far
2089// ::= I # protected near
2090// ::= J # protected far
2091// ::= Q # public near
2092// ::= R # public far
2093// <static-adjustment> ::= G <static-offset> # private near
2094// ::= H <static-offset> # private far
2095// ::= O <static-offset> # protected near
2096// ::= P <static-offset> # protected far
2097// ::= W <static-offset> # public near
2098// ::= X <static-offset> # public far
2099// <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near
2100// ::= $1 <virtual-shift> <static-offset> # private far
2101// ::= $2 <virtual-shift> <static-offset> # protected near
2102// ::= $3 <virtual-shift> <static-offset> # protected far
2103// ::= $4 <virtual-shift> <static-offset> # public near
2104// ::= $5 <virtual-shift> <static-offset> # public far
2105// <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift>
2106// <vtordisp-shift> ::= <offset-to-vtordisp>
2107// <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset>
2108// <offset-to-vtordisp>
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002109static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
2110 const ThisAdjustment &Adjustment,
2111 MicrosoftCXXNameMangler &Mangler,
2112 raw_ostream &Out) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002113 if (!Adjustment.Virtual.isEmpty()) {
2114 Out << '$';
2115 char AccessSpec;
2116 switch (MD->getAccess()) {
2117 case AS_none:
2118 llvm_unreachable("Unsupported access specifier");
2119 case AS_private:
2120 AccessSpec = '0';
2121 break;
2122 case AS_protected:
2123 AccessSpec = '2';
2124 break;
2125 case AS_public:
2126 AccessSpec = '4';
2127 }
2128 if (Adjustment.Virtual.Microsoft.VBPtrOffset) {
2129 Out << 'R' << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002130 Mangler.mangleNumber(
2131 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset));
2132 Mangler.mangleNumber(
2133 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset));
2134 Mangler.mangleNumber(
2135 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2136 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002137 } else {
2138 Out << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002139 Mangler.mangleNumber(
2140 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2141 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002142 }
2143 } else if (Adjustment.NonVirtual != 0) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002144 switch (MD->getAccess()) {
2145 case AS_none:
2146 llvm_unreachable("Unsupported access specifier");
2147 case AS_private:
2148 Out << 'G';
2149 break;
2150 case AS_protected:
2151 Out << 'O';
2152 break;
2153 case AS_public:
2154 Out << 'W';
2155 }
David Majnemer2a816452013-12-09 10:44:32 +00002156 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002157 } else {
2158 switch (MD->getAccess()) {
2159 case AS_none:
2160 llvm_unreachable("Unsupported access specifier");
2161 case AS_private:
2162 Out << 'A';
2163 break;
2164 case AS_protected:
2165 Out << 'I';
2166 break;
2167 case AS_public:
2168 Out << 'Q';
2169 }
2170 }
2171}
2172
Reid Kleckner96f8f932014-02-05 17:27:08 +00002173void
2174MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
2175 raw_ostream &Out) {
2176 MicrosoftVTableContext *VTContext =
2177 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
2178 const MicrosoftVTableContext::MethodVFTableLocation &ML =
2179 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
Hans Wennborg88497d62013-11-15 17:24:45 +00002180
2181 MicrosoftCXXNameMangler Mangler(*this, Out);
Reid Kleckner96f8f932014-02-05 17:27:08 +00002182 Mangler.getStream() << "\01?";
2183 Mangler.mangleVirtualMemPtrThunk(MD, ML);
Hans Wennborg88497d62013-11-15 17:24:45 +00002184}
2185
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002186void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
2187 const ThunkInfo &Thunk,
2188 raw_ostream &Out) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002189 MicrosoftCXXNameMangler Mangler(*this, Out);
2190 Out << "\01?";
2191 Mangler.mangleName(MD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002192 mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out);
2193 if (!Thunk.Return.isEmpty())
Craig Topper36250ad2014-05-12 05:36:57 +00002194 assert(Thunk.Method != nullptr &&
2195 "Thunk info should hold the overridee decl");
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002196
2197 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD;
2198 Mangler.mangleFunctionType(
2199 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002200}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002201
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002202void MicrosoftMangleContextImpl::mangleCXXDtorThunk(
2203 const CXXDestructorDecl *DD, CXXDtorType Type,
2204 const ThisAdjustment &Adjustment, raw_ostream &Out) {
2205 // FIXME: Actually, the dtor thunk should be emitted for vector deleting
2206 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor
2207 // mangling manually until we support both deleting dtor types.
2208 assert(Type == Dtor_Deleting);
2209 MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type);
2210 Out << "\01??_E";
2211 Mangler.mangleName(DD->getParent());
2212 mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out);
2213 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002214}
Reid Kleckner7810af02013-06-19 15:20:38 +00002215
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002216void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002217 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2218 raw_ostream &Out) {
Reid Kleckner7810af02013-06-19 15:20:38 +00002219 // <mangled-name> ::= ?_7 <class-name> <storage-class>
2220 // <cvr-qualifiers> [<name>] @
Guy Benyei11169dd2012-12-18 14:30:41 +00002221 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner7810af02013-06-19 15:20:38 +00002222 // is always '6' for vftables.
Guy Benyei11169dd2012-12-18 14:30:41 +00002223 MicrosoftCXXNameMangler Mangler(*this, Out);
2224 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002225 Mangler.mangleName(Derived);
2226 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
David Majnemerb926dbc2014-04-23 05:16:53 +00002227 for (const CXXRecordDecl *RD : BasePath)
2228 Mangler.mangleName(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002229 Mangler.getStream() << '@';
2230}
Reid Kleckner7810af02013-06-19 15:20:38 +00002231
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002232void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner7810af02013-06-19 15:20:38 +00002233 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2234 raw_ostream &Out) {
2235 // <mangled-name> ::= ?_8 <class-name> <storage-class>
2236 // <cvr-qualifiers> [<name>] @
2237 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
2238 // is always '7' for vbtables.
2239 MicrosoftCXXNameMangler Mangler(*this, Out);
2240 Mangler.getStream() << "\01??_8";
2241 Mangler.mangleName(Derived);
2242 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
David Majnemerb926dbc2014-04-23 05:16:53 +00002243 for (const CXXRecordDecl *RD : BasePath)
2244 Mangler.mangleName(RD);
Reid Kleckner7810af02013-06-19 15:20:38 +00002245 Mangler.getStream() << '@';
2246}
2247
David Majnemera96b7ee2014-05-13 00:44:44 +00002248void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &Out) {
2249 MicrosoftCXXNameMangler Mangler(*this, Out);
2250 Mangler.getStream() << "\01??_R0";
2251 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result);
2252 Mangler.getStream() << "@8";
Guy Benyei11169dd2012-12-18 14:30:41 +00002253}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002254
David Majnemera96b7ee2014-05-13 00:44:44 +00002255void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T,
2256 raw_ostream &Out) {
2257 MicrosoftCXXNameMangler Mangler(*this, Out);
2258 Mangler.getStream() << '.';
2259 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result);
2260}
2261
2262void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassDescriptor(
Warren Hunt5c2b4ea2014-05-23 16:07:43 +00002263 const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset,
David Majnemeraeb55c92014-05-13 06:57:43 +00002264 uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) {
David Majnemera96b7ee2014-05-13 00:44:44 +00002265 MicrosoftCXXNameMangler Mangler(*this, Out);
2266 Mangler.getStream() << "\01??_R1";
David Majnemera96b7ee2014-05-13 00:44:44 +00002267 Mangler.mangleNumber(NVOffset);
2268 Mangler.mangleNumber(VBPtrOffset);
2269 Mangler.mangleNumber(VBTableOffset);
2270 Mangler.mangleNumber(Flags);
David Majnemeraeb55c92014-05-13 06:57:43 +00002271 Mangler.mangleName(Derived);
Warren Hunt5c2b4ea2014-05-23 16:07:43 +00002272 Mangler.getStream() << "8";
David Majnemera96b7ee2014-05-13 00:44:44 +00002273}
2274
2275void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassArray(
2276 const CXXRecordDecl *Derived, raw_ostream &Out) {
2277 MicrosoftCXXNameMangler Mangler(*this, Out);
2278 Mangler.getStream() << "\01??_R2";
2279 Mangler.mangleName(Derived);
Warren Hunt5c2b4ea2014-05-23 16:07:43 +00002280 Mangler.getStream() << "8";
David Majnemera96b7ee2014-05-13 00:44:44 +00002281}
2282
2283void MicrosoftMangleContextImpl::mangleCXXRTTIClassHierarchyDescriptor(
2284 const CXXRecordDecl *Derived, raw_ostream &Out) {
2285 MicrosoftCXXNameMangler Mangler(*this, Out);
2286 Mangler.getStream() << "\01??_R3";
2287 Mangler.mangleName(Derived);
Warren Hunt5c2b4ea2014-05-23 16:07:43 +00002288 Mangler.getStream() << "8";
David Majnemera96b7ee2014-05-13 00:44:44 +00002289}
2290
2291void MicrosoftMangleContextImpl::mangleCXXRTTICompleteObjectLocator(
David Majnemeraeb55c92014-05-13 06:57:43 +00002292 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2293 raw_ostream &Out) {
2294 // <mangled-name> ::= ?_R4 <class-name> <storage-class>
2295 // <cvr-qualifiers> [<name>] @
2296 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
2297 // is always '6' for vftables.
David Majnemera96b7ee2014-05-13 00:44:44 +00002298 MicrosoftCXXNameMangler Mangler(*this, Out);
2299 Mangler.getStream() << "\01??_R4";
2300 Mangler.mangleName(Derived);
David Majnemeraeb55c92014-05-13 06:57:43 +00002301 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
2302 for (const CXXRecordDecl *RD : BasePath)
2303 Mangler.mangleName(RD);
2304 Mangler.getStream() << '@';
Guy Benyei11169dd2012-12-18 14:30:41 +00002305}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002306
Reid Klecknercc99e262013-11-19 23:23:00 +00002307void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) {
2308 // This is just a made up unique string for the purposes of tbaa. undname
2309 // does *not* know how to demangle it.
2310 MicrosoftCXXNameMangler Mangler(*this, Out);
2311 Mangler.getStream() << '?';
2312 Mangler.mangleType(T, SourceRange());
2313}
2314
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002315void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
2316 CXXCtorType Type,
2317 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002318 MicrosoftCXXNameMangler mangler(*this, Out);
2319 mangler.mangle(D);
2320}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002321
2322void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
2323 CXXDtorType Type,
2324 raw_ostream &Out) {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00002325 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei11169dd2012-12-18 14:30:41 +00002326 mangler.mangle(D);
2327}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002328
2329void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
David Majnemerdaff3702014-05-01 17:50:17 +00002330 unsigned,
David Majnemer210e6bfa12013-12-13 00:39:38 +00002331 raw_ostream &) {
2332 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2333 "cannot mangle this reference temporary yet");
2334 getDiags().Report(VD->getLocation(), DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00002335}
2336
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002337void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
2338 raw_ostream &Out) {
David Majnemer25e1a5e2013-12-13 00:52:45 +00002339 // TODO: This is not correct, especially with respect to MSVC2013. MSVC2013
2340 // utilizes thread local variables to implement thread safe, re-entrant
2341 // initialization for statics. They no longer differentiate between an
2342 // externally visible and non-externally visible static with respect to
2343 // mangling, they all get $TSS <number>.
2344 //
2345 // N.B. This means that they can get more than 32 static variable guards in a
2346 // scope. It also means that they broke compatibility with their own ABI.
2347
David Majnemer2206bf52014-03-05 08:57:59 +00002348 // <guard-name> ::= ?_B <postfix> @5 <scope-depth>
Reid Klecknerd8110b62013-09-10 20:14:30 +00002349 // ::= ?$S <guard-num> @ <postfix> @4IA
2350
2351 // The first mangling is what MSVC uses to guard static locals in inline
2352 // functions. It uses a different mangling in external functions to support
2353 // guarding more than 32 variables. MSVC rejects inline functions with more
2354 // than 32 static locals. We don't fully implement the second mangling
2355 // because those guards are not externally visible, and instead use LLVM's
2356 // default renaming when creating a new guard variable.
2357 MicrosoftCXXNameMangler Mangler(*this, Out);
2358
2359 bool Visible = VD->isExternallyVisible();
2360 // <operator-name> ::= ?_B # local static guard
2361 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
David Majnemer34b49892014-03-06 19:57:36 +00002362 unsigned ScopeDepth = 0;
2363 if (Visible && !getNextDiscriminator(VD, ScopeDepth))
2364 // If we do not have a discriminator and are emitting a guard variable for
2365 // use at global scope, then mangling the nested name will not be enough to
2366 // remove ambiguities.
2367 Mangler.mangle(VD, "");
2368 else
2369 Mangler.mangleNestedName(VD);
David Majnemer2206bf52014-03-05 08:57:59 +00002370 Mangler.getStream() << (Visible ? "@5" : "@4IA");
David Majnemer34b49892014-03-06 19:57:36 +00002371 if (ScopeDepth)
David Majnemer2206bf52014-03-05 08:57:59 +00002372 Mangler.mangleNumber(ScopeDepth);
Reid Klecknerd8110b62013-09-10 20:14:30 +00002373}
2374
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002375void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
2376 raw_ostream &Out,
2377 char CharCode) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002378 MicrosoftCXXNameMangler Mangler(*this, Out);
2379 Mangler.getStream() << "\01??__" << CharCode;
2380 Mangler.mangleName(D);
David Majnemerf55feec2014-03-06 19:10:27 +00002381 if (D->isStaticDataMember()) {
2382 Mangler.mangleVariableEncoding(D);
2383 Mangler.getStream() << '@';
2384 }
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002385 // This is the function class mangling. These stubs are global, non-variadic,
2386 // cdecl functions that return void and take no args.
2387 Mangler.getStream() << "YAXXZ";
2388}
2389
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002390void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2391 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002392 // <initializer-name> ::= ?__E <name> YAXXZ
2393 mangleInitFiniStub(D, Out, 'E');
2394}
2395
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002396void
2397MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2398 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002399 // <destructor-name> ::= ?__F <name> YAXXZ
2400 mangleInitFiniStub(D, Out, 'F');
Reid Klecknerd8110b62013-09-10 20:14:30 +00002401}
2402
David Majnemer58e5bee2014-03-24 21:43:36 +00002403void MicrosoftMangleContextImpl::mangleStringLiteral(const StringLiteral *SL,
2404 raw_ostream &Out) {
2405 // <char-type> ::= 0 # char
2406 // ::= 1 # wchar_t
2407 // ::= ??? # char16_t/char32_t will need a mangling too...
2408 //
2409 // <literal-length> ::= <non-negative integer> # the length of the literal
2410 //
2411 // <encoded-crc> ::= <hex digit>+ @ # crc of the literal including
2412 // # null-terminator
2413 //
2414 // <encoded-string> ::= <simple character> # uninteresting character
2415 // ::= '?$' <hex digit> <hex digit> # these two nibbles
2416 // # encode the byte for the
2417 // # character
2418 // ::= '?' [a-z] # \xe1 - \xfa
2419 // ::= '?' [A-Z] # \xc1 - \xda
2420 // ::= '?' [0-9] # [,/\:. \n\t'-]
2421 //
2422 // <literal> ::= '??_C@_' <char-type> <literal-length> <encoded-crc>
2423 // <encoded-string> '@'
2424 MicrosoftCXXNameMangler Mangler(*this, Out);
2425 Mangler.getStream() << "\01??_C@_";
2426
2427 // <char-type>: The "kind" of string literal is encoded into the mangled name.
2428 // TODO: This needs to be updated when MSVC gains support for unicode
2429 // literals.
2430 if (SL->isAscii())
2431 Mangler.getStream() << '0';
2432 else if (SL->isWide())
2433 Mangler.getStream() << '1';
2434 else
2435 llvm_unreachable("unexpected string literal kind!");
2436
2437 // <literal-length>: The next part of the mangled name consists of the length
2438 // of the string.
2439 // The StringLiteral does not consider the NUL terminator byte(s) but the
2440 // mangling does.
2441 // N.B. The length is in terms of bytes, not characters.
2442 Mangler.mangleNumber(SL->getByteLength() + SL->getCharByteWidth());
2443
2444 // We will use the "Rocksoft^tm Model CRC Algorithm" to describe the
2445 // properties of our CRC:
2446 // Width : 32
2447 // Poly : 04C11DB7
2448 // Init : FFFFFFFF
2449 // RefIn : True
2450 // RefOut : True
2451 // XorOut : 00000000
2452 // Check : 340BC6D9
2453 uint32_t CRC = 0xFFFFFFFFU;
2454
2455 auto UpdateCRC = [&CRC](char Byte) {
2456 for (unsigned i = 0; i < 8; ++i) {
2457 bool Bit = CRC & 0x80000000U;
2458 if (Byte & (1U << i))
2459 Bit = !Bit;
2460 CRC <<= 1;
2461 if (Bit)
2462 CRC ^= 0x04C11DB7U;
2463 }
2464 };
2465
David Majnemer4b293eb2014-03-31 17:18:53 +00002466 auto GetLittleEndianByte = [&Mangler, &SL](unsigned Index) {
2467 unsigned CharByteWidth = SL->getCharByteWidth();
2468 uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);
David Majnemere49c4b62014-03-31 21:46:05 +00002469 unsigned OffsetInCodeUnit = Index % CharByteWidth;
2470 return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff);
David Majnemer4b293eb2014-03-31 17:18:53 +00002471 };
2472
2473 auto GetBigEndianByte = [&Mangler, &SL](unsigned Index) {
2474 unsigned CharByteWidth = SL->getCharByteWidth();
2475 uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);
David Majnemere49c4b62014-03-31 21:46:05 +00002476 unsigned OffsetInCodeUnit = (CharByteWidth - 1) - (Index % CharByteWidth);
2477 return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff);
David Majnemer4b293eb2014-03-31 17:18:53 +00002478 };
2479
David Majnemer58e5bee2014-03-24 21:43:36 +00002480 // CRC all the bytes of the StringLiteral.
David Majnemer4b293eb2014-03-31 17:18:53 +00002481 for (unsigned I = 0, E = SL->getByteLength(); I != E; ++I)
2482 UpdateCRC(GetLittleEndianByte(I));
David Majnemer58e5bee2014-03-24 21:43:36 +00002483
2484 // The NUL terminator byte(s) were not present earlier,
2485 // we need to manually process those bytes into the CRC.
2486 for (unsigned NullTerminator = 0; NullTerminator < SL->getCharByteWidth();
2487 ++NullTerminator)
2488 UpdateCRC('\x00');
2489
2490 // The literature refers to the process of reversing the bits in the final CRC
2491 // output as "reflection".
2492 CRC = llvm::reverseBits(CRC);
2493
2494 // <encoded-crc>: The CRC is encoded utilizing the standard number mangling
2495 // scheme.
2496 Mangler.mangleNumber(CRC);
2497
David Majnemer981c3062014-03-30 16:38:02 +00002498 // <encoded-string>: The mangled name also contains the first 32 _characters_
David Majnemer58e5bee2014-03-24 21:43:36 +00002499 // (including null-terminator bytes) of the StringLiteral.
2500 // Each character is encoded by splitting them into bytes and then encoding
2501 // the constituent bytes.
2502 auto MangleByte = [&Mangler](char Byte) {
2503 // There are five different manglings for characters:
2504 // - [a-zA-Z0-9_$]: A one-to-one mapping.
2505 // - ?[a-z]: The range from \xe1 to \xfa.
2506 // - ?[A-Z]: The range from \xc1 to \xda.
2507 // - ?[0-9]: The set of [,/\:. \n\t'-].
2508 // - ?$XX: A fallback which maps nibbles.
David Majnemer10befcf2014-04-01 00:05:57 +00002509 if (isIdentifierBody(Byte, /*AllowDollar=*/true)) {
David Majnemer58e5bee2014-03-24 21:43:36 +00002510 Mangler.getStream() << Byte;
David Majnemer10befcf2014-04-01 00:05:57 +00002511 } else if (isLetter(Byte & 0x7f)) {
2512 Mangler.getStream() << '?' << static_cast<char>(Byte & 0x7f);
David Majnemer58e5bee2014-03-24 21:43:36 +00002513 } else {
2514 switch (Byte) {
2515 case ',':
2516 Mangler.getStream() << "?0";
2517 break;
2518 case '/':
2519 Mangler.getStream() << "?1";
2520 break;
2521 case '\\':
2522 Mangler.getStream() << "?2";
2523 break;
2524 case ':':
2525 Mangler.getStream() << "?3";
2526 break;
2527 case '.':
2528 Mangler.getStream() << "?4";
2529 break;
2530 case ' ':
2531 Mangler.getStream() << "?5";
2532 break;
2533 case '\n':
2534 Mangler.getStream() << "?6";
2535 break;
2536 case '\t':
2537 Mangler.getStream() << "?7";
2538 break;
2539 case '\'':
2540 Mangler.getStream() << "?8";
2541 break;
2542 case '-':
2543 Mangler.getStream() << "?9";
2544 break;
2545 default:
2546 Mangler.getStream() << "?$";
2547 Mangler.getStream() << static_cast<char>('A' + ((Byte >> 4) & 0xf));
2548 Mangler.getStream() << static_cast<char>('A' + (Byte & 0xf));
2549 break;
2550 }
2551 }
2552 };
2553
David Majnemer58e5bee2014-03-24 21:43:36 +00002554 // Enforce our 32 character max.
2555 unsigned NumCharsToMangle = std::min(32U, SL->getLength());
David Majnemer4b293eb2014-03-31 17:18:53 +00002556 for (unsigned I = 0, E = NumCharsToMangle * SL->getCharByteWidth(); I != E;
2557 ++I)
2558 MangleByte(GetBigEndianByte(I));
David Majnemer58e5bee2014-03-24 21:43:36 +00002559
2560 // Encode the NUL terminator if there is room.
2561 if (NumCharsToMangle < 32)
David Majnemer4b293eb2014-03-31 17:18:53 +00002562 for (unsigned NullTerminator = 0; NullTerminator < SL->getCharByteWidth();
2563 ++NullTerminator)
2564 MangleByte(0);
David Majnemer58e5bee2014-03-24 21:43:36 +00002565
2566 Mangler.getStream() << '@';
2567}
2568
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002569MicrosoftMangleContext *
2570MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2571 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002572}