2008-01-26 09:36:00 +08:00
|
|
|
//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Constant Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2008-10-06 13:59:01 +08:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-24 23:20:52 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
2008-05-30 18:24:46 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class ConstStructBuilder {
|
2009-07-24 23:20:52 +08:00
|
|
|
CodeGenModule &CGM;
|
|
|
|
CodeGenFunction *CGF;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Packed;
|
2009-07-25 08:24:56 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned NextFieldOffsetInBytes;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-02 10:15:20 +08:00
|
|
|
unsigned LLVMStructAlignment;
|
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
std::vector<llvm::Constant *> Elements;
|
|
|
|
|
|
|
|
ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
|
2009-10-02 10:15:20 +08:00
|
|
|
: CGM(CGM), CGF(CGF), Packed(false), NextFieldOffsetInBytes(0),
|
|
|
|
LLVMStructAlignment(1) { }
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
|
2009-07-24 23:20:52 +08:00
|
|
|
const Expr *InitExpr) {
|
|
|
|
uint64_t FieldOffsetInBytes = FieldOffset / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
assert(NextFieldOffsetInBytes <= FieldOffsetInBytes
|
2009-07-24 23:20:52 +08:00
|
|
|
&& "Field offset mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Emit the field.
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(InitExpr, Field->getType(), CGF);
|
|
|
|
if (!C)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned FieldAlignment = getAlignment(C);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Round up the field offset to the alignment of the field type.
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t AlignedNextFieldOffsetInBytes =
|
2009-07-24 23:20:52 +08:00
|
|
|
llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
if (AlignedNextFieldOffsetInBytes > FieldOffsetInBytes) {
|
2009-07-25 09:40:29 +08:00
|
|
|
assert(!Packed && "Alignment is wrong even with a packed struct!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 09:40:29 +08:00
|
|
|
// Convert the struct to a packed struct.
|
2009-10-02 10:15:20 +08:00
|
|
|
ConvertStructToPacked();
|
|
|
|
|
2009-07-25 09:40:29 +08:00
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
|
|
|
|
// We need to append padding.
|
2009-07-25 09:40:29 +08:00
|
|
|
AppendPadding(FieldOffsetInBytes - NextFieldOffsetInBytes);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
assert(NextFieldOffsetInBytes == FieldOffsetInBytes &&
|
|
|
|
"Did not add enough padding!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Add the field.
|
|
|
|
Elements.push_back(C);
|
|
|
|
NextFieldOffsetInBytes = AlignedNextFieldOffsetInBytes + getSizeInBytes(C);
|
2009-10-02 12:52:12 +08:00
|
|
|
|
|
|
|
if (Packed)
|
|
|
|
assert(LLVMStructAlignment == 1 && "Packed struct not byte-aligned!");
|
|
|
|
else
|
|
|
|
LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
|
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
|
2009-07-27 09:23:51 +08:00
|
|
|
const Expr *InitExpr) {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::ConstantInt *CI =
|
|
|
|
cast_or_null<llvm::ConstantInt>(CGM.EmitConstantExpr(InitExpr,
|
|
|
|
Field->getType(),
|
2009-07-27 09:23:51 +08:00
|
|
|
CGF));
|
|
|
|
// FIXME: Can this ever happen?
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
if (FieldOffset > NextFieldOffsetInBytes * 8) {
|
2009-07-27 10:56:37 +08:00
|
|
|
// We need to add padding.
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t NumBytes =
|
|
|
|
llvm::RoundUpToAlignment(FieldOffset -
|
2009-07-27 10:56:37 +08:00
|
|
|
NextFieldOffsetInBytes * 8, 8) / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
AppendPadding(NumBytes);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t FieldSize =
|
2009-07-27 09:23:51 +08:00
|
|
|
Field->getBitWidth()->EvaluateAsInt(CGM.getContext()).getZExtValue();
|
|
|
|
|
|
|
|
llvm::APInt FieldValue = CI->getValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
// Promote the size of FieldValue if necessary
|
|
|
|
// FIXME: This should never occur, but currently it can because initializer
|
|
|
|
// constants are cast to bool, and because clang is not enforcing bitfield
|
|
|
|
// width limits.
|
|
|
|
if (FieldSize > FieldValue.getBitWidth())
|
|
|
|
FieldValue.zext(FieldSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
// Truncate the size of FieldValue to the bit field size.
|
|
|
|
if (FieldSize < FieldValue.getBitWidth())
|
|
|
|
FieldValue.trunc(FieldSize);
|
|
|
|
|
|
|
|
if (FieldOffset < NextFieldOffsetInBytes * 8) {
|
2009-07-27 10:56:37 +08:00
|
|
|
// Either part of the field or the entire field can go into the previous
|
|
|
|
// byte.
|
|
|
|
assert(!Elements.empty() && "Elements can't be empty!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
unsigned BitsInPreviousByte =
|
2009-07-27 10:56:37 +08:00
|
|
|
NextFieldOffsetInBytes * 8 - FieldOffset;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
bool FitsCompletelyInPreviousByte =
|
2009-07-27 10:56:37 +08:00
|
|
|
BitsInPreviousByte >= FieldValue.getBitWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
llvm::APInt Tmp = FieldValue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
if (!FitsCompletelyInPreviousByte) {
|
|
|
|
unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
Tmp = Tmp.lshr(NewFieldWidth);
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
|
|
|
|
|
|
|
// We want the remaining high bits.
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
} else {
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
|
|
|
|
|
|
|
// We want the remaining low bits.
|
|
|
|
FieldValue = FieldValue.lshr(BitsInPreviousByte);
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
Tmp.zext(8);
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
if (FitsCompletelyInPreviousByte)
|
|
|
|
Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
|
|
|
|
} else {
|
|
|
|
Tmp = Tmp.shl(8 - BitsInPreviousByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or in the bits that go into the previous byte.
|
2009-12-09 05:12:32 +08:00
|
|
|
if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(Elements.back()))
|
|
|
|
Tmp |= Val->getValue();
|
|
|
|
else
|
|
|
|
assert(isa<llvm::UndefValue>(Elements.back()));
|
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
if (FitsCompletelyInPreviousByte)
|
|
|
|
return true;
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
while (FieldValue.getBitWidth() > 8) {
|
|
|
|
llvm::APInt Tmp;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
// We want the high bits.
|
2009-07-27 10:56:37 +08:00
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp = Tmp.lshr(Tmp.getBitWidth() - 8);
|
|
|
|
Tmp.trunc(8);
|
|
|
|
} else {
|
|
|
|
// We want the low bits.
|
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp.trunc(8);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
FieldValue = FieldValue.lshr(8);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
|
|
|
|
NextFieldOffsetInBytes++;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
FieldValue.trunc(FieldValue.getBitWidth() - 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(FieldValue.getBitWidth() > 0 &&
|
|
|
|
"Should have at least one bit left!");
|
|
|
|
assert(FieldValue.getBitWidth() <= 8 &&
|
|
|
|
"Should not have more than a byte left!");
|
|
|
|
|
|
|
|
if (FieldValue.getBitWidth() < 8) {
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
unsigned BitWidth = FieldValue.getBitWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
FieldValue.zext(8);
|
|
|
|
FieldValue = FieldValue << (8 - BitWidth);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else
|
2009-07-27 09:23:51 +08:00
|
|
|
FieldValue.zext(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the last element.
|
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
|
|
|
|
FieldValue));
|
|
|
|
NextFieldOffsetInBytes++;
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
void AppendPadding(uint64_t NumBytes) {
|
|
|
|
if (!NumBytes)
|
|
|
|
return;
|
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (NumBytes > 1)
|
2009-07-30 06:16:19 +08:00
|
|
|
Ty = llvm::ArrayType::get(Ty, NumBytes);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-10-29 19:27:06 +08:00
|
|
|
llvm::Constant *C = llvm::UndefValue::get(Ty);
|
2009-07-24 23:20:52 +08:00
|
|
|
Elements.push_back(C);
|
|
|
|
assert(getAlignment(C) == 1 && "Padding must have 1 byte alignment!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
NextFieldOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendTailPadding(uint64_t RecordSize) {
|
|
|
|
assert(RecordSize % 8 == 0 && "Invalid record size!");
|
|
|
|
|
|
|
|
uint64_t RecordSizeInBytes = RecordSize / 8;
|
|
|
|
assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
|
|
|
|
AppendPadding(NumPadBytes);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-02 10:15:20 +08:00
|
|
|
void ConvertStructToPacked() {
|
|
|
|
std::vector<llvm::Constant *> PackedElements;
|
|
|
|
uint64_t ElementOffsetInBytes = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
|
|
|
|
llvm::Constant *C = Elements[i];
|
|
|
|
|
|
|
|
unsigned ElementAlign =
|
|
|
|
CGM.getTargetData().getABITypeAlignment(C->getType());
|
|
|
|
uint64_t AlignedElementOffsetInBytes =
|
|
|
|
llvm::RoundUpToAlignment(ElementOffsetInBytes, ElementAlign);
|
|
|
|
|
|
|
|
if (AlignedElementOffsetInBytes > ElementOffsetInBytes) {
|
|
|
|
// We need some padding.
|
|
|
|
uint64_t NumBytes =
|
|
|
|
AlignedElementOffsetInBytes - ElementOffsetInBytes;
|
|
|
|
|
2009-11-14 05:23:46 +08:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext());
|
2009-10-02 10:15:20 +08:00
|
|
|
if (NumBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumBytes);
|
|
|
|
|
2009-10-29 19:27:06 +08:00
|
|
|
llvm::Constant *Padding = llvm::UndefValue::get(Ty);
|
2009-10-02 10:15:20 +08:00
|
|
|
PackedElements.push_back(Padding);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(Padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedElements.push_back(C);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ElementOffsetInBytes == NextFieldOffsetInBytes &&
|
|
|
|
"Packing the struct changed its size!");
|
|
|
|
|
|
|
|
Elements = PackedElements;
|
|
|
|
LLVMStructAlignment = 1;
|
|
|
|
Packed = true;
|
|
|
|
}
|
|
|
|
|
2009-08-02 07:11:24 +08:00
|
|
|
bool Build(InitListExpr *ILE) {
|
2009-07-30 05:53:49 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
|
2009-07-24 23:20:52 +08:00
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned FieldNo = 0;
|
|
|
|
unsigned ElementNo = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2009-07-24 23:20:52 +08:00
|
|
|
ElementNo < ILE->getNumInits() && Field != FieldEnd;
|
|
|
|
++Field, ++FieldNo) {
|
2009-08-02 07:11:24 +08:00
|
|
|
if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
|
|
|
|
continue;
|
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
if (Field->isBitField()) {
|
|
|
|
if (!Field->getIdentifier())
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
|
|
|
|
ILE->getInit(ElementNo)))
|
|
|
|
return false;
|
2009-07-24 23:20:52 +08:00
|
|
|
} else {
|
|
|
|
if (!AppendField(*Field, Layout.getFieldOffset(FieldNo),
|
|
|
|
ILE->getInit(ElementNo)))
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
ElementNo++;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
uint64_t LayoutSizeInBytes = Layout.getSize() / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
if (NextFieldOffsetInBytes > LayoutSizeInBytes) {
|
|
|
|
// If the struct is bigger than the size of the record type,
|
|
|
|
// we must have a flexible array member at the end.
|
|
|
|
assert(RD->hasFlexibleArrayMember() &&
|
|
|
|
"Must have flexible array member if struct is bigger than type!");
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
// No tail padding is necessary.
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-02 10:15:20 +08:00
|
|
|
uint64_t LLVMSizeInBytes = llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
|
|
|
|
LLVMStructAlignment);
|
|
|
|
|
|
|
|
// Check if we need to convert the struct to a packed struct.
|
|
|
|
if (NextFieldOffsetInBytes <= LayoutSizeInBytes &&
|
|
|
|
LLVMSizeInBytes > LayoutSizeInBytes) {
|
|
|
|
assert(!Packed && "Size mismatch!");
|
|
|
|
|
|
|
|
ConvertStructToPacked();
|
|
|
|
assert(NextFieldOffsetInBytes == LayoutSizeInBytes &&
|
|
|
|
"Converting to packed did not help!");
|
|
|
|
}
|
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Append tail padding if necessary.
|
|
|
|
AppendTailPadding(Layout.getSize());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
assert(Layout.getSize() / 8 == NextFieldOffsetInBytes &&
|
2009-07-24 23:20:52 +08:00
|
|
|
"Tail padding mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned getAlignment(const llvm::Constant *C) const {
|
|
|
|
if (Packed)
|
|
|
|
return 1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
return CGM.getTargetData().getABITypeAlignment(C->getType());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
uint64_t getSizeInBytes(const llvm::Constant *C) const {
|
|
|
|
return CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
public:
|
|
|
|
static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
|
2009-08-02 07:11:24 +08:00
|
|
|
InitListExpr *ILE) {
|
2009-07-24 23:20:52 +08:00
|
|
|
ConstStructBuilder Builder(CGM, CGF);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
if (!Builder.Build(ILE))
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
llvm::Constant *Result =
|
2009-08-06 09:24:27 +08:00
|
|
|
llvm::ConstantStruct::get(CGM.getLLVMContext(),
|
2009-08-06 07:18:46 +08:00
|
|
|
Builder.Elements, Builder.Packed);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
assert(llvm::RoundUpToAlignment(Builder.NextFieldOffsetInBytes,
|
2009-09-09 23:08:12 +08:00
|
|
|
Builder.getAlignment(Result)) ==
|
2009-07-25 08:24:56 +08:00
|
|
|
Builder.getSizeInBytes(Result) && "Size mismatch!");
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-07-25 02:20:38 +08:00
|
|
|
return Result;
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-29 03:45:26 +08:00
|
|
|
class ConstExprEmitter :
|
2008-01-26 12:30:23 +08:00
|
|
|
public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
|
2008-01-26 09:36:00 +08:00
|
|
|
CodeGenModule &CGM;
|
2008-02-27 05:41:45 +08:00
|
|
|
CodeGenFunction *CGF;
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::LLVMContext &VMContext;
|
2008-01-26 09:36:00 +08:00
|
|
|
public:
|
2008-02-27 05:41:45 +08:00
|
|
|
ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
|
2009-07-15 07:10:40 +08:00
|
|
|
: CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitStmt(Stmt *S) {
|
2009-03-04 00:43:34 +08:00
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
llvm::Constant *VisitParenExpr(ParenExpr *PE) {
|
|
|
|
return Visit(PE->getSubExpr());
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return Visit(E->getInitializer());
|
|
|
|
}
|
2009-10-13 15:14:16 +08:00
|
|
|
|
2009-10-03 23:02:02 +08:00
|
|
|
llvm::Constant *EmitMemberFunctionPointer(CXXMethodDecl *MD) {
|
|
|
|
assert(MD->isInstance() && "Member function must not be static!");
|
|
|
|
|
2010-01-05 13:04:05 +08:00
|
|
|
MD = MD->getCanonicalDecl();
|
|
|
|
|
2009-10-03 23:02:02 +08:00
|
|
|
const llvm::Type *PtrDiffTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
llvm::Constant *Values[2];
|
|
|
|
|
|
|
|
// Get the function pointer (or index if this is a virtual function).
|
|
|
|
if (MD->isVirtual()) {
|
2009-11-28 04:47:55 +08:00
|
|
|
uint64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
|
2009-10-03 23:02:02 +08:00
|
|
|
|
2009-11-28 04:47:55 +08:00
|
|
|
// The pointer is 1 + the virtual table offset in bytes.
|
|
|
|
Values[0] = llvm::ConstantInt::get(PtrDiffTy, (Index * 8) + 1);
|
2009-10-03 23:02:02 +08:00
|
|
|
} else {
|
|
|
|
llvm::Constant *FuncPtr = CGM.GetAddrOfFunction(MD);
|
|
|
|
|
|
|
|
Values[0] = llvm::ConstantExpr::getPtrToInt(FuncPtr, PtrDiffTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The adjustment will always be 0.
|
|
|
|
Values[1] = llvm::ConstantInt::get(PtrDiffTy, 0);
|
|
|
|
|
|
|
|
return llvm::ConstantStruct::get(CGM.getLLVMContext(),
|
|
|
|
Values, 2, /*Packed=*/false);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-03 23:02:02 +08:00
|
|
|
llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) {
|
|
|
|
if (const MemberPointerType *MPT =
|
|
|
|
E->getType()->getAs<MemberPointerType>()) {
|
|
|
|
QualType T = MPT->getPointeeType();
|
2010-02-02 11:37:46 +08:00
|
|
|
DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr());
|
|
|
|
|
|
|
|
NamedDecl *ND = DRE->getDecl();
|
|
|
|
if (T->isFunctionProtoType())
|
|
|
|
return EmitMemberFunctionPointer(cast<CXXMethodDecl>(ND));
|
2009-10-03 23:02:02 +08:00
|
|
|
|
2010-02-02 11:37:46 +08:00
|
|
|
// We have a pointer to data member.
|
|
|
|
return CGM.EmitPointerToDataMember(cast<FieldDecl>(ND));
|
2009-10-03 23:02:02 +08:00
|
|
|
}
|
2010-02-02 11:37:46 +08:00
|
|
|
|
2009-10-03 23:02:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-13 15:14:16 +08:00
|
|
|
llvm::Constant *VisitBinSub(BinaryOperator *E) {
|
|
|
|
// This must be a pointer/pointer subtraction. This only happens for
|
|
|
|
// address of label.
|
|
|
|
if (!isa<AddrLabelExpr>(E->getLHS()->IgnoreParenNoopCasts(CGM.getContext())) ||
|
|
|
|
!isa<AddrLabelExpr>(E->getRHS()->IgnoreParenNoopCasts(CGM.getContext())))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
llvm::Constant *LHS = CGM.EmitConstantExpr(E->getLHS(),
|
|
|
|
E->getLHS()->getType(), CGF);
|
|
|
|
llvm::Constant *RHS = CGM.EmitConstantExpr(E->getRHS(),
|
|
|
|
E->getRHS()->getType(), CGF);
|
|
|
|
|
|
|
|
const llvm::Type *ResultType = ConvertType(E->getType());
|
|
|
|
LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType);
|
|
|
|
RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType);
|
|
|
|
|
|
|
|
// No need to divide by element size, since addr of label is always void*,
|
|
|
|
// which has size 1 in GNUish.
|
|
|
|
return llvm::ConstantExpr::getSub(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2008-08-19 07:01:59 +08:00
|
|
|
llvm::Constant *VisitCastExpr(CastExpr* E) {
|
2009-08-23 07:54:44 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
case CastExpr::CK_ToUnion: {
|
|
|
|
// GCC cast to union extension
|
|
|
|
assert(E->getType()->isUnionType() &&
|
|
|
|
"Destination type is not union type!");
|
2009-01-17 08:48:48 +08:00
|
|
|
const llvm::Type *Ty = ConvertType(E->getType());
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *SubExpr = E->getSubExpr();
|
2009-08-23 07:54:44 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *C =
|
2009-08-01 05:38:39 +08:00
|
|
|
CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF);
|
|
|
|
if (!C)
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
// Build a struct with the union sub-element as the first member,
|
|
|
|
// and padded to the appropriate size
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
Elts.push_back(C);
|
|
|
|
Types.push_back(C->getType());
|
|
|
|
unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
assert(CurSize <= TotalSize && "Union size mismatch!");
|
|
|
|
if (unsigned NumPadBytes = TotalSize - CurSize) {
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
|
2009-08-01 05:38:39 +08:00
|
|
|
if (NumPadBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumPadBytes);
|
2009-08-23 07:54:44 +08:00
|
|
|
|
2009-10-29 19:27:06 +08:00
|
|
|
Elts.push_back(llvm::UndefValue::get(Ty));
|
2009-08-01 05:38:39 +08:00
|
|
|
Types.push_back(Ty);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-23 07:54:44 +08:00
|
|
|
llvm::StructType* STy =
|
|
|
|
llvm::StructType::get(C->getType()->getContext(), Types, false);
|
2009-08-01 05:38:39 +08:00
|
|
|
return llvm::ConstantStruct::get(STy, Elts);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
2009-08-23 08:02:11 +08:00
|
|
|
case CastExpr::CK_NullToMemberPointer:
|
|
|
|
return CGM.EmitNullConstant(E->getType());
|
2009-10-03 23:13:22 +08:00
|
|
|
|
|
|
|
case CastExpr::CK_BaseToDerivedMemberPointer: {
|
|
|
|
Expr *SubExpr = E->getSubExpr();
|
|
|
|
|
|
|
|
const MemberPointerType *SrcTy =
|
|
|
|
SubExpr->getType()->getAs<MemberPointerType>();
|
|
|
|
const MemberPointerType *DestTy =
|
|
|
|
E->getType()->getAs<MemberPointerType>();
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseClass =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(SrcTy->getClass())->getDecl());
|
|
|
|
const CXXRecordDecl *DerivedClass =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(DestTy->getClass())->getDecl());
|
|
|
|
|
|
|
|
if (SrcTy->getPointeeType()->isFunctionProtoType()) {
|
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF);
|
|
|
|
if (!C)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
|
|
|
|
|
|
|
|
// Check if we need to update the adjustment.
|
2010-01-31 09:36:53 +08:00
|
|
|
if (llvm::Constant *Offset =
|
|
|
|
CGM.GetNonVirtualBaseClassOffset(DerivedClass, BaseClass)) {
|
2009-10-03 23:13:22 +08:00
|
|
|
llvm::Constant *Values[2];
|
|
|
|
|
|
|
|
Values[0] = CS->getOperand(0);
|
|
|
|
Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
|
|
|
|
return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
|
|
|
|
/*Packed=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CS;
|
|
|
|
}
|
|
|
|
}
|
2009-10-19 04:31:03 +08:00
|
|
|
|
|
|
|
case CastExpr::CK_BitCast:
|
|
|
|
// This must be a member function pointer cast.
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
|
2009-08-23 07:54:44 +08:00
|
|
|
default: {
|
|
|
|
// FIXME: This should be handled by the CK_NoOp cast kind.
|
|
|
|
// Explicit and implicit no-op casts
|
|
|
|
QualType Ty = E->getType(), SubTy = E->getSubExpr()->getType();
|
|
|
|
if (CGM.getContext().hasSameUnqualifiedType(Ty, SubTy))
|
2009-10-14 06:12:09 +08:00
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
|
|
|
|
// Handle integer->integer casts for address-of-label differences.
|
|
|
|
if (Ty->isIntegerType() && SubTy->isIntegerType() &&
|
|
|
|
CGF) {
|
|
|
|
llvm::Value *Src = Visit(E->getSubExpr());
|
|
|
|
if (Src == 0) return 0;
|
|
|
|
|
|
|
|
// Use EmitScalarConversion to perform the conversion.
|
|
|
|
return cast<llvm::Constant>(CGF->EmitScalarConversion(Src, SubTy, Ty));
|
|
|
|
}
|
|
|
|
|
2009-08-23 07:54:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-02-22 15:29:04 +08:00
|
|
|
}
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-01-29 09:15:48 +08:00
|
|
|
|
2008-04-08 12:40:51 +08:00
|
|
|
llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
return Visit(DAE->getExpr());
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
|
2008-02-22 01:45:41 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-05-30 18:24:46 +08:00
|
|
|
const llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(ConvertType(ILE->getType()));
|
2008-02-22 01:45:41 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
|
|
|
// FIXME: Check for wide strings
|
2009-02-25 06:18:39 +08:00
|
|
|
// FIXME: Check for NumInitElements exactly equal to 1??
|
2009-09-09 23:08:12 +08:00
|
|
|
if (NumInitElements > 0 &&
|
2009-02-25 06:18:39 +08:00
|
|
|
(isa<StringLiteral>(ILE->getInit(0)) ||
|
|
|
|
isa<ObjCEncodeExpr>(ILE->getInit(0))) &&
|
2008-08-04 15:31:14 +08:00
|
|
|
ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
|
2008-02-22 01:45:41 +08:00
|
|
|
return Visit(ILE->getInit(0));
|
2008-02-05 10:39:50 +08:00
|
|
|
const llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Initialising an array requires us to automatically
|
2008-02-05 10:39:50 +08:00
|
|
|
// initialise any elements that have not been initialised explicitly
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
|
|
|
|
|
|
|
// Copy initializer elements.
|
|
|
|
unsigned i = 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
bool RewriteType = false;
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
RewriteType |= (C->getType() != ElemTy);
|
2008-02-05 10:39:50 +08:00
|
|
|
Elts.push_back(C);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-05-31 03:58:50 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
// Initialize remaining array elements.
|
2009-04-14 05:47:26 +08:00
|
|
|
// FIXME: This doesn't handle member pointers correctly!
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumElements; ++i)
|
2009-08-01 04:28:54 +08:00
|
|
|
Elts.push_back(llvm::Constant::getNullValue(ElemTy));
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-31 03:58:50 +08:00
|
|
|
if (RewriteType) {
|
|
|
|
// FIXME: Try to avoid packing the array
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
for (unsigned i = 0; i < Elts.size(); ++i)
|
|
|
|
Types.push_back(Elts[i]->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
const llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
|
2009-08-06 07:18:46 +08:00
|
|
|
Types, true);
|
2009-07-28 06:29:56 +08:00
|
|
|
return llvm::ConstantStruct::get(SType, Elts);
|
2008-05-31 03:58:50 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return llvm::ConstantArray::get(AType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
|
2009-07-27 13:54:15 +08:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
|
2009-08-01 05:34:04 +08:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
2008-05-30 18:24:46 +08:00
|
|
|
}
|
|
|
|
|
2009-01-30 14:13:25 +08:00
|
|
|
llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
|
2009-04-14 05:47:26 +08:00
|
|
|
return CGM.EmitNullConstant(E->getType());
|
2009-01-30 14:13:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isScalarType()) {
|
|
|
|
// We have a scalar in braces. Just use the first element.
|
2009-04-08 12:48:15 +08:00
|
|
|
if (ILE->getNumInits() > 0) {
|
|
|
|
Expr *Init = ILE->getInit(0);
|
|
|
|
return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
return CGM.EmitNullConstant(ILE->getType());
|
2008-01-30 07:23:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isArrayType())
|
|
|
|
return EmitArrayInitialization(ILE);
|
|
|
|
|
2009-11-22 07:56:04 +08:00
|
|
|
if (ILE->getType()->isRecordType())
|
2008-05-30 18:24:46 +08:00
|
|
|
return EmitStructInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isUnionType())
|
|
|
|
return EmitUnionInitialization(ILE);
|
|
|
|
|
2010-01-03 07:43:59 +08:00
|
|
|
// If ILE was a constant vector, we would have handled it already.
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isVectorType())
|
2010-01-03 07:43:59 +08:00
|
|
|
return 0;
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
assert(0 && "Unable to handle InitListExpr");
|
2008-03-01 16:45:05 +08:00
|
|
|
// Get rid of control reaches end of void function warning.
|
|
|
|
// Not reached.
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-02-22 01:57:49 +08:00
|
|
|
|
2010-02-02 16:02:49 +08:00
|
|
|
llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
|
|
|
|
if (!E->getConstructor()->isTrivial())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Only copy and default constructors can be trivial.
|
|
|
|
|
|
|
|
QualType Ty = E->getType();
|
|
|
|
|
|
|
|
if (E->getNumArgs()) {
|
|
|
|
assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
|
|
|
|
assert(E->getConstructor()->isCopyConstructor() &&
|
|
|
|
"trivial ctor has argument but isn't a copy ctor");
|
|
|
|
|
|
|
|
Expr *Arg = E->getArg(0);
|
|
|
|
assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
|
|
|
|
"argument to copy ctor is of wrong type");
|
|
|
|
|
|
|
|
return Visit(E->getArg(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return CGM.EmitNullConstant(Ty);
|
|
|
|
}
|
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitStringLiteral(StringLiteral *E) {
|
2008-02-11 08:02:17 +08:00
|
|
|
assert(!E->getType()->isPointerType() && "Strings are always arrays");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
// This must be a string initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
2009-08-14 05:57:51 +08:00
|
|
|
return llvm::ConstantArray::get(VMContext,
|
|
|
|
CGM.GetStringForStringLiteral(E), false);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
|
|
|
|
// This must be an @encode initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
|
|
|
std::string Str;
|
|
|
|
CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
|
|
|
|
const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
// Resize the string to the right size, adding zeros at the end, or
|
|
|
|
// truncating as needed.
|
|
|
|
Str.resize(CAT->getSize().getZExtValue(), '\0');
|
2009-08-14 05:57:51 +08:00
|
|
|
return llvm::ConstantArray::get(VMContext, Str, false);
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-29 19:22:45 +08:00
|
|
|
llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
2009-02-20 06:01:56 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
// Utility methods
|
|
|
|
const llvm::Type *ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
|
|
|
}
|
2008-01-26 12:30:23 +08:00
|
|
|
|
2008-11-16 14:23:45 +08:00
|
|
|
public:
|
2008-01-26 12:30:23 +08:00
|
|
|
llvm::Constant *EmitLValue(Expr *E) {
|
2008-01-26 10:08:50 +08:00
|
|
|
switch (E->getStmtClass()) {
|
2008-02-11 08:23:10 +08:00
|
|
|
default: break;
|
2008-01-26 10:08:50 +08:00
|
|
|
case Expr::CompoundLiteralExprClass: {
|
|
|
|
// Note that due to the nature of compound literals, this is guaranteed
|
|
|
|
// to be the only use of the variable, so we just generate it here.
|
2008-01-26 12:30:23 +08:00
|
|
|
CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
|
|
|
|
llvm::Constant* C = Visit(CLE->getInitializer());
|
2009-02-18 02:43:32 +08:00
|
|
|
// FIXME: "Leaked" on failure.
|
|
|
|
if (C)
|
2009-07-09 03:05:04 +08:00
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
|
2009-08-27 04:01:39 +08:00
|
|
|
E->getType().isConstant(CGM.getContext()),
|
2009-02-18 02:43:32 +08:00
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2009-08-27 04:01:39 +08:00
|
|
|
C, ".compoundliteral", 0, false,
|
|
|
|
E->getType().getAddressSpace());
|
2008-01-26 10:08:50 +08:00
|
|
|
return C;
|
2008-01-26 12:30:23 +08:00
|
|
|
}
|
2009-10-24 02:54:35 +08:00
|
|
|
case Expr::DeclRefExprClass: {
|
2008-10-22 00:13:35 +08:00
|
|
|
NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
|
2008-01-26 10:08:50 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
|
2009-09-11 07:43:36 +08:00
|
|
|
return CGM.GetAddrOfFunction(FD);
|
2008-04-16 06:42:06 +08:00
|
|
|
if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
|
2009-02-25 02:41:57 +08:00
|
|
|
// We can never refer to a variable with local storage.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!VD->hasLocalStorage()) {
|
2009-02-25 02:41:57 +08:00
|
|
|
if (VD->isFileVarDecl() || VD->hasExternalStorage())
|
|
|
|
return CGM.GetAddrOfGlobalVar(VD);
|
|
|
|
else if (VD->isBlockVarDecl()) {
|
|
|
|
assert(CGF && "Can't access static local vars without CGF");
|
|
|
|
return CGF->GetAddrOfStaticLocalVar(VD);
|
|
|
|
}
|
2008-04-16 06:42:06 +08:00
|
|
|
}
|
2008-02-27 05:41:45 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
break;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
|
2009-02-25 06:18:39 +08:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
|
2008-11-16 14:23:45 +08:00
|
|
|
case Expr::ObjCStringLiteralClass: {
|
|
|
|
ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
|
2010-01-23 10:40:42 +08:00
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.getObjCRuntime().GenerateConstantString(SL->getString());
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-12-12 13:18:02 +08:00
|
|
|
case Expr::PredefinedExprClass: {
|
2009-11-14 16:37:13 +08:00
|
|
|
unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
|
|
|
|
if (CGF) {
|
|
|
|
LValue Res = CGF->EmitPredefinedFunctionName(Type);
|
|
|
|
return cast<llvm::Constant>(Res.getAddress());
|
|
|
|
} else if (Type == PredefinedExpr::PrettyFunction) {
|
|
|
|
return CGM.GetAddrOfConstantCString("top level", ".tmp");
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-14 16:37:13 +08:00
|
|
|
return CGM.GetAddrOfConstantCString("", ".tmp");
|
2008-12-12 13:18:02 +08:00
|
|
|
}
|
2009-01-25 09:21:06 +08:00
|
|
|
case Expr::AddrLabelExprClass: {
|
|
|
|
assert(CGF && "Invalid address of label expression outside function.");
|
2009-10-29 07:59:40 +08:00
|
|
|
llvm::Constant *Ptr =
|
|
|
|
CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
|
|
|
|
return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType()));
|
2009-01-25 09:21:06 +08:00
|
|
|
}
|
2009-01-25 09:54:01 +08:00
|
|
|
case Expr::CallExprClass: {
|
|
|
|
CallExpr* CE = cast<CallExpr>(E);
|
2010-01-23 10:40:42 +08:00
|
|
|
unsigned builtin = CE->isBuiltinCall(CGM.getContext());
|
|
|
|
if (builtin !=
|
|
|
|
Builtin::BI__builtin___CFStringMakeConstantString &&
|
|
|
|
builtin !=
|
|
|
|
Builtin::BI__builtin___NSStringMakeConstantString)
|
2009-01-25 09:54:01 +08:00
|
|
|
break;
|
|
|
|
const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
|
|
|
|
const StringLiteral *Literal = cast<StringLiteral>(Arg);
|
2010-01-23 10:40:42 +08:00
|
|
|
if (builtin ==
|
|
|
|
Builtin::BI__builtin___NSStringMakeConstantString) {
|
|
|
|
return CGM.getObjCRuntime().GenerateConstantString(Literal);
|
|
|
|
}
|
2009-04-01 00:53:37 +08:00
|
|
|
// FIXME: need to deal with UCN conversion issues.
|
2009-04-01 21:55:36 +08:00
|
|
|
return CGM.GetAddrOfConstantCFString(Literal);
|
2009-01-25 09:54:01 +08:00
|
|
|
}
|
2009-02-19 09:01:04 +08:00
|
|
|
case Expr::BlockExprClass: {
|
2009-03-01 09:09:12 +08:00
|
|
|
std::string FunctionName;
|
|
|
|
if (CGF)
|
|
|
|
FunctionName = CGF->CurFn->getName();
|
|
|
|
else
|
|
|
|
FunctionName = "global";
|
|
|
|
|
|
|
|
return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
|
2009-02-19 09:01:04 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
}
|
2009-02-18 02:43:32 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2008-02-27 05:41:45 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
|
2009-04-08 12:48:15 +08:00
|
|
|
QualType DestType,
|
2008-07-27 06:37:01 +08:00
|
|
|
CodeGenFunction *CGF) {
|
2008-12-01 10:42:14 +08:00
|
|
|
Expr::EvalResult Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 09:08:03 +08:00
|
|
|
bool Success = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-27 14:04:58 +08:00
|
|
|
if (DestType->isReferenceType())
|
|
|
|
Success = E->EvaluateAsLValue(Result, Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2009-04-11 09:08:03 +08:00
|
|
|
Success = E->Evaluate(Result, Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-14 16:51:33 +08:00
|
|
|
if (Success && !Result.HasSideEffects) {
|
2008-12-01 10:42:14 +08:00
|
|
|
switch (Result.Val.getKind()) {
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::Uninitialized:
|
2009-02-18 02:43:32 +08:00
|
|
|
assert(0 && "Constant expressions should be initialized.");
|
|
|
|
return 0;
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::LValue: {
|
2009-04-11 09:08:03 +08:00
|
|
|
const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *Offset =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
|
2010-01-15 20:37:54 +08:00
|
|
|
Result.Val.getLValueOffset().getQuantity());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
llvm::Constant *C;
|
2008-12-01 10:42:14 +08:00
|
|
|
if (const Expr *LVBase = Result.Val.getLValueBase()) {
|
2009-02-20 05:44:24 +08:00
|
|
|
C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
|
|
|
|
|
|
|
|
// Apply offset if necessary.
|
|
|
|
if (!Offset->isNullValue()) {
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
|
2009-07-30 02:54:39 +08:00
|
|
|
llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type);
|
|
|
|
Casted = llvm::ConstantExpr::getGetElementPtr(Casted, &Offset, 1);
|
|
|
|
C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
|
2009-02-20 05:44:24 +08:00
|
|
|
}
|
2008-11-16 14:23:45 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getPtrToInt(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
} else {
|
|
|
|
C = Offset;
|
|
|
|
|
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getIntToPtr(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
// If the types don't match this should only be a truncate.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (C->getType() != DestTy)
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getTrunc(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
return C;
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2008-11-17 11:57:28 +08:00
|
|
|
case APValue::Int: {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *C = llvm::ConstantInt::get(VMContext,
|
2009-07-25 07:12:58 +08:00
|
|
|
Result.Val.getInt());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
if (C->getType() == llvm::Type::getInt1Ty(VMContext)) {
|
2008-11-16 04:45:50 +08:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-30 02:54:39 +08:00
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-11-17 11:57:28 +08:00
|
|
|
}
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::ComplexInt: {
|
|
|
|
llvm::Constant *Complex[2];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 07:12:58 +08:00
|
|
|
Complex[0] = llvm::ConstantInt::get(VMContext,
|
|
|
|
Result.Val.getComplexIntReal());
|
2009-09-09 23:08:12 +08:00
|
|
|
Complex[1] = llvm::ConstantInt::get(VMContext,
|
2009-07-25 07:12:58 +08:00
|
|
|
Result.Val.getComplexIntImag());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-20 04:00:52 +08:00
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
return llvm::ConstantStruct::get(VMContext, Complex, 2, false);
|
2009-01-29 06:24:07 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::Float:
|
2009-07-28 05:00:51 +08:00
|
|
|
return llvm::ConstantFP::get(VMContext, Result.Val.getFloat());
|
2008-11-17 09:58:55 +08:00
|
|
|
case APValue::ComplexFloat: {
|
|
|
|
llvm::Constant *Complex[2];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Complex[0] = llvm::ConstantFP::get(VMContext,
|
2009-07-28 05:00:51 +08:00
|
|
|
Result.Val.getComplexFloatReal());
|
|
|
|
Complex[1] = llvm::ConstantFP::get(VMContext,
|
|
|
|
Result.Val.getComplexFloatImag());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-20 04:00:52 +08:00
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
return llvm::ConstantStruct::get(VMContext, Complex, 2, false);
|
2008-11-17 09:58:55 +08:00
|
|
|
}
|
2009-01-18 09:01:34 +08:00
|
|
|
case APValue::Vector: {
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> Inits;
|
|
|
|
unsigned NumElts = Result.Val.getVectorLength();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 09:01:34 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
APValue &Elt = Result.Val.getVectorElt(i);
|
|
|
|
if (Elt.isInt())
|
2009-07-25 07:12:58 +08:00
|
|
|
Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
|
2009-01-18 09:01:34 +08:00
|
|
|
else
|
2009-07-28 05:00:51 +08:00
|
|
|
Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2009-07-29 05:22:35 +08:00
|
|
|
return llvm::ConstantVector::get(&Inits[0], Inits.size());
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
}
|
2008-06-01 23:31:44 +08:00
|
|
|
|
|
|
|
llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
|
2009-08-14 05:57:51 +08:00
|
|
|
if (C && C->getType() == llvm::Type::getInt1Ty(VMContext)) {
|
2008-06-01 23:31:44 +08:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-30 02:54:39 +08:00
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
2008-06-01 23:31:44 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
|
2010-02-02 13:17:25 +08:00
|
|
|
static bool containsPointerToDataMember(CodeGenTypes &Types, QualType T) {
|
|
|
|
// No need to check for member pointers when not compiling C++.
|
|
|
|
if (!Types.getContext().getLangOptions().CPlusPlus)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
T = Types.getContext().getBaseElementType(T);
|
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
|
|
|
|
// FIXME: It would be better if there was a way to explicitly compute the
|
|
|
|
// record layout instead of converting to a type.
|
|
|
|
Types.ConvertTagDeclType(RD);
|
|
|
|
|
|
|
|
const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
|
|
|
|
return Layout.containsPointerToDataMember();
|
|
|
|
}
|
|
|
|
|
2009-09-29 10:09:01 +08:00
|
|
|
if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
|
|
|
|
return !MPT->getPointeeType()->isFunctionType();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-14 05:47:26 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
|
2010-02-02 13:17:25 +08:00
|
|
|
if (!containsPointerToDataMember(getTypes(), T))
|
2009-08-25 01:16:23 +08:00
|
|
|
return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
|
2010-02-02 13:17:25 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
QualType ElementTy = CAT->getElementType();
|
|
|
|
|
2010-02-02 13:17:25 +08:00
|
|
|
llvm::Constant *Element = EmitNullConstant(ElementTy);
|
|
|
|
unsigned NumElements = CAT->getSize().getZExtValue();
|
|
|
|
std::vector<llvm::Constant *> Array(NumElements);
|
|
|
|
for (unsigned i = 0; i != NumElements; ++i)
|
|
|
|
Array[i] = Element;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-02 13:17:25 +08:00
|
|
|
const llvm::ArrayType *ATy =
|
|
|
|
cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
|
|
|
|
return llvm::ConstantArray::get(ATy, Array);
|
2009-08-10 02:26:27 +08:00
|
|
|
}
|
2009-08-23 09:25:01 +08:00
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
2010-02-02 13:17:25 +08:00
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
assert(!RD->getNumBases() &&
|
|
|
|
"FIXME: Handle zero-initializing structs with bases and "
|
|
|
|
"pointers to data members.");
|
|
|
|
const llvm::StructType *STy =
|
|
|
|
cast<llvm::StructType>(getTypes().ConvertTypeForMem(T));
|
|
|
|
unsigned NumElements = STy->getNumElements();
|
|
|
|
std::vector<llvm::Constant *> Elements(NumElements);
|
|
|
|
|
|
|
|
for (RecordDecl::field_iterator I = RD->field_begin(),
|
|
|
|
E = RD->field_end(); I != E; ++I) {
|
|
|
|
const FieldDecl *FD = *I;
|
|
|
|
|
|
|
|
unsigned FieldNo = getTypes().getLLVMFieldNo(FD);
|
|
|
|
Elements[FieldNo] = EmitNullConstant(FD->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through all other fields and zero them out.
|
|
|
|
for (unsigned i = 0; i != NumElements; ++i) {
|
|
|
|
if (!Elements[i])
|
|
|
|
Elements[i] = llvm::Constant::getNullValue(STy->getElementType(i));
|
2009-08-23 09:25:01 +08:00
|
|
|
}
|
2010-02-02 13:17:25 +08:00
|
|
|
|
|
|
|
return llvm::ConstantStruct::get(STy, Elements);
|
2009-08-23 09:25:01 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-02 13:17:25 +08:00
|
|
|
assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
|
|
|
|
"Should only see pointers to data members here!");
|
|
|
|
|
|
|
|
// Itanium C++ ABI 2.3:
|
|
|
|
// A NULL pointer is represented as -1.
|
|
|
|
return llvm::ConstantInt::get(getTypes().ConvertTypeForMem(T), -1,
|
|
|
|
/*isSigned=*/true);
|
2009-04-14 05:47:26 +08:00
|
|
|
}
|
2010-02-02 11:37:46 +08:00
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::EmitPointerToDataMember(const FieldDecl *FD) {
|
|
|
|
|
|
|
|
// Itanium C++ ABI 2.3:
|
|
|
|
// A pointer to data member is an offset from the base address of the class
|
|
|
|
// object containing it, represented as a ptrdiff_t
|
|
|
|
|
|
|
|
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(FD->getParent());
|
|
|
|
QualType ClassType =
|
|
|
|
getContext().getTypeDeclType(const_cast<CXXRecordDecl *>(ClassDecl));
|
|
|
|
|
|
|
|
const llvm::StructType *ClassLTy =
|
|
|
|
cast<llvm::StructType>(getTypes().ConvertType(ClassType));
|
|
|
|
|
|
|
|
unsigned FieldNo = getTypes().getLLVMFieldNo(FD);
|
|
|
|
uint64_t Offset =
|
|
|
|
getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo);
|
|
|
|
|
|
|
|
const llvm::Type *PtrDiffTy =
|
|
|
|
getTypes().ConvertType(getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
return llvm::ConstantInt::get(PtrDiffTy, Offset);
|
|
|
|
}
|