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