Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===--- 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 Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
| 22 | #include "clang/AST/DeclTemplate.h" |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 25 | #include "clang/AST/VTableBuilder.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 26 | #include "clang/Basic/ABI.h" |
| 27 | #include "clang/Basic/DiagnosticOptions.h" |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
David Majnemer | 3843a05 | 2014-03-23 17:47:16 +0000 | [diff] [blame] | 31 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
| 34 | namespace { |
| 35 | |
David Majnemer | d5a42b8 | 2013-09-13 09:03:14 +0000 | [diff] [blame] | 36 | /// \brief Retrieve the declaration context that should be used when mangling |
| 37 | /// the given declaration. |
| 38 | static const DeclContext *getEffectiveDeclContext(const Decl *D) { |
| 39 | // The ABI assumes that lambda closure types that occur within |
| 40 | // default arguments live in the context of the function. However, due to |
| 41 | // the way in which Clang parses and creates function declarations, this is |
| 42 | // not the case: the lambda closure type ends up living in the context |
| 43 | // where the function itself resides, because the function declaration itself |
| 44 | // had not yet been created. Fix the context here. |
| 45 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 46 | if (RD->isLambda()) |
| 47 | if (ParmVarDecl *ContextParam = |
| 48 | dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) |
| 49 | return ContextParam->getDeclContext(); |
| 50 | } |
| 51 | |
| 52 | // Perform the same check for block literals. |
| 53 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 54 | if (ParmVarDecl *ContextParam = |
| 55 | dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl())) |
| 56 | return ContextParam->getDeclContext(); |
| 57 | } |
| 58 | |
| 59 | const DeclContext *DC = D->getDeclContext(); |
| 60 | if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC)) |
| 61 | return getEffectiveDeclContext(CD); |
| 62 | |
| 63 | return DC; |
| 64 | } |
| 65 | |
| 66 | static const DeclContext *getEffectiveParentContext(const DeclContext *DC) { |
| 67 | return getEffectiveDeclContext(cast<Decl>(DC)); |
| 68 | } |
| 69 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 70 | static const FunctionDecl *getStructor(const NamedDecl *ND) { |
| 71 | if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 72 | return FTD->getTemplatedDecl(); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 73 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 74 | const auto *FD = cast<FunctionDecl>(ND); |
| 75 | if (const auto *FTD = FD->getPrimaryTemplate()) |
| 76 | return FTD->getTemplatedDecl(); |
| 77 | |
| 78 | return FD; |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 79 | } |
| 80 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 81 | static bool isLambda(const NamedDecl *ND) { |
| 82 | const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND); |
| 83 | if (!Record) |
| 84 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 85 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 86 | return Record->isLambda(); |
| 87 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 88 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 89 | /// MicrosoftMangleContextImpl - Overrides the default MangleContext for the |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 90 | /// Microsoft Visual C++ ABI. |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 91 | class MicrosoftMangleContextImpl : public MicrosoftMangleContext { |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 92 | typedef std::pair<const DeclContext *, IdentifierInfo *> DiscriminatorKeyTy; |
| 93 | llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator; |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 94 | llvm::DenseMap<const NamedDecl *, unsigned> Uniquifier; |
David Majnemer | f017ec3 | 2014-03-05 10:35:06 +0000 | [diff] [blame] | 95 | llvm::DenseMap<const CXXRecordDecl *, unsigned> LambdaIds; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 96 | llvm::DenseMap<const NamedDecl *, unsigned> SEHFilterIds; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 97 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 98 | public: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 99 | MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags) |
| 100 | : MicrosoftMangleContext(Context, Diags) {} |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 101 | bool shouldMangleCXXName(const NamedDecl *D) override; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 102 | bool shouldMangleStringLiteral(const StringLiteral *SL) override; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 103 | void mangleCXXName(const NamedDecl *D, raw_ostream &Out) override; |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 104 | void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, |
| 105 | raw_ostream &) override; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 106 | void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, |
| 107 | raw_ostream &) override; |
| 108 | void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 109 | const ThisAdjustment &ThisAdjustment, |
| 110 | raw_ostream &) override; |
| 111 | void mangleCXXVFTable(const CXXRecordDecl *Derived, |
| 112 | ArrayRef<const CXXRecordDecl *> BasePath, |
| 113 | raw_ostream &Out) override; |
| 114 | void mangleCXXVBTable(const CXXRecordDecl *Derived, |
| 115 | ArrayRef<const CXXRecordDecl *> BasePath, |
| 116 | raw_ostream &Out) override; |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 117 | void mangleCXXThrowInfo(QualType T, bool IsConst, bool IsVolatile, |
| 118 | uint32_t NumEntries, raw_ostream &Out) override; |
| 119 | void mangleCXXCatchableTypeArray(QualType T, uint32_t NumEntries, |
| 120 | raw_ostream &Out) override; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 121 | void mangleCXXCatchableType(QualType T, const CXXConstructorDecl *CD, |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 122 | CXXCtorType CT, uint32_t Size, uint32_t NVOffset, |
David Majnemer | 999cbf9 | 2015-03-10 19:01:51 +0000 | [diff] [blame] | 123 | int32_t VBPtrOffset, uint32_t VBIndex, |
| 124 | raw_ostream &Out) override; |
David Majnemer | 5f0dd61 | 2015-03-17 20:35:05 +0000 | [diff] [blame^] | 125 | void mangleCXXHandlerMapEntry(QualType T, bool IsConst, bool IsVolatile, |
| 126 | bool IsReference, raw_ostream &Out); |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 127 | void mangleCXXRTTI(QualType T, raw_ostream &Out) override; |
| 128 | void mangleCXXRTTIName(QualType T, raw_ostream &Out) override; |
David Majnemer | aeb55c9 | 2014-05-13 06:57:43 +0000 | [diff] [blame] | 129 | void mangleCXXRTTIBaseClassDescriptor(const CXXRecordDecl *Derived, |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 130 | uint32_t NVOffset, int32_t VBPtrOffset, |
David Majnemer | aeb55c9 | 2014-05-13 06:57:43 +0000 | [diff] [blame] | 131 | uint32_t VBTableOffset, uint32_t Flags, |
| 132 | raw_ostream &Out) override; |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 133 | void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived, |
| 134 | raw_ostream &Out) override; |
| 135 | void mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived, |
| 136 | raw_ostream &Out) override; |
David Majnemer | aeb55c9 | 2014-05-13 06:57:43 +0000 | [diff] [blame] | 137 | void |
| 138 | mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived, |
| 139 | ArrayRef<const CXXRecordDecl *> BasePath, |
| 140 | raw_ostream &Out) override; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 141 | void mangleTypeName(QualType T, raw_ostream &) override; |
| 142 | void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
| 143 | raw_ostream &) override; |
| 144 | void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
| 145 | raw_ostream &) override; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 146 | void mangleReferenceTemporary(const VarDecl *, unsigned ManglingNumber, |
| 147 | raw_ostream &) override; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 148 | void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out) override; |
| 149 | void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override; |
| 150 | void mangleDynamicAtExitDestructor(const VarDecl *D, |
| 151 | raw_ostream &Out) override; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 152 | void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl, |
| 153 | raw_ostream &Out) override; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 154 | void mangleStringLiteral(const StringLiteral *SL, raw_ostream &Out) override; |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 155 | void mangleCXXVTableBitSet(const CXXRecordDecl *RD, |
| 156 | raw_ostream &Out) override; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 157 | bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { |
| 158 | // Lambda closure types are already numbered. |
| 159 | if (isLambda(ND)) |
| 160 | return false; |
| 161 | |
| 162 | const DeclContext *DC = getEffectiveDeclContext(ND); |
| 163 | if (!DC->isFunctionOrMethod()) |
| 164 | return false; |
| 165 | |
| 166 | // Use the canonical number for externally visible decls. |
| 167 | if (ND->isExternallyVisible()) { |
| 168 | disc = getASTContext().getManglingNumber(ND); |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | // Anonymous tags are already numbered. |
| 173 | if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) { |
| 174 | if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl()) |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // Make up a reasonable number for internal decls. |
| 179 | unsigned &discriminator = Uniquifier[ND]; |
| 180 | if (!discriminator) |
| 181 | discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())]; |
David Majnemer | 040fa34 | 2014-10-05 06:44:53 +0000 | [diff] [blame] | 182 | disc = discriminator + 1; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 183 | return true; |
| 184 | } |
Reid Kleckner | 1ece9fc | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 185 | |
David Majnemer | f017ec3 | 2014-03-05 10:35:06 +0000 | [diff] [blame] | 186 | unsigned getLambdaId(const CXXRecordDecl *RD) { |
| 187 | assert(RD->isLambda() && "RD must be a lambda!"); |
| 188 | assert(!RD->isExternallyVisible() && "RD must not be visible!"); |
| 189 | assert(RD->getLambdaManglingNumber() == 0 && |
| 190 | "RD must not have a mangling number!"); |
| 191 | std::pair<llvm::DenseMap<const CXXRecordDecl *, unsigned>::iterator, bool> |
| 192 | Result = LambdaIds.insert(std::make_pair(RD, LambdaIds.size())); |
| 193 | return Result.first->second; |
| 194 | } |
| 195 | |
Reid Kleckner | 1ece9fc | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 196 | private: |
| 197 | void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 200 | /// MicrosoftCXXNameMangler - Manage the mangling of a single name for the |
| 201 | /// Microsoft Visual C++ ABI. |
| 202 | class MicrosoftCXXNameMangler { |
| 203 | MicrosoftMangleContextImpl &Context; |
| 204 | raw_ostream &Out; |
| 205 | |
| 206 | /// The "structor" is the top-level declaration being mangled, if |
| 207 | /// that's not a template specialization; otherwise it's the pattern |
| 208 | /// for that specialization. |
| 209 | const NamedDecl *Structor; |
| 210 | unsigned StructorType; |
| 211 | |
David Majnemer | 0d0560c | 2014-09-25 19:43:56 +0000 | [diff] [blame] | 212 | typedef llvm::SmallVector<std::string, 10> BackRefVec; |
| 213 | BackRefVec NameBackReferences; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 214 | |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 215 | typedef llvm::DenseMap<void *, unsigned> ArgBackRefMap; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 216 | ArgBackRefMap TypeBackReferences; |
| 217 | |
| 218 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 219 | |
| 220 | // FIXME: If we add support for __ptr32/64 qualifiers, then we should push |
| 221 | // this check into mangleQualifiers(). |
| 222 | const bool PointersAre64Bit; |
| 223 | |
| 224 | public: |
| 225 | enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result }; |
| 226 | |
| 227 | MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 228 | : Context(C), Out(Out_), Structor(nullptr), StructorType(-1), |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 229 | PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) == |
| 230 | 64) {} |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 231 | |
| 232 | MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_, |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 233 | const CXXConstructorDecl *D, CXXCtorType Type) |
| 234 | : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), |
| 235 | PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) == |
| 236 | 64) {} |
| 237 | |
| 238 | MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_, |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 239 | const CXXDestructorDecl *D, CXXDtorType Type) |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 240 | : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 241 | PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) == |
| 242 | 64) {} |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 243 | |
| 244 | raw_ostream &getStream() const { return Out; } |
| 245 | |
| 246 | void mangle(const NamedDecl *D, StringRef Prefix = "\01?"); |
| 247 | void mangleName(const NamedDecl *ND); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 248 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 249 | void mangleVariableEncoding(const VarDecl *VD); |
| 250 | void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD); |
| 251 | void mangleMemberFunctionPointer(const CXXRecordDecl *RD, |
| 252 | const CXXMethodDecl *MD); |
| 253 | void mangleVirtualMemPtrThunk( |
| 254 | const CXXMethodDecl *MD, |
| 255 | const MicrosoftVTableContext::MethodVFTableLocation &ML); |
| 256 | void mangleNumber(int64_t Number); |
| 257 | void mangleType(QualType T, SourceRange Range, |
| 258 | QualifierMangleMode QMM = QMM_Mangle); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 259 | void mangleFunctionType(const FunctionType *T, |
| 260 | const FunctionDecl *D = nullptr, |
David Majnemer | 2f6a96e | 2014-08-12 17:53:10 +0000 | [diff] [blame] | 261 | bool ForceThisQuals = false); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 262 | void mangleNestedName(const NamedDecl *ND); |
| 263 | |
| 264 | private: |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 265 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 266 | mangleUnqualifiedName(ND, ND->getDeclName()); |
| 267 | } |
| 268 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); |
| 269 | void mangleSourceName(StringRef Name); |
| 270 | void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc); |
| 271 | void mangleCXXDtorType(CXXDtorType T); |
| 272 | void mangleQualifiers(Qualifiers Quals, bool IsMember); |
David Majnemer | e3785bb | 2014-04-23 05:16:56 +0000 | [diff] [blame] | 273 | void mangleRefQualifier(RefQualifierKind RefQualifier); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 274 | void manglePointerCVQualifiers(Qualifiers Quals); |
| 275 | void manglePointerExtQualifiers(Qualifiers Quals, const Type *PointeeType); |
| 276 | |
| 277 | void mangleUnscopedTemplateName(const TemplateDecl *ND); |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 278 | void |
| 279 | mangleTemplateInstantiationName(const TemplateDecl *TD, |
| 280 | const TemplateArgumentList &TemplateArgs); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 281 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
| 282 | |
| 283 | void mangleArgumentType(QualType T, SourceRange Range); |
| 284 | |
| 285 | // Declare manglers for every type class. |
| 286 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 287 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 288 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \ |
| 289 | SourceRange Range); |
| 290 | #include "clang/AST/TypeNodes.def" |
| 291 | #undef ABSTRACT_TYPE |
| 292 | #undef NON_CANONICAL_TYPE |
| 293 | #undef TYPE |
| 294 | |
| 295 | void mangleType(const TagDecl *TD); |
| 296 | void mangleDecayedArrayType(const ArrayType *T); |
| 297 | void mangleArrayType(const ArrayType *T); |
| 298 | void mangleFunctionClass(const FunctionDecl *FD); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 299 | void mangleCallingConvention(CallingConv CC); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 300 | void mangleCallingConvention(const FunctionType *T); |
| 301 | void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean); |
| 302 | void mangleExpression(const Expr *E); |
| 303 | void mangleThrowSpecification(const FunctionProtoType *T); |
| 304 | |
| 305 | void mangleTemplateArgs(const TemplateDecl *TD, |
| 306 | const TemplateArgumentList &TemplateArgs); |
David Majnemer | 57fbc0c | 2014-08-05 22:43:45 +0000 | [diff] [blame] | 307 | void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA, |
| 308 | const NamedDecl *Parm); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 309 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Rafael Espindola | 002667c | 2013-10-16 01:40:34 +0000 | [diff] [blame] | 312 | bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) { |
David Majnemer | d5a42b8 | 2013-09-13 09:03:14 +0000 | [diff] [blame] | 313 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 314 | LanguageLinkage L = FD->getLanguageLinkage(); |
| 315 | // Overloadable functions need mangling. |
| 316 | if (FD->hasAttr<OverloadableAttr>()) |
| 317 | return true; |
| 318 | |
David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 319 | // The ABI expects that we would never mangle "typical" user-defined entry |
| 320 | // points regardless of visibility or freestanding-ness. |
| 321 | // |
| 322 | // N.B. This is distinct from asking about "main". "main" has a lot of |
| 323 | // special rules associated with it in the standard while these |
| 324 | // user-defined entry points are outside of the purview of the standard. |
| 325 | // For example, there can be only one definition for "main" in a standards |
| 326 | // compliant program; however nothing forbids the existence of wmain and |
| 327 | // WinMain in the same translation unit. |
| 328 | if (FD->isMSVCRTEntryPoint()) |
David Majnemer | d5a42b8 | 2013-09-13 09:03:14 +0000 | [diff] [blame] | 329 | return false; |
| 330 | |
| 331 | // C++ functions and those whose names are not a simple identifier need |
| 332 | // mangling. |
| 333 | if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage) |
| 334 | return true; |
| 335 | |
| 336 | // C functions are not mangled. |
| 337 | if (L == CLanguageLinkage) |
| 338 | return false; |
| 339 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 340 | |
| 341 | // Otherwise, no mangling is done outside C++ mode. |
| 342 | if (!getASTContext().getLangOpts().CPlusPlus) |
| 343 | return false; |
| 344 | |
David Majnemer | d5a42b8 | 2013-09-13 09:03:14 +0000 | [diff] [blame] | 345 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 346 | // C variables are not mangled. |
| 347 | if (VD->isExternC()) |
| 348 | return false; |
| 349 | |
| 350 | // Variables at global scope with non-internal linkage are not mangled. |
| 351 | const DeclContext *DC = getEffectiveDeclContext(D); |
| 352 | // Check for extern variable declared locally. |
| 353 | if (DC->isFunctionOrMethod() && D->hasLinkage()) |
| 354 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 355 | DC = getEffectiveParentContext(DC); |
| 356 | |
| 357 | if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage && |
| 358 | !isa<VarTemplateSpecializationDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 362 | return true; |
| 363 | } |
| 364 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 365 | bool |
| 366 | MicrosoftMangleContextImpl::shouldMangleStringLiteral(const StringLiteral *SL) { |
David Majnemer | 6fc2d81 | 2014-11-22 06:20:38 +0000 | [diff] [blame] | 367 | return true; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 368 | } |
| 369 | |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 370 | void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 371 | // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. |
| 372 | // Therefore it's really important that we don't decorate the |
| 373 | // name with leading underscores or leading/trailing at signs. So, by |
| 374 | // default, we emit an asm marker at the start so we get the name right. |
| 375 | // Callers can override this with a custom prefix. |
| 376 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 377 | // <mangled-name> ::= ? <name> <type-encoding> |
| 378 | Out << Prefix; |
| 379 | mangleName(D); |
| 380 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 381 | mangleFunctionEncoding(FD); |
| 382 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 383 | mangleVariableEncoding(VD); |
| 384 | else { |
| 385 | // TODO: Fields? Can MSVC even mangle them? |
| 386 | // Issue a diagnostic for now. |
| 387 | DiagnosticsEngine &Diags = Context.getDiags(); |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 388 | unsigned DiagID = Diags.getCustomDiagID( |
| 389 | DiagnosticsEngine::Error, "cannot mangle this declaration yet"); |
| 390 | Diags.Report(D->getLocation(), DiagID) << D->getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 395 | // <type-encoding> ::= <function-class> <function-type> |
| 396 | |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 397 | // Since MSVC operates on the type as written and not the canonical type, it |
| 398 | // actually matters which decl we have here. MSVC appears to choose the |
| 399 | // first, since it is most likely to be the declaration in a header file. |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 400 | FD = FD->getFirstDecl(); |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 401 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 402 | // We should never ever see a FunctionNoProtoType at this point. |
| 403 | // We don't even know how to mangle their types anyway :). |
Reid Kleckner | 9a7f3e6 | 2013-10-08 00:58:57 +0000 | [diff] [blame] | 404 | const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 405 | |
David Majnemer | d5a42b8 | 2013-09-13 09:03:14 +0000 | [diff] [blame] | 406 | // extern "C" functions can hold entities that must be mangled. |
| 407 | // As it stands, these functions still need to get expressed in the full |
| 408 | // external name. They have their class and type omitted, replaced with '9'. |
| 409 | if (Context.shouldMangleDeclName(FD)) { |
| 410 | // First, the function class. |
| 411 | mangleFunctionClass(FD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 412 | |
Timur Iskhodzhanov | 555a772 | 2013-10-04 11:25:05 +0000 | [diff] [blame] | 413 | mangleFunctionType(FT, FD); |
David Majnemer | d5a42b8 | 2013-09-13 09:03:14 +0000 | [diff] [blame] | 414 | } else |
| 415 | Out << '9'; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { |
| 419 | // <type-encoding> ::= <storage-class> <variable-type> |
| 420 | // <storage-class> ::= 0 # private static member |
| 421 | // ::= 1 # protected static member |
| 422 | // ::= 2 # public static member |
| 423 | // ::= 3 # global |
| 424 | // ::= 4 # static local |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 425 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 426 | // The first character in the encoding (after the name) is the storage class. |
| 427 | if (VD->isStaticDataMember()) { |
| 428 | // If it's a static member, it also encodes the access level. |
| 429 | switch (VD->getAccess()) { |
| 430 | default: |
| 431 | case AS_private: Out << '0'; break; |
| 432 | case AS_protected: Out << '1'; break; |
| 433 | case AS_public: Out << '2'; break; |
| 434 | } |
| 435 | } |
| 436 | else if (!VD->isStaticLocal()) |
| 437 | Out << '3'; |
| 438 | else |
| 439 | Out << '4'; |
| 440 | // Now mangle the type. |
| 441 | // <variable-type> ::= <type> <cvr-qualifiers> |
| 442 | // ::= <type> <pointee-cvr-qualifiers> # pointers, references |
| 443 | // Pointers and references are odd. The type of 'int * const foo;' gets |
| 444 | // mangled as 'QAHA' instead of 'PAHB', for example. |
Zachary Turner | 87422d9 | 2014-06-25 05:37:25 +0000 | [diff] [blame] | 445 | SourceRange SR = VD->getSourceRange(); |
David Majnemer | b9a5f2d | 2014-01-21 20:33:36 +0000 | [diff] [blame] | 446 | QualType Ty = VD->getType(); |
David Majnemer | 6dda7bb | 2013-08-15 08:13:23 +0000 | [diff] [blame] | 447 | if (Ty->isPointerType() || Ty->isReferenceType() || |
| 448 | Ty->isMemberPointerType()) { |
Zachary Turner | 87422d9 | 2014-06-25 05:37:25 +0000 | [diff] [blame] | 449 | mangleType(Ty, SR, QMM_Drop); |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 450 | manglePointerExtQualifiers( |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 451 | Ty.getDesugaredType(getASTContext()).getLocalQualifiers(), nullptr); |
David Majnemer | 6dda7bb | 2013-08-15 08:13:23 +0000 | [diff] [blame] | 452 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) { |
| 453 | mangleQualifiers(MPT->getPointeeType().getQualifiers(), true); |
| 454 | // Member pointers are suffixed with a back reference to the member |
| 455 | // pointer's class name. |
| 456 | mangleName(MPT->getClass()->getAsCXXRecordDecl()); |
| 457 | } else |
| 458 | mangleQualifiers(Ty->getPointeeType().getQualifiers(), false); |
David Majnemer | a2724ae | 2013-08-09 05:56:24 +0000 | [diff] [blame] | 459 | } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 460 | // Global arrays are funny, too. |
David Majnemer | 5a1b204 | 2013-09-11 04:44:30 +0000 | [diff] [blame] | 461 | mangleDecayedArrayType(AT); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 462 | if (AT->getElementType()->isArrayType()) |
| 463 | Out << 'A'; |
| 464 | else |
| 465 | mangleQualifiers(Ty.getQualifiers(), false); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 466 | } else { |
Zachary Turner | 87422d9 | 2014-06-25 05:37:25 +0000 | [diff] [blame] | 467 | mangleType(Ty, SR, QMM_Drop); |
Will Wilson | 5f38367 | 2014-11-05 13:54:21 +0000 | [diff] [blame] | 468 | mangleQualifiers(Ty.getQualifiers(), false); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 472 | void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD, |
David Majnemer | 1e378e4 | 2014-02-06 12:46:52 +0000 | [diff] [blame] | 473 | const ValueDecl *VD) { |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 474 | // <member-data-pointer> ::= <integer-literal> |
| 475 | // ::= $F <number> <number> |
| 476 | // ::= $G <number> <number> <number> |
| 477 | |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 478 | int64_t FieldOffset; |
| 479 | int64_t VBTableOffset; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 480 | MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel(); |
David Majnemer | 1e378e4 | 2014-02-06 12:46:52 +0000 | [diff] [blame] | 481 | if (VD) { |
| 482 | FieldOffset = getASTContext().getFieldOffset(VD); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 483 | assert(FieldOffset % getASTContext().getCharWidth() == 0 && |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 484 | "cannot take address of bitfield"); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 485 | FieldOffset /= getASTContext().getCharWidth(); |
| 486 | |
| 487 | VBTableOffset = 0; |
| 488 | } else { |
| 489 | FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1; |
| 490 | |
| 491 | VBTableOffset = -1; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 492 | } |
| 493 | |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 494 | char Code = '\0'; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 495 | switch (IM) { |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 496 | case MSInheritanceAttr::Keyword_single_inheritance: Code = '0'; break; |
| 497 | case MSInheritanceAttr::Keyword_multiple_inheritance: Code = '0'; break; |
| 498 | case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'F'; break; |
| 499 | case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'G'; break; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 500 | } |
| 501 | |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 502 | Out << '$' << Code; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 503 | |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 504 | mangleNumber(FieldOffset); |
| 505 | |
Reid Kleckner | bf94e6e | 2014-04-07 18:07:03 +0000 | [diff] [blame] | 506 | // The C++ standard doesn't allow base-to-derived member pointer conversions |
| 507 | // in template parameter contexts, so the vbptr offset of data member pointers |
| 508 | // is always zero. |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 509 | if (MSInheritanceAttr::hasVBPtrOffsetField(IM)) |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 510 | mangleNumber(0); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 511 | if (MSInheritanceAttr::hasVBTableOffsetField(IM)) |
| 512 | mangleNumber(VBTableOffset); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void |
| 516 | MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD, |
| 517 | const CXXMethodDecl *MD) { |
| 518 | // <member-function-pointer> ::= $1? <name> |
| 519 | // ::= $H? <name> <number> |
| 520 | // ::= $I? <name> <number> <number> |
| 521 | // ::= $J? <name> <number> <number> <number> |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 522 | |
| 523 | MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel(); |
| 524 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 525 | char Code = '\0'; |
| 526 | switch (IM) { |
| 527 | case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break; |
| 528 | case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break; |
| 529 | case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break; |
| 530 | case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break; |
| 531 | } |
| 532 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 533 | // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr |
| 534 | // thunk. |
| 535 | uint64_t NVOffset = 0; |
| 536 | uint64_t VBTableOffset = 0; |
Reid Kleckner | bf94e6e | 2014-04-07 18:07:03 +0000 | [diff] [blame] | 537 | uint64_t VBPtrOffset = 0; |
David Majnemer | 6a729c6 | 2014-06-11 04:55:08 +0000 | [diff] [blame] | 538 | if (MD) { |
| 539 | Out << '$' << Code << '?'; |
| 540 | if (MD->isVirtual()) { |
| 541 | MicrosoftVTableContext *VTContext = |
| 542 | cast<MicrosoftVTableContext>(getASTContext().getVTableContext()); |
| 543 | const MicrosoftVTableContext::MethodVFTableLocation &ML = |
| 544 | VTContext->getMethodVFTableLocation(GlobalDecl(MD)); |
| 545 | mangleVirtualMemPtrThunk(MD, ML); |
| 546 | NVOffset = ML.VFPtrOffset.getQuantity(); |
| 547 | VBTableOffset = ML.VBTableIndex * 4; |
| 548 | if (ML.VBase) { |
| 549 | const ASTRecordLayout &Layout = getASTContext().getASTRecordLayout(RD); |
| 550 | VBPtrOffset = Layout.getVBPtrOffset().getQuantity(); |
| 551 | } |
| 552 | } else { |
| 553 | mangleName(MD); |
| 554 | mangleFunctionEncoding(MD); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 555 | } |
| 556 | } else { |
David Majnemer | 6a729c6 | 2014-06-11 04:55:08 +0000 | [diff] [blame] | 557 | // Null single inheritance member functions are encoded as a simple nullptr. |
| 558 | if (IM == MSInheritanceAttr::Keyword_single_inheritance) { |
| 559 | Out << "$0A@"; |
| 560 | return; |
| 561 | } |
| 562 | if (IM == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 563 | VBTableOffset = -1; |
| 564 | Out << '$' << Code; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM)) |
| 568 | mangleNumber(NVOffset); |
| 569 | if (MSInheritanceAttr::hasVBPtrOffsetField(IM)) |
Reid Kleckner | bf94e6e | 2014-04-07 18:07:03 +0000 | [diff] [blame] | 570 | mangleNumber(VBPtrOffset); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 571 | if (MSInheritanceAttr::hasVBTableOffsetField(IM)) |
| 572 | mangleNumber(VBTableOffset); |
| 573 | } |
| 574 | |
| 575 | void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk( |
| 576 | const CXXMethodDecl *MD, |
| 577 | const MicrosoftVTableContext::MethodVFTableLocation &ML) { |
| 578 | // Get the vftable offset. |
| 579 | CharUnits PointerWidth = getASTContext().toCharUnitsFromBits( |
| 580 | getASTContext().getTargetInfo().getPointerWidth(0)); |
| 581 | uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity(); |
| 582 | |
| 583 | Out << "?_9"; |
| 584 | mangleName(MD->getParent()); |
| 585 | Out << "$B"; |
| 586 | mangleNumber(OffsetInVFTable); |
| 587 | Out << 'A'; |
David Majnemer | bc02d32 | 2015-03-14 06:34:41 +0000 | [diff] [blame] | 588 | mangleCallingConvention(MD->getType()->getAs<FunctionProtoType>()); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 591 | void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { |
| 592 | // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 593 | |
| 594 | // Always start with the unqualified name. |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 595 | mangleUnqualifiedName(ND); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 596 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 597 | mangleNestedName(ND); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 598 | |
| 599 | // Terminate the whole name with an '@'. |
| 600 | Out << '@'; |
| 601 | } |
| 602 | |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 603 | void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { |
| 604 | // <non-negative integer> ::= A@ # when Number == 0 |
| 605 | // ::= <decimal digit> # when 1 <= Number <= 10 |
| 606 | // ::= <hex digit>+ @ # when Number >= 10 |
| 607 | // |
| 608 | // <number> ::= [?] <non-negative integer> |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 609 | |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 610 | uint64_t Value = static_cast<uint64_t>(Number); |
| 611 | if (Number < 0) { |
| 612 | Value = -Value; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 613 | Out << '?'; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 614 | } |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 615 | |
| 616 | if (Value == 0) |
| 617 | Out << "A@"; |
| 618 | else if (Value >= 1 && Value <= 10) |
| 619 | Out << (Value - 1); |
| 620 | else { |
| 621 | // Numbers that are not encoded as decimal digits are represented as nibbles |
| 622 | // in the range of ASCII characters 'A' to 'P'. |
| 623 | // The number 0x123450 would be encoded as 'BCDEFA' |
| 624 | char EncodedNumberBuffer[sizeof(uint64_t) * 2]; |
Craig Topper | e3d2ecbe | 2014-06-28 23:22:33 +0000 | [diff] [blame] | 625 | MutableArrayRef<char> BufferRef(EncodedNumberBuffer); |
| 626 | MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin(); |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 627 | for (; Value != 0; Value >>= 4) |
| 628 | *I++ = 'A' + (Value & 0xf); |
| 629 | Out.write(I.base(), I - BufferRef.rbegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 630 | Out << '@'; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | static const TemplateDecl * |
Reid Kleckner | 5251886 | 2013-03-20 01:40:23 +0000 | [diff] [blame] | 635 | isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 636 | // Check if we have a function template. |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 637 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 638 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
Reid Kleckner | 5251886 | 2013-03-20 01:40:23 +0000 | [diff] [blame] | 639 | TemplateArgs = FD->getTemplateSpecializationArgs(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 640 | return TD; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // Check if we have a class template. |
| 645 | if (const ClassTemplateSpecializationDecl *Spec = |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 646 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
Reid Kleckner | 5251886 | 2013-03-20 01:40:23 +0000 | [diff] [blame] | 647 | TemplateArgs = &Spec->getTemplateArgs(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 648 | return Spec->getSpecializedTemplate(); |
| 649 | } |
| 650 | |
David Majnemer | 8f77453 | 2014-03-04 05:38:05 +0000 | [diff] [blame] | 651 | // Check if we have a variable template. |
| 652 | if (const VarTemplateSpecializationDecl *Spec = |
| 653 | dyn_cast<VarTemplateSpecializationDecl>(ND)) { |
| 654 | TemplateArgs = &Spec->getTemplateArgs(); |
| 655 | return Spec->getSpecializedTemplate(); |
| 656 | } |
| 657 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 658 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 659 | } |
| 660 | |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 661 | void MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 662 | DeclarationName Name) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 663 | // <unqualified-name> ::= <operator-name> |
| 664 | // ::= <ctor-dtor-name> |
| 665 | // ::= <source-name> |
| 666 | // ::= <template-name> |
Reid Kleckner | 5251886 | 2013-03-20 01:40:23 +0000 | [diff] [blame] | 667 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 668 | // Check if we have a template. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 669 | const TemplateArgumentList *TemplateArgs = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 670 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Reid Kleckner | c16c447 | 2013-07-13 00:43:39 +0000 | [diff] [blame] | 671 | // Function templates aren't considered for name back referencing. This |
| 672 | // makes sense since function templates aren't likely to occur multiple |
| 673 | // times in a symbol. |
| 674 | // FIXME: Test alias template mangling with MSVC 2013. |
| 675 | if (!isa<ClassTemplateDecl>(TD)) { |
| 676 | mangleTemplateInstantiationName(TD, *TemplateArgs); |
Reid Kleckner | 11c6f61 | 2014-06-26 22:42:18 +0000 | [diff] [blame] | 677 | Out << '@'; |
Reid Kleckner | c16c447 | 2013-07-13 00:43:39 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 681 | // Here comes the tricky thing: if we need to mangle something like |
| 682 | // void foo(A::X<Y>, B::X<Y>), |
| 683 | // the X<Y> part is aliased. However, if you need to mangle |
| 684 | // void foo(A::X<A::Y>, A::X<B::Y>), |
| 685 | // the A::X<> part is not aliased. |
| 686 | // That said, from the mangler's perspective we have a structure like this: |
| 687 | // namespace[s] -> type[ -> template-parameters] |
| 688 | // but from the Clang perspective we have |
| 689 | // type [ -> template-parameters] |
| 690 | // \-> namespace[s] |
| 691 | // What we do is we create a new mangler, mangle the same type (without |
David Majnemer | c986fcc | 2014-06-08 04:51:13 +0000 | [diff] [blame] | 692 | // a namespace suffix) to a string using the extra mangler and then use |
| 693 | // the mangled type name as a key to check the mangling of different types |
| 694 | // for aliasing. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 695 | |
Reid Kleckner | 11c6f61 | 2014-06-26 22:42:18 +0000 | [diff] [blame] | 696 | llvm::SmallString<64> TemplateMangling; |
| 697 | llvm::raw_svector_ostream Stream(TemplateMangling); |
David Majnemer | c986fcc | 2014-06-08 04:51:13 +0000 | [diff] [blame] | 698 | MicrosoftCXXNameMangler Extra(Context, Stream); |
| 699 | Extra.mangleTemplateInstantiationName(TD, *TemplateArgs); |
| 700 | Stream.flush(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 701 | |
Reid Kleckner | 11c6f61 | 2014-06-26 22:42:18 +0000 | [diff] [blame] | 702 | mangleSourceName(TemplateMangling); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 703 | return; |
| 704 | } |
| 705 | |
| 706 | switch (Name.getNameKind()) { |
| 707 | case DeclarationName::Identifier: { |
| 708 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 709 | mangleSourceName(II->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 710 | break; |
| 711 | } |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 712 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 713 | // Otherwise, an anonymous entity. We must have a declaration. |
| 714 | assert(ND && "mangling empty name without declaration"); |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 715 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 716 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 717 | if (NS->isAnonymousNamespace()) { |
| 718 | Out << "?A@"; |
| 719 | break; |
| 720 | } |
| 721 | } |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 722 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 723 | if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 724 | // We must have an anonymous union or struct declaration. |
| 725 | const CXXRecordDecl *RD = VD->getType()->getAsCXXRecordDecl(); |
| 726 | assert(RD && "expected variable decl to have a record type"); |
| 727 | // Anonymous types with no tag or typedef get the name of their |
| 728 | // declarator mangled in. If they have no declarator, number them with |
| 729 | // a $S prefix. |
| 730 | llvm::SmallString<64> Name("$S"); |
| 731 | // Get a unique id for the anonymous struct. |
| 732 | Name += llvm::utostr(Context.getAnonymousStructId(RD) + 1); |
| 733 | mangleSourceName(Name.str()); |
| 734 | break; |
| 735 | } |
| 736 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 737 | // We must have an anonymous struct. |
| 738 | const TagDecl *TD = cast<TagDecl>(ND); |
| 739 | if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) { |
| 740 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 741 | "Typedef should not be in another decl context!"); |
| 742 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 743 | "Typedef was not named!"); |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 744 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | |
David Majnemer | f017ec3 | 2014-03-05 10:35:06 +0000 | [diff] [blame] | 748 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) { |
| 749 | if (Record->isLambda()) { |
| 750 | llvm::SmallString<10> Name("<lambda_"); |
| 751 | unsigned LambdaId; |
| 752 | if (Record->getLambdaManglingNumber()) |
| 753 | LambdaId = Record->getLambdaManglingNumber(); |
| 754 | else |
| 755 | LambdaId = Context.getLambdaId(Record); |
| 756 | |
| 757 | Name += llvm::utostr(LambdaId); |
| 758 | Name += ">"; |
| 759 | |
| 760 | mangleSourceName(Name); |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 765 | llvm::SmallString<64> Name("<unnamed-type-"); |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 766 | if (TD->hasDeclaratorForAnonDecl()) { |
David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 767 | // Anonymous types with no tag or typedef get the name of their |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 768 | // declarator mangled in if they have one. |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 769 | Name += TD->getDeclaratorForAnonDecl()->getName(); |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 770 | } else { |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 771 | // Otherwise, number the types using a $S prefix. |
| 772 | Name += "$S"; |
David Majnemer | f017ec3 | 2014-03-05 10:35:06 +0000 | [diff] [blame] | 773 | Name += llvm::utostr(Context.getAnonymousStructId(TD)); |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 774 | } |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 775 | Name += ">"; |
| 776 | mangleSourceName(Name.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 777 | break; |
| 778 | } |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 779 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 780 | case DeclarationName::ObjCZeroArgSelector: |
| 781 | case DeclarationName::ObjCOneArgSelector: |
| 782 | case DeclarationName::ObjCMultiArgSelector: |
| 783 | llvm_unreachable("Can't mangle Objective-C selector names here!"); |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 784 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 785 | case DeclarationName::CXXConstructorName: |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 786 | if (Structor == getStructor(ND)) { |
| 787 | if (StructorType == Ctor_CopyingClosure) { |
| 788 | Out << "?_O"; |
| 789 | return; |
| 790 | } |
| 791 | if (StructorType == Ctor_DefaultClosure) { |
| 792 | Out << "?_F"; |
| 793 | return; |
| 794 | } |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 795 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 796 | Out << "?0"; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 797 | return; |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 798 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 799 | case DeclarationName::CXXDestructorName: |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 800 | if (ND == Structor) |
| 801 | // If the named decl is the C++ destructor we're mangling, |
| 802 | // use the type we were given. |
| 803 | mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); |
| 804 | else |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 805 | // Otherwise, use the base destructor name. This is relevant if a |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 806 | // class with a destructor is declared within a destructor. |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 807 | mangleCXXDtorType(Dtor_Base); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 808 | break; |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 809 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 810 | case DeclarationName::CXXConversionFunctionName: |
| 811 | // <operator-name> ::= ?B # (cast) |
| 812 | // The target type is encoded as the return type. |
| 813 | Out << "?B"; |
| 814 | break; |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 815 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 816 | case DeclarationName::CXXOperatorName: |
| 817 | mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation()); |
| 818 | break; |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 819 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 820 | case DeclarationName::CXXLiteralOperatorName: { |
David Majnemer | e31a3ed | 2014-06-04 16:46:26 +0000 | [diff] [blame] | 821 | Out << "?__K"; |
| 822 | mangleSourceName(Name.getCXXLiteralIdentifier()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 823 | break; |
| 824 | } |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 825 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 826 | case DeclarationName::CXXUsingDirective: |
| 827 | llvm_unreachable("Can't mangle a using directive name!"); |
| 828 | } |
| 829 | } |
| 830 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 831 | void MicrosoftCXXNameMangler::mangleNestedName(const NamedDecl *ND) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 832 | // <postfix> ::= <unqualified-name> [<postfix>] |
| 833 | // ::= <substitution> [<postfix>] |
David Majnemer | ff9075f | 2014-08-06 03:12:47 +0000 | [diff] [blame] | 834 | const DeclContext *DC = getEffectiveDeclContext(ND); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 835 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 836 | while (!DC->isTranslationUnit()) { |
| 837 | if (isa<TagDecl>(ND) || isa<VarDecl>(ND)) { |
| 838 | unsigned Disc; |
| 839 | if (Context.getNextDiscriminator(ND, Disc)) { |
| 840 | Out << '?'; |
| 841 | mangleNumber(Disc); |
| 842 | Out << '?'; |
| 843 | } |
| 844 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 845 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 846 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { |
Alp Toker | c7dc062 | 2014-05-11 22:10:52 +0000 | [diff] [blame] | 847 | DiagnosticsEngine &Diags = Context.getDiags(); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 848 | unsigned DiagID = |
| 849 | Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 850 | "cannot mangle a local inside this block yet"); |
| 851 | Diags.Report(BD->getLocation(), DiagID); |
| 852 | |
| 853 | // FIXME: This is completely, utterly, wrong; see ItaniumMangle |
| 854 | // for how this should be done. |
| 855 | Out << "__block_invoke" << Context.getBlockId(BD, false); |
| 856 | Out << '@'; |
| 857 | continue; |
| 858 | } else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) { |
| 859 | mangleObjCMethodName(Method); |
| 860 | } else if (isa<NamedDecl>(DC)) { |
| 861 | ND = cast<NamedDecl>(DC); |
| 862 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 863 | mangle(FD, "?"); |
| 864 | break; |
| 865 | } else |
| 866 | mangleUnqualifiedName(ND); |
| 867 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 868 | DC = DC->getParent(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 872 | void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 873 | // Microsoft uses the names on the case labels for these dtor variants. Clang |
| 874 | // uses the Itanium terminology internally. Everything in this ABI delegates |
| 875 | // towards the base dtor. |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 876 | switch (T) { |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 877 | // <operator-name> ::= ?1 # destructor |
| 878 | case Dtor_Base: Out << "?1"; return; |
| 879 | // <operator-name> ::= ?_D # vbase destructor |
| 880 | case Dtor_Complete: Out << "?_D"; return; |
| 881 | // <operator-name> ::= ?_G # scalar deleting destructor |
| 882 | case Dtor_Deleting: Out << "?_G"; return; |
| 883 | // <operator-name> ::= ?_E # vector deleting destructor |
| 884 | // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need |
| 885 | // it. |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 886 | case Dtor_Comdat: |
| 887 | llvm_unreachable("not expecting a COMDAT"); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 888 | } |
| 889 | llvm_unreachable("Unsupported dtor type?"); |
| 890 | } |
| 891 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 892 | void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, |
| 893 | SourceLocation Loc) { |
| 894 | switch (OO) { |
| 895 | // ?0 # constructor |
| 896 | // ?1 # destructor |
| 897 | // <operator-name> ::= ?2 # new |
| 898 | case OO_New: Out << "?2"; break; |
| 899 | // <operator-name> ::= ?3 # delete |
| 900 | case OO_Delete: Out << "?3"; break; |
| 901 | // <operator-name> ::= ?4 # = |
| 902 | case OO_Equal: Out << "?4"; break; |
| 903 | // <operator-name> ::= ?5 # >> |
| 904 | case OO_GreaterGreater: Out << "?5"; break; |
| 905 | // <operator-name> ::= ?6 # << |
| 906 | case OO_LessLess: Out << "?6"; break; |
| 907 | // <operator-name> ::= ?7 # ! |
| 908 | case OO_Exclaim: Out << "?7"; break; |
| 909 | // <operator-name> ::= ?8 # == |
| 910 | case OO_EqualEqual: Out << "?8"; break; |
| 911 | // <operator-name> ::= ?9 # != |
| 912 | case OO_ExclaimEqual: Out << "?9"; break; |
| 913 | // <operator-name> ::= ?A # [] |
| 914 | case OO_Subscript: Out << "?A"; break; |
| 915 | // ?B # conversion |
| 916 | // <operator-name> ::= ?C # -> |
| 917 | case OO_Arrow: Out << "?C"; break; |
| 918 | // <operator-name> ::= ?D # * |
| 919 | case OO_Star: Out << "?D"; break; |
| 920 | // <operator-name> ::= ?E # ++ |
| 921 | case OO_PlusPlus: Out << "?E"; break; |
| 922 | // <operator-name> ::= ?F # -- |
| 923 | case OO_MinusMinus: Out << "?F"; break; |
| 924 | // <operator-name> ::= ?G # - |
| 925 | case OO_Minus: Out << "?G"; break; |
| 926 | // <operator-name> ::= ?H # + |
| 927 | case OO_Plus: Out << "?H"; break; |
| 928 | // <operator-name> ::= ?I # & |
| 929 | case OO_Amp: Out << "?I"; break; |
| 930 | // <operator-name> ::= ?J # ->* |
| 931 | case OO_ArrowStar: Out << "?J"; break; |
| 932 | // <operator-name> ::= ?K # / |
| 933 | case OO_Slash: Out << "?K"; break; |
| 934 | // <operator-name> ::= ?L # % |
| 935 | case OO_Percent: Out << "?L"; break; |
| 936 | // <operator-name> ::= ?M # < |
| 937 | case OO_Less: Out << "?M"; break; |
| 938 | // <operator-name> ::= ?N # <= |
| 939 | case OO_LessEqual: Out << "?N"; break; |
| 940 | // <operator-name> ::= ?O # > |
| 941 | case OO_Greater: Out << "?O"; break; |
| 942 | // <operator-name> ::= ?P # >= |
| 943 | case OO_GreaterEqual: Out << "?P"; break; |
| 944 | // <operator-name> ::= ?Q # , |
| 945 | case OO_Comma: Out << "?Q"; break; |
| 946 | // <operator-name> ::= ?R # () |
| 947 | case OO_Call: Out << "?R"; break; |
| 948 | // <operator-name> ::= ?S # ~ |
| 949 | case OO_Tilde: Out << "?S"; break; |
| 950 | // <operator-name> ::= ?T # ^ |
| 951 | case OO_Caret: Out << "?T"; break; |
| 952 | // <operator-name> ::= ?U # | |
| 953 | case OO_Pipe: Out << "?U"; break; |
| 954 | // <operator-name> ::= ?V # && |
| 955 | case OO_AmpAmp: Out << "?V"; break; |
| 956 | // <operator-name> ::= ?W # || |
| 957 | case OO_PipePipe: Out << "?W"; break; |
| 958 | // <operator-name> ::= ?X # *= |
| 959 | case OO_StarEqual: Out << "?X"; break; |
| 960 | // <operator-name> ::= ?Y # += |
| 961 | case OO_PlusEqual: Out << "?Y"; break; |
| 962 | // <operator-name> ::= ?Z # -= |
| 963 | case OO_MinusEqual: Out << "?Z"; break; |
| 964 | // <operator-name> ::= ?_0 # /= |
| 965 | case OO_SlashEqual: Out << "?_0"; break; |
| 966 | // <operator-name> ::= ?_1 # %= |
| 967 | case OO_PercentEqual: Out << "?_1"; break; |
| 968 | // <operator-name> ::= ?_2 # >>= |
| 969 | case OO_GreaterGreaterEqual: Out << "?_2"; break; |
| 970 | // <operator-name> ::= ?_3 # <<= |
| 971 | case OO_LessLessEqual: Out << "?_3"; break; |
| 972 | // <operator-name> ::= ?_4 # &= |
| 973 | case OO_AmpEqual: Out << "?_4"; break; |
| 974 | // <operator-name> ::= ?_5 # |= |
| 975 | case OO_PipeEqual: Out << "?_5"; break; |
| 976 | // <operator-name> ::= ?_6 # ^= |
| 977 | case OO_CaretEqual: Out << "?_6"; break; |
| 978 | // ?_7 # vftable |
| 979 | // ?_8 # vbtable |
| 980 | // ?_9 # vcall |
| 981 | // ?_A # typeof |
| 982 | // ?_B # local static guard |
| 983 | // ?_C # string |
| 984 | // ?_D # vbase destructor |
| 985 | // ?_E # vector deleting destructor |
| 986 | // ?_F # default constructor closure |
| 987 | // ?_G # scalar deleting destructor |
| 988 | // ?_H # vector constructor iterator |
| 989 | // ?_I # vector destructor iterator |
| 990 | // ?_J # vector vbase constructor iterator |
| 991 | // ?_K # virtual displacement map |
| 992 | // ?_L # eh vector constructor iterator |
| 993 | // ?_M # eh vector destructor iterator |
| 994 | // ?_N # eh vector vbase constructor iterator |
| 995 | // ?_O # copy constructor closure |
| 996 | // ?_P<name> # udt returning <name> |
| 997 | // ?_Q # <unknown> |
| 998 | // ?_R0 # RTTI Type Descriptor |
| 999 | // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) |
| 1000 | // ?_R2 # RTTI Base Class Array |
| 1001 | // ?_R3 # RTTI Class Hierarchy Descriptor |
| 1002 | // ?_R4 # RTTI Complete Object Locator |
| 1003 | // ?_S # local vftable |
| 1004 | // ?_T # local vftable constructor closure |
| 1005 | // <operator-name> ::= ?_U # new[] |
| 1006 | case OO_Array_New: Out << "?_U"; break; |
| 1007 | // <operator-name> ::= ?_V # delete[] |
| 1008 | case OO_Array_Delete: Out << "?_V"; break; |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 1009 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1010 | case OO_Conditional: { |
| 1011 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1012 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1013 | "cannot mangle this conditional operator yet"); |
| 1014 | Diags.Report(Loc, DiagID); |
| 1015 | break; |
| 1016 | } |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 1017 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1018 | case OO_None: |
| 1019 | case NUM_OVERLOADED_OPERATORS: |
| 1020 | llvm_unreachable("Not an overloaded operator"); |
| 1021 | } |
| 1022 | } |
| 1023 | |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 1024 | void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1025 | // <source name> ::= <identifier> @ |
David Majnemer | 0d0560c | 2014-09-25 19:43:56 +0000 | [diff] [blame] | 1026 | BackRefVec::iterator Found = |
| 1027 | std::find(NameBackReferences.begin(), NameBackReferences.end(), Name); |
David Majnemer | c986fcc | 2014-06-08 04:51:13 +0000 | [diff] [blame] | 1028 | if (Found == NameBackReferences.end()) { |
David Majnemer | 0d0560c | 2014-09-25 19:43:56 +0000 | [diff] [blame] | 1029 | if (NameBackReferences.size() < 10) |
| 1030 | NameBackReferences.push_back(Name); |
David Majnemer | 956bc11 | 2013-11-25 17:50:19 +0000 | [diff] [blame] | 1031 | Out << Name << '@'; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1032 | } else { |
David Majnemer | 0d0560c | 2014-09-25 19:43:56 +0000 | [diff] [blame] | 1033 | Out << (Found - NameBackReferences.begin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 1038 | Context.mangleObjCMethodName(MD, Out); |
| 1039 | } |
| 1040 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1041 | void MicrosoftCXXNameMangler::mangleTemplateInstantiationName( |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 1042 | const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1043 | // <template-name> ::= <unscoped-template-name> <template-args> |
| 1044 | // ::= <substitution> |
| 1045 | // Always start with the unqualified name. |
| 1046 | |
| 1047 | // Templates have their own context for back references. |
| 1048 | ArgBackRefMap OuterArgsContext; |
David Majnemer | 0d0560c | 2014-09-25 19:43:56 +0000 | [diff] [blame] | 1049 | BackRefVec OuterTemplateContext; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1050 | NameBackReferences.swap(OuterTemplateContext); |
| 1051 | TypeBackReferences.swap(OuterArgsContext); |
| 1052 | |
| 1053 | mangleUnscopedTemplateName(TD); |
Reid Kleckner | 5251886 | 2013-03-20 01:40:23 +0000 | [diff] [blame] | 1054 | mangleTemplateArgs(TD, TemplateArgs); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Restore the previous back reference contexts. |
| 1057 | NameBackReferences.swap(OuterTemplateContext); |
| 1058 | TypeBackReferences.swap(OuterArgsContext); |
| 1059 | } |
| 1060 | |
| 1061 | void |
| 1062 | MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) { |
| 1063 | // <unscoped-template-name> ::= ?$ <unqualified-name> |
| 1064 | Out << "?$"; |
| 1065 | mangleUnqualifiedName(TD); |
| 1066 | } |
| 1067 | |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 1068 | void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value, |
| 1069 | bool IsBoolean) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1070 | // <integer-literal> ::= $0 <number> |
| 1071 | Out << "$0"; |
| 1072 | // Make sure booleans are encoded as 0/1. |
| 1073 | if (IsBoolean && Value.getBoolValue()) |
| 1074 | mangleNumber(1); |
Will Wilson | e74c281 | 2014-12-11 05:47:10 +0000 | [diff] [blame] | 1075 | else if (Value.isSigned()) |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 1076 | mangleNumber(Value.getSExtValue()); |
Will Wilson | e74c281 | 2014-12-11 05:47:10 +0000 | [diff] [blame] | 1077 | else |
| 1078 | mangleNumber(Value.getZExtValue()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 1081 | void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1082 | // See if this is a constant expression. |
| 1083 | llvm::APSInt Value; |
| 1084 | if (E->isIntegerConstantExpr(Value, Context.getASTContext())) { |
| 1085 | mangleIntegerLiteral(Value, E->getType()->isBooleanType()); |
| 1086 | return; |
| 1087 | } |
| 1088 | |
Reid Kleckner | b4848e7 | 2014-06-10 20:06:25 +0000 | [diff] [blame] | 1089 | // Look through no-op casts like template parameter substitutions. |
| 1090 | E = E->IgnoreParenNoopCasts(Context.getASTContext()); |
| 1091 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1092 | const CXXUuidofExpr *UE = nullptr; |
David Majnemer | 8eaab6f | 2013-08-13 06:32:20 +0000 | [diff] [blame] | 1093 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 1094 | if (UO->getOpcode() == UO_AddrOf) |
| 1095 | UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr()); |
| 1096 | } else |
| 1097 | UE = dyn_cast<CXXUuidofExpr>(E); |
| 1098 | |
| 1099 | if (UE) { |
| 1100 | // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from |
| 1101 | // const __s_GUID _GUID_{lower case UUID with underscores} |
| 1102 | StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext()); |
| 1103 | std::string Name = "_GUID_" + Uuid.lower(); |
| 1104 | std::replace(Name.begin(), Name.end(), '-', '_'); |
| 1105 | |
David Majnemer | e9cab2f | 2013-08-13 09:17:25 +0000 | [diff] [blame] | 1106 | // If we had to peek through an address-of operator, treat this like we are |
David Majnemer | 8eaab6f | 2013-08-13 06:32:20 +0000 | [diff] [blame] | 1107 | // dealing with a pointer type. Otherwise, treat it like a const reference. |
| 1108 | // |
| 1109 | // N.B. This matches up with the handling of TemplateArgument::Declaration |
| 1110 | // in mangleTemplateArg |
| 1111 | if (UE == E) |
| 1112 | Out << "$E?"; |
| 1113 | else |
| 1114 | Out << "$1?"; |
| 1115 | Out << Name << "@@3U__s_GUID@@B"; |
| 1116 | return; |
| 1117 | } |
| 1118 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1119 | // As bad as this diagnostic is, it's better than crashing. |
| 1120 | DiagnosticsEngine &Diags = Context.getDiags(); |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 1121 | unsigned DiagID = Diags.getCustomDiagID( |
| 1122 | DiagnosticsEngine::Error, "cannot yet mangle expression type %0"); |
| 1123 | Diags.Report(E->getExprLoc(), DiagID) << E->getStmtClassName() |
| 1124 | << E->getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
David Majnemer | 8265dec | 2014-03-30 16:30:54 +0000 | [diff] [blame] | 1127 | void MicrosoftCXXNameMangler::mangleTemplateArgs( |
| 1128 | const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) { |
Reid Kleckner | 11c6f61 | 2014-06-26 22:42:18 +0000 | [diff] [blame] | 1129 | // <template-args> ::= <template-arg>+ |
David Majnemer | 57fbc0c | 2014-08-05 22:43:45 +0000 | [diff] [blame] | 1130 | const TemplateParameterList *TPL = TD->getTemplateParameters(); |
| 1131 | assert(TPL->size() == TemplateArgs.size() && |
| 1132 | "size mismatch between args and parms!"); |
| 1133 | |
| 1134 | unsigned Idx = 0; |
David Majnemer | 8265dec | 2014-03-30 16:30:54 +0000 | [diff] [blame] | 1135 | for (const TemplateArgument &TA : TemplateArgs.asArray()) |
David Majnemer | 57fbc0c | 2014-08-05 22:43:45 +0000 | [diff] [blame] | 1136 | mangleTemplateArg(TD, TA, TPL->getParam(Idx++)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1139 | void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD, |
David Majnemer | 57fbc0c | 2014-08-05 22:43:45 +0000 | [diff] [blame] | 1140 | const TemplateArgument &TA, |
| 1141 | const NamedDecl *Parm) { |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 1142 | // <template-arg> ::= <type> |
| 1143 | // ::= <integer-literal> |
| 1144 | // ::= <member-data-pointer> |
| 1145 | // ::= <member-function-pointer> |
| 1146 | // ::= $E? <name> <type-encoding> |
| 1147 | // ::= $1? <name> <type-encoding> |
| 1148 | // ::= $0A@ |
| 1149 | // ::= <template-args> |
| 1150 | |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1151 | switch (TA.getKind()) { |
| 1152 | case TemplateArgument::Null: |
| 1153 | llvm_unreachable("Can't mangle null template arguments!"); |
David Majnemer | 08177c5 | 2013-08-27 08:21:25 +0000 | [diff] [blame] | 1154 | case TemplateArgument::TemplateExpansion: |
| 1155 | llvm_unreachable("Can't mangle template expansion arguments!"); |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1156 | case TemplateArgument::Type: { |
| 1157 | QualType T = TA.getAsType(); |
| 1158 | mangleType(T, SourceRange(), QMM_Escape); |
| 1159 | break; |
| 1160 | } |
David Majnemer | e8fdc06 | 2013-08-13 01:25:35 +0000 | [diff] [blame] | 1161 | case TemplateArgument::Declaration: { |
| 1162 | const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl()); |
David Majnemer | 1e378e4 | 2014-02-06 12:46:52 +0000 | [diff] [blame] | 1163 | if (isa<FieldDecl>(ND) || isa<IndirectFieldDecl>(ND)) { |
Reid Kleckner | e253b09 | 2014-02-08 01:15:37 +0000 | [diff] [blame] | 1164 | mangleMemberDataPointer( |
| 1165 | cast<CXXRecordDecl>(ND->getDeclContext())->getMostRecentDecl(), |
| 1166 | cast<ValueDecl>(ND)); |
Reid Kleckner | 09b47d1 | 2014-02-05 18:59:38 +0000 | [diff] [blame] | 1167 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Nick Lewycky | 1f52966 | 2014-02-05 23:53:29 +0000 | [diff] [blame] | 1168 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
Reid Kleckner | 09b47d1 | 2014-02-05 18:59:38 +0000 | [diff] [blame] | 1169 | if (MD && MD->isInstance()) |
Reid Kleckner | e253b09 | 2014-02-08 01:15:37 +0000 | [diff] [blame] | 1170 | mangleMemberFunctionPointer(MD->getParent()->getMostRecentDecl(), MD); |
Reid Kleckner | 09b47d1 | 2014-02-05 18:59:38 +0000 | [diff] [blame] | 1171 | else |
Nick Lewycky | 1f52966 | 2014-02-05 23:53:29 +0000 | [diff] [blame] | 1172 | mangle(FD, "$1?"); |
Reid Kleckner | 09b47d1 | 2014-02-05 18:59:38 +0000 | [diff] [blame] | 1173 | } else { |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1174 | mangle(ND, TA.getParamTypeForDecl()->isReferenceType() ? "$E?" : "$1?"); |
Reid Kleckner | 09b47d1 | 2014-02-05 18:59:38 +0000 | [diff] [blame] | 1175 | } |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1176 | break; |
David Majnemer | e8fdc06 | 2013-08-13 01:25:35 +0000 | [diff] [blame] | 1177 | } |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1178 | case TemplateArgument::Integral: |
| 1179 | mangleIntegerLiteral(TA.getAsIntegral(), |
| 1180 | TA.getIntegralType()->isBooleanType()); |
| 1181 | break; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 1182 | case TemplateArgument::NullPtr: { |
| 1183 | QualType T = TA.getNullPtrType(); |
| 1184 | if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) { |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 1185 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
David Majnemer | 1918787 | 2014-06-11 07:08:37 +0000 | [diff] [blame] | 1186 | if (MPT->isMemberFunctionPointerType() && isa<ClassTemplateDecl>(TD)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1187 | mangleMemberFunctionPointer(RD, nullptr); |
David Majnemer | 1918787 | 2014-06-11 07:08:37 +0000 | [diff] [blame] | 1188 | return; |
| 1189 | } |
| 1190 | if (MPT->isMemberDataPointer()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1191 | mangleMemberDataPointer(RD, nullptr); |
David Majnemer | 1918787 | 2014-06-11 07:08:37 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | } |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 1194 | } |
David Majnemer | 1918787 | 2014-06-11 07:08:37 +0000 | [diff] [blame] | 1195 | Out << "$0A@"; |
David Majnemer | ae465ef | 2013-08-05 21:33:59 +0000 | [diff] [blame] | 1196 | break; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 1197 | } |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1198 | case TemplateArgument::Expression: |
| 1199 | mangleExpression(TA.getAsExpr()); |
| 1200 | break; |
David Majnemer | 06fa05a | 2014-06-04 16:46:32 +0000 | [diff] [blame] | 1201 | case TemplateArgument::Pack: { |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 1202 | ArrayRef<TemplateArgument> TemplateArgs = TA.getPackAsArray(); |
David Majnemer | 06fa05a | 2014-06-04 16:46:32 +0000 | [diff] [blame] | 1203 | if (TemplateArgs.empty()) { |
David Majnemer | 57fbc0c | 2014-08-05 22:43:45 +0000 | [diff] [blame] | 1204 | if (isa<TemplateTypeParmDecl>(Parm) || |
| 1205 | isa<TemplateTemplateParmDecl>(Parm)) |
| 1206 | Out << "$$V"; |
| 1207 | else if (isa<NonTypeTemplateParmDecl>(Parm)) |
| 1208 | Out << "$S"; |
| 1209 | else |
| 1210 | llvm_unreachable("unexpected template parameter decl!"); |
David Majnemer | 06fa05a | 2014-06-04 16:46:32 +0000 | [diff] [blame] | 1211 | } else { |
| 1212 | for (const TemplateArgument &PA : TemplateArgs) |
David Majnemer | 57fbc0c | 2014-08-05 22:43:45 +0000 | [diff] [blame] | 1213 | mangleTemplateArg(TD, PA, Parm); |
David Majnemer | 06fa05a | 2014-06-04 16:46:32 +0000 | [diff] [blame] | 1214 | } |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1215 | break; |
David Majnemer | 06fa05a | 2014-06-04 16:46:32 +0000 | [diff] [blame] | 1216 | } |
David Majnemer | 8d5f9ab | 2014-07-30 08:20:03 +0000 | [diff] [blame] | 1217 | case TemplateArgument::Template: { |
| 1218 | const NamedDecl *ND = |
| 1219 | TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl(); |
| 1220 | if (const auto *TD = dyn_cast<TagDecl>(ND)) { |
| 1221 | mangleType(TD); |
| 1222 | } else if (isa<TypeAliasDecl>(ND)) { |
David Majnemer | 9a3bc59 | 2014-08-19 07:29:03 +0000 | [diff] [blame] | 1223 | Out << "$$Y"; |
| 1224 | mangleName(ND); |
David Majnemer | 8d5f9ab | 2014-07-30 08:20:03 +0000 | [diff] [blame] | 1225 | } else { |
| 1226 | llvm_unreachable("unexpected template template NamedDecl!"); |
| 1227 | } |
David Majnemer | 0db0ca4 | 2013-08-05 22:26:46 +0000 | [diff] [blame] | 1228 | break; |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1229 | } |
David Majnemer | 8d5f9ab | 2014-07-30 08:20:03 +0000 | [diff] [blame] | 1230 | } |
Reid Kleckner | f0ae35b | 2013-07-02 18:10:07 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1233 | void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, |
| 1234 | bool IsMember) { |
| 1235 | // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> |
| 1236 | // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); |
| 1237 | // 'I' means __restrict (32/64-bit). |
| 1238 | // Note that the MSVC __restrict keyword isn't the same as the C99 restrict |
| 1239 | // keyword! |
| 1240 | // <base-cvr-qualifiers> ::= A # near |
| 1241 | // ::= B # near const |
| 1242 | // ::= C # near volatile |
| 1243 | // ::= D # near const volatile |
| 1244 | // ::= E # far (16-bit) |
| 1245 | // ::= F # far const (16-bit) |
| 1246 | // ::= G # far volatile (16-bit) |
| 1247 | // ::= H # far const volatile (16-bit) |
| 1248 | // ::= I # huge (16-bit) |
| 1249 | // ::= J # huge const (16-bit) |
| 1250 | // ::= K # huge volatile (16-bit) |
| 1251 | // ::= L # huge const volatile (16-bit) |
| 1252 | // ::= M <basis> # based |
| 1253 | // ::= N <basis> # based const |
| 1254 | // ::= O <basis> # based volatile |
| 1255 | // ::= P <basis> # based const volatile |
| 1256 | // ::= Q # near member |
| 1257 | // ::= R # near const member |
| 1258 | // ::= S # near volatile member |
| 1259 | // ::= T # near const volatile member |
| 1260 | // ::= U # far member (16-bit) |
| 1261 | // ::= V # far const member (16-bit) |
| 1262 | // ::= W # far volatile member (16-bit) |
| 1263 | // ::= X # far const volatile member (16-bit) |
| 1264 | // ::= Y # huge member (16-bit) |
| 1265 | // ::= Z # huge const member (16-bit) |
| 1266 | // ::= 0 # huge volatile member (16-bit) |
| 1267 | // ::= 1 # huge const volatile member (16-bit) |
| 1268 | // ::= 2 <basis> # based member |
| 1269 | // ::= 3 <basis> # based const member |
| 1270 | // ::= 4 <basis> # based volatile member |
| 1271 | // ::= 5 <basis> # based const volatile member |
| 1272 | // ::= 6 # near function (pointers only) |
| 1273 | // ::= 7 # far function (pointers only) |
| 1274 | // ::= 8 # near method (pointers only) |
| 1275 | // ::= 9 # far method (pointers only) |
| 1276 | // ::= _A <basis> # based function (pointers only) |
| 1277 | // ::= _B <basis> # based function (far?) (pointers only) |
| 1278 | // ::= _C <basis> # based method (pointers only) |
| 1279 | // ::= _D <basis> # based method (far?) (pointers only) |
| 1280 | // ::= _E # block (Clang) |
| 1281 | // <basis> ::= 0 # __based(void) |
| 1282 | // ::= 1 # __based(segment)? |
| 1283 | // ::= 2 <name> # __based(name) |
| 1284 | // ::= 3 # ? |
| 1285 | // ::= 4 # ? |
| 1286 | // ::= 5 # not really based |
| 1287 | bool HasConst = Quals.hasConst(), |
| 1288 | HasVolatile = Quals.hasVolatile(); |
David Majnemer | 89594f3 | 2013-08-05 22:43:06 +0000 | [diff] [blame] | 1289 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1290 | if (!IsMember) { |
| 1291 | if (HasConst && HasVolatile) { |
| 1292 | Out << 'D'; |
| 1293 | } else if (HasVolatile) { |
| 1294 | Out << 'C'; |
| 1295 | } else if (HasConst) { |
| 1296 | Out << 'B'; |
| 1297 | } else { |
| 1298 | Out << 'A'; |
| 1299 | } |
| 1300 | } else { |
| 1301 | if (HasConst && HasVolatile) { |
| 1302 | Out << 'T'; |
| 1303 | } else if (HasVolatile) { |
| 1304 | Out << 'S'; |
| 1305 | } else if (HasConst) { |
| 1306 | Out << 'R'; |
| 1307 | } else { |
| 1308 | Out << 'Q'; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | // FIXME: For now, just drop all extension qualifiers on the floor. |
| 1313 | } |
| 1314 | |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1315 | void |
David Majnemer | e3785bb | 2014-04-23 05:16:56 +0000 | [diff] [blame] | 1316 | MicrosoftCXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) { |
| 1317 | // <ref-qualifier> ::= G # lvalue reference |
| 1318 | // ::= H # rvalue-reference |
| 1319 | switch (RefQualifier) { |
| 1320 | case RQ_None: |
| 1321 | break; |
| 1322 | |
| 1323 | case RQ_LValue: |
| 1324 | Out << 'G'; |
| 1325 | break; |
| 1326 | |
| 1327 | case RQ_RValue: |
| 1328 | Out << 'H'; |
| 1329 | break; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | void |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1334 | MicrosoftCXXNameMangler::manglePointerExtQualifiers(Qualifiers Quals, |
| 1335 | const Type *PointeeType) { |
| 1336 | bool HasRestrict = Quals.hasRestrict(); |
| 1337 | if (PointersAre64Bit && (!PointeeType || !PointeeType->isFunctionType())) |
| 1338 | Out << 'E'; |
| 1339 | |
| 1340 | if (HasRestrict) |
| 1341 | Out << 'I'; |
| 1342 | } |
| 1343 | |
| 1344 | void MicrosoftCXXNameMangler::manglePointerCVQualifiers(Qualifiers Quals) { |
| 1345 | // <pointer-cv-qualifiers> ::= P # no qualifiers |
| 1346 | // ::= Q # const |
| 1347 | // ::= R # volatile |
| 1348 | // ::= S # const volatile |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1349 | bool HasConst = Quals.hasConst(), |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1350 | HasVolatile = Quals.hasVolatile(); |
David Majnemer | 0b6bf8a | 2014-02-18 12:58:35 +0000 | [diff] [blame] | 1351 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1352 | if (HasConst && HasVolatile) { |
| 1353 | Out << 'S'; |
| 1354 | } else if (HasVolatile) { |
| 1355 | Out << 'R'; |
| 1356 | } else if (HasConst) { |
| 1357 | Out << 'Q'; |
| 1358 | } else { |
| 1359 | Out << 'P'; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | void MicrosoftCXXNameMangler::mangleArgumentType(QualType T, |
| 1364 | SourceRange Range) { |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1365 | // MSVC will backreference two canonically equivalent types that have slightly |
| 1366 | // different manglings when mangled alone. |
David Majnemer | 5a1b204 | 2013-09-11 04:44:30 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Decayed types do not match up with non-decayed versions of the same type. |
| 1369 | // |
| 1370 | // e.g. |
| 1371 | // void (*x)(void) will not form a backreference with void x(void) |
| 1372 | void *TypePtr; |
| 1373 | if (const DecayedType *DT = T->getAs<DecayedType>()) { |
| 1374 | TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr(); |
| 1375 | // If the original parameter was textually written as an array, |
| 1376 | // instead treat the decayed parameter like it's const. |
| 1377 | // |
| 1378 | // e.g. |
| 1379 | // int [] -> int * const |
| 1380 | if (DT->getOriginalType()->isArrayType()) |
| 1381 | T = T.withConst(); |
| 1382 | } else |
| 1383 | TypePtr = T.getCanonicalType().getAsOpaquePtr(); |
| 1384 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1385 | ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr); |
| 1386 | |
| 1387 | if (Found == TypeBackReferences.end()) { |
| 1388 | size_t OutSizeBefore = Out.GetNumBytesInBuffer(); |
| 1389 | |
David Majnemer | 5a1b204 | 2013-09-11 04:44:30 +0000 | [diff] [blame] | 1390 | mangleType(T, Range, QMM_Drop); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1391 | |
| 1392 | // See if it's worth creating a back reference. |
| 1393 | // Only types longer than 1 character are considered |
| 1394 | // and only 10 back references slots are available: |
| 1395 | bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1); |
| 1396 | if (LongerThanOneChar && TypeBackReferences.size() < 10) { |
| 1397 | size_t Size = TypeBackReferences.size(); |
| 1398 | TypeBackReferences[TypePtr] = Size; |
| 1399 | } |
| 1400 | } else { |
| 1401 | Out << Found->second; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range, |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1406 | QualifierMangleMode QMM) { |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1407 | // Don't use the canonical types. MSVC includes things like 'const' on |
| 1408 | // pointer arguments to function pointers that canonicalization strips away. |
| 1409 | T = T.getDesugaredType(getASTContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1410 | Qualifiers Quals = T.getLocalQualifiers(); |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1411 | if (const ArrayType *AT = getASTContext().getAsArrayType(T)) { |
| 1412 | // If there were any Quals, getAsArrayType() pushed them onto the array |
| 1413 | // element type. |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1414 | if (QMM == QMM_Mangle) |
| 1415 | Out << 'A'; |
| 1416 | else if (QMM == QMM_Escape || QMM == QMM_Result) |
| 1417 | Out << "$$B"; |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1418 | mangleArrayType(AT); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() || |
| 1423 | T->isBlockPointerType(); |
| 1424 | |
| 1425 | switch (QMM) { |
| 1426 | case QMM_Drop: |
| 1427 | break; |
| 1428 | case QMM_Mangle: |
| 1429 | if (const FunctionType *FT = dyn_cast<FunctionType>(T)) { |
| 1430 | Out << '6'; |
Timur Iskhodzhanov | 555a772 | 2013-10-04 11:25:05 +0000 | [diff] [blame] | 1431 | mangleFunctionType(FT); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1432 | return; |
| 1433 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1434 | mangleQualifiers(Quals, false); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | case QMM_Escape: |
| 1437 | if (!IsPointer && Quals) { |
| 1438 | Out << "$$C"; |
| 1439 | mangleQualifiers(Quals, false); |
| 1440 | } |
| 1441 | break; |
| 1442 | case QMM_Result: |
| 1443 | if ((!IsPointer && Quals) || isa<TagType>(T)) { |
| 1444 | Out << '?'; |
| 1445 | mangleQualifiers(Quals, false); |
| 1446 | } |
| 1447 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1450 | // We have to mangle these now, while we still have enough information. |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1451 | if (IsPointer) { |
| 1452 | manglePointerCVQualifiers(Quals); |
| 1453 | manglePointerExtQualifiers(Quals, T->getPointeeType().getTypePtr()); |
| 1454 | } |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1455 | const Type *ty = T.getTypePtr(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1456 | |
| 1457 | switch (ty->getTypeClass()) { |
| 1458 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 1459 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
| 1460 | case Type::CLASS: \ |
| 1461 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
| 1462 | return; |
| 1463 | #define TYPE(CLASS, PARENT) \ |
| 1464 | case Type::CLASS: \ |
| 1465 | mangleType(cast<CLASS##Type>(ty), Range); \ |
| 1466 | break; |
| 1467 | #include "clang/AST/TypeNodes.def" |
| 1468 | #undef ABSTRACT_TYPE |
| 1469 | #undef NON_CANONICAL_TYPE |
| 1470 | #undef TYPE |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, |
| 1475 | SourceRange Range) { |
| 1476 | // <type> ::= <builtin-type> |
| 1477 | // <builtin-type> ::= X # void |
| 1478 | // ::= C # signed char |
| 1479 | // ::= D # char |
| 1480 | // ::= E # unsigned char |
| 1481 | // ::= F # short |
| 1482 | // ::= G # unsigned short (or wchar_t if it's not a builtin) |
| 1483 | // ::= H # int |
| 1484 | // ::= I # unsigned int |
| 1485 | // ::= J # long |
| 1486 | // ::= K # unsigned long |
| 1487 | // L # <none> |
| 1488 | // ::= M # float |
| 1489 | // ::= N # double |
| 1490 | // ::= O # long double (__float80 is mangled differently) |
| 1491 | // ::= _J # long long, __int64 |
| 1492 | // ::= _K # unsigned long long, __int64 |
| 1493 | // ::= _L # __int128 |
| 1494 | // ::= _M # unsigned __int128 |
| 1495 | // ::= _N # bool |
| 1496 | // _O # <array in parameter> |
| 1497 | // ::= _T # __float80 (Intel) |
| 1498 | // ::= _W # wchar_t |
| 1499 | // ::= _Z # __float80 (Digital Mars) |
| 1500 | switch (T->getKind()) { |
| 1501 | case BuiltinType::Void: Out << 'X'; break; |
| 1502 | case BuiltinType::SChar: Out << 'C'; break; |
| 1503 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; |
| 1504 | case BuiltinType::UChar: Out << 'E'; break; |
| 1505 | case BuiltinType::Short: Out << 'F'; break; |
| 1506 | case BuiltinType::UShort: Out << 'G'; break; |
| 1507 | case BuiltinType::Int: Out << 'H'; break; |
| 1508 | case BuiltinType::UInt: Out << 'I'; break; |
| 1509 | case BuiltinType::Long: Out << 'J'; break; |
| 1510 | case BuiltinType::ULong: Out << 'K'; break; |
| 1511 | case BuiltinType::Float: Out << 'M'; break; |
| 1512 | case BuiltinType::Double: Out << 'N'; break; |
| 1513 | // TODO: Determine size and mangle accordingly |
| 1514 | case BuiltinType::LongDouble: Out << 'O'; break; |
| 1515 | case BuiltinType::LongLong: Out << "_J"; break; |
| 1516 | case BuiltinType::ULongLong: Out << "_K"; break; |
| 1517 | case BuiltinType::Int128: Out << "_L"; break; |
| 1518 | case BuiltinType::UInt128: Out << "_M"; break; |
| 1519 | case BuiltinType::Bool: Out << "_N"; break; |
David Majnemer | eca5a20 | 2014-11-21 09:06:49 +0000 | [diff] [blame] | 1520 | case BuiltinType::Char16: Out << "_S"; break; |
| 1521 | case BuiltinType::Char32: Out << "_U"; break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1522 | case BuiltinType::WChar_S: |
| 1523 | case BuiltinType::WChar_U: Out << "_W"; break; |
| 1524 | |
| 1525 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1526 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 1527 | case BuiltinType::Id: |
| 1528 | #include "clang/AST/BuiltinTypes.def" |
| 1529 | case BuiltinType::Dependent: |
| 1530 | llvm_unreachable("placeholder types shouldn't get to name mangling"); |
| 1531 | |
| 1532 | case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; |
| 1533 | case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; |
| 1534 | case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1535 | |
| 1536 | case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break; |
| 1537 | case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break; |
| 1538 | case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break; |
| 1539 | case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break; |
| 1540 | case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break; |
| 1541 | case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1542 | case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1543 | case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break; |
David Majnemer | b4119f7 | 2013-12-13 01:06:04 +0000 | [diff] [blame] | 1544 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1545 | case BuiltinType::NullPtr: Out << "$$T"; break; |
| 1546 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1547 | case BuiltinType::Half: { |
| 1548 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1549 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1550 | "cannot mangle this built-in %0 type yet"); |
| 1551 | Diags.Report(Range.getBegin(), DiagID) |
| 1552 | << T->getName(Context.getASTContext().getPrintingPolicy()) |
| 1553 | << Range; |
| 1554 | break; |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | // <type> ::= <function-type> |
| 1560 | void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T, |
| 1561 | SourceRange) { |
| 1562 | // Structors only appear in decls, so at this point we know it's not a |
| 1563 | // structor type. |
| 1564 | // FIXME: This may not be lambda-friendly. |
David Majnemer | 2f6a96e | 2014-08-12 17:53:10 +0000 | [diff] [blame] | 1565 | if (T->getTypeQuals() || T->getRefQualifier() != RQ_None) { |
| 1566 | Out << "$$A8@@"; |
| 1567 | mangleFunctionType(T, /*D=*/nullptr, /*ForceThisQuals=*/true); |
| 1568 | } else { |
| 1569 | Out << "$$A6"; |
| 1570 | mangleFunctionType(T); |
| 1571 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1572 | } |
| 1573 | void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T, |
| 1574 | SourceRange) { |
| 1575 | llvm_unreachable("Can't mangle K&R function prototypes"); |
| 1576 | } |
| 1577 | |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1578 | void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T, |
| 1579 | const FunctionDecl *D, |
David Majnemer | 2f6a96e | 2014-08-12 17:53:10 +0000 | [diff] [blame] | 1580 | bool ForceThisQuals) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1581 | // <function-type> ::= <this-cvr-qualifiers> <calling-convention> |
| 1582 | // <return-type> <argument-list> <throw-spec> |
| 1583 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 1584 | |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1585 | SourceRange Range; |
| 1586 | if (D) Range = D->getSourceRange(); |
| 1587 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1588 | bool IsStructor = false, HasThisQuals = ForceThisQuals, IsCtorClosure = false; |
| 1589 | CallingConv CC = T->getCallConv(); |
Timur Iskhodzhanov | 555a772 | 2013-10-04 11:25:05 +0000 | [diff] [blame] | 1590 | if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) { |
| 1591 | if (MD->isInstance()) |
David Majnemer | 2f6a96e | 2014-08-12 17:53:10 +0000 | [diff] [blame] | 1592 | HasThisQuals = true; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1593 | if (isa<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | 555a772 | 2013-10-04 11:25:05 +0000 | [diff] [blame] | 1594 | IsStructor = true; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1595 | } else if (isa<CXXConstructorDecl>(MD)) { |
| 1596 | IsStructor = true; |
| 1597 | IsCtorClosure = (StructorType == Ctor_CopyingClosure || |
| 1598 | StructorType == Ctor_DefaultClosure) && |
| 1599 | getStructor(MD) == Structor; |
| 1600 | if (IsCtorClosure) |
| 1601 | CC = getASTContext().getDefaultCallingConvention( |
| 1602 | /*IsVariadic=*/false, /*IsCXXMethod=*/true); |
| 1603 | } |
Timur Iskhodzhanov | 555a772 | 2013-10-04 11:25:05 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1606 | // If this is a C++ instance method, mangle the CVR qualifiers for the |
| 1607 | // this pointer. |
David Majnemer | 2f6a96e | 2014-08-12 17:53:10 +0000 | [diff] [blame] | 1608 | if (HasThisQuals) { |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1609 | Qualifiers Quals = Qualifiers::fromCVRMask(Proto->getTypeQuals()); |
David Majnemer | 2f6a96e | 2014-08-12 17:53:10 +0000 | [diff] [blame] | 1610 | manglePointerExtQualifiers(Quals, /*PointeeType=*/nullptr); |
David Majnemer | e3785bb | 2014-04-23 05:16:56 +0000 | [diff] [blame] | 1611 | mangleRefQualifier(Proto->getRefQualifier()); |
David Majnemer | 2f6a96e | 2014-08-12 17:53:10 +0000 | [diff] [blame] | 1612 | mangleQualifiers(Quals, /*IsMember=*/false); |
David Majnemer | 6dda7bb | 2013-08-15 08:13:23 +0000 | [diff] [blame] | 1613 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1614 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1615 | mangleCallingConvention(CC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1616 | |
| 1617 | // <return-type> ::= <type> |
| 1618 | // ::= @ # structors (they have no declared return type) |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1619 | if (IsStructor) { |
| 1620 | if (isa<CXXDestructorDecl>(D) && D == Structor && |
| 1621 | StructorType == Dtor_Deleting) { |
| 1622 | // The scalar deleting destructor takes an extra int argument. |
| 1623 | // However, the FunctionType generated has 0 arguments. |
| 1624 | // FIXME: This is a temporary hack. |
| 1625 | // Maybe should fix the FunctionType creation instead? |
Timur Iskhodzhanov | f46993e | 2013-08-26 10:32:04 +0000 | [diff] [blame] | 1626 | Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z"); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1627 | return; |
| 1628 | } |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1629 | if (IsCtorClosure) { |
| 1630 | // Default constructor closure and copy constructor closure both return |
| 1631 | // void. |
| 1632 | Out << 'X'; |
| 1633 | |
| 1634 | if (StructorType == Ctor_DefaultClosure) { |
| 1635 | // Default constructor closure always has no arguments. |
| 1636 | Out << 'X'; |
| 1637 | } else if (StructorType == Ctor_CopyingClosure) { |
| 1638 | // Copy constructor closure always takes an unqualified reference. |
| 1639 | mangleArgumentType(getASTContext().getLValueReferenceType( |
| 1640 | Proto->getParamType(0) |
| 1641 | ->getAs<LValueReferenceType>() |
| 1642 | ->getPointeeType(), |
| 1643 | /*SpelledAsLValue=*/true), |
| 1644 | Range); |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 1645 | Out << '@'; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1646 | } else { |
| 1647 | llvm_unreachable("unexpected constructor closure!"); |
| 1648 | } |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 1649 | Out << 'Z'; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1650 | return; |
| 1651 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1652 | Out << '@'; |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1653 | } else { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1654 | QualType ResultType = Proto->getReturnType(); |
David Majnemer | 2e1e0491 | 2014-04-01 05:29:46 +0000 | [diff] [blame] | 1655 | if (const auto *AT = |
| 1656 | dyn_cast_or_null<AutoType>(ResultType->getContainedAutoType())) { |
| 1657 | Out << '?'; |
| 1658 | mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false); |
| 1659 | Out << '?'; |
| 1660 | mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>"); |
| 1661 | Out << '@'; |
| 1662 | } else { |
| 1663 | if (ResultType->isVoidType()) |
| 1664 | ResultType = ResultType.getUnqualifiedType(); |
| 1665 | mangleType(ResultType, Range, QMM_Result); |
| 1666 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | // <argument-list> ::= X # void |
| 1670 | // ::= <type>+ @ |
| 1671 | // ::= <type>* Z # varargs |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1672 | if (Proto->getNumParams() == 0 && !Proto->isVariadic()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1673 | Out << 'X'; |
| 1674 | } else { |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1675 | // Happens for function pointer type arguments for example. |
David Majnemer | b926dbc | 2014-04-23 05:16:53 +0000 | [diff] [blame] | 1676 | for (const QualType Arg : Proto->param_types()) |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 1677 | mangleArgumentType(Arg, Range); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1678 | // <builtin-type> ::= Z # ellipsis |
| 1679 | if (Proto->isVariadic()) |
| 1680 | Out << 'Z'; |
| 1681 | else |
| 1682 | Out << '@'; |
| 1683 | } |
| 1684 | |
| 1685 | mangleThrowSpecification(Proto); |
| 1686 | } |
| 1687 | |
| 1688 | void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1689 | // <function-class> ::= <member-function> E? # E designates a 64-bit 'this' |
| 1690 | // # pointer. in 64-bit mode *all* |
| 1691 | // # 'this' pointers are 64-bit. |
| 1692 | // ::= <global-function> |
| 1693 | // <member-function> ::= A # private: near |
| 1694 | // ::= B # private: far |
| 1695 | // ::= C # private: static near |
| 1696 | // ::= D # private: static far |
| 1697 | // ::= E # private: virtual near |
| 1698 | // ::= F # private: virtual far |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1699 | // ::= I # protected: near |
| 1700 | // ::= J # protected: far |
| 1701 | // ::= K # protected: static near |
| 1702 | // ::= L # protected: static far |
| 1703 | // ::= M # protected: virtual near |
| 1704 | // ::= N # protected: virtual far |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1705 | // ::= Q # public: near |
| 1706 | // ::= R # public: far |
| 1707 | // ::= S # public: static near |
| 1708 | // ::= T # public: static far |
| 1709 | // ::= U # public: virtual near |
| 1710 | // ::= V # public: virtual far |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1711 | // <global-function> ::= Y # global near |
| 1712 | // ::= Z # global far |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1713 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 1714 | switch (MD->getAccess()) { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 1715 | case AS_none: |
| 1716 | llvm_unreachable("Unsupported access specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1717 | case AS_private: |
| 1718 | if (MD->isStatic()) |
| 1719 | Out << 'C'; |
| 1720 | else if (MD->isVirtual()) |
| 1721 | Out << 'E'; |
| 1722 | else |
| 1723 | Out << 'A'; |
| 1724 | break; |
| 1725 | case AS_protected: |
| 1726 | if (MD->isStatic()) |
| 1727 | Out << 'K'; |
| 1728 | else if (MD->isVirtual()) |
| 1729 | Out << 'M'; |
| 1730 | else |
| 1731 | Out << 'I'; |
| 1732 | break; |
| 1733 | case AS_public: |
| 1734 | if (MD->isStatic()) |
| 1735 | Out << 'S'; |
| 1736 | else if (MD->isVirtual()) |
| 1737 | Out << 'U'; |
| 1738 | else |
| 1739 | Out << 'Q'; |
| 1740 | } |
| 1741 | } else |
| 1742 | Out << 'Y'; |
| 1743 | } |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1744 | void MicrosoftCXXNameMangler::mangleCallingConvention(CallingConv CC) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1745 | // <calling-convention> ::= A # __cdecl |
| 1746 | // ::= B # __export __cdecl |
| 1747 | // ::= C # __pascal |
| 1748 | // ::= D # __export __pascal |
| 1749 | // ::= E # __thiscall |
| 1750 | // ::= F # __export __thiscall |
| 1751 | // ::= G # __stdcall |
| 1752 | // ::= H # __export __stdcall |
| 1753 | // ::= I # __fastcall |
| 1754 | // ::= J # __export __fastcall |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 1755 | // ::= Q # __vectorcall |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1756 | // The 'export' calling conventions are from a bygone era |
| 1757 | // (*cough*Win16*cough*) when functions were declared for export with |
| 1758 | // that keyword. (It didn't actually export them, it just made them so |
| 1759 | // that they could be in a DLL and somebody from another module could call |
| 1760 | // them.) |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1761 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1762 | switch (CC) { |
| 1763 | default: |
| 1764 | llvm_unreachable("Unsupported CC for mangling"); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 1765 | case CC_X86_64Win64: |
| 1766 | case CC_X86_64SysV: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1767 | case CC_C: Out << 'A'; break; |
| 1768 | case CC_X86Pascal: Out << 'C'; break; |
| 1769 | case CC_X86ThisCall: Out << 'E'; break; |
| 1770 | case CC_X86StdCall: Out << 'G'; break; |
| 1771 | case CC_X86FastCall: Out << 'I'; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 1772 | case CC_X86VectorCall: Out << 'Q'; break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1773 | } |
| 1774 | } |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 1775 | void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) { |
| 1776 | mangleCallingConvention(T->getCallConv()); |
| 1777 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1778 | void MicrosoftCXXNameMangler::mangleThrowSpecification( |
| 1779 | const FunctionProtoType *FT) { |
| 1780 | // <throw-spec> ::= Z # throw(...) (default) |
| 1781 | // ::= @ # throw() or __declspec/__attribute__((nothrow)) |
| 1782 | // ::= <type>+ |
| 1783 | // NOTE: Since the Microsoft compiler ignores throw specifications, they are |
| 1784 | // all actually mangled as 'Z'. (They're ignored because their associated |
| 1785 | // functionality isn't implemented, and probably never will be.) |
| 1786 | Out << 'Z'; |
| 1787 | } |
| 1788 | |
| 1789 | void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T, |
| 1790 | SourceRange Range) { |
| 1791 | // Probably should be mangled as a template instantiation; need to see what |
| 1792 | // VC does first. |
| 1793 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1794 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1795 | "cannot mangle this unresolved dependent type yet"); |
| 1796 | Diags.Report(Range.getBegin(), DiagID) |
| 1797 | << Range; |
| 1798 | } |
| 1799 | |
| 1800 | // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> |
| 1801 | // <union-type> ::= T <name> |
| 1802 | // <struct-type> ::= U <name> |
| 1803 | // <class-type> ::= V <name> |
David Majnemer | 048f90c | 2013-12-09 04:28:34 +0000 | [diff] [blame] | 1804 | // <enum-type> ::= W4 <name> |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1805 | void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) { |
David Majnemer | 0db0ca4 | 2013-08-05 22:26:46 +0000 | [diff] [blame] | 1806 | mangleType(cast<TagType>(T)->getDecl()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1807 | } |
| 1808 | void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) { |
David Majnemer | 0db0ca4 | 2013-08-05 22:26:46 +0000 | [diff] [blame] | 1809 | mangleType(cast<TagType>(T)->getDecl()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1810 | } |
David Majnemer | 0db0ca4 | 2013-08-05 22:26:46 +0000 | [diff] [blame] | 1811 | void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) { |
| 1812 | switch (TD->getTagKind()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1813 | case TTK_Union: |
| 1814 | Out << 'T'; |
| 1815 | break; |
| 1816 | case TTK_Struct: |
| 1817 | case TTK_Interface: |
| 1818 | Out << 'U'; |
| 1819 | break; |
| 1820 | case TTK_Class: |
| 1821 | Out << 'V'; |
| 1822 | break; |
| 1823 | case TTK_Enum: |
David Majnemer | 048f90c | 2013-12-09 04:28:34 +0000 | [diff] [blame] | 1824 | Out << "W4"; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1825 | break; |
| 1826 | } |
David Majnemer | 0db0ca4 | 2013-08-05 22:26:46 +0000 | [diff] [blame] | 1827 | mangleName(TD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | // <type> ::= <array-type> |
| 1831 | // <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> |
| 1832 | // [Y <dimension-count> <dimension>+] |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1833 | // <element-type> # as global, E is never required |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1834 | // It's supposed to be the other way around, but for some strange reason, it |
| 1835 | // isn't. Today this behavior is retained for the sole purpose of backwards |
| 1836 | // compatibility. |
David Majnemer | 5a1b204 | 2013-09-11 04:44:30 +0000 | [diff] [blame] | 1837 | void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1838 | // This isn't a recursive mangling, so now we have to do it all in this |
| 1839 | // one call. |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1840 | manglePointerCVQualifiers(T->getElementType().getQualifiers()); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1841 | mangleType(T->getElementType(), SourceRange()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1842 | } |
| 1843 | void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T, |
| 1844 | SourceRange) { |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1845 | llvm_unreachable("Should have been special cased"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1846 | } |
| 1847 | void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T, |
| 1848 | SourceRange) { |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1849 | llvm_unreachable("Should have been special cased"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1850 | } |
| 1851 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T, |
| 1852 | SourceRange) { |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1853 | llvm_unreachable("Should have been special cased"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1854 | } |
| 1855 | void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T, |
| 1856 | SourceRange) { |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1857 | llvm_unreachable("Should have been special cased"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1858 | } |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1859 | void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) { |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1860 | QualType ElementTy(T, 0); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1861 | SmallVector<llvm::APInt, 3> Dimensions; |
| 1862 | for (;;) { |
| 1863 | if (const ConstantArrayType *CAT = |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 1864 | getASTContext().getAsConstantArrayType(ElementTy)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1865 | Dimensions.push_back(CAT->getSize()); |
| 1866 | ElementTy = CAT->getElementType(); |
| 1867 | } else if (ElementTy->isVariableArrayType()) { |
| 1868 | const VariableArrayType *VAT = |
| 1869 | getASTContext().getAsVariableArrayType(ElementTy); |
| 1870 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1871 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1872 | "cannot mangle this variable-length array yet"); |
| 1873 | Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID) |
| 1874 | << VAT->getBracketsRange(); |
| 1875 | return; |
| 1876 | } else if (ElementTy->isDependentSizedArrayType()) { |
| 1877 | // The dependent expression has to be folded into a constant (TODO). |
| 1878 | const DependentSizedArrayType *DSAT = |
| 1879 | getASTContext().getAsDependentSizedArrayType(ElementTy); |
| 1880 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1881 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1882 | "cannot mangle this dependent-length array yet"); |
| 1883 | Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID) |
| 1884 | << DSAT->getBracketsRange(); |
| 1885 | return; |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1886 | } else if (const IncompleteArrayType *IAT = |
David Majnemer | 02e9af4 | 2014-04-23 05:16:51 +0000 | [diff] [blame] | 1887 | getASTContext().getAsIncompleteArrayType(ElementTy)) { |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1888 | Dimensions.push_back(llvm::APInt(32, 0)); |
| 1889 | ElementTy = IAT->getElementType(); |
| 1890 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1891 | else break; |
| 1892 | } |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1893 | Out << 'Y'; |
| 1894 | // <dimension-count> ::= <number> # number of extra dimensions |
| 1895 | mangleNumber(Dimensions.size()); |
David Majnemer | b926dbc | 2014-04-23 05:16:53 +0000 | [diff] [blame] | 1896 | for (const llvm::APInt &Dimension : Dimensions) |
| 1897 | mangleNumber(Dimension.getLimitedValue()); |
Reid Kleckner | 18da98e | 2013-06-24 19:21:52 +0000 | [diff] [blame] | 1898 | mangleType(ElementTy, SourceRange(), QMM_Escape); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | // <type> ::= <pointer-to-member-type> |
| 1902 | // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> |
| 1903 | // <class name> <type> |
| 1904 | void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T, |
| 1905 | SourceRange Range) { |
| 1906 | QualType PointeeType = T->getPointeeType(); |
| 1907 | if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { |
| 1908 | Out << '8'; |
| 1909 | mangleName(T->getClass()->castAs<RecordType>()->getDecl()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1910 | mangleFunctionType(FPT, nullptr, true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1911 | } else { |
| 1912 | mangleQualifiers(PointeeType.getQualifiers(), true); |
| 1913 | mangleName(T->getClass()->castAs<RecordType>()->getDecl()); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1914 | mangleType(PointeeType, Range, QMM_Drop); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T, |
| 1919 | SourceRange Range) { |
| 1920 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1921 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1922 | "cannot mangle this template type parameter type yet"); |
| 1923 | Diags.Report(Range.getBegin(), DiagID) |
| 1924 | << Range; |
| 1925 | } |
| 1926 | |
| 1927 | void MicrosoftCXXNameMangler::mangleType( |
| 1928 | const SubstTemplateTypeParmPackType *T, |
| 1929 | SourceRange Range) { |
| 1930 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1931 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1932 | "cannot mangle this substituted parameter pack yet"); |
| 1933 | Diags.Report(Range.getBegin(), DiagID) |
| 1934 | << Range; |
| 1935 | } |
| 1936 | |
| 1937 | // <type> ::= <pointer-type> |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1938 | // <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type> |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 1939 | // # the E is required for 64-bit non-static pointers |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1940 | void MicrosoftCXXNameMangler::mangleType(const PointerType *T, |
| 1941 | SourceRange Range) { |
| 1942 | QualType PointeeTy = T->getPointeeType(); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1943 | mangleType(PointeeTy, Range); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1944 | } |
| 1945 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T, |
| 1946 | SourceRange Range) { |
| 1947 | // Object pointers never have qualifiers. |
| 1948 | Out << 'A'; |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1949 | manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1950 | mangleType(T->getPointeeType(), Range); |
| 1951 | } |
| 1952 | |
| 1953 | // <type> ::= <reference-type> |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1954 | // <reference-type> ::= A E? <cvr-qualifiers> <type> |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 1955 | // # the E is required for 64-bit non-static lvalue references |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1956 | void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T, |
| 1957 | SourceRange Range) { |
| 1958 | Out << 'A'; |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1959 | manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr()); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1960 | mangleType(T->getPointeeType(), Range); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | // <type> ::= <r-value-reference-type> |
Reid Kleckner | 369f316 | 2013-05-14 20:30:42 +0000 | [diff] [blame] | 1964 | // <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type> |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 1965 | // # the E is required for 64-bit non-static rvalue references |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1966 | void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T, |
| 1967 | SourceRange Range) { |
| 1968 | Out << "$$Q"; |
David Majnemer | 8eec58f | 2014-02-18 14:20:10 +0000 | [diff] [blame] | 1969 | manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr()); |
Peter Collingbourne | 2816c02 | 2013-04-25 04:25:40 +0000 | [diff] [blame] | 1970 | mangleType(T->getPointeeType(), Range); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | void MicrosoftCXXNameMangler::mangleType(const ComplexType *T, |
| 1974 | SourceRange Range) { |
| 1975 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 1976 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 1977 | "cannot mangle this complex number type yet"); |
| 1978 | Diags.Report(Range.getBegin(), DiagID) |
| 1979 | << Range; |
| 1980 | } |
| 1981 | |
| 1982 | void MicrosoftCXXNameMangler::mangleType(const VectorType *T, |
| 1983 | SourceRange Range) { |
Reid Kleckner | e7e64d8 | 2013-03-26 16:56:59 +0000 | [diff] [blame] | 1984 | const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>(); |
| 1985 | assert(ET && "vectors with non-builtin elements are unsupported"); |
| 1986 | uint64_t Width = getASTContext().getTypeSize(T); |
| 1987 | // Pattern match exactly the typedefs in our intrinsic headers. Anything that |
| 1988 | // doesn't match the Intel types uses a custom mangling below. |
| 1989 | bool IntelVector = true; |
| 1990 | if (Width == 64 && ET->getKind() == BuiltinType::LongLong) { |
| 1991 | Out << "T__m64"; |
| 1992 | } else if (Width == 128 || Width == 256) { |
| 1993 | if (ET->getKind() == BuiltinType::Float) |
| 1994 | Out << "T__m" << Width; |
| 1995 | else if (ET->getKind() == BuiltinType::LongLong) |
| 1996 | Out << "T__m" << Width << 'i'; |
| 1997 | else if (ET->getKind() == BuiltinType::Double) |
| 1998 | Out << "U__m" << Width << 'd'; |
| 1999 | else |
| 2000 | IntelVector = false; |
| 2001 | } else { |
| 2002 | IntelVector = false; |
| 2003 | } |
| 2004 | |
| 2005 | if (!IntelVector) { |
| 2006 | // The MS ABI doesn't have a special mangling for vector types, so we define |
| 2007 | // our own mangling to handle uses of __vector_size__ on user-specified |
| 2008 | // types, and for extensions like __v4sf. |
| 2009 | Out << "T__clang_vec" << T->getNumElements() << '_'; |
| 2010 | mangleType(ET, Range); |
| 2011 | } |
| 2012 | |
| 2013 | Out << "@@"; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2014 | } |
Reid Kleckner | e7e64d8 | 2013-03-26 16:56:59 +0000 | [diff] [blame] | 2015 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2016 | void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T, |
| 2017 | SourceRange Range) { |
| 2018 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2019 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2020 | "cannot mangle this extended vector type yet"); |
| 2021 | Diags.Report(Range.getBegin(), DiagID) |
| 2022 | << Range; |
| 2023 | } |
| 2024 | void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T, |
| 2025 | SourceRange Range) { |
| 2026 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2027 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2028 | "cannot mangle this dependent-sized extended vector type yet"); |
| 2029 | Diags.Report(Range.getBegin(), DiagID) |
| 2030 | << Range; |
| 2031 | } |
| 2032 | |
| 2033 | void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, |
| 2034 | SourceRange) { |
| 2035 | // ObjC interfaces have structs underlying them. |
| 2036 | Out << 'U'; |
| 2037 | mangleName(T->getDecl()); |
| 2038 | } |
| 2039 | |
| 2040 | void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T, |
| 2041 | SourceRange Range) { |
| 2042 | // We don't allow overloading by different protocol qualification, |
| 2043 | // so mangling them isn't necessary. |
| 2044 | mangleType(T->getBaseType(), Range); |
| 2045 | } |
| 2046 | |
| 2047 | void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T, |
| 2048 | SourceRange Range) { |
| 2049 | Out << "_E"; |
| 2050 | |
| 2051 | QualType pointee = T->getPointeeType(); |
Timur Iskhodzhanov | 555a772 | 2013-10-04 11:25:05 +0000 | [diff] [blame] | 2052 | mangleFunctionType(pointee->castAs<FunctionProtoType>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 2055 | void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *, |
| 2056 | SourceRange) { |
| 2057 | llvm_unreachable("Cannot mangle injected class name type."); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T, |
| 2061 | SourceRange Range) { |
| 2062 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2063 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2064 | "cannot mangle this template specialization type yet"); |
| 2065 | Diags.Report(Range.getBegin(), DiagID) |
| 2066 | << Range; |
| 2067 | } |
| 2068 | |
| 2069 | void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, |
| 2070 | SourceRange Range) { |
| 2071 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2072 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2073 | "cannot mangle this dependent name type yet"); |
| 2074 | Diags.Report(Range.getBegin(), DiagID) |
| 2075 | << Range; |
| 2076 | } |
| 2077 | |
| 2078 | void MicrosoftCXXNameMangler::mangleType( |
| 2079 | const DependentTemplateSpecializationType *T, |
| 2080 | SourceRange Range) { |
| 2081 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2082 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2083 | "cannot mangle this dependent template specialization type yet"); |
| 2084 | Diags.Report(Range.getBegin(), DiagID) |
| 2085 | << Range; |
| 2086 | } |
| 2087 | |
| 2088 | void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, |
| 2089 | SourceRange Range) { |
| 2090 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2091 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2092 | "cannot mangle this pack expansion yet"); |
| 2093 | Diags.Report(Range.getBegin(), DiagID) |
| 2094 | << Range; |
| 2095 | } |
| 2096 | |
| 2097 | void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, |
| 2098 | SourceRange Range) { |
| 2099 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2100 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2101 | "cannot mangle this typeof(type) yet"); |
| 2102 | Diags.Report(Range.getBegin(), DiagID) |
| 2103 | << Range; |
| 2104 | } |
| 2105 | |
| 2106 | void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, |
| 2107 | SourceRange Range) { |
| 2108 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2109 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2110 | "cannot mangle this typeof(expression) yet"); |
| 2111 | Diags.Report(Range.getBegin(), DiagID) |
| 2112 | << Range; |
| 2113 | } |
| 2114 | |
| 2115 | void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, |
| 2116 | SourceRange Range) { |
| 2117 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2118 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2119 | "cannot mangle this decltype() yet"); |
| 2120 | Diags.Report(Range.getBegin(), DiagID) |
| 2121 | << Range; |
| 2122 | } |
| 2123 | |
| 2124 | void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T, |
| 2125 | SourceRange Range) { |
| 2126 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2127 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2128 | "cannot mangle this unary transform type yet"); |
| 2129 | Diags.Report(Range.getBegin(), DiagID) |
| 2130 | << Range; |
| 2131 | } |
| 2132 | |
| 2133 | void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) { |
David Majnemer | b9a5f2d | 2014-01-21 20:33:36 +0000 | [diff] [blame] | 2134 | assert(T->getDeducedType().isNull() && "expecting a dependent type!"); |
| 2135 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2136 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2137 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2138 | "cannot mangle this 'auto' type yet"); |
| 2139 | Diags.Report(Range.getBegin(), DiagID) |
| 2140 | << Range; |
| 2141 | } |
| 2142 | |
| 2143 | void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, |
| 2144 | SourceRange Range) { |
| 2145 | DiagnosticsEngine &Diags = Context.getDiags(); |
| 2146 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2147 | "cannot mangle this C11 atomic type yet"); |
| 2148 | Diags.Report(Range.getBegin(), DiagID) |
| 2149 | << Range; |
| 2150 | } |
| 2151 | |
Rafael Espindola | 002667c | 2013-10-16 01:40:34 +0000 | [diff] [blame] | 2152 | void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D, |
| 2153 | raw_ostream &Out) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2154 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 2155 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 2156 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 2157 | "Invalid mangleName() call on 'structor decl!"); |
| 2158 | |
| 2159 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 2160 | getASTContext().getSourceManager(), |
| 2161 | "Mangling declaration"); |
| 2162 | |
| 2163 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2164 | return Mangler.mangle(D); |
| 2165 | } |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2166 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2167 | // <this-adjustment> ::= <no-adjustment> | <static-adjustment> | |
| 2168 | // <virtual-adjustment> |
| 2169 | // <no-adjustment> ::= A # private near |
| 2170 | // ::= B # private far |
| 2171 | // ::= I # protected near |
| 2172 | // ::= J # protected far |
| 2173 | // ::= Q # public near |
| 2174 | // ::= R # public far |
| 2175 | // <static-adjustment> ::= G <static-offset> # private near |
| 2176 | // ::= H <static-offset> # private far |
| 2177 | // ::= O <static-offset> # protected near |
| 2178 | // ::= P <static-offset> # protected far |
| 2179 | // ::= W <static-offset> # public near |
| 2180 | // ::= X <static-offset> # public far |
| 2181 | // <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near |
| 2182 | // ::= $1 <virtual-shift> <static-offset> # private far |
| 2183 | // ::= $2 <virtual-shift> <static-offset> # protected near |
| 2184 | // ::= $3 <virtual-shift> <static-offset> # protected far |
| 2185 | // ::= $4 <virtual-shift> <static-offset> # public near |
| 2186 | // ::= $5 <virtual-shift> <static-offset> # public far |
| 2187 | // <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift> |
| 2188 | // <vtordisp-shift> ::= <offset-to-vtordisp> |
| 2189 | // <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset> |
| 2190 | // <offset-to-vtordisp> |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2191 | static void mangleThunkThisAdjustment(const CXXMethodDecl *MD, |
| 2192 | const ThisAdjustment &Adjustment, |
| 2193 | MicrosoftCXXNameMangler &Mangler, |
| 2194 | raw_ostream &Out) { |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2195 | if (!Adjustment.Virtual.isEmpty()) { |
| 2196 | Out << '$'; |
| 2197 | char AccessSpec; |
| 2198 | switch (MD->getAccess()) { |
| 2199 | case AS_none: |
| 2200 | llvm_unreachable("Unsupported access specifier"); |
| 2201 | case AS_private: |
| 2202 | AccessSpec = '0'; |
| 2203 | break; |
| 2204 | case AS_protected: |
| 2205 | AccessSpec = '2'; |
| 2206 | break; |
| 2207 | case AS_public: |
| 2208 | AccessSpec = '4'; |
| 2209 | } |
| 2210 | if (Adjustment.Virtual.Microsoft.VBPtrOffset) { |
| 2211 | Out << 'R' << AccessSpec; |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 2212 | Mangler.mangleNumber( |
| 2213 | static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset)); |
| 2214 | Mangler.mangleNumber( |
| 2215 | static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset)); |
| 2216 | Mangler.mangleNumber( |
| 2217 | static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); |
| 2218 | Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual)); |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2219 | } else { |
| 2220 | Out << AccessSpec; |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 2221 | Mangler.mangleNumber( |
| 2222 | static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); |
| 2223 | Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual)); |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2224 | } |
| 2225 | } else if (Adjustment.NonVirtual != 0) { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2226 | switch (MD->getAccess()) { |
| 2227 | case AS_none: |
| 2228 | llvm_unreachable("Unsupported access specifier"); |
| 2229 | case AS_private: |
| 2230 | Out << 'G'; |
| 2231 | break; |
| 2232 | case AS_protected: |
| 2233 | Out << 'O'; |
| 2234 | break; |
| 2235 | case AS_public: |
| 2236 | Out << 'W'; |
| 2237 | } |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 2238 | Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual)); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2239 | } else { |
| 2240 | switch (MD->getAccess()) { |
| 2241 | case AS_none: |
| 2242 | llvm_unreachable("Unsupported access specifier"); |
| 2243 | case AS_private: |
| 2244 | Out << 'A'; |
| 2245 | break; |
| 2246 | case AS_protected: |
| 2247 | Out << 'I'; |
| 2248 | break; |
| 2249 | case AS_public: |
| 2250 | Out << 'Q'; |
| 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2255 | void |
| 2256 | MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, |
| 2257 | raw_ostream &Out) { |
| 2258 | MicrosoftVTableContext *VTContext = |
| 2259 | cast<MicrosoftVTableContext>(getASTContext().getVTableContext()); |
| 2260 | const MicrosoftVTableContext::MethodVFTableLocation &ML = |
| 2261 | VTContext->getMethodVFTableLocation(GlobalDecl(MD)); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2262 | |
| 2263 | MicrosoftCXXNameMangler Mangler(*this, Out); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2264 | Mangler.getStream() << "\01?"; |
| 2265 | Mangler.mangleVirtualMemPtrThunk(MD, ML); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2268 | void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD, |
| 2269 | const ThunkInfo &Thunk, |
| 2270 | raw_ostream &Out) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2271 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2272 | Out << "\01?"; |
| 2273 | Mangler.mangleName(MD); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2274 | mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out); |
| 2275 | if (!Thunk.Return.isEmpty()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2276 | assert(Thunk.Method != nullptr && |
| 2277 | "Thunk info should hold the overridee decl"); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2278 | |
| 2279 | const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD; |
| 2280 | Mangler.mangleFunctionType( |
| 2281 | DeclForFPT->getType()->castAs<FunctionProtoType>(), MD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2282 | } |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2283 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2284 | void MicrosoftMangleContextImpl::mangleCXXDtorThunk( |
| 2285 | const CXXDestructorDecl *DD, CXXDtorType Type, |
| 2286 | const ThisAdjustment &Adjustment, raw_ostream &Out) { |
| 2287 | // FIXME: Actually, the dtor thunk should be emitted for vector deleting |
| 2288 | // dtors rather than scalar deleting dtors. Just use the vector deleting dtor |
| 2289 | // mangling manually until we support both deleting dtor types. |
| 2290 | assert(Type == Dtor_Deleting); |
| 2291 | MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type); |
| 2292 | Out << "\01??_E"; |
| 2293 | Mangler.mangleName(DD->getParent()); |
| 2294 | mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out); |
| 2295 | Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2296 | } |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 2297 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2298 | void MicrosoftMangleContextImpl::mangleCXXVFTable( |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 2299 | const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, |
| 2300 | raw_ostream &Out) { |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 2301 | // <mangled-name> ::= ?_7 <class-name> <storage-class> |
| 2302 | // <cvr-qualifiers> [<name>] @ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2303 | // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 2304 | // is always '6' for vftables. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2305 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2306 | Mangler.getStream() << "\01??_7"; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 2307 | Mangler.mangleName(Derived); |
| 2308 | Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const. |
David Majnemer | b926dbc | 2014-04-23 05:16:53 +0000 | [diff] [blame] | 2309 | for (const CXXRecordDecl *RD : BasePath) |
| 2310 | Mangler.mangleName(RD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2311 | Mangler.getStream() << '@'; |
| 2312 | } |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 2313 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2314 | void MicrosoftMangleContextImpl::mangleCXXVBTable( |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 2315 | const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, |
| 2316 | raw_ostream &Out) { |
| 2317 | // <mangled-name> ::= ?_8 <class-name> <storage-class> |
| 2318 | // <cvr-qualifiers> [<name>] @ |
| 2319 | // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> |
| 2320 | // is always '7' for vbtables. |
| 2321 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2322 | Mangler.getStream() << "\01??_8"; |
| 2323 | Mangler.mangleName(Derived); |
| 2324 | Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const. |
David Majnemer | b926dbc | 2014-04-23 05:16:53 +0000 | [diff] [blame] | 2325 | for (const CXXRecordDecl *RD : BasePath) |
| 2326 | Mangler.mangleName(RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 2327 | Mangler.getStream() << '@'; |
| 2328 | } |
| 2329 | |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2330 | void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &Out) { |
| 2331 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2332 | Mangler.getStream() << "\01??_R0"; |
| 2333 | Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result); |
| 2334 | Mangler.getStream() << "@8"; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2335 | } |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2336 | |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2337 | void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, |
| 2338 | raw_ostream &Out) { |
| 2339 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2340 | Mangler.getStream() << '.'; |
| 2341 | Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result); |
| 2342 | } |
| 2343 | |
David Majnemer | 5f0dd61 | 2015-03-17 20:35:05 +0000 | [diff] [blame^] | 2344 | void MicrosoftMangleContextImpl::mangleCXXHandlerMapEntry(QualType T, |
| 2345 | bool IsConst, |
| 2346 | bool IsVolatile, |
| 2347 | bool IsReference, |
| 2348 | raw_ostream &Out) { |
| 2349 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2350 | Mangler.getStream() << "llvm.eh.handlermapentry."; |
| 2351 | if (IsConst) |
| 2352 | Mangler.getStream() << "const."; |
| 2353 | if (IsVolatile) |
| 2354 | Mangler.getStream() << "volatile."; |
| 2355 | if (IsReference) |
| 2356 | Mangler.getStream() << "reference."; |
| 2357 | Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result); |
| 2358 | } |
| 2359 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 2360 | void MicrosoftMangleContextImpl::mangleCXXThrowInfo(QualType T, |
| 2361 | bool IsConst, |
| 2362 | bool IsVolatile, |
| 2363 | uint32_t NumEntries, |
| 2364 | raw_ostream &Out) { |
| 2365 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2366 | Mangler.getStream() << "_TI"; |
| 2367 | if (IsConst) |
| 2368 | Mangler.getStream() << 'C'; |
| 2369 | if (IsVolatile) |
| 2370 | Mangler.getStream() << 'V'; |
| 2371 | Mangler.getStream() << NumEntries; |
| 2372 | Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result); |
| 2373 | } |
| 2374 | |
| 2375 | void MicrosoftMangleContextImpl::mangleCXXCatchableTypeArray( |
| 2376 | QualType T, uint32_t NumEntries, raw_ostream &Out) { |
| 2377 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2378 | Mangler.getStream() << "_CTA"; |
| 2379 | Mangler.getStream() << NumEntries; |
| 2380 | Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result); |
| 2381 | } |
| 2382 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 2383 | void MicrosoftMangleContextImpl::mangleCXXCatchableType( |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 2384 | QualType T, const CXXConstructorDecl *CD, CXXCtorType CT, uint32_t Size, |
| 2385 | uint32_t NVOffset, int32_t VBPtrOffset, uint32_t VBIndex, |
| 2386 | raw_ostream &Out) { |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 2387 | MicrosoftCXXNameMangler Mangler(*this, Out); |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 2388 | Mangler.getStream() << "_CT"; |
| 2389 | |
| 2390 | llvm::SmallString<64> RTTIMangling; |
| 2391 | { |
| 2392 | llvm::raw_svector_ostream Stream(RTTIMangling); |
| 2393 | mangleCXXRTTI(T, Stream); |
| 2394 | } |
| 2395 | Mangler.getStream() << RTTIMangling.substr(1); |
| 2396 | |
David Majnemer | 999cbf9 | 2015-03-10 19:01:51 +0000 | [diff] [blame] | 2397 | // VS2015 CTP6 omits the copy-constructor in the mangled name. This name is, |
| 2398 | // in fact, superfluous but I'm not sure the change was made consciously. |
| 2399 | // TODO: Revisit this when VS2015 gets released. |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 2400 | llvm::SmallString<64> CopyCtorMangling; |
| 2401 | if (CD) { |
| 2402 | llvm::raw_svector_ostream Stream(CopyCtorMangling); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 2403 | mangleCXXCtor(CD, CT, Stream); |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 2404 | } |
| 2405 | Mangler.getStream() << CopyCtorMangling.substr(1); |
| 2406 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 2407 | Mangler.getStream() << Size; |
David Majnemer | 999cbf9 | 2015-03-10 19:01:51 +0000 | [diff] [blame] | 2408 | if (VBPtrOffset == -1) { |
| 2409 | if (NVOffset) { |
| 2410 | Mangler.getStream() << NVOffset; |
| 2411 | } |
| 2412 | } else { |
| 2413 | Mangler.getStream() << NVOffset; |
| 2414 | Mangler.getStream() << VBPtrOffset; |
| 2415 | Mangler.getStream() << VBIndex; |
| 2416 | } |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2419 | void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassDescriptor( |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2420 | const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset, |
David Majnemer | aeb55c9 | 2014-05-13 06:57:43 +0000 | [diff] [blame] | 2421 | uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) { |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2422 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2423 | Mangler.getStream() << "\01??_R1"; |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2424 | Mangler.mangleNumber(NVOffset); |
| 2425 | Mangler.mangleNumber(VBPtrOffset); |
| 2426 | Mangler.mangleNumber(VBTableOffset); |
| 2427 | Mangler.mangleNumber(Flags); |
David Majnemer | aeb55c9 | 2014-05-13 06:57:43 +0000 | [diff] [blame] | 2428 | Mangler.mangleName(Derived); |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2429 | Mangler.getStream() << "8"; |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassArray( |
| 2433 | const CXXRecordDecl *Derived, raw_ostream &Out) { |
| 2434 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2435 | Mangler.getStream() << "\01??_R2"; |
| 2436 | Mangler.mangleName(Derived); |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2437 | Mangler.getStream() << "8"; |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | void MicrosoftMangleContextImpl::mangleCXXRTTIClassHierarchyDescriptor( |
| 2441 | const CXXRecordDecl *Derived, raw_ostream &Out) { |
| 2442 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2443 | Mangler.getStream() << "\01??_R3"; |
| 2444 | Mangler.mangleName(Derived); |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2445 | Mangler.getStream() << "8"; |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | void MicrosoftMangleContextImpl::mangleCXXRTTICompleteObjectLocator( |
David Majnemer | aeb55c9 | 2014-05-13 06:57:43 +0000 | [diff] [blame] | 2449 | const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, |
| 2450 | raw_ostream &Out) { |
| 2451 | // <mangled-name> ::= ?_R4 <class-name> <storage-class> |
| 2452 | // <cvr-qualifiers> [<name>] @ |
| 2453 | // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> |
| 2454 | // is always '6' for vftables. |
David Majnemer | a96b7ee | 2014-05-13 00:44:44 +0000 | [diff] [blame] | 2455 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2456 | Mangler.getStream() << "\01??_R4"; |
| 2457 | Mangler.mangleName(Derived); |
David Majnemer | aeb55c9 | 2014-05-13 06:57:43 +0000 | [diff] [blame] | 2458 | Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const. |
| 2459 | for (const CXXRecordDecl *RD : BasePath) |
| 2460 | Mangler.mangleName(RD); |
| 2461 | Mangler.getStream() << '@'; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2462 | } |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2463 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 2464 | void MicrosoftMangleContextImpl::mangleSEHFilterExpression( |
| 2465 | const NamedDecl *EnclosingDecl, raw_ostream &Out) { |
| 2466 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2467 | // The function body is in the same comdat as the function with the handler, |
| 2468 | // so the numbering here doesn't have to be the same across TUs. |
| 2469 | // |
| 2470 | // <mangled-name> ::= ?filt$ <filter-number> @0 |
| 2471 | Mangler.getStream() << "\01?filt$" << SEHFilterIds[EnclosingDecl]++ << "@0@"; |
| 2472 | Mangler.mangleName(EnclosingDecl); |
| 2473 | } |
| 2474 | |
Reid Kleckner | cc99e26 | 2013-11-19 23:23:00 +0000 | [diff] [blame] | 2475 | void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) { |
| 2476 | // This is just a made up unique string for the purposes of tbaa. undname |
| 2477 | // does *not* know how to demangle it. |
| 2478 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2479 | Mangler.getStream() << '?'; |
| 2480 | Mangler.mangleType(T, SourceRange()); |
| 2481 | } |
| 2482 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2483 | void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D, |
| 2484 | CXXCtorType Type, |
| 2485 | raw_ostream &Out) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 2486 | MicrosoftCXXNameMangler mangler(*this, Out, D, Type); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2487 | mangler.mangle(D); |
| 2488 | } |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2489 | |
| 2490 | void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D, |
| 2491 | CXXDtorType Type, |
| 2492 | raw_ostream &Out) { |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 2493 | MicrosoftCXXNameMangler mangler(*this, Out, D, Type); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2494 | mangler.mangle(D); |
| 2495 | } |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2496 | |
| 2497 | void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD, |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 2498 | unsigned, |
David Majnemer | 210e6bfa1 | 2013-12-13 00:39:38 +0000 | [diff] [blame] | 2499 | raw_ostream &) { |
| 2500 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
| 2501 | "cannot mangle this reference temporary yet"); |
| 2502 | getDiags().Report(VD->getLocation(), DiagID); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2505 | void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD, |
| 2506 | raw_ostream &Out) { |
David Majnemer | a00381f | 2014-10-05 06:44:55 +0000 | [diff] [blame] | 2507 | // TODO: This is not correct, especially with respect to VS "14". VS "14" |
David Majnemer | 25e1a5e | 2013-12-13 00:52:45 +0000 | [diff] [blame] | 2508 | // utilizes thread local variables to implement thread safe, re-entrant |
| 2509 | // initialization for statics. They no longer differentiate between an |
| 2510 | // externally visible and non-externally visible static with respect to |
| 2511 | // mangling, they all get $TSS <number>. |
| 2512 | // |
| 2513 | // N.B. This means that they can get more than 32 static variable guards in a |
| 2514 | // scope. It also means that they broke compatibility with their own ABI. |
| 2515 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 2516 | // <guard-name> ::= ?_B <postfix> @5 <scope-depth> |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2517 | // ::= ?$S <guard-num> @ <postfix> @4IA |
| 2518 | |
| 2519 | // The first mangling is what MSVC uses to guard static locals in inline |
| 2520 | // functions. It uses a different mangling in external functions to support |
| 2521 | // guarding more than 32 variables. MSVC rejects inline functions with more |
| 2522 | // than 32 static locals. We don't fully implement the second mangling |
| 2523 | // because those guards are not externally visible, and instead use LLVM's |
| 2524 | // default renaming when creating a new guard variable. |
| 2525 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2526 | |
| 2527 | bool Visible = VD->isExternallyVisible(); |
| 2528 | // <operator-name> ::= ?_B # local static guard |
| 2529 | Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@"); |
David Majnemer | 34b4989 | 2014-03-06 19:57:36 +0000 | [diff] [blame] | 2530 | unsigned ScopeDepth = 0; |
| 2531 | if (Visible && !getNextDiscriminator(VD, ScopeDepth)) |
| 2532 | // If we do not have a discriminator and are emitting a guard variable for |
| 2533 | // use at global scope, then mangling the nested name will not be enough to |
| 2534 | // remove ambiguities. |
| 2535 | Mangler.mangle(VD, ""); |
| 2536 | else |
| 2537 | Mangler.mangleNestedName(VD); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 2538 | Mangler.getStream() << (Visible ? "@5" : "@4IA"); |
David Majnemer | 34b4989 | 2014-03-06 19:57:36 +0000 | [diff] [blame] | 2539 | if (ScopeDepth) |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 2540 | Mangler.mangleNumber(ScopeDepth); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2543 | void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D, |
| 2544 | raw_ostream &Out, |
| 2545 | char CharCode) { |
Reid Kleckner | 1ece9fc | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 2546 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2547 | Mangler.getStream() << "\01??__" << CharCode; |
| 2548 | Mangler.mangleName(D); |
David Majnemer | f55feec | 2014-03-06 19:10:27 +0000 | [diff] [blame] | 2549 | if (D->isStaticDataMember()) { |
| 2550 | Mangler.mangleVariableEncoding(D); |
| 2551 | Mangler.getStream() << '@'; |
| 2552 | } |
Reid Kleckner | 1ece9fc | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 2553 | // This is the function class mangling. These stubs are global, non-variadic, |
| 2554 | // cdecl functions that return void and take no args. |
| 2555 | Mangler.getStream() << "YAXXZ"; |
| 2556 | } |
| 2557 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2558 | void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D, |
| 2559 | raw_ostream &Out) { |
Reid Kleckner | 1ece9fc | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 2560 | // <initializer-name> ::= ?__E <name> YAXXZ |
| 2561 | mangleInitFiniStub(D, Out, 'E'); |
| 2562 | } |
| 2563 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2564 | void |
| 2565 | MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D, |
| 2566 | raw_ostream &Out) { |
Reid Kleckner | 1ece9fc | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 2567 | // <destructor-name> ::= ?__F <name> YAXXZ |
| 2568 | mangleInitFiniStub(D, Out, 'F'); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2571 | void MicrosoftMangleContextImpl::mangleStringLiteral(const StringLiteral *SL, |
| 2572 | raw_ostream &Out) { |
| 2573 | // <char-type> ::= 0 # char |
| 2574 | // ::= 1 # wchar_t |
| 2575 | // ::= ??? # char16_t/char32_t will need a mangling too... |
| 2576 | // |
| 2577 | // <literal-length> ::= <non-negative integer> # the length of the literal |
| 2578 | // |
| 2579 | // <encoded-crc> ::= <hex digit>+ @ # crc of the literal including |
| 2580 | // # null-terminator |
| 2581 | // |
| 2582 | // <encoded-string> ::= <simple character> # uninteresting character |
| 2583 | // ::= '?$' <hex digit> <hex digit> # these two nibbles |
| 2584 | // # encode the byte for the |
| 2585 | // # character |
| 2586 | // ::= '?' [a-z] # \xe1 - \xfa |
| 2587 | // ::= '?' [A-Z] # \xc1 - \xda |
| 2588 | // ::= '?' [0-9] # [,/\:. \n\t'-] |
| 2589 | // |
| 2590 | // <literal> ::= '??_C@_' <char-type> <literal-length> <encoded-crc> |
| 2591 | // <encoded-string> '@' |
| 2592 | MicrosoftCXXNameMangler Mangler(*this, Out); |
| 2593 | Mangler.getStream() << "\01??_C@_"; |
| 2594 | |
| 2595 | // <char-type>: The "kind" of string literal is encoded into the mangled name. |
David Majnemer | 98e77a2 | 2014-11-21 19:57:25 +0000 | [diff] [blame] | 2596 | if (SL->isWide()) |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2597 | Mangler.getStream() << '1'; |
| 2598 | else |
David Majnemer | 98e77a2 | 2014-11-21 19:57:25 +0000 | [diff] [blame] | 2599 | Mangler.getStream() << '0'; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2600 | |
| 2601 | // <literal-length>: The next part of the mangled name consists of the length |
| 2602 | // of the string. |
| 2603 | // The StringLiteral does not consider the NUL terminator byte(s) but the |
| 2604 | // mangling does. |
| 2605 | // N.B. The length is in terms of bytes, not characters. |
| 2606 | Mangler.mangleNumber(SL->getByteLength() + SL->getCharByteWidth()); |
| 2607 | |
| 2608 | // We will use the "Rocksoft^tm Model CRC Algorithm" to describe the |
| 2609 | // properties of our CRC: |
| 2610 | // Width : 32 |
| 2611 | // Poly : 04C11DB7 |
| 2612 | // Init : FFFFFFFF |
| 2613 | // RefIn : True |
| 2614 | // RefOut : True |
| 2615 | // XorOut : 00000000 |
| 2616 | // Check : 340BC6D9 |
| 2617 | uint32_t CRC = 0xFFFFFFFFU; |
| 2618 | |
| 2619 | auto UpdateCRC = [&CRC](char Byte) { |
| 2620 | for (unsigned i = 0; i < 8; ++i) { |
| 2621 | bool Bit = CRC & 0x80000000U; |
| 2622 | if (Byte & (1U << i)) |
| 2623 | Bit = !Bit; |
| 2624 | CRC <<= 1; |
| 2625 | if (Bit) |
| 2626 | CRC ^= 0x04C11DB7U; |
| 2627 | } |
| 2628 | }; |
| 2629 | |
David Majnemer | 4b293eb | 2014-03-31 17:18:53 +0000 | [diff] [blame] | 2630 | auto GetLittleEndianByte = [&Mangler, &SL](unsigned Index) { |
| 2631 | unsigned CharByteWidth = SL->getCharByteWidth(); |
| 2632 | uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth); |
David Majnemer | e49c4b6 | 2014-03-31 21:46:05 +0000 | [diff] [blame] | 2633 | unsigned OffsetInCodeUnit = Index % CharByteWidth; |
| 2634 | return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff); |
David Majnemer | 4b293eb | 2014-03-31 17:18:53 +0000 | [diff] [blame] | 2635 | }; |
| 2636 | |
| 2637 | auto GetBigEndianByte = [&Mangler, &SL](unsigned Index) { |
| 2638 | unsigned CharByteWidth = SL->getCharByteWidth(); |
| 2639 | uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth); |
David Majnemer | e49c4b6 | 2014-03-31 21:46:05 +0000 | [diff] [blame] | 2640 | unsigned OffsetInCodeUnit = (CharByteWidth - 1) - (Index % CharByteWidth); |
| 2641 | return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff); |
David Majnemer | 4b293eb | 2014-03-31 17:18:53 +0000 | [diff] [blame] | 2642 | }; |
| 2643 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2644 | // CRC all the bytes of the StringLiteral. |
David Majnemer | 4b293eb | 2014-03-31 17:18:53 +0000 | [diff] [blame] | 2645 | for (unsigned I = 0, E = SL->getByteLength(); I != E; ++I) |
| 2646 | UpdateCRC(GetLittleEndianByte(I)); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2647 | |
| 2648 | // The NUL terminator byte(s) were not present earlier, |
| 2649 | // we need to manually process those bytes into the CRC. |
| 2650 | for (unsigned NullTerminator = 0; NullTerminator < SL->getCharByteWidth(); |
| 2651 | ++NullTerminator) |
| 2652 | UpdateCRC('\x00'); |
| 2653 | |
| 2654 | // The literature refers to the process of reversing the bits in the final CRC |
| 2655 | // output as "reflection". |
| 2656 | CRC = llvm::reverseBits(CRC); |
| 2657 | |
| 2658 | // <encoded-crc>: The CRC is encoded utilizing the standard number mangling |
| 2659 | // scheme. |
| 2660 | Mangler.mangleNumber(CRC); |
| 2661 | |
David Majnemer | 981c306 | 2014-03-30 16:38:02 +0000 | [diff] [blame] | 2662 | // <encoded-string>: The mangled name also contains the first 32 _characters_ |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2663 | // (including null-terminator bytes) of the StringLiteral. |
| 2664 | // Each character is encoded by splitting them into bytes and then encoding |
| 2665 | // the constituent bytes. |
| 2666 | auto MangleByte = [&Mangler](char Byte) { |
| 2667 | // There are five different manglings for characters: |
| 2668 | // - [a-zA-Z0-9_$]: A one-to-one mapping. |
| 2669 | // - ?[a-z]: The range from \xe1 to \xfa. |
| 2670 | // - ?[A-Z]: The range from \xc1 to \xda. |
| 2671 | // - ?[0-9]: The set of [,/\:. \n\t'-]. |
| 2672 | // - ?$XX: A fallback which maps nibbles. |
David Majnemer | 10befcf | 2014-04-01 00:05:57 +0000 | [diff] [blame] | 2673 | if (isIdentifierBody(Byte, /*AllowDollar=*/true)) { |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2674 | Mangler.getStream() << Byte; |
David Majnemer | 10befcf | 2014-04-01 00:05:57 +0000 | [diff] [blame] | 2675 | } else if (isLetter(Byte & 0x7f)) { |
| 2676 | Mangler.getStream() << '?' << static_cast<char>(Byte & 0x7f); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2677 | } else { |
David Majnemer | 07bc611 | 2014-12-22 06:24:49 +0000 | [diff] [blame] | 2678 | const char SpecialChars[] = {',', '/', '\\', ':', '.', |
| 2679 | ' ', '\n', '\t', '\'', '-'}; |
| 2680 | const char *Pos = |
| 2681 | std::find(std::begin(SpecialChars), std::end(SpecialChars), Byte); |
| 2682 | if (Pos != std::end(SpecialChars)) { |
| 2683 | Mangler.getStream() << '?' << (Pos - std::begin(SpecialChars)); |
| 2684 | } else { |
| 2685 | Mangler.getStream() << "?$"; |
| 2686 | Mangler.getStream() << static_cast<char>('A' + ((Byte >> 4) & 0xf)); |
| 2687 | Mangler.getStream() << static_cast<char>('A' + (Byte & 0xf)); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2688 | } |
| 2689 | } |
| 2690 | }; |
| 2691 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2692 | // Enforce our 32 character max. |
| 2693 | unsigned NumCharsToMangle = std::min(32U, SL->getLength()); |
David Majnemer | 4b293eb | 2014-03-31 17:18:53 +0000 | [diff] [blame] | 2694 | for (unsigned I = 0, E = NumCharsToMangle * SL->getCharByteWidth(); I != E; |
| 2695 | ++I) |
David Majnemer | 98e77a2 | 2014-11-21 19:57:25 +0000 | [diff] [blame] | 2696 | if (SL->isWide()) |
| 2697 | MangleByte(GetBigEndianByte(I)); |
| 2698 | else |
| 2699 | MangleByte(GetLittleEndianByte(I)); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2700 | |
| 2701 | // Encode the NUL terminator if there is room. |
| 2702 | if (NumCharsToMangle < 32) |
David Majnemer | 4b293eb | 2014-03-31 17:18:53 +0000 | [diff] [blame] | 2703 | for (unsigned NullTerminator = 0; NullTerminator < SL->getCharByteWidth(); |
| 2704 | ++NullTerminator) |
| 2705 | MangleByte(0); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2706 | |
| 2707 | Mangler.getStream() << '@'; |
| 2708 | } |
| 2709 | |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 2710 | void MicrosoftMangleContextImpl::mangleCXXVTableBitSet(const CXXRecordDecl *RD, |
| 2711 | raw_ostream &Out) { |
| 2712 | llvm::report_fatal_error("Cannot mangle bitsets yet"); |
| 2713 | } |
| 2714 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 2715 | MicrosoftMangleContext * |
| 2716 | MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) { |
| 2717 | return new MicrosoftMangleContextImpl(Context, Diags); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2718 | } |