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"
|
2010-11-25 06:55:48 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-07-19 04:20:21 +08:00
|
|
|
#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-09-22 22:32:24 +08:00
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
2010-04-08 10:59:49 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
2011-03-19 09:00:36 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
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
|
|
|
|
2010-05-29 05:24:37 +08:00
|
|
|
/// BaseSubobjectInfo - Represents a single base subobject in a complete class.
|
|
|
|
/// For a class hierarchy like
|
|
|
|
///
|
|
|
|
/// class A { };
|
|
|
|
/// class B : A { };
|
|
|
|
/// class C : A, B { };
|
|
|
|
///
|
|
|
|
/// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo
|
|
|
|
/// instances, one for B and two for A.
|
|
|
|
///
|
|
|
|
/// If a base is virtual, it will only have one BaseSubobjectInfo allocated.
|
|
|
|
struct BaseSubobjectInfo {
|
|
|
|
/// Class - The class for this base info.
|
2010-05-29 05:13:31 +08:00
|
|
|
const CXXRecordDecl *Class;
|
2010-05-29 05:24:37 +08:00
|
|
|
|
|
|
|
/// IsVirtual - Whether the BaseInfo represents a virtual base or not.
|
2010-05-29 05:13:31 +08:00
|
|
|
bool IsVirtual;
|
|
|
|
|
2010-05-29 05:24:37 +08:00
|
|
|
/// Bases - Information about the base subobjects.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<BaseSubobjectInfo*, 4> Bases;
|
2010-05-29 05:24:37 +08:00
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
/// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base
|
|
|
|
/// of this base info (if one exists).
|
|
|
|
BaseSubobjectInfo *PrimaryVirtualBaseInfo;
|
2010-05-29 05:24:37 +08:00
|
|
|
|
|
|
|
// FIXME: Document.
|
|
|
|
const BaseSubobjectInfo *Derived;
|
2010-05-29 05:13:31 +08:00
|
|
|
};
|
|
|
|
|
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 {
|
2011-01-12 17:06:06 +08:00
|
|
|
const ASTContext &Context;
|
2010-11-01 05:54:55 +08:00
|
|
|
uint64_t CharWidth;
|
|
|
|
|
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.
|
2011-07-23 18:55:15 +08:00
|
|
|
typedef SmallVector<const CXXRecordDecl *, 1> ClassVectorTy;
|
2010-11-01 05:22:43 +08:00
|
|
|
typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy;
|
2010-05-27 13:41:06 +08:00
|
|
|
EmptyClassOffsetsMapTy EmptyClassOffsets;
|
|
|
|
|
2010-06-08 23:56:03 +08:00
|
|
|
/// MaxEmptyClassOffset - The highest offset known to contain an empty
|
|
|
|
/// base subobject.
|
2010-11-01 05:39:24 +08:00
|
|
|
CharUnits MaxEmptyClassOffset;
|
2010-06-08 23:56:03 +08:00
|
|
|
|
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 13:41:06 +08:00
|
|
|
|
2010-11-01 05:39:24 +08:00
|
|
|
void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
2010-05-29 05:24:37 +08:00
|
|
|
void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset, bool PlacingEmptyBase);
|
2010-05-27 13:41:06 +08:00
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *Class,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset);
|
|
|
|
void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
2010-06-08 23:56:03 +08:00
|
|
|
/// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty
|
|
|
|
/// subobjects beyond the given offset.
|
2010-11-01 05:39:24 +08:00
|
|
|
bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const {
|
2010-06-08 23:56:03 +08:00
|
|
|
return Offset <= MaxEmptyClassOffset;
|
|
|
|
}
|
|
|
|
|
2010-11-01 05:54:55 +08:00
|
|
|
CharUnits
|
|
|
|
getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const {
|
|
|
|
uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
|
|
|
|
assert(FieldOffset % CharWidth == 0 &&
|
|
|
|
"Field offset not at char boundary!");
|
|
|
|
|
2011-01-24 09:28:50 +08:00
|
|
|
return Context.toCharUnitsFromBits(FieldOffset);
|
2010-11-01 05:22:43 +08:00
|
|
|
}
|
|
|
|
|
2010-08-19 08:55:19 +08:00
|
|
|
protected:
|
2010-11-01 05:39:24 +08:00
|
|
|
bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
|
|
|
|
CharUnits Offset) const;
|
2010-08-19 08:55:19 +08:00
|
|
|
|
|
|
|
bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset);
|
2010-08-19 08:55:19 +08:00
|
|
|
|
|
|
|
bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *Class,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset) const;
|
2010-08-19 08:55:19 +08:00
|
|
|
bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
|
2010-11-01 05:54:55 +08:00
|
|
|
CharUnits Offset) const;
|
2010-08-19 08:55:19 +08:00
|
|
|
|
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.
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits SizeOfLargestEmptySubobject;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2011-01-12 17:06:06 +08:00
|
|
|
EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class)
|
2010-11-01 06:13:23 +08:00
|
|
|
: Context(Context), CharWidth(Context.getCharWidth()), Class(Class) {
|
2010-05-27 08:07:01 +08:00
|
|
|
ComputeEmptySubobjectSizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2010-06-08 23:56:03 +08:00
|
|
|
bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
|
|
|
/// CanPlaceFieldAtOffset - Return whether a field can be placed at the given
|
|
|
|
/// offset.
|
2010-11-01 06:13:23 +08:00
|
|
|
bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits 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());
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits EmptySize;
|
2010-05-26 23:54:25 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
|
|
|
|
if (BaseDecl->isEmpty()) {
|
|
|
|
// If the class decl is empty, get its size.
|
2011-02-09 09:59:34 +08:00
|
|
|
EmptySize = Layout.getSize();
|
2010-05-26 23:54:25 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, we get the largest empty subobject for the decl.
|
|
|
|
EmptySize = Layout.getSizeOfLargestEmptySubobject();
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
if (EmptySize > SizeOfLargestEmptySubobject)
|
|
|
|
SizeOfLargestEmptySubobject = EmptySize;
|
2010-05-26 23:54:25 +08:00
|
|
|
}
|
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;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits EmptySize;
|
2010-05-26 23:54:25 +08:00
|
|
|
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.
|
2011-02-09 09:59:34 +08:00
|
|
|
EmptySize = Layout.getSize();
|
2010-05-26 23:54:25 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, we get the largest empty subobject for the decl.
|
|
|
|
EmptySize = Layout.getSizeOfLargestEmptySubobject();
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
if (EmptySize > SizeOfLargestEmptySubobject)
|
|
|
|
SizeOfLargestEmptySubobject = EmptySize;
|
2010-05-26 23:54:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
bool
|
|
|
|
EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
|
2010-11-01 05:39:24 +08:00
|
|
|
CharUnits Offset) const {
|
2010-05-28 02:20:57 +08:00
|
|
|
// We only need to check empty bases.
|
|
|
|
if (!RD->isEmpty())
|
|
|
|
return true;
|
|
|
|
|
2010-11-01 05:39:24 +08:00
|
|
|
EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset);
|
2010-05-28 02:20:57 +08:00
|
|
|
if (I == EmptyClassOffsets.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const ClassVectorTy& Classes = I->second;
|
|
|
|
if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// There is already an empty class of the same type at this offset.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD,
|
2010-11-01 05:39:24 +08:00
|
|
|
CharUnits Offset) {
|
2010-05-28 02:20:57 +08:00
|
|
|
// We only care about empty bases.
|
|
|
|
if (!RD->isEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-12-30 07:02:58 +08:00
|
|
|
// If we have empty structures inside an union, we can assign both
|
|
|
|
// the same offset. Just avoid pushing them twice in the list.
|
2010-11-01 05:39:24 +08:00
|
|
|
ClassVectorTy& Classes = EmptyClassOffsets[Offset];
|
2010-12-30 07:02:58 +08:00
|
|
|
if (std::find(Classes.begin(), Classes.end(), RD) != Classes.end())
|
|
|
|
return;
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
Classes.push_back(RD);
|
2010-06-08 23:56:03 +08:00
|
|
|
|
|
|
|
// Update the empty class offset.
|
2010-11-01 05:39:24 +08:00
|
|
|
if (Offset > MaxEmptyClassOffset)
|
|
|
|
MaxEmptyClassOffset = Offset;
|
2010-05-28 02:20:57 +08:00
|
|
|
}
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
bool
|
2010-11-01 06:13:23 +08:00
|
|
|
EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
|
|
|
|
CharUnits Offset) {
|
2010-06-09 00:20:35 +08:00
|
|
|
// We don't have to keep looking past the maximum offset that's known to
|
|
|
|
// contain an empty class.
|
2010-11-01 06:13:23 +08:00
|
|
|
if (!AnyEmptySubobjectsBeyondOffset(Offset))
|
2010-06-09 00:20:35 +08:00
|
|
|
return true;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
if (!CanPlaceSubobjectAtOffset(Info->Class, Offset))
|
2010-05-28 02:20:57 +08:00
|
|
|
return false;
|
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
// Traverse all non-virtual bases.
|
2010-05-30 05:10:24 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
|
2010-05-27 13:41:06 +08:00
|
|
|
for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
|
2010-05-29 05:24:37 +08:00
|
|
|
BaseSubobjectInfo* Base = Info->Bases[I];
|
2010-05-27 13:41:06 +08:00
|
|
|
if (Base->IsVirtual)
|
|
|
|
continue;
|
|
|
|
|
2010-11-01 08:21:58 +08:00
|
|
|
CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
|
2010-05-27 13:41:06 +08:00
|
|
|
|
|
|
|
if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
if (Info->PrimaryVirtualBaseInfo) {
|
|
|
|
BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
|
2010-05-27 13:41:06 +08:00
|
|
|
|
|
|
|
if (Info == PrimaryVirtualBaseInfo->Derived) {
|
|
|
|
if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
// Traverse all member variables.
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
|
|
|
|
E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-11-01 05:54:55 +08:00
|
|
|
if (FD->isBitField())
|
|
|
|
continue;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
2010-05-28 02:20:57 +08:00
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-29 05:24:37 +08:00
|
|
|
void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset,
|
2010-06-14 02:00:18 +08:00
|
|
|
bool PlacingEmptyBase) {
|
|
|
|
if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
|
|
|
|
// We know that the only empty subobjects that can conflict with empty
|
|
|
|
// subobject of non-empty bases, are empty bases that can be placed at
|
|
|
|
// offset zero. Because of this, we only need to keep track of empty base
|
|
|
|
// subobjects with offsets less than the size of the largest empty
|
|
|
|
// subobject for our class.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
AddSubobjectAtOffset(Info->Class, Offset);
|
2010-05-30 05:10:24 +08:00
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
// Traverse all non-virtual bases.
|
2010-05-30 05:10:24 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
|
2010-05-27 13:41:06 +08:00
|
|
|
for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
|
2010-05-29 05:24:37 +08:00
|
|
|
BaseSubobjectInfo* Base = Info->Bases[I];
|
2010-05-27 13:41:06 +08:00
|
|
|
if (Base->IsVirtual)
|
|
|
|
continue;
|
2010-05-30 05:10:24 +08:00
|
|
|
|
2010-11-01 08:21:58 +08:00
|
|
|
CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
|
2010-06-14 02:00:18 +08:00
|
|
|
UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
|
2010-05-27 13:41:06 +08:00
|
|
|
}
|
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
if (Info->PrimaryVirtualBaseInfo) {
|
|
|
|
BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
|
2010-05-27 13:41:06 +08:00
|
|
|
|
|
|
|
if (Info == PrimaryVirtualBaseInfo->Derived)
|
2010-06-14 02:00:18 +08:00
|
|
|
UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
|
|
|
|
PlacingEmptyBase);
|
2010-05-27 13:41:06 +08:00
|
|
|
}
|
2010-05-28 02:20:57 +08:00
|
|
|
|
|
|
|
// Traverse all member variables.
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
|
|
|
|
E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-11-01 05:54:55 +08:00
|
|
|
if (FD->isBitField())
|
|
|
|
continue;
|
2010-05-30 05:10:24 +08:00
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
|
|
|
UpdateEmptyFieldSubobjects(FD, FieldOffset);
|
2010-05-28 02:20:57 +08:00
|
|
|
}
|
2010-05-27 13:41:06 +08:00
|
|
|
}
|
|
|
|
|
2010-05-30 04:49:49 +08:00
|
|
|
bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits 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.
|
2010-11-01 06:13:23 +08:00
|
|
|
if (SizeOfLargestEmptySubobject.isZero())
|
2010-05-27 08:07:01 +08:00
|
|
|
return true;
|
|
|
|
|
2010-05-27 13:41:06 +08:00
|
|
|
if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
|
|
|
|
return false;
|
2010-05-28 02:20:57 +08:00
|
|
|
|
|
|
|
// We are able to place the base at this offset. Make sure to update the
|
|
|
|
// empty base subobject map.
|
2010-06-14 02:00:18 +08:00
|
|
|
UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
|
2010-05-27 08:07:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
bool
|
|
|
|
EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *Class,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset) const {
|
2010-06-09 00:20:35 +08:00
|
|
|
// We don't have to keep looking past the maximum offset that's known to
|
|
|
|
// contain an empty class.
|
2010-11-01 06:13:23 +08:00
|
|
|
if (!AnyEmptySubobjectsBeyondOffset(Offset))
|
2010-06-09 00:20:35 +08:00
|
|
|
return true;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
if (!CanPlaceSubobjectAtOffset(RD, Offset))
|
2010-05-28 02:20:57 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Traverse all non-virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-11-01 08:21:58 +08:00
|
|
|
CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
|
2010-05-28 02:20:57 +08:00
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-09 03:09:24 +08:00
|
|
|
if (RD == Class) {
|
|
|
|
// This is the most derived class, traverse virtual bases as well.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
|
|
|
|
E = RD->vbases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *VBaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
|
2010-06-09 03:09:24 +08:00
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
// Traverse all member variables.
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
|
|
|
|
I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-11-01 05:54:55 +08:00
|
|
|
if (FD->isBitField())
|
|
|
|
continue;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-01 05:54:55 +08:00
|
|
|
bool
|
|
|
|
EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
|
|
|
|
CharUnits Offset) const {
|
2010-06-09 00:20:35 +08:00
|
|
|
// We don't have to keep looking past the maximum offset that's known to
|
|
|
|
// contain an empty class.
|
2010-11-01 05:54:55 +08:00
|
|
|
if (!AnyEmptySubobjectsBeyondOffset(Offset))
|
2010-06-09 00:20:35 +08:00
|
|
|
return true;
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
QualType T = FD->getType();
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
2010-11-01 06:13:23 +08:00
|
|
|
return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
|
2010-05-28 02:20:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an array type we need to look at every element.
|
|
|
|
if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
|
|
|
|
QualType ElemTy = Context.getBaseElementType(AT);
|
|
|
|
const RecordType *RT = ElemTy->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
uint64_t NumElements = Context.getConstantArrayElementCount(AT);
|
2010-11-01 05:54:55 +08:00
|
|
|
CharUnits ElementOffset = Offset;
|
2010-05-28 02:20:57 +08:00
|
|
|
for (uint64_t I = 0; I != NumElements; ++I) {
|
2010-06-09 00:20:35 +08:00
|
|
|
// We don't have to keep looking past the maximum offset that's known to
|
|
|
|
// contain an empty class.
|
2010-11-01 05:54:55 +08:00
|
|
|
if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
|
2010-06-09 00:20:35 +08:00
|
|
|
return true;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
|
2010-05-28 02:20:57 +08:00
|
|
|
return false;
|
|
|
|
|
2011-02-09 09:59:34 +08:00
|
|
|
ElementOffset += Layout.getSize();
|
2010-05-28 02:20:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-11-01 06:13:23 +08:00
|
|
|
EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD,
|
|
|
|
CharUnits Offset) {
|
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
|
2010-05-28 02:20:57 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// We are able to place the member variable at this offset.
|
|
|
|
// Make sure to update the empty base subobject map.
|
|
|
|
UpdateEmptyFieldSubobjects(FD, Offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *Class,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset) {
|
2010-06-14 01:49:16 +08:00
|
|
|
// We know that the only empty subobjects that can conflict with empty
|
2010-06-14 02:00:18 +08:00
|
|
|
// field subobjects are subobjects of empty bases that can be placed at offset
|
2010-06-14 01:49:16 +08:00
|
|
|
// zero. Because of this, we only need to keep track of empty field
|
|
|
|
// subobjects with offsets less than the size of the largest empty
|
|
|
|
// subobject for our class.
|
|
|
|
if (Offset >= SizeOfLargestEmptySubobject)
|
|
|
|
return;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
AddSubobjectAtOffset(RD, Offset);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Traverse all non-virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-11-01 08:21:58 +08:00
|
|
|
CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
|
2010-05-28 02:20:57 +08:00
|
|
|
UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
|
|
|
|
}
|
|
|
|
|
2010-06-09 03:09:24 +08:00
|
|
|
if (RD == Class) {
|
|
|
|
// This is the most derived class, traverse virtual bases as well.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
|
|
|
|
E = RD->vbases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *VBaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
|
2010-06-09 03:09:24 +08:00
|
|
|
UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
// Traverse all member variables.
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
|
|
|
|
I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-11-01 23:14:51 +08:00
|
|
|
if (FD->isBitField())
|
|
|
|
continue;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
UpdateEmptyFieldSubobjects(FD, FieldOffset);
|
2010-05-28 02:20:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits Offset) {
|
2010-05-28 02:20:57 +08:00
|
|
|
QualType T = FD->getType();
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
UpdateEmptyFieldSubobjects(RD, RD, Offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an array type we need to update every element.
|
|
|
|
if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
|
|
|
|
QualType ElemTy = Context.getBaseElementType(AT);
|
|
|
|
const RecordType *RT = ElemTy->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
uint64_t NumElements = Context.getConstantArrayElementCount(AT);
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits ElementOffset = Offset;
|
2010-05-28 02:20:57 +08:00
|
|
|
|
|
|
|
for (uint64_t I = 0; I != NumElements; ++I) {
|
2010-06-14 01:49:16 +08:00
|
|
|
// We know that the only empty subobjects that can conflict with empty
|
2010-06-14 02:00:18 +08:00
|
|
|
// field subobjects are subobjects of empty bases that can be placed at
|
2010-06-14 01:49:16 +08:00
|
|
|
// offset zero. Because of this, we only need to keep track of empty field
|
|
|
|
// subobjects with offsets less than the size of the largest empty
|
|
|
|
// subobject for our class.
|
|
|
|
if (ElementOffset >= SizeOfLargestEmptySubobject)
|
|
|
|
return;
|
|
|
|
|
2010-05-28 02:20:57 +08:00
|
|
|
UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
|
2011-02-09 09:59:34 +08:00
|
|
|
ElementOffset += Layout.getSize();
|
2010-05-28 02:20:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-26 13:58:59 +08:00
|
|
|
class RecordLayoutBuilder {
|
2010-08-19 08:55:19 +08:00
|
|
|
protected:
|
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
|
|
|
|
2011-01-12 17:06:06 +08:00
|
|
|
const ASTContext &Context;
|
2010-05-26 13:41:04 +08:00
|
|
|
|
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.
|
2011-02-16 10:05:21 +08:00
|
|
|
CharUnits Alignment;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
/// \brief The alignment if attribute packed is not used.
|
2011-02-16 10:11:31 +08:00
|
|
|
CharUnits UnpackedAlignment;
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<uint64_t, 16> FieldOffsets;
|
2010-05-26 13:41:04 +08:00
|
|
|
|
|
|
|
/// Packed - Whether the record is packed or not.
|
2010-05-27 13:45:51 +08:00
|
|
|
unsigned Packed : 1;
|
|
|
|
|
|
|
|
unsigned IsUnion : 1;
|
|
|
|
|
|
|
|
unsigned IsMac68kAlign : 1;
|
2011-04-27 07:52:16 +08:00
|
|
|
|
|
|
|
unsigned IsMsStruct : 1;
|
2010-05-26 13:41:04 +08:00
|
|
|
|
|
|
|
/// 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.
|
2011-02-17 09:49:42 +08:00
|
|
|
CharUnits 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
|
|
|
|
2011-02-16 09:52:01 +08:00
|
|
|
CharUnits NonVirtualSize;
|
2011-02-16 09:43:15 +08:00
|
|
|
CharUnits NonVirtualAlignment;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2011-05-04 04:21:04 +08:00
|
|
|
FieldDecl *ZeroLengthBitfield;
|
2011-05-03 01:20:56 +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;
|
|
|
|
|
2010-11-01 05:01:46 +08:00
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> 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.
|
2010-11-25 06:55:48 +08:00
|
|
|
CXXIndirectPrimaryBaseSet 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
|
|
|
|
2011-01-12 17:06:06 +08:00
|
|
|
RecordLayoutBuilder(const ASTContext &Context, EmptySubobjectMap
|
|
|
|
*EmptySubobjects)
|
2011-02-16 10:05:21 +08:00
|
|
|
: Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
|
2011-02-16 10:11:31 +08:00
|
|
|
Alignment(CharUnits::One()), UnpackedAlignment(Alignment),
|
2011-04-27 07:52:16 +08:00
|
|
|
Packed(false), IsUnion(false),
|
|
|
|
IsMac68kAlign(false), IsMsStruct(false),
|
2011-02-17 09:49:42 +08:00
|
|
|
UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
|
|
|
|
DataSize(0), NonVirtualSize(CharUnits::Zero()),
|
2011-05-03 01:20:56 +08:00
|
|
|
NonVirtualAlignment(CharUnits::One()),
|
2011-05-04 04:21:04 +08:00
|
|
|
ZeroLengthBitfield(0), PrimaryBase(0),
|
2011-02-17 09:49:42 +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);
|
2010-09-22 22:32:24 +08:00
|
|
|
void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
|
|
|
|
bool FieldPacked, const FieldDecl *D);
|
2010-05-26 13:41:04 +08:00
|
|
|
void LayoutBitField(const FieldDecl *D);
|
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
/// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
|
|
|
|
llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
|
|
|
|
BaseSubobjectInfoMapTy;
|
|
|
|
|
|
|
|
/// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
|
|
|
|
/// of the class we're laying out to their base subobject info.
|
|
|
|
BaseSubobjectInfoMapTy VirtualBaseInfo;
|
|
|
|
|
|
|
|
/// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
|
|
|
|
/// class we're laying out to their base subobject info.
|
|
|
|
BaseSubobjectInfoMapTy NonVirtualBaseInfo;
|
|
|
|
|
|
|
|
/// ComputeBaseSubobjectInfo - Compute the base subobject information for the
|
|
|
|
/// bases of the given class.
|
|
|
|
void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
|
|
|
|
|
|
|
|
/// ComputeBaseSubobjectInfo - Compute the base subobject information for a
|
|
|
|
/// single class and all of its base classes.
|
|
|
|
BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
|
|
|
|
bool IsVirtual,
|
|
|
|
BaseSubobjectInfo *Derived);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
2011-03-01 09:22:45 +08:00
|
|
|
virtual CharUnits GetVirtualPointersSize(const CXXRecordDecl *RD) const;
|
2010-08-19 08:55:19 +08:00
|
|
|
|
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-30 01:42:25 +08:00
|
|
|
void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
2010-11-01 06:20:42 +08:00
|
|
|
void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
|
|
|
|
CharUnits Offset);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
|
|
|
/// LayoutVirtualBases - Lays out all the virtual bases.
|
|
|
|
void LayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *MostDerivedClass);
|
|
|
|
|
|
|
|
/// LayoutVirtualBase - Lays out a single virtual base.
|
2010-05-30 01:48:36 +08:00
|
|
|
void LayoutVirtualBase(const BaseSubobjectInfo *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-11-01 06:20:42 +08:00
|
|
|
/// placed, in chars.
|
|
|
|
CharUnits LayoutBase(const BaseSubobjectInfo *Base);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
2010-05-26 23:10:00 +08:00
|
|
|
/// InitializeLayout - Initialize record layout for the given record decl.
|
2010-05-27 13:45:51 +08:00
|
|
|
void InitializeLayout(const Decl *D);
|
2010-05-26 23:10:00 +08:00
|
|
|
|
2010-05-26 13:41:04 +08:00
|
|
|
/// FinishLayout - Finalize record layout. Adjust record size based on the
|
|
|
|
/// alignment.
|
2010-09-22 22:32:24 +08:00
|
|
|
void FinishLayout(const NamedDecl *D);
|
|
|
|
|
2011-02-20 02:58:07 +08:00
|
|
|
void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
|
|
|
|
void UpdateAlignment(CharUnits NewAlignment) {
|
2010-09-22 22:32:24 +08:00
|
|
|
UpdateAlignment(NewAlignment, NewAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
|
|
|
|
uint64_t UnpackedOffset, unsigned UnpackedAlign,
|
|
|
|
bool isPacked, const FieldDecl *D);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
|
2010-05-26 13:41:04 +08:00
|
|
|
|
2011-02-24 09:13:28 +08:00
|
|
|
CharUnits getSize() const {
|
2011-02-24 09:33:05 +08:00
|
|
|
assert(Size % Context.getCharWidth() == 0);
|
2011-02-24 09:13:28 +08:00
|
|
|
return Context.toCharUnitsFromBits(Size);
|
|
|
|
}
|
|
|
|
uint64_t getSizeInBits() const { return Size; }
|
|
|
|
|
|
|
|
void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
|
|
|
|
void setSize(uint64_t NewSize) { Size = NewSize; }
|
|
|
|
|
|
|
|
CharUnits getDataSize() const {
|
2011-02-24 09:33:05 +08:00
|
|
|
assert(DataSize % Context.getCharWidth() == 0);
|
2011-02-24 09:13:28 +08:00
|
|
|
return Context.toCharUnitsFromBits(DataSize);
|
|
|
|
}
|
|
|
|
uint64_t getDataSizeInBits() const { return DataSize; }
|
|
|
|
|
|
|
|
void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
|
|
|
|
void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
|
|
|
|
|
|
|
|
|
2010-08-15 18:17:39 +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-08-25 08:32:14 +08:00
|
|
|
|
|
|
|
virtual ~RecordLayoutBuilder() { }
|
2010-05-26 13:41:04 +08:00
|
|
|
};
|
2010-05-26 17:58:31 +08:00
|
|
|
} // end anonymous namespace
|
2010-05-26 13:41:04 +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.
|
2010-11-25 09:51:53 +08:00
|
|
|
if (I->isVirtual() && Context.isNearlyEmpty(Base)) {
|
2010-03-11 11:39:12 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 09:22:45 +08:00
|
|
|
CharUnits
|
2010-08-19 08:55:19 +08:00
|
|
|
RecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
|
2011-09-02 08:18:52 +08:00
|
|
|
return Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
2010-08-19 08:55:19 +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.
|
2010-11-25 06:55:48 +08:00
|
|
|
RD->getIndirectPrimaryBases(IndirectPrimaryBases);
|
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.
|
2011-03-01 09:22:45 +08:00
|
|
|
setSize(getSize() + GetVirtualPointersSize(RD));
|
2011-02-28 10:01:38 +08:00
|
|
|
setDataSize(getSize());
|
2010-03-11 08:15:35 +08:00
|
|
|
|
2011-02-20 02:58:07 +08:00
|
|
|
CharUnits UnpackedBaseAlign =
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
|
2011-02-20 02:58:07 +08:00
|
|
|
CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
|
2010-12-09 10:47:58 +08:00
|
|
|
|
|
|
|
// The maximum field alignment overrides base align.
|
2011-02-17 09:49:42 +08:00
|
|
|
if (!MaxFieldAlignment.isZero()) {
|
2011-02-20 02:58:07 +08:00
|
|
|
BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
|
|
|
|
UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
|
2010-12-09 10:47:58 +08:00
|
|
|
}
|
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
// Update the alignment.
|
2010-12-09 10:47:58 +08:00
|
|
|
UpdateAlignment(BaseAlign, UnpackedBaseAlign);
|
2009-08-06 06:37:18 +08:00
|
|
|
}
|
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
BaseSubobjectInfo *
|
|
|
|
RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
|
|
|
|
bool IsVirtual,
|
|
|
|
BaseSubobjectInfo *Derived) {
|
|
|
|
BaseSubobjectInfo *Info;
|
|
|
|
|
|
|
|
if (IsVirtual) {
|
|
|
|
// Check if we already have info about this virtual base.
|
|
|
|
BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
|
|
|
|
if (InfoSlot) {
|
|
|
|
assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
|
|
|
|
return InfoSlot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't, create it.
|
|
|
|
InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
|
|
|
|
Info = InfoSlot;
|
|
|
|
} else {
|
|
|
|
Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
Info->Class = RD;
|
|
|
|
Info->IsVirtual = IsVirtual;
|
|
|
|
Info->Derived = 0;
|
|
|
|
Info->PrimaryVirtualBaseInfo = 0;
|
|
|
|
|
|
|
|
const CXXRecordDecl *PrimaryVirtualBase = 0;
|
|
|
|
BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
|
|
|
|
|
|
|
|
// Check if this base has a primary virtual base.
|
|
|
|
if (RD->getNumVBases()) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2010-11-25 07:12:57 +08:00
|
|
|
if (Layout.isPrimaryBaseVirtual()) {
|
2010-05-30 01:35:14 +08:00
|
|
|
// This base does have a primary virtual base.
|
|
|
|
PrimaryVirtualBase = Layout.getPrimaryBase();
|
|
|
|
assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
|
|
|
|
|
|
|
|
// Now check if we have base subobject info about this primary base.
|
|
|
|
PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
|
|
|
|
|
|
|
|
if (PrimaryVirtualBaseInfo) {
|
|
|
|
if (PrimaryVirtualBaseInfo->Derived) {
|
|
|
|
// We did have info about this primary base, and it turns out that it
|
|
|
|
// has already been claimed as a primary virtual base for another
|
|
|
|
// base.
|
|
|
|
PrimaryVirtualBase = 0;
|
|
|
|
} else {
|
|
|
|
// We can claim this base as our primary base.
|
|
|
|
Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
|
|
|
|
PrimaryVirtualBaseInfo->Derived = Info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through all direct bases.
|
|
|
|
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(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
|
|
|
|
// Traversing the bases must have created the base info for our primary
|
|
|
|
// virtual base.
|
|
|
|
PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
|
|
|
|
assert(PrimaryVirtualBaseInfo &&
|
|
|
|
"Did not create a primary virtual base!");
|
|
|
|
|
|
|
|
// Claim the primary virtual base as our primary virtual base.
|
|
|
|
Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
|
|
|
|
PrimaryVirtualBaseInfo->Derived = Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
|
|
|
|
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());
|
|
|
|
|
|
|
|
// Compute the base subobject info for this base.
|
|
|
|
BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
|
|
|
|
|
|
|
|
if (IsVirtual) {
|
|
|
|
// ComputeBaseInfo has already added this base for us.
|
|
|
|
assert(VirtualBaseInfo.count(BaseDecl) &&
|
|
|
|
"Did not add virtual base!");
|
|
|
|
} else {
|
|
|
|
// 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-03-11 06:21:28 +08:00
|
|
|
void
|
2010-05-26 13:58:59 +08:00
|
|
|
RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
|
2010-05-30 01:35:14 +08:00
|
|
|
// Then, determine the primary base class.
|
2010-03-11 08:15:35 +08:00
|
|
|
DeterminePrimaryBase(RD);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
// Compute base subobject info.
|
|
|
|
ComputeBaseSubobjectInfo(RD);
|
|
|
|
|
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-05-30 01:35:14 +08:00
|
|
|
// If the primary virtual base was a primary virtual base of some other
|
|
|
|
// base class we'll have to steal it.
|
|
|
|
BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
|
|
|
|
PrimaryBaseInfo->Derived = 0;
|
|
|
|
|
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-30 01:48:36 +08:00
|
|
|
LayoutVirtualBase(PrimaryBaseInfo);
|
2010-05-30 01:42:25 +08:00
|
|
|
} else {
|
|
|
|
BaseSubobjectInfo *PrimaryBaseInfo =
|
|
|
|
NonVirtualBaseInfo.lookup(PrimaryBase);
|
|
|
|
assert(PrimaryBaseInfo &&
|
|
|
|
"Did not find base info for non-virtual primary base!");
|
|
|
|
|
|
|
|
LayoutNonVirtualBase(PrimaryBaseInfo);
|
|
|
|
}
|
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;
|
|
|
|
|
2010-05-30 01:42:25 +08:00
|
|
|
const CXXRecordDecl *BaseDecl =
|
2010-03-11 08:15:35 +08:00
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// Skip the primary base.
|
2010-05-30 01:42:25 +08:00
|
|
|
if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
|
2010-03-11 08:15:35 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Lay out the base.
|
2010-05-30 01:42:25 +08:00
|
|
|
BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
|
|
|
|
assert(BaseInfo && "Did not find base info for non-virtual base!");
|
|
|
|
|
|
|
|
LayoutNonVirtualBase(BaseInfo);
|
2010-03-11 06:21:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-30 01:42:25 +08:00
|
|
|
void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
|
2010-03-11 06:26:24 +08:00
|
|
|
// Layout the base.
|
2010-11-01 06:20:42 +08:00
|
|
|
CharUnits Offset = LayoutBase(Base);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 06:26:24 +08:00
|
|
|
// Add its base class offset.
|
2010-05-30 01:42:25 +08:00
|
|
|
assert(!Bases.count(Base->Class) && "base offset already exists!");
|
2010-11-01 06:20:42 +08:00
|
|
|
Bases.insert(std::make_pair(Base->Class, Offset));
|
2010-05-30 03:44:50 +08:00
|
|
|
|
|
|
|
AddPrimaryVirtualBaseOffsets(Base, Offset);
|
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-30 03:44:50 +08:00
|
|
|
RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
|
2010-11-01 06:20:42 +08:00
|
|
|
CharUnits Offset) {
|
2010-05-30 03:44:50 +08:00
|
|
|
// This base isn't interesting, it has no virtual bases.
|
|
|
|
if (!Info->Class->getNumVBases())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// First, check if we have a virtual primary base to add offsets for.
|
|
|
|
if (Info->PrimaryVirtualBaseInfo) {
|
|
|
|
assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
|
|
|
|
"Primary virtual base is not virtual!");
|
|
|
|
if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
|
|
|
|
// Add the offset.
|
|
|
|
assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
|
|
|
|
"primary vbase offset already exists!");
|
|
|
|
VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
|
2010-11-01 06:20:42 +08:00
|
|
|
Offset));
|
2010-05-30 03:44:50 +08:00
|
|
|
|
|
|
|
// Traverse the primary virtual base.
|
|
|
|
AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
|
|
|
|
}
|
2010-04-16 00:12:58 +08:00
|
|
|
}
|
|
|
|
|
2010-05-30 03:44:50 +08:00
|
|
|
// Now go through all direct non-virtual bases.
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
|
|
|
|
for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
|
|
|
|
const BaseSubobjectInfo *Base = Info->Bases[I];
|
|
|
|
if (Base->IsVirtual)
|
2010-04-16 00:12:58 +08:00
|
|
|
continue;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-11-01 08:21:58 +08:00
|
|
|
CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
|
2010-05-30 03:44:50 +08:00
|
|
|
AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
|
2010-04-16 00:12:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-25 07:12:57 +08:00
|
|
|
PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
|
2010-04-11 02:42:27 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-05-30 01:48:36 +08:00
|
|
|
const CXXRecordDecl *BaseDecl =
|
2010-03-11 12:24:02 +08:00
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
if (I->isVirtual()) {
|
2010-05-30 01:48:36 +08:00
|
|
|
if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
|
|
|
|
bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
|
2010-04-11 02:42:27 +08:00
|
|
|
|
|
|
|
// Only lay out the virtual base if it's not an indirect primary base.
|
|
|
|
if (!IndirectPrimaryBase) {
|
|
|
|
// Only visit virtual bases once.
|
2010-05-30 01:48:36 +08:00
|
|
|
if (!VisitedVirtualBases.insert(BaseDecl))
|
2010-04-11 02:42:27 +08:00
|
|
|
continue;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-30 01:48:36 +08:00
|
|
|
const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
|
|
|
|
assert(BaseInfo && "Did not find virtual base info!");
|
|
|
|
LayoutVirtualBase(BaseInfo);
|
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-05-30 01:48:36 +08:00
|
|
|
if (!BaseDecl->getNumVBases()) {
|
2010-03-11 12:24:02 +08:00
|
|
|
// This base isn't interesting since it doesn't have any virtual bases.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-30 01:48:36 +08:00
|
|
|
LayoutVirtualBases(BaseDecl, MostDerivedClass);
|
2009-08-06 21:41:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-30 01:48:36 +08:00
|
|
|
void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
|
2010-05-30 03:44:50 +08:00
|
|
|
assert(!Base->Derived && "Trying to lay out a primary virtual base!");
|
|
|
|
|
2010-03-11 06:26:24 +08:00
|
|
|
// Layout the base.
|
2010-11-01 06:20:42 +08:00
|
|
|
CharUnits Offset = LayoutBase(Base);
|
2010-03-11 06:26:24 +08:00
|
|
|
|
|
|
|
// Add its base class offset.
|
2010-05-30 01:48:36 +08:00
|
|
|
assert(!VBases.count(Base->Class) && "vbase offset already exists!");
|
2010-11-01 06:20:42 +08:00
|
|
|
VBases.insert(std::make_pair(Base->Class, Offset));
|
2010-05-30 03:44:50 +08:00
|
|
|
|
|
|
|
AddPrimaryVirtualBaseOffsets(Base, Offset);
|
2010-03-11 06:21:28 +08:00
|
|
|
}
|
|
|
|
|
2010-11-01 06:20:42 +08:00
|
|
|
CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
|
2010-05-30 04:47:33 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
|
2010-03-11 06:21:28 +08:00
|
|
|
|
|
|
|
// If we have an empty base class, try to place it at offset 0.
|
2010-05-30 04:47:33 +08:00
|
|
|
if (Base->Class->isEmpty() &&
|
2010-11-01 06:13:23 +08:00
|
|
|
EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
|
2011-02-28 10:01:38 +08:00
|
|
|
setSize(std::max(getSize(), Layout.getSize()));
|
2010-03-11 06:21:28 +08:00
|
|
|
|
2010-11-01 06:20:42 +08:00
|
|
|
return CharUnits::Zero();
|
2010-03-11 06:21:28 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2011-02-20 02:58:07 +08:00
|
|
|
CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign();
|
|
|
|
CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
|
2010-12-09 08:35:20 +08:00
|
|
|
|
|
|
|
// The maximum field alignment overrides base align.
|
2011-02-17 09:49:42 +08:00
|
|
|
if (!MaxFieldAlignment.isZero()) {
|
2011-02-20 02:58:07 +08:00
|
|
|
BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
|
|
|
|
UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
|
2010-12-09 08:35:20 +08:00
|
|
|
}
|
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.
|
2011-02-28 10:01:38 +08:00
|
|
|
CharUnits Offset = getDataSize().RoundUpToAlignment(BaseAlign);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-03-11 06:21:28 +08:00
|
|
|
// Try to place the base.
|
2011-02-28 10:01:38 +08:00
|
|
|
while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
|
|
|
|
Offset += BaseAlign;
|
2010-03-11 06:21:28 +08:00
|
|
|
|
2010-05-30 04:47:33 +08:00
|
|
|
if (!Base->Class->isEmpty()) {
|
2010-03-11 06:21:28 +08:00
|
|
|
// Update the data size.
|
2011-02-28 10:01:38 +08:00
|
|
|
setDataSize(Offset + Layout.getNonVirtualSize());
|
2010-03-11 06:21:28 +08:00
|
|
|
|
2011-02-28 10:01:38 +08:00
|
|
|
setSize(std::max(getSize(), getDataSize()));
|
2010-03-11 06:21:28 +08:00
|
|
|
} else
|
2011-02-28 10:01:38 +08:00
|
|
|
setSize(std::max(getSize(), Offset + Layout.getSize()));
|
2010-03-11 06:21:28 +08:00
|
|
|
|
|
|
|
// Remember max struct/class alignment.
|
2010-12-09 08:35:20 +08:00
|
|
|
UpdateAlignment(BaseAlign, UnpackedBaseAlign);
|
2010-03-11 06:21:28 +08:00
|
|
|
|
2011-02-28 10:01:38 +08:00
|
|
|
return Offset;
|
2010-03-11 06:21:28 +08:00
|
|
|
}
|
|
|
|
|
2010-05-27 13:45:51 +08:00
|
|
|
void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
|
|
|
|
if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
|
|
|
|
IsUnion = RD->isUnion();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2009-08-09 03:38:24 +08:00
|
|
|
Packed = D->hasAttr<PackedAttr>();
|
2011-04-27 07:52:16 +08:00
|
|
|
|
|
|
|
IsMsStruct = D->hasAttr<MsStructAttr>();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-05-27 13:45:51 +08:00
|
|
|
// mac68k alignment supersedes maximum field alignment and attribute aligned,
|
|
|
|
// and forces all structures to have 2-byte alignment. The IBM docs on it
|
|
|
|
// allude to additional (more complicated) semantics, especially with regard
|
|
|
|
// to bit-fields, but gcc appears not to follow that.
|
|
|
|
if (D->hasAttr<AlignMac68kAttr>()) {
|
|
|
|
IsMac68kAlign = true;
|
2011-02-17 09:49:42 +08:00
|
|
|
MaxFieldAlignment = CharUnits::fromQuantity(2);
|
2011-02-16 10:05:21 +08:00
|
|
|
Alignment = CharUnits::fromQuantity(2);
|
2010-05-27 13:45:51 +08:00
|
|
|
} else {
|
|
|
|
if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
|
2011-02-17 09:49:42 +08:00
|
|
|
MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
if (unsigned MaxAlign = D->getMaxAlignment())
|
2011-02-20 02:58:07 +08:00
|
|
|
UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
|
2010-05-27 13:45:51 +08:00
|
|
|
}
|
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.
|
2010-09-22 22:32:24 +08:00
|
|
|
FinishLayout(D);
|
2010-05-26 23:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
|
|
|
|
InitializeLayout(RD);
|
|
|
|
|
|
|
|
// Lay out the vtable and the non-virtual bases.
|
|
|
|
LayoutNonVirtualBases(RD);
|
|
|
|
|
|
|
|
LayoutFields(RD);
|
|
|
|
|
2011-03-10 09:53:59 +08:00
|
|
|
NonVirtualSize = Context.toCharUnitsFromBits(
|
|
|
|
llvm::RoundUpToAlignment(getSizeInBits(),
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getCharAlign()));
|
2011-02-16 10:05:21 +08:00
|
|
|
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();
|
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.
|
2010-09-22 22:32:24 +08:00
|
|
|
FinishLayout(RD);
|
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
|
|
|
|
2011-02-20 02:58:07 +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.
|
2011-02-24 09:13:28 +08:00
|
|
|
setSize(SL.getDataSize());
|
2011-02-28 10:01:38 +08:00
|
|
|
setDataSize(getSize());
|
2009-07-19 04:50:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-27 13:45:51 +08:00
|
|
|
InitializeLayout(D);
|
2009-07-19 04:50:59 +08:00
|
|
|
// Layout each ivar sequentially.
|
2011-07-22 10:08:32 +08:00
|
|
|
for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD;
|
|
|
|
IVD = IVD->getNextIvar())
|
2011-06-29 02:05:25 +08:00
|
|
|
LayoutField(IVD);
|
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.
|
2010-09-22 22:32:24 +08:00
|
|
|
FinishLayout(D);
|
2009-07-19 04:50:59 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2011-04-27 07:52:16 +08:00
|
|
|
const FieldDecl *LastFD = 0;
|
2011-05-04 04:21:04 +08:00
|
|
|
ZeroLengthBitfield = 0;
|
2011-05-12 00:58:31 +08:00
|
|
|
unsigned RemainingInAlignment = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = D->field_begin(),
|
2011-04-27 07:52:16 +08:00
|
|
|
FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
|
|
|
|
if (IsMsStruct) {
|
2011-05-04 04:21:04 +08:00
|
|
|
FieldDecl *FD = (*Field);
|
|
|
|
if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD))
|
|
|
|
ZeroLengthBitfield = FD;
|
2011-04-27 07:52:16 +08:00
|
|
|
// Zero-length bitfields following non-bitfield members are
|
|
|
|
// ignored:
|
2011-05-04 04:21:04 +08:00
|
|
|
else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
|
2011-04-27 07:52:16 +08:00
|
|
|
continue;
|
2011-05-07 06:42:22 +08:00
|
|
|
// FIXME. streamline these conditions into a simple one.
|
2011-05-07 05:56:12 +08:00
|
|
|
else if (Context.BitfieldFollowsBitfield(FD, LastFD) ||
|
2011-08-05 07:34:15 +08:00
|
|
|
Context.BitfieldFollowsNonBitfield(FD, LastFD) ||
|
|
|
|
Context.NonBitfieldFollowsBitfield(FD, LastFD)) {
|
2011-05-07 06:42:22 +08:00
|
|
|
// 1) Adjacent bit fields are packed into the same 1-, 2-, or
|
2011-05-05 02:51:37 +08:00
|
|
|
// 4-byte allocation unit if the integral types are the same
|
|
|
|
// size and if the next bit field fits into the current
|
|
|
|
// allocation unit without crossing the boundary imposed by the
|
|
|
|
// common alignment requirements of the bit fields.
|
2011-05-07 06:42:22 +08:00
|
|
|
// 2) Establish a new alignment for a bitfield following
|
2011-05-07 05:56:12 +08:00
|
|
|
// a non-bitfield if size of their types differ.
|
2011-05-07 06:42:22 +08:00
|
|
|
// 3) Establish a new alignment for a non-bitfield following
|
|
|
|
// a bitfield if size of their types differ.
|
2011-05-05 02:51:37 +08:00
|
|
|
std::pair<uint64_t, unsigned> FieldInfo =
|
|
|
|
Context.getTypeInfo(FD->getType());
|
|
|
|
uint64_t TypeSize = FieldInfo.first;
|
|
|
|
unsigned FieldAlign = FieldInfo.second;
|
2011-05-10 06:03:17 +08:00
|
|
|
// This check is needed for 'long long' in -m32 mode.
|
|
|
|
if (TypeSize > FieldAlign)
|
|
|
|
FieldAlign = TypeSize;
|
2011-05-05 02:51:37 +08:00
|
|
|
FieldInfo = Context.getTypeInfo(LastFD->getType());
|
|
|
|
uint64_t TypeSizeLastFD = FieldInfo.first;
|
|
|
|
unsigned FieldAlignLastFD = FieldInfo.second;
|
2011-05-10 06:03:17 +08:00
|
|
|
// This check is needed for 'long long' in -m32 mode.
|
|
|
|
if (TypeSizeLastFD > FieldAlignLastFD)
|
|
|
|
FieldAlignLastFD = TypeSizeLastFD;
|
2011-05-12 00:58:31 +08:00
|
|
|
|
2011-05-05 02:51:37 +08:00
|
|
|
if (TypeSizeLastFD != TypeSize) {
|
2011-05-12 00:58:31 +08:00
|
|
|
if (RemainingInAlignment &&
|
|
|
|
LastFD && LastFD->isBitField() &&
|
|
|
|
LastFD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
|
|
|
|
// If previous field was a bitfield with some remaining unfilled
|
|
|
|
// bits, pad the field so current field starts on its type boundary.
|
|
|
|
uint64_t FieldOffset =
|
|
|
|
getDataSizeInBits() - UnfilledBitsInLastByte;
|
|
|
|
uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
|
|
|
|
setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getCharAlign()));
|
2011-05-12 00:58:31 +08:00
|
|
|
setSize(std::max(getSizeInBits(), getDataSizeInBits()));
|
|
|
|
RemainingInAlignment = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-05 02:51:37 +08:00
|
|
|
uint64_t UnpaddedFieldOffset =
|
|
|
|
getDataSizeInBits() - UnfilledBitsInLastByte;
|
|
|
|
FieldAlign = std::max(FieldAlign, FieldAlignLastFD);
|
2011-05-12 00:58:31 +08:00
|
|
|
|
2011-05-11 03:00:50 +08:00
|
|
|
// The maximum field alignment overrides the aligned attribute.
|
|
|
|
if (!MaxFieldAlignment.isZero()) {
|
|
|
|
unsigned MaxFieldAlignmentInBits =
|
|
|
|
Context.toBits(MaxFieldAlignment);
|
|
|
|
FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
|
|
|
|
}
|
2011-05-12 00:58:31 +08:00
|
|
|
|
2011-05-05 02:51:37 +08:00
|
|
|
uint64_t NewSizeInBits =
|
|
|
|
llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign);
|
|
|
|
setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getCharAlign()));
|
2011-05-05 02:51:37 +08:00
|
|
|
UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
|
|
|
|
setSize(std::max(getSizeInBits(), getDataSizeInBits()));
|
|
|
|
}
|
2011-05-12 00:58:31 +08:00
|
|
|
if (FD->isBitField()) {
|
|
|
|
uint64_t FieldSize =
|
|
|
|
FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
|
|
|
|
assert (FieldSize > 0 && "LayoutFields - ms_struct layout");
|
|
|
|
if (RemainingInAlignment < FieldSize)
|
|
|
|
RemainingInAlignment = TypeSize - FieldSize;
|
|
|
|
else
|
|
|
|
RemainingInAlignment -= FieldSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (FD->isBitField()) {
|
|
|
|
uint64_t FieldSize =
|
|
|
|
FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
|
|
|
|
std::pair<uint64_t, unsigned> FieldInfo =
|
|
|
|
Context.getTypeInfo(FD->getType());
|
|
|
|
uint64_t TypeSize = FieldInfo.first;
|
|
|
|
RemainingInAlignment = TypeSize - FieldSize;
|
2011-05-05 02:51:37 +08:00
|
|
|
}
|
2011-04-27 07:52:16 +08:00
|
|
|
LastFD = FD;
|
|
|
|
}
|
2011-09-02 08:18:52 +08:00
|
|
|
else if (!Context.getTargetInfo().useBitFieldTypeAlignment() &&
|
|
|
|
Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
|
2011-08-04 09:21:14 +08:00
|
|
|
FieldDecl *FD = (*Field);
|
2011-08-06 06:38:04 +08:00
|
|
|
if (FD->isBitField() &&
|
|
|
|
FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue() == 0)
|
2011-08-04 09:21:14 +08:00
|
|
|
ZeroLengthBitfield = FD;
|
|
|
|
}
|
2009-07-19 05:48:39 +08:00
|
|
|
LayoutField(*Field);
|
2011-04-27 07:52:16 +08:00
|
|
|
}
|
2011-05-12 00:58:31 +08:00
|
|
|
if (IsMsStruct && RemainingInAlignment &&
|
|
|
|
LastFD && LastFD->isBitField() &&
|
|
|
|
LastFD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
|
|
|
|
// If we ended a bitfield before the full length of the type then
|
|
|
|
// pad the struct out to the full length of the last type.
|
|
|
|
uint64_t FieldOffset =
|
|
|
|
getDataSizeInBits() - UnfilledBitsInLastByte;
|
|
|
|
uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
|
|
|
|
setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getCharAlign()));
|
2011-05-12 00:58:31 +08:00
|
|
|
setSize(std::max(getSizeInBits(), getDataSizeInBits()));
|
|
|
|
}
|
2009-07-19 05:48:39 +08:00
|
|
|
}
|
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
|
2010-09-22 22:32:24 +08:00
|
|
|
uint64_t TypeSize,
|
|
|
|
bool FieldPacked,
|
|
|
|
const FieldDecl *D) {
|
2010-04-16 23:57:11 +08:00
|
|
|
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
|
|
|
|
2011-03-01 09:36:00 +08:00
|
|
|
CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
|
2010-04-16 23:57:11 +08:00
|
|
|
|
|
|
|
// 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;
|
2011-02-24 09:13:28 +08:00
|
|
|
uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
if (IsUnion) {
|
2011-02-24 09:13:28 +08:00
|
|
|
setDataSize(std::max(getDataSizeInBits(), FieldSize));
|
2010-04-18 04:21:41 +08:00
|
|
|
FieldOffset = 0;
|
2010-04-16 23:57:11 +08:00
|
|
|
} else {
|
2011-08-06 06:38:04 +08:00
|
|
|
// The bitfield is allocated starting at the next offset aligned
|
|
|
|
// appropriately for T', with length n bits.
|
2011-03-01 09:36:00 +08:00
|
|
|
FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
|
|
|
|
Context.toBits(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
|
|
|
|
2011-03-10 10:00:35 +08:00
|
|
|
setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getCharAlign()));
|
2011-02-24 09:13:28 +08:00
|
|
|
UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
|
2010-04-16 23:57:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place this field at the current location.
|
|
|
|
FieldOffsets.push_back(FieldOffset);
|
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
|
2011-03-01 09:36:00 +08:00
|
|
|
Context.toBits(TypeAlign), FieldPacked, D);
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
// Update the size.
|
2011-02-24 09:13:28 +08:00
|
|
|
setSize(std::max(getSizeInBits(), getDataSizeInBits()));
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
// Remember max struct/class alignment.
|
2011-03-01 09:36:00 +08:00
|
|
|
UpdateAlignment(TypeAlign);
|
2010-04-16 23:57:11 +08:00
|
|
|
}
|
|
|
|
|
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>();
|
2011-02-24 09:13:28 +08:00
|
|
|
uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
|
2010-09-22 22:32:24 +08:00
|
|
|
uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
|
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;
|
2011-05-04 04:21:04 +08:00
|
|
|
|
2011-05-10 06:03:17 +08:00
|
|
|
// This check is needed for 'long long' in -m32 mode.
|
|
|
|
if (IsMsStruct && (TypeSize > FieldAlign))
|
|
|
|
FieldAlign = TypeSize;
|
2011-08-04 09:21:14 +08:00
|
|
|
|
2011-05-04 04:21:04 +08:00
|
|
|
if (ZeroLengthBitfield) {
|
2011-08-06 06:38:04 +08:00
|
|
|
std::pair<uint64_t, unsigned> FieldInfo;
|
|
|
|
unsigned ZeroLengthBitfieldAlignment;
|
|
|
|
if (IsMsStruct) {
|
|
|
|
// If a zero-length bitfield is inserted after a bitfield,
|
|
|
|
// and the alignment of the zero-length bitfield is
|
|
|
|
// greater than the member that follows it, `bar', `bar'
|
|
|
|
// will be aligned as the type of the zero-length bitfield.
|
|
|
|
if (ZeroLengthBitfield != D) {
|
|
|
|
FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType());
|
|
|
|
ZeroLengthBitfieldAlignment = FieldInfo.second;
|
|
|
|
// Ignore alignment of subsequent zero-length bitfields.
|
|
|
|
if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0))
|
|
|
|
FieldAlign = ZeroLengthBitfieldAlignment;
|
|
|
|
if (FieldSize)
|
|
|
|
ZeroLengthBitfield = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The alignment of a zero-length bitfield affects the alignment
|
|
|
|
// of the next member. The alignment is the max of the zero
|
|
|
|
// length bitfield's alignment and a target specific fixed value.
|
|
|
|
unsigned ZeroLengthBitfieldBoundary =
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getZeroLengthBitfieldBoundary();
|
2011-08-06 06:38:04 +08:00
|
|
|
if (ZeroLengthBitfieldBoundary > FieldAlign)
|
|
|
|
FieldAlign = ZeroLengthBitfieldBoundary;
|
2011-05-04 04:21:04 +08:00
|
|
|
}
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
if (FieldSize > TypeSize) {
|
2010-09-22 22:32:24 +08:00
|
|
|
LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
|
2010-04-16 23:57:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
// The align if the field is not packed. This is to check if the attribute
|
|
|
|
// was unnecessary (-Wpacked).
|
|
|
|
unsigned UnpackedFieldAlign = FieldAlign;
|
|
|
|
uint64_t UnpackedFieldOffset = FieldOffset;
|
2011-09-02 08:18:52 +08:00
|
|
|
if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)
|
2010-09-22 22:32:24 +08:00
|
|
|
UnpackedFieldAlign = 1;
|
|
|
|
|
2011-08-06 06:38:04 +08:00
|
|
|
if (FieldPacked ||
|
2011-09-02 08:18:52 +08:00
|
|
|
(!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield))
|
2009-11-23 01:37:31 +08:00
|
|
|
FieldAlign = 1;
|
2010-08-19 07:23:40 +08:00
|
|
|
FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
|
2010-09-22 22:32:24 +08:00
|
|
|
UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->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.
|
2011-02-17 09:49:42 +08:00
|
|
|
if (!MaxFieldAlignment.isZero()) {
|
|
|
|
unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
|
|
|
|
FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
|
|
|
|
UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
|
2010-09-22 22:32:24 +08:00
|
|
|
}
|
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)
|
2010-06-30 02:34:35 +08:00
|
|
|
FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
if (FieldSize == 0 ||
|
|
|
|
(UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)
|
|
|
|
UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
|
|
|
|
UnpackedFieldAlign);
|
|
|
|
|
2011-08-06 06:38:04 +08:00
|
|
|
// Padding members don't affect overall alignment, unless zero length bitfield
|
|
|
|
// alignment is enabled.
|
2011-09-02 08:18:52 +08:00
|
|
|
if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment())
|
2010-09-22 22:32:24 +08:00
|
|
|
FieldAlign = UnpackedFieldAlign = 1;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2011-08-06 06:38:04 +08:00
|
|
|
if (!IsMsStruct)
|
|
|
|
ZeroLengthBitfield = 0;
|
|
|
|
|
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
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
|
|
|
|
UnpackedFieldAlign, FieldPacked, D);
|
|
|
|
|
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.
|
2011-02-24 09:13:28 +08:00
|
|
|
setDataSize(std::max(getDataSizeInBits(), FieldSize));
|
2009-11-23 03:13:51 +08:00
|
|
|
} else {
|
|
|
|
uint64_t NewSizeInBits = FieldOffset + FieldSize;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2011-03-10 10:00:35 +08:00
|
|
|
setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getCharAlign()));
|
2011-02-24 09:13:28 +08:00
|
|
|
UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
|
2009-11-23 03:13:51 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 03:13:51 +08:00
|
|
|
// Update the size.
|
2011-02-24 09:13:28 +08:00
|
|
|
setSize(std::max(getSizeInBits(), getDataSizeInBits()));
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// Remember max struct/class alignment.
|
2011-02-20 02:58:07 +08:00
|
|
|
UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
|
|
|
|
Context.toCharUnitsFromBits(UnpackedFieldAlign));
|
2009-11-23 01:37:31 +08:00
|
|
|
}
|
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
|
|
|
|
2011-02-24 09:13:28 +08:00
|
|
|
uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
|
2010-09-22 22:32:24 +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>();
|
2011-02-20 10:06:09 +08:00
|
|
|
CharUnits FieldOffset =
|
2011-02-24 09:13:28 +08:00
|
|
|
IsUnion ? CharUnits::Zero() : getDataSize();
|
2011-02-20 10:06:09 +08:00
|
|
|
CharUnits FieldSize;
|
|
|
|
CharUnits 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.
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldSize = CharUnits::Zero();
|
2010-04-16 23:07:51 +08:00
|
|
|
const ArrayType* ATy = Context.getAsArrayType(D->getType());
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
|
2009-11-23 01:37:31 +08:00
|
|
|
} else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
|
|
|
|
unsigned AS = RT->getPointeeType().getAddressSpace();
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldSize =
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS));
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldAlign =
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS));
|
2009-11-23 01:37:31 +08:00
|
|
|
} else {
|
2011-02-20 10:06:09 +08:00
|
|
|
std::pair<CharUnits, CharUnits> FieldInfo =
|
|
|
|
Context.getTypeInfoInChars(D->getType());
|
2009-11-23 01:37:31 +08:00
|
|
|
FieldSize = FieldInfo.first;
|
2009-07-19 04:20:21 +08:00
|
|
|
FieldAlign = FieldInfo.second;
|
2011-08-04 09:21:14 +08:00
|
|
|
|
2011-05-04 04:21:04 +08:00
|
|
|
if (ZeroLengthBitfield) {
|
2011-08-04 09:21:14 +08:00
|
|
|
CharUnits ZeroLengthBitfieldBoundary =
|
|
|
|
Context.toCharUnitsFromBits(
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getZeroLengthBitfieldBoundary());
|
2011-08-04 09:21:14 +08:00
|
|
|
if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) {
|
|
|
|
// If a zero-length bitfield is inserted after a bitfield,
|
|
|
|
// and the alignment of the zero-length bitfield is
|
|
|
|
// greater than the member that follows it, `bar', `bar'
|
|
|
|
// will be aligned as the type of the zero-length bitfield.
|
|
|
|
std::pair<CharUnits, CharUnits> FieldInfo =
|
|
|
|
Context.getTypeInfoInChars(ZeroLengthBitfield->getType());
|
|
|
|
CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second;
|
|
|
|
if (ZeroLengthBitfieldAlignment > FieldAlign)
|
|
|
|
FieldAlign = ZeroLengthBitfieldAlignment;
|
2011-08-05 03:25:14 +08:00
|
|
|
} else if (ZeroLengthBitfieldBoundary > FieldAlign) {
|
2011-08-04 09:21:14 +08:00
|
|
|
// Align 'bar' based on a fixed alignment specified by the target.
|
2011-09-02 08:18:52 +08:00
|
|
|
assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
|
2011-08-05 01:52:43 +08:00
|
|
|
"ZeroLengthBitfieldBoundary should only be used in conjunction"
|
|
|
|
" with useZeroLengthBitfieldAlignment.");
|
2011-08-04 09:21:14 +08:00
|
|
|
FieldAlign = ZeroLengthBitfieldBoundary;
|
|
|
|
}
|
2011-05-04 04:21:04 +08:00
|
|
|
ZeroLengthBitfield = 0;
|
|
|
|
}
|
2011-02-01 23:15:22 +08:00
|
|
|
|
2011-05-05 02:51:37 +08:00
|
|
|
if (Context.getLangOptions().MSBitfields || IsMsStruct) {
|
2011-02-01 23:15:22 +08:00
|
|
|
// If MS bitfield layout is required, figure out what type is being
|
|
|
|
// laid out and align the field to the width of that type.
|
|
|
|
|
|
|
|
// Resolve all typedefs down to their base type and round up the field
|
|
|
|
// alignment if necessary.
|
|
|
|
QualType T = Context.getBaseElementType(D->getType());
|
|
|
|
if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
|
2011-02-20 10:06:09 +08:00
|
|
|
CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
|
2011-02-01 23:15:22 +08:00
|
|
|
if (TypeSize > FieldAlign)
|
|
|
|
FieldAlign = TypeSize;
|
|
|
|
}
|
|
|
|
}
|
2009-11-23 01:37:31 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
// The align if the field is not packed. This is to check if the attribute
|
|
|
|
// was unnecessary (-Wpacked).
|
2011-02-20 10:06:09 +08:00
|
|
|
CharUnits UnpackedFieldAlign = FieldAlign;
|
|
|
|
CharUnits UnpackedFieldOffset = FieldOffset;
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
if (FieldPacked)
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldAlign = CharUnits::One();
|
|
|
|
CharUnits MaxAlignmentInChars =
|
|
|
|
Context.toCharUnitsFromBits(D->getMaxAlignment());
|
|
|
|
FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
|
|
|
|
UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-23 01:37:31 +08:00
|
|
|
// The maximum field alignment overrides the aligned attribute.
|
2011-02-17 09:49:42 +08:00
|
|
|
if (!MaxFieldAlignment.isZero()) {
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
|
|
|
|
UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
|
2010-09-22 22:32:24 +08:00
|
|
|
}
|
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.
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
|
|
|
|
UnpackedFieldOffset =
|
|
|
|
UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-05-30 14:52:33 +08:00
|
|
|
if (!IsUnion && EmptySubobjects) {
|
|
|
|
// Check if we can place the field at this offset.
|
2011-02-20 10:06:09 +08:00
|
|
|
while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
|
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
|
|
|
}
|
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.
|
2011-02-20 10:06:09 +08:00
|
|
|
FieldOffsets.push_back(Context.toBits(FieldOffset));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-20 10:06:09 +08:00
|
|
|
CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
|
|
|
|
Context.toBits(UnpackedFieldOffset),
|
|
|
|
Context.toBits(UnpackedFieldAlign), FieldPacked, D);
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
// Reserve space for this field.
|
2011-02-25 00:40:53 +08:00
|
|
|
uint64_t FieldSizeInBits = Context.toBits(FieldSize);
|
2009-07-19 04:20:21 +08:00
|
|
|
if (IsUnion)
|
2011-02-25 00:40:53 +08:00
|
|
|
setSize(std::max(getSizeInBits(), FieldSizeInBits));
|
2009-07-19 04:20:21 +08:00
|
|
|
else
|
2011-02-28 10:01:38 +08:00
|
|
|
setSize(FieldOffset + FieldSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-26 09:34:51 +08:00
|
|
|
// Update the data size.
|
2011-02-25 00:40:53 +08:00
|
|
|
setDataSize(getSizeInBits());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
// Remember max struct/class alignment.
|
2011-02-20 10:06:09 +08:00
|
|
|
UpdateAlignment(FieldAlign, UnpackedFieldAlign);
|
2009-07-19 04:20:21 +08:00
|
|
|
}
|
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
|
2009-07-19 04:20:21 +08:00
|
|
|
// In C++, records cannot be of size 0.
|
2011-02-24 09:13:28 +08:00
|
|
|
if (Context.getLangOptions().CPlusPlus && getSizeInBits() == 0) {
|
2011-02-03 03:36:18 +08:00
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
// Compatibility with gcc requires a class (pod or non-pod)
|
|
|
|
// which is not empty but of size 0; such as having fields of
|
|
|
|
// array of zero-length, remains of Size 0
|
|
|
|
if (RD->isEmpty())
|
2011-02-28 10:01:38 +08:00
|
|
|
setSize(CharUnits::One());
|
2011-02-03 03:36:18 +08:00
|
|
|
}
|
|
|
|
else
|
2011-02-28 10:01:38 +08:00
|
|
|
setSize(CharUnits::One());
|
2011-02-03 03:36:18 +08:00
|
|
|
}
|
2009-07-19 04:20:21 +08:00
|
|
|
// Finally, round the size of the record up to the alignment of the
|
|
|
|
// record itself.
|
2011-02-24 09:13:28 +08:00
|
|
|
uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
|
2011-02-28 10:01:38 +08:00
|
|
|
uint64_t UnpackedSizeInBits =
|
2011-02-24 09:13:28 +08:00
|
|
|
llvm::RoundUpToAlignment(getSizeInBits(),
|
|
|
|
Context.toBits(UnpackedAlignment));
|
2011-02-28 10:01:38 +08:00
|
|
|
CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
|
2011-02-24 09:13:28 +08:00
|
|
|
setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)));
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2011-09-02 08:18:52 +08:00
|
|
|
unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
|
2010-09-22 22:32:24 +08:00
|
|
|
if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
|
|
|
|
// Warn if padding was introduced to the struct/class/union.
|
2011-02-24 09:13:28 +08:00
|
|
|
if (getSizeInBits() > UnpaddedSize) {
|
|
|
|
unsigned PadSize = getSizeInBits() - UnpaddedSize;
|
2010-09-22 22:32:24 +08:00
|
|
|
bool InBits = true;
|
|
|
|
if (PadSize % CharBitNum == 0) {
|
|
|
|
PadSize = PadSize / CharBitNum;
|
|
|
|
InBits = false;
|
|
|
|
}
|
|
|
|
Diag(RD->getLocation(), diag::warn_padded_struct_size)
|
|
|
|
<< Context.getTypeDeclType(RD)
|
|
|
|
<< PadSize
|
|
|
|
<< (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn if we packed it unnecessarily. If the alignment is 1 byte don't
|
|
|
|
// bother since there won't be alignment issues.
|
2011-02-24 09:13:28 +08:00
|
|
|
if (Packed && UnpackedAlignment > CharUnits::One() &&
|
2011-02-28 10:01:38 +08:00
|
|
|
getSize() == UnpackedSize)
|
2010-09-22 22:32:24 +08:00
|
|
|
Diag(D->getLocation(), diag::warn_unnecessary_packed)
|
|
|
|
<< Context.getTypeDeclType(RD);
|
|
|
|
}
|
2009-07-19 04:20:21 +08:00
|
|
|
}
|
|
|
|
|
2011-02-20 02:58:07 +08:00
|
|
|
void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
|
|
|
|
CharUnits UnpackedNewAlignment) {
|
2010-05-27 13:45:51 +08:00
|
|
|
// The alignment is not modified when using 'mac68k' alignment.
|
|
|
|
if (IsMac68kAlign)
|
|
|
|
return;
|
|
|
|
|
2011-02-20 02:58:07 +08:00
|
|
|
if (NewAlignment > Alignment) {
|
|
|
|
assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
|
|
|
|
"Alignment not a power of 2"));
|
|
|
|
Alignment = NewAlignment;
|
2010-09-22 22:32:24 +08:00
|
|
|
}
|
|
|
|
|
2011-02-20 02:58:07 +08:00
|
|
|
if (UnpackedNewAlignment > UnpackedAlignment) {
|
|
|
|
assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
|
2010-09-22 22:32:24 +08:00
|
|
|
"Alignment not a power of 2"));
|
2011-02-20 02:58:07 +08:00
|
|
|
UnpackedAlignment = UnpackedNewAlignment;
|
2010-09-22 22:32:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
|
|
|
|
uint64_t UnpaddedOffset,
|
|
|
|
uint64_t UnpackedOffset,
|
|
|
|
unsigned UnpackedAlign,
|
|
|
|
bool isPacked,
|
|
|
|
const FieldDecl *D) {
|
|
|
|
// We let objc ivars without warning, objc interfaces generally are not used
|
|
|
|
// for padding tricks.
|
|
|
|
if (isa<ObjCIvarDecl>(D))
|
2009-07-19 04:20:21 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-07 03:40:45 +08:00
|
|
|
// Don't warn about structs created without a SourceLocation. This can
|
|
|
|
// be done by clients of the AST, such as codegen.
|
|
|
|
if (D->getLocation().isInvalid())
|
|
|
|
return;
|
|
|
|
|
2011-09-02 08:18:52 +08:00
|
|
|
unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
// Warn if padding was introduced to the struct/class.
|
|
|
|
if (!IsUnion && Offset > UnpaddedOffset) {
|
|
|
|
unsigned PadSize = Offset - UnpaddedOffset;
|
|
|
|
bool InBits = true;
|
|
|
|
if (PadSize % CharBitNum == 0) {
|
|
|
|
PadSize = PadSize / CharBitNum;
|
|
|
|
InBits = false;
|
|
|
|
}
|
|
|
|
if (D->getIdentifier())
|
|
|
|
Diag(D->getLocation(), diag::warn_padded_struct_field)
|
|
|
|
<< (D->getParent()->isStruct() ? 0 : 1) // struct|class
|
|
|
|
<< Context.getTypeDeclType(D->getParent())
|
|
|
|
<< PadSize
|
|
|
|
<< (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
|
|
|
|
<< D->getIdentifier();
|
|
|
|
else
|
|
|
|
Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
|
|
|
|
<< (D->getParent()->isStruct() ? 0 : 1) // struct|class
|
|
|
|
<< Context.getTypeDeclType(D->getParent())
|
|
|
|
<< PadSize
|
|
|
|
<< (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn if we packed it unnecessarily. If the alignment is 1 byte don't
|
|
|
|
// bother since there won't be alignment issues.
|
|
|
|
if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
|
|
|
|
Diag(D->getLocation(), diag::warn_unnecessary_packed)
|
|
|
|
<< D->getIdentifier();
|
2009-07-19 04:20:21 +08:00
|
|
|
}
|
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) {
|
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
|
|
|
|
2011-06-11 05:53:06 +08:00
|
|
|
// A class that is not externally visible doesn't have a key function. (Or
|
2009-12-08 11:56:49 +08:00
|
|
|
// at least, there's no point to assigning a key function to such a class;
|
|
|
|
// this doesn't affect the ABI.)
|
2011-06-11 05:53:06 +08:00
|
|
|
if (RD->getLinkage() != ExternalLinkage)
|
2009-12-08 11:56:49 +08:00
|
|
|
return 0;
|
|
|
|
|
2010-10-13 10:39:41 +08:00
|
|
|
// Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
|
|
|
|
// Same behavior as GCC.
|
|
|
|
TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
|
|
|
|
if (TSK == TSK_ImplicitInstantiation ||
|
|
|
|
TSK == TSK_ExplicitInstantiationDefinition)
|
|
|
|
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-09-22 22:32:24 +08:00
|
|
|
DiagnosticBuilder
|
|
|
|
RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
|
2010-11-19 04:06:41 +08:00
|
|
|
return Context.getDiagnostics().Report(Loc, DiagID);
|
2010-09-22 22:32:24 +08:00
|
|
|
}
|
|
|
|
|
2010-10-23 00:33:16 +08:00
|
|
|
namespace {
|
|
|
|
// This class implements layout specific to the Microsoft ABI.
|
|
|
|
class MSRecordLayoutBuilder : public RecordLayoutBuilder {
|
|
|
|
public:
|
2011-01-12 17:06:06 +08:00
|
|
|
MSRecordLayoutBuilder(const ASTContext& Ctx,
|
|
|
|
EmptySubobjectMap *EmptySubobjects) :
|
2010-10-23 00:33:16 +08:00
|
|
|
RecordLayoutBuilder(Ctx, EmptySubobjects) {}
|
|
|
|
|
2011-03-01 09:22:45 +08:00
|
|
|
virtual CharUnits GetVirtualPointersSize(const CXXRecordDecl *RD) const;
|
2010-10-23 00:33:16 +08:00
|
|
|
};
|
|
|
|
}
|
2010-08-19 08:55:19 +08:00
|
|
|
|
2011-03-01 09:22:45 +08:00
|
|
|
CharUnits
|
2010-08-19 08:55:19 +08:00
|
|
|
MSRecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
|
|
|
|
// We should reserve space for two pointers if the class has both
|
|
|
|
// virtual functions and virtual bases.
|
2011-03-01 09:22:45 +08:00
|
|
|
CharUnits PointerWidth =
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
2010-08-19 08:55:19 +08:00
|
|
|
if (RD->isPolymorphic() && RD->getNumVBases() > 0)
|
2011-03-01 09:22:45 +08:00
|
|
|
return 2 * PointerWidth;
|
|
|
|
return PointerWidth;
|
2010-08-19 08:55:19 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2011-01-12 17:06:06 +08:00
|
|
|
const ASTRecordLayout &
|
|
|
|
ASTContext::getASTRecordLayout(const RecordDecl *D) const {
|
2010-05-26 12:56:53 +08:00
|
|
|
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-08-19 08:55:19 +08:00
|
|
|
// When compiling for Microsoft, use the special MS builder.
|
2010-08-25 08:32:14 +08:00
|
|
|
llvm::OwningPtr<RecordLayoutBuilder> Builder;
|
2011-09-02 08:18:52 +08:00
|
|
|
switch (Target->getCXXABI()) {
|
2010-08-19 10:18:14 +08:00
|
|
|
default:
|
2010-08-25 08:32:14 +08:00
|
|
|
Builder.reset(new RecordLayoutBuilder(*this, &EmptySubobjects));
|
2010-08-19 10:18:14 +08:00
|
|
|
break;
|
|
|
|
case CXXABI_Microsoft:
|
2010-08-25 08:32:14 +08:00
|
|
|
Builder.reset(new MSRecordLayoutBuilder(*this, &EmptySubobjects));
|
2010-08-19 10:18:14 +08:00
|
|
|
}
|
2011-03-19 09:00:36 +08:00
|
|
|
// Recover resources if we crash before exiting this method.
|
2011-03-22 09:15:19 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder>
|
|
|
|
RecordBuilderCleanup(Builder.get());
|
2011-03-19 09:00:36 +08:00
|
|
|
|
2010-08-19 08:55:19 +08:00
|
|
|
Builder->Layout(RD);
|
2010-05-26 13:10:47 +08:00
|
|
|
|
|
|
|
// 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.
|
2011-02-28 10:01:38 +08:00
|
|
|
CharUnits DataSize =
|
|
|
|
IsPODForThePurposeOfLayout ? Builder->getSize() : Builder->getDataSize();
|
|
|
|
CharUnits NonVirtualSize =
|
|
|
|
IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;
|
2010-05-26 13:10:47 +08:00
|
|
|
|
2010-05-27 10:25:46 +08:00
|
|
|
NewEntry =
|
2011-02-28 10:01:38 +08:00
|
|
|
new (*this) ASTRecordLayout(*this, Builder->getSize(),
|
2011-02-16 10:05:21 +08:00
|
|
|
Builder->Alignment,
|
2011-02-28 10:01:38 +08:00
|
|
|
DataSize,
|
2011-02-11 10:20:09 +08:00
|
|
|
Builder->FieldOffsets.data(),
|
2010-08-19 08:55:19 +08:00
|
|
|
Builder->FieldOffsets.size(),
|
2011-02-16 09:52:01 +08:00
|
|
|
NonVirtualSize,
|
2011-02-16 09:43:15 +08:00
|
|
|
Builder->NonVirtualAlignment,
|
2010-05-27 08:07:01 +08:00
|
|
|
EmptySubobjects.SizeOfLargestEmptySubobject,
|
2010-08-19 08:55:19 +08:00
|
|
|
Builder->PrimaryBase,
|
|
|
|
Builder->PrimaryBaseIsVirtual,
|
|
|
|
Builder->Bases, Builder->VBases);
|
2010-05-26 13:10:47 +08:00
|
|
|
} 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 =
|
2011-02-28 10:01:38 +08:00
|
|
|
new (*this) ASTRecordLayout(*this, Builder.getSize(),
|
2011-02-16 10:05:21 +08:00
|
|
|
Builder.Alignment,
|
2011-02-28 10:01:38 +08:00
|
|
|
Builder.getSize(),
|
2010-05-26 13:10:47 +08:00
|
|
|
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-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,
|
2011-01-12 17:06:06 +08:00
|
|
|
const ObjCImplementationDecl *Impl) const {
|
2010-05-26 12:56:53 +08:00
|
|
|
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 =
|
2011-02-28 10:01:38 +08:00
|
|
|
new (*this) ASTRecordLayout(*this, Builder.getSize(),
|
2011-02-16 10:05:21 +08:00
|
|
|
Builder.Alignment,
|
2011-02-28 10:01:38 +08:00
|
|
|
Builder.getDataSize(),
|
2010-05-26 13:04:25 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void PrintOffset(raw_ostream &OS,
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits Offset, unsigned IndentLevel) {
|
|
|
|
OS << llvm::format("%4d | ", Offset.getQuantity());
|
2010-04-08 10:59:49 +08:00
|
|
|
OS.indent(IndentLevel * 2);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DumpCXXRecordLayout(raw_ostream &OS,
|
2011-01-12 17:06:06 +08:00
|
|
|
const CXXRecordDecl *RD, const ASTContext &C,
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits Offset,
|
2010-04-08 10:59:49 +08:00
|
|
|
unsigned IndentLevel,
|
|
|
|
const char* Description,
|
|
|
|
bool IncludeVirtualBases) {
|
2010-11-01 07:45:59 +08:00
|
|
|
const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
|
|
|
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++;
|
|
|
|
|
2010-11-01 07:45:59 +08:00
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2010-04-08 10:59:49 +08:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
|
|
|
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;
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits FieldOffset = Offset +
|
2011-01-18 09:56:16 +08:00
|
|
|
C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
|
2010-04-08 10:59:49 +08:00
|
|
|
|
|
|
|
if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
|
|
|
|
if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
|
|
|
DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
|
2010-08-18 06:39:59 +08:00
|
|
|
Field->getName().data(),
|
2010-04-08 10:59:49 +08:00
|
|
|
/*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());
|
|
|
|
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
|
2010-04-08 10:59:49 +08:00
|
|
|
DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
|
|
|
|
VBase == PrimaryBase ?
|
|
|
|
"(primary virtual base)" : "(virtual base)",
|
|
|
|
/*IncludeVirtualBases=*/false);
|
|
|
|
}
|
|
|
|
|
2011-02-09 09:59:34 +08:00
|
|
|
OS << " sizeof=" << Layout.getSize().getQuantity();
|
2011-02-11 10:20:09 +08:00
|
|
|
OS << ", dsize=" << Layout.getDataSize().getQuantity();
|
2011-02-15 10:32:40 +08:00
|
|
|
OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
|
2011-02-01 09:52:10 +08:00
|
|
|
OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
|
2011-02-08 10:02:47 +08:00
|
|
|
OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n';
|
2010-04-08 10:59:49 +08:00
|
|
|
OS << '\n';
|
|
|
|
}
|
2010-04-20 04:44:53 +08:00
|
|
|
|
|
|
|
void ASTContext::DumpRecordLayout(const RecordDecl *RD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &OS) const {
|
2010-04-20 04:44:53 +08:00
|
|
|
const ASTRecordLayout &Info = getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
|
2010-11-01 07:45:59 +08:00
|
|
|
return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0,
|
2010-04-20 04:44:53 +08:00
|
|
|
/*IncludeVirtualBases=*/true);
|
|
|
|
|
|
|
|
OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
|
|
|
|
OS << "Record: ";
|
|
|
|
RD->dump();
|
|
|
|
OS << "\nLayout: ";
|
|
|
|
OS << "<ASTRecordLayout\n";
|
2011-02-11 09:54:29 +08:00
|
|
|
OS << " Size:" << toBits(Info.getSize()) << "\n";
|
2011-02-11 10:20:09 +08:00
|
|
|
OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
|
2011-02-15 10:32:40 +08:00
|
|
|
OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
|
2010-04-20 04:44:53 +08:00
|
|
|
OS << " FieldOffsets: [";
|
|
|
|
for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
|
|
|
|
if (i) OS << ", ";
|
|
|
|
OS << Info.getFieldOffset(i);
|
|
|
|
}
|
|
|
|
OS << "]>\n";
|
|
|
|
}
|