2021-09-08 19:45:36 +08:00
|
|
|
use std::{
|
|
|
|
borrow::BorrowMut,
|
|
|
|
collections::{HashMap, HashSet},
|
|
|
|
fmt::Debug,
|
|
|
|
iter::FromIterator,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-08-03 13:38:27 +08:00
|
|
|
|
2024-10-03 12:37:56 +08:00
|
|
|
use inkwell::values::BasicValueEnum;
|
|
|
|
use itertools::Itertools;
|
|
|
|
use parking_lot::RwLock;
|
|
|
|
|
2024-10-03 14:21:11 +08:00
|
|
|
use nac3parser::ast::{self, Expr, Location, Stmt, StrRef};
|
2024-10-03 12:37:56 +08:00
|
|
|
|
2021-08-25 15:29:58 +08:00
|
|
|
use crate::{
|
2024-10-03 12:37:56 +08:00
|
|
|
codegen::{CodeGenContext, CodeGenerator},
|
2022-02-12 21:00:12 +08:00
|
|
|
symbol_resolver::{SymbolResolver, ValueEnum},
|
2024-06-13 13:28:39 +08:00
|
|
|
typecheck::{
|
2024-10-03 12:37:56 +08:00
|
|
|
type_inferencer::{CodeLocation, PrimitiveStore},
|
|
|
|
typedef::{
|
|
|
|
CallId, FunSignature, FuncArg, SharedUnifier, Type, TypeEnum, TypeVarId, Unifier,
|
|
|
|
VarMap,
|
|
|
|
},
|
2024-06-13 13:28:39 +08:00
|
|
|
},
|
2021-08-25 15:29:58 +08:00
|
|
|
};
|
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
|
|
|
|
2022-02-12 21:00:12 +08:00
|
|
|
pub mod builtins;
|
2021-09-12 05:10:10 +08:00
|
|
|
pub mod composer;
|
2021-11-23 07:32:09 +08:00
|
|
|
pub mod helper;
|
2023-11-17 17:30:27 +08:00
|
|
|
pub mod numpy;
|
2021-12-01 22:44:53 +08:00
|
|
|
pub 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
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
type GenCallCallback = dyn for<'ctx, 'a> Fn(
|
|
|
|
&mut CodeGenContext<'ctx, 'a>,
|
|
|
|
Option<(Type, ValueEnum<'ctx>)>,
|
|
|
|
(&FunSignature, DefinitionId),
|
|
|
|
Vec<(Option<StrRef>, ValueEnum<'ctx>)>,
|
|
|
|
&mut dyn CodeGenerator,
|
|
|
|
) -> Result<Option<BasicValueEnum<'ctx>>, String>
|
|
|
|
+ Send
|
|
|
|
+ Sync;
|
2021-09-30 17:07:48 +08:00
|
|
|
|
|
|
|
pub struct GenCall {
|
2024-04-23 14:35:11 +08:00
|
|
|
fp: Box<GenCallCallback>,
|
2021-09-30 17:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GenCall {
|
2023-12-08 17:43:32 +08:00
|
|
|
#[must_use]
|
2024-04-23 14:35:11 +08:00
|
|
|
pub fn new(fp: Box<GenCallCallback>) -> GenCall {
|
2021-09-30 17:07:48 +08:00
|
|
|
GenCall { fp }
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
/// Creates a dummy instance of [`GenCall`], which invokes [`unreachable!()`] with the given
|
2024-04-23 14:36:29 +08:00
|
|
|
/// `reason`.
|
|
|
|
#[must_use]
|
|
|
|
pub fn create_dummy(reason: String) -> GenCall {
|
|
|
|
Self::new(Box::new(move |_, _, _, _, _| unreachable!("{reason}")))
|
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
pub fn run<'ctx>(
|
2021-09-30 17:07:48 +08:00
|
|
|
&self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2022-02-12 21:00:12 +08:00
|
|
|
obj: Option<(Type, ValueEnum<'ctx>)>,
|
2021-09-30 17:07:48 +08:00
|
|
|
fun: (&FunSignature, DefinitionId),
|
2022-02-12 21:00:12 +08:00
|
|
|
args: Vec<(Option<StrRef>, ValueEnum<'ctx>)>,
|
|
|
|
generator: &mut dyn CodeGenerator,
|
2022-02-21 17:52:34 +08:00
|
|
|
) -> Result<Option<BasicValueEnum<'ctx>>, String> {
|
2022-02-12 21:00:12 +08:00
|
|
|
(self.fp)(ctx, obj, fun, args, generator)
|
2021-09-30 17:07:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>>,
|
2024-03-04 23:38:52 +08:00
|
|
|
pub subst: VarMap,
|
2021-08-25 15:29:58 +08:00
|
|
|
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 {
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Name for error messages and symbols.
|
2021-09-22 17:19:27 +08:00
|
|
|
name: StrRef,
|
2024-06-21 14:14:01 +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>,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Class fields.
|
|
|
|
///
|
|
|
|
/// Name and type is mutable.
|
2021-11-06 22:48:08 +08:00
|
|
|
fields: Vec<(StrRef, Type, bool)>,
|
2024-06-19 16:35:03 +08:00
|
|
|
/// Class Attributes.
|
|
|
|
///
|
|
|
|
/// Name, type, value.
|
|
|
|
attributes: Vec<(StrRef, Type, ast::Constant)>,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Class methods, pointing to the corresponding function definition.
|
2021-09-22 17:19:27 +08:00
|
|
|
methods: Vec<(StrRef, Type, DefinitionId)>,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Ancestor classes, including itself.
|
2021-08-23 02:52:54 +08:00
|
|
|
ancestors: Vec<TypeAnnotation>,
|
2023-10-10 13:37:59 +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>>,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Constructor type.
|
2021-09-19 22:54:06 +08:00
|
|
|
constructor: Option<Type>,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Definition location.
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: Option<Location>,
|
2021-08-03 13:38:27 +08:00
|
|
|
},
|
|
|
|
Function {
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Prefix for symbol, should be unique globally.
|
2021-08-07 15:06:39 +08:00
|
|
|
name: String,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Simple name, the same as in method/function definition.
|
2021-09-22 17:19:27 +08:00
|
|
|
simple_name: StrRef,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Function signature.
|
2021-08-03 13:38:27 +08:00
|
|
|
signature: Type,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Instantiated type variable IDs.
|
2024-06-13 13:28:39 +08:00
|
|
|
var_id: Vec<TypeVarId>,
|
2021-08-03 13:38:27 +08:00
|
|
|
/// Function instance to symbol mapping
|
2023-10-10 13:37:59 +08:00
|
|
|
///
|
|
|
|
/// * Key: String representation of type variable values, sorted by variable ID in ascending
|
2024-08-21 11:10:52 +08:00
|
|
|
/// order, including type variables associated with the class.
|
2023-10-10 13:37:59 +08:00
|
|
|
/// * Value: Function symbol name.
|
2021-08-03 13:38:27 +08:00
|
|
|
instance_to_symbol: HashMap<String, String>,
|
|
|
|
/// Function instances to annotated AST mapping
|
2023-10-10 13:37:59 +08:00
|
|
|
///
|
|
|
|
/// * Key: String representation of type variable values, sorted by variable ID in ascending
|
2024-08-21 11:10:52 +08:00
|
|
|
/// order, including type variables associated with the class. Excluding rigid type
|
|
|
|
/// variables.
|
2023-10-10 13:37:59 +08:00
|
|
|
///
|
|
|
|
/// 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>,
|
2023-10-10 13:37:59 +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>>,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Custom code generation callback.
|
2022-02-12 21:00:12 +08:00
|
|
|
codegen_callback: Option<Arc<GenCall>>,
|
2023-10-10 13:37:59 +08:00
|
|
|
/// Definition location.
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: Option<Location>,
|
2021-08-03 13:38:27 +08:00
|
|
|
},
|
2024-10-03 14:21:11 +08:00
|
|
|
Variable {
|
|
|
|
/// Qualified name of the global variable, should be unique globally.
|
|
|
|
name: String,
|
|
|
|
|
|
|
|
/// Simple name, the same as in method/function definition.
|
|
|
|
simple_name: StrRef,
|
|
|
|
|
|
|
|
/// Type of the global variable.
|
|
|
|
ty: Type,
|
|
|
|
|
|
|
|
/// The declared type of the global variable.
|
|
|
|
ty_decl: Expr,
|
|
|
|
|
|
|
|
/// Symbol resolver of the module defined the class.
|
|
|
|
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
|
|
|
|
|
|
|
|
/// Definition location.
|
|
|
|
loc: Option<Location>,
|
|
|
|
},
|
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
|
|
|
}
|