nac3/nac3core/src/toplevel/mod.rs

88 lines
3.0 KiB
Rust
Raw Normal View History

2021-09-08 19:45:36 +08:00
use std::{
borrow::BorrowMut,
collections::{HashMap, HashSet},
fmt::Debug,
iter::FromIterator,
ops::{Deref, DerefMut},
sync::Arc,
};
2021-08-03 13:38:27 +08:00
2021-08-05 14:55:23 +08:00
use super::typecheck::type_inferencer::PrimitiveStore;
use super::typecheck::typedef::{FunSignature, FuncArg, SharedUnifier, Type, TypeEnum, Unifier};
use crate::{
symbol_resolver::SymbolResolver,
typecheck::{type_inferencer::CodeLocation, typedef::CallId},
};
use itertools::{izip, Itertools};
2021-09-08 19:45:36 +08:00
use parking_lot::RwLock;
2021-08-10 21:57:31 +08:00
use rustpython_parser::ast::{self, Stmt};
2021-08-03 13:38:27 +08:00
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)]
pub struct DefinitionId(pub usize);
2021-08-23 02:52:54 +08:00
pub mod composer;
mod helper;
mod type_annotation;
use composer::*;
use type_annotation::*;
#[cfg(test)]
mod test;
2021-08-23 02:52:54 +08:00
#[derive(Clone, Debug)]
pub struct FunInstance {
pub body: Vec<Stmt<Option<Type>>>,
pub calls: HashMap<CodeLocation, CallId>,
pub subst: HashMap<u32, Type>,
pub unifier_id: usize,
}
#[derive(Debug, Clone)]
2021-08-03 13:38:27 +08:00
pub enum TopLevelDef {
Class {
// name for error messages and symbols
name: String,
2021-08-03 13:38:27 +08:00
// object ID used for TypeEnum
object_id: DefinitionId,
2021-08-27 01:39:15 +08:00
/// type variables bounded to the class.
type_vars: Vec<Type>,
2021-08-07 15:06:39 +08:00
// class fields
2021-08-03 13:38:27 +08:00
fields: Vec<(String, Type)>,
// class methods, pointing to the corresponding function definition.
2021-08-07 15:06:39 +08:00
methods: Vec<(String, Type, DefinitionId)>,
2021-08-03 13:38:27 +08:00
// ancestor classes, including itself.
2021-08-23 02:52:54 +08:00
ancestors: Vec<TypeAnnotation>,
// symbol resolver of the module defined the class, none if it is built-in type
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
2021-09-19 22:54:06 +08:00
// constructor type
constructor: Option<Type>,
2021-08-03 13:38:27 +08:00
},
Function {
2021-08-07 15:06:39 +08:00
// prefix for symbol, should be unique globally, and not ending with numbers
name: String,
2021-09-19 22:54:06 +08:00
// simple name, the same as in method/function definition
simple_name: String,
2021-08-07 15:06:39 +08:00
// function signature.
2021-08-03 13:38:27 +08:00
signature: Type,
// instantiated type variable IDs
var_id: Vec<u32>,
2021-08-03 13:38:27 +08:00
/// Function instance to symbol mapping
/// Key: string representation of type variable values, sorted by variable ID in ascending
/// order, including type variables associated with the class.
/// Value: function symbol name.
instance_to_symbol: HashMap<String, String>,
/// Function instances to annotated AST mapping
/// Key: string representation of type variable values, sorted by variable ID in ascending
/// order, including type variables associated with the class. Excluding rigid type
/// variables.
/// rigid type variables that would be substituted when the function is instantiated.
instance_to_stmt: HashMap<String, FunInstance>,
// symbol resolver of the module defined the class
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
2021-08-03 13:38:27 +08:00
},
}
pub struct TopLevelContext {
pub definitions: Arc<RwLock<Vec<Arc<RwLock<TopLevelDef>>>>>,
2021-08-11 14:37:26 +08:00
pub unifiers: Arc<RwLock<Vec<(SharedUnifier, PrimitiveStore)>>>,
}