Compare commits

...

1 Commits

Author SHA1 Message Date
ychenfo 87b71a2db3 nac3core: maintain order of unifying dummy types in top level 2021-12-01 01:55:38 +08:00
1 changed files with 17 additions and 18 deletions

View File

@ -1,4 +1,4 @@
use std::cell::RefCell; use std::{cell::RefCell, collections::LinkedList};
use nac3parser::ast::fold::Fold; use nac3parser::ast::fold::Fold;
use inkwell::{FloatPredicate, IntPredicate}; use inkwell::{FloatPredicate, IntPredicate};
@ -946,6 +946,7 @@ impl TopLevelComposer {
let unifier = self.unifier.borrow_mut(); let unifier = self.unifier.borrow_mut();
let mut type_var_to_concrete_def: HashMap<Type, TypeAnnotation> = HashMap::new(); let mut type_var_to_concrete_def: HashMap<Type, TypeAnnotation> = HashMap::new();
let mut ordered_type_vars: LinkedList<Type> = LinkedList::new();
// skip 5 to skip analyzing the primitives // skip 5 to skip analyzing the primitives
for (class_def, class_ast) in def_ast_list.iter().skip(self.built_in_num) { for (class_def, class_ast) in def_ast_list.iter().skip(self.built_in_num) {
@ -956,14 +957,12 @@ impl TopLevelComposer {
&temp_def_list, &temp_def_list,
unifier, unifier,
primitives, primitives,
&mut type_var_to_concrete_def, (&mut type_var_to_concrete_def, &mut ordered_type_vars),
&self.keyword_list, &self.keyword_list,
)? )?
} }
} }
// println!("type_var_to_concrete_def1: {:?}", type_var_to_concrete_def);
// handle the inheritanced methods and fields // handle the inheritanced methods and fields
let mut current_ancestor_depth: usize = 2; let mut current_ancestor_depth: usize = 2;
loop { loop {
@ -998,21 +997,15 @@ impl TopLevelComposer {
} }
} }
// println!("type_var_to_concrete_def3: {:?}\n", type_var_to_concrete_def);
// unification of previously assigned typevar // unification of previously assigned typevar
for (ty, def) in type_var_to_concrete_def { for ty in ordered_type_vars {
// println!(
// "{:?}_{} -> {:?}\n",
// ty,
// unifier.stringify(ty,
// &mut |id| format!("class{}", id),
// &mut |id| format!("tvar{}", id)
// ),
// def
// );
let target_ty = 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)?; unifier.unify(ty, target_ty)?;
} }
@ -1229,9 +1222,11 @@ impl TopLevelComposer {
temp_def_list: &[Arc<RwLock<TopLevelDef>>], temp_def_list: &[Arc<RwLock<TopLevelDef>>],
unifier: &mut Unifier, unifier: &mut Unifier,
primitives: &PrimitiveStore, primitives: &PrimitiveStore,
type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>, type_var_to_concrete_def: (&mut HashMap<Type, TypeAnnotation>, &mut LinkedList<Type>),
// ordered_type_vars: &mut LinkedList<Type>,
keyword_list: &HashSet<StrRef>, keyword_list: &HashSet<StrRef>,
) -> Result<(), String> { ) -> Result<(), String> {
let (type_var_to_concrete_def, ordered_type_vars) = type_var_to_concrete_def;
let mut class_def = class_def.write(); let mut class_def = class_def.write();
let ( let (
class_id, class_id,
@ -1381,6 +1376,7 @@ impl TopLevelComposer {
// into the list for later unification // into the list for later unification
type_var_to_concrete_def type_var_to_concrete_def
.insert(dummy_func_arg.ty, type_ann.clone()); .insert(dummy_func_arg.ty, type_ann.clone());
ordered_type_vars.push_back(dummy_func_arg.ty);
result.push(dummy_func_arg) result.push(dummy_func_arg)
} }
} }
@ -1415,6 +1411,7 @@ impl TopLevelComposer {
} }
let dummy_return_type = unifier.get_fresh_var().0; let dummy_return_type = unifier.get_fresh_var().0;
type_var_to_concrete_def.insert(dummy_return_type, annotation.clone()); type_var_to_concrete_def.insert(dummy_return_type, annotation.clone());
ordered_type_vars.push_back(dummy_return_type);
dummy_return_type dummy_return_type
} else { } else {
// if do not have return annotation, return none // if do not have return annotation, return none
@ -1424,6 +1421,7 @@ impl TopLevelComposer {
dummy_return_type, dummy_return_type,
TypeAnnotation::Primitive(primitives.none), TypeAnnotation::Primitive(primitives.none),
); );
ordered_type_vars.push_back(dummy_return_type);
dummy_return_type dummy_return_type
} }
}; };
@ -1502,6 +1500,7 @@ impl TopLevelComposer {
} }
} }
type_var_to_concrete_def.insert(dummy_field_type, annotation); type_var_to_concrete_def.insert(dummy_field_type, annotation);
ordered_type_vars.push_back(dummy_field_type);
} else { } else {
return Err("same class fields defined twice".into()); return Err("same class fields defined twice".into());
} }