WIP: nac3artiq: add support for class objects without __init__ method #417

Closed
abdul124 wants to merge 2 commits from support_class_objects into master
2 changed files with 43 additions and 6 deletions

View File

@ -0,0 +1,26 @@
from min_artiq import *
from numpy import int32, ceil
@nac3
class Foo:
attr: KernelInvariant[int32]
@nac3
class Bar:
core: KernelInvariant[Core]
attr2: KernelInvariant[int32]
def __init__(self):
self.core = Core()
self.attr2 = 4
@kernel
def run(self):
self.core.reset()
a: Foo = Foo()
if __name__ == "__main__":
Bar().run()

View File

@ -333,7 +333,14 @@ impl InnerResolver {
Ok(Ok((primitives.option, false))) Ok(Ok((primitives.option, false)))
} else if ty_id == self.primitive_ids.none { } else if ty_id == self.primitive_ids.none {
unreachable!("none cannot be typeid") unreachable!("none cannot be typeid")
} else if let Some(def_id) = self.pyid_to_def.read().get(&ty_id).copied() { } else if let Some(def_id) = {
let py_obj_id: u64 = self.helper.id_fn.call1(py, (pyty,))?.extract(py)?;
self.pyid_to_def
.read()
.get(&ty_id)
.copied()
.or_else(|| self.pyid_to_def.read().get(&py_obj_id).copied())
} {
let def = defs[def_id.0].read(); let def = defs[def_id.0].read();
let TopLevelDef::Class { object_id, type_vars, fields, methods, .. } = &*def else { let TopLevelDef::Class { object_id, type_vars, fields, methods, .. } = &*def else {
// only object is supported, functions are not supported // only object is supported, functions are not supported
@ -599,7 +606,9 @@ impl InnerResolver {
let pyid_to_def = self.pyid_to_def.read(); let pyid_to_def = self.pyid_to_def.read();
let constructor_ty = pyid_to_def.get(&py_obj_id).and_then(|def_id| { let constructor_ty = pyid_to_def.get(&py_obj_id).and_then(|def_id| {
defs.iter().find_map(|def| { defs.iter().find_map(|def| {
if let TopLevelDef::Class { object_id, methods, constructor, .. } = &*def.read() { if let Some(rear_guard) = def.try_read() {
if let TopLevelDef::Class { object_id, methods, constructor, .. } = &*rear_guard
{
if object_id == def_id if object_id == def_id
&& constructor.is_some() && constructor.is_some()
&& methods.iter().any(|(s, _, _)| s == &"__init__".into()) && methods.iter().any(|(s, _, _)| s == &"__init__".into())
@ -607,6 +616,7 @@ impl InnerResolver {
return *constructor; return *constructor;
} }
} }
}
None None
}) })
}); });
@ -625,6 +635,7 @@ impl InnerResolver {
self.primitive_ids.generic_alias.1, self.primitive_ids.generic_alias.1,
] ]
.contains(&self.helper.id_fn.call1(py, (ty.clone(),))?.extract::<u64>(py)?) .contains(&self.helper.id_fn.call1(py, (ty.clone(),))?.extract::<u64>(py)?)
|| self.pyid_to_def.read().contains_key(&py_obj_id)
{ {
obj obj
} else { } else {