forked from M-Labs/nac3
1
0
Fork 0

try to use def list ast tuple and remove method_to_def_id map

This commit is contained in:
ychenfo 2021-08-18 10:01:11 +08:00
parent 619963dc8c
commit 4fcd48e4c8
2 changed files with 103 additions and 92 deletions

View File

@ -1,12 +1,12 @@
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
use std::ops::Deref; use std::ops::{Deref, DerefMut};
use std::{collections::HashMap, collections::HashSet, sync::Arc}; use std::{collections::HashMap, collections::HashSet, sync::Arc};
use super::typecheck::type_inferencer::PrimitiveStore; use super::typecheck::type_inferencer::PrimitiveStore;
use super::typecheck::typedef::{SharedUnifier, Type, TypeEnum, Unifier}; use super::typecheck::typedef::{SharedUnifier, Type, TypeEnum, Unifier};
use crate::symbol_resolver::SymbolResolver; use crate::symbol_resolver::SymbolResolver;
use crate::typecheck::typedef::{FunSignature, FuncArg}; use crate::typecheck::typedef::{FunSignature, FuncArg};
use itertools::chain; use itertools::{Itertools, chain};
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
use rustpython_parser::ast::{self, Stmt}; use rustpython_parser::ast::{self, Stmt};
@ -64,16 +64,14 @@ impl TopLevelDef {
} }
pub struct TopLevelContext { pub struct TopLevelContext {
pub definitions: Arc<RwLock<Vec<RwLock<TopLevelDef>>>>, pub definitions: Arc<RwLock<Vec<Arc<RwLock<TopLevelDef>>>>>,
pub unifiers: Arc<RwLock<Vec<(SharedUnifier, PrimitiveStore)>>>, pub unifiers: Arc<RwLock<Vec<(SharedUnifier, PrimitiveStore)>>>,
} }
pub struct TopLevelComposer { pub struct TopLevelComposer {
// list of top level definitions, same as top level context // list of top level definitions, same as top level context
pub definition_list: Arc<RwLock<Vec<RwLock<TopLevelDef>>>>, pub definition_ast_list: Arc<RwLock<Vec<(Arc<RwLock<TopLevelDef>>, Option<ast::Stmt<()>>)>>>,
// list of top level ast, the index is same as the field `definition_list`
pub ast_list: Vec<Option<ast::Stmt<()>>>,
// start as a primitive unifier, will add more top_level defs inside // start as a primitive unifier, will add more top_level defs inside
pub unifier: Unifier, pub unifier: Unifier,
// primitive store // primitive store
@ -86,8 +84,14 @@ pub struct TopLevelComposer {
impl TopLevelComposer { impl TopLevelComposer {
pub fn to_top_level_context(&self) -> TopLevelContext { pub fn to_top_level_context(&self) -> TopLevelContext {
let def_list = self
.definition_ast_list
.read()
.iter()
.map(|(x, _)| x.clone())
.collect::<Vec<_>>();
TopLevelContext { TopLevelContext {
definitions: self.definition_list.clone(), definitions: RwLock::new(def_list).into(),
// FIXME: all the big unifier or? // FIXME: all the big unifier or?
unifiers: Default::default(), unifiers: Default::default(),
} }
@ -136,18 +140,19 @@ impl TopLevelComposer {
let primitives = Self::make_primitives(); let primitives = Self::make_primitives();
let top_level_def_list = vec![ let top_level_def_list = vec![
RwLock::new(Self::make_top_level_class_def(0, None)), Arc::new(RwLock::new(Self::make_top_level_class_def(0, None))),
RwLock::new(Self::make_top_level_class_def(1, None)), Arc::new(RwLock::new(Self::make_top_level_class_def(1, None))),
RwLock::new(Self::make_top_level_class_def(2, None)), Arc::new(RwLock::new(Self::make_top_level_class_def(2, None))),
RwLock::new(Self::make_top_level_class_def(3, None)), Arc::new(RwLock::new(Self::make_top_level_class_def(3, None))),
RwLock::new(Self::make_top_level_class_def(4, None)), Arc::new(RwLock::new(Self::make_top_level_class_def(4, None))),
]; ];
let ast_list: Vec<Option<ast::Stmt<()>>> = vec![None, None, None, None, None]; let ast_list: Vec<Option<ast::Stmt<()>>> = vec![None, None, None, None, None];
let composer = TopLevelComposer { let composer = TopLevelComposer {
definition_list: RwLock::new(top_level_def_list).into(), definition_ast_list: RwLock::new(
ast_list, top_level_def_list.into_iter().zip(ast_list).collect_vec()
).into(),
primitives: primitives.0, primitives: primitives.0,
unifier: primitives.1.into(), unifier: primitives.1.into(),
class_method_to_def_id: Default::default(), class_method_to_def_id: Default::default(),
@ -202,62 +207,77 @@ impl TopLevelComposer {
ast: ast::Stmt<()>, ast: ast::Stmt<()>,
resolver: Option<Arc<Mutex<dyn SymbolResolver + Send + Sync>>>, resolver: Option<Arc<Mutex<dyn SymbolResolver + Send + Sync>>>,
) -> Result<(String, DefinitionId), String> { ) -> Result<(String, DefinitionId), String> {
let (mut def_list, ast_list) = (self.definition_list.write(), &mut self.ast_list); let mut def_list = self.definition_ast_list.write();
assert_eq!(def_list.len(), ast_list.len());
match &ast.node { match &ast.node {
ast::StmtKind::ClassDef { name, body, .. } => { ast::StmtKind::ClassDef { name, body, .. } => {
let class_name = name.to_string(); let class_name = name.to_string();
let class_def_id = def_list.len(); let class_def_id = def_list.len();
// add the class to the definition lists // add the class to the definition lists
def_list
.push(Self::make_top_level_class_def(class_def_id, resolver.clone()).into());
// since later when registering class method, ast will still be used, // since later when registering class method, ast will still be used,
// here push None temporarly, later will move the ast inside // here push None temporarly, later will move the ast inside
ast_list.push(None); let mut class_def_ast = (
Arc::new(RwLock::new(
Self::make_top_level_class_def(class_def_id, resolver.clone())
)),
None
);
// parse class def body and register class methods into the def list. // parse class def body and register class methods into the def list.
// module's symbol resolver would not know the name of the class methods, // module's symbol resolver would not know the name of the class methods,
// thus cannot return their definition_id? so we have to manage it ourselves // thus cannot return their definition_id
// by using `class_method_to_def_id` let mut class_method_name_def_ids: Vec<(String, Arc<RwLock<TopLevelDef>>, DefinitionId)> = Vec::new();
let mut class_method_index_offset = 0;
for b in body { for b in body {
if let ast::StmtKind::FunctionDef { name, .. } = &b.node { if let ast::StmtKind::FunctionDef { name: method_name, .. } = &b.node {
let fun_name = Self::name_mangling(class_name.clone(), name); let method_name = Self::name_mangling(class_name.clone(), method_name);
let def_id = def_list.len(); let method_def_id = def_list.len() + {
class_method_index_offset += 1;
class_method_index_offset
};
// add to the definition list // dummy method define here
def_list.push(
Self::make_top_level_function_def(
fun_name.clone(),
self.unifier.add_ty(TypeEnum::TFunc(
FunSignature {
args: Default::default(),
ret: self.primitives.none,
vars: Default::default(),
}
.into(),
)),
resolver.clone(),
)
.into(),
);
// the ast of class method is in the class, push None in to the list here // the ast of class method is in the class, push None in to the list here
ast_list.push(None); class_method_name_def_ids.push((
method_name.clone(),
RwLock::new(Self::make_top_level_function_def(
method_name.clone(),
self.primitives.none,
resolver.clone(),
)).into(),
DefinitionId(method_def_id)
));
}
}
// move the ast to the entry of the class in the ast_list
class_def_ast.1 = Some(ast);
// class method, do not let the symbol manager manage it, use our own map // put methods into the class def
self.class_method_to_def_id.insert(fun_name, DefinitionId(def_id)); {
let mut class_def = class_def_ast.0.write();
let class_def_methods =
if let TopLevelDef::Class { methods, .. } = class_def.deref_mut() {
methods
} else { unimplemented!() };
for (name, _, id) in &class_method_name_def_ids {
class_def_methods.push((name.into(), self.primitives.none, *id));
} }
} }
// move the ast to the entry of the class in the ast_list // now class_def_ast and class_method_def_ast_ids are ok, put them into actual def list in correct order
ast_list[class_def_id] = Some(ast); def_list.push(class_def_ast);
for (_, def, _) in class_method_name_def_ids {
def_list.push((def, None));
}
// put the constructor into the def_list // put the constructor into the def_list
def_list def_list
.push(TopLevelDef::Initializer { class_id: DefinitionId(class_def_id) }.into()); .push((
ast_list.push(None); RwLock::new(
TopLevelDef::Initializer { class_id: DefinitionId(class_def_id) }
).into(),
None
));
// class, put its def_id into the to be analyzed set // class, put its def_id into the to be analyzed set
let to_be_analyzed = &mut self.to_be_analyzed_class; let to_be_analyzed = &mut self.to_be_analyzed_class;
@ -270,11 +290,11 @@ impl TopLevelComposer {
let fun_name = name.to_string(); let fun_name = name.to_string();
// add to the definition list // add to the definition list
def_list.push( def_list.push((
Self::make_top_level_function_def(name.into(), self.primitives.none, resolver) RwLock::new(Self::make_top_level_function_def(name.into(), self.primitives.none, resolver))
.into(), .into(),
); Some(ast)
ast_list.push(Some(ast)); ));
// return // return
Ok((fun_name, DefinitionId(def_list.len() - 1))) Ok((fun_name, DefinitionId(def_list.len() - 1)))
@ -286,20 +306,17 @@ impl TopLevelComposer {
/// step 1, analyze the type vars associated with top level class /// step 1, analyze the type vars associated with top level class
fn analyze_top_level_class_type_var(&mut self) -> Result<(), String> { fn analyze_top_level_class_type_var(&mut self) -> Result<(), String> {
let mut def_list = self.definition_list.write(); let mut def_list = self.definition_ast_list.write();
let ast_list = &self.ast_list;
let converted_top_level = &self.to_top_level_context(); let converted_top_level = &self.to_top_level_context();
let primitives = &self.primitives; let primitives = &self.primitives;
let unifier = &mut self.unifier; let unifier = &mut self.unifier;
for (class_def, class_ast) in def_list for (class_def, class_ast) in def_list.iter_mut()
.iter_mut()
.zip(ast_list.iter())
.collect::<Vec<(&mut RwLock<TopLevelDef>, &Option<ast::Stmt<()>>)>>()
{ {
// only deal with class def here // only deal with class def here
let (class_bases, class_def_type_vars, class_resolver) = { let mut class_def = class_def.write();
if let TopLevelDef::Class { type_vars, resolver, .. } = class_def.get_mut() { let (class_bases_ast, class_def_type_vars, class_resolver) = {
if let TopLevelDef::Class { type_vars, resolver, .. } = class_def.deref_mut() {
if let Some(ast::Located { if let Some(ast::Located {
node: ast::StmtKind::ClassDef { bases, .. }, .. node: ast::StmtKind::ClassDef { bases, .. }, ..
}) = class_ast }) = class_ast
@ -312,9 +329,10 @@ impl TopLevelComposer {
continue; continue;
} }
}; };
let class_resolver = class_resolver.as_ref().unwrap().lock();
let mut is_generic = false; let mut is_generic = false;
for b in class_bases { for b in class_bases_ast {
match &b.node { match &b.node {
// analyze typevars bounded to the class, // analyze typevars bounded to the class,
// only support things like `class A(Generic[T, V])`, // only support things like `class A(Generic[T, V])`,
@ -322,10 +340,7 @@ impl TopLevelComposer {
// i.e. only simple names are allowed in the subscript // i.e. only simple names are allowed in the subscript
// should update the TopLevelDef::Class.typevars and the TypeEnum::TObj.params // should update the TopLevelDef::Class.typevars and the TypeEnum::TObj.params
ast::ExprKind::Subscript { value, slice, .. } ast::ExprKind::Subscript { value, slice, .. }
if { if matches!(&value.node, ast::ExprKind::Name { id, .. } if id == "Generic") => {
matches!(&value.node, ast::ExprKind::Name { id, .. } if id == "Generic")
} =>
{
if !is_generic { if !is_generic {
is_generic = true; is_generic = true;
} else { } else {
@ -338,7 +353,7 @@ impl TopLevelComposer {
let type_vars = elts let type_vars = elts
.iter() .iter()
.map(|e| { .map(|e| {
class_resolver.as_ref().unwrap().lock().parse_type_annotation( class_resolver.parse_type_annotation(
converted_top_level, converted_top_level,
unifier.borrow_mut(), unifier.borrow_mut(),
primitives, primitives,
@ -368,7 +383,7 @@ impl TopLevelComposer {
// `class A(Generic[T])` // `class A(Generic[T])`
} else { } else {
let ty = let ty =
class_resolver.as_ref().unwrap().lock().parse_type_annotation( class_resolver.parse_type_annotation(
converted_top_level, converted_top_level,
unifier.borrow_mut(), unifier.borrow_mut(),
primitives, primitives,
@ -400,19 +415,16 @@ impl TopLevelComposer {
/// if the type var associated with class `B` has not been handled properly, /// if the type var associated with class `B` has not been handled properly,
/// the parse of type annotation of `B[int, bool]` will fail /// the parse of type annotation of `B[int, bool]` will fail
fn analyze_top_level_class_bases(&mut self) -> Result<(), String> { fn analyze_top_level_class_bases(&mut self) -> Result<(), String> {
let mut def_list = self.definition_list.write(); let mut def_list = self.definition_ast_list.write();
let ast_list = &self.ast_list;
let converted_top_level = &self.to_top_level_context(); let converted_top_level = &self.to_top_level_context();
let primitives = &self.primitives; let primitives = &self.primitives;
let unifier = &mut self.unifier; let unifier = &mut self.unifier;
for (class_def, class_ast) in def_list for (class_def, class_ast) in def_list.iter_mut()
.iter_mut()
.zip(ast_list.iter())
.collect::<Vec<(&mut RwLock<TopLevelDef>, &Option<ast::Stmt<()>>)>>()
{ {
let mut class_def = class_def.write();
let (class_bases, class_ancestors, class_resolver) = { let (class_bases, class_ancestors, class_resolver) = {
if let TopLevelDef::Class { ancestors, resolver, .. } = class_def.get_mut() { if let TopLevelDef::Class { ancestors, resolver, .. } = class_def.deref_mut() {
if let Some(ast::Located { if let Some(ast::Located {
node: ast::StmtKind::ClassDef { bases, .. }, .. node: ast::StmtKind::ClassDef { bases, .. }, ..
}) = class_ast }) = class_ast
@ -425,6 +437,7 @@ impl TopLevelComposer {
continue; continue;
} }
}; };
let class_resolver = class_resolver.as_ref().unwrap().lock();
for b in class_bases { for b in class_bases {
// type vars have already been handled, so skip on `Generic[...]` // type vars have already been handled, so skip on `Generic[...]`
if let ast::ExprKind::Subscript { value, .. } = &b.node { if let ast::ExprKind::Subscript { value, .. } = &b.node {
@ -435,7 +448,7 @@ impl TopLevelComposer {
} }
} }
// get the def id of the base class // get the def id of the base class
let base_ty = class_resolver.as_ref().unwrap().lock().parse_type_annotation( let base_ty = class_resolver.parse_type_annotation(
converted_top_level, converted_top_level,
unifier.borrow_mut(), unifier.borrow_mut(),
primitives, primitives,
@ -457,31 +470,29 @@ impl TopLevelComposer {
/// step 3, class fields and methods /// step 3, class fields and methods
fn analyze_top_level_class_fields_methods(&mut self) -> Result<(), String> { fn analyze_top_level_class_fields_methods(&mut self) -> Result<(), String> {
let mut def_list = self.definition_list.write(); let mut def_list = self.definition_ast_list.write();
let ast_list = &self.ast_list;
let converted_top_level = &self.to_top_level_context(); let converted_top_level = &self.to_top_level_context();
let class_method_to_def_id = &self.class_method_to_def_id;
let primitives = &self.primitives; let primitives = &self.primitives;
let to_be_analyzed_class = &mut self.to_be_analyzed_class; let to_be_analyzed_class = &mut self.to_be_analyzed_class;
let unifier = &mut self.unifier; let unifier = &mut self.unifier;
while !to_be_analyzed_class.is_empty() { while !to_be_analyzed_class.is_empty() {
let class_ind = to_be_analyzed_class.remove(0).0; let class_ind = to_be_analyzed_class.remove(0).0;
let (class_name, class_body) = { let (class_name, class_body, classs_def) = {
let class_ast = &ast_list[class_ind]; let class_ast = def_list[class_ind].1.as_ref();
if let Some(ast::Located { if let Some(ast::Located {
node: ast::StmtKind::ClassDef { name, body, .. }, .. node: ast::StmtKind::ClassDef { name, body, .. }, ..
}) = class_ast }) = class_ast
{ {
(name, body) let class_def = def_list[class_ind].0;
(name, body, class_def)
} else { } else {
unreachable!("should be class def ast") unreachable!("should be class def ast")
} }
}; };
let class_methods_parsing_result: Vec<(String, Type, DefinitionId)> = let class_methods_parsing_result: Vec<(String, Type, DefinitionId)> = vec![];
Default::default(); let class_fields_parsing_result: Vec<(String, Type)> = vec![];
let class_fields_parsing_result: Vec<(String, Type)> = Default::default();
for b in class_body { for b in class_body {
if let ast::StmtKind::FunctionDef { if let ast::StmtKind::FunctionDef {
args: method_args_ast, args: method_args_ast,
@ -516,7 +527,7 @@ impl TopLevelComposer {
}; };
let (class_fields, class_methods, class_resolver) = { let (class_fields, class_methods, class_resolver) = {
if let TopLevelDef::Class { resolver, fields, methods, .. } = if let TopLevelDef::Class { resolver, fields, methods, .. } =
class_def.get_mut() class_def.0.get_mut()
{ {
(fields, methods, resolver) (fields, methods, resolver)
} else { } else {

View File

@ -118,7 +118,7 @@ impl TestEnvironment {
fn new() -> TestEnvironment { fn new() -> TestEnvironment {
let mut unifier = Unifier::new(); let mut unifier = Unifier::new();
let mut identifier_mapping = HashMap::new(); let mut identifier_mapping = HashMap::new();
let mut top_level_defs = Vec::new(); let mut top_level_defs: Vec<Arc<RwLock<TopLevelDef>>> = Vec::new();
let int32 = unifier.add_ty(TypeEnum::TObj { let int32 = unifier.add_ty(TypeEnum::TObj {
obj_id: DefinitionId(0), obj_id: DefinitionId(0),
fields: HashMap::new().into(), fields: HashMap::new().into(),
@ -153,7 +153,7 @@ impl TestEnvironment {
methods: Default::default(), methods: Default::default(),
ancestors: Default::default(), ancestors: Default::default(),
resolver: None, resolver: None,
})); }).into());
} }
let primitives = PrimitiveStore { int32, int64, float, bool, none }; let primitives = PrimitiveStore { int32, int64, float, bool, none };
@ -172,7 +172,7 @@ impl TestEnvironment {
methods: Default::default(), methods: Default::default(),
ancestors: Default::default(), ancestors: Default::default(),
resolver: None, resolver: None,
})); }).into());
identifier_mapping.insert( identifier_mapping.insert(
"Foo".into(), "Foo".into(),
@ -205,7 +205,7 @@ impl TestEnvironment {
methods: Default::default(), methods: Default::default(),
ancestors: Default::default(), ancestors: Default::default(),
resolver: None, resolver: None,
})); }).into());
identifier_mapping.insert( identifier_mapping.insert(
"Bar".into(), "Bar".into(),
unifier.add_ty(TypeEnum::TFunc( unifier.add_ty(TypeEnum::TFunc(
@ -229,7 +229,7 @@ impl TestEnvironment {
methods: Default::default(), methods: Default::default(),
ancestors: Default::default(), ancestors: Default::default(),
resolver: None, resolver: None,
})); }).into());
identifier_mapping.insert( identifier_mapping.insert(
"Bar2".into(), "Bar2".into(),
unifier.add_ty(TypeEnum::TFunc( unifier.add_ty(TypeEnum::TFunc(