2021-08-11 17:28:29 +08:00
|
|
|
use std::collections::HashMap;
|
2021-09-07 00:20:40 +08:00
|
|
|
use std::fmt::Debug;
|
2021-08-23 11:13:45 +08:00
|
|
|
use std::{cell::RefCell, sync::Arc};
|
2021-08-11 17:28:29 +08:00
|
|
|
|
|
|
|
use crate::typecheck::{
|
|
|
|
type_inferencer::PrimitiveStore,
|
|
|
|
typedef::{Type, Unifier},
|
|
|
|
};
|
2021-11-06 22:48:08 +08:00
|
|
|
use crate::{
|
|
|
|
codegen::CodeGenContext,
|
|
|
|
toplevel::{DefinitionId, TopLevelDef},
|
|
|
|
};
|
2021-08-11 17:28:29 +08:00
|
|
|
use crate::{location::Location, typecheck::typedef::TypeEnum};
|
2021-11-06 22:48:08 +08:00
|
|
|
use inkwell::values::BasicValueEnum;
|
2021-08-11 17:28:29 +08:00
|
|
|
use itertools::{chain, izip};
|
2021-11-03 17:11:00 +08:00
|
|
|
use nac3parser::ast::{Expr, 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),
|
|
|
|
Double(f64),
|
|
|
|
Bool(bool),
|
2021-08-07 17:25:14 +08:00
|
|
|
Tuple(Vec<SymbolValue>),
|
|
|
|
// we should think about how to implement bytes later...
|
|
|
|
// Bytes(&'a [u8]),
|
2021-06-28 14:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SymbolResolver {
|
2021-08-12 10:25:32 +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,
|
2021-08-11 17:28:29 +08:00
|
|
|
) -> Option<Type>;
|
2021-08-12 10:25:32 +08:00
|
|
|
// get the top-level definition of identifiers
|
2021-09-22 17:19:27 +08:00
|
|
|
fn get_identifier_def(&self, str: StrRef) -> Option<DefinitionId>;
|
2021-11-06 22:48:08 +08:00
|
|
|
fn get_symbol_value<'ctx, 'a>(
|
|
|
|
&self,
|
|
|
|
str: StrRef,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
|
|
|
) -> Option<BasicValueEnum<'ctx>>;
|
2021-09-22 17:19:27 +08:00
|
|
|
fn get_symbol_location(&self, str: StrRef) -> Option<Location>;
|
2021-11-23 07:32:09 +08:00
|
|
|
fn get_default_param_value(&self, expr: &nac3parser::ast::Expr) -> Option<SymbolValue>;
|
2021-08-11 17:28:29 +08:00
|
|
|
// handle function call etc.
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:19:27 +08:00
|
|
|
thread_local! {
|
|
|
|
static IDENTIFIER_ID: [StrRef; 8] = [
|
|
|
|
"int32".into(),
|
|
|
|
"int64".into(),
|
|
|
|
"float".into(),
|
|
|
|
"bool".into(),
|
|
|
|
"None".into(),
|
|
|
|
"virtual".into(),
|
|
|
|
"list".into(),
|
|
|
|
"tuple".into()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-08-12 14:44:50 +08:00
|
|
|
// convert type annotation into type
|
|
|
|
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>,
|
|
|
|
) -> Result<Type, 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];
|
|
|
|
let none_id = ids[4];
|
|
|
|
let virtual_id = ids[5];
|
|
|
|
let list_id = ids[6];
|
|
|
|
let tuple_id = ids[7];
|
|
|
|
|
2021-08-12 14:44:50 +08:00
|
|
|
match &expr.node {
|
2021-09-22 17:19:27 +08:00
|
|
|
Name { id, .. } => {
|
|
|
|
if *id == int32_id {
|
|
|
|
Ok(primitives.int32)
|
|
|
|
} else if *id == int64_id {
|
|
|
|
Ok(primitives.int64)
|
|
|
|
} else if *id == float_id {
|
|
|
|
Ok(primitives.float)
|
|
|
|
} else if *id == bool_id {
|
|
|
|
Ok(primitives.bool)
|
|
|
|
} else if *id == none_id {
|
|
|
|
Ok(primitives.none)
|
|
|
|
} else {
|
|
|
|
let obj_id = resolver.get_identifier_def(*id);
|
2021-08-12 14:44:50 +08:00
|
|
|
if let Some(obj_id) = obj_id {
|
2021-08-19 17:31:23 +08:00
|
|
|
let def = top_level_defs[obj_id.0].read();
|
2021-08-12 14:44:50 +08:00
|
|
|
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
|
|
|
if !type_vars.is_empty() {
|
|
|
|
return Err(format!(
|
|
|
|
"Unexpected number of type parameters: expected {} but got 0",
|
|
|
|
type_vars.len()
|
|
|
|
));
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
2021-08-12 14:44:50 +08:00
|
|
|
let fields = RefCell::new(
|
|
|
|
chain(
|
2021-11-06 22:48:08 +08:00
|
|
|
fields.iter().map(|(k, v, m)| (*k, (*v, *m))),
|
|
|
|
methods.iter().map(|(k, v, _)| (*k, (*v, false))),
|
2021-08-12 14:44:50 +08:00
|
|
|
)
|
|
|
|
.collect(),
|
|
|
|
);
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id,
|
|
|
|
fields,
|
|
|
|
params: Default::default(),
|
|
|
|
}))
|
2021-08-11 17:28:29 +08:00
|
|
|
} else {
|
2021-08-12 14:44:50 +08:00
|
|
|
Err("Cannot use function name as type".into())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// it could be a type variable
|
|
|
|
let ty = resolver
|
2021-10-06 16:07:42 +08:00
|
|
|
.get_symbol_type(unifier, top_level_defs, primitives, *id)
|
2021-08-23 02:52:54 +08:00
|
|
|
.ok_or_else(|| "unknown type variable name".to_owned())?;
|
2021-08-12 14:44:50 +08:00
|
|
|
if let TypeEnum::TVar { .. } = &*unifier.get_ty(ty) {
|
|
|
|
Ok(ty)
|
|
|
|
} else {
|
2021-09-22 17:19:27 +08:00
|
|
|
Err(format!("Unknown type annotation {}", id))
|
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-08-12 14:44:50 +08:00
|
|
|
Subscript { value, slice, .. } => {
|
|
|
|
if let Name { id, .. } = &value.node {
|
2021-09-22 17:19:27 +08:00
|
|
|
if *id == virtual_id {
|
|
|
|
let ty = parse_type_annotation(
|
|
|
|
resolver,
|
|
|
|
top_level_defs,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
slice,
|
|
|
|
)?;
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
|
|
|
} else if *id == list_id {
|
|
|
|
let ty = parse_type_annotation(
|
|
|
|
resolver,
|
|
|
|
top_level_defs,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
slice,
|
|
|
|
)?;
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TList { ty }))
|
|
|
|
} else if *id == tuple_id {
|
|
|
|
if let Tuple { elts, .. } = &slice.node {
|
|
|
|
let ty = elts
|
|
|
|
.iter()
|
|
|
|
.map(|elt| {
|
|
|
|
parse_type_annotation(
|
|
|
|
resolver,
|
|
|
|
top_level_defs,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
elt,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TTuple { ty }))
|
|
|
|
} else {
|
|
|
|
Err("Expected multiple elements for tuple".into())
|
2021-08-24 14:58:19 +08:00
|
|
|
}
|
2021-09-22 17:19:27 +08:00
|
|
|
} else {
|
|
|
|
let types = if let Tuple { elts, .. } = &slice.node {
|
|
|
|
elts.iter()
|
|
|
|
.map(|v| {
|
|
|
|
parse_type_annotation(
|
|
|
|
resolver,
|
|
|
|
top_level_defs,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
v,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
|
|
} else {
|
|
|
|
vec![parse_type_annotation(
|
2021-08-24 14:58:19 +08:00
|
|
|
resolver,
|
|
|
|
top_level_defs,
|
|
|
|
unifier,
|
|
|
|
primitives,
|
|
|
|
slice,
|
2021-09-22 17:19:27 +08:00
|
|
|
)?]
|
|
|
|
};
|
2021-08-24 14:58:19 +08:00
|
|
|
|
2021-09-22 17:19:27 +08:00
|
|
|
let obj_id = resolver
|
|
|
|
.get_identifier_def(*id)
|
|
|
|
.ok_or_else(|| format!("Unknown type annotation {}", id))?;
|
|
|
|
let def = top_level_defs[obj_id.0].read();
|
|
|
|
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
|
|
|
if types.len() != type_vars.len() {
|
|
|
|
return Err(format!(
|
|
|
|
"Unexpected number of type parameters: expected {} but got {}",
|
|
|
|
type_vars.len(),
|
|
|
|
types.len()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
subst.insert(id, *ty);
|
2021-08-24 14:58:19 +08:00
|
|
|
}
|
2021-09-22 17:19:27 +08:00
|
|
|
let mut fields = fields
|
|
|
|
.iter()
|
2021-11-06 22:48:08 +08:00
|
|
|
.map(|(attr, ty, is_mutable)| {
|
2021-09-22 17:19:27 +08:00
|
|
|
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
2021-11-06 22:48:08 +08:00
|
|
|
(*attr, (ty, *is_mutable))
|
2021-09-22 17:19:27 +08:00
|
|
|
})
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
fields.extend(methods.iter().map(|(attr, ty, _)| {
|
|
|
|
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
2021-11-06 22:48:08 +08:00
|
|
|
(*attr, (ty, false))
|
2021-09-22 17:19:27 +08:00
|
|
|
}));
|
|
|
|
Ok(unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id,
|
|
|
|
fields: fields.into(),
|
|
|
|
params: subst.into(),
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Err("Cannot use function name as type".into())
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 14:44:50 +08:00
|
|
|
} else {
|
|
|
|
Err("unsupported type expression".into())
|
2021-08-11 17:28:29 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 14:44:50 +08:00
|
|
|
_ => Err("unsupported type expression".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>,
|
|
|
|
) -> Result<Type, 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
|
|
|
}
|
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
|
|
|
}
|