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 #ifndef V8_PARSING_PARSER_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_H |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 1329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1340 impl()->IsEvalOrArguments(impl()->AsIdentifier(expression))) { | 1340 impl()->IsEvalOrArguments(impl()->AsIdentifier(expression))) { |
1341 return false; | 1341 return false; |
1342 } | 1342 } |
1343 return true; | 1343 return true; |
1344 } | 1344 } |
1345 | 1345 |
1346 bool IsValidPattern(ExpressionT expression) { | 1346 bool IsValidPattern(ExpressionT expression) { |
1347 return expression->IsObjectLiteral() || expression->IsArrayLiteral(); | 1347 return expression->IsObjectLiteral() || expression->IsArrayLiteral(); |
1348 } | 1348 } |
1349 | 1349 |
| 1350 // Due to hoisting, the value of a 'var'-declared variable may actually change |
| 1351 // even if the code contains only the "initial" assignment, namely when that |
| 1352 // assignment occurs inside a loop. For example: |
| 1353 // |
| 1354 // let i = 10; |
| 1355 // do { var x = i } while (i--): |
| 1356 // |
| 1357 // As a simple and very conservative approximation of this, we explicitly mark |
| 1358 // as maybe-assigned any non-lexical variable whose initializing "declaration" |
| 1359 // does not syntactically occur in the function scope. (In the example above, |
| 1360 // it occurs in a block scope.) |
| 1361 // |
| 1362 // Note that non-lexical variables include temporaries, which may also get |
| 1363 // assigned inside a loop due to the various rewritings that the parser |
| 1364 // performs. |
| 1365 // |
| 1366 static void MarkLoopVariableAsAssigned(Scope* scope, Variable* var); |
| 1367 |
1350 // Keep track of eval() calls since they disable all local variable | 1368 // Keep track of eval() calls since they disable all local variable |
1351 // optimizations. This checks if expression is an eval call, and if yes, | 1369 // optimizations. This checks if expression is an eval call, and if yes, |
1352 // forwards the information to scope. | 1370 // forwards the information to scope. |
1353 Call::PossiblyEval CheckPossibleEvalCall(ExpressionT expression, | 1371 Call::PossiblyEval CheckPossibleEvalCall(ExpressionT expression, |
1354 Scope* scope) { | 1372 Scope* scope) { |
1355 if (impl()->IsIdentifier(expression) && | 1373 if (impl()->IsIdentifier(expression) && |
1356 impl()->IsEval(impl()->AsIdentifier(expression))) { | 1374 impl()->IsEval(impl()->AsIdentifier(expression))) { |
1357 scope->RecordEvalCall(); | 1375 scope->RecordEvalCall(); |
1358 if (is_sloppy(scope->language_mode())) { | 1376 if (is_sloppy(scope->language_mode())) { |
1359 // For sloppy scopes we also have to record the call at function level, | 1377 // For sloppy scopes we also have to record the call at function level, |
(...skipping 4331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5691 block->statements()->Add(loop, zone()); | 5709 block->statements()->Add(loop, zone()); |
5692 block->set_scope(for_scope); | 5710 block->set_scope(for_scope); |
5693 loop->Initialize(init, cond, next, body); | 5711 loop->Initialize(init, cond, next, body); |
5694 return block; | 5712 return block; |
5695 } | 5713 } |
5696 | 5714 |
5697 loop->Initialize(init, cond, next, body); | 5715 loop->Initialize(init, cond, next, body); |
5698 return loop; | 5716 return loop; |
5699 } | 5717 } |
5700 | 5718 |
5701 #undef CHECK_OK | 5719 template <typename Impl> |
5702 #undef CHECK_OK_CUSTOM | 5720 void ParserBase<Impl>::MarkLoopVariableAsAssigned(Scope* scope, Variable* var) { |
| 5721 if (!IsLexicalVariableMode(var->mode()) && !scope->is_function_scope()) { |
| 5722 var->set_maybe_assigned(); |
| 5723 } |
| 5724 } |
5703 | 5725 |
5704 template <typename Impl> | 5726 template <typename Impl> |
5705 void ParserBase<Impl>::ObjectLiteralChecker::CheckDuplicateProto( | 5727 void ParserBase<Impl>::ObjectLiteralChecker::CheckDuplicateProto( |
5706 Token::Value property) { | 5728 Token::Value property) { |
5707 if (property == Token::SMI || property == Token::NUMBER) return; | 5729 if (property == Token::SMI || property == Token::NUMBER) return; |
5708 | 5730 |
5709 if (IsProto()) { | 5731 if (IsProto()) { |
5710 if (has_seen_proto_) { | 5732 if (has_seen_proto_) { |
5711 this->parser()->classifier()->RecordExpressionError( | 5733 this->parser()->classifier()->RecordExpressionError( |
5712 this->scanner()->location(), MessageTemplate::kDuplicateProto); | 5734 this->scanner()->location(), MessageTemplate::kDuplicateProto); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5744 if (has_seen_constructor_) { | 5766 if (has_seen_constructor_) { |
5745 this->parser()->ReportMessage(MessageTemplate::kDuplicateConstructor); | 5767 this->parser()->ReportMessage(MessageTemplate::kDuplicateConstructor); |
5746 *ok = false; | 5768 *ok = false; |
5747 return; | 5769 return; |
5748 } | 5770 } |
5749 has_seen_constructor_ = true; | 5771 has_seen_constructor_ = true; |
5750 return; | 5772 return; |
5751 } | 5773 } |
5752 } | 5774 } |
5753 | 5775 |
| 5776 #undef CHECK_OK |
| 5777 #undef CHECK_OK_CUSTOM |
5754 #undef CHECK_OK_VOID | 5778 #undef CHECK_OK_VOID |
5755 | 5779 |
5756 } // namespace internal | 5780 } // namespace internal |
5757 } // namespace v8 | 5781 } // namespace v8 |
5758 | 5782 |
5759 #endif // V8_PARSING_PARSER_BASE_H | 5783 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |