clang API Documentation

CGRecordLayoutBuilder.cpp
Go to the documentation of this file.
00001 //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder  ----*- 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 // Builder implementation for CGRecordLayout objects.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "CGRecordLayout.h"
00015 #include "CGCXXABI.h"
00016 #include "CodeGenTypes.h"
00017 #include "clang/AST/ASTContext.h"
00018 #include "clang/AST/Attr.h"
00019 #include "clang/AST/CXXInheritance.h"
00020 #include "clang/AST/DeclCXX.h"
00021 #include "clang/AST/Expr.h"
00022 #include "clang/AST/RecordLayout.h"
00023 #include "clang/Frontend/CodeGenOptions.h"
00024 #include "llvm/IR/DataLayout.h"
00025 #include "llvm/IR/DerivedTypes.h"
00026 #include "llvm/IR/Type.h"
00027 #include "llvm/Support/Debug.h"
00028 #include "llvm/Support/MathExtras.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 using namespace clang;
00031 using namespace CodeGen;
00032 
00033 namespace {
00034 /// The CGRecordLowering is responsible for lowering an ASTRecordLayout to an
00035 /// llvm::Type.  Some of the lowering is straightforward, some is not.  Here we
00036 /// detail some of the complexities and weirdnesses here.
00037 /// * LLVM does not have unions - Unions can, in theory be represented by any
00038 ///   llvm::Type with correct size.  We choose a field via a specific heuristic
00039 ///   and add padding if necessary.
00040 /// * LLVM does not have bitfields - Bitfields are collected into contiguous
00041 ///   runs and allocated as a single storage type for the run.  ASTRecordLayout
00042 ///   contains enough information to determine where the runs break.  Microsoft
00043 ///   and Itanium follow different rules and use different codepaths.
00044 /// * It is desired that, when possible, bitfields use the appropriate iN type
00045 ///   when lowered to llvm types.  For example unsigned x : 24 gets lowered to
00046 ///   i24.  This isn't always possible because i24 has storage size of 32 bit
00047 ///   and if it is possible to use that extra byte of padding we must use
00048 ///   [i8 x 3] instead of i24.  The function clipTailPadding does this.
00049 ///   C++ examples that require clipping:
00050 ///   struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3
00051 ///   struct A { int a : 24; }; // a must be clipped because a struct like B
00052 //    could exist: struct B : A { char b; }; // b goes at offset 3
00053 /// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized
00054 ///   fields.  The existing asserts suggest that LLVM assumes that *every* field
00055 ///   has an underlying storage type.  Therefore empty structures containing
00056 ///   zero sized subobjects such as empty records or zero sized arrays still get
00057 ///   a zero sized (empty struct) storage type.
00058 /// * Clang reads the complete type rather than the base type when generating
00059 ///   code to access fields.  Bitfields in tail position with tail padding may
00060 ///   be clipped in the base class but not the complete class (we may discover
00061 ///   that the tail padding is not used in the complete class.) However,
00062 ///   because LLVM reads from the complete type it can generate incorrect code
00063 ///   if we do not clip the tail padding off of the bitfield in the complete
00064 ///   layout.  This introduces a somewhat awkward extra unnecessary clip stage.
00065 ///   The location of the clip is stored internally as a sentinal of type
00066 ///   SCISSOR.  If LLVM were updated to read base types (which it probably
00067 ///   should because locations of things such as VBases are bogus in the llvm
00068 ///   type anyway) then we could eliminate the SCISSOR.
00069 /// * Itanium allows nearly empty primary virtual bases.  These bases don't get
00070 ///   get their own storage because they're laid out as part of another base
00071 ///   or at the beginning of the structure.  Determining if a VBase actually
00072 ///   gets storage awkwardly involves a walk of all bases.
00073 /// * VFPtrs and VBPtrs do *not* make a record NotZeroInitializable.
00074 struct CGRecordLowering {
00075   // MemberInfo is a helper structure that contains information about a record
00076   // member.  In additional to the standard member types, there exists a
00077   // sentinal member type that ensures correct rounding.
00078   struct MemberInfo {
00079     CharUnits Offset;
00080     enum InfoKind { VFPtr, VBPtr, Field, Base, VBase, Scissor } Kind;
00081     llvm::Type *Data;
00082     union {
00083       const FieldDecl *FD;
00084       const CXXRecordDecl *RD;
00085     };
00086     MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
00087                const FieldDecl *FD = nullptr)
00088       : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {}
00089     MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
00090                const CXXRecordDecl *RD)
00091       : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {}
00092     // MemberInfos are sorted so we define a < operator.
00093     bool operator <(const MemberInfo& a) const { return Offset < a.Offset; }
00094   };
00095   // The constructor.
00096   CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed);
00097   // Short helper routines.
00098   /// \brief Constructs a MemberInfo instance from an offset and llvm::Type *.
00099   MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) {
00100     return MemberInfo(Offset, MemberInfo::Field, Data);
00101   }
00102   bool useMSABI() {
00103     return Context.getTargetInfo().getCXXABI().isMicrosoft() ||
00104            D->isMsStruct(Context);
00105   }
00106   /// \brief Wraps llvm::Type::getIntNTy with some implicit arguments.
00107   llvm::Type *getIntNType(uint64_t NumBits) {
00108     return llvm::Type::getIntNTy(Types.getLLVMContext(),
00109         (unsigned)llvm::RoundUpToAlignment(NumBits, 8));
00110   }
00111   /// \brief Gets an llvm type of size NumBytes and alignment 1.
00112   llvm::Type *getByteArrayType(CharUnits NumBytes) {
00113     assert(!NumBytes.isZero() && "Empty byte arrays aren't allowed.");
00114     llvm::Type *Type = llvm::Type::getInt8Ty(Types.getLLVMContext());
00115     return NumBytes == CharUnits::One() ? Type :
00116         (llvm::Type *)llvm::ArrayType::get(Type, NumBytes.getQuantity());
00117   }
00118   /// \brief Gets the storage type for a field decl and handles storage
00119   /// for itanium bitfields that are smaller than their declared type.
00120   llvm::Type *getStorageType(const FieldDecl *FD) {
00121     llvm::Type *Type = Types.ConvertTypeForMem(FD->getType());
00122     return useMSABI() || !FD->isBitField() ? Type :
00123         getIntNType(std::min(FD->getBitWidthValue(Context),
00124                              (unsigned)Context.toBits(getSize(Type))));
00125   }
00126   /// \brief Gets the llvm Basesubobject type from a CXXRecordDecl.
00127   llvm::Type *getStorageType(const CXXRecordDecl *RD) {
00128     return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType();
00129   }
00130   CharUnits bitsToCharUnits(uint64_t BitOffset) {
00131     return Context.toCharUnitsFromBits(BitOffset);
00132   }
00133   CharUnits getSize(llvm::Type *Type) {
00134     return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
00135   }
00136   CharUnits getAlignment(llvm::Type *Type) {
00137     return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type));
00138   }
00139   bool isZeroInitializable(const FieldDecl *FD) {
00140     const Type *Type = FD->getType()->getBaseElementTypeUnsafe();
00141     if (const MemberPointerType *MPT = Type->getAs<MemberPointerType>())
00142       return Types.getCXXABI().isZeroInitializable(MPT);
00143     if (const RecordType *RT = Type->getAs<RecordType>())
00144       return isZeroInitializable(RT->getDecl());
00145     return true;
00146   }
00147   bool isZeroInitializable(const RecordDecl *RD) {
00148     return Types.getCGRecordLayout(RD).isZeroInitializable();
00149   }
00150   void appendPaddingBytes(CharUnits Size) {
00151     if (!Size.isZero())
00152       FieldTypes.push_back(getByteArrayType(Size));
00153   }
00154   uint64_t getFieldBitOffset(const FieldDecl *FD) {
00155     return Layout.getFieldOffset(FD->getFieldIndex());
00156   }
00157   // Layout routines.
00158   void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset, 
00159                        llvm::Type *StorageType);
00160   /// \brief Lowers an ASTRecordLayout to a llvm type.
00161   void lower(bool NonVirtualBaseType);
00162   void lowerUnion();
00163   void accumulateFields();
00164   void accumulateBitFields(RecordDecl::field_iterator Field,
00165                         RecordDecl::field_iterator FieldEnd);
00166   void accumulateBases();
00167   void accumulateVPtrs();
00168   void accumulateVBases();
00169   /// \brief Recursively searches all of the bases to find out if a vbase is
00170   /// not the primary vbase of some base class.
00171   bool hasOwnStorage(const CXXRecordDecl *Decl, const CXXRecordDecl *Query);
00172   void calculateZeroInit();
00173   /// \brief Lowers bitfield storage types to I8 arrays for bitfields with tail
00174   /// padding that is or can potentially be used.
00175   void clipTailPadding();
00176   /// \brief Determines if we need a packed llvm struct.
00177   void determinePacked(bool NVBaseType);
00178   /// \brief Inserts padding everwhere it's needed.
00179   void insertPadding();
00180   /// \brief Fills out the structures that are ultimately consumed.
00181   void fillOutputFields();
00182   // Input memoization fields.
00183   CodeGenTypes &Types;
00184   const ASTContext &Context;
00185   const RecordDecl *D;
00186   const CXXRecordDecl *RD;
00187   const ASTRecordLayout &Layout;
00188   const llvm::DataLayout &DataLayout;
00189   // Helpful intermediate data-structures.
00190   std::vector<MemberInfo> Members;
00191   // Output fields, consumed by CodeGenTypes::ComputeRecordLayout.
00192   SmallVector<llvm::Type *, 16> FieldTypes;
00193   llvm::DenseMap<const FieldDecl *, unsigned> Fields;
00194   llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
00195   llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
00196   llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
00197   bool IsZeroInitializable : 1;
00198   bool IsZeroInitializableAsBase : 1;
00199   bool Packed : 1;
00200 private:
00201   CGRecordLowering(const CGRecordLowering &) LLVM_DELETED_FUNCTION;
00202   void operator =(const CGRecordLowering &) LLVM_DELETED_FUNCTION;
00203 };
00204 } // namespace {
00205 
00206 CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D,                                 bool Packed)
00207   : Types(Types), Context(Types.getContext()), D(D),
00208     RD(dyn_cast<CXXRecordDecl>(D)),
00209     Layout(Types.getContext().getASTRecordLayout(D)),
00210     DataLayout(Types.getDataLayout()), IsZeroInitializable(true),
00211     IsZeroInitializableAsBase(true), Packed(Packed) {}
00212 
00213 void CGRecordLowering::setBitFieldInfo(
00214     const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) {
00215   CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()];
00216   Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
00217   Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset));
00218   Info.Size = FD->getBitWidthValue(Context);
00219   Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType);
00220   // Here we calculate the actual storage alignment of the bits.  E.g if we've
00221   // got an alignment >= 2 and the bitfield starts at offset 6 we've got an
00222   // alignment of 2.
00223   Info.StorageAlignment =
00224       Layout.getAlignment().alignmentAtOffset(StartOffset).getQuantity();
00225   if (Info.Size > Info.StorageSize)
00226     Info.Size = Info.StorageSize;
00227   // Reverse the bit offsets for big endian machines. Because we represent
00228   // a bitfield as a single large integer load, we can imagine the bits
00229   // counting from the most-significant-bit instead of the
00230   // least-significant-bit.
00231   if (DataLayout.isBigEndian())
00232     Info.Offset = Info.StorageSize - (Info.Offset + Info.Size);
00233 }
00234 
00235 void CGRecordLowering::lower(bool NVBaseType) {
00236   // The lowering process implemented in this function takes a variety of
00237   // carefully ordered phases.
00238   // 1) Store all members (fields and bases) in a list and sort them by offset.
00239   // 2) Add a 1-byte capstone member at the Size of the structure.
00240   // 3) Clip bitfield storages members if their tail padding is or might be
00241   //    used by another field or base.  The clipping process uses the capstone 
00242   //    by treating it as another object that occurs after the record.
00243   // 4) Determine if the llvm-struct requires packing.  It's important that this
00244   //    phase occur after clipping, because clipping changes the llvm type.
00245   //    This phase reads the offset of the capstone when determining packedness
00246   //    and updates the alignment of the capstone to be equal of the alignment
00247   //    of the record after doing so.
00248   // 5) Insert padding everywhere it is needed.  This phase requires 'Packed' to
00249   //    have been computed and needs to know the alignment of the record in
00250   //    order to understand if explicit tail padding is needed.
00251   // 6) Remove the capstone, we don't need it anymore.
00252   // 7) Determine if this record can be zero-initialized.  This phase could have
00253   //    been placed anywhere after phase 1.
00254   // 8) Format the complete list of members in a way that can be consumed by
00255   //    CodeGenTypes::ComputeRecordLayout.
00256   CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize();
00257   if (D->isUnion())
00258     return lowerUnion();
00259   accumulateFields();
00260   // RD implies C++.
00261   if (RD) {
00262     accumulateVPtrs();
00263     accumulateBases();
00264     if (Members.empty())
00265       return appendPaddingBytes(Size);
00266     if (!NVBaseType)
00267       accumulateVBases();
00268   }
00269   std::stable_sort(Members.begin(), Members.end());
00270   Members.push_back(StorageInfo(Size, getIntNType(8)));
00271   clipTailPadding();
00272   determinePacked(NVBaseType);
00273   insertPadding();
00274   Members.pop_back();
00275   calculateZeroInit();
00276   fillOutputFields();
00277 }
00278 
00279 void CGRecordLowering::lowerUnion() {
00280   CharUnits LayoutSize = Layout.getSize();
00281   llvm::Type *StorageType = nullptr;
00282   bool SeenNamedMember = false;
00283   // Iterate through the fields setting bitFieldInfo and the Fields array. Also
00284   // locate the "most appropriate" storage type.  The heuristic for finding the
00285   // storage type isn't necessary, the first (non-0-length-bitfield) field's
00286   // type would work fine and be simpler but would be different than what we've
00287   // been doing and cause lit tests to change.
00288   for (const auto *Field : D->fields()) {
00289     if (Field->isBitField()) {
00290       // Skip 0 sized bitfields.
00291       if (Field->getBitWidthValue(Context) == 0)
00292         continue;
00293       llvm::Type *FieldType = getStorageType(Field);
00294       if (LayoutSize < getSize(FieldType))
00295         FieldType = getByteArrayType(LayoutSize);
00296       setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
00297     }
00298     Fields[Field->getCanonicalDecl()] = 0;
00299     llvm::Type *FieldType = getStorageType(Field);
00300     // Compute zero-initializable status.
00301     // This union might not be zero initialized: it may contain a pointer to
00302     // data member which might have some exotic initialization sequence.
00303     // If this is the case, then we aught not to try and come up with a "better"
00304     // type, it might not be very easy to come up with a Constant which
00305     // correctly initializes it.
00306     if (!SeenNamedMember && Field->getDeclName()) {
00307       SeenNamedMember = true;
00308       if (!isZeroInitializable(Field)) {
00309         IsZeroInitializable = IsZeroInitializableAsBase = false;
00310         StorageType = FieldType;
00311       }
00312     }
00313     // Because our union isn't zero initializable, we won't be getting a better
00314     // storage type.
00315     if (!IsZeroInitializable)
00316       continue;
00317     // Conditionally update our storage type if we've got a new "better" one.
00318     if (!StorageType ||
00319         getAlignment(FieldType) >  getAlignment(StorageType) ||
00320         (getAlignment(FieldType) == getAlignment(StorageType) &&
00321         getSize(FieldType) > getSize(StorageType)))
00322       StorageType = FieldType;
00323   }
00324   // If we have no storage type just pad to the appropriate size and return.
00325   if (!StorageType)
00326     return appendPaddingBytes(LayoutSize);
00327   // If our storage size was bigger than our required size (can happen in the
00328   // case of packed bitfields on Itanium) then just use an I8 array.
00329   if (LayoutSize < getSize(StorageType))
00330     StorageType = getByteArrayType(LayoutSize);
00331   FieldTypes.push_back(StorageType);
00332   appendPaddingBytes(LayoutSize - getSize(StorageType));
00333   // Set packed if we need it.
00334   if (LayoutSize % getAlignment(StorageType))
00335     Packed = true;
00336 }
00337 
00338 void CGRecordLowering::accumulateFields() {
00339   for (RecordDecl::field_iterator Field = D->field_begin(),
00340                                   FieldEnd = D->field_end();
00341     Field != FieldEnd;)
00342     if (Field->isBitField()) {
00343       RecordDecl::field_iterator Start = Field;
00344       // Iterate to gather the list of bitfields.
00345       for (++Field; Field != FieldEnd && Field->isBitField(); ++Field);
00346       accumulateBitFields(Start, Field);
00347     } else {
00348       Members.push_back(MemberInfo(
00349           bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
00350           getStorageType(*Field), *Field));
00351       ++Field;
00352     }
00353 }
00354 
00355 void
00356 CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field,
00357                                       RecordDecl::field_iterator FieldEnd) {
00358   // Run stores the first element of the current run of bitfields.  FieldEnd is
00359   // used as a special value to note that we don't have a current run.  A
00360   // bitfield run is a contiguous collection of bitfields that can be stored in
00361   // the same storage block.  Zero-sized bitfields and bitfields that would
00362   // cross an alignment boundary break a run and start a new one.
00363   RecordDecl::field_iterator Run = FieldEnd;
00364   // Tail is the offset of the first bit off the end of the current run.  It's
00365   // used to determine if the ASTRecordLayout is treating these two bitfields as
00366   // contiguous.  StartBitOffset is offset of the beginning of the Run.
00367   uint64_t StartBitOffset, Tail = 0;
00368   if (useMSABI()) {
00369     for (; Field != FieldEnd; ++Field) {
00370       uint64_t BitOffset = getFieldBitOffset(*Field);
00371       // Zero-width bitfields end runs.
00372       if (Field->getBitWidthValue(Context) == 0) {
00373         Run = FieldEnd;
00374         continue;
00375       }
00376       llvm::Type *Type = Types.ConvertTypeForMem(Field->getType());
00377       // If we don't have a run yet, or don't live within the previous run's
00378       // allocated storage then we allocate some storage and start a new run.
00379       if (Run == FieldEnd || BitOffset >= Tail) {
00380         Run = Field;
00381         StartBitOffset = BitOffset;
00382         Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type);
00383         // Add the storage member to the record.  This must be added to the
00384         // record before the bitfield members so that it gets laid out before
00385         // the bitfields it contains get laid out.
00386         Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
00387       }
00388       // Bitfields get the offset of their storage but come afterward and remain
00389       // there after a stable sort.
00390       Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
00391                                    MemberInfo::Field, nullptr, *Field));
00392     }
00393     return;
00394   }
00395   for (;;) {
00396     // Check to see if we need to start a new run.
00397     if (Run == FieldEnd) {
00398       // If we're out of fields, return.
00399       if (Field == FieldEnd)
00400         break;
00401       // Any non-zero-length bitfield can start a new run.
00402       if (Field->getBitWidthValue(Context) != 0) {
00403         Run = Field;
00404         StartBitOffset = getFieldBitOffset(*Field);
00405         Tail = StartBitOffset + Field->getBitWidthValue(Context);
00406       }
00407       ++Field;
00408       continue;
00409     }
00410     // Add bitfields to the run as long as they qualify.
00411     if (Field != FieldEnd && Field->getBitWidthValue(Context) != 0 &&
00412         Tail == getFieldBitOffset(*Field)) {
00413       Tail += Field->getBitWidthValue(Context);
00414       ++Field;
00415       continue;
00416     }
00417     // We've hit a break-point in the run and need to emit a storage field.
00418     llvm::Type *Type = getIntNType(Tail - StartBitOffset);
00419     // Add the storage member to the record and set the bitfield info for all of
00420     // the bitfields in the run.  Bitfields get the offset of their storage but
00421     // come afterward and remain there after a stable sort.
00422     Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
00423     for (; Run != Field; ++Run)
00424       Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
00425                                    MemberInfo::Field, nullptr, *Run));
00426     Run = FieldEnd;
00427   }
00428 }
00429 
00430 void CGRecordLowering::accumulateBases() {
00431   // If we've got a primary virtual base, we need to add it with the bases.
00432   if (Layout.isPrimaryBaseVirtual()) {
00433     const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase();
00434     Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base,
00435                                  getStorageType(BaseDecl), BaseDecl));
00436   }
00437   // Accumulate the non-virtual bases.
00438   for (const auto &Base : RD->bases()) {
00439     if (Base.isVirtual())
00440       continue;
00441     const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
00442     if (!BaseDecl->isEmpty())
00443       Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
00444           MemberInfo::Base, getStorageType(BaseDecl), BaseDecl));
00445   }
00446 }
00447 
00448 void CGRecordLowering::accumulateVPtrs() {
00449   if (Layout.hasOwnVFPtr())
00450     Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
00451         llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)->
00452             getPointerTo()->getPointerTo()));
00453   if (Layout.hasOwnVBPtr())
00454     Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
00455         llvm::Type::getInt32PtrTy(Types.getLLVMContext())));
00456 }
00457 
00458 void CGRecordLowering::accumulateVBases() {
00459   CharUnits ScissorOffset = Layout.getNonVirtualSize();
00460   // In the itanium ABI, it's possible to place a vbase at a dsize that is
00461   // smaller than the nvsize.  Here we check to see if such a base is placed
00462   // before the nvsize and set the scissor offset to that, instead of the
00463   // nvsize.
00464   if (!useMSABI())
00465     for (const auto &Base : RD->vbases()) {
00466       const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
00467       if (BaseDecl->isEmpty())
00468         continue;
00469       // If the vbase is a primary virtual base of some base, then it doesn't
00470       // get its own storage location but instead lives inside of that base.
00471       if (Context.isNearlyEmpty(BaseDecl) && !hasOwnStorage(RD, BaseDecl))
00472         continue;
00473       ScissorOffset = std::min(ScissorOffset,
00474                                Layout.getVBaseClassOffset(BaseDecl));
00475     }
00476   Members.push_back(MemberInfo(ScissorOffset, MemberInfo::Scissor, nullptr,
00477                                RD));
00478   for (const auto &Base : RD->vbases()) {
00479     const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
00480     if (BaseDecl->isEmpty())
00481       continue;
00482     CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl);
00483     // If the vbase is a primary virtual base of some base, then it doesn't
00484     // get its own storage location but instead lives inside of that base.
00485     if (!useMSABI() && Context.isNearlyEmpty(BaseDecl) &&
00486         !hasOwnStorage(RD, BaseDecl)) {
00487       Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr,
00488                                    BaseDecl));
00489       continue;
00490     }
00491     // If we've got a vtordisp, add it as a storage type.
00492     if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp())
00493       Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4),
00494                                     getIntNType(32)));
00495     Members.push_back(MemberInfo(Offset, MemberInfo::VBase,
00496                                  getStorageType(BaseDecl), BaseDecl));
00497   }
00498 }
00499 
00500 bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl,
00501                                      const CXXRecordDecl *Query) {
00502   const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl);
00503   if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query)
00504     return false;
00505   for (const auto &Base : Decl->bases())
00506     if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query))
00507       return false;
00508   return true;
00509 }
00510 
00511 void CGRecordLowering::calculateZeroInit() {
00512   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
00513                                                MemberEnd = Members.end();
00514        IsZeroInitializableAsBase && Member != MemberEnd; ++Member) {
00515     if (Member->Kind == MemberInfo::Field) {
00516       if (!Member->FD || isZeroInitializable(Member->FD))
00517         continue;
00518       IsZeroInitializable = IsZeroInitializableAsBase = false;
00519     } else if (Member->Kind == MemberInfo::Base ||
00520                Member->Kind == MemberInfo::VBase) {
00521       if (isZeroInitializable(Member->RD))
00522         continue;
00523       IsZeroInitializable = false;
00524       if (Member->Kind == MemberInfo::Base)
00525         IsZeroInitializableAsBase = false;
00526     }
00527   }
00528 }
00529 
00530 void CGRecordLowering::clipTailPadding() {
00531   std::vector<MemberInfo>::iterator Prior = Members.begin();
00532   CharUnits Tail = getSize(Prior->Data);
00533   for (std::vector<MemberInfo>::iterator Member = Prior + 1,
00534                                          MemberEnd = Members.end();
00535        Member != MemberEnd; ++Member) {
00536     // Only members with data and the scissor can cut into tail padding.
00537     if (!Member->Data && Member->Kind != MemberInfo::Scissor)
00538       continue;
00539     if (Member->Offset < Tail) {
00540       assert(Prior->Kind == MemberInfo::Field && !Prior->FD &&
00541              "Only storage fields have tail padding!");
00542       Prior->Data = getByteArrayType(bitsToCharUnits(llvm::RoundUpToAlignment(
00543           cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8)));
00544     }
00545     if (Member->Data)
00546       Prior = Member;
00547     Tail = Prior->Offset + getSize(Prior->Data);
00548   }
00549 }
00550 
00551 void CGRecordLowering::determinePacked(bool NVBaseType) {
00552   if (Packed)
00553     return;
00554   CharUnits Alignment = CharUnits::One();
00555   CharUnits NVAlignment = CharUnits::One();
00556   CharUnits NVSize =
00557       !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero();
00558   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
00559                                                MemberEnd = Members.end();
00560        Member != MemberEnd; ++Member) {
00561     if (!Member->Data)
00562       continue;
00563     // If any member falls at an offset that it not a multiple of its alignment,
00564     // then the entire record must be packed.
00565     if (Member->Offset % getAlignment(Member->Data))
00566       Packed = true;
00567     if (Member->Offset < NVSize)
00568       NVAlignment = std::max(NVAlignment, getAlignment(Member->Data));
00569     Alignment = std::max(Alignment, getAlignment(Member->Data));
00570   }
00571   // If the size of the record (the capstone's offset) is not a multiple of the
00572   // record's alignment, it must be packed.
00573   if (Members.back().Offset % Alignment)
00574     Packed = true;
00575   // If the non-virtual sub-object is not a multiple of the non-virtual
00576   // sub-object's alignment, it must be packed.  We cannot have a packed
00577   // non-virtual sub-object and an unpacked complete object or vise versa.
00578   if (NVSize % NVAlignment)
00579     Packed = true;
00580   // Update the alignment of the sentinal.
00581   if (!Packed)
00582     Members.back().Data = getIntNType(Context.toBits(Alignment));
00583 }
00584 
00585 void CGRecordLowering::insertPadding() {
00586   std::vector<std::pair<CharUnits, CharUnits> > Padding;
00587   CharUnits Size = CharUnits::Zero();
00588   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
00589                                                MemberEnd = Members.end();
00590        Member != MemberEnd; ++Member) {
00591     if (!Member->Data)
00592       continue;
00593     CharUnits Offset = Member->Offset;
00594     assert(Offset >= Size);
00595     // Insert padding if we need to.
00596     if (Offset != Size.RoundUpToAlignment(Packed ? CharUnits::One() :
00597                                           getAlignment(Member->Data)))
00598       Padding.push_back(std::make_pair(Size, Offset - Size));
00599     Size = Offset + getSize(Member->Data);
00600   }
00601   if (Padding.empty())
00602     return;
00603   // Add the padding to the Members list and sort it.
00604   for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator
00605         Pad = Padding.begin(), PadEnd = Padding.end();
00606         Pad != PadEnd; ++Pad)
00607     Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second)));
00608   std::stable_sort(Members.begin(), Members.end());
00609 }
00610 
00611 void CGRecordLowering::fillOutputFields() {
00612   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
00613                                                MemberEnd = Members.end();
00614        Member != MemberEnd; ++Member) {
00615     if (Member->Data)
00616       FieldTypes.push_back(Member->Data);
00617     if (Member->Kind == MemberInfo::Field) {
00618       if (Member->FD)
00619         Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1;
00620       // A field without storage must be a bitfield.
00621       if (!Member->Data)
00622         setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back());
00623     } else if (Member->Kind == MemberInfo::Base)
00624       NonVirtualBases[Member->RD] = FieldTypes.size() - 1;
00625     else if (Member->Kind == MemberInfo::VBase)
00626       VirtualBases[Member->RD] = FieldTypes.size() - 1;
00627   }
00628 }
00629 
00630 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
00631                                         const FieldDecl *FD,
00632                                         uint64_t Offset, uint64_t Size,
00633                                         uint64_t StorageSize,
00634                                         uint64_t StorageAlignment) {
00635   // This function is vestigial from CGRecordLayoutBuilder days but is still 
00636   // used in GCObjCRuntime.cpp.  That usage has a "fixme" attached to it that
00637   // when addressed will allow for the removal of this function.
00638   llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
00639   CharUnits TypeSizeInBytes =
00640     CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
00641   uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
00642 
00643   bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
00644 
00645   if (Size > TypeSizeInBits) {
00646     // We have a wide bit-field. The extra bits are only used for padding, so
00647     // if we have a bitfield of type T, with size N:
00648     //
00649     // T t : N;
00650     //
00651     // We can just assume that it's:
00652     //
00653     // T t : sizeof(T);
00654     //
00655     Size = TypeSizeInBits;
00656   }
00657 
00658   // Reverse the bit offsets for big endian machines. Because we represent
00659   // a bitfield as a single large integer load, we can imagine the bits
00660   // counting from the most-significant-bit instead of the
00661   // least-significant-bit.
00662   if (Types.getDataLayout().isBigEndian()) {
00663     Offset = StorageSize - (Offset + Size);
00664   }
00665 
00666   return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment);
00667 }
00668 
00669 CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
00670                                                   llvm::StructType *Ty) {
00671   CGRecordLowering Builder(*this, D, /*Packed=*/false);
00672 
00673   Builder.lower(/*NonVirtualBaseType=*/false);
00674 
00675   // If we're in C++, compute the base subobject type.
00676   llvm::StructType *BaseTy = nullptr;
00677   if (isa<CXXRecordDecl>(D) && !D->isUnion() && !D->hasAttr<FinalAttr>()) {
00678     BaseTy = Ty;
00679     if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
00680       CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed);
00681       BaseBuilder.lower(/*NonVirtualBaseType=*/true);
00682       BaseTy = llvm::StructType::create(
00683           getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed);
00684       addRecordTypeName(D, BaseTy, ".base");
00685       // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work
00686       // on both of them with the same index.
00687       assert(Builder.Packed == BaseBuilder.Packed &&
00688              "Non-virtual and complete types must agree on packedness");
00689     }
00690   }
00691 
00692   // Fill in the struct *after* computing the base type.  Filling in the body
00693   // signifies that the type is no longer opaque and record layout is complete,
00694   // but we may need to recursively layout D while laying D out as a base type.
00695   Ty->setBody(Builder.FieldTypes, Builder.Packed);
00696 
00697   CGRecordLayout *RL =
00698     new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
00699                         Builder.IsZeroInitializableAsBase);
00700 
00701   RL->NonVirtualBases.swap(Builder.NonVirtualBases);
00702   RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
00703 
00704   // Add all the field numbers.
00705   RL->FieldInfo.swap(Builder.Fields);
00706 
00707   // Add bitfield info.
00708   RL->BitFields.swap(Builder.BitFields);
00709 
00710   // Dump the layout, if requested.
00711   if (getContext().getLangOpts().DumpRecordLayouts) {
00712     llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
00713     llvm::outs() << "Record: ";
00714     D->dump(llvm::outs());
00715     llvm::outs() << "\nLayout: ";
00716     RL->print(llvm::outs());
00717   }
00718 
00719 #ifndef NDEBUG
00720   // Verify that the computed LLVM struct size matches the AST layout size.
00721   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
00722 
00723   uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
00724   assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
00725          "Type size mismatch!");
00726 
00727   if (BaseTy) {
00728     CharUnits NonVirtualSize  = Layout.getNonVirtualSize();
00729 
00730     uint64_t AlignedNonVirtualTypeSizeInBits = 
00731       getContext().toBits(NonVirtualSize);
00732 
00733     assert(AlignedNonVirtualTypeSizeInBits == 
00734            getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
00735            "Type size mismatch!");
00736   }
00737                                      
00738   // Verify that the LLVM and AST field offsets agree.
00739   llvm::StructType *ST =
00740     dyn_cast<llvm::StructType>(RL->getLLVMType());
00741   const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
00742 
00743   const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
00744   RecordDecl::field_iterator it = D->field_begin();
00745   for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
00746     const FieldDecl *FD = *it;
00747 
00748     // For non-bit-fields, just check that the LLVM struct offset matches the
00749     // AST offset.
00750     if (!FD->isBitField()) {
00751       unsigned FieldNo = RL->getLLVMFieldNo(FD);
00752       assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
00753              "Invalid field offset!");
00754       continue;
00755     }
00756     
00757     // Ignore unnamed bit-fields.
00758     if (!FD->getDeclName())
00759       continue;
00760 
00761     // Don't inspect zero-length bitfields.
00762     if (FD->getBitWidthValue(getContext()) == 0)
00763       continue;
00764 
00765     const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
00766     llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
00767 
00768     // Unions have overlapping elements dictating their layout, but for
00769     // non-unions we can verify that this section of the layout is the exact
00770     // expected size.
00771     if (D->isUnion()) {
00772       // For unions we verify that the start is zero and the size
00773       // is in-bounds. However, on BE systems, the offset may be non-zero, but
00774       // the size + offset should match the storage size in that case as it
00775       // "starts" at the back.
00776       if (getDataLayout().isBigEndian())
00777         assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
00778                Info.StorageSize &&
00779                "Big endian union bitfield does not end at the back");
00780       else
00781         assert(Info.Offset == 0 &&
00782                "Little endian union bitfield with a non-zero offset");
00783       assert(Info.StorageSize <= SL->getSizeInBits() &&
00784              "Union not large enough for bitfield storage");
00785     } else {
00786       assert(Info.StorageSize ==
00787              getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
00788              "Storage size does not match the element type size");
00789     }
00790     assert(Info.Size > 0 && "Empty bitfield!");
00791     assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
00792            "Bitfield outside of its allocated storage");
00793   }
00794 #endif
00795 
00796   return RL;
00797 }
00798 
00799 void CGRecordLayout::print(raw_ostream &OS) const {
00800   OS << "<CGRecordLayout\n";
00801   OS << "  LLVMType:" << *CompleteObjectType << "\n";
00802   if (BaseSubobjectType)
00803     OS << "  NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; 
00804   OS << "  IsZeroInitializable:" << IsZeroInitializable << "\n";
00805   OS << "  BitFields:[\n";
00806 
00807   // Print bit-field infos in declaration order.
00808   std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
00809   for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
00810          it = BitFields.begin(), ie = BitFields.end();
00811        it != ie; ++it) {
00812     const RecordDecl *RD = it->first->getParent();
00813     unsigned Index = 0;
00814     for (RecordDecl::field_iterator
00815            it2 = RD->field_begin(); *it2 != it->first; ++it2)
00816       ++Index;
00817     BFIs.push_back(std::make_pair(Index, &it->second));
00818   }
00819   llvm::array_pod_sort(BFIs.begin(), BFIs.end());
00820   for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
00821     OS.indent(4);
00822     BFIs[i].second->print(OS);
00823     OS << "\n";
00824   }
00825 
00826   OS << "]>\n";
00827 }
00828 
00829 void CGRecordLayout::dump() const {
00830   print(llvm::errs());
00831 }
00832 
00833 void CGBitFieldInfo::print(raw_ostream &OS) const {
00834   OS << "<CGBitFieldInfo"
00835      << " Offset:" << Offset
00836      << " Size:" << Size
00837      << " IsSigned:" << IsSigned
00838      << " StorageSize:" << StorageSize
00839      << " StorageAlignment:" << StorageAlignment << ">";
00840 }
00841 
00842 void CGBitFieldInfo::dump() const {
00843   print(llvm::errs());
00844 }