forked from M-Labs/nac3
1
0
Fork 0

core: Add PrimitiveStore into Unifier

This will be used during unification between a const generic variable
and a `Literal`.
This commit is contained in:
David Mak 2023-12-13 18:23:32 +08:00 committed by sb10q
parent c7735d935b
commit 5f692debd8
4 changed files with 19 additions and 1 deletions

View File

@ -580,6 +580,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
let (unifier, primitives) = &top_level_ctx.unifiers.read()[task.unifier_index]; let (unifier, primitives) = &top_level_ctx.unifiers.read()[task.unifier_index];
(Unifier::from_shared_unifier(unifier), *primitives) (Unifier::from_shared_unifier(unifier), *primitives)
}; };
unifier.put_primitive_store(&primitives);
unifier.top_level = Some(top_level_ctx.clone()); unifier.top_level = Some(top_level_ctx.clone());
let mut cache = HashMap::new(); let mut cache = HashMap::new();

View File

@ -145,6 +145,7 @@ impl TopLevelComposer {
exception, exception,
option, option,
}; };
unifier.put_primitive_store(&primitives);
crate::typecheck::magic_methods::set_primitives_magic_methods(&primitives, &mut unifier); crate::typecheck::magic_methods::set_primitives_magic_methods(&primitives, &mut unifier);
(primitives, unifier) (primitives, unifier)
} }

View File

@ -148,6 +148,7 @@ impl TestEnvironment {
uint64, uint64,
option, option,
}; };
unifier.put_primitive_store(&primitives);
set_primitives_magic_methods(&primitives, &mut unifier); set_primitives_magic_methods(&primitives, &mut unifier);
let id_to_name = [ let id_to_name = [
@ -296,6 +297,8 @@ impl TestEnvironment {
option, option,
}; };
unifier.put_primitive_store(&primitives);
let (v0, id) = unifier.get_dummy_var(); let (v0, id) = unifier.get_dummy_var();
let foo_ty = unifier.add_ty(TypeEnum::TObj { let foo_ty = unifier.add_ty(TypeEnum::TObj {

View File

@ -13,6 +13,7 @@ use super::type_error::{TypeError, TypeErrorKind};
use super::unification_table::{UnificationKey, UnificationTable}; use super::unification_table::{UnificationKey, UnificationTable};
use crate::symbol_resolver::SymbolValue; use crate::symbol_resolver::SymbolValue;
use crate::toplevel::{DefinitionId, TopLevelContext, TopLevelDef}; use crate::toplevel::{DefinitionId, TopLevelContext, TopLevelDef};
use crate::typecheck::type_inferencer::PrimitiveStore;
#[cfg(test)] #[cfg(test)]
mod test; mod test;
@ -211,7 +212,8 @@ pub struct Unifier {
pub(crate) calls: Vec<Rc<Call>>, pub(crate) calls: Vec<Rc<Call>>,
var_id: u32, var_id: u32,
unify_cache: HashSet<(Type, Type)>, unify_cache: HashSet<(Type, Type)>,
snapshot: Option<(usize, u32)> snapshot: Option<(usize, u32)>,
primitive_store: Option<PrimitiveStore>,
} }
impl Default for Unifier { impl Default for Unifier {
@ -231,9 +233,19 @@ impl Unifier {
unify_cache: HashSet::new(), unify_cache: HashSet::new(),
top_level: None, top_level: None,
snapshot: None, snapshot: None,
primitive_store: None,
} }
} }
/// Sets the [PrimitiveStore] instance within this `Unifier`.
///
/// This function can only be invoked once. Any subsequent invocations will result in an
/// assertion error..
pub fn put_primitive_store(&mut self, primitives: &PrimitiveStore) {
assert!(self.primitive_store.is_none());
self.primitive_store.replace(primitives.clone());
}
pub unsafe fn get_unification_table(&mut self) -> &mut UnificationTable<Rc<TypeEnum>> { pub unsafe fn get_unification_table(&mut self) -> &mut UnificationTable<Rc<TypeEnum>> {
&mut self.unification_table &mut self.unification_table
} }
@ -252,6 +264,7 @@ impl Unifier {
top_level: None, top_level: None,
unify_cache: HashSet::new(), unify_cache: HashSet::new(),
snapshot: None, snapshot: None,
primitive_store: None,
} }
} }