nac3/nac3core/src/typedef.rs

61 lines
1.3 KiB
Rust
Raw Normal View History

2020-12-31 11:26:51 +08:00
use std::collections::HashMap;
use std::rc::Rc;
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct PrimitiveId(pub(crate) usize);
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct ClassId(pub(crate) usize);
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct ParamId(pub(crate) usize);
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct VariableId(pub(crate) usize);
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
2020-12-31 15:31:59 +08:00
pub enum TypeEnum {
2020-12-31 11:26:51 +08:00
BotType,
SelfType,
PrimitiveType(PrimitiveId),
ClassType(ClassId),
VirtualClassType(ClassId),
2020-12-31 15:31:59 +08:00
ParametricType(ParamId, Vec<Rc<TypeEnum>>),
2020-12-31 11:26:51 +08:00
TypeVariable(VariableId),
}
2020-12-31 15:31:59 +08:00
pub type Type = Rc<TypeEnum>;
2020-12-31 11:26:51 +08:00
#[derive(Clone)]
pub struct FnDef {
// we assume methods first argument to be SelfType,
// so the first argument is not contained here
2020-12-31 15:31:59 +08:00
pub args: Vec<Type>,
pub result: Option<Type>,
2020-12-31 11:26:51 +08:00
}
#[derive(Clone)]
pub struct TypeDef<'a> {
pub name: &'a str,
2020-12-31 15:31:59 +08:00
pub fields: HashMap<&'a str, Type>,
2020-12-31 11:26:51 +08:00
pub methods: HashMap<&'a str, FnDef>,
}
#[derive(Clone)]
pub struct ClassDef<'a> {
pub base: TypeDef<'a>,
pub parents: Vec<ClassId>,
}
#[derive(Clone)]
pub struct ParametricDef<'a> {
pub base: TypeDef<'a>,
pub params: Vec<VariableId>,
}
#[derive(Clone)]
pub struct VarDef<'a> {
pub name: &'a str,
2020-12-31 15:31:59 +08:00
pub bound: Vec<Type>,
2020-12-31 11:26:51 +08:00
}