rename find_generics_... to find_and_replace_generics

Other renames as well
This commit is contained in:
Kasey White 2023-02-27 00:55:57 -05:00 committed by Lucas
parent 451d9d8493
commit fe1f200e4d
2 changed files with 221 additions and 188 deletions

View File

@ -1129,10 +1129,11 @@ pub fn match_ir_for_recursion(
} }
} }
pub fn find_generics_to_replace(tipo: &mut Arc<Type>, generic_types: &IndexMap<u64, Arc<Type>>) { pub fn find_and_replace_generics(tipo: &mut Arc<Type>, mono_types: &IndexMap<u64, Arc<Type>>) {
if let Some(id) = tipo.get_generic() { if let Some(id) = tipo.get_generic() {
//If generic does not have a type we know of like a None in option then just use same type // If a generic does not have a type we know of
*tipo = generic_types.get(&id).unwrap_or(tipo).clone(); // like a None in option then just use same type
*tipo = mono_types.get(&id).unwrap_or(tipo).clone();
} else if tipo.is_generic() { } else if tipo.is_generic() {
match &**tipo { match &**tipo {
Type::App { Type::App {
@ -1144,7 +1145,7 @@ pub fn find_generics_to_replace(tipo: &mut Arc<Type>, generic_types: &IndexMap<u
let mut new_args = vec![]; let mut new_args = vec![];
for arg in args { for arg in args {
let mut arg = arg.clone(); let mut arg = arg.clone();
find_generics_to_replace(&mut arg, generic_types); find_and_replace_generics(&mut arg, mono_types);
new_args.push(arg); new_args.push(arg);
} }
let t = Type::App { let t = Type::App {
@ -1159,12 +1160,12 @@ pub fn find_generics_to_replace(tipo: &mut Arc<Type>, generic_types: &IndexMap<u
let mut new_args = vec![]; let mut new_args = vec![];
for arg in args { for arg in args {
let mut arg = arg.clone(); let mut arg = arg.clone();
find_generics_to_replace(&mut arg, generic_types); find_and_replace_generics(&mut arg, mono_types);
new_args.push(arg); new_args.push(arg);
} }
let mut ret = ret.clone(); let mut ret = ret.clone();
find_generics_to_replace(&mut ret, generic_types); find_and_replace_generics(&mut ret, mono_types);
let t = Type::Fn { let t = Type::Fn {
args: new_args, args: new_args,
@ -1176,7 +1177,7 @@ pub fn find_generics_to_replace(tipo: &mut Arc<Type>, generic_types: &IndexMap<u
let mut new_elems = vec![]; let mut new_elems = vec![];
for elem in elems { for elem in elems {
let mut elem = elem.clone(); let mut elem = elem.clone();
find_generics_to_replace(&mut elem, generic_types); find_and_replace_generics(&mut elem, mono_types);
new_elems.push(elem); new_elems.push(elem);
} }
let t = Type::Tuple { elems: new_elems }; let t = Type::Tuple { elems: new_elems };
@ -1188,7 +1189,7 @@ pub fn find_generics_to_replace(tipo: &mut Arc<Type>, generic_types: &IndexMap<u
TypeVar::Unbound { .. } => todo!(), TypeVar::Unbound { .. } => todo!(),
TypeVar::Link { tipo } => { TypeVar::Link { tipo } => {
let mut tipo = tipo; let mut tipo = tipo;
find_generics_to_replace(&mut tipo, generic_types); find_and_replace_generics(&mut tipo, mono_types);
tipo tipo
} }
TypeVar::Generic { .. } => unreachable!(), TypeVar::Generic { .. } => unreachable!(),
@ -1364,7 +1365,7 @@ pub fn wrap_validator_args(term: Term<Name>, arguments: &[TypedArg]) -> Term<Nam
pub fn monomorphize( pub fn monomorphize(
ir: Vec<Air>, ir: Vec<Air>,
generic_types: IndexMap<u64, Arc<Type>>, mono_types: IndexMap<u64, Arc<Type>>,
full_type: &Arc<Type>, full_type: &Arc<Type>,
) -> (String, Vec<Air>) { ) -> (String, Vec<Air>) {
let mut new_air = ir.clone(); let mut new_air = ir.clone();
@ -1381,7 +1382,7 @@ pub fn monomorphize(
if constructor.tipo.is_generic() { if constructor.tipo.is_generic() {
let mut tipo = constructor.tipo.clone(); let mut tipo = constructor.tipo.clone();
find_generics_to_replace(&mut tipo, &generic_types); find_and_replace_generics(&mut tipo, &mono_types);
let mut variant = String::new(); let mut variant = String::new();
@ -1415,7 +1416,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::List { new_air[index] = Air::List {
scope, scope,
@ -1435,7 +1436,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::ListAccessor { new_air[index] = Air::ListAccessor {
scope, scope,
@ -1455,7 +1456,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::ListExpose { new_air[index] = Air::ListExpose {
scope, scope,
@ -1474,7 +1475,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::BinOp { new_air[index] = Air::BinOp {
scope, scope,
@ -1493,7 +1494,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::Builtin { new_air[index] = Air::Builtin {
scope, scope,
@ -1507,7 +1508,7 @@ pub fn monomorphize(
Air::UnWrapData { scope, tipo } => { Air::UnWrapData { scope, tipo } => {
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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::UnWrapData { scope, tipo }; new_air[index] = Air::UnWrapData { scope, tipo };
needs_variant = true; needs_variant = true;
@ -1516,7 +1517,7 @@ pub fn monomorphize(
Air::WrapData { scope, tipo } => { Air::WrapData { scope, tipo } => {
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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::WrapData { scope, tipo }; new_air[index] = Air::WrapData { scope, tipo };
needs_variant = true; needs_variant = true;
@ -1529,7 +1530,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::When { new_air[index] = Air::When {
scope, scope,
@ -1547,7 +1548,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::Clause { new_air[index] = Air::Clause {
scope, scope,
@ -1567,7 +1568,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::ListClause { new_air[index] = Air::ListClause {
scope, scope,
@ -1590,7 +1591,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::TupleClause { new_air[index] = Air::TupleClause {
scope, scope,
@ -1611,7 +1612,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::ClauseGuard { new_air[index] = Air::ClauseGuard {
scope, scope,
@ -1630,7 +1631,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::ListClauseGuard { new_air[index] = Air::ListClauseGuard {
scope, scope,
@ -1645,7 +1646,7 @@ pub fn monomorphize(
Air::Tuple { scope, tipo, count } => { Air::Tuple { scope, tipo, count } => {
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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::Tuple { scope, tipo, count }; new_air[index] = Air::Tuple { scope, tipo, count };
needs_variant = true; needs_variant = true;
@ -1658,7 +1659,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::TupleIndex { new_air[index] = Air::TupleIndex {
scope, scope,
@ -1671,7 +1672,7 @@ pub fn monomorphize(
Air::ErrorTerm { scope, tipo } => { Air::ErrorTerm { scope, tipo } => {
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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::ErrorTerm { scope, tipo }; new_air[index] = Air::ErrorTerm { scope, tipo };
needs_variant = true; needs_variant = true;
@ -1680,7 +1681,7 @@ pub fn monomorphize(
Air::Trace { scope, tipo } => { Air::Trace { scope, tipo } => {
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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::Trace { scope, tipo }; new_air[index] = Air::Trace { scope, tipo };
needs_variant = true; needs_variant = true;
@ -1694,7 +1695,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::Record { new_air[index] = Air::Record {
scope, scope,
@ -1712,7 +1713,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::RecordAccess { new_air[index] = Air::RecordAccess {
scope, scope,
@ -1731,7 +1732,7 @@ pub fn monomorphize(
for (ind, name, tipo) in indices { for (ind, name, tipo) in indices {
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_and_replace_generics(&mut tipo, &mono_types);
needs_variant = true; needs_variant = true;
new_indices.push((ind, name, tipo)); new_indices.push((ind, name, tipo));
} else { } else {
@ -1755,7 +1756,7 @@ pub fn monomorphize(
for (ind, tipo) in indices { for (ind, tipo) in indices {
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_and_replace_generics(&mut tipo, &mono_types);
needs_variant = true; needs_variant = true;
new_indices.push((ind, tipo)); new_indices.push((ind, tipo));
} else { } else {
@ -1763,7 +1764,7 @@ pub fn monomorphize(
} }
} }
if tipo.is_generic() { if tipo.is_generic() {
find_generics_to_replace(&mut tipo, &generic_types); find_and_replace_generics(&mut tipo, &mono_types);
} }
new_air[index] = Air::RecordUpdate { new_air[index] = Air::RecordUpdate {
scope, scope,
@ -1780,7 +1781,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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::TupleAccessor { new_air[index] = Air::TupleAccessor {
scope, scope,
@ -1795,7 +1796,7 @@ pub fn monomorphize(
Air::Call { scope, count, tipo } => { Air::Call { scope, count, tipo } => {
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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::Call { scope, count, tipo }; new_air[index] = Air::Call { scope, count, tipo };
needs_variant = true; needs_variant = true;
@ -1804,7 +1805,7 @@ pub fn monomorphize(
Air::If { scope, tipo } => { Air::If { scope, tipo } => {
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_and_replace_generics(&mut tipo, &mono_types);
new_air[index] = Air::If { scope, tipo }; new_air[index] = Air::If { scope, tipo };
needs_variant = true; needs_variant = true;
@ -1984,17 +1985,17 @@ pub fn replace_opaque_type(t: &mut Arc<Type>, data_types: IndexMap<DataTypeKey,
let data_type = lookup_data_type_by_tipo(data_types.clone(), t).unwrap(); let data_type = lookup_data_type_by_tipo(data_types.clone(), t).unwrap();
let new_type_fields = data_type.typed_parameters.clone(); let new_type_fields = data_type.typed_parameters.clone();
let mut generics_type_map: IndexMap<u64, Arc<Type>> = IndexMap::new(); let mut mono_types: IndexMap<u64, Arc<Type>> = IndexMap::new();
for (tipo, param) in new_type_fields.iter().zip(t.arg_types().unwrap()) { for (tipo, param) in new_type_fields.iter().zip(t.arg_types().unwrap()) {
let mut map = generics_type_map.into_iter().collect_vec(); let mut map = mono_types.into_iter().collect_vec();
map.append(&mut get_generics_and_type(tipo, &param)); map.append(&mut get_generics_and_type(tipo, &param));
generics_type_map = map.into_iter().collect(); mono_types = map.into_iter().collect();
} }
let mut generic_type = data_type.constructors[0].arguments[0].tipo.clone(); let mut generic_type = data_type.constructors[0].arguments[0].tipo.clone();
find_generics_to_replace(&mut generic_type, &generics_type_map); find_and_replace_generics(&mut generic_type, &mono_types);
replace_opaque_type(&mut generic_type, data_types.clone()); replace_opaque_type(&mut generic_type, data_types.clone());
*t = generic_type; *t = generic_type;

View File

@ -89,10 +89,16 @@ impl<'a> CodeGenerator<'a> {
self.build_ir(body, &mut ir_stack, scope); self.build_ir(body, &mut ir_stack, scope);
println!("{:#?}", ir_stack);
self.define_ir(&mut ir_stack); self.define_ir(&mut ir_stack);
println!("{:#?}", ir_stack);
self.convert_opaque_type_to_inner_ir(&mut ir_stack); self.convert_opaque_type_to_inner_ir(&mut ir_stack);
println!("{:#?}", ir_stack);
let mut term = self.uplc_code_gen(&mut ir_stack); let mut term = self.uplc_code_gen(&mut ir_stack);
if self.needs_field_access { if self.needs_field_access {
@ -3144,6 +3150,8 @@ impl<'a> CodeGenerator<'a> {
let mut recursion_func_map_to_add = recursion_func_map.clone(); let mut recursion_func_map_to_add = recursion_func_map.clone();
println!("ARE WE GETTING HERE");
for func_index in func_index_map.clone().iter() { for func_index in func_index_map.clone().iter() {
let func = func_index.0; let func = func_index.0;
@ -3252,11 +3260,10 @@ impl<'a> CodeGenerator<'a> {
if let ValueConstructorVariant::ModuleFn { if let ValueConstructorVariant::ModuleFn {
name, name,
module, module,
builtin, builtin: None,
.. ..
} = &constructor.variant } = &constructor.variant
{ {
if builtin.is_none() {
let non_variant_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(),
@ -3269,24 +3276,33 @@ impl<'a> CodeGenerator<'a> {
self.build_ir(&function.body, &mut func_ir, scope.to_vec()); self.build_ir(&function.body, &mut func_ir, scope.to_vec());
println!(
"OUR FUNC IR FOR {:#?} IS: {:#?}",
non_variant_function_key, func_ir
);
let param_types = constructor.tipo.arg_types().unwrap(); let param_types = constructor.tipo.arg_types().unwrap();
let mut generics_type_map: IndexMap<u64, Arc<Type>> = IndexMap::new(); let mut mono_types: IndexMap<u64, Arc<Type>> = IndexMap::new();
for (index, arg) in function.arguments.iter().enumerate() { for (index, arg) in function.arguments.iter().enumerate() {
if arg.tipo.is_generic() { if arg.tipo.is_generic() {
let mut map = generics_type_map.into_iter().collect_vec(); let mut map = mono_types.into_iter().collect_vec();
map.append(&mut get_generics_and_type( map.append(&mut get_generics_and_type(
&arg.tipo, &arg.tipo,
&param_types[index], &param_types[index],
)); ));
generics_type_map = map.into_iter().collect(); mono_types = map.into_iter().collect();
} }
} }
println!("DID WE GET HERE");
let (variant_name, func_ir) = let (variant_name, func_ir) =
monomorphize(func_ir, generics_type_map, &constructor.tipo); monomorphize(func_ir, mono_types, &constructor.tipo);
println!("AND HERE?");
let function_key = FunctionAccessKey { let function_key = FunctionAccessKey {
module_name: module.clone(), module_name: module.clone(),
@ -3345,36 +3361,29 @@ impl<'a> CodeGenerator<'a> {
} else if let (Some(function), Type::Fn { .. }) = } else if let (Some(function), Type::Fn { .. }) =
(function, &*tipo) (function, &*tipo)
{ {
let mut generics_type_map: IndexMap<u64, Arc<Type>> = let mut mono_types: IndexMap<u64, Arc<Type>> =
IndexMap::new(); IndexMap::new();
let param_types = tipo.arg_types().unwrap(); let param_types = tipo.arg_types().unwrap();
for (index, arg) in for (index, arg) in function.arguments.iter().enumerate() {
function.arguments.iter().enumerate()
{
if arg.tipo.is_generic() { if arg.tipo.is_generic() {
let mut map = let mut map = mono_types.into_iter().collect_vec();
generics_type_map.into_iter().collect_vec();
map.append(&mut get_generics_and_type( map.append(&mut get_generics_and_type(
&arg.tipo, &arg.tipo,
&param_types[index], &param_types[index],
)); ));
generics_type_map = map.into_iter().collect(); mono_types = map.into_iter().collect();
} }
} }
let mut func_ir = vec![]; let mut func_ir = vec![];
self.build_ir( self.build_ir(&function.body, &mut func_ir, scope.to_vec());
&function.body,
&mut func_ir,
scope.to_vec(),
);
let (variant_name, _) = let (variant_name, _) =
monomorphize(func_ir, generics_type_map, &tipo); monomorphize(func_ir, mono_types, &tipo);
func_calls.insert( func_calls.insert(
FunctionAccessKey { FunctionAccessKey {
@ -3403,6 +3412,8 @@ impl<'a> CodeGenerator<'a> {
} }
} }
println!("FUNC CALLS IS {:#?}", func_calls);
let recursive = if func_calls.get(&function_key).is_some() { let recursive = if func_calls.get(&function_key).is_some() {
func_calls.remove(&function_key); func_calls.remove(&function_key);
true true
@ -3421,12 +3432,31 @@ impl<'a> CodeGenerator<'a> {
}, },
); );
} }
} else {
println!("ARE WE GETTING HERE323323");
for func in to_be_defined_map.clone().iter() {
if get_common_ancestor(scope, func.1) == scope.to_vec() {
if let Some(index_scope) = func_index_map.get(func.0) {
if get_common_ancestor(index_scope, func.1) == scope.to_vec() {
func_index_map.insert(func.0.clone(), scope.clone());
to_be_defined_map.shift_remove(func.0);
} else {
to_be_defined_map.insert(
func.0.clone(),
get_common_ancestor(index_scope, func.1),
);
}
} else {
func_index_map.insert(func.0.clone(), scope.clone());
to_be_defined_map.shift_remove(func.0);
}
}
} }
} }
} }
a => { a => {
let scope = a.scope(); let scope = a.scope();
println!("ARE WE GETTING HERE2222");
for func in to_be_defined_map.clone().iter() { for func in to_be_defined_map.clone().iter() {
if get_common_ancestor(&scope, func.1) == scope.to_vec() { if get_common_ancestor(&scope, func.1) == scope.to_vec() {
if let Some(index_scope) = func_index_map.get(func.0) { if let Some(index_scope) = func_index_map.get(func.0) {
@ -3449,6 +3479,8 @@ impl<'a> CodeGenerator<'a> {
} }
} }
println!("REACHED HERE");
// Still to be defined // Still to be defined
for func in to_be_defined_map.clone().iter() { for func in to_be_defined_map.clone().iter() {
let index_scope = func_index_map.get(func.0).unwrap(); let index_scope = func_index_map.get(func.0).unwrap();