changeset 28:1c80c18f3c82 trunk

[svn r32] * Fixed problems with arrays members of aggregates
author lindquist
date Thu, 04 Oct 2007 12:49:37 +0200
parents 92408a3a2bac
children 253a5fc4033a
files gen/toir.c gen/toobj.c test/bug3.d
diffstat 3 files changed, 45 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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<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;
 }
--- 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<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 @@
                 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);
     }
--- /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);
+}