2021-08-31 15:22:45 +08:00
|
|
|
use crate::{
|
|
|
|
location::Location,
|
|
|
|
symbol_resolver::{SymbolResolver, SymbolValue},
|
|
|
|
toplevel::DefinitionId,
|
|
|
|
typecheck::{
|
|
|
|
type_inferencer::PrimitiveStore,
|
|
|
|
typedef::{Type, Unifier},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use indoc::indoc;
|
2021-09-07 00:20:40 +08:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use rustpython_parser::parser::parse_program;
|
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2021-08-31 15:22:45 +08:00
|
|
|
use test_case::test_case;
|
|
|
|
|
2021-09-07 00:20:40 +08:00
|
|
|
use super::*;
|
2021-08-31 15:22:45 +08:00
|
|
|
|
2021-09-08 19:27:32 +08:00
|
|
|
struct ResolverInternal {
|
|
|
|
id_to_type: Mutex<HashMap<String, Type>>,
|
|
|
|
id_to_def: Mutex<HashMap<String, DefinitionId>>,
|
|
|
|
class_names: Mutex<HashMap<String, Type>>,
|
2021-08-31 15:22:45 +08:00
|
|
|
}
|
|
|
|
|
2021-09-08 19:27:32 +08:00
|
|
|
impl ResolverInternal {
|
|
|
|
fn add_id_def(&self, id: String, def: DefinitionId) {
|
|
|
|
self.id_to_def.lock().insert(id, def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Resolver(Arc<ResolverInternal>);
|
|
|
|
|
2021-08-31 15:22:45 +08:00
|
|
|
impl SymbolResolver for Resolver {
|
|
|
|
fn get_symbol_type(&self, _: &mut Unifier, _: &PrimitiveStore, str: &str) -> Option<Type> {
|
2021-09-08 19:27:32 +08:00
|
|
|
self.0.id_to_type.lock().get(str).cloned()
|
2021-08-31 15:22:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_symbol_value(&self, _: &str) -> Option<SymbolValue> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_symbol_location(&self, _: &str) -> Option<Location> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_identifier_def(&self, id: &str) -> Option<DefinitionId> {
|
2021-09-08 19:27:32 +08:00
|
|
|
self.0.id_to_def.lock().get(id).cloned()
|
2021-08-31 15:22:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_case(
|
|
|
|
vec![
|
|
|
|
indoc! {"
|
2021-09-07 00:20:40 +08:00
|
|
|
def fun(a: int32) -> int32:
|
2021-08-31 15:22:45 +08:00
|
|
|
return a
|
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class A:
|
|
|
|
def __init__(self):
|
2021-09-07 00:20:40 +08:00
|
|
|
self.a: int32 = 3
|
2021-08-31 15:22:45 +08:00
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class B:
|
|
|
|
def __init__(self):
|
|
|
|
self.b: float = 4.3
|
|
|
|
|
|
|
|
def fun(self):
|
|
|
|
self.b = self.b + 3.0
|
2021-08-31 15:41:48 +08:00
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
def foo(a: float):
|
|
|
|
a + 1.0
|
2021-08-31 17:40:38 +08:00
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class C(B):
|
|
|
|
def __init__(self):
|
2021-09-07 00:20:40 +08:00
|
|
|
self.c: int32 = 4
|
2021-08-31 17:40:38 +08:00
|
|
|
self.a: bool = True
|
2021-08-31 15:22:45 +08:00
|
|
|
"}
|
2021-09-08 19:27:32 +08:00
|
|
|
];
|
|
|
|
"register"
|
2021-08-31 15:22:45 +08:00
|
|
|
)]
|
|
|
|
fn test_simple_register(source: Vec<&str>) {
|
|
|
|
let mut composer = TopLevelComposer::new();
|
|
|
|
|
|
|
|
for s in source {
|
|
|
|
let ast = parse_program(s).unwrap();
|
|
|
|
let ast = ast[0].clone();
|
|
|
|
|
|
|
|
composer.register_top_level(ast, None).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 19:23:04 +08:00
|
|
|
|
|
|
|
#[test_case(
|
|
|
|
vec![
|
|
|
|
indoc! {"
|
2021-09-07 00:20:40 +08:00
|
|
|
def fun(a: int32) -> int32:
|
2021-09-06 19:23:04 +08:00
|
|
|
return a
|
|
|
|
"},
|
2021-09-07 00:20:40 +08:00
|
|
|
indoc! {"
|
|
|
|
def foo(a: float):
|
|
|
|
a + 1.0
|
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
def f(b: int64) -> int32:
|
|
|
|
return 3
|
|
|
|
"},
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
"fn[[a=0], 0]",
|
|
|
|
"fn[[a=2], 4]",
|
|
|
|
"fn[[b=1], 0]",
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
"fun",
|
|
|
|
"foo",
|
|
|
|
"f"
|
2021-09-08 19:27:32 +08:00
|
|
|
];
|
|
|
|
"function compose"
|
2021-09-06 19:23:04 +08:00
|
|
|
)]
|
2021-09-07 00:20:40 +08:00
|
|
|
fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&str>) {
|
2021-09-06 19:23:04 +08:00
|
|
|
let mut composer = TopLevelComposer::new();
|
2021-09-08 02:27:12 +08:00
|
|
|
|
2021-09-08 19:27:32 +08:00
|
|
|
let internal_resolver = Arc::new(ResolverInternal {
|
2021-09-06 19:23:04 +08:00
|
|
|
id_to_def: Default::default(),
|
|
|
|
id_to_type: Default::default(),
|
|
|
|
class_names: Default::default(),
|
2021-09-08 19:27:32 +08:00
|
|
|
});
|
2021-09-08 19:45:36 +08:00
|
|
|
let resolver = Arc::new(
|
|
|
|
Box::new(Resolver(internal_resolver.clone())) as Box<dyn SymbolResolver + Send + Sync>
|
|
|
|
);
|
2021-09-06 19:23:04 +08:00
|
|
|
|
|
|
|
for s in source {
|
|
|
|
let ast = parse_program(s).unwrap();
|
|
|
|
let ast = ast[0].clone();
|
|
|
|
|
|
|
|
let (id, def_id) = composer.register_top_level(ast, Some(resolver.clone())).unwrap();
|
2021-09-08 19:27:32 +08:00
|
|
|
internal_resolver.add_id_def(id, def_id);
|
2021-09-06 19:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
composer.start_analysis().unwrap();
|
2021-09-08 02:27:12 +08:00
|
|
|
|
|
|
|
for (i, (def, _)) in composer.definition_ast_list.iter().skip(5).enumerate() {
|
2021-09-07 00:20:40 +08:00
|
|
|
let def = &*def.read();
|
|
|
|
if let TopLevelDef::Function { signature, name, .. } = def {
|
2021-09-08 02:27:12 +08:00
|
|
|
let ty_str =
|
|
|
|
composer
|
|
|
|
.unifier
|
|
|
|
.stringify(*signature, &mut |id| id.to_string(), &mut |id| id.to_string());
|
2021-09-07 00:20:40 +08:00
|
|
|
assert_eq!(ty_str, tys[i]);
|
|
|
|
assert_eq!(name, names[i]);
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 19:23:04 +08:00
|
|
|
}
|
2021-09-07 10:03:31 +08:00
|
|
|
|
|
|
|
#[test_case(
|
|
|
|
vec![
|
|
|
|
indoc! {"
|
2021-09-07 17:30:15 +08:00
|
|
|
class A():
|
|
|
|
def __init__(self):
|
|
|
|
self.a: int32 = 3
|
|
|
|
def fun(self, b: B):
|
2021-09-07 10:03:31 +08:00
|
|
|
pass
|
2021-09-08 02:27:12 +08:00
|
|
|
def foo(self, a: T, b: V):
|
|
|
|
pass
|
2021-09-07 10:03:31 +08:00
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class B(C):
|
2021-09-07 17:30:15 +08:00
|
|
|
def __init__(self):
|
2021-09-07 10:03:31 +08:00
|
|
|
pass
|
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class C(A):
|
2021-09-07 17:30:15 +08:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def fun(self, b: B):
|
|
|
|
a = 1
|
2021-09-07 10:03:31 +08:00
|
|
|
pass
|
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
def foo(a: A):
|
|
|
|
pass
|
|
|
|
"},
|
2021-09-08 02:27:12 +08:00
|
|
|
indoc! {"
|
|
|
|
def ff(a: T) -> V:
|
|
|
|
pass
|
|
|
|
"}
|
2021-09-07 17:30:15 +08:00
|
|
|
],
|
|
|
|
vec![
|
|
|
|
indoc! {"5: Class {
|
|
|
|
name: \"A\",
|
|
|
|
def_id: DefinitionId(5),
|
|
|
|
ancestors: [CustomClassKind { id: DefinitionId(5), params: [] }],
|
|
|
|
fields: [(\"a\", \"0\")],
|
|
|
|
methods: [(\"__init__\", \"fn[[self=5], 5]\", DefinitionId(6)), (\"fun\", \"fn[[self=5, b=9], 4]\", DefinitionId(7))],
|
|
|
|
type_vars: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"6: Function {
|
|
|
|
name: \"A__init__\",
|
|
|
|
sig: \"fn[[self=5], 5]\",
|
|
|
|
var_id: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"7: Function {
|
|
|
|
name: \"Afun\",
|
|
|
|
sig: \"fn[[self=5, b=9], 4]\",
|
|
|
|
var_id: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"8: Initializer { DefinitionId(5) }"},
|
|
|
|
|
|
|
|
indoc! {"9: Class {
|
|
|
|
name: \"B\",
|
|
|
|
def_id: DefinitionId(9),
|
|
|
|
ancestors: [CustomClassKind { id: DefinitionId(9), params: [] }, CustomClassKind { id: DefinitionId(12), params: [] }, CustomClassKind { id: DefinitionId(5), params: [] }],
|
|
|
|
fields: [(\"a\", \"0\")],
|
|
|
|
methods: [(\"__init__\", \"fn[[self=9], 9]\", DefinitionId(10)), (\"fun\", \"fn[[self=12, b=9], 4]\", DefinitionId(14))],
|
|
|
|
type_vars: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"10: Function {
|
|
|
|
name: \"B__init__\",
|
|
|
|
sig: \"fn[[self=9], 9]\",
|
|
|
|
var_id: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"11: Initializer { DefinitionId(9) }"},
|
|
|
|
|
|
|
|
indoc! {"12: Class {
|
|
|
|
name: \"C\",
|
|
|
|
def_id: DefinitionId(12),
|
|
|
|
ancestors: [CustomClassKind { id: DefinitionId(12), params: [] }, CustomClassKind { id: DefinitionId(5), params: [] }],
|
|
|
|
fields: [(\"a\", \"0\")],
|
|
|
|
methods: [(\"__init__\", \"fn[[self=12], 12]\", DefinitionId(13)), (\"fun\", \"fn[[self=12, b=9], 4]\", DefinitionId(14))],
|
|
|
|
type_vars: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"13: Function {
|
|
|
|
name: \"C__init__\",
|
|
|
|
sig: \"fn[[self=12], 12]\",
|
|
|
|
var_id: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"14: Function {
|
|
|
|
name: \"Cfun\",
|
|
|
|
sig: \"fn[[self=12, b=9], 4]\",
|
|
|
|
var_id: []
|
|
|
|
}"},
|
|
|
|
|
|
|
|
indoc! {"15: Initializer { DefinitionId(12) }"},
|
|
|
|
|
|
|
|
indoc! {"16: Function {
|
|
|
|
name: \"foo\",
|
|
|
|
sig: \"fn[[a=5], 4]\",
|
|
|
|
var_id: []
|
|
|
|
}"},
|
2021-09-08 19:27:32 +08:00
|
|
|
];
|
|
|
|
"simple class compose"
|
|
|
|
)]
|
|
|
|
#[test_case(
|
|
|
|
vec![
|
|
|
|
indoc! {"
|
|
|
|
class Generic_A(Generic[T, V]):
|
|
|
|
def __init__():
|
|
|
|
pass
|
|
|
|
"}
|
|
|
|
],
|
|
|
|
vec![];
|
|
|
|
"generic class"
|
2021-09-07 10:03:31 +08:00
|
|
|
)]
|
2021-09-07 17:30:15 +08:00
|
|
|
fn test_simple_class_analyze(source: Vec<&str>, res: Vec<&str>) {
|
2021-09-07 10:03:31 +08:00
|
|
|
let mut composer = TopLevelComposer::new();
|
2021-09-08 02:27:12 +08:00
|
|
|
|
|
|
|
let tvar_t = composer.unifier.get_fresh_var();
|
|
|
|
let tvar_v = composer
|
|
|
|
.unifier
|
|
|
|
.get_fresh_var_with_range(&[composer.primitives_ty.bool, composer.primitives_ty.int32]);
|
2021-09-08 19:27:32 +08:00
|
|
|
println!("t: {}, {:?}", tvar_t.1, tvar_t.0);
|
|
|
|
println!("v: {}, {:?}\n", tvar_v.1, tvar_v.0);
|
2021-09-08 02:27:12 +08:00
|
|
|
|
2021-09-08 19:27:32 +08:00
|
|
|
let internal_resolver = Arc::new(ResolverInternal {
|
2021-09-07 10:03:31 +08:00
|
|
|
id_to_def: Default::default(),
|
2021-09-08 19:45:36 +08:00
|
|
|
id_to_type: Mutex::new(
|
|
|
|
vec![("T".to_string(), tvar_t.0), ("V".to_string(), tvar_v.0)].into_iter().collect(),
|
|
|
|
),
|
2021-09-07 10:03:31 +08:00
|
|
|
class_names: Default::default(),
|
2021-09-08 19:27:32 +08:00
|
|
|
});
|
2021-09-08 19:45:36 +08:00
|
|
|
let resolver = Arc::new(
|
|
|
|
Box::new(Resolver(internal_resolver.clone())) as Box<dyn SymbolResolver + Send + Sync>
|
|
|
|
);
|
2021-09-07 10:03:31 +08:00
|
|
|
|
|
|
|
for s in source {
|
|
|
|
let ast = parse_program(s).unwrap();
|
|
|
|
let ast = ast[0].clone();
|
|
|
|
|
|
|
|
let (id, def_id) = composer.register_top_level(ast, Some(resolver.clone())).unwrap();
|
2021-09-08 19:27:32 +08:00
|
|
|
internal_resolver.add_id_def(id, def_id);
|
2021-09-07 10:03:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
composer.start_analysis().unwrap();
|
2021-09-08 02:27:12 +08:00
|
|
|
|
2021-09-07 17:30:15 +08:00
|
|
|
// skip 5 to skip primitives
|
|
|
|
for (i, (def, _)) in composer.definition_ast_list.iter().skip(5).enumerate() {
|
|
|
|
let def = &*def.read();
|
2021-09-08 19:27:32 +08:00
|
|
|
// println!(
|
|
|
|
// "{}: {}\n",
|
|
|
|
// i + 5,
|
|
|
|
// def.to_string(
|
|
|
|
// composer.unifier.borrow_mut(),
|
|
|
|
// &mut |id| format!("class{}", id),
|
|
|
|
// &mut |id| format!("tvar{}", id)
|
|
|
|
// )
|
|
|
|
// );
|
2021-09-08 02:27:12 +08:00
|
|
|
// assert_eq!(
|
|
|
|
// format!(
|
|
|
|
// "{}: {}",
|
|
|
|
// i + 5,
|
|
|
|
// def.to_string(
|
|
|
|
// composer.unifier.borrow_mut(),
|
|
|
|
// &mut |id| id.to_string(),
|
|
|
|
// &mut |id| id.to_string()
|
|
|
|
// )
|
|
|
|
// ),
|
|
|
|
// res[i]
|
|
|
|
// )
|
2021-09-07 17:30:15 +08:00
|
|
|
}
|
2021-09-08 02:27:12 +08:00
|
|
|
}
|