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
172
173
174
175
176
177
178
use middle::ty;
use middle::ty_fold;
use std::cell::Cell;
use std::iter::repeat;
use syntax::codemap::Span;
pub trait RegionScope {
fn anon_regions(&self,
span: Span,
count: uint)
-> Result<Vec<ty::Region>, Option<Vec<(String, uint)>>>;
fn default_region_bound(&self, span: Span) -> Option<ty::Region>;
}
#[deriving(Copy)]
pub struct ExplicitRscope;
impl RegionScope for ExplicitRscope {
fn default_region_bound(&self, _span: Span) -> Option<ty::Region> {
None
}
fn anon_regions(&self,
_span: Span,
_count: uint)
-> Result<Vec<ty::Region>, Option<Vec<(String, uint)>>> {
Err(None)
}
}
pub struct UnelidableRscope(Vec<(String, uint)>);
impl UnelidableRscope {
pub fn new(v: Vec<(String, uint)>) -> UnelidableRscope {
UnelidableRscope(v)
}
}
impl RegionScope for UnelidableRscope {
fn default_region_bound(&self, _span: Span) -> Option<ty::Region> {
None
}
fn anon_regions(&self,
_span: Span,
_count: uint)
-> Result<Vec<ty::Region>, Option<Vec<(String, uint)>>> {
let UnelidableRscope(ref v) = *self;
Err(Some(v.clone()))
}
}
#[allow(missing_copy_implementations)]
pub struct SpecificRscope {
default: ty::Region
}
impl SpecificRscope {
pub fn new(r: ty::Region) -> SpecificRscope {
SpecificRscope { default: r }
}
}
impl RegionScope for SpecificRscope {
fn default_region_bound(&self, _span: Span) -> Option<ty::Region> {
Some(self.default)
}
fn anon_regions(&self,
_span: Span,
count: uint)
-> Result<Vec<ty::Region>, Option<Vec<(String, uint)>>>
{
Ok(repeat(self.default).take(count).collect())
}
}
pub struct BindingRscope {
anon_bindings: Cell<u32>,
}
impl BindingRscope {
pub fn new() -> BindingRscope {
BindingRscope {
anon_bindings: Cell::new(0),
}
}
fn next_region(&self) -> ty::Region {
let idx = self.anon_bindings.get();
self.anon_bindings.set(idx + 1);
ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(idx))
}
}
impl RegionScope for BindingRscope {
fn default_region_bound(&self, _span: Span) -> Option<ty::Region>
{
Some(self.next_region())
}
fn anon_regions(&self,
_: Span,
count: uint)
-> Result<Vec<ty::Region>, Option<Vec<(String, uint)>>>
{
Ok(range(0, count).map(|_| self.next_region()).collect())
}
}
pub struct ShiftedRscope<'r> {
base_scope: &'r (RegionScope+'r)
}
impl<'r> ShiftedRscope<'r> {
pub fn new(base_scope: &'r (RegionScope+'r)) -> ShiftedRscope<'r> {
ShiftedRscope { base_scope: base_scope }
}
}
impl<'r> RegionScope for ShiftedRscope<'r> {
fn default_region_bound(&self, span: Span) -> Option<ty::Region>
{
self.base_scope.default_region_bound(span)
.map(|r| ty_fold::shift_region(r, 1))
}
fn anon_regions(&self,
span: Span,
count: uint)
-> Result<Vec<ty::Region>, Option<Vec<(String, uint)>>>
{
match self.base_scope.anon_regions(span, count) {
Ok(mut v) => {
for r in v.iter_mut() {
*r = ty_fold::shift_region(*r, 1);
}
Ok(v)
}
Err(errs) => {
Err(errs)
}
}
}
}