Have load use env.vars_to_types

This commit is contained in:
Richard Feldman 2022-05-28 12:56:25 -04:00
parent ce0422186d
commit 09fbfe621c
No known key found for this signature in database
GPG Key ID: 7E4127D1E4241798
2 changed files with 45 additions and 44 deletions

View File

@ -25,15 +25,14 @@ pub struct Env<'a> {
}
impl<'a> Env<'a> {
pub fn vars_to_types<I, V>(&mut self, variables: I) -> Types
pub fn vars_to_types<I>(&mut self, variables: I) -> Types
where
I: IntoIterator<Item = V>,
V: AsRef<Variable>,
I: IntoIterator<Item = Variable>,
{
let mut types = Types::default();
for var in variables {
self.add_type(*var.as_ref(), &mut types);
self.add_type(var, &mut types);
}
self.resolve_pending_recursive_types(&mut types);
@ -50,7 +49,7 @@ impl<'a> Env<'a> {
add_type_help(self, layout, var, None, types, None)
}
fn add_pending_recursive_type(&self, type_id: TypeId, var: Variable) {
fn add_pending_recursive_type(&mut self, type_id: TypeId, var: Variable) {
self.pending_recursive_types.insert(type_id, var);
}

View File

@ -59,53 +59,55 @@ pub fn load_types(
let mut answer = Vec::with_capacity(Architecture::iter().size_hint().0);
for architecture in Architecture::iter() {
let mut layout_cache = LayoutCache::new(architecture.into());
let mut env = Env {
arena,
layout_cache: &mut layout_cache,
interns: &interns,
struct_names: Default::default(),
enum_names: Default::default(),
subs,
};
let mut types = Types::default();
let defs_iter = decls.iter().flat_map(|decl| match decl {
Declaration::Declare(def) => {
vec![def.clone()]
}
Declaration::DeclareRec(defs, cycle_mark) => {
if cycle_mark.is_illegal(subs) {
Vec::new()
} else {
defs.clone()
}
}
Declaration::Builtin(..) => {
unreachable!("Builtin decl in userspace module?")
}
Declaration::InvalidCycle(..) => Vec::new(),
});
for decl in decls.iter() {
let defs = match decl {
Declaration::Declare(def) => {
vec![def.clone()]
}
Declaration::DeclareRec(defs, cycle_mark) => {
if cycle_mark.is_illegal(subs) {
Vec::new()
} else {
defs.clone()
}
}
Declaration::Builtin(..) => {
unreachable!("Builtin decl in userspace module?")
}
Declaration::InvalidCycle(..) => Vec::new(),
};
for Def {
loc_pattern,
pattern_vars,
..
} in defs.into_iter()
{
let vars_iter = defs_iter.filter_map(
|Def {
loc_pattern,
pattern_vars,
..
}| {
if let Pattern::Identifier(sym) = loc_pattern.value {
let var = pattern_vars
.get(&sym)
.expect("Indetifier known but it has no var?");
env.add_type(*var, &mut types);
Some(*var)
} else {
// figure out if we need to export non-identifier defs - when would that
// happen?
// figure out if we need to export non-identifier defs - when
// would that happen?
None
}
}
}
},
);
let mut layout_cache = LayoutCache::new(architecture.into());
let mut env = Env {
arena,
layout_cache: &mut layout_cache,
interns: &interns,
subs,
struct_names: Default::default(),
enum_names: Default::default(),
pending_recursive_types: Default::default(),
known_recursive_types: Default::default(),
};
let types = env.vars_to_types(vars_iter);
answer.push((architecture, types));
}