nac3/nac3artiq/src/symbol_resolver.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

2021-09-23 19:30:03 +08:00
use nac3core::{
location::Location,
symbol_resolver::{SymbolResolver, SymbolValue},
toplevel::DefinitionId,
typecheck::{
type_inferencer::PrimitiveStore,
typedef::{Type, Unifier},
},
};
2021-09-30 22:30:54 +08:00
use parking_lot::{Mutex, RwLock};
2021-09-24 09:58:58 +08:00
use rustpython_parser::ast::StrRef;
2021-09-23 19:30:03 +08:00
use std::{collections::HashMap, sync::Arc};
2021-09-30 22:30:54 +08:00
pub struct Resolver {
2021-09-24 09:58:58 +08:00
pub id_to_type: Mutex<HashMap<StrRef, Type>>,
pub id_to_def: Mutex<HashMap<StrRef, DefinitionId>>,
pub class_names: Mutex<HashMap<StrRef, Type>>,
2021-09-30 22:30:54 +08:00
pub pyid_to_def: Arc<RwLock<HashMap<u64, DefinitionId>>>,
pub pyid_to_type: Arc<RwLock<HashMap<u64, Type>>>,
// module specific
pub name_to_pyid: HashMap<StrRef, u64>,
2021-09-23 19:30:03 +08:00
}
impl SymbolResolver for Resolver {
2021-09-24 09:58:58 +08:00
fn get_symbol_type(&self, _: &mut Unifier, _: &PrimitiveStore, str: StrRef) -> Option<Type> {
2021-09-30 22:30:54 +08:00
let mut id_to_type = self.id_to_type.lock();
id_to_type.get(&str).cloned().or_else(|| {
let py_id = self.name_to_pyid.get(&str);
let result = py_id.and_then(|id| self.pyid_to_type.read().get(&id).copied());
if let Some(result) = &result {
id_to_type.insert(str, *result);
}
result
})
2021-09-23 19:30:03 +08:00
}
2021-09-24 09:58:58 +08:00
fn get_symbol_value(&self, _: StrRef) -> Option<SymbolValue> {
2021-09-23 19:30:03 +08:00
unimplemented!()
}
2021-09-24 09:58:58 +08:00
fn get_symbol_location(&self, _: StrRef) -> Option<Location> {
2021-09-23 19:30:03 +08:00
unimplemented!()
}
2021-09-24 09:58:58 +08:00
fn get_identifier_def(&self, id: StrRef) -> Option<DefinitionId> {
2021-09-30 22:30:54 +08:00
let mut id_to_def = self.id_to_def.lock();
id_to_def.get(&id).cloned().or_else(|| {
let py_id = self.name_to_pyid.get(&id);
let result = py_id.and_then(|id| self.pyid_to_def.read().get(&id).copied());
if let Some(result) = &result {
id_to_def.insert(id, *result);
}
result
})
2021-09-23 19:30:03 +08:00
}
}