Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/parsing/expression-scope-reparenter.h"
6 :
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"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : namespace {
16 :
17 : class Reparenter final : public AstTraversalVisitor<Reparenter> {
18 : public:
19 : Reparenter(uintptr_t stack_limit, Expression* initializer, Scope* scope)
20 1688 : : AstTraversalVisitor(stack_limit, initializer), scope_(scope) {}
21 :
22 : private:
23 : // This is required so that the overriden Visit* methods can be
24 : // called by the base class (template).
25 : friend class AstTraversalVisitor<Reparenter>;
26 :
27 : void VisitFunctionLiteral(FunctionLiteral* expr);
28 : void VisitClassLiteral(ClassLiteral* expr);
29 : void VisitVariableProxy(VariableProxy* expr);
30 :
31 : void VisitBlock(Block* stmt);
32 : void VisitTryCatchStatement(TryCatchStatement* stmt);
33 : void VisitWithStatement(WithStatement* stmt);
34 :
35 : Scope* scope_;
36 : };
37 :
38 152 : void Reparenter::VisitFunctionLiteral(FunctionLiteral* function_literal) {
39 152 : function_literal->scope()->ReplaceOuterScope(scope_);
40 : }
41 :
42 0 : void Reparenter::VisitClassLiteral(ClassLiteral* class_literal) {
43 0 : class_literal->scope()->ReplaceOuterScope(scope_);
44 : // No need to visit the constructor since it will have the class
45 : // scope on its scope chain.
46 : DCHECK_EQ(class_literal->constructor()->scope()->outer_scope(),
47 : class_literal->scope());
48 : #if DEBUG
49 : // The same goes for the rest of the class, but we do some
50 : // sanity checking in debug mode.
51 : ZoneList<ClassLiteralProperty*>* props = class_literal->properties();
52 : for (int i = 0; i < props->length(); ++i) {
53 : ClassLiteralProperty* prop = props->at(i);
54 : // No need to visit the values, since all values are functions with
55 : // the class scope on their scope chain.
56 : DCHECK(prop->value()->IsFunctionLiteral());
57 : DCHECK_EQ(prop->value()->AsFunctionLiteral()->scope()->outer_scope(),
58 : class_literal->scope());
59 : }
60 : #endif
61 : }
62 :
63 3827 : void Reparenter::VisitVariableProxy(VariableProxy* proxy) {
64 3827 : if (!proxy->is_resolved()) {
65 1259 : if (scope_->outer_scope()->RemoveUnresolved(proxy)) {
66 1259 : scope_->AddUnresolved(proxy);
67 : }
68 : } else {
69 : // Ensure that temporaries we find are already in the correct scope.
70 : DCHECK(proxy->var()->mode() != TEMPORARY ||
71 : proxy->var()->scope() == scope_->GetClosureScope());
72 : }
73 3827 : }
74 :
75 210 : void Reparenter::VisitBlock(Block* stmt) {
76 210 : if (stmt->scope() != nullptr)
77 30 : stmt->scope()->ReplaceOuterScope(scope_);
78 : else
79 180 : VisitStatements(stmt->statements());
80 210 : }
81 :
82 80 : void Reparenter::VisitTryCatchStatement(TryCatchStatement* stmt) {
83 40 : Visit(stmt->try_block());
84 80 : stmt->scope()->ReplaceOuterScope(scope_);
85 40 : }
86 :
87 20 : void Reparenter::VisitWithStatement(WithStatement* stmt) {
88 10 : Visit(stmt->expression());
89 20 : stmt->scope()->ReplaceOuterScope(scope_);
90 10 : }
91 :
92 : } // anonymous namespace
93 :
94 1688 : void ReparentExpressionScope(uintptr_t stack_limit, Expression* expr,
95 : Scope* scope) {
96 : // The only case that uses this code is block scopes for parameters containing
97 : // sloppy eval.
98 : DCHECK(scope->is_block_scope());
99 : DCHECK(scope->is_declaration_scope());
100 : DCHECK(scope->AsDeclarationScope()->calls_sloppy_eval());
101 : DCHECK(scope->outer_scope()->is_function_scope());
102 :
103 : Reparenter r(stack_limit, expr, scope);
104 : r.Run();
105 1688 : }
106 :
107 : } // namespace internal
108 : } // namespace v8
|