Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:40

0001 //===- RecordLayout.h - Layout information for a struct/union ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines the RecordLayout interface.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_AST_RECORDLAYOUT_H
0014 #define LLVM_CLANG_AST_RECORDLAYOUT_H
0015 
0016 #include "clang/AST/ASTVector.h"
0017 #include "clang/AST/CharUnits.h"
0018 #include "clang/AST/DeclCXX.h"
0019 #include "clang/Basic/LLVM.h"
0020 #include "llvm/ADT/ArrayRef.h"
0021 #include "llvm/ADT/DenseMap.h"
0022 #include "llvm/ADT/PointerIntPair.h"
0023 #include <cassert>
0024 #include <cstdint>
0025 
0026 namespace clang {
0027 
0028 class ASTContext;
0029 class CXXRecordDecl;
0030 
0031 /// ASTRecordLayout -
0032 /// This class contains layout information for one RecordDecl,
0033 /// which is a struct/union/class.  The decl represented must be a definition,
0034 /// not a forward declaration.
0035 /// This class is also used to contain layout information for one
0036 /// ObjCInterfaceDecl. FIXME - Find appropriate name.
0037 /// These objects are managed by ASTContext.
0038 class ASTRecordLayout {
0039 public:
0040   struct VBaseInfo {
0041     /// The offset to this virtual base in the complete-object layout
0042     /// of this class.
0043     CharUnits VBaseOffset;
0044 
0045   private:
0046     /// Whether this virtual base requires a vtordisp field in the
0047     /// Microsoft ABI.  These fields are required for certain operations
0048     /// in constructors and destructors.
0049     bool HasVtorDisp = false;
0050 
0051   public:
0052     VBaseInfo() = default;
0053     VBaseInfo(CharUnits VBaseOffset, bool hasVtorDisp)
0054         : VBaseOffset(VBaseOffset), HasVtorDisp(hasVtorDisp) {}
0055 
0056     bool hasVtorDisp() const { return HasVtorDisp; }
0057   };
0058 
0059   using VBaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, VBaseInfo>;
0060 
0061 private:
0062   friend class ASTContext;
0063 
0064   /// Size - Size of record in characters.
0065   CharUnits Size;
0066 
0067   /// DataSize - Size of record in characters without tail padding.
0068   CharUnits DataSize;
0069 
0070   // Alignment - Alignment of record in characters.
0071   CharUnits Alignment;
0072 
0073   // PreferredAlignment - Preferred alignment of record in characters. This
0074   // can be different than Alignment in cases where it is beneficial for
0075   // performance or backwards compatibility preserving (e.g. AIX-ABI).
0076   CharUnits PreferredAlignment;
0077 
0078   // UnadjustedAlignment - Maximum of the alignments of the record members in
0079   // characters.
0080   CharUnits UnadjustedAlignment;
0081 
0082   /// RequiredAlignment - The required alignment of the object.  In the MS-ABI
0083   /// the __declspec(align()) trumps #pramga pack and must always be obeyed.
0084   CharUnits RequiredAlignment;
0085 
0086   /// FieldOffsets - Array of field offsets in bits.
0087   ASTVector<uint64_t> FieldOffsets;
0088 
0089   /// CXXRecordLayoutInfo - Contains C++ specific layout information.
0090   struct CXXRecordLayoutInfo {
0091     /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
0092     /// the size of the object without virtual bases.
0093     CharUnits NonVirtualSize;
0094 
0095     /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
0096     /// which is the alignment of the object without virtual bases.
0097     CharUnits NonVirtualAlignment;
0098 
0099     /// PreferredNVAlignment - The preferred non-virtual alignment (in chars) of
0100     /// an object, which is the preferred alignment of the object without
0101     /// virtual bases.
0102     CharUnits PreferredNVAlignment;
0103 
0104     /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
0105     /// (either a base or a member). Will be zero if the class doesn't contain
0106     /// any empty subobjects.
0107     CharUnits SizeOfLargestEmptySubobject;
0108 
0109     /// VBPtrOffset - Virtual base table offset (Microsoft-only).
0110     CharUnits VBPtrOffset;
0111 
0112     /// HasOwnVFPtr - Does this class provide a virtual function table
0113     /// (vtable in Itanium, vftbl in Microsoft) that is independent from
0114     /// its base classes?
0115     bool HasOwnVFPtr : 1;
0116 
0117     /// HasVFPtr - Does this class have a vftable that could be extended by
0118     /// a derived class.  The class may have inherited this pointer from
0119     /// a primary base class.
0120     bool HasExtendableVFPtr : 1;
0121 
0122     /// EndsWithZeroSizedObject - True if this class contains a zero sized
0123     /// member or base or a base with a zero sized member or base.
0124     /// Only used for MS-ABI.
0125     bool EndsWithZeroSizedObject : 1;
0126 
0127     /// True if this class is zero sized or first base is zero sized or
0128     /// has this property.  Only used for MS-ABI.
0129     bool LeadsWithZeroSizedBase : 1;
0130 
0131     /// PrimaryBase - The primary base info for this record.
0132     llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
0133 
0134     /// BaseSharingVBPtr - The base we share vbptr with.
0135     const CXXRecordDecl *BaseSharingVBPtr;
0136 
0137     /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
0138     using BaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, CharUnits>;
0139 
0140     /// BaseOffsets - Contains a map from base classes to their offset.
0141     BaseOffsetsMapTy BaseOffsets;
0142 
0143     /// VBaseOffsets - Contains a map from vbase classes to their offset.
0144     VBaseOffsetsMapTy VBaseOffsets;
0145   };
0146 
0147   /// CXXInfo - If the record layout is for a C++ record, this will have
0148   /// C++ specific information about the record.
0149   CXXRecordLayoutInfo *CXXInfo = nullptr;
0150 
0151   ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
0152                   CharUnits preferredAlignment, CharUnits unadjustedAlignment,
0153                   CharUnits requiredAlignment, CharUnits datasize,
0154                   ArrayRef<uint64_t> fieldoffsets);
0155 
0156   using BaseOffsetsMapTy = CXXRecordLayoutInfo::BaseOffsetsMapTy;
0157 
0158   // Constructor for C++ records.
0159   ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
0160                   CharUnits preferredAlignment, CharUnits unadjustedAlignment,
0161                   CharUnits requiredAlignment, bool hasOwnVFPtr,
0162                   bool hasExtendableVFPtr, CharUnits vbptroffset,
0163                   CharUnits datasize, ArrayRef<uint64_t> fieldoffsets,
0164                   CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
0165                   CharUnits preferrednvalignment,
0166                   CharUnits SizeOfLargestEmptySubobject,
0167                   const CXXRecordDecl *PrimaryBase, bool IsPrimaryBaseVirtual,
0168                   const CXXRecordDecl *BaseSharingVBPtr,
0169                   bool EndsWithZeroSizedObject, bool LeadsWithZeroSizedBase,
0170                   const BaseOffsetsMapTy &BaseOffsets,
0171                   const VBaseOffsetsMapTy &VBaseOffsets);
0172 
0173   ~ASTRecordLayout() = default;
0174 
0175   void Destroy(ASTContext &Ctx);
0176 
0177 public:
0178   ASTRecordLayout(const ASTRecordLayout &) = delete;
0179   ASTRecordLayout &operator=(const ASTRecordLayout &) = delete;
0180 
0181   /// getAlignment - Get the record alignment in characters.
0182   CharUnits getAlignment() const { return Alignment; }
0183 
0184   /// getPreferredFieldAlignment - Get the record preferred alignment in
0185   /// characters.
0186   CharUnits getPreferredAlignment() const { return PreferredAlignment; }
0187 
0188   /// getUnadjustedAlignment - Get the record alignment in characters, before
0189   /// alignment adjustement.
0190   CharUnits getUnadjustedAlignment() const { return UnadjustedAlignment; }
0191 
0192   /// getSize - Get the record size in characters.
0193   CharUnits getSize() const { return Size; }
0194 
0195   /// getFieldCount - Get the number of fields in the layout.
0196   unsigned getFieldCount() const { return FieldOffsets.size(); }
0197 
0198   /// getFieldOffset - Get the offset of the given field index, in
0199   /// bits.
0200   uint64_t getFieldOffset(unsigned FieldNo) const {
0201     return FieldOffsets[FieldNo];
0202   }
0203 
0204   /// getDataSize() - Get the record data size, which is the record size
0205   /// without tail padding, in characters.
0206   CharUnits getDataSize() const { return DataSize; }
0207 
0208   /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
0209   /// which is the size of the object without virtual bases.
0210   CharUnits getNonVirtualSize() const {
0211     assert(CXXInfo && "Record layout does not have C++ specific info!");
0212 
0213     return CXXInfo->NonVirtualSize;
0214   }
0215 
0216   /// getNonVirtualAlignment - Get the non-virtual alignment (in chars) of an
0217   /// object, which is the alignment of the object without virtual bases.
0218   CharUnits getNonVirtualAlignment() const {
0219     assert(CXXInfo && "Record layout does not have C++ specific info!");
0220 
0221     return CXXInfo->NonVirtualAlignment;
0222   }
0223 
0224   /// getPreferredNVAlignment - Get the preferred non-virtual alignment (in
0225   /// chars) of an object, which is the preferred alignment of the object
0226   /// without virtual bases.
0227   CharUnits getPreferredNVAlignment() const {
0228     assert(CXXInfo && "Record layout does not have C++ specific info!");
0229 
0230     return CXXInfo->PreferredNVAlignment;
0231   }
0232 
0233   /// getPrimaryBase - Get the primary base for this record.
0234   const CXXRecordDecl *getPrimaryBase() const {
0235     assert(CXXInfo && "Record layout does not have C++ specific info!");
0236 
0237     return CXXInfo->PrimaryBase.getPointer();
0238   }
0239 
0240   /// isPrimaryBaseVirtual - Get whether the primary base for this record
0241   /// is virtual or not.
0242   bool isPrimaryBaseVirtual() const {
0243     assert(CXXInfo && "Record layout does not have C++ specific info!");
0244 
0245     return CXXInfo->PrimaryBase.getInt();
0246   }
0247 
0248   /// getBaseClassOffset - Get the offset, in chars, for the given base class.
0249   CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
0250     assert(CXXInfo && "Record layout does not have C++ specific info!");
0251 
0252     Base = Base->getDefinition();
0253     assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
0254 
0255     return CXXInfo->BaseOffsets[Base];
0256   }
0257 
0258   /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
0259   CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
0260     assert(CXXInfo && "Record layout does not have C++ specific info!");
0261 
0262     VBase = VBase->getDefinition();
0263     assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
0264 
0265     return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
0266   }
0267 
0268   CharUnits getSizeOfLargestEmptySubobject() const {
0269     assert(CXXInfo && "Record layout does not have C++ specific info!");
0270     return CXXInfo->SizeOfLargestEmptySubobject;
0271   }
0272 
0273   /// hasOwnVFPtr - Does this class provide its own virtual-function
0274   /// table pointer, rather than inheriting one from a primary base
0275   /// class?  If so, it is at offset zero.
0276   ///
0277   /// This implies that the ABI has no primary base class, meaning
0278   /// that it has no base classes that are suitable under the conditions
0279   /// of the ABI.
0280   bool hasOwnVFPtr() const {
0281     assert(CXXInfo && "Record layout does not have C++ specific info!");
0282     return CXXInfo->HasOwnVFPtr;
0283   }
0284 
0285   /// hasVFPtr - Does this class have a virtual function table pointer
0286   /// that can be extended by a derived class?  This is synonymous with
0287   /// this class having a VFPtr at offset zero.
0288   bool hasExtendableVFPtr() const {
0289     assert(CXXInfo && "Record layout does not have C++ specific info!");
0290     return CXXInfo->HasExtendableVFPtr;
0291   }
0292 
0293   /// hasOwnVBPtr - Does this class provide its own virtual-base
0294   /// table pointer, rather than inheriting one from a primary base
0295   /// class?
0296   ///
0297   /// This implies that the ABI has no primary base class, meaning
0298   /// that it has no base classes that are suitable under the conditions
0299   /// of the ABI.
0300   bool hasOwnVBPtr() const {
0301     assert(CXXInfo && "Record layout does not have C++ specific info!");
0302     return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
0303   }
0304 
0305   /// hasVBPtr - Does this class have a virtual function table pointer.
0306   bool hasVBPtr() const {
0307     assert(CXXInfo && "Record layout does not have C++ specific info!");
0308     return !CXXInfo->VBPtrOffset.isNegative();
0309   }
0310 
0311   CharUnits getRequiredAlignment() const { return RequiredAlignment; }
0312 
0313   bool endsWithZeroSizedObject() const {
0314     return CXXInfo && CXXInfo->EndsWithZeroSizedObject;
0315   }
0316 
0317   bool leadsWithZeroSizedBase() const {
0318     assert(CXXInfo && "Record layout does not have C++ specific info!");
0319     return CXXInfo->LeadsWithZeroSizedBase;
0320   }
0321 
0322   /// getVBPtrOffset - Get the offset for virtual base table pointer.
0323   /// This is only meaningful with the Microsoft ABI.
0324   CharUnits getVBPtrOffset() const {
0325     assert(CXXInfo && "Record layout does not have C++ specific info!");
0326     return CXXInfo->VBPtrOffset;
0327   }
0328 
0329   const CXXRecordDecl *getBaseSharingVBPtr() const {
0330     assert(CXXInfo && "Record layout does not have C++ specific info!");
0331     return CXXInfo->BaseSharingVBPtr;
0332   }
0333 
0334   const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
0335     assert(CXXInfo && "Record layout does not have C++ specific info!");
0336     return CXXInfo->VBaseOffsets;
0337   }
0338 };
0339 
0340 } // namespace clang
0341 
0342 #endif // LLVM_CLANG_AST_RECORDLAYOUT_H