Fix hashing of llvm type

This commit is contained in:
Siu Kwan Lam 2013-05-16 11:34:27 -05:00
commit 331f71f26f
2 changed files with 29 additions and 1 deletions

View file

@ -800,6 +800,9 @@ class Type(llvm.Wrapper):
def __str__(self):
return str(self._ptr)
def __hash__(self):
return hash(self._ptr)
def __eq__(self, rhs):
return self._ptr is rhs._ptr
@ -901,7 +904,6 @@ class StructType(Type):
def is_layout_identical(self, other):
return self._ptr.isLayoutIdentical(other._ptr)
class ArrayType(Type):
"""Represents an array type."""
_type_ = api.llvm.ArrayType

View file

@ -1258,6 +1258,32 @@ class TestStruct(TestCase):
tests.append(TestStruct)
# ---------------------------------------------------------------------------
class TestTypeHash(TestCase):
def test_scalar_type(self):
i32a = Type.int(32)
i32b = Type.int(32)
i64a = Type.int(64)
i64b = Type.int(64)
ts = set([i32a, i32b, i64a, i64b])
self.assertTrue(len(ts))
self.assertTrue(i32a in ts)
self.assertTrue(i64b in ts)
def test_struct_type(self):
ta = Type.struct([Type.int(32), Type.float()])
tb = Type.struct([Type.int(32), Type.float()])
tc = Type.struct([Type.int(32), Type.int(32), Type.float()])
ts = set([ta, tb, tc])
self.assertTrue(len(ts) == 2)
self.assertTrue(ta in ts)
self.assertTrue(tb in ts)
self.assertTrue(tc in ts)
tests.append(TestTypeHash)
# ---------------------------------------------------------------------------
def run(verbosity=1):