Compare commits
3 Commits
1a31a50b8a
...
4948395ca2
Author | SHA1 | Date |
---|---|---|
David Mak | 4948395ca2 | |
David Mak | 3db3061d99 | |
David Mak | 51c2175c80 |
|
@ -0,0 +1,24 @@
|
|||
from min_artiq import *
|
||||
from numpy import int32
|
||||
|
||||
|
||||
@nac3
|
||||
class EmptyList:
|
||||
core: KernelInvariant[Core]
|
||||
|
||||
def __init__(self):
|
||||
self.core = Core()
|
||||
|
||||
@rpc
|
||||
def get_empty(self) -> list[int32]:
|
||||
return []
|
||||
|
||||
@kernel
|
||||
def run(self):
|
||||
a: list[int32] = self.get_empty()
|
||||
if a != []:
|
||||
raise ValueError
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
EmptyList().run()
|
|
@ -991,8 +991,15 @@ impl InnerResolver {
|
|||
}
|
||||
_ => unreachable!("must be list"),
|
||||
};
|
||||
let ty = ctx.get_llvm_type(generator, elem_ty);
|
||||
let size_t = generator.get_size_type(ctx.ctx);
|
||||
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)
|
||||
};
|
||||
let arr_ty = ctx
|
||||
.ctx
|
||||
.struct_type(&[ty.ptr_type(AddressSpace::default()).into(), size_t.into()], false);
|
||||
|
|
|
@ -951,9 +951,9 @@ pub fn destructure_range<'ctx>(
|
|||
/// Allocates a List structure with the given [type][ty] and [length]. The name of the resulting
|
||||
/// LLVM value is `{name}.addr`, or `list.addr` if [name] is not specified.
|
||||
///
|
||||
/// Setting `ty` to [`None`] implies that the list does not have a known element type, which is only
|
||||
/// valid for empty lists. It is undefined behavior to generate a sized list with an unknown element
|
||||
/// type.
|
||||
/// Setting `ty` to [`None`] implies that the list is empty **and** does not have a known element
|
||||
/// type, and will therefore set the `list.data` type as `size_t*`. It is undefined behavior to
|
||||
/// generate a sized list with an unknown element type.
|
||||
pub fn allocate_list<'ctx, G: CodeGenerator + ?Sized>(
|
||||
generator: &mut G,
|
||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||
|
|
|
@ -1637,7 +1637,7 @@ pub fn gen_stmt<G: CodeGenerator>(
|
|||
};
|
||||
ctx.make_assert_impl(
|
||||
generator,
|
||||
test.into_int_value(),
|
||||
generator.bool_to_i1(ctx, test.into_int_value()),
|
||||
"0:AssertionError",
|
||||
err_msg,
|
||||
[None, None, None],
|
||||
|
|
|
@ -564,7 +564,7 @@ impl<'a> BuiltinBuilder<'a> {
|
|||
match (&tld, prim.details()) {
|
||||
(
|
||||
TopLevelDef::Class { name, object_id, .. },
|
||||
PrimDefDetails::PrimClass { name: exp_name },
|
||||
PrimDefDetails::PrimClass { name: exp_name, .. },
|
||||
) => {
|
||||
let exp_object_id = prim.id();
|
||||
assert_eq!(name, &exp_name.into());
|
||||
|
|
|
@ -766,6 +766,7 @@ impl TopLevelComposer {
|
|||
let target_ty = get_type_from_type_annotation_kinds(
|
||||
&temp_def_list,
|
||||
unifier,
|
||||
primitives,
|
||||
&def,
|
||||
&mut subst_list,
|
||||
)?;
|
||||
|
@ -936,6 +937,7 @@ impl TopLevelComposer {
|
|||
let ty = get_type_from_type_annotation_kinds(
|
||||
temp_def_list.as_ref(),
|
||||
unifier,
|
||||
primitives_store,
|
||||
&type_annotation,
|
||||
&mut None,
|
||||
)?;
|
||||
|
@ -1002,6 +1004,7 @@ impl TopLevelComposer {
|
|||
get_type_from_type_annotation_kinds(
|
||||
&temp_def_list,
|
||||
unifier,
|
||||
primitives_store,
|
||||
&return_ty_annotation,
|
||||
&mut None,
|
||||
)?
|
||||
|
@ -1622,6 +1625,7 @@ impl TopLevelComposer {
|
|||
let self_type = get_type_from_type_annotation_kinds(
|
||||
&def_list,
|
||||
unifier,
|
||||
primitives_ty,
|
||||
&make_self_type_annotation(type_vars, *object_id),
|
||||
&mut None,
|
||||
)?;
|
||||
|
@ -1803,7 +1807,11 @@ impl TopLevelComposer {
|
|||
|
||||
let ty_ann = make_self_type_annotation(type_vars, *class_id);
|
||||
let self_ty = get_type_from_type_annotation_kinds(
|
||||
&def_list, unifier, &ty_ann, &mut None,
|
||||
&def_list,
|
||||
unifier,
|
||||
primitives_ty,
|
||||
&ty_ann,
|
||||
&mut None,
|
||||
)?;
|
||||
vars.extend(type_vars.iter().map(|ty| {
|
||||
let TypeEnum::TVar { id, .. } = &*unifier.get_ty(*ty) else {
|
||||
|
|
|
@ -113,7 +113,7 @@ pub enum PrimDef {
|
|||
/// Associated details of a [`PrimDef`]
|
||||
pub enum PrimDefDetails {
|
||||
PrimFunction { name: &'static str, simple_name: &'static str },
|
||||
PrimClass { name: &'static str },
|
||||
PrimClass { name: &'static str, get_ty_fn: fn(&PrimitiveStore) -> Type },
|
||||
}
|
||||
|
||||
impl PrimDef {
|
||||
|
@ -155,15 +155,17 @@ impl PrimDef {
|
|||
#[must_use]
|
||||
pub fn name(&self) -> &'static str {
|
||||
match self.details() {
|
||||
PrimDefDetails::PrimFunction { name, .. } | PrimDefDetails::PrimClass { name } => name,
|
||||
PrimDefDetails::PrimFunction { name, .. } | PrimDefDetails::PrimClass { name, .. } => {
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the associated details of this [`PrimDef`]
|
||||
#[must_use]
|
||||
pub fn details(self) -> PrimDefDetails {
|
||||
fn class(name: &'static str) -> PrimDefDetails {
|
||||
PrimDefDetails::PrimClass { name }
|
||||
fn class(name: &'static str, get_ty_fn: fn(&PrimitiveStore) -> Type) -> PrimDefDetails {
|
||||
PrimDefDetails::PrimClass { name, get_ty_fn }
|
||||
}
|
||||
|
||||
fn fun(name: &'static str, simple_name: Option<&'static str>) -> PrimDefDetails {
|
||||
|
@ -171,22 +173,22 @@ impl PrimDef {
|
|||
}
|
||||
|
||||
match self {
|
||||
PrimDef::Int32 => class("int32"),
|
||||
PrimDef::Int64 => class("int64"),
|
||||
PrimDef::Float => class("float"),
|
||||
PrimDef::Bool => class("bool"),
|
||||
PrimDef::None => class("none"),
|
||||
PrimDef::Range => class("range"),
|
||||
PrimDef::Str => class("str"),
|
||||
PrimDef::Exception => class("Exception"),
|
||||
PrimDef::UInt32 => class("uint32"),
|
||||
PrimDef::UInt64 => class("uint64"),
|
||||
PrimDef::Option => class("Option"),
|
||||
PrimDef::Int32 => class("int32", |primitives| primitives.int32),
|
||||
PrimDef::Int64 => class("int64", |primitives| primitives.int64),
|
||||
PrimDef::Float => class("float", |primitives| primitives.float),
|
||||
PrimDef::Bool => class("bool", |primitives| primitives.bool),
|
||||
PrimDef::None => class("none", |primitives| primitives.none),
|
||||
PrimDef::Range => class("range", |primitives| primitives.range),
|
||||
PrimDef::Str => class("str", |primitives| primitives.str),
|
||||
PrimDef::Exception => class("Exception", |primitives| primitives.exception),
|
||||
PrimDef::UInt32 => class("uint32", |primitives| primitives.uint32),
|
||||
PrimDef::UInt64 => class("uint64", |primitives| primitives.uint64),
|
||||
PrimDef::Option => class("Option", |primitives| primitives.option),
|
||||
PrimDef::OptionIsSome => fun("Option.is_some", Some("is_some")),
|
||||
PrimDef::OptionIsNone => fun("Option.is_none", Some("is_none")),
|
||||
PrimDef::OptionUnwrap => fun("Option.unwrap", Some("unwrap")),
|
||||
PrimDef::List => class("list"),
|
||||
PrimDef::NDArray => class("ndarray"),
|
||||
PrimDef::List => class("list", |primitives| primitives.list),
|
||||
PrimDef::NDArray => class("ndarray", |primitives| primitives.ndarray),
|
||||
PrimDef::NDArrayCopy => fun("ndarray.copy", Some("copy")),
|
||||
PrimDef::NDArrayFill => fun("ndarray.fill", Some("fill")),
|
||||
PrimDef::FunInt32 => fun("int32", None),
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use super::*;
|
||||
use crate::symbol_resolver::SymbolValue;
|
||||
use crate::toplevel::helper::PrimDef;
|
||||
use crate::toplevel::helper::{PrimDef, PrimDefDetails};
|
||||
use crate::typecheck::typedef::VarMap;
|
||||
use nac3parser::ast::Constant;
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TypeAnnotation {
|
||||
|
@ -357,6 +358,7 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
|
|||
pub fn get_type_from_type_annotation_kinds(
|
||||
top_level_defs: &[Arc<RwLock<TopLevelDef>>],
|
||||
unifier: &mut Unifier,
|
||||
primitives: &PrimitiveStore,
|
||||
ann: &TypeAnnotation,
|
||||
subst_list: &mut Option<Vec<Type>>,
|
||||
) -> Result<Type, HashSet<String>> {
|
||||
|
@ -379,100 +381,141 @@ pub fn get_type_from_type_annotation_kinds(
|
|||
let param_ty = params
|
||||
.iter()
|
||||
.map(|x| {
|
||||
get_type_from_type_annotation_kinds(top_level_defs, unifier, x, subst_list)
|
||||
get_type_from_type_annotation_kinds(
|
||||
top_level_defs,
|
||||
unifier,
|
||||
primitives,
|
||||
x,
|
||||
subst_list,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
let subst = {
|
||||
// check for compatible range
|
||||
// TODO: if allow type var to be applied(now this disallowed in the parse_to_type_annotation), need more check
|
||||
let mut result = VarMap::new();
|
||||
for (tvar, p) in type_vars.iter().zip(param_ty) {
|
||||
match unifier.get_ty(*tvar).as_ref() {
|
||||
TypeEnum::TVar {
|
||||
id,
|
||||
range,
|
||||
fields: None,
|
||||
name,
|
||||
loc,
|
||||
is_const_generic: false,
|
||||
} => {
|
||||
let ok: bool = {
|
||||
// create a temp type var and unify to check compatibility
|
||||
p == *tvar || {
|
||||
let temp = unifier.get_fresh_var_with_range(
|
||||
range.as_slice(),
|
||||
*name,
|
||||
*loc,
|
||||
);
|
||||
unifier.unify(temp.ty, p).is_ok()
|
||||
}
|
||||
};
|
||||
if ok {
|
||||
result.insert(*id, p);
|
||||
} else {
|
||||
return Err(HashSet::from([format!(
|
||||
"cannot apply type {} to type variable with id {:?}",
|
||||
unifier.internal_stringify(
|
||||
p,
|
||||
&mut |id| format!("class{id}"),
|
||||
&mut |id| format!("typevar{id}"),
|
||||
&mut None
|
||||
),
|
||||
*id
|
||||
)]));
|
||||
}
|
||||
}
|
||||
let ty = if let Some(prim_def) = PrimDef::iter().find(|prim| prim.id() == *obj_id) {
|
||||
// Primitive TopLevelDefs do not contain all fields that are present in their Type
|
||||
// counterparts, so directly perform subst on the Type instead.
|
||||
|
||||
TypeEnum::TVar { id, range, name, loc, is_const_generic: true, .. } => {
|
||||
let ty = range[0];
|
||||
let ok: bool = {
|
||||
// create a temp type var and unify to check compatibility
|
||||
p == *tvar || {
|
||||
let temp = unifier.get_fresh_const_generic_var(ty, *name, *loc);
|
||||
unifier.unify(temp.ty, p).is_ok()
|
||||
}
|
||||
};
|
||||
if ok {
|
||||
result.insert(*id, p);
|
||||
} else {
|
||||
return Err(HashSet::from([format!(
|
||||
"cannot apply type {} to type variable {}",
|
||||
unifier.stringify(p),
|
||||
name.unwrap_or_else(|| format!("typevar{id}").into()),
|
||||
)]));
|
||||
}
|
||||
}
|
||||
let PrimDefDetails::PrimClass { get_ty_fn, .. } = prim_def.details() else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
_ => unreachable!("must be generic type var"),
|
||||
let base_ty = get_ty_fn(primitives);
|
||||
let params =
|
||||
if let TypeEnum::TObj { params, .. } = &*unifier.get_ty_immutable(base_ty) {
|
||||
params.clone()
|
||||
} else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
unifier
|
||||
.subst(
|
||||
get_ty_fn(primitives),
|
||||
¶ms
|
||||
.iter()
|
||||
.zip(param_ty)
|
||||
.map(|(obj_tv, param)| (*obj_tv.0, param))
|
||||
.collect(),
|
||||
)
|
||||
.unwrap_or(base_ty)
|
||||
} else {
|
||||
let subst = {
|
||||
// check for compatible range
|
||||
// TODO: if allow type var to be applied(now this disallowed in the parse_to_type_annotation), need more check
|
||||
let mut result = VarMap::new();
|
||||
for (tvar, p) in type_vars.iter().zip(param_ty) {
|
||||
match unifier.get_ty(*tvar).as_ref() {
|
||||
TypeEnum::TVar {
|
||||
id,
|
||||
range,
|
||||
fields: None,
|
||||
name,
|
||||
loc,
|
||||
is_const_generic: false,
|
||||
} => {
|
||||
let ok: bool = {
|
||||
// create a temp type var and unify to check compatibility
|
||||
p == *tvar || {
|
||||
let temp = unifier.get_fresh_var_with_range(
|
||||
range.as_slice(),
|
||||
*name,
|
||||
*loc,
|
||||
);
|
||||
unifier.unify(temp.ty, p).is_ok()
|
||||
}
|
||||
};
|
||||
if ok {
|
||||
result.insert(*id, p);
|
||||
} else {
|
||||
return Err(HashSet::from([format!(
|
||||
"cannot apply type {} to type variable with id {:?}",
|
||||
unifier.internal_stringify(
|
||||
p,
|
||||
&mut |id| format!("class{id}"),
|
||||
&mut |id| format!("typevar{id}"),
|
||||
&mut None
|
||||
),
|
||||
*id
|
||||
)]));
|
||||
}
|
||||
}
|
||||
|
||||
TypeEnum::TVar {
|
||||
id, range, name, loc, is_const_generic: true, ..
|
||||
} => {
|
||||
let ty = range[0];
|
||||
let ok: bool = {
|
||||
// create a temp type var and unify to check compatibility
|
||||
p == *tvar || {
|
||||
let temp =
|
||||
unifier.get_fresh_const_generic_var(ty, *name, *loc);
|
||||
unifier.unify(temp.ty, p).is_ok()
|
||||
}
|
||||
};
|
||||
if ok {
|
||||
result.insert(*id, p);
|
||||
} else {
|
||||
return Err(HashSet::from([format!(
|
||||
"cannot apply type {} to type variable {}",
|
||||
unifier.stringify(p),
|
||||
name.unwrap_or_else(|| format!("typevar{id}").into()),
|
||||
)]));
|
||||
}
|
||||
}
|
||||
|
||||
_ => unreachable!("must be generic type var"),
|
||||
}
|
||||
}
|
||||
result
|
||||
};
|
||||
// Class Attributes keep a copy with Class Definition and are not added to objects
|
||||
let mut tobj_fields = methods
|
||||
.iter()
|
||||
.map(|(name, ty, _)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
// methods are immutable
|
||||
(*name, (subst_ty, false))
|
||||
})
|
||||
.collect::<HashMap<_, _>>();
|
||||
tobj_fields.extend(fields.iter().map(|(name, ty, mutability)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
(*name, (subst_ty, *mutability))
|
||||
}));
|
||||
let need_subst = !subst.is_empty();
|
||||
let ty = unifier.add_ty(TypeEnum::TObj {
|
||||
obj_id: *obj_id,
|
||||
fields: tobj_fields,
|
||||
params: subst,
|
||||
});
|
||||
|
||||
if need_subst {
|
||||
if let Some(wl) = subst_list.as_mut() {
|
||||
wl.push(ty);
|
||||
}
|
||||
}
|
||||
result
|
||||
|
||||
ty
|
||||
};
|
||||
// Class Attributes keep a copy with Class Definition and are not added to objects
|
||||
let mut tobj_fields = methods
|
||||
.iter()
|
||||
.map(|(name, ty, _)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
// methods are immutable
|
||||
(*name, (subst_ty, false))
|
||||
})
|
||||
.collect::<HashMap<_, _>>();
|
||||
tobj_fields.extend(fields.iter().map(|(name, ty, mutability)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
(*name, (subst_ty, *mutability))
|
||||
}));
|
||||
let need_subst = !subst.is_empty();
|
||||
let ty = unifier.add_ty(TypeEnum::TObj {
|
||||
obj_id: *obj_id,
|
||||
fields: tobj_fields,
|
||||
params: subst,
|
||||
});
|
||||
if need_subst {
|
||||
if let Some(wl) = subst_list.as_mut() {
|
||||
wl.push(ty);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ty)
|
||||
}
|
||||
TypeAnnotation::Primitive(ty) | TypeAnnotation::TypeVar(ty) => Ok(*ty),
|
||||
|
@ -490,6 +533,7 @@ pub fn get_type_from_type_annotation_kinds(
|
|||
let ty = get_type_from_type_annotation_kinds(
|
||||
top_level_defs,
|
||||
unifier,
|
||||
primitives,
|
||||
ty.as_ref(),
|
||||
subst_list,
|
||||
)?;
|
||||
|
@ -499,7 +543,13 @@ pub fn get_type_from_type_annotation_kinds(
|
|||
let tys = tys
|
||||
.iter()
|
||||
.map(|x| {
|
||||
get_type_from_type_annotation_kinds(top_level_defs, unifier, x, subst_list)
|
||||
get_type_from_type_annotation_kinds(
|
||||
top_level_defs,
|
||||
unifier,
|
||||
primitives,
|
||||
x,
|
||||
subst_list,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(unifier.add_ty(TypeEnum::TTuple { ty: tys }))
|
||||
|
|
|
@ -113,7 +113,9 @@ fn handle_typevar_definition(
|
|||
x,
|
||||
HashMap::new(),
|
||||
)?;
|
||||
get_type_from_type_annotation_kinds(def_list, unifier, &ty, &mut None)
|
||||
get_type_from_type_annotation_kinds(
|
||||
def_list, unifier, primitives, &ty, &mut None,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let loc = func.location;
|
||||
|
@ -152,7 +154,7 @@ fn handle_typevar_definition(
|
|||
HashMap::new(),
|
||||
)?;
|
||||
let constraint =
|
||||
get_type_from_type_annotation_kinds(def_list, unifier, &ty, &mut None)?;
|
||||
get_type_from_type_annotation_kinds(def_list, unifier, primitives, &ty, &mut None)?;
|
||||
let loc = func.location;
|
||||
|
||||
Ok(unifier.get_fresh_const_generic_var(constraint, Some(generic_name), Some(loc)).ty)
|
||||
|
|
Loading…
Reference in New Issue