OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/ast/ast.h" | 10 #include "src/ast/ast.h" |
(...skipping 1961 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1972 VariableMode mode, int pos, bool* ok) { | 1972 VariableMode mode, int pos, bool* ok) { |
1973 return DeclareVariable(name, mode, DefaultInitializationFlag(mode), pos, ok); | 1973 return DeclareVariable(name, mode, DefaultInitializationFlag(mode), pos, ok); |
1974 } | 1974 } |
1975 | 1975 |
1976 Declaration* Parser::DeclareVariable(const AstRawString* name, | 1976 Declaration* Parser::DeclareVariable(const AstRawString* name, |
1977 VariableMode mode, InitializationFlag init, | 1977 VariableMode mode, InitializationFlag init, |
1978 int pos, bool* ok) { | 1978 int pos, bool* ok) { |
1979 DCHECK_NOT_NULL(name); | 1979 DCHECK_NOT_NULL(name); |
1980 VariableProxy* proxy = NewUnresolved(name, mode); | 1980 VariableProxy* proxy = NewUnresolved(name, mode); |
1981 Declaration* declaration = | 1981 Declaration* declaration = |
1982 factory()->NewVariableDeclaration(proxy, mode, scope(), pos); | 1982 factory()->NewVariableDeclaration(proxy, scope(), pos); |
1983 Declare(declaration, DeclarationDescriptor::NORMAL, init, CHECK_OK); | 1983 Declare(declaration, DeclarationDescriptor::NORMAL, mode, init, CHECK_OK); |
1984 return declaration; | 1984 return declaration; |
1985 } | 1985 } |
1986 | 1986 |
1987 Variable* Parser::Declare(Declaration* declaration, | 1987 Variable* Parser::Declare(Declaration* declaration, |
1988 DeclarationDescriptor::Kind declaration_kind, | 1988 DeclarationDescriptor::Kind declaration_kind, |
1989 InitializationFlag init, bool* ok, Scope* scope) { | 1989 VariableMode mode, InitializationFlag init, bool* ok, |
| 1990 Scope* scope) { |
| 1991 DCHECK(IsDeclaredVariableMode(mode) && mode != CONST_LEGACY); |
| 1992 |
1990 VariableProxy* proxy = declaration->proxy(); | 1993 VariableProxy* proxy = declaration->proxy(); |
1991 DCHECK(proxy->raw_name() != NULL); | 1994 DCHECK(proxy->raw_name() != NULL); |
1992 const AstRawString* name = proxy->raw_name(); | 1995 const AstRawString* name = proxy->raw_name(); |
1993 VariableMode mode = declaration->mode(); | 1996 |
1994 DCHECK(IsDeclaredVariableMode(mode) && mode != CONST_LEGACY); | |
1995 bool is_function_declaration = declaration->IsFunctionDeclaration(); | |
1996 if (scope == nullptr) scope = this->scope(); | 1997 if (scope == nullptr) scope = this->scope(); |
1997 if (mode == VAR) scope = scope->GetDeclarationScope(); | 1998 if (mode == VAR) scope = scope->GetDeclarationScope(); |
1998 | |
1999 DCHECK(!scope->is_catch_scope()); | 1999 DCHECK(!scope->is_catch_scope()); |
2000 DCHECK(!scope->is_with_scope()); | 2000 DCHECK(!scope->is_with_scope()); |
2001 DCHECK(scope->is_declaration_scope() || | 2001 DCHECK(scope->is_declaration_scope() || |
2002 (IsLexicalVariableMode(mode) && scope->is_block_scope())); | 2002 (IsLexicalVariableMode(mode) && scope->is_block_scope())); |
2003 | 2003 |
| 2004 bool is_function_declaration = declaration->IsFunctionDeclaration(); |
| 2005 |
2004 Variable* var = NULL; | 2006 Variable* var = NULL; |
2005 if (scope->is_eval_scope() && is_sloppy(scope->language_mode()) && | 2007 if (scope->is_eval_scope() && is_sloppy(scope->language_mode()) && |
2006 mode == VAR) { | 2008 mode == VAR) { |
2007 // In a var binding in a sloppy direct eval, pollute the enclosing scope | 2009 // In a var binding in a sloppy direct eval, pollute the enclosing scope |
2008 // with this new binding by doing the following: | 2010 // with this new binding by doing the following: |
2009 // The proxy is bound to a lookup variable to force a dynamic declaration | 2011 // The proxy is bound to a lookup variable to force a dynamic declaration |
2010 // using the DeclareEvalVar or DeclareEvalFunction runtime functions. | 2012 // using the DeclareEvalVar or DeclareEvalFunction runtime functions. |
2011 Variable::Kind kind = Variable::NORMAL; | 2013 Variable::Kind kind = Variable::NORMAL; |
2012 // TODO(sigurds) figure out if kNotAssigned is OK here | 2014 // TODO(sigurds) figure out if kNotAssigned is OK here |
2013 var = new (zone()) Variable(scope, name, mode, kind, init, kNotAssigned); | 2015 var = new (zone()) Variable(scope, name, mode, kind, init, kNotAssigned); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2205 : FunctionKind::kNormalFunction, | 2207 : FunctionKind::kNormalFunction, |
2206 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); | 2208 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); |
2207 | 2209 |
2208 // In ES6, a function behaves as a lexical binding, except in | 2210 // In ES6, a function behaves as a lexical binding, except in |
2209 // a script scope, or the initial scope of eval or another function. | 2211 // a script scope, or the initial scope of eval or another function. |
2210 VariableMode mode = | 2212 VariableMode mode = |
2211 (!scope()->is_declaration_scope() || scope()->is_module_scope()) ? LET | 2213 (!scope()->is_declaration_scope() || scope()->is_module_scope()) ? LET |
2212 : VAR; | 2214 : VAR; |
2213 VariableProxy* proxy = NewUnresolved(variable_name, mode); | 2215 VariableProxy* proxy = NewUnresolved(variable_name, mode); |
2214 Declaration* declaration = | 2216 Declaration* declaration = |
2215 factory()->NewFunctionDeclaration(proxy, mode, fun, scope(), pos); | 2217 factory()->NewFunctionDeclaration(proxy, fun, scope(), pos); |
2216 Declare(declaration, DeclarationDescriptor::NORMAL, kCreatedInitialized, | 2218 Declare(declaration, DeclarationDescriptor::NORMAL, mode, kCreatedInitialized, |
2217 CHECK_OK); | 2219 CHECK_OK); |
2218 if (names) names->Add(variable_name, zone()); | 2220 if (names) names->Add(variable_name, zone()); |
2219 EmptyStatement* empty = factory()->NewEmptyStatement(kNoSourcePosition); | 2221 EmptyStatement* empty = factory()->NewEmptyStatement(kNoSourcePosition); |
2220 // Async functions don't undergo sloppy mode block scoped hoisting, and don't | 2222 // Async functions don't undergo sloppy mode block scoped hoisting, and don't |
2221 // allow duplicates in a block. Both are represented by the | 2223 // allow duplicates in a block. Both are represented by the |
2222 // sloppy_block_function_map. Don't add them to the map for async functions. | 2224 // sloppy_block_function_map. Don't add them to the map for async functions. |
2223 // Generators are also supposed to be prohibited; currently doing this behind | 2225 // Generators are also supposed to be prohibited; currently doing this behind |
2224 // a flag and UseCounting violations to assess web compatibility. | 2226 // a flag and UseCounting violations to assess web compatibility. |
2225 if (is_sloppy(language_mode()) && !scope()->is_declaration_scope() && | 2227 if (is_sloppy(language_mode()) && !scope()->is_declaration_scope() && |
2226 !is_async && !(allow_harmony_restrictive_generators() && is_generator)) { | 2228 !is_async && !(allow_harmony_restrictive_generators() && is_generator)) { |
(...skipping 2705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4932 // in the previously reserved spot. | 4934 // in the previously reserved spot. |
4933 // NOTE: We create a proxy and resolve it here so that in the | 4935 // NOTE: We create a proxy and resolve it here so that in the |
4934 // future we can change the AST to only refer to VariableProxies | 4936 // future we can change the AST to only refer to VariableProxies |
4935 // instead of Variables and Proxies as is the case now. | 4937 // instead of Variables and Proxies as is the case now. |
4936 DCHECK_EQ(function_scope, scope()); | 4938 DCHECK_EQ(function_scope, scope()); |
4937 VariableMode fvar_mode = is_strict(language_mode()) ? CONST : CONST_LEGACY; | 4939 VariableMode fvar_mode = is_strict(language_mode()) ? CONST : CONST_LEGACY; |
4938 Variable* fvar = new (zone()) | 4940 Variable* fvar = new (zone()) |
4939 Variable(scope(), function_name, fvar_mode, Variable::NORMAL, | 4941 Variable(scope(), function_name, fvar_mode, Variable::NORMAL, |
4940 kCreatedInitialized, kNotAssigned); | 4942 kCreatedInitialized, kNotAssigned); |
4941 VariableProxy* proxy = factory()->NewVariableProxy(fvar); | 4943 VariableProxy* proxy = factory()->NewVariableProxy(fvar); |
4942 VariableDeclaration* fvar_declaration = factory()->NewVariableDeclaration( | 4944 VariableDeclaration* fvar_declaration = |
4943 proxy, fvar_mode, scope(), kNoSourcePosition); | 4945 factory()->NewVariableDeclaration(proxy, scope(), kNoSourcePosition); |
4944 function_scope->DeclareFunctionVar(fvar_declaration); | 4946 function_scope->DeclareFunctionVar(fvar_declaration); |
4945 | 4947 |
4946 VariableProxy* fproxy = factory()->NewVariableProxy(fvar); | 4948 VariableProxy* fproxy = factory()->NewVariableProxy(fvar); |
4947 result->Set(kFunctionNameAssignmentIndex, | 4949 result->Set(kFunctionNameAssignmentIndex, |
4948 factory()->NewExpressionStatement( | 4950 factory()->NewExpressionStatement( |
4949 factory()->NewAssignment(Token::INIT, fproxy, | 4951 factory()->NewAssignment(Token::INIT, fproxy, |
4950 factory()->NewThisFunction(pos), | 4952 factory()->NewThisFunction(pos), |
4951 kNoSourcePosition), | 4953 kNoSourcePosition), |
4952 kNoSourcePosition)); | 4954 kNoSourcePosition)); |
4953 } | 4955 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5013 BlockState block_state(&scope_state_); | 5015 BlockState block_state(&scope_state_); |
5014 RaiseLanguageMode(STRICT); | 5016 RaiseLanguageMode(STRICT); |
5015 #ifdef DEBUG | 5017 #ifdef DEBUG |
5016 scope()->SetScopeName(name); | 5018 scope()->SetScopeName(name); |
5017 #endif | 5019 #endif |
5018 | 5020 |
5019 VariableProxy* proxy = nullptr; | 5021 VariableProxy* proxy = nullptr; |
5020 if (name != nullptr) { | 5022 if (name != nullptr) { |
5021 proxy = NewUnresolved(name, CONST); | 5023 proxy = NewUnresolved(name, CONST); |
5022 // TODO(verwaest): declare via block_state. | 5024 // TODO(verwaest): declare via block_state. |
5023 Declaration* declaration = factory()->NewVariableDeclaration( | 5025 Declaration* declaration = |
5024 proxy, CONST, block_state.scope(), pos); | 5026 factory()->NewVariableDeclaration(proxy, block_state.scope(), pos); |
5025 Declare(declaration, DeclarationDescriptor::NORMAL, | 5027 Declare(declaration, DeclarationDescriptor::NORMAL, CONST, |
5026 DefaultInitializationFlag(CONST), CHECK_OK); | 5028 DefaultInitializationFlag(CONST), CHECK_OK); |
5027 } | 5029 } |
5028 | 5030 |
5029 Expression* extends = nullptr; | 5031 Expression* extends = nullptr; |
5030 if (Check(Token::EXTENDS)) { | 5032 if (Check(Token::EXTENDS)) { |
5031 block_state.set_start_position(scanner()->location().end_pos); | 5033 block_state.set_start_position(scanner()->location().end_pos); |
5032 ExpressionClassifier extends_classifier(this); | 5034 ExpressionClassifier extends_classifier(this); |
5033 extends = ParseLeftHandSideExpression(&extends_classifier, CHECK_OK); | 5035 extends = ParseLeftHandSideExpression(&extends_classifier, CHECK_OK); |
5034 CheckNoTailCallExpressions(&extends_classifier, CHECK_OK); | 5036 CheckNoTailCallExpressions(&extends_classifier, CHECK_OK); |
5035 RewriteNonPattern(&extends_classifier, CHECK_OK); | 5037 RewriteNonPattern(&extends_classifier, CHECK_OK); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5209 // For each var-binding that shadows a parameter, insert an assignment | 5211 // For each var-binding that shadows a parameter, insert an assignment |
5210 // initializing the variable with the parameter. | 5212 // initializing the variable with the parameter. |
5211 Scope* inner_scope = inner_block->scope(); | 5213 Scope* inner_scope = inner_block->scope(); |
5212 DCHECK(inner_scope->is_declaration_scope()); | 5214 DCHECK(inner_scope->is_declaration_scope()); |
5213 Scope* function_scope = inner_scope->outer_scope(); | 5215 Scope* function_scope = inner_scope->outer_scope(); |
5214 DCHECK(function_scope->is_function_scope()); | 5216 DCHECK(function_scope->is_function_scope()); |
5215 ZoneList<Declaration*>* decls = inner_scope->declarations(); | 5217 ZoneList<Declaration*>* decls = inner_scope->declarations(); |
5216 BlockState block_state(&scope_state_, inner_scope); | 5218 BlockState block_state(&scope_state_, inner_scope); |
5217 for (int i = 0; i < decls->length(); ++i) { | 5219 for (int i = 0; i < decls->length(); ++i) { |
5218 Declaration* decl = decls->at(i); | 5220 Declaration* decl = decls->at(i); |
5219 if (decl->mode() != VAR || !decl->IsVariableDeclaration()) continue; | 5221 if (decl->proxy()->var()->mode() != VAR || !decl->IsVariableDeclaration()) { |
| 5222 continue; |
| 5223 } |
5220 const AstRawString* name = decl->proxy()->raw_name(); | 5224 const AstRawString* name = decl->proxy()->raw_name(); |
5221 Variable* parameter = function_scope->LookupLocal(name); | 5225 Variable* parameter = function_scope->LookupLocal(name); |
5222 if (parameter == nullptr) continue; | 5226 if (parameter == nullptr) continue; |
5223 VariableProxy* to = NewUnresolved(name, VAR); | 5227 VariableProxy* to = NewUnresolved(name, VAR); |
5224 VariableProxy* from = factory()->NewVariableProxy(parameter); | 5228 VariableProxy* from = factory()->NewVariableProxy(parameter); |
5225 Expression* assignment = | 5229 Expression* assignment = |
5226 factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition); | 5230 factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition); |
5227 Statement* statement = | 5231 Statement* statement = |
5228 factory()->NewExpressionStatement(assignment, kNoSourcePosition); | 5232 factory()->NewExpressionStatement(assignment, kNoSourcePosition); |
5229 inner_block->statements()->InsertAt(0, statement, zone()); | 5233 inner_block->statements()->InsertAt(0, statement, zone()); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5291 } | 5295 } |
5292 query_scope = query_scope->outer_scope(); | 5296 query_scope = query_scope->outer_scope(); |
5293 } while (query_scope != outer_scope); | 5297 } while (query_scope != outer_scope); |
5294 | 5298 |
5295 if (!should_hoist) continue; | 5299 if (!should_hoist) continue; |
5296 | 5300 |
5297 // Declare a var-style binding for the function in the outer scope | 5301 // Declare a var-style binding for the function in the outer scope |
5298 if (!var_created) { | 5302 if (!var_created) { |
5299 var_created = true; | 5303 var_created = true; |
5300 VariableProxy* proxy = scope->NewUnresolved(factory(), name); | 5304 VariableProxy* proxy = scope->NewUnresolved(factory(), name); |
5301 Declaration* declaration = factory()->NewVariableDeclaration( | 5305 Declaration* declaration = |
5302 proxy, VAR, scope, kNoSourcePosition); | 5306 factory()->NewVariableDeclaration(proxy, scope, kNoSourcePosition); |
5303 Declare(declaration, DeclarationDescriptor::NORMAL, | 5307 Declare(declaration, DeclarationDescriptor::NORMAL, VAR, |
5304 DefaultInitializationFlag(VAR), ok, scope); | 5308 DefaultInitializationFlag(VAR), ok, scope); |
5305 DCHECK(ok); // Based on the preceding check, this should not fail | 5309 DCHECK(ok); // Based on the preceding check, this should not fail |
5306 if (!ok) return; | 5310 if (!ok) return; |
5307 } | 5311 } |
5308 | 5312 |
5309 // Read from the local lexical scope and write to the function scope | 5313 // Read from the local lexical scope and write to the function scope |
5310 VariableProxy* to = scope->NewUnresolved(factory(), name); | 5314 VariableProxy* to = scope->NewUnresolved(factory(), name); |
5311 VariableProxy* from = delegate->scope()->NewUnresolved(factory(), name); | 5315 VariableProxy* from = delegate->scope()->NewUnresolved(factory(), name); |
5312 Expression* assignment = | 5316 Expression* assignment = |
5313 factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition); | 5317 factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition); |
(...skipping 1777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7091 node->Print(Isolate::Current()); | 7095 node->Print(Isolate::Current()); |
7092 } | 7096 } |
7093 #endif // DEBUG | 7097 #endif // DEBUG |
7094 | 7098 |
7095 #undef CHECK_OK | 7099 #undef CHECK_OK |
7096 #undef CHECK_OK_VOID | 7100 #undef CHECK_OK_VOID |
7097 #undef CHECK_FAILED | 7101 #undef CHECK_FAILED |
7098 | 7102 |
7099 } // namespace internal | 7103 } // namespace internal |
7100 } // namespace v8 | 7104 } // namespace v8 |
OLD | NEW |