From 87b71a2db3c046cc03288854a6db9dd0940b40d7 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Wed, 1 Dec 2021 01:39:41 +0800 Subject: [PATCH] nac3core: maintain order of unifying dummy types in top level --- nac3core/src/toplevel/composer.rs | 35 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index a059362a..72920453 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1,4 +1,4 @@ -use std::cell::RefCell; +use std::{cell::RefCell, collections::LinkedList}; use nac3parser::ast::fold::Fold; use inkwell::{FloatPredicate, IntPredicate}; @@ -946,6 +946,7 @@ impl TopLevelComposer { let unifier = self.unifier.borrow_mut(); let mut type_var_to_concrete_def: HashMap = HashMap::new(); + let mut ordered_type_vars: LinkedList = LinkedList::new(); // skip 5 to skip analyzing the primitives for (class_def, class_ast) in def_ast_list.iter().skip(self.built_in_num) { @@ -956,14 +957,12 @@ impl TopLevelComposer { &temp_def_list, unifier, primitives, - &mut type_var_to_concrete_def, + (&mut type_var_to_concrete_def, &mut ordered_type_vars), &self.keyword_list, )? } } - // println!("type_var_to_concrete_def1: {:?}", type_var_to_concrete_def); - // handle the inheritanced methods and fields let mut current_ancestor_depth: usize = 2; loop { @@ -998,21 +997,15 @@ impl TopLevelComposer { } } - // println!("type_var_to_concrete_def3: {:?}\n", type_var_to_concrete_def); - // unification of previously assigned typevar - for (ty, def) in type_var_to_concrete_def { - // println!( - // "{:?}_{} -> {:?}\n", - // ty, - // unifier.stringify(ty, - // &mut |id| format!("class{}", id), - // &mut |id| format!("tvar{}", id) - // ), - // def - // ); + for ty in ordered_type_vars { let target_ty = - get_type_from_type_annotation_kinds(&temp_def_list, unifier, primitives, &def)?; + get_type_from_type_annotation_kinds( + &temp_def_list, + unifier, + primitives, + type_var_to_concrete_def.get(&ty).unwrap() + )?; unifier.unify(ty, target_ty)?; } @@ -1229,9 +1222,11 @@ impl TopLevelComposer { temp_def_list: &[Arc>], unifier: &mut Unifier, primitives: &PrimitiveStore, - type_var_to_concrete_def: &mut HashMap, + type_var_to_concrete_def: (&mut HashMap, &mut LinkedList), + // ordered_type_vars: &mut LinkedList, keyword_list: &HashSet, ) -> Result<(), String> { + let (type_var_to_concrete_def, ordered_type_vars) = type_var_to_concrete_def; let mut class_def = class_def.write(); let ( class_id, @@ -1381,6 +1376,7 @@ impl TopLevelComposer { // into the list for later unification type_var_to_concrete_def .insert(dummy_func_arg.ty, type_ann.clone()); + ordered_type_vars.push_back(dummy_func_arg.ty); result.push(dummy_func_arg) } } @@ -1415,6 +1411,7 @@ impl TopLevelComposer { } let dummy_return_type = unifier.get_fresh_var().0; type_var_to_concrete_def.insert(dummy_return_type, annotation.clone()); + ordered_type_vars.push_back(dummy_return_type); dummy_return_type } else { // if do not have return annotation, return none @@ -1424,6 +1421,7 @@ impl TopLevelComposer { dummy_return_type, TypeAnnotation::Primitive(primitives.none), ); + ordered_type_vars.push_back(dummy_return_type); dummy_return_type } }; @@ -1502,6 +1500,7 @@ impl TopLevelComposer { } } type_var_to_concrete_def.insert(dummy_field_type, annotation); + ordered_type_vars.push_back(dummy_field_type); } else { return Err("same class fields defined twice".into()); }