forked from M-Labs/nac3
add Arc<Mutex<dyn SymbolResolver>> and change from Box<SymbolResolve> to Arc<SymbolResolver>, need format and cleanup
This commit is contained in:
parent
43236db9bd
commit
42a636b4ce
|
@ -8,7 +8,7 @@ use inkwell::{
|
||||||
basic_block::BasicBlock, builder::Builder, context::Context, module::Module,
|
basic_block::BasicBlock, builder::Builder, context::Context, module::Module,
|
||||||
types::BasicTypeEnum, values::PointerValue,
|
types::BasicTypeEnum, values::PointerValue,
|
||||||
};
|
};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::{Mutex, RwLock};
|
||||||
use rustpython_parser::ast::{self, Stmt};
|
use rustpython_parser::ast::{self, Stmt};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||||||
|
@ -26,6 +26,8 @@ pub enum TopLevelDef {
|
||||||
methods: Vec<(String, Type, DefinitionId)>,
|
methods: Vec<(String, Type, DefinitionId)>,
|
||||||
// ancestor classes, including itself.
|
// ancestor classes, including itself.
|
||||||
ancestors: Vec<DefinitionId>,
|
ancestors: Vec<DefinitionId>,
|
||||||
|
// symbol resolver of the module defined the class, none if it is built-in type
|
||||||
|
resolver: Option<Arc<Mutex<dyn SymbolResolver + Send>>>
|
||||||
},
|
},
|
||||||
Function {
|
Function {
|
||||||
// prefix for symbol, should be unique globally, and not ending with numbers
|
// prefix for symbol, should be unique globally, and not ending with numbers
|
||||||
|
@ -44,6 +46,8 @@ pub enum TopLevelDef {
|
||||||
/// Value: AST annotated with types together with a unification table index. Could contain
|
/// Value: AST annotated with types together with a unification table index. Could contain
|
||||||
/// rigid type variables that would be substituted when the function is instantiated.
|
/// rigid type variables that would be substituted when the function is instantiated.
|
||||||
instance_to_stmt: HashMap<String, (Stmt<Option<Type>>, usize)>,
|
instance_to_stmt: HashMap<String, (Stmt<Option<Type>>, usize)>,
|
||||||
|
// symbol resolver of the module defined the class
|
||||||
|
resolver: Option<Arc<Mutex<dyn SymbolResolver + Send>>>
|
||||||
},
|
},
|
||||||
Initializer {
|
Initializer {
|
||||||
class_id: DefinitionId,
|
class_id: DefinitionId,
|
||||||
|
@ -80,27 +84,28 @@ pub struct CodeGenContext<'ctx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn name_mangling(mut class_name: String, method_name: &str) -> String { // need to further extend to more name mangling like instantiations of typevar
|
pub fn name_mangling(mut class_name: String, method_name: &str) -> String {
|
||||||
|
// need to further extend to more name mangling like instantiations of typevar
|
||||||
class_name.push_str(method_name);
|
class_name.push_str(method_name);
|
||||||
class_name
|
class_name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TopLevelDefInfo<'a> {
|
pub struct TopLevelDefInfo {
|
||||||
// like adding some info on top of the TopLevelDef for later parsing the class bases, method,
|
// like adding some info on top of the TopLevelDef for later parsing the class bases, method,
|
||||||
// and function sigatures
|
// and function sigatures
|
||||||
def: TopLevelDef, // the definition entry
|
def: TopLevelDef, // the definition entry
|
||||||
ty: Type, // the entry in the top_level unifier
|
ty: Type, // the entry in the top_level unifier
|
||||||
ast: Option<ast::Stmt<()>>, // the ast submitted by applications, primitives and class methods will have None value here
|
ast: Option<ast::Stmt<()>>, // the ast submitted by applications, primitives and class methods will have None value here
|
||||||
resolver: Option<&'a dyn SymbolResolver> // the resolver
|
// resolver: Option<&'a dyn SymbolResolver> // the resolver
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TopLevelComposer<'a> {
|
pub struct TopLevelComposer {
|
||||||
pub definition_list: Vec<TopLevelDefInfo<'a>>,
|
pub definition_list: Vec<TopLevelDefInfo>,
|
||||||
pub primitives: PrimitiveStore,
|
pub primitives: PrimitiveStore,
|
||||||
pub unifier: Unifier,
|
pub unifier: Unifier,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TopLevelComposer<'a> {
|
impl TopLevelComposer {
|
||||||
pub fn make_primitives() -> (PrimitiveStore, Unifier) {
|
pub fn make_primitives() -> (PrimitiveStore, Unifier) {
|
||||||
let mut unifier = Unifier::new();
|
let mut unifier = Unifier::new();
|
||||||
let int32 = unifier.add_ty(TypeEnum::TObj {
|
let int32 = unifier.add_ty(TypeEnum::TObj {
|
||||||
|
@ -135,35 +140,30 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let primitives = Self::make_primitives();
|
let primitives = Self::make_primitives();
|
||||||
let definition_list: Vec<TopLevelDefInfo<'a>> = vec![
|
let definition_list: Vec<TopLevelDefInfo> = vec![
|
||||||
TopLevelDefInfo {
|
TopLevelDefInfo {
|
||||||
def: Self::make_top_level_class_def(0),
|
def: Self::make_top_level_class_def(0, None),
|
||||||
ast: None,
|
ast: None,
|
||||||
resolver: None,
|
|
||||||
ty: primitives.0.int32,
|
ty: primitives.0.int32,
|
||||||
},
|
},
|
||||||
TopLevelDefInfo {
|
TopLevelDefInfo {
|
||||||
def: Self::make_top_level_class_def(1),
|
def: Self::make_top_level_class_def(1, None),
|
||||||
ast: None,
|
ast: None,
|
||||||
resolver: None,
|
|
||||||
ty: primitives.0.int64,
|
ty: primitives.0.int64,
|
||||||
},
|
},
|
||||||
TopLevelDefInfo {
|
TopLevelDefInfo {
|
||||||
def: Self::make_top_level_class_def(2),
|
def: Self::make_top_level_class_def(2, None),
|
||||||
ast: None,
|
ast: None,
|
||||||
resolver: None,
|
|
||||||
ty: primitives.0.float,
|
ty: primitives.0.float,
|
||||||
},
|
},
|
||||||
TopLevelDefInfo {
|
TopLevelDefInfo {
|
||||||
def: Self::make_top_level_class_def(3),
|
def: Self::make_top_level_class_def(3, None),
|
||||||
ast: None,
|
ast: None,
|
||||||
resolver: None,
|
|
||||||
ty: primitives.0.bool,
|
ty: primitives.0.bool,
|
||||||
},
|
},
|
||||||
TopLevelDefInfo {
|
TopLevelDefInfo {
|
||||||
def: Self::make_top_level_class_def(4),
|
def: Self::make_top_level_class_def(4, None),
|
||||||
ast: None,
|
ast: None,
|
||||||
resolver: None,
|
|
||||||
ty: primitives.0.none,
|
ty: primitives.0.none,
|
||||||
},
|
},
|
||||||
]; // the entries for primitive types
|
]; // the entries for primitive types
|
||||||
|
@ -171,22 +171,24 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// already include the definition_id of itself inside the ancestors vector
|
/// already include the definition_id of itself inside the ancestors vector
|
||||||
pub fn make_top_level_class_def(index: usize) -> TopLevelDef {
|
pub fn make_top_level_class_def(index: usize, resolver: Option<Arc<Mutex<dyn SymbolResolver + Send>>>) -> TopLevelDef {
|
||||||
TopLevelDef::Class {
|
TopLevelDef::Class {
|
||||||
object_id: DefinitionId(index),
|
object_id: DefinitionId(index),
|
||||||
type_vars: Default::default(),
|
type_vars: Default::default(),
|
||||||
fields: Default::default(),
|
fields: Default::default(),
|
||||||
methods: Default::default(),
|
methods: Default::default(),
|
||||||
ancestors: vec![DefinitionId(index)],
|
ancestors: vec![DefinitionId(index)],
|
||||||
|
resolver
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_top_level_function_def(name: String, ty: Type) -> TopLevelDef {
|
pub fn make_top_level_function_def(name: String, ty: Type, resolver: Option<Arc<Mutex<dyn SymbolResolver + Send>>>) -> TopLevelDef {
|
||||||
TopLevelDef::Function {
|
TopLevelDef::Function {
|
||||||
name,
|
name,
|
||||||
signature: ty,
|
signature: ty,
|
||||||
instance_to_symbol: Default::default(),
|
instance_to_symbol: Default::default(),
|
||||||
instance_to_stmt: Default::default(),
|
instance_to_stmt: Default::default(),
|
||||||
|
resolver
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,7 +207,7 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
pub fn register_top_level(
|
pub fn register_top_level(
|
||||||
&mut self,
|
&mut self,
|
||||||
ast: ast::Stmt<()>,
|
ast: ast::Stmt<()>,
|
||||||
resolver: &'a dyn SymbolResolver,
|
resolver: Option<Arc<Mutex<dyn SymbolResolver + Send>>>
|
||||||
) -> Result<Vec<(String, DefinitionId, Type)>, String> {
|
) -> Result<Vec<(String, DefinitionId, Type)>, String> {
|
||||||
match &ast.node {
|
match &ast.node {
|
||||||
ast::StmtKind::ClassDef { name, body, .. } => {
|
ast::StmtKind::ClassDef { name, body, .. } => {
|
||||||
|
@ -239,8 +241,7 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
// add to the definition list
|
// add to the definition list
|
||||||
self.definition_list.push(
|
self.definition_list.push(
|
||||||
TopLevelDefInfo {
|
TopLevelDefInfo {
|
||||||
def: Self::make_top_level_function_def(fun_name.clone(), ty),
|
def: Self::make_top_level_function_def(fun_name.clone(), ty, None), // FIXME:
|
||||||
resolver: Some(resolver),
|
|
||||||
ty,
|
ty,
|
||||||
ast: None // since it is inside the class def body statments
|
ast: None // since it is inside the class def body statments
|
||||||
}
|
}
|
||||||
|
@ -257,7 +258,6 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
},
|
},
|
||||||
ty: self.primitives.none, // arbitary picked one
|
ty: self.primitives.none, // arbitary picked one
|
||||||
ast: None, // it is inside the class def body statments
|
ast: None, // it is inside the class def body statments
|
||||||
resolver: Some(resolver)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
// FIXME: should we return this to the symbol resolver?, should be yes
|
// FIXME: should we return this to the symbol resolver?, should be yes
|
||||||
|
@ -267,8 +267,7 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
// add to the definition list
|
// add to the definition list
|
||||||
self.definition_list.push(
|
self.definition_list.push(
|
||||||
TopLevelDefInfo {
|
TopLevelDefInfo {
|
||||||
def: Self::make_top_level_class_def(class_def_id),
|
def: Self::make_top_level_class_def(class_def_id, resolver),
|
||||||
resolver: Some(resolver),
|
|
||||||
ast: Some(ast),
|
ast: Some(ast),
|
||||||
ty,
|
ty,
|
||||||
}
|
}
|
||||||
|
@ -291,9 +290,9 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
self.definition_list.push(TopLevelDefInfo {
|
self.definition_list.push(TopLevelDefInfo {
|
||||||
def: Self::make_top_level_function_def(
|
def: Self::make_top_level_function_def(
|
||||||
name.into(),
|
name.into(),
|
||||||
self.primitives.none
|
self.primitives.none,
|
||||||
|
resolver
|
||||||
),
|
),
|
||||||
resolver: Some(resolver),
|
|
||||||
ast: Some(ast),
|
ast: Some(ast),
|
||||||
ty,
|
ty,
|
||||||
});
|
});
|
||||||
|
@ -308,7 +307,7 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
/// this should be called after all top level classes are registered, and will actually fill in those fields of the previous dummy one
|
/// this should be called after all top level classes are registered, and will actually fill in those fields of the previous dummy one
|
||||||
pub fn analyze_top_level(&mut self) -> Result<(), String> {
|
pub fn analyze_top_level(&mut self) -> Result<(), String> {
|
||||||
for mut d in &mut self.definition_list {
|
for mut d in &mut self.definition_list {
|
||||||
if let (Some(ast), Some(resolver)) = (&d.ast, d.resolver) {
|
if let Some(ast) = &d.ast {
|
||||||
match &ast.node {
|
match &ast.node {
|
||||||
ast::StmtKind::ClassDef {
|
ast::StmtKind::ClassDef {
|
||||||
bases,
|
bases,
|
||||||
|
@ -320,15 +319,17 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
ancestors,
|
ancestors,
|
||||||
fields,
|
fields,
|
||||||
methods,
|
methods,
|
||||||
type_vars
|
type_vars,
|
||||||
|
// resolver,
|
||||||
) = if let TopLevelDef::Class {
|
) = if let TopLevelDef::Class {
|
||||||
object_id,
|
object_id,
|
||||||
ancestors,
|
ancestors,
|
||||||
fields,
|
fields,
|
||||||
methods,
|
methods,
|
||||||
type_vars
|
type_vars,
|
||||||
|
resolver
|
||||||
} = &mut d.def {
|
} = &mut d.def {
|
||||||
(object_id, ancestors, fields, methods, type_vars)
|
(object_id, ancestors, fields, methods, type_vars) // FIXME: this unwrap is not safe
|
||||||
} else { unreachable!() };
|
} else { unreachable!() };
|
||||||
|
|
||||||
// try to get mutable reference of the entry in the unification table, get the `TypeEnum`
|
// try to get mutable reference of the entry in the unification table, get the `TypeEnum`
|
||||||
|
@ -361,7 +362,7 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
match &e.node {
|
match &e.node {
|
||||||
ast::ExprKind::Name {id, ..} => {
|
ast::ExprKind::Name {id, ..} => {
|
||||||
// the def_list
|
// the def_list
|
||||||
type_vars.push(resolver.get_symbol_type(id).ok_or_else(|| "unknown type variable".to_string())?);
|
// type_vars.push(resolver.get_symbol_type(id).ok_or_else(|| "unknown type variable".to_string())?); FIXME:
|
||||||
|
|
||||||
// the TypeEnum of the class
|
// the TypeEnum of the class
|
||||||
// FIXME: the `params` destructed above is not mutable, even if this is mutable, what should the key be?
|
// FIXME: the `params` destructed above is not mutable, even if this is mutable, what should the key be?
|
||||||
|
@ -376,7 +377,7 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
// `class Foo(Generic[T]):`
|
// `class Foo(Generic[T]):`
|
||||||
ast::ExprKind::Name {id, ..} => {
|
ast::ExprKind::Name {id, ..} => {
|
||||||
// the def_list
|
// the def_list
|
||||||
type_vars.push(resolver.get_symbol_type(id).ok_or_else(|| "unknown type variable".to_string())?);
|
// type_vars.push(resolver.get_symbol_type(id).ok_or_else(|| "unknown type variable".to_string())?); FIXME:
|
||||||
|
|
||||||
// the TypeEnum of the class
|
// the TypeEnum of the class
|
||||||
// FIXME: the `params` destructed above is not mutable, even if this is mutable, what should the key be?
|
// FIXME: the `params` destructed above is not mutable, even if this is mutable, what should the key be?
|
||||||
|
@ -396,20 +397,20 @@ impl<'a> TopLevelComposer<'a> {
|
||||||
// base class, name directly available inside the
|
// base class, name directly available inside the
|
||||||
// module, can use this module's symbol resolver
|
// module, can use this module's symbol resolver
|
||||||
ast::ExprKind::Name {id, ..} => {
|
ast::ExprKind::Name {id, ..} => {
|
||||||
let def_id = resolver.get_identifier_def(id);
|
// let def_id = resolver.get_identifier_def(id); FIXME:
|
||||||
// the definition list
|
// the definition list
|
||||||
ancestors.push(def_id);
|
// ancestors.push(def_id);
|
||||||
},
|
},
|
||||||
|
|
||||||
// base class, things can be like `class A(BaseModule.Base)`, here we have to get the
|
// base class, things can be like `class A(BaseModule.Base)`, here we have to get the
|
||||||
// symbol resolver of the module `BaseModule`?
|
// symbol resolver of the module `BaseModule`?
|
||||||
ast::ExprKind::Attribute {value, attr, ..} => {
|
ast::ExprKind::Attribute {value, attr, ..} => {
|
||||||
if let ast::ExprKind::Name {id, ..} = &value.node {
|
if let ast::ExprKind::Name {id, ..} = &value.node {
|
||||||
if let Some(base_module_resolver) = resolver.get_module_resolver(id) {
|
// if let Some(base_module_resolver) = resolver.get_module_resolver(id) {
|
||||||
let def_id = base_module_resolver.get_identifier_def(attr);
|
// let def_id = base_module_resolver.get_identifier_def(attr);
|
||||||
// the definition list
|
// // the definition list
|
||||||
ancestors.push(def_id);
|
// ancestors.push(def_id);
|
||||||
} else { return Err("unkown imported module".into()) }
|
// } else { return Err("unkown imported module".into()) } FIXME:
|
||||||
} else { return Err("unkown imported module".into()) }
|
} else { return Err("unkown imported module".into()) }
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub struct PrimitiveStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FunctionData {
|
pub struct FunctionData {
|
||||||
pub resolver: Box<dyn SymbolResolver>,
|
pub resolver: Arc<dyn SymbolResolver>,
|
||||||
pub return_type: Option<Type>,
|
pub return_type: Option<Type>,
|
||||||
pub bound_variables: Vec<Type>,
|
pub bound_variables: Vec<Type>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,10 +100,10 @@ impl TestEnvironment {
|
||||||
let mut identifier_mapping = HashMap::new();
|
let mut identifier_mapping = HashMap::new();
|
||||||
identifier_mapping.insert("None".into(), none);
|
identifier_mapping.insert("None".into(), none);
|
||||||
|
|
||||||
let resolver = Box::new(Resolver {
|
let resolver = Arc::new(Resolver {
|
||||||
identifier_mapping: identifier_mapping.clone(),
|
identifier_mapping: identifier_mapping.clone(),
|
||||||
class_names: Default::default(),
|
class_names: Default::default(),
|
||||||
}) as Box<dyn SymbolResolver>;
|
}) as Arc<dyn SymbolResolver>;
|
||||||
|
|
||||||
TestEnvironment {
|
TestEnvironment {
|
||||||
unifier,
|
unifier,
|
||||||
|
@ -226,8 +226,8 @@ impl TestEnvironment {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let resolver =
|
let resolver =
|
||||||
Box::new(Resolver { identifier_mapping: identifier_mapping.clone(), class_names })
|
Arc::new(Resolver { identifier_mapping: identifier_mapping.clone(), class_names })
|
||||||
as Box<dyn SymbolResolver>;
|
as Arc<dyn SymbolResolver>;
|
||||||
|
|
||||||
TestEnvironment {
|
TestEnvironment {
|
||||||
unifier,
|
unifier,
|
||||||
|
|
Loading…
Reference in New Issue