@samitouri / QOS-React-2 / commits / 8ed6c6ab08

[rust] simplify function context visitor

Joe Savona committed Jul 14, 2023 at 10:46 UTC 8ed6c6ab088c4cf022b85335534acd8991106161
6 files changed +94 -58
compiler/forget/crates/build-hir/src/build.rs
+2
@@ -440,7 +440,9 @@ fn lower_function<'a>(
440 expr: FunctionExpression,
441 ) -> Result<hir::FunctionExpression<'a>, BuildDiagnostic> {
442 let FunctionExpression { function, .. } = expr;
443 + println!("get_context_identifiers() ...");
444 let context_identifiers = get_context_identifiers(env, &function);
445 + println!("ok");
446 let mut context = env.vec_new();
447 let mut seen = HashSet::new();
448 for identifier in context_identifiers {
compiler/forget/crates/build-hir/src/context.rs
-34
@@ -50,40 +50,6 @@ impl<'ast> Visitor<'ast> for ContextVisitor<'ast> {
50 self.lvalue = prev_lvalue;
51 }
52
53 - fn visit_function(&mut self, function: &'ast Function) {
54 - self.visit_lvalue(|visitor| {
55 - for param in &function.params {
56 - visitor.visit_pattern(param);
57 - }
58 - });
59 - self.default_visit_function(function);
60 - }
61 -
62 - fn visit_expression(&mut self, expr: &'ast Expression) {
63 - if let Expression::AssignmentExpression(expr) = expr {
64 - self.visit_lvalue(|visitor| visitor.visit_assignment_target(&expr.left));
65 - self.visit_expression(&expr.right);
66 - return;
67 - }
68 - let mut object = expr;
69 - while let Expression::MemberExpression(expr) = object {
70 - match &expr.object {
71 - ExpressionOrSuper::Super(_) => return,
72 - ExpressionOrSuper::Expression(expr) => {
73 - object = expr;
74 - }
75 - }
76 - if expr.computed {
77 - self.visit_expression(&expr.property);
78 - }
79 - }
80 - if let Expression::Identifier(identifier) = object {
81 - self.visit_identifier(identifier);
82 - } else {
83 - self.default_visit_expression(expr);
84 - }
85 - }
86 -
53 fn visit_identifier(&mut self, identifier: &'ast Identifier) {
54 let binding = identifier.binding.unwrap();
55 match binding {
compiler/forget/crates/estree/src/visit.rs
+20 -5
@@ -1,7 +1,7 @@
1 use crate::{
2 - AssignmentTarget, Expression, ExpressionOrSpread, ForInInit, ForInit, Function, FunctionBody,
3 - Identifier, ImportDeclarationSpecifier, ImportOrExportDeclaration, Literal, ModuleItem,
4 - Pattern, Program, Statement, SwitchCase, VariableDeclarator,
2 + AssignmentTarget, Expression, ExpressionOrSpread, ExpressionOrSuper, ForInInit, ForInit,
3 + Function, FunctionBody, Identifier, ImportDeclarationSpecifier, ImportOrExportDeclaration,
4 + Literal, ModuleItem, Pattern, Program, Statement, SwitchCase, VariableDeclarator,
5 };
6
7 /// Trait for visiting an estree
@@ -24,7 +24,11 @@ pub trait Visitor<'ast> {
24 }
25
26 fn default_visit_function(&mut self, function: &'ast Function) {
27 - // todo: params and such
27 + self.visit_lvalue(|visitor| {
28 + for param in &function.params {
29 + visitor.visit_pattern(param);
30 + }
31 + });
32 match &function.body {
33 Some(FunctionBody::BlockStatement(body)) => {
34 for stmt in &body.body {
@@ -247,7 +251,7 @@ pub trait Visitor<'ast> {
251 }
252
253 fn visit_expression(&mut self, expr: &'ast Expression) {
250 - self.visit_expression(expr);
254 + self.default_visit_expression(expr);
255 }
256
257 fn default_visit_expression(&mut self, expr: &'ast Expression) {
@@ -276,6 +280,17 @@ pub trait Visitor<'ast> {
280 }
281 Expression::Literal(expr) => self.visit_literal(expr),
282 Expression::FunctionExpression(expr) => self.visit_function(&expr.function),
283 + Expression::MemberExpression(expr) => {
284 + match &expr.object {
285 + ExpressionOrSuper::Super(_object) => {
286 + // todo
287 + }
288 + ExpressionOrSuper::Expression(object) => self.visit_expression(object),
289 + };
290 + if !expr.computed {
291 + self.visit_expression(&expr.property)
292 + }
293 + }
294 _ => {
295 todo!("more expression types")
296 }
compiler/forget/crates/fixtures/tests/fixtures/function-expressions.js
+11 -3
@@ -1,6 +1,6 @@
1 function Component(props) {
2 - const y = 2;
3 - const foo = function foo(x) {
2 + const x = 2;
3 + const foo = function foo(y) {
4 let a = 1;
5 let b;
6 if (a === 1) {
@@ -8,6 +8,14 @@ function Component(props) {
8 } else {
9 b = false;
10 }
11 - return x + y + b;
11 + x + y + a + b;
12 + const bar = function bar(z) {
13 + let c = 2;
14 + let d;
15 + d = 3;
16 + x + y + a + b + z + c + d;
17 + };
18 + bar;
19 + foo;
20 };
21 }
compiler/forget/crates/fixtures/tests/fixtures_test.rs
+6 -1
@@ -16,6 +16,7 @@ fn fixtures() {
16 println!("fixture {}", path.to_str().unwrap());
17 let input = std::fs::read_to_string(path).unwrap();
18 let ast = parse(&input, path.to_str().unwrap()).unwrap();
19 + println!("ok parse");
20
21 let mut output = String::new();
22
@@ -39,11 +40,15 @@ fn fixtures() {
40 // let mut out = String::new();
41 // fun.print(&fun.body, &mut out).unwrap();
42 // println!("{out}");
42 -
43 + println!("ok build");
44 enter_ssa(&environment, &mut fun).unwrap();
45 + println!("ok enter_ssa");
46 eliminate_redundant_phis(&environment, &mut fun);
47 + println!("ok eliminate_redundant_phis");
48 constant_propagation(&environment, &mut fun);
49 + println!("ok constant_propagation");
50 fun.print(&fun.body, &mut output).unwrap();
51 + println!("ok print");
52 }
53 Err(error) => {
54 write!(&mut output, "{}", error,).unwrap();
compiler/forget/crates/fixtures/tests/snapshots/fixtures_test__fixtures@function-expressions.js.snap
+55 -15
@@ -5,8 +5,8 @@ input_file: crates/fixtures/tests/fixtures/function-expressions.js
5 ---
6 Input:
7 function Component(props) {
8 - const y = 2;
9 - const foo = function foo(x) {
8 + const x = 2;
9 + const foo = function foo(y) {
10 let a = 1;
11 let b;
12 if (a === 1) {
@@ -14,28 +14,36 @@ function Component(props) {
14 } else {
15 b = false;
16 }
17 - return x + y + b;
17 + x + y + a + b;
18 + const bar = function bar(z) {
19 + let c = 2;
20 + let d;
21 + d = 3;
22 + x + y + a + b + z + c + d;
23 + };
24 + bar;
25 + foo;
26 };
27 }
28
29
30 Output:
31 function Component(
24 - unknown props$6,
32 + unknown props$11,
33 )
34 entry bb0
35 bb0 (block)
36 [0] #0 = 2
29 - [1] #1 = StoreLocal Const unknown y$7 = unknown #0
30 - [2] #2 = Function @deps[] @context[unknown y$7]:
37 + [1] #1 = StoreLocal Const unknown x$12 = unknown #0
38 + [2] #2 = Function @deps[] @context[unknown x$12, unknown foo$1]:
39 function foo(
32 - unknown x$8,
40 + unknown y$13,
41 )
42 entry bb1
43 bb1 (block)
44 [0] #0 = 1
37 - [1] #1 = StoreLocal Let unknown a$9 = unknown #0
38 - [2] #2 = DeclareLocal Let unknown b$10
45 + [1] #1 = StoreLocal Let unknown a$14 = unknown #0
46 + [2] #2 = DeclareLocal Let unknown b$15
47 [3] #9 = 1
48 [4] #10 = 1
49 [5] #11 = true
@@ -45,16 +53,48 @@ bb0 (block)
53 [7] #3 = 5
54 [8] #4 = 3
55 [9] #5 = 8
48 - [10] #6 = StoreLocal Reassign unknown b$11 = unknown #5
56 + [10] #6 = StoreLocal Reassign unknown b$16 = unknown #5
57 [11] Goto bb2
58 bb2 (block)
59 predecessors: bb3
52 - [12] #12 = LoadLocal unknown x$8
53 - [13] #13 = 2
60 + [12] #12 = 2
61 + [13] #13 = LoadLocal unknown y$13
62 [14] #14 = Binary unknown #12 + unknown #13
55 - [15] #15 = 8
63 + [15] #15 = 1
64 [16] #16 = Binary unknown #14 + unknown #15
57 - [17] Return unknown #16
58 - [3] #3 = StoreLocal Const unknown foo$16 = unknown #2
65 + [17] #17 = 8
66 + [18] #18 = Binary unknown #16 + unknown #17
67 + [19] #19 = Function @deps[] @context[unknown x$18, unknown y$19, unknown a$20, unknown b$21]:
68 + function bar(
69 + unknown z$22,
70 + )
71 + entry bb5
72 + bb5 (block)
73 + [0] #0 = 2
74 + [1] #1 = StoreLocal Let unknown c$23 = unknown #0
75 + [2] #2 = DeclareLocal Let unknown d$24
76 + [3] #3 = 3
77 + [4] #4 = StoreLocal Reassign unknown d$25 = unknown #3
78 + [5] #5 = LoadLocal unknown x$18
79 + [6] #6 = LoadLocal unknown y$19
80 + [7] #7 = Binary unknown #5 + unknown #6
81 + [8] #8 = LoadLocal unknown a$20
82 + [9] #9 = Binary unknown #7 + unknown #8
83 + [10] #10 = LoadLocal unknown b$21
84 + [11] #11 = Binary unknown #9 + unknown #10
85 + [12] #12 = LoadLocal unknown z$22
86 + [13] #13 = Binary unknown #11 + unknown #12
87 + [14] #14 = 2
88 + [15] #15 = Binary unknown #13 + unknown #14
89 + [16] #16 = 3
90 + [17] #17 = Binary unknown #15 + unknown #16
91 + [18] #18 = <undefined>
92 + [19] Return unknown #18
93 + [20] #20 = StoreLocal Const unknown bar$26 = unknown #19
94 + [21] #21 = LoadLocal unknown bar$26
95 + [22] #22 = LoadLocal unknown foo$1
96 + [23] #23 = <undefined>
97 + [24] Return unknown #23
98 + [3] #3 = StoreLocal Const unknown foo$28 = unknown #2
99 [4] #4 = <undefined>
100 [5] Return unknown #4