/
env.rs
224 lines (203 loc) · 6.28 KB
/
env.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the "hack" directory of this source tree.
mod adata_state;
mod class_expr;
pub mod emitter; // emitter is public API for mutating state
mod iterator;
pub mod jump_targets;
mod label;
mod local;
use std::sync::Arc;
use ast_scope::Scope;
use ast_scope::ScopeItem;
use bitflags::bitflags;
pub use class_expr::ClassExpr;
use emitter::Emitter;
use hhbc::IterId;
use hhbc::Label;
use hhbc::Local;
pub use iterator::*;
pub use label::*;
pub use local::*;
use oxidized::ast;
use oxidized::namespace_env::Env as NamespaceEnv;
bitflags! {
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct Flags: u8 {
const NEEDS_LOCAL_THIS = 0b0000_0001;
const IN_TRY = 0b0000_0010;
const ALLOWS_ARRAY_APPEND = 0b0000_0100;
}
}
/// `'a` is an AST lifetime, `'arena` the lifetime of the `InstrSeq`
/// arena.
#[derive(Clone, Debug)]
pub struct Env<'a, 'arena> {
pub arena: &'arena bumpalo::Bump,
pub flags: Flags,
pub jump_targets_gen: jump_targets::Gen,
pub scope: Scope<'a, 'arena>,
pub namespace: Arc<NamespaceEnv>,
pub call_context: Option<String>,
pub pipe_var: Option<Local>,
}
impl<'a, 'arena> Env<'a, 'arena> {
pub fn default(arena: &'arena bumpalo::Bump, namespace: Arc<NamespaceEnv>) -> Self {
Env {
arena,
namespace,
flags: Flags::default(),
jump_targets_gen: jump_targets::Gen::default(),
scope: Scope::default(),
call_context: None,
pipe_var: None,
}
}
pub fn with_allows_array_append<F, R>(&mut self, alloc: &'arena bumpalo::Bump, f: F) -> R
where
F: FnOnce(&'arena bumpalo::Bump, &mut Self) -> R,
{
let old = self.flags.contains(Flags::ALLOWS_ARRAY_APPEND);
self.flags.set(Flags::ALLOWS_ARRAY_APPEND, true);
let r = f(alloc, self);
self.flags.set(Flags::ALLOWS_ARRAY_APPEND, old);
r
}
pub fn with_need_local_this(&mut self, need_local_this: bool) {
if need_local_this {
self.flags |= Flags::NEEDS_LOCAL_THIS;
}
}
pub fn with_pipe_var(&mut self, local: Local) {
self.pipe_var = Some(local);
}
pub fn with_scope(mut self, scope: Scope<'a, 'arena>) -> Env<'a, 'arena> {
self.scope = scope;
self
}
pub fn make_class_env(arena: &'arena bumpalo::Bump, class: &'a ast::Class_) -> Env<'a, 'arena> {
let scope = Scope::with_item(ScopeItem::Class(ast_scope::Class::new_ref(class)));
Env::default(arena, Arc::clone(&class.namespace)).with_scope(scope)
}
pub fn do_in_loop_body<'decl, R, F>(
&mut self,
e: &mut Emitter<'arena, 'decl>,
label_break: Label,
label_continue: Label,
iterator: Option<IterId>,
b: &[ast::Stmt],
f: F,
) -> R
where
F: FnOnce(&mut Self, &mut Emitter<'arena, 'decl>, &[ast::Stmt]) -> R,
{
self.jump_targets_gen
.with_loop(label_break, label_continue, iterator);
self.run_and_release_ids(e, |env, e| f(env, e, b))
}
pub fn do_in_switch_body<'decl, R, F>(
&mut self,
e: &mut Emitter<'arena, 'decl>,
end_label: Label,
cases: &[ast::Case],
dfl: &Option<ast::DefaultCase>,
f: F,
) -> R
where
F: FnOnce(
&mut Self,
&mut Emitter<'arena, 'decl>,
&[ast::Case],
&Option<ast::DefaultCase>,
) -> R,
{
self.jump_targets_gen.with_switch(end_label);
self.run_and_release_ids(e, |env, e| f(env, e, cases, dfl))
}
pub fn do_in_try_catch_body<'decl, R, F>(
&mut self,
e: &mut Emitter<'arena, 'decl>,
finally_label: Label,
try_block: &[ast::Stmt],
catch_block: &[ast::Catch],
f: F,
) -> R
where
F: FnOnce(&mut Self, &mut Emitter<'arena, 'decl>, &[ast::Stmt], &[ast::Catch]) -> R,
{
self.jump_targets_gen.with_try_catch(finally_label);
self.run_and_release_ids(e, |env, e| f(env, e, try_block, catch_block))
}
pub fn do_in_try_body<'decl, R, F>(
&mut self,
e: &mut Emitter<'arena, 'decl>,
finally_label: Label,
block: &[ast::Stmt],
f: F,
) -> R
where
F: FnOnce(&mut Self, &mut Emitter<'arena, 'decl>, &[ast::Stmt]) -> R,
{
self.jump_targets_gen.with_try(finally_label);
self.run_and_release_ids(e, |env, e| f(env, e, block))
}
pub fn do_in_finally_body<'decl, R, F>(
&mut self,
e: &mut Emitter<'arena, 'decl>,
block: &[ast::Stmt],
f: F,
) -> R
where
F: FnOnce(&mut Self, &mut Emitter<'arena, 'decl>, &[ast::Stmt]) -> R,
{
self.jump_targets_gen.with_finally();
self.run_and_release_ids(e, |env, e| f(env, e, block))
}
pub fn do_in_using_body<'decl, R, F>(
&mut self,
e: &mut Emitter<'arena, 'decl>,
finally_label: Label,
block: &[ast::Stmt],
f: F,
) -> R
where
F: FnOnce(&mut Self, &mut Emitter<'arena, 'decl>, &[ast::Stmt]) -> R,
{
self.jump_targets_gen.with_using(finally_label);
self.run_and_release_ids(e, |env, e| f(env, e, block))
}
pub fn do_function<'decl, R, F>(
&mut self,
e: &mut Emitter<'arena, 'decl>,
defs: &[ast::Stmt],
f: F,
) -> R
where
F: FnOnce(&mut Self, &mut Emitter<'arena, 'decl>, &[ast::Stmt]) -> R,
{
self.jump_targets_gen.with_function();
self.run_and_release_ids(e, |env, e| f(env, e, defs))
}
fn run_and_release_ids<'decl, R, F>(&mut self, e: &mut Emitter<'arena, 'decl>, f: F) -> R
where
F: FnOnce(&mut Self, &mut Emitter<'arena, 'decl>) -> R,
{
let res = f(self, e);
self.jump_targets_gen.release_ids();
self.jump_targets_gen.revert();
res
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn make_env() {
let a = bumpalo::Bump::new();
let alloc: &bumpalo::Bump = &a;
let namespace = Arc::new(NamespaceEnv::empty(vec![], false, false));
let _: Env<'_, '_> = Env::default(alloc, namespace);
}
}