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

Side by Side Diff: src/parser.cc

Issue 1218803006: Add a flag for legacy const semantics (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add message tests Created 5 years, 5 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
« no previous file with comments | « src/flag-definitions.h ('k') | src/preparser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/ast-literal-reindexer.h" 9 #include "src/ast-literal-reindexer.h"
10 #include "src/bailout-reason.h" 10 #include "src/bailout-reason.h"
(...skipping 901 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 set_allow_harmony_sloppy(FLAG_harmony_sloppy); 912 set_allow_harmony_sloppy(FLAG_harmony_sloppy);
913 set_allow_harmony_unicode(FLAG_harmony_unicode); 913 set_allow_harmony_unicode(FLAG_harmony_unicode);
914 set_allow_harmony_computed_property_names( 914 set_allow_harmony_computed_property_names(
915 FLAG_harmony_computed_property_names); 915 FLAG_harmony_computed_property_names);
916 set_allow_harmony_rest_params(FLAG_harmony_rest_parameters); 916 set_allow_harmony_rest_params(FLAG_harmony_rest_parameters);
917 set_allow_harmony_spreadcalls(FLAG_harmony_spreadcalls); 917 set_allow_harmony_spreadcalls(FLAG_harmony_spreadcalls);
918 set_allow_harmony_destructuring(FLAG_harmony_destructuring); 918 set_allow_harmony_destructuring(FLAG_harmony_destructuring);
919 set_allow_harmony_spread_arrays(FLAG_harmony_spread_arrays); 919 set_allow_harmony_spread_arrays(FLAG_harmony_spread_arrays);
920 set_allow_harmony_new_target(FLAG_harmony_new_target); 920 set_allow_harmony_new_target(FLAG_harmony_new_target);
921 set_allow_strong_mode(FLAG_strong_mode); 921 set_allow_strong_mode(FLAG_strong_mode);
922 set_allow_legacy_const(FLAG_legacy_const);
922 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 923 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
923 ++feature) { 924 ++feature) {
924 use_counts_[feature] = 0; 925 use_counts_[feature] = 0;
925 } 926 }
926 if (info->ast_value_factory() == NULL) { 927 if (info->ast_value_factory() == NULL) {
927 // info takes ownership of AstValueFactory. 928 // info takes ownership of AstValueFactory.
928 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); 929 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed()));
929 info->set_ast_value_factory_owned(); 930 info->set_ast_value_factory_owned();
930 ast_value_factory_ = info->ast_value_factory(); 931 ast_value_factory_ = info->ast_value_factory();
931 } 932 }
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
1372 switch (peek()) { 1373 switch (peek()) {
1373 case Token::FUNCTION: 1374 case Token::FUNCTION:
1374 return ParseFunctionDeclaration(NULL, ok); 1375 return ParseFunctionDeclaration(NULL, ok);
1375 case Token::CLASS: 1376 case Token::CLASS:
1376 if (scope_->class_declaration_group_start() < 0) { 1377 if (scope_->class_declaration_group_start() < 0) {
1377 scope_->set_class_declaration_group_start( 1378 scope_->set_class_declaration_group_start(
1378 scanner()->peek_location().beg_pos); 1379 scanner()->peek_location().beg_pos);
1379 } 1380 }
1380 return ParseClassDeclaration(NULL, ok); 1381 return ParseClassDeclaration(NULL, ok);
1381 case Token::CONST: 1382 case Token::CONST:
1383 if (allow_const()) {
1384 return ParseVariableStatement(kStatementListItem, NULL, ok);
1385 }
1386 break;
1382 case Token::VAR: 1387 case Token::VAR:
1383 return ParseVariableStatement(kStatementListItem, NULL, ok); 1388 return ParseVariableStatement(kStatementListItem, NULL, ok);
1384 case Token::LET: 1389 case Token::LET:
1385 if (is_strict(language_mode())) { 1390 if (is_strict(language_mode())) {
1386 return ParseVariableStatement(kStatementListItem, NULL, ok); 1391 return ParseVariableStatement(kStatementListItem, NULL, ok);
1387 } 1392 }
1388 // Fall through. 1393 break;
1389 default: 1394 default:
1390 return ParseStatement(NULL, ok); 1395 break;
1391 } 1396 }
1397 return ParseStatement(NULL, ok);
1392 } 1398 }
1393 1399
1394 1400
1395 Statement* Parser::ParseModuleItem(bool* ok) { 1401 Statement* Parser::ParseModuleItem(bool* ok) {
1396 // (Ecma 262 6th Edition, 15.2): 1402 // (Ecma 262 6th Edition, 15.2):
1397 // ModuleItem : 1403 // ModuleItem :
1398 // ImportDeclaration 1404 // ImportDeclaration
1399 // ExportDeclaration 1405 // ExportDeclaration
1400 // StatementListItem 1406 // StatementListItem
1401 1407
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
1930 case Token::DEBUGGER: 1936 case Token::DEBUGGER:
1931 return ParseDebuggerStatement(ok); 1937 return ParseDebuggerStatement(ok);
1932 1938
1933 case Token::VAR: 1939 case Token::VAR:
1934 return ParseVariableStatement(kStatement, NULL, ok); 1940 return ParseVariableStatement(kStatement, NULL, ok);
1935 1941
1936 case Token::CONST: 1942 case Token::CONST:
1937 // In ES6 CONST is not allowed as a Statement, only as a 1943 // In ES6 CONST is not allowed as a Statement, only as a
1938 // LexicalDeclaration, however we continue to allow it in sloppy mode for 1944 // LexicalDeclaration, however we continue to allow it in sloppy mode for
1939 // backwards compatibility. 1945 // backwards compatibility.
1940 if (is_sloppy(language_mode())) { 1946 if (is_sloppy(language_mode()) && allow_legacy_const()) {
1941 return ParseVariableStatement(kStatement, NULL, ok); 1947 return ParseVariableStatement(kStatement, NULL, ok);
1942 } 1948 }
1943 1949
1944 // Fall through. 1950 // Fall through.
1945 default: 1951 default:
1946 return ParseExpressionOrLabelledStatement(labels, ok); 1952 return ParseExpressionOrLabelledStatement(labels, ok);
1947 } 1953 }
1948 } 1954 }
1949 1955
1950 Statement* Parser::ParseStatementAsUnlabelled( 1956 Statement* Parser::ParseStatementAsUnlabelled(
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
2419 parsing_result->descriptor.is_const = false; 2425 parsing_result->descriptor.is_const = false;
2420 parsing_result->descriptor.init_op = Token::INIT_VAR; 2426 parsing_result->descriptor.init_op = Token::INIT_VAR;
2421 if (peek() == Token::VAR) { 2427 if (peek() == Token::VAR) {
2422 if (is_strong(language_mode())) { 2428 if (is_strong(language_mode())) {
2423 Scanner::Location location = scanner()->peek_location(); 2429 Scanner::Location location = scanner()->peek_location();
2424 ReportMessageAt(location, MessageTemplate::kStrongVar); 2430 ReportMessageAt(location, MessageTemplate::kStrongVar);
2425 *ok = false; 2431 *ok = false;
2426 return; 2432 return;
2427 } 2433 }
2428 Consume(Token::VAR); 2434 Consume(Token::VAR);
2429 } else if (peek() == Token::CONST) { 2435 } else if (peek() == Token::CONST && allow_const()) {
2430 Consume(Token::CONST); 2436 Consume(Token::CONST);
2431 if (is_sloppy(language_mode())) { 2437 if (is_sloppy(language_mode()) && allow_legacy_const()) {
2432 parsing_result->descriptor.mode = CONST_LEGACY; 2438 parsing_result->descriptor.mode = CONST_LEGACY;
2433 parsing_result->descriptor.init_op = Token::INIT_CONST_LEGACY; 2439 parsing_result->descriptor.init_op = Token::INIT_CONST_LEGACY;
2434 ++use_counts_[v8::Isolate::kLegacyConst]; 2440 ++use_counts_[v8::Isolate::kLegacyConst];
2435 } else { 2441 } else {
2442 DCHECK(is_strict(language_mode()));
2436 DCHECK(var_context != kStatement); 2443 DCHECK(var_context != kStatement);
2437 parsing_result->descriptor.mode = CONST; 2444 parsing_result->descriptor.mode = CONST;
2438 parsing_result->descriptor.init_op = Token::INIT_CONST; 2445 parsing_result->descriptor.init_op = Token::INIT_CONST;
2439 } 2446 }
2440 parsing_result->descriptor.is_const = true; 2447 parsing_result->descriptor.is_const = true;
2441 parsing_result->descriptor.needs_init = true; 2448 parsing_result->descriptor.needs_init = true;
2442 } else if (peek() == Token::LET && is_strict(language_mode())) { 2449 } else if (peek() == Token::LET && is_strict(language_mode())) {
2443 Consume(Token::LET); 2450 Consume(Token::LET);
2444 DCHECK(var_context != kStatement); 2451 DCHECK(var_context != kStatement);
2445 parsing_result->descriptor.mode = LET; 2452 parsing_result->descriptor.mode = LET;
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after
3483 // Create an in-between scope for let-bound iteration variables. 3490 // Create an in-between scope for let-bound iteration variables.
3484 Scope* saved_scope = scope_; 3491 Scope* saved_scope = scope_;
3485 Scope* for_scope = NewScope(scope_, BLOCK_SCOPE); 3492 Scope* for_scope = NewScope(scope_, BLOCK_SCOPE);
3486 scope_ = for_scope; 3493 scope_ = for_scope;
3487 Expect(Token::FOR, CHECK_OK); 3494 Expect(Token::FOR, CHECK_OK);
3488 Expect(Token::LPAREN, CHECK_OK); 3495 Expect(Token::LPAREN, CHECK_OK);
3489 for_scope->set_start_position(scanner()->location().beg_pos); 3496 for_scope->set_start_position(scanner()->location().beg_pos);
3490 bool is_let_identifier_expression = false; 3497 bool is_let_identifier_expression = false;
3491 DeclarationParsingResult parsing_result; 3498 DeclarationParsingResult parsing_result;
3492 if (peek() != Token::SEMICOLON) { 3499 if (peek() != Token::SEMICOLON) {
3493 if (peek() == Token::VAR || peek() == Token::CONST || 3500 if (peek() == Token::VAR || (peek() == Token::CONST && allow_const()) ||
3494 (peek() == Token::LET && is_strict(language_mode()))) { 3501 (peek() == Token::LET && is_strict(language_mode()))) {
3495 ParseVariableDeclarations(kForStatement, &parsing_result, CHECK_OK); 3502 ParseVariableDeclarations(kForStatement, &parsing_result, CHECK_OK);
3496 is_const = parsing_result.descriptor.mode == CONST; 3503 is_const = parsing_result.descriptor.mode == CONST;
3497 3504
3498 int num_decl = parsing_result.declarations.length(); 3505 int num_decl = parsing_result.declarations.length();
3499 bool accept_IN = num_decl >= 1; 3506 bool accept_IN = num_decl >= 1;
3500 bool accept_OF = true; 3507 bool accept_OF = true;
3501 ForEachStatement::VisitMode mode; 3508 ForEachStatement::VisitMode mode;
3502 int each_beg_pos = scanner()->location().beg_pos; 3509 int each_beg_pos = scanner()->location().beg_pos;
3503 int each_end_pos = scanner()->location().end_pos; 3510 int each_end_pos = scanner()->location().end_pos;
(...skipping 20 matching lines...) Expand all
3524 MessageTemplate::kForInLoopInitializer); 3531 MessageTemplate::kForInLoopInitializer);
3525 } 3532 }
3526 *ok = false; 3533 *ok = false;
3527 return nullptr; 3534 return nullptr;
3528 } 3535 }
3529 3536
3530 DCHECK(parsing_result.declarations.length() == 1); 3537 DCHECK(parsing_result.declarations.length() == 1);
3531 Block* init_block = nullptr; 3538 Block* init_block = nullptr;
3532 3539
3533 // special case for legacy for (var/const x =.... in) 3540 // special case for legacy for (var/const x =.... in)
3534 if (is_sloppy(language_mode()) && 3541 if (!IsLexicalVariableMode(parsing_result.descriptor.mode) &&
3535 !IsLexicalVariableMode(parsing_result.descriptor.mode) &&
3536 parsing_result.declarations[0].initializer != nullptr) { 3542 parsing_result.declarations[0].initializer != nullptr) {
3537 VariableProxy* single_var = scope_->NewUnresolved( 3543 VariableProxy* single_var = scope_->NewUnresolved(
3538 factory(), parsing_result.SingleName(), Variable::NORMAL, 3544 factory(), parsing_result.SingleName(), Variable::NORMAL,
3539 each_beg_pos, each_end_pos); 3545 each_beg_pos, each_end_pos);
3540 init_block = factory()->NewBlock( 3546 init_block = factory()->NewBlock(
3541 nullptr, 2, true, parsing_result.descriptor.declaration_pos); 3547 nullptr, 2, true, parsing_result.descriptor.declaration_pos);
3542 init_block->AddStatement( 3548 init_block->AddStatement(
3543 factory()->NewExpressionStatement( 3549 factory()->NewExpressionStatement(
3544 factory()->NewAssignment( 3550 factory()->NewAssignment(
3545 Token::ASSIGN, single_var, 3551 Token::ASSIGN, single_var,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
3608 factory()->NewVariableProxy(temp, each_beg_pos, each_end_pos); 3614 factory()->NewVariableProxy(temp, each_beg_pos, each_end_pos);
3609 InitializeForEachStatement(loop, temp_proxy, enumerable, body_block); 3615 InitializeForEachStatement(loop, temp_proxy, enumerable, body_block);
3610 scope_ = for_scope; 3616 scope_ = for_scope;
3611 body_scope->set_end_position(scanner()->location().end_pos); 3617 body_scope->set_end_position(scanner()->location().end_pos);
3612 body_scope = body_scope->FinalizeBlockScope(); 3618 body_scope = body_scope->FinalizeBlockScope();
3613 if (body_scope != nullptr) { 3619 if (body_scope != nullptr) {
3614 body_block->set_scope(body_scope); 3620 body_block->set_scope(body_scope);
3615 } 3621 }
3616 3622
3617 // Create a TDZ for any lexically-bound names. 3623 // Create a TDZ for any lexically-bound names.
3618 if (is_strict(language_mode()) && 3624 if (IsLexicalVariableMode(parsing_result.descriptor.mode)) {
3619 IsLexicalVariableMode(parsing_result.descriptor.mode)) {
3620 DCHECK_NULL(init_block); 3625 DCHECK_NULL(init_block);
3621 3626
3622 init_block = 3627 init_block =
3623 factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition); 3628 factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition);
3624 3629
3625 for (int i = 0; i < lexical_bindings.length(); ++i) { 3630 for (int i = 0; i < lexical_bindings.length(); ++i) {
3626 // TODO(adamk): This needs to be some sort of special 3631 // TODO(adamk): This needs to be some sort of special
3627 // INTERNAL variable that's invisible to the debugger 3632 // INTERNAL variable that's invisible to the debugger
3628 // but visible to everything else. 3633 // but visible to everything else.
3629 VariableProxy* tdz_proxy = NewUnresolved(lexical_bindings[i], LET); 3634 VariableProxy* tdz_proxy = NewUnresolved(lexical_bindings[i], LET);
(...skipping 2307 matching lines...) Expand 10 before | Expand all | Expand 10 after
5937 Expression* Parser::SpreadCallNew(Expression* function, 5942 Expression* Parser::SpreadCallNew(Expression* function,
5938 ZoneList<v8::internal::Expression*>* args, 5943 ZoneList<v8::internal::Expression*>* args,
5939 int pos) { 5944 int pos) {
5940 args->InsertAt(0, function, zone()); 5945 args->InsertAt(0, function, zone());
5941 5946
5942 return factory()->NewCallRuntime( 5947 return factory()->NewCallRuntime(
5943 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5948 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5944 } 5949 }
5945 } // namespace internal 5950 } // namespace internal
5946 } // namespace v8 5951 } // namespace v8
OLDNEW
« no previous file with comments | « src/flag-definitions.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698