From 47fae21af77f8f424c5e700a0a406b3b6e29b9ef Mon Sep 17 00:00:00 2001 From: Kasey White Date: Fri, 16 Dec 2022 06:16:02 -0500 Subject: [PATCH] minor fix to monomorphize --- crates/lang/src/builder.rs | 18 ++++++++-- crates/lang/src/tipo.rs | 15 +++++++++ crates/lang/src/uplc.rs | 69 ++++++++++++++++++++------------------ 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/crates/lang/src/builder.rs b/crates/lang/src/builder.rs index 50c3bacd..0bd4dce0 100644 --- a/crates/lang/src/builder.rs +++ b/crates/lang/src/builder.rs @@ -925,7 +925,7 @@ pub fn monomorphize( ) -> (String, Vec) { 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); } diff --git a/crates/lang/src/tipo.rs b/crates/lang/src/tipo.rs index 6b457ff3..48a10f74 100644 --- a/crates/lang/src/tipo.rs +++ b/crates/lang/src/tipo.rs @@ -193,6 +193,14 @@ impl Type { } } + pub fn arg_types(&self) -> Option>> { + match self { + Self::Fn { args, .. } => Some(args.clone()), + Self::App { args, .. } => Some(args.clone()), + _ => None, + } + } + pub fn get_generic(&self) -> Option { match self { Type::Var { tipo } => tipo.borrow().get_generic(), @@ -457,6 +465,13 @@ impl TypeVar { pub fn get_inner_type(&self) -> Vec> { 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![], } } diff --git a/crates/lang/src/uplc.rs b/crates/lang/src/uplc.rs index 6a17ffc3..84cfd18c 100644 --- a/crates/lang/src/uplc.rs +++ b/crates/lang/src/uplc.rs @@ -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,11 +1714,43 @@ 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(), }; + + 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> = 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, + ¶m_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) { 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() { to_be_defined_map.insert(function_key.clone(), scope.to_vec()); } 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> = 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, - ¶m_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()); let mut func_calls = vec![];