ItaniumCXXABI.cpp 11 KB

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