clang API Documentation

RecordLayout.h
Go to the documentation of this file.
00001 //===--- RecordLayout.h - Layout information for a struct/union -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the RecordLayout interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_AST_RECORDLAYOUT_H
00015 #define LLVM_CLANG_AST_RECORDLAYOUT_H
00016 
00017 #include "clang/AST/CharUnits.h"
00018 #include "clang/AST/DeclCXX.h"
00019 #include "llvm/ADT/DenseMap.h"
00020 
00021 namespace clang {
00022   class ASTContext;
00023   class FieldDecl;
00024   class RecordDecl;
00025   class CXXRecordDecl;
00026 
00027 /// ASTRecordLayout -
00028 /// This class contains layout information for one RecordDecl,
00029 /// which is a struct/union/class.  The decl represented must be a definition,
00030 /// not a forward declaration.
00031 /// This class is also used to contain layout information for one
00032 /// ObjCInterfaceDecl. FIXME - Find appropriate name.
00033 /// These objects are managed by ASTContext.
00034 class ASTRecordLayout {
00035 public:
00036   struct VBaseInfo {
00037     /// The offset to this virtual base in the complete-object layout
00038     /// of this class.
00039     CharUnits VBaseOffset;
00040 
00041   private:
00042     /// Whether this virtual base requires a vtordisp field in the
00043     /// Microsoft ABI.  These fields are required for certain operations
00044     /// in constructors and destructors.
00045     bool HasVtorDisp;
00046 
00047   public:
00048     bool hasVtorDisp() const { return HasVtorDisp; }
00049 
00050     VBaseInfo() : HasVtorDisp(false) {}
00051 
00052     VBaseInfo(CharUnits VBaseOffset, bool hasVtorDisp) :
00053      VBaseOffset(VBaseOffset), HasVtorDisp(hasVtorDisp) {}
00054   };
00055 
00056   typedef llvm::DenseMap<const CXXRecordDecl *, VBaseInfo>
00057     VBaseOffsetsMapTy;
00058 
00059 private:
00060   /// Size - Size of record in characters.
00061   CharUnits Size;
00062 
00063   /// DataSize - Size of record in characters without tail padding.
00064   CharUnits DataSize;
00065 
00066   // Alignment - Alignment of record in characters.
00067   CharUnits Alignment;
00068 
00069   /// RequiredAlignment - The required alignment of the object.  In the MS-ABI
00070   /// the __declspec(align()) trumps #pramga pack and must always be obeyed.
00071   CharUnits RequiredAlignment;
00072 
00073   /// FieldOffsets - Array of field offsets in bits.
00074   uint64_t *FieldOffsets;
00075 
00076   // FieldCount - Number of fields.
00077   unsigned FieldCount;
00078 
00079   /// CXXRecordLayoutInfo - Contains C++ specific layout information.
00080   struct CXXRecordLayoutInfo {
00081     /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
00082     /// the size of the object without virtual bases.
00083     CharUnits NonVirtualSize;
00084 
00085     /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
00086     /// which is the alignment of the object without virtual bases.
00087     CharUnits NonVirtualAlignment;
00088 
00089     /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
00090     /// (either a base or a member). Will be zero if the class doesn't contain
00091     /// any empty subobjects.
00092     CharUnits SizeOfLargestEmptySubobject;
00093 
00094     /// VBPtrOffset - Virtual base table offset (Microsoft-only).
00095     CharUnits VBPtrOffset;
00096 
00097     /// HasOwnVFPtr - Does this class provide a virtual function table
00098     /// (vtable in Itanium, vftbl in Microsoft) that is independent from
00099     /// its base classes?
00100     bool HasOwnVFPtr : 1;
00101 
00102     /// HasVFPtr - Does this class have a vftable that could be extended by
00103     /// a derived class.  The class may have inherited this pointer from
00104     /// a primary base class.
00105     bool HasExtendableVFPtr : 1;
00106 
00107     /// HasZeroSizedSubObject - True if this class contains a zero sized member
00108     /// or base or a base with a zero sized member or base.  Only used for
00109     /// MS-ABI.
00110     bool HasZeroSizedSubObject : 1;
00111 
00112     /// \brief True if this class is zero sized or first base is zero sized or
00113     /// has this property.  Only used for MS-ABI.
00114     bool LeadsWithZeroSizedBase : 1;
00115 
00116     /// PrimaryBase - The primary base info for this record.
00117     llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
00118 
00119     /// BaseSharingVBPtr - The base we share vbptr with.
00120     const CXXRecordDecl *BaseSharingVBPtr;
00121     
00122     /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
00123     typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
00124     
00125     /// BaseOffsets - Contains a map from base classes to their offset.
00126     BaseOffsetsMapTy BaseOffsets;
00127 
00128     /// VBaseOffsets - Contains a map from vbase classes to their offset.
00129     VBaseOffsetsMapTy VBaseOffsets;
00130   };
00131 
00132   /// CXXInfo - If the record layout is for a C++ record, this will have
00133   /// C++ specific information about the record.
00134   CXXRecordLayoutInfo *CXXInfo;
00135 
00136   friend class ASTContext;
00137 
00138   ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
00139                   CharUnits requiredAlignment,
00140                   CharUnits datasize, const uint64_t *fieldoffsets,
00141                   unsigned fieldcount);
00142 
00143   // Constructor for C++ records.
00144   typedef CXXRecordLayoutInfo::BaseOffsetsMapTy BaseOffsetsMapTy;
00145   ASTRecordLayout(const ASTContext &Ctx,
00146                   CharUnits size, CharUnits alignment,
00147                   CharUnits requiredAlignment,
00148                   bool hasOwnVFPtr, bool hasExtendableVFPtr,
00149                   CharUnits vbptroffset,
00150                   CharUnits datasize,
00151                   const uint64_t *fieldoffsets, unsigned fieldcount,
00152                   CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
00153                   CharUnits SizeOfLargestEmptySubobject,
00154                   const CXXRecordDecl *PrimaryBase,
00155                   bool IsPrimaryBaseVirtual,
00156                   const CXXRecordDecl *BaseSharingVBPtr,
00157                   bool HasZeroSizedSubObject,
00158                   bool LeadsWithZeroSizedBase,
00159                   const BaseOffsetsMapTy& BaseOffsets,
00160                   const VBaseOffsetsMapTy& VBaseOffsets);
00161 
00162   ~ASTRecordLayout() {}
00163 
00164   void Destroy(ASTContext &Ctx);
00165   
00166   ASTRecordLayout(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
00167   void operator=(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
00168 public:
00169 
00170   /// getAlignment - Get the record alignment in characters.
00171   CharUnits getAlignment() const { return Alignment; }
00172 
00173   /// getSize - Get the record size in characters.
00174   CharUnits getSize() const { return Size; }
00175 
00176   /// getFieldCount - Get the number of fields in the layout.
00177   unsigned getFieldCount() const { return FieldCount; }
00178 
00179   /// getFieldOffset - Get the offset of the given field index, in
00180   /// bits.
00181   uint64_t getFieldOffset(unsigned FieldNo) const {
00182     assert (FieldNo < FieldCount && "Invalid Field No");
00183     return FieldOffsets[FieldNo];
00184   }
00185 
00186   /// getDataSize() - Get the record data size, which is the record size
00187   /// without tail padding, in characters.
00188   CharUnits getDataSize() const {
00189     return DataSize;
00190   }
00191 
00192   /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
00193   /// which is the size of the object without virtual bases.
00194   CharUnits getNonVirtualSize() const {
00195     assert(CXXInfo && "Record layout does not have C++ specific info!");
00196 
00197     return CXXInfo->NonVirtualSize;
00198   }
00199 
00200   /// getNonVirtualSize - Get the non-virtual alignment (in chars) of an object,
00201   /// which is the alignment of the object without virtual bases.
00202   CharUnits getNonVirtualAlignment() const {
00203     assert(CXXInfo && "Record layout does not have C++ specific info!");
00204 
00205     return CXXInfo->NonVirtualAlignment;
00206   }
00207 
00208   /// getPrimaryBase - Get the primary base for this record.
00209   const CXXRecordDecl *getPrimaryBase() const {
00210     assert(CXXInfo && "Record layout does not have C++ specific info!");
00211 
00212     return CXXInfo->PrimaryBase.getPointer();
00213   }
00214 
00215   /// isPrimaryBaseVirtual - Get whether the primary base for this record
00216   /// is virtual or not.
00217   bool isPrimaryBaseVirtual() const {
00218     assert(CXXInfo && "Record layout does not have C++ specific info!");
00219 
00220     return CXXInfo->PrimaryBase.getInt();
00221   }
00222 
00223   /// getBaseClassOffset - Get the offset, in chars, for the given base class.
00224   CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
00225     assert(CXXInfo && "Record layout does not have C++ specific info!");
00226     assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
00227 
00228     return CXXInfo->BaseOffsets[Base];
00229   }
00230 
00231   /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
00232   CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
00233     assert(CXXInfo && "Record layout does not have C++ specific info!");
00234     assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
00235 
00236     return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
00237   }
00238 
00239   CharUnits getSizeOfLargestEmptySubobject() const {
00240     assert(CXXInfo && "Record layout does not have C++ specific info!");
00241     return CXXInfo->SizeOfLargestEmptySubobject;
00242   }
00243 
00244   /// hasOwnVFPtr - Does this class provide its own virtual-function
00245   /// table pointer, rather than inheriting one from a primary base
00246   /// class?  If so, it is at offset zero.
00247   ///
00248   /// This implies that the ABI has no primary base class, meaning
00249   /// that it has no base classes that are suitable under the conditions
00250   /// of the ABI.
00251   bool hasOwnVFPtr() const {
00252     assert(CXXInfo && "Record layout does not have C++ specific info!");
00253     return CXXInfo->HasOwnVFPtr;
00254   }
00255 
00256   /// hasVFPtr - Does this class have a virtual function table pointer
00257   /// that can be extended by a derived class?  This is synonymous with
00258   /// this class having a VFPtr at offset zero.
00259   bool hasExtendableVFPtr() const {
00260     assert(CXXInfo && "Record layout does not have C++ specific info!");
00261     return CXXInfo->HasExtendableVFPtr;
00262   }
00263   
00264   /// hasOwnVBPtr - Does this class provide its own virtual-base
00265   /// table pointer, rather than inheriting one from a primary base
00266   /// class?
00267   ///
00268   /// This implies that the ABI has no primary base class, meaning
00269   /// that it has no base classes that are suitable under the conditions
00270   /// of the ABI.
00271   bool hasOwnVBPtr() const {
00272     assert(CXXInfo && "Record layout does not have C++ specific info!");
00273     return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
00274   }
00275 
00276   /// hasVBPtr - Does this class have a virtual function table pointer.
00277   bool hasVBPtr() const {
00278     assert(CXXInfo && "Record layout does not have C++ specific info!");
00279     return !CXXInfo->VBPtrOffset.isNegative();
00280   }
00281 
00282   CharUnits getRequiredAlignment() const {
00283     return RequiredAlignment;
00284   }
00285 
00286   bool hasZeroSizedSubObject() const {
00287     return CXXInfo && CXXInfo->HasZeroSizedSubObject;
00288   }
00289 
00290   bool leadsWithZeroSizedBase() const {
00291     assert(CXXInfo && "Record layout does not have C++ specific info!");
00292     return CXXInfo->LeadsWithZeroSizedBase;
00293   }
00294 
00295   /// getVBPtrOffset - Get the offset for virtual base table pointer.
00296   /// This is only meaningful with the Microsoft ABI.
00297   CharUnits getVBPtrOffset() const {
00298     assert(CXXInfo && "Record layout does not have C++ specific info!");
00299     return CXXInfo->VBPtrOffset;
00300   }
00301 
00302   const CXXRecordDecl *getBaseSharingVBPtr() const {
00303     assert(CXXInfo && "Record layout does not have C++ specific info!");
00304     return CXXInfo->BaseSharingVBPtr;
00305   }
00306 
00307   const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
00308     assert(CXXInfo && "Record layout does not have C++ specific info!");
00309     return CXXInfo->VBaseOffsets;
00310   }
00311 };
00312 
00313 }  // end namespace clang
00314 
00315 #endif