2010-05-26 13:41:04 +08:00
|
|
|
//=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==//
|
2009-07-19 04:20:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/Attr.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2009-07-19 08:18:47 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-07-19 04:50:59 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-07-19 04:20:21 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2010-05-26 13:41:04 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-07-19 04:20:21 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-04-08 10:59:49 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
2010-05-26 13:41:04 +08:00
|
|
|
#include <map>
|
2009-07-19 04:20:21 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2010-05-26 17:58:31 +08:00
|
|
|
namespace {
|
2010-05-26 23:32:58 +08:00
|
|
|
|
|
|
|
/// EmptySubobjectMap - Keeps track of which empty subobjects exist at different
|
|
|
|
/// offsets while laying out a C++ class.
|
|
|
|
class EmptySubobjectMap {
|
|
|
|
ASTContext &Context;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:32:58 +08:00
|
|
|
/// Class - The class whose empty entries we're keeping track of.
|
|
|
|
const CXXRecordDecl *Class;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
/// EmptyClassOffsets - A map from offsets to empty record decls.
|
|
|
|
typedef llvm::SmallVector<const CXXRecordDecl *, 1> ClassVectorTy;
|
|
|
|
typedef llvm::DenseMap<uint64_t, ClassVectorTy> EmptyClassOffsetsMapTy;
|
|
|
|
EmptyClassOffsetsMapTy EmptyClassOffsets;
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
/// ComputeEmptySubobjectSizes - Compute the size of the largest base or
|
2010-05-26 23:54:25 +08:00
|
|
|
/// member subobject that is empty.
|
|
|
|
void ComputeEmptySubobjectSizes();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
struct BaseInfo {
|
|
|
|
const CXXRecordDecl *Class;
|
|
|
|
bool IsVirtual;
|
|
|
|
|
|
|
|
const CXXRecordDecl *PrimaryVirtualBase;
|
|
|
|
|
|
|
|
llvm::SmallVector<BaseInfo*, 4> Bases;
|
|
|
|
const BaseInfo *Derived;
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, BaseInfo *> VirtualBaseInfo;
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, BaseInfo *> NonVirtualBaseInfo;
|
|
|
|
|
|
|
|
BaseInfo *ComputeBaseInfo(const CXXRecordDecl *RD, bool IsVirtual,
|
|
|
|
const BaseInfo *Derived);
|
|
|
|
void ComputeBaseInfo();
|
|
|
|
|
|
|
|
bool CanPlaceBaseSubobjectAtOffset(const BaseInfo *Info, uint64_t Offset);
|
|
|
|
void UpdateEmptyBaseSubobjects(const BaseInfo *Info, uint64_t Offset);
|
|
|
|
|
2010-05-26 23:32:58 +08:00
|
|
|
public:
|
2010-05-26 23:54:25 +08:00
|
|
|
/// This holds the size of the largest empty subobject (either a base
|
2010-05-27 10:25:46 +08:00
|
|
|
/// or a member). Will be zero if the record being built doesn't contain
|
2010-05-26 23:54:25 +08:00
|
|
|
/// any empty classes.
|
|
|
|
uint64_t SizeOfLargestEmptySubobject;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:32:58 +08:00
|
|
|
EmptySubobjectMap(ASTContext &Context, const CXXRecordDecl *Class)
|
2010-05-27 08:07:01 +08:00
|
|
|
: Context(Context), Class(Class), SizeOfLargestEmptySubobject(0) {
|
|
|
|
ComputeEmptySubobjectSizes();
|
2010-05-27 13:41:06 +08:00
|
|
|
|
|
|
|
ComputeBaseInfo();
|
2010-05-27 08:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CanPlaceBaseAtOffset - Return whether the given base class can be placed
|
|
|
|
/// at the given offset.
|
2010-05-27 10:25:46 +08:00
|
|
|
/// Returns false if placing the record will result in two components
|
2010-05-27 08:07:01 +08:00
|
|
|
/// (direct or indirect) of the same type having the same offset.
|
|
|
|
bool CanPlaceBaseAtOffset(const CXXRecordDecl *RD, bool BaseIsVirtual,
|
|
|
|
uint64_t Offset);
|
2010-05-26 23:32:58 +08:00
|
|
|
};
|
2010-05-26 23:54:25 +08:00
|
|
|
|
|
|
|
void EmptySubobjectMap::ComputeEmptySubobjectSizes() {
|
|
|
|
// Check the bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
|
|
|
|
E = Class->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
uint64_t EmptySize = 0;
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
|
|
|
|
if (BaseDecl->isEmpty()) {
|
|
|
|
// If the class decl is empty, get its size.
|
|
|
|
EmptySize = Layout.getSize();
|
|
|
|
} else {
|
|
|
|
// Otherwise, we get the largest empty subobject for the decl.
|
|
|
|
EmptySize = Layout.getSizeOfLargestEmptySubobject();
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
|
|
|
SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
|
2010-05-26 23:54:25 +08:00
|
|
|
EmptySize);
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:54:25 +08:00
|
|
|
// Check the fields.
|
|
|
|
for (CXXRecordDecl::field_iterator I = Class->field_begin(),
|
|
|
|
E = Class->field_end(); I != E; ++I) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
|
|
|
const RecordType *RT =
|
2010-05-26 23:54:25 +08:00
|
|
|
Context.getBaseElementType(FD->getType())->getAs<RecordType>();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:54:25 +08:00
|
|
|
// We only care about record types.
|
|
|
|
if (!RT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint64_t EmptySize = 0;
|
|
|
|
const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
|
|
|
|
if (MemberDecl->isEmpty()) {
|
|
|
|
// If the class decl is empty, get its size.
|
|
|
|
EmptySize = Layout.getSize();
|
|
|
|
} else {
|
|
|
|
// Otherwise, we get the largest empty subobject for the decl.
|
|
|
|
EmptySize = Layout.getSizeOfLargestEmptySubobject();
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
|
|
|
SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
|
2010-05-26 23:54:25 +08:00
|
|
|
EmptySize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
EmptySubobjectMap::BaseInfo *
|
|
|
|
EmptySubobjectMap::ComputeBaseInfo(const CXXRecordDecl *RD, bool IsVirtual,
|
|
|
|
const BaseInfo *Derived) {
|
|
|
|
BaseInfo *Info;
|
|
|
|
|
|
|
|
if (IsVirtual) {
|
|
|
|
BaseInfo *&InfoSlot = VirtualBaseInfo[RD];
|
|
|
|
if (InfoSlot) {
|
|
|
|
assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
|
|
|
|
return InfoSlot;
|
|
|
|
}
|
|
|
|
|
|
|
|
InfoSlot = new (Context) BaseInfo;
|
|
|
|
Info = InfoSlot;
|
|
|
|
} else {
|
|
|
|
Info = new (Context) BaseInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
Info->Class = RD;
|
|
|
|
Info->IsVirtual = IsVirtual;
|
|
|
|
Info->Derived = Derived;
|
|
|
|
Info->PrimaryVirtualBase = 0;
|
|
|
|
|
|
|
|
if (RD->getNumVBases()) {
|
|
|
|
// Check if this class has a primary virtual base.
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
if (Layout.getPrimaryBaseWasVirtual()) {
|
|
|
|
Info->PrimaryVirtualBase = Layout.getPrimaryBase();
|
|
|
|
assert(Info->PrimaryVirtualBase &&
|
|
|
|
"Didn't have a primary virtual base!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
bool IsVirtual = I->isVirtual();
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
Info->Bases.push_back(ComputeBaseInfo(BaseDecl, IsVirtual, Info));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmptySubobjectMap::ComputeBaseInfo() {
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
|
|
|
|
E = Class->bases_end(); I != E; ++I) {
|
|
|
|
bool IsVirtual = I->isVirtual();
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
BaseInfo *Info = ComputeBaseInfo(BaseDecl, IsVirtual, /*Derived=*/0);
|
|
|
|
if (IsVirtual) {
|
|
|
|
// ComputeBaseInfo has already added this base for us.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the base info to the map of non-virtual bases.
|
|
|
|
assert(!NonVirtualBaseInfo.count(BaseDecl) &&
|
|
|
|
"Non-virtual base already exists!");
|
|
|
|
NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
bool
|
2010-05-27 13:41:06 +08:00
|
|
|
EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseInfo *Info,
|
|
|
|
uint64_t Offset) {
|
|
|
|
// Traverse all non-virtual bases.
|
|
|
|
for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
|
|
|
|
BaseInfo* Base = Info->Bases[I];
|
|
|
|
if (Base->IsVirtual)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
|
|
|
|
uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
|
|
|
|
|
|
|
|
if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Info->PrimaryVirtualBase) {
|
|
|
|
BaseInfo *PrimaryVirtualBaseInfo =
|
|
|
|
VirtualBaseInfo.lookup(Info->PrimaryVirtualBase);
|
|
|
|
assert(PrimaryVirtualBaseInfo && "Didn't find base info!");
|
|
|
|
|
|
|
|
if (Info == PrimaryVirtualBaseInfo->Derived) {
|
|
|
|
if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Member variables.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseInfo *Info,
|
|
|
|
uint64_t Offset) {
|
|
|
|
if (Info->Class->isEmpty()) {
|
|
|
|
// FIXME: Record that there is an empty class at this offset.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse all non-virtual bases.
|
|
|
|
for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
|
|
|
|
BaseInfo* Base = Info->Bases[I];
|
|
|
|
if (Base->IsVirtual)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
|
|
|
|
uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
|
|
|
|
|
|
|
|
UpdateEmptyBaseSubobjects(Base, BaseOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Info->PrimaryVirtualBase) {
|
|
|
|
BaseInfo *PrimaryVirtualBaseInfo =
|
|
|
|
VirtualBaseInfo.lookup(Info->PrimaryVirtualBase);
|
|
|
|
assert(PrimaryVirtualBaseInfo && "Didn't find base info!");
|
|
|
|
|
|
|
|
if (Info == PrimaryVirtualBaseInfo->Derived)
|
|
|
|
UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Member variables.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EmptySubobjectMap::CanPlaceBaseAtOffset(const CXXRecordDecl *RD,
|
|
|
|
bool BaseIsVirtual,
|
|
|
|
uint64_t Offset) {
|
2010-05-27 08:07:01 +08:00
|
|
|
// If we know this class doesn't have any empty subobjects we don't need to
|
|
|
|
// bother checking.
|
|
|
|
if (!SizeOfLargestEmptySubobject)
|
|
|
|
return true;
|
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
BaseInfo *Info;
|
|
|
|
|
|
|
|
if (BaseIsVirtual)
|
|
|
|
Info = VirtualBaseInfo.lookup(RD);
|
|
|
|
else
|
|
|
|
Info = NonVirtualBaseInfo.lookup(RD);
|
|
|
|
|
|
|
|
if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UpdateEmptyBaseSubobjects(Info, Offset);
|
2010-05-27 08:07:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
class RecordLayoutBuilder {
|
2010-05-26 13:41:04 +08:00
|
|
|
// FIXME: Remove this and make the appropriate fields public.
|
|
|
|
friend class clang::ASTContext;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
ASTContext &Context;
|
|
|
|
|
2010-05-26 23:32:58 +08:00
|
|
|
EmptySubobjectMap *EmptySubobjects;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// Size - The current size of the record layout.
|
|
|
|
uint64_t Size;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// Alignment - The current alignment of the record layout.
|
|
|
|
unsigned Alignment;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
llvm::SmallVector<uint64_t, 16> FieldOffsets;
|
|
|
|
|
|
|
|
/// Packed - Whether the record is packed or not.
|
|
|
|
bool Packed;
|
|
|
|
|
|
|
|
/// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
|
|
|
|
/// this contains the number of bits in the last byte that can be used for
|
|
|
|
/// an adjacent bitfield if necessary.
|
|
|
|
unsigned char UnfilledBitsInLastByte;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// MaxFieldAlignment - The maximum allowed field alignment. This is set by
|
2010-05-27 10:25:46 +08:00
|
|
|
/// #pragma pack.
|
2010-05-26 13:41:04 +08:00
|
|
|
unsigned MaxFieldAlignment;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// DataSize - The data size of the record being laid out.
|
|
|
|
uint64_t DataSize;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
bool IsUnion;
|
|
|
|
|
|
|
|
uint64_t NonVirtualSize;
|
|
|
|
unsigned NonVirtualAlignment;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// PrimaryBase - the primary base class (if one exists) of the class
|
|
|
|
/// we're laying out.
|
|
|
|
const CXXRecordDecl *PrimaryBase;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
|
|
|
|
/// out is virtual.
|
|
|
|
bool PrimaryBaseIsVirtual;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, uint64_t> BaseOffsetsMapTy;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// Bases - base classes and their offsets in the record.
|
|
|
|
BaseOffsetsMapTy Bases;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
// VBases - virtual base classes and their offsets in the record.
|
|
|
|
BaseOffsetsMapTy VBases;
|
|
|
|
|
|
|
|
/// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
|
|
|
|
/// primary base classes for some other direct or indirect base class.
|
|
|
|
llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
|
|
|
|
/// inheritance graph order. Used for determining the primary base class.
|
|
|
|
const CXXRecordDecl *FirstNearlyEmptyVBase;
|
|
|
|
|
|
|
|
/// VisitedVirtualBases - A set of all the visited virtual bases, used to
|
|
|
|
/// avoid visiting virtual bases more than once.
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// EmptyClassOffsets - A map from offsets to empty record decls.
|
|
|
|
typedef std::multimap<uint64_t, const CXXRecordDecl *> EmptyClassOffsetsTy;
|
|
|
|
EmptyClassOffsetsTy EmptyClassOffsets;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
RecordLayoutBuilder(ASTContext &Context, EmptySubobjectMap *EmptySubobjects)
|
|
|
|
: Context(Context), EmptySubobjects(EmptySubobjects), Size(0), Alignment(8),
|
2010-05-27 10:25:46 +08:00
|
|
|
Packed(false), UnfilledBitsInLastByte(0), MaxFieldAlignment(0), DataSize(0),
|
|
|
|
IsUnion(false), NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0),
|
2010-05-26 23:54:25 +08:00
|
|
|
PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { }
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
void Layout(const RecordDecl *D);
|
2010-05-26 23:10:00 +08:00
|
|
|
void Layout(const CXXRecordDecl *D);
|
2010-05-26 13:41:04 +08:00
|
|
|
void Layout(const ObjCInterfaceDecl *D);
|
|
|
|
|
|
|
|
void LayoutFields(const RecordDecl *D);
|
|
|
|
void LayoutField(const FieldDecl *D);
|
|
|
|
void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize);
|
|
|
|
void LayoutBitField(const FieldDecl *D);
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
/// ComputeEmptySubobjectSizes - Compute the size of the largest base or
|
2010-05-26 13:41:04 +08:00
|
|
|
/// member subobject that is empty.
|
|
|
|
void ComputeEmptySubobjectSizes(const CXXRecordDecl *RD);
|
|
|
|
|
|
|
|
/// DeterminePrimaryBase - Determine the primary base of the given class.
|
|
|
|
void DeterminePrimaryBase(const CXXRecordDecl *RD);
|
|
|
|
|
|
|
|
void SelectPrimaryVBase(const CXXRecordDecl *RD);
|
2010-05-27 10:25:46 +08:00
|
|
|
|
|
|
|
/// IdentifyPrimaryBases - Identify all virtual base classes, direct or
|
|
|
|
/// indirect, that are primary base classes for some other direct or indirect
|
2010-05-26 13:41:04 +08:00
|
|
|
/// base class.
|
|
|
|
void IdentifyPrimaryBases(const CXXRecordDecl *RD);
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
bool IsNearlyEmpty(const CXXRecordDecl *RD) const;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
|
|
|
/// LayoutNonVirtualBases - Determines the primary base class (if any) and
|
2010-05-26 13:41:04 +08:00
|
|
|
/// lays it out. Will then proceed to lay out all non-virtual base clasess.
|
|
|
|
void LayoutNonVirtualBases(const CXXRecordDecl *RD);
|
|
|
|
|
|
|
|
/// LayoutNonVirtualBase - Lays out a single non-virtual base.
|
2010-05-27 08:07:01 +08:00
|
|
|
void LayoutNonVirtualBase(const CXXRecordDecl *Base);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
|
|
|
void AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
|
|
|
|
const CXXRecordDecl *MostDerivedClass);
|
|
|
|
|
|
|
|
/// LayoutVirtualBases - Lays out all the virtual bases.
|
|
|
|
void LayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *MostDerivedClass);
|
|
|
|
|
|
|
|
/// LayoutVirtualBase - Lays out a single virtual base.
|
2010-05-27 08:07:01 +08:00
|
|
|
void LayoutVirtualBase(const CXXRecordDecl *Base);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
/// LayoutBase - Will lay out a base and return the offset where it was
|
2010-05-26 13:41:04 +08:00
|
|
|
/// placed, in bits.
|
2010-05-27 08:07:01 +08:00
|
|
|
uint64_t LayoutBase(const CXXRecordDecl *Base, bool BaseIsVirtual);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
|
|
|
/// canPlaceRecordAtOffset - Return whether a record (either a base class
|
2010-05-27 10:25:46 +08:00
|
|
|
/// or a field) can be placed at the given offset.
|
|
|
|
/// Returns false if placing the record will result in two components
|
2010-05-26 13:41:04 +08:00
|
|
|
/// (direct or indirect) of the same type having the same offset.
|
|
|
|
bool canPlaceRecordAtOffset(const CXXRecordDecl *RD, uint64_t Offset,
|
|
|
|
bool CheckVBases) const;
|
|
|
|
|
|
|
|
/// canPlaceFieldAtOffset - Return whether a field can be placed at the given
|
|
|
|
/// offset.
|
|
|
|
bool canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const;
|
|
|
|
|
|
|
|
/// UpdateEmptyClassOffsets - Called after a record (either a base class
|
|
|
|
/// or a field) has been placed at the given offset. Will update the
|
|
|
|
/// EmptyClassOffsets map if the class is empty or has any empty bases or
|
|
|
|
/// fields.
|
|
|
|
void UpdateEmptyClassOffsets(const CXXRecordDecl *RD, uint64_t Offset,
|
|
|
|
bool UpdateVBases);
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
/// UpdateEmptyClassOffsets - Called after a field has been placed at the
|
2010-05-26 13:41:04 +08:00
|
|
|
/// given offset.
|
|
|
|
void UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t Offset);
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
/// InitializeLayout - Initialize record layout for the given record decl.
|
|
|
|
void InitializeLayout(const RecordDecl *D);
|
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// FinishLayout - Finalize record layout. Adjust record size based on the
|
|
|
|
/// alignment.
|
|
|
|
void FinishLayout();
|
|
|
|
|
|
|
|
void UpdateAlignment(unsigned NewAlignment);
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
|
2010-05-26 13:41:04 +08:00
|
|
|
public:
|
|
|
|
static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
|
|
|
|
};
|
2010-05-26 17:58:31 +08:00
|
|
|
} // end anonymous namespace
|
2010-05-26 13:41:04 +08:00
|
|
|
|
2009-08-06 06:37:18 +08:00
|
|
|
/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
|
|
|
|
/// no other data.
|
2010-05-26 13:58:59 +08:00
|
|
|
bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
|
2009-08-06 06:37:18 +08:00
|
|
|
// FIXME: Audit the corners
|
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return false;
|
2010-04-16 23:07:51 +08:00
|
|
|
const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD);
|
|
|
|
if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0))
|
2009-08-06 06:37:18 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
|
2010-04-08 10:59:49 +08:00
|
|
|
const ASTRecordLayout::PrimaryBaseInfo &BaseInfo =
|
2010-04-16 23:07:51 +08:00
|
|
|
Context.getASTRecordLayout(RD).getPrimaryBaseInfo();
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-22 11:02:06 +08:00
|
|
|
// If the record has a primary base class that is virtual, add it to the set
|
|
|
|
// of primary bases.
|
2009-11-28 06:14:40 +08:00
|
|
|
if (BaseInfo.isVirtual())
|
|
|
|
IndirectPrimaryBases.insert(BaseInfo.getBase());
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-22 11:02:06 +08:00
|
|
|
// Now traverse all bases and find primary bases for them.
|
2009-08-06 06:37:18 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
2009-10-26 01:03:50 +08:00
|
|
|
assert(!i->getType()->isDependentType() &&
|
|
|
|
"Cannot layout class with dependent bases.");
|
2009-09-09 23:08:12 +08:00
|
|
|
const CXXRecordDecl *Base =
|
2009-08-11 12:03:59 +08:00
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-08-11 12:03:59 +08:00
|
|
|
// Only bases with virtual bases participate in computing the
|
|
|
|
// indirect primary virtual base classes.
|
2009-08-14 06:53:07 +08:00
|
|
|
if (Base->getNumVBases())
|
2009-09-22 11:02:06 +08:00
|
|
|
IdentifyPrimaryBases(Base);
|
2009-08-06 06:37:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-22 11:02:06 +08:00
|
|
|
void
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
|
2010-03-11 11:39:12 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
2010-03-11 11:39:12 +08:00
|
|
|
assert(!I->getType()->isDependentType() &&
|
2009-10-26 01:03:50 +08:00
|
|
|
"Cannot layout class with dependent bases.");
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const CXXRecordDecl *Base =
|
2010-03-11 11:39:12 +08:00
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
2010-03-11 08:15:35 +08:00
|
|
|
|
2010-03-11 11:39:12 +08:00
|
|
|
// Check if this is a nearly empty virtual base.
|
|
|
|
if (I->isVirtual() && IsNearlyEmpty(Base)) {
|
|
|
|
// If it's not an indirect primary base, then we've found our primary
|
|
|
|
// base.
|
2009-09-22 11:02:06 +08:00
|
|
|
if (!IndirectPrimaryBases.count(Base)) {
|
2010-05-26 13:20:58 +08:00
|
|
|
PrimaryBase = Base;
|
|
|
|
PrimaryBaseIsVirtual = true;
|
2009-08-13 05:50:08 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 11:39:12 +08:00
|
|
|
// Is this the first nearly empty virtual base?
|
|
|
|
if (!FirstNearlyEmptyVBase)
|
|
|
|
FirstNearlyEmptyVBase = Base;
|
2009-08-13 05:50:08 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
SelectPrimaryVBase(Base);
|
2010-05-26 13:20:58 +08:00
|
|
|
if (PrimaryBase)
|
2010-02-15 12:28:35 +08:00
|
|
|
return;
|
2009-08-13 05:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
/// DeterminePrimaryBase - Determine the primary base of the given class.
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
|
2010-03-11 08:15:35 +08:00
|
|
|
// If the class isn't dynamic, it won't have a primary base.
|
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-22 11:02:06 +08:00
|
|
|
// Compute all the primary virtual bases for all of our direct and
|
2009-08-14 07:26:06 +08:00
|
|
|
// indirect bases, and record all their primary virtual base classes.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
2009-10-26 01:03:50 +08:00
|
|
|
assert(!i->getType()->isDependentType() &&
|
2010-03-11 08:15:35 +08:00
|
|
|
"Cannot lay out class with dependent bases.");
|
2009-09-09 23:08:12 +08:00
|
|
|
const CXXRecordDecl *Base =
|
2009-08-14 07:26:06 +08:00
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
2009-09-22 11:02:06 +08:00
|
|
|
IdentifyPrimaryBases(Base);
|
2009-08-14 07:26:06 +08:00
|
|
|
}
|
|
|
|
|
2010-04-08 10:59:49 +08:00
|
|
|
// If the record has a dynamic base class, attempt to choose a primary base
|
|
|
|
// class. It is the first (in direct base class order) non-virtual dynamic
|
2009-09-22 11:02:06 +08:00
|
|
|
// base class, if one exists.
|
2009-08-06 06:37:18 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
2009-11-28 06:05:05 +08:00
|
|
|
// Ignore virtual bases.
|
|
|
|
if (i->isVirtual())
|
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-28 06:05:05 +08:00
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
if (Base->isDynamicClass()) {
|
|
|
|
// We found it.
|
2010-05-26 13:20:58 +08:00
|
|
|
PrimaryBase = Base;
|
|
|
|
PrimaryBaseIsVirtual = false;
|
2009-11-28 06:05:05 +08:00
|
|
|
return;
|
2009-08-06 06:37:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it is the first nearly empty virtual base that is not an
|
2009-08-11 12:03:59 +08:00
|
|
|
// indirect primary virtual base class, if one exists.
|
2010-03-11 08:15:35 +08:00
|
|
|
if (RD->getNumVBases() != 0) {
|
|
|
|
SelectPrimaryVBase(RD);
|
2010-05-26 13:20:58 +08:00
|
|
|
if (PrimaryBase)
|
2010-03-11 08:15:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-08-06 06:37:18 +08:00
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
// Otherwise, it is the first nearly empty virtual base that is not an
|
|
|
|
// indirect primary virtual base class, if one exists.
|
|
|
|
if (FirstNearlyEmptyVBase) {
|
2010-05-26 13:20:58 +08:00
|
|
|
PrimaryBase = FirstNearlyEmptyVBase;
|
|
|
|
PrimaryBaseIsVirtual = true;
|
2009-08-06 06:37:18 +08:00
|
|
|
return;
|
2010-03-11 08:15:35 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
// Otherwise there is no primary base class.
|
2010-05-26 13:20:58 +08:00
|
|
|
assert(!PrimaryBase && "Should not get here with a primary base!");
|
2010-03-11 08:15:35 +08:00
|
|
|
|
|
|
|
// Allocate the virtual table pointer at offset zero.
|
|
|
|
assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
// Update the size.
|
2010-04-16 23:07:51 +08:00
|
|
|
Size += Context.Target.getPointerWidth(0);
|
2010-03-11 08:15:35 +08:00
|
|
|
DataSize = Size;
|
|
|
|
|
|
|
|
// Update the alignment.
|
2010-04-16 23:07:51 +08:00
|
|
|
UpdateAlignment(Context.Target.getPointerAlign(0));
|
2009-08-06 06:37:18 +08:00
|
|
|
}
|
|
|
|
|
2010-03-11 06:21:28 +08:00
|
|
|
void
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
|
2010-03-11 08:15:35 +08:00
|
|
|
// First, determine the primary base class.
|
|
|
|
DeterminePrimaryBase(RD);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
// If we have a primary base class, lay it out.
|
2010-05-26 13:20:58 +08:00
|
|
|
if (PrimaryBase) {
|
|
|
|
if (PrimaryBaseIsVirtual) {
|
2010-03-11 08:15:35 +08:00
|
|
|
// We have a virtual primary base, insert it as an indirect primary base.
|
2010-05-26 13:20:58 +08:00
|
|
|
IndirectPrimaryBases.insert(PrimaryBase);
|
2010-03-11 13:42:17 +08:00
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
assert(!VisitedVirtualBases.count(PrimaryBase) &&
|
2010-05-26 13:20:58 +08:00
|
|
|
"vbase already visited!");
|
|
|
|
VisitedVirtualBases.insert(PrimaryBase);
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:20:58 +08:00
|
|
|
LayoutVirtualBase(PrimaryBase);
|
2010-03-11 08:15:35 +08:00
|
|
|
} else
|
2010-05-26 13:20:58 +08:00
|
|
|
LayoutNonVirtualBase(PrimaryBase);
|
2010-03-11 08:15:35 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
// Now lay out the non-virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
2010-03-11 08:15:35 +08:00
|
|
|
|
|
|
|
// Ignore virtual bases.
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// Skip the primary base.
|
2010-05-26 13:31:23 +08:00
|
|
|
if (Base == PrimaryBase && !PrimaryBaseIsVirtual)
|
2010-03-11 08:15:35 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Lay out the base.
|
|
|
|
LayoutNonVirtualBase(Base);
|
2010-03-11 06:21:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
void RecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *Base) {
|
2010-03-11 06:26:24 +08:00
|
|
|
// Layout the base.
|
2010-05-27 08:07:01 +08:00
|
|
|
uint64_t Offset = LayoutBase(Base, /*BaseIsVirtual=*/false);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 06:26:24 +08:00
|
|
|
// Add its base class offset.
|
2010-05-27 08:07:01 +08:00
|
|
|
if (!Bases.insert(std::make_pair(Base, Offset)).second)
|
2010-03-11 12:10:39 +08:00
|
|
|
assert(false && "Added same base offset more than once!");
|
2010-03-11 06:21:28 +08:00
|
|
|
}
|
2009-11-05 12:02:15 +08:00
|
|
|
|
2010-04-16 00:12:58 +08:00
|
|
|
void
|
2010-05-27 10:25:46 +08:00
|
|
|
RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD,
|
2010-04-16 00:12:58 +08:00
|
|
|
uint64_t Offset,
|
|
|
|
const CXXRecordDecl *MostDerivedClass) {
|
|
|
|
// We already have the offset for the primary base of the most derived class.
|
|
|
|
if (RD != MostDerivedClass) {
|
2010-04-16 23:07:51 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2010-04-16 00:12:58 +08:00
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
|
|
|
// If this is a primary virtual base and we haven't seen it before, add it.
|
|
|
|
if (PrimaryBase && Layout.getPrimaryBaseWasVirtual() &&
|
|
|
|
!VBases.count(PrimaryBase))
|
|
|
|
VBases.insert(std::make_pair(PrimaryBase, Offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
assert(!I->getType()->isDependentType() &&
|
|
|
|
"Cannot layout class with dependent bases.");
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 00:12:58 +08:00
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
if (!BaseDecl->getNumVBases()) {
|
|
|
|
// This base isn't interesting since it doesn't have any virtual bases.
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 00:12:58 +08:00
|
|
|
// Compute the offset of this base.
|
|
|
|
uint64_t BaseOffset;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 00:12:58 +08:00
|
|
|
if (I->isVirtual()) {
|
|
|
|
// If we don't know this vbase yet, don't visit it. It will be visited
|
|
|
|
// later.
|
|
|
|
if (!VBases.count(BaseDecl)) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 00:12:58 +08:00
|
|
|
// Check if we've already visited this base.
|
|
|
|
if (!VisitedVirtualBases.insert(BaseDecl))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We want the vbase offset from the class we're currently laying out.
|
|
|
|
BaseOffset = VBases[BaseDecl];
|
|
|
|
} else if (RD == MostDerivedClass) {
|
|
|
|
// We want the base offset from the class we're currently laying out.
|
|
|
|
assert(Bases.count(BaseDecl) && "Did not find base!");
|
|
|
|
BaseOffset = Bases[BaseDecl];
|
|
|
|
} else {
|
2010-04-16 23:07:51 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2010-04-16 00:12:58 +08:00
|
|
|
BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddPrimaryVirtualBaseOffsets(BaseDecl, BaseOffset, MostDerivedClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-08 10:59:49 +08:00
|
|
|
void
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
|
2010-04-16 00:12:58 +08:00
|
|
|
const CXXRecordDecl *MostDerivedClass) {
|
2010-03-11 12:33:54 +08:00
|
|
|
const CXXRecordDecl *PrimaryBase;
|
2010-04-11 02:42:27 +08:00
|
|
|
bool PrimaryBaseIsVirtual;
|
2010-03-11 13:42:17 +08:00
|
|
|
|
2010-04-11 02:42:27 +08:00
|
|
|
if (MostDerivedClass == RD) {
|
2010-05-26 13:20:58 +08:00
|
|
|
PrimaryBase = this->PrimaryBase;
|
|
|
|
PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
|
2010-04-11 02:42:27 +08:00
|
|
|
} else {
|
2010-04-16 23:07:51 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2010-03-11 12:33:54 +08:00
|
|
|
PrimaryBase = Layout.getPrimaryBase();
|
2010-04-11 02:42:27 +08:00
|
|
|
PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual();
|
|
|
|
}
|
|
|
|
|
2010-03-11 12:24:02 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
assert(!I->getType()->isDependentType() &&
|
2009-10-26 01:03:50 +08:00
|
|
|
"Cannot layout class with dependent bases.");
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const CXXRecordDecl *Base =
|
2010-03-11 12:24:02 +08:00
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
if (I->isVirtual()) {
|
2010-04-11 02:42:27 +08:00
|
|
|
if (PrimaryBase != Base || !PrimaryBaseIsVirtual) {
|
|
|
|
bool IndirectPrimaryBase = IndirectPrimaryBases.count(Base);
|
|
|
|
|
|
|
|
// Only lay out the virtual base if it's not an indirect primary base.
|
|
|
|
if (!IndirectPrimaryBase) {
|
|
|
|
// Only visit virtual bases once.
|
|
|
|
if (!VisitedVirtualBases.insert(Base))
|
|
|
|
continue;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-03-11 13:42:17 +08:00
|
|
|
LayoutVirtualBase(Base);
|
2010-03-11 12:10:39 +08:00
|
|
|
}
|
2009-11-05 12:02:15 +08:00
|
|
|
}
|
2009-08-17 03:04:13 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 12:24:02 +08:00
|
|
|
if (!Base->getNumVBases()) {
|
|
|
|
// This base isn't interesting since it doesn't have any virtual bases.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-16 00:12:58 +08:00
|
|
|
LayoutVirtualBases(Base, MostDerivedClass);
|
2009-08-06 21:41:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
void RecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *Base) {
|
2010-03-11 06:26:24 +08:00
|
|
|
// Layout the base.
|
2010-05-27 08:07:01 +08:00
|
|
|
uint64_t Offset = LayoutBase(Base, /*BaseIsVirtual=*/true);
|
2010-03-11 06:26:24 +08:00
|
|
|
|
|
|
|
// Add its base class offset.
|
2010-05-27 08:07:01 +08:00
|
|
|
if (!VBases.insert(std::make_pair(Base, Offset)).second)
|
2010-03-11 12:10:39 +08:00
|
|
|
assert(false && "Added same vbase offset more than once!");
|
2010-03-11 06:21:28 +08:00
|
|
|
}
|
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
uint64_t RecordLayoutBuilder::LayoutBase(const CXXRecordDecl *Base,
|
|
|
|
bool BaseIsVirtual) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base);
|
2010-03-11 06:21:28 +08:00
|
|
|
|
|
|
|
// If we have an empty base class, try to place it at offset 0.
|
2010-05-27 10:25:46 +08:00
|
|
|
if (Base->isEmpty() &&
|
2010-05-27 08:07:01 +08:00
|
|
|
EmptySubobjects->CanPlaceBaseAtOffset(Base, BaseIsVirtual, 0) &&
|
|
|
|
canPlaceRecordAtOffset(Base, 0, /*CheckVBases=*/false)) {
|
2010-03-11 06:21:28 +08:00
|
|
|
// We were able to place the class at offset 0.
|
2010-05-27 08:07:01 +08:00
|
|
|
UpdateEmptyClassOffsets(Base, 0, /*UpdateVBases=*/false);
|
2010-03-11 06:21:28 +08:00
|
|
|
|
2010-05-09 06:35:05 +08:00
|
|
|
Size = std::max(Size, Layout.getSize());
|
2010-03-11 06:21:28 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 06:35:05 +08:00
|
|
|
unsigned BaseAlign = Layout.getNonVirtualAlign();
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 06:21:28 +08:00
|
|
|
// Round up the current record size to the base's alignment boundary.
|
|
|
|
uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 06:21:28 +08:00
|
|
|
// Try to place the base.
|
|
|
|
while (true) {
|
2010-05-27 08:07:01 +08:00
|
|
|
if (EmptySubobjects->CanPlaceBaseAtOffset(Base, BaseIsVirtual, Offset) &&
|
|
|
|
canPlaceRecordAtOffset(Base, Offset, /*CheckVBases=*/false))
|
2010-03-11 06:21:28 +08:00
|
|
|
break;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 06:21:28 +08:00
|
|
|
Offset += BaseAlign;
|
|
|
|
}
|
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
if (!Base->isEmpty()) {
|
2010-03-11 06:21:28 +08:00
|
|
|
// Update the data size.
|
2010-05-09 06:35:05 +08:00
|
|
|
DataSize = Offset + Layout.getNonVirtualSize();
|
2010-03-11 06:21:28 +08:00
|
|
|
|
|
|
|
Size = std::max(Size, DataSize);
|
|
|
|
} else
|
2010-05-09 06:35:05 +08:00
|
|
|
Size = std::max(Size, Offset + Layout.getSize());
|
2010-03-11 06:21:28 +08:00
|
|
|
|
|
|
|
// Remember max struct/class alignment.
|
|
|
|
UpdateAlignment(BaseAlign);
|
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
UpdateEmptyClassOffsets(Base, Offset, /*UpdateVBases=*/false);
|
2010-03-11 06:21:28 +08:00
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
bool
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD,
|
2010-05-27 10:25:46 +08:00
|
|
|
uint64_t Offset,
|
2010-05-10 23:26:14 +08:00
|
|
|
bool CheckVBases) const {
|
2009-09-24 11:22:10 +08:00
|
|
|
// Look for an empty class with the same type at the same offset.
|
2010-04-08 10:59:49 +08:00
|
|
|
for (EmptyClassOffsetsTy::const_iterator I =
|
|
|
|
EmptyClassOffsets.lower_bound(Offset),
|
|
|
|
E = EmptyClassOffsets.upper_bound(Offset); I != E; ++I) {
|
|
|
|
|
2009-09-24 11:22:10 +08:00
|
|
|
if (I->second == RD)
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 13:03:38 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2009-09-24 13:21:31 +08:00
|
|
|
|
|
|
|
// Check bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
2009-10-26 01:03:50 +08:00
|
|
|
assert(!I->getType()->isDependentType() &&
|
|
|
|
"Cannot layout class with dependent bases.");
|
2009-09-24 13:21:31 +08:00
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 13:03:38 +08:00
|
|
|
const CXXRecordDecl *BaseDecl =
|
2009-09-24 13:21:31 +08:00
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-05-09 13:03:38 +08:00
|
|
|
uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-10 23:26:14 +08:00
|
|
|
if (!canPlaceRecordAtOffset(BaseDecl, Offset + BaseOffset,
|
|
|
|
/*CheckVBases=*/false))
|
2009-09-24 13:21:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-25 23:39:00 +08:00
|
|
|
// Check fields.
|
|
|
|
unsigned FieldNo = 0;
|
2010-04-08 10:59:49 +08:00
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
|
2009-09-25 23:39:00 +08:00
|
|
|
I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 13:03:38 +08:00
|
|
|
uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-25 23:39:00 +08:00
|
|
|
if (!canPlaceFieldAtOffset(FD, Offset + FieldOffset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:26:14 +08:00
|
|
|
if (CheckVBases) {
|
|
|
|
// FIXME: virtual bases.
|
|
|
|
}
|
|
|
|
|
2009-09-24 11:13:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
bool RecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD,
|
2009-09-25 08:02:51 +08:00
|
|
|
uint64_t Offset) const {
|
2009-09-25 09:23:32 +08:00
|
|
|
QualType T = FD->getType();
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
2009-09-25 08:02:51 +08:00
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
|
2010-05-10 23:26:14 +08:00
|
|
|
return canPlaceRecordAtOffset(RD, Offset, /*CheckVBases=*/true);
|
2009-09-25 08:02:51 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-16 23:07:51 +08:00
|
|
|
if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
|
|
|
|
QualType ElemTy = Context.getBaseElementType(AT);
|
2009-09-25 09:23:32 +08:00
|
|
|
const RecordType *RT = ElemTy->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return true;
|
|
|
|
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (!RD)
|
|
|
|
return true;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 06:35:05 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2009-09-25 09:23:32 +08:00
|
|
|
|
2010-04-16 23:07:51 +08:00
|
|
|
uint64_t NumElements = Context.getConstantArrayElementCount(AT);
|
2009-11-05 12:02:15 +08:00
|
|
|
uint64_t ElementOffset = Offset;
|
2009-09-25 09:23:32 +08:00
|
|
|
for (uint64_t I = 0; I != NumElements; ++I) {
|
2010-05-10 23:26:14 +08:00
|
|
|
if (!canPlaceRecordAtOffset(RD, ElementOffset, /*CheckVBases=*/true))
|
2009-09-25 09:23:32 +08:00
|
|
|
return false;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 06:35:05 +08:00
|
|
|
ElementOffset += Layout.getSize();
|
2009-09-25 09:23:32 +08:00
|
|
|
}
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-25 08:02:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD,
|
2010-05-10 23:28:59 +08:00
|
|
|
uint64_t Offset,
|
|
|
|
bool UpdateVBases) {
|
2009-09-24 11:22:10 +08:00
|
|
|
if (RD->isEmpty())
|
|
|
|
EmptyClassOffsets.insert(std::make_pair(Offset, RD));
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 06:35:05 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2009-09-24 13:21:31 +08:00
|
|
|
|
|
|
|
// Update bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
2009-10-26 01:03:50 +08:00
|
|
|
assert(!I->getType()->isDependentType() &&
|
|
|
|
"Cannot layout class with dependent bases.");
|
2009-09-24 13:21:31 +08:00
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-24 13:21:31 +08:00
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 06:35:05 +08:00
|
|
|
uint64_t BaseClassOffset = Layout.getBaseClassOffset(Base);
|
2010-05-10 23:28:59 +08:00
|
|
|
UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset,
|
|
|
|
/*UpdateVBases=*/false);
|
2009-09-24 13:21:31 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-09-25 23:39:00 +08:00
|
|
|
// Update fields.
|
|
|
|
unsigned FieldNo = 0;
|
2010-04-08 10:59:49 +08:00
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
|
2009-09-25 23:39:00 +08:00
|
|
|
I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-09 06:35:05 +08:00
|
|
|
uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
|
2009-09-25 23:39:00 +08:00
|
|
|
UpdateEmptyClassOffsets(FD, Offset + FieldOffset);
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-24 02:14:24 +08:00
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-10 23:28:59 +08:00
|
|
|
if (UpdateVBases) {
|
|
|
|
// FIXME: Update virtual bases.
|
2010-05-24 02:14:24 +08:00
|
|
|
} else if (PrimaryBase && Layout.getPrimaryBaseWasVirtual()) {
|
|
|
|
// We always want to update the offsets of a primary virtual base.
|
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"primary base class offset must always be 0!");
|
|
|
|
UpdateEmptyClassOffsets(PrimaryBase, Offset, /*UpdateVBases=*/false);
|
2010-05-10 23:28:59 +08:00
|
|
|
}
|
2009-09-24 11:13:30 +08:00
|
|
|
}
|
|
|
|
|
2009-09-25 09:54:38 +08:00
|
|
|
void
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD,
|
2009-09-25 09:54:38 +08:00
|
|
|
uint64_t Offset) {
|
|
|
|
QualType T = FD->getType();
|
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
2010-05-10 23:28:59 +08:00
|
|
|
UpdateEmptyClassOffsets(RD, Offset, /*UpdateVBases=*/true);
|
2009-09-25 09:54:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-16 23:07:51 +08:00
|
|
|
if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
|
|
|
|
QualType ElemTy = Context.getBaseElementType(AT);
|
2009-09-25 09:54:38 +08:00
|
|
|
const RecordType *RT = ElemTy->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return;
|
|
|
|
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (!RD)
|
|
|
|
return;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-16 23:07:51 +08:00
|
|
|
const ASTRecordLayout &Info = Context.getASTRecordLayout(RD);
|
2009-09-25 09:54:38 +08:00
|
|
|
|
2010-04-16 23:07:51 +08:00
|
|
|
uint64_t NumElements = Context.getConstantArrayElementCount(AT);
|
2009-11-05 12:02:15 +08:00
|
|
|
uint64_t ElementOffset = Offset;
|
2009-09-25 09:54:38 +08:00
|
|
|
|
|
|
|
for (uint64_t I = 0; I != NumElements; ++I) {
|
2010-05-10 23:28:59 +08:00
|
|
|
UpdateEmptyClassOffsets(RD, ElementOffset, /*UpdateVBases=*/true);
|
2009-09-25 09:54:38 +08:00
|
|
|
ElementOffset += Info.getSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
void RecordLayoutBuilder::InitializeLayout(const RecordDecl *D) {
|
2009-07-19 04:20:21 +08:00
|
|
|
IsUnion = D->isUnion();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2009-08-09 03:38:24 +08:00
|
|
|
Packed = D->hasAttr<PackedAttr>();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-27 09:12:46 +08:00
|
|
|
if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
|
|
|
|
MaxFieldAlignment = MFAA->getAlignment();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
|
2009-11-21 16:43:09 +08:00
|
|
|
UpdateAlignment(AA->getMaxAlignment());
|
2010-05-26 23:10:00 +08:00
|
|
|
}
|
2009-07-19 08:18:47 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
void RecordLayoutBuilder::Layout(const RecordDecl *D) {
|
|
|
|
InitializeLayout(D);
|
2009-07-19 05:48:39 +08:00
|
|
|
LayoutFields(D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
// Finally, round the size of the total struct up to the alignment of the
|
|
|
|
// struct itself.
|
|
|
|
FinishLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
|
|
|
|
InitializeLayout(RD);
|
|
|
|
|
|
|
|
// Lay out the vtable and the non-virtual bases.
|
|
|
|
LayoutNonVirtualBases(RD);
|
|
|
|
|
|
|
|
LayoutFields(RD);
|
|
|
|
|
2009-07-29 03:24:15 +08:00
|
|
|
NonVirtualSize = Size;
|
|
|
|
NonVirtualAlignment = Alignment;
|
2009-07-30 08:22:38 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
// Lay out the virtual bases and add the primary virtual base offsets.
|
|
|
|
LayoutVirtualBases(RD, RD);
|
2010-04-16 00:12:58 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
VisitedVirtualBases.clear();
|
|
|
|
AddPrimaryVirtualBaseOffsets(RD, 0, RD);
|
2009-08-06 21:41:24 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
// Finally, round the size of the total struct up to the alignment of the
|
|
|
|
// struct itself.
|
|
|
|
FinishLayout();
|
2010-05-26 23:10:00 +08:00
|
|
|
|
2010-04-11 05:24:48 +08:00
|
|
|
#ifndef NDEBUG
|
2010-05-26 23:10:00 +08:00
|
|
|
// Check that we have base offsets for all bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
assert(Bases.count(BaseDecl) && "Did not find base offset!");
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
// And all virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
|
|
|
|
E = RD->vbases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
assert(VBases.count(BaseDecl) && "Did not find base offset!");
|
2010-04-11 05:24:48 +08:00
|
|
|
}
|
|
|
|
#endif
|
2009-07-19 04:20:21 +08:00
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
|
2009-07-19 04:50:59 +08:00
|
|
|
if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
|
2010-04-16 23:07:51 +08:00
|
|
|
const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
|
2009-07-19 04:50:59 +08:00
|
|
|
|
|
|
|
UpdateAlignment(SL.getAlignment());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:50:59 +08:00
|
|
|
// We start laying out ivars not at the end of the superclass
|
|
|
|
// structure, but at the next byte following the last field.
|
2009-07-19 05:26:44 +08:00
|
|
|
Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
|
2009-09-26 09:34:51 +08:00
|
|
|
DataSize = Size;
|
2009-07-19 04:50:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-09 03:38:24 +08:00
|
|
|
Packed = D->hasAttr<PackedAttr>();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-27 09:12:46 +08:00
|
|
|
if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
|
|
|
|
MaxFieldAlignment = MFAA->getAlignment();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:50:59 +08:00
|
|
|
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
|
2009-11-21 16:43:09 +08:00
|
|
|
UpdateAlignment(AA->getMaxAlignment());
|
2010-05-27 09:12:46 +08:00
|
|
|
|
2009-07-19 04:50:59 +08:00
|
|
|
// Layout each ivar sequentially.
|
|
|
|
llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
|
2010-04-16 23:07:51 +08:00
|
|
|
Context.ShallowCollectObjCIvars(D, Ivars);
|
2009-07-19 04:50:59 +08:00
|
|
|
for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
|
|
|
|
LayoutField(Ivars[i]);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:50:59 +08:00
|
|
|
// Finally, round the size of the total struct up to the alignment of the
|
|
|
|
// struct itself.
|
|
|
|
FinishLayout();
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
|
2009-07-19 05:48:39 +08:00
|
|
|
// Layout each field, for now, just sequentially, respecting alignment. In
|
|
|
|
// the future, this will need to be tweakable by targets.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = D->field_begin(),
|
2010-04-08 10:59:49 +08:00
|
|
|
FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
|
2009-07-19 05:48:39 +08:00
|
|
|
LayoutField(*Field);
|
|
|
|
}
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
|
2010-04-16 23:57:11 +08:00
|
|
|
uint64_t TypeSize) {
|
|
|
|
assert(Context.getLangOptions().CPlusPlus &&
|
|
|
|
"Can only have wide bit-fields in C++!");
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
// Itanium C++ ABI 2.4:
|
2010-05-27 10:25:46 +08:00
|
|
|
// If sizeof(T)*8 < n, let T' be the largest integral POD type with
|
2010-04-16 23:57:11 +08:00
|
|
|
// sizeof(T')*8 <= n.
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
QualType IntegralPODTypes[] = {
|
2010-05-27 10:25:46 +08:00
|
|
|
Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
|
2010-04-16 23:57:11 +08:00
|
|
|
Context.UnsignedLongTy, Context.UnsignedLongLongTy
|
|
|
|
};
|
|
|
|
|
|
|
|
QualType Type;
|
|
|
|
for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
|
|
|
|
I != E; ++I) {
|
|
|
|
uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
|
|
|
|
|
|
|
|
if (Size > FieldSize)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Type = IntegralPODTypes[I];
|
|
|
|
}
|
|
|
|
assert(!Type.isNull() && "Did not find a type!");
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
unsigned TypeAlign = Context.getTypeAlign(Type);
|
|
|
|
|
|
|
|
// We're not going to use any of the unfilled bits in the last byte.
|
|
|
|
UnfilledBitsInLastByte = 0;
|
|
|
|
|
2010-04-18 04:21:41 +08:00
|
|
|
uint64_t FieldOffset;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
if (IsUnion) {
|
|
|
|
DataSize = std::max(DataSize, FieldSize);
|
2010-04-18 04:21:41 +08:00
|
|
|
FieldOffset = 0;
|
2010-04-16 23:57:11 +08:00
|
|
|
} else {
|
2010-04-18 04:21:41 +08:00
|
|
|
// The bitfield is allocated starting at the next offset aligned appropriately
|
2010-05-27 10:25:46 +08:00
|
|
|
// for T', with length n bits.
|
2010-04-18 04:21:41 +08:00
|
|
|
FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign);
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
uint64_t NewSizeInBits = FieldOffset + FieldSize;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
|
|
|
|
UnfilledBitsInLastByte = DataSize - NewSizeInBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Place this field at the current location.
|
|
|
|
FieldOffsets.push_back(FieldOffset);
|
|
|
|
|
|
|
|
// Update the size.
|
|
|
|
Size = std::max(Size, DataSize);
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
// Remember max struct/class alignment.
|
|
|
|
UpdateAlignment(TypeAlign);
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
|
2009-11-23 01:37:31 +08:00
|
|
|
bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
|
2009-11-23 03:13:51 +08:00
|
|
|
uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte);
|
2010-04-16 23:07:51 +08:00
|
|
|
uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-16 23:07:51 +08:00
|
|
|
std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
|
2009-11-23 01:37:31 +08:00
|
|
|
uint64_t TypeSize = FieldInfo.first;
|
|
|
|
unsigned FieldAlign = FieldInfo.second;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
if (FieldSize > TypeSize) {
|
|
|
|
LayoutWideBitField(FieldSize, TypeSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-16 23:07:51 +08:00
|
|
|
if (FieldPacked || !Context.Target.useBitFieldTypeAlignment())
|
2009-11-23 01:37:31 +08:00
|
|
|
FieldAlign = 1;
|
|
|
|
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
|
|
|
|
FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// The maximum field alignment overrides the aligned attribute.
|
|
|
|
if (MaxFieldAlignment)
|
|
|
|
FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-15 14:18:39 +08:00
|
|
|
// Check if we need to add padding to give the field the correct alignment.
|
2009-11-23 01:37:31 +08:00
|
|
|
if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
|
|
|
|
FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-15 14:18:39 +08:00
|
|
|
// Padding members don't affect overall alignment.
|
2009-11-23 01:37:31 +08:00
|
|
|
if (!D->getIdentifier())
|
|
|
|
FieldAlign = 1;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// Place this field at the current location.
|
|
|
|
FieldOffsets.push_back(FieldOffset);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 03:13:51 +08:00
|
|
|
// Update DataSize to include the last byte containing (part of) the bitfield.
|
|
|
|
if (IsUnion) {
|
|
|
|
// FIXME: I think FieldSize should be TypeSize here.
|
|
|
|
DataSize = std::max(DataSize, FieldSize);
|
|
|
|
} else {
|
|
|
|
uint64_t NewSizeInBits = FieldOffset + FieldSize;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 03:13:51 +08:00
|
|
|
DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
|
|
|
|
UnfilledBitsInLastByte = DataSize - NewSizeInBits;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 03:13:51 +08:00
|
|
|
// Update the size.
|
|
|
|
Size = std::max(Size, DataSize);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// Remember max struct/class alignment.
|
|
|
|
UpdateAlignment(FieldAlign);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
|
2009-11-23 01:37:31 +08:00
|
|
|
if (D->isBitField()) {
|
|
|
|
LayoutBitField(D);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-23 03:13:51 +08:00
|
|
|
// Reset the unfilled bits.
|
|
|
|
UnfilledBitsInLastByte = 0;
|
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
|
|
|
|
uint64_t FieldOffset = IsUnion ? 0 : DataSize;
|
|
|
|
uint64_t FieldSize;
|
|
|
|
unsigned FieldAlign;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
if (D->getType()->isIncompleteArrayType()) {
|
|
|
|
// This is a flexible array member; we can't directly
|
|
|
|
// query getTypeInfo about these, so we figure it out here.
|
|
|
|
// Flexible array members don't have any size, but they
|
|
|
|
// have to be aligned appropriately for their element type.
|
|
|
|
FieldSize = 0;
|
2010-04-16 23:07:51 +08:00
|
|
|
const ArrayType* ATy = Context.getAsArrayType(D->getType());
|
|
|
|
FieldAlign = Context.getTypeAlign(ATy->getElementType());
|
2009-11-23 01:37:31 +08:00
|
|
|
} else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
|
|
|
|
unsigned AS = RT->getPointeeType().getAddressSpace();
|
2010-04-16 23:07:51 +08:00
|
|
|
FieldSize = Context.Target.getPointerWidth(AS);
|
|
|
|
FieldAlign = Context.Target.getPointerAlign(AS);
|
2009-11-23 01:37:31 +08:00
|
|
|
} else {
|
2010-04-16 23:07:51 +08:00
|
|
|
std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
|
2009-11-23 01:37:31 +08:00
|
|
|
FieldSize = FieldInfo.first;
|
2009-07-19 04:20:21 +08:00
|
|
|
FieldAlign = FieldInfo.second;
|
2009-11-23 01:37:31 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
if (FieldPacked)
|
|
|
|
FieldAlign = 8;
|
|
|
|
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
|
|
|
|
FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// The maximum field alignment overrides the aligned attribute.
|
|
|
|
if (MaxFieldAlignment)
|
|
|
|
FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// Round up the current record size to the field's alignment boundary.
|
|
|
|
FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
if (!IsUnion) {
|
|
|
|
while (true) {
|
|
|
|
// Check if we can place the field at this offset.
|
|
|
|
if (canPlaceFieldAtOffset(D, FieldOffset))
|
|
|
|
break;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// We couldn't place the field at the offset. Try again at a new offset.
|
|
|
|
FieldOffset += FieldAlign;
|
2009-09-25 08:02:51 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
UpdateEmptyClassOffsets(D, FieldOffset);
|
2009-07-19 04:20:21 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
// Place this field at the current location.
|
|
|
|
FieldOffsets.push_back(FieldOffset);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
// Reserve space for this field.
|
|
|
|
if (IsUnion)
|
|
|
|
Size = std::max(Size, FieldSize);
|
|
|
|
else
|
|
|
|
Size = FieldOffset + FieldSize;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-26 09:34:51 +08:00
|
|
|
// Update the data size.
|
|
|
|
DataSize = Size;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
// Remember max struct/class alignment.
|
|
|
|
UpdateAlignment(FieldAlign);
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::FinishLayout() {
|
2009-07-19 04:20:21 +08:00
|
|
|
// In C++, records cannot be of size 0.
|
2010-04-16 23:07:51 +08:00
|
|
|
if (Context.getLangOptions().CPlusPlus && Size == 0)
|
2009-07-19 04:20:21 +08:00
|
|
|
Size = 8;
|
|
|
|
// Finally, round the size of the record up to the alignment of the
|
|
|
|
// record itself.
|
2009-11-23 01:37:31 +08:00
|
|
|
Size = llvm::RoundUpToAlignment(Size, Alignment);
|
2009-07-19 04:20:21 +08:00
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
|
2009-07-19 04:20:21 +08:00
|
|
|
if (NewAlignment <= Alignment)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
Alignment = NewAlignment;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-07 12:35:11 +08:00
|
|
|
const CXXMethodDecl *
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
|
2009-12-07 12:35:11 +08:00
|
|
|
assert(RD->isDynamicClass() && "Class does not have any virtual methods!");
|
|
|
|
|
2010-04-20 04:44:53 +08:00
|
|
|
// If a class isn't polymorphic it doesn't have a key function.
|
2009-12-07 12:35:11 +08:00
|
|
|
if (!RD->isPolymorphic())
|
|
|
|
return 0;
|
2009-12-08 11:56:49 +08:00
|
|
|
|
|
|
|
// A class inside an anonymous namespace doesn't have a key function. (Or
|
|
|
|
// at least, there's no point to assigning a key function to such a class;
|
|
|
|
// this doesn't affect the ABI.)
|
|
|
|
if (RD->isInAnonymousNamespace())
|
|
|
|
return 0;
|
|
|
|
|
2010-04-08 10:59:49 +08:00
|
|
|
for (CXXRecordDecl::method_iterator I = RD->method_begin(),
|
|
|
|
E = RD->method_end(); I != E; ++I) {
|
2009-12-07 12:35:11 +08:00
|
|
|
const CXXMethodDecl *MD = *I;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-12-07 12:35:11 +08:00
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-12-07 12:35:11 +08:00
|
|
|
if (MD->isPure())
|
|
|
|
continue;
|
2009-12-08 11:56:49 +08:00
|
|
|
|
2009-12-07 12:35:11 +08:00
|
|
|
// Ignore implicit member functions, they are always marked as inline, but
|
|
|
|
// they don't have a body until they're defined.
|
|
|
|
if (MD->isImplicit())
|
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-01-06 03:06:31 +08:00
|
|
|
if (MD->isInlineSpecified())
|
|
|
|
continue;
|
2009-12-07 12:35:11 +08:00
|
|
|
|
|
|
|
if (MD->hasInlineBody())
|
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-12-07 12:35:11 +08:00
|
|
|
// We found it.
|
|
|
|
return MD;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-12-07 12:35:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-26 12:56:53 +08:00
|
|
|
/// getASTRecordLayout - Get or compute information about the layout of the
|
|
|
|
/// specified record (struct/union/class), which indicates its size and field
|
|
|
|
/// position information.
|
|
|
|
const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
|
|
|
|
D = D->getDefinition();
|
|
|
|
assert(D && "Cannot get layout of forward declarations!");
|
|
|
|
|
|
|
|
// Look up this layout, if already laid out, return what we have.
|
|
|
|
// Note that we can't save a reference to the entry because this function
|
|
|
|
// is recursive.
|
|
|
|
const ASTRecordLayout *Entry = ASTRecordLayouts[D];
|
|
|
|
if (Entry) return *Entry;
|
|
|
|
|
2010-05-26 13:10:47 +08:00
|
|
|
const ASTRecordLayout *NewEntry;
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
|
2010-05-27 08:07:01 +08:00
|
|
|
EmptySubobjectMap EmptySubobjects(*this, RD);
|
2010-05-27 13:41:06 +08:00
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
RecordLayoutBuilder Builder(*this, &EmptySubobjects);
|
2010-05-26 13:10:47 +08:00
|
|
|
Builder.Layout(RD);
|
|
|
|
|
|
|
|
// FIXME: This is not always correct. See the part about bitfields at
|
|
|
|
// http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
|
|
|
|
// FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
|
|
|
|
bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
|
|
|
|
|
|
|
|
// FIXME: This should be done in FinalizeLayout.
|
|
|
|
uint64_t DataSize =
|
|
|
|
IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize;
|
|
|
|
uint64_t NonVirtualSize =
|
|
|
|
IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
NewEntry =
|
2010-05-26 13:10:47 +08:00
|
|
|
new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
|
|
|
|
DataSize, Builder.FieldOffsets.data(),
|
|
|
|
Builder.FieldOffsets.size(),
|
|
|
|
NonVirtualSize,
|
|
|
|
Builder.NonVirtualAlignment,
|
2010-05-27 08:07:01 +08:00
|
|
|
EmptySubobjects.SizeOfLargestEmptySubobject,
|
2010-05-26 13:10:47 +08:00
|
|
|
Builder.PrimaryBase,
|
2010-05-26 13:25:15 +08:00
|
|
|
Builder.PrimaryBaseIsVirtual,
|
2010-05-26 13:10:47 +08:00
|
|
|
Builder.Bases, Builder.VBases);
|
|
|
|
} else {
|
2010-05-27 08:07:01 +08:00
|
|
|
RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
|
2010-05-26 13:10:47 +08:00
|
|
|
Builder.Layout(D);
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 13:10:47 +08:00
|
|
|
NewEntry =
|
|
|
|
new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
|
|
|
|
Builder.Size,
|
|
|
|
Builder.FieldOffsets.data(),
|
|
|
|
Builder.FieldOffsets.size());
|
|
|
|
}
|
|
|
|
|
2010-05-26 12:56:53 +08:00
|
|
|
ASTRecordLayouts[D] = NewEntry;
|
|
|
|
|
|
|
|
if (getLangOptions().DumpRecordLayouts) {
|
|
|
|
llvm::errs() << "\n*** Dumping AST Record Layout\n";
|
|
|
|
DumpRecordLayout(D, llvm::errs());
|
|
|
|
}
|
|
|
|
|
|
|
|
return *NewEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
|
|
|
|
RD = cast<CXXRecordDecl>(RD->getDefinition());
|
|
|
|
assert(RD && "Cannot get key function for forward declarations!");
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 12:56:53 +08:00
|
|
|
const CXXMethodDecl *&Entry = KeyFunctions[RD];
|
2010-05-27 10:25:46 +08:00
|
|
|
if (!Entry)
|
2010-05-26 13:58:59 +08:00
|
|
|
Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
|
2010-05-26 12:56:53 +08:00
|
|
|
else
|
2010-05-26 13:58:59 +08:00
|
|
|
assert(Entry == RecordLayoutBuilder::ComputeKeyFunction(RD) &&
|
2010-05-26 12:56:53 +08:00
|
|
|
"Key function changed!");
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 12:56:53 +08:00
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getInterfaceLayoutImpl - Get or compute information about the
|
|
|
|
/// layout of the given interface.
|
|
|
|
///
|
|
|
|
/// \param Impl - If given, also include the layout of the interface's
|
|
|
|
/// implementation. This may differ by including synthesized ivars.
|
|
|
|
const ASTRecordLayout &
|
|
|
|
ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
|
|
|
|
const ObjCImplementationDecl *Impl) {
|
|
|
|
assert(!D->isForwardDecl() && "Invalid interface decl!");
|
|
|
|
|
|
|
|
// Look up this layout, if already laid out, return what we have.
|
|
|
|
ObjCContainerDecl *Key =
|
|
|
|
Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
|
|
|
|
if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
|
|
|
|
return *Entry;
|
|
|
|
|
|
|
|
// Add in synthesized ivar count if laying out an implementation.
|
|
|
|
if (Impl) {
|
|
|
|
unsigned SynthCount = CountNonClassIvars(D);
|
|
|
|
// If there aren't any sythesized ivars then reuse the interface
|
|
|
|
// entry. Note we can't cache this because we simply free all
|
|
|
|
// entries later; however we shouldn't look up implementations
|
|
|
|
// frequently.
|
|
|
|
if (SynthCount == 0)
|
|
|
|
return getObjCLayout(D, 0);
|
|
|
|
}
|
|
|
|
|
2010-05-27 08:07:01 +08:00
|
|
|
RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
|
2010-05-26 13:04:25 +08:00
|
|
|
Builder.Layout(D);
|
|
|
|
|
2010-05-26 12:56:53 +08:00
|
|
|
const ASTRecordLayout *NewEntry =
|
2010-05-26 13:04:25 +08:00
|
|
|
new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
|
|
|
|
Builder.DataSize,
|
|
|
|
Builder.FieldOffsets.data(),
|
|
|
|
Builder.FieldOffsets.size());
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-26 12:56:53 +08:00
|
|
|
ObjCLayouts[Key] = NewEntry;
|
|
|
|
|
|
|
|
return *NewEntry;
|
|
|
|
}
|
|
|
|
|
2010-04-08 10:59:49 +08:00
|
|
|
static void PrintOffset(llvm::raw_ostream &OS,
|
|
|
|
uint64_t Offset, unsigned IndentLevel) {
|
|
|
|
OS << llvm::format("%4d | ", Offset);
|
|
|
|
OS.indent(IndentLevel * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DumpCXXRecordLayout(llvm::raw_ostream &OS,
|
|
|
|
const CXXRecordDecl *RD, ASTContext &C,
|
|
|
|
uint64_t Offset,
|
|
|
|
unsigned IndentLevel,
|
|
|
|
const char* Description,
|
|
|
|
bool IncludeVirtualBases) {
|
|
|
|
const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
PrintOffset(OS, Offset, IndentLevel);
|
2010-04-20 00:39:44 +08:00
|
|
|
OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
|
2010-04-08 10:59:49 +08:00
|
|
|
if (Description)
|
|
|
|
OS << ' ' << Description;
|
|
|
|
if (RD->isEmpty())
|
|
|
|
OS << " (empty)";
|
|
|
|
OS << '\n';
|
|
|
|
|
|
|
|
IndentLevel++;
|
|
|
|
|
|
|
|
const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
|
|
|
|
|
|
|
|
// Vtable pointer.
|
|
|
|
if (RD->isDynamicClass() && !PrimaryBase) {
|
|
|
|
PrintOffset(OS, Offset, IndentLevel);
|
2010-04-17 17:33:03 +08:00
|
|
|
OS << '(' << RD << " vtable pointer)\n";
|
2010-04-08 10:59:49 +08:00
|
|
|
}
|
|
|
|
// Dump (non-virtual) bases
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
assert(!I->getType()->isDependentType() &&
|
|
|
|
"Cannot layout class with dependent bases.");
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
|
|
|
|
|
|
|
|
DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
|
|
|
|
Base == PrimaryBase ? "(primary base)" : "(base)",
|
|
|
|
/*IncludeVirtualBases=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump fields.
|
|
|
|
uint64_t FieldNo = 0;
|
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(),
|
|
|
|
E = RD->field_end(); I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *Field = *I;
|
|
|
|
uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
|
|
|
|
|
|
|
|
if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
|
|
|
|
if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
|
|
|
DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
|
|
|
|
Field->getNameAsCString(),
|
|
|
|
/*IncludeVirtualBases=*/true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintOffset(OS, FieldOffset, IndentLevel);
|
2010-04-17 17:33:03 +08:00
|
|
|
OS << Field->getType().getAsString() << ' ' << Field << '\n';
|
2010-04-08 10:59:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IncludeVirtualBases)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Dump virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
|
|
|
|
E = RD->vbases_end(); I != E; ++I) {
|
|
|
|
assert(I->isVirtual() && "Found non-virtual class!");
|
|
|
|
const CXXRecordDecl *VBase =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
|
|
|
|
DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
|
|
|
|
VBase == PrimaryBase ?
|
|
|
|
"(primary virtual base)" : "(virtual base)",
|
|
|
|
/*IncludeVirtualBases=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << " sizeof=" << Info.getSize() / 8;
|
|
|
|
OS << ", dsize=" << Info.getDataSize() / 8;
|
|
|
|
OS << ", align=" << Info.getAlignment() / 8 << '\n';
|
|
|
|
OS << " nvsize=" << Info.getNonVirtualSize() / 8;
|
|
|
|
OS << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
|
|
|
|
OS << '\n';
|
|
|
|
}
|
2010-04-20 04:44:53 +08:00
|
|
|
|
|
|
|
void ASTContext::DumpRecordLayout(const RecordDecl *RD,
|
|
|
|
llvm::raw_ostream &OS) {
|
|
|
|
const ASTRecordLayout &Info = getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
|
|
|
|
return DumpCXXRecordLayout(OS, CXXRD, *this, 0, 0, 0,
|
|
|
|
/*IncludeVirtualBases=*/true);
|
|
|
|
|
|
|
|
OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
|
|
|
|
OS << "Record: ";
|
|
|
|
RD->dump();
|
|
|
|
OS << "\nLayout: ";
|
|
|
|
OS << "<ASTRecordLayout\n";
|
|
|
|
OS << " Size:" << Info.getSize() << "\n";
|
|
|
|
OS << " DataSize:" << Info.getDataSize() << "\n";
|
|
|
|
OS << " Alignment:" << Info.getAlignment() << "\n";
|
|
|
|
OS << " FieldOffsets: [";
|
|
|
|
for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
|
|
|
|
if (i) OS << ", ";
|
|
|
|
OS << Info.getFieldOffset(i);
|
|
|
|
}
|
|
|
|
OS << "]>\n";
|
|
|
|
}
|