Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed Jan 13, 2025
1 parent cf4618d commit d0fbc32
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
10 changes: 10 additions & 0 deletions tests/programs/specializations.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

use std::tuple::Pair;

pub fn main(&io: &IO) {
io.println([1, 2, 3, 4].to_string[; N32::to_string]());
io.println(["abc", "def", "ghi"].to_string[; String::to_string]());
io.println(['x', 'y', 'z'].to_string[; Char::to_string]());
io.println([true, false].to_string[; Bool::to_string]());
io.println([(1, 'a'), (2, 'b')].to_string[; Pair::to_string[; N32::to_string, Char::to_string]]());
}
2 changes: 1 addition & 1 deletion vine/examples/sub_min.vi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub fn main(&io: &IO) {
let list = [4, 3, 7, 9];
sub_min(&list);
io.println(list.to_string[;N32::to_string]());
io.println(list.to_string[; N32::to_string]());
}

pub fn sub_min(&list: &List[N32]) {
Expand Down
1 change: 0 additions & 1 deletion vine/src/checker/typeof_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ impl<'core> Checker<'core, '_> {
if path.generics.as_ref().is_some_and(|g| !g.impls.is_empty()) {
self.core.report(Diag::UnexpectedImplArgs { span });
}
// dbg!(&path);
let def_id = path.path.resolved.unwrap();
let def = &self.resolver.defs[def_id];
let Some(type_def) = &def.type_def else {
Expand Down
5 changes: 3 additions & 2 deletions vine/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ impl<'core, 'a> VirEmitter<'core, 'a> {
Port::Wire(w) => Tree::Var(format!("w{}", wire_offset + w.0)),
Port::Const(def) => Tree::Global(resolver.defs[*def].canonical.to_string()),
Port::Rel(rel) => {
let (def, spec) = spec.rels[*rel];
Tree::Global(format!("{}::{}", resolver.defs[def].canonical, spec.0))
let (def, spec, singular) = spec.rels[*rel];
let path = &resolver.defs[def].canonical;
Tree::Global(if singular { path.to_string() } else { format!("{}::{}", path, spec.0) })
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion vine/src/resolver/resolve_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl<'core> VisitMut<'core, '_> for ResolveVisitor<'core, '_> {
self._visit_type(ty);
}

fn _visit_impl(&mut self, impl_: &mut Impl<'core>) {
fn visit_impl(&mut self, impl_: &mut Impl<'core>) {
if let ImplKind::Path(path) = &mut impl_.kind {
if path.generics.is_none() {
if let Some(ident) = path.path.as_ident() {
Expand All @@ -383,6 +383,7 @@ impl<'core> VisitMut<'core, '_> for ResolveVisitor<'core, '_> {
impl_.kind = ImplKind::Error(self.resolver.core.report(diag));
}
}
self._visit_impl(impl_);
}
}

Expand Down
34 changes: 17 additions & 17 deletions vine/src/specializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@ impl<'core, 'a> Specializer<'core, 'a> {
fn initialize(&mut self) {
for def_id in self.resolver.defs.keys() {
let def = &mut self.resolver.defs[def_id];
let def_info = if let Some(mut value) = def.value_def.take() {
let impl_params = value.impl_params.len();
let def_info = if let Some(mut value_def) = def.value_def.take() {
let impl_params = value_def.impl_params.len();
let mut extractor = RelExtractor { rels: IdxVec::new(), resolver: self.resolver };
if let ValueDefKind::Expr(expr) = &mut value.kind {
if let ValueDefKind::Expr(expr) = &mut value_def.kind {
extractor.visit_expr(expr);
}
DefInfo {
impl_params,
rels: extractor.rels,
specs_lookup: HashMap::new(),
specs: IdxVec::new(),
}
let rels = extractor.rels;
self.resolver.defs[def_id].value_def = Some(value_def);
DefInfo { impl_params, rels, specs_lookup: HashMap::new(), specs: IdxVec::new() }
} else {
DefInfo::default()
};
Expand Down Expand Up @@ -68,11 +65,10 @@ impl<'core, 'a> Specializer<'core, 'a> {
let mut spec = Spec::default();
for rel_id in def.rels.keys() {
let rel = &self.defs[def_id].rels[rel_id];
spec.rels.push(match rel {
Rel::Def(rel_def_id, impls) => (
*rel_def_id,
self.specialize(*rel_def_id, impls.iter().map(|x| instantiate(x, &impl_args)).collect()),
),
let (rel_def_id, rel_impl_args) = match rel {
Rel::Def(rel_def_id, impls) => {
(*rel_def_id, impls.iter().map(|x| instantiate(x, &impl_args)).collect())
}
Rel::Subitem(impl_, name) => {
let impl_ = instantiate(impl_, &impl_args);
let subitem_id = self.resolver.defs[impl_.0]
Expand All @@ -84,9 +80,12 @@ impl<'core, 'a> Specializer<'core, 'a> {
.find(|(_, n, _)| n == name)
.unwrap()
.2;
(subitem_id, self.specialize(subitem_id, impl_.1))
(subitem_id, impl_.1)
}
});
};
let singular = rel_impl_args.is_empty();
let rel_spec_id = self.specialize(rel_def_id, rel_impl_args);
spec.rels.push((rel_def_id, rel_spec_id, singular));
}
self.defs[def_id].specs[spec_id] = Some(spec);
spec_id
Expand Down Expand Up @@ -117,6 +116,7 @@ impl<'core> VisitMut<'core, '_> for RelExtractor<'core, '_> {
}
}
}
self._visit_expr(expr);
}
}

Expand All @@ -135,7 +135,7 @@ new_idx!(pub ImplId);
#[derive(Debug, Default)]
pub struct Spec {
pub singular: bool,
pub rels: IdxVec<RelId, (DefId, SpecId)>,
pub rels: IdxVec<RelId, (DefId, SpecId, bool)>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down

0 comments on commit d0fbc32

Please sign in to comment.