diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 4ad363c7..b267d330 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -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, + fun_vars: &HashMap, + filter: Option<&Vec>, +) -> 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>, ) -> 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 { diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 57ca2643..8dd5404c 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -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,