ItaniumCXXABI.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This provides C++ AST support targeting the Itanium C++ ABI, which is
  10. // documented at:
  11. // http://www.codesourcery.com/public/cxx-abi/abi.html
  12. // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
  13. //
  14. // It also supports the closely-related ARM C++ ABI, documented at:
  15. // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "CXXABI.h"
  19. #include "clang/AST/ASTContext.h"
  20. #include "clang/AST/DeclCXX.h"
  21. #include "clang/AST/Mangle.h"
  22. #include "clang/AST/MangleNumberingContext.h"
  23. #include "clang/AST/RecordLayout.h"
  24. #include "clang/AST/Type.h"
  25. #include "clang/Basic/TargetInfo.h"
  26. #include "llvm/ADT/FoldingSet.h"
  27. #include "llvm/ADT/iterator.h"
  28. using namespace clang;
  29. namespace {
  30. /// According to Itanium C++ ABI 5.1.2:
  31. /// the name of an anonymous union is considered to be
  32. /// the name of the first named data member found by a pre-order,
  33. /// depth-first, declaration-order walk of the data members of
  34. /// the anonymous union.
  35. /// If there is no such data member (i.e., if all of the data members
  36. /// in the union are unnamed), then there is no way for a program to
  37. /// refer to the anonymous union, and there is therefore no need to mangle its name.
  38. ///
  39. /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
  40. static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
  41. const RecordType *RT = VD.getType()->getAs<RecordType>();
  42. assert(RT && "type of VarDecl is expected to be RecordType.");
  43. assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
  44. if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
  45. return FD->getIdentifier();
  46. }
  47. return nullptr;
  48. }
  49. /// The name of a decomposition declaration.
  50. struct DecompositionDeclName {
  51. using BindingArray = ArrayRef<const BindingDecl*>;
  52. /// Representative example of a set of bindings with these names.
  53. BindingArray Bindings;
  54. /// Iterators over the sequence of identifiers in the name.
  55. struct Iterator
  56. : llvm::iterator_adaptor_base<Iterator, BindingArray::const_iterator,
  57. std::random_access_iterator_tag,
  58. const IdentifierInfo *> {
  59. Iterator(BindingArray::const_iterator It) : iterator_adaptor_base(It) {}
  60. const IdentifierInfo *operator*() const {
  61. return (*this->I)->getIdentifier();
  62. }
  63. };
  64. Iterator begin() const { return Iterator(Bindings.begin()); }
  65. Iterator end() const { return Iterator(Bindings.end()); }
  66. };
  67. }
  68. namespace llvm {
  69. template<typename T> bool isDenseMapKeyEmpty(T V) {
  70. return llvm::DenseMapInfo<T>::isEqual(
  71. V, llvm::DenseMapInfo<T>::getEmptyKey());
  72. }
  73. template<typename T> bool isDenseMapKeyTombstone(T V) {
  74. return llvm::DenseMapInfo<T>::isEqual(
  75. V, llvm::DenseMapInfo<T>::getTombstoneKey());
  76. }
  77. template<typename T>
  78. Optional<bool> areDenseMapKeysEqualSpecialValues(T LHS, T RHS) {
  79. bool LHSEmpty = isDenseMapKeyEmpty(LHS);
  80. bool RHSEmpty = isDenseMapKeyEmpty(RHS);
  81. if (LHSEmpty || RHSEmpty)
  82. return LHSEmpty && RHSEmpty;
  83. bool LHSTombstone = isDenseMapKeyTombstone(LHS);
  84. bool RHSTombstone = isDenseMapKeyTombstone(RHS);
  85. if (LHSTombstone || RHSTombstone)
  86. return LHSTombstone && RHSTombstone;
  87. return None;
  88. }
  89. template<>
  90. struct DenseMapInfo<DecompositionDeclName> {
  91. using ArrayInfo = llvm::DenseMapInfo<ArrayRef<const BindingDecl*>>;
  92. static DecompositionDeclName getEmptyKey() {
  93. return {ArrayInfo::getEmptyKey()};
  94. }
  95. static DecompositionDeclName getTombstoneKey() {
  96. return {ArrayInfo::getTombstoneKey()};
  97. }
  98. static unsigned getHashValue(DecompositionDeclName Key) {
  99. assert(!isEqual(Key, getEmptyKey()) && !isEqual(Key, getTombstoneKey()));
  100. return llvm::hash_combine_range(Key.begin(), Key.end());
  101. }
  102. static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS) {
  103. if (Optional<bool> Result = areDenseMapKeysEqualSpecialValues(
  104. LHS.Bindings, RHS.Bindings))
  105. return *Result;
  106. return LHS.Bindings.size() == RHS.Bindings.size() &&
  107. std::equal(LHS.begin(), LHS.end(), RHS.begin());
  108. }
  109. };
  110. }
  111. namespace {
  112. /// Keeps track of the mangled names of lambda expressions and block
  113. /// literals within a particular context.
  114. class ItaniumNumberingContext : public MangleNumberingContext {
  115. ItaniumMangleContext *Mangler;
  116. llvm::StringMap<unsigned> LambdaManglingNumbers;
  117. unsigned BlockManglingNumber = 0;
  118. llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
  119. llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
  120. llvm::DenseMap<DecompositionDeclName, unsigned>
  121. DecompsitionDeclManglingNumbers;
  122. public:
  123. ItaniumNumberingContext(ItaniumMangleContext *Mangler) : Mangler(Mangler) {}
  124. unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
  125. const CXXRecordDecl *Lambda = CallOperator->getParent();
  126. assert(Lambda->isLambda());
  127. // Computation of the <lambda-sig> is non-trivial and subtle. Rather than
  128. // duplicating it here, just mangle the <lambda-sig> directly.
  129. llvm::SmallString<128> LambdaSig;
  130. llvm::raw_svector_ostream Out(LambdaSig);
  131. Mangler->mangleLambdaSig(Lambda, Out);
  132. return ++LambdaManglingNumbers[LambdaSig];
  133. }
  134. unsigned getManglingNumber(const BlockDecl *BD) override {
  135. return ++BlockManglingNumber;
  136. }
  137. unsigned getStaticLocalNumber(const VarDecl *VD) override {
  138. return 0;
  139. }
  140. /// Variable decls are numbered by identifier.
  141. unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
  142. if (auto *DD = dyn_cast<DecompositionDecl>(VD)) {
  143. DecompositionDeclName Name{DD->bindings()};
  144. return ++DecompsitionDeclManglingNumbers[Name];
  145. }
  146. const IdentifierInfo *Identifier = VD->getIdentifier();
  147. if (!Identifier) {
  148. // VarDecl without an identifier represents an anonymous union
  149. // declaration.
  150. Identifier = findAnonymousUnionVarDeclName(*VD);
  151. }
  152. return ++VarManglingNumbers[Identifier];
  153. }
  154. unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
  155. return ++TagManglingNumbers[TD->getIdentifier()];
  156. }
  157. };
  158. // A version of this for SYCL that makes sure that 'device' mangling context
  159. // matches the lambda mangling number, so that __builtin_sycl_unique_stable_name
  160. // can be consistently generated between a MS and Itanium host by just referring
  161. // to the device mangling number.
  162. class ItaniumSYCLNumberingContext : public ItaniumNumberingContext {
  163. llvm::DenseMap<const CXXMethodDecl *, unsigned> ManglingNumbers;
  164. using ManglingItr = decltype(ManglingNumbers)::iterator;
  165. public:
  166. ItaniumSYCLNumberingContext(ItaniumMangleContext *Mangler)
  167. : ItaniumNumberingContext(Mangler) {}
  168. unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
  169. unsigned Number = ItaniumNumberingContext::getManglingNumber(CallOperator);
  170. std::pair<ManglingItr, bool> emplace_result =
  171. ManglingNumbers.try_emplace(CallOperator, Number);
  172. (void)emplace_result;
  173. assert(emplace_result.second && "Lambda number set multiple times?");
  174. return Number;
  175. }
  176. using ItaniumNumberingContext::getManglingNumber;
  177. unsigned getDeviceManglingNumber(const CXXMethodDecl *CallOperator) override {
  178. ManglingItr Itr = ManglingNumbers.find(CallOperator);
  179. assert(Itr != ManglingNumbers.end() && "Lambda not yet mangled?");
  180. return Itr->second;
  181. }
  182. };
  183. class ItaniumCXXABI : public CXXABI {
  184. private:
  185. std::unique_ptr<MangleContext> Mangler;
  186. protected:
  187. ASTContext &Context;
  188. public:
  189. ItaniumCXXABI(ASTContext &Ctx)
  190. : Mangler(Ctx.createMangleContext()), Context(Ctx) {}
  191. MemberPointerInfo
  192. getMemberPointerInfo(const MemberPointerType *MPT) const override {
  193. const TargetInfo &Target = Context.getTargetInfo();
  194. TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
  195. MemberPointerInfo MPI;
  196. MPI.Width = Target.getTypeWidth(PtrDiff);
  197. MPI.Align = Target.getTypeAlign(PtrDiff);
  198. MPI.HasPadding = false;
  199. if (MPT->isMemberFunctionPointer())
  200. MPI.Width *= 2;
  201. return MPI;
  202. }
  203. CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
  204. const llvm::Triple &T = Context.getTargetInfo().getTriple();
  205. if (!isVariadic && T.isWindowsGNUEnvironment() &&
  206. T.getArch() == llvm::Triple::x86)
  207. return CC_X86ThisCall;
  208. return Context.getTargetInfo().getDefaultCallingConv();
  209. }
  210. // We cheat and just check that the class has a vtable pointer, and that it's
  211. // only big enough to have a vtable pointer and nothing more (or less).
  212. bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
  213. // Check that the class has a vtable pointer.
  214. if (!RD->isDynamicClass())
  215. return false;
  216. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  217. CharUnits PointerSize =
  218. Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
  219. return Layout.getNonVirtualSize() == PointerSize;
  220. }
  221. const CXXConstructorDecl *
  222. getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
  223. return nullptr;
  224. }
  225. void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
  226. CXXConstructorDecl *CD) override {}
  227. void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
  228. TypedefNameDecl *DD) override {}
  229. TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
  230. return nullptr;
  231. }
  232. void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
  233. DeclaratorDecl *DD) override {}
  234. DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
  235. return nullptr;
  236. }
  237. std::unique_ptr<MangleNumberingContext>
  238. createMangleNumberingContext() const override {
  239. if (Context.getLangOpts().isSYCL())
  240. return std::make_unique<ItaniumSYCLNumberingContext>(
  241. cast<ItaniumMangleContext>(Mangler.get()));
  242. return std::make_unique<ItaniumNumberingContext>(
  243. cast<ItaniumMangleContext>(Mangler.get()));
  244. }
  245. };
  246. }
  247. CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
  248. return new ItaniumCXXABI(Ctx);
  249. }
  250. std::unique_ptr<MangleNumberingContext>
  251. clang::createItaniumNumberingContext(MangleContext *Mangler) {
  252. return std::make_unique<ItaniumNumberingContext>(
  253. cast<ItaniumMangleContext>(Mangler));
  254. }