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-09-30 17:07:48 +08:00
|
|
|
use super::codegen::CodeGenContext;
|
2021-08-05 14:55:23 +08:00
|
|
|
use super::typecheck::type_inferencer::PrimitiveStore;
|
2021-08-24 17:43:41 +08:00
|
|
|
use super::typecheck::typedef::{FunSignature, FuncArg, SharedUnifier, Type, TypeEnum, Unifier};
|
2021-08-25 15:29:58 +08:00
|
|
|
use crate::{
|
|
|
|
symbol_resolver::SymbolResolver,
|
|
|
|
typecheck::{type_inferencer::CodeLocation, typedef::CallId},
|
|
|
|
};
|
2021-09-08 02:27:12 +08:00
|
|
|
use itertools::{izip, Itertools};
|
2021-09-08 19:45:36 +08:00
|
|
|
use parking_lot::RwLock;
|
2021-11-03 17:11:00 +08:00
|
|
|
use nac3parser::ast::{self, Stmt, StrRef};
|
2021-09-30 17:07:48 +08:00
|
|
|
use inkwell::values::BasicValueEnum;
|
2021-08-03 13:38:27 +08:00
|
|
|
|
2021-09-07 00:20:40 +08:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)]
|
2021-08-24 17:14:34 +08:00
|
|
|
pub struct DefinitionId(pub usize);
|
2021-08-23 02:52:54 +08:00
|
|
|
|
2021-09-12 05:10:10 +08:00
|
|
|
pub mod composer;
|
2021-11-23 07:32:09 +08:00
|
|
|
pub mod helper;
|
2021-08-24 17:14:34 +08:00
|
|
|
mod type_annotation;
|
2021-09-12 05:10:10 +08:00
|
|
|
use composer::*;
|
2021-08-24 17:14:34 +08:00
|
|
|
use type_annotation::*;
|
2021-08-31 15:22:45 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
2021-08-23 02:52:54 +08:00
|
|
|
|
2021-09-30 17:07:48 +08:00
|
|
|
type GenCallCallback = Box<
|
|
|
|
dyn for<'ctx, 'a> Fn(
|
|
|
|
&mut CodeGenContext<'ctx, 'a>,
|
|
|
|
Option<(Type, BasicValueEnum)>,
|
|
|
|
(&FunSignature, DefinitionId),
|
|
|
|
Vec<(Option<StrRef>, BasicValueEnum<'ctx>)>,
|
|
|
|
) -> Option<BasicValueEnum<'ctx>>
|
|
|
|
+ Send
|
|
|
|
+ Sync,
|
|
|
|
>;
|
|
|
|
|
|
|
|
pub struct GenCall {
|
|
|
|
fp: GenCallCallback,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenCall {
|
|
|
|
pub fn new(fp: GenCallCallback) -> GenCall {
|
|
|
|
GenCall { fp }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run<'ctx, 'a>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
|
|
|
obj: Option<(Type, BasicValueEnum<'ctx>)>,
|
|
|
|
fun: (&FunSignature, DefinitionId),
|
|
|
|
args: Vec<(Option<StrRef>, BasicValueEnum<'ctx>)>,
|
|
|
|
) -> Option<BasicValueEnum<'ctx>> {
|
|
|
|
(self.fp)(ctx, obj, fun, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for GenCall {
|
|
|
|
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 00:20:40 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2021-08-25 15:29:58 +08:00
|
|
|
pub struct FunInstance {
|
2021-09-22 16:04:25 +08:00
|
|
|
pub body: Arc<Vec<Stmt<Option<Type>>>>,
|
|
|
|
pub calls: Arc<HashMap<CodeLocation, CallId>>,
|
2021-08-25 15:29:58 +08:00
|
|
|
pub subst: HashMap<u32, Type>,
|
|
|
|
pub unifier_id: usize,
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:16:48 +08:00
|
|
|
#[derive(Debug, Clone)]
|
2021-08-03 13:38:27 +08:00
|
|
|
pub enum TopLevelDef {
|
|
|
|
Class {
|
2021-08-21 14:51:46 +08:00
|
|
|
// name for error messages and symbols
|
2021-09-22 17:19:27 +08:00
|
|
|
name: StrRef,
|
2021-08-03 13:38:27 +08:00
|
|
|
// object ID used for TypeEnum
|
2021-08-06 10:30:57 +08:00
|
|
|
object_id: DefinitionId,
|
2021-08-27 01:39:15 +08:00
|
|
|
/// type variables bounded to the class.
|
2021-08-29 18:19:29 +08:00
|
|
|
type_vars: Vec<Type>,
|
2021-08-07 15:06:39 +08:00
|
|
|
// class fields
|
2021-11-06 22:48:08 +08:00
|
|
|
// name, type, is mutable
|
|
|
|
fields: Vec<(StrRef, Type, bool)>,
|
2021-08-03 13:38:27 +08:00
|
|
|
// class methods, pointing to the corresponding function definition.
|
2021-09-22 17:19:27 +08:00
|
|
|
methods: Vec<(StrRef, 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>,
|
2021-08-11 15:11:51 +08:00
|
|
|
// symbol resolver of the module defined the class, none if it is built-in type
|
2021-10-16 18:08:13 +08:00
|
|
|
resolver: Option<Arc<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-09-22 14:45:42 +08:00
|
|
|
// prefix for symbol, should be unique globally
|
2021-08-07 15:06:39 +08:00
|
|
|
name: String,
|
2021-09-19 22:54:06 +08:00
|
|
|
// simple name, the same as in method/function definition
|
2021-09-22 17:19:27 +08:00
|
|
|
simple_name: StrRef,
|
2021-08-07 15:06:39 +08:00
|
|
|
// function signature.
|
2021-08-03 13:38:27 +08:00
|
|
|
signature: Type,
|
2021-08-25 15:29:58 +08:00
|
|
|
// 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.
|
2021-08-25 15:29:58 +08:00
|
|
|
instance_to_stmt: HashMap<String, FunInstance>,
|
2021-08-11 15:11:51 +08:00
|
|
|
// symbol resolver of the module defined the class
|
2021-10-16 18:08:13 +08:00
|
|
|
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
|
2021-09-30 17:07:48 +08:00
|
|
|
// custom codegen callback
|
|
|
|
codegen_callback: Option<Arc<GenCall>>
|
2021-08-03 13:38:27 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TopLevelContext {
|
2021-08-23 10:34:11 +08:00
|
|
|
pub definitions: Arc<RwLock<Vec<Arc<RwLock<TopLevelDef>>>>>,
|
2021-08-11 14:37:26 +08:00
|
|
|
pub unifiers: Arc<RwLock<Vec<(SharedUnifier, PrimitiveStore)>>>,
|
2021-09-25 21:44:00 +08:00
|
|
|
pub personality_symbol: Option<String>,
|
2021-08-03 14:11:41 +08:00
|
|
|
}
|