2021-09-07 00:20:40 +08:00
|
|
|
use std::fmt::Debug;
|
2022-02-21 17:52:34 +08:00
|
|
|
use std::sync::Arc;
|
2023-11-15 17:30:26 +08:00
|
|
|
use std::{collections::HashMap, collections::HashSet, fmt::Display};
|
2023-12-01 15:56:18 +08:00
|
|
|
use std::rc::Rc;
|
2021-08-11 17:28:29 +08:00
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
use crate::typecheck::typedef::TypeEnum;
|
2021-11-06 22:48:08 +08:00
|
|
|
use crate::{
|
|
|
|
codegen::CodeGenContext,
|
2023-12-01 15:56:18 +08:00
|
|
|
toplevel::{DefinitionId, TopLevelDef, type_annotation::TypeAnnotation},
|
2021-11-06 22:48:08 +08:00
|
|
|
};
|
2022-02-12 21:09:23 +08:00
|
|
|
use crate::{
|
|
|
|
codegen::CodeGenerator,
|
|
|
|
typecheck::{
|
|
|
|
type_inferencer::PrimitiveStore,
|
|
|
|
typedef::{Type, Unifier},
|
|
|
|
},
|
|
|
|
};
|
2022-03-27 02:23:22 +08:00
|
|
|
use inkwell::values::{BasicValueEnum, FloatValue, IntValue, PointerValue, StructValue};
|
2024-02-06 12:29:21 +08:00
|
|
|
use itertools::{chain, Itertools, izip};
|
2023-12-01 15:56:18 +08:00
|
|
|
use nac3parser::ast::{Constant, Expr, Location, StrRef};
|
2021-11-06 22:48:08 +08:00
|
|
|
use parking_lot::RwLock;
|
2021-06-28 14:48:04 +08:00
|
|
|
|
2021-10-17 13:02:18 +08:00
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
2021-08-07 17:25:14 +08:00
|
|
|
pub enum SymbolValue {
|
2021-06-28 14:48:04 +08:00
|
|
|
I32(i32),
|
|
|
|
I64(i64),
|
2022-03-05 03:45:09 +08:00
|
|
|
U32(u32),
|
|
|
|
U64(u64),
|
2022-02-12 21:09:23 +08:00
|
|
|
Str(String),
|
2021-06-28 14:48:04 +08:00
|
|
|
Double(f64),
|
|
|
|
Bool(bool),
|
2021-08-07 17:25:14 +08:00
|
|
|
Tuple(Vec<SymbolValue>),
|
2022-03-30 03:14:21 +08:00
|
|
|
OptionSome(Box<SymbolValue>),
|
|
|
|
OptionNone,
|
2021-11-20 19:50:25 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:56:18 +08:00
|
|
|
impl SymbolValue {
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Creates a [`SymbolValue`] from a [`Constant`].
|
2023-12-01 15:56:18 +08:00
|
|
|
///
|
|
|
|
/// * `constant` - The constant to create the value from.
|
2023-12-08 17:43:32 +08:00
|
|
|
/// * `expected_ty` - The expected type of the [`SymbolValue`].
|
2023-12-01 15:56:18 +08:00
|
|
|
pub fn from_constant(
|
|
|
|
constant: &Constant,
|
|
|
|
expected_ty: Type,
|
|
|
|
primitives: &PrimitiveStore,
|
|
|
|
unifier: &mut Unifier
|
|
|
|
) -> Result<Self, String> {
|
|
|
|
match constant {
|
|
|
|
Constant::None => {
|
|
|
|
if unifier.unioned(expected_ty, primitives.option) {
|
|
|
|
Ok(SymbolValue::OptionNone)
|
|
|
|
} else {
|
2023-12-08 17:43:32 +08:00
|
|
|
Err(format!("Expected {expected_ty:?}, but got Option"))
|
2023-12-01 15:56:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Constant::Bool(b) => {
|
|
|
|
if unifier.unioned(expected_ty, primitives.bool) {
|
|
|
|
Ok(SymbolValue::Bool(*b))
|
|
|
|
} else {
|
2023-12-08 17:43:32 +08:00
|
|
|
Err(format!("Expected {expected_ty:?}, but got bool"))
|
2023-12-01 15:56:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Constant::Str(s) => {
|
|
|
|
if unifier.unioned(expected_ty, primitives.str) {
|
|
|
|
Ok(SymbolValue::Str(s.to_string()))
|
|
|
|
} else {
|
2023-12-08 17:43:32 +08:00
|
|
|
Err(format!("Expected {expected_ty:?}, but got str"))
|
2023-12-01 15:56:18 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Constant::Int(i) => {
|
|
|
|
if unifier.unioned(expected_ty, primitives.int32) {
|
|
|
|
i32::try_from(*i)
|
2023-12-06 11:49:02 +08:00
|
|
|
.map(SymbolValue::I32)
|
2023-12-01 15:56:18 +08:00
|
|
|
.map_err(|e| e.to_string())
|
|
|
|
} else if unifier.unioned(expected_ty, primitives.int64) {
|
|
|
|
i64::try_from(*i)
|
2023-12-06 11:49:02 +08:00
|
|
|
.map(SymbolValue::I64)
|
2023-12-01 15:56:18 +08:00
|
|
|
.map_err(|e| e.to_string())
|
|
|
|
} else if unifier.unioned(expected_ty, primitives.uint32) {
|
|
|
|
u32::try_from(*i)
|
2023-12-06 11:49:02 +08:00
|
|
|
.map(SymbolValue::U32)
|
2023-12-01 15:56:18 +08:00
|
|
|
.map_err(|e| e.to_string())
|
|
|
|
} else if unifier.unioned(expected_ty, primitives.uint64) {
|
|
|
|
u64::try_from(*i)
|
2023-12-06 11:49:02 +08:00
|
|
|
.map(SymbolValue::U64)
|
2023-12-01 15:56:18 +08:00
|
|
|
.map_err(|e| e.to_string())
|
|
|
|
} else {
|
2023-12-05 14:37:08 +08:00
|
|
|
Err(format!("Expected {}, but got int", unifier.stringify(expected_ty)))
|
2023-12-01 15:56:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Constant::Tuple(t) => {
|
|
|
|
let expected_ty = unifier.get_ty(expected_ty);
|
|
|
|
let TypeEnum::TTuple { ty } = expected_ty.as_ref() else {
|
|
|
|
return Err(format!("Expected {:?}, but got Tuple", expected_ty.get_type_name()))
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(ty.len(), t.len());
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
let elems = t
|
|
|
|
.iter()
|
2023-12-01 15:56:18 +08:00
|
|
|
.zip(ty)
|
|
|
|
.map(|(constant, ty)| Self::from_constant(constant, *ty, primitives, unifier))
|
|
|
|
.collect::<Result<Vec<SymbolValue>, _>>()?;
|
|
|
|
Ok(SymbolValue::Tuple(elems))
|
|
|
|
}
|
|
|
|
Constant::Float(f) => {
|
|
|
|
if unifier.unioned(expected_ty, primitives.float) {
|
|
|
|
Ok(SymbolValue::Double(*f))
|
|
|
|
} else {
|
2023-12-08 17:43:32 +08:00
|
|
|
Err(format!("Expected {expected_ty:?}, but got float"))
|
2023-12-01 15:56:18 +08:00
|
|
|
}
|
|
|
|
},
|
2023-12-08 17:43:32 +08:00
|
|
|
_ => Err(format!("Unsupported value type {constant:?}")),
|
2023-12-01 15:56:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 18:26:42 +08:00
|
|
|
/// Creates a [`SymbolValue`] from a [`Constant`], with its type being inferred from the constant value.
|
|
|
|
///
|
|
|
|
/// * `constant` - The constant to create the value from.
|
|
|
|
pub fn from_constant_inferred(
|
|
|
|
constant: &Constant,
|
|
|
|
unifier: &mut Unifier
|
|
|
|
) -> Result<Self, String> {
|
|
|
|
match constant {
|
|
|
|
Constant::None => Ok(SymbolValue::OptionNone),
|
|
|
|
Constant::Bool(b) => Ok(SymbolValue::Bool(*b)),
|
|
|
|
Constant::Str(s) => Ok(SymbolValue::Str(s.to_string())),
|
|
|
|
Constant::Int(i) => {
|
|
|
|
let i = *i;
|
|
|
|
if i >= 0 {
|
|
|
|
i32::try_from(i).map(SymbolValue::I32)
|
|
|
|
.or_else(|_| i64::try_from(i).map(SymbolValue::I64))
|
|
|
|
.map_err(|_| format!("Literal cannot be expressed as any integral type: {i}"))
|
|
|
|
} else {
|
|
|
|
u32::try_from(i).map(SymbolValue::U32)
|
|
|
|
.or_else(|_| u64::try_from(i).map(SymbolValue::U64))
|
|
|
|
.map_err(|_| format!("Literal cannot be expressed as any integral type: {i}"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Constant::Tuple(t) => {
|
|
|
|
let elems = t
|
|
|
|
.iter()
|
|
|
|
.map(|constant| Self::from_constant_inferred(constant, unifier))
|
|
|
|
.collect::<Result<Vec<SymbolValue>, _>>()?;
|
|
|
|
Ok(SymbolValue::Tuple(elems))
|
|
|
|
}
|
|
|
|
Constant::Float(f) => Ok(SymbolValue::Double(*f)),
|
|
|
|
_ => Err(format!("Unsupported value type {constant:?}")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Returns the [`Type`] representing the data type of this value.
|
2023-12-01 15:56:18 +08:00
|
|
|
pub fn get_type(&self, primitives: &PrimitiveStore, unifier: &mut Unifier) -> Type {
|
|
|
|
match self {
|
|
|
|
SymbolValue::I32(_) => primitives.int32,
|
|
|
|
SymbolValue::I64(_) => primitives.int64,
|
|
|
|
SymbolValue::U32(_) => primitives.uint32,
|
|
|
|
SymbolValue::U64(_) => primitives.uint64,
|
|
|
|
SymbolValue::Str(_) => primitives.str,
|
|
|
|
SymbolValue::Double(_) => primitives.float,
|
|
|
|
SymbolValue::Bool(_) => primitives.bool,
|
|
|
|
SymbolValue::Tuple(vs) => {
|
|
|
|
let vs_tys = vs
|
|
|
|
.iter()
|
|
|
|
.map(|v| v.get_type(primitives, unifier))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
unifier.add_ty(TypeEnum::TTuple {
|
|
|
|
ty: vs_tys,
|
|
|
|
})
|
|
|
|
}
|
2023-12-08 17:43:32 +08:00
|
|
|
SymbolValue::OptionSome(_) | SymbolValue::OptionNone => primitives.option,
|
2023-12-01 15:56:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Returns the [`TypeAnnotation`] representing the data type of this value.
|
2023-12-01 15:56:18 +08:00
|
|
|
pub fn get_type_annotation(&self, primitives: &PrimitiveStore, unifier: &mut Unifier) -> TypeAnnotation {
|
|
|
|
match self {
|
|
|
|
SymbolValue::Bool(..) => TypeAnnotation::Primitive(primitives.bool),
|
|
|
|
SymbolValue::Double(..) => TypeAnnotation::Primitive(primitives.float),
|
|
|
|
SymbolValue::I32(..) => TypeAnnotation::Primitive(primitives.int32),
|
|
|
|
SymbolValue::I64(..) => TypeAnnotation::Primitive(primitives.int64),
|
|
|
|
SymbolValue::U32(..) => TypeAnnotation::Primitive(primitives.uint32),
|
|
|
|
SymbolValue::U64(..) => TypeAnnotation::Primitive(primitives.uint64),
|
|
|
|
SymbolValue::Str(..) => TypeAnnotation::Primitive(primitives.str),
|
|
|
|
SymbolValue::Tuple(vs) => {
|
|
|
|
let vs_tys = vs
|
|
|
|
.iter()
|
|
|
|
.map(|v| v.get_type_annotation(primitives, unifier))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
TypeAnnotation::Tuple(vs_tys)
|
|
|
|
}
|
|
|
|
SymbolValue::OptionNone => TypeAnnotation::CustomClass {
|
|
|
|
id: primitives.option.get_obj_id(unifier),
|
2023-12-08 17:43:32 +08:00
|
|
|
params: Vec::default(),
|
2023-12-01 15:56:18 +08:00
|
|
|
},
|
|
|
|
SymbolValue::OptionSome(v) => {
|
|
|
|
let ty = v.get_type_annotation(primitives, unifier);
|
|
|
|
TypeAnnotation::CustomClass {
|
|
|
|
id: primitives.option.get_obj_id(unifier),
|
|
|
|
params: vec![ty],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Returns the [`TypeEnum`] representing the data type of this value.
|
2023-12-01 15:56:18 +08:00
|
|
|
pub fn get_type_enum(&self, primitives: &PrimitiveStore, unifier: &mut Unifier) -> Rc<TypeEnum> {
|
|
|
|
let ty = self.get_type(primitives, unifier);
|
|
|
|
unifier.get_ty(ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:52:34 +08:00
|
|
|
impl Display for SymbolValue {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2023-12-08 17:43:32 +08:00
|
|
|
SymbolValue::I32(i) => write!(f, "{i}"),
|
|
|
|
SymbolValue::I64(i) => write!(f, "int64({i})"),
|
|
|
|
SymbolValue::U32(i) => write!(f, "uint32({i})"),
|
|
|
|
SymbolValue::U64(i) => write!(f, "uint64({i})"),
|
|
|
|
SymbolValue::Str(s) => write!(f, "\"{s}\""),
|
|
|
|
SymbolValue::Double(d) => write!(f, "{d}"),
|
2022-02-21 18:27:46 +08:00
|
|
|
SymbolValue::Bool(b) => {
|
|
|
|
if *b {
|
|
|
|
write!(f, "True")
|
|
|
|
} else {
|
|
|
|
write!(f, "False")
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 17:52:34 +08:00
|
|
|
SymbolValue::Tuple(t) => {
|
2023-12-08 17:43:32 +08:00
|
|
|
write!(f, "({})", t.iter().map(|v| format!("{v}")).collect::<Vec<_>>().join(", "))
|
2022-02-21 17:52:34 +08:00
|
|
|
}
|
2023-12-08 17:43:32 +08:00
|
|
|
SymbolValue::OptionSome(v) => write!(f, "Some({v})"),
|
2022-03-30 03:14:21 +08:00
|
|
|
SymbolValue::OptionNone => write!(f, "none"),
|
2022-02-21 17:52:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 19:50:25 +08:00
|
|
|
pub trait StaticValue {
|
2023-09-20 16:59:47 +08:00
|
|
|
/// Returns a unique identifier for this value.
|
2021-11-20 19:50:25 +08:00
|
|
|
fn get_unique_identifier(&self) -> u64;
|
|
|
|
|
2023-10-18 13:40:37 +08:00
|
|
|
/// Returns the constant object represented by this unique identifier.
|
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
|
|
|
generator: &mut dyn CodeGenerator,
|
|
|
|
) -> BasicValueEnum<'ctx>;
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Converts this value to a LLVM [`BasicValueEnum`].
|
2023-12-06 11:49:02 +08:00
|
|
|
fn to_basic_value_enum<'ctx>(
|
2021-11-20 19:50:25 +08:00
|
|
|
&self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2022-02-12 21:17:37 +08:00
|
|
|
generator: &mut dyn CodeGenerator,
|
2022-04-08 03:26:42 +08:00
|
|
|
expected_ty: Type,
|
2022-02-28 23:09:14 +08:00
|
|
|
) -> Result<BasicValueEnum<'ctx>, String>;
|
2021-11-20 19:50:25 +08:00
|
|
|
|
2023-09-20 16:59:47 +08:00
|
|
|
/// Returns a field within this value.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn get_field<'ctx>(
|
2021-11-20 19:50:25 +08:00
|
|
|
&self,
|
|
|
|
name: StrRef,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-11-20 19:50:25 +08:00
|
|
|
) -> Option<ValueEnum<'ctx>>;
|
2022-04-10 16:25:15 +08:00
|
|
|
|
2023-09-20 16:59:47 +08:00
|
|
|
/// Returns a single element of this tuple.
|
2022-04-10 16:25:15 +08:00
|
|
|
fn get_tuple_element<'ctx>(&self, index: u32) -> Option<ValueEnum<'ctx>>;
|
2021-11-20 19:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum ValueEnum<'ctx> {
|
2023-10-18 13:40:37 +08:00
|
|
|
/// [ValueEnum] representing a static value.
|
2021-11-20 19:50:25 +08:00
|
|
|
Static(Arc<dyn StaticValue + Send + Sync>),
|
2023-10-18 13:40:37 +08:00
|
|
|
|
|
|
|
/// [ValueEnum] representing a dynamic value.
|
2021-11-20 19:50:25 +08:00
|
|
|
Dynamic(BasicValueEnum<'ctx>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> From<BasicValueEnum<'ctx>> for ValueEnum<'ctx> {
|
|
|
|
fn from(v: BasicValueEnum<'ctx>) -> Self {
|
|
|
|
ValueEnum::Dynamic(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> From<PointerValue<'ctx>> for ValueEnum<'ctx> {
|
|
|
|
fn from(v: PointerValue<'ctx>) -> Self {
|
|
|
|
ValueEnum::Dynamic(v.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> From<IntValue<'ctx>> for ValueEnum<'ctx> {
|
|
|
|
fn from(v: IntValue<'ctx>) -> Self {
|
|
|
|
ValueEnum::Dynamic(v.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> From<FloatValue<'ctx>> for ValueEnum<'ctx> {
|
|
|
|
fn from(v: FloatValue<'ctx>) -> Self {
|
|
|
|
ValueEnum::Dynamic(v.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 02:23:22 +08:00
|
|
|
impl<'ctx> From<StructValue<'ctx>> for ValueEnum<'ctx> {
|
|
|
|
fn from(v: StructValue<'ctx>) -> Self {
|
|
|
|
ValueEnum::Dynamic(v.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 19:50:25 +08:00
|
|
|
impl<'ctx> ValueEnum<'ctx> {
|
2023-10-18 13:40:37 +08:00
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Converts this [`ValueEnum`] to a [`BasicValueEnum`].
|
2021-11-20 19:50:25 +08:00
|
|
|
pub fn to_basic_value_enum<'a>(
|
|
|
|
self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
2021-12-27 22:55:51 +08:00
|
|
|
generator: &mut dyn CodeGenerator,
|
2022-04-08 03:26:42 +08:00
|
|
|
expected_ty: Type,
|
2022-02-28 23:09:14 +08:00
|
|
|
) -> Result<BasicValueEnum<'ctx>, String> {
|
2021-11-20 19:50:25 +08:00
|
|
|
match self {
|
2022-04-08 03:26:42 +08:00
|
|
|
ValueEnum::Static(v) => v.to_basic_value_enum(ctx, generator, expected_ty),
|
2022-02-28 23:09:14 +08:00
|
|
|
ValueEnum::Dynamic(v) => Ok(v),
|
2021-11-20 19:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 14:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SymbolResolver {
|
2023-10-18 13:40:37 +08:00
|
|
|
/// Get type of type variable identifier or top-level function type,
|
2021-08-11 17:28:29 +08:00
|
|
|
fn get_symbol_type(
|
|
|
|
&self,
|
|
|
|
unifier: &mut Unifier,
|
2021-10-06 16:07:42 +08:00
|
|
|
top_level_defs: &[Arc<RwLock<TopLevelDef>>],
|
2021-08-11 17:28:29 +08:00
|
|
|
primitives: &PrimitiveStore,
|
2021-09-22 17:19:27 +08:00
|
|
|
str: StrRef,
|
2022-01-13 03:14:06 +08:00
|
|
|
) -> Result<Type, String>;
|
2021-11-20 19:50:25 +08:00
|
|
|
|
2023-10-18 13:40:37 +08:00
|
|
|
/// Get the top-level definition of identifiers.
|
2023-11-15 17:30:26 +08:00
|
|
|
fn get_identifier_def(&self, str: StrRef) -> Result<DefinitionId, HashSet<String>>;
|
2021-11-20 19:50:25 +08:00
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn get_symbol_value<'ctx>(
|
2021-11-06 22:48:08 +08:00
|
|
|
&self,
|
|
|
|
str: StrRef,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-11-20 19:50:25 +08:00
|
|
|
) -> Option<ValueEnum<'ctx>>;
|
|
|
|
|
2023-10-26 13:52:40 +08:00
|
|
|
fn get_default_param_value(&self, expr: &Expr) -> Option<SymbolValue>;
|
2022-02-12 21:15:50 +08:00
|
|
|
fn get_string_id(&self, s: &str) -> i32;
|
2022-03-05 00:26:35 +08:00
|
|
|
fn get_exception_id(&self, tyid: usize) -> usize;
|
2022-03-24 21:29:46 +08:00
|
|
|
|
|
|
|
fn handle_deferred_eval(
|
|
|
|
&self,
|
|
|
|
_unifier: &mut Unifier,
|
|
|
|
_top_level_defs: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
_primitives: &PrimitiveStore
|
|
|
|
) -> Result<(), String> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
|
|
|
|
2021-09-22 17:19:27 +08:00
|
|
|
thread_local! {
|
2024-02-06 12:29:21 +08:00
|
|
|
static IDENTIFIER_ID: [StrRef; 13] = [
|
2021-09-22 17:19:27 +08:00
|
|
|
"int32".into(),
|
|
|
|
"int64".into(),
|
|
|
|
"float".into(),
|
|
|
|
"bool".into(),
|
|
|
|
"virtual".into(),
|
|
|
|
"list".into(),
|
2023-11-06 18:03:52 +08:00
|
|
|
"ndarray".into(),
|
2022-02-12 21:09:23 +08:00
|
|
|
"tuple".into(),
|
|
|
|
"str".into(),
|
|
|
|
"Exception".into(),
|
2022-03-05 03:45:09 +08:00
|
|
|
"uint32".into(),
|
|
|
|
"uint64".into(),
|
2024-02-06 12:29:21 +08:00
|
|
|
"Literal".into(),
|
2021-09-22 17:19:27 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-10-18 13:40:37 +08:00
|
|
|
/// Converts a type annotation into a [Type].
|
2021-08-12 14:44:50 +08:00
|
|
|
pub fn parse_type_annotation<T>(
|
|
|
|
resolver: &dyn SymbolResolver,
|
2021-08-19 17:31:23 +08:00
|
|
|
top_level_defs: &[Arc<RwLock<TopLevelDef>>],
|
2021-08-12 14:44:50 +08:00
|
|
|
unifier: &mut Unifier,
|
|
|
|
primitives: &PrimitiveStore,
|
|
|
|
expr: &Expr<T>,
|
2023-11-15 17:30:26 +08:00
|
|
|
) -> Result<Type, HashSet<String>> {
|
2021-11-03 17:11:00 +08:00
|
|
|
use nac3parser::ast::ExprKind::*;
|
2021-11-06 22:48:08 +08:00
|
|
|
let ids = IDENTIFIER_ID.with(|ids| *ids);
|
2021-09-22 17:19:27 +08:00
|
|
|
let int32_id = ids[0];
|
|
|
|
let int64_id = ids[1];
|
|
|
|
let float_id = ids[2];
|
|
|
|
let bool_id = ids[3];
|
2022-03-26 15:09:15 +08:00
|
|
|
let virtual_id = ids[4];
|
|
|
|
let list_id = ids[5];
|
2023-11-06 18:03:52 +08:00
|
|
|
let ndarray_id = ids[6];
|
|
|
|
let tuple_id = ids[7];
|
|
|
|
let str_id = ids[8];
|
|
|
|
let exn_id = ids[9];
|
|
|
|
let uint32_id = ids[10];
|
|
|
|
let uint64_id = ids[11];
|
2024-02-06 12:29:21 +08:00
|
|
|
let literal_id = ids[12];
|
2021-09-22 17:19:27 +08:00
|
|
|
|
2022-02-21 17:52:34 +08:00
|
|
|
let name_handling = |id: &StrRef, loc: Location, unifier: &mut Unifier| {
|
2021-12-01 22:44:53 +08:00
|
|
|
if *id == int32_id {
|
|
|
|
Ok(primitives.int32)
|
|
|
|
} else if *id == int64_id {
|
|
|
|
Ok(primitives.int64)
|
2022-03-05 03:45:09 +08:00
|
|
|
} else if *id == uint32_id {
|
|
|
|
Ok(primitives.uint32)
|
|
|
|
} else if *id == uint64_id {
|
|
|
|
Ok(primitives.uint64)
|
2021-12-01 22:44:53 +08:00
|
|
|
} else if *id == float_id {
|
|
|
|
Ok(primitives.float)
|
|
|
|
} else if *id == bool_id {
|
|
|
|
Ok(primitives.bool)
|
2022-02-12 21:09:23 +08:00
|
|
|
} else if *id == str_id {
|
|
|
|
Ok(primitives.str)
|
|
|
|
} else if *id == exn_id {
|
|
|
|
Ok(primitives.exception)
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
|
|
|
let obj_id = resolver.get_identifier_def(*id);
|
2023-12-08 17:43:32 +08:00
|
|
|
if let Ok(obj_id) = obj_id {
|
|
|
|
let def = top_level_defs[obj_id.0].read();
|
|
|
|
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
|
|
|
if !type_vars.is_empty() {
|
2023-11-15 17:30:26 +08:00
|
|
|
return Err(HashSet::from([
|
|
|
|
format!(
|
|
|
|
"Unexpected number of type parameters: expected {} but got 0",
|
|
|
|
type_vars.len()
|
|
|
|
),
|
|
|
|
]))
|
2022-02-21 17:52:34 +08:00
|
|
|
}
|
2023-12-08 17:43:32 +08:00
|
|
|
let fields = chain(
|
|
|
|
fields.iter().map(|(k, v, m)| (*k, (*v, *m))),
|
|
|
|
methods.iter().map(|(k, v, _)| (*k, (*v, false))),
|
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id,
|
|
|
|
fields,
|
|
|
|
params: HashMap::default(),
|
|
|
|
}))
|
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([
|
|
|
|
format!("Cannot use function name as type at {loc}"),
|
|
|
|
]))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
2023-12-08 17:43:32 +08:00
|
|
|
} else {
|
|
|
|
let ty = resolver
|
|
|
|
.get_symbol_type(unifier, top_level_defs, primitives, *id)
|
2023-11-15 17:30:26 +08:00
|
|
|
.map_err(|e| HashSet::from([
|
|
|
|
format!("Unknown type annotation at {loc}: {e}"),
|
|
|
|
]))?;
|
2023-12-08 17:43:32 +08:00
|
|
|
if let TypeEnum::TVar { .. } = &*unifier.get_ty(ty) {
|
|
|
|
Ok(ty)
|
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([
|
|
|
|
format!("Unknown type annotation {id} at {loc}"),
|
|
|
|
]))
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
2021-08-12 14:44:50 +08:00
|
|
|
}
|
2021-11-06 22:48:08 +08:00
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let subscript_name_handle = |id: &StrRef, slice: &Expr<T>, unifier: &mut Unifier| {
|
|
|
|
if *id == virtual_id {
|
2022-02-12 21:09:23 +08:00
|
|
|
let ty = parse_type_annotation(resolver, top_level_defs, unifier, primitives, slice)?;
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
|
|
|
} else if *id == list_id {
|
2022-02-12 21:09:23 +08:00
|
|
|
let ty = parse_type_annotation(resolver, top_level_defs, unifier, primitives, slice)?;
|
2021-12-01 22:44:53 +08:00
|
|
|
Ok(unifier.add_ty(TypeEnum::TList { ty }))
|
2023-11-06 18:03:52 +08:00
|
|
|
} else if *id == ndarray_id {
|
|
|
|
let Tuple { elts, .. } = &slice.node else {
|
|
|
|
return Err(HashSet::from([
|
|
|
|
String::from("Expected 2 type arguments for ndarray"),
|
|
|
|
]))
|
|
|
|
};
|
|
|
|
if elts.len() < 2 {
|
|
|
|
return Err(HashSet::from([
|
|
|
|
String::from("Expected 2 type arguments for ndarray"),
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty = parse_type_annotation(resolver, top_level_defs, unifier, primitives, &elts[0])?;
|
|
|
|
let ndims = parse_type_annotation(resolver, top_level_defs, unifier, primitives, &elts[1])?;
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TNDArray { ty, ndims }))
|
2021-12-01 22:44:53 +08:00
|
|
|
} else if *id == tuple_id {
|
|
|
|
if let Tuple { elts, .. } = &slice.node {
|
|
|
|
let ty = elts
|
|
|
|
.iter()
|
|
|
|
.map(|elt| {
|
2022-02-12 21:09:23 +08:00
|
|
|
parse_type_annotation(resolver, top_level_defs, unifier, primitives, elt)
|
2021-12-01 22:44:53 +08:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TTuple { ty }))
|
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([
|
|
|
|
"Expected multiple elements for tuple".into()
|
|
|
|
]))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
2024-02-06 12:29:21 +08:00
|
|
|
} else if *id == literal_id {
|
|
|
|
let mut parse_literal = |elt: &Expr<T>| {
|
|
|
|
let ty = parse_type_annotation(resolver, top_level_defs, unifier, primitives, elt)?;
|
|
|
|
let ty_enum = &*unifier.get_ty_immutable(ty);
|
|
|
|
match ty_enum {
|
|
|
|
TypeEnum::TLiteral { values, .. } => Ok(values.clone()),
|
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("Expected literal in type argument for Literal at {}", elt.location),
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let values = if let Tuple { elts, .. } = &slice.node {
|
|
|
|
elts.iter()
|
|
|
|
.map(|elt| parse_literal(elt))
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
|
|
} else {
|
|
|
|
vec![parse_literal(slice)?]
|
|
|
|
}.into_iter().flatten().collect_vec();
|
|
|
|
|
|
|
|
Ok(unifier.get_fresh_literal(values, Some(slice.location)))
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
|
|
|
let types = if let Tuple { elts, .. } = &slice.node {
|
|
|
|
elts.iter()
|
|
|
|
.map(|v| {
|
2022-02-12 21:09:23 +08:00
|
|
|
parse_type_annotation(resolver, top_level_defs, unifier, primitives, v)
|
2021-12-01 22:44:53 +08:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
|
|
} else {
|
2022-02-12 21:09:23 +08:00
|
|
|
vec![parse_type_annotation(resolver, top_level_defs, unifier, primitives, slice)?]
|
2021-12-01 22:44:53 +08:00
|
|
|
};
|
2021-08-24 14:58:19 +08:00
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
let obj_id = resolver.get_identifier_def(*id)?;
|
2021-12-01 22:44:53 +08:00
|
|
|
let def = top_level_defs[obj_id.0].read();
|
|
|
|
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
|
|
|
if types.len() != type_vars.len() {
|
2023-11-15 17:30:26 +08:00
|
|
|
return Err(HashSet::from([
|
|
|
|
format!(
|
|
|
|
"Unexpected number of type parameters: expected {} but got {}",
|
|
|
|
type_vars.len(),
|
|
|
|
types.len()
|
|
|
|
),
|
|
|
|
]))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
let mut subst = HashMap::new();
|
|
|
|
for (var, ty) in izip!(type_vars.iter(), types.iter()) {
|
|
|
|
let id = if let TypeEnum::TVar { id, .. } = &*unifier.get_ty(*var) {
|
|
|
|
*id
|
2021-09-22 17:19:27 +08:00
|
|
|
} else {
|
2021-12-01 22:44:53 +08:00
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
subst.insert(id, *ty);
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
2021-12-01 22:44:53 +08:00
|
|
|
let mut fields = fields
|
|
|
|
.iter()
|
|
|
|
.map(|(attr, ty, is_mutable)| {
|
|
|
|
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
|
|
|
(*attr, (ty, *is_mutable))
|
|
|
|
})
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
fields.extend(methods.iter().map(|(attr, ty, _)| {
|
|
|
|
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
|
|
|
(*attr, (ty, false))
|
|
|
|
}));
|
2022-02-21 18:27:46 +08:00
|
|
|
Ok(unifier.add_ty(TypeEnum::TObj { obj_id, fields, params: subst }))
|
2021-12-01 22:44:53 +08:00
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([
|
|
|
|
"Cannot use function name as type".into(),
|
|
|
|
]))
|
2021-12-01 22:44:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match &expr.node {
|
2022-02-21 17:52:34 +08:00
|
|
|
Name { id, .. } => name_handling(id, expr.location, unifier),
|
2021-12-01 22:44:53 +08:00
|
|
|
Subscript { value, slice, .. } => {
|
|
|
|
if let Name { id, .. } = &value.node {
|
|
|
|
subscript_name_handle(id, slice, unifier)
|
2021-08-12 14:44:50 +08:00
|
|
|
} else {
|
2023-11-15 17:30:26 +08:00
|
|
|
Err(HashSet::from([
|
|
|
|
format!("unsupported type expression at {}", expr.location),
|
|
|
|
]))
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 12:29:21 +08:00
|
|
|
Constant { value, .. } => SymbolValue::from_constant_inferred(value, unifier)
|
|
|
|
.map(|v| unifier.get_fresh_literal(vec![v], Some(expr.location.clone())))
|
|
|
|
.map_err(|err| HashSet::from([err])),
|
2023-11-15 17:30:26 +08:00
|
|
|
_ => Err(HashSet::from([
|
|
|
|
format!("unsupported type expression at {}", expr.location),
|
|
|
|
])),
|
2021-08-12 14:44:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 13:33:59 +08:00
|
|
|
impl dyn SymbolResolver + Send + Sync {
|
2021-08-12 14:44:50 +08:00
|
|
|
pub fn parse_type_annotation<T>(
|
|
|
|
&self,
|
2021-08-19 17:31:23 +08:00
|
|
|
top_level_defs: &[Arc<RwLock<TopLevelDef>>],
|
2021-08-12 14:44:50 +08:00
|
|
|
unifier: &mut Unifier,
|
|
|
|
primitives: &PrimitiveStore,
|
|
|
|
expr: &Expr<T>,
|
2023-11-15 17:30:26 +08:00
|
|
|
) -> Result<Type, HashSet<String>> {
|
2021-08-19 17:31:23 +08:00
|
|
|
parse_type_annotation(self, top_level_defs, unifier, primitives, expr)
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
2022-02-12 21:15:50 +08:00
|
|
|
|
|
|
|
pub fn get_type_name(
|
|
|
|
&self,
|
|
|
|
top_level_defs: &[Arc<RwLock<TopLevelDef>>],
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
ty: Type,
|
|
|
|
) -> String {
|
2022-02-21 17:52:34 +08:00
|
|
|
unifier.internal_stringify(
|
2022-02-12 21:15:50 +08:00
|
|
|
ty,
|
|
|
|
&mut |id| {
|
2023-12-12 13:38:27 +08:00
|
|
|
let TopLevelDef::Class { name, .. } = &*top_level_defs[id].read() else {
|
2022-02-12 21:15:50 +08:00
|
|
|
unreachable!("expected class definition")
|
2023-12-12 13:38:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
name.to_string()
|
2022-02-12 21:15:50 +08:00
|
|
|
},
|
2023-12-08 17:43:32 +08:00
|
|
|
&mut |id| format!("typevar{id}"),
|
2022-02-21 18:27:46 +08:00
|
|
|
&mut None,
|
2022-02-12 21:15:50 +08:00
|
|
|
)
|
|
|
|
}
|
2021-06-28 14:48:04 +08:00
|
|
|
}
|
2021-09-07 00:20:40 +08:00
|
|
|
|
|
|
|
impl Debug for dyn SymbolResolver + Send + Sync {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "")
|
|
|
|
}
|
2021-09-08 02:27:12 +08:00
|
|
|
}
|