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, symbol_resolver::SymbolValue,
toplevel::{DefinitionId, TopLevelDef}, toplevel::{DefinitionId, TopLevelDef},
typecheck::typedef::{FunSignature, FuncArg, Type, TypeEnum}, typecheck::typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier},
}; };
use inkwell::{ use inkwell::{
types::{BasicType, BasicTypeEnum}, types::{BasicType, BasicTypeEnum},
@ -21,6 +21,31 @@ use nac3parser::ast::{
use super::CodeGenerator; 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> { impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
pub fn build_gep_and_load( pub fn build_gep_and_load(
&mut self, &mut self,
@ -36,23 +61,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
fun: &FunSignature, fun: &FunSignature,
filter: Option<&Vec<u32>>, filter: Option<&Vec<u32>>,
) -> String { ) -> String {
let mut vars = obj get_subst_key(&mut self.unifier, obj, &fun.vars, filter)
.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(", ")
} }
pub fn get_attr_index(&mut self, ty: Type, attr: StrRef) -> usize { pub fn get_attr_index(&mut self, ty: Type, attr: StrRef) -> usize {

View File

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