5 #include "src/parsing/expression-scope-reparenter.h" 7 #include "src/ast/ast-traversal-visitor.h" 8 #include "src/ast/ast.h" 9 #include "src/ast/scopes.h" 10 #include "src/objects-inl.h" 17 class Reparenter final :
public AstTraversalVisitor<Reparenter> {
19 Reparenter(
uintptr_t stack_limit, Expression* initializer, Scope* scope)
20 : AstTraversalVisitor(stack_limit, initializer), scope_(scope) {}
25 friend class AstTraversalVisitor<Reparenter>;
27 void VisitFunctionLiteral(FunctionLiteral* expr);
28 void VisitClassLiteral(ClassLiteral* expr);
29 void VisitVariableProxy(VariableProxy* expr);
30 void VisitRewritableExpression(RewritableExpression* expr);
32 void VisitBlock(Block* stmt);
33 void VisitTryCatchStatement(TryCatchStatement* stmt);
34 void VisitWithStatement(WithStatement* stmt);
39 void Reparenter::VisitFunctionLiteral(FunctionLiteral* function_literal) {
40 function_literal->scope()->ReplaceOuterScope(scope_);
43 void Reparenter::VisitClassLiteral(ClassLiteral* class_literal) {
44 class_literal->scope()->ReplaceOuterScope(scope_);
47 DCHECK_EQ(class_literal->constructor()->scope()->outer_scope(),
48 class_literal->scope());
50 if (class_literal->static_fields_initializer() !=
nullptr) {
52 class_literal->static_fields_initializer()->scope()->outer_scope(),
53 class_literal->scope());
58 ZonePtrList<ClassLiteralProperty>* props = class_literal->properties();
59 for (
int i = 0;
i < props->length(); ++
i) {
60 ClassLiteralProperty* prop = props->at(
i);
63 DCHECK(prop->value()->IsFunctionLiteral());
64 DCHECK_EQ(prop->value()->AsFunctionLiteral()->scope()->outer_scope(),
65 class_literal->scope());
70 void Reparenter::VisitVariableProxy(VariableProxy* proxy) {
71 if (!proxy->is_resolved()) {
72 if (scope_->outer_scope()->RemoveUnresolved(proxy)) {
73 scope_->AddUnresolved(proxy);
77 DCHECK(proxy->var()->mode() != VariableMode::kTemporary ||
78 proxy->var()->scope() == scope_->GetClosureScope());
82 void Reparenter::VisitRewritableExpression(RewritableExpression* expr) {
83 Visit(expr->expression());
84 expr->set_scope(scope_);
87 void Reparenter::VisitBlock(Block* stmt) {
89 stmt->scope()->ReplaceOuterScope(scope_);
91 VisitStatements(stmt->statements());
94 void Reparenter::VisitTryCatchStatement(TryCatchStatement* stmt) {
95 Visit(stmt->try_block());
97 stmt->scope()->ReplaceOuterScope(scope_);
99 Visit(stmt->catch_block());
103 void Reparenter::VisitWithStatement(WithStatement* stmt) {
104 Visit(stmt->expression());
105 stmt->scope()->ReplaceOuterScope(scope_);
110 void ReparentExpressionScope(
uintptr_t stack_limit, Expression* expr,
114 DCHECK(scope->is_block_scope());
115 DCHECK(scope->is_declaration_scope());
116 DCHECK(scope->AsDeclarationScope()->calls_sloppy_eval());
117 DCHECK(scope->outer_scope()->is_function_scope());
119 Reparenter r(stack_limit, expr, scope);