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

Side by Side Diff: src/preparser.h

Issue 1135243004: [es6] Support super.property in eval and arrow functions (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: git rebase Created 5 years, 7 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 #ifndef V8_PREPARSER_H 5 #ifndef V8_PREPARSER_H
6 #define V8_PREPARSER_H 6 #define V8_PREPARSER_H
7 7
8 #include "src/v8.h" 8 #include "src/v8.h"
9 9
10 #include "src/bailout-reason.h" 10 #include "src/bailout-reason.h"
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 } 332 }
333 ~ParsingModeScope() { 333 ~ParsingModeScope() {
334 parser_->mode_ = old_mode_; 334 parser_->mode_ = old_mode_;
335 } 335 }
336 336
337 private: 337 private:
338 ParserBase* parser_; 338 ParserBase* parser_;
339 Mode old_mode_; 339 Mode old_mode_;
340 }; 340 };
341 341
342 Scope* NewScope(Scope* parent, ScopeType scope_type, 342 Scope* NewScope(Scope* parent, ScopeType scope_type) {
343 FunctionKind kind = kNormalFunction) { 343 // Must always pass the function kind for FUNCTION_SCOPE and ARROW_SCOPE.
344 DCHECK(scope_type != FUNCTION_SCOPE && scope_type != ARROW_SCOPE);
adamk 2015/05/26 19:32:33 You can split this into two DCHECKs to make errors
arv (Not doing code reviews) 2015/05/26 19:39:58 Done.
345 return NewScope(parent, scope_type, kNormalFunction);
346 }
347
348 Scope* NewScope(Scope* parent, ScopeType scope_type, FunctionKind kind) {
344 DCHECK(ast_value_factory()); 349 DCHECK(ast_value_factory());
345 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules()); 350 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules());
346 DCHECK((scope_type == FUNCTION_SCOPE && IsValidFunctionKind(kind)) || 351 DCHECK(scope_type != ARROW_SCOPE || IsArrowFunction(kind));
347 kind == kNormalFunction);
348 Scope* result = new (zone()) 352 Scope* result = new (zone())
349 Scope(zone(), parent, scope_type, ast_value_factory(), kind); 353 Scope(zone(), parent, scope_type, ast_value_factory(), kind);
350 result->Initialize(); 354 result->Initialize();
351 return result; 355 return result;
352 } 356 }
353 357
354 Scanner* scanner() const { return scanner_; } 358 Scanner* scanner() const { return scanner_; }
355 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } 359 AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
356 int position() { return scanner_->location().beg_pos; } 360 int position() { return scanner_->location().beg_pos; }
357 int peek_position() { return scanner_->peek_location().beg_pos; } 361 int peek_position() { return scanner_->peek_location().beg_pos; }
(...skipping 1346 matching lines...) Expand 10 before | Expand all | Expand 10 after
1704 return PreParserIdentifier::Default(); 1708 return PreParserIdentifier::Default();
1705 } 1709 }
1706 1710
1707 static PreParserExpression ThisExpression(Scope* scope, 1711 static PreParserExpression ThisExpression(Scope* scope,
1708 PreParserFactory* factory, 1712 PreParserFactory* factory,
1709 int pos) { 1713 int pos) {
1710 return PreParserExpression::This(); 1714 return PreParserExpression::This();
1711 } 1715 }
1712 1716
1713 static PreParserExpression SuperReference(Scope* scope, 1717 static PreParserExpression SuperReference(Scope* scope,
1714 PreParserFactory* factory) { 1718 PreParserFactory* factory,
1719 int pos) {
1715 return PreParserExpression::Default(); 1720 return PreParserExpression::Default();
1716 } 1721 }
1717 1722
1718 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope, 1723 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope,
1719 int pos, int end_pos) { 1724 int pos, int end_pos) {
1720 return PreParserExpression::Default(); 1725 return PreParserExpression::Default();
1721 } 1726 }
1722 1727
1723 static PreParserExpression ExpressionFromLiteral( 1728 static PreParserExpression ExpressionFromLiteral(
1724 Token::Value token, int pos, Scanner* scanner, 1729 Token::Value token, int pos, Scanner* scanner,
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after
2386 if (!classifier->is_valid_binding_pattern()) { 2391 if (!classifier->is_valid_binding_pattern()) {
2387 ArrowFormalParametersUnexpectedToken(classifier); 2392 ArrowFormalParametersUnexpectedToken(classifier);
2388 } 2393 }
2389 BindingPatternUnexpectedToken(classifier); 2394 BindingPatternUnexpectedToken(classifier);
2390 Consume(Token::LPAREN); 2395 Consume(Token::LPAREN);
2391 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { 2396 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) {
2392 // As a primary expression, the only thing that can follow "()" is "=>". 2397 // As a primary expression, the only thing that can follow "()" is "=>".
2393 classifier->RecordBindingPatternError(scanner()->location(), 2398 classifier->RecordBindingPatternError(scanner()->location(),
2394 MessageTemplate::kUnexpectedToken, 2399 MessageTemplate::kUnexpectedToken,
2395 Token::String(Token::RPAREN)); 2400 Token::String(Token::RPAREN));
2396 Scope* scope = this->NewScope(scope_, ARROW_SCOPE); 2401 Scope* scope =
2402 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction);
2397 scope->set_start_position(beg_pos); 2403 scope->set_start_position(beg_pos);
2398 ExpressionClassifier args_classifier; 2404 ExpressionClassifier args_classifier;
2399 bool has_rest = false; 2405 bool has_rest = false;
2400 result = this->ParseArrowFunctionLiteral(scope, has_rest, 2406 result = this->ParseArrowFunctionLiteral(scope, has_rest,
2401 args_classifier, CHECK_OK); 2407 args_classifier, CHECK_OK);
2402 } else { 2408 } else {
2403 // Heuristically try to detect immediately called functions before 2409 // Heuristically try to detect immediately called functions before
2404 // seeing the call parentheses. 2410 // seeing the call parentheses.
2405 parenthesized_function_ = (peek() == Token::FUNCTION); 2411 parenthesized_function_ = (peek() == Token::FUNCTION);
2406 result = this->ParseExpression(true, classifier, CHECK_OK); 2412 result = this->ParseExpression(true, classifier, CHECK_OK);
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after
2913 accept_IN, &arrow_formals_classifier, CHECK_OK); 2919 accept_IN, &arrow_formals_classifier, CHECK_OK);
2914 classifier->Accumulate(arrow_formals_classifier); 2920 classifier->Accumulate(arrow_formals_classifier);
2915 2921
2916 if (allow_harmony_arrow_functions() && peek() == Token::ARROW) { 2922 if (allow_harmony_arrow_functions() && peek() == Token::ARROW) {
2917 checkpoint.Restore(); 2923 checkpoint.Restore();
2918 BindingPatternUnexpectedToken(classifier); 2924 BindingPatternUnexpectedToken(classifier);
2919 ValidateArrowFormalParameters(&arrow_formals_classifier, expression, 2925 ValidateArrowFormalParameters(&arrow_formals_classifier, expression,
2920 CHECK_OK); 2926 CHECK_OK);
2921 Scanner::Location loc(lhs_location.beg_pos, scanner()->location().end_pos); 2927 Scanner::Location loc(lhs_location.beg_pos, scanner()->location().end_pos);
2922 bool has_rest = false; 2928 bool has_rest = false;
2923 Scope* scope = this->NewScope(scope_, ARROW_SCOPE); 2929 Scope* scope =
2930 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction);
2924 scope->set_start_position(lhs_location.beg_pos); 2931 scope->set_start_position(lhs_location.beg_pos);
2925 Scanner::Location duplicate_loc = Scanner::Location::invalid(); 2932 Scanner::Location duplicate_loc = Scanner::Location::invalid();
2926 this->ParseArrowFunctionFormalParameters(scope, expression, loc, &has_rest, 2933 this->ParseArrowFunctionFormalParameters(scope, expression, loc, &has_rest,
2927 &duplicate_loc, CHECK_OK); 2934 &duplicate_loc, CHECK_OK);
2928 if (duplicate_loc.IsValid()) { 2935 if (duplicate_loc.IsValid()) {
2929 arrow_formals_classifier.RecordDuplicateFormalParameterError( 2936 arrow_formals_classifier.RecordDuplicateFormalParameterError(
2930 duplicate_loc); 2937 duplicate_loc);
2931 } 2938 }
2932 expression = this->ParseArrowFunctionLiteral( 2939 expression = this->ParseArrowFunctionLiteral(
2933 scope, has_rest, arrow_formals_classifier, CHECK_OK); 2940 scope, has_rest, arrow_formals_classifier, CHECK_OK);
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
3500 typename ParserBase<Traits>::ExpressionT 3507 typename ParserBase<Traits>::ExpressionT
3501 ParserBase<Traits>::ParseStrongSuperCallExpression( 3508 ParserBase<Traits>::ParseStrongSuperCallExpression(
3502 ExpressionClassifier* classifier, bool* ok) { 3509 ExpressionClassifier* classifier, bool* ok) {
3503 // SuperCallExpression :: (strong mode) 3510 // SuperCallExpression :: (strong mode)
3504 // 'super' '(' ExpressionList ')' 3511 // 'super' '(' ExpressionList ')'
3505 BindingPatternUnexpectedToken(classifier); 3512 BindingPatternUnexpectedToken(classifier);
3506 3513
3507 Consume(Token::SUPER); 3514 Consume(Token::SUPER);
3508 int pos = position(); 3515 int pos = position();
3509 Scanner::Location super_loc = scanner()->location(); 3516 Scanner::Location super_loc = scanner()->location();
3510 ExpressionT expr = this->SuperReference(scope_, factory()); 3517 ExpressionT expr = this->SuperReference(scope_, factory(), pos);
3511 3518
3512 if (peek() != Token::LPAREN) { 3519 if (peek() != Token::LPAREN) {
3513 ReportMessage(MessageTemplate::kStrongConstructorSuper); 3520 ReportMessage(MessageTemplate::kStrongConstructorSuper);
3514 *ok = false; 3521 *ok = false;
3515 return this->EmptyExpression(); 3522 return this->EmptyExpression();
3516 } 3523 }
3517 3524
3518 Scanner::Location spread_pos; 3525 Scanner::Location spread_pos;
3519 typename Traits::Type::ExpressionList args = 3526 typename Traits::Type::ExpressionList args =
3520 ParseArguments(&spread_pos, classifier, CHECK_OK); 3527 ParseArguments(&spread_pos, classifier, CHECK_OK);
(...skipping 28 matching lines...) Expand all
3549 return factory()->NewCall(expr, args, pos); 3556 return factory()->NewCall(expr, args, pos);
3550 } 3557 }
3551 } 3558 }
3552 3559
3553 3560
3554 template <class Traits> 3561 template <class Traits>
3555 typename ParserBase<Traits>::ExpressionT 3562 typename ParserBase<Traits>::ExpressionT
3556 ParserBase<Traits>::ParseSuperExpression(bool is_new, 3563 ParserBase<Traits>::ParseSuperExpression(bool is_new,
3557 ExpressionClassifier* classifier, 3564 ExpressionClassifier* classifier,
3558 bool* ok) { 3565 bool* ok) {
3566 int pos = position();
3559 Expect(Token::SUPER, CHECK_OK); 3567 Expect(Token::SUPER, CHECK_OK);
3560 3568
3561 // TODO(wingo): Does this actually work with lazily compiled arrows? 3569 Scope* scope = scope_->DeclarationScope();
3562 FunctionState* function_state = function_state_; 3570
3563 while (IsArrowFunction(function_state->kind())) { 3571 while (scope->is_eval_scope() || scope->is_arrow_scope()) {
3564 function_state = function_state->outer(); 3572 scope = scope->outer_scope();
3573 DCHECK_NOT_NULL(scope);
3574 scope = scope->DeclarationScope();
3565 } 3575 }
3566 // TODO(arv): Handle eval scopes similarly.
3567 3576
3568 FunctionKind kind = function_state->kind(); 3577 FunctionKind kind = scope->function_kind();
3569 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || 3578 if (IsConciseMethod(kind) || IsAccessorFunction(kind) ||
3570 i::IsConstructor(kind)) { 3579 i::IsConstructor(kind)) {
3571 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { 3580 if (peek() == Token::PERIOD || peek() == Token::LBRACK) {
3572 scope_->RecordSuperPropertyUsage(); 3581 scope->RecordSuperPropertyUsage();
3573 return this->SuperReference(scope_, factory()); 3582 return this->SuperReference(scope_, factory(), pos);
3574 } 3583 }
3575 // new super() is never allowed. 3584 // new super() is never allowed.
3576 // super() is only allowed in derived constructor 3585 // super() is only allowed in derived constructor
3577 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { 3586 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) {
3578 if (is_strong(language_mode())) { 3587 if (is_strong(language_mode())) {
3579 // Super calls in strong mode are parsed separately. 3588 // Super calls in strong mode are parsed separately.
3580 ReportMessageAt(scanner()->location(), 3589 ReportMessageAt(scanner()->location(),
3581 MessageTemplate::kStrongConstructorSuper); 3590 MessageTemplate::kStrongConstructorSuper);
3582 *ok = false; 3591 *ok = false;
3583 return this->EmptyExpression(); 3592 return this->EmptyExpression();
3584 } 3593 }
3585 function_state->set_super_location(scanner()->location()); 3594 // TODO(rossberg): This might not be the correct FunctionState for the
3586 return this->SuperReference(scope_, factory()); 3595 // method here.
3596 function_state_->set_super_location(scanner()->location());
3597 return this->SuperReference(scope_, factory(), pos);
3587 } 3598 }
3588 } 3599 }
3589 3600
3590 ReportMessageAt(scanner()->location(), MessageTemplate::kUnexpectedSuper); 3601 ReportMessageAt(scanner()->location(), MessageTemplate::kUnexpectedSuper);
3591 *ok = false; 3602 *ok = false;
3592 return this->EmptyExpression(); 3603 return this->EmptyExpression();
3593 } 3604 }
3594 3605
3595 3606
3596 template <class Traits> 3607 template <class Traits>
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
4027 *ok = false; 4038 *ok = false;
4028 return; 4039 return;
4029 } 4040 }
4030 has_seen_constructor_ = true; 4041 has_seen_constructor_ = true;
4031 return; 4042 return;
4032 } 4043 }
4033 } 4044 }
4034 } } // v8::internal 4045 } } // v8::internal
4035 4046
4036 #endif // V8_PREPARSER_H 4047 #endif // V8_PREPARSER_H
OLDNEW
« no previous file with comments | « src/parser.cc ('k') | src/preparser.cc » ('j') | test/mjsunit/harmony/object-literals-super.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698