changeset 1512:09734fb929c0

Make == for associative arrays test for equality, not identity. _aaEq was added to runtime/internal/aaA.d which forwards to TypeInfo_AssociativeArray.equals in genobj.d. On the codegen side, DtoAAEquals was added to gen/aa.cpp and is called from EqualExp::toElem in gen/toir.cpp. I assume that the frontend will produce an error if == is used on associative arrays of different type. This fixes DMD bug 1429.
author Christian Kamm <kamm incasoftware de>
date Sun, 21 Jun 2009 19:05:24 +0200
parents 5b66008246bb
children 8a5570ddad25
files gen/aa.cpp gen/aa.h gen/runtime.cpp gen/toir.cpp runtime/internal/aaA.d runtime/internal/genobj.d tests/mini/aaequality.d
diffstat 7 files changed, 120 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/gen/aa.cpp	Sun Jun 21 00:12:29 2009 +0200
+++ b/gen/aa.cpp	Sun Jun 21 19:05:24 2009 +0200
@@ -218,3 +218,24 @@
     // call runtime
     gIR->CreateCallOrInvoke(func, args.begin(), args.end());
 }
+
+/////////////////////////////////////////////////////////////////////////////////////
+
+LLValue* DtoAAEquals(Loc& loc, TOK op, DValue* l, DValue* r)
+{
+    Type* t = l->getType()->toBasetype();
+    assert(t == r->getType()->toBasetype() && "aa equality is only defined for aas of same type");
+
+    llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_aaEq");
+    const llvm::FunctionType* funcTy = func->getFunctionType();
+    
+    LLValue* aaval = DtoBitCast(l->getRVal(), funcTy->getParamType(0));
+    LLValue* abval = DtoBitCast(r->getRVal(), funcTy->getParamType(1));
+    LLValue* aaTypeInfo = DtoTypeInfoOf(t);
+    LLValue* res = gIR->CreateCallOrInvoke3(func, aaval, abval, aaTypeInfo, "aaEqRes").getInstruction();
+    
+    res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp");
+    if (op == TOKnotequal)
+        res = gIR->ir->CreateNot(res, "tmp");
+    return res;
+}
\ No newline at end of file
--- a/gen/aa.h	Sun Jun 21 00:12:29 2009 +0200
+++ b/gen/aa.h	Sun Jun 21 19:05:24 2009 +0200
@@ -4,5 +4,6 @@
 DValue* DtoAAIndex(Loc& loc, Type* type, DValue* aa, DValue* key, bool lvalue);
 DValue* DtoAAIn(Loc& loc, Type* type, DValue* aa, DValue* key);
 void DtoAARemove(Loc& loc, DValue* aa, DValue* key);
+LLValue* DtoAAEquals(Loc& loc, TOK op, DValue* l, DValue* r);
 
 #endif // LDC_GEN_AA_H
--- a/gen/runtime.cpp	Sun Jun 21 00:12:29 2009 +0200
+++ b/gen/runtime.cpp	Sun Jun 21 19:05:24 2009 +0200
@@ -220,6 +220,8 @@
             = Attr_1_NoCapture.addAttr(0, NoAlias),
         Attr_NoAlias_3_NoCapture
             = Attr_NoAlias.addAttr(3, NoCapture),
+        Attr_1_2_NoCapture
+            = Attr_1_NoCapture.addAttr(2, NoCapture),
         Attr_1_3_NoCapture
             = Attr_1_NoCapture.addAttr(3, NoCapture),
         Attr_1_4_NoCapture
@@ -738,6 +740,18 @@
             ->setAttributes(Attr_1_NoCapture);
     }
 
+    // int _aaEq(AA aa, AA ab, TypeInfo_AssociativeArray ti)
+    {
+        std::string fname("_aaEq");
+        std::vector<const LLType*> types;
+        types.push_back(aaTy);
+        types.push_back(aaTy);
+        types.push_back(typeInfoTy);
+        const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false);
+        llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)
+            ->setAttributes(Attr_1_2_NoCapture);
+    }
+
     /////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////
--- a/gen/toir.cpp	Sun Jun 21 00:12:29 2009 +0200
+++ b/gen/toir.cpp	Sun Jun 21 19:05:24 2009 +0200
@@ -1448,7 +1448,7 @@
 
     // the Tclass catches interface comparisons, regular
     // class equality should be rewritten as a.opEquals(b) by this time
