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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2012-07-05 02:45:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-19 04:20:21 +08:00
|
|
|
#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"
|
|
|
|
#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/ADT/SmallSet.h"
|
2011-03-19 09:00:36 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/MathExtras.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.
|
2014-05-04 02:44:26 +08:00
|
|
|
typedef llvm::TinyPtrVector<const CXXRecordDecl *> 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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : Class->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2010-05-26 23:54:25 +08:00
|
|
|
|
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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const FieldDecl *FD : Class->fields()) {
|
2010-05-27 10:25:46 +08:00
|
|
|
const RecordType *RT =
|
2014-07-16 14:04:00 +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;
|
2014-04-12 00:57:42 +08:00
|
|
|
const CXXRecordDecl *MemberDecl = RT->getAsCXXRecordDecl();
|
2010-05-26 23:54:25 +08:00
|
|
|
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;
|
2014-07-16 14:04:00 +08:00
|
|
|
|
|
|
|
const ClassVectorTy &Classes = I->second;
|
2010-05-28 02:20:57 +08:00
|
|
|
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;
|
|
|
|
|
2013-05-15 04:30:42 +08:00
|
|
|
// If we have empty structures inside a union, we can assign both
|
2010-12-30 07:02:58 +08:00
|
|
|
// the same offset. Just avoid pushing them twice in the list.
|
2014-07-16 14:04:00 +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);
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const BaseSubobjectInfo *Base : Info->Bases) {
|
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) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (I->isBitField())
|
2010-11-01 05:54:55 +08:00
|
|
|
continue;
|
2014-07-16 14:04:00 +08:00
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
2012-06-07 04:45:41 +08:00
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
|
2010-05-28 02:20:57 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-16 14:04:00 +08:00
|
|
|
|
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);
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const BaseSubobjectInfo *Base : Info->Bases) {
|
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) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (I->isBitField())
|
2010-11-01 05:54:55 +08:00
|
|
|
continue;
|
2010-05-30 05:10:24 +08:00
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
2012-06-07 04:45:41 +08:00
|
|
|
UpdateEmptyFieldSubobjects(*I, 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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
if (Base.isVirtual())
|
2010-05-28 02:20:57 +08:00
|
|
|
continue;
|
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2010-05-28 02:20:57 +08:00
|
|
|
|
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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2014-04-12 00:57:42 +08:00
|
|
|
|
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) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (I->isBitField())
|
2010-11-01 05:54:55 +08:00
|
|
|
continue;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
2012-06-07 04:45:41 +08:00
|
|
|
if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
|
2010-05-28 02:20:57 +08:00
|
|
|
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();
|
2014-04-12 00:57:42 +08:00
|
|
|
if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
|
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;
|
2014-04-12 00:57:42 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
|
2010-05-28 02:20:57 +08:00
|
|
|
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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
if (Base.isVirtual())
|
2010-05-28 02:20:57 +08:00
|
|
|
continue;
|
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2010-05-28 02:20:57 +08:00
|
|
|
|
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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *VBaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2014-04-12 00:57:42 +08:00
|
|
|
|
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) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (I->isBitField())
|
2010-11-01 23:14:51 +08:00
|
|
|
continue;
|
|
|
|
|
2010-11-01 06:13:23 +08:00
|
|
|
CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
|
2010-05-28 02:20:57 +08:00
|
|
|
|
2012-06-07 04:45:41 +08:00
|
|
|
UpdateEmptyFieldSubobjects(*I, 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();
|
2014-04-12 00:57:42 +08:00
|
|
|
if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
|
2010-05-28 02:20:57 +08:00
|
|
|
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;
|
2014-04-12 00:57:42 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
|
2010-05-28 02:20:57 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 16:55:32 +08:00
|
|
|
typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy;
|
|
|
|
|
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
|
|
|
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
/// \brief Whether the external AST source has provided a layout for this
|
|
|
|
/// record.
|
|
|
|
unsigned ExternalLayout : 1;
|
2012-01-28 08:53:29 +08:00
|
|
|
|
|
|
|
/// \brief Whether we need to infer alignment, even when we have an
|
|
|
|
/// externally-provided layout.
|
|
|
|
unsigned InferAlignment : 1;
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
|
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
|
|
|
|
2013-06-27 04:50:34 +08:00
|
|
|
/// UnfilledBitsInLastUnit - If the last field laid out was a bitfield,
|
|
|
|
/// this contains the number of bits in the last unit that can be used for
|
|
|
|
/// an adjacent bitfield if necessary. The unit in question is usually
|
|
|
|
/// a byte, but larger units are used if IsMsStruct.
|
|
|
|
unsigned char UnfilledBitsInLastUnit;
|
|
|
|
/// LastBitfieldTypeSize - If IsMsStruct, represents the size of the type
|
|
|
|
/// of the previous field if it was a bitfield.
|
|
|
|
unsigned char LastBitfieldTypeSize;
|
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
|
|
|
|
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;
|
|
|
|
|
2012-05-01 16:55:32 +08:00
|
|
|
/// HasOwnVFPtr - Whether the class provides its own vtable/vftbl
|
|
|
|
/// pointer, as opposed to inheriting one from a primary base class.
|
|
|
|
bool HasOwnVFPtr;
|
2011-10-22 06:49:56 +08:00
|
|
|
|
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.
|
2012-05-01 16:55:32 +08:00
|
|
|
ASTRecordLayout::VBaseOffsetsMapTy VBases;
|
2010-05-26 13:41:04 +08:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
/// \brief Externally-provided size.
|
|
|
|
uint64_t ExternalSize;
|
|
|
|
|
|
|
|
/// \brief Externally-provided alignment.
|
|
|
|
uint64_t ExternalAlign;
|
|
|
|
|
|
|
|
/// \brief Externally-provided field offsets.
|
|
|
|
llvm::DenseMap<const FieldDecl *, uint64_t> ExternalFieldOffsets;
|
|
|
|
|
|
|
|
/// \brief Externally-provided direct, non-virtual base offsets.
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalBaseOffsets;
|
|
|
|
|
|
|
|
/// \brief Externally-provided virtual base offsets.
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalVirtualBaseOffsets;
|
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
RecordLayoutBuilder(const ASTContext &Context,
|
|
|
|
EmptySubobjectMap *EmptySubobjects)
|
2011-02-16 10:05:21 +08:00
|
|
|
: Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
|
2011-11-08 12:01:03 +08:00
|
|
|
Alignment(CharUnits::One()), UnpackedAlignment(CharUnits::One()),
|
2012-01-28 08:53:29 +08:00
|
|
|
ExternalLayout(false), InferAlignment(false),
|
|
|
|
Packed(false), IsUnion(false), IsMac68kAlign(false), IsMsStruct(false),
|
2013-06-27 04:50:34 +08:00
|
|
|
UnfilledBitsInLastUnit(0), LastBitfieldTypeSize(0),
|
|
|
|
MaxFieldAlignment(CharUnits::Zero()),
|
2011-02-17 09:49:42 +08:00
|
|
|
DataSize(0), NonVirtualSize(CharUnits::Zero()),
|
2011-05-03 01:20:56 +08:00
|
|
|
NonVirtualAlignment(CharUnits::One()),
|
2014-05-12 13:36:57 +08:00
|
|
|
PrimaryBase(nullptr), PrimaryBaseIsVirtual(false),
|
2012-05-01 16:55:32 +08:00
|
|
|
HasOwnVFPtr(false),
|
2014-05-12 13:36:57 +08:00
|
|
|
FirstNearlyEmptyVBase(nullptr) {}
|
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);
|
2014-10-17 04:54:52 +08:00
|
|
|
void LayoutField(const FieldDecl *D, bool InsertExtraPadding);
|
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);
|
2011-11-08 12:01:03 +08:00
|
|
|
|
2013-01-26 06:30:49 +08:00
|
|
|
TargetCXXABI getCXXABI() const {
|
|
|
|
return Context.getTargetInfo().getCXXABI();
|
|
|
|
}
|
|
|
|
|
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-10-22 06:49:56 +08:00
|
|
|
void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign);
|
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.
|
2013-10-24 07:53:07 +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);
|
|
|
|
}
|
|
|
|
|
2012-01-28 08:53:29 +08:00
|
|
|
/// \brief Retrieve the externally-supplied field offset for the given
|
|
|
|
/// field.
|
|
|
|
///
|
|
|
|
/// \param Field The field whose offset is being queried.
|
|
|
|
/// \param ComputedOffset The offset that we've computed for this field.
|
|
|
|
uint64_t updateExternalFieldOffset(const FieldDecl *Field,
|
|
|
|
uint64_t ComputedOffset);
|
|
|
|
|
2010-09-22 22:32:24 +08:00
|
|
|
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; }
|
|
|
|
|
2011-09-28 03:12:27 +08:00
|
|
|
CharUnits getAligment() const { return Alignment; }
|
|
|
|
|
2011-02-24 09:13:28 +08:00
|
|
|
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; }
|
|
|
|
|
2012-09-16 04:20:27 +08:00
|
|
|
RecordLayoutBuilder(const RecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const RecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
|
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) {
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : RD->bases()) {
|
|
|
|
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
|
|
|
|
2014-04-12 00:57:42 +08:00
|
|
|
const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();
|
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.
|
2014-03-13 23:41:46 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : RD->bases()) {
|
2009-11-28 06:05:05 +08:00
|
|
|
// Ignore virtual bases.
|
2014-03-13 23:41:46 +08:00
|
|
|
if (I.isVirtual())
|
2009-11-28 06:05:05 +08:00
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2014-04-12 00:57:42 +08:00
|
|
|
const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();
|
2009-11-28 06:05:05 +08:00
|
|
|
|
2013-10-24 07:53:07 +08:00
|
|
|
if (Base->isDynamicClass()) {
|
2009-11-28 06:05:05 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 08:55:28 +08:00
|
|
|
// Under the Itanium ABI, if there is no non-virtual primary base class,
|
|
|
|
// try to compute the primary virtual base. The primary virtual base is
|
|
|
|
// the first nearly empty virtual base that is not an 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
|
|
|
|
2011-10-18 08:55:28 +08:00
|
|
|
// Otherwise, it is the first indirect primary base class, if one exists.
|
2010-03-11 08:15:35 +08:00
|
|
|
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-05-26 13:20:58 +08:00
|
|
|
assert(!PrimaryBase && "Should not get here with a primary base!");
|
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;
|
2014-05-12 13:36:57 +08:00
|
|
|
Info->Derived = nullptr;
|
|
|
|
Info->PrimaryVirtualBaseInfo = nullptr;
|
|
|
|
|
|
|
|
const CXXRecordDecl *PrimaryVirtualBase = nullptr;
|
|
|
|
BaseSubobjectInfo *PrimaryVirtualBaseInfo = nullptr;
|
2010-05-30 01:35:14 +08:00
|
|
|
|
|
|
|
// 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
|
2014-05-12 13:36:57 +08:00
|
|
|
// base.
|
|
|
|
PrimaryVirtualBase = nullptr;
|
2010-05-30 01:35:14 +08:00
|
|
|
} else {
|
|
|
|
// We can claim this base as our primary base.
|
|
|
|
Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
|
|
|
|
PrimaryVirtualBaseInfo->Derived = Info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through all direct bases.
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : RD->bases()) {
|
|
|
|
bool IsVirtual = I.isVirtual();
|
2014-04-12 00:57:42 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
|
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
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) {
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : RD->bases()) {
|
|
|
|
bool IsVirtual = I.isVirtual();
|
2010-05-30 01:35:14 +08:00
|
|
|
|
2014-04-12 00:57:42 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
|
|
|
|
|
2010-05-30 01:35:14 +08:00
|
|
|
// Compute the base subobject info for this base.
|
2014-05-12 13:36:57 +08:00
|
|
|
BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual,
|
|
|
|
nullptr);
|
2010-05-30 01:35:14 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 08:55:28 +08:00
|
|
|
void
|
2011-10-22 06:49:56 +08:00
|
|
|
RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) {
|
2011-10-18 08:55:28 +08:00
|
|
|
CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
|
|
|
|
|
|
|
|
// The maximum field alignment overrides base align.
|
|
|
|
if (!MaxFieldAlignment.isZero()) {
|
|
|
|
BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
|
|
|
|
UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round up the current record size to pointer alignment.
|
2011-10-22 06:49:56 +08:00
|
|
|
setSize(getSize().RoundUpToAlignment(BaseAlign));
|
|
|
|
setDataSize(getSize());
|
2011-10-18 08:55:28 +08:00
|
|
|
|
|
|
|
// Update the alignment.
|
|
|
|
UpdateAlignment(BaseAlign, UnpackedBaseAlign);
|
|
|
|
}
|
|
|
|
|
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);
|
2014-05-12 13:36:57 +08:00
|
|
|
PrimaryBaseInfo->Derived = nullptr;
|
|
|
|
|
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-04-08 10:59:49 +08:00
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
// If this class needs a vtable/vf-table and didn't get one from a
|
|
|
|
// primary base, add it in now.
|
2013-10-24 07:53:07 +08:00
|
|
|
} else if (RD->isDynamicClass()) {
|
2011-10-18 08:55:28 +08:00
|
|
|
assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
|
|
|
|
CharUnits PtrWidth =
|
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
2011-10-22 06:49:56 +08:00
|
|
|
CharUnits PtrAlign =
|
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
|
|
|
|
EnsureVTablePointerAlignment(PtrAlign);
|
2012-05-01 16:55:32 +08:00
|
|
|
HasOwnVFPtr = true;
|
2011-10-18 08:55:28 +08:00
|
|
|
setSize(getSize() + PtrWidth);
|
|
|
|
setDataSize(getSize());
|
|
|
|
}
|
|
|
|
|
2010-03-11 08:15:35 +08:00
|
|
|
// Now lay out the non-virtual bases.
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : RD->bases()) {
|
2010-03-11 08:15:35 +08:00
|
|
|
|
2013-10-25 15:40:50 +08:00
|
|
|
// Ignore virtual bases.
|
2014-03-13 23:41:46 +08:00
|
|
|
if (I.isVirtual())
|
2010-03-11 08:15:35 +08:00
|
|
|
continue;
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
|
2011-11-08 12:01:03 +08:00
|
|
|
|
|
|
|
// Skip the primary base, because we've already laid it out. The
|
|
|
|
// !PrimaryBaseIsVirtual check is required because we might have a
|
|
|
|
// non-virtual base of the same type as a primary virtual 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,
|
2012-05-01 16:55:32 +08:00
|
|
|
ASTRecordLayout::VBaseInfo(Offset, false)));
|
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);
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const BaseSubobjectInfo *Base : Info->Bases) {
|
2010-05-30 03:44:50 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
assert(!Base.getType()->isDependentType() &&
|
2009-10-26 01:03:50 +08:00
|
|
|
"Cannot layout class with dependent bases.");
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2010-03-11 12:24:02 +08:00
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
if (Base.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 07:53:07 +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!");
|
2012-05-01 16:55:32 +08:00
|
|
|
VBases.insert(std::make_pair(Base->Class,
|
2013-10-24 07:53:07 +08:00
|
|
|
ASTRecordLayout::VBaseInfo(Offset, false)));
|
2012-05-01 16:55:32 +08:00
|
|
|
|
2013-10-24 07:53:07 +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
|
|
|
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
|
|
|
|
CharUnits Offset;
|
|
|
|
|
|
|
|
// Query the external layout to see if it provides an offset.
|
|
|
|
bool HasExternalLayout = false;
|
|
|
|
if (ExternalLayout) {
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known;
|
|
|
|
if (Base->IsVirtual) {
|
|
|
|
Known = ExternalVirtualBaseOffsets.find(Base->Class);
|
|
|
|
if (Known != ExternalVirtualBaseOffsets.end()) {
|
|
|
|
Offset = Known->second;
|
|
|
|
HasExternalLayout = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Known = ExternalBaseOffsets.find(Base->Class);
|
|
|
|
if (Known != ExternalBaseOffsets.end()) {
|
|
|
|
Offset = Known->second;
|
|
|
|
HasExternalLayout = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 08:30:56 +08:00
|
|
|
CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlignment();
|
2013-07-16 08:21:28 +08:00
|
|
|
CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
|
|
|
|
|
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() &&
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
(!HasExternalLayout || Offset == CharUnits::Zero()) &&
|
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()));
|
2013-07-16 08:21:28 +08:00
|
|
|
UpdateAlignment(BaseAlign, UnpackedBaseAlign);
|
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
|
|
|
|
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
|
|
|
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
if (!HasExternalLayout) {
|
|
|
|
// Round up the current record size to the base's alignment boundary.
|
|
|
|
Offset = getDataSize().RoundUpToAlignment(BaseAlign);
|
2010-03-11 06:21:28 +08:00
|
|
|
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
// Try to place the base.
|
|
|
|
while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
|
|
|
|
Offset += BaseAlign;
|
|
|
|
} else {
|
|
|
|
bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset);
|
|
|
|
(void)Allowed;
|
|
|
|
assert(Allowed && "Base subobject externally placed at overlapping offset");
|
2012-10-27 06:31:14 +08:00
|
|
|
|
|
|
|
if (InferAlignment && Offset < getDataSize().RoundUpToAlignment(BaseAlign)){
|
|
|
|
// The externally-supplied base offset is before the base offset we
|
|
|
|
// computed. Assume that the structure is packed.
|
|
|
|
Alignment = CharUnits::One();
|
|
|
|
InferAlignment = false;
|
|
|
|
}
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +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) {
|
2012-10-13 07:29:20 +08:00
|
|
|
if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
|
2010-05-27 13:45:51 +08:00
|
|
|
IsUnion = RD->isUnion();
|
2012-10-13 07:29:20 +08:00
|
|
|
IsMsStruct = RD->isMsStruct(Context);
|
|
|
|
}
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2012-10-13 07:29:20 +08:00
|
|
|
Packed = D->hasAttr<PackedAttr>();
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2011-10-06 05:04:55 +08:00
|
|
|
// Honor the default struct packing maximum alignment flag.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) {
|
2011-10-06 05:04:55 +08:00
|
|
|
MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
|
|
|
|
// If there is an external AST source, ask it for the various offsets.
|
|
|
|
if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
|
|
|
|
if (ExternalASTSource *External = Context.getExternalSource()) {
|
|
|
|
ExternalLayout = External->layoutRecordType(RD,
|
|
|
|
ExternalSize,
|
|
|
|
ExternalAlign,
|
|
|
|
ExternalFieldOffsets,
|
|
|
|
ExternalBaseOffsets,
|
|
|
|
ExternalVirtualBaseOffsets);
|
|
|
|
|
|
|
|
// Update based on external alignment.
|
|
|
|
if (ExternalLayout) {
|
2012-01-28 08:53:29 +08:00
|
|
|
if (ExternalAlign > 0) {
|
|
|
|
Alignment = Context.toCharUnitsFromBits(ExternalAlign);
|
|
|
|
} else {
|
|
|
|
// The external source didn't have alignment information; infer it.
|
|
|
|
InferAlignment = true;
|
|
|
|
}
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +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
|
|
|
|
2013-10-24 07:53:07 +08:00
|
|
|
// Lay out the virtual bases and add the primary virtual base offsets.
|
|
|
|
LayoutVirtualBases(RD, RD);
|
2011-11-08 12:01:03 +08:00
|
|
|
|
|
|
|
// Finally, round the size of the total struct up to the alignment
|
2011-12-01 08:37:01 +08:00
|
|
|
// of the struct itself.
|
|
|
|
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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
if (Base.isVirtual())
|
2010-05-26 23:10:00 +08:00
|
|
|
continue;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2010-05-26 23:10:00 +08:00
|
|
|
|
|
|
|
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.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
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())
|
2014-10-17 04:54:52 +08:00
|
|
|
LayoutField(IVD, false);
|
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.
|
2014-10-17 04:54:52 +08:00
|
|
|
bool InsertExtraPadding = D->mayInsertExtraPadding(/*EmitRemark=*/true);
|
2014-10-28 03:34:10 +08:00
|
|
|
bool HasFlexibleArrayMember = D->hasFlexibleArrayMember();
|
|
|
|
for (auto I = D->field_begin(), End = D->field_end(); I != End; ++I) {
|
|
|
|
auto Next(I);
|
|
|
|
++Next;
|
|
|
|
LayoutField(*I,
|
|
|
|
InsertExtraPadding && (Next != End || !HasFlexibleArrayMember));
|
|
|
|
}
|
2009-07-19 05:48:39 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 18:22:03 +08:00
|
|
|
// Rounds the specified size to have it a multiple of the char size.
|
|
|
|
static uint64_t
|
|
|
|
roundUpSizeToCharAlignment(uint64_t Size,
|
|
|
|
const ASTContext &Context) {
|
|
|
|
uint64_t CharAlignment = Context.getTargetInfo().getCharAlign();
|
|
|
|
return llvm::RoundUpToAlignment(Size, CharAlignment);
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-03-11 15:00:24 +08:00
|
|
|
assert(Context.getLangOpts().CPlusPlus &&
|
2010-04-16 23:57:11 +08:00
|
|
|
"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;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const QualType &QT : IntegralPODTypes) {
|
|
|
|
uint64_t Size = Context.getTypeSize(QT);
|
2010-04-16 23:57:11 +08:00
|
|
|
|
|
|
|
if (Size > FieldSize)
|
|
|
|
break;
|
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
Type = QT;
|
2010-04-16 23:57:11 +08:00
|
|
|
}
|
|
|
|
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.
|
2013-06-27 04:50:34 +08:00
|
|
|
UnfilledBitsInLastUnit = 0;
|
|
|
|
LastBitfieldTypeSize = 0;
|
2010-04-16 23:57:11 +08:00
|
|
|
|
2010-04-18 04:21:41 +08:00
|
|
|
uint64_t FieldOffset;
|
2013-06-27 04:50:34 +08:00
|
|
|
uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit;
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2010-04-16 23:57:11 +08:00
|
|
|
if (IsUnion) {
|
2014-10-17 18:22:03 +08:00
|
|
|
uint64_t RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize,
|
|
|
|
Context);
|
|
|
|
setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize));
|
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()));
|
2013-06-27 04:50:34 +08:00
|
|
|
UnfilledBitsInLastUnit = 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-10-11 02:28:20 +08:00
|
|
|
uint64_t FieldSize = D->getBitWidthValue(Context);
|
2014-07-30 09:30:47 +08:00
|
|
|
TypeInfo FieldInfo = Context.getTypeInfo(D->getType());
|
|
|
|
uint64_t TypeSize = FieldInfo.Width;
|
|
|
|
unsigned FieldAlign = FieldInfo.Align;
|
2013-06-27 04:50:34 +08:00
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// UnfilledBitsInLastUnit is the difference between the end of the
|
|
|
|
// last allocated bitfield (i.e. the first bit offset available for
|
|
|
|
// bitfields) and the end of the current data size in bits (i.e. the
|
|
|
|
// first bit offset available for non-bitfields). The current data
|
|
|
|
// size in bits is always a multiple of the char size; additionally,
|
|
|
|
// for ms_struct records it's also a multiple of the
|
|
|
|
// LastBitfieldTypeSize (if set).
|
|
|
|
|
2014-02-13 08:50:08 +08:00
|
|
|
// The struct-layout algorithm is dictated by the platform ABI,
|
|
|
|
// which in principle could use almost any rules it likes. In
|
|
|
|
// practice, UNIXy targets tend to inherit the algorithm described
|
|
|
|
// in the System V generic ABI. The basic bitfield layout rule in
|
|
|
|
// System V is to place bitfields at the next available bit offset
|
|
|
|
// where the entire bitfield would fit in an aligned storage unit of
|
|
|
|
// the declared type; it's okay if an earlier or later non-bitfield
|
|
|
|
// is allocated in the same storage unit. However, some targets
|
|
|
|
// (those that !useBitFieldTypeAlignment(), e.g. ARM APCS) don't
|
|
|
|
// require this storage unit to be aligned, and therefore always put
|
|
|
|
// the bitfield at the next available bit offset.
|
|
|
|
|
|
|
|
// ms_struct basically requests a complete replacement of the
|
|
|
|
// platform ABI's struct-layout algorithm, with the high-level goal
|
|
|
|
// of duplicating MSVC's layout. For non-bitfields, this follows
|
|
|
|
// the the standard algorithm. The basic bitfield layout rule is to
|
|
|
|
// allocate an entire unit of the bitfield's declared type
|
|
|
|
// (e.g. 'unsigned long'), then parcel it up among successive
|
|
|
|
// bitfields whose declared types have the same size, making a new
|
|
|
|
// unit as soon as the last can no longer store the whole value.
|
|
|
|
// Since it completely replaces the platform ABI's algorithm,
|
|
|
|
// settings like !useBitFieldTypeAlignment() do not apply.
|
|
|
|
|
|
|
|
// A zero-width bitfield forces the use of a new storage unit for
|
|
|
|
// later bitfields. In general, this occurs by rounding up the
|
|
|
|
// current size of the struct as if the algorithm were about to
|
|
|
|
// place a non-bitfield of the field's formal type. Usually this
|
|
|
|
// does not change the alignment of the struct itself, but it does
|
|
|
|
// on some targets (those that useZeroLengthBitfieldAlignment(),
|
|
|
|
// e.g. ARM). In ms_struct layout, zero-width bitfields are
|
|
|
|
// ignored unless they follow a non-zero-width bitfield.
|
|
|
|
|
|
|
|
// A field alignment restriction (e.g. from #pragma pack) or
|
|
|
|
// specification (e.g. from __attribute__((aligned))) changes the
|
|
|
|
// formal alignment of the field. For System V, this alters the
|
|
|
|
// required alignment of the notional storage unit that must contain
|
|
|
|
// the bitfield. For ms_struct, this only affects the placement of
|
|
|
|
// new storage units. In both cases, the effect of #pragma pack is
|
|
|
|
// ignored on zero-width bitfields.
|
|
|
|
|
|
|
|
// On System V, a packed field (e.g. from #pragma pack or
|
|
|
|
// __attribute__((packed))) always uses the next available bit
|
|
|
|
// offset.
|
|
|
|
|
2014-02-28 04:30:49 +08:00
|
|
|
// In an ms_struct struct, the alignment of a fundamental type is
|
|
|
|
// always equal to its size. This is necessary in order to mimic
|
|
|
|
// the i386 alignment rules on targets which might not fully align
|
|
|
|
// all types (e.g. Darwin PPC32, where alignof(long long) == 4).
|
2014-01-29 15:53:44 +08:00
|
|
|
|
|
|
|
// First, some simple bookkeeping to perform for ms_struct structs.
|
2013-06-27 04:50:34 +08:00
|
|
|
if (IsMsStruct) {
|
2014-01-29 15:53:44 +08:00
|
|
|
// The field alignment for integer types is always the size.
|
2011-05-10 06:03:17 +08:00
|
|
|
FieldAlign = TypeSize;
|
2014-01-29 15:53:44 +08:00
|
|
|
|
|
|
|
// If the previous field was not a bitfield, or was a bitfield
|
|
|
|
// with a different storage unit size, we're done with that
|
|
|
|
// storage unit.
|
2013-06-27 04:50:34 +08:00
|
|
|
if (LastBitfieldTypeSize != TypeSize) {
|
2014-01-29 15:53:44 +08:00
|
|
|
// Also, ignore zero-length bitfields after non-bitfields.
|
|
|
|
if (!LastBitfieldTypeSize && !FieldSize)
|
|
|
|
FieldAlign = 1;
|
|
|
|
|
2013-06-27 04:50:34 +08:00
|
|
|
UnfilledBitsInLastUnit = 0;
|
|
|
|
LastBitfieldTypeSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// If the field is wider than its declared type, it follows
|
|
|
|
// different rules in all cases.
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// Compute the next available bit offset.
|
|
|
|
uint64_t FieldOffset =
|
|
|
|
IsUnion ? 0 : (getDataSizeInBits() - UnfilledBitsInLastUnit);
|
|
|
|
|
|
|
|
// Handle targets that don't honor bitfield type alignment.
|
2014-02-13 08:50:08 +08:00
|
|
|
if (!IsMsStruct && !Context.getTargetInfo().useBitFieldTypeAlignment()) {
|
2014-01-29 15:53:44 +08:00
|
|
|
// Some such targets do honor it on zero-width bitfields.
|
|
|
|
if (FieldSize == 0 &&
|
|
|
|
Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
|
|
|
|
// The alignment to round up to is the max of the field's natural
|
|
|
|
// alignment and a target-specific fixed value (sometimes zero).
|
|
|
|
unsigned ZeroLengthBitfieldBoundary =
|
|
|
|
Context.getTargetInfo().getZeroLengthBitfieldBoundary();
|
|
|
|
FieldAlign = std::max(FieldAlign, ZeroLengthBitfieldBoundary);
|
|
|
|
|
|
|
|
// If that doesn't apply, just ignore the field alignment.
|
|
|
|
} else {
|
|
|
|
FieldAlign = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember the alignment we would have used if the field were not packed.
|
2010-09-22 22:32:24 +08:00
|
|
|
unsigned UnpackedFieldAlign = FieldAlign;
|
|
|
|
|
2014-02-13 10:45:10 +08:00
|
|
|
// Ignore the field alignment if the field is packed unless it has zero-size.
|
|
|
|
if (!IsMsStruct && FieldPacked && FieldSize != 0)
|
2009-11-23 01:37:31 +08:00
|
|
|
FieldAlign = 1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// But, if there's an 'aligned' attribute on the field, honor that.
|
|
|
|
if (unsigned ExplicitFieldAlign = D->getMaxAlignment()) {
|
|
|
|
FieldAlign = std::max(FieldAlign, ExplicitFieldAlign);
|
|
|
|
UnpackedFieldAlign = std::max(UnpackedFieldAlign, ExplicitFieldAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
// But, if there's a #pragma pack in play, that takes precedent over
|
|
|
|
// even the 'aligned' attribute, for non-zero-width bitfields.
|
|
|
|
if (!MaxFieldAlignment.isZero() && FieldSize) {
|
2011-02-17 09:49:42 +08:00
|
|
|
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
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// For purposes of diagnostics, we're going to simultaneously
|
|
|
|
// compute the field offsets that we would have used if we weren't
|
|
|
|
// adding any alignment padding or if the field weren't packed.
|
|
|
|
uint64_t UnpaddedFieldOffset = FieldOffset;
|
|
|
|
uint64_t UnpackedFieldOffset = FieldOffset;
|
2013-06-27 04:50:34 +08:00
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// Check if we need to add padding to fit the bitfield within an
|
|
|
|
// allocation unit with the right size and alignment. The rules are
|
|
|
|
// somewhat different here for ms_struct structs.
|
|
|
|
if (IsMsStruct) {
|
|
|
|
// If it's not a zero-width bitfield, and we can fit the bitfield
|
|
|
|
// into the active storage unit (and we haven't already decided to
|
|
|
|
// start a new storage unit), just do so, regardless of any other
|
|
|
|
// other consideration. Otherwise, round up to the right alignment.
|
|
|
|
if (FieldSize == 0 || FieldSize > UnfilledBitsInLastUnit) {
|
|
|
|
FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
|
|
|
|
UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
|
|
|
|
UnpackedFieldAlign);
|
|
|
|
UnfilledBitsInLastUnit = 0;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
} else {
|
|
|
|
// #pragma pack, with any value, suppresses the insertion of padding.
|
|
|
|
bool AllowPadding = MaxFieldAlignment.isZero();
|
|
|
|
|
|
|
|
// Compute the real offset.
|
|
|
|
if (FieldSize == 0 ||
|
|
|
|
(AllowPadding &&
|
|
|
|
(FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)) {
|
|
|
|
FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat the computation for diagnostic purposes.
|
|
|
|
if (FieldSize == 0 ||
|
|
|
|
(AllowPadding &&
|
|
|
|
(UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize))
|
|
|
|
UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
|
|
|
|
UnpackedFieldAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're using external layout, give the external layout a chance
|
|
|
|
// to override this information.
|
2012-01-28 08:53:29 +08:00
|
|
|
if (ExternalLayout)
|
|
|
|
FieldOffset = updateExternalFieldOffset(D, FieldOffset);
|
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// Okay, place the bitfield at the calculated offset.
|
2009-11-23 01:37:31 +08:00
|
|
|
FieldOffsets.push_back(FieldOffset);
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2014-01-29 15:53:44 +08:00
|
|
|
// Bookkeeping:
|
|
|
|
|
|
|
|
// Anonymous members don't affect the overall record alignment,
|
|
|
|
// except on targets where they do.
|
|
|
|
if (!IsMsStruct &&
|
|
|
|
!Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
|
|
|
|
!D->getIdentifier())
|
|
|
|
FieldAlign = UnpackedFieldAlign = 1;
|
|
|
|
|
|
|
|
// Diagnose differences in layout due to padding or packing.
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
if (!ExternalLayout)
|
|
|
|
CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
|
|
|
|
UnpackedFieldAlign, FieldPacked, D);
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2009-11-23 03:13:51 +08:00
|
|
|
// Update DataSize to include the last byte containing (part of) the bitfield.
|
2014-01-29 15:53:44 +08:00
|
|
|
|
|
|
|
// For unions, this is just a max operation, as usual.
|
2009-11-23 03:13:51 +08:00
|
|
|
if (IsUnion) {
|
2014-10-17 18:22:03 +08:00
|
|
|
uint64_t RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize,
|
|
|
|
Context);
|
|
|
|
setDataSize(std::max(getDataSizeInBits(), RoundedFieldSize));
|
2014-01-29 15:53:44 +08:00
|
|
|
// For non-zero-width bitfields in ms_struct structs, allocate a new
|
|
|
|
// storage unit if necessary.
|
|
|
|
} else if (IsMsStruct && FieldSize) {
|
|
|
|
// We should have cleared UnfilledBitsInLastUnit in every case
|
|
|
|
// where we changed storage units.
|
|
|
|
if (!UnfilledBitsInLastUnit) {
|
|
|
|
setDataSize(FieldOffset + TypeSize);
|
|
|
|
UnfilledBitsInLastUnit = TypeSize;
|
2013-06-27 04:50:34 +08:00
|
|
|
}
|
2014-01-29 15:53:44 +08:00
|
|
|
UnfilledBitsInLastUnit -= FieldSize;
|
|
|
|
LastBitfieldTypeSize = TypeSize;
|
|
|
|
|
|
|
|
// Otherwise, bump the data size up to include the bitfield,
|
|
|
|
// including padding up to char alignment, and then remember how
|
|
|
|
// bits we didn't use.
|
|
|
|
} else {
|
|
|
|
uint64_t NewSizeInBits = FieldOffset + FieldSize;
|
|
|
|
uint64_t CharAlignment = Context.getTargetInfo().getCharAlign();
|
|
|
|
setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, CharAlignment));
|
|
|
|
UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits;
|
|
|
|
|
|
|
|
// The only time we can get here for an ms_struct is if this is a
|
|
|
|
// zero-width bitfield, which doesn't count as anything for the
|
|
|
|
// purposes of unfilled bits.
|
|
|
|
LastBitfieldTypeSize = 0;
|
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
|
|
|
|
2014-10-17 04:54:52 +08:00
|
|
|
void RecordLayoutBuilder::LayoutField(const FieldDecl *D,
|
|
|
|
bool InsertExtraPadding) {
|
2009-11-23 01:37:31 +08:00
|
|
|
if (D->isBitField()) {
|
|
|
|
LayoutBitField(D);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-27 04:50:34 +08:00
|
|
|
uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit;
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2009-11-23 03:13:51 +08:00
|
|
|
// Reset the unfilled bits.
|
2013-06-27 04:50:34 +08:00
|
|
|
UnfilledBitsInLastUnit = 0;
|
|
|
|
LastBitfieldTypeSize = 0;
|
2009-11-23 03:13:51 +08:00
|
|
|
|
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
|
|
|
|
2012-10-13 07:29:20 +08:00
|
|
|
if (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
|
|
|
|
2012-01-28 08:53:29 +08:00
|
|
|
// Round up the current record size to the field's alignment boundary.
|
|
|
|
FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
|
|
|
|
UnpackedFieldOffset =
|
|
|
|
UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
|
|
|
|
|
|
|
|
if (ExternalLayout) {
|
|
|
|
FieldOffset = Context.toCharUnitsFromBits(
|
|
|
|
updateExternalFieldOffset(D, Context.toBits(FieldOffset)));
|
|
|
|
|
|
|
|
if (!IsUnion && EmptySubobjects) {
|
|
|
|
// Record the fact that we're placing a field at this offset.
|
|
|
|
bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset);
|
|
|
|
(void)Allowed;
|
|
|
|
assert(Allowed && "Externally-placed field cannot be placed here");
|
|
|
|
}
|
|
|
|
} else {
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
if (!IsUnion && EmptySubobjects) {
|
|
|
|
// Check if we can place the field at this offset.
|
|
|
|
while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
|
|
|
|
// 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
|
|
|
}
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +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
|
|
|
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
if (!ExternalLayout)
|
|
|
|
CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
|
|
|
|
Context.toBits(UnpackedFieldOffset),
|
|
|
|
Context.toBits(UnpackedFieldAlign), FieldPacked, D);
|
2010-09-22 22:32:24 +08:00
|
|
|
|
2014-10-28 03:34:10 +08:00
|
|
|
if (InsertExtraPadding) {
|
2014-10-17 04:54:52 +08:00
|
|
|
CharUnits ASanAlignment = CharUnits::fromQuantity(8);
|
|
|
|
CharUnits ExtraSizeForAsan = ASanAlignment;
|
|
|
|
if (FieldSize % ASanAlignment)
|
|
|
|
ExtraSizeForAsan +=
|
|
|
|
ASanAlignment - CharUnits::fromQuantity(FieldSize % ASanAlignment);
|
|
|
|
FieldSize += ExtraSizeForAsan;
|
|
|
|
}
|
|
|
|
|
2009-07-19 04:20:21 +08:00
|
|
|
// Reserve space for this field.
|
2012-01-13 07:27:03 +08:00
|
|
|
uint64_t FieldSizeInBits = Context.toBits(FieldSize);
|
2009-07-19 04:20:21 +08:00
|
|
|
if (IsUnion)
|
2012-01-13 07:48:56 +08:00
|
|
|
setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits));
|
2009-07-19 04:20:21 +08:00
|
|
|
else
|
2012-01-13 07:48:56 +08:00
|
|
|
setDataSize(FieldOffset + FieldSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-13 07:48:56 +08:00
|
|
|
// Update the size.
|
|
|
|
setSize(std::max(getSizeInBits(), getDataSizeInBits()));
|
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.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Context.getLangOpts().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
|
|
|
}
|
2011-12-01 08:37:01 +08:00
|
|
|
|
2012-10-27 06:31:14 +08:00
|
|
|
// Finally, round the size of the record up to the alignment of the
|
|
|
|
// record itself.
|
2013-06-27 04:50:34 +08:00
|
|
|
uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastUnit;
|
2012-10-27 06:31:14 +08:00
|
|
|
uint64_t UnpackedSizeInBits =
|
|
|
|
llvm::RoundUpToAlignment(getSizeInBits(),
|
|
|
|
Context.toBits(UnpackedAlignment));
|
|
|
|
CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
|
|
|
|
uint64_t RoundedSize
|
|
|
|
= llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment));
|
|
|
|
|
|
|
|
if (ExternalLayout) {
|
|
|
|
// If we're inferring alignment, and the external size is smaller than
|
|
|
|
// our size after we've rounded up to alignment, conservatively set the
|
|
|
|
// alignment to 1.
|
|
|
|
if (InferAlignment && ExternalSize < RoundedSize) {
|
|
|
|
Alignment = CharUnits::One();
|
|
|
|
InferAlignment = false;
|
|
|
|
}
|
|
|
|
setSize(ExternalSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the size to the final size.
|
|
|
|
setSize(RoundedSize);
|
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) {
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
// The alignment is not modified when using 'mac68k' alignment or when
|
2012-01-28 08:53:29 +08:00
|
|
|
// we have an externally-supplied layout that also provides overall alignment.
|
|
|
|
if (IsMac68kAlign || (ExternalLayout && !InferAlignment))
|
2010-05-27 13:45:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-28 08:53:29 +08:00
|
|
|
uint64_t
|
|
|
|
RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field,
|
|
|
|
uint64_t ComputedOffset) {
|
|
|
|
assert(ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() &&
|
|
|
|
"Field does not have an external offset");
|
|
|
|
|
|
|
|
uint64_t ExternalFieldOffset = ExternalFieldOffsets[Field];
|
|
|
|
|
|
|
|
if (InferAlignment && ExternalFieldOffset < ComputedOffset) {
|
|
|
|
// The externally-supplied field offset is before the field offset we
|
|
|
|
// computed. Assume that the structure is packed.
|
2012-10-27 06:31:14 +08:00
|
|
|
Alignment = CharUnits::One();
|
2012-01-28 08:53:29 +08:00
|
|
|
InferAlignment = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the externally-supplied field offset.
|
2012-09-01 06:14:25 +08:00
|
|
|
return ExternalFieldOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get diagnostic %select index for tag kind for
|
|
|
|
/// field padding diagnostic message.
|
|
|
|
/// WARNING: Indexes apply to particular diagnostics only!
|
|
|
|
///
|
|
|
|
/// \returns diagnostic %select index.
|
|
|
|
static unsigned getPaddingDiagFromTagKind(TagTypeKind Tag) {
|
|
|
|
switch (Tag) {
|
|
|
|
case TTK_Struct: return 0;
|
|
|
|
case TTK_Interface: return 1;
|
|
|
|
case TTK_Class: return 2;
|
|
|
|
default: llvm_unreachable("Invalid tag kind for field padding diagnostic!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
|
|
|
|
uint64_t UnpaddedOffset,
|
|
|
|
uint64_t UnpackedOffset,
|
2010-09-22 22:32:24 +08:00
|
|
|
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;
|
2012-09-01 06:14:25 +08:00
|
|
|
}
|
|
|
|
if (D->getIdentifier())
|
|
|
|
Diag(D->getLocation(), diag::warn_padded_struct_field)
|
|
|
|
<< getPaddingDiagFromTagKind(D->getParent()->getTagKind())
|
|
|
|
<< 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)
|
|
|
|
<< getPaddingDiagFromTagKind(D->getParent()->getTagKind())
|
|
|
|
<< Context.getTypeDeclType(D->getParent())
|
|
|
|
<< PadSize
|
|
|
|
<< (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
|
2010-09-22 22:32:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
static const CXXMethodDecl *computeKeyFunction(ASTContext &Context,
|
|
|
|
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())
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
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.)
|
2013-05-13 08:12:11 +08:00
|
|
|
if (!RD->isExternallyVisible())
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2009-12-08 11:56:49 +08:00
|
|
|
|
2014-03-25 07:54:09 +08:00
|
|
|
// Template instantiations don't have key functions per Itanium C++ ABI 5.2.6.
|
2010-10-13 10:39:41 +08:00
|
|
|
// Same behavior as GCC.
|
|
|
|
TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
|
|
|
|
if (TSK == TSK_ImplicitInstantiation ||
|
2014-03-25 07:54:09 +08:00
|
|
|
TSK == TSK_ExplicitInstantiationDeclaration ||
|
2010-10-13 10:39:41 +08:00
|
|
|
TSK == TSK_ExplicitInstantiationDefinition)
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2010-10-13 10:39:41 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
bool allowInlineFunctions =
|
|
|
|
Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline();
|
|
|
|
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXMethodDecl *MD : RD->methods()) {
|
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
|
|
|
|
2012-08-03 23:43:22 +08:00
|
|
|
// Ignore inline deleted or defaulted functions.
|
2012-08-03 16:39:58 +08:00
|
|
|
if (!MD->isUserProvided())
|
|
|
|
continue;
|
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
// In certain ABIs, ignore functions with out-of-line inline definitions.
|
|
|
|
if (!allowInlineFunctions) {
|
|
|
|
const FunctionDecl *Def;
|
|
|
|
if (MD->hasBody(Def) && Def->isInlineSpecified())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-07 12:35:11 +08:00
|
|
|
// We found it.
|
|
|
|
return MD;
|
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2009-12-07 12:35:11 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-29 09:14:22 +08:00
|
|
|
/// Does the target C++ ABI require us to skip over the tail-padding
|
|
|
|
/// of the given class (considering it as a base class) when allocating
|
|
|
|
/// objects?
|
|
|
|
static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) {
|
|
|
|
switch (ABI.getTailPaddingUseRules()) {
|
|
|
|
case TargetCXXABI::AlwaysUseTailPadding:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TargetCXXABI::UseTailPaddingUnlessPOD03:
|
|
|
|
// FIXME: To the extent that this is meant to cover the Itanium ABI
|
|
|
|
// rules, we should implement the restrictions about over-sized
|
|
|
|
// bitfields:
|
|
|
|
//
|
|
|
|
// http://mentorembedded.github.com/cxx-abi/abi.html#POD :
|
|
|
|
// In general, a type is considered a POD for the purposes of
|
|
|
|
// layout if it is a POD type (in the sense of ISO C++
|
|
|
|
// [basic.types]). However, a POD-struct or POD-union (in the
|
|
|
|
// sense of ISO C++ [class]) with a bitfield member whose
|
|
|
|
// declared width is wider than the declared type of the
|
|
|
|
// bitfield is not a POD for the purpose of layout. Similarly,
|
|
|
|
// an array type is not a POD for the purpose of layout if the
|
|
|
|
// element type of the array is not a POD for the purpose of
|
|
|
|
// layout.
|
|
|
|
//
|
|
|
|
// Where references to the ISO C++ are made in this paragraph,
|
|
|
|
// the Technical Corrigendum 1 version of the standard is
|
|
|
|
// intended.
|
|
|
|
return RD->isPOD();
|
|
|
|
|
|
|
|
case TargetCXXABI::UseTailPaddingUnlessPOD11:
|
|
|
|
// This is equivalent to RD->getTypeForDecl().isCXX11PODType(),
|
|
|
|
// but with a lot of abstraction penalty stripped off. This does
|
|
|
|
// assume that these properties are set correctly even in C++98
|
|
|
|
// mode; fortunately, that is true because we want to assign
|
|
|
|
// consistently semantics to the type-traits intrinsics (or at
|
|
|
|
// least as many of them as possible).
|
|
|
|
return RD->isTrivial() && RD->isStandardLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("bad tail-padding use kind");
|
|
|
|
}
|
|
|
|
|
2013-10-12 04:19:00 +08:00
|
|
|
static bool isMsLayout(const RecordDecl* D) {
|
2013-10-24 07:53:07 +08:00
|
|
|
return D->getASTContext().getTargetInfo().getCXXABI().isMicrosoft();
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This section contains an implementation of struct layout that is, up to the
|
2014-04-11 08:54:15 +08:00
|
|
|
// included tests, compatible with cl.exe (2013). The layout produced is
|
2013-10-12 04:19:00 +08:00
|
|
|
// significantly different than those produced by the Itanium ABI. Here we note
|
|
|
|
// the most important differences.
|
|
|
|
//
|
|
|
|
// * The alignment of bitfields in unions is ignored when computing the
|
|
|
|
// alignment of the union.
|
2013-12-06 00:25:25 +08:00
|
|
|
// * The existence of zero-width bitfield that occurs after anything other than
|
2013-10-12 04:19:00 +08:00
|
|
|
// a non-zero length bitfield is ignored.
|
2014-04-11 08:54:15 +08:00
|
|
|
// * There is no explicit primary base for the purposes of layout. All bases
|
|
|
|
// with vfptrs are laid out first, followed by all bases without vfptrs.
|
2013-10-12 04:19:00 +08:00
|
|
|
// * The Itanium equivalent vtable pointers are split into a vfptr (virtual
|
|
|
|
// function pointer) and a vbptr (virtual base pointer). They can each be
|
2013-10-24 07:53:07 +08:00
|
|
|
// shared with a, non-virtual bases. These bases need not be the same. vfptrs
|
2014-04-11 08:54:15 +08:00
|
|
|
// always occur at offset 0. vbptrs can occur at an arbitrary offset and are
|
|
|
|
// placed after the lexiographically last non-virtual base. This placement
|
|
|
|
// is always before fields but can be in the middle of the non-virtual bases
|
|
|
|
// due to the two-pass layout scheme for non-virtual-bases.
|
2013-10-12 04:19:00 +08:00
|
|
|
// * Virtual bases sometimes require a 'vtordisp' field that is laid out before
|
|
|
|
// the virtual base and is used in conjunction with virtual overrides during
|
2014-04-11 08:54:15 +08:00
|
|
|
// construction and destruction. This is always a 4 byte value and is used as
|
|
|
|
// an alternative to constructor vtables.
|
2013-10-12 04:19:00 +08:00
|
|
|
// * vtordisps are allocated in a block of memory with size and alignment equal
|
|
|
|
// to the alignment of the completed structure (before applying __declspec(
|
2013-10-24 07:53:07 +08:00
|
|
|
// align())). The vtordisp always occur at the end of the allocation block,
|
|
|
|
// immediately prior to the virtual base.
|
2014-04-11 08:54:15 +08:00
|
|
|
// * vfptrs are injected after all bases and fields have been laid out. In
|
|
|
|
// order to guarantee proper alignment of all fields, the vfptr injection
|
|
|
|
// pushes all bases and fields back by the alignment imposed by those bases
|
|
|
|
// and fields. This can potentially add a significant amount of padding.
|
|
|
|
// vfptrs are always injected at offset 0.
|
|
|
|
// * vbptrs are injected after all bases and fields have been laid out. In
|
|
|
|
// order to guarantee proper alignment of all fields, the vfptr injection
|
|
|
|
// pushes all bases and fields back by the alignment imposed by those bases
|
|
|
|
// and fields. This can potentially add a significant amount of padding.
|
|
|
|
// vbptrs are injected immediately after the last non-virtual base as
|
|
|
|
// lexiographically ordered in the code. If this site isn't pointer aligned
|
|
|
|
// the vbptr is placed at the next properly aligned location. Enough padding
|
|
|
|
// is added to guarantee a fit.
|
|
|
|
// * The last zero sized non-virtual base can be placed at the end of the
|
|
|
|
// struct (potentially aliasing another object), or may alias with the first
|
|
|
|
// field, even if they are of the same type.
|
|
|
|
// * The last zero size virtual base may be placed at the end of the struct
|
|
|
|
// potentially aliasing another object.
|
2013-12-07 03:54:25 +08:00
|
|
|
// * The ABI attempts to avoid aliasing of zero sized bases by adding padding
|
|
|
|
// between bases or vbases with specific properties. The criteria for
|
|
|
|
// additional padding between two bases is that the first base is zero sized
|
2014-04-10 05:57:24 +08:00
|
|
|
// or ends with a zero sized subobject and the second base is zero sized or
|
2014-04-11 08:54:15 +08:00
|
|
|
// trails with a zero sized base or field (sharing of vfptrs can reorder the
|
|
|
|
// layout of the so the leading base is not always the first one declared).
|
|
|
|
// This rule does take into account fields that are not records, so padding
|
|
|
|
// will occur even if the last field is, e.g. an int. The padding added for
|
|
|
|
// bases is 1 byte. The padding added between vbases depends on the alignment
|
|
|
|
// of the object but is at least 4 bytes (in both 32 and 64 bit modes).
|
|
|
|
// * There is no concept of non-virtual alignment, non-virtual alignment and
|
|
|
|
// alignment are always identical.
|
|
|
|
// * There is a distinction between alignment and required alignment.
|
|
|
|
// __declspec(align) changes the required alignment of a struct. This
|
|
|
|
// alignment is _always_ obeyed, even in the presence of #pragma pack. A
|
2014-10-08 13:45:39 +08:00
|
|
|
// record inherits required alignment from all of its fields and bases.
|
2014-01-10 09:28:05 +08:00
|
|
|
// * __declspec(align) on bitfields has the effect of changing the bitfield's
|
2014-04-11 08:54:15 +08:00
|
|
|
// alignment instead of its required alignment. This is the only known way
|
|
|
|
// to make the alignment of a struct bigger than 8. Interestingly enough
|
|
|
|
// this alignment is also immune to the effects of #pragma pack and can be
|
|
|
|
// used to create structures with large alignment under #pragma pack.
|
|
|
|
// However, because it does not impact required alignment, such a structure,
|
|
|
|
// when used as a field or base, will not be aligned if #pragma pack is
|
|
|
|
// still active at the time of use.
|
|
|
|
//
|
2014-05-06 03:53:42 +08:00
|
|
|
// Known incompatibilities:
|
2014-04-11 08:54:15 +08:00
|
|
|
// * all: #pragma pack between fields in a record
|
|
|
|
// * 2010 and back: If the last field in a record is a bitfield, every object
|
|
|
|
// laid out after the record will have extra padding inserted before it. The
|
|
|
|
// extra padding will have size equal to the size of the storage class of the
|
|
|
|
// bitfield. 0 sized bitfields don't exhibit this behavior and the extra
|
|
|
|
// padding can be avoided by adding a 0 sized bitfield after the non-zero-
|
|
|
|
// sized bitfield.
|
|
|
|
// * 2012 and back: In 64-bit mode, if the alignment of a record is 16 or
|
|
|
|
// greater due to __declspec(align()) then a second layout phase occurs after
|
|
|
|
// The locations of the vf and vb pointers are known. This layout phase
|
|
|
|
// suffers from the "last field is a bitfield" bug in 2010 and results in
|
|
|
|
// _every_ field getting padding put in front of it, potentially including the
|
|
|
|
// vfptr, leaving the vfprt at a non-zero location which results in a fault if
|
|
|
|
// anything tries to read the vftbl. The second layout phase also treats
|
2014-05-06 03:53:42 +08:00
|
|
|
// bitfields as separate entities and gives them each storage rather than
|
2014-04-11 08:54:15 +08:00
|
|
|
// packing them. Additionally, because this phase appears to perform a
|
|
|
|
// (an unstable) sort on the members before laying them out and because merged
|
|
|
|
// bitfields have the same address, the bitfields end up in whatever order
|
|
|
|
// the sort left them in, a behavior we could never hope to replicate.
|
2013-10-12 04:19:00 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct MicrosoftRecordLayoutBuilder {
|
2014-01-09 08:30:56 +08:00
|
|
|
struct ElementInfo {
|
|
|
|
CharUnits Size;
|
|
|
|
CharUnits Alignment;
|
|
|
|
};
|
2013-10-12 04:19:00 +08:00
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
|
|
|
|
MicrosoftRecordLayoutBuilder(const ASTContext &Context) : Context(Context) {}
|
|
|
|
private:
|
|
|
|
MicrosoftRecordLayoutBuilder(const MicrosoftRecordLayoutBuilder &)
|
|
|
|
LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const MicrosoftRecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
|
|
|
|
public:
|
|
|
|
void layout(const RecordDecl *RD);
|
|
|
|
void cxxLayout(const CXXRecordDecl *RD);
|
|
|
|
/// \brief Initializes size and alignment and honors some flags.
|
|
|
|
void initializeLayout(const RecordDecl *RD);
|
|
|
|
/// \brief Initialized C++ layout, compute alignment and virtual alignment and
|
2013-12-06 00:25:25 +08:00
|
|
|
/// existence of vfptrs and vbptrs. Alignment is needed before the vfptr is
|
2013-10-12 04:19:00 +08:00
|
|
|
/// laid out.
|
|
|
|
void initializeCXXLayout(const CXXRecordDecl *RD);
|
|
|
|
void layoutNonVirtualBases(const CXXRecordDecl *RD);
|
2014-01-09 08:30:56 +08:00
|
|
|
void layoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
|
|
|
|
const ASTRecordLayout &BaseLayout,
|
|
|
|
const ASTRecordLayout *&PreviousBaseLayout);
|
|
|
|
void injectVFPtr(const CXXRecordDecl *RD);
|
|
|
|
void injectVBPtr(const CXXRecordDecl *RD);
|
2013-10-12 04:19:00 +08:00
|
|
|
/// \brief Lays out the fields of the record. Also rounds size up to
|
|
|
|
/// alignment.
|
|
|
|
void layoutFields(const RecordDecl *RD);
|
|
|
|
void layoutField(const FieldDecl *FD);
|
|
|
|
void layoutBitField(const FieldDecl *FD);
|
|
|
|
/// \brief Lays out a single zero-width bit-field in the record and handles
|
|
|
|
/// special cases associated with zero-width bit-fields.
|
|
|
|
void layoutZeroWidthBitField(const FieldDecl *FD);
|
|
|
|
void layoutVirtualBases(const CXXRecordDecl *RD);
|
2013-12-10 09:44:39 +08:00
|
|
|
void finalizeLayout(const RecordDecl *RD);
|
2014-01-09 08:30:56 +08:00
|
|
|
/// \brief Gets the size and alignment of a base taking pragma pack and
|
|
|
|
/// __declspec(align) into account.
|
2014-04-11 06:15:18 +08:00
|
|
|
ElementInfo getAdjustedElementInfo(const ASTRecordLayout &Layout);
|
2014-01-09 08:30:56 +08:00
|
|
|
/// \brief Gets the size and alignment of a field taking pragma pack and
|
|
|
|
/// __declspec(align) into account. It also updates RequiredAlignment as a
|
|
|
|
/// side effect because it is most convenient to do so here.
|
|
|
|
ElementInfo getAdjustedElementInfo(const FieldDecl *FD);
|
2013-10-12 04:19:00 +08:00
|
|
|
/// \brief Places a field at an offset in CharUnits.
|
|
|
|
void placeFieldAtOffset(CharUnits FieldOffset) {
|
|
|
|
FieldOffsets.push_back(Context.toBits(FieldOffset));
|
|
|
|
}
|
|
|
|
/// \brief Places a bitfield at a bit offset.
|
|
|
|
void placeFieldAtBitOffset(uint64_t FieldOffset) {
|
|
|
|
FieldOffsets.push_back(FieldOffset);
|
|
|
|
}
|
|
|
|
/// \brief Compute the set of virtual bases for which vtordisps are required.
|
2014-09-24 06:58:15 +08:00
|
|
|
void computeVtorDispSet(
|
|
|
|
llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtorDispSet,
|
|
|
|
const CXXRecordDecl *RD) const;
|
2013-10-12 04:19:00 +08:00
|
|
|
const ASTContext &Context;
|
|
|
|
/// \brief The size of the record being laid out.
|
|
|
|
CharUnits Size;
|
2014-02-21 09:40:35 +08:00
|
|
|
/// \brief The non-virtual size of the record layout.
|
|
|
|
CharUnits NonVirtualSize;
|
|
|
|
/// \brief The data size of the record layout.
|
2014-01-09 08:30:56 +08:00
|
|
|
CharUnits DataSize;
|
2013-10-12 04:19:00 +08:00
|
|
|
/// \brief The current alignment of the record layout.
|
|
|
|
CharUnits Alignment;
|
|
|
|
/// \brief The maximum allowed field alignment. This is set by #pragma pack.
|
|
|
|
CharUnits MaxFieldAlignment;
|
2013-12-06 08:01:17 +08:00
|
|
|
/// \brief The alignment that this record must obey. This is imposed by
|
|
|
|
/// __declspec(align()) on the record itself or one of its fields or bases.
|
|
|
|
CharUnits RequiredAlignment;
|
2013-10-12 04:19:00 +08:00
|
|
|
/// \brief The size of the allocation of the currently active bitfield.
|
|
|
|
/// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield
|
|
|
|
/// is true.
|
|
|
|
CharUnits CurrentBitfieldSize;
|
2014-01-09 08:30:56 +08:00
|
|
|
/// \brief Offset to the virtual base table pointer (if one exists).
|
|
|
|
CharUnits VBPtrOffset;
|
2014-09-30 14:45:43 +08:00
|
|
|
/// \brief Minimum record size possible.
|
|
|
|
CharUnits MinEmptyStructSize;
|
2014-01-09 08:30:56 +08:00
|
|
|
/// \brief The size and alignment info of a pointer.
|
|
|
|
ElementInfo PointerInfo;
|
2013-10-12 04:19:00 +08:00
|
|
|
/// \brief The primary base class (if one exists).
|
|
|
|
const CXXRecordDecl *PrimaryBase;
|
|
|
|
/// \brief The class we share our vb-pointer with.
|
|
|
|
const CXXRecordDecl *SharedVBPtrBase;
|
2014-01-09 08:30:56 +08:00
|
|
|
/// \brief The collection of field offsets.
|
|
|
|
SmallVector<uint64_t, 16> FieldOffsets;
|
2013-10-12 04:19:00 +08:00
|
|
|
/// \brief Base classes and their offsets in the record.
|
|
|
|
BaseOffsetsMapTy Bases;
|
|
|
|
/// \brief virtual base classes and their offsets in the record.
|
|
|
|
ASTRecordLayout::VBaseOffsetsMapTy VBases;
|
2014-01-09 08:30:56 +08:00
|
|
|
/// \brief The number of remaining bits in our last bitfield allocation.
|
|
|
|
/// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield is
|
|
|
|
/// true.
|
|
|
|
unsigned RemainingBitsInField;
|
|
|
|
bool IsUnion : 1;
|
|
|
|
/// \brief True if the last field laid out was a bitfield and was not 0
|
|
|
|
/// width.
|
|
|
|
bool LastFieldIsNonZeroWidthBitfield : 1;
|
|
|
|
/// \brief True if the class has its own vftable pointer.
|
|
|
|
bool HasOwnVFPtr : 1;
|
|
|
|
/// \brief True if the class has a vbtable pointer.
|
|
|
|
bool HasVBPtr : 1;
|
2014-04-10 05:57:24 +08:00
|
|
|
/// \brief True if the last sub-object within the type is zero sized or the
|
|
|
|
/// object itself is zero sized. This *does not* count members that are not
|
|
|
|
/// records. Only used for MS-ABI.
|
|
|
|
bool EndsWithZeroSizedObject : 1;
|
2013-12-07 03:54:25 +08:00
|
|
|
/// \brief True if this class is zero sized or first base is zero sized or
|
|
|
|
/// has this property. Only used for MS-ABI.
|
|
|
|
bool LeadsWithZeroSizedBase : 1;
|
2013-10-12 04:19:00 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2014-01-09 08:30:56 +08:00
|
|
|
MicrosoftRecordLayoutBuilder::ElementInfo
|
|
|
|
MicrosoftRecordLayoutBuilder::getAdjustedElementInfo(
|
2014-04-11 06:15:18 +08:00
|
|
|
const ASTRecordLayout &Layout) {
|
2014-01-09 08:30:56 +08:00
|
|
|
ElementInfo Info;
|
|
|
|
Info.Alignment = Layout.getAlignment();
|
|
|
|
// Respect pragma pack.
|
2013-12-06 08:01:17 +08:00
|
|
|
if (!MaxFieldAlignment.isZero())
|
2014-01-09 08:30:56 +08:00
|
|
|
Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment);
|
|
|
|
// Track zero-sized subobjects here where it's already available.
|
2014-04-10 05:57:24 +08:00
|
|
|
EndsWithZeroSizedObject = Layout.hasZeroSizedSubObject();
|
2014-01-09 08:30:56 +08:00
|
|
|
// Respect required alignment, this is necessary because we may have adjusted
|
2014-01-11 09:16:40 +08:00
|
|
|
// the alignment in the case of pragam pack. Note that the required alignment
|
|
|
|
// doesn't actually apply to the struct alignment at this point.
|
|
|
|
Alignment = std::max(Alignment, Info.Alignment);
|
2014-04-11 06:15:18 +08:00
|
|
|
RequiredAlignment = std::max(RequiredAlignment, Layout.getRequiredAlignment());
|
2014-01-09 08:30:56 +08:00
|
|
|
Info.Alignment = std::max(Info.Alignment, Layout.getRequiredAlignment());
|
2014-04-11 06:15:18 +08:00
|
|
|
Info.Size = Layout.getNonVirtualSize();
|
2014-01-09 08:30:56 +08:00
|
|
|
return Info;
|
2013-12-06 08:01:17 +08:00
|
|
|
}
|
|
|
|
|
2014-01-09 08:30:56 +08:00
|
|
|
MicrosoftRecordLayoutBuilder::ElementInfo
|
|
|
|
MicrosoftRecordLayoutBuilder::getAdjustedElementInfo(
|
|
|
|
const FieldDecl *FD) {
|
2014-07-30 09:30:47 +08:00
|
|
|
// Get the alignment of the field type's natural alignment, ignore any
|
|
|
|
// alignment attributes.
|
2014-01-09 08:30:56 +08:00
|
|
|
ElementInfo Info;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(Info.Size, Info.Alignment) =
|
2014-07-30 09:30:47 +08:00
|
|
|
Context.getTypeInfoInChars(FD->getType()->getUnqualifiedDesugaredType());
|
|
|
|
// Respect align attributes on the field.
|
|
|
|
CharUnits FieldRequiredAlignment =
|
2014-01-10 09:28:05 +08:00
|
|
|
Context.toCharUnitsFromBits(FD->getMaxAlignment());
|
2014-07-30 09:30:47 +08:00
|
|
|
// Respect align attributes on the type.
|
|
|
|
if (Context.isAlignmentRequired(FD->getType()))
|
|
|
|
FieldRequiredAlignment = std::max(
|
|
|
|
Context.getTypeAlignInChars(FD->getType()), FieldRequiredAlignment);
|
2013-12-07 03:54:25 +08:00
|
|
|
// Respect attributes applied to subobjects of the field.
|
2014-04-11 06:15:18 +08:00
|
|
|
if (FD->isBitField())
|
|
|
|
// For some reason __declspec align impacts alignment rather than required
|
|
|
|
// alignment when it is applied to bitfields.
|
2014-01-10 09:28:05 +08:00
|
|
|
Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment);
|
2014-04-11 06:15:18 +08:00
|
|
|
else {
|
|
|
|
if (auto RT =
|
|
|
|
FD->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
|
|
|
|
auto const &Layout = Context.getASTRecordLayout(RT->getDecl());
|
|
|
|
EndsWithZeroSizedObject = Layout.hasZeroSizedSubObject();
|
|
|
|
FieldRequiredAlignment = std::max(FieldRequiredAlignment,
|
|
|
|
Layout.getRequiredAlignment());
|
|
|
|
}
|
2014-01-10 09:28:05 +08:00
|
|
|
// Capture required alignment as a side-effect.
|
|
|
|
RequiredAlignment = std::max(RequiredAlignment, FieldRequiredAlignment);
|
|
|
|
}
|
2014-04-11 06:15:18 +08:00
|
|
|
// Respect pragma pack, attribute pack and declspec align
|
|
|
|
if (!MaxFieldAlignment.isZero())
|
|
|
|
Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment);
|
|
|
|
if (FD->hasAttr<PackedAttr>())
|
|
|
|
Info.Alignment = CharUnits::One();
|
|
|
|
Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment);
|
2014-01-09 08:30:56 +08:00
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftRecordLayoutBuilder::layout(const RecordDecl *RD) {
|
2014-09-30 14:45:43 +08:00
|
|
|
// For C record layout, zero-sized records always have size 4.
|
|
|
|
MinEmptyStructSize = CharUnits::fromQuantity(4);
|
2014-01-09 08:30:56 +08:00
|
|
|
initializeLayout(RD);
|
|
|
|
layoutFields(RD);
|
|
|
|
DataSize = Size = Size.RoundUpToAlignment(Alignment);
|
2014-02-12 08:43:02 +08:00
|
|
|
RequiredAlignment = std::max(
|
|
|
|
RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment()));
|
2014-01-09 08:30:56 +08:00
|
|
|
finalizeLayout(RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftRecordLayoutBuilder::cxxLayout(const CXXRecordDecl *RD) {
|
2014-09-30 14:45:43 +08:00
|
|
|
// The C++ standard says that empty structs have size 1.
|
|
|
|
MinEmptyStructSize = CharUnits::One();
|
2014-01-09 08:30:56 +08:00
|
|
|
initializeLayout(RD);
|
|
|
|
initializeCXXLayout(RD);
|
|
|
|
layoutNonVirtualBases(RD);
|
|
|
|
layoutFields(RD);
|
2014-03-25 05:37:27 +08:00
|
|
|
injectVBPtr(RD);
|
|
|
|
injectVFPtr(RD);
|
|
|
|
if (HasOwnVFPtr || (HasVBPtr && !SharedVBPtrBase))
|
|
|
|
Alignment = std::max(Alignment, PointerInfo.Alignment);
|
2014-04-11 06:15:18 +08:00
|
|
|
auto RoundingAlignment = Alignment;
|
|
|
|
if (!MaxFieldAlignment.isZero())
|
|
|
|
RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment);
|
|
|
|
NonVirtualSize = Size = Size.RoundUpToAlignment(RoundingAlignment);
|
2014-02-12 08:43:02 +08:00
|
|
|
RequiredAlignment = std::max(
|
|
|
|
RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment()));
|
2014-01-09 08:30:56 +08:00
|
|
|
layoutVirtualBases(RD);
|
|
|
|
finalizeLayout(RD);
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftRecordLayoutBuilder::initializeLayout(const RecordDecl *RD) {
|
|
|
|
IsUnion = RD->isUnion();
|
|
|
|
Size = CharUnits::Zero();
|
|
|
|
Alignment = CharUnits::One();
|
2013-12-06 08:01:17 +08:00
|
|
|
// In 64-bit mode we always perform an alignment step after laying out vbases.
|
|
|
|
// In 32-bit mode we do not. The check to see if we need to perform alignment
|
|
|
|
// checks the RequiredAlignment field and performs alignment if it isn't 0.
|
2014-04-11 07:23:34 +08:00
|
|
|
RequiredAlignment = Context.getTargetInfo().getPointerWidth(0) == 64 ?
|
|
|
|
CharUnits::One() : CharUnits::Zero();
|
2013-10-12 04:19:00 +08:00
|
|
|
// Compute the maximum field alignment.
|
|
|
|
MaxFieldAlignment = CharUnits::Zero();
|
|
|
|
// Honor the default struct packing maximum alignment flag.
|
|
|
|
if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct)
|
2014-01-10 09:28:05 +08:00
|
|
|
MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
|
|
|
|
// Honor the packing attribute. The MS-ABI ignores pragma pack if its larger
|
|
|
|
// than the pointer size.
|
|
|
|
if (const MaxFieldAlignmentAttr *MFAA = RD->getAttr<MaxFieldAlignmentAttr>()){
|
|
|
|
unsigned PackedAlignment = MFAA->getAlignment();
|
|
|
|
if (PackedAlignment <= Context.getTargetInfo().getPointerWidth(0))
|
|
|
|
MaxFieldAlignment = Context.toCharUnitsFromBits(PackedAlignment);
|
|
|
|
}
|
2013-10-12 04:19:00 +08:00
|
|
|
// Packed attribute forces max field alignment to be 1.
|
|
|
|
if (RD->hasAttr<PackedAttr>())
|
|
|
|
MaxFieldAlignment = CharUnits::One();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MicrosoftRecordLayoutBuilder::initializeCXXLayout(const CXXRecordDecl *RD) {
|
2014-04-10 05:57:24 +08:00
|
|
|
EndsWithZeroSizedObject = false;
|
2014-01-09 08:30:56 +08:00
|
|
|
LeadsWithZeroSizedBase = false;
|
|
|
|
HasOwnVFPtr = false;
|
2013-10-12 04:19:00 +08:00
|
|
|
HasVBPtr = false;
|
2014-05-12 13:36:57 +08:00
|
|
|
PrimaryBase = nullptr;
|
|
|
|
SharedVBPtrBase = nullptr;
|
2014-01-09 08:30:56 +08:00
|
|
|
// Calculate pointer size and alignment. These are used for vfptr and vbprt
|
|
|
|
// injection.
|
|
|
|
PointerInfo.Size =
|
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
|
|
|
PointerInfo.Alignment = PointerInfo.Size;
|
|
|
|
// Respect pragma pack.
|
|
|
|
if (!MaxFieldAlignment.isZero())
|
|
|
|
PointerInfo.Alignment = std::min(PointerInfo.Alignment, MaxFieldAlignment);
|
|
|
|
}
|
2013-10-12 04:19:00 +08:00
|
|
|
|
2014-01-09 08:30:56 +08:00
|
|
|
void
|
|
|
|
MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) {
|
|
|
|
// The MS-ABI lays out all bases that contain leading vfptrs before it lays
|
|
|
|
// out any bases that do not contain vfptrs. We implement this as two passes
|
|
|
|
// over the bases. This approach guarantees that the primary base is laid out
|
|
|
|
// first. We use these passes to calculate some additional aggregated
|
|
|
|
// information about the bases, such as reqruied alignment and the presence of
|
|
|
|
// zero sized members.
|
2014-05-12 13:36:57 +08:00
|
|
|
const ASTRecordLayout *PreviousBaseLayout = nullptr;
|
2014-01-09 08:30:56 +08:00
|
|
|
// Iterate through the bases and lay out the non-virtual ones.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2014-01-09 08:30:56 +08:00
|
|
|
const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
|
|
|
|
// Mark and skip virtual bases.
|
2014-07-16 14:04:00 +08:00
|
|
|
if (Base.isVirtual()) {
|
2013-10-12 04:19:00 +08:00
|
|
|
HasVBPtr = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// Check fo a base to share a VBPtr with.
|
|
|
|
if (!SharedVBPtrBase && BaseLayout.hasVBPtr()) {
|
2013-10-12 04:19:00 +08:00
|
|
|
SharedVBPtrBase = BaseDecl;
|
|
|
|
HasVBPtr = true;
|
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// Only lay out bases with extendable VFPtrs on the first pass.
|
|
|
|
if (!BaseLayout.hasExtendableVFPtr())
|
|
|
|
continue;
|
|
|
|
// If we don't have a primary base, this one qualifies.
|
2014-01-14 03:55:52 +08:00
|
|
|
if (!PrimaryBase) {
|
2014-01-09 08:30:56 +08:00
|
|
|
PrimaryBase = BaseDecl;
|
2014-01-14 03:55:52 +08:00
|
|
|
LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase();
|
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// Lay out the base.
|
|
|
|
layoutNonVirtualBase(BaseDecl, BaseLayout, PreviousBaseLayout);
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// Figure out if we need a fresh VFPtr for this class.
|
|
|
|
if (!PrimaryBase && RD->isDynamicClass())
|
2013-10-12 04:19:00 +08:00
|
|
|
for (CXXRecordDecl::method_iterator i = RD->method_begin(),
|
|
|
|
e = RD->method_end();
|
2014-01-09 08:30:56 +08:00
|
|
|
!HasOwnVFPtr && i != e; ++i)
|
|
|
|
HasOwnVFPtr = i->isVirtual() && i->size_overridden_methods() == 0;
|
|
|
|
// If we don't have a primary base then we have a leading object that could
|
|
|
|
// itself lead with a zero-sized object, something we track.
|
|
|
|
bool CheckLeadingLayout = !PrimaryBase;
|
2013-10-12 04:19:00 +08:00
|
|
|
// Iterate through the bases and lay out the non-virtual ones.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
if (Base.isVirtual())
|
2013-10-12 04:19:00 +08:00
|
|
|
continue;
|
2014-07-16 14:04:00 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2014-01-09 08:30:56 +08:00
|
|
|
const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
|
|
|
|
// Only lay out bases without extendable VFPtrs on the second pass.
|
2014-04-11 07:23:34 +08:00
|
|
|
if (BaseLayout.hasExtendableVFPtr()) {
|
|
|
|
VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize();
|
2013-12-13 06:33:37 +08:00
|
|
|
continue;
|
2014-04-11 07:23:34 +08:00
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// If this is the first layout, check to see if it leads with a zero sized
|
|
|
|
// object. If it does, so do we.
|
|
|
|
if (CheckLeadingLayout) {
|
|
|
|
CheckLeadingLayout = false;
|
|
|
|
LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase();
|
2013-12-13 06:33:37 +08:00
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// Lay out the base.
|
|
|
|
layoutNonVirtualBase(BaseDecl, BaseLayout, PreviousBaseLayout);
|
2014-04-11 07:23:34 +08:00
|
|
|
VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize();
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// Set our VBPtroffset if we know it at this point.
|
2013-10-12 04:19:00 +08:00
|
|
|
if (!HasVBPtr)
|
|
|
|
VBPtrOffset = CharUnits::fromQuantity(-1);
|
2014-01-14 08:31:30 +08:00
|
|
|
else if (SharedVBPtrBase) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(SharedVBPtrBase);
|
|
|
|
VBPtrOffset = Bases[SharedVBPtrBase] + Layout.getVBPtrOffset();
|
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftRecordLayoutBuilder::layoutNonVirtualBase(
|
|
|
|
const CXXRecordDecl *BaseDecl,
|
|
|
|
const ASTRecordLayout &BaseLayout,
|
|
|
|
const ASTRecordLayout *&PreviousBaseLayout) {
|
2014-01-10 09:28:05 +08:00
|
|
|
// Insert padding between two bases if the left first one is zero sized or
|
|
|
|
// contains a zero sized subobject and the right is zero sized or one leads
|
|
|
|
// with a zero sized base.
|
|
|
|
if (PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() &&
|
|
|
|
BaseLayout.leadsWithZeroSizedBase())
|
|
|
|
Size++;
|
|
|
|
ElementInfo Info = getAdjustedElementInfo(BaseLayout);
|
|
|
|
CharUnits BaseOffset = Size.RoundUpToAlignment(Info.Alignment);
|
|
|
|
Bases.insert(std::make_pair(BaseDecl, BaseOffset));
|
2014-02-21 09:40:35 +08:00
|
|
|
Size = BaseOffset + BaseLayout.getNonVirtualSize();
|
2014-01-10 09:28:05 +08:00
|
|
|
PreviousBaseLayout = &BaseLayout;
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftRecordLayoutBuilder::layoutFields(const RecordDecl *RD) {
|
|
|
|
LastFieldIsNonZeroWidthBitfield = false;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const FieldDecl *Field : RD->fields())
|
2014-03-09 04:12:42 +08:00
|
|
|
layoutField(Field);
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) {
|
|
|
|
if (FD->isBitField()) {
|
|
|
|
layoutBitField(FD);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LastFieldIsNonZeroWidthBitfield = false;
|
2014-01-09 08:30:56 +08:00
|
|
|
ElementInfo Info = getAdjustedElementInfo(FD);
|
2014-04-13 16:15:50 +08:00
|
|
|
Alignment = std::max(Alignment, Info.Alignment);
|
2013-10-12 04:19:00 +08:00
|
|
|
if (IsUnion) {
|
2014-01-09 08:30:56 +08:00
|
|
|
placeFieldAtOffset(CharUnits::Zero());
|
|
|
|
Size = std::max(Size, Info.Size);
|
2013-10-12 04:19:00 +08:00
|
|
|
} else {
|
2014-01-09 08:30:56 +08:00
|
|
|
CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment);
|
2013-10-12 04:19:00 +08:00
|
|
|
placeFieldAtOffset(FieldOffset);
|
2014-01-09 08:30:56 +08:00
|
|
|
Size = FieldOffset + Info.Size;
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) {
|
|
|
|
unsigned Width = FD->getBitWidthValue(Context);
|
|
|
|
if (Width == 0) {
|
|
|
|
layoutZeroWidthBitField(FD);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
ElementInfo Info = getAdjustedElementInfo(FD);
|
2013-10-12 04:19:00 +08:00
|
|
|
// Clamp the bitfield to a containable size for the sake of being able
|
|
|
|
// to lay them out. Sema will throw an error.
|
2014-01-09 08:30:56 +08:00
|
|
|
if (Width > Context.toBits(Info.Size))
|
|
|
|
Width = Context.toBits(Info.Size);
|
2013-10-12 04:19:00 +08:00
|
|
|
// Check to see if this bitfield fits into an existing allocation. Note:
|
|
|
|
// MSVC refuses to pack bitfields of formal types with different sizes
|
|
|
|
// into the same allocation.
|
|
|
|
if (!IsUnion && LastFieldIsNonZeroWidthBitfield &&
|
2014-01-09 08:30:56 +08:00
|
|
|
CurrentBitfieldSize == Info.Size && Width <= RemainingBitsInField) {
|
2013-10-12 04:19:00 +08:00
|
|
|
placeFieldAtBitOffset(Context.toBits(Size) - RemainingBitsInField);
|
|
|
|
RemainingBitsInField -= Width;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LastFieldIsNonZeroWidthBitfield = true;
|
2014-01-09 08:30:56 +08:00
|
|
|
CurrentBitfieldSize = Info.Size;
|
2013-10-12 04:19:00 +08:00
|
|
|
if (IsUnion) {
|
2014-01-09 08:30:56 +08:00
|
|
|
placeFieldAtOffset(CharUnits::Zero());
|
|
|
|
Size = std::max(Size, Info.Size);
|
2014-04-13 16:15:50 +08:00
|
|
|
// TODO: Add a Sema warning that MS ignores bitfield alignment in unions.
|
2013-10-12 04:19:00 +08:00
|
|
|
} else {
|
|
|
|
// Allocate a new block of memory and place the bitfield in it.
|
2014-01-09 08:30:56 +08:00
|
|
|
CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment);
|
2013-10-12 04:19:00 +08:00
|
|
|
placeFieldAtOffset(FieldOffset);
|
2014-01-09 08:30:56 +08:00
|
|
|
Size = FieldOffset + Info.Size;
|
2014-04-13 16:15:50 +08:00
|
|
|
Alignment = std::max(Alignment, Info.Alignment);
|
2014-01-09 08:30:56 +08:00
|
|
|
RemainingBitsInField = Context.toBits(Info.Size) - Width;
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MicrosoftRecordLayoutBuilder::layoutZeroWidthBitField(const FieldDecl *FD) {
|
|
|
|
// Zero-width bitfields are ignored unless they follow a non-zero-width
|
|
|
|
// bitfield.
|
|
|
|
if (!LastFieldIsNonZeroWidthBitfield) {
|
|
|
|
placeFieldAtOffset(IsUnion ? CharUnits::Zero() : Size);
|
|
|
|
// TODO: Add a Sema warning that MS ignores alignment for zero
|
2013-12-05 12:47:09 +08:00
|
|
|
// sized bitfields that occur after zero-size bitfields or non-bitfields.
|
2013-10-12 04:19:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LastFieldIsNonZeroWidthBitfield = false;
|
2014-01-09 08:30:56 +08:00
|
|
|
ElementInfo Info = getAdjustedElementInfo(FD);
|
2013-10-12 04:19:00 +08:00
|
|
|
if (IsUnion) {
|
2014-01-09 08:30:56 +08:00
|
|
|
placeFieldAtOffset(CharUnits::Zero());
|
|
|
|
Size = std::max(Size, Info.Size);
|
2014-04-13 16:15:50 +08:00
|
|
|
// TODO: Add a Sema warning that MS ignores bitfield alignment in unions.
|
2013-10-12 04:19:00 +08:00
|
|
|
} else {
|
|
|
|
// Round up the current record size to the field's alignment boundary.
|
2014-01-09 08:30:56 +08:00
|
|
|
CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment);
|
2013-10-12 04:19:00 +08:00
|
|
|
placeFieldAtOffset(FieldOffset);
|
|
|
|
Size = FieldOffset;
|
2014-04-13 16:15:50 +08:00
|
|
|
Alignment = std::max(Alignment, Info.Alignment);
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 08:30:56 +08:00
|
|
|
void MicrosoftRecordLayoutBuilder::injectVBPtr(const CXXRecordDecl *RD) {
|
2014-01-14 08:31:30 +08:00
|
|
|
if (!HasVBPtr || SharedVBPtrBase)
|
2014-01-09 08:30:56 +08:00
|
|
|
return;
|
|
|
|
// Inject the VBPointer at the injection site.
|
|
|
|
CharUnits InjectionSite = VBPtrOffset;
|
|
|
|
// But before we do, make sure it's properly aligned.
|
|
|
|
VBPtrOffset = VBPtrOffset.RoundUpToAlignment(PointerInfo.Alignment);
|
|
|
|
// Determine where the first field should be laid out after the vbptr.
|
|
|
|
CharUnits FieldStart = VBPtrOffset + PointerInfo.Size;
|
|
|
|
// Make sure that the amount we push the fields back by is a multiple of the
|
|
|
|
// alignment.
|
2014-02-12 08:43:02 +08:00
|
|
|
CharUnits Offset = (FieldStart - InjectionSite).RoundUpToAlignment(
|
|
|
|
std::max(RequiredAlignment, Alignment));
|
2014-01-09 08:30:56 +08:00
|
|
|
// Increase the size of the object and push back all fields by the offset
|
|
|
|
// amount.
|
|
|
|
Size += Offset;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (uint64_t &FieldOffset : FieldOffsets)
|
|
|
|
FieldOffset += Context.toBits(Offset);
|
|
|
|
for (BaseOffsetsMapTy::value_type &Base : Bases)
|
|
|
|
if (Base.second >= InjectionSite)
|
|
|
|
Base.second += Offset;
|
2013-12-10 09:44:39 +08:00
|
|
|
}
|
|
|
|
|
2014-01-09 08:30:56 +08:00
|
|
|
void MicrosoftRecordLayoutBuilder::injectVFPtr(const CXXRecordDecl *RD) {
|
|
|
|
if (!HasOwnVFPtr)
|
2013-10-12 04:19:00 +08:00
|
|
|
return;
|
2014-01-09 08:30:56 +08:00
|
|
|
// Make sure that the amount we push the struct back by is a multiple of the
|
|
|
|
// alignment.
|
2014-02-12 08:43:02 +08:00
|
|
|
CharUnits Offset = PointerInfo.Size.RoundUpToAlignment(
|
|
|
|
std::max(RequiredAlignment, Alignment));
|
2014-01-09 08:30:56 +08:00
|
|
|
// Increase the size of the object and push back all fields, the vbptr and all
|
|
|
|
// bases by the offset amount.
|
|
|
|
Size += Offset;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (uint64_t &FieldOffset : FieldOffsets)
|
|
|
|
FieldOffset += Context.toBits(Offset);
|
2014-01-09 08:30:56 +08:00
|
|
|
if (HasVBPtr)
|
|
|
|
VBPtrOffset += Offset;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (BaseOffsetsMapTy::value_type &Base : Bases)
|
|
|
|
Base.second += Offset;
|
2014-01-09 08:30:56 +08:00
|
|
|
}
|
2013-10-12 04:19:00 +08:00
|
|
|
|
2014-01-09 08:30:56 +08:00
|
|
|
void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) {
|
|
|
|
if (!HasVBPtr)
|
|
|
|
return;
|
|
|
|
// Vtordisps are always 4 bytes (even in 64-bit mode)
|
|
|
|
CharUnits VtorDispSize = CharUnits::fromQuantity(4);
|
|
|
|
CharUnits VtorDispAlignment = VtorDispSize;
|
|
|
|
// vtordisps respect pragma pack.
|
|
|
|
if (!MaxFieldAlignment.isZero())
|
|
|
|
VtorDispAlignment = std::min(VtorDispAlignment, MaxFieldAlignment);
|
|
|
|
// The alignment of the vtordisp is at least the required alignment of the
|
|
|
|
// entire record. This requirement may be present to support vtordisp
|
|
|
|
// injection.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &VBase : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl();
|
2014-02-12 08:43:02 +08:00
|
|
|
const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
|
|
|
|
RequiredAlignment =
|
|
|
|
std::max(RequiredAlignment, BaseLayout.getRequiredAlignment());
|
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
VtorDispAlignment = std::max(VtorDispAlignment, RequiredAlignment);
|
|
|
|
// Compute the vtordisp set.
|
2014-09-24 06:58:15 +08:00
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtorDispSet;
|
|
|
|
computeVtorDispSet(HasVtorDispSet, RD);
|
2013-10-12 04:19:00 +08:00
|
|
|
// Iterate through the virtual bases and lay them out.
|
2014-05-12 13:36:57 +08:00
|
|
|
const ASTRecordLayout *PreviousBaseLayout = nullptr;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &VBase : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl();
|
2014-01-09 08:30:56 +08:00
|
|
|
const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
|
2014-09-24 06:58:15 +08:00
|
|
|
bool HasVtordisp = HasVtorDispSet.count(BaseDecl) > 0;
|
2014-01-09 08:30:56 +08:00
|
|
|
// Insert padding between two bases if the left first one is zero sized or
|
|
|
|
// contains a zero sized subobject and the right is zero sized or one leads
|
|
|
|
// with a zero sized base. The padding between virtual bases is 4
|
|
|
|
// bytes (in both 32 and 64 bits modes) and always involves rounding up to
|
|
|
|
// the required alignment, we don't know why.
|
2014-04-12 08:20:50 +08:00
|
|
|
if ((PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() &&
|
2014-07-16 15:16:58 +08:00
|
|
|
BaseLayout.leadsWithZeroSizedBase()) || HasVtordisp) {
|
2014-01-09 08:30:56 +08:00
|
|
|
Size = Size.RoundUpToAlignment(VtorDispAlignment) + VtorDispSize;
|
2014-07-17 08:55:19 +08:00
|
|
|
Alignment = std::max(VtorDispAlignment, Alignment);
|
2014-07-16 15:16:58 +08:00
|
|
|
}
|
2014-01-09 08:30:56 +08:00
|
|
|
// Insert the virtual base.
|
|
|
|
ElementInfo Info = getAdjustedElementInfo(BaseLayout);
|
2014-01-10 09:28:05 +08:00
|
|
|
CharUnits BaseOffset = Size.RoundUpToAlignment(Info.Alignment);
|
2014-01-09 08:30:56 +08:00
|
|
|
VBases.insert(std::make_pair(BaseDecl,
|
|
|
|
ASTRecordLayout::VBaseInfo(BaseOffset, HasVtordisp)));
|
2014-02-21 09:40:35 +08:00
|
|
|
Size = BaseOffset + BaseLayout.getNonVirtualSize();
|
2014-01-09 08:30:56 +08:00
|
|
|
PreviousBaseLayout = &BaseLayout;
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 06:28:32 +08:00
|
|
|
void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) {
|
2014-01-09 08:30:56 +08:00
|
|
|
// Respect required alignment. Note that in 32-bit mode Required alignment
|
2014-09-30 14:45:43 +08:00
|
|
|
// may be 0 and cause size not to be updated.
|
2014-02-21 09:40:35 +08:00
|
|
|
DataSize = Size;
|
2014-01-09 08:30:56 +08:00
|
|
|
if (!RequiredAlignment.isZero()) {
|
|
|
|
Alignment = std::max(Alignment, RequiredAlignment);
|
2014-04-11 06:15:18 +08:00
|
|
|
auto RoundingAlignment = Alignment;
|
|
|
|
if (!MaxFieldAlignment.isZero())
|
|
|
|
RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment);
|
|
|
|
RoundingAlignment = std::max(RoundingAlignment, RequiredAlignment);
|
|
|
|
Size = Size.RoundUpToAlignment(RoundingAlignment);
|
2014-01-09 08:30:56 +08:00
|
|
|
}
|
2013-12-07 03:54:25 +08:00
|
|
|
if (Size.isZero()) {
|
2014-04-10 05:57:24 +08:00
|
|
|
EndsWithZeroSizedObject = true;
|
2013-12-07 03:54:25 +08:00
|
|
|
LeadsWithZeroSizedBase = true;
|
2014-09-30 14:45:43 +08:00
|
|
|
// Zero-sized structures have size equal to their alignment if a
|
|
|
|
// __declspec(align) came into play.
|
|
|
|
if (RequiredAlignment >= MinEmptyStructSize)
|
|
|
|
Size = Alignment;
|
|
|
|
else
|
|
|
|
Size = MinEmptyStructSize;
|
2013-12-07 03:54:25 +08:00
|
|
|
}
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
|
2014-04-12 06:05:28 +08:00
|
|
|
// Recursively walks the non-virtual bases of a class and determines if any of
|
|
|
|
// them are in the bases with overridden methods set.
|
2014-07-16 14:30:31 +08:00
|
|
|
static bool
|
|
|
|
RequiresVtordisp(const llvm::SmallPtrSetImpl<const CXXRecordDecl *> &
|
|
|
|
BasesWithOverriddenMethods,
|
|
|
|
const CXXRecordDecl *RD) {
|
2014-04-12 06:05:28 +08:00
|
|
|
if (BasesWithOverriddenMethods.count(RD))
|
2013-10-12 04:19:00 +08:00
|
|
|
return true;
|
|
|
|
// If any of a virtual bases non-virtual bases (recursively) requires a
|
|
|
|
// vtordisp than so does this virtual base.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases())
|
|
|
|
if (!Base.isVirtual() &&
|
2014-04-12 06:05:28 +08:00
|
|
|
RequiresVtordisp(BasesWithOverriddenMethods,
|
2014-07-16 14:04:00 +08:00
|
|
|
Base.getType()->getAsCXXRecordDecl()))
|
2013-10-12 04:19:00 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-24 06:58:15 +08:00
|
|
|
void MicrosoftRecordLayoutBuilder::computeVtorDispSet(
|
|
|
|
llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtordispSet,
|
|
|
|
const CXXRecordDecl *RD) const {
|
2014-02-13 07:50:26 +08:00
|
|
|
// /vd2 or #pragma vtordisp(2): Always use vtordisps for virtual bases with
|
|
|
|
// vftables.
|
|
|
|
if (RD->getMSVtorDispMode() == MSVtorDispAttr::ForVFTable) {
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2014-02-13 07:50:26 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
|
|
|
|
if (Layout.hasExtendableVFPtr())
|
|
|
|
HasVtordispSet.insert(BaseDecl);
|
|
|
|
}
|
2014-09-24 06:58:15 +08:00
|
|
|
return;
|
2014-02-13 07:50:26 +08:00
|
|
|
}
|
|
|
|
|
2013-10-12 04:19:00 +08:00
|
|
|
// If any of our bases need a vtordisp for this type, so do we. Check our
|
|
|
|
// direct bases for vtordisp requirements.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2013-10-12 04:19:00 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
|
2014-04-12 00:57:42 +08:00
|
|
|
for (const auto &bi : Layout.getVBaseOffsetsMap())
|
|
|
|
if (bi.second.hasVtorDisp())
|
|
|
|
HasVtordispSet.insert(bi.first);
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
2014-04-13 10:27:32 +08:00
|
|
|
// We don't introduce any additional vtordisps if either:
|
|
|
|
// * A user declared constructor or destructor aren't declared.
|
|
|
|
// * #pragma vtordisp(0) or the /vd0 flag are in use.
|
|
|
|
if ((!RD->hasUserDeclaredConstructor() && !RD->hasUserDeclaredDestructor()) ||
|
|
|
|
RD->getMSVtorDispMode() == MSVtorDispAttr::Never)
|
2014-09-24 06:58:15 +08:00
|
|
|
return;
|
2014-04-13 10:27:32 +08:00
|
|
|
// /vd1 or #pragma vtordisp(1): Try to guess based on whether we think it's
|
|
|
|
// possible for a partially constructed object with virtual base overrides to
|
|
|
|
// escape a non-trivial constructor.
|
|
|
|
assert(RD->getMSVtorDispMode() == MSVtorDispAttr::ForVBaseOverride);
|
2014-04-12 06:05:28 +08:00
|
|
|
// Compute a set of base classes which define methods we override. A virtual
|
|
|
|
// base in this set will require a vtordisp. A virtual base that transitively
|
|
|
|
// contains one of these bases as a non-virtual base will also require a
|
|
|
|
// vtordisp.
|
|
|
|
llvm::SmallPtrSet<const CXXMethodDecl *, 8> Work;
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 2> BasesWithOverriddenMethods;
|
2014-09-24 06:58:15 +08:00
|
|
|
// Seed the working set with our non-destructor, non-pure virtual methods.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXMethodDecl *MD : RD->methods())
|
2014-09-24 06:58:15 +08:00
|
|
|
if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD) && !MD->isPure())
|
2014-07-16 14:04:00 +08:00
|
|
|
Work.insert(MD);
|
2014-04-12 06:05:28 +08:00
|
|
|
while (!Work.empty()) {
|
|
|
|
const CXXMethodDecl *MD = *Work.begin();
|
|
|
|
CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
|
|
|
|
e = MD->end_overridden_methods();
|
|
|
|
// If a virtual method has no-overrides it lives in its parent's vtable.
|
|
|
|
if (i == e)
|
|
|
|
BasesWithOverriddenMethods.insert(MD->getParent());
|
|
|
|
else
|
|
|
|
Work.insert(i, e);
|
|
|
|
// We've finished processing this element, remove it from the working set.
|
|
|
|
Work.erase(MD);
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
2014-04-12 06:05:28 +08:00
|
|
|
// For each of our virtual bases, check if it is in the set of overridden
|
|
|
|
// bases or if it transitively contains a non-virtual base that is.
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
2014-01-09 08:30:56 +08:00
|
|
|
if (!HasVtordispSet.count(BaseDecl) &&
|
2014-04-12 06:05:28 +08:00
|
|
|
RequiresVtordisp(BasesWithOverriddenMethods, BaseDecl))
|
2014-01-09 08:30:56 +08:00
|
|
|
HasVtordispSet.insert(BaseDecl);
|
2013-10-12 04:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get or compute information about the layout of the specified record
|
|
|
|
/// (struct/union/class), which indicates its size and field position
|
|
|
|
/// information.
|
|
|
|
const ASTRecordLayout *
|
|
|
|
ASTContext::BuildMicrosoftASTRecordLayout(const RecordDecl *D) const {
|
|
|
|
MicrosoftRecordLayoutBuilder Builder(*this);
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
Builder.cxxLayout(RD);
|
|
|
|
return new (*this) ASTRecordLayout(
|
2013-12-06 08:01:17 +08:00
|
|
|
*this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment,
|
2014-01-09 08:30:56 +08:00
|
|
|
Builder.HasOwnVFPtr,
|
|
|
|
Builder.HasOwnVFPtr || Builder.PrimaryBase,
|
2014-02-21 09:40:35 +08:00
|
|
|
Builder.VBPtrOffset, Builder.NonVirtualSize, Builder.FieldOffsets.data(),
|
|
|
|
Builder.FieldOffsets.size(), Builder.NonVirtualSize,
|
2014-01-09 08:30:56 +08:00
|
|
|
Builder.Alignment, CharUnits::Zero(), Builder.PrimaryBase,
|
2013-12-07 03:54:25 +08:00
|
|
|
false, Builder.SharedVBPtrBase,
|
2014-04-10 05:57:24 +08:00
|
|
|
Builder.EndsWithZeroSizedObject, Builder.LeadsWithZeroSizedBase,
|
2013-12-07 03:54:25 +08:00
|
|
|
Builder.Bases, Builder.VBases);
|
2013-10-12 04:19:00 +08:00
|
|
|
} else {
|
|
|
|
Builder.layout(D);
|
|
|
|
return new (*this) ASTRecordLayout(
|
2013-12-06 08:01:17 +08:00
|
|
|
*this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment,
|
|
|
|
Builder.Size, Builder.FieldOffsets.data(), Builder.FieldOffsets.size());
|
2013-10-12 04:19:00 +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 {
|
2011-10-07 10:39:22 +08:00
|
|
|
// These asserts test different things. A record has a definition
|
|
|
|
// as soon as we begin to parse the definition. That definition is
|
|
|
|
// not a complete definition (which is what isDefinition() tests)
|
|
|
|
// until we *finish* parsing the definition.
|
2012-02-08 08:04:52 +08:00
|
|
|
|
|
|
|
if (D->hasExternalLexicalStorage() && !D->getDefinition())
|
|
|
|
getExternalSource()->CompleteType(const_cast<RecordDecl*>(D));
|
|
|
|
|
2010-05-26 12:56:53 +08:00
|
|
|
D = D->getDefinition();
|
|
|
|
assert(D && "Cannot get layout of forward declarations!");
|
2013-06-26 06:19:15 +08:00
|
|
|
assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!");
|
2011-10-07 14:10:15 +08:00
|
|
|
assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
|
2010-05-26 12:56:53 +08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
const ASTRecordLayout *NewEntry = nullptr;
|
2010-05-26 13:10:47 +08:00
|
|
|
|
2014-02-21 07:07:29 +08:00
|
|
|
if (isMsLayout(D) && !D->getASTContext().getExternalSource()) {
|
2013-10-12 04:19:00 +08:00
|
|
|
NewEntry = BuildMicrosoftASTRecordLayout(D);
|
|
|
|
} else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
|
2010-05-27 08:07:01 +08:00
|
|
|
EmptySubobjectMap EmptySubobjects(*this, RD);
|
2011-11-08 12:01:03 +08:00
|
|
|
RecordLayoutBuilder Builder(*this, &EmptySubobjects);
|
|
|
|
Builder.Layout(RD);
|
|
|
|
|
2013-01-29 09:14:22 +08:00
|
|
|
// In certain situations, we are allowed to lay out objects in the
|
|
|
|
// tail-padding of base classes. This is ABI-dependent.
|
|
|
|
// FIXME: this should be stored in the record layout.
|
|
|
|
bool skipTailPadding =
|
|
|
|
mustSkipTailPadding(getTargetInfo().getCXXABI(), cast<CXXRecordDecl>(D));
|
2010-05-26 13:10:47 +08:00
|
|
|
|
|
|
|
// FIXME: This should be done in FinalizeLayout.
|
2011-02-28 10:01:38 +08:00
|
|
|
CharUnits DataSize =
|
2013-01-29 09:14:22 +08:00
|
|
|
skipTailPadding ? Builder.getSize() : Builder.getDataSize();
|
2011-02-28 10:01:38 +08:00
|
|
|
CharUnits NonVirtualSize =
|
2013-01-29 09:14:22 +08:00
|
|
|
skipTailPadding ? DataSize : Builder.NonVirtualSize;
|
2010-05-27 10:25:46 +08:00
|
|
|
NewEntry =
|
2011-11-08 12:01:03 +08:00
|
|
|
new (*this) ASTRecordLayout(*this, Builder.getSize(),
|
2013-12-06 08:01:17 +08:00
|
|
|
Builder.Alignment,
|
|
|
|
/*RequiredAlignment : used by MS-ABI)*/
|
2011-11-08 12:01:03 +08:00
|
|
|
Builder.Alignment,
|
2012-05-01 16:55:32 +08:00
|
|
|
Builder.HasOwnVFPtr,
|
2013-10-12 04:19:00 +08:00
|
|
|
RD->isDynamicClass(),
|
2013-10-24 07:53:07 +08:00
|
|
|
CharUnits::fromQuantity(-1),
|
2011-02-28 10:01:38 +08:00
|
|
|
DataSize,
|
2011-11-08 12:01:03 +08:00
|
|
|
Builder.FieldOffsets.data(),
|
|
|
|
Builder.FieldOffsets.size(),
|
2011-02-16 09:52:01 +08:00
|
|
|
NonVirtualSize,
|
2011-11-08 12:01:03 +08:00
|
|
|
Builder.NonVirtualAlignment,
|
2010-05-27 08:07:01 +08:00
|
|
|
EmptySubobjects.SizeOfLargestEmptySubobject,
|
2011-11-08 12:01:03 +08:00
|
|
|
Builder.PrimaryBase,
|
|
|
|
Builder.PrimaryBaseIsVirtual,
|
2014-05-12 13:36:57 +08:00
|
|
|
nullptr, false, false,
|
2011-11-08 12:01:03 +08:00
|
|
|
Builder.Bases, Builder.VBases);
|
2010-05-26 13:10:47 +08:00
|
|
|
} else {
|
2014-05-12 13:36:57 +08:00
|
|
|
RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr);
|
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(),
|
2013-12-06 08:01:17 +08:00
|
|
|
Builder.Alignment,
|
|
|
|
/*RequiredAlignment : used by MS-ABI)*/
|
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;
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().DumpRecordLayouts) {
|
2013-07-13 06:30:03 +08:00
|
|
|
llvm::outs() << "\n*** Dumping AST Record Layout\n";
|
|
|
|
DumpRecordLayout(D, llvm::outs(), getLangOpts().DumpRecordLayoutsSimple);
|
2010-05-26 12:56:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return *NewEntry;
|
|
|
|
}
|
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) {
|
2013-05-30 00:18:30 +08:00
|
|
|
if (!getTargetInfo().getCXXABI().hasKeyFunctions())
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2013-05-30 00:18:30 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
assert(RD->getDefinition() && "Cannot get key function for forward decl!");
|
2010-05-26 12:56:53 +08:00
|
|
|
RD = cast<CXXRecordDecl>(RD->getDefinition());
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2014-07-07 14:38:20 +08:00
|
|
|
// Beware:
|
|
|
|
// 1) computing the key function might trigger deserialization, which might
|
|
|
|
// invalidate iterators into KeyFunctions
|
|
|
|
// 2) 'get' on the LazyDeclPtr might also trigger deserialization and
|
|
|
|
// invalidate the LazyDeclPtr within the map itself
|
|
|
|
LazyDeclPtr Entry = KeyFunctions[RD];
|
|
|
|
const Decl *Result =
|
|
|
|
Entry ? Entry.get(getExternalSource()) : computeKeyFunction(*this, RD);
|
|
|
|
|
|
|
|
// Store it back if it changed.
|
|
|
|
if (Entry.isOffset() || Entry.isValid() != bool(Result))
|
|
|
|
KeyFunctions[RD] = const_cast<Decl*>(Result);
|
|
|
|
|
|
|
|
return cast_or_null<CXXMethodDecl>(Result);
|
2013-01-26 06:31:03 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 07:59:27 +08:00
|
|
|
void ASTContext::setNonKeyFunction(const CXXMethodDecl *Method) {
|
2013-10-17 23:37:26 +08:00
|
|
|
assert(Method == Method->getFirstDecl() &&
|
2013-01-26 06:31:03 +08:00
|
|
|
"not working with method declaration from class definition");
|
2010-05-27 10:25:46 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
// Look up the cache entry. Since we're working with the first
|
|
|
|
// declaration, its parent must be the class definition, which is
|
|
|
|
// the correct key for the KeyFunctions hash.
|
2013-08-30 07:59:27 +08:00
|
|
|
llvm::DenseMap<const CXXRecordDecl*, LazyDeclPtr>::iterator
|
|
|
|
I = KeyFunctions.find(Method->getParent());
|
2013-01-26 06:31:03 +08:00
|
|
|
|
|
|
|
// If it's not cached, there's nothing to do.
|
2013-08-30 07:59:27 +08:00
|
|
|
if (I == KeyFunctions.end()) return;
|
2013-01-26 06:31:03 +08:00
|
|
|
|
|
|
|
// If it is cached, check whether it's the target method, and if so,
|
2014-07-07 14:38:20 +08:00
|
|
|
// remove it from the cache. Note, the call to 'get' might invalidate
|
|
|
|
// the iterator and the LazyDeclPtr object within the map.
|
|
|
|
LazyDeclPtr Ptr = I->second;
|
|
|
|
if (Ptr.get(getExternalSource()) == Method) {
|
2013-01-26 06:31:03 +08:00
|
|
|
// FIXME: remember that we did this for module / chained PCH state?
|
2014-07-07 14:38:20 +08:00
|
|
|
KeyFunctions.erase(Method->getParent());
|
2013-01-26 06:31:03 +08:00
|
|
|
}
|
2010-05-26 12:56:53 +08:00
|
|
|
}
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) {
|
|
|
|
const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent());
|
|
|
|
return Layout.getFieldOffset(FD->getFieldIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const {
|
|
|
|
uint64_t OffsetInBits;
|
|
|
|
if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
|
|
|
|
OffsetInBits = ::getFieldOffset(*this, FD);
|
|
|
|
} else {
|
|
|
|
const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD);
|
|
|
|
|
|
|
|
OffsetInBits = 0;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const NamedDecl *ND : IFD->chain())
|
|
|
|
OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(ND));
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return OffsetInBits;
|
|
|
|
}
|
|
|
|
|
2011-10-05 14:00:51 +08:00
|
|
|
/// getObjCLayout - Get or compute information about the layout of the
|
|
|
|
/// given interface.
|
2010-05-26 12:56:53 +08:00
|
|
|
///
|
|
|
|
/// \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 {
|
2011-12-20 23:50:13 +08:00
|
|
|
// Retrieve the definition
|
2012-03-16 00:33:08 +08:00
|
|
|
if (D->hasExternalLexicalStorage() && !D->getDefinition())
|
|
|
|
getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D));
|
2011-12-20 23:50:13 +08:00
|
|
|
D = D->getDefinition();
|
|
|
|
assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!");
|
2010-05-26 12:56:53 +08:00
|
|
|
|
|
|
|
// Look up this layout, if already laid out, return what we have.
|
2012-09-06 23:59:27 +08:00
|
|
|
const ObjCContainerDecl *Key =
|
|
|
|
Impl ? (const ObjCContainerDecl*) Impl : (const ObjCContainerDecl*) D;
|
2010-05-26 12:56:53 +08:00
|
|
|
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)
|
2014-05-12 13:36:57 +08:00
|
|
|
return getObjCLayout(D, nullptr);
|
2010-05-26 12:56:53 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr);
|
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(),
|
2013-12-06 08:01:17 +08:00
|
|
|
Builder.Alignment,
|
|
|
|
/*RequiredAlignment : used by MS-ABI)*/
|
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) {
|
2011-11-05 17:02:52 +08:00
|
|
|
OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity());
|
2010-04-08 10:59:49 +08:00
|
|
|
OS.indent(IndentLevel * 2);
|
|
|
|
}
|
|
|
|
|
2012-12-08 08:07:24 +08:00
|
|
|
static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) {
|
|
|
|
OS << " | ";
|
|
|
|
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();
|
2013-10-12 04:19:00 +08:00
|
|
|
bool HasOwnVFPtr = Layout.hasOwnVFPtr();
|
|
|
|
bool HasOwnVBPtr = Layout.hasOwnVBPtr();
|
2010-04-08 10:59:49 +08:00
|
|
|
|
|
|
|
// Vtable pointer.
|
2013-10-12 04:19:00 +08:00
|
|
|
if (RD->isDynamicClass() && !PrimaryBase && !isMsLayout(RD)) {
|
2010-04-08 10:59:49 +08:00
|
|
|
PrintOffset(OS, Offset, IndentLevel);
|
2011-10-15 02:45:37 +08:00
|
|
|
OS << '(' << *RD << " vtable pointer)\n";
|
2013-10-12 04:19:00 +08:00
|
|
|
} else if (HasOwnVFPtr) {
|
|
|
|
PrintOffset(OS, Offset, IndentLevel);
|
|
|
|
// vfptr (for Microsoft C++ ABI)
|
|
|
|
OS << '(' << *RD << " vftable pointer)\n";
|
2010-04-08 10:59:49 +08:00
|
|
|
}
|
2013-10-12 04:19:00 +08:00
|
|
|
|
2014-02-28 09:03:09 +08:00
|
|
|
// Collect nvbases.
|
|
|
|
SmallVector<const CXXRecordDecl *, 4> Bases;
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
assert(!Base.getType()->isDependentType() &&
|
2010-04-08 10:59:49 +08:00
|
|
|
"Cannot layout class with dependent bases.");
|
2014-07-16 14:04:00 +08:00
|
|
|
if (!Base.isVirtual())
|
|
|
|
Bases.push_back(Base.getType()->getAsCXXRecordDecl());
|
2014-02-28 09:03:09 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2014-02-28 09:03:09 +08:00
|
|
|
// Sort nvbases by offset.
|
2014-03-01 22:48:57 +08:00
|
|
|
std::stable_sort(Bases.begin(), Bases.end(),
|
|
|
|
[&](const CXXRecordDecl *L, const CXXRecordDecl *R) {
|
|
|
|
return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R);
|
|
|
|
});
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2014-02-28 09:03:09 +08:00
|
|
|
// Dump (non-virtual) bases
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXRecordDecl *Base : Bases) {
|
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);
|
|
|
|
}
|
2011-10-22 06:49:56 +08:00
|
|
|
|
2013-10-12 04:19:00 +08:00
|
|
|
// vbptr (for Microsoft C++ ABI)
|
|
|
|
if (HasOwnVBPtr) {
|
2011-09-28 03:12:27 +08:00
|
|
|
PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
|
2011-10-15 02:45:37 +08:00
|
|
|
OS << '(' << *RD << " vbtable pointer)\n";
|
2011-09-28 03:12:27 +08:00
|
|
|
}
|
2010-04-08 10:59:49 +08:00
|
|
|
|
|
|
|
// Dump fields.
|
|
|
|
uint64_t FieldNo = 0;
|
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(),
|
|
|
|
E = RD->field_end(); I != E; ++I, ++FieldNo) {
|
2012-06-07 04:45:41 +08:00
|
|
|
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
|
|
|
|
2014-04-12 00:57:42 +08:00
|
|
|
if (const CXXRecordDecl *D = Field.getType()->getAsCXXRecordDecl()) {
|
|
|
|
DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
|
|
|
|
Field.getName().data(),
|
|
|
|
/*IncludeVirtualBases=*/true);
|
|
|
|
continue;
|
2010-04-08 10:59:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PrintOffset(OS, FieldOffset, IndentLevel);
|
2012-04-30 10:36:29 +08:00
|
|
|
OS << Field.getType().getAsString() << ' ' << Field << '\n';
|
2010-04-08 10:59:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IncludeVirtualBases)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Dump virtual bases.
|
2012-05-01 16:55:32 +08:00
|
|
|
const ASTRecordLayout::VBaseOffsetsMapTy &vtordisps =
|
|
|
|
Layout.getVBaseOffsetsMap();
|
2014-07-16 14:04:00 +08:00
|
|
|
for (const CXXBaseSpecifier &Base : RD->vbases()) {
|
|
|
|
assert(Base.isVirtual() && "Found non-virtual class!");
|
|
|
|
const CXXRecordDecl *VBase = Base.getType()->getAsCXXRecordDecl();
|
2010-04-08 10:59:49 +08:00
|
|
|
|
2010-11-01 07:45:59 +08:00
|
|
|
CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
|
2012-05-01 16:55:32 +08:00
|
|
|
|
|
|
|
if (vtordisps.find(VBase)->second.hasVtorDisp()) {
|
|
|
|
PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel);
|
|
|
|
OS << "(vtordisp for vbase " << *VBase << ")\n";
|
|
|
|
}
|
|
|
|
|
2010-04-08 10:59:49 +08:00
|
|
|
DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
|
|
|
|
VBase == PrimaryBase ?
|
|
|
|
"(primary virtual base)" : "(virtual base)",
|
|
|
|
/*IncludeVirtualBases=*/false);
|
|
|
|
}
|
|
|
|
|
2012-12-08 08:07:24 +08:00
|
|
|
PrintIndentNoOffset(OS, IndentLevel - 1);
|
|
|
|
OS << "[sizeof=" << Layout.getSize().getQuantity();
|
2013-10-12 04:19:00 +08:00
|
|
|
if (!isMsLayout(RD))
|
|
|
|
OS << ", dsize=" << Layout.getDataSize().getQuantity();
|
2011-02-15 10:32:40 +08:00
|
|
|
OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
|
2012-12-08 08:07:24 +08:00
|
|
|
|
|
|
|
PrintIndentNoOffset(OS, IndentLevel - 1);
|
|
|
|
OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
|
2014-01-09 08:30:56 +08:00
|
|
|
OS << ", nvalign=" << Layout.getNonVirtualAlignment().getQuantity() << "]\n";
|
2010-04-08 10:59:49 +08:00
|
|
|
}
|
2010-04-20 04:44:53 +08:00
|
|
|
|
|
|
|
void ASTContext::DumpRecordLayout(const RecordDecl *RD,
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
raw_ostream &OS,
|
|
|
|
bool Simple) const {
|
2010-04-20 04:44:53 +08:00
|
|
|
const ASTRecordLayout &Info = getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
if (!Simple)
|
2014-05-12 13:36:57 +08:00
|
|
|
return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, nullptr,
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
/*IncludeVirtualBases=*/true);
|
2010-04-20 04:44:53 +08:00
|
|
|
|
|
|
|
OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
if (!Simple) {
|
|
|
|
OS << "Record: ";
|
|
|
|
RD->dump();
|
|
|
|
}
|
2010-04-20 04:44:53 +08:00
|
|
|
OS << "\nLayout: ";
|
|
|
|
OS << "<ASTRecordLayout\n";
|
2011-02-11 09:54:29 +08:00
|
|
|
OS << " Size:" << toBits(Info.getSize()) << "\n";
|
2013-10-12 04:19:00 +08:00
|
|
|
if (!isMsLayout(RD))
|
|
|
|
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";
|
|
|
|
}
|