2021-08-03 14:11:41 +08:00
|
|
|
use std::{collections::HashMap, 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::{SharedUnifier, Type, Unifier};
|
2021-08-07 10:28:41 +08:00
|
|
|
use crate::symbol_resolver::SymbolResolver;
|
2021-08-07 15:06:39 +08:00
|
|
|
use inkwell::{builder::Builder, context::Context, module::Module, types::BasicTypeEnum, values::PointerValue};
|
2021-08-03 14:11:41 +08:00
|
|
|
use parking_lot::RwLock;
|
2021-08-03 13:38:27 +08:00
|
|
|
use rustpython_parser::ast::Stmt;
|
|
|
|
|
2021-08-06 10:30:57 +08:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
|
|
|
pub struct DefinitionId(pub usize);
|
2021-08-03 13:38:27 +08:00
|
|
|
|
|
|
|
pub enum TopLevelDef {
|
|
|
|
Class {
|
|
|
|
// object ID used for TypeEnum
|
2021-08-06 10:30:57 +08:00
|
|
|
object_id: DefinitionId,
|
2021-08-03 13:38:27 +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.
|
|
|
|
ancestors: Vec<DefinitionId>,
|
|
|
|
},
|
|
|
|
Function {
|
2021-08-07 15:06:39 +08:00
|
|
|
// prefix for symbol, should be unique globally, and not ending with numbers
|
|
|
|
name: String,
|
|
|
|
// function signature.
|
2021-08-03 13:38:27 +08:00
|
|
|
signature: Type,
|
|
|
|
/// 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.
|
|
|
|
/// Value: AST annotated with types together with a unification table index. Could contain
|
|
|
|
/// rigid type variables that would be substituted when the function is instantiated.
|
2021-08-05 14:55:23 +08:00
|
|
|
instance_to_stmt: HashMap<String, (Stmt<Option<Type>>, usize)>,
|
2021-08-03 13:38:27 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CodeGenTask {
|
|
|
|
pub subst: HashMap<usize, Type>,
|
|
|
|
pub symbol_name: String,
|
2021-08-05 14:55:23 +08:00
|
|
|
pub body: Stmt<Option<Type>>,
|
2021-08-03 13:38:27 +08:00
|
|
|
pub unifier: SharedUnifier,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TopLevelContext {
|
2021-08-05 14:55:23 +08:00
|
|
|
pub definitions: Arc<RwLock<Vec<RwLock<TopLevelDef>>>>,
|
|
|
|
pub unifiers: Arc<RwLock<Vec<SharedUnifier>>>,
|
2021-08-03 13:38:27 +08:00
|
|
|
}
|
2021-08-03 14:11:41 +08:00
|
|
|
|
2021-08-05 14:55:23 +08:00
|
|
|
pub struct CodeGenContext<'ctx> {
|
|
|
|
pub ctx: &'ctx Context,
|
|
|
|
pub builder: Builder<'ctx>,
|
|
|
|
pub module: Module<'ctx>,
|
|
|
|
pub top_level: &'ctx TopLevelContext,
|
2021-08-03 14:11:41 +08:00
|
|
|
pub unifier: Unifier,
|
2021-08-05 14:55:23 +08:00
|
|
|
pub resolver: Box<dyn SymbolResolver>,
|
|
|
|
pub var_assignment: HashMap<String, PointerValue<'ctx>>,
|
2021-08-07 15:06:39 +08:00
|
|
|
pub type_cache: HashMap<Type, BasicTypeEnum<'ctx>>,
|
|
|
|
pub primitives: PrimitiveStore,
|
2021-08-03 14:11:41 +08:00
|
|
|
}
|