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

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: 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
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 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 ExpressionT ParseMemberWithNewPrefixesExpression(bool* ok); 595 ExpressionT ParseMemberWithNewPrefixesExpression(bool* ok);
592 ExpressionT ParseMemberExpression(bool* ok); 596 ExpressionT ParseMemberExpression(bool* ok);
593 ExpressionT ParseMemberExpressionContinuation(ExpressionT expression, 597 ExpressionT ParseMemberExpressionContinuation(ExpressionT expression,
594 bool* ok); 598 bool* ok);
595 ExpressionT ParseArrowFunctionLiteral(int start_pos, ExpressionT params_ast, 599 ExpressionT ParseArrowFunctionLiteral(int start_pos, ExpressionT params_ast,
596 bool* ok); 600 bool* ok);
597 ExpressionT ParseTemplateLiteral(ExpressionT tag, int start, bool* ok); 601 ExpressionT ParseTemplateLiteral(ExpressionT tag, int start, bool* ok);
598 void AddTemplateExpression(ExpressionT); 602 void AddTemplateExpression(ExpressionT);
599 ExpressionT ParseSuperExpression(bool is_new, bool* ok); 603 ExpressionT ParseSuperExpression(bool is_new, bool* ok);
600 604
605 FormalParameterT ParseFormalParameter(DuplicateFinder* duplicate_finder,
606 Scanner::Location* eval_args_error_loc,
607 Scanner::Location* dupe_error_loc,
608 Scanner::Location* reserved_error_loc,
609 bool* ok);
610 FormalParameterListT ParseFormalParameterList(
611 Scanner::Location* eval_args_error_loc, Scanner::Location* dupe_error_loc,
612 Scanner::Location* reserved_error_loc, bool* is_rest, bool* ok);
613 void CheckArityRestrictions(
614 int param_count, FunctionLiteral::ArityRestriction arity_restriction,
615 int formals_start_pos, int formals_end_pos, bool* ok);
616
601 // Checks if the expression is a valid reference expression (e.g., on the 617 // Checks if the expression is a valid reference expression (e.g., on the
602 // left-hand side of assignments). Although ruled out by ECMA as early errors, 618 // left-hand side of assignments). Although ruled out by ECMA as early errors,
603 // we allow calls for web compatibility and rewrite them to a runtime throw. 619 // we allow calls for web compatibility and rewrite them to a runtime throw.
604 ExpressionT CheckAndRewriteReferenceExpression( 620 ExpressionT CheckAndRewriteReferenceExpression(
605 ExpressionT expression, 621 ExpressionT expression,
606 Scanner::Location location, const char* message, bool* ok); 622 Scanner::Location location, const char* message, bool* ok);
607 623
608 // Used to validate property names in object literals and class literals 624 // Used to validate property names in object literals and class literals
609 enum PropertyKind { 625 enum PropertyKind {
610 kAccessorProperty, 626 kAccessorProperty,
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseStrongField; 1008 typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseStrongField;
993 typedef BitField<bool, ParenthesizationField::kNext, 1> 1009 typedef BitField<bool, ParenthesizationField::kNext, 1>
994 IsValidArrowParamListField; 1010 IsValidArrowParamListField;
995 typedef BitField<PreParserIdentifier::Type, ParenthesizationField::kNext, 10> 1011 typedef BitField<PreParserIdentifier::Type, ParenthesizationField::kNext, 10>
996 IdentifierTypeField; 1012 IdentifierTypeField;
997 1013
998 uint32_t code_; 1014 uint32_t code_;
999 }; 1015 };
1000 1016
1001 1017
1002 // PreParserExpressionList doesn't actually store the expressions because 1018 // The pre-parser doesn't need to build lists of expressions, identifiers, or
1003 // PreParser doesn't need to. 1019 // the like.
1004 class PreParserExpressionList { 1020 template <typename T>
1021 class PreParserList {
1005 public: 1022 public:
1006 // These functions make list->Add(some_expression) work (and do nothing). 1023 // These functions make list->Add(some_expression) work (and do nothing).
1007 PreParserExpressionList() : length_(0) {} 1024 PreParserList() : length_(0) {}
1008 PreParserExpressionList* operator->() { return this; } 1025 PreParserList* operator->() { return this; }
1009 void Add(PreParserExpression, void*) { ++length_; } 1026 void Add(T, void*) { ++length_; }
1010 int length() const { return length_; } 1027 int length() const { return length_; }
1011 private: 1028 private:
1012 int length_; 1029 int length_;
1013 }; 1030 };
1014 1031
1015 1032
1033 typedef PreParserList<PreParserExpression> PreParserExpressionList;
1034 typedef PreParserList<PreParserIdentifier> PreParserFormalParameterList;
1035
1036
1016 class PreParserStatement { 1037 class PreParserStatement {
1017 public: 1038 public:
1018 static PreParserStatement Default() { 1039 static PreParserStatement Default() {
1019 return PreParserStatement(kUnknownStatement); 1040 return PreParserStatement(kUnknownStatement);
1020 } 1041 }
1021 1042
1022 static PreParserStatement FunctionDeclaration() { 1043 static PreParserStatement FunctionDeclaration() {
1023 return PreParserStatement(kFunctionDeclaration); 1044 return PreParserStatement(kFunctionDeclaration);
1024 } 1045 }
1025 1046
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 kUseStrictExpressionStatement, 1082 kUseStrictExpressionStatement,
1062 kUseStrongExpressionStatement, 1083 kUseStrongExpressionStatement,
1063 kFunctionDeclaration 1084 kFunctionDeclaration
1064 }; 1085 };
1065 1086
1066 explicit PreParserStatement(Type code) : code_(code) {} 1087 explicit PreParserStatement(Type code) : code_(code) {}
1067 Type code_; 1088 Type code_;
1068 }; 1089 };
1069 1090
1070 1091
1071 1092 typedef PreParserList<PreParserStatement> PreParserStatementList;
1072 // PreParserStatementList doesn't actually store the statements because
1073 // the PreParser does not need them.
1074 class PreParserStatementList {
1075 public:
1076 // These functions make list->Add(some_expression) work as no-ops.
1077 PreParserStatementList() {}
1078 PreParserStatementList* operator->() { return this; }
1079 void Add(PreParserStatement, void*) {}
1080 };
1081 1093
1082 1094
1083 class PreParserFactory { 1095 class PreParserFactory {
1084 public: 1096 public:
1085 explicit PreParserFactory(void* unused_value_factory) {} 1097 explicit PreParserFactory(void* unused_value_factory) {}
1086 PreParserExpression NewStringLiteral(PreParserIdentifier identifier, 1098 PreParserExpression NewStringLiteral(PreParserIdentifier identifier,
1087 int pos) { 1099 int pos) {
1088 return PreParserExpression::Default(); 1100 return PreParserExpression::Default();
1089 } 1101 }
1090 PreParserExpression NewNumberLiteral(double number, 1102 PreParserExpression NewNumberLiteral(double number,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1235 // Return types for traversing functions. 1247 // Return types for traversing functions.
1236 typedef PreParserIdentifier Identifier; 1248 typedef PreParserIdentifier Identifier;
1237 typedef PreParserExpression Expression; 1249 typedef PreParserExpression Expression;
1238 typedef PreParserExpression YieldExpression; 1250 typedef PreParserExpression YieldExpression;
1239 typedef PreParserExpression FunctionLiteral; 1251 typedef PreParserExpression FunctionLiteral;
1240 typedef PreParserExpression ClassLiteral; 1252 typedef PreParserExpression ClassLiteral;
1241 typedef PreParserExpression ObjectLiteralProperty; 1253 typedef PreParserExpression ObjectLiteralProperty;
1242 typedef PreParserExpression Literal; 1254 typedef PreParserExpression Literal;
1243 typedef PreParserExpressionList ExpressionList; 1255 typedef PreParserExpressionList ExpressionList;
1244 typedef PreParserExpressionList PropertyList; 1256 typedef PreParserExpressionList PropertyList;
1257 typedef PreParserIdentifier FormalParameter;
1258 typedef PreParserFormalParameterList FormalParameterList;
1245 typedef PreParserStatementList StatementList; 1259 typedef PreParserStatementList StatementList;
1246 1260
1247 // For constructing objects returned by the traversing functions. 1261 // For constructing objects returned by the traversing functions.
1248 typedef PreParserFactory Factory; 1262 typedef PreParserFactory Factory;
1249 }; 1263 };
1250 1264
1251 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {} 1265 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {}
1252 1266
1253 // Helper functions for recursive descent. 1267 // Helper functions for recursive descent.
1254 static bool IsEval(PreParserIdentifier identifier) { 1268 static bool IsEval(PreParserIdentifier identifier) {
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1389 } 1403 }
1390 static PreParserExpression EmptyObjectLiteralProperty() { 1404 static PreParserExpression EmptyObjectLiteralProperty() {
1391 return PreParserExpression::Default(); 1405 return PreParserExpression::Default();
1392 } 1406 }
1393 static PreParserExpression EmptyFunctionLiteral() { 1407 static PreParserExpression EmptyFunctionLiteral() {
1394 return PreParserExpression::Default(); 1408 return PreParserExpression::Default();
1395 } 1409 }
1396 static PreParserExpressionList NullExpressionList() { 1410 static PreParserExpressionList NullExpressionList() {
1397 return PreParserExpressionList(); 1411 return PreParserExpressionList();
1398 } 1412 }
1413 static PreParserIdentifier EmptyFormalParameter() {
1414 return PreParserIdentifier::Default();
1415 }
1416 static PreParserFormalParameterList NullFormalParameterList() {
1417 return PreParserFormalParameterList();
1418 }
1399 1419
1400 // Odd-ball literal creators. 1420 // Odd-ball literal creators.
1401 static PreParserExpression GetLiteralTheHole(int position, 1421 static PreParserExpression GetLiteralTheHole(int position,
1402 PreParserFactory* factory) { 1422 PreParserFactory* factory) {
1403 return PreParserExpression::Default(); 1423 return PreParserExpression::Default();
1404 } 1424 }
1405 1425
1406 // Producing data during the recursive descent. 1426 // Producing data during the recursive descent.
1407 PreParserIdentifier GetSymbol(Scanner* scanner); 1427 PreParserIdentifier GetSymbol(Scanner* scanner);
1408 PreParserIdentifier GetNumberAsSymbol(Scanner* scanner); 1428 PreParserIdentifier GetNumberAsSymbol(Scanner* scanner);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1453 } 1473 }
1454 1474
1455 static PreParserStatementList NewStatementList(int size, Zone* zone) { 1475 static PreParserStatementList NewStatementList(int size, Zone* zone) {
1456 return PreParserStatementList(); 1476 return PreParserStatementList();
1457 } 1477 }
1458 1478
1459 static PreParserExpressionList NewPropertyList(int size, Zone* zone) { 1479 static PreParserExpressionList NewPropertyList(int size, Zone* zone) {
1460 return PreParserExpressionList(); 1480 return PreParserExpressionList();
1461 } 1481 }
1462 1482
1483 static PreParserFormalParameterList NewFormalParameterList(int size,
1484 Zone* zone) {
1485 return PreParserFormalParameterList();
1486 }
1487
1463 V8_INLINE void SkipLazyFunctionBody(PreParserIdentifier function_name, 1488 V8_INLINE void SkipLazyFunctionBody(PreParserIdentifier function_name,
1464 int* materialized_literal_count, 1489 int* materialized_literal_count,
1465 int* expected_property_count, bool* ok) { 1490 int* expected_property_count, bool* ok) {
1466 UNREACHABLE(); 1491 UNREACHABLE();
1467 } 1492 }
1468 1493
1469 V8_INLINE PreParserStatementList 1494 V8_INLINE PreParserStatementList
1470 ParseEagerFunctionBody(PreParserIdentifier function_name, int pos, 1495 ParseEagerFunctionBody(PreParserIdentifier function_name, int pos,
1471 Variable* fvar, Token::Value fvar_init_op, 1496 Variable* fvar, Token::Value fvar_init_op,
1472 FunctionKind kind, bool* ok); 1497 FunctionKind kind, bool* ok);
(...skipping 1498 matching lines...) Expand 10 before | Expand all | Expand 10 after
2971 default: 2996 default:
2972 return expression; 2997 return expression;
2973 } 2998 }
2974 } 2999 }
2975 DCHECK(false); 3000 DCHECK(false);
2976 return this->EmptyExpression(); 3001 return this->EmptyExpression();
2977 } 3002 }
2978 3003
2979 3004
2980 template <class Traits> 3005 template <class Traits>
3006 typename ParserBase<Traits>::FormalParameterT
3007 ParserBase<Traits>::ParseFormalParameter(DuplicateFinder* duplicate_finder,
3008 Scanner::Location* eval_args_error_loc,
3009 Scanner::Location* dupe_error_loc,
3010 Scanner::Location* reserved_error_loc,
3011 bool* ok) {
3012 // FormalParameter[Yield,GeneratorParameter] :
3013 // BindingElement[?Yield, ?GeneratorParameter]
3014 bool is_strict_reserved;
3015 IdentifierT name =
3016 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, ok);
3017 if (!*ok) return this->EmptyFormalParameter();
3018
3019 // Store locations for possible future error reports.
3020 if (!eval_args_error_loc->IsValid() && this->IsEvalOrArguments(name)) {
3021 *eval_args_error_loc = scanner()->location();
3022 }
3023 if (!reserved_error_loc->IsValid() && is_strict_reserved) {
3024 *reserved_error_loc = scanner()->location();
3025 }
3026 if (!dupe_error_loc->IsValid()) {
3027 int prev_value = scanner()->FindSymbol(duplicate_finder, 1);
3028 if (prev_value != 0) *dupe_error_loc = scanner()->location();
3029 }
3030
3031 return name;
3032 }
3033
3034
3035 template <class Traits>
3036 typename ParserBase<Traits>::FormalParameterListT
3037 ParserBase<Traits>::ParseFormalParameterList(
3038 Scanner::Location* eval_args_error_loc, Scanner::Location* dupe_error_loc,
3039 Scanner::Location* reserved_error_loc, bool* is_rest, bool* ok) {
3040 // FormalParameters[Yield,GeneratorParameter] :
3041 // [empty]
3042 // FormalParameterList[?Yield, ?GeneratorParameter]
3043 //
3044 // FormalParameterList[Yield,GeneratorParameter] :
3045 // FunctionRestParameter[?Yield]
3046 // FormalsList[?Yield, ?GeneratorParameter]
3047 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield]
3048 //
3049 // FormalsList[Yield,GeneratorParameter] :
3050 // FormalParameter[?Yield, ?GeneratorParameter]
3051 // FormalsList[?Yield, ?GeneratorParameter] ,
3052 // FormalParameter[?Yield,?GeneratorParameter]
3053
3054 FormalParameterListT result = this->NewFormalParameterList(4, zone_);
3055 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
3056
3057 if (peek() != Token::RPAREN) {
3058 do {
3059 *is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS);
3060 FormalParameterT param =
3061 ParseFormalParameter(&duplicate_finder, eval_args_error_loc,
3062 dupe_error_loc, reserved_error_loc, ok);
3063 if (!*ok) return this->NullFormalParameterList();
3064 result->Add(param, zone());
3065 if (result->length() > Code::kMaxArguments) {
3066 ReportMessage("too_many_parameters");
3067 *ok = false;
3068 return this->NullFormalParameterList();
3069 }
3070 } while (!*is_rest && Check(Token::COMMA));
3071 }
3072
3073 return result;
3074 }
3075
3076
3077 template <class Traits>
3078 void ParserBase<Traits>::CheckArityRestrictions(
3079 int param_count, FunctionLiteral::ArityRestriction arity_restriction,
3080 int formals_start_pos, int formals_end_pos, bool* ok) {
3081 switch (arity_restriction) {
3082 case FunctionLiteral::GETTER_ARITY:
3083 if (param_count != 0) {
3084 ReportMessageAt(Scanner::Location(formals_start_pos, formals_end_pos),
3085 "bad_getter_arity");
3086 *ok = false;
3087 }
3088 break;
3089 case FunctionLiteral::SETTER_ARITY:
3090 if (param_count != 1) {
3091 ReportMessageAt(Scanner::Location(formals_start_pos, formals_end_pos),
3092 "bad_setter_arity");
3093 *ok = false;
3094 }
3095 break;
3096 default:
3097 break;
3098 }
3099 }
3100
3101 template <class Traits>
2981 typename ParserBase<Traits>::ExpressionT 3102 typename ParserBase<Traits>::ExpressionT
2982 ParserBase<Traits>::ParseArrowFunctionLiteral(int start_pos, 3103 ParserBase<Traits>::ParseArrowFunctionLiteral(int start_pos,
2983 ExpressionT params_ast, 3104 ExpressionT params_ast,
2984 bool* ok) { 3105 bool* ok) {
2985 if (peek() == Token::ARROW && scanner_->HasAnyLineTerminatorBeforeNext()) { 3106 if (peek() == Token::ARROW && scanner_->HasAnyLineTerminatorBeforeNext()) {
2986 // ASI inserts `;` after arrow parameters if a line terminator is found. 3107 // ASI inserts `;` after arrow parameters if a line terminator is found.
2987 // `=> ...` is never a valid expression, so report as syntax error. 3108 // `=> ...` is never a valid expression, so report as syntax error.
2988 // If next token is not `=>`, it's a syntax error anyways. 3109 // If next token is not `=>`, it's a syntax error anyways.
2989 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW); 3110 ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW);
2990 *ok = false; 3111 *ok = false;
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
3262 *ok = false; 3383 *ok = false;
3263 return; 3384 return;
3264 } 3385 }
3265 has_seen_constructor_ = true; 3386 has_seen_constructor_ = true;
3266 return; 3387 return;
3267 } 3388 }
3268 } 3389 }
3269 } } // v8::internal 3390 } } // v8::internal
3270 3391
3271 #endif // V8_PREPARSER_H 3392 #endif // V8_PREPARSER_H
OLDNEW
« src/messages.js ('K') | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698