changeset 424:c8d98ccad0cc

Error if static array is cast to an array such that oldarraysize % newelemsize != 0.
author Christian Kamm <kamm incasoftware de>
date Tue, 29 Jul 2008 12:32:01 +0200
parents 3424f0fab7a9
children 944d43f3779f
files gen/arrays.cpp gen/arrays.h gen/llvmhelpers.cpp gen/toir.cpp
diffstat 4 files changed, 23 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/gen/arrays.cpp	Tue Jul 29 10:55:58 2008 +0200
+++ b/gen/arrays.cpp	Tue Jul 29 12:32:01 2008 +0200
@@ -672,7 +672,7 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////
 // helper for eq and cmp
-static LLValue* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r, bool useti)
+static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue* r, bool useti)
 {
     Logger::println("comparing arrays");
     LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, func);
@@ -689,9 +689,9 @@
     if ((l_ty->ty == Tsarray) || (r_ty->ty == Tsarray)) {
         Type* a_ty = l_ty->next->arrayOf();
         if (l_ty->ty == Tsarray)
-            l = DtoCastArray(l, a_ty);
+            l = DtoCastArray(loc, l, a_ty);
         if (r_ty->ty == Tsarray)
-            r = DtoCastArray(r, a_ty);
+            r = DtoCastArray(loc, r, a_ty);
     }
 
     Logger::println("giving storage");
@@ -758,9 +758,9 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
-LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r)
+LLValue* DtoArrayEquals(Loc& loc, TOK op, DValue* l, DValue* r)
 {
-    LLValue* res = DtoArrayEqCmp_impl("_adEq", l, r, true);
+    LLValue* res = DtoArrayEqCmp_impl(loc, "_adEq", l, r, true);
     res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp");
     if (op == TOKnotequal)
         res = gIR->ir->CreateNot(res, "tmp");
@@ -769,7 +769,7 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
-LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r)
+LLValue* DtoArrayCompare(Loc& loc, TOK op, DValue* l, DValue* r)
 {
     LLValue* res = 0;
 
@@ -817,9 +817,9 @@
     {
         Type* t = DtoDType(DtoDType(l->getType())->next);
         if (t->ty == Tchar)
-            res = DtoArrayEqCmp_impl("_adCmpChar", l, r, false);
+            res = DtoArrayEqCmp_impl(loc, "_adCmpChar", l, r, false);
         else
-            res = DtoArrayEqCmp_impl("_adCmp", l, r, true);
+            res = DtoArrayEqCmp_impl(loc, "_adCmp", l, r, true);
         res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp");
     }
 
@@ -944,7 +944,7 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
-DValue* DtoCastArray(DValue* u, Type* to)
+DValue* DtoCastArray(Loc& loc, DValue* u, Type* to)
 {
     Logger::println("DtoCastArray");
     LOG_SCOPE;
@@ -968,6 +968,7 @@
     }
     else if (totype->ty == Tarray) {
         Logger::cout() << "to array" << '\n';
+
         const LLType* ptrty = DtoArrayType(totype)->getContainedType(1);
         const LLType* ety = DtoTypeNotVoid(fromtype->next);
 
@@ -986,6 +987,13 @@
                 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
                 assert(isaPointer(uval->getType()));
                 const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0));
+
+                if(arrty->getNumElements() % totype->next->size() != 0)
+                {
+                    error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars());
+                    fatal();
+                }
+
                 rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false);
                 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
                 rval = DtoBitCast(uval, ptrty);
--- a/gen/arrays.h	Tue Jul 29 10:55:58 2008 +0200
+++ b/gen/arrays.h	Tue Jul 29 12:32:01 2008 +0200
@@ -30,8 +30,8 @@
 
 void DtoStaticArrayCopy(LLValue* dst, LLValue* src);
 
-LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r);
-LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r);
+LLValue* DtoArrayEquals(Loc& loc, TOK op, DValue* l, DValue* r);
+LLValue* DtoArrayCompare(Loc& loc, TOK op, DValue* l, DValue* r);
 
 LLValue* DtoDynArrayIs(TOK op, DValue* l, DValue* r);
 
@@ -40,6 +40,6 @@
 LLValue* DtoArrayLen(DValue* v);
 LLValue* DtoArrayPtr(DValue* v);
 
-DValue* DtoCastArray(DValue* val, Type* to);
+DValue* DtoCastArray(Loc& loc, DValue* val, Type* to);
 
 #endif // LLVMC_GEN_ARRAYS_H
--- a/gen/llvmhelpers.cpp	Tue Jul 29 10:55:58 2008 +0200
+++ b/gen/llvmhelpers.cpp	Tue Jul 29 12:32:01 2008 +0200
@@ -846,7 +846,7 @@
         return DtoCastClass(val, to);
     }
     else if (fromtype->ty == Tarray || fromtype->ty == Tsarray) {
-        return DtoCastArray(val, to);
+        return DtoCastArray(loc, val, to);
     }
     else if (fromtype->ty == Tpointer || fromtype->ty == Tfunction) {
         return DtoCastPtr(loc, val, to);
--- a/gen/toir.cpp	Tue Jul 29 10:55:58 2008 +0200
+++ b/gen/toir.cpp	Tue Jul 29 12:32:01 2008 +0200
@@ -1309,7 +1309,7 @@
     else if (t->ty == Tsarray || t->ty == Tarray)
     {
         Logger::println("static or dynamic array");
-        eval = DtoArrayCompare(op,l,r);
+        eval = DtoArrayCompare(loc,op,l,r);
     }
     else
     {
@@ -1382,7 +1382,7 @@
     else if (t->ty == Tsarray || t->ty == Tarray)
     {
         Logger::println("static or dynamic array");
-        eval = DtoArrayEquals(op,l,r);
+        eval = DtoArrayEquals(loc,op,l,r);
     }
     else if (t->ty == Tdelegate)
     {