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

Side by Side Diff: src/parsing/parser.cc

Issue 1678303002: [es7] implement exponentiation operator proposal (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 9 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 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-expression-rewriter.h" 9 #include "src/ast/ast-expression-rewriter.h"
10 #include "src/ast/ast-expression-visitor.h" 10 #include "src/ast/ast-expression-visitor.h"
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 uint32_t value = DoubleToUint32(x_val) >> shift; 432 uint32_t value = DoubleToUint32(x_val) >> shift;
433 *x = factory->NewNumberLiteral(value, pos, has_dot); 433 *x = factory->NewNumberLiteral(value, pos, has_dot);
434 return true; 434 return true;
435 } 435 }
436 case Token::SAR: { 436 case Token::SAR: {
437 uint32_t shift = DoubleToInt32(y_val) & 0x1f; 437 uint32_t shift = DoubleToInt32(y_val) & 0x1f;
438 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); 438 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
439 *x = factory->NewNumberLiteral(value, pos, has_dot); 439 *x = factory->NewNumberLiteral(value, pos, has_dot);
440 return true; 440 return true;
441 } 441 }
442 case Token::EXP: {
443 double value = std::pow(x_val, y_val);
444 int int_value = static_cast<int>(value);
445 *x = factory->NewNumberLiteral(
446 int_value == value && value != -0.0 ? int_value : value, pos,
447 has_dot);
448 return true;
449 }
442 default: 450 default:
443 break; 451 break;
444 } 452 }
445 } 453 }
446 return false; 454 return false;
447 } 455 }
448 456
449 457
450 Expression* ParserTraits::BuildUnaryExpression(Expression* expression, 458 Expression* ParserTraits::BuildUnaryExpression(Expression* expression,
451 Token::Value op, int pos, 459 Token::Value op, int pos,
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 set_allow_natives(FLAG_allow_natives_syntax || info->is_native()); 786 set_allow_natives(FLAG_allow_natives_syntax || info->is_native());
779 set_allow_harmony_sloppy(FLAG_harmony_sloppy); 787 set_allow_harmony_sloppy(FLAG_harmony_sloppy);
780 set_allow_harmony_sloppy_function(FLAG_harmony_sloppy_function); 788 set_allow_harmony_sloppy_function(FLAG_harmony_sloppy_function);
781 set_allow_harmony_sloppy_let(FLAG_harmony_sloppy_let); 789 set_allow_harmony_sloppy_let(FLAG_harmony_sloppy_let);
782 set_allow_legacy_const(FLAG_legacy_const); 790 set_allow_legacy_const(FLAG_legacy_const);
783 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions); 791 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions);
784 set_allow_harmony_function_name(FLAG_harmony_function_name); 792 set_allow_harmony_function_name(FLAG_harmony_function_name);
785 set_allow_harmony_function_sent(FLAG_harmony_function_sent); 793 set_allow_harmony_function_sent(FLAG_harmony_function_sent);
786 set_allow_harmony_restrictive_declarations( 794 set_allow_harmony_restrictive_declarations(
787 FLAG_harmony_restrictive_declarations); 795 FLAG_harmony_restrictive_declarations);
796 set_allow_harmony_exponentiation_operator(
797 FLAG_harmony_exponentiation_operator);
788 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 798 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
789 ++feature) { 799 ++feature) {
790 use_counts_[feature] = 0; 800 use_counts_[feature] = 0;
791 } 801 }
792 if (info->ast_value_factory() == NULL) { 802 if (info->ast_value_factory() == NULL) {
793 // info takes ownership of AstValueFactory. 803 // info takes ownership of AstValueFactory.
794 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); 804 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed()));
795 info->set_ast_value_factory_owned(); 805 info->set_ast_value_factory_owned();
796 ast_value_factory_ = info->ast_value_factory(); 806 ast_value_factory_ = info->ast_value_factory();
797 } 807 }
(...skipping 3875 matching lines...) Expand 10 before | Expand all | Expand 10 after
4673 reusable_preparser_ = new PreParser(zone(), &scanner_, ast_value_factory(), 4683 reusable_preparser_ = new PreParser(zone(), &scanner_, ast_value_factory(),
4674 NULL, stack_limit_); 4684 NULL, stack_limit_);
4675 reusable_preparser_->set_allow_lazy(true); 4685 reusable_preparser_->set_allow_lazy(true);
4676 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name()); 4686 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name());
4677 SET_ALLOW(natives); 4687 SET_ALLOW(natives);
4678 SET_ALLOW(harmony_sloppy); 4688 SET_ALLOW(harmony_sloppy);
4679 SET_ALLOW(harmony_sloppy_let); 4689 SET_ALLOW(harmony_sloppy_let);
4680 SET_ALLOW(harmony_do_expressions); 4690 SET_ALLOW(harmony_do_expressions);
4681 SET_ALLOW(harmony_function_name); 4691 SET_ALLOW(harmony_function_name);
4682 SET_ALLOW(harmony_function_sent); 4692 SET_ALLOW(harmony_function_sent);
4693 SET_ALLOW(harmony_exponentiation_operator);
4683 #undef SET_ALLOW 4694 #undef SET_ALLOW
4684 } 4695 }
4685 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( 4696 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
4686 language_mode(), function_state_->kind(), scope_->has_simple_parameters(), 4697 language_mode(), function_state_->kind(), scope_->has_simple_parameters(),
4687 logger, bookmark); 4698 logger, bookmark);
4688 if (pre_parse_timer_ != NULL) { 4699 if (pre_parse_timer_ != NULL) {
4689 pre_parse_timer_->Stop(); 4700 pre_parse_timer_->Stop();
4690 } 4701 }
4691 return result; 4702 return result;
4692 } 4703 }
(...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after
5363 void Parser::RaiseLanguageMode(LanguageMode mode) { 5374 void Parser::RaiseLanguageMode(LanguageMode mode) {
5364 LanguageMode old = scope_->language_mode(); 5375 LanguageMode old = scope_->language_mode();
5365 SetLanguageMode(scope_, old > mode ? old : mode); 5376 SetLanguageMode(scope_, old > mode ? old : mode);
5366 } 5377 }
5367 5378
5368 5379
5369 void ParserTraits::RewriteDestructuringAssignments() { 5380 void ParserTraits::RewriteDestructuringAssignments() {
5370 parser_->RewriteDestructuringAssignments(); 5381 parser_->RewriteDestructuringAssignments();
5371 } 5382 }
5372 5383
5384 Expression* ParserTraits::RewriteExponentiation(Expression* left,
5385 Expression* right, int pos) {
5386 return parser_->RewriteExponentiation(left, right, pos);
5387 }
5388
5389 Expression* ParserTraits::RewriteAssignExponentiation(Expression* left,
5390 Expression* right,
5391 int pos) {
5392 return parser_->RewriteAssignExponentiation(left, right, pos);
5393 }
5373 5394
5374 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier, 5395 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier,
5375 bool* ok) { 5396 bool* ok) {
5376 parser_->RewriteNonPattern(classifier, ok); 5397 parser_->RewriteNonPattern(classifier, ok);
5377 } 5398 }
5378 5399
5379 5400
5380 Zone* ParserTraits::zone() const { 5401 Zone* ParserTraits::zone() const {
5381 return parser_->function_state_->scope()->zone(); 5402 return parser_->function_state_->scope()->zone();
5382 } 5403 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
5452 RewritableExpression* to_rewrite = 5473 RewritableExpression* to_rewrite =
5453 pair.assignment->AsRewritableExpression(); 5474 pair.assignment->AsRewritableExpression();
5454 DCHECK_NOT_NULL(to_rewrite); 5475 DCHECK_NOT_NULL(to_rewrite);
5455 if (!to_rewrite->is_rewritten()) { 5476 if (!to_rewrite->is_rewritten()) {
5456 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite, 5477 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite,
5457 pair.scope); 5478 pair.scope);
5458 } 5479 }
5459 } 5480 }
5460 } 5481 }
5461 5482
5483 Expression* Parser::RewriteExponentiation(Expression* left, Expression* right,
5484 int pos) {
5485 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone());
5486 args->Add(left, zone());
5487 args->Add(right, zone());
5488 return factory()->NewCallRuntime(Context::MATH_POW_METHOD_INDEX, args, pos);
5489 }
5490
5491 Expression* Parser::RewriteAssignExponentiation(Expression* left,
5492 Expression* right, int pos) {
5493 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone());
5494 if (left->IsVariableProxy()) {
5495 VariableProxy* lhs = left->AsVariableProxy();
5496
5497 Expression* result;
5498 DCHECK_NOT_NULL(lhs->raw_name());
5499 result =
5500 this->ExpressionFromIdentifier(lhs->raw_name(), lhs->position(),
5501 lhs->end_position(), scope_, factory());
5502 args->Add(left, zone());
5503 args->Add(right, zone());
5504 Expression* call =
5505 factory()->NewCallRuntime(Context::MATH_POW_METHOD_INDEX, args, pos);
5506 return factory()->NewAssignment(Token::ASSIGN, result, call, pos);
5507 } else if (left->IsProperty()) {
5508 Property* prop = left->AsProperty();
5509 auto temp_obj = scope_->NewTemporary(ast_value_factory()->empty_string());
5510 auto temp_key = scope_->NewTemporary(ast_value_factory()->empty_string());
5511 Expression* assign_obj = factory()->NewAssignment(
5512 Token::ASSIGN, factory()->NewVariableProxy(temp_obj), prop->obj(),
5513 RelocInfo::kNoPosition);
5514 Expression* assign_key = factory()->NewAssignment(
5515 Token::ASSIGN, factory()->NewVariableProxy(temp_key), prop->key(),
5516 RelocInfo::kNoPosition);
5517 args->Add(factory()->NewProperty(factory()->NewVariableProxy(temp_obj),
5518 factory()->NewVariableProxy(temp_key),
5519 left->position()),
5520 zone());
5521 args->Add(right, zone());
5522 Expression* call =
5523 factory()->NewCallRuntime(Context::MATH_POW_METHOD_INDEX, args, pos);
5524 Expression* target = factory()->NewProperty(
5525 factory()->NewVariableProxy(temp_obj),
5526 factory()->NewVariableProxy(temp_key), RelocInfo::kNoPosition);
5527 Expression* assign =
5528 factory()->NewAssignment(Token::ASSIGN, target, call, pos);
5529 return factory()->NewBinaryOperation(
5530 Token::COMMA, assign_obj,
5531 factory()->NewBinaryOperation(Token::COMMA, assign_key, assign, pos),
5532 pos);
5533 }
5534 UNREACHABLE();
5535 return nullptr;
5536 }
5462 5537
5463 Expression* Parser::RewriteSpreads(ArrayLiteral* lit) { 5538 Expression* Parser::RewriteSpreads(ArrayLiteral* lit) {
5464 // Array literals containing spreads are rewritten using do expressions, e.g. 5539 // Array literals containing spreads are rewritten using do expressions, e.g.
5465 // [1, 2, 3, ...x, 4, ...y, 5] 5540 // [1, 2, 3, ...x, 4, ...y, 5]
5466 // is roughly rewritten as: 5541 // is roughly rewritten as:
5467 // do { 5542 // do {
5468 // $R = [1, 2, 3]; 5543 // $R = [1, 2, 3];
5469 // for ($i of x) %AppendElement($R, $i); 5544 // for ($i of x) %AppendElement($R, $i);
5470 // %AppendElement($R, 4); 5545 // %AppendElement($R, 4);
5471 // for ($j of y) %AppendElement($R, $j); 5546 // for ($j of y) %AppendElement($R, $j);
(...skipping 1311 matching lines...) Expand 10 before | Expand all | Expand 10 after
6783 try_block, target); 6858 try_block, target);
6784 final_loop = target; 6859 final_loop = target;
6785 } 6860 }
6786 6861
6787 return final_loop; 6862 return final_loop;
6788 } 6863 }
6789 6864
6790 6865
6791 } // namespace internal 6866 } // namespace internal
6792 } // namespace v8 6867 } // namespace v8
OLDNEW
« no previous file with comments | « src/parsing/parser.h ('k') | src/parsing/parser-base.h » ('j') | src/parsing/parser-base.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698