nac3core: top level use codegen official get_subst_key

This commit is contained in:
ychenfo 2021-11-10 23:38:47 +08:00
parent c2706fa720
commit a9635f0979
2 changed files with 35 additions and 33 deletions

View File

@ -7,7 +7,7 @@ use crate::{
},
symbol_resolver::SymbolValue,
toplevel::{DefinitionId, TopLevelDef},
typecheck::typedef::{FunSignature, FuncArg, Type, TypeEnum},
typecheck::typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier},
};
use inkwell::{
types::{BasicType, BasicTypeEnum},
@ -21,6 +21,31 @@ use nac3parser::ast::{
use super::CodeGenerator;
pub fn get_subst_key(
unifier: &mut Unifier,
obj: Option<Type>,
fun_vars: &HashMap<u32, Type>,
filter: Option<&Vec<u32>>,
) -> String {
let mut vars = obj
.map(|ty| {
if let TypeEnum::TObj { params, .. } = &*unifier.get_ty(ty) {
params.borrow().clone()
} else {
unreachable!()
}
})
.unwrap_or_default();
vars.extend(fun_vars.iter());
let sorted =
vars.keys().filter(|id| filter.map(|v| v.contains(id)).unwrap_or(true)).sorted();
sorted
.map(|id| {
unifier.stringify(vars[id], &mut |id| id.to_string(), &mut |id| id.to_string())
})
.join(", ")
}
impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
pub fn build_gep_and_load(
&mut self,
@ -36,23 +61,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
fun: &FunSignature,
filter: Option<&Vec<u32>>,
) -> String {
let mut vars = obj
.map(|ty| {
if let TypeEnum::TObj { params, .. } = &*self.unifier.get_ty(ty) {
params.borrow().clone()
} else {
unreachable!()
}
})
.unwrap_or_default();
vars.extend(fun.vars.iter());
let sorted =
vars.keys().filter(|id| filter.map(|v| v.contains(id)).unwrap_or(true)).sorted();
sorted
.map(|id| {
self.unifier.stringify(vars[id], &mut |id| id.to_string(), &mut |id| id.to_string())
})
.join(", ")
get_subst_key(&mut self.unifier, obj, &fun.vars, filter)
}
pub fn get_attr_index(&mut self, ty: Type, attr: StrRef) -> usize {

View File

@ -6,6 +6,7 @@ use inkwell::FloatPredicate;
use crate::{
symbol_resolver::SymbolValue,
typecheck::type_inferencer::{FunctionData, Inferencer},
codegen::expr::get_subst_key,
};
use super::*;
@ -1835,21 +1836,13 @@ impl TopLevelComposer {
instance_to_stmt.insert(
// NOTE: refer to codegen/expr/get_subst_key function
{
let unifier = &mut self.unifier;
subst
.keys()
.sorted()
.map(|id| {
let ty = subst.get(id).unwrap();
unifier.stringify(
*ty,
&mut |id| id.to_string(),
&mut |id| id.to_string(),
)
})
.join(", ")
},
get_subst_key(
&mut self.unifier,
self_type,
&subst,
None
),
FunInstance {
body: Arc::new(fun_body),
unifier_id: 0,