| 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 "src/api.h" | 7 #include "src/api.h" |
| 8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
| 9 #include "src/ast/ast-literal-reindexer.h" | 9 #include "src/ast/ast-literal-reindexer.h" |
| 10 #include "src/ast/scopeinfo.h" | 10 #include "src/ast/scopeinfo.h" |
| (...skipping 2007 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2018 } | 2018 } |
| 2019 | 2019 |
| 2020 | 2020 |
| 2021 VariableProxy* Parser::NewUnresolved(const AstRawString* name, | 2021 VariableProxy* Parser::NewUnresolved(const AstRawString* name, |
| 2022 VariableMode mode) { | 2022 VariableMode mode) { |
| 2023 // If we are inside a function, a declaration of a var/const variable is a | 2023 // If we are inside a function, a declaration of a var/const variable is a |
| 2024 // truly local variable, and the scope of the variable is always the function | 2024 // truly local variable, and the scope of the variable is always the function |
| 2025 // scope. | 2025 // scope. |
| 2026 // Let/const variables in harmony mode are always added to the immediately | 2026 // Let/const variables in harmony mode are always added to the immediately |
| 2027 // enclosing scope. | 2027 // enclosing scope. |
| 2028 return DeclarationScope(mode)->NewUnresolved( | 2028 Scope* scope = |
| 2029 factory(), name, Variable::NORMAL, scanner()->location().beg_pos, | 2029 IsLexicalVariableMode(mode) ? scope_ : scope_->DeclarationScope(); |
| 2030 scanner()->location().end_pos); | 2030 return scope->NewUnresolved(factory(), name, Variable::NORMAL, |
| 2031 scanner()->location().beg_pos, |
| 2032 scanner()->location().end_pos); |
| 2031 } | 2033 } |
| 2032 | 2034 |
| 2033 | 2035 |
| 2034 Variable* Parser::Declare(Declaration* declaration, | 2036 Variable* Parser::Declare(Declaration* declaration, |
| 2035 DeclarationDescriptor::Kind declaration_kind, | 2037 DeclarationDescriptor::Kind declaration_kind, |
| 2036 bool resolve, bool* ok, Scope* scope) { | 2038 bool resolve, bool* ok, Scope* scope) { |
| 2037 VariableProxy* proxy = declaration->proxy(); | 2039 VariableProxy* proxy = declaration->proxy(); |
| 2038 DCHECK(proxy->raw_name() != NULL); | 2040 DCHECK(proxy->raw_name() != NULL); |
| 2039 const AstRawString* name = proxy->raw_name(); | 2041 const AstRawString* name = proxy->raw_name(); |
| 2040 VariableMode mode = declaration->mode(); | 2042 VariableMode mode = declaration->mode(); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2203 Expect(Token::COMMA, CHECK_OK); | 2205 Expect(Token::COMMA, CHECK_OK); |
| 2204 } | 2206 } |
| 2205 } | 2207 } |
| 2206 Expect(Token::RPAREN, CHECK_OK); | 2208 Expect(Token::RPAREN, CHECK_OK); |
| 2207 Expect(Token::SEMICOLON, CHECK_OK); | 2209 Expect(Token::SEMICOLON, CHECK_OK); |
| 2208 | 2210 |
| 2209 // Make sure that the function containing the native declaration | 2211 // Make sure that the function containing the native declaration |
| 2210 // isn't lazily compiled. The extension structures are only | 2212 // isn't lazily compiled. The extension structures are only |
| 2211 // accessible while parsing the first time not when reparsing | 2213 // accessible while parsing the first time not when reparsing |
| 2212 // because of lazy compilation. | 2214 // because of lazy compilation. |
| 2213 DeclarationScope(VAR)->ForceEagerCompilation(); | 2215 // TODO(adamk): Should this be ClosureScope()? |
| 2216 scope_->DeclarationScope()->ForceEagerCompilation(); |
| 2214 | 2217 |
| 2215 // TODO(1240846): It's weird that native function declarations are | 2218 // TODO(1240846): It's weird that native function declarations are |
| 2216 // introduced dynamically when we meet their declarations, whereas | 2219 // introduced dynamically when we meet their declarations, whereas |
| 2217 // other functions are set up when entering the surrounding scope. | 2220 // other functions are set up when entering the surrounding scope. |
| 2218 VariableProxy* proxy = NewUnresolved(name, VAR); | 2221 VariableProxy* proxy = NewUnresolved(name, VAR); |
| 2219 Declaration* declaration = | 2222 Declaration* declaration = |
| 2220 factory()->NewVariableDeclaration(proxy, VAR, scope_, pos); | 2223 factory()->NewVariableDeclaration(proxy, VAR, scope_, pos); |
| 2221 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 2224 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); |
| 2222 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral( | 2225 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral( |
| 2223 name, extension_, RelocInfo::kNoPosition); | 2226 name, extension_, RelocInfo::kNoPosition); |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2440 parsing_result->descriptor.parser = this; | 2443 parsing_result->descriptor.parser = this; |
| 2441 parsing_result->descriptor.declaration_kind = DeclarationDescriptor::NORMAL; | 2444 parsing_result->descriptor.declaration_kind = DeclarationDescriptor::NORMAL; |
| 2442 parsing_result->descriptor.declaration_pos = peek_position(); | 2445 parsing_result->descriptor.declaration_pos = peek_position(); |
| 2443 parsing_result->descriptor.initialization_pos = peek_position(); | 2446 parsing_result->descriptor.initialization_pos = peek_position(); |
| 2444 parsing_result->descriptor.mode = VAR; | 2447 parsing_result->descriptor.mode = VAR; |
| 2445 // True if the binding needs initialization. 'let' and 'const' declared | 2448 // True if the binding needs initialization. 'let' and 'const' declared |
| 2446 // bindings are created uninitialized by their declaration nodes and | 2449 // bindings are created uninitialized by their declaration nodes and |
| 2447 // need initialization. 'var' declared bindings are always initialized | 2450 // need initialization. 'var' declared bindings are always initialized |
| 2448 // immediately by their declaration nodes. | 2451 // immediately by their declaration nodes. |
| 2449 parsing_result->descriptor.needs_init = false; | 2452 parsing_result->descriptor.needs_init = false; |
| 2450 parsing_result->descriptor.is_const = false; | |
| 2451 if (peek() == Token::VAR) { | 2453 if (peek() == Token::VAR) { |
| 2452 if (is_strong(language_mode())) { | 2454 if (is_strong(language_mode())) { |
| 2453 Scanner::Location location = scanner()->peek_location(); | 2455 Scanner::Location location = scanner()->peek_location(); |
| 2454 ReportMessageAt(location, MessageTemplate::kStrongVar); | 2456 ReportMessageAt(location, MessageTemplate::kStrongVar); |
| 2455 *ok = false; | 2457 *ok = false; |
| 2456 return; | 2458 return; |
| 2457 } | 2459 } |
| 2458 Consume(Token::VAR); | 2460 Consume(Token::VAR); |
| 2459 } else if (peek() == Token::CONST && allow_const()) { | 2461 } else if (peek() == Token::CONST && allow_const()) { |
| 2460 Consume(Token::CONST); | 2462 Consume(Token::CONST); |
| 2461 if (is_sloppy(language_mode()) && allow_legacy_const()) { | 2463 if (is_sloppy(language_mode()) && allow_legacy_const()) { |
| 2462 parsing_result->descriptor.mode = CONST_LEGACY; | 2464 parsing_result->descriptor.mode = CONST_LEGACY; |
| 2463 ++use_counts_[v8::Isolate::kLegacyConst]; | 2465 ++use_counts_[v8::Isolate::kLegacyConst]; |
| 2464 } else { | 2466 } else { |
| 2465 DCHECK(is_strict(language_mode()) || allow_harmony_sloppy()); | 2467 DCHECK(is_strict(language_mode()) || allow_harmony_sloppy()); |
| 2466 DCHECK(var_context != kStatement); | 2468 DCHECK(var_context != kStatement); |
| 2467 parsing_result->descriptor.mode = CONST; | 2469 parsing_result->descriptor.mode = CONST; |
| 2468 } | 2470 } |
| 2469 parsing_result->descriptor.is_const = true; | |
| 2470 parsing_result->descriptor.needs_init = true; | 2471 parsing_result->descriptor.needs_init = true; |
| 2471 } else if (peek() == Token::LET && allow_let()) { | 2472 } else if (peek() == Token::LET && allow_let()) { |
| 2472 Consume(Token::LET); | 2473 Consume(Token::LET); |
| 2473 DCHECK(var_context != kStatement); | 2474 DCHECK(var_context != kStatement); |
| 2474 parsing_result->descriptor.mode = LET; | 2475 parsing_result->descriptor.mode = LET; |
| 2475 parsing_result->descriptor.needs_init = true; | 2476 parsing_result->descriptor.needs_init = true; |
| 2476 } else { | 2477 } else { |
| 2477 UNREACHABLE(); // by current callers | 2478 UNREACHABLE(); // by current callers |
| 2478 } | 2479 } |
| 2479 | 2480 |
| 2480 parsing_result->descriptor.declaration_scope = | |
| 2481 DeclarationScope(parsing_result->descriptor.mode); | |
| 2482 parsing_result->descriptor.scope = scope_; | 2481 parsing_result->descriptor.scope = scope_; |
| 2483 parsing_result->descriptor.hoist_scope = nullptr; | 2482 parsing_result->descriptor.hoist_scope = nullptr; |
| 2484 | 2483 |
| 2485 | 2484 |
| 2486 bool first_declaration = true; | 2485 bool first_declaration = true; |
| 2487 int bindings_start = peek_position(); | 2486 int bindings_start = peek_position(); |
| 2488 bool is_for_iteration_variable; | 2487 bool is_for_iteration_variable; |
| 2489 do { | 2488 do { |
| 2490 if (fni_ != NULL) fni_->Enter(); | 2489 if (fni_ != NULL) fni_->Enter(); |
| 2491 | 2490 |
| (...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3154 | 3153 |
| 3155 block_scope->set_start_position(scanner()->location().beg_pos); | 3154 block_scope->set_start_position(scanner()->location().beg_pos); |
| 3156 { | 3155 { |
| 3157 BlockState block_state(&scope_, block_scope); | 3156 BlockState block_state(&scope_, block_scope); |
| 3158 Target target(&this->target_stack_, catch_block); | 3157 Target target(&this->target_stack_, catch_block); |
| 3159 | 3158 |
| 3160 if (!is_simple) { | 3159 if (!is_simple) { |
| 3161 DeclarationDescriptor descriptor; | 3160 DeclarationDescriptor descriptor; |
| 3162 descriptor.declaration_kind = DeclarationDescriptor::NORMAL; | 3161 descriptor.declaration_kind = DeclarationDescriptor::NORMAL; |
| 3163 descriptor.parser = this; | 3162 descriptor.parser = this; |
| 3164 descriptor.declaration_scope = scope_; | |
| 3165 descriptor.scope = scope_; | 3163 descriptor.scope = scope_; |
| 3166 descriptor.hoist_scope = nullptr; | 3164 descriptor.hoist_scope = nullptr; |
| 3167 descriptor.mode = LET; | 3165 descriptor.mode = LET; |
| 3168 descriptor.is_const = false; | |
| 3169 descriptor.needs_init = true; | 3166 descriptor.needs_init = true; |
| 3170 descriptor.declaration_pos = pattern->position(); | 3167 descriptor.declaration_pos = pattern->position(); |
| 3171 descriptor.initialization_pos = pattern->position(); | 3168 descriptor.initialization_pos = pattern->position(); |
| 3172 | 3169 |
| 3173 DeclarationParsingResult::Declaration decl( | 3170 DeclarationParsingResult::Declaration decl( |
| 3174 pattern, pattern->position(), | 3171 pattern, pattern->position(), |
| 3175 factory()->NewVariableProxy(catch_variable)); | 3172 factory()->NewVariableProxy(catch_variable)); |
| 3176 | 3173 |
| 3177 PatternRewriter::DeclareAndInitializeVariables( | 3174 PatternRewriter::DeclareAndInitializeVariables( |
| 3178 catch_block, &descriptor, &decl, nullptr, CHECK_OK); | 3175 catch_block, &descriptor, &decl, nullptr, CHECK_OK); |
| (...skipping 1350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4529 const ParserFormalParameters& parameters, bool* ok) { | 4526 const ParserFormalParameters& parameters, bool* ok) { |
| 4530 DCHECK(!parameters.is_simple); | 4527 DCHECK(!parameters.is_simple); |
| 4531 DCHECK(scope_->is_function_scope()); | 4528 DCHECK(scope_->is_function_scope()); |
| 4532 Block* init_block = | 4529 Block* init_block = |
| 4533 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); | 4530 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); |
| 4534 for (int i = 0; i < parameters.params.length(); ++i) { | 4531 for (int i = 0; i < parameters.params.length(); ++i) { |
| 4535 auto parameter = parameters.params[i]; | 4532 auto parameter = parameters.params[i]; |
| 4536 DeclarationDescriptor descriptor; | 4533 DeclarationDescriptor descriptor; |
| 4537 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; | 4534 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; |
| 4538 descriptor.parser = this; | 4535 descriptor.parser = this; |
| 4539 descriptor.declaration_scope = scope_; | |
| 4540 descriptor.scope = scope_; | 4536 descriptor.scope = scope_; |
| 4541 descriptor.hoist_scope = nullptr; | 4537 descriptor.hoist_scope = nullptr; |
| 4542 descriptor.mode = LET; | 4538 descriptor.mode = LET; |
| 4543 descriptor.is_const = false; | |
| 4544 descriptor.needs_init = true; | 4539 descriptor.needs_init = true; |
| 4545 descriptor.declaration_pos = parameter.pattern->position(); | 4540 descriptor.declaration_pos = parameter.pattern->position(); |
| 4546 // The position that will be used by the AssignmentExpression | 4541 // The position that will be used by the AssignmentExpression |
| 4547 // which copies from the temp parameter to the pattern. | 4542 // which copies from the temp parameter to the pattern. |
| 4548 // | 4543 // |
| 4549 // TODO(adamk): Should this be RelocInfo::kNoPosition, since | 4544 // TODO(adamk): Should this be RelocInfo::kNoPosition, since |
| 4550 // it's just copying from a temp var to the real param var? | 4545 // it's just copying from a temp var to the real param var? |
| 4551 descriptor.initialization_pos = parameter.pattern->position(); | 4546 descriptor.initialization_pos = parameter.pattern->position(); |
| 4552 // The initializer position which will end up in, | 4547 // The initializer position which will end up in, |
| 4553 // Variable::initializer_position(), used for hole check elimination. | 4548 // Variable::initializer_position(), used for hole check elimination. |
| (...skipping 1901 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6455 } | 6450 } |
| 6456 | 6451 |
| 6457 | 6452 |
| 6458 void Parser::RaiseLanguageMode(LanguageMode mode) { | 6453 void Parser::RaiseLanguageMode(LanguageMode mode) { |
| 6459 SetLanguageMode(scope_, | 6454 SetLanguageMode(scope_, |
| 6460 static_cast<LanguageMode>(scope_->language_mode() | mode)); | 6455 static_cast<LanguageMode>(scope_->language_mode() | mode)); |
| 6461 } | 6456 } |
| 6462 | 6457 |
| 6463 } // namespace internal | 6458 } // namespace internal |
| 6464 } // namespace v8 | 6459 } // namespace v8 |
| OLD | NEW |