From 7ad72b7eea57fb444f6546e75233d51c833200c6 Mon Sep 17 00:00:00 2001 From: Siddhangana Date: Thu, 19 Dec 2024 13:15:05 +0800 Subject: [PATCH] Implement name mangling logic in symbol_resolver --- nac3artiq/src/codegen.rs | 9 +++++++++ nac3artiq/src/symbol_resolver.rs | 21 ++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index 653f41a3..d788c66d 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -102,6 +102,15 @@ impl<'a> ArtiqCodeGenerator<'a> { } } + /// To resolve class name dynamically - name field is used to obtain class name + pub fn get_class_name(&self) -> Option { + if self.name.is_empty() { + None + } else { + Some(self.name.clone()) + } + } + /// If the generator is currently in a direct-`parallel` block context, emits IR that resets the /// position of the timeline to the initial timeline position before entering the `parallel` /// block. diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index 6507dc20..b9ac4b51 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -1516,9 +1516,20 @@ impl SymbolResolver for Resolver { _: &mut CodeGenContext<'ctx, '_>, _: &mut dyn CodeGenerator, ) -> Option> { + let (resolved_id, is_dunder) = if id.starts_with("__") && !id.ends_with("__") && !id.to_string().contains('.') { + if let Some(class_name) = self.get_class_name() { + 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> { @@ -1527,14 +1538,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) })