-    if (t->isintegral() || t->ty == Tpointer || t->ty == Tclass || t->ty == Taarray)
+    if (t->isintegral() || t->ty == Tpointer || t->ty == Tclass)
     {
         Logger::println("integral or pointer or interface");
         llvm::ICmpInst::Predicate cmpop;
@@ -1482,6 +1482,11 @@
         Logger::println("static or dynamic array");
         eval = DtoArrayEquals(loc,op,l,r);
     }
+    else if (t->ty == Taarray)
+    {
+        Logger::println("associative array");
+        eval = DtoAAEquals(loc,op,l,r);
+    }
     else if (t->ty == Tdelegate)
     {
         Logger::println("delegate");
--- a/runtime/internal/aaA.d	Sun Jun 21 00:12:29 2009 +0200
+++ b/runtime/internal/aaA.d	Sun Jun 21 19:05:24 2009 +0200
@@ -705,6 +705,11 @@
 }
 
 
+int _aaEq(AA aa, AA ab, TypeInfo_AssociativeArray ti)
+{
+    return ti.equals(&aa, &ab);
+}
+
 /***********************************
  * Construct an associative array of type ti from
  * length pairs of key/value pairs.
--- a/runtime/internal/genobj.d	Sun Jun 21 00:12:29 2009 +0200
+++ b/runtime/internal/genobj.d	Sun Jun 21 19:05:24 2009 +0200
@@ -47,6 +47,8 @@
     import util.string;
     import tango.stdc.stdio;  // : printf, snprintf;
     import tango.core.Version;
+    
+    import aaA;
 
     extern (C) void onOutOfMemoryError();
     extern (C) Object _d_allocclass(ClassInfo ci);
@@ -555,6 +557,29 @@
     }
 
     // BUG: need to add the rest of the functions
+    
+    int equals(void *p1, void *p2)
+    {
+        AA aa = *cast(AA*)p1;
+        AA ab = *cast(AA*)p2;
+        
+        if (_aaLen(aa) != _aaLen(ab))
+            return 0;
+        
+        int equal = 1;
+        int eq_x(void* k, void* va)
+        {
+            void* vb = _aaIn(ab, key, k);
+            if (!vb || !value.equals(va, vb))
+            {
+                equal = 0;
+                return 1; // break
+            }
+            return 0;
+        }
+        _aaApply2(aa, key.tsize(), &eq_x);
+        return equal;
+    }
 
     size_t tsize()
     {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/aaequality.d	Sun Jun 21 19:05:24 2009 +0200
@@ -0,0 +1,48 @@
+void test(K,V)(K k1, V v1, K k2, V v2, K k3, V v3)
+{
+    V[K] a, b;
+    a[k1] = v1;
+    a[k2] = v2;
+    assert(a != b);
+    assert(b != a);
+    assert(a == a);
+    assert(b == b);
+    
+    b[k1] = v1;
+    assert(a != b);
+    assert(b != a);
+
+    b[k2] = v2;
+    assert(a == b);
+    assert(b == a);
+    
+    b[k1] = v2;
+    assert(a != b);
+    assert(b != a);
+    
+    b[k1] = v1;
+    b[k2] = v3;
+    assert(a != b);
+    assert(b != a);
+    
+    b[k2] = v2;
+    b[k3] = v3;
+    assert(a != b);
+    assert(b != a);
+}
+
+void main()
+{
+    test!(int,int)(1, 2, 3, 4, 5, 6);
+    test!(char[],int)("abc", 2, "def", 4, "geh", 6);
+    test!(int,char[])(1, "abc", 2, "def", 3, "geh");
+    test!(char[],char[])("123", "abc", "456", "def", "789", "geh");
+    
+    Object a = new Object, b = new Object, c = new Object;
+    test!(Object, Object)(a, a, b, b, c, c);
+    
+    int[int] a2 = [1:2, 2:3, 3:4];
+    int[int] b2 = [1:2, 2:5, 3:4];
+    int[int] c2 = [1:2, 2:3];
+    test!(int,int[int])(1,a2, 2,b2, 3,c2);
+}
\ No newline at end of file