Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: src/preparser.cc

Issue 1084983002: [strong] Implement static restrictions on switch statement (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 <cmath> 5 #include <cmath>
6 6
7 #include "src/allocation.h" 7 #include "src/allocation.h"
8 #include "src/base/logging.h" 8 #include "src/base/logging.h"
9 #include "src/conversions-inl.h" 9 #include "src/conversions-inl.h"
10 #include "src/conversions.h" 10 #include "src/conversions.h"
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 if (!*ok) return Statement::Default(); \ 228 if (!*ok) return Statement::Default(); \
229 ((void)0 229 ((void)0
230 #define DUMMY ) // to make indentation work 230 #define DUMMY ) // to make indentation work
231 #undef DUMMY 231 #undef DUMMY
232 232
233 233
234 PreParser::Statement PreParser::ParseStatement(bool* ok) { 234 PreParser::Statement PreParser::ParseStatement(bool* ok) {
235 // Statement :: 235 // Statement ::
236 // EmptyStatement 236 // EmptyStatement
237 // ... 237 // ...
238
239 if (peek() == Token::SEMICOLON) {
rossberg 2015/04/14 20:19:49 Did you add a test covering this?
conradw 2015/04/15 11:40:03 Done, was indirectly covered before but now direct
240 Next();
241 return Statement::Default();
242 }
238 return ParseSubStatement(ok); 243 return ParseSubStatement(ok);
239 } 244 }
240 245
241 246
242 PreParser::Statement PreParser::ParseSubStatement(bool* ok) { 247 PreParser::Statement PreParser::ParseSubStatement(bool* ok) {
243 // Statement :: 248 // Statement ::
244 // Block 249 // Block
245 // VariableStatement 250 // VariableStatement
246 // EmptyStatement 251 // EmptyStatement
247 // ExpressionStatement 252 // ExpressionStatement
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 393
389 394
390 PreParser::Statement PreParser::ParseBlock(bool* ok) { 395 PreParser::Statement PreParser::ParseBlock(bool* ok) {
391 // Block :: 396 // Block ::
392 // '{' Statement* '}' 397 // '{' Statement* '}'
393 398
394 // Note that a Block does not introduce a new execution scope! 399 // Note that a Block does not introduce a new execution scope!
395 // (ECMA-262, 3rd, 12.2) 400 // (ECMA-262, 3rd, 12.2)
396 // 401 //
397 Expect(Token::LBRACE, CHECK_OK); 402 Expect(Token::LBRACE, CHECK_OK);
403 Statement final = Statement::Default();
398 while (peek() != Token::RBRACE) { 404 while (peek() != Token::RBRACE) {
399 if (is_strict(language_mode())) { 405 if (is_strict(language_mode())) {
400 ParseStatementListItem(CHECK_OK); 406 final = ParseStatementListItem(CHECK_OK);
401 } else { 407 } else {
402 ParseStatement(CHECK_OK); 408 final = ParseStatement(CHECK_OK);
403 } 409 }
404 } 410 }
405 Expect(Token::RBRACE, ok); 411 Expect(Token::RBRACE, ok);
406 return Statement::Default(); 412 return final;
407 } 413 }
408 414
409 415
410 PreParser::Statement PreParser::ParseVariableStatement( 416 PreParser::Statement PreParser::ParseVariableStatement(
411 VariableDeclarationContext var_context, 417 VariableDeclarationContext var_context,
412 bool* ok) { 418 bool* ok) {
413 // VariableStatement :: 419 // VariableStatement ::
414 // VariableDeclarations ';' 420 // VariableDeclarations ';'
415 421
416 Statement result = ParseVariableDeclarations(var_context, nullptr, nullptr, 422 Statement result = ParseVariableDeclarations(var_context, nullptr, nullptr,
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 // Even if the expression starts with an identifier, it is not necessarily an 544 // Even if the expression starts with an identifier, it is not necessarily an
539 // identifier. For example, "foo + bar" starts with an identifier but is not 545 // identifier. For example, "foo + bar" starts with an identifier but is not
540 // an identifier. 546 // an identifier.
541 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) { 547 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) {
542 // Expression is a single identifier, and not, e.g., a parenthesized 548 // Expression is a single identifier, and not, e.g., a parenthesized
543 // identifier. 549 // identifier.
544 DCHECK(!expr.AsIdentifier().IsFutureReserved()); 550 DCHECK(!expr.AsIdentifier().IsFutureReserved());
545 DCHECK(is_sloppy(language_mode()) || 551 DCHECK(is_sloppy(language_mode()) ||
546 !IsFutureStrictReserved(expr.AsIdentifier())); 552 !IsFutureStrictReserved(expr.AsIdentifier()));
547 Consume(Token::COLON); 553 Consume(Token::COLON);
548 return ParseStatement(ok); 554 Statement statement = ParseStatement(ok);
555 if (statement.IsStrongSwitchTerminatingStatement()) {
556 return Statement::Default();
557 }
558 return statement;
549 // Preparsing is disabled for extensions (because the extension details 559 // Preparsing is disabled for extensions (because the extension details
550 // aren't passed to lazily compiled functions), so we don't 560 // aren't passed to lazily compiled functions), so we don't
551 // accept "native function" in the preparser. 561 // accept "native function" in the preparser.
552 } 562 }
553 // Parsed expression statement. 563 // Parsed expression statement.
554 // Detect attempts at 'let' declarations in sloppy mode. 564 // Detect attempts at 'let' declarations in sloppy mode.
555 if (peek() == Token::IDENTIFIER && is_sloppy(language_mode()) && 565 if (peek() == Token::IDENTIFIER && is_sloppy(language_mode()) &&
556 expr.IsIdentifier() && expr.AsIdentifier().IsLet()) { 566 expr.IsIdentifier() && expr.AsIdentifier().IsLet()) {
557 ReportMessage("sloppy_lexical", NULL); 567 ReportMessage("sloppy_lexical", NULL);
558 *ok = false; 568 *ok = false;
(...skipping 28 matching lines...) Expand all
587 Expect(Token::CONTINUE, CHECK_OK); 597 Expect(Token::CONTINUE, CHECK_OK);
588 Token::Value tok = peek(); 598 Token::Value tok = peek();
589 if (!scanner()->HasAnyLineTerminatorBeforeNext() && 599 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
590 tok != Token::SEMICOLON && 600 tok != Token::SEMICOLON &&
591 tok != Token::RBRACE && 601 tok != Token::RBRACE &&
592 tok != Token::EOS) { 602 tok != Token::EOS) {
593 // ECMA allows "eval" or "arguments" as labels even in strict mode. 603 // ECMA allows "eval" or "arguments" as labels even in strict mode.
594 ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK); 604 ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
595 } 605 }
596 ExpectSemicolon(CHECK_OK); 606 ExpectSemicolon(CHECK_OK);
597 return Statement::Default(); 607 return Statement::Terminating();
598 } 608 }
599 609
600 610
601 PreParser::Statement PreParser::ParseBreakStatement(bool* ok) { 611 PreParser::Statement PreParser::ParseBreakStatement(bool* ok) {
602 // BreakStatement :: 612 // BreakStatement ::
603 // 'break' [no line terminator] Identifier? ';' 613 // 'break' [no line terminator] Identifier? ';'
604 614
605 Expect(Token::BREAK, CHECK_OK); 615 Expect(Token::BREAK, CHECK_OK);
606 Token::Value tok = peek(); 616 Token::Value tok = peek();
607 if (!scanner()->HasAnyLineTerminatorBeforeNext() && 617 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
608 tok != Token::SEMICOLON && 618 tok != Token::SEMICOLON &&
609 tok != Token::RBRACE && 619 tok != Token::RBRACE &&
610 tok != Token::EOS) { 620 tok != Token::EOS) {
611 // ECMA allows "eval" or "arguments" as labels even in strict mode. 621 // ECMA allows "eval" or "arguments" as labels even in strict mode.
612 ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK); 622 ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
613 } 623 }
614 ExpectSemicolon(CHECK_OK); 624 ExpectSemicolon(CHECK_OK);
615 return Statement::Default(); 625 return Statement::Terminating();
616 } 626 }
617 627
618 628
619 PreParser::Statement PreParser::ParseReturnStatement(bool* ok) { 629 PreParser::Statement PreParser::ParseReturnStatement(bool* ok) {
620 // ReturnStatement :: 630 // ReturnStatement ::
621 // 'return' [no line terminator] Expression? ';' 631 // 'return' [no line terminator] Expression? ';'
622 632
623 // Consume the return token. It is necessary to do before 633 // Consume the return token. It is necessary to do before
624 // reporting any errors on it, because of the way errors are 634 // reporting any errors on it, because of the way errors are
625 // reported (underlining). 635 // reported (underlining).
(...skipping 14 matching lines...) Expand all
640 i::IsConstructor(function_state_->kind())) { 650 i::IsConstructor(function_state_->kind())) {
641 int pos = peek_position(); 651 int pos = peek_position();
642 ReportMessageAt(Scanner::Location(pos, pos + 1), 652 ReportMessageAt(Scanner::Location(pos, pos + 1),
643 "strong_constructor_return_value"); 653 "strong_constructor_return_value");
644 *ok = false; 654 *ok = false;
645 return Statement::Default(); 655 return Statement::Default();
646 } 656 }
647 ParseExpression(true, CHECK_OK); 657 ParseExpression(true, CHECK_OK);
648 } 658 }
649 ExpectSemicolon(CHECK_OK); 659 ExpectSemicolon(CHECK_OK);
650 return Statement::Default(); 660 return Statement::Terminating();
651 } 661 }
652 662
653 663
654 PreParser::Statement PreParser::ParseWithStatement(bool* ok) { 664 PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
655 // WithStatement :: 665 // WithStatement ::
656 // 'with' '(' Expression ')' Statement 666 // 'with' '(' Expression ')' Statement
657 Expect(Token::WITH, CHECK_OK); 667 Expect(Token::WITH, CHECK_OK);
658 if (is_strict(language_mode())) { 668 if (is_strict(language_mode())) {
659 ReportMessageAt(scanner()->location(), "strict_mode_with"); 669 ReportMessageAt(scanner()->location(), "strict_mode_with");
660 *ok = false; 670 *ok = false;
(...skipping 23 matching lines...) Expand all
684 Token::Value token = peek(); 694 Token::Value token = peek();
685 while (token != Token::RBRACE) { 695 while (token != Token::RBRACE) {
686 if (token == Token::CASE) { 696 if (token == Token::CASE) {
687 Expect(Token::CASE, CHECK_OK); 697 Expect(Token::CASE, CHECK_OK);
688 ParseExpression(true, CHECK_OK); 698 ParseExpression(true, CHECK_OK);
689 } else { 699 } else {
690 Expect(Token::DEFAULT, CHECK_OK); 700 Expect(Token::DEFAULT, CHECK_OK);
691 } 701 }
692 Expect(Token::COLON, CHECK_OK); 702 Expect(Token::COLON, CHECK_OK);
693 token = peek(); 703 token = peek();
704 Statement statement = Statement::Terminating();
694 while (token != Token::CASE && 705 while (token != Token::CASE &&
695 token != Token::DEFAULT && 706 token != Token::DEFAULT &&
696 token != Token::RBRACE) { 707 token != Token::RBRACE) {
697 ParseStatementListItem(CHECK_OK); 708 statement = ParseStatementListItem(CHECK_OK);
698 token = peek(); 709 token = peek();
699 } 710 }
711 if (is_strong(language_mode()) &&
712 !statement.IsStrongSwitchTerminatingStatement()) {
713 ReportMessageAt(scanner()->location(), "strong_unterminated_switch");
714 *ok = false;
715 return Statement::Default();
716 }
700 } 717 }
701 Expect(Token::RBRACE, ok); 718 Expect(Token::RBRACE, ok);
702 return Statement::Default(); 719 return Statement::Default();
703 } 720 }
704 721
705 722
706 PreParser::Statement PreParser::ParseDoWhileStatement(bool* ok) { 723 PreParser::Statement PreParser::ParseDoWhileStatement(bool* ok) {
707 // DoStatement :: 724 // DoStatement ::
708 // 'do' Statement 'while' '(' Expression ')' ';' 725 // 'do' Statement 'while' '(' Expression ')' ';'
709 726
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 // 'throw' [no line terminator] Expression ';' 837 // 'throw' [no line terminator] Expression ';'
821 838
822 Expect(Token::THROW, CHECK_OK); 839 Expect(Token::THROW, CHECK_OK);
823 if (scanner()->HasAnyLineTerminatorBeforeNext()) { 840 if (scanner()->HasAnyLineTerminatorBeforeNext()) {
824 ReportMessageAt(scanner()->location(), "newline_after_throw"); 841 ReportMessageAt(scanner()->location(), "newline_after_throw");
825 *ok = false; 842 *ok = false;
826 return Statement::Default(); 843 return Statement::Default();
827 } 844 }
828 ParseExpression(true, CHECK_OK); 845 ParseExpression(true, CHECK_OK);
829 ExpectSemicolon(ok); 846 ExpectSemicolon(ok);
830 return Statement::Default(); 847 return Statement::Terminating();
831 } 848 }
832 849
833 850
834 PreParser::Statement PreParser::ParseTryStatement(bool* ok) { 851 PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
835 // TryStatement :: 852 // TryStatement ::
836 // 'try' Block Catch 853 // 'try' Block Catch
837 // 'try' Block Finally 854 // 'try' Block Finally
838 // 'try' Block Catch Finally 855 // 'try' Block Catch Finally
839 // 856 //
840 // Catch :: 857 // Catch ::
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 1062
1046 DCHECK(!spread_pos.IsValid()); 1063 DCHECK(!spread_pos.IsValid());
1047 1064
1048 return Expression::Default(); 1065 return Expression::Default();
1049 } 1066 }
1050 1067
1051 #undef CHECK_OK 1068 #undef CHECK_OK
1052 1069
1053 1070
1054 } } // v8::internal 1071 } } // v8::internal
OLDNEW
« src/preparser.h ('K') | « src/preparser.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698