From c01087cc3c55aba8a30cd8c549e6fb575ef72c52 Mon Sep 17 00:00:00 2001 From: Siddhangana Date: Tue, 24 Dec 2024 09:05:18 +0800 Subject: [PATCH] Add get_class_name and edit get_symbol_value --- nac3artiq/src/symbol_resolver.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index b9ac4b5..261e92a 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -947,6 +947,28 @@ impl InnerResolver { } } + pub fn get_class_name(&self, field_id: StrRef, defs: &[Arc>]) -> Option { + + 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,11 +1535,13 @@ impl SymbolResolver for Resolver { fn get_symbol_value<'ctx>( &self, id: StrRef, - _: &mut CodeGenContext<'ctx, '_>, + ctx: &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 (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)