RecordLayout.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- RecordLayout.h - Layout information for a struct/union ---*- 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. // This file defines the RecordLayout interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_RECORDLAYOUT_H
  18. #define LLVM_CLANG_AST_RECORDLAYOUT_H
  19. #include "clang/AST/ASTVector.h"
  20. #include "clang/AST/CharUnits.h"
  21. #include "clang/AST/DeclCXX.h"
  22. #include "clang/Basic/LLVM.h"
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/ADT/DenseMap.h"
  25. #include "llvm/ADT/PointerIntPair.h"
  26. #include <cassert>
  27. #include <cstdint>
  28. namespace clang {
  29. class ASTContext;
  30. class CXXRecordDecl;
  31. /// ASTRecordLayout -
  32. /// This class contains layout information for one RecordDecl,
  33. /// which is a struct/union/class. The decl represented must be a definition,
  34. /// not a forward declaration.
  35. /// This class is also used to contain layout information for one
  36. /// ObjCInterfaceDecl. FIXME - Find appropriate name.
  37. /// These objects are managed by ASTContext.
  38. class ASTRecordLayout {
  39. public:
  40. struct VBaseInfo {
  41. /// The offset to this virtual base in the complete-object layout
  42. /// of this class.
  43. CharUnits VBaseOffset;
  44. private:
  45. /// Whether this virtual base requires a vtordisp field in the
  46. /// Microsoft ABI. These fields are required for certain operations
  47. /// in constructors and destructors.
  48. bool HasVtorDisp = false;
  49. public:
  50. VBaseInfo() = default;
  51. VBaseInfo(CharUnits VBaseOffset, bool hasVtorDisp)
  52. : VBaseOffset(VBaseOffset), HasVtorDisp(hasVtorDisp) {}
  53. bool hasVtorDisp() const { return HasVtorDisp; }
  54. };
  55. using VBaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, VBaseInfo>;
  56. private:
  57. friend class ASTContext;
  58. /// Size - Size of record in characters.
  59. CharUnits Size;
  60. /// DataSize - Size of record in characters without tail padding.
  61. CharUnits DataSize;
  62. // Alignment - Alignment of record in characters.
  63. CharUnits Alignment;
  64. // PreferredAlignment - Preferred alignment of record in characters. This
  65. // can be different than Alignment in cases where it is beneficial for
  66. // performance or backwards compatibility preserving (e.g. AIX-ABI).
  67. CharUnits PreferredAlignment;
  68. // UnadjustedAlignment - Maximum of the alignments of the record members in
  69. // characters.
  70. CharUnits UnadjustedAlignment;
  71. /// RequiredAlignment - The required alignment of the object. In the MS-ABI
  72. /// the __declspec(align()) trumps #pramga pack and must always be obeyed.
  73. CharUnits RequiredAlignment;
  74. /// FieldOffsets - Array of field offsets in bits.
  75. ASTVector<uint64_t> FieldOffsets;
  76. /// CXXRecordLayoutInfo - Contains C++ specific layout information.
  77. struct CXXRecordLayoutInfo {
  78. /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
  79. /// the size of the object without virtual bases.
  80. CharUnits NonVirtualSize;
  81. /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
  82. /// which is the alignment of the object without virtual bases.
  83. CharUnits NonVirtualAlignment;
  84. /// PreferredNVAlignment - The preferred non-virtual alignment (in chars) of
  85. /// an object, which is the preferred alignment of the object without
  86. /// virtual bases.
  87. CharUnits PreferredNVAlignment;
  88. /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
  89. /// (either a base or a member). Will be zero if the class doesn't contain
  90. /// any empty subobjects.
  91. CharUnits SizeOfLargestEmptySubobject;
  92. /// VBPtrOffset - Virtual base table offset (Microsoft-only).
  93. CharUnits VBPtrOffset;
  94. /// HasOwnVFPtr - Does this class provide a virtual function table
  95. /// (vtable in Itanium, vftbl in Microsoft) that is independent from
  96. /// its base classes?
  97. bool HasOwnVFPtr : 1;
  98. /// HasVFPtr - Does this class have a vftable that could be extended by
  99. /// a derived class. The class may have inherited this pointer from
  100. /// a primary base class.
  101. bool HasExtendableVFPtr : 1;
  102. /// EndsWithZeroSizedObject - True if this class contains a zero sized
  103. /// member or base or a base with a zero sized member or base.
  104. /// Only used for MS-ABI.
  105. bool EndsWithZeroSizedObject : 1;
  106. /// True if this class is zero sized or first base is zero sized or
  107. /// has this property. Only used for MS-ABI.
  108. bool LeadsWithZeroSizedBase : 1;
  109. /// PrimaryBase - The primary base info for this record.
  110. llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
  111. /// BaseSharingVBPtr - The base we share vbptr with.
  112. const CXXRecordDecl *BaseSharingVBPtr;
  113. /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
  114. using BaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, CharUnits>;
  115. /// BaseOffsets - Contains a map from base classes to their offset.
  116. BaseOffsetsMapTy BaseOffsets;
  117. /// VBaseOffsets - Contains a map from vbase classes to their offset.
  118. VBaseOffsetsMapTy VBaseOffsets;
  119. };
  120. /// CXXInfo - If the record layout is for a C++ record, this will have
  121. /// C++ specific information about the record.
  122. CXXRecordLayoutInfo *CXXInfo = nullptr;
  123. ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
  124. CharUnits preferredAlignment, CharUnits unadjustedAlignment,
  125. CharUnits requiredAlignment, CharUnits datasize,
  126. ArrayRef<uint64_t> fieldoffsets);
  127. using BaseOffsetsMapTy = CXXRecordLayoutInfo::BaseOffsetsMapTy;
  128. // Constructor for C++ records.
  129. ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
  130. CharUnits preferredAlignment, CharUnits unadjustedAlignment,
  131. CharUnits requiredAlignment, bool hasOwnVFPtr,
  132. bool hasExtendableVFPtr, CharUnits vbptroffset,
  133. CharUnits datasize, ArrayRef<uint64_t> fieldoffsets,
  134. CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
  135. CharUnits preferrednvalignment,
  136. CharUnits SizeOfLargestEmptySubobject,
  137. const CXXRecordDecl *PrimaryBase, bool IsPrimaryBaseVirtual,
  138. const CXXRecordDecl *BaseSharingVBPtr,
  139. bool EndsWithZeroSizedObject, bool LeadsWithZeroSizedBase,
  140. const BaseOffsetsMapTy &BaseOffsets,
  141. const VBaseOffsetsMapTy &VBaseOffsets);
  142. ~ASTRecordLayout() = default;
  143. void Destroy(ASTContext &Ctx);
  144. public:
  145. ASTRecordLayout(const ASTRecordLayout &) = delete;
  146. ASTRecordLayout &operator=(const ASTRecordLayout &) = delete;
  147. /// getAlignment - Get the record alignment in characters.
  148. CharUnits getAlignment() const { return Alignment; }
  149. /// getPreferredFieldAlignment - Get the record preferred alignment in
  150. /// characters.
  151. CharUnits getPreferredAlignment() const { return PreferredAlignment; }
  152. /// getUnadjustedAlignment - Get the record alignment in characters, before
  153. /// alignment adjustement.
  154. CharUnits getUnadjustedAlignment() const { return UnadjustedAlignment; }
  155. /// getSize - Get the record size in characters.
  156. CharUnits getSize() const { return Size; }
  157. /// getFieldCount - Get the number of fields in the layout.
  158. unsigned getFieldCount() const { return FieldOffsets.size(); }
  159. /// getFieldOffset - Get the offset of the given field index, in
  160. /// bits.
  161. uint64_t getFieldOffset(unsigned FieldNo) const {
  162. return FieldOffsets[FieldNo];
  163. }
  164. /// getDataSize() - Get the record data size, which is the record size
  165. /// without tail padding, in characters.
  166. CharUnits getDataSize() const { return DataSize; }
  167. /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
  168. /// which is the size of the object without virtual bases.
  169. CharUnits getNonVirtualSize() const {
  170. assert(CXXInfo && "Record layout does not have C++ specific info!");
  171. return CXXInfo->NonVirtualSize;
  172. }
  173. /// getNonVirtualAlignment - Get the non-virtual alignment (in chars) of an
  174. /// object, which is the alignment of the object without virtual bases.
  175. CharUnits getNonVirtualAlignment() const {
  176. assert(CXXInfo && "Record layout does not have C++ specific info!");
  177. return CXXInfo->NonVirtualAlignment;
  178. }
  179. /// getPreferredNVAlignment - Get the preferred non-virtual alignment (in
  180. /// chars) of an object, which is the preferred alignment of the object
  181. /// without virtual bases.
  182. CharUnits getPreferredNVAlignment() const {
  183. assert(CXXInfo && "Record layout does not have C++ specific info!");
  184. return CXXInfo->PreferredNVAlignment;
  185. }
  186. /// getPrimaryBase - Get the primary base for this record.
  187. const CXXRecordDecl *getPrimaryBase() const {
  188. assert(CXXInfo && "Record layout does not have C++ specific info!");
  189. return CXXInfo->PrimaryBase.getPointer();
  190. }
  191. /// isPrimaryBaseVirtual - Get whether the primary base for this record
  192. /// is virtual or not.
  193. bool isPrimaryBaseVirtual() const {
  194. assert(CXXInfo && "Record layout does not have C++ specific info!");
  195. return CXXInfo->PrimaryBase.getInt();
  196. }
  197. /// getBaseClassOffset - Get the offset, in chars, for the given base class.
  198. CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
  199. assert(CXXInfo && "Record layout does not have C++ specific info!");
  200. Base = Base->getDefinition();
  201. assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
  202. return CXXInfo->BaseOffsets[Base];
  203. }
  204. /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
  205. CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
  206. assert(CXXInfo && "Record layout does not have C++ specific info!");
  207. VBase = VBase->getDefinition();
  208. assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
  209. return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
  210. }
  211. CharUnits getSizeOfLargestEmptySubobject() const {
  212. assert(CXXInfo && "Record layout does not have C++ specific info!");
  213. return CXXInfo->SizeOfLargestEmptySubobject;
  214. }
  215. /// hasOwnVFPtr - Does this class provide its own virtual-function
  216. /// table pointer, rather than inheriting one from a primary base
  217. /// class? If so, it is at offset zero.
  218. ///
  219. /// This implies that the ABI has no primary base class, meaning
  220. /// that it has no base classes that are suitable under the conditions
  221. /// of the ABI.
  222. bool hasOwnVFPtr() const {
  223. assert(CXXInfo && "Record layout does not have C++ specific info!");
  224. return CXXInfo->HasOwnVFPtr;
  225. }
  226. /// hasVFPtr - Does this class have a virtual function table pointer
  227. /// that can be extended by a derived class? This is synonymous with
  228. /// this class having a VFPtr at offset zero.
  229. bool hasExtendableVFPtr() const {
  230. assert(CXXInfo && "Record layout does not have C++ specific info!");
  231. return CXXInfo->HasExtendableVFPtr;
  232. }
  233. /// hasOwnVBPtr - Does this class provide its own virtual-base
  234. /// table pointer, rather than inheriting one from a primary base
  235. /// class?
  236. ///
  237. /// This implies that the ABI has no primary base class, meaning
  238. /// that it has no base classes that are suitable under the conditions
  239. /// of the ABI.
  240. bool hasOwnVBPtr() const {
  241. assert(CXXInfo && "Record layout does not have C++ specific info!");
  242. return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
  243. }
  244. /// hasVBPtr - Does this class have a virtual function table pointer.
  245. bool hasVBPtr() const {
  246. assert(CXXInfo && "Record layout does not have C++ specific info!");
  247. return !CXXInfo->VBPtrOffset.isNegative();
  248. }
  249. CharUnits getRequiredAlignment() const { return RequiredAlignment; }
  250. bool endsWithZeroSizedObject() const {
  251. return CXXInfo && CXXInfo->EndsWithZeroSizedObject;
  252. }
  253. bool leadsWithZeroSizedBase() const {
  254. assert(CXXInfo && "Record layout does not have C++ specific info!");
  255. return CXXInfo->LeadsWithZeroSizedBase;
  256. }
  257. /// getVBPtrOffset - Get the offset for virtual base table pointer.
  258. /// This is only meaningful with the Microsoft ABI.
  259. CharUnits getVBPtrOffset() const {
  260. assert(CXXInfo && "Record layout does not have C++ specific info!");
  261. return CXXInfo->VBPtrOffset;
  262. }
  263. const CXXRecordDecl *getBaseSharingVBPtr() const {
  264. assert(CXXInfo && "Record layout does not have C++ specific info!");
  265. return CXXInfo->BaseSharingVBPtr;
  266. }
  267. const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
  268. assert(CXXInfo && "Record layout does not have C++ specific info!");
  269. return CXXInfo->VBaseOffsets;
  270. }
  271. };
  272. } // namespace clang
  273. #endif // LLVM_CLANG_AST_RECORDLAYOUT_H
  274. #ifdef __GNUC__
  275. #pragma GCC diagnostic pop
  276. #endif