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

View File

@ -187,6 +187,7 @@ impl<'a> CodeGenerator<'a> {
scope,
params: arg_names,
});
ir_stack.append(&mut func_body);
}
TypedExpr::List {
@ -1552,7 +1553,8 @@ impl<'a> CodeGenerator<'a> {
let to_insert = temp_func_index_map
.iter()
.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();
@ -1712,25 +1714,19 @@ impl<'a> CodeGenerator<'a> {
} = &constructor.variant
{
if builtin.is_none() {
let mut function_key = FunctionAccessKey {
let non_variant_function_key = FunctionAccessKey {
module_name: module.clone(),
function_name: name.clone(),
variant_name: String::new(),
};
if let Some(scope_prev) = to_be_defined_map.get(&function_key) {
let new_scope = get_common_ancestor(scope, scope_prev);
to_be_defined_map.insert(function_key, new_scope);
} else if func_components.get(&function_key).is_some() {
to_be_defined_map.insert(function_key.clone(), scope.to_vec());
} else {
let function = self.functions.get(&function_key).unwrap();
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.function_types().unwrap();
let param_types = constructor.tipo.arg_types().unwrap();
let mut generics_type_map: HashMap<u64, Arc<Type>> = HashMap::new();
@ -1749,12 +1745,19 @@ impl<'a> CodeGenerator<'a> {
let (variant_name, mut func_ir) =
monomorphize(func_ir, generics_type_map, &constructor.tipo);
function_key = FunctionAccessKey {
let function_key = FunctionAccessKey {
module_name: module.clone(),
function_name: function_key.function_name,
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) {
let new_scope = get_common_ancestor(scope, scope_prev);
to_be_defined_map.insert(function_key, new_scope);
} else if func_components.get(&function_key).is_some() {
to_be_defined_map.insert(function_key.clone(), scope.to_vec());
} else {
to_be_defined_map.insert(function_key.clone(), scope.to_vec());
let mut func_calls = vec![];