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

Side by Side Diff: src/preparser.h

Issue 1078093002: Factor formal argument parsing into ParserBase (Closed) Base URL: https://chromium.googlesource.com/v8/v8@master
Patch Set: Rebase on top of "undefined" error detection, remove bits of utils.h patch that crept in 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
« no previous file with comments | « src/parser.cc ('k') | src/preparser.cc » ('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 #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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 // typedef GeneratorVariable; 44 // typedef GeneratorVariable;
45 // // Return types for traversing functions. 45 // // Return types for traversing functions.
46 // typedef Identifier; 46 // typedef Identifier;
47 // typedef Expression; 47 // typedef Expression;
48 // typedef FunctionLiteral; 48 // typedef FunctionLiteral;
49 // typedef ClassLiteral; 49 // typedef ClassLiteral;
50 // typedef ObjectLiteralProperty; 50 // typedef ObjectLiteralProperty;
51 // typedef Literal; 51 // typedef Literal;
52 // typedef ExpressionList; 52 // typedef ExpressionList;
53 // typedef PropertyList; 53 // typedef PropertyList;
54 // typedef FormalParameter;
55 // typedef FormalParameterList;
54 // // For constructing objects returned by the traversing functions. 56 // // For constructing objects returned by the traversing functions.
55 // typedef Factory; 57 // typedef Factory;
56 // }; 58 // };
57 // // ... 59 // // ...
58 // }; 60 // };
59 61
60 template <typename Traits> 62 template <typename Traits>
61 class ParserBase : public Traits { 63 class ParserBase : public Traits {
62 public: 64 public:
63 // Shorten type names defined by Traits. 65 // Shorten type names defined by Traits.
64 typedef typename Traits::Type::Expression ExpressionT; 66 typedef typename Traits::Type::Expression ExpressionT;
65 typedef typename Traits::Type::Identifier IdentifierT; 67 typedef typename Traits::Type::Identifier IdentifierT;
68 typedef typename Traits::Type::FormalParameter FormalParameterT;
69 typedef typename Traits::Type::FormalParameterList FormalParameterListT;
66 typedef typename Traits::Type::FunctionLiteral FunctionLiteralT; 70 typedef typename Traits::Type::FunctionLiteral FunctionLiteralT;
67 typedef typename Traits::Type::Literal LiteralT; 71 typedef typename Traits::Type::Literal LiteralT;
68 typedef typename Traits::Type::ObjectLiteralProperty ObjectLiteralPropertyT; 72 typedef typename Traits::Type::ObjectLiteralProperty ObjectLiteralPropertyT;
69 73
70 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit, 74 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
71 v8::Extension* extension, AstValueFactory* ast_value_factory, 75 v8::Extension* extension, AstValueFactory* ast_value_factory,
72 ParserRecorder* log, typename Traits::Type::Parser this_object) 76 ParserRecorder* log, typename Traits::Type::Parser this_object)
73 : Traits(this_object), 77 : Traits(this_object),
74 parenthesized_function_(false), 78 parenthesized_function_(false),
75 scope_(NULL), 79 scope_(NULL),
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 ExpressionT ParseMemberWithNewPrefixesExpression(bool* ok); 603 ExpressionT ParseMemberWithNewPrefixesExpression(bool* ok);
600 ExpressionT ParseMemberExpression(bool* ok); 604 ExpressionT ParseMemberExpression(bool* ok);
601 ExpressionT ParseMemberExpressionContinuation(ExpressionT expression, 605 ExpressionT ParseMemberExpressionContinuation(ExpressionT expression,
602 bool* ok); 606 bool* ok);
603 ExpressionT ParseArrowFunctionLiteral(int start_pos, ExpressionT params_ast, 607 ExpressionT ParseArrowFunctionLiteral(int start_pos, ExpressionT params_ast,
604 bool* ok); 608 bool* ok);
605 ExpressionT ParseTemplateLiteral(ExpressionT tag, int start, bool* ok); 609 ExpressionT ParseTemplateLiteral(ExpressionT tag, int start, bool* ok);
606 void AddTemplateExpression(ExpressionT); 610 void AddTemplateExpression(ExpressionT);
607 ExpressionT ParseSuperExpression(bool is_new, bool* ok); 611 ExpressionT ParseSuperExpression(bool is_new, bool* ok);
608 612
613 FormalParameterT ParseFormalParameter(DuplicateFinder* duplicate_finder,
614 Scanner::Location* eval_args_error_loc,
615 Scanner::Location* undefined_error_loc,
616 Scanner::Location* dupe_error_loc,
617 Scanner::Location* reserved_error_loc,
618 bool* ok);
619 FormalParameterListT ParseFormalParameterList(
620 Scanner::Location* eval_args_error_loc,
621 Scanner::Location* undefined_error_loc, Scanner::Location* dupe_error_loc,
622 Scanner::Location* reserved_error_loc, bool* is_rest, bool* ok);
623 void CheckArityRestrictions(
624 int param_count, FunctionLiteral::ArityRestriction arity_restriction,
625 int formals_start_pos, int formals_end_pos, bool* ok);
626
609 // Checks if the expression is a valid reference expression (e.g., on the 627 // Checks if the expression is a valid reference expression (e.g., on the
610 // left-hand side of assignments). Although ruled out by ECMA as early errors, 628 // left-hand side of assignments). Although ruled out by ECMA as early errors,
611 // we allow calls for web compatibility and rewrite them to a runtime throw. 629 // we allow calls for web compatibility and rewrite them to a runtime throw.
612 ExpressionT CheckAndRewriteReferenceExpression( 630 ExpressionT CheckAndRewriteReferenceExpression(
613 ExpressionT expression, 631 ExpressionT expression,
614 Scanner::Location location, const char* message, bool* ok); 632 Scanner::Location location, const char* message, bool* ok);
615 633
616 // Used to validate property names in object literals and class literals 634 // Used to validate property names in object literals and class literals
617 enum PropertyKind { 635 enum PropertyKind {
618 kAccessorProperty, 636 kAccessorProperty,
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
1033 typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseStrongField; 1051 typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseStrongField;
1034 typedef BitField<ValidArrowParam, ParenthesizationField::kNext, 2> 1052 typedef BitField<ValidArrowParam, ParenthesizationField::kNext, 2>
1035 IsValidArrowParamListField; 1053 IsValidArrowParamListField;
1036 typedef BitField<PreParserIdentifier::Type, ParenthesizationField::kNext, 10> 1054 typedef BitField<PreParserIdentifier::Type, ParenthesizationField::kNext, 10>
1037 IdentifierTypeField; 1055 IdentifierTypeField;
1038 1056
1039 uint32_t code_; 1057 uint32_t code_;
1040 }; 1058 };
1041 1059
1042 1060
1043 // PreParserExpressionList doesn't actually store the expressions because 1061 // The pre-parser doesn't need to build lists of expressions, identifiers, or
1044 // PreParser doesn't need to. 1062 // the like.
1045 class PreParserExpressionList { 1063 template <typename T>
1064 class PreParserList {
1046 public: 1065 public:
1047 // These functions make list->Add(some_expression) work (and do nothing). 1066 // These functions make list->Add(some_expression) work (and do nothing).
1048 PreParserExpressionList() : length_(0) {} 1067 PreParserList() : length_(0) {}
1049 PreParserExpressionList* operator->() { return this; } 1068 PreParserList* operator->() { return this; }
1050 void Add(PreParserExpression, void*) { ++length_; } 1069 void Add(T, void*) { ++length_; }
1051 int length() const { return length_; } 1070 int length() const { return length_; }
1052 private: 1071 private:
1053 int length_; 1072 int length_;
1054 }; 1073 };
1055 1074
1056 1075
1076 typedef PreParserList<PreParserExpression> PreParserExpressionList;
1077 typedef PreParserList<PreParserIdentifier> PreParserFormalParameterList;
1078
1079
1057 class PreParserStatement { 1080 class PreParserStatement {
1058 public: 1081 public:
1059 static PreParserStatement Default() { 1082 static PreParserStatement Default() {
1060 return PreParserStatement(kUnknownStatement); 1083 return PreParserStatement(kUnknownStatement);
1061 } 1084 }
1062 1085
1063 static PreParserStatement FunctionDeclaration() { 1086 static PreParserStatement FunctionDeclaration() {
1064 return PreParserStatement(kFunctionDeclaration); 1087 return PreParserStatement(kFunctionDeclaration);
1065 } 1088 }
1066 1089
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 kUseStrictExpressionStatement, 1125 kUseStrictExpressionStatement,
1103 kUseStrongExpressionStatement, 1126 kUseStrongExpressionStatement,
1104 kFunctionDeclaration 1127 kFunctionDeclaration
1105 }; 1128 };
1106 1129
1107 explicit PreParserStatement(Type code) : code_(code) {} 1130 explicit PreParserStatement(Type code) : code_(code) {}
1108 Type code_; 1131 Type code_;
1109 }; 1132 };
1110 1133
1111 1134
1112 1135 typedef PreParserList<PreParserStatement> PreParserStatementList;
1113 // PreParserStatementList doesn't actually store the statements because
1114 // the PreParser does not need them.
1115 class PreParserStatementList {
1116 public:
1117 // These functions make list->Add(some_expression) work as no-ops.
1118 PreParserStatementList() {}
1119 PreParserStatementList* operator->() { return this; }
1120 void Add(PreParserStatement, void*) {}
1121 };
1122 1136
1123 1137
1124 class PreParserFactory { 1138 class PreParserFactory {
1125 public: 1139 public:
1126 explicit PreParserFactory(void* unused_value_factory) {} 1140 explicit PreParserFactory(void* unused_value_factory) {}
1127 PreParserExpression NewStringLiteral(PreParserIdentifier identifier, 1141 PreParserExpression NewStringLiteral(PreParserIdentifier identifier,
1128 int pos) { 1142 int pos) {
1129 return PreParserExpression::Default(); 1143 return PreParserExpression::Default();
1130 } 1144 }
1131 PreParserExpression NewNumberLiteral(double number, 1145 PreParserExpression NewNumberLiteral(double number,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1276 // Return types for traversing functions. 1290 // Return types for traversing functions.
1277 typedef PreParserIdentifier Identifier; 1291 typedef PreParserIdentifier Identifier;
1278 typedef PreParserExpression Expression; 1292 typedef PreParserExpression Expression;
1279 typedef PreParserExpression YieldExpression; 1293 typedef PreParserExpression YieldExpression;
1280 typedef PreParserExpression FunctionLiteral; 1294 typedef PreParserExpression FunctionLiteral;
1281 typedef PreParserExpression ClassLiteral; 1295 typedef PreParserExpression ClassLiteral;
1282 typedef PreParserExpression ObjectLiteralProperty; 1296 typedef PreParserExpression ObjectLiteralProperty;
1283 typedef PreParserExpression Literal; 1297 typedef PreParserExpression Literal;
1284 typedef PreParserExpressionList ExpressionList; 1298 typedef PreParserExpressionList ExpressionList;
1285 typedef PreParserExpressionList PropertyList; 1299 typedef PreParserExpressionList PropertyList;
1300 typedef PreParserIdentifier FormalParameter;
1301 typedef PreParserFormalParameterList FormalParameterList;
1286 typedef PreParserStatementList StatementList; 1302 typedef PreParserStatementList StatementList;
1287 1303
1288 // For constructing objects returned by the traversing functions. 1304 // For constructing objects returned by the traversing functions.
1289 typedef PreParserFactory Factory; 1305 typedef PreParserFactory Factory;
1290 }; 1306 };
1291 1307
1292 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {} 1308 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {}
1293 1309
1294 // Helper functions for recursive descent. 1310 // Helper functions for recursive descent.
1295 static bool IsEval(PreParserIdentifier identifier) { 1311 static bool IsEval(PreParserIdentifier identifier) {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1434 } 1450 }
1435 static PreParserExpression EmptyObjectLiteralProperty() { 1451 static PreParserExpression EmptyObjectLiteralProperty() {
1436 return PreParserExpression::Default(); 1452 return PreParserExpression::Default();
1437 } 1453 }
1438 static PreParserExpression EmptyFunctionLiteral() { 1454 static PreParserExpression EmptyFunctionLiteral() {
1439 return PreParserExpression::Default(); 1455 return PreParserExpression::Default();
1440 } 1456 }
1441 static PreParserExpressionList NullExpressionList() { 1457 static PreParserExpressionList NullExpressionList() {
1442 return PreParserExpressionList(); 1458 return PreParserExpressionList();
1443 } 1459 }
1460 static PreParserIdentifier EmptyFormalParameter() {
1461 return PreParserIdentifier::Default();
1462 }
1463 static PreParserFormalParameterList NullFormalParameterList() {
1464 return PreParserFormalParameterList();
1465 }
1444 1466
1445 // Odd-ball literal creators. 1467 // Odd-ball literal creators.
1446 static PreParserExpression GetLiteralTheHole(int position, 1468 static PreParserExpression GetLiteralTheHole(int position,
1447 PreParserFactory* factory) { 1469 PreParserFactory* factory) {
1448 return PreParserExpression::Default(); 1470 return PreParserExpression::Default();
1449 } 1471 }
1450 1472
1451 // Producing data during the recursive descent. 1473 // Producing data during the recursive descent.
1452 PreParserIdentifier GetSymbol(Scanner* scanner); 1474 PreParserIdentifier GetSymbol(Scanner* scanner);
1453 PreParserIdentifier GetNumberAsSymbol(Scanner* scanner); 1475 PreParserIdentifier GetNumberAsSymbol(Scanner* scanner);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1498 } 1520 }
1499 1521
1500 static PreParserStatementList NewStatementList(int size, Zone* zone) { 1522 static PreParserStatementList NewStatementList(int size, Zone* zone) {
1501 return PreParserStatementList(); 1523 return PreParserStatementList();
1502 } 1524 }
1503 1525
1504 static PreParserExpressionList NewPropertyList(int size, Zone* zone) { 1526 static PreParserExpressionList NewPropertyList(int size, Zone* zone) {
1505 return PreParserExpressionList(); 1527 return PreParserExpressionList();
1506 } 1528 }
1507 1529
1530 static PreParserFormalParameterList NewFormalParameterList(int size,
1531 Zone* zone) {
1532 return PreParserFormalParameterList();
1533 }
1534
1508 V8_INLINE void SkipLazyFunctionBody(PreParserIdentifier function_name, 1535 V8_INLINE void SkipLazyFunctionBody(PreParserIdentifier function_name,
1509 int* materialized_literal_count, 1536 int* materialized_literal_count,
1510 int* expected_property_count, bool* ok) { 1537 int* expected_property_count, bool* ok) {
1511 UNREACHABLE(); 1538 UNREACHABLE();
1512 } 1539 }
1513 1540
1514 V8_INLINE PreParserStatementList 1541 V8_INLINE PreParserStatementList
1515 ParseEagerFunctionBody(PreParserIdentifier function_name, int pos, 1542 ParseEagerFunctionBody(PreParserIdentifier function_name, int pos,
1516 Variable* fvar, Token::Value fvar_init_op, 1543 Variable* fvar, Token::Value fvar_init_op,
1517 FunctionKind kind, bool* ok); 1544 FunctionKind kind, bool* ok);
(...skipping 1501 matching lines...) Expand 10 before | Expand all | Expand 10 after
3019 default: 3046 default:
3020 return expression; 3047 return expression;
3021 } 3048 }
3022 } 3049 }
3023 DCHECK(false); 3050 DCHECK(false);
3024 return this->EmptyExpression(); 3051 return this->EmptyExpression();
3025 } 3052 }
3026 3053
3027 3054
3028 template <class Traits> 3055 template <class Traits>
3056 typename ParserBase<Traits>::FormalParameterT
3057 ParserBase<Traits>::ParseFormalParameter(DuplicateFinder* duplicate_finder,
3058 Scanner::Location* eval_args_error_loc,
3059 Scanner::Location* undefined_error_loc,
3060 Scanner::Location* dupe_error_loc,
3061 Scanner::Location* reserved_error_loc,
3062 bool* ok) {
3063 // FormalParameter[Yield,GeneratorParameter] :
3064 // BindingElement[?Yield, ?GeneratorParameter]
3065 bool is_strict_reserved;
3066 IdentifierT name =
3067 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, ok);
3068 if (!*ok) return this->EmptyFormalParameter();
3069
3070 // Store locations for possible future error reports.
3071 if (!eval_args_error_loc->IsValid() && this->IsEvalOrArguments(name)) {
3072 *eval_args_error_loc = scanner()->location();
3073 }
3074 if (!undefined_error_loc->IsValid() && this->IsUndefined(name)) {
3075 *undefined_error_loc = scanner()->location();
3076 }
3077 if (!reserved_error_loc->IsValid() && is_strict_reserved) {
3078 *reserved_error_loc = scanner()->location();
3079 }
3080 if (!dupe_error_loc->IsValid()) {
3081 int prev_value = scanner()->FindSymbol(duplicate_finder, 1);
3082 if (prev_value != 0) *dupe_error_loc = scanner()->location();
3083 }
3084
3085 return name;
3086 }
3087
3088
3089 template <class Traits>
3090 typename ParserBase<Traits>::FormalParameterListT
3091 ParserBase<Traits>::ParseFormalParameterList(
3092 Scanner::Location* eval_args_error_loc,
3093 Scanner::Location* undefined_error_loc, Scanner::Location* dupe_error_loc,
3094 Scanner::Location* reserved_error_loc, bool* is_rest, bool* ok) {
3095 // FormalParameters[Yield,GeneratorParameter] :
3096 // [empty]
3097 // FormalParameterList[?Yield, ?GeneratorParameter]
3098 //
3099 // FormalParameterList[Yield,GeneratorParameter] :
3100 // FunctionRestParameter[?Yield]
3101 // FormalsList[?Yield, ?GeneratorParameter]
3102 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield]
3103 //
3104 // FormalsList[Yield,GeneratorParameter] :
3105 // FormalParameter[?Yield, ?GeneratorParameter]
3106 // FormalsList[?Yield, ?GeneratorParameter] ,
3107 // FormalParameter[?Yield,?GeneratorParameter]
3108
3109 FormalParameterListT result = this->NewFormalParameterList(4, zone_);
3110 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
3111
3112 if (peek() != Token::RPAREN) {
3113 do {
3114 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS);
3115 FormalParameterT param = ParseFormalParameter(
3116 &duplicate_finder, eval_args_error_loc, undefined_error_loc,
3117 dupe_error_loc, reserved_error_loc, ok);
3118 if (!*ok) return this->NullFormalParameterList();
3119 result->Add(param, zone());
3120 if (result->length() > Code::kMaxArguments) {
3121 ReportMessage("too_many_parameters");
3122 *ok = false;
3123 return this->NullFormalParameterList();
3124 }
3125 } while (!*is_rest && Check(Token::COMMA));
3126 }
3127
3128 if (is_rest && peek() == Token::COMMA) {
3129 ReportMessageAt(scanner()->peek_location(), "param_after_rest");
3130 *ok = false;
3131 return this->NullFormalParameterList();
3132 }
3133
3134 return result;
3135 }
3136
3137
3138 template <class Traits>
3139 void ParserBase<Traits>::CheckArityRestrictions(
3140 int param_count, FunctionLiteral::ArityRestriction arity_restriction,
3141 int formals_start_pos, int formals_end_pos, bool* ok) {
3142 switch (arity_restriction) {
3143 case FunctionLiteral::GETTER_ARITY:
3144 if (param_count != 0) {
3145 ReportMessageAt(Scanner::Location(formals_start_pos, formals_end_pos),
3146 "bad_getter_arity");
3147 *ok = false;
3148 }
3149 break;
3150 case FunctionLiteral::SETTER_ARITY:
3151 if (param_count != 1) {
3152 ReportMessageAt(Scanner::Location(formals_start_pos, formals_end_pos),
3153 "bad_setter_arity");
3154 *ok = false;
3155 }
3156 break;
3157 default:
3158 break;
3159 }
3160 }
3161
3162 template <class Traits>
3029 typename ParserBase<Traits>::ExpressionT 3163 typename ParserBase<Traits>::ExpressionT
3030 ParserBase<Traits>::ParseArrowFunctionLiteral(int start_pos, 3164 ParserBase<Traits>::ParseArrowFunctionLiteral(int start_pos,
3031 ExpressionT params_ast, 3165 ExpressionT params_ast,
3032 bool* ok) { 3166 bool* ok) {
3033 if (peek() == Token::ARROW && scanner_->HasAnyLineTerminatorBeforeNext()) { 3167 if (peek() == Token::ARROW && scanner_->HasAnyLineTerminatorBeforeNext()) {
3034 // ASI inserts `;` after arrow parameters if a line terminator is found. 3168 // ASI inserts `;` after arrow parameters if a line terminator is found.
3035 // `=> ...` is never a valid expression, so report as syntax error. 3169 // `=> ...` is never a valid expression, so report as syntax error.
3036 // If next token is not `=>`, it's a syntax error anyways. 3170 // If next token is not `=>`, it's a syntax error anyways.
3037 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW); 3171 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW);
3038 *ok = false; 3172 *ok = false;
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
3326 *ok = false; 3460 *ok = false;
3327 return; 3461 return;
3328 } 3462 }
3329 has_seen_constructor_ = true; 3463 has_seen_constructor_ = true;
3330 return; 3464 return;
3331 } 3465 }
3332 } 3466 }
3333 } } // v8::internal 3467 } } // v8::internal
3334 3468
3335 #endif // V8_PREPARSER_H 3469 #endif // V8_PREPARSER_H
OLDNEW
« no previous file with comments | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698