-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathvisitors.rs
171 lines (150 loc) · 5.27 KB
/
visitors.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! Visitor helpers
use crate::{BoundVar, ControlFlow, DebruijnIndex, Interner, Visit, Visitor};
/// Visitor extensions.
pub trait VisitExt<I: Interner>: Visit<I> {
/// Check whether there are free (non-bound) variables.
fn has_free_vars(&self, interner: &I) -> bool {
self.visit_with(
&mut FindFreeVarsVisitor { interner },
DebruijnIndex::INNERMOST,
)
.is_break()
}
/// Validates binders.
fn validate_binders(&self, interner: &I) -> bool {
self.visit_with(
&mut binders_check::BindersValidator {
interner,
stack: Vec::new(),
},
DebruijnIndex::INNERMOST,
)
.is_continue()
}
}
impl<T, I: Interner> VisitExt<I> for T where T: Visit<I> {}
struct FindFreeVarsVisitor<'i, I: Interner> {
interner: &'i I,
}
impl<'i, I: Interner> Visitor<'i, I> for FindFreeVarsVisitor<'i, I> {
type BreakTy = ();
fn as_dyn(&mut self) -> &mut dyn Visitor<'i, I, BreakTy = Self::BreakTy> {
self
}
fn interner(&self) -> &'i I {
self.interner
}
fn visit_free_var(
&mut self,
_bound_var: BoundVar,
_outer_binder: DebruijnIndex,
) -> ControlFlow<()> {
ControlFlow::BREAK
}
}
mod binders_check {
use crate::{
interner::Interner,
visit::{ControlFlow, SuperVisit, Visitor},
Const, DebruijnIndex, Lifetime, Ty, VariableKind,
};
pub struct BindersValidator<'i, I: Interner> {
pub(crate) interner: &'i I,
pub(crate) stack: Vec<Vec<VariableKind<I>>>,
}
impl<'i, I: Interner> Visitor<'i, I> for BindersValidator<'i, I> {
type BreakTy = ();
fn as_dyn(&mut self) -> &mut dyn Visitor<'i, I, BreakTy = Self::BreakTy> {
self
}
fn interner(&self) -> &'i I {
self.interner
}
fn visit_ty(
&mut self,
ty: &Ty<I>,
outer_binder: DebruijnIndex,
) -> ControlFlow<Self::BreakTy> {
if let Some(bv) = ty.bound_var(self.interner) {
assert_eq!(self.stack.len(), outer_binder.depth() as usize);
if bv.debruijn < outer_binder {
let kinds = &self.stack[self.stack.len() - 1 - bv.debruijn.depth() as usize];
match kinds.get(bv.index) {
Some(VariableKind::Ty(_)) => {}
_ => {
return ControlFlow::BREAK;
}
}
} else {
return ControlFlow::BREAK;
}
}
ty.super_visit_with(self.as_dyn(), outer_binder)
}
fn visit_const(
&mut self,
constant: &Const<I>,
outer_binder: DebruijnIndex,
) -> ControlFlow<Self::BreakTy> {
if let Some(bv) = constant.bound_var(self.interner) {
assert_eq!(self.stack.len(), outer_binder.depth() as usize);
if bv.debruijn < outer_binder {
let kinds = &self.stack[self.stack.len() - 1 - bv.debruijn.depth() as usize];
match kinds.get(bv.index) {
Some(VariableKind::Const(_ty)) => {
// FIXME: validate that type can match?
}
_ => {
return ControlFlow::BREAK;
}
}
} else {
return ControlFlow::BREAK;
}
}
constant.super_visit_with(self.as_dyn(), outer_binder)
}
fn visit_lifetime(
&mut self,
lifetime: &Lifetime<I>,
outer_binder: DebruijnIndex,
) -> ControlFlow<Self::BreakTy> {
if let Some(bv) = lifetime.bound_var(self.interner) {
assert_eq!(self.stack.len(), outer_binder.depth() as usize);
if bv.debruijn < outer_binder {
let kinds = &self.stack[self.stack.len() - 1 - bv.debruijn.depth() as usize];
match kinds.get(bv.index) {
Some(VariableKind::Lifetime) => {}
_ => {
return ControlFlow::BREAK;
}
}
} else {
return ControlFlow::BREAK;
}
}
lifetime.super_visit_with(self.as_dyn(), outer_binder)
}
fn before_binders(&mut self, kinds: &crate::VariableKinds<I>) {
self.stack.push(kinds.as_slice(self.interner).to_vec());
}
fn before_canonical(&mut self, kinds: &crate::CanonicalVarKinds<I>) {
self.stack.push(
kinds
.iter(self.interner)
.map(|wk| wk.kind.clone())
.collect(),
);
}
fn before_fn_pointer_substs(&mut self, number: usize) {
self.stack.push(
std::iter::repeat_with(|| VariableKind::Lifetime)
.take(number)
.collect(),
)
}
fn after_any_binders(&mut self) {
self.stack.pop().unwrap();
}
}
}