OLD | NEW |
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 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 323 |
324 | 324 |
325 bool ParserTraits::IsEvalOrArguments(const AstRawString* identifier) const { | 325 bool ParserTraits::IsEvalOrArguments(const AstRawString* identifier) const { |
326 return IsEval(identifier) || IsArguments(identifier); | 326 return IsEval(identifier) || IsArguments(identifier); |
327 } | 327 } |
328 | 328 |
329 bool ParserTraits::IsUndefined(const AstRawString* identifier) const { | 329 bool ParserTraits::IsUndefined(const AstRawString* identifier) const { |
330 return identifier == parser_->ast_value_factory()->undefined_string(); | 330 return identifier == parser_->ast_value_factory()->undefined_string(); |
331 } | 331 } |
332 | 332 |
| 333 bool ParserTraits::IsAwait(const AstRawString* identifier) const { |
| 334 return identifier == parser_->ast_value_factory()->await_string(); |
| 335 } |
| 336 |
333 bool ParserTraits::IsPrototype(const AstRawString* identifier) const { | 337 bool ParserTraits::IsPrototype(const AstRawString* identifier) const { |
334 return identifier == parser_->ast_value_factory()->prototype_string(); | 338 return identifier == parser_->ast_value_factory()->prototype_string(); |
335 } | 339 } |
336 | 340 |
337 | 341 |
338 bool ParserTraits::IsConstructor(const AstRawString* identifier) const { | 342 bool ParserTraits::IsConstructor(const AstRawString* identifier) const { |
339 return identifier == parser_->ast_value_factory()->constructor_string(); | 343 return identifier == parser_->ast_value_factory()->constructor_string(); |
340 } | 344 } |
341 | 345 |
342 | 346 |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 set_allow_tailcalls(FLAG_harmony_tailcalls && !info->is_native() && | 795 set_allow_tailcalls(FLAG_harmony_tailcalls && !info->is_native() && |
792 info->isolate()->is_tail_call_elimination_enabled()); | 796 info->isolate()->is_tail_call_elimination_enabled()); |
793 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions); | 797 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions); |
794 set_allow_harmony_for_in(FLAG_harmony_for_in); | 798 set_allow_harmony_for_in(FLAG_harmony_for_in); |
795 set_allow_harmony_function_name(FLAG_harmony_function_name); | 799 set_allow_harmony_function_name(FLAG_harmony_function_name); |
796 set_allow_harmony_function_sent(FLAG_harmony_function_sent); | 800 set_allow_harmony_function_sent(FLAG_harmony_function_sent); |
797 set_allow_harmony_restrictive_declarations( | 801 set_allow_harmony_restrictive_declarations( |
798 FLAG_harmony_restrictive_declarations); | 802 FLAG_harmony_restrictive_declarations); |
799 set_allow_harmony_exponentiation_operator( | 803 set_allow_harmony_exponentiation_operator( |
800 FLAG_harmony_exponentiation_operator); | 804 FLAG_harmony_exponentiation_operator); |
| 805 set_allow_harmony_async_await(FLAG_harmony_async_await); |
801 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; | 806 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; |
802 ++feature) { | 807 ++feature) { |
803 use_counts_[feature] = 0; | 808 use_counts_[feature] = 0; |
804 } | 809 } |
805 if (info->ast_value_factory() == NULL) { | 810 if (info->ast_value_factory() == NULL) { |
806 // info takes ownership of AstValueFactory. | 811 // info takes ownership of AstValueFactory. |
807 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); | 812 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); |
808 info->set_ast_value_factory_owned(); | 813 info->set_ast_value_factory_owned(); |
809 ast_value_factory_ = info->ast_value_factory(); | 814 ast_value_factory_ = info->ast_value_factory(); |
810 } | 815 } |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1066 FunctionState function_state(&function_state_, &scope_, scope, | 1071 FunctionState function_state(&function_state_, &scope_, scope, |
1067 shared_info->kind(), &function_factory); | 1072 shared_info->kind(), &function_factory); |
1068 DCHECK(is_sloppy(scope->language_mode()) || | 1073 DCHECK(is_sloppy(scope->language_mode()) || |
1069 is_strict(info->language_mode())); | 1074 is_strict(info->language_mode())); |
1070 DCHECK(info->language_mode() == shared_info->language_mode()); | 1075 DCHECK(info->language_mode() == shared_info->language_mode()); |
1071 FunctionLiteral::FunctionType function_type = | 1076 FunctionLiteral::FunctionType function_type = |
1072 ComputeFunctionType(shared_info); | 1077 ComputeFunctionType(shared_info); |
1073 bool ok = true; | 1078 bool ok = true; |
1074 | 1079 |
1075 if (shared_info->is_arrow()) { | 1080 if (shared_info->is_arrow()) { |
| 1081 bool is_async = allow_harmony_async_await() && shared_info->is_async(); |
| 1082 if (is_async) { |
| 1083 DCHECK(!scanner()->HasAnyLineTerminatorAfterNext()); |
| 1084 Consume(Token::ASYNC); |
| 1085 DCHECK(peek_any_identifier() || peek() == Token::LPAREN); |
| 1086 } |
| 1087 |
1076 // TODO(adamk): We should construct this scope from the ScopeInfo. | 1088 // TODO(adamk): We should construct this scope from the ScopeInfo. |
1077 Scope* scope = | 1089 Scope* scope = |
1078 NewScope(scope_, FUNCTION_SCOPE, FunctionKind::kArrowFunction); | 1090 NewScope(scope_, FUNCTION_SCOPE, FunctionKind::kArrowFunction); |
1079 | 1091 |
1080 // These two bits only need to be explicitly set because we're | 1092 // These two bits only need to be explicitly set because we're |
1081 // not passing the ScopeInfo to the Scope constructor. | 1093 // not passing the ScopeInfo to the Scope constructor. |
1082 // TODO(adamk): Remove these calls once the above NewScope call | 1094 // TODO(adamk): Remove these calls once the above NewScope call |
1083 // passes the ScopeInfo. | 1095 // passes the ScopeInfo. |
1084 if (shared_info->scope_info()->CallsEval()) { | 1096 if (shared_info->scope_info()->CallsEval()) { |
1085 scope->RecordEvalCall(); | 1097 scope->RecordEvalCall(); |
1086 } | 1098 } |
1087 SetLanguageMode(scope, shared_info->language_mode()); | 1099 SetLanguageMode(scope, shared_info->language_mode()); |
1088 | 1100 |
1089 scope->set_start_position(shared_info->start_position()); | 1101 scope->set_start_position(shared_info->start_position()); |
1090 ExpressionClassifier formals_classifier(this); | 1102 ExpressionClassifier formals_classifier(this); |
1091 ParserFormalParameters formals(scope); | 1103 ParserFormalParameters formals(scope); |
1092 Checkpoint checkpoint(this); | 1104 Checkpoint checkpoint(this); |
1093 { | 1105 { |
1094 // Parsing patterns as variable reference expression creates | 1106 // Parsing patterns as variable reference expression creates |
1095 // NewUnresolved references in current scope. Entrer arrow function | 1107 // NewUnresolved references in current scope. Entrer arrow function |
1096 // scope for formal parameter parsing. | 1108 // scope for formal parameter parsing. |
1097 BlockState block_state(&scope_, scope); | 1109 BlockState block_state(&scope_, scope); |
| 1110 function_state.set_parse_phase(FunctionParsePhase::FormalParameters); |
1098 if (Check(Token::LPAREN)) { | 1111 if (Check(Token::LPAREN)) { |
1099 // '(' StrictFormalParameters ')' | 1112 // '(' StrictFormalParameters ')' |
1100 ParseFormalParameterList(&formals, &formals_classifier, &ok); | 1113 ParseFormalParameterList(&formals, &formals_classifier, &ok); |
1101 if (ok) ok = Check(Token::RPAREN); | 1114 if (ok) ok = Check(Token::RPAREN); |
1102 } else { | 1115 } else { |
1103 // BindingIdentifier | 1116 // BindingIdentifier |
1104 ParseFormalParameter(&formals, &formals_classifier, &ok); | 1117 ParseFormalParameter(&formals, &formals_classifier, &ok); |
1105 if (ok) { | 1118 if (ok) { |
1106 DeclareFormalParameter(formals.scope, formals.at(0), | 1119 DeclareFormalParameter(formals.scope, formals.at(0), |
1107 &formals_classifier); | 1120 &formals_classifier); |
1108 } | 1121 } |
1109 } | 1122 } |
1110 } | 1123 } |
1111 | 1124 |
1112 if (ok) { | 1125 if (ok) { |
| 1126 function_state.set_parse_phase(FunctionParsePhase::FunctionBody); |
1113 checkpoint.Restore(&formals.materialized_literals_count); | 1127 checkpoint.Restore(&formals.materialized_literals_count); |
1114 // Pass `accept_IN=true` to ParseArrowFunctionLiteral --- This should | 1128 // Pass `accept_IN=true` to ParseArrowFunctionLiteral --- This should |
1115 // not be observable, or else the preparser would have failed. | 1129 // not be observable, or else the preparser would have failed. |
1116 Expression* expression = | 1130 Expression* expression = ParseArrowFunctionLiteral( |
1117 ParseArrowFunctionLiteral(true, formals, formals_classifier, &ok); | 1131 true, formals, is_async, formals_classifier, &ok); |
1118 if (ok) { | 1132 if (ok) { |
1119 // Scanning must end at the same position that was recorded | 1133 // Scanning must end at the same position that was recorded |
1120 // previously. If not, parsing has been interrupted due to a stack | 1134 // previously. If not, parsing has been interrupted due to a stack |
1121 // overflow, at which point the partially parsed arrow function | 1135 // overflow, at which point the partially parsed arrow function |
1122 // concise body happens to be a valid expression. This is a problem | 1136 // concise body happens to be a valid expression. This is a problem |
1123 // only for arrow functions with single expression bodies, since there | 1137 // only for arrow functions with single expression bodies, since there |
1124 // is no end token such as "}" for normal functions. | 1138 // is no end token such as "}" for normal functions. |
1125 if (scanner()->location().end_pos == shared_info->end_position()) { | 1139 if (scanner()->location().end_pos == shared_info->end_position()) { |
1126 // The pre-parser saw an arrow function here, so the full parser | 1140 // The pre-parser saw an arrow function here, so the full parser |
1127 // must produce a FunctionLiteral. | 1141 // must produce a FunctionLiteral. |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1246 | 1260 |
1247 return 0; | 1261 return 0; |
1248 } | 1262 } |
1249 | 1263 |
1250 | 1264 |
1251 Statement* Parser::ParseStatementListItem(bool* ok) { | 1265 Statement* Parser::ParseStatementListItem(bool* ok) { |
1252 // (Ecma 262 6th Edition, 13.1): | 1266 // (Ecma 262 6th Edition, 13.1): |
1253 // StatementListItem: | 1267 // StatementListItem: |
1254 // Statement | 1268 // Statement |
1255 // Declaration | 1269 // Declaration |
1256 | 1270 const Token::Value peeked = peek(); |
1257 switch (peek()) { | 1271 switch (peeked) { |
1258 case Token::FUNCTION: | 1272 case Token::FUNCTION: |
1259 return ParseHoistableDeclaration(NULL, ok); | 1273 return ParseHoistableDeclaration(NULL, ok); |
1260 case Token::CLASS: | 1274 case Token::CLASS: |
1261 Consume(Token::CLASS); | 1275 Consume(Token::CLASS); |
1262 return ParseClassDeclaration(NULL, ok); | 1276 return ParseClassDeclaration(NULL, ok); |
1263 case Token::CONST: | 1277 case Token::CONST: |
1264 return ParseVariableStatement(kStatementListItem, NULL, ok); | 1278 return ParseVariableStatement(kStatementListItem, NULL, ok); |
1265 case Token::VAR: | 1279 case Token::VAR: |
1266 return ParseVariableStatement(kStatementListItem, NULL, ok); | 1280 return ParseVariableStatement(kStatementListItem, NULL, ok); |
1267 case Token::LET: | 1281 case Token::LET: |
1268 if (IsNextLetKeyword()) { | 1282 if (IsNextLetKeyword()) { |
1269 return ParseVariableStatement(kStatementListItem, NULL, ok); | 1283 return ParseVariableStatement(kStatementListItem, NULL, ok); |
1270 } | 1284 } |
1271 break; | 1285 break; |
| 1286 case Token::ASYNC: |
| 1287 if (allow_harmony_async_await() && PeekAhead() == Token::FUNCTION && |
| 1288 !scanner()->HasAnyLineTerminatorAfterNext()) { |
| 1289 Consume(Token::ASYNC); |
| 1290 return ParseAsyncFunctionDeclaration(NULL, ok); |
| 1291 } |
| 1292 /* falls through */ |
1272 default: | 1293 default: |
1273 break; | 1294 break; |
1274 } | 1295 } |
1275 return ParseStatement(NULL, kAllowLabelledFunctionStatement, ok); | 1296 return ParseStatement(NULL, kAllowLabelledFunctionStatement, ok); |
1276 } | 1297 } |
1277 | 1298 |
1278 | 1299 |
1279 Statement* Parser::ParseModuleItem(bool* ok) { | 1300 Statement* Parser::ParseModuleItem(bool* ok) { |
1280 // (Ecma 262 6th Edition, 15.2): | 1301 // (Ecma 262 6th Edition, 15.2): |
1281 // ModuleItem : | 1302 // ModuleItem : |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1551 // GeneratorDeclaration[+Default] :: | 1572 // GeneratorDeclaration[+Default] :: |
1552 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' | 1573 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' |
1553 default_export = ParseFunctionLiteral( | 1574 default_export = ParseFunctionLiteral( |
1554 default_string, Scanner::Location::invalid(), | 1575 default_string, Scanner::Location::invalid(), |
1555 kSkipFunctionNameCheck, | 1576 kSkipFunctionNameCheck, |
1556 is_generator ? FunctionKind::kGeneratorFunction | 1577 is_generator ? FunctionKind::kGeneratorFunction |
1557 : FunctionKind::kNormalFunction, | 1578 : FunctionKind::kNormalFunction, |
1558 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); | 1579 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); |
1559 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); | 1580 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); |
1560 } else { | 1581 } else { |
1561 result = ParseHoistableDeclaration(pos, is_generator, &names, CHECK_OK); | 1582 result = ParseHoistableDeclaration( |
| 1583 pos, is_generator ? ParseFunctionFlags::kIsGenerator |
| 1584 : ParseFunctionFlags::kIsNormal, |
| 1585 &names, CHECK_OK); |
1562 } | 1586 } |
1563 break; | 1587 break; |
1564 } | 1588 } |
1565 | 1589 |
1566 case Token::CLASS: | 1590 case Token::CLASS: |
1567 Consume(Token::CLASS); | 1591 Consume(Token::CLASS); |
1568 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) { | 1592 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) { |
1569 // ClassDeclaration[+Default] :: | 1593 // ClassDeclaration[+Default] :: |
1570 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' | 1594 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' |
1571 default_export = ParseClassLiteral(nullptr, default_string, | 1595 default_export = ParseClassLiteral(nullptr, default_string, |
(...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2053 return factory()->NewExpressionStatement( | 2077 return factory()->NewExpressionStatement( |
2054 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition), | 2078 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition), |
2055 pos); | 2079 pos); |
2056 } | 2080 } |
2057 | 2081 |
2058 | 2082 |
2059 Statement* Parser::ParseHoistableDeclaration( | 2083 Statement* Parser::ParseHoistableDeclaration( |
2060 ZoneList<const AstRawString*>* names, bool* ok) { | 2084 ZoneList<const AstRawString*>* names, bool* ok) { |
2061 Expect(Token::FUNCTION, CHECK_OK); | 2085 Expect(Token::FUNCTION, CHECK_OK); |
2062 int pos = position(); | 2086 int pos = position(); |
2063 bool is_generator = Check(Token::MUL); | 2087 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; |
2064 return ParseHoistableDeclaration(pos, is_generator, names, ok); | 2088 if (Check(Token::MUL)) { |
| 2089 flags |= ParseFunctionFlags::kIsGenerator; |
| 2090 } |
| 2091 return ParseHoistableDeclaration(pos, flags, names, ok); |
2065 } | 2092 } |
2066 | 2093 |
| 2094 Statement* Parser::ParseAsyncFunctionDeclaration( |
| 2095 ZoneList<const AstRawString*>* names, bool* ok) { |
| 2096 DCHECK_EQ(scanner()->current_token(), Token::ASYNC); |
| 2097 int pos = position(); |
| 2098 Expect(Token::FUNCTION, CHECK_OK); |
| 2099 ParseFunctionFlags flags = ParseFunctionFlags::kIsAsync; |
| 2100 return ParseHoistableDeclaration(pos, flags, names, ok); |
| 2101 } |
2067 | 2102 |
2068 Statement* Parser::ParseHoistableDeclaration( | 2103 Statement* Parser::ParseHoistableDeclaration( |
2069 int pos, bool is_generator, ZoneList<const AstRawString*>* names, | 2104 int pos, ParseFunctionFlags flags, ZoneList<const AstRawString*>* names, |
2070 bool* ok) { | 2105 bool* ok) { |
2071 // FunctionDeclaration :: | 2106 // FunctionDeclaration :: |
2072 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}' | 2107 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}' |
2073 // GeneratorDeclaration :: | 2108 // GeneratorDeclaration :: |
2074 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}' | 2109 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}' |
2075 // | 2110 // |
2076 // 'function' and '*' (if present) have been consumed by the caller. | 2111 // 'function' and '*' (if present) have been consumed by the caller. |
| 2112 const bool is_generator = flags & ParseFunctionFlags::kIsGenerator; |
| 2113 const bool is_async = flags & ParseFunctionFlags::kIsAsync; |
| 2114 DCHECK(!is_generator || !is_async); |
| 2115 |
2077 bool is_strict_reserved = false; | 2116 bool is_strict_reserved = false; |
2078 const AstRawString* name = ParseIdentifierOrStrictReservedWord( | 2117 const AstRawString* name = ParseIdentifierOrStrictReservedWord( |
2079 &is_strict_reserved, CHECK_OK); | 2118 &is_strict_reserved, CHECK_OK); |
2080 | 2119 |
| 2120 if (V8_UNLIKELY(is_async_function() && is_async && this->IsAwait(name))) { |
| 2121 ReportMessageAt(scanner()->location(), |
| 2122 MessageTemplate::kAwaitBindingIdentifier); |
| 2123 *ok = false; |
| 2124 return nullptr; |
| 2125 } |
| 2126 |
2081 FuncNameInferrer::State fni_state(fni_); | 2127 FuncNameInferrer::State fni_state(fni_); |
2082 if (fni_ != NULL) fni_->PushEnclosingName(name); | 2128 if (fni_ != NULL) fni_->PushEnclosingName(name); |
2083 FunctionLiteral* fun = ParseFunctionLiteral( | 2129 FunctionLiteral* fun = ParseFunctionLiteral( |
2084 name, scanner()->location(), | 2130 name, scanner()->location(), |
2085 is_strict_reserved ? kFunctionNameIsStrictReserved | 2131 is_strict_reserved ? kFunctionNameIsStrictReserved |
2086 : kFunctionNameValidityUnknown, | 2132 : kFunctionNameValidityUnknown, |
2087 is_generator ? FunctionKind::kGeneratorFunction | 2133 is_generator ? FunctionKind::kGeneratorFunction |
2088 : FunctionKind::kNormalFunction, | 2134 : is_async ? FunctionKind::kAsyncFunction |
| 2135 : FunctionKind::kNormalFunction, |
2089 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); | 2136 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); |
2090 | 2137 |
2091 // Even if we're not at the top-level of the global or a function | 2138 // Even if we're not at the top-level of the global or a function |
2092 // scope, we treat it as such and introduce the function with its | 2139 // scope, we treat it as such and introduce the function with its |
2093 // initial value upon entering the corresponding scope. | 2140 // initial value upon entering the corresponding scope. |
2094 // In ES6, a function behaves as a lexical binding, except in | 2141 // In ES6, a function behaves as a lexical binding, except in |
2095 // a script scope, or the initial scope of eval or another function. | 2142 // a script scope, or the initial scope of eval or another function. |
2096 VariableMode mode = | 2143 VariableMode mode = |
2097 (!scope_->is_declaration_scope() || scope_->is_module_scope()) ? LET | 2144 (!scope_->is_declaration_scope() || scope_->is_module_scope()) ? LET |
2098 : VAR; | 2145 : VAR; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2391 return true; | 2438 return true; |
2392 } | 2439 } |
2393 } | 2440 } |
2394 } | 2441 } |
2395 return false; | 2442 return false; |
2396 } | 2443 } |
2397 | 2444 |
2398 Statement* Parser::ParseFunctionDeclaration(bool* ok) { | 2445 Statement* Parser::ParseFunctionDeclaration(bool* ok) { |
2399 Consume(Token::FUNCTION); | 2446 Consume(Token::FUNCTION); |
2400 int pos = position(); | 2447 int pos = position(); |
2401 bool is_generator = Check(Token::MUL); | 2448 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; |
2402 if (allow_harmony_restrictive_declarations() && is_generator) { | 2449 if (Check(Token::MUL)) { |
2403 ParserTraits::ReportMessageAt( | 2450 flags |= ParseFunctionFlags::kIsGenerator; |
2404 scanner()->location(), | 2451 if (allow_harmony_restrictive_declarations()) { |
2405 MessageTemplate::kGeneratorInLegacyContext); | 2452 ParserTraits::ReportMessageAt(scanner()->location(), |
2406 *ok = false; | 2453 MessageTemplate::kGeneratorInLegacyContext); |
2407 return nullptr; | 2454 *ok = false; |
| 2455 return nullptr; |
| 2456 } |
2408 } | 2457 } |
2409 return ParseHoistableDeclaration(pos, is_generator, nullptr, CHECK_OK); | 2458 |
| 2459 return ParseHoistableDeclaration(pos, flags, nullptr, CHECK_OK); |
2410 } | 2460 } |
2411 | 2461 |
2412 Statement* Parser::ParseExpressionOrLabelledStatement( | 2462 Statement* Parser::ParseExpressionOrLabelledStatement( |
2413 ZoneList<const AstRawString*>* labels, | 2463 ZoneList<const AstRawString*>* labels, |
2414 AllowLabelledFunctionStatement allow_function, bool* ok) { | 2464 AllowLabelledFunctionStatement allow_function, bool* ok) { |
2415 // ExpressionStatement | LabelledStatement :: | 2465 // ExpressionStatement | LabelledStatement :: |
2416 // Expression ';' | 2466 // Expression ';' |
2417 // Identifier ':' Statement | 2467 // Identifier ':' Statement |
2418 // | 2468 // |
2419 // ExpressionStatement[Yield] : | 2469 // ExpressionStatement[Yield] : |
(...skipping 1614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4034 // expressions. This also marks the FunctionState as a generator. | 4084 // expressions. This also marks the FunctionState as a generator. |
4035 Variable* temp = scope_->NewTemporary( | 4085 Variable* temp = scope_->NewTemporary( |
4036 ast_value_factory()->dot_generator_object_string()); | 4086 ast_value_factory()->dot_generator_object_string()); |
4037 function_state.set_generator_object_variable(temp); | 4087 function_state.set_generator_object_variable(temp); |
4038 } | 4088 } |
4039 | 4089 |
4040 Expect(Token::LPAREN, CHECK_OK); | 4090 Expect(Token::LPAREN, CHECK_OK); |
4041 int start_position = scanner()->location().beg_pos; | 4091 int start_position = scanner()->location().beg_pos; |
4042 scope_->set_start_position(start_position); | 4092 scope_->set_start_position(start_position); |
4043 ParserFormalParameters formals(scope); | 4093 ParserFormalParameters formals(scope); |
| 4094 function_state.set_parse_phase(FunctionParsePhase::FormalParameters); |
4044 ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK); | 4095 ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK); |
4045 arity = formals.Arity(); | 4096 arity = formals.Arity(); |
4046 Expect(Token::RPAREN, CHECK_OK); | 4097 Expect(Token::RPAREN, CHECK_OK); |
4047 int formals_end_position = scanner()->location().end_pos; | 4098 int formals_end_position = scanner()->location().end_pos; |
4048 | 4099 |
4049 CheckArityRestrictions(arity, kind, formals.has_rest, start_position, | 4100 CheckArityRestrictions(arity, kind, formals.has_rest, start_position, |
4050 formals_end_position, CHECK_OK); | 4101 formals_end_position, CHECK_OK); |
4051 Expect(Token::LBRACE, CHECK_OK); | 4102 Expect(Token::LBRACE, CHECK_OK); |
4052 | |
4053 // Don't include the rest parameter into the function's formal parameter | 4103 // Don't include the rest parameter into the function's formal parameter |
4054 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, | 4104 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, |
4055 // which says whether we need to create an arguments adaptor frame). | 4105 // which says whether we need to create an arguments adaptor frame). |
4056 if (formals.has_rest) arity--; | 4106 if (formals.has_rest) arity--; |
4057 | 4107 |
4058 // Determine if the function can be parsed lazily. Lazy parsing is different | 4108 // Determine if the function can be parsed lazily. Lazy parsing is different |
4059 // from lazy compilation; we need to parse more eagerly than we compile. | 4109 // from lazy compilation; we need to parse more eagerly than we compile. |
4060 | 4110 |
4061 // We can only parse lazily if we also compile lazily. The heuristics for | 4111 // We can only parse lazily if we also compile lazily. The heuristics for |
4062 // lazy compilation are: | 4112 // lazy compilation are: |
(...skipping 28 matching lines...) Expand all Loading... |
4091 bool is_lazily_parsed = mode() == PARSE_LAZILY && | 4141 bool is_lazily_parsed = mode() == PARSE_LAZILY && |
4092 scope_->AllowsLazyParsing() && | 4142 scope_->AllowsLazyParsing() && |
4093 !function_state_->this_function_is_parenthesized(); | 4143 !function_state_->this_function_is_parenthesized(); |
4094 | 4144 |
4095 // Eager or lazy parse? | 4145 // Eager or lazy parse? |
4096 // If is_lazily_parsed, we'll parse lazy. If we can set a bookmark, we'll | 4146 // If is_lazily_parsed, we'll parse lazy. If we can set a bookmark, we'll |
4097 // pass it to SkipLazyFunctionBody, which may use it to abort lazy | 4147 // pass it to SkipLazyFunctionBody, which may use it to abort lazy |
4098 // parsing if it suspect that wasn't a good idea. If so, or if we didn't | 4148 // parsing if it suspect that wasn't a good idea. If so, or if we didn't |
4099 // try to lazy parse in the first place, we'll have to parse eagerly. | 4149 // try to lazy parse in the first place, we'll have to parse eagerly. |
4100 Scanner::BookmarkScope bookmark(scanner()); | 4150 Scanner::BookmarkScope bookmark(scanner()); |
| 4151 function_state.set_parse_phase(FunctionParsePhase::FunctionBody); |
4101 if (is_lazily_parsed) { | 4152 if (is_lazily_parsed) { |
4102 Scanner::BookmarkScope* maybe_bookmark = | 4153 Scanner::BookmarkScope* maybe_bookmark = |
4103 bookmark.Set() ? &bookmark : nullptr; | 4154 bookmark.Set() ? &bookmark : nullptr; |
4104 SkipLazyFunctionBody(&materialized_literal_count, | 4155 SkipLazyFunctionBody(&materialized_literal_count, |
4105 &expected_property_count, /*CHECK_OK*/ ok, | 4156 &expected_property_count, /*CHECK_OK*/ ok, |
4106 maybe_bookmark); | 4157 maybe_bookmark); |
4107 | 4158 |
4108 materialized_literal_count += formals.materialized_literals_count + | 4159 materialized_literal_count += formals.materialized_literals_count + |
4109 function_state.materialized_literal_count(); | 4160 function_state.materialized_literal_count(); |
4110 | 4161 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4198 expected_property_count, arity, duplicate_parameters, function_type, | 4249 expected_property_count, arity, duplicate_parameters, function_type, |
4199 eager_compile_hint, kind, pos); | 4250 eager_compile_hint, kind, pos); |
4200 function_literal->set_function_token_position(function_token_pos); | 4251 function_literal->set_function_token_position(function_token_pos); |
4201 if (should_be_used_once_hint) | 4252 if (should_be_used_once_hint) |
4202 function_literal->set_should_be_used_once_hint(); | 4253 function_literal->set_should_be_used_once_hint(); |
4203 | 4254 |
4204 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); | 4255 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
4205 return function_literal; | 4256 return function_literal; |
4206 } | 4257 } |
4207 | 4258 |
| 4259 Expression* Parser::ParseAsyncFunctionExpression(bool* ok) { |
| 4260 // AsyncFunctionDeclaration :: |
| 4261 // async [no LineTerminator here] function ( FormalParameters[Await] ) |
| 4262 // { AsyncFunctionBody } |
| 4263 // |
| 4264 // async [no LineTerminator here] function BindingIdentifier[Await] |
| 4265 // ( FormalParameters[Await] ) { AsyncFunctionBody } |
| 4266 DCHECK_EQ(scanner()->current_token(), Token::ASYNC); |
| 4267 int pos = position(); |
| 4268 Expect(Token::FUNCTION, CHECK_OK); |
| 4269 bool is_strict_reserved = false; |
| 4270 const AstRawString* name = nullptr; |
| 4271 FunctionLiteral::FunctionType type = FunctionLiteral::kAnonymousExpression; |
| 4272 |
| 4273 if (peek_any_identifier()) { |
| 4274 type = FunctionLiteral::kNamedExpression; |
| 4275 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); |
| 4276 if (this->IsAwait(name)) { |
| 4277 ReportMessageAt(scanner()->location(), |
| 4278 MessageTemplate::kAwaitBindingIdentifier); |
| 4279 *ok = false; |
| 4280 return nullptr; |
| 4281 } |
| 4282 } |
| 4283 return ParseFunctionLiteral(name, scanner()->location(), |
| 4284 is_strict_reserved ? kFunctionNameIsStrictReserved |
| 4285 : kFunctionNameValidityUnknown, |
| 4286 FunctionKind::kAsyncFunction, pos, type, |
| 4287 language_mode(), CHECK_OK); |
| 4288 } |
4208 | 4289 |
4209 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, | 4290 void Parser::SkipLazyFunctionBody(int* materialized_literal_count, |
4210 int* expected_property_count, bool* ok, | 4291 int* expected_property_count, bool* ok, |
4211 Scanner::BookmarkScope* bookmark) { | 4292 Scanner::BookmarkScope* bookmark) { |
4212 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); | 4293 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet()); |
4213 if (produce_cached_parse_data()) CHECK(log_); | 4294 if (produce_cached_parse_data()) CHECK(log_); |
4214 | 4295 |
4215 int function_block_pos = position(); | 4296 int function_block_pos = position(); |
4216 if (consume_cached_parse_data() && !cached_parse_data_->rejected()) { | 4297 if (consume_cached_parse_data() && !cached_parse_data_->rejected()) { |
4217 // If we have cached data, we use it to skip parsing the function body. The | 4298 // If we have cached data, we use it to skip parsing the function body. The |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4597 NULL, stack_limit_); | 4678 NULL, stack_limit_); |
4598 reusable_preparser_->set_allow_lazy(true); | 4679 reusable_preparser_->set_allow_lazy(true); |
4599 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name()); | 4680 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name()); |
4600 SET_ALLOW(natives); | 4681 SET_ALLOW(natives); |
4601 SET_ALLOW(harmony_do_expressions); | 4682 SET_ALLOW(harmony_do_expressions); |
4602 SET_ALLOW(harmony_for_in); | 4683 SET_ALLOW(harmony_for_in); |
4603 SET_ALLOW(harmony_function_name); | 4684 SET_ALLOW(harmony_function_name); |
4604 SET_ALLOW(harmony_function_sent); | 4685 SET_ALLOW(harmony_function_sent); |
4605 SET_ALLOW(harmony_exponentiation_operator); | 4686 SET_ALLOW(harmony_exponentiation_operator); |
4606 SET_ALLOW(harmony_restrictive_declarations); | 4687 SET_ALLOW(harmony_restrictive_declarations); |
| 4688 SET_ALLOW(harmony_async_await); |
4607 #undef SET_ALLOW | 4689 #undef SET_ALLOW |
4608 } | 4690 } |
4609 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( | 4691 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( |
4610 language_mode(), function_state_->kind(), scope_->has_simple_parameters(), | 4692 language_mode(), function_state_->kind(), scope_->has_simple_parameters(), |
4611 parsing_module_, logger, bookmark, use_counts_); | 4693 parsing_module_, logger, bookmark, use_counts_); |
4612 if (pre_parse_timer_ != NULL) { | 4694 if (pre_parse_timer_ != NULL) { |
4613 pre_parse_timer_->Stop(); | 4695 pre_parse_timer_->Stop(); |
4614 } | 4696 } |
4615 return result; | 4697 return result; |
4616 } | 4698 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4667 FunctionLiteral* constructor = NULL; | 4749 FunctionLiteral* constructor = NULL; |
4668 bool has_seen_constructor = false; | 4750 bool has_seen_constructor = false; |
4669 | 4751 |
4670 Expect(Token::LBRACE, CHECK_OK); | 4752 Expect(Token::LBRACE, CHECK_OK); |
4671 | 4753 |
4672 const bool has_extends = extends != nullptr; | 4754 const bool has_extends = extends != nullptr; |
4673 while (peek() != Token::RBRACE) { | 4755 while (peek() != Token::RBRACE) { |
4674 if (Check(Token::SEMICOLON)) continue; | 4756 if (Check(Token::SEMICOLON)) continue; |
4675 FuncNameInferrer::State fni_state(fni_); | 4757 FuncNameInferrer::State fni_state(fni_); |
4676 const bool in_class = true; | 4758 const bool in_class = true; |
4677 const bool is_static = false; | |
4678 bool is_computed_name = false; // Classes do not care about computed | 4759 bool is_computed_name = false; // Classes do not care about computed |
4679 // property names here. | 4760 // property names here. |
4680 ExpressionClassifier property_classifier(this); | 4761 ExpressionClassifier property_classifier(this); |
4681 const AstRawString* property_name = nullptr; | 4762 const AstRawString* property_name = nullptr; |
4682 ObjectLiteral::Property* property = ParsePropertyDefinition( | 4763 ObjectLiteral::Property* property = ParsePropertyDefinition( |
4683 &checker, in_class, has_extends, is_static, &is_computed_name, | 4764 &checker, in_class, has_extends, MethodKind::Normal, &is_computed_name, |
4684 &has_seen_constructor, &property_classifier, &property_name, CHECK_OK); | 4765 &has_seen_constructor, &property_classifier, &property_name, CHECK_OK); |
4685 RewriteNonPattern(&property_classifier, CHECK_OK); | 4766 RewriteNonPattern(&property_classifier, CHECK_OK); |
4686 if (classifier != nullptr) { | 4767 if (classifier != nullptr) { |
4687 classifier->Accumulate(&property_classifier, | 4768 classifier->Accumulate(&property_classifier, |
4688 ExpressionClassifier::ExpressionProductions); | 4769 ExpressionClassifier::ExpressionProductions); |
4689 } | 4770 } |
4690 | 4771 |
4691 if (has_seen_constructor && constructor == NULL) { | 4772 if (has_seen_constructor && constructor == NULL) { |
4692 constructor = GetPropertyValue(property)->AsFunctionLiteral(); | 4773 constructor = GetPropertyValue(property)->AsFunctionLiteral(); |
4693 DCHECK_NOT_NULL(constructor); | 4774 DCHECK_NOT_NULL(constructor); |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5304 for (int i = 0; i < tail_call_expressions.length(); ++i) { | 5385 for (int i = 0; i < tail_call_expressions.length(); ++i) { |
5305 Expression* expression = tail_call_expressions[i]; | 5386 Expression* expression = tail_call_expressions[i]; |
5306 // If only FLAG_harmony_explicit_tailcalls is enabled then expression | 5387 // If only FLAG_harmony_explicit_tailcalls is enabled then expression |
5307 // must be a Call expression. | 5388 // must be a Call expression. |
5308 DCHECK(FLAG_harmony_tailcalls || !FLAG_harmony_explicit_tailcalls || | 5389 DCHECK(FLAG_harmony_tailcalls || !FLAG_harmony_explicit_tailcalls || |
5309 expression->IsCall()); | 5390 expression->IsCall()); |
5310 MarkTailPosition(expression); | 5391 MarkTailPosition(expression); |
5311 } | 5392 } |
5312 } | 5393 } |
5313 | 5394 |
| 5395 Expression* ParserTraits::ExpressionListToExpression( |
| 5396 ZoneList<Expression*>* args) { |
| 5397 AstNodeFactory* factory = parser_->factory(); |
| 5398 Expression* expr = args->at(0); |
| 5399 for (int i = 1; i < args->length(); ++i) { |
| 5400 expr = factory->NewBinaryOperation(Token::COMMA, expr, args->at(i), |
| 5401 expr->position()); |
| 5402 } |
| 5403 return expr; |
| 5404 } |
| 5405 |
5314 void ParserTraits::RewriteDestructuringAssignments() { | 5406 void ParserTraits::RewriteDestructuringAssignments() { |
5315 parser_->RewriteDestructuringAssignments(); | 5407 parser_->RewriteDestructuringAssignments(); |
5316 } | 5408 } |
5317 | 5409 |
5318 Expression* ParserTraits::RewriteExponentiation(Expression* left, | 5410 Expression* ParserTraits::RewriteExponentiation(Expression* left, |
5319 Expression* right, int pos) { | 5411 Expression* right, int pos) { |
5320 return parser_->RewriteExponentiation(left, right, pos); | 5412 return parser_->RewriteExponentiation(left, right, pos); |
5321 } | 5413 } |
5322 | 5414 |
5323 Expression* ParserTraits::RewriteAssignExponentiation(Expression* left, | 5415 Expression* ParserTraits::RewriteAssignExponentiation(Expression* left, |
5324 Expression* right, | 5416 Expression* right, |
5325 int pos) { | 5417 int pos) { |
5326 return parser_->RewriteAssignExponentiation(left, right, pos); | 5418 return parser_->RewriteAssignExponentiation(left, right, pos); |
5327 } | 5419 } |
5328 | 5420 |
5329 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier, | 5421 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier, |
5330 bool* ok) { | 5422 bool* ok) { |
5331 parser_->RewriteNonPattern(classifier, ok); | 5423 parser_->RewriteNonPattern(classifier, ok); |
5332 } | 5424 } |
5333 | 5425 |
| 5426 Expression* ParserTraits::RewriteAwaitExpression(Expression* value, int pos) { |
| 5427 // TODO(caitp): Implement AsyncFunctionAwait() |
| 5428 // per tc39.github.io/ecmascript-asyncawait/#abstract-ops-async-function-await |
| 5429 return value; |
| 5430 } |
5334 | 5431 |
5335 Zone* ParserTraits::zone() const { | 5432 Zone* ParserTraits::zone() const { |
5336 return parser_->function_state_->scope()->zone(); | 5433 return parser_->function_state_->scope()->zone(); |
5337 } | 5434 } |
5338 | 5435 |
5339 | 5436 |
5340 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const { | 5437 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const { |
5341 return parser_->function_state_->non_patterns_to_rewrite(); | 5438 return parser_->function_state_->non_patterns_to_rewrite(); |
5342 } | 5439 } |
5343 | 5440 |
(...skipping 1472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6816 try_block, target); | 6913 try_block, target); |
6817 final_loop = target; | 6914 final_loop = target; |
6818 } | 6915 } |
6819 | 6916 |
6820 return final_loop; | 6917 return final_loop; |
6821 } | 6918 } |
6822 | 6919 |
6823 | 6920 |
6824 } // namespace internal | 6921 } // namespace internal |
6825 } // namespace v8 | 6922 } // namespace v8 |
OLD | NEW |