[svn r32] * Fixed problems with arrays members of aggregates

This commit is contained in:
Tomas Lindquist Olsen
2007-10-04 12:49:37 +02:00
parent 02cf2ac384
commit 67f3d8ae60
3 changed files with 45 additions and 11 deletions

View File

@@ -228,12 +228,16 @@ elem* NullExp::toElem(IRState* p)
LOG_SCOPE;
elem* e = new elem;
const llvm::Type* t = LLVM_DtoType(type);
if (llvm::isa<llvm::StructType>(t))
t = llvm::PointerType::get(t);
Logger::cout() << *t << '\n';
e->val = llvm::Constant::getNullValue(t);
if (type->ty == Tarray) {
assert(llvm::isa<llvm::StructType>(t));
e->val = llvm::ConstantAggregateZero::get(t);
}
else
e->val = llvm::Constant::getNullValue(t);
assert(e->val);
Logger::cout() << *e->val << '\n';
Logger::cout() << "null value is now " << *e->val << '\n';
e->type = elem::NUL;
return e;
}

View File

@@ -574,17 +574,25 @@ void VarDeclaration::toObjFile()
const llvm::Type* _type = LLVM_DtoType(type);
gIR->topstruct().fields.push_back(_type);
llvm::Constant* _init = LLVM_DtoInitializer(type, init);
if (_type != _init->getType())
llvm::Constant*_init = LLVM_DtoInitializer(type, init);
assert(_init);
Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n';
if (!_init || _type != _init->getType())
{
if (llvm::isa<llvm::ArrayType>(_type))
if (type->ty == Tsarray)
{
const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type);
uint64_t n = arrty->getNumElements();
std::vector<llvm::Constant*> vals(n,_init);
_init = llvm::ConstantArray::get(arrty, vals);
}
else if (llvm::isa<llvm::StructType>(_type)) {
else if (type->ty == Tarray)
{
assert(llvm::isa<llvm::StructType>(_type));
_init = llvm::ConstantAggregateZero::get(_type);
}
else if (type->ty == Tstruct)
{
const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type);
TypeStruct* ts = (TypeStruct*)type;
assert(ts);
@@ -592,8 +600,14 @@ void VarDeclaration::toObjFile()
assert(ts->sym->llvmInitZ);
_init = ts->sym->llvmInitZ;
}
else
assert(0);
else if (type->ty == Tclass)
{
_init = llvm::Constant::getNullValue(_type);
}
else {
Logger::println("failed for type %s", type->toChars());
assert(0);
}
}
gIR->topstruct().inits.push_back(_init);
}

16
test/bug3.d Normal file
View File

@@ -0,0 +1,16 @@
module bug3;
struct S
{
int[] arr;
char[5] ch;
}
void main()
{
S s;
s.arr = new int[5];
s.arr[1] = 32;
assert(s.arr[0] == 0);
assert(s.arr[1] == 32);
}