ItaniumCXXABI.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
  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. // This provides C++ AST support targeting the Itanium C++ ABI, which is
  11. // documented at:
  12. // http://www.codesourcery.com/public/cxx-abi/abi.html
  13. // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
  14. //
  15. // It also supports the closely-related ARM C++ ABI, documented at:
  16. // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "CXXABI.h"
  20. #include "clang/AST/ASTContext.h"
  21. #include "clang/AST/DeclCXX.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. using namespace clang;
  27. namespace {
  28. /// According to Itanium C++ ABI 5.1.2:
  29. /// the name of an anonymous union is considered to be
  30. /// the name of the first named data member found by a pre-order,
  31. /// depth-first, declaration-order walk of the data members of
  32. /// the anonymous union.
  33. /// If there is no such data member (i.e., if all of the data members
  34. /// in the union are unnamed), then there is no way for a program to
  35. /// refer to the anonymous union, and there is therefore no need to mangle its name.
  36. ///
  37. /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
  38. static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
  39. const RecordType *RT = VD.getType()->getAs<RecordType>();
  40. assert(RT && "type of VarDecl is expected to be RecordType.");
  41. assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
  42. if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
  43. return FD->getIdentifier();
  44. }
  45. return nullptr;
  46. }
  47. /// \brief Keeps track of the mangled names of lambda expressions and block
  48. /// literals within a particular context.
  49. class ItaniumNumberingContext : public MangleNumberingContext {
  50. llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
  51. llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
  52. llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
  53. public:
  54. unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
  55. const FunctionProtoType *Proto =
  56. CallOperator->getType()->getAs<FunctionProtoType>();
  57. ASTContext &Context = CallOperator->getASTContext();
  58. QualType Key =
  59. Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(),
  60. FunctionProtoType::ExtProtoInfo(), None); // HLSL Change - add param modifiers
  61. Key = Context.getCanonicalType(Key);
  62. return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
  63. }
  64. unsigned getManglingNumber(const BlockDecl *BD) override {
  65. const Type *Ty = nullptr;
  66. return ++ManglingNumbers[Ty];
  67. }
  68. unsigned getStaticLocalNumber(const VarDecl *VD) override {
  69. return 0;
  70. }
  71. /// Variable decls are numbered by identifier.
  72. unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
  73. const IdentifierInfo *Identifier = VD->getIdentifier();
  74. if (!Identifier) {
  75. // VarDecl without an identifier represents an anonymous union declaration.
  76. Identifier = findAnonymousUnionVarDeclName(*VD);
  77. }
  78. return ++VarManglingNumbers[Identifier];
  79. }
  80. unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
  81. return ++TagManglingNumbers[TD->getIdentifier()];
  82. }
  83. };
  84. class ItaniumCXXABI : public CXXABI {
  85. protected:
  86. ASTContext &Context;
  87. public:
  88. ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
  89. std::pair<uint64_t, unsigned>
  90. getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override {
  91. const TargetInfo &Target = Context.getTargetInfo();
  92. TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
  93. uint64_t Width = Target.getTypeWidth(PtrDiff);
  94. unsigned Align = Target.getTypeAlign(PtrDiff);
  95. if (MPT->isMemberFunctionPointer())
  96. Width = 2 * Width;
  97. return std::make_pair(Width, Align);
  98. }
  99. CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
  100. const llvm::Triple &T = Context.getTargetInfo().getTriple();
  101. if (!isVariadic && T.isWindowsGNUEnvironment() &&
  102. T.getArch() == llvm::Triple::x86)
  103. return CC_X86ThisCall;
  104. return CC_C;
  105. }
  106. // We cheat and just check that the class has a vtable pointer, and that it's
  107. // only big enough to have a vtable pointer and nothing more (or less).
  108. bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
  109. // Check that the class has a vtable pointer.
  110. if (!RD->isDynamicClass())
  111. return false;
  112. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  113. CharUnits PointerSize =
  114. Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
  115. return Layout.getNonVirtualSize() == PointerSize;
  116. }
  117. const CXXConstructorDecl *
  118. getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
  119. return nullptr;
  120. }
  121. void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
  122. CXXConstructorDecl *CD) override {}
  123. void addDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
  124. unsigned ParmIdx, Expr *DAE) override {}
  125. Expr *getDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
  126. unsigned ParmIdx) override {
  127. return nullptr;
  128. }
  129. MangleNumberingContext *createMangleNumberingContext() const override {
  130. return new ItaniumNumberingContext();
  131. }
  132. };
  133. }
  134. CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
  135. return new ItaniumCXXABI(Ctx);
  136. }