2021-11-23 07:32:09 +08:00
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
|
|
use crate::symbol_resolver::SymbolValue;
|
2022-02-21 18:27:46 +08:00
|
|
|
use nac3parser::ast::{Constant, Location};
|
2021-11-23 07:32:09 +08:00
|
|
|
|
2021-08-27 10:21:51 +08:00
|
|
|
use super::*;
|
|
|
|
|
2021-09-07 17:30:15 +08:00
|
|
|
impl TopLevelDef {
|
2022-02-21 18:27:46 +08:00
|
|
|
pub fn to_string(&self, unifier: &mut Unifier) -> String {
|
2021-09-07 17:30:15 +08:00
|
|
|
match self {
|
2022-02-21 18:27:46 +08:00
|
|
|
TopLevelDef::Class { name, ancestors, fields, methods, type_vars, .. } => {
|
2021-09-07 17:30:15 +08:00
|
|
|
let fields_str = fields
|
|
|
|
.iter()
|
2022-02-21 18:27:46 +08:00
|
|
|
.map(|(n, ty, _)| (n.to_string(), unifier.stringify(*ty)))
|
2021-09-07 17:30:15 +08:00
|
|
|
.collect_vec();
|
2021-09-08 02:27:12 +08:00
|
|
|
|
2021-09-07 17:30:15 +08:00
|
|
|
let methods_str = methods
|
|
|
|
.iter()
|
2022-02-21 18:27:46 +08:00
|
|
|
.map(|(n, ty, id)| (n.to_string(), unifier.stringify(*ty), *id))
|
2021-09-07 17:30:15 +08:00
|
|
|
.collect_vec();
|
|
|
|
format!(
|
2021-11-05 20:28:21 +08:00
|
|
|
"Class {{\nname: {:?},\nancestors: {:?},\nfields: {:?},\nmethods: {:?},\ntype_vars: {:?}\n}}",
|
2021-09-07 17:30:15 +08:00
|
|
|
name,
|
2021-11-05 20:28:21 +08:00
|
|
|
ancestors.iter().map(|ancestor| ancestor.stringify(unifier)).collect_vec(),
|
|
|
|
fields_str.iter().map(|(a, _)| a).collect_vec(),
|
|
|
|
methods_str.iter().map(|(a, b, _)| (a, b)).collect_vec(),
|
2022-02-21 17:52:34 +08:00
|
|
|
type_vars.iter().map(|id| unifier.stringify(*id)).collect_vec(),
|
2021-09-07 17:30:15 +08:00
|
|
|
)
|
|
|
|
}
|
2021-09-08 02:27:12 +08:00
|
|
|
TopLevelDef::Function { name, signature, var_id, .. } => format!(
|
|
|
|
"Function {{\nname: {:?},\nsig: {:?},\nvar_id: {:?}\n}}",
|
|
|
|
name,
|
2022-02-21 17:52:34 +08:00
|
|
|
unifier.stringify(*signature),
|
2021-09-09 00:44:56 +08:00
|
|
|
{
|
2021-09-12 13:14:46 +08:00
|
|
|
// preserve the order for debug output and test
|
2021-09-09 00:44:56 +08:00
|
|
|
let mut r = var_id.clone();
|
|
|
|
r.sort_unstable();
|
|
|
|
r
|
|
|
|
}
|
2021-09-08 02:27:12 +08:00
|
|
|
),
|
2021-09-07 17:30:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:21:51 +08:00
|
|
|
impl TopLevelComposer {
|
2023-12-08 17:43:32 +08:00
|
|
|
#[must_use]
|
2021-08-27 10:21:51 +08:00
|
|
|
pub fn make_primitives() -> (PrimitiveStore, Unifier) {
|
|
|
|
let mut unifier = Unifier::new();
|
|
|
|
let int32 = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(0),
|
2022-02-21 18:27:46 +08:00
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
2021-08-27 10:21:51 +08:00
|
|
|
});
|
|
|
|
let int64 = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(1),
|
2022-02-21 18:27:46 +08:00
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
2021-08-27 10:21:51 +08:00
|
|
|
});
|
|
|
|
let float = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(2),
|
2022-02-21 18:27:46 +08:00
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
2021-08-27 10:21:51 +08:00
|
|
|
});
|
|
|
|
let bool = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(3),
|
2022-02-21 18:27:46 +08:00
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
2021-08-27 10:21:51 +08:00
|
|
|
});
|
|
|
|
let none = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(4),
|
2022-02-21 18:27:46 +08:00
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
2021-08-27 10:21:51 +08:00
|
|
|
});
|
2021-10-23 23:53:36 +08:00
|
|
|
let range = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(5),
|
2022-02-21 18:27:46 +08:00
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
2021-10-23 23:53:36 +08:00
|
|
|
});
|
2021-11-02 23:22:37 +08:00
|
|
|
let str = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(6),
|
2022-02-21 18:27:46 +08:00
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
2021-11-02 23:22:37 +08:00
|
|
|
});
|
2022-02-12 21:09:23 +08:00
|
|
|
let exception = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(7),
|
|
|
|
fields: vec![
|
|
|
|
("__name__".into(), (int32, true)),
|
2022-05-29 19:14:00 +08:00
|
|
|
("__file__".into(), (str, true)),
|
2022-02-12 21:09:23 +08:00
|
|
|
("__line__".into(), (int32, true)),
|
|
|
|
("__col__".into(), (int32, true)),
|
|
|
|
("__func__".into(), (str, true)),
|
|
|
|
("__message__".into(), (str, true)),
|
|
|
|
("__param0__".into(), (int64, true)),
|
|
|
|
("__param1__".into(), (int64, true)),
|
|
|
|
("__param2__".into(), (int64, true)),
|
2022-02-21 18:27:46 +08:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect::<HashMap<_, _>>(),
|
|
|
|
params: HashMap::new(),
|
2022-02-12 21:09:23 +08:00
|
|
|
});
|
2022-03-05 03:45:09 +08:00
|
|
|
let uint32 = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(8),
|
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
|
|
|
});
|
|
|
|
let uint64 = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(9),
|
|
|
|
fields: HashMap::new(),
|
|
|
|
params: HashMap::new(),
|
|
|
|
});
|
2022-03-26 15:09:15 +08:00
|
|
|
|
|
|
|
let option_type_var = unifier.get_fresh_var(Some("option_type_var".into()), None);
|
|
|
|
let is_some_type_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: vec![],
|
|
|
|
ret: bool,
|
|
|
|
vars: HashMap::from([(option_type_var.1, option_type_var.0)]),
|
|
|
|
}));
|
|
|
|
let unwrap_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: vec![],
|
|
|
|
ret: option_type_var.0,
|
|
|
|
vars: HashMap::from([(option_type_var.1, option_type_var.0)]),
|
|
|
|
}));
|
|
|
|
let option = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(10),
|
|
|
|
fields: vec![
|
|
|
|
("is_some".into(), (is_some_type_fun_ty, true)),
|
|
|
|
("is_none".into(), (is_some_type_fun_ty, true)),
|
|
|
|
("unwrap".into(), (unwrap_fun_ty, true)),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect::<HashMap<_, _>>(),
|
|
|
|
params: HashMap::from([(option_type_var.1, option_type_var.0)]),
|
|
|
|
});
|
|
|
|
|
|
|
|
let primitives = PrimitiveStore {
|
|
|
|
int32,
|
|
|
|
int64,
|
2023-12-08 17:43:32 +08:00
|
|
|
uint32,
|
|
|
|
uint64,
|
2022-03-26 15:09:15 +08:00
|
|
|
float,
|
|
|
|
bool,
|
|
|
|
none,
|
|
|
|
range,
|
|
|
|
str,
|
|
|
|
exception,
|
|
|
|
option,
|
|
|
|
};
|
2021-08-27 10:21:51 +08:00
|
|
|
crate::typecheck::magic_methods::set_primitives_magic_methods(&primitives, &mut unifier);
|
|
|
|
(primitives, unifier)
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// already include the `definition_id` of itself inside the ancestors vector
|
|
|
|
/// when first registering, the `type_vars`, fields, methods, ancestors are invalid
|
|
|
|
#[must_use]
|
2021-08-27 10:21:51 +08:00
|
|
|
pub fn make_top_level_class_def(
|
|
|
|
index: usize,
|
2021-10-16 18:08:13 +08:00
|
|
|
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
|
2021-09-22 17:19:27 +08:00
|
|
|
name: StrRef,
|
2021-09-20 14:24:16 +08:00
|
|
|
constructor: Option<Type>,
|
2022-02-21 18:27:46 +08:00
|
|
|
loc: Option<Location>,
|
2021-08-27 10:21:51 +08:00
|
|
|
) -> TopLevelDef {
|
|
|
|
TopLevelDef::Class {
|
2021-09-22 17:19:27 +08:00
|
|
|
name,
|
2021-08-27 10:21:51 +08:00
|
|
|
object_id: DefinitionId(index),
|
2023-12-08 17:43:32 +08:00
|
|
|
type_vars: Vec::default(),
|
|
|
|
fields: Vec::default(),
|
|
|
|
methods: Vec::default(),
|
|
|
|
ancestors: Vec::default(),
|
2021-09-19 22:54:06 +08:00
|
|
|
constructor,
|
2021-08-27 10:21:51 +08:00
|
|
|
resolver,
|
2022-02-21 17:52:34 +08:00
|
|
|
loc,
|
2021-08-27 10:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// when first registering, the type is a invalid value
|
2023-12-08 17:43:32 +08:00
|
|
|
#[must_use]
|
2021-08-27 10:21:51 +08:00
|
|
|
pub fn make_top_level_function_def(
|
|
|
|
name: String,
|
2021-09-22 17:19:27 +08:00
|
|
|
simple_name: StrRef,
|
2021-08-27 10:21:51 +08:00
|
|
|
ty: Type,
|
2021-10-16 18:08:13 +08:00
|
|
|
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
|
2022-02-21 18:27:46 +08:00
|
|
|
loc: Option<Location>,
|
2021-08-27 10:21:51 +08:00
|
|
|
) -> TopLevelDef {
|
|
|
|
TopLevelDef::Function {
|
|
|
|
name,
|
2021-09-19 22:54:06 +08:00
|
|
|
simple_name,
|
2021-08-27 10:21:51 +08:00
|
|
|
signature: ty,
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-08-27 10:21:51 +08:00
|
|
|
resolver,
|
2021-09-30 17:07:48 +08:00
|
|
|
codegen_callback: None,
|
2022-02-21 17:52:34 +08:00
|
|
|
loc,
|
2021-08-27 10:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
#[must_use]
|
2021-08-27 10:21:51 +08:00
|
|
|
pub fn make_class_method_name(mut class_name: String, method_name: &str) -> String {
|
2021-09-12 13:14:46 +08:00
|
|
|
class_name.push('.');
|
2021-08-27 10:21:51 +08:00
|
|
|
class_name.push_str(method_name);
|
|
|
|
class_name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_class_method_def_info(
|
2021-09-22 17:19:27 +08:00
|
|
|
class_methods_def: &[(StrRef, Type, DefinitionId)],
|
|
|
|
method_name: StrRef,
|
2023-11-15 17:30:26 +08:00
|
|
|
) -> Result<(Type, DefinitionId), HashSet<String>> {
|
2021-08-27 10:21:51 +08:00
|
|
|
for (name, ty, def_id) in class_methods_def {
|
2021-09-22 17:19:27 +08:00
|
|
|
if name == &method_name {
|
2021-08-27 10:21:51 +08:00
|
|
|
return Ok((*ty, *def_id));
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([format!("no method {method_name} in the current class")]))
|
2021-08-27 10:21:51 +08:00
|
|
|
}
|
|
|
|
|
2021-08-30 12:05:01 +08:00
|
|
|
/// get all base class def id of a class, excluding itself. \
|
|
|
|
/// this function should called only after the direct parent is set
|
|
|
|
/// and before all the ancestors are set
|
|
|
|
/// and when we allow single inheritance \
|
|
|
|
/// the order of the returned list is from the child to the deepest ancestor
|
|
|
|
pub fn get_all_ancestors_helper(
|
|
|
|
child: &TypeAnnotation,
|
2021-08-27 10:21:51 +08:00
|
|
|
temp_def_list: &[Arc<RwLock<TopLevelDef>>],
|
2023-11-15 17:30:26 +08:00
|
|
|
) -> Result<Vec<TypeAnnotation>, HashSet<String>> {
|
2021-08-30 12:05:01 +08:00
|
|
|
let mut result: Vec<TypeAnnotation> = Vec::new();
|
|
|
|
let mut parent = Self::get_parent(child, temp_def_list);
|
|
|
|
while let Some(p) = parent {
|
|
|
|
parent = Self::get_parent(&p, temp_def_list);
|
2021-11-06 23:00:18 +08:00
|
|
|
let p_id = if let TypeAnnotation::CustomClass { id, .. } = &p {
|
2021-08-31 09:57:07 +08:00
|
|
|
*id
|
|
|
|
} else {
|
|
|
|
unreachable!("must be class kind annotation")
|
|
|
|
};
|
|
|
|
// check cycle
|
|
|
|
let no_cycle = result.iter().all(|x| {
|
2021-11-06 23:00:18 +08:00
|
|
|
if let TypeAnnotation::CustomClass { id, .. } = x {
|
2021-08-31 09:57:07 +08:00
|
|
|
id.0 != p_id.0
|
|
|
|
} else {
|
|
|
|
unreachable!("must be class kind annotation")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if no_cycle {
|
|
|
|
result.push(p);
|
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
return Err(HashSet::from(["cyclic inheritance detected".into()]));
|
2021-08-31 09:57:07 +08:00
|
|
|
}
|
2021-08-30 12:05:01 +08:00
|
|
|
}
|
2021-08-31 09:57:07 +08:00
|
|
|
Ok(result)
|
2021-08-30 12:05:01 +08:00
|
|
|
}
|
2021-08-27 10:21:51 +08:00
|
|
|
|
2021-08-30 22:46:50 +08:00
|
|
|
/// should only be called when finding all ancestors, so panic when wrong
|
2021-08-30 12:05:01 +08:00
|
|
|
fn get_parent(
|
|
|
|
child: &TypeAnnotation,
|
|
|
|
temp_def_list: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
) -> Option<TypeAnnotation> {
|
2021-11-06 23:00:18 +08:00
|
|
|
let child_id = if let TypeAnnotation::CustomClass { id, .. } = child {
|
2021-08-30 22:46:50 +08:00
|
|
|
*id
|
|
|
|
} else {
|
|
|
|
unreachable!("should be class type annotation")
|
|
|
|
};
|
2021-08-30 12:05:01 +08:00
|
|
|
let child_def = temp_def_list.get(child_id.0).unwrap();
|
|
|
|
let child_def = child_def.read();
|
|
|
|
if let TopLevelDef::Class { ancestors, .. } = &*child_def {
|
2023-12-08 17:43:32 +08:00
|
|
|
if ancestors.is_empty() {
|
2021-08-30 22:46:50 +08:00
|
|
|
None
|
2023-12-08 17:43:32 +08:00
|
|
|
} else {
|
|
|
|
Some(ancestors[0].clone())
|
2021-08-30 22:46:50 +08:00
|
|
|
}
|
2021-08-27 10:21:51 +08:00
|
|
|
} else {
|
2021-08-30 22:46:50 +08:00
|
|
|
unreachable!("child must be top level class def")
|
2021-08-27 10:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 17:38:07 +08:00
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// get the `var_id` of a given `TVar` type
|
2023-11-15 17:30:26 +08:00
|
|
|
pub fn get_var_id(var_ty: Type, unifier: &mut Unifier) -> Result<u32, HashSet<String>> {
|
2021-08-31 09:57:07 +08:00
|
|
|
if let TypeEnum::TVar { id, .. } = unifier.get_ty(var_ty).as_ref() {
|
|
|
|
Ok(*id)
|
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([
|
|
|
|
"not type var".to_string(),
|
|
|
|
]))
|
2021-08-31 09:57:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_overload_function_type(
|
|
|
|
this: Type,
|
|
|
|
other: Type,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
type_var_to_concrete_def: &HashMap<Type, TypeAnnotation>,
|
|
|
|
) -> bool {
|
|
|
|
let this = unifier.get_ty(this);
|
|
|
|
let this = this.as_ref();
|
2021-08-30 17:38:07 +08:00
|
|
|
let other = unifier.get_ty(other);
|
|
|
|
let other = other.as_ref();
|
2022-02-21 18:27:46 +08:00
|
|
|
if let (
|
|
|
|
TypeEnum::TFunc(FunSignature { args: this_args, ret: this_ret, .. }),
|
|
|
|
TypeEnum::TFunc(FunSignature { args: other_args, ret: other_ret, .. }),
|
|
|
|
) = (this, other)
|
|
|
|
{
|
2021-08-31 09:57:07 +08:00
|
|
|
// check args
|
|
|
|
let args_ok = this_args
|
2021-08-30 17:38:07 +08:00
|
|
|
.iter()
|
2021-08-31 09:57:07 +08:00
|
|
|
.map(|FuncArg { name, ty, .. }| (name, type_var_to_concrete_def.get(ty).unwrap()))
|
|
|
|
.zip(other_args.iter().map(|FuncArg { name, ty, .. }| {
|
|
|
|
(name, type_var_to_concrete_def.get(ty).unwrap())
|
|
|
|
}))
|
|
|
|
.all(|(this, other)| {
|
2021-09-22 17:19:27 +08:00
|
|
|
if this.0 == &"self".into() && this.0 == other.0 {
|
2021-08-31 09:57:07 +08:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
this.0 == other.0
|
|
|
|
&& check_overload_type_annotation_compatible(this.1, other.1, unifier)
|
|
|
|
}
|
|
|
|
});
|
2021-08-30 17:38:07 +08:00
|
|
|
|
2021-08-31 09:57:07 +08:00
|
|
|
// check rets
|
|
|
|
let ret_ok = check_overload_type_annotation_compatible(
|
|
|
|
type_var_to_concrete_def.get(this_ret).unwrap(),
|
|
|
|
type_var_to_concrete_def.get(other_ret).unwrap(),
|
|
|
|
unifier,
|
|
|
|
);
|
2021-08-30 17:38:07 +08:00
|
|
|
|
2021-08-31 09:57:07 +08:00
|
|
|
// return
|
|
|
|
args_ok && ret_ok
|
|
|
|
} else {
|
|
|
|
unreachable!("this function must be called with function type")
|
2021-08-30 17:38:07 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 22:46:50 +08:00
|
|
|
|
2021-08-31 09:57:07 +08:00
|
|
|
pub fn check_overload_field_type(
|
|
|
|
this: Type,
|
|
|
|
other: Type,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
type_var_to_concrete_def: &HashMap<Type, TypeAnnotation>,
|
|
|
|
) -> bool {
|
|
|
|
check_overload_type_annotation_compatible(
|
|
|
|
type_var_to_concrete_def.get(&this).unwrap(),
|
|
|
|
type_var_to_concrete_def.get(&other).unwrap(),
|
|
|
|
unifier,
|
|
|
|
)
|
2021-08-30 22:46:50 +08:00
|
|
|
}
|
2021-09-21 02:48:42 +08:00
|
|
|
|
2023-11-15 17:30:26 +08:00
|
|
|
pub fn get_all_assigned_field(stmts: &[Stmt<()>]) -> Result<HashSet<StrRef>, HashSet<String>> {
|
2021-09-22 17:19:27 +08:00
|
|
|
let mut result = HashSet::new();
|
2021-09-21 02:48:42 +08:00
|
|
|
for s in stmts {
|
|
|
|
match &s.node {
|
|
|
|
ast::StmtKind::AnnAssign { target, .. }
|
|
|
|
if {
|
|
|
|
if let ast::ExprKind::Attribute { value, .. } = &target.node {
|
|
|
|
if let ast::ExprKind::Name { id, .. } = &value.node {
|
2021-09-22 17:19:27 +08:00
|
|
|
id == &"self".into()
|
2021-09-21 02:48:42 +08:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} =>
|
|
|
|
{
|
2023-11-15 17:30:26 +08:00
|
|
|
return Err(HashSet::from([
|
|
|
|
format!(
|
|
|
|
"redundant type annotation for class fields at {}",
|
|
|
|
s.location
|
|
|
|
),
|
|
|
|
]))
|
2021-09-21 02:48:42 +08:00
|
|
|
}
|
|
|
|
ast::StmtKind::Assign { targets, .. } => {
|
|
|
|
for t in targets {
|
|
|
|
if let ast::ExprKind::Attribute { value, attr, .. } = &t.node {
|
|
|
|
if let ast::ExprKind::Name { id, .. } = &value.node {
|
2021-09-22 17:19:27 +08:00
|
|
|
if id == &"self".into() {
|
2021-09-22 17:56:48 +08:00
|
|
|
result.insert(*attr);
|
2021-09-21 02:48:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: do not check for For and While?
|
|
|
|
ast::StmtKind::For { body, orelse, .. }
|
|
|
|
| ast::StmtKind::While { body, orelse, .. } => {
|
|
|
|
result.extend(Self::get_all_assigned_field(body.as_slice())?);
|
|
|
|
result.extend(Self::get_all_assigned_field(orelse.as_slice())?);
|
|
|
|
}
|
|
|
|
ast::StmtKind::If { body, orelse, .. } => {
|
|
|
|
let inited_for_sure = Self::get_all_assigned_field(body.as_slice())?
|
|
|
|
.intersection(&Self::get_all_assigned_field(orelse.as_slice())?)
|
2023-12-08 17:43:32 +08:00
|
|
|
.copied()
|
2021-09-22 17:19:27 +08:00
|
|
|
.collect::<HashSet<_>>();
|
2021-09-21 02:48:42 +08:00
|
|
|
result.extend(inited_for_sure);
|
|
|
|
}
|
|
|
|
ast::StmtKind::Try { body, orelse, finalbody, .. } => {
|
|
|
|
let inited_for_sure = Self::get_all_assigned_field(body.as_slice())?
|
|
|
|
.intersection(&Self::get_all_assigned_field(orelse.as_slice())?)
|
2023-12-08 17:43:32 +08:00
|
|
|
.copied()
|
2021-09-22 17:19:27 +08:00
|
|
|
.collect::<HashSet<_>>();
|
2021-09-21 02:48:42 +08:00
|
|
|
result.extend(inited_for_sure);
|
|
|
|
result.extend(Self::get_all_assigned_field(finalbody.as_slice())?);
|
|
|
|
}
|
|
|
|
ast::StmtKind::With { body, .. } => {
|
|
|
|
result.extend(Self::get_all_assigned_field(body.as_slice())?);
|
|
|
|
}
|
2023-12-08 17:43:32 +08:00
|
|
|
ast::StmtKind::Pass { .. }
|
|
|
|
| ast::StmtKind::Assert { .. }
|
|
|
|
| ast::StmtKind::Expr { .. } => {}
|
2021-09-21 02:48:42 +08:00
|
|
|
|
|
|
|
_ => {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
2021-11-23 07:32:09 +08:00
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
pub fn parse_parameter_default_value(
|
|
|
|
default: &ast::Expr,
|
|
|
|
resolver: &(dyn SymbolResolver + Send + Sync),
|
2023-11-15 17:30:26 +08:00
|
|
|
) -> Result<SymbolValue, HashSet<String>> {
|
2021-11-23 07:32:09 +08:00
|
|
|
parse_parameter_default_value(default, resolver)
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
pub fn check_default_param_type(
|
|
|
|
val: &SymbolValue,
|
|
|
|
ty: &TypeAnnotation,
|
|
|
|
primitive: &PrimitiveStore,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
) -> Result<(), String> {
|
2022-03-30 03:50:34 +08:00
|
|
|
fn is_compatible(
|
|
|
|
found: &TypeAnnotation,
|
|
|
|
expect: &TypeAnnotation,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
primitive: &PrimitiveStore,
|
|
|
|
) -> bool {
|
|
|
|
match (found, expect) {
|
|
|
|
(TypeAnnotation::Primitive(f), TypeAnnotation::Primitive(e)) => {
|
|
|
|
unifier.unioned(*f, *e)
|
2022-03-30 03:14:21 +08:00
|
|
|
}
|
2022-03-30 03:50:34 +08:00
|
|
|
(
|
|
|
|
TypeAnnotation::CustomClass { id: f_id, params: f_param },
|
|
|
|
TypeAnnotation::CustomClass { id: e_id, params: e_param },
|
|
|
|
) => {
|
|
|
|
*f_id == *e_id
|
|
|
|
&& *f_id == primitive.option.get_obj_id(unifier)
|
|
|
|
&& (f_param.is_empty()
|
|
|
|
|| (f_param.len() == 1
|
|
|
|
&& e_param.len() == 1
|
|
|
|
&& is_compatible(&f_param[0], &e_param[0], unifier, primitive)))
|
2022-03-30 03:14:21 +08:00
|
|
|
}
|
2022-03-30 03:50:34 +08:00
|
|
|
(TypeAnnotation::Tuple(f), TypeAnnotation::Tuple(e)) => {
|
|
|
|
f.len() == e.len()
|
|
|
|
&& f.iter()
|
|
|
|
.zip(e.iter())
|
|
|
|
.all(|(f, e)| is_compatible(f, e, unifier, primitive))
|
|
|
|
}
|
|
|
|
_ => false,
|
2022-03-30 03:14:21 +08:00
|
|
|
}
|
2022-03-30 03:50:34 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:56:18 +08:00
|
|
|
let found = val.get_type_annotation(primitive, unifier);
|
2023-12-08 17:43:32 +08:00
|
|
|
if is_compatible(&found, ty, unifier, primitive) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-11-23 07:32:09 +08:00
|
|
|
Err(format!(
|
|
|
|
"incompatible default parameter type, expect {}, found {}",
|
|
|
|
ty.stringify(unifier),
|
2022-03-30 03:50:34 +08:00
|
|
|
found.stringify(unifier),
|
2021-11-23 07:32:09 +08:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
pub fn parse_parameter_default_value(
|
|
|
|
default: &ast::Expr,
|
|
|
|
resolver: &(dyn SymbolResolver + Send + Sync),
|
2023-11-15 17:30:26 +08:00
|
|
|
) -> Result<SymbolValue, HashSet<String>> {
|
|
|
|
fn handle_constant(val: &Constant, loc: &Location) -> Result<SymbolValue, HashSet<String>> {
|
2021-11-23 07:32:09 +08:00
|
|
|
match val {
|
2022-03-08 02:30:04 +08:00
|
|
|
Constant::Int(v) => {
|
|
|
|
if let Ok(v) = (*v).try_into() {
|
|
|
|
Ok(SymbolValue::I32(v))
|
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([format!("integer value out of range at {loc}")]))
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
2022-03-08 02:30:04 +08:00
|
|
|
}
|
2021-11-23 07:32:09 +08:00
|
|
|
Constant::Float(v) => Ok(SymbolValue::Double(*v)),
|
|
|
|
Constant::Bool(v) => Ok(SymbolValue::Bool(*v)),
|
|
|
|
Constant::Tuple(tuple) => Ok(SymbolValue::Tuple(
|
2022-02-21 18:27:46 +08:00
|
|
|
tuple.iter().map(|x| handle_constant(x, loc)).collect::<Result<Vec<_>, _>>()?,
|
2021-11-23 07:32:09 +08:00
|
|
|
)),
|
2023-11-15 17:30:26 +08:00
|
|
|
Constant::None => Err(HashSet::from([
|
|
|
|
format!(
|
|
|
|
"`None` is not supported, use `none` for option type instead ({loc})"
|
|
|
|
),
|
|
|
|
])),
|
2021-11-23 07:32:09 +08:00
|
|
|
_ => unimplemented!("this constant is not supported at {}", loc),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match &default.node {
|
|
|
|
ast::ExprKind::Constant { value, .. } => handle_constant(value, &default.location),
|
2022-03-08 02:30:04 +08:00
|
|
|
ast::ExprKind::Call { func, args, .. } if args.len() == 1 => {
|
2021-11-23 07:32:09 +08:00
|
|
|
match &func.node {
|
2022-03-08 02:30:04 +08:00
|
|
|
ast::ExprKind::Name { id, .. } if *id == "int64".into() => match &args[0].node {
|
|
|
|
ast::ExprKind::Constant { value: Constant::Int(v), .. } => {
|
|
|
|
let v: Result<i64, _> = (*v).try_into();
|
|
|
|
match v {
|
|
|
|
Ok(v) => Ok(SymbolValue::I64(v)),
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("default param value out of range at {}", default.location)
|
|
|
|
])),
|
2022-03-08 02:30:04 +08:00
|
|
|
}
|
|
|
|
}
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("only allow constant integer here at {}", default.location),
|
|
|
|
]))
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
2022-03-08 02:30:04 +08:00
|
|
|
ast::ExprKind::Name { id, .. } if *id == "uint32".into() => match &args[0].node {
|
|
|
|
ast::ExprKind::Constant { value: Constant::Int(v), .. } => {
|
2022-03-05 03:45:09 +08:00
|
|
|
let v: Result<u32, _> = (*v).try_into();
|
|
|
|
match v {
|
|
|
|
Ok(v) => Ok(SymbolValue::U32(v)),
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("default param value out of range at {}", default.location),
|
|
|
|
])),
|
2022-03-05 03:45:09 +08:00
|
|
|
}
|
|
|
|
}
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("only allow constant integer here at {}", default.location),
|
|
|
|
]))
|
2022-03-05 03:45:09 +08:00
|
|
|
}
|
2022-03-08 02:30:04 +08:00
|
|
|
ast::ExprKind::Name { id, .. } if *id == "uint64".into() => match &args[0].node {
|
|
|
|
ast::ExprKind::Constant { value: Constant::Int(v), .. } => {
|
2022-03-05 03:45:09 +08:00
|
|
|
let v: Result<u64, _> = (*v).try_into();
|
|
|
|
match v {
|
|
|
|
Ok(v) => Ok(SymbolValue::U64(v)),
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("default param value out of range at {}", default.location),
|
|
|
|
])),
|
2022-03-05 03:45:09 +08:00
|
|
|
}
|
|
|
|
}
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("only allow constant integer here at {}", default.location),
|
|
|
|
]))
|
2022-03-05 03:45:09 +08:00
|
|
|
}
|
2022-03-30 03:14:21 +08:00
|
|
|
ast::ExprKind::Name { id, .. } if *id == "Some".into() => Ok(
|
|
|
|
SymbolValue::OptionSome(
|
|
|
|
Box::new(parse_parameter_default_value(&args[0], resolver)?)
|
|
|
|
)
|
|
|
|
),
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("unsupported default parameter at {}", default.location),
|
|
|
|
])),
|
2022-03-05 03:45:09 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-23 07:32:09 +08:00
|
|
|
ast::ExprKind::Tuple { elts, .. } => Ok(SymbolValue::Tuple(elts
|
|
|
|
.iter()
|
|
|
|
.map(|x| parse_parameter_default_value(x, resolver))
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
|
|
)),
|
2022-03-30 03:14:21 +08:00
|
|
|
ast::ExprKind::Name { id, .. } if id == &"none".into() => Ok(SymbolValue::OptionNone),
|
2021-11-23 07:32:09 +08:00
|
|
|
ast::ExprKind::Name { id, .. } => {
|
|
|
|
resolver.get_default_param_value(default).ok_or_else(
|
2023-11-15 17:30:26 +08:00
|
|
|
|| HashSet::from([
|
|
|
|
format!(
|
|
|
|
"`{}` cannot be used as a default parameter at {} \
|
|
|
|
(not primitive type, option or tuple / not defined?)",
|
|
|
|
id,
|
|
|
|
default.location
|
|
|
|
),
|
|
|
|
])
|
2021-11-23 07:32:09 +08:00
|
|
|
)
|
|
|
|
}
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!(
|
|
|
|
"unsupported default parameter (not primitive type, option or tuple) at {}",
|
|
|
|
default.location
|
|
|
|
),
|
|
|
|
]))
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
2021-08-27 10:21:51 +08:00
|
|
|
}
|