Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 1 | //===--- Mangle.h - Mangle C++ Names ----------------------------*- C++ -*-===// |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 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 | // Implements C++ name mangling according to the Itanium C++ ABI, |
| 11 | // which is used in GCC 3.2 and newer (and many compilers that are |
| 12 | // ABI-compatible with GCC): |
| 13 | // |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 17 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 18 | #ifndef LLVM_CLANG_CODEGEN_MANGLE_H |
| 19 | #define LLVM_CLANG_CODEGEN_MANGLE_H |
| 20 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 21 | #include "CGCXX.h" |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 22 | #include "GlobalDecl.h" |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 23 | #include "clang/AST/Type.h" |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringRef.h" |
| 26 | #include "llvm/ADT/SmallString.h" |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 28 | |
| 29 | namespace clang { |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 30 | class ASTContext; |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 31 | class BlockDecl; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 32 | class CXXConstructorDecl; |
| 33 | class CXXDestructorDecl; |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 34 | class CXXMethodDecl; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 35 | class FunctionDecl; |
| 36 | class NamedDecl; |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 37 | class ObjCMethodDecl; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 38 | class VarDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 40 | namespace CodeGen { |
Douglas Gregor | 9ff659b | 2010-03-24 16:59:08 +0000 | [diff] [blame] | 41 | struct ThisAdjustment; |
| 42 | struct ThunkInfo; |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 43 | |
| 44 | /// MangleBuffer - a convenient class for storing a name which is |
| 45 | /// either the result of a mangling or is a constant string with |
| 46 | /// external memory ownership. |
| 47 | class MangleBuffer { |
| 48 | public: |
| 49 | void setString(llvm::StringRef Ref) { |
| 50 | String = Ref; |
| 51 | } |
| 52 | |
| 53 | llvm::SmallVectorImpl<char> &getBuffer() { |
| 54 | return Buffer; |
| 55 | } |
| 56 | |
| 57 | llvm::StringRef getString() const { |
| 58 | if (!String.empty()) return String; |
| 59 | return Buffer.str(); |
| 60 | } |
| 61 | |
| 62 | operator llvm::StringRef() const { |
| 63 | return getString(); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | llvm::StringRef String; |
| 68 | llvm::SmallString<256> Buffer; |
| 69 | }; |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 71 | /// MangleContext - Context for tracking state which persists across multiple |
| 72 | /// calls to the C++ name mangler. |
| 73 | class MangleContext { |
| 74 | ASTContext &Context; |
John McCall | 6ae1f35 | 2010-04-09 22:26:14 +0000 | [diff] [blame] | 75 | Diagnostic &Diags; |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 76 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 77 | llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds; |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 78 | unsigned Discriminator; |
| 79 | llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier; |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 80 | llvm::DenseMap<const BlockDecl*, unsigned> GlobalBlockIds; |
| 81 | llvm::DenseMap<const BlockDecl*, unsigned> LocalBlockIds; |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 83 | public: |
John McCall | 6ae1f35 | 2010-04-09 22:26:14 +0000 | [diff] [blame] | 84 | explicit MangleContext(ASTContext &Context, |
| 85 | Diagnostic &Diags) |
| 86 | : Context(Context), Diags(Diags) { } |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 87 | |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 88 | virtual ~MangleContext() { } |
| 89 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 90 | ASTContext &getASTContext() const { return Context; } |
| 91 | |
John McCall | 6ae1f35 | 2010-04-09 22:26:14 +0000 | [diff] [blame] | 92 | Diagnostic &getDiags() const { return Diags; } |
| 93 | |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 94 | void startNewFunction() { LocalBlockIds.clear(); } |
| 95 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 96 | uint64_t getAnonymousStructId(const TagDecl *TD) { |
| 97 | std::pair<llvm::DenseMap<const TagDecl *, |
| 98 | uint64_t>::iterator, bool> Result = |
| 99 | AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size())); |
| 100 | return Result.first->second; |
| 101 | } |
| 102 | |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 103 | unsigned getBlockId(const BlockDecl *BD, bool Local) { |
| 104 | llvm::DenseMap<const BlockDecl *, unsigned> &BlockIds |
| 105 | = Local? LocalBlockIds : GlobalBlockIds; |
| 106 | std::pair<llvm::DenseMap<const BlockDecl *, unsigned>::iterator, bool> |
| 107 | Result = BlockIds.insert(std::make_pair(BD, BlockIds.size())); |
| 108 | return Result.first->second; |
| 109 | } |
| 110 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 111 | /// @name Mangler Entry Points |
| 112 | /// @{ |
| 113 | |
Charles Davis | 971154d | 2010-06-11 04:25:47 +0000 | [diff] [blame] | 114 | virtual bool shouldMangleDeclName(const NamedDecl *D); |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 115 | virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); |
| 116 | virtual void mangleThunk(const CXXMethodDecl *MD, |
| 117 | const ThunkInfo &Thunk, |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 118 | llvm::SmallVectorImpl<char> &); |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 119 | virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 120 | const ThisAdjustment &ThisAdjustment, |
| 121 | llvm::SmallVectorImpl<char> &); |
Anders Carlsson | 715edf2 | 2010-06-26 16:09:40 +0000 | [diff] [blame] | 122 | virtual void mangleReferenceTemporary(const VarDecl *D, |
| 123 | llvm::SmallVectorImpl<char> &); |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 124 | virtual void mangleCXXVTable(const CXXRecordDecl *RD, |
| 125 | llvm::SmallVectorImpl<char> &); |
| 126 | virtual void mangleCXXVTT(const CXXRecordDecl *RD, |
| 127 | llvm::SmallVectorImpl<char> &); |
| 128 | virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, |
| 129 | const CXXRecordDecl *Type, |
| 130 | llvm::SmallVectorImpl<char> &); |
| 131 | virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); |
| 132 | virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); |
| 133 | virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
| 134 | llvm::SmallVectorImpl<char> &); |
| 135 | virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
| 136 | llvm::SmallVectorImpl<char> &); |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 137 | void mangleBlock(GlobalDecl GD, |
| 138 | const BlockDecl *BD, llvm::SmallVectorImpl<char> &); |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 139 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 140 | // This is pretty lame. |
| 141 | void mangleItaniumGuardVariable(const VarDecl *D, |
| 142 | llvm::SmallVectorImpl<char> &); |
| 143 | |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 144 | void mangleInitDiscriminator() { |
| 145 | Discriminator = 0; |
| 146 | } |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 147 | |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 148 | bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { |
| 149 | unsigned &discriminator = Uniquifier[ND]; |
| 150 | if (!discriminator) |
| 151 | discriminator = ++Discriminator; |
| 152 | if (discriminator == 1) |
| 153 | return false; |
| 154 | disc = discriminator-2; |
| 155 | return true; |
| 156 | } |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 157 | /// @} |
| 158 | }; |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 159 | |
| 160 | /// MiscNameMangler - Mangles Objective-C method names and blocks. |
| 161 | class MiscNameMangler { |
| 162 | MangleContext &Context; |
| 163 | llvm::raw_svector_ostream Out; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 164 | |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 165 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 166 | |
| 167 | public: |
| 168 | MiscNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res); |
| 169 | |
| 170 | llvm::raw_svector_ostream &getStream() { return Out; } |
| 171 | |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 172 | void mangleBlock(GlobalDecl GD, const BlockDecl *BD); |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 173 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
| 174 | }; |
| 175 | |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 176 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | #endif |