charon_lib/transform/
unbind_item_vars.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Replace variables bound at the top-level with `Free` vars. This is for convenience for
//! consumers of the charon ast.
use derive_generic_visitor::*;
use index_vec::Idx;

use crate::ast::*;

use super::{ctx::TransformPass, TransformCtx};

/// Replace variables bound at the top-level with `Free` vars.
#[derive(Default, Visitor)]
pub(crate) struct UnbindVarVisitor {
    // Tracks the depth of binders we're inside of.
    binder_depth: DeBruijnId,
}

impl VisitAstMut for UnbindVarVisitor {
    fn enter_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
        self.binder_depth = self.binder_depth.incr()
    }
    fn exit_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
        self.binder_depth = self.binder_depth.decr()
    }
    fn enter_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
        self.binder_depth = self.binder_depth.incr()
    }
    fn exit_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
        self.binder_depth = self.binder_depth.decr()
    }

    fn exit_de_bruijn_var<T: AstVisitable + Idx>(&mut self, var: &mut DeBruijnVar<T>) {
        match var {
            DeBruijnVar::Bound(dbid, varid) if *dbid == self.binder_depth => {
                *var = DeBruijnVar::Free(*varid)
            }
            DeBruijnVar::Bound(..) => {}
            DeBruijnVar::Free(_) => unreachable!("Found unexpected free variable"),
        }
    }
}

pub struct Check;
impl TransformPass for Check {
    fn transform_ctx(&self, ctx: &mut TransformCtx) {
        let mut visitor = UnbindVarVisitor::default();
        for mut item in ctx.translated.all_items_mut() {
            item.drive_mut(&mut visitor);
        }
    }
}