# HG changeset patch # User lindquist # Date 1191494977 -7200 # Node ID 1c80c18f3c828c568b3c0094f945ceee3509abd1 # Parent 92408a3a2bac9ece2a515a3a8f719d4ee330de3f [svn r32] * Fixed problems with arrays members of aggregates diff -r 92408a3a2bac -r 1c80c18f3c82 gen/toir.c --- a/gen/toir.c Thu Oct 04 11:39:53 2007 +0200 +++ b/gen/toir.c Thu Oct 04 12:49:37 2007 +0200 @@ -228,12 +228,16 @@ LOG_SCOPE; elem* e = new elem; const llvm::Type* t = LLVM_DtoType(type); - if (llvm::isa(t)) - t = llvm::PointerType::get(t); - Logger::cout() << *t << '\n'; - e->val = llvm::Constant::getNullValue(t); + + if (type->ty == Tarray) { + assert(llvm::isa(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; } diff -r 92408a3a2bac -r 1c80c18f3c82 gen/toobj.c --- a/gen/toobj.c Thu Oct 04 11:39:53 2007 +0200 +++ b/gen/toobj.c Thu Oct 04 12:49:37 2007 +0200 @@ -574,17 +574,25 @@ 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(_type)) + if (type->ty == Tsarray) { const llvm::ArrayType* arrty = llvm::cast(_type); uint64_t n = arrty->getNumElements(); std::vector vals(n,_init); _init = llvm::ConstantArray::get(arrty, vals); } - else if (llvm::isa(_type)) { + else if (type->ty == Tarray) + { + assert(llvm::isa(_type)); + _init = llvm::ConstantAggregateZero::get(_type); + } + else if (type->ty == Tstruct) + { const llvm::StructType* structty = llvm::cast(_type); TypeStruct* ts = (TypeStruct*)type; assert(ts); @@ -592,8 +600,14 @@ 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); } diff -r 92408a3a2bac -r 1c80c18f3c82 test/bug3.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/bug3.d Thu Oct 04 12:49:37 2007 +0200 @@ -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); +}