minor fix to monomorphize

This commit is contained in:
Kasey White 2022-12-16 06:16:02 -05:00 committed by Lucas
parent 2bce818110
commit 47fae21af7
3 changed files with 67 additions and 35 deletions

View File

@ -925,7 +925,7 @@ pub fn monomorphize(
) -> (String, Vec<Air>) { ) -> (String, Vec<Air>) {
let mut new_air = ir.clone(); let mut new_air = ir.clone();
let mut new_name = String::new(); let mut new_name = String::new();
let mut needs_variant = false;
for (index, ir) in ir.into_iter().enumerate() { for (index, ir) in ir.into_iter().enumerate() {
match ir { match ir {
Air::Var { Air::Var {
@ -960,6 +960,7 @@ pub fn monomorphize(
name, name,
variant_name: variant, variant_name: variant,
}; };
needs_variant = true;
} }
} }
Air::List { Air::List {
@ -978,6 +979,7 @@ pub fn monomorphize(
tipo, tipo,
tail, tail,
}; };
needs_variant = false;
} }
} }
Air::ListAccessor { Air::ListAccessor {
@ -996,6 +998,7 @@ pub fn monomorphize(
tipo, tipo,
tail, tail,
}; };
needs_variant = false;
} }
} }
Air::ListExpose { Air::ListExpose {
@ -1014,6 +1017,7 @@ pub fn monomorphize(
tipo, tipo,
tail, tail,
}; };
needs_variant = false;
} }
} }
@ -1033,6 +1037,7 @@ pub fn monomorphize(
tipo, tipo,
count, count,
}; };
needs_variant = false;
} }
} }
Air::Builtin { scope, func, tipo } => { Air::Builtin { scope, func, tipo } => {
@ -1041,6 +1046,7 @@ pub fn monomorphize(
find_generics_to_replace(&mut tipo, &generic_types); find_generics_to_replace(&mut tipo, &generic_types);
new_air[index] = Air::Builtin { scope, func, tipo }; new_air[index] = Air::Builtin { scope, func, tipo };
needs_variant = false;
} }
} }
// TODO check on assignment if type is needed // TODO check on assignment if type is needed
@ -1059,6 +1065,7 @@ pub fn monomorphize(
subject_name, subject_name,
tipo, tipo,
}; };
needs_variant = false;
} }
} }
Air::Clause { Air::Clause {
@ -1077,6 +1084,7 @@ pub fn monomorphize(
subject_name, subject_name,
complex_clause, complex_clause,
}; };
needs_variant = false;
} }
} }
Air::ListClause { Air::ListClause {
@ -1097,6 +1105,7 @@ pub fn monomorphize(
complex_clause, complex_clause,
next_tail_name, next_tail_name,
}; };
needs_variant = false;
} }
} }
Air::ClauseGuard { Air::ClauseGuard {
@ -1113,6 +1122,7 @@ pub fn monomorphize(
subject_name, subject_name,
tipo, tipo,
}; };
needs_variant = false;
} }
} }
Air::RecordAccess { Air::RecordAccess {
@ -1129,6 +1139,7 @@ pub fn monomorphize(
index: record_index, index: record_index,
tipo, tipo,
}; };
needs_variant = false;
} }
} }
Air::FieldsExpose { Air::FieldsExpose {
@ -1141,6 +1152,7 @@ pub fn monomorphize(
if tipo.is_generic() { if tipo.is_generic() {
let mut tipo = tipo.clone(); let mut tipo = tipo.clone();
find_generics_to_replace(&mut tipo, &generic_types); find_generics_to_replace(&mut tipo, &generic_types);
needs_variant = false;
} }
new_indices.push((ind, name, tipo)); new_indices.push((ind, name, tipo));
} }
@ -1156,6 +1168,7 @@ pub fn monomorphize(
find_generics_to_replace(&mut tipo, &generic_types); find_generics_to_replace(&mut tipo, &generic_types);
new_air[index] = Air::Tuple { scope, count, tipo }; new_air[index] = Air::Tuple { scope, count, tipo };
needs_variant = false;
} }
} }
Air::Todo { scope, label, tipo } => { Air::Todo { scope, label, tipo } => {
@ -1164,6 +1177,7 @@ pub fn monomorphize(
find_generics_to_replace(&mut tipo, &generic_types); find_generics_to_replace(&mut tipo, &generic_types);
new_air[index] = Air::Todo { scope, label, tipo }; new_air[index] = Air::Todo { scope, label, tipo };
needs_variant = false;
} }
} }
Air::RecordUpdate { .. } => todo!(), Air::RecordUpdate { .. } => todo!(),
@ -1173,7 +1187,7 @@ pub fn monomorphize(
} }
if let Type::Fn { args, .. } = &**full_type { if let Type::Fn { args, .. } = &**full_type {
if full_type.is_generic() { if needs_variant {
for arg in args { for arg in args {
get_variant_name(&mut new_name, arg); get_variant_name(&mut new_name, arg);
} }

View File

@ -193,6 +193,14 @@ impl Type {
} }
} }
pub fn arg_types(&self) -> Option<Vec<Arc<Self>>> {
match self {
Self::Fn { args, .. } => Some(args.clone()),
Self::App { args, .. } => Some(args.clone()),
_ => None,
}
}
pub fn get_generic(&self) -> Option<u64> { pub fn get_generic(&self) -> Option<u64> {
match self { match self {
Type::Var { tipo } => tipo.borrow().get_generic(), Type::Var { tipo } => tipo.borrow().get_generic(),
@ -457,6 +465,13 @@ impl TypeVar {
pub fn get_inner_type(&self) -> Vec<Arc<Type>> { pub fn get_inner_type(&self) -> Vec<Arc<Type>> {
match self { match self {
Self::Link { tipo } => tipo.get_inner_types(), Self::Link { tipo } => tipo.get_inner_types(),
var @ Self::Generic { .. } => {
let tipos = vec![Type::Var {
tipo: RefCell::new(var.clone()).into(),
}
.into()];
tipos
}
_ => vec![], _ => vec![],
} }
} }

View File

@ -187,6 +187,7 @@ impl<'a> CodeGenerator<'a> {
scope, scope,
params: arg_names, params: arg_names,
}); });
ir_stack.append(&mut func_body); ir_stack.append(&mut func_body);
} }
TypedExpr::List { TypedExpr::List {
@ -1552,7 +1553,8 @@ impl<'a> CodeGenerator<'a> {
let to_insert = temp_func_index_map let to_insert = temp_func_index_map
.iter() .iter()
.filter(|func| { .filter(|func| {
func.1.clone() == ir.scope() && !self.defined_functions.contains_key(func.0) get_common_ancestor(func.1, &ir.scope()) == ir.scope()
&& !self.defined_functions.contains_key(func.0)
}) })
.collect_vec(); .collect_vec();
@ -1712,11 +1714,43 @@ impl<'a> CodeGenerator<'a> {
} = &constructor.variant } = &constructor.variant
{ {
if builtin.is_none() { if builtin.is_none() {
let mut function_key = FunctionAccessKey { let non_variant_function_key = FunctionAccessKey {
module_name: module.clone(), module_name: module.clone(),
function_name: name.clone(), function_name: name.clone(),
variant_name: String::new(), variant_name: String::new(),
}; };
let function = self.functions.get(&non_variant_function_key).unwrap();
let mut func_ir = vec![];
self.build_ir(&function.body, &mut func_ir, scope.to_vec());
let param_types = constructor.tipo.arg_types().unwrap();
let mut generics_type_map: HashMap<u64, Arc<Type>> = HashMap::new();
for (index, arg) in function.arguments.iter().enumerate() {
if arg.tipo.is_generic() {
let mut map = generics_type_map.into_iter().collect_vec();
map.append(&mut get_generics_and_type(
&arg.tipo,
&param_types[index],
));
generics_type_map = map.into_iter().collect();
}
}
let (variant_name, mut func_ir) =
monomorphize(func_ir, generics_type_map, &constructor.tipo);
let function_key = FunctionAccessKey {
module_name: module.clone(),
function_name: non_variant_function_key.function_name,
variant_name: variant_name.clone(),
};
if let Some(scope_prev) = to_be_defined_map.get(&function_key) { if let Some(scope_prev) = to_be_defined_map.get(&function_key) {
let new_scope = get_common_ancestor(scope, scope_prev); let new_scope = get_common_ancestor(scope, scope_prev);
@ -1724,37 +1758,6 @@ impl<'a> CodeGenerator<'a> {
} else if func_components.get(&function_key).is_some() { } else if func_components.get(&function_key).is_some() {
to_be_defined_map.insert(function_key.clone(), scope.to_vec()); to_be_defined_map.insert(function_key.clone(), scope.to_vec());
} else { } else {
let function = self.functions.get(&function_key).unwrap();
let mut func_ir = vec![];
self.build_ir(&function.body, &mut func_ir, scope.to_vec());
let (param_types, _) = constructor.tipo.function_types().unwrap();
let mut generics_type_map: HashMap<u64, Arc<Type>> = HashMap::new();
for (index, arg) in function.arguments.iter().enumerate() {
if arg.tipo.is_generic() {
let mut map = generics_type_map.into_iter().collect_vec();
map.append(&mut get_generics_and_type(
&arg.tipo,
&param_types[index],
));
generics_type_map = map.into_iter().collect();
}
}
let (variant_name, mut func_ir) =
monomorphize(func_ir, generics_type_map, &constructor.tipo);
function_key = FunctionAccessKey {
module_name: module.clone(),
function_name: function_key.function_name,
variant_name: variant_name.clone(),
};
to_be_defined_map.insert(function_key.clone(), scope.to_vec()); to_be_defined_map.insert(function_key.clone(), scope.to_vec());
let mut func_calls = vec![]; let mut func_calls = vec![];