-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathcapture.rs
369 lines (351 loc) · 11.2 KB
/
capture.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::collections::{BTreeMap, BTreeSet};
use crate::{
ast::{ArgList, Expr, ExprKind, InterpolatedStringPart, Literal, Phase, Reference, Scope, StmtKind, Symbol},
debug,
type_check::type_env::TypeEnv,
type_check::Type,
};
/* This is a definition of how a resource is captured. The most basic way to capture a resource
is to use a subset of its client's methods. In that case we need to specify the name of the method
used in the capture. Currently this is the only capture definition supported.
In the future we might want add more verbose capture definitions like regexes on method parameters etc. */
#[derive(Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct CaptureDef {
pub method: String,
}
struct Capture {
pub object: Symbol,
pub def: CaptureDef,
}
pub type Captures = BTreeMap<String, BTreeSet<CaptureDef>>;
fn collect_captures(capture_list: Vec<Capture>) -> Captures {
let mut captures: Captures = BTreeMap::new();
for capture in capture_list {
captures
.entry(capture.object.name)
.or_insert(BTreeSet::new())
.insert(capture.def);
}
captures
}
pub fn scan_for_inflights_in_scope(scope: &Scope) {
for s in scope.statements.iter() {
match &s.kind {
StmtKind::ForLoop {
iterator: _,
iterable: _,
statements,
} => scan_for_inflights_in_scope(&statements),
StmtKind::If {
condition: _,
statements,
else_statements,
} => {
scan_for_inflights_in_scope(statements);
if let Some(else_statements) = else_statements {
scan_for_inflights_in_scope(else_statements);
}
}
StmtKind::Scope(s) => scan_for_inflights_in_scope(s),
StmtKind::Class {
name: _,
members: _,
methods,
constructor,
parent: _,
is_resource: _,
} => {
match constructor.signature.flight {
Phase::Inflight => {
// TODO: what do I do with these?
scan_captures_in_inflight_scope(&constructor.statements);
}
Phase::Independent => scan_for_inflights_in_scope(&constructor.statements),
Phase::Preflight => scan_for_inflights_in_scope(&constructor.statements),
}
for (_, method_def) in methods.iter() {
match method_def.signature.flight {
Phase::Inflight => {
// TODO: what do I do with these?
scan_captures_in_inflight_scope(&method_def.statements);
}
Phase::Independent => scan_for_inflights_in_scope(&constructor.statements),
Phase::Preflight => scan_for_inflights_in_scope(&method_def.statements),
}
}
}
StmtKind::VariableDef {
var_name: _,
initial_value,
type_: _,
} => {
scan_for_inflights_in_expression(initial_value);
}
StmtKind::Expression(exp) => {
scan_for_inflights_in_expression(exp);
}
StmtKind::Assignment { variable: _, value } => {
scan_for_inflights_in_expression(value);
}
StmtKind::Return(Some(exp)) => {
scan_for_inflights_in_expression(exp);
}
_ => (),
}
}
}
pub fn scan_for_inflights_in_expression(expr: &Expr) {
match &expr.kind {
ExprKind::New {
class: _,
obj_id: _,
obj_scope,
arg_list,
} => {
if let Some(obj_scope) = obj_scope {
scan_for_inflights_in_expression(obj_scope);
}
scan_for_inflights_in_arglist(arg_list);
}
ExprKind::Literal(Literal::InterpolatedString(istr)) => {
for part in istr.parts.iter() {
if let InterpolatedStringPart::Expr(e) = part {
scan_for_inflights_in_expression(e);
}
}
}
ExprKind::Reference(Reference::NestedIdentifier { object, property: _ }) => {
scan_for_inflights_in_expression(object);
}
ExprKind::Call { function, args } => {
if let Reference::NestedIdentifier { object, property: _ } = function {
scan_for_inflights_in_expression(object);
}
scan_for_inflights_in_arglist(args);
}
ExprKind::Unary { op: _, exp } => {
scan_for_inflights_in_expression(exp);
}
ExprKind::Binary { op: _, lexp, rexp } => {
scan_for_inflights_in_expression(lexp);
scan_for_inflights_in_expression(rexp);
}
ExprKind::StructLiteral { type_: _, fields } => {
for (_, value) in fields.iter() {
scan_for_inflights_in_expression(value);
}
}
ExprKind::MapLiteral { type_: _, fields } => {
for (_, value) in fields.iter() {
scan_for_inflights_in_expression(value);
}
}
ExprKind::FunctionClosure(func_def) => {
if let Phase::Inflight = func_def.signature.flight {
let mut func_captures = func_def.captures.borrow_mut();
assert!(func_captures.is_none());
*func_captures = Some(collect_captures(scan_captures_in_inflight_scope(&func_def.statements)));
}
}
_ => (),
}
}
fn scan_for_inflights_in_arglist(args: &ArgList) {
for arg in args.pos_args.iter() {
scan_for_inflights_in_expression(arg);
}
for (_, arg_expr) in args.named_args.iter() {
scan_for_inflights_in_expression(arg_expr);
}
}
fn scan_captures_in_call(reference: &Reference, args: &ArgList, env: &TypeEnv, statement_idx: usize) -> Vec<Capture> {
let mut res = vec![];
if let Reference::NestedIdentifier { object, property } = reference {
res.extend(scan_captures_in_expression(&object, env, statement_idx));
// If the expression evaluates to a resource we should check what method of the resource we're accessing
if let &Type::ResourceObject(resource) = object.evaluated_type.borrow().unwrap().into() {
let resource = resource.as_resource().unwrap();
let (prop_type, _flight) = match resource.env.lookup_ext(property, None) {
Ok(_type) => _type,
Err(type_error) => {
panic!("Type error: {}", type_error);
}
};
let func = prop_type.as_function_sig().unwrap();
if matches!(func.flight, Phase::Preflight) {
panic!("Can't access preflight method {} inflight", property);
}
debug!(
"We seem to be accessing the preflight method {}.{} {} inflight!",
resource.name.name, property.name, property.span
);
}
}
for arg in args.pos_args.iter() {
res.extend(scan_captures_in_expression(&arg, env, statement_idx));
}
for arg in args.named_args.values() {
res.extend(scan_captures_in_expression(arg, env, statement_idx));
}
res
}
fn scan_captures_in_expression(exp: &Expr, env: &TypeEnv, statement_idx: usize) -> Vec<Capture> {
let mut res = vec![];
match &exp.kind {
ExprKind::New {
class: _,
obj_id: _,
obj_scope: _,
arg_list,
} => {
for e in arg_list.pos_args.iter() {
res.extend(scan_captures_in_expression(e, env, statement_idx));
}
for e in arg_list.named_args.values() {
res.extend(scan_captures_in_expression(e, env, statement_idx));
}
}
ExprKind::Reference(r) => match r {
Reference::Identifier(symbol) => {
// Lookup the symbol
let (t, f) = match env.lookup_ext(&symbol, Some(statement_idx)) {
Ok(_type) => _type,
Err(type_error) => {
panic!("Type error: {}", type_error);
}
};
if let (Some(resource), Phase::Preflight) = (t.as_resource_object(), f) {
// TODO: for now we add all resource client methods to the capture, in the future this should be done based on:
// 1. explicit capture definitions
// 2. analyzing inflight code and figuring out what methods are being used on the object
res.extend(
resource
.methods()
.filter(|(_, sig)| {
matches!(
sig.as_function_sig().unwrap().flight,
Phase::Inflight
)
})
.map(|(name, _)| Capture {
object: symbol.clone(),
def: CaptureDef { method: name.clone() },
})
.collect::<Vec<Capture>>(),
);
}
}
Reference::NestedIdentifier { object, property: _ } => {
res.extend(scan_captures_in_expression(object, env, statement_idx));
// If the expression evaluates to a resource we should check if we need to capture the property as well
// TODO: do we really need this? I think we capture `object` above recursively and therefore don't need special handling of `property`.
// if let &Type::ResourceObject(resource) = object.evaluated_type.borrow().unwrap().into() {
// let resource = resource.as_resource().unwrap();
// let (prop_type, flight) = resource.env.lookup_ext(property);
// if prop_type.as_resource_object().is_some() && matches!(flight, Flight::Pre) {
// debug!(
// "We seem to be accessing the preflight resource {} {} inflight!",
// r, property.span
// );
// res.push(r);
// }
// }
}
},
ExprKind::Call { function, args } => res.extend(scan_captures_in_call(&function, &args, env, statement_idx)),
ExprKind::Unary { op: _, exp } => res.extend(scan_captures_in_expression(exp, env, statement_idx)),
ExprKind::Binary { op: _, lexp, rexp } => {
scan_captures_in_expression(lexp, env, statement_idx);
scan_captures_in_expression(rexp, env, statement_idx);
}
ExprKind::Literal(_) => {}
ExprKind::StructLiteral { fields, .. } => {
for v in fields.values() {
res.extend(scan_captures_in_expression(&v, env, statement_idx));
}
}
ExprKind::MapLiteral { fields, .. } => {
for v in fields.values() {
res.extend(scan_captures_in_expression(&v, env, statement_idx));
}
}
ExprKind::FunctionClosure(func_def) => {
// Can't define preflight stuff in inflight context
assert!(func_def.signature.flight != Phase::Preflight);
if let Phase::Inflight = func_def.signature.flight {
let mut func_captures = func_def.captures.borrow_mut();
assert!(func_captures.is_none());
*func_captures = Some(collect_captures(scan_captures_in_inflight_scope(&func_def.statements)));
}
}
}
res
}
fn scan_captures_in_inflight_scope(scope: &Scope) -> Vec<Capture> {
let mut res = vec![];
let env_ref = scope.env.borrow();
let env = env_ref.as_ref().unwrap();
// Make sure we're looking for captures only in inflight code
assert!(matches!(env.flight, Phase::Inflight));
for s in scope.statements.iter() {
match &s.kind {
StmtKind::VariableDef {
var_name: _,
initial_value,
type_: _,
} => res.extend(scan_captures_in_expression(initial_value, env, s.idx)),
StmtKind::ForLoop {
iterator: _,
iterable,
statements,
} => {
res.extend(scan_captures_in_expression(iterable, env, s.idx));
res.extend(scan_captures_in_inflight_scope(statements));
}
StmtKind::If {
condition,
statements,
else_statements,
} => {
res.extend(scan_captures_in_expression(condition, env, s.idx));
res.extend(scan_captures_in_inflight_scope(statements));
if let Some(else_statements) = else_statements {
res.extend(scan_captures_in_inflight_scope(else_statements));
}
}
StmtKind::Expression(e) => res.extend(scan_captures_in_expression(e, env, s.idx)),
StmtKind::Assignment { variable: _, value } => res.extend(scan_captures_in_expression(value, env, s.idx)),
StmtKind::Return(e) => {
if let Some(e) = e {
res.extend(scan_captures_in_expression(e, env, s.idx));
}
}
StmtKind::Scope(s) => res.extend(scan_captures_in_inflight_scope(s)),
StmtKind::Class {
name: _,
members: _,
methods,
constructor,
parent: _,
is_resource: _,
} => {
res.extend(scan_captures_in_inflight_scope(&constructor.statements));
for (_, m) in methods.iter() {
res.extend(scan_captures_in_inflight_scope(&m.statements))
}
}
StmtKind::Use {
module_name: _,
identifier: _,
} => {
todo!()
}
StmtKind::Struct {
name: _,
extends: _,
members: _,
} => {}
}
}
res
}