Add get_class_name and edit get_symbol_value

This commit is contained in:
Siddhangana 2024-12-24 09:05:18 +08:00
parent ef53c7a2cb
commit c01087cc3c

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,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<ValueEnum<'ctx>> {
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)