2009-11-24 13:51:11 +08:00
|
|
|
//===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===//
|
2009-09-12 12:27:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation of classes
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2009-10-07 06:43:30 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-09-12 12:27:24 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-10-07 06:43:30 +08:00
|
|
|
|
2009-09-12 12:27:24 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
static uint64_t
|
|
|
|
ComputeNonVirtualBaseClassOffset(ASTContext &Context, CXXBasePaths &Paths,
|
|
|
|
unsigned Start) {
|
|
|
|
uint64_t Offset = 0;
|
2009-09-12 12:27:24 +08:00
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
const CXXBasePath &Path = Paths.front();
|
|
|
|
for (unsigned i = Start, e = Path.size(); i != e; ++i) {
|
|
|
|
const CXXBasePathElement& Element = Path[i];
|
2009-09-12 12:27:24 +08:00
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
// Get the layout.
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
|
2009-09-12 12:27:24 +08:00
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
const CXXBaseSpecifier *BS = Element.Base;
|
|
|
|
assert(!BS->isVirtual() && "Should not see virtual bases here!");
|
2009-09-12 12:27:24 +08:00
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(BS->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// Add the offset.
|
|
|
|
Offset += Layout.getBaseClassOffset(Base) / 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Offset;
|
2009-09-12 12:27:24 +08:00
|
|
|
}
|
|
|
|
|
2009-09-29 11:13:20 +08:00
|
|
|
llvm::Constant *
|
2009-10-03 22:56:57 +08:00
|
|
|
CodeGenModule::GetCXXBaseClassOffset(const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) {
|
2009-09-29 11:13:20 +08:00
|
|
|
if (ClassDecl == BaseClassDecl)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false,
|
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/false);
|
|
|
|
if (!const_cast<CXXRecordDecl *>(ClassDecl)->
|
|
|
|
isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) {
|
|
|
|
assert(false && "Class must be derived from the passed in base class!");
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-29 11:13:20 +08:00
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
uint64_t Offset = ComputeNonVirtualBaseClassOffset(getContext(), Paths, 0);
|
2009-09-29 11:13:20 +08:00
|
|
|
if (!Offset)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-03 22:56:57 +08:00
|
|
|
const llvm::Type *PtrDiffTy =
|
|
|
|
Types.ConvertType(getContext().getPointerDiffType());
|
2009-09-29 11:13:20 +08:00
|
|
|
|
|
|
|
return llvm::ConstantInt::get(PtrDiffTy, Offset);
|
|
|
|
}
|
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
static llvm::Value *GetCXXBaseClassOffset(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *BaseValue,
|
|
|
|
const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) {
|
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false,
|
2009-11-28 11:31:34 +08:00
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/false);
|
2009-10-07 06:43:30 +08:00
|
|
|
if (!const_cast<CXXRecordDecl *>(ClassDecl)->
|
|
|
|
isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) {
|
|
|
|
assert(false && "Class must be derived from the passed in base class!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Start = 0;
|
|
|
|
llvm::Value *VirtualOffset = 0;
|
2009-11-28 11:31:34 +08:00
|
|
|
|
|
|
|
const CXXBasePath &Path = Paths.front();
|
|
|
|
const CXXRecordDecl *VBase = 0;
|
|
|
|
for (unsigned i = 0, e = Path.size(); i != e; ++i) {
|
|
|
|
const CXXBasePathElement& Element = Path[i];
|
|
|
|
if (Element.Base->isVirtual()) {
|
|
|
|
Start = i+1;
|
|
|
|
QualType VBaseType = Element.Base->getType();
|
|
|
|
VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
|
2009-10-07 06:43:30 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-28 11:31:34 +08:00
|
|
|
if (VBase)
|
|
|
|
VirtualOffset =
|
|
|
|
CGF.GetVirtualCXXBaseClassOffset(BaseValue, ClassDecl, VBase);
|
2009-10-07 06:43:30 +08:00
|
|
|
|
|
|
|
uint64_t Offset =
|
|
|
|
ComputeNonVirtualBaseClassOffset(CGF.getContext(), Paths, Start);
|
|
|
|
|
|
|
|
if (!Offset)
|
|
|
|
return VirtualOffset;
|
|
|
|
|
|
|
|
const llvm::Type *PtrDiffTy =
|
|
|
|
CGF.ConvertType(CGF.getContext().getPointerDiffType());
|
|
|
|
llvm::Value *NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, Offset);
|
|
|
|
|
|
|
|
if (VirtualOffset)
|
|
|
|
return CGF.Builder.CreateAdd(VirtualOffset, NonVirtualOffset);
|
|
|
|
|
|
|
|
return NonVirtualOffset;
|
|
|
|
}
|
|
|
|
|
2009-12-03 11:06:55 +08:00
|
|
|
// FIXME: This probably belongs in CGVtable, but it relies on
|
|
|
|
// the static function ComputeNonVirtualBaseClassOffset, so we should make that
|
|
|
|
// a CodeGenModule member function as well.
|
|
|
|
ThunkAdjustment
|
|
|
|
CodeGenModule::ComputeThunkAdjustment(const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) {
|
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false,
|
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/false);
|
|
|
|
if (!const_cast<CXXRecordDecl *>(ClassDecl)->
|
|
|
|
isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) {
|
|
|
|
assert(false && "Class must be derived from the passed in base class!");
|
|
|
|
return ThunkAdjustment();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Start = 0;
|
|
|
|
uint64_t VirtualOffset = 0;
|
|
|
|
|
|
|
|
const CXXBasePath &Path = Paths.front();
|
|
|
|
const CXXRecordDecl *VBase = 0;
|
|
|
|
for (unsigned i = 0, e = Path.size(); i != e; ++i) {
|
|
|
|
const CXXBasePathElement& Element = Path[i];
|
|
|
|
if (Element.Base->isVirtual()) {
|
|
|
|
Start = i+1;
|
|
|
|
QualType VBaseType = Element.Base->getType();
|
|
|
|
VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (VBase)
|
|
|
|
VirtualOffset =
|
|
|
|
getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
|
|
|
|
|
|
|
|
uint64_t Offset =
|
|
|
|
ComputeNonVirtualBaseClassOffset(getContext(), Paths, Start);
|
|
|
|
return ThunkAdjustment(Offset, VirtualOffset);
|
|
|
|
}
|
|
|
|
|
2009-09-12 12:27:24 +08:00
|
|
|
llvm::Value *
|
2009-11-24 01:57:54 +08:00
|
|
|
CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value,
|
|
|
|
const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl,
|
|
|
|
bool NullCheckValue) {
|
2009-09-23 05:58:22 +08:00
|
|
|
QualType BTy =
|
|
|
|
getContext().getCanonicalType(
|
|
|
|
getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl)));
|
|
|
|
const llvm::Type *BasePtrTy = llvm::PointerType::getUnqual(ConvertType(BTy));
|
2009-09-12 12:27:24 +08:00
|
|
|
|
2009-10-07 06:43:30 +08:00
|
|
|
if (ClassDecl == BaseClassDecl) {
|
2009-09-23 05:58:22 +08:00
|
|
|
// Just cast back.
|
2009-11-24 01:57:54 +08:00
|
|
|
return Builder.CreateBitCast(Value, BasePtrTy);
|
2009-09-23 05:58:22 +08:00
|
|
|
}
|
2009-11-11 06:48:10 +08:00
|
|
|
|
2009-09-12 14:04:24 +08:00
|
|
|
llvm::BasicBlock *CastNull = 0;
|
|
|
|
llvm::BasicBlock *CastNotNull = 0;
|
|
|
|
llvm::BasicBlock *CastEnd = 0;
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
CastNull = createBasicBlock("cast.null");
|
|
|
|
CastNotNull = createBasicBlock("cast.notnull");
|
|
|
|
CastEnd = createBasicBlock("cast.end");
|
|
|
|
|
|
|
|
llvm::Value *IsNull =
|
2009-11-24 01:57:54 +08:00
|
|
|
Builder.CreateICmpEQ(Value,
|
|
|
|
llvm::Constant::getNullValue(Value->getType()));
|
2009-09-12 14:04:24 +08:00
|
|
|
Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
|
|
|
|
EmitBlock(CastNotNull);
|
|
|
|
}
|
|
|
|
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
|
2009-11-11 06:48:10 +08:00
|
|
|
|
|
|
|
llvm::Value *Offset =
|
2009-11-24 01:57:54 +08:00
|
|
|
GetCXXBaseClassOffset(*this, Value, ClassDecl, BaseClassDecl);
|
2009-09-12 12:27:24 +08:00
|
|
|
|
2009-11-11 06:48:10 +08:00
|
|
|
if (Offset) {
|
|
|
|
// Apply the offset.
|
2009-11-24 01:57:54 +08:00
|
|
|
Value = Builder.CreateBitCast(Value, Int8PtrTy);
|
|
|
|
Value = Builder.CreateGEP(Value, Offset, "add.ptr");
|
2009-11-11 06:48:10 +08:00
|
|
|
}
|
2009-09-12 12:27:24 +08:00
|
|
|
|
|
|
|
// Cast back.
|
2009-11-24 01:57:54 +08:00
|
|
|
Value = Builder.CreateBitCast(Value, BasePtrTy);
|
2009-09-12 14:04:24 +08:00
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
Builder.CreateBr(CastEnd);
|
|
|
|
EmitBlock(CastNull);
|
|
|
|
Builder.CreateBr(CastEnd);
|
|
|
|
EmitBlock(CastEnd);
|
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
llvm::PHINode *PHI = Builder.CreatePHI(Value->getType());
|
|
|
|
PHI->reserveOperandSpace(2);
|
|
|
|
PHI->addIncoming(Value, CastNotNull);
|
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
|
|
|
|
CastNull);
|
|
|
|
Value = PHI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
|
|
|
|
const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *DerivedClassDecl,
|
|
|
|
bool NullCheckValue) {
|
|
|
|
QualType DerivedTy =
|
|
|
|
getContext().getCanonicalType(
|
|
|
|
getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(DerivedClassDecl)));
|
|
|
|
const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
|
|
|
|
|
|
|
|
if (ClassDecl == DerivedClassDecl) {
|
|
|
|
// Just cast back.
|
|
|
|
return Builder.CreateBitCast(Value, DerivedPtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::BasicBlock *CastNull = 0;
|
|
|
|
llvm::BasicBlock *CastNotNull = 0;
|
|
|
|
llvm::BasicBlock *CastEnd = 0;
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
CastNull = createBasicBlock("cast.null");
|
|
|
|
CastNotNull = createBasicBlock("cast.notnull");
|
|
|
|
CastEnd = createBasicBlock("cast.end");
|
|
|
|
|
|
|
|
llvm::Value *IsNull =
|
|
|
|
Builder.CreateICmpEQ(Value,
|
|
|
|
llvm::Constant::getNullValue(Value->getType()));
|
|
|
|
Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
|
|
|
|
EmitBlock(CastNotNull);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *Offset = GetCXXBaseClassOffset(*this, Value, DerivedClassDecl,
|
|
|
|
ClassDecl);
|
|
|
|
if (Offset) {
|
|
|
|
// Apply the offset.
|
|
|
|
Value = Builder.CreatePtrToInt(Value, Offset->getType());
|
|
|
|
Value = Builder.CreateSub(Value, Offset);
|
|
|
|
Value = Builder.CreateIntToPtr(Value, DerivedPtrTy);
|
|
|
|
} else {
|
|
|
|
// Just cast.
|
|
|
|
Value = Builder.CreateBitCast(Value, DerivedPtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
Builder.CreateBr(CastEnd);
|
|
|
|
EmitBlock(CastNull);
|
|
|
|
Builder.CreateBr(CastEnd);
|
|
|
|
EmitBlock(CastEnd);
|
|
|
|
|
|
|
|
llvm::PHINode *PHI = Builder.CreatePHI(Value->getType());
|
2009-09-12 14:04:24 +08:00
|
|
|
PHI->reserveOperandSpace(2);
|
2009-11-24 01:57:54 +08:00
|
|
|
PHI->addIncoming(Value, CastNotNull);
|
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
|
2009-09-12 14:04:24 +08:00
|
|
|
CastNull);
|
2009-11-24 01:57:54 +08:00
|
|
|
Value = PHI;
|
2009-09-12 14:04:24 +08:00
|
|
|
}
|
2009-09-12 12:27:24 +08:00
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
return Value;
|
2009-09-12 12:27:24 +08:00
|
|
|
}
|