Compare commits

..

4 Commits

2 changed files with 78 additions and 6 deletions

View File

@ -0,0 +1,37 @@
from min_artiq import *
from numpy import int32
@nac3
class NameManglingTest:
core: KernelInvariant[Core]
__var1: int32
__var2__: int32
__: int32
def __init__(self):
self.core = Core()
self.__var1 = 42
self.__var2__ = 10
self.__ = 99
@rpc
def get_var1(self) -> int32:
return self.__var1
@rpc
def get_var2(self) -> int32:
return self.__var2__
@rpc
def get_var3(self) -> int32:
return self.__
@kernel
def run(self):
assert self.get_var1() == 42
assert self.get_var2() == 10
assert self.get_var3() == 99
if __name__ == "__main__":
NameManglingTest().run()

View File

@ -947,6 +947,28 @@ impl InnerResolver {
}
}
pub fn get_class_name(&self, field_id: StrRef, defs: &[Arc<RwLock<TopLevelDef>>]) -> Option<String> {
let field_to_val = self.field_to_val.read();
let class_id = field_to_val.iter().find_map(|(&(id, ref name), _)| {
if *name == field_id {
Some(id)
} else {
None
}
})?;
let id_to_def = self.id_to_def.read();
let def_id = id_to_def.get(&StrRef::from(class_id.to_string()))?;
let definition = defs[def_id.0].read();
if let TopLevelDef::Class { name, .. } = &*definition {
Some(name.to_string()) // return class name
} else {
None // then not a class
}
}
pub fn get_obj_value<'ctx>(
&self,
py: Python,
@ -1513,12 +1535,25 @@ impl SymbolResolver for Resolver {
fn get_symbol_value<'ctx>(
&self,
id: StrRef,
_: &mut CodeGenContext<'ctx, '_>,
ctx: &mut CodeGenContext<'ctx, '_>,
_: &mut dyn CodeGenerator,
) -> Option<ValueEnum<'ctx>> {
let (resolved_id, _is_dunder) = if id.to_string().starts_with("__") && !id.to_string().ends_with("__") && !id.to_string().contains('.') {
let inner_resolver = &self.0;
if let Some(class_name) = inner_resolver.get_class_name(id, &ctx.top_level.definitions.read()) {
let stripped_class_name = class_name.trim_start_matches('_');
let mangled_id: StrRef = format!("_{}{}", stripped_class_name, id).into();
(mangled_id, true)
} else {
(id, false)
}
} else {
(id, false)
};
let sym_value = {
let id_to_val = self.0.id_to_pyval.read();
id_to_val.get(&id).cloned()
id_to_val.get(&resolved_id).cloned()
}
.or_else(|| {
Python::with_gil(|py| -> PyResult<Option<(u64, PyObject)>> {
@ -1527,14 +1562,14 @@ impl SymbolResolver for Resolver {
let members: &PyDict = obj.getattr("__dict__").unwrap().downcast().unwrap();
for (key, val) in members {
let key: &str = key.extract()?;
if key == id.to_string() {
let id = self.0.helper.id_fn.call1(py, (val,))?.extract(py)?;
sym_value = Some((id, val.extract()?));
if key == resolved_id.to_string() {
let pyid = self.0.helper.id_fn.call1(py, (val,))?.extract(py)?;
sym_value = Some((pyid, val.extract()?));
break;
}
}
if let Some((pyid, val)) = &sym_value {
self.0.id_to_pyval.write().insert(id, (*pyid, val.clone()));
self.0.id_to_pyval.write().insert(resolved_id, (*pyid, val.clone()));
}
Ok(sym_value)
})