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/preparser.h

Issue 1348773004: [cleanup] refactor ParsePropertyDefinition for clarity (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 3 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 | « no previous file | no next file » | 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 #ifndef V8_PREPARSER_H 5 #ifndef V8_PREPARSER_H
6 #define V8_PREPARSER_H 6 #define V8_PREPARSER_H
7 7
8 #include "src/bailout-reason.h" 8 #include "src/bailout-reason.h"
9 #include "src/expression-classifier.h" 9 #include "src/expression-classifier.h"
10 #include "src/func-name-inferrer.h" 10 #include "src/func-name-inferrer.h"
(...skipping 2593 matching lines...) Expand 10 before | Expand all | Expand 10 after
2604 int next_beg_pos = scanner()->peek_location().beg_pos; 2604 int next_beg_pos = scanner()->peek_location().beg_pos;
2605 int next_end_pos = scanner()->peek_location().end_pos; 2605 int next_end_pos = scanner()->peek_location().end_pos;
2606 ExpressionT name_expression = ParsePropertyName( 2606 ExpressionT name_expression = ParsePropertyName(
2607 &name, &is_get, &is_set, &name_is_static, is_computed_name, classifier, 2607 &name, &is_get, &is_set, &name_is_static, is_computed_name, classifier,
2608 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); 2608 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2609 2609
2610 if (fni_ != nullptr && !*is_computed_name) { 2610 if (fni_ != nullptr && !*is_computed_name) {
2611 this->PushLiteralName(fni_, name); 2611 this->PushLiteralName(fni_, name);
2612 } 2612 }
2613 2613
2614 if (!in_class && !is_generator && peek() == Token::COLON) { 2614 if (!in_class && !is_generator) {
2615 // PropertyDefinition : PropertyName ':' AssignmentExpression 2615 if (peek() == Token::COLON) {
adamk 2015/09/16 19:03:14 Please add DCHECK(!is_static) above this line.
caitp (gmail) 2015/09/16 20:10:25 Done.
2616 if (!*is_computed_name) { 2616 // PropertyDefinition
2617 checker->CheckProperty(name_token, kValueProperty, is_static, 2617 // PropertyName : AssignmentExpression
adamk 2015/09/16 19:03:14 Please quote the ':' in this production (otherwise
caitp (gmail) 2015/09/16 20:10:25 Done.
2618 is_generator, 2618 if (!*is_computed_name) {
2619 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); 2619 checker->CheckProperty(name_token, kValueProperty, false, false,
2620 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2621 }
2622 Consume(Token::COLON);
2623 value = this->ParseAssignmentExpression(
2624 true, classifier, CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2625 return factory()->NewObjectLiteralProperty(name_expression, value, false,
2626 *is_computed_name);
2627 } else if (Token::IsIdentifier(name_token, language_mode(),
adamk 2015/09/16 19:03:13 Style nit: I think it would be easier to read this
caitp (gmail) 2015/09/16 20:10:25 Done.
2628 this->is_generator()) &&
2629 (peek() == Token::COMMA || peek() == Token::RBRACE ||
2630 peek() == Token::ASSIGN)) {
2631 // PropertyDefinition
2632 // IdentifierReference
2633 // CoverInitializedName
2634 //
2635 // CoverInitializedName
2636 // IdentifierReference : AssignmentExpression
adamk 2015/09/16 19:03:14 Do you mean "IdentifierReference Initializer" ?
caitp (gmail) 2015/09/16 20:10:25 Yes, not sure how this got so mixed up =) Done ---
adamk 2015/09/16 20:58:00 I'd go with the ?, that's what we use elsewhere in
caitp (gmail) 2015/09/16 21:01:09 Done.
2637 if (classifier->duplicate_finder() != nullptr &&
2638 scanner()->FindSymbol(classifier->duplicate_finder(), 1) != 0) {
2639 classifier->RecordDuplicateFormalParameterError(scanner()->location());
2640 }
2641
2642 ExpressionT lhs = this->ExpressionFromIdentifier(
2643 name, next_beg_pos, next_end_pos, scope_, factory());
2644
2645 if (peek() == Token::ASSIGN) {
2646 this->ExpressionUnexpectedToken(classifier);
2647 Consume(Token::ASSIGN);
2648 ExpressionClassifier rhs_classifier;
2649 ExpressionT rhs = this->ParseAssignmentExpression(
2650 true, &rhs_classifier, CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2651 classifier->Accumulate(rhs_classifier,
2652 ExpressionClassifier::ExpressionProductions);
2653 value = factory()->NewAssignment(Token::ASSIGN, lhs, rhs,
2654 RelocInfo::kNoPosition);
2655 } else {
2656 value = lhs;
2657 }
2658
2659 return factory()->NewObjectLiteralProperty(
2660 name_expression, value, ObjectLiteralProperty::COMPUTED, false,
2661 false);
2620 } 2662 }
2621 Consume(Token::COLON); 2663 }
2622 value = this->ParseAssignmentExpression(
2623 true, classifier, CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2624 2664
2625 } else if (is_generator || peek() == Token::LPAREN) { 2665
2626 // Concise Method 2666 if (is_generator || peek() == Token::LPAREN) {
2667 // MethodDefinition
2668 // PropertyName ( StrictFormalParameters ) { FunctionBody }
adamk 2015/09/16 19:03:14 More quoting, please. Not going to mark up the res
caitp (gmail) 2015/09/16 20:10:25 Done.
2669 // * PropertyName ( StrictFormalParameters ) { FunctionBody }
2627 if (!*is_computed_name) { 2670 if (!*is_computed_name) {
2628 checker->CheckProperty(name_token, kMethodProperty, is_static, 2671 checker->CheckProperty(name_token, kMethodProperty, is_static,
2629 is_generator, 2672 is_generator,
2630 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); 2673 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2631 } 2674 }
2632 2675
2633 FunctionKind kind = is_generator ? FunctionKind::kConciseGeneratorMethod 2676 FunctionKind kind = is_generator ? FunctionKind::kConciseGeneratorMethod
2634 : FunctionKind::kConciseMethod; 2677 : FunctionKind::kConciseMethod;
2635 2678
2636 if (in_class && !is_static && this->IsConstructor(name)) { 2679 if (in_class && !is_static && this->IsConstructor(name)) {
2637 *has_seen_constructor = true; 2680 *has_seen_constructor = true;
2638 kind = has_extends ? FunctionKind::kSubclassConstructor 2681 kind = has_extends ? FunctionKind::kSubclassConstructor
2639 : FunctionKind::kBaseConstructor; 2682 : FunctionKind::kBaseConstructor;
2640 } 2683 }
2641 2684
2642 if (!in_class) kind = WithObjectLiteralBit(kind); 2685 if (!in_class) kind = WithObjectLiteralBit(kind);
2643 2686
2644 value = this->ParseFunctionLiteral( 2687 value = this->ParseFunctionLiteral(
2645 name, scanner()->location(), kSkipFunctionNameCheck, kind, 2688 name, scanner()->location(), kSkipFunctionNameCheck, kind,
2646 RelocInfo::kNoPosition, FunctionLiteral::ANONYMOUS_EXPRESSION, 2689 RelocInfo::kNoPosition, FunctionLiteral::ANONYMOUS_EXPRESSION,
2647 FunctionLiteral::NORMAL_ARITY, language_mode(), 2690 FunctionLiteral::NORMAL_ARITY, language_mode(),
2648 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); 2691 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2649 2692
2650 return factory()->NewObjectLiteralProperty(name_expression, value, 2693 return factory()->NewObjectLiteralProperty(name_expression, value,
2651 ObjectLiteralProperty::COMPUTED, 2694 ObjectLiteralProperty::COMPUTED,
2652 is_static, *is_computed_name); 2695 is_static, *is_computed_name);
2653
2654 } else if (in_class && name_is_static && !is_static) { 2696 } else if (in_class && name_is_static && !is_static) {
adamk 2015/09/16 19:03:14 Nit again: I'd recommend making this not an 'else'
caitp (gmail) 2015/09/16 20:10:25 Done.
2655 // static MethodDefinition 2697 // ClassElement (static)
2698 // static MethodDefinition
2656 return ParsePropertyDefinition(checker, true, has_extends, true, 2699 return ParsePropertyDefinition(checker, true, has_extends, true,
2657 is_computed_name, nullptr, classifier, ok); 2700 is_computed_name, nullptr, classifier, ok);
2658 } else if ((is_get || is_set) && 2701 } else if (is_get || is_set) {
2659 (in_class || (peek() != Token::RBRACE && peek() != Token::COMMA && 2702 // MethodDefinition (Accessors)
2660 peek() != Token::ASSIGN))) { 2703 // get PropertyName ( ) { FunctionBody }
2661 // Accessor 2704 // set PropertyName ( PropertySetParameterList ) { FunctionBody }
2662 name = this->EmptyIdentifier(); 2705 name = this->EmptyIdentifier();
2663 bool dont_care = false; 2706 bool dont_care = false;
2664 name_token = peek(); 2707 name_token = peek();
2665 2708
2666 name_expression = ParsePropertyName( 2709 name_expression = ParsePropertyName(
2667 &name, &dont_care, &dont_care, &dont_care, is_computed_name, classifier, 2710 &name, &dont_care, &dont_care, &dont_care, is_computed_name, classifier,
2668 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); 2711 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2669 2712
2670 if (!*is_computed_name) { 2713 if (!*is_computed_name) {
2671 checker->CheckProperty(name_token, kAccessorProperty, is_static, 2714 checker->CheckProperty(name_token, kAccessorProperty, is_static,
(...skipping 14 matching lines...) Expand all
2686 // statically we can skip the extra runtime check. 2729 // statically we can skip the extra runtime check.
2687 if (!*is_computed_name) { 2730 if (!*is_computed_name) {
2688 name_expression = 2731 name_expression =
2689 factory()->NewStringLiteral(name, name_expression->position()); 2732 factory()->NewStringLiteral(name, name_expression->position());
2690 } 2733 }
2691 2734
2692 return factory()->NewObjectLiteralProperty( 2735 return factory()->NewObjectLiteralProperty(
2693 name_expression, value, 2736 name_expression, value,
2694 is_get ? ObjectLiteralProperty::GETTER : ObjectLiteralProperty::SETTER, 2737 is_get ? ObjectLiteralProperty::GETTER : ObjectLiteralProperty::SETTER,
2695 is_static, *is_computed_name); 2738 is_static, *is_computed_name);
2696
2697 } else if (!in_class && Token::IsIdentifier(name_token, language_mode(),
2698 this->is_generator())) {
2699 DCHECK(!*is_computed_name);
2700 DCHECK(!is_static);
2701
2702 if (classifier->duplicate_finder() != nullptr &&
2703 scanner()->FindSymbol(classifier->duplicate_finder(), 1) != 0) {
2704 classifier->RecordDuplicateFormalParameterError(scanner()->location());
2705 }
2706
2707 ExpressionT lhs = this->ExpressionFromIdentifier(
2708 name, next_beg_pos, next_end_pos, scope_, factory());
2709 if (peek() == Token::ASSIGN) {
2710 this->ExpressionUnexpectedToken(classifier);
2711 Consume(Token::ASSIGN);
2712 ExpressionClassifier rhs_classifier;
2713 ExpressionT rhs = this->ParseAssignmentExpression(
2714 true, &rhs_classifier, CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
2715 classifier->Accumulate(rhs_classifier,
2716 ExpressionClassifier::ExpressionProductions);
2717 value = factory()->NewAssignment(Token::ASSIGN, lhs, rhs,
2718 RelocInfo::kNoPosition);
2719 } else {
2720 value = lhs;
2721 }
2722 return factory()->NewObjectLiteralProperty(
2723 name_expression, value, ObjectLiteralProperty::COMPUTED, false, false);
2724
2725 } else { 2739 } else {
adamk 2015/09/16 19:03:13 You can move the contents of the else clause up to
caitp (gmail) 2015/09/16 20:10:25 Done.
2726 Token::Value next = Next(); 2740 Token::Value next = Next();
2727 ReportUnexpectedToken(next); 2741 ReportUnexpectedToken(next);
2728 *ok = false; 2742 *ok = false;
2729 return this->EmptyObjectLiteralProperty(); 2743 return this->EmptyObjectLiteralProperty();
2730 } 2744 }
2731 2745
2732 return factory()->NewObjectLiteralProperty(name_expression, value, is_static, 2746 UNREACHABLE();
adamk 2015/09/16 19:03:13 ...and remove this line.
caitp (gmail) 2015/09/16 20:10:25 Done.
2733 *is_computed_name);
2734 } 2747 }
2735 2748
2736 2749
2737 template <class Traits> 2750 template <class Traits>
2738 typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseObjectLiteral( 2751 typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseObjectLiteral(
2739 ExpressionClassifier* classifier, bool* ok) { 2752 ExpressionClassifier* classifier, bool* ok) {
2740 // ObjectLiteral :: 2753 // ObjectLiteral ::
2741 // '{' (PropertyDefinition (',' PropertyDefinition)* ','? )? '}' 2754 // '{' (PropertyDefinition (',' PropertyDefinition)* ','? )? '}'
2742 2755
2743 int pos = peek_position(); 2756 int pos = peek_position();
(...skipping 1415 matching lines...) Expand 10 before | Expand all | Expand 10 after
4159 *ok = false; 4172 *ok = false;
4160 return; 4173 return;
4161 } 4174 }
4162 has_seen_constructor_ = true; 4175 has_seen_constructor_ = true;
4163 return; 4176 return;
4164 } 4177 }
4165 } 4178 }
4166 } } // v8::internal 4179 } } // v8::internal
4167 4180
4168 #endif // V8_PREPARSER_H 4181 #endif // V8_PREPARSER_H
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698