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 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 int function_token_position, FunctionLiteral::FunctionType type, | 752 int function_token_position, FunctionLiteral::FunctionType type, |
753 LanguageMode language_mode, typesystem::TypeFlags type_flags, bool* ok) { | 753 LanguageMode language_mode, typesystem::TypeFlags type_flags, bool* ok) { |
754 return parser_->ParseFunctionLiteral( | 754 return parser_->ParseFunctionLiteral( |
755 name, function_name_location, function_name_validity, kind, | 755 name, function_name_location, function_name_validity, kind, |
756 function_token_position, type, language_mode, type_flags, ok); | 756 function_token_position, type, language_mode, type_flags, ok); |
757 } | 757 } |
758 | 758 |
759 | 759 |
760 ClassLiteral* ParserTraits::ParseClassLiteral( | 760 ClassLiteral* ParserTraits::ParseClassLiteral( |
761 const AstRawString* name, Scanner::Location class_name_location, | 761 const AstRawString* name, Scanner::Location class_name_location, |
762 bool name_is_strict_reserved, int pos, bool* ok) { | 762 bool name_is_strict_reserved, int pos, bool ambient, bool* ok) { |
763 return parser_->ParseClassLiteral(name, class_name_location, | 763 return parser_->ParseClassLiteral(name, class_name_location, |
764 name_is_strict_reserved, pos, ok); | 764 name_is_strict_reserved, pos, ambient, ok); |
765 } | 765 } |
766 | 766 |
767 | 767 |
768 Parser::Parser(ParseInfo* info) | 768 Parser::Parser(ParseInfo* info) |
769 : ParserBase<ParserTraits>(info->zone(), &scanner_, info->stack_limit(), | 769 : ParserBase<ParserTraits>(info->zone(), &scanner_, info->stack_limit(), |
770 info->extension(), info->ast_value_factory(), | 770 info->extension(), info->ast_value_factory(), |
771 NULL, this), | 771 NULL, this), |
772 scanner_(info->unicode_cache()), | 772 scanner_(info->unicode_cache()), |
773 reusable_preparser_(NULL), | 773 reusable_preparser_(NULL), |
774 original_scope_(NULL), | 774 original_scope_(NULL), |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 // Don't count the mode in the use counters--give the program a chance | 925 // Don't count the mode in the use counters--give the program a chance |
926 // to enable script/module-wide strict mode below. | 926 // to enable script/module-wide strict mode below. |
927 scope_->SetLanguageMode(info->language_mode()); | 927 scope_->SetLanguageMode(info->language_mode()); |
928 if (info->is_typed()) scope_->SetTyped(); | 928 if (info->is_typed()) scope_->SetTyped(); |
929 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); | 929 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); |
930 bool ok = true; | 930 bool ok = true; |
931 int beg_pos = scanner()->location().beg_pos; | 931 int beg_pos = scanner()->location().beg_pos; |
932 if (info->is_module()) { | 932 if (info->is_module()) { |
933 ParseModuleItemList(body, &ok); | 933 ParseModuleItemList(body, &ok); |
934 } else { | 934 } else { |
935 ParseStatementList(body, Token::EOS, &ok); | 935 ParseStatementList(body, Token::EOS, true, &ok); |
936 } | 936 } |
937 | 937 |
938 // The parser will peek but not consume EOS. Our scope logically goes all | 938 // The parser will peek but not consume EOS. Our scope logically goes all |
939 // the way to the EOS, though. | 939 // the way to the EOS, though. |
940 scope->set_end_position(scanner()->peek_location().beg_pos); | 940 scope->set_end_position(scanner()->peek_location().beg_pos); |
941 | 941 |
942 if (ok && is_strict(language_mode())) { | 942 if (ok && is_strict(language_mode())) { |
943 CheckStrictOctalLiteral(beg_pos, scanner()->location().end_pos, &ok); | 943 CheckStrictOctalLiteral(beg_pos, scanner()->location().end_pos, &ok); |
944 } | 944 } |
945 if (ok && is_sloppy(language_mode()) && allow_harmony_sloppy_function()) { | 945 if (ok && is_sloppy(language_mode()) && allow_harmony_sloppy_function()) { |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1150 | 1150 |
1151 if (result != NULL) { | 1151 if (result != NULL) { |
1152 Handle<String> inferred_name(shared_info->inferred_name()); | 1152 Handle<String> inferred_name(shared_info->inferred_name()); |
1153 result->set_inferred_name(inferred_name); | 1153 result->set_inferred_name(inferred_name); |
1154 } | 1154 } |
1155 return result; | 1155 return result; |
1156 } | 1156 } |
1157 | 1157 |
1158 | 1158 |
1159 void* Parser::ParseStatementList(ZoneList<Statement*>* body, int end_token, | 1159 void* Parser::ParseStatementList(ZoneList<Statement*>* body, int end_token, |
1160 bool* ok) { | 1160 bool top_level, bool* ok) { |
1161 // StatementList :: | 1161 // StatementList :: |
1162 // (StatementListItem)* <end_token> | 1162 // (StatementListItem)* <end_token> |
1163 | 1163 |
1164 // Allocate a target stack to use for this set of source | 1164 // Allocate a target stack to use for this set of source |
1165 // elements. This way, all scripts and functions get their own | 1165 // elements. This way, all scripts and functions get their own |
1166 // target stack thus avoiding illegal breaks and continues across | 1166 // target stack thus avoiding illegal breaks and continues across |
1167 // functions. | 1167 // functions. |
1168 TargetScope scope(&this->target_stack_); | 1168 TargetScope scope(&this->target_stack_); |
1169 | 1169 |
1170 DCHECK(body != NULL); | 1170 DCHECK(body != NULL); |
1171 bool directive_prologue = true; // Parsing directive prologue. | 1171 bool directive_prologue = true; // Parsing directive prologue. |
1172 | 1172 |
1173 while (peek() != end_token) { | 1173 while (peek() != end_token) { |
1174 if (directive_prologue && peek() != Token::STRING) { | 1174 if (directive_prologue && peek() != Token::STRING) { |
1175 directive_prologue = false; | 1175 directive_prologue = false; |
1176 } | 1176 } |
1177 | 1177 |
1178 Scanner::Location token_loc = scanner()->peek_location(); | 1178 Scanner::Location token_loc = scanner()->peek_location(); |
1179 Statement* stat = ParseStatementListItem(CHECK_OK); | 1179 Statement* stat = ParseStatementListItem(top_level, CHECK_OK); |
1180 if (stat == NULL || stat->IsEmpty()) { | 1180 if (stat == NULL || stat->IsEmpty()) { |
1181 directive_prologue = false; // End of directive prologue. | 1181 directive_prologue = false; // End of directive prologue. |
1182 continue; | 1182 continue; |
1183 } | 1183 } |
1184 | 1184 |
1185 if (directive_prologue) { | 1185 if (directive_prologue) { |
1186 // A shot at a directive. | 1186 // A shot at a directive. |
1187 ExpressionStatement* e_stat; | 1187 ExpressionStatement* e_stat; |
1188 Literal* literal; | 1188 Literal* literal; |
1189 // Still processing directive prologue? | 1189 // Still processing directive prologue? |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1259 RaiseLanguageMode(SLOPPY); | 1259 RaiseLanguageMode(SLOPPY); |
1260 } | 1260 } |
1261 | 1261 |
1262 body->Add(stat, zone()); | 1262 body->Add(stat, zone()); |
1263 } | 1263 } |
1264 | 1264 |
1265 return 0; | 1265 return 0; |
1266 } | 1266 } |
1267 | 1267 |
1268 | 1268 |
1269 Statement* Parser::ParseStatementListItem(bool* ok) { | 1269 Statement* Parser::ParseStatementListItem(bool top_level, bool* ok) { |
1270 // (Ecma 262 6th Edition, 13.1): | 1270 // (Ecma 262 6th Edition, 13.1): |
1271 // StatementListItem: | 1271 // StatementListItem: |
1272 // Statement | 1272 // Statement |
1273 // Declaration | 1273 // Declaration |
1274 | 1274 |
| 1275 // Allow ambient variable, function, and class declarations. |
| 1276 bool ambient = |
| 1277 scope_->typed() && CheckContextualKeyword(CStrVector("declare")); |
| 1278 if (ambient && !top_level) { |
| 1279 *ok = false; |
| 1280 ReportMessage(MessageTemplate::kIllegalDeclare); |
| 1281 return nullptr; |
| 1282 } |
1275 switch (peek()) { | 1283 switch (peek()) { |
1276 case Token::FUNCTION: | 1284 case Token::FUNCTION: |
1277 return ParseFunctionDeclaration(NULL, ok); | 1285 return ParseFunctionDeclaration(NULL, ambient, ok); |
1278 case Token::CLASS: | 1286 case Token::CLASS: |
1279 Consume(Token::CLASS); | 1287 Consume(Token::CLASS); |
1280 return ParseClassDeclaration(NULL, ok); | 1288 return ParseClassDeclaration(NULL, ambient, ok); |
1281 case Token::CONST: | 1289 case Token::CONST: |
1282 if (allow_const()) { | 1290 if (allow_const()) { |
1283 return ParseVariableStatement(kStatementListItem, NULL, ok); | 1291 return ParseVariableStatement(kStatementListItem, NULL, ambient, ok); |
1284 } | 1292 } |
1285 break; | 1293 break; |
1286 case Token::VAR: | 1294 case Token::VAR: |
1287 return ParseVariableStatement(kStatementListItem, NULL, ok); | 1295 return ParseVariableStatement(kStatementListItem, NULL, ambient, ok); |
1288 case Token::LET: | 1296 case Token::LET: |
1289 if (IsNextLetKeyword()) { | 1297 if (IsNextLetKeyword()) { |
1290 return ParseVariableStatement(kStatementListItem, NULL, ok); | 1298 return ParseVariableStatement(kStatementListItem, NULL, ambient, ok); |
1291 } | 1299 } |
1292 break; | 1300 break; |
1293 case Token::IDENTIFIER: | 1301 case Token::IDENTIFIER: |
1294 case Token::FUTURE_STRICT_RESERVED_WORD: { | 1302 case Token::FUTURE_STRICT_RESERVED_WORD: { |
1295 if (!scope_->typed()) break; | 1303 if (!scope_->typed() || ambient) break; |
1296 int pos = peek_position(); | 1304 int pos = peek_position(); |
1297 if (CheckContextualKeyword(CStrVector("type"))) { | 1305 if (CheckContextualKeyword(CStrVector("type"))) { |
1298 return ParseTypeAliasDeclaration(pos, ok); | 1306 return ParseTypeAliasDeclaration(pos, ok); |
1299 } else if (CheckContextualKeyword(CStrVector("interface"))) { | 1307 } else if (CheckContextualKeyword(CStrVector("interface"))) { |
1300 return ParseInterfaceDeclaration(pos, ok); | 1308 return ParseInterfaceDeclaration(pos, ok); |
1301 } | 1309 } |
1302 break; | 1310 break; |
1303 } | 1311 } |
1304 // TODO(nikolaos): ambient | |
1305 default: | 1312 default: |
1306 break; | 1313 break; |
1307 } | 1314 } |
| 1315 if (ambient) { |
| 1316 *ok = false; |
| 1317 ReportMessageAt(scanner()->peek_location(), |
| 1318 MessageTemplate::kBadAmbientDeclaration); |
| 1319 return nullptr; |
| 1320 } |
1308 return ParseStatement(NULL, kAllowLabelledFunctionStatement, ok); | 1321 return ParseStatement(NULL, kAllowLabelledFunctionStatement, ok); |
1309 } | 1322 } |
1310 | 1323 |
1311 | 1324 |
1312 Statement* Parser::ParseModuleItem(bool* ok) { | 1325 Statement* Parser::ParseModuleItem(bool* ok) { |
1313 // (Ecma 262 6th Edition, 15.2): | 1326 // (Ecma 262 6th Edition, 15.2): |
1314 // ModuleItem : | 1327 // ModuleItem : |
1315 // ImportDeclaration | 1328 // ImportDeclaration |
1316 // ExportDeclaration | 1329 // ExportDeclaration |
1317 // StatementListItem | 1330 // StatementListItem |
1318 | 1331 |
1319 switch (peek()) { | 1332 switch (peek()) { |
1320 case Token::IMPORT: | 1333 case Token::IMPORT: |
1321 return ParseImportDeclaration(ok); | 1334 return ParseImportDeclaration(ok); |
1322 case Token::EXPORT: | 1335 case Token::EXPORT: |
1323 return ParseExportDeclaration(ok); | 1336 return ParseExportDeclaration(ok); |
1324 default: | 1337 default: |
1325 return ParseStatementListItem(ok); | 1338 return ParseStatementListItem(true, ok); |
1326 } | 1339 } |
1327 } | 1340 } |
1328 | 1341 |
1329 | 1342 |
1330 void* Parser::ParseModuleItemList(ZoneList<Statement*>* body, bool* ok) { | 1343 void* Parser::ParseModuleItemList(ZoneList<Statement*>* body, bool* ok) { |
1331 // (Ecma 262 6th Edition, 15.2): | 1344 // (Ecma 262 6th Edition, 15.2): |
1332 // Module : | 1345 // Module : |
1333 // ModuleBody? | 1346 // ModuleBody? |
1334 // | 1347 // |
1335 // ModuleBody : | 1348 // ModuleBody : |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1606 // 'export' 'default' ClassDeclaration | 1619 // 'export' 'default' ClassDeclaration |
1607 // 'export' 'default' AssignmentExpression[In] ';' | 1620 // 'export' 'default' AssignmentExpression[In] ';' |
1608 | 1621 |
1609 Expect(Token::DEFAULT, CHECK_OK); | 1622 Expect(Token::DEFAULT, CHECK_OK); |
1610 Scanner::Location default_loc = scanner()->location(); | 1623 Scanner::Location default_loc = scanner()->location(); |
1611 | 1624 |
1612 const AstRawString* default_string = ast_value_factory()->default_string(); | 1625 const AstRawString* default_string = ast_value_factory()->default_string(); |
1613 ZoneList<const AstRawString*> names(1, zone()); | 1626 ZoneList<const AstRawString*> names(1, zone()); |
1614 Statement* result = nullptr; | 1627 Statement* result = nullptr; |
1615 Expression* default_export = nullptr; | 1628 Expression* default_export = nullptr; |
| 1629 |
| 1630 // Allow ambient function and class declarations to be exported as default. |
| 1631 int ambient_pos = peek_position(); |
| 1632 bool ambient = |
| 1633 scope_->typed() && CheckContextualKeyword(CStrVector("declare")); |
| 1634 |
1616 switch (peek()) { | 1635 switch (peek()) { |
1617 case Token::FUNCTION: { | 1636 case Token::FUNCTION: { |
1618 Consume(Token::FUNCTION); | 1637 Consume(Token::FUNCTION); |
1619 int pos = position(); | 1638 int pos = position(); |
1620 bool is_generator = Check(Token::MUL); | 1639 bool is_generator = Check(Token::MUL); |
1621 if (peek() == Token::LPAREN || (scope_->typed() && Check(Token::LT))) { | 1640 if (peek() == Token::LPAREN || (scope_->typed() && peek() == Token::LT)) { |
1622 // FunctionDeclaration[+Default] :: | 1641 // FunctionDeclaration[+Default] :: |
1623 // 'function' '(' FormalParameters ')' '{' FunctionBody '}' | 1642 // 'function' '(' FormalParameters ')' '{' FunctionBody '}' |
1624 // | 1643 // |
1625 // GeneratorDeclaration[+Default] :: | 1644 // GeneratorDeclaration[+Default] :: |
1626 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' | 1645 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' |
1627 default_export = ParseFunctionLiteral( | 1646 typesystem::TypeFlags type_flags = |
1628 default_string, Scanner::Location::invalid(), | 1647 ambient ? typesystem::kAmbient : typesystem::kAllowSignature; |
1629 kSkipFunctionNameCheck, | 1648 default_export = |
1630 is_generator ? FunctionKind::kGeneratorFunction | 1649 ParseFunctionLiteral(default_string, Scanner::Location::invalid(), |
1631 : FunctionKind::kNormalFunction, | 1650 kSkipFunctionNameCheck, |
1632 pos, FunctionLiteral::kDeclaration, language_mode(), | 1651 is_generator ? FunctionKind::kGeneratorFunction |
1633 typesystem::kAllowSignature, CHECK_OK); | 1652 : FunctionKind::kNormalFunction, |
| 1653 pos, FunctionLiteral::kDeclaration, |
| 1654 language_mode(), type_flags, CHECK_OK); |
1634 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); | 1655 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); |
1635 } else { | 1656 } else { |
1636 result = ParseFunctionDeclaration(pos, is_generator, &names, CHECK_OK); | 1657 result = ParseFunctionDeclaration(pos, is_generator, &names, ambient, |
| 1658 CHECK_OK); |
1637 } | 1659 } |
1638 break; | 1660 break; |
1639 } | 1661 } |
1640 | 1662 |
1641 case Token::CLASS: | 1663 case Token::CLASS: |
1642 Consume(Token::CLASS); | 1664 Consume(Token::CLASS); |
1643 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) { | 1665 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) { |
1644 // ClassDeclaration[+Default] :: | 1666 // ClassDeclaration[+Default] :: |
1645 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' | 1667 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' |
1646 default_export = | 1668 default_export = |
1647 ParseClassLiteral(default_string, Scanner::Location::invalid(), | 1669 ParseClassLiteral(default_string, Scanner::Location::invalid(), |
1648 false, position(), CHECK_OK); | 1670 false, position(), ambient, CHECK_OK); |
1649 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); | 1671 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); |
1650 } else { | 1672 } else { |
1651 result = ParseClassDeclaration(&names, CHECK_OK); | 1673 result = ParseClassDeclaration(&names, ambient, CHECK_OK); |
1652 } | 1674 } |
1653 break; | 1675 break; |
1654 | 1676 |
1655 default: { | 1677 default: { |
| 1678 if (ambient) { |
| 1679 *ok = false; |
| 1680 ReportMessageAt(scanner()->peek_location(), |
| 1681 MessageTemplate::kBadAmbientDeclaration); |
| 1682 return nullptr; |
| 1683 } |
| 1684 |
1656 int pos = peek_position(); | 1685 int pos = peek_position(); |
1657 ExpressionClassifier classifier(this); | 1686 ExpressionClassifier classifier(this); |
1658 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK); | 1687 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK); |
1659 RewriteNonPattern(&classifier, CHECK_OK); | 1688 RewriteNonPattern(&classifier, CHECK_OK); |
1660 | 1689 |
1661 ExpectSemicolon(CHECK_OK); | 1690 ExpectSemicolon(CHECK_OK); |
1662 result = factory()->NewExpressionStatement(expr, pos); | 1691 result = factory()->NewExpressionStatement(expr, pos); |
1663 break; | 1692 break; |
1664 } | 1693 } |
1665 } | 1694 } |
1666 | 1695 |
| 1696 // Exported ambients are not checked. |
| 1697 if (ambient) return factory()->NewEmptyStatement(ambient_pos); |
| 1698 |
1667 DCHECK_LE(names.length(), 1); | 1699 DCHECK_LE(names.length(), 1); |
1668 if (names.length() == 1) { | 1700 if (names.length() == 1) { |
1669 scope_->module()->AddLocalExport(default_string, names.first(), zone(), ok); | 1701 scope_->module()->AddLocalExport(default_string, names.first(), zone(), ok); |
1670 if (!*ok) { | 1702 if (!*ok) { |
1671 ParserTraits::ReportMessageAt( | 1703 ParserTraits::ReportMessageAt( |
1672 default_loc, MessageTemplate::kDuplicateExport, default_string); | 1704 default_loc, MessageTemplate::kDuplicateExport, default_string); |
1673 return nullptr; | 1705 return nullptr; |
1674 } | 1706 } |
1675 } else { | 1707 } else { |
1676 // TODO(ES6): Assign result to a const binding with the name "*default*" | 1708 // TODO(ES6): Assign result to a const binding with the name "*default*" |
1677 // and add an export entry with "*default*" as the local name. | 1709 // and add an export entry with "*default*" as the local name. |
1678 USE(default_export); | 1710 USE(default_export); |
1679 } | 1711 } |
1680 | 1712 |
1681 return result; | 1713 return result; |
1682 } | 1714 } |
1683 | 1715 |
1684 | 1716 |
1685 Statement* Parser::ParseExportDeclaration(bool* ok) { | 1717 Statement* Parser::ParseExportDeclaration(bool* ok) { |
1686 // ExportDeclaration: | 1718 // ExportDeclaration: |
1687 // 'export' '*' 'from' ModuleSpecifier ';' | 1719 // 'export' '*' 'from' ModuleSpecifier ';' |
1688 // 'export' ExportClause ('from' ModuleSpecifier)? ';' | 1720 // 'export' ExportClause ('from' ModuleSpecifier)? ';' |
1689 // 'export' VariableStatement | 1721 // 'export' VariableStatement |
1690 // 'export' Declaration | 1722 // 'export' Declaration |
1691 // 'export' 'default' ... (handled in ParseExportDefault) | 1723 // 'export' 'default' ... (handled in ParseExportDefault) |
1692 | 1724 |
1693 int pos = peek_position(); | 1725 int pos = peek_position(); |
1694 Expect(Token::EXPORT, CHECK_OK); | 1726 Expect(Token::EXPORT, CHECK_OK); |
1695 | 1727 |
| 1728 // Allow exported ambient variable, function, and class declarations. |
| 1729 bool ambient = |
| 1730 scope_->typed() && CheckContextualKeyword(CStrVector("declare")); |
| 1731 |
1696 Statement* result = NULL; | 1732 Statement* result = NULL; |
1697 ZoneList<const AstRawString*> names(1, zone()); | 1733 ZoneList<const AstRawString*> names(1, zone()); |
| 1734 if (ambient && (peek() == Token::DEFAULT || peek() == Token::MUL || |
| 1735 peek() == Token::LBRACE)) { |
| 1736 *ok = false; |
| 1737 ReportMessageAt(scanner()->peek_location(), |
| 1738 MessageTemplate::kBadAmbientDeclaration); |
| 1739 return nullptr; |
| 1740 } |
1698 switch (peek()) { | 1741 switch (peek()) { |
1699 case Token::DEFAULT: | 1742 case Token::DEFAULT: |
1700 return ParseExportDefault(ok); | 1743 return ParseExportDefault(ok); |
1701 | 1744 |
1702 case Token::MUL: { | 1745 case Token::MUL: { |
1703 Consume(Token::MUL); | 1746 Consume(Token::MUL); |
1704 ExpectContextualKeyword(CStrVector("from"), CHECK_OK); | 1747 ExpectContextualKeyword(CStrVector("from"), CHECK_OK); |
1705 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK); | 1748 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK); |
1706 scope_->module()->AddModuleRequest(module_specifier, zone()); | 1749 scope_->module()->AddModuleRequest(module_specifier, zone()); |
1707 // TODO(ES6): scope_->module()->AddStarExport(...) | 1750 // TODO(ES6): scope_->module()->AddStarExport(...) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1755 scope_->module()->AddModuleRequest(indirect_export_module_specifier, | 1798 scope_->module()->AddModuleRequest(indirect_export_module_specifier, |
1756 zone()); | 1799 zone()); |
1757 for (int i = 0; i < length; ++i) { | 1800 for (int i = 0; i < length; ++i) { |
1758 // TODO(ES6): scope_->module()->AddIndirectExport(...);( | 1801 // TODO(ES6): scope_->module()->AddIndirectExport(...);( |
1759 } | 1802 } |
1760 } | 1803 } |
1761 return factory()->NewEmptyStatement(pos); | 1804 return factory()->NewEmptyStatement(pos); |
1762 } | 1805 } |
1763 | 1806 |
1764 case Token::FUNCTION: | 1807 case Token::FUNCTION: |
1765 result = ParseFunctionDeclaration(&names, CHECK_OK); | 1808 result = ParseFunctionDeclaration(&names, ambient, CHECK_OK); |
1766 break; | 1809 break; |
1767 | 1810 |
1768 case Token::CLASS: | 1811 case Token::CLASS: |
1769 Consume(Token::CLASS); | 1812 Consume(Token::CLASS); |
1770 result = ParseClassDeclaration(&names, CHECK_OK); | 1813 result = ParseClassDeclaration(&names, ambient, CHECK_OK); |
1771 break; | 1814 break; |
1772 | 1815 |
1773 case Token::VAR: | 1816 case Token::VAR: |
1774 case Token::LET: | 1817 case Token::LET: |
1775 case Token::CONST: | 1818 case Token::CONST: |
1776 result = ParseVariableStatement(kStatementListItem, &names, CHECK_OK); | 1819 result = |
| 1820 ParseVariableStatement(kStatementListItem, &names, ambient, CHECK_OK); |
1777 break; | 1821 break; |
1778 | 1822 |
1779 default: | 1823 default: |
1780 *ok = false; | 1824 *ok = false; |
1781 ReportUnexpectedToken(scanner()->current_token()); | 1825 ReportUnexpectedToken(scanner()->current_token()); |
1782 return NULL; | 1826 return NULL; |
1783 } | 1827 } |
1784 | 1828 |
| 1829 // Exported ambients are not checked. |
| 1830 if (ambient) return factory()->NewEmptyStatement(pos); |
| 1831 |
1785 // Extract declared names into export declarations. | 1832 // Extract declared names into export declarations. |
1786 ModuleDescriptor* descriptor = scope_->module(); | 1833 ModuleDescriptor* descriptor = scope_->module(); |
1787 for (int i = 0; i < names.length(); ++i) { | 1834 for (int i = 0; i < names.length(); ++i) { |
1788 descriptor->AddLocalExport(names[i], names[i], zone(), ok); | 1835 descriptor->AddLocalExport(names[i], names[i], zone(), ok); |
1789 if (!*ok) { | 1836 if (!*ok) { |
1790 // TODO(adamk): Possibly report this error at the right place. | 1837 // TODO(adamk): Possibly report this error at the right place. |
1791 ParserTraits::ReportMessage(MessageTemplate::kDuplicateExport, names[i]); | 1838 ParserTraits::ReportMessage(MessageTemplate::kDuplicateExport, names[i]); |
1792 return NULL; | 1839 return NULL; |
1793 } | 1840 } |
1794 } | 1841 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1892 is_strict(language_mode()) | 1939 is_strict(language_mode()) |
1893 ? MessageTemplate::kStrictFunction | 1940 ? MessageTemplate::kStrictFunction |
1894 : MessageTemplate::kSloppyFunction); | 1941 : MessageTemplate::kSloppyFunction); |
1895 *ok = false; | 1942 *ok = false; |
1896 return nullptr; | 1943 return nullptr; |
1897 | 1944 |
1898 case Token::DEBUGGER: | 1945 case Token::DEBUGGER: |
1899 return ParseDebuggerStatement(ok); | 1946 return ParseDebuggerStatement(ok); |
1900 | 1947 |
1901 case Token::VAR: | 1948 case Token::VAR: |
1902 return ParseVariableStatement(kStatement, NULL, ok); | 1949 return ParseVariableStatement(kStatement, NULL, false, ok); |
1903 | 1950 |
1904 default: | 1951 default: |
1905 return ParseExpressionOrLabelledStatement(labels, allow_function, ok); | 1952 return ParseExpressionOrLabelledStatement(labels, allow_function, ok); |
1906 } | 1953 } |
1907 } | 1954 } |
1908 | 1955 |
1909 Statement* Parser::ParseStatementAsUnlabelled( | 1956 Statement* Parser::ParseStatementAsUnlabelled( |
1910 ZoneList<const AstRawString*>* labels, bool* ok) { | 1957 ZoneList<const AstRawString*>* labels, bool* ok) { |
1911 switch (peek()) { | 1958 switch (peek()) { |
1912 case Token::CONTINUE: | 1959 case Token::CONTINUE: |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2135 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 2182 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); |
2136 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral( | 2183 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral( |
2137 name, extension_, RelocInfo::kNoPosition); | 2184 name, extension_, RelocInfo::kNoPosition); |
2138 return factory()->NewExpressionStatement( | 2185 return factory()->NewExpressionStatement( |
2139 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition), | 2186 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition), |
2140 pos); | 2187 pos); |
2141 } | 2188 } |
2142 | 2189 |
2143 | 2190 |
2144 Statement* Parser::ParseFunctionDeclaration( | 2191 Statement* Parser::ParseFunctionDeclaration( |
2145 ZoneList<const AstRawString*>* names, bool* ok) { | 2192 ZoneList<const AstRawString*>* names, bool ambient, bool* ok) { |
2146 Expect(Token::FUNCTION, CHECK_OK); | 2193 Expect(Token::FUNCTION, CHECK_OK); |
2147 int pos = position(); | 2194 int pos = position(); |
2148 bool is_generator = Check(Token::MUL); | 2195 bool is_generator = Check(Token::MUL); |
2149 return ParseFunctionDeclaration(pos, is_generator, names, ok); | 2196 return ParseFunctionDeclaration(pos, is_generator, names, ambient, ok); |
2150 } | 2197 } |
2151 | 2198 |
2152 | 2199 |
2153 Statement* Parser::ParseFunctionDeclaration( | 2200 Statement* Parser::ParseFunctionDeclaration( |
2154 int pos, bool is_generator, ZoneList<const AstRawString*>* names, | 2201 int pos, bool is_generator, ZoneList<const AstRawString*>* names, |
2155 bool* ok) { | 2202 bool ambient, bool* ok) { |
2156 // FunctionDeclaration :: | 2203 // FunctionDeclaration :: |
2157 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}' | 2204 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}' |
2158 // GeneratorDeclaration :: | 2205 // GeneratorDeclaration :: |
2159 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}' | 2206 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}' |
2160 // | 2207 // |
2161 // 'function' and '*' (if present) have been consumed by the caller. | 2208 // 'function' and '*' (if present) have been consumed by the caller. |
2162 bool is_strict_reserved = false; | 2209 bool is_strict_reserved = false; |
2163 const AstRawString* name = ParseIdentifierOrStrictReservedWord( | 2210 const AstRawString* name = ParseIdentifierOrStrictReservedWord( |
2164 &is_strict_reserved, CHECK_OK); | 2211 &is_strict_reserved, CHECK_OK); |
2165 | 2212 |
2166 FuncNameInferrer::State fni_state(fni_); | 2213 FuncNameInferrer::State fni_state(fni_); |
2167 if (fni_ != NULL) fni_->PushEnclosingName(name); | 2214 if (fni_ != NULL) fni_->PushEnclosingName(name); |
| 2215 typesystem::TypeFlags type_flags = |
| 2216 ambient ? typesystem::kAmbient : typesystem::kAllowSignature; |
2168 FunctionLiteral* fun = | 2217 FunctionLiteral* fun = |
2169 ParseFunctionLiteral(name, scanner()->location(), | 2218 ParseFunctionLiteral(name, scanner()->location(), |
2170 is_strict_reserved ? kFunctionNameIsStrictReserved | 2219 is_strict_reserved ? kFunctionNameIsStrictReserved |
2171 : kFunctionNameValidityUnknown, | 2220 : kFunctionNameValidityUnknown, |
2172 is_generator ? FunctionKind::kGeneratorFunction | 2221 is_generator ? FunctionKind::kGeneratorFunction |
2173 : FunctionKind::kNormalFunction, | 2222 : FunctionKind::kNormalFunction, |
2174 pos, FunctionLiteral::kDeclaration, language_mode(), | 2223 pos, FunctionLiteral::kDeclaration, language_mode(), |
2175 typesystem::kAllowSignature, CHECK_OK); | 2224 type_flags, CHECK_OK); |
2176 // Return no function declaration if just the signature was given. | 2225 // Return no function declaration if just the signature was given. |
2177 EmptyStatement* empty = factory()->NewEmptyStatement(RelocInfo::kNoPosition); | 2226 EmptyStatement* empty = factory()->NewEmptyStatement(RelocInfo::kNoPosition); |
2178 if (fun == nullptr) return empty; | 2227 if (fun == nullptr) return empty; |
2179 | 2228 |
2180 // Even if we're not at the top-level of the global or a function | 2229 // Even if we're not at the top-level of the global or a function |
2181 // scope, we treat it as such and introduce the function with its | 2230 // scope, we treat it as such and introduce the function with its |
2182 // initial value upon entering the corresponding scope. | 2231 // initial value upon entering the corresponding scope. |
2183 // In ES6, a function behaves as a lexical binding, except in | 2232 // In ES6, a function behaves as a lexical binding, except in |
2184 // a script scope, or the initial scope of eval or another function. | 2233 // a script scope, or the initial scope of eval or another function. |
2185 VariableMode mode = | 2234 VariableMode mode = |
(...skipping 12 matching lines...) Expand all Loading... |
2198 factory()->NewSloppyBlockFunctionStatement(empty, scope_); | 2247 factory()->NewSloppyBlockFunctionStatement(empty, scope_); |
2199 scope_->DeclarationScope()->sloppy_block_function_map()->Declare(name, | 2248 scope_->DeclarationScope()->sloppy_block_function_map()->Declare(name, |
2200 delegate); | 2249 delegate); |
2201 return delegate; | 2250 return delegate; |
2202 } | 2251 } |
2203 return empty; | 2252 return empty; |
2204 } | 2253 } |
2205 | 2254 |
2206 | 2255 |
2207 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, | 2256 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, |
2208 bool* ok) { | 2257 bool ambient, bool* ok) { |
2209 // ClassDeclaration :: | 2258 // ClassDeclaration :: |
2210 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}' | 2259 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}' |
2211 // | 2260 // |
2212 // 'class' is expected to be consumed by the caller. | 2261 // 'class' is expected to be consumed by the caller. |
2213 // | 2262 // |
2214 // A ClassDeclaration | 2263 // A ClassDeclaration |
2215 // | 2264 // |
2216 // class C { ... } | 2265 // class C { ... } |
2217 // | 2266 // |
2218 // has the same semantics as: | 2267 // has the same semantics as: |
2219 // | 2268 // |
2220 // let C = class C { ... }; | 2269 // let C = class C { ... }; |
2221 // | 2270 // |
2222 // so rewrite it as such. | 2271 // so rewrite it as such. |
2223 | 2272 |
2224 if (!allow_harmony_sloppy() && is_sloppy(language_mode())) { | 2273 if (!allow_harmony_sloppy() && is_sloppy(language_mode())) { |
2225 ReportMessage(MessageTemplate::kSloppyLexical); | 2274 ReportMessage(MessageTemplate::kSloppyLexical); |
2226 *ok = false; | 2275 *ok = false; |
2227 return NULL; | 2276 return NULL; |
2228 } | 2277 } |
2229 | 2278 |
2230 int pos = position(); | 2279 int pos = position(); |
2231 bool is_strict_reserved = false; | 2280 bool is_strict_reserved = false; |
2232 const AstRawString* name = | 2281 const AstRawString* name = |
2233 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); | 2282 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); |
2234 ClassLiteral* value = ParseClassLiteral(name, scanner()->location(), | 2283 ClassLiteral* value = ParseClassLiteral( |
2235 is_strict_reserved, pos, CHECK_OK); | 2284 name, scanner()->location(), is_strict_reserved, pos, ambient, CHECK_OK); |
| 2285 // Return no class declaration in case of an ambient. |
| 2286 if (ambient) return factory()->NewEmptyStatement(RelocInfo::kNoPosition); |
2236 | 2287 |
2237 VariableProxy* proxy = NewUnresolved(name, LET); | 2288 VariableProxy* proxy = NewUnresolved(name, LET); |
2238 Declaration* declaration = | 2289 Declaration* declaration = |
2239 factory()->NewVariableDeclaration(proxy, LET, scope_, pos); | 2290 factory()->NewVariableDeclaration(proxy, LET, scope_, pos); |
2240 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 2291 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); |
2241 proxy->var()->set_initializer_position(position()); | 2292 proxy->var()->set_initializer_position(position()); |
2242 Assignment* assignment = | 2293 Assignment* assignment = |
2243 factory()->NewAssignment(Token::INIT, proxy, value, pos); | 2294 factory()->NewAssignment(Token::INIT, proxy, value, pos); |
2244 Statement* assignment_statement = | 2295 Statement* assignment_statement = |
2245 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition); | 2296 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition); |
(...skipping 14 matching lines...) Expand all Loading... |
2260 factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition); | 2311 factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition); |
2261 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE); | 2312 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE); |
2262 | 2313 |
2263 // Parse the statements and collect escaping labels. | 2314 // Parse the statements and collect escaping labels. |
2264 Expect(Token::LBRACE, CHECK_OK); | 2315 Expect(Token::LBRACE, CHECK_OK); |
2265 block_scope->set_start_position(scanner()->location().beg_pos); | 2316 block_scope->set_start_position(scanner()->location().beg_pos); |
2266 { BlockState block_state(&scope_, block_scope); | 2317 { BlockState block_state(&scope_, block_scope); |
2267 Target target(&this->target_stack_, body); | 2318 Target target(&this->target_stack_, body); |
2268 | 2319 |
2269 while (peek() != Token::RBRACE) { | 2320 while (peek() != Token::RBRACE) { |
2270 Statement* stat = ParseStatementListItem(CHECK_OK); | 2321 Statement* stat = ParseStatementListItem(false, CHECK_OK); |
2271 if (stat && !stat->IsEmpty()) { | 2322 if (stat && !stat->IsEmpty()) { |
2272 body->statements()->Add(stat, zone()); | 2323 body->statements()->Add(stat, zone()); |
2273 } | 2324 } |
2274 } | 2325 } |
2275 } | 2326 } |
2276 Expect(Token::RBRACE, CHECK_OK); | 2327 Expect(Token::RBRACE, CHECK_OK); |
2277 block_scope->set_end_position(scanner()->location().end_pos); | 2328 block_scope->set_end_position(scanner()->location().end_pos); |
2278 if (finalize_block_scope) { | 2329 if (finalize_block_scope) { |
2279 block_scope = block_scope->FinalizeBlockScope(); | 2330 block_scope = block_scope->FinalizeBlockScope(); |
2280 } | 2331 } |
(...skipping 14 matching lines...) Expand all Loading... |
2295 for (auto declaration : declarations) { | 2346 for (auto declaration : declarations) { |
2296 PatternRewriter::DeclareAndInitializeVariables( | 2347 PatternRewriter::DeclareAndInitializeVariables( |
2297 result, &descriptor, &declaration, names, CHECK_OK); | 2348 result, &descriptor, &declaration, names, CHECK_OK); |
2298 } | 2349 } |
2299 return result; | 2350 return result; |
2300 } | 2351 } |
2301 | 2352 |
2302 | 2353 |
2303 Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context, | 2354 Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context, |
2304 ZoneList<const AstRawString*>* names, | 2355 ZoneList<const AstRawString*>* names, |
2305 bool* ok) { | 2356 bool ambient, bool* ok) { |
2306 // VariableStatement :: | 2357 // VariableStatement :: |
2307 // VariableDeclarations ';' | 2358 // VariableDeclarations ';' |
2308 | 2359 |
2309 // The scope of a var/const declared variable anywhere inside a function | 2360 // The scope of a var/const declared variable anywhere inside a function |
2310 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can | 2361 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can |
2311 // transform a source-level var/const declaration into a (Function) | 2362 // transform a source-level var/const declaration into a (Function) |
2312 // Scope declaration, and rewrite the source-level initialization into an | 2363 // Scope declaration, and rewrite the source-level initialization into an |
2313 // assignment statement. We use a block to collect multiple assignments. | 2364 // assignment statement. We use a block to collect multiple assignments. |
2314 // | 2365 // |
2315 // We mark the block as initializer block because we don't want the | 2366 // We mark the block as initializer block because we don't want the |
2316 // rewriter to add a '.result' assignment to such a block (to get compliant | 2367 // rewriter to add a '.result' assignment to such a block (to get compliant |
2317 // behavior for code such as print(eval('var x = 7')), and for cosmetic | 2368 // behavior for code such as print(eval('var x = 7')), and for cosmetic |
2318 // reasons when pretty-printing. Also, unless an assignment (initialization) | 2369 // reasons when pretty-printing. Also, unless an assignment (initialization) |
2319 // is inside an initializer block, it is ignored. | 2370 // is inside an initializer block, it is ignored. |
2320 | 2371 |
2321 DeclarationParsingResult parsing_result; | 2372 DeclarationParsingResult parsing_result; |
2322 Block* result = | 2373 Block* result = ParseVariableDeclarations(var_context, &parsing_result, names, |
2323 ParseVariableDeclarations(var_context, &parsing_result, names, CHECK_OK); | 2374 ambient, CHECK_OK); |
2324 ExpectSemicolon(CHECK_OK); | 2375 ExpectSemicolon(CHECK_OK); |
2325 return result; | 2376 return result; |
2326 } | 2377 } |
2327 | 2378 |
2328 Block* Parser::ParseVariableDeclarations( | 2379 Block* Parser::ParseVariableDeclarations( |
2329 VariableDeclarationContext var_context, | 2380 VariableDeclarationContext var_context, |
2330 DeclarationParsingResult* parsing_result, | 2381 DeclarationParsingResult* parsing_result, |
2331 ZoneList<const AstRawString*>* names, bool* ok) { | 2382 ZoneList<const AstRawString*>* names, bool ambient, bool* ok) { |
2332 // VariableDeclarations :: | 2383 // VariableDeclarations :: |
2333 // ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[','] | 2384 // ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[','] |
2334 // | 2385 // |
2335 // The ES6 Draft Rev3 specifies the following grammar for const declarations | 2386 // The ES6 Draft Rev3 specifies the following grammar for const declarations |
2336 // | 2387 // |
2337 // ConstDeclaration :: | 2388 // ConstDeclaration :: |
2338 // const ConstBinding (',' ConstBinding)* ';' | 2389 // const ConstBinding (',' ConstBinding)* ';' |
2339 // ConstBinding :: | 2390 // ConstBinding :: |
2340 // Identifier '=' AssignmentExpression | 2391 // Identifier '=' AssignmentExpression |
2341 // | 2392 // |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2393 } | 2444 } |
2394 } | 2445 } |
2395 | 2446 |
2396 // Parse optional type annotation. | 2447 // Parse optional type annotation. |
2397 typename TypeSystem::Type type = this->EmptyType(); | 2448 typename TypeSystem::Type type = this->EmptyType(); |
2398 if (scope_->typed() && Check(Token::COLON)) { // Braces required here. | 2449 if (scope_->typed() && Check(Token::COLON)) { // Braces required here. |
2399 type = ParseValidType(CHECK_OK); | 2450 type = ParseValidType(CHECK_OK); |
2400 } | 2451 } |
2401 USE(type); // TODO(nikolaos): really use it! | 2452 USE(type); // TODO(nikolaos): really use it! |
2402 | 2453 |
| 2454 // Skip initializers, for ambient declarations. |
| 2455 if (ambient) { |
| 2456 first_declaration = false; |
| 2457 continue; |
| 2458 } |
| 2459 |
2403 Scanner::Location variable_loc = scanner()->location(); | 2460 Scanner::Location variable_loc = scanner()->location(); |
2404 const AstRawString* single_name = | 2461 const AstRawString* single_name = |
2405 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name() | 2462 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name() |
2406 : nullptr; | 2463 : nullptr; |
2407 if (single_name != nullptr) { | 2464 if (single_name != nullptr) { |
2408 if (fni_ != NULL) fni_->PushVariableName(single_name); | 2465 if (fni_ != NULL) fni_->PushVariableName(single_name); |
2409 } | 2466 } |
2410 | 2467 |
2411 Expression* value = NULL; | 2468 Expression* value = NULL; |
2412 int initializer_position = RelocInfo::kNoPosition; | 2469 int initializer_position = RelocInfo::kNoPosition; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2550 } | 2607 } |
2551 labels->Add(label, zone()); | 2608 labels->Add(label, zone()); |
2552 // Remove the "ghost" variable that turned out to be a label | 2609 // Remove the "ghost" variable that turned out to be a label |
2553 // from the top scope. This way, we don't try to resolve it | 2610 // from the top scope. This way, we don't try to resolve it |
2554 // during the scope processing. | 2611 // during the scope processing. |
2555 scope_->RemoveUnresolved(var); | 2612 scope_->RemoveUnresolved(var); |
2556 Expect(Token::COLON, CHECK_OK); | 2613 Expect(Token::COLON, CHECK_OK); |
2557 // ES#sec-labelled-function-declarations Labelled Function Declarations | 2614 // ES#sec-labelled-function-declarations Labelled Function Declarations |
2558 if (peek() == Token::FUNCTION && is_sloppy(language_mode())) { | 2615 if (peek() == Token::FUNCTION && is_sloppy(language_mode())) { |
2559 if (allow_function == kAllowLabelledFunctionStatement) { | 2616 if (allow_function == kAllowLabelledFunctionStatement) { |
2560 return ParseFunctionDeclaration(labels, ok); | 2617 return ParseFunctionDeclaration(labels, false, ok); |
2561 } else { | 2618 } else { |
2562 return ParseScopedStatement(labels, true, ok); | 2619 return ParseScopedStatement(labels, true, ok); |
2563 } | 2620 } |
2564 } | 2621 } |
2565 return ParseStatement(labels, kDisallowLabelledFunctionStatement, ok); | 2622 return ParseStatement(labels, kDisallowLabelledFunctionStatement, ok); |
2566 } | 2623 } |
2567 | 2624 |
2568 // If we have an extension, we allow a native function declaration. | 2625 // If we have an extension, we allow a native function declaration. |
2569 // A native function declaration starts with "native function" with | 2626 // A native function declaration starts with "native function" with |
2570 // no line-terminator between the two words. | 2627 // no line-terminator between the two words. |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2820 *default_seen_ptr = true; | 2877 *default_seen_ptr = true; |
2821 } | 2878 } |
2822 Expect(Token::COLON, CHECK_OK); | 2879 Expect(Token::COLON, CHECK_OK); |
2823 int pos = position(); | 2880 int pos = position(); |
2824 ZoneList<Statement*>* statements = | 2881 ZoneList<Statement*>* statements = |
2825 new(zone()) ZoneList<Statement*>(5, zone()); | 2882 new(zone()) ZoneList<Statement*>(5, zone()); |
2826 Statement* stat = NULL; | 2883 Statement* stat = NULL; |
2827 while (peek() != Token::CASE && | 2884 while (peek() != Token::CASE && |
2828 peek() != Token::DEFAULT && | 2885 peek() != Token::DEFAULT && |
2829 peek() != Token::RBRACE) { | 2886 peek() != Token::RBRACE) { |
2830 stat = ParseStatementListItem(CHECK_OK); | 2887 stat = ParseStatementListItem(false, CHECK_OK); |
2831 statements->Add(stat, zone()); | 2888 statements->Add(stat, zone()); |
2832 } | 2889 } |
2833 return factory()->NewCaseClause(label, statements, pos); | 2890 return factory()->NewCaseClause(label, statements, pos); |
2834 } | 2891 } |
2835 | 2892 |
2836 | 2893 |
2837 Statement* Parser::ParseSwitchStatement(ZoneList<const AstRawString*>* labels, | 2894 Statement* Parser::ParseSwitchStatement(ZoneList<const AstRawString*>* labels, |
2838 bool* ok) { | 2895 bool* ok) { |
2839 // SwitchStatement :: | 2896 // SwitchStatement :: |
2840 // 'switch' '(' Expression ')' '{' CaseClause* '}' | 2897 // 'switch' '(' Expression ')' '{' CaseClause* '}' |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3055 | 3112 |
3056 Block* init_block = | 3113 Block* init_block = |
3057 factory()->NewBlock(nullptr, 8, true, RelocInfo::kNoPosition); | 3114 factory()->NewBlock(nullptr, 8, true, RelocInfo::kNoPosition); |
3058 PatternRewriter::DeclareAndInitializeVariables( | 3115 PatternRewriter::DeclareAndInitializeVariables( |
3059 init_block, &descriptor, &decl, nullptr, CHECK_OK); | 3116 init_block, &descriptor, &decl, nullptr, CHECK_OK); |
3060 catch_block->statements()->Add(init_block, zone()); | 3117 catch_block->statements()->Add(init_block, zone()); |
3061 } | 3118 } |
3062 | 3119 |
3063 Expect(Token::LBRACE, CHECK_OK); | 3120 Expect(Token::LBRACE, CHECK_OK); |
3064 while (peek() != Token::RBRACE) { | 3121 while (peek() != Token::RBRACE) { |
3065 Statement* stat = ParseStatementListItem(CHECK_OK); | 3122 Statement* stat = ParseStatementListItem(false, CHECK_OK); |
3066 if (stat && !stat->IsEmpty()) { | 3123 if (stat && !stat->IsEmpty()) { |
3067 catch_block->statements()->Add(stat, zone()); | 3124 catch_block->statements()->Add(stat, zone()); |
3068 } | 3125 } |
3069 } | 3126 } |
3070 Consume(Token::RBRACE); | 3127 Consume(Token::RBRACE); |
3071 } | 3128 } |
3072 block_scope->set_end_position(scanner()->location().end_pos); | 3129 block_scope->set_end_position(scanner()->location().end_pos); |
3073 block_scope = block_scope->FinalizeBlockScope(); | 3130 block_scope = block_scope->FinalizeBlockScope(); |
3074 catch_block->set_scope(block_scope); | 3131 catch_block->set_scope(block_scope); |
3075 } | 3132 } |
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3553 return ParseSubStatement(labels, kDisallowLabelledFunctionStatement, ok); | 3610 return ParseSubStatement(labels, kDisallowLabelledFunctionStatement, ok); |
3554 } else { | 3611 } else { |
3555 if (legacy) { | 3612 if (legacy) { |
3556 ++use_counts_[v8::Isolate::kLegacyFunctionDeclaration]; | 3613 ++use_counts_[v8::Isolate::kLegacyFunctionDeclaration]; |
3557 } | 3614 } |
3558 // Make a block around the statement for a lexical binding | 3615 // Make a block around the statement for a lexical binding |
3559 // is introduced by a FunctionDeclaration. | 3616 // is introduced by a FunctionDeclaration. |
3560 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE); | 3617 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE); |
3561 BlockState block_state(&scope_, body_scope); | 3618 BlockState block_state(&scope_, body_scope); |
3562 Block* block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition); | 3619 Block* block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition); |
3563 Statement* body = ParseFunctionDeclaration(NULL, CHECK_OK); | 3620 Statement* body = ParseFunctionDeclaration(NULL, false, CHECK_OK); |
3564 block->statements()->Add(body, zone()); | 3621 block->statements()->Add(body, zone()); |
3565 body_scope->set_end_position(scanner()->location().end_pos); | 3622 body_scope->set_end_position(scanner()->location().end_pos); |
3566 body_scope = body_scope->FinalizeBlockScope(); | 3623 body_scope = body_scope->FinalizeBlockScope(); |
3567 block->set_scope(body_scope); | 3624 block->set_scope(body_scope); |
3568 return block; | 3625 return block; |
3569 } | 3626 } |
3570 } | 3627 } |
3571 | 3628 |
3572 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, | 3629 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, |
3573 bool* ok) { | 3630 bool* ok) { |
3574 int stmt_pos = peek_position(); | 3631 int stmt_pos = peek_position(); |
3575 Statement* init = NULL; | 3632 Statement* init = NULL; |
3576 ZoneList<const AstRawString*> lexical_bindings(1, zone()); | 3633 ZoneList<const AstRawString*> lexical_bindings(1, zone()); |
3577 | 3634 |
3578 // Create an in-between scope for let-bound iteration variables. | 3635 // Create an in-between scope for let-bound iteration variables. |
3579 Scope* for_scope = NewScope(scope_, BLOCK_SCOPE); | 3636 Scope* for_scope = NewScope(scope_, BLOCK_SCOPE); |
3580 | 3637 |
3581 BlockState block_state(&scope_, for_scope); | 3638 BlockState block_state(&scope_, for_scope); |
3582 Expect(Token::FOR, CHECK_OK); | 3639 Expect(Token::FOR, CHECK_OK); |
3583 Expect(Token::LPAREN, CHECK_OK); | 3640 Expect(Token::LPAREN, CHECK_OK); |
3584 for_scope->set_start_position(scanner()->location().beg_pos); | 3641 for_scope->set_start_position(scanner()->location().beg_pos); |
3585 bool is_let_identifier_expression = false; | 3642 bool is_let_identifier_expression = false; |
3586 DeclarationParsingResult parsing_result; | 3643 DeclarationParsingResult parsing_result; |
3587 if (peek() != Token::SEMICOLON) { | 3644 if (peek() != Token::SEMICOLON) { |
3588 if (peek() == Token::VAR || (peek() == Token::CONST && allow_const()) || | 3645 if (peek() == Token::VAR || (peek() == Token::CONST && allow_const()) || |
3589 (peek() == Token::LET && IsNextLetKeyword())) { | 3646 (peek() == Token::LET && IsNextLetKeyword())) { |
3590 ParseVariableDeclarations(kForStatement, &parsing_result, nullptr, | 3647 ParseVariableDeclarations(kForStatement, &parsing_result, nullptr, false, |
3591 CHECK_OK); | 3648 CHECK_OK); |
3592 | 3649 |
3593 ForEachStatement::VisitMode mode = ForEachStatement::ENUMERATE; | 3650 ForEachStatement::VisitMode mode = ForEachStatement::ENUMERATE; |
3594 int each_beg_pos = scanner()->location().beg_pos; | 3651 int each_beg_pos = scanner()->location().beg_pos; |
3595 int each_end_pos = scanner()->location().end_pos; | 3652 int each_end_pos = scanner()->location().end_pos; |
3596 | 3653 |
3597 if (CheckInOrOf(&mode, ok)) { | 3654 if (CheckInOrOf(&mode, ok)) { |
3598 if (!*ok) return nullptr; | 3655 if (!*ok) return nullptr; |
3599 if (parsing_result.declarations.length() != 1) { | 3656 if (parsing_result.declarations.length() != 1) { |
3600 ParserTraits::ReportMessageAt( | 3657 ParserTraits::ReportMessageAt( |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4218 | 4275 |
4219 // Parse optional type annotation. | 4276 // Parse optional type annotation. |
4220 typename TypeSystem::Type result_type = this->EmptyType(); | 4277 typename TypeSystem::Type result_type = this->EmptyType(); |
4221 if (scope_->typed() && | 4278 if (scope_->typed() && |
4222 !(type_flags & typesystem::kDisallowTypeAnnotation) && | 4279 !(type_flags & typesystem::kDisallowTypeAnnotation) && |
4223 Check(Token::COLON)) { // Braces required here. | 4280 Check(Token::COLON)) { // Braces required here. |
4224 result_type = ParseValidType(CHECK_OK); | 4281 result_type = ParseValidType(CHECK_OK); |
4225 } | 4282 } |
4226 USE(result_type); // TODO(nikolaos): really use it! | 4283 USE(result_type); // TODO(nikolaos): really use it! |
4227 | 4284 |
4228 // Allow for a function signature (i.e., a literal without body). | 4285 // Allow or even enforce a function signature (i.e., literal without body), |
4229 // In that case, return a nullptr instead of a function literal. | 4286 // In that case, return a nullptr instead of a function literal. |
4230 if (peek() != Token::LBRACE && scope_->typed() && | 4287 if ((type_flags & typesystem::kDisallowBody) || |
4231 (type_flags & typesystem::kAllowSignature)) { | 4288 (peek() != Token::LBRACE && scope_->typed() && |
| 4289 (type_flags & typesystem::kAllowSignature))) { |
4232 ExpectSemicolon(CHECK_OK); | 4290 ExpectSemicolon(CHECK_OK); |
4233 return nullptr; | 4291 return nullptr; |
4234 } | 4292 } |
4235 | 4293 |
4236 Expect(Token::LBRACE, CHECK_OK); | 4294 Expect(Token::LBRACE, CHECK_OK); |
4237 | 4295 |
4238 // Don't include the rest parameter into the function's formal parameter | 4296 // Don't include the rest parameter into the function's formal parameter |
4239 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, | 4297 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, |
4240 // which says whether we need to create an arguments adaptor frame). | 4298 // which says whether we need to create an arguments adaptor frame). |
4241 if (formals.has_rest) arity--; | 4299 if (formals.has_rest) arity--; |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4667 Token::INIT, init_proxy, allocation, RelocInfo::kNoPosition); | 4725 Token::INIT, init_proxy, allocation, RelocInfo::kNoPosition); |
4668 VariableProxy* get_proxy = factory()->NewVariableProxy( | 4726 VariableProxy* get_proxy = factory()->NewVariableProxy( |
4669 function_state_->generator_object_variable()); | 4727 function_state_->generator_object_variable()); |
4670 Yield* yield = | 4728 Yield* yield = |
4671 factory()->NewYield(get_proxy, assignment, RelocInfo::kNoPosition); | 4729 factory()->NewYield(get_proxy, assignment, RelocInfo::kNoPosition); |
4672 try_block->statements()->Add( | 4730 try_block->statements()->Add( |
4673 factory()->NewExpressionStatement(yield, RelocInfo::kNoPosition), | 4731 factory()->NewExpressionStatement(yield, RelocInfo::kNoPosition), |
4674 zone()); | 4732 zone()); |
4675 } | 4733 } |
4676 | 4734 |
4677 ParseStatementList(try_block->statements(), Token::RBRACE, CHECK_OK); | 4735 ParseStatementList(try_block->statements(), Token::RBRACE, false, |
| 4736 CHECK_OK); |
4678 | 4737 |
4679 Statement* final_return = factory()->NewReturnStatement( | 4738 Statement* final_return = factory()->NewReturnStatement( |
4680 BuildIteratorResult(nullptr, true), RelocInfo::kNoPosition); | 4739 BuildIteratorResult(nullptr, true), RelocInfo::kNoPosition); |
4681 try_block->statements()->Add(final_return, zone()); | 4740 try_block->statements()->Add(final_return, zone()); |
4682 | 4741 |
4683 Block* finally_block = | 4742 Block* finally_block = |
4684 factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition); | 4743 factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition); |
4685 ZoneList<Expression*>* args = | 4744 ZoneList<Expression*>* args = |
4686 new (zone()) ZoneList<Expression*>(1, zone()); | 4745 new (zone()) ZoneList<Expression*>(1, zone()); |
4687 VariableProxy* call_proxy = factory()->NewVariableProxy( | 4746 VariableProxy* call_proxy = factory()->NewVariableProxy( |
4688 function_state_->generator_object_variable()); | 4747 function_state_->generator_object_variable()); |
4689 args->Add(call_proxy, zone()); | 4748 args->Add(call_proxy, zone()); |
4690 Expression* call = factory()->NewCallRuntime( | 4749 Expression* call = factory()->NewCallRuntime( |
4691 Runtime::kGeneratorClose, args, RelocInfo::kNoPosition); | 4750 Runtime::kGeneratorClose, args, RelocInfo::kNoPosition); |
4692 finally_block->statements()->Add( | 4751 finally_block->statements()->Add( |
4693 factory()->NewExpressionStatement(call, RelocInfo::kNoPosition), | 4752 factory()->NewExpressionStatement(call, RelocInfo::kNoPosition), |
4694 zone()); | 4753 zone()); |
4695 | 4754 |
4696 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, | 4755 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, |
4697 RelocInfo::kNoPosition), | 4756 RelocInfo::kNoPosition), |
4698 zone()); | 4757 zone()); |
4699 } else { | 4758 } else { |
4700 ParseStatementList(body, Token::RBRACE, CHECK_OK); | 4759 ParseStatementList(body, Token::RBRACE, false, CHECK_OK); |
4701 } | 4760 } |
4702 | 4761 |
4703 if (IsSubclassConstructor(kind)) { | 4762 if (IsSubclassConstructor(kind)) { |
4704 body->Add( | 4763 body->Add( |
4705 factory()->NewReturnStatement( | 4764 factory()->NewReturnStatement( |
4706 this->ThisExpression(scope_, factory(), RelocInfo::kNoPosition), | 4765 this->ThisExpression(scope_, factory(), RelocInfo::kNoPosition), |
4707 RelocInfo::kNoPosition), | 4766 RelocInfo::kNoPosition), |
4708 zone()); | 4767 zone()); |
4709 } | 4768 } |
4710 } | 4769 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4800 if (pre_parse_timer_ != NULL) { | 4859 if (pre_parse_timer_ != NULL) { |
4801 pre_parse_timer_->Stop(); | 4860 pre_parse_timer_->Stop(); |
4802 } | 4861 } |
4803 return result; | 4862 return result; |
4804 } | 4863 } |
4805 | 4864 |
4806 | 4865 |
4807 ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name, | 4866 ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name, |
4808 Scanner::Location class_name_location, | 4867 Scanner::Location class_name_location, |
4809 bool name_is_strict_reserved, int pos, | 4868 bool name_is_strict_reserved, int pos, |
4810 bool* ok) { | 4869 bool ambient, bool* ok) { |
4811 // All parts of a ClassDeclaration and ClassExpression are strict code. | 4870 // All parts of a ClassDeclaration and ClassExpression are strict code. |
4812 if (name_is_strict_reserved) { | 4871 if (name_is_strict_reserved) { |
4813 ReportMessageAt(class_name_location, | 4872 ReportMessageAt(class_name_location, |
4814 MessageTemplate::kUnexpectedStrictReserved); | 4873 MessageTemplate::kUnexpectedStrictReserved); |
4815 *ok = false; | 4874 *ok = false; |
4816 return NULL; | 4875 return NULL; |
4817 } | 4876 } |
4818 if (IsEvalOrArguments(name)) { | 4877 if (IsEvalOrArguments(name)) { |
4819 ReportMessageAt(class_name_location, MessageTemplate::kStrictEvalArguments); | 4878 ReportMessageAt(class_name_location, MessageTemplate::kStrictEvalArguments); |
4820 *ok = false; | 4879 *ok = false; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4887 if (Check(Token::SEMICOLON)) continue; | 4946 if (Check(Token::SEMICOLON)) continue; |
4888 FuncNameInferrer::State fni_state(fni_); | 4947 FuncNameInferrer::State fni_state(fni_); |
4889 const bool in_class = true; | 4948 const bool in_class = true; |
4890 const bool is_static = false; | 4949 const bool is_static = false; |
4891 bool is_computed_name = false; // Classes do not care about computed | 4950 bool is_computed_name = false; // Classes do not care about computed |
4892 // property names here. | 4951 // property names here. |
4893 ExpressionClassifier classifier(this); | 4952 ExpressionClassifier classifier(this); |
4894 const AstRawString* property_name = nullptr; | 4953 const AstRawString* property_name = nullptr; |
4895 ObjectLiteral::Property* property = ParsePropertyDefinition( | 4954 ObjectLiteral::Property* property = ParsePropertyDefinition( |
4896 &checker, in_class, has_extends, is_static, &is_computed_name, | 4955 &checker, in_class, has_extends, is_static, &is_computed_name, |
4897 &has_seen_constructor, &classifier, &property_name, CHECK_OK); | 4956 &has_seen_constructor, &classifier, &property_name, ambient, CHECK_OK); |
4898 // Ignore member variable declarations in typed mode. | 4957 // Ignore member variable declarations, method signatures and members of |
| 4958 // ambients in typed mode. |
4899 if (property == nullptr) continue; | 4959 if (property == nullptr) continue; |
4900 RewriteNonPattern(&classifier, CHECK_OK); | 4960 RewriteNonPattern(&classifier, CHECK_OK); |
4901 | 4961 |
4902 if (has_seen_constructor && constructor == NULL) { | 4962 if (has_seen_constructor && constructor == NULL) { |
4903 constructor = GetPropertyValue(property)->AsFunctionLiteral(); | 4963 constructor = GetPropertyValue(property)->AsFunctionLiteral(); |
4904 DCHECK_NOT_NULL(constructor); | 4964 DCHECK_NOT_NULL(constructor); |
4905 constructor->set_raw_name( | 4965 constructor->set_raw_name( |
4906 name != nullptr ? name : ast_value_factory()->empty_string()); | 4966 name != nullptr ? name : ast_value_factory()->empty_string()); |
4907 } else { | 4967 } else { |
4908 properties->Add(property, zone()); | 4968 properties->Add(property, zone()); |
(...skipping 2098 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7007 try_block, target); | 7067 try_block, target); |
7008 final_loop = target; | 7068 final_loop = target; |
7009 } | 7069 } |
7010 | 7070 |
7011 return final_loop; | 7071 return final_loop; |
7012 } | 7072 } |
7013 | 7073 |
7014 | 7074 |
7015 } // namespace internal | 7075 } // namespace internal |
7016 } // namespace v8 | 7076 } // namespace v8 |
OLD | NEW |