2024-08-19 14:17:31 +08:00
|
|
|
use crate::PrimitivePythonId;
|
2024-06-14 14:48:29 +08:00
|
|
|
use inkwell::{
|
2024-08-19 14:17:31 +08:00
|
|
|
module::Linkage,
|
2024-06-14 14:48:29 +08:00
|
|
|
types::{BasicType, BasicTypeEnum},
|
|
|
|
values::BasicValueEnum,
|
|
|
|
AddressSpace,
|
|
|
|
};
|
|
|
|
use itertools::Itertools;
|
2021-09-23 19:30:03 +08:00
|
|
|
use nac3core::{
|
2024-06-14 14:48:29 +08:00
|
|
|
codegen::{
|
|
|
|
classes::{NDArrayType, ProxyType},
|
|
|
|
CodeGenContext, CodeGenerator,
|
|
|
|
},
|
2021-11-27 21:29:27 +08:00
|
|
|
symbol_resolver::{StaticValue, SymbolResolver, SymbolValue, ValueEnum},
|
2024-02-27 13:39:05 +08:00
|
|
|
toplevel::{
|
2024-06-12 15:01:01 +08:00
|
|
|
helper::PrimDef,
|
2024-03-26 19:14:56 +08:00
|
|
|
numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
|
2024-06-12 14:45:03 +08:00
|
|
|
DefinitionId, TopLevelDef,
|
2024-02-27 13:39:05 +08:00
|
|
|
},
|
2021-09-23 19:30:03 +08:00
|
|
|
typecheck::{
|
|
|
|
type_inferencer::PrimitiveStore,
|
2024-06-14 09:46:42 +08:00
|
|
|
typedef::{into_var_map, iter_type_vars, Type, TypeEnum, TypeVar, Unifier, VarMap},
|
2021-09-23 19:30:03 +08:00
|
|
|
},
|
|
|
|
};
|
2021-11-27 21:29:27 +08:00
|
|
|
use nac3parser::ast::{self, StrRef};
|
2021-09-30 22:30:54 +08:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2021-10-06 16:07:42 +08:00
|
|
|
use pyo3::{
|
2021-12-05 20:30:03 +08:00
|
|
|
types::{PyDict, PyTuple},
|
2021-10-06 16:07:42 +08:00
|
|
|
PyAny, PyObject, PyResult, Python,
|
|
|
|
};
|
|
|
|
use std::{
|
2023-11-15 17:30:26 +08:00
|
|
|
collections::{HashMap, HashSet},
|
2022-03-24 21:29:46 +08:00
|
|
|
sync::{
|
2024-06-12 14:45:03 +08:00
|
|
|
atomic::{AtomicBool, Ordering::Relaxed},
|
2022-03-24 21:29:46 +08:00
|
|
|
Arc,
|
2024-06-12 14:45:03 +08:00
|
|
|
},
|
2021-10-06 16:07:42 +08:00
|
|
|
};
|
|
|
|
|
2021-12-05 20:30:03 +08:00
|
|
|
pub enum PrimitiveValue {
|
|
|
|
I32(i32),
|
|
|
|
I64(i64),
|
2022-03-05 03:45:09 +08:00
|
|
|
U32(u32),
|
|
|
|
U64(u64),
|
2021-12-05 20:30:03 +08:00
|
|
|
F64(f64),
|
|
|
|
Bool(bool),
|
2024-06-14 10:22:33 +08:00
|
|
|
Str(String),
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
|
|
|
|
2024-04-01 16:22:40 +08:00
|
|
|
/// An entry in the [`DeferredEvaluationStore`], containing the deferred types, a [`PyObject`]
|
|
|
|
/// representing the `__constraints__` of the type variables, and the name of the type to be
|
|
|
|
/// instantiated.
|
|
|
|
type DeferredEvaluationEntry = (Vec<Type>, PyObject, String);
|
|
|
|
|
2022-03-24 21:29:46 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DeferredEvaluationStore {
|
|
|
|
needs_defer: Arc<AtomicBool>,
|
2024-04-01 16:22:40 +08:00
|
|
|
store: Arc<RwLock<Vec<DeferredEvaluationEntry>>>,
|
2022-03-24 21:29:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DeferredEvaluationStore {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
DeferredEvaluationStore {
|
|
|
|
needs_defer: Arc::new(AtomicBool::new(true)),
|
|
|
|
store: Arc::new(RwLock::new(Vec::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
/// A class field as stored in the [`InnerResolver`], represented by the ID and name of the
|
2024-04-01 16:22:40 +08:00
|
|
|
/// associated [`PythonValue`].
|
|
|
|
type ResolverField = (u64, StrRef);
|
|
|
|
/// A class field as stored in Python, represented by the `id()` and [`PyObject`] of the field.
|
|
|
|
type PyFieldHandle = (u64, PyObject);
|
|
|
|
|
2021-11-20 21:15:15 +08:00
|
|
|
pub struct InnerResolver {
|
2021-12-05 20:30:03 +08:00
|
|
|
pub id_to_type: RwLock<HashMap<StrRef, Type>>,
|
|
|
|
pub id_to_def: RwLock<HashMap<StrRef, DefinitionId>>,
|
|
|
|
pub id_to_pyval: RwLock<HashMap<StrRef, (u64, PyObject)>>,
|
|
|
|
pub id_to_primitive: RwLock<HashMap<u64, PrimitiveValue>>,
|
2024-04-01 16:22:40 +08:00
|
|
|
pub field_to_val: RwLock<HashMap<ResolverField, Option<PyFieldHandle>>>,
|
2022-03-25 22:42:01 +08:00
|
|
|
pub global_value_ids: Arc<RwLock<HashMap<u64, PyObject>>>,
|
2021-09-24 09:58:58 +08:00
|
|
|
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>>>,
|
2021-10-06 16:07:42 +08:00
|
|
|
pub primitive_ids: PrimitivePythonId,
|
2021-11-20 21:15:15 +08:00
|
|
|
pub helper: PythonHelper,
|
2022-02-12 21:17:37 +08:00
|
|
|
pub string_store: Arc<RwLock<HashMap<String, i32>>>,
|
2022-03-05 00:26:35 +08:00
|
|
|
pub exception_ids: Arc<RwLock<HashMap<usize, usize>>>,
|
2022-03-24 21:29:46 +08:00
|
|
|
pub deferred_eval_store: DeferredEvaluationStore,
|
2021-09-30 22:30:54 +08:00
|
|
|
// module specific
|
|
|
|
pub name_to_pyid: HashMap<StrRef, u64>,
|
2021-10-06 16:07:42 +08:00
|
|
|
pub module: PyObject,
|
|
|
|
}
|
|
|
|
|
2021-11-20 21:15:15 +08:00
|
|
|
pub struct Resolver(pub Arc<InnerResolver>);
|
|
|
|
|
2021-12-04 22:57:48 +08:00
|
|
|
#[derive(Clone)]
|
2021-11-20 21:15:15 +08:00
|
|
|
pub struct PythonHelper {
|
|
|
|
pub type_fn: PyObject,
|
|
|
|
pub len_fn: PyObject,
|
|
|
|
pub id_fn: PyObject,
|
2021-12-01 22:44:53 +08:00
|
|
|
pub origin_ty_fn: PyObject,
|
|
|
|
pub args_ty_fn: PyObject,
|
2022-02-12 21:17:37 +08:00
|
|
|
pub store_obj: PyObject,
|
|
|
|
pub store_str: PyObject,
|
2021-11-20 21:15:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PythonValue {
|
|
|
|
id: u64,
|
|
|
|
value: PyObject,
|
2022-02-12 21:17:37 +08:00
|
|
|
store_obj: PyObject,
|
2021-11-20 21:15:15 +08:00
|
|
|
resolver: Arc<InnerResolver>,
|
2021-10-06 16:07:42 +08:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:15:15 +08:00
|
|
|
impl StaticValue for PythonValue {
|
|
|
|
fn get_unique_identifier(&self) -> u64 {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn get_const_obj<'ctx>(
|
2022-02-12 21:17:37 +08:00
|
|
|
&self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2022-02-12 21:17:37 +08:00
|
|
|
_: &mut dyn CodeGenerator,
|
|
|
|
) -> BasicValueEnum<'ctx> {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.module.get_global(format!("{}_const", self.id).as_str()).map_or_else(
|
|
|
|
|| {
|
|
|
|
Python::with_gil(|py| -> PyResult<BasicValueEnum<'ctx>> {
|
|
|
|
let id: u32 = self.store_obj.call1(py, (self.value.clone(),))?.extract(py)?;
|
|
|
|
let struct_type = ctx.ctx.struct_type(&[ctx.ctx.i32_type().into()], false);
|
|
|
|
let global = ctx.module.add_global(
|
|
|
|
struct_type,
|
|
|
|
None,
|
|
|
|
format!("{}_const", self.id).as_str(),
|
|
|
|
);
|
|
|
|
global.set_constant(true);
|
2024-08-19 14:17:31 +08:00
|
|
|
// Set linkage of global to private to avoid name collisions
|
|
|
|
global.set_linkage(Linkage::Private);
|
2024-06-12 14:45:03 +08:00
|
|
|
global.set_initializer(&ctx.ctx.const_struct(
|
2024-06-12 15:13:09 +08:00
|
|
|
&[ctx.ctx.i32_type().const_int(u64::from(id), false).into()],
|
2024-06-12 14:45:03 +08:00
|
|
|
false,
|
|
|
|
));
|
|
|
|
Ok(global.as_pointer_value().into())
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|val| val.as_pointer_value().into(),
|
|
|
|
)
|
2022-02-12 21:17:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:15:15 +08:00
|
|
|
fn to_basic_value_enum<'ctx, 'a>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
2021-12-27 22:55:51 +08:00
|
|
|
generator: &mut dyn CodeGenerator,
|
2022-04-10 01:02:52 +08:00
|
|
|
expected_ty: Type,
|
2022-02-28 23:09:14 +08:00
|
|
|
) -> Result<BasicValueEnum<'ctx>, String> {
|
2021-12-05 20:30:03 +08:00
|
|
|
if let Some(val) = self.resolver.id_to_primitive.read().get(&self.id) {
|
2022-02-28 23:09:14 +08:00
|
|
|
return Ok(match val {
|
2021-12-05 20:30:03 +08:00
|
|
|
PrimitiveValue::I32(val) => ctx.ctx.i32_type().const_int(*val as u64, false).into(),
|
|
|
|
PrimitiveValue::I64(val) => ctx.ctx.i64_type().const_int(*val as u64, false).into(),
|
2024-06-12 15:13:09 +08:00
|
|
|
PrimitiveValue::U32(val) => {
|
|
|
|
ctx.ctx.i32_type().const_int(u64::from(*val), false).into()
|
|
|
|
}
|
2023-12-06 11:03:46 +08:00
|
|
|
PrimitiveValue::U64(val) => ctx.ctx.i64_type().const_int(*val, false).into(),
|
2021-12-05 20:30:03 +08:00
|
|
|
PrimitiveValue::F64(val) => ctx.ctx.f64_type().const_float(*val).into(),
|
2024-06-12 15:13:09 +08:00
|
|
|
PrimitiveValue::Bool(val) => {
|
|
|
|
ctx.ctx.i8_type().const_int(u64::from(*val), false).into()
|
|
|
|
}
|
2024-08-13 16:24:17 +08:00
|
|
|
PrimitiveValue::Str(val) => ctx.gen_string(generator, val).into(),
|
2022-02-28 23:09:14 +08:00
|
|
|
});
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
|
|
|
if let Some(global) = ctx.module.get_global(&self.id.to_string()) {
|
2022-02-28 23:09:14 +08:00
|
|
|
return Ok(global.as_pointer_value().into());
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:15:15 +08:00
|
|
|
Python::with_gil(|py| -> PyResult<BasicValueEnum<'ctx>> {
|
|
|
|
self.resolver
|
2022-04-10 01:02:52 +08:00
|
|
|
.get_obj_value(py, self.value.as_ref(py), ctx, generator, expected_ty)
|
2021-11-20 21:15:15 +08:00
|
|
|
.map(Option::unwrap)
|
2024-06-12 14:45:03 +08:00
|
|
|
})
|
|
|
|
.map_err(|e| e.to_string())
|
2021-11-20 21:15:15 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn get_field<'ctx>(
|
2021-11-20 21:15:15 +08:00
|
|
|
&self,
|
|
|
|
name: StrRef,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-11-20 21:15:15 +08:00
|
|
|
) -> Option<ValueEnum<'ctx>> {
|
2021-12-05 20:30:03 +08:00
|
|
|
{
|
|
|
|
let field_to_val = self.resolver.field_to_val.read();
|
|
|
|
field_to_val.get(&(self.id, name)).cloned()
|
|
|
|
}
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
Python::with_gil(|py| -> PyResult<Option<(u64, PyObject)>> {
|
|
|
|
let helper = &self.resolver.helper;
|
|
|
|
let ty = helper.type_fn.call1(py, (&self.value,))?;
|
|
|
|
let ty_id: u64 = helper.id_fn.call1(py, (ty,))?.extract(py)?;
|
2022-04-10 03:31:04 +08:00
|
|
|
// for optimizing unwrap KernelInvariant
|
|
|
|
if ty_id == self.resolver.primitive_ids.option && name == "_nac3_option".into() {
|
2023-09-01 16:56:32 +08:00
|
|
|
let obj = self.value.getattr(py, name.to_string().as_str())?;
|
2022-04-10 03:31:04 +08:00
|
|
|
let id = self.resolver.helper.id_fn.call1(py, (&obj,))?.extract(py)?;
|
2023-12-06 11:14:26 +08:00
|
|
|
return if self.id == self.resolver.primitive_ids.none {
|
|
|
|
Ok(None)
|
2022-04-10 03:31:04 +08:00
|
|
|
} else {
|
2023-12-06 11:14:26 +08:00
|
|
|
Ok(Some((id, obj)))
|
2024-06-12 14:45:03 +08:00
|
|
|
};
|
2022-04-10 03:31:04 +08:00
|
|
|
}
|
2021-12-05 20:30:03 +08:00
|
|
|
let def_id = { *self.resolver.pyid_to_def.read().get(&ty_id).unwrap() };
|
|
|
|
let mut mutable = true;
|
|
|
|
let defs = ctx.top_level.definitions.read();
|
|
|
|
if let TopLevelDef::Class { fields, .. } = &*defs[def_id.0].read() {
|
2023-12-11 15:04:35 +08:00
|
|
|
for (field_name, _, is_mutable) in fields {
|
2021-12-05 20:30:03 +08:00
|
|
|
if field_name == &name {
|
|
|
|
mutable = *is_mutable;
|
|
|
|
break;
|
|
|
|
}
|
2021-11-20 21:15:15 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-05 20:30:03 +08:00
|
|
|
let result = if mutable {
|
|
|
|
None
|
|
|
|
} else {
|
2023-09-01 16:56:32 +08:00
|
|
|
let obj = self.value.getattr(py, name.to_string().as_str())?;
|
2021-12-05 20:30:03 +08:00
|
|
|
let id = self.resolver.helper.id_fn.call1(py, (&obj,))?.extract(py)?;
|
|
|
|
Some((id, obj))
|
|
|
|
};
|
2022-02-21 18:27:46 +08:00
|
|
|
self.resolver.field_to_val.write().insert((self.id, name), result.clone());
|
2021-12-05 20:30:03 +08:00
|
|
|
Ok(result)
|
2021-11-20 21:15:15 +08:00
|
|
|
})
|
2021-12-05 20:30:03 +08:00
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.map(|(id, obj)| {
|
|
|
|
ValueEnum::Static(Arc::new(PythonValue {
|
|
|
|
id,
|
|
|
|
value: obj,
|
2022-02-12 21:17:37 +08:00
|
|
|
store_obj: self.store_obj.clone(),
|
2022-04-10 16:25:15 +08:00
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_tuple_element<'ctx>(&self, index: u32) -> Option<ValueEnum<'ctx>> {
|
|
|
|
Python::with_gil(|py| -> PyResult<Option<(u64, PyObject)>> {
|
|
|
|
let helper = &self.resolver.helper;
|
|
|
|
let ty = helper.type_fn.call1(py, (&self.value,))?;
|
|
|
|
let ty_id: u64 = helper.id_fn.call1(py, (ty,))?.extract(py)?;
|
|
|
|
assert_eq!(ty_id, self.resolver.primitive_ids.tuple);
|
|
|
|
let tup: &PyTuple = self.value.extract(py)?;
|
2022-08-05 16:58:30 +08:00
|
|
|
let elem = tup.get_item(index as usize)?;
|
2022-04-10 16:25:15 +08:00
|
|
|
let id = self.resolver.helper.id_fn.call1(py, (elem,))?.extract(py)?;
|
|
|
|
Ok(Some((id, elem.into())))
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
.map(|(id, obj)| {
|
|
|
|
ValueEnum::Static(Arc::new(PythonValue {
|
|
|
|
id,
|
|
|
|
value: obj,
|
|
|
|
store_obj: self.store_obj.clone(),
|
2021-12-05 20:30:03 +08:00
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
}))
|
2021-11-20 21:15:15 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InnerResolver {
|
2021-10-06 16:07:42 +08:00
|
|
|
fn get_list_elem_type(
|
|
|
|
&self,
|
2021-11-20 21:15:15 +08:00
|
|
|
py: Python,
|
2021-10-06 16:07:42 +08:00
|
|
|
list: &PyAny,
|
|
|
|
len: usize,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
defs: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
primitives: &PrimitiveStore,
|
2022-01-13 03:14:06 +08:00
|
|
|
) -> PyResult<Result<Type, String>> {
|
|
|
|
let mut ty = match self.get_obj_type(py, list.get_item(0)?, unifier, defs, primitives)? {
|
|
|
|
Ok(t) => t,
|
2023-12-11 15:04:35 +08:00
|
|
|
Err(e) => return Ok(Err(format!("type error ({e}) at element #0 of the list"))),
|
2022-01-13 03:14:06 +08:00
|
|
|
};
|
|
|
|
for i in 1..len {
|
|
|
|
let b = match list
|
2021-10-06 16:07:42 +08:00
|
|
|
.get_item(i)
|
2022-02-21 18:27:46 +08:00
|
|
|
.map(|elem| self.get_obj_type(py, elem, unifier, defs, primitives))??
|
|
|
|
{
|
|
|
|
Ok(t) => t,
|
2024-06-12 14:45:03 +08:00
|
|
|
Err(e) => return Ok(Err(format!("type error ({e}) at element #{i} of the list"))),
|
2022-02-21 18:27:46 +08:00
|
|
|
};
|
2022-01-13 03:14:06 +08:00
|
|
|
ty = match unifier.unify(ty, b) {
|
2023-12-11 15:04:35 +08:00
|
|
|
Ok(()) => ty,
|
2022-02-21 18:27:46 +08:00
|
|
|
Err(e) => {
|
|
|
|
return Ok(Err(format!(
|
2023-12-06 11:49:02 +08:00
|
|
|
"inhomogeneous type ({}) at element #{i} of the list",
|
|
|
|
e.to_display(unifier)
|
2022-02-21 18:27:46 +08:00
|
|
|
)))
|
|
|
|
}
|
2022-01-13 03:14:06 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(Ok(ty))
|
2021-10-06 16:07:42 +08:00
|
|
|
}
|
|
|
|
|
2023-10-18 13:40:37 +08:00
|
|
|
/// Handles python objects that represent types themselves,
|
2023-12-06 18:28:44 +08:00
|
|
|
///
|
2023-10-18 13:40:37 +08:00
|
|
|
/// Primitives and class types should be themselves, use `ty_id` to check;
|
2023-12-11 15:04:35 +08:00
|
|
|
/// `TypeVars` and `GenericAlias`(`A[int, bool]`) should use `ty_ty_id` to check.
|
2023-12-06 18:28:44 +08:00
|
|
|
///
|
2023-10-18 13:40:37 +08:00
|
|
|
/// The `bool` value returned indicates whether they are instantiated or not
|
2021-12-01 22:44:53 +08:00
|
|
|
fn get_pyty_obj_type(
|
2021-10-06 16:07:42 +08:00
|
|
|
&self,
|
2021-11-20 21:15:15 +08:00
|
|
|
py: Python,
|
2021-12-01 22:44:53 +08:00
|
|
|
pyty: &PyAny,
|
2021-10-06 16:07:42 +08:00
|
|
|
unifier: &mut Unifier,
|
|
|
|
defs: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
primitives: &PrimitiveStore,
|
2021-12-01 22:44:53 +08:00
|
|
|
) -> PyResult<Result<(Type, bool), String>> {
|
2021-12-05 20:30:03 +08:00
|
|
|
let ty_id: u64 = self.helper.id_fn.call1(py, (pyty,))?.extract(py)?;
|
2022-02-21 18:27:46 +08:00
|
|
|
let ty_ty_id: u64 =
|
|
|
|
self.helper.id_fn.call1(py, (self.helper.type_fn.call1(py, (pyty,))?,))?.extract(py)?;
|
2021-10-06 16:07:42 +08:00
|
|
|
|
|
|
|
if ty_id == self.primitive_ids.int || ty_id == self.primitive_ids.int32 {
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(Ok((primitives.int32, true)))
|
2021-10-06 16:07:42 +08:00
|
|
|
} else if ty_id == self.primitive_ids.int64 {
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(Ok((primitives.int64, true)))
|
2022-03-05 03:45:09 +08:00
|
|
|
} else if ty_id == self.primitive_ids.uint32 {
|
|
|
|
Ok(Ok((primitives.uint32, true)))
|
|
|
|
} else if ty_id == self.primitive_ids.uint64 {
|
|
|
|
Ok(Ok((primitives.uint64, true)))
|
2024-06-18 14:14:12 +08:00
|
|
|
} else if ty_id == self.primitive_ids.bool || ty_id == self.primitive_ids.np_bool_ {
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(Ok((primitives.bool, true)))
|
2024-06-18 14:14:12 +08:00
|
|
|
} else if ty_id == self.primitive_ids.string || ty_id == self.primitive_ids.np_str_ {
|
2024-06-14 10:22:33 +08:00
|
|
|
Ok(Ok((primitives.str, true)))
|
2023-12-06 11:49:02 +08:00
|
|
|
} else if ty_id == self.primitive_ids.float || ty_id == self.primitive_ids.float64 {
|
2022-04-12 10:33:28 +08:00
|
|
|
Ok(Ok((primitives.float, true)))
|
2022-02-12 21:17:37 +08:00
|
|
|
} else if ty_id == self.primitive_ids.exception {
|
|
|
|
Ok(Ok((primitives.exception, true)))
|
2022-02-21 18:27:46 +08:00
|
|
|
} else if ty_id == self.primitive_ids.list {
|
2021-12-01 22:44:53 +08:00
|
|
|
// do not handle type var param and concrete check here
|
2024-07-02 11:05:05 +08:00
|
|
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
|
|
|
&*unifier.get_ty_immutable(primitives.list)
|
|
|
|
{
|
|
|
|
assert_eq!(*obj_id, PrimDef::List.id());
|
|
|
|
iter_type_vars(params).nth(0).unwrap()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
2024-06-13 13:28:39 +08:00
|
|
|
let var = unifier.get_dummy_var().ty;
|
2024-07-02 11:05:05 +08:00
|
|
|
let list = unifier
|
|
|
|
.subst(primitives.list, &into_var_map([TypeVar { id: list_tvar.id, ty: var }]))
|
|
|
|
.unwrap();
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(Ok((list, false)))
|
2023-11-06 18:03:52 +08:00
|
|
|
} else if ty_id == self.primitive_ids.ndarray {
|
|
|
|
// do not handle type var param and concrete check here
|
2024-06-13 13:28:39 +08:00
|
|
|
let var = unifier.get_dummy_var().ty;
|
|
|
|
let ndims = unifier.get_fresh_const_generic_var(primitives.usize(), None, None).ty;
|
2024-02-27 13:39:05 +08:00
|
|
|
let ndarray = make_ndarray_ty(unifier, primitives, Some(var), Some(ndims));
|
2023-11-06 18:03:52 +08:00
|
|
|
Ok(Ok((ndarray, false)))
|
2021-10-06 16:07:42 +08:00
|
|
|
} else if ty_id == self.primitive_ids.tuple {
|
2021-12-01 22:44:53 +08:00
|
|
|
// do not handle type var param and concrete check here
|
2024-07-10 12:27:59 +08:00
|
|
|
Ok(Ok((unifier.add_ty(TypeEnum::TTuple { ty: vec![], is_vararg_ctx: false }), false)))
|
2022-03-26 15:09:15 +08:00
|
|
|
} else if ty_id == self.primitive_ids.option {
|
|
|
|
Ok(Ok((primitives.option, false)))
|
|
|
|
} else if ty_id == self.primitive_ids.none {
|
|
|
|
unreachable!("none cannot be typeid")
|
2023-12-11 15:04:35 +08:00
|
|
|
} else if let Some(def_id) = self.pyid_to_def.read().get(&ty_id).copied() {
|
2021-10-07 15:57:45 +08:00
|
|
|
let def = defs[def_id.0].read();
|
2023-12-12 13:38:27 +08:00
|
|
|
let TopLevelDef::Class { object_id, type_vars, fields, methods, .. } = &*def else {
|
2021-12-01 22:44:53 +08:00
|
|
|
// only object is supported, functions are not supported
|
|
|
|
unreachable!("function type is not supported, should not be queried")
|
2023-12-12 13:38:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// do not handle type var param and concrete check here, and no subst
|
|
|
|
Ok(Ok({
|
|
|
|
let ty = TypeEnum::TObj {
|
|
|
|
obj_id: *object_id,
|
|
|
|
params: type_vars
|
|
|
|
.iter()
|
|
|
|
.map(|x| {
|
|
|
|
let TypeEnum::TVar { id, .. } = &*unifier.get_ty(*x) else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
(*id, *x)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
fields: {
|
|
|
|
let mut res = methods
|
|
|
|
.iter()
|
|
|
|
.map(|(iden, ty, _)| (*iden, (*ty, false)))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
res.extend(fields.clone().into_iter().map(|x| (x.0, (x.1, x.2))));
|
|
|
|
res
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// here also false, later instantiation use python object to check compatible
|
|
|
|
(unifier.add_ty(ty), false)
|
|
|
|
}))
|
2021-12-01 22:44:53 +08:00
|
|
|
} else if ty_ty_id == self.primitive_ids.typevar {
|
2022-02-21 17:52:34 +08:00
|
|
|
let name: &str = pyty.getattr("__name__").unwrap().extract().unwrap();
|
2023-12-06 18:28:44 +08:00
|
|
|
let (constraint_types, is_const_generic) = {
|
2021-12-01 22:44:53 +08:00
|
|
|
let constraints = pyty.getattr("__constraints__").unwrap();
|
|
|
|
let mut result: Vec<Type> = vec![];
|
2022-03-24 21:29:46 +08:00
|
|
|
let needs_defer = self.deferred_eval_store.needs_defer.load(Relaxed);
|
2023-12-06 18:28:44 +08:00
|
|
|
|
|
|
|
let mut is_const_generic = false;
|
2022-08-05 16:58:30 +08:00
|
|
|
for i in 0usize.. {
|
2021-12-01 22:44:53 +08:00
|
|
|
if let Ok(constr) = constraints.get_item(i) {
|
2023-12-06 18:28:44 +08:00
|
|
|
let constr_id: u64 = self.helper.id_fn.call1(py, (constr,))?.extract(py)?;
|
2023-12-11 12:11:41 +08:00
|
|
|
if constr_id == self.primitive_ids.const_generic_marker {
|
2023-12-06 18:28:44 +08:00
|
|
|
is_const_generic = true;
|
2024-06-12 14:45:03 +08:00
|
|
|
continue;
|
2023-12-06 18:28:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !is_const_generic && needs_defer {
|
2024-06-13 13:28:39 +08:00
|
|
|
result.push(unifier.get_dummy_var().ty);
|
2022-03-24 21:29:46 +08:00
|
|
|
} else {
|
|
|
|
result.push({
|
|
|
|
match self.get_pyty_obj_type(py, constr, unifier, defs, primitives)? {
|
|
|
|
Ok((ty, _)) => {
|
|
|
|
if unifier.is_concrete(ty, &[]) {
|
|
|
|
ty
|
|
|
|
} else {
|
|
|
|
return Ok(Err(format!(
|
|
|
|
"the {}th constraint of TypeVar `{}` is not concrete",
|
|
|
|
i + 1,
|
|
|
|
pyty.getattr("__name__")?.extract::<String>()?
|
|
|
|
)));
|
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
2022-03-24 21:29:46 +08:00
|
|
|
Err(err) => return Ok(Err(err)),
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
});
|
2022-03-24 21:29:46 +08:00
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-12-06 18:28:44 +08:00
|
|
|
|
|
|
|
if !is_const_generic && needs_defer {
|
2024-06-12 14:45:03 +08:00
|
|
|
self.deferred_eval_store.store.write().push((
|
|
|
|
result.clone(),
|
|
|
|
constraints.extract()?,
|
|
|
|
pyty.getattr("__name__")?.extract::<String>()?,
|
|
|
|
));
|
2022-03-24 21:29:46 +08:00
|
|
|
}
|
2023-12-06 18:28:44 +08:00
|
|
|
|
|
|
|
(result, is_const_generic)
|
2021-12-01 22:44:53 +08:00
|
|
|
};
|
2023-12-06 18:28:44 +08:00
|
|
|
|
|
|
|
let res = if is_const_generic {
|
|
|
|
if constraint_types.len() != 1 {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Ok(Err(format!(
|
|
|
|
"ConstGeneric expects 1 argument, got {}",
|
|
|
|
constraint_types.len()
|
|
|
|
)));
|
2023-12-06 18:28:44 +08:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:28:39 +08:00
|
|
|
unifier.get_fresh_const_generic_var(constraint_types[0], Some(name.into()), None).ty
|
2023-12-06 18:28:44 +08:00
|
|
|
} else {
|
2024-06-13 13:28:39 +08:00
|
|
|
unifier.get_fresh_var_with_range(&constraint_types, Some(name.into()), None).ty
|
2023-12-06 18:28:44 +08:00
|
|
|
};
|
|
|
|
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(Ok((res, true)))
|
2021-12-05 20:30:03 +08:00
|
|
|
} else if ty_ty_id == self.primitive_ids.generic_alias.0
|
|
|
|
|| ty_ty_id == self.primitive_ids.generic_alias.1
|
|
|
|
{
|
2021-12-01 22:44:53 +08:00
|
|
|
let origin = self.helper.origin_ty_fn.call1(py, (pyty,))?;
|
|
|
|
let args = self.helper.args_ty_fn.call1(py, (pyty,))?;
|
2023-09-01 16:56:32 +08:00
|
|
|
let args: &PyTuple = args.downcast(py)?;
|
2021-12-05 20:30:03 +08:00
|
|
|
let origin_ty =
|
|
|
|
match self.get_pyty_obj_type(py, origin.as_ref(py), unifier, defs, primitives)? {
|
|
|
|
Ok((ty, false)) => ty,
|
|
|
|
Ok((_, true)) => {
|
|
|
|
return Ok(Err("instantiated type does not take type parameters".into()))
|
|
|
|
}
|
|
|
|
Err(err) => return Ok(Err(err)),
|
|
|
|
};
|
2021-12-01 22:44:53 +08:00
|
|
|
|
|
|
|
match &*unifier.get_ty(origin_ty) {
|
2024-07-02 11:05:05 +08:00
|
|
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::List.id() => {
|
2021-12-01 22:44:53 +08:00
|
|
|
if args.len() == 1 {
|
2021-12-05 20:30:03 +08:00
|
|
|
let ty = match self.get_pyty_obj_type(
|
|
|
|
py,
|
2022-08-05 16:58:30 +08:00
|
|
|
args.get_item(0)?,
|
2021-12-05 20:30:03 +08:00
|
|
|
unifier,
|
|
|
|
defs,
|
|
|
|
primitives,
|
|
|
|
)? {
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(ty) => ty,
|
2021-12-05 20:30:03 +08:00
|
|
|
Err(err) => return Ok(Err(err)),
|
2021-12-01 22:44:53 +08:00
|
|
|
};
|
|
|
|
if !unifier.is_concrete(ty.0, &[]) && !ty.1 {
|
2022-01-13 03:14:06 +08:00
|
|
|
return Ok(Err(
|
2022-02-21 18:27:46 +08:00
|
|
|
"type list should take concrete parameters in typevar range".into(),
|
2022-01-13 03:14:06 +08:00
|
|
|
));
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
2024-07-02 11:05:05 +08:00
|
|
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
|
|
|
&*unifier.get_ty_immutable(primitives.list)
|
|
|
|
{
|
|
|
|
assert_eq!(*obj_id, PrimDef::List.id());
|
|
|
|
iter_type_vars(params).nth(0).unwrap()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
let list = unifier
|
|
|
|
.subst(
|
|
|
|
primitives.list,
|
|
|
|
&into_var_map([TypeVar { id: list_tvar.id, ty: ty.0 }]),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
Ok(Ok((list, true)))
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
2021-12-05 20:30:03 +08:00
|
|
|
return Ok(Err(format!(
|
|
|
|
"type list needs exactly 1 type parameters, found {}",
|
|
|
|
args.len()
|
|
|
|
)));
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
2024-06-12 15:01:01 +08:00
|
|
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
2023-11-06 18:03:52 +08:00
|
|
|
if args.len() != 2 {
|
|
|
|
return Ok(Err(format!(
|
|
|
|
"type list needs exactly 2 type parameters, found {}",
|
|
|
|
args.len()
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2024-06-17 14:38:06 +08:00
|
|
|
// npt.NDArray[T] == np.ndarray[Any, np.dtype[T]]
|
|
|
|
let ndarray_dtype_pyty =
|
|
|
|
self.helper.args_ty_fn.call1(py, (args.get_item(1)?,))?;
|
|
|
|
let dtype = ndarray_dtype_pyty.downcast::<PyTuple>(py)?.get_item(0)?;
|
|
|
|
|
|
|
|
let ty = match self.get_pyty_obj_type(py, dtype, unifier, defs, primitives)? {
|
|
|
|
Ok(ty) => ty,
|
|
|
|
Err(err) => return Ok(Err(err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
if !unifier.is_concrete(ty.0, &[]) && !ty.1 {
|
|
|
|
return Ok(Err(
|
|
|
|
"type `ndarray` should take concrete parameters for dtype".into()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Ok((make_ndarray_ty(unifier, primitives, Some(ty.0), None), true)))
|
2023-11-06 18:03:52 +08:00
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
TypeEnum::TTuple { .. } => {
|
|
|
|
let args = match args
|
|
|
|
.iter()
|
|
|
|
.map(|x| self.get_pyty_obj_type(py, x, unifier, defs, primitives))
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Result<Vec<_>, _>>() {
|
|
|
|
Ok(args) if !args.is_empty() => args
|
|
|
|
.into_iter()
|
|
|
|
.map(|(x, check)| if !unifier.is_concrete(x, &[]) && !check {
|
|
|
|
panic!("type tuple should take concrete parameters in type var ranges")
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
Err(err) => return Ok(Err(err)),
|
|
|
|
_ => return Ok(Err("tuple type needs at least 1 type parameters".to_string()))
|
|
|
|
};
|
2024-07-10 12:27:59 +08:00
|
|
|
Ok(Ok((
|
|
|
|
unifier.add_ty(TypeEnum::TTuple { ty: args, is_vararg_ctx: false }),
|
|
|
|
true,
|
|
|
|
)))
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
TypeEnum::TObj { params, obj_id, .. } => {
|
|
|
|
let subst = {
|
|
|
|
if params.len() != args.len() {
|
|
|
|
return Ok(Err(format!(
|
|
|
|
"for class #{}, expect {} type parameters, got {}.",
|
|
|
|
obj_id.0,
|
|
|
|
params.len(),
|
|
|
|
args.len(),
|
2021-12-05 20:30:03 +08:00
|
|
|
)));
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
let args = match args
|
|
|
|
.iter()
|
|
|
|
.map(|x| self.get_pyty_obj_type(py, x, unifier, defs, primitives))
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Result<Vec<_>, _>>() {
|
|
|
|
Ok(args) => args
|
|
|
|
.into_iter()
|
|
|
|
.map(|(x, check)| if !unifier.is_concrete(x, &[]) && !check {
|
|
|
|
panic!("type class should take concrete parameters in type var ranges")
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
Err(err) => return Ok(Err(err)),
|
|
|
|
};
|
|
|
|
params
|
|
|
|
.iter()
|
|
|
|
.zip(args.iter())
|
|
|
|
.map(|((id, _), ty)| (*id, *ty))
|
2024-03-04 23:38:52 +08:00
|
|
|
.collect::<VarMap>()
|
2021-12-01 22:44:53 +08:00
|
|
|
};
|
2022-02-21 18:27:46 +08:00
|
|
|
Ok(Ok((unifier.subst(origin_ty, &subst).unwrap_or(origin_ty), true)))
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
TypeEnum::TVirtual { .. } => {
|
|
|
|
if args.len() == 1 {
|
2021-12-05 20:30:03 +08:00
|
|
|
let ty = match self.get_pyty_obj_type(
|
|
|
|
py,
|
2022-08-05 16:58:30 +08:00
|
|
|
args.get_item(0)?,
|
2021-12-05 20:30:03 +08:00
|
|
|
unifier,
|
|
|
|
defs,
|
|
|
|
primitives,
|
|
|
|
)? {
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(ty) => ty,
|
2021-12-05 20:30:03 +08:00
|
|
|
Err(err) => return Ok(Err(err)),
|
2021-12-01 22:44:53 +08:00
|
|
|
};
|
2023-12-11 15:04:35 +08:00
|
|
|
assert!(
|
|
|
|
unifier.is_concrete(ty.0, &[]) || ty.1,
|
|
|
|
"virtual class should take concrete parameters in type var ranges"
|
|
|
|
);
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(Ok((unifier.add_ty(TypeEnum::TVirtual { ty: ty.0 }), true)))
|
|
|
|
} else {
|
2021-12-05 20:30:03 +08:00
|
|
|
return Ok(Err(format!(
|
|
|
|
"virtual class needs exactly 1 type parameters, found {}",
|
|
|
|
args.len()
|
|
|
|
)));
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-05 20:30:03 +08:00
|
|
|
_ => unimplemented!(),
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
} else if ty_id == self.primitive_ids.virtual_id {
|
2021-12-05 20:30:03 +08:00
|
|
|
Ok(Ok((
|
|
|
|
{
|
2024-06-13 13:28:39 +08:00
|
|
|
let ty = TypeEnum::TVirtual { ty: unifier.get_dummy_var().ty };
|
2021-12-05 20:30:03 +08:00
|
|
|
unifier.add_ty(ty)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)))
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
2022-02-21 18:27:46 +08:00
|
|
|
let str_fn =
|
|
|
|
pyo3::types::PyModule::import(py, "builtins").unwrap().getattr("repr").unwrap();
|
2022-02-21 17:52:34 +08:00
|
|
|
let str_repr: String = str_fn.call1((pyty,)).unwrap().extract().unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
Ok(Err(format!("{str_repr} is not registered with NAC3 (@nac3 decorator missing?)")))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 22:42:01 +08:00
|
|
|
pub fn get_obj_type(
|
2021-12-01 22:44:53 +08:00
|
|
|
&self,
|
|
|
|
py: Python,
|
|
|
|
obj: &PyAny,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
defs: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
primitives: &PrimitiveStore,
|
2022-01-13 03:14:06 +08:00
|
|
|
) -> PyResult<Result<Type, String>> {
|
2021-12-01 22:44:53 +08:00
|
|
|
let ty = self.helper.type_fn.call1(py, (obj,)).unwrap();
|
2022-03-23 03:38:48 +08:00
|
|
|
let py_obj_id: u64 = self.helper.id_fn.call1(py, (obj,))?.extract(py)?;
|
|
|
|
if let Some(ty) = self.pyid_to_type.read().get(&py_obj_id) {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Ok(Ok(*ty));
|
2022-02-28 23:08:42 +08:00
|
|
|
}
|
2022-07-28 19:18:36 +08:00
|
|
|
|
|
|
|
// check if constructor function exists in the methods list
|
|
|
|
let pyid_to_def = self.pyid_to_def.read();
|
2024-06-12 14:45:03 +08:00
|
|
|
let constructor_ty = pyid_to_def.get(&py_obj_id).and_then(|def_id| {
|
|
|
|
defs.iter().find_map(|def| {
|
2024-06-19 17:53:55 +08:00
|
|
|
if let Some(rear_guard) = def.try_read() {
|
|
|
|
if let TopLevelDef::Class { object_id, methods, constructor, .. } = &*rear_guard
|
2024-06-12 14:45:03 +08:00
|
|
|
{
|
2024-06-19 17:53:55 +08:00
|
|
|
if object_id == def_id
|
|
|
|
&& constructor.is_some()
|
|
|
|
&& methods.iter().any(|(s, _, _)| s == &"__init__".into())
|
|
|
|
{
|
|
|
|
return *constructor;
|
|
|
|
}
|
2022-07-28 19:18:36 +08:00
|
|
|
}
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
|
|
|
});
|
2022-07-28 19:18:36 +08:00
|
|
|
|
|
|
|
if let Some(ty) = constructor_ty {
|
|
|
|
self.pyid_to_type.write().insert(py_obj_id, ty);
|
2024-06-12 14:45:03 +08:00
|
|
|
return Ok(Ok(ty));
|
2022-07-28 19:18:36 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 22:44:53 +08:00
|
|
|
let (extracted_ty, inst_check) = match self.get_pyty_obj_type(
|
|
|
|
py,
|
|
|
|
{
|
2021-12-05 20:30:03 +08:00
|
|
|
if [
|
|
|
|
self.primitive_ids.typevar,
|
2021-12-01 22:44:53 +08:00
|
|
|
self.primitive_ids.generic_alias.0,
|
2021-12-05 20:30:03 +08:00
|
|
|
self.primitive_ids.generic_alias.1,
|
|
|
|
]
|
2022-02-21 18:27:46 +08:00
|
|
|
.contains(&self.helper.id_fn.call1(py, (ty.clone(),))?.extract::<u64>(py)?)
|
|
|
|
{
|
2021-12-01 22:44:53 +08:00
|
|
|
obj
|
|
|
|
} else {
|
|
|
|
ty.as_ref(py)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unifier,
|
|
|
|
defs,
|
2021-12-05 20:30:03 +08:00
|
|
|
primitives,
|
2021-12-01 22:44:53 +08:00
|
|
|
)? {
|
|
|
|
Ok(s) => s,
|
2024-06-19 17:53:55 +08:00
|
|
|
Err(e) => {
|
|
|
|
// Allow access to Class Attributes of Classes without having to initialize Objects
|
|
|
|
if self.pyid_to_def.read().contains_key(&py_obj_id) {
|
|
|
|
if let Some(def_id) = self.pyid_to_def.read().get(&py_obj_id).copied() {
|
|
|
|
let def = defs[def_id.0].read();
|
|
|
|
let TopLevelDef::Class { object_id, .. } = &*def else {
|
|
|
|
// only object is supported, functions are not supported
|
|
|
|
unreachable!("function type is not supported, should not be queried")
|
|
|
|
};
|
|
|
|
|
|
|
|
let ty = TypeEnum::TObj {
|
|
|
|
obj_id: *object_id,
|
|
|
|
params: VarMap::new(),
|
|
|
|
fields: HashMap::new(),
|
|
|
|
};
|
|
|
|
(unifier.add_ty(ty), true)
|
|
|
|
} else {
|
|
|
|
return Ok(Err(e));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Err(e));
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
};
|
2022-03-18 01:07:44 +08:00
|
|
|
match (&*unifier.get_ty(extracted_ty), inst_check) {
|
2023-11-06 18:03:52 +08:00
|
|
|
// do the instantiation for these four types
|
2024-07-02 11:05:05 +08:00
|
|
|
(TypeEnum::TObj { obj_id, params, .. }, false) if *obj_id == PrimDef::List.id() => {
|
|
|
|
let ty = iter_type_vars(params).nth(0).unwrap().ty;
|
2021-12-01 22:44:53 +08:00
|
|
|
let len: usize = self.helper.len_fn.call1(py, (obj,))?.extract(py)?;
|
|
|
|
if len == 0 {
|
|
|
|
assert!(matches!(
|
2024-07-02 11:05:05 +08:00
|
|
|
&*unifier.get_ty(ty),
|
2022-02-21 17:52:34 +08:00
|
|
|
TypeEnum::TVar { fields: None, range, .. }
|
|
|
|
if range.is_empty()
|
2021-12-01 22:44:53 +08:00
|
|
|
));
|
2022-01-13 03:14:06 +08:00
|
|
|
Ok(Ok(extracted_ty))
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
2021-12-05 20:30:03 +08:00
|
|
|
let actual_ty =
|
|
|
|
self.get_list_elem_type(py, obj, len, unifier, defs, primitives)?;
|
2022-01-13 03:14:06 +08:00
|
|
|
match actual_ty {
|
2024-07-02 11:05:05 +08:00
|
|
|
Ok(t) => match unifier.unify(ty, t) {
|
|
|
|
Ok(()) => {
|
|
|
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
|
|
|
&*unifier.get_ty_immutable(primitives.list)
|
|
|
|
{
|
|
|
|
assert_eq!(*obj_id, PrimDef::List.id());
|
|
|
|
iter_type_vars(params).nth(0).unwrap()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
let list = unifier
|
|
|
|
.subst(
|
|
|
|
primitives.list,
|
|
|
|
&into_var_map([TypeVar { id: list_tvar.id, ty }]),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
Ok(Ok(list))
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
Err(e) => Ok(Err(format!(
|
|
|
|
"type error ({}) for the list",
|
2023-12-06 11:49:02 +08:00
|
|
|
e.to_display(unifier)
|
2022-02-21 18:27:46 +08:00
|
|
|
))),
|
|
|
|
},
|
2022-01-13 03:14:06 +08:00
|
|
|
Err(e) => Ok(Err(e)),
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 15:01:01 +08:00
|
|
|
(TypeEnum::TObj { obj_id, .. }, false) if *obj_id == PrimDef::NDArray.id() => {
|
2024-03-26 19:14:56 +08:00
|
|
|
let (ty, ndims) = unpack_ndarray_var_tys(unifier, extracted_ty);
|
2024-06-14 14:48:29 +08:00
|
|
|
let len: usize = obj.getattr("ndim")?.extract()?;
|
2023-11-06 18:03:52 +08:00
|
|
|
if len == 0 {
|
|
|
|
assert!(matches!(
|
2024-02-27 13:39:05 +08:00
|
|
|
&*unifier.get_ty(ty),
|
2023-11-06 18:03:52 +08:00
|
|
|
TypeEnum::TVar { fields: None, range, .. }
|
|
|
|
if range.is_empty()
|
|
|
|
));
|
|
|
|
Ok(Ok(extracted_ty))
|
|
|
|
} else {
|
2024-06-14 14:48:29 +08:00
|
|
|
let dtype = obj.getattr("dtype")?.getattr("type")?;
|
|
|
|
let dtype_ty = self.get_pyty_obj_type(py, dtype, unifier, defs, primitives)?;
|
|
|
|
match dtype_ty {
|
|
|
|
Ok((t, _)) => match unifier.unify(ty, t) {
|
2024-02-27 13:39:05 +08:00
|
|
|
Ok(()) => {
|
2024-06-12 14:45:03 +08:00
|
|
|
let ndarray_ty =
|
|
|
|
make_ndarray_ty(unifier, primitives, Some(ty), Some(ndims));
|
2024-02-27 13:39:05 +08:00
|
|
|
|
|
|
|
Ok(Ok(ndarray_ty))
|
|
|
|
}
|
2023-11-06 18:03:52 +08:00
|
|
|
Err(e) => Ok(Err(format!(
|
|
|
|
"type error ({}) for the ndarray",
|
2024-02-20 18:07:55 +08:00
|
|
|
e.to_display(unifier),
|
2023-11-06 18:03:52 +08:00
|
|
|
))),
|
|
|
|
},
|
|
|
|
Err(e) => Ok(Err(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
(TypeEnum::TTuple { .. }, false) => {
|
2023-09-01 16:56:32 +08:00
|
|
|
let elements: &PyTuple = obj.downcast()?;
|
2022-01-13 03:14:06 +08:00
|
|
|
let types: Result<Result<Vec<_>, _>, _> = elements
|
2021-10-08 22:43:23 +08:00
|
|
|
.iter()
|
2021-12-01 22:44:53 +08:00
|
|
|
.map(|elem| self.get_obj_type(py, elem, unifier, defs, primitives))
|
2021-10-08 22:43:23 +08:00
|
|
|
.collect();
|
2021-12-01 22:44:53 +08:00
|
|
|
let types = types?;
|
2024-07-10 12:27:59 +08:00
|
|
|
Ok(types.map(|types| {
|
|
|
|
unifier.add_ty(TypeEnum::TTuple { ty: types, is_vararg_ctx: false })
|
|
|
|
}))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
2022-03-26 15:09:15 +08:00
|
|
|
// special handling for option type since its class member layout in python side
|
|
|
|
// is special and cannot be mapped directly to a nac3 type as below
|
|
|
|
(TypeEnum::TObj { obj_id, params, .. }, false)
|
2024-03-27 10:36:02 +08:00
|
|
|
if *obj_id == primitives.option.obj_id(unifier).unwrap() =>
|
2022-03-26 15:09:15 +08:00
|
|
|
{
|
2023-12-11 15:04:35 +08:00
|
|
|
let Ok(field_data) = obj.getattr("_nac3_option") else {
|
|
|
|
unreachable!("cannot be None")
|
2022-03-26 15:09:15 +08:00
|
|
|
};
|
|
|
|
// if is `none`
|
|
|
|
let zelf_id: u64 = self.helper.id_fn.call1(py, (obj,))?.extract(py)?;
|
|
|
|
if zelf_id == self.primitive_ids.none {
|
2023-12-12 13:38:27 +08:00
|
|
|
let ty_enum = unifier.get_ty_immutable(primitives.option);
|
|
|
|
let TypeEnum::TObj { params, .. } = ty_enum.as_ref() else {
|
|
|
|
unreachable!("must be tobj")
|
|
|
|
};
|
|
|
|
|
2024-06-13 16:03:32 +08:00
|
|
|
let var_map = into_var_map(iter_type_vars(params).map(|tvar| {
|
2024-06-13 13:28:39 +08:00
|
|
|
let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty)
|
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2023-12-12 13:38:27 +08:00
|
|
|
|
2024-06-13 13:28:39 +08:00
|
|
|
assert_eq!(*id, tvar.id);
|
|
|
|
let ty = unifier.get_fresh_var_with_range(range, *name, *loc).ty;
|
|
|
|
TypeVar { id: *id, ty }
|
|
|
|
}));
|
2024-06-12 14:45:03 +08:00
|
|
|
return Ok(Ok(unifier.subst(primitives.option, &var_map).unwrap()));
|
2022-03-26 15:09:15 +08:00
|
|
|
}
|
2022-03-25 22:42:01 +08:00
|
|
|
|
2022-03-26 15:09:15 +08:00
|
|
|
let ty = match self.get_obj_type(py, field_data, unifier, defs, primitives)? {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => {
|
|
|
|
return Ok(Err(format!(
|
2023-12-11 15:04:35 +08:00
|
|
|
"error when getting type of the option object ({e})"
|
2022-03-26 15:09:15 +08:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
};
|
2024-03-04 23:38:52 +08:00
|
|
|
let new_var_map: VarMap = params.iter().map(|(id, _)| (*id, ty)).collect();
|
2022-03-26 15:09:15 +08:00
|
|
|
let res = unifier.subst(extracted_ty, &new_var_map).unwrap_or(extracted_ty);
|
|
|
|
Ok(Ok(res))
|
|
|
|
}
|
2022-03-23 00:22:28 +08:00
|
|
|
(TypeEnum::TObj { params, fields, .. }, false) => {
|
2022-03-23 03:38:48 +08:00
|
|
|
self.pyid_to_type.write().insert(py_obj_id, extracted_ty);
|
2024-06-13 16:03:32 +08:00
|
|
|
let var_map = into_var_map(iter_type_vars(params).map(|tvar| {
|
2024-06-13 13:28:39 +08:00
|
|
|
let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty)
|
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2023-12-12 13:38:27 +08:00
|
|
|
|
2024-06-13 13:28:39 +08:00
|
|
|
assert_eq!(*id, tvar.id);
|
|
|
|
let ty = unifier.get_fresh_var_with_range(range, *name, *loc).ty;
|
|
|
|
TypeVar { id: *id, ty }
|
|
|
|
}));
|
2022-03-02 21:39:45 +08:00
|
|
|
let mut instantiate_obj = || {
|
|
|
|
// loop through non-function fields of the class to get the instantiated value
|
2023-12-11 15:04:35 +08:00
|
|
|
for field in fields {
|
2022-03-02 21:39:45 +08:00
|
|
|
let name: String = (*field.0).into();
|
2024-06-12 14:45:03 +08:00
|
|
|
if let TypeEnum::TFunc(..) = &*unifier.get_ty(field.1 .0) {
|
2022-03-02 21:39:45 +08:00
|
|
|
continue;
|
2023-12-11 15:04:35 +08:00
|
|
|
}
|
|
|
|
let field_data = match obj.getattr(name.as_str()) {
|
|
|
|
Ok(d) => d,
|
|
|
|
Err(e) => return Ok(Err(format!("{e}"))),
|
|
|
|
};
|
2024-06-12 14:45:03 +08:00
|
|
|
let ty =
|
|
|
|
match self.get_obj_type(py, field_data, unifier, defs, primitives)? {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => {
|
|
|
|
return Ok(Err(format!(
|
|
|
|
"error when getting type of field `{name}` ({e})"
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let field_ty = unifier.subst(field.1 .0, &var_map).unwrap_or(field.1 .0);
|
2023-12-11 15:04:35 +08:00
|
|
|
if let Err(e) = unifier.unify(ty, field_ty) {
|
|
|
|
// field type mismatch
|
|
|
|
return Ok(Err(format!(
|
|
|
|
"error when getting type of field `{name}` ({})",
|
|
|
|
e.to_display(unifier)
|
|
|
|
)));
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
2021-10-07 15:57:45 +08:00
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
for ty in var_map.values() {
|
2022-03-02 21:39:45 +08:00
|
|
|
// must be concrete type
|
|
|
|
if !unifier.is_concrete(*ty, &[]) {
|
|
|
|
return Ok(Err("object is not of concrete type".into()));
|
|
|
|
}
|
2021-10-07 15:57:45 +08:00
|
|
|
}
|
2024-06-12 14:45:03 +08:00
|
|
|
let extracted_ty =
|
|
|
|
unifier.subst(extracted_ty, &var_map).unwrap_or(extracted_ty);
|
2022-03-18 01:07:44 +08:00
|
|
|
Ok(Ok(extracted_ty))
|
2022-03-02 21:39:45 +08:00
|
|
|
};
|
2022-03-23 03:38:48 +08:00
|
|
|
let result = instantiate_obj();
|
|
|
|
// update/remove the cache according to the result
|
|
|
|
match result {
|
|
|
|
Ok(Ok(ty)) => self.pyid_to_type.write().insert(py_obj_id, ty),
|
2024-06-12 14:45:03 +08:00
|
|
|
_ => self.pyid_to_type.write().remove(&py_obj_id),
|
2022-03-23 03:38:48 +08:00
|
|
|
};
|
|
|
|
result
|
2021-10-07 15:57:45 +08:00
|
|
|
}
|
2022-04-12 22:05:59 +08:00
|
|
|
_ => {
|
|
|
|
// check integer bounds
|
|
|
|
if unifier.unioned(extracted_ty, primitives.int32) {
|
|
|
|
obj.extract::<i32>().map_or_else(
|
2023-12-11 15:04:35 +08:00
|
|
|
|_| Ok(Err(format!("{obj} is not in the range of int32"))),
|
2024-06-12 14:45:03 +08:00
|
|
|
|_| Ok(Ok(extracted_ty)),
|
2022-04-12 22:05:59 +08:00
|
|
|
)
|
|
|
|
} else if unifier.unioned(extracted_ty, primitives.int64) {
|
|
|
|
obj.extract::<i64>().map_or_else(
|
2023-12-11 15:04:35 +08:00
|
|
|
|_| Ok(Err(format!("{obj} is not in the range of int64"))),
|
2024-06-12 14:45:03 +08:00
|
|
|
|_| Ok(Ok(extracted_ty)),
|
2022-04-12 22:05:59 +08:00
|
|
|
)
|
|
|
|
} else if unifier.unioned(extracted_ty, primitives.uint32) {
|
|
|
|
obj.extract::<u32>().map_or_else(
|
2023-12-11 15:04:35 +08:00
|
|
|
|_| Ok(Err(format!("{obj} is not in the range of uint32"))),
|
2024-06-12 14:45:03 +08:00
|
|
|
|_| Ok(Ok(extracted_ty)),
|
2022-04-12 22:05:59 +08:00
|
|
|
)
|
|
|
|
} else if unifier.unioned(extracted_ty, primitives.uint64) {
|
|
|
|
obj.extract::<u64>().map_or_else(
|
2023-12-11 15:04:35 +08:00
|
|
|
|_| Ok(Err(format!("{obj} is not in the range of uint64"))),
|
2024-06-12 14:45:03 +08:00
|
|
|
|_| Ok(Ok(extracted_ty)),
|
2022-04-12 22:05:59 +08:00
|
|
|
)
|
|
|
|
} else if unifier.unioned(extracted_ty, primitives.bool) {
|
|
|
|
obj.extract::<bool>().map_or_else(
|
2023-12-11 15:04:35 +08:00
|
|
|
|_| Ok(Err(format!("{obj} is not in the range of bool"))),
|
2024-06-12 14:45:03 +08:00
|
|
|
|_| Ok(Ok(extracted_ty)),
|
2022-04-12 22:05:59 +08:00
|
|
|
)
|
|
|
|
} else if unifier.unioned(extracted_ty, primitives.float) {
|
|
|
|
obj.extract::<f64>().map_or_else(
|
2023-12-11 15:04:35 +08:00
|
|
|
|_| Ok(Err(format!("{obj} is not in the range of float64"))),
|
2024-06-12 14:45:03 +08:00
|
|
|
|_| Ok(Ok(extracted_ty)),
|
2022-04-12 22:05:59 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Ok(Ok(extracted_ty))
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 01:07:44 +08:00
|
|
|
}
|
2021-10-06 16:07:42 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
pub fn get_obj_value<'ctx>(
|
2021-10-06 16:07:42 +08:00
|
|
|
&self,
|
2021-11-20 21:15:15 +08:00
|
|
|
py: Python,
|
2021-10-06 16:07:42 +08:00
|
|
|
obj: &PyAny,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-12-27 22:55:51 +08:00
|
|
|
generator: &mut dyn CodeGenerator,
|
2022-04-10 01:02:52 +08:00
|
|
|
expected_ty: Type,
|
2021-10-06 16:07:42 +08:00
|
|
|
) -> PyResult<Option<BasicValueEnum<'ctx>>> {
|
2022-02-21 18:27:46 +08:00
|
|
|
let ty_id: u64 =
|
|
|
|
self.helper.id_fn.call1(py, (self.helper.type_fn.call1(py, (obj,))?,))?.extract(py)?;
|
2021-12-05 20:30:03 +08:00
|
|
|
let id: u64 = self.helper.id_fn.call1(py, (obj,))?.extract(py)?;
|
2021-10-06 16:07:42 +08:00
|
|
|
if ty_id == self.primitive_ids.int || ty_id == self.primitive_ids.int32 {
|
2022-04-12 22:05:59 +08:00
|
|
|
let val: i32 = obj.extract().unwrap();
|
2022-02-21 18:27:46 +08:00
|
|
|
self.id_to_primitive.write().insert(id, PrimitiveValue::I32(val));
|
2021-10-06 16:07:42 +08:00
|
|
|
Ok(Some(ctx.ctx.i32_type().const_int(val as u64, false).into()))
|
|
|
|
} else if ty_id == self.primitive_ids.int64 {
|
2022-04-12 22:05:59 +08:00
|
|
|
let val: i64 = obj.extract().unwrap();
|
2022-02-21 18:27:46 +08:00
|
|
|
self.id_to_primitive.write().insert(id, PrimitiveValue::I64(val));
|
2021-10-06 16:07:42 +08:00
|
|
|
Ok(Some(ctx.ctx.i64_type().const_int(val as u64, false).into()))
|
2022-03-05 03:45:09 +08:00
|
|
|
} else if ty_id == self.primitive_ids.uint32 {
|
2022-04-12 22:05:59 +08:00
|
|
|
let val: u32 = obj.extract().unwrap();
|
2022-03-05 03:45:09 +08:00
|
|
|
self.id_to_primitive.write().insert(id, PrimitiveValue::U32(val));
|
2024-06-12 15:13:09 +08:00
|
|
|
Ok(Some(ctx.ctx.i32_type().const_int(u64::from(val), false).into()))
|
2022-03-05 03:45:09 +08:00
|
|
|
} else if ty_id == self.primitive_ids.uint64 {
|
2022-04-12 22:05:59 +08:00
|
|
|
let val: u64 = obj.extract().unwrap();
|
2022-03-05 03:45:09 +08:00
|
|
|
self.id_to_primitive.write().insert(id, PrimitiveValue::U64(val));
|
|
|
|
Ok(Some(ctx.ctx.i64_type().const_int(val, false).into()))
|
2024-06-18 14:14:12 +08:00
|
|
|
} else if ty_id == self.primitive_ids.bool || ty_id == self.primitive_ids.np_bool_ {
|
2022-04-12 22:05:59 +08:00
|
|
|
let val: bool = obj.extract().unwrap();
|
2022-02-21 18:27:46 +08:00
|
|
|
self.id_to_primitive.write().insert(id, PrimitiveValue::Bool(val));
|
2024-06-12 15:13:09 +08:00
|
|
|
Ok(Some(ctx.ctx.i8_type().const_int(u64::from(val), false).into()))
|
2024-06-18 14:14:12 +08:00
|
|
|
} else if ty_id == self.primitive_ids.string || ty_id == self.primitive_ids.np_str_ {
|
2024-06-14 10:22:33 +08:00
|
|
|
let val: String = obj.extract().unwrap();
|
|
|
|
self.id_to_primitive.write().insert(id, PrimitiveValue::Str(val.clone()));
|
2024-08-13 16:24:17 +08:00
|
|
|
Ok(Some(ctx.gen_string(generator, val).into()))
|
2022-04-12 10:33:28 +08:00
|
|
|
} else if ty_id == self.primitive_ids.float || ty_id == self.primitive_ids.float64 {
|
2022-04-12 22:05:59 +08:00
|
|
|
let val: f64 = obj.extract().unwrap();
|
2022-02-21 18:27:46 +08:00
|
|
|
self.id_to_primitive.write().insert(id, PrimitiveValue::F64(val));
|
2021-10-06 16:07:42 +08:00
|
|
|
Ok(Some(ctx.ctx.f64_type().const_float(val).into()))
|
|
|
|
} else if ty_id == self.primitive_ids.list {
|
|
|
|
let id_str = id.to_string();
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-12-05 20:30:03 +08:00
|
|
|
if let Some(global) = ctx.module.get_global(&id_str) {
|
|
|
|
return Ok(Some(global.as_pointer_value().into()));
|
|
|
|
}
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-11-20 21:15:15 +08:00
|
|
|
let len: usize = self.helper.len_fn.call1(py, (obj,))?.extract(py)?;
|
2024-07-02 11:05:05 +08:00
|
|
|
let elem_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() {
|
|
|
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
|
|
|
iter_type_vars(params).nth(0).unwrap().ty
|
|
|
|
}
|
|
|
|
_ => unreachable!("must be list"),
|
2021-10-07 15:57:45 +08:00
|
|
|
};
|
2021-12-27 22:55:51 +08:00
|
|
|
let size_t = generator.get_size_type(ctx.ctx);
|
2024-07-15 16:56:55 +08:00
|
|
|
let ty = if len == 0
|
|
|
|
&& matches!(&*ctx.unifier.get_ty_immutable(elem_ty), TypeEnum::TVar { .. })
|
|
|
|
{
|
|
|
|
// The default type for zero-length lists of unknown element type is size_t
|
|
|
|
size_t.into()
|
|
|
|
} else {
|
|
|
|
ctx.get_llvm_type(generator, elem_ty)
|
|
|
|
};
|
2022-02-21 18:27:46 +08:00
|
|
|
let arr_ty = ctx
|
|
|
|
.ctx
|
2023-01-12 19:31:03 +08:00
|
|
|
.struct_type(&[ty.ptr_type(AddressSpace::default()).into(), size_t.into()], false);
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-10-06 16:07:42 +08:00
|
|
|
{
|
2022-03-25 22:42:01 +08:00
|
|
|
if self.global_value_ids.read().contains_key(&id) {
|
2021-10-06 16:07:42 +08:00
|
|
|
let global = ctx.module.get_global(&id_str).unwrap_or_else(|| {
|
2023-01-12 19:31:03 +08:00
|
|
|
ctx.module.add_global(arr_ty, Some(AddressSpace::default()), &id_str)
|
2021-10-06 16:07:42 +08:00
|
|
|
});
|
|
|
|
return Ok(Some(global.as_pointer_value().into()));
|
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
self.global_value_ids.write().insert(id, obj.into());
|
2021-10-06 16:07:42 +08:00
|
|
|
}
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-10-06 16:07:42 +08:00
|
|
|
let arr: Result<Option<Vec<_>>, _> = (0..len)
|
|
|
|
.map(|i| {
|
2024-06-12 14:45:03 +08:00
|
|
|
obj.get_item(i).and_then(|elem| {
|
|
|
|
self.get_obj_value(py, elem, ctx, generator, elem_ty).map_err(|e| {
|
|
|
|
super::CompileError::new_err(format!("Error getting element {i}: {e}"))
|
|
|
|
})
|
|
|
|
})
|
2021-10-06 16:07:42 +08:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let arr = arr?.unwrap();
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-10-06 16:07:42 +08:00
|
|
|
let arr_global = ctx.module.add_global(
|
|
|
|
ty.array_type(len as u32),
|
2023-01-12 19:31:03 +08:00
|
|
|
Some(AddressSpace::default()),
|
2021-10-06 16:07:42 +08:00
|
|
|
&(id_str.clone() + "_"),
|
|
|
|
);
|
|
|
|
let arr: BasicValueEnum = if ty.is_int_type() {
|
2022-02-21 18:27:46 +08:00
|
|
|
let arr: Vec<_> = arr.into_iter().map(BasicValueEnum::into_int_value).collect();
|
2021-10-06 16:07:42 +08:00
|
|
|
ty.into_int_type().const_array(&arr)
|
|
|
|
} else if ty.is_float_type() {
|
2022-02-21 18:27:46 +08:00
|
|
|
let arr: Vec<_> = arr.into_iter().map(BasicValueEnum::into_float_value).collect();
|
2021-10-06 16:07:42 +08:00
|
|
|
ty.into_float_type().const_array(&arr)
|
|
|
|
} else if ty.is_array_type() {
|
2022-02-21 18:27:46 +08:00
|
|
|
let arr: Vec<_> = arr.into_iter().map(BasicValueEnum::into_array_value).collect();
|
2021-10-06 16:07:42 +08:00
|
|
|
ty.into_array_type().const_array(&arr)
|
|
|
|
} else if ty.is_struct_type() {
|
2022-02-21 18:27:46 +08:00
|
|
|
let arr: Vec<_> = arr.into_iter().map(BasicValueEnum::into_struct_value).collect();
|
2021-10-06 16:07:42 +08:00
|
|
|
ty.into_struct_type().const_array(&arr)
|
|
|
|
} else if ty.is_pointer_type() {
|
2022-02-21 18:27:46 +08:00
|
|
|
let arr: Vec<_> = arr.into_iter().map(BasicValueEnum::into_pointer_value).collect();
|
2021-10-06 16:07:42 +08:00
|
|
|
ty.into_pointer_type().const_array(&arr)
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
.into();
|
|
|
|
arr_global.set_initializer(&arr);
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-10-06 16:07:42 +08:00
|
|
|
let val = arr_ty.const_named_struct(&[
|
2024-06-12 14:45:03 +08:00
|
|
|
arr_global
|
|
|
|
.as_pointer_value()
|
|
|
|
.const_cast(ty.ptr_type(AddressSpace::default()))
|
|
|
|
.into(),
|
2021-12-27 22:55:51 +08:00
|
|
|
size_t.const_int(len as u64, false).into(),
|
2021-10-06 16:07:42 +08:00
|
|
|
]);
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2023-01-12 19:31:03 +08:00
|
|
|
let global = ctx.module.add_global(arr_ty, Some(AddressSpace::default()), &id_str);
|
2021-10-06 16:07:42 +08:00
|
|
|
global.set_initializer(&val);
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-10-06 16:07:42 +08:00
|
|
|
Ok(Some(global.as_pointer_value().into()))
|
2023-11-06 18:03:52 +08:00
|
|
|
} else if ty_id == self.primitive_ids.ndarray {
|
2024-06-14 14:48:29 +08:00
|
|
|
let id_str = id.to_string();
|
|
|
|
|
|
|
|
if let Some(global) = ctx.module.get_global(&id_str) {
|
|
|
|
return Ok(Some(global.as_pointer_value().into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let ndarray_ty = if matches!(&*ctx.unifier.get_ty_immutable(expected_ty), TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id())
|
|
|
|
{
|
|
|
|
expected_ty
|
|
|
|
} else {
|
|
|
|
unreachable!("must be ndarray")
|
|
|
|
};
|
|
|
|
let (ndarray_dtype, ndarray_ndims) =
|
|
|
|
unpack_ndarray_var_tys(&mut ctx.unifier, ndarray_ty);
|
|
|
|
|
|
|
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
|
|
let ndarray_dtype_llvm_ty = ctx.get_llvm_type(generator, ndarray_dtype);
|
|
|
|
let ndarray_llvm_ty = NDArrayType::new(generator, ctx.ctx, ndarray_dtype_llvm_ty);
|
|
|
|
|
|
|
|
{
|
|
|
|
if self.global_value_ids.read().contains_key(&id) {
|
|
|
|
let global = ctx.module.get_global(&id_str).unwrap_or_else(|| {
|
|
|
|
ctx.module.add_global(
|
|
|
|
ndarray_llvm_ty.as_underlying_type(),
|
|
|
|
Some(AddressSpace::default()),
|
|
|
|
&id_str,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
return Ok(Some(global.as_pointer_value().into()));
|
|
|
|
}
|
|
|
|
self.global_value_ids.write().insert(id, obj.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndarray_ndims)
|
|
|
|
else {
|
|
|
|
unreachable!("Expected Literal for ndarray_ndims")
|
|
|
|
};
|
|
|
|
|
|
|
|
let ndarray_ndims = if values.len() == 1 {
|
|
|
|
values[0].clone()
|
|
|
|
} else {
|
|
|
|
todo!("Unpacking literal of more than one element unimplemented")
|
|
|
|
};
|
|
|
|
let Ok(ndarray_ndims) = u64::try_from(ndarray_ndims) else {
|
|
|
|
unreachable!("Expected u64 value for ndarray_ndims")
|
|
|
|
};
|
|
|
|
|
|
|
|
// Obtain the shape of the ndarray
|
|
|
|
let shape_tuple: &PyTuple = obj.getattr("shape")?.downcast()?;
|
|
|
|
assert_eq!(shape_tuple.len(), ndarray_ndims as usize);
|
|
|
|
let shape_values: Result<Option<Vec<_>>, _> = shape_tuple
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, elem)| {
|
|
|
|
self.get_obj_value(py, elem, ctx, generator, ctx.primitives.usize()).map_err(
|
|
|
|
|e| super::CompileError::new_err(format!("Error getting element {i}: {e}")),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let shape_values = shape_values?.unwrap();
|
|
|
|
let shape_values = llvm_usize.const_array(
|
|
|
|
&shape_values.into_iter().map(BasicValueEnum::into_int_value).collect_vec(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// create a global for ndarray.shape and initialize it using the shape
|
|
|
|
let shape_global = ctx.module.add_global(
|
|
|
|
llvm_usize.array_type(ndarray_ndims as u32),
|
|
|
|
Some(AddressSpace::default()),
|
|
|
|
&(id_str.clone() + ".shape"),
|
|
|
|
);
|
|
|
|
shape_global.set_initializer(&shape_values);
|
|
|
|
|
|
|
|
// Obtain the (flattened) elements of the ndarray
|
|
|
|
let sz: usize = obj.getattr("size")?.extract()?;
|
|
|
|
let data: Result<Option<Vec<_>>, _> = (0..sz)
|
|
|
|
.map(|i| {
|
|
|
|
obj.getattr("flat")?.get_item(i).and_then(|elem| {
|
|
|
|
self.get_obj_value(py, elem, ctx, generator, ndarray_dtype).map_err(|e| {
|
|
|
|
super::CompileError::new_err(format!("Error getting element {i}: {e}"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let data = data?.unwrap().into_iter();
|
|
|
|
let data = match ndarray_dtype_llvm_ty {
|
|
|
|
BasicTypeEnum::ArrayType(ty) => {
|
|
|
|
ty.const_array(&data.map(BasicValueEnum::into_array_value).collect_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicTypeEnum::FloatType(ty) => {
|
|
|
|
ty.const_array(&data.map(BasicValueEnum::into_float_value).collect_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicTypeEnum::IntType(ty) => {
|
|
|
|
ty.const_array(&data.map(BasicValueEnum::into_int_value).collect_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicTypeEnum::PointerType(ty) => {
|
|
|
|
ty.const_array(&data.map(BasicValueEnum::into_pointer_value).collect_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicTypeEnum::StructType(ty) => {
|
|
|
|
ty.const_array(&data.map(BasicValueEnum::into_struct_value).collect_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicTypeEnum::VectorType(_) => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// create a global for ndarray.data and initialize it using the elements
|
|
|
|
let data_global = ctx.module.add_global(
|
|
|
|
ndarray_dtype_llvm_ty.array_type(sz as u32),
|
|
|
|
Some(AddressSpace::default()),
|
|
|
|
&(id_str.clone() + ".data"),
|
|
|
|
);
|
|
|
|
data_global.set_initializer(&data);
|
|
|
|
|
|
|
|
// create a global for the ndarray object and initialize it
|
|
|
|
let value = ndarray_llvm_ty.as_underlying_type().const_named_struct(&[
|
|
|
|
llvm_usize.const_int(ndarray_ndims, false).into(),
|
|
|
|
shape_global
|
|
|
|
.as_pointer_value()
|
|
|
|
.const_cast(llvm_usize.ptr_type(AddressSpace::default()))
|
|
|
|
.into(),
|
|
|
|
data_global
|
|
|
|
.as_pointer_value()
|
|
|
|
.const_cast(ndarray_dtype_llvm_ty.ptr_type(AddressSpace::default()))
|
|
|
|
.into(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let ndarray = ctx.module.add_global(
|
|
|
|
ndarray_llvm_ty.as_underlying_type(),
|
|
|
|
Some(AddressSpace::default()),
|
|
|
|
&id_str,
|
|
|
|
);
|
|
|
|
ndarray.set_initializer(&value);
|
|
|
|
|
|
|
|
Ok(Some(ndarray.as_pointer_value().into()))
|
2021-10-06 16:07:42 +08:00
|
|
|
} else if ty_id == self.primitive_ids.tuple {
|
2023-12-12 13:38:27 +08:00
|
|
|
let expected_ty_enum = ctx.unifier.get_ty_immutable(expected_ty);
|
2024-07-10 12:27:59 +08:00
|
|
|
let TypeEnum::TTuple { ty, is_vararg_ctx: false } = expected_ty_enum.as_ref() else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
let tup_tys = ty.iter();
|
|
|
|
let elements: &PyTuple = obj.downcast()?;
|
|
|
|
assert_eq!(elements.len(), tup_tys.len());
|
2024-06-12 14:45:03 +08:00
|
|
|
let val: Result<Option<Vec<_>>, _> = elements
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.zip(tup_tys)
|
|
|
|
.map(|((i, elem), ty)| {
|
|
|
|
self.get_obj_value(py, elem, ctx, generator, *ty).map_err(|e| {
|
|
|
|
super::CompileError::new_err(format!("Error getting element {i}: {e}"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
2023-12-12 13:38:27 +08:00
|
|
|
let val = val?.unwrap();
|
|
|
|
let val = ctx.ctx.const_struct(&val, false);
|
|
|
|
Ok(Some(val.into()))
|
2022-03-26 15:09:15 +08:00
|
|
|
} else if ty_id == self.primitive_ids.option {
|
2022-04-10 01:02:52 +08:00
|
|
|
let option_val_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() {
|
|
|
|
TypeEnum::TObj { obj_id, params, .. }
|
2024-03-27 10:36:02 +08:00
|
|
|
if *obj_id == ctx.primitives.option.obj_id(&ctx.unifier).unwrap() =>
|
2022-04-10 01:02:52 +08:00
|
|
|
{
|
|
|
|
*params.iter().next().unwrap().1
|
|
|
|
}
|
2024-06-12 14:45:03 +08:00
|
|
|
_ => unreachable!("must be option type"),
|
2022-04-10 01:02:52 +08:00
|
|
|
};
|
2022-03-26 15:09:15 +08:00
|
|
|
if id == self.primitive_ids.none {
|
2022-04-10 01:02:52 +08:00
|
|
|
// for option type, just a null ptr
|
|
|
|
Ok(Some(
|
|
|
|
ctx.get_llvm_type(generator, option_val_ty)
|
2023-01-12 19:31:03 +08:00
|
|
|
.ptr_type(AddressSpace::default())
|
2022-04-10 01:02:52 +08:00
|
|
|
.const_null()
|
|
|
|
.into(),
|
|
|
|
))
|
2022-03-26 15:09:15 +08:00
|
|
|
} else {
|
|
|
|
match self
|
2024-06-12 14:45:03 +08:00
|
|
|
.get_obj_value(
|
|
|
|
py,
|
|
|
|
obj.getattr("_nac3_option").unwrap(),
|
|
|
|
ctx,
|
|
|
|
generator,
|
|
|
|
option_val_ty,
|
|
|
|
)
|
2022-03-26 15:09:15 +08:00
|
|
|
.map_err(|e| {
|
|
|
|
super::CompileError::new_err(format!(
|
2023-12-11 15:04:35 +08:00
|
|
|
"Error getting value of Option object: {e}"
|
2022-03-26 15:09:15 +08:00
|
|
|
))
|
|
|
|
})? {
|
|
|
|
Some(v) => {
|
2023-12-11 15:04:35 +08:00
|
|
|
let global_str = format!("{id}_option");
|
2022-03-26 15:09:15 +08:00
|
|
|
{
|
2022-03-25 22:42:01 +08:00
|
|
|
if self.global_value_ids.read().contains_key(&id) {
|
2024-06-12 14:45:03 +08:00
|
|
|
let global =
|
|
|
|
ctx.module.get_global(&global_str).unwrap_or_else(|| {
|
|
|
|
ctx.module.add_global(
|
|
|
|
v.get_type(),
|
|
|
|
Some(AddressSpace::default()),
|
|
|
|
&global_str,
|
|
|
|
)
|
|
|
|
});
|
2022-03-26 15:09:15 +08:00
|
|
|
return Ok(Some(global.as_pointer_value().into()));
|
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
self.global_value_ids.write().insert(id, obj.into());
|
2022-03-26 15:09:15 +08:00
|
|
|
}
|
2024-06-12 14:45:03 +08:00
|
|
|
let global = ctx.module.add_global(
|
|
|
|
v.get_type(),
|
|
|
|
Some(AddressSpace::default()),
|
|
|
|
&global_str,
|
|
|
|
);
|
2022-03-26 15:09:15 +08:00
|
|
|
global.set_initializer(&v);
|
|
|
|
Ok(Some(global.as_pointer_value().into()))
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
2022-03-26 15:09:15 +08:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 16:07:42 +08:00
|
|
|
} else {
|
2021-10-07 15:57:45 +08:00
|
|
|
let id_str = id.to_string();
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-12-05 20:30:03 +08:00
|
|
|
if let Some(global) = ctx.module.get_global(&id_str) {
|
|
|
|
return Ok(Some(global.as_pointer_value().into()));
|
|
|
|
}
|
2022-04-10 01:02:52 +08:00
|
|
|
|
2021-10-07 15:57:45 +08:00
|
|
|
let top_level_defs = ctx.top_level.definitions.read();
|
|
|
|
let ty = self
|
2021-11-20 21:15:15 +08:00
|
|
|
.get_obj_type(py, obj, &mut ctx.unifier, &top_level_defs, &ctx.primitives)?
|
2021-10-07 15:57:45 +08:00
|
|
|
.unwrap();
|
|
|
|
let ty = ctx
|
2021-12-27 22:56:07 +08:00
|
|
|
.get_llvm_type(generator, ty)
|
2021-10-07 15:57:45 +08:00
|
|
|
.into_pointer_type()
|
|
|
|
.get_element_type()
|
2022-02-12 21:17:37 +08:00
|
|
|
.into_struct_type();
|
2021-10-07 15:57:45 +08:00
|
|
|
{
|
2022-03-25 22:42:01 +08:00
|
|
|
if self.global_value_ids.read().contains_key(&id) {
|
2021-10-07 15:57:45 +08:00
|
|
|
let global = ctx.module.get_global(&id_str).unwrap_or_else(|| {
|
2023-01-12 19:31:03 +08:00
|
|
|
ctx.module.add_global(ty, Some(AddressSpace::default()), &id_str)
|
2021-10-07 15:57:45 +08:00
|
|
|
});
|
|
|
|
return Ok(Some(global.as_pointer_value().into()));
|
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
self.global_value_ids.write().insert(id, obj.into());
|
2021-10-07 15:57:45 +08:00
|
|
|
}
|
|
|
|
// should be classes
|
2022-02-21 18:27:46 +08:00
|
|
|
let definition =
|
|
|
|
top_level_defs.get(self.pyid_to_def.read().get(&ty_id).unwrap().0).unwrap().read();
|
2023-12-12 13:38:27 +08:00
|
|
|
let TopLevelDef::Class { fields, .. } = &*definition else { unreachable!() };
|
|
|
|
|
|
|
|
let values: Result<Option<Vec<_>>, _> = fields
|
|
|
|
.iter()
|
|
|
|
.map(|(name, ty, _)| {
|
2024-06-12 14:45:03 +08:00
|
|
|
self.get_obj_value(
|
|
|
|
py,
|
|
|
|
obj.getattr(name.to_string().as_str())?,
|
|
|
|
ctx,
|
|
|
|
generator,
|
|
|
|
*ty,
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
super::CompileError::new_err(format!("Error getting field {name}: {e}"))
|
|
|
|
})
|
2023-12-12 13:38:27 +08:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let values = values?;
|
|
|
|
if let Some(values) = values {
|
|
|
|
let val = ty.const_named_struct(&values);
|
|
|
|
let global = ctx.module.get_global(&id_str).unwrap_or_else(|| {
|
|
|
|
ctx.module.add_global(ty, Some(AddressSpace::default()), &id_str)
|
|
|
|
});
|
|
|
|
global.set_initializer(&val);
|
|
|
|
Ok(Some(global.as_pointer_value().into()))
|
2021-10-07 15:57:45 +08:00
|
|
|
} else {
|
2023-12-12 13:38:27 +08:00
|
|
|
Ok(None)
|
2021-10-07 15:57:45 +08:00
|
|
|
}
|
2021-10-06 16:07:42 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-23 07:32:09 +08:00
|
|
|
|
2021-11-27 21:29:27 +08:00
|
|
|
fn get_default_param_obj_value(
|
|
|
|
&self,
|
|
|
|
py: Python,
|
|
|
|
obj: &PyAny,
|
|
|
|
) -> PyResult<Result<SymbolValue, String>> {
|
2022-03-30 03:14:21 +08:00
|
|
|
let id: u64 = self.helper.id_fn.call1(py, (obj,))?.extract(py)?;
|
2022-02-21 18:27:46 +08:00
|
|
|
let ty_id: u64 =
|
|
|
|
self.helper.id_fn.call1(py, (self.helper.type_fn.call1(py, (obj,))?,))?.extract(py)?;
|
|
|
|
Ok(if ty_id == self.primitive_ids.int || ty_id == self.primitive_ids.int32 {
|
|
|
|
let val: i32 = obj.extract()?;
|
|
|
|
Ok(SymbolValue::I32(val))
|
|
|
|
} else if ty_id == self.primitive_ids.int64 {
|
|
|
|
let val: i64 = obj.extract()?;
|
|
|
|
Ok(SymbolValue::I64(val))
|
2022-03-30 03:17:06 +08:00
|
|
|
} else if ty_id == self.primitive_ids.uint32 {
|
|
|
|
let val: u32 = obj.extract()?;
|
|
|
|
Ok(SymbolValue::U32(val))
|
|
|
|
} else if ty_id == self.primitive_ids.uint64 {
|
|
|
|
let val: u64 = obj.extract()?;
|
|
|
|
Ok(SymbolValue::U64(val))
|
2024-06-18 14:14:12 +08:00
|
|
|
} else if ty_id == self.primitive_ids.bool || ty_id == self.primitive_ids.np_bool_ {
|
2022-02-21 18:27:46 +08:00
|
|
|
let val: bool = obj.extract()?;
|
|
|
|
Ok(SymbolValue::Bool(val))
|
2024-06-18 14:14:12 +08:00
|
|
|
} else if ty_id == self.primitive_ids.string || ty_id == self.primitive_ids.np_str_ {
|
2024-06-14 10:22:33 +08:00
|
|
|
let val: String = obj.extract()?;
|
|
|
|
Ok(SymbolValue::Str(val))
|
2022-04-12 10:33:28 +08:00
|
|
|
} else if ty_id == self.primitive_ids.float || ty_id == self.primitive_ids.float64 {
|
2022-02-21 18:27:46 +08:00
|
|
|
let val: f64 = obj.extract()?;
|
|
|
|
Ok(SymbolValue::Double(val))
|
|
|
|
} else if ty_id == self.primitive_ids.tuple {
|
2023-09-01 16:56:32 +08:00
|
|
|
let elements: &PyTuple = obj.downcast()?;
|
2022-02-21 18:27:46 +08:00
|
|
|
let elements: Result<Result<Vec<_>, String>, _> =
|
|
|
|
elements.iter().map(|elem| self.get_default_param_obj_value(py, elem)).collect();
|
2022-03-30 03:14:21 +08:00
|
|
|
elements?.map(SymbolValue::Tuple)
|
|
|
|
} else if ty_id == self.primitive_ids.option {
|
|
|
|
if id == self.primitive_ids.none {
|
|
|
|
Ok(SymbolValue::OptionNone)
|
|
|
|
} else {
|
2024-06-12 14:45:03 +08:00
|
|
|
self.get_default_param_obj_value(py, obj.getattr("_nac3_option").unwrap())?
|
2022-03-30 03:14:21 +08:00
|
|
|
.map(|v| SymbolValue::OptionSome(Box::new(v)))
|
|
|
|
}
|
2022-02-21 18:27:46 +08:00
|
|
|
} else {
|
2022-03-30 03:14:21 +08:00
|
|
|
Err("only primitives values, option and tuple can be default parameter value".into())
|
2022-02-21 18:27:46 +08:00
|
|
|
})
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
2021-09-23 19:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SymbolResolver for Resolver {
|
2021-11-27 21:29:27 +08:00
|
|
|
fn get_default_param_value(&self, expr: &ast::Expr) -> Option<SymbolValue> {
|
2023-12-12 13:38:27 +08:00
|
|
|
let ast::ExprKind::Name { id, .. } = &expr.node else {
|
|
|
|
unreachable!("only for resolving names")
|
|
|
|
};
|
|
|
|
|
|
|
|
Python::with_gil(|py| -> PyResult<Option<SymbolValue>> {
|
|
|
|
let obj: &PyAny = self.0.module.extract(py)?;
|
|
|
|
let members: &PyDict = obj.getattr("__dict__").unwrap().downcast().unwrap();
|
|
|
|
let mut sym_value = None;
|
|
|
|
for (key, val) in members {
|
|
|
|
let key: &str = key.extract()?;
|
|
|
|
if key == id.to_string() {
|
|
|
|
if let Ok(Ok(v)) = self.0.get_default_param_obj_value(py, val) {
|
|
|
|
sym_value = Some(v);
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
2023-12-12 13:38:27 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
2023-12-12 13:38:27 +08:00
|
|
|
Ok(sym_value)
|
2024-06-12 14:45:03 +08:00
|
|
|
})
|
|
|
|
.unwrap()
|
2021-11-23 07:32:09 +08:00
|
|
|
}
|
|
|
|
|
2021-10-06 16:07:42 +08:00
|
|
|
fn get_symbol_type(
|
|
|
|
&self,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
defs: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
primitives: &PrimitiveStore,
|
|
|
|
str: StrRef,
|
2022-01-13 03:14:06 +08:00
|
|
|
) -> Result<Type, String> {
|
2023-12-11 15:04:35 +08:00
|
|
|
if let Some(ty) = {
|
2021-12-05 20:30:03 +08:00
|
|
|
let id_to_type = self.0.id_to_type.read();
|
2023-12-11 15:04:35 +08:00
|
|
|
id_to_type.get(&str).copied()
|
2022-01-13 03:14:06 +08:00
|
|
|
} {
|
2023-12-11 15:04:35 +08:00
|
|
|
Ok(ty)
|
|
|
|
} else {
|
|
|
|
let Some(id) = self.0.name_to_pyid.get(&str) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("cannot find symbol `{str}`"));
|
2023-12-11 15:04:35 +08:00
|
|
|
};
|
|
|
|
let result = if let Some(t) = {
|
|
|
|
let pyid_to_type = self.0.pyid_to_type.read();
|
|
|
|
pyid_to_type.get(id).copied()
|
|
|
|
} {
|
|
|
|
Ok(t)
|
|
|
|
} else {
|
|
|
|
Python::with_gil(|py| -> PyResult<Result<Type, String>> {
|
|
|
|
let obj: &PyAny = self.0.module.extract(py)?;
|
|
|
|
let mut sym_ty = Err(format!("cannot find symbol `{str}`"));
|
|
|
|
let members: &PyDict = obj.getattr("__dict__").unwrap().downcast().unwrap();
|
|
|
|
for (key, val) in members {
|
|
|
|
let key: &str = key.extract()?;
|
|
|
|
if key == str.to_string() {
|
|
|
|
sym_ty = self.0.get_obj_type(py, val, unifier, defs, primitives)?;
|
|
|
|
break;
|
2021-10-06 16:07:42 +08:00
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
}
|
|
|
|
if let Ok(t) = sym_ty {
|
|
|
|
if let TypeEnum::TVar { .. } = &*unifier.get_ty(t) {
|
|
|
|
self.0.pyid_to_type.write().insert(*id, t);
|
2022-01-13 03:14:06 +08:00
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
}
|
|
|
|
Ok(sym_ty)
|
2024-06-12 14:45:03 +08:00
|
|
|
})
|
|
|
|
.unwrap()
|
2023-12-11 15:04:35 +08:00
|
|
|
};
|
|
|
|
result
|
2022-01-13 03:14:06 +08:00
|
|
|
}
|
2021-09-23 19:30:03 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn get_symbol_value<'ctx>(
|
2021-10-06 16:07:42 +08:00
|
|
|
&self,
|
|
|
|
id: StrRef,
|
2023-12-06 11:49:02 +08:00
|
|
|
_: &mut CodeGenContext<'ctx, '_>,
|
2021-11-20 19:50:25 +08:00
|
|
|
) -> Option<ValueEnum<'ctx>> {
|
2021-12-05 20:30:03 +08:00
|
|
|
let sym_value = {
|
|
|
|
let id_to_val = self.0.id_to_pyval.read();
|
|
|
|
id_to_val.get(&id).cloned()
|
|
|
|
}
|
|
|
|
.or_else(|| {
|
|
|
|
Python::with_gil(|py| -> PyResult<Option<(u64, PyObject)>> {
|
|
|
|
let obj: &PyAny = self.0.module.extract(py)?;
|
|
|
|
let mut sym_value: Option<(u64, PyObject)> = None;
|
2023-09-01 16:56:32 +08:00
|
|
|
let members: &PyDict = obj.getattr("__dict__").unwrap().downcast().unwrap();
|
2023-12-11 15:04:35 +08:00
|
|
|
for (key, val) in members {
|
2021-12-05 20:30:03 +08:00
|
|
|
let key: &str = key.extract()?;
|
|
|
|
if key == id.to_string() {
|
|
|
|
let id = self.0.helper.id_fn.call1(py, (val,))?.extract(py)?;
|
|
|
|
sym_value = Some((id, val.extract()?));
|
|
|
|
break;
|
|
|
|
}
|
2021-10-06 16:07:42 +08:00
|
|
|
}
|
2021-12-05 20:30:03 +08:00
|
|
|
if let Some((pyid, val)) = &sym_value {
|
|
|
|
self.0.id_to_pyval.write().insert(id, (*pyid, val.clone()));
|
|
|
|
}
|
|
|
|
Ok(sym_value)
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
});
|
|
|
|
sym_value.map(|(id, v)| {
|
|
|
|
ValueEnum::Static(Arc::new(PythonValue {
|
|
|
|
id,
|
|
|
|
value: v,
|
2022-02-12 21:17:37 +08:00
|
|
|
store_obj: self.0.helper.store_obj.clone(),
|
2021-12-05 20:30:03 +08:00
|
|
|
resolver: self.0.clone(),
|
|
|
|
}))
|
2021-10-06 16:07:42 +08:00
|
|
|
})
|
2021-09-23 19:30:03 +08:00
|
|
|
}
|
|
|
|
|
2023-11-15 17:30:26 +08:00
|
|
|
fn get_identifier_def(&self, id: StrRef) -> Result<DefinitionId, HashSet<String>> {
|
2021-12-05 20:30:03 +08:00
|
|
|
{
|
|
|
|
let id_to_def = self.0.id_to_def.read();
|
2023-12-11 15:04:35 +08:00
|
|
|
id_to_def.get(&id).copied().ok_or_else(String::new)
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|
2022-02-21 17:52:34 +08:00
|
|
|
.or_else(|_| {
|
2024-06-12 14:45:03 +08:00
|
|
|
let py_id = self
|
|
|
|
.0
|
|
|
|
.name_to_pyid
|
|
|
|
.get(&id)
|
|
|
|
.ok_or_else(|| HashSet::from([format!("Undefined identifier `{id}`")]))?;
|
|
|
|
let result = self.0.pyid_to_def.read().get(py_id).copied().ok_or_else(|| {
|
|
|
|
HashSet::from([format!(
|
|
|
|
"`{id}` is not registered with NAC3 (@nac3 decorator missing?)"
|
|
|
|
)])
|
|
|
|
})?;
|
2022-02-21 17:52:34 +08:00
|
|
|
self.0.id_to_def.write().insert(id, result);
|
|
|
|
Ok(result)
|
2021-09-30 22:30:54 +08:00
|
|
|
})
|
2021-09-23 19:30:03 +08:00
|
|
|
}
|
2022-02-12 21:17:37 +08:00
|
|
|
|
|
|
|
fn get_string_id(&self, s: &str) -> i32 {
|
|
|
|
let mut string_store = self.0.string_store.write();
|
|
|
|
if let Some(id) = string_store.get(s) {
|
|
|
|
*id
|
|
|
|
} else {
|
|
|
|
let id = Python::with_gil(|py| -> PyResult<i32> {
|
2022-02-21 18:27:46 +08:00
|
|
|
self.0.helper.store_str.call1(py, (s,))?.extract(py)
|
|
|
|
})
|
|
|
|
.unwrap();
|
2022-02-12 21:17:37 +08:00
|
|
|
string_store.insert(s.into(), id);
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 00:26:35 +08:00
|
|
|
|
2022-03-24 21:29:46 +08:00
|
|
|
fn handle_deferred_eval(
|
|
|
|
&self,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
defs: &[Arc<RwLock<TopLevelDef>>],
|
2024-06-12 14:45:03 +08:00
|
|
|
primitives: &PrimitiveStore,
|
2022-03-24 21:29:46 +08:00
|
|
|
) -> Result<(), String> {
|
|
|
|
// we don't need a lock because this will only be run in a single thread
|
|
|
|
if self.0.deferred_eval_store.needs_defer.load(Relaxed) {
|
|
|
|
self.0.deferred_eval_store.needs_defer.store(false, Relaxed);
|
|
|
|
let store = self.0.deferred_eval_store.store.read();
|
|
|
|
Python::with_gil(|py| -> PyResult<Result<(), String>> {
|
|
|
|
for (variables, constraints, name) in store.iter() {
|
|
|
|
let constraints: &PyAny = constraints.as_ref(py);
|
|
|
|
for (i, var) in variables.iter().enumerate() {
|
|
|
|
if let Ok(constr) = constraints.get_item(i) {
|
|
|
|
match self.0.get_pyty_obj_type(py, constr, unifier, defs, primitives)? {
|
|
|
|
Ok((ty, _)) => {
|
|
|
|
if !unifier.is_concrete(ty, &[]) {
|
|
|
|
return Ok(Err(format!(
|
|
|
|
"the {}th constraint of TypeVar `{}` is not concrete",
|
|
|
|
i + 1,
|
|
|
|
name,
|
|
|
|
)));
|
|
|
|
}
|
2023-12-11 15:04:35 +08:00
|
|
|
unifier.unify(ty, *var).unwrap();
|
2022-03-24 21:29:46 +08:00
|
|
|
}
|
|
|
|
Err(err) => return Ok(Err(err)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Ok(()))
|
2024-06-12 14:45:03 +08:00
|
|
|
})
|
|
|
|
.unwrap()?;
|
2022-03-24 21:29:46 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-05 00:26:35 +08:00
|
|
|
fn get_exception_id(&self, tyid: usize) -> usize {
|
|
|
|
let exn_ids = self.0.exception_ids.read();
|
2023-12-11 15:04:35 +08:00
|
|
|
exn_ids.get(&tyid).copied().unwrap_or(0)
|
2022-03-05 00:26:35 +08:00
|
|
|
}
|
2021-12-05 20:30:03 +08:00
|
|
|
}
|