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