Thunk.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===----- Thunk.h - Declarations related to VTable Thunks ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Enums/classes describing THUNK related information about constructors,
  16. /// destructors and thunks.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_BASIC_THUNK_H
  20. #define LLVM_CLANG_BASIC_THUNK_H
  21. #include <cstdint>
  22. #include <cstring>
  23. namespace clang {
  24. class CXXMethodDecl;
  25. /// A return adjustment.
  26. struct ReturnAdjustment {
  27. /// The non-virtual adjustment from the derived object to its
  28. /// nearest virtual base.
  29. int64_t NonVirtual;
  30. /// Holds the ABI-specific information about the virtual return
  31. /// adjustment, if needed.
  32. union VirtualAdjustment {
  33. // Itanium ABI
  34. struct {
  35. /// The offset (in bytes), relative to the address point
  36. /// of the virtual base class offset.
  37. int64_t VBaseOffsetOffset;
  38. } Itanium;
  39. // Microsoft ABI
  40. struct {
  41. /// The offset (in bytes) of the vbptr, relative to the beginning
  42. /// of the derived class.
  43. uint32_t VBPtrOffset;
  44. /// Index of the virtual base in the vbtable.
  45. uint32_t VBIndex;
  46. } Microsoft;
  47. VirtualAdjustment() { memset(this, 0, sizeof(*this)); }
  48. bool Equals(const VirtualAdjustment &Other) const {
  49. return memcmp(this, &Other, sizeof(Other)) == 0;
  50. }
  51. bool isEmpty() const {
  52. VirtualAdjustment Zero;
  53. return Equals(Zero);
  54. }
  55. bool Less(const VirtualAdjustment &RHS) const {
  56. return memcmp(this, &RHS, sizeof(RHS)) < 0;
  57. }
  58. } Virtual;
  59. ReturnAdjustment() : NonVirtual(0) {}
  60. bool isEmpty() const { return !NonVirtual && Virtual.isEmpty(); }
  61. friend bool operator==(const ReturnAdjustment &LHS,
  62. const ReturnAdjustment &RHS) {
  63. return LHS.NonVirtual == RHS.NonVirtual && LHS.Virtual.Equals(RHS.Virtual);
  64. }
  65. friend bool operator!=(const ReturnAdjustment &LHS,
  66. const ReturnAdjustment &RHS) {
  67. return !(LHS == RHS);
  68. }
  69. friend bool operator<(const ReturnAdjustment &LHS,
  70. const ReturnAdjustment &RHS) {
  71. if (LHS.NonVirtual < RHS.NonVirtual)
  72. return true;
  73. return LHS.NonVirtual == RHS.NonVirtual && LHS.Virtual.Less(RHS.Virtual);
  74. }
  75. };
  76. /// A \c this pointer adjustment.
  77. struct ThisAdjustment {
  78. /// The non-virtual adjustment from the derived object to its
  79. /// nearest virtual base.
  80. int64_t NonVirtual;
  81. /// Holds the ABI-specific information about the virtual this
  82. /// adjustment, if needed.
  83. union VirtualAdjustment {
  84. // Itanium ABI
  85. struct {
  86. /// The offset (in bytes), relative to the address point,
  87. /// of the virtual call offset.
  88. int64_t VCallOffsetOffset;
  89. } Itanium;
  90. struct {
  91. /// The offset of the vtordisp (in bytes), relative to the ECX.
  92. int32_t VtordispOffset;
  93. /// The offset of the vbptr of the derived class (in bytes),
  94. /// relative to the ECX after vtordisp adjustment.
  95. int32_t VBPtrOffset;
  96. /// The offset (in bytes) of the vbase offset in the vbtable.
  97. int32_t VBOffsetOffset;
  98. } Microsoft;
  99. VirtualAdjustment() { memset(this, 0, sizeof(*this)); }
  100. bool Equals(const VirtualAdjustment &Other) const {
  101. return memcmp(this, &Other, sizeof(Other)) == 0;
  102. }
  103. bool isEmpty() const {
  104. VirtualAdjustment Zero;
  105. return Equals(Zero);
  106. }
  107. bool Less(const VirtualAdjustment &RHS) const {
  108. return memcmp(this, &RHS, sizeof(RHS)) < 0;
  109. }
  110. } Virtual;
  111. ThisAdjustment() : NonVirtual(0) {}
  112. bool isEmpty() const { return !NonVirtual && Virtual.isEmpty(); }
  113. friend bool operator==(const ThisAdjustment &LHS, const ThisAdjustment &RHS) {
  114. return LHS.NonVirtual == RHS.NonVirtual && LHS.Virtual.Equals(RHS.Virtual);
  115. }
  116. friend bool operator!=(const ThisAdjustment &LHS, const ThisAdjustment &RHS) {
  117. return !(LHS == RHS);
  118. }
  119. friend bool operator<(const ThisAdjustment &LHS, const ThisAdjustment &RHS) {
  120. if (LHS.NonVirtual < RHS.NonVirtual)
  121. return true;
  122. return LHS.NonVirtual == RHS.NonVirtual && LHS.Virtual.Less(RHS.Virtual);
  123. }
  124. };
  125. /// The \c this pointer adjustment as well as an optional return
  126. /// adjustment for a thunk.
  127. struct ThunkInfo {
  128. /// The \c this pointer adjustment.
  129. ThisAdjustment This;
  130. /// The return adjustment.
  131. ReturnAdjustment Return;
  132. /// Holds a pointer to the overridden method this thunk is for,
  133. /// if needed by the ABI to distinguish different thunks with equal
  134. /// adjustments. Otherwise, null.
  135. /// CAUTION: In the unlikely event you need to sort ThunkInfos, consider using
  136. /// an ABI-specific comparator.
  137. const CXXMethodDecl *Method;
  138. ThunkInfo() : Method(nullptr) {}
  139. ThunkInfo(const ThisAdjustment &This, const ReturnAdjustment &Return,
  140. const CXXMethodDecl *Method = nullptr)
  141. : This(This), Return(Return), Method(Method) {}
  142. friend bool operator==(const ThunkInfo &LHS, const ThunkInfo &RHS) {
  143. return LHS.This == RHS.This && LHS.Return == RHS.Return &&
  144. LHS.Method == RHS.Method;
  145. }
  146. bool isEmpty() const {
  147. return This.isEmpty() && Return.isEmpty() && Method == nullptr;
  148. }
  149. };
  150. } // end namespace clang
  151. #endif
  152. #ifdef __GNUC__
  153. #pragma GCC diagnostic pop
  154. #endif