mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-01-11 18:33:14 +01:00
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.
This commit is contained in:
21
gen/aa.cpp
21
gen/aa.cpp
@@ -218,3 +218,24 @@ void DtoAARemove(Loc& loc, DValue* aa, DValue* key)
|
||||
// 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;
|
||||
}
|
||||
1
gen/aa.h
1
gen/aa.h
@@ -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
|
||||
|
||||
@@ -220,6 +220,8 @@ static void LLVM_D_BuildRuntimeModule()
|
||||
= 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 @@ static void LLVM_D_BuildRuntimeModule()
|
||||
->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);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -1448,7 +1448,7 @@ DValue* EqualExp::toElem(IRState* p)
|
||||
|
||||
// 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 @@ DValue* EqualExp::toElem(IRState* p)
|
||||
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");
|
||||
|
||||
@@ -705,6 +705,11 @@ body
|
||||
}
|
||||
|
||||
|
||||
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.
|
||||
|
||||
@@ -47,6 +47,8 @@ private
|
||||
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 @@ class TypeInfo_AssociativeArray : TypeInfo
|
||||
}
|
||||
|
||||
// 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()
|
||||
{
|
||||
|
||||
48
tests/mini/aaequality.d
Normal file
48
tests/mini/aaequality.d
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user