import gc
from mlir.ir import *
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
def testUnknown():
with Context() as ctx:
loc = Location.unknown()
assert loc.context is ctx
ctx = None
gc.collect()
print("unknown str:", str(loc))
print("unknown repr:", repr(loc))
run(testUnknown)
def testLocationAttr():
with Context() as ctxt:
loc = Location.unknown()
attr = loc.attr
clone = Location.from_attr(attr)
gc.collect()
print("loc:", str(loc))
print("clone:", str(clone))
assert loc == clone
run(testLocationAttr)
def testFileLineCol():
with Context() as ctx:
loc = Location.file("foo1.txt", 123, 56)
range = Location.file("foo2.txt", 123, 56, 124, 100)
ctx = None
gc.collect()
print("file str:", str(loc))
print("file repr:", repr(loc))
print("file range str:", str(range))
print("file range repr:", repr(range))
assert loc.is_a_file()
assert not loc.is_a_name()
assert not loc.is_a_callsite()
assert not loc.is_a_fused()
print("file filename:", loc.filename)
print("file start_line:", loc.start_line)
print("file start_col:", loc.start_col)
print("file end_line:", loc.end_line)
print("file end_col:", loc.end_col)
assert range.is_a_file()
print("file filename:", range.filename)
print("file start_line:", range.start_line)
print("file start_col:", range.start_col)
print("file end_line:", range.end_line)
print("file end_col:", range.end_col)
with Context() as ctx:
ctx.allow_unregistered_dialects = True
loc = Location.file("foo3.txt", 127, 61)
with loc:
i32 = IntegerType.get_signless(32)
module = Module.create()
with InsertionPoint(module.body):
new_value = Operation.create("custom.op1", results=[i32]).result
print("new_value location: ", new_value.location)
run(testFileLineCol)
def testName():
with Context() as ctx:
loc = Location.name("nombre")
loc_with_child_loc = Location.name("naam", loc)
ctx = None
gc.collect()
print("name str:", str(loc))
print("name repr:", repr(loc))
print("name str:", str(loc_with_child_loc))
print("name repr:", repr(loc_with_child_loc))
assert loc.is_a_name()
print("name name_str:", loc.name_str)
print("name child_loc:", loc.child_loc)
assert loc_with_child_loc.is_a_name()
print("name name_str:", loc_with_child_loc.name_str)
print("name child_loc_with_child_loc:", loc_with_child_loc.child_loc)
run(testName)
def testCallSite():
with Context() as ctx:
loc = Location.callsite(
Location.file("foo.text", 123, 45),
[Location.file("util.foo", 379, 21), Location.file("main.foo", 100, 63)],
)
ctx = None
print("callsite str:", str(loc))
print("callsite repr:", repr(loc))
assert loc.is_a_callsite()
print("callsite callee:", loc.callee)
print("callsite caller:", loc.caller)
run(testCallSite)
def testFused():
with Context() as ctx:
loc_single = Location.fused([Location.name("apple")])
loc = Location.fused([Location.name("apple"), Location.name("banana")])
attr = Attribute.parse('"sauteed"')
loc_attr = Location.fused(
[Location.name("carrot"), Location.name("potatoes")], attr
)
loc_empty = Location.fused([])
loc_empty_attr = Location.fused([], attr)
loc_single_attr = Location.fused([Location.name("apple")], attr)
ctx = None
assert not loc_single.is_a_fused()
print("fused str:", str(loc_single))
print("fused repr:", repr(loc_single))
print("fused locations:", loc_single.locations)
assert loc.is_a_fused()
print("fused str:", str(loc))
print("fused repr:", repr(loc))
print("fused locations:", loc.locations)
assert loc_attr.is_a_fused()
print("fused str:", str(loc_attr))
print("fused repr:", repr(loc_attr))
print("fused locations:", loc_attr.locations)
assert not loc_empty.is_a_fused()
print("fused str:", str(loc_empty))
print("fused repr:", repr(loc_empty))
print("fused locations:", loc_empty.locations)
assert loc_empty_attr.is_a_fused()
print("fused str:", str(loc_empty_attr))
print("fused repr:", repr(loc_empty_attr))
print("fused locations:", loc_empty_attr.locations)
assert loc_single_attr.is_a_fused()
print("fused str:", str(loc_single_attr))
print("fused repr:", repr(loc_single_attr))
print("fused locations:", loc_single_attr.locations)
run(testFused)
def testLocationCapsule():
with Context() as ctx:
loc1 = Location.file("foo.txt", 123, 56)
loc_capsule = loc1._CAPIPtr
print(loc_capsule)
loc2 = Location._CAPICreate(loc_capsule)
assert loc2 == loc1
assert loc2.context is ctx
run(testLocationCapsule)