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" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 26 | |
| 27 | namespace clang { |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 28 | class ASTContext; |
| 29 | class CXXConstructorDecl; |
| 30 | class CXXDestructorDecl; |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 31 | class CXXMethodDecl; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 32 | class FunctionDecl; |
| 33 | class NamedDecl; |
| 34 | class VarDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 36 | namespace CodeGen { |
Douglas Gregor | 9ff659b | 2010-03-24 16:59:08 +0000 | [diff] [blame^] | 37 | struct ThisAdjustment; |
| 38 | struct ThunkInfo; |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 39 | class CovariantThunkAdjustment; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 40 | class ThunkAdjustment; |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 41 | |
| 42 | /// MangleBuffer - a convenient class for storing a name which is |
| 43 | /// either the result of a mangling or is a constant string with |
| 44 | /// external memory ownership. |
| 45 | class MangleBuffer { |
| 46 | public: |
| 47 | void setString(llvm::StringRef Ref) { |
| 48 | String = Ref; |
| 49 | } |
| 50 | |
| 51 | llvm::SmallVectorImpl<char> &getBuffer() { |
| 52 | return Buffer; |
| 53 | } |
| 54 | |
| 55 | llvm::StringRef getString() const { |
| 56 | if (!String.empty()) return String; |
| 57 | return Buffer.str(); |
| 58 | } |
| 59 | |
| 60 | operator llvm::StringRef() const { |
| 61 | return getString(); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | llvm::StringRef String; |
| 66 | llvm::SmallString<256> Buffer; |
| 67 | }; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 69 | /// MangleContext - Context for tracking state which persists across multiple |
| 70 | /// calls to the C++ name mangler. |
| 71 | class MangleContext { |
| 72 | ASTContext &Context; |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 73 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 74 | llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds; |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 75 | unsigned Discriminator; |
| 76 | llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier; |
| 77 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 78 | public: |
| 79 | explicit MangleContext(ASTContext &Context) |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 80 | : Context(Context) { } |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 81 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 82 | ASTContext &getASTContext() const { return Context; } |
| 83 | |
| 84 | uint64_t getAnonymousStructId(const TagDecl *TD) { |
| 85 | std::pair<llvm::DenseMap<const TagDecl *, |
| 86 | uint64_t>::iterator, bool> Result = |
| 87 | AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size())); |
| 88 | return Result.first->second; |
| 89 | } |
| 90 | |
| 91 | /// @name Mangler Entry Points |
| 92 | /// @{ |
| 93 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 94 | bool shouldMangleDeclName(const NamedDecl *D); |
| 95 | |
| 96 | void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 97 | void mangleThunk(const FunctionDecl *FD, |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 98 | const ThunkAdjustment &ThisAdjustment, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 99 | llvm::SmallVectorImpl<char> &); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 100 | void mangleThunk(const CXXMethodDecl *MD, |
| 101 | const ThunkInfo &Thunk, |
| 102 | llvm::SmallVectorImpl<char> &); |
Eli Friedman | 61d89b6 | 2009-12-03 00:03:05 +0000 | [diff] [blame] | 103 | void mangleCXXDtorThunk(const CXXDestructorDecl *D, CXXDtorType Type, |
| 104 | const ThunkAdjustment &ThisAdjustment, |
| 105 | llvm::SmallVectorImpl<char> &); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 106 | void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 107 | const ThisAdjustment &ThisAdjustment, |
| 108 | llvm::SmallVectorImpl<char> &); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 109 | void mangleCovariantThunk(const FunctionDecl *FD, |
| 110 | const CovariantThunkAdjustment& Adjustment, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 111 | llvm::SmallVectorImpl<char> &); |
| 112 | void mangleGuardVariable(const VarDecl *D, llvm::SmallVectorImpl<char> &); |
| 113 | void mangleCXXVtable(const CXXRecordDecl *RD, llvm::SmallVectorImpl<char> &); |
| 114 | void mangleCXXVTT(const CXXRecordDecl *RD, llvm::SmallVectorImpl<char> &); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 115 | void mangleCXXCtorVtable(const CXXRecordDecl *RD, int64_t Offset, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 116 | const CXXRecordDecl *Type, |
| 117 | llvm::SmallVectorImpl<char> &); |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 118 | void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); |
| 119 | void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 120 | void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 121 | llvm::SmallVectorImpl<char> &); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 122 | void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 123 | llvm::SmallVectorImpl<char> &); |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 124 | |
| 125 | void mangleInitDiscriminator() { |
| 126 | Discriminator = 0; |
| 127 | } |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 128 | |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 129 | bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { |
| 130 | unsigned &discriminator = Uniquifier[ND]; |
| 131 | if (!discriminator) |
| 132 | discriminator = ++Discriminator; |
| 133 | if (discriminator == 1) |
| 134 | return false; |
| 135 | disc = discriminator-2; |
| 136 | return true; |
| 137 | } |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 138 | /// @} |
| 139 | }; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 140 | |
| 141 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | #endif |