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 #ifndef V8_PARSING_PARSER_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_H |
7 | 7 |
8 #include "src/ast/scopes.h" | 8 #include "src/ast/scopes.h" |
9 #include "src/bailout-reason.h" | 9 #include "src/bailout-reason.h" |
10 #include "src/hashmap.h" | 10 #include "src/hashmap.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 // typedef Identifier; | 67 // typedef Identifier; |
68 // typedef Expression; | 68 // typedef Expression; |
69 // typedef FunctionLiteral; | 69 // typedef FunctionLiteral; |
70 // typedef ClassLiteral; | 70 // typedef ClassLiteral; |
71 // typedef ObjectLiteralProperty; | 71 // typedef ObjectLiteralProperty; |
72 // typedef Literal; | 72 // typedef Literal; |
73 // typedef ExpressionList; | 73 // typedef ExpressionList; |
74 // typedef PropertyList; | 74 // typedef PropertyList; |
75 // typedef FormalParameter; | 75 // typedef FormalParameter; |
76 // typedef FormalParameters; | 76 // typedef FormalParameters; |
| 77 // typedef Statement; |
| 78 // typedef StatementList; |
77 // // For constructing objects returned by the traversing functions. | 79 // // For constructing objects returned by the traversing functions. |
78 // typedef Factory; | 80 // typedef Factory; |
| 81 // // For classifying and validating expressions. |
| 82 // typedef ExpressionClassifier. |
| 83 // // A struct with extra types for the optional type system. |
| 84 // struct TypeSystem { |
| 85 // typedef Type; |
| 86 // typedef TypeList; |
| 87 // typedef TypeParameter; |
| 88 // typedef TypeParameters; |
| 89 // typedef FormalParameter; |
| 90 // typedef FormalParameters; |
| 91 // typedef TypeMember; |
| 92 // typedef TypeMembers; |
| 93 // }; |
79 // }; | 94 // }; |
80 // // ... | 95 // // ... |
81 // }; | 96 // }; |
82 | 97 |
83 template <typename Traits> | 98 template <typename Traits> |
84 class ParserBase : public Traits { | 99 class ParserBase : public Traits { |
85 public: | 100 public: |
86 // Shorten type names defined by Traits. | 101 // Shorten type names defined by Traits. |
87 typedef typename Traits::Type::Expression ExpressionT; | 102 typedef typename Traits::Type::Expression ExpressionT; |
88 typedef typename Traits::Type::Identifier IdentifierT; | 103 typedef typename Traits::Type::Identifier IdentifierT; |
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 bool* ok); | 781 bool* ok); |
767 ExpressionT ParseArrayLiteral(ExpressionClassifier* classifier, bool* ok); | 782 ExpressionT ParseArrayLiteral(ExpressionClassifier* classifier, bool* ok); |
768 ExpressionT ParsePropertyName(IdentifierT* name, bool* is_get, bool* is_set, | 783 ExpressionT ParsePropertyName(IdentifierT* name, bool* is_get, bool* is_set, |
769 bool* is_computed_name, | 784 bool* is_computed_name, |
770 ExpressionClassifier* classifier, bool* ok); | 785 ExpressionClassifier* classifier, bool* ok); |
771 ExpressionT ParsePropertyNameInType(bool* ok); | 786 ExpressionT ParsePropertyNameInType(bool* ok); |
772 ExpressionT ParseObjectLiteral(ExpressionClassifier* classifier, bool* ok); | 787 ExpressionT ParseObjectLiteral(ExpressionClassifier* classifier, bool* ok); |
773 ObjectLiteralPropertyT ParsePropertyDefinition( | 788 ObjectLiteralPropertyT ParsePropertyDefinition( |
774 ObjectLiteralCheckerBase* checker, bool in_class, bool has_extends, | 789 ObjectLiteralCheckerBase* checker, bool in_class, bool has_extends, |
775 bool is_static, bool* is_computed_name, bool* has_seen_constructor, | 790 bool is_static, bool* is_computed_name, bool* has_seen_constructor, |
776 ExpressionClassifier* classifier, IdentifierT* name, bool* ok); | 791 ExpressionClassifier* classifier, IdentifierT* name, bool ambient, |
| 792 bool* ok); |
777 typename Traits::Type::ExpressionList ParseArguments( | 793 typename Traits::Type::ExpressionList ParseArguments( |
778 Scanner::Location* first_spread_pos, ExpressionClassifier* classifier, | 794 Scanner::Location* first_spread_pos, ExpressionClassifier* classifier, |
779 bool* ok); | 795 bool* ok); |
780 | 796 |
781 ExpressionT ParseAssignmentExpression(bool accept_IN, | 797 ExpressionT ParseAssignmentExpression(bool accept_IN, |
782 ExpressionClassifier* classifier, | 798 ExpressionClassifier* classifier, |
783 bool* ok); | 799 bool* ok); |
784 ExpressionT ParseYieldExpression(ExpressionClassifier* classifier, bool* ok); | 800 ExpressionT ParseYieldExpression(ExpressionClassifier* classifier, bool* ok); |
785 ExpressionT ParseConditionalExpression(bool accept_IN, | 801 ExpressionT ParseConditionalExpression(bool accept_IN, |
786 ExpressionClassifier* classifier, | 802 ExpressionClassifier* classifier, |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1397 IdentifierT name = this->EmptyIdentifier(); | 1413 IdentifierT name = this->EmptyIdentifier(); |
1398 bool is_strict_reserved_name = false; | 1414 bool is_strict_reserved_name = false; |
1399 Scanner::Location class_name_location = Scanner::Location::invalid(); | 1415 Scanner::Location class_name_location = Scanner::Location::invalid(); |
1400 if (peek_any_identifier()) { | 1416 if (peek_any_identifier()) { |
1401 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, | 1417 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, |
1402 CHECK_OK); | 1418 CHECK_OK); |
1403 class_name_location = scanner()->location(); | 1419 class_name_location = scanner()->location(); |
1404 } | 1420 } |
1405 return this->ParseClassLiteral(name, class_name_location, | 1421 return this->ParseClassLiteral(name, class_name_location, |
1406 is_strict_reserved_name, | 1422 is_strict_reserved_name, |
1407 class_token_position, ok); | 1423 class_token_position, false, ok); |
1408 } | 1424 } |
1409 | 1425 |
1410 case Token::TEMPLATE_SPAN: | 1426 case Token::TEMPLATE_SPAN: |
1411 case Token::TEMPLATE_TAIL: | 1427 case Token::TEMPLATE_TAIL: |
1412 BindingPatternUnexpectedToken(classifier); | 1428 BindingPatternUnexpectedToken(classifier); |
1413 return this->ParseTemplateLiteral(Traits::NoTemplateTag(), beg_pos, | 1429 return this->ParseTemplateLiteral(Traits::NoTemplateTag(), beg_pos, |
1414 classifier, ok); | 1430 classifier, ok); |
1415 | 1431 |
1416 case Token::MOD: | 1432 case Token::MOD: |
1417 if (allow_natives() || extension_ != NULL) { | 1433 if (allow_natives() || extension_ != NULL) { |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1672 ? factory()->NewNumberLiteral(index, pos) | 1688 ? factory()->NewNumberLiteral(index, pos) |
1673 : factory()->NewStringLiteral(name, pos); | 1689 : factory()->NewStringLiteral(name, pos); |
1674 } | 1690 } |
1675 | 1691 |
1676 | 1692 |
1677 template <class Traits> | 1693 template <class Traits> |
1678 typename ParserBase<Traits>::ObjectLiteralPropertyT | 1694 typename ParserBase<Traits>::ObjectLiteralPropertyT |
1679 ParserBase<Traits>::ParsePropertyDefinition( | 1695 ParserBase<Traits>::ParsePropertyDefinition( |
1680 ObjectLiteralCheckerBase* checker, bool in_class, bool has_extends, | 1696 ObjectLiteralCheckerBase* checker, bool in_class, bool has_extends, |
1681 bool is_static, bool* is_computed_name, bool* has_seen_constructor, | 1697 bool is_static, bool* is_computed_name, bool* has_seen_constructor, |
1682 ExpressionClassifier* classifier, IdentifierT* name, bool* ok) { | 1698 ExpressionClassifier* classifier, IdentifierT* name, bool ambient, |
| 1699 bool* ok) { |
1683 DCHECK(!in_class || is_static || has_seen_constructor != nullptr); | 1700 DCHECK(!in_class || is_static || has_seen_constructor != nullptr); |
1684 | 1701 |
1685 // Parse index member declarations in typed mode. | 1702 // Parse index member declarations in typed mode. |
1686 // We implicitly disallow computed property names, in this case, | 1703 // We implicitly disallow computed property names, in this case, |
1687 // i.e., class C { [42](x) {} } does not work in typed mode. | 1704 // i.e., class C { [42](x) {} } does not work in typed mode. |
1688 if (in_class && !is_static && scope_->typed() && Check(Token::LBRACK)) { | 1705 if (in_class && !is_static && scope_->typed() && Check(Token::LBRACK)) { |
1689 int property_pos = peek_position(); | 1706 int property_pos = peek_position(); |
1690 IdentifierT property_name = | 1707 IdentifierT property_name = |
1691 ParseIdentifierName(CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); | 1708 ParseIdentifierName(CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); |
1692 ExpressionT property = | 1709 ExpressionT property = |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1830 : typesystem::kNormalTypes; | 1847 : typesystem::kNormalTypes; |
1831 | 1848 |
1832 if (in_class && !is_static && this->IsConstructor(*name)) { | 1849 if (in_class && !is_static && this->IsConstructor(*name)) { |
1833 *has_seen_constructor = true; | 1850 *has_seen_constructor = true; |
1834 kind = has_extends ? FunctionKind::kSubclassConstructor | 1851 kind = has_extends ? FunctionKind::kSubclassConstructor |
1835 : FunctionKind::kBaseConstructor; | 1852 : FunctionKind::kBaseConstructor; |
1836 type_flags = typesystem::kConstructorTypes; | 1853 type_flags = typesystem::kConstructorTypes; |
1837 } | 1854 } |
1838 // Allow signatures when in a class. | 1855 // Allow signatures when in a class. |
1839 if (in_class) type_flags |= typesystem::kAllowSignature; | 1856 if (in_class) type_flags |= typesystem::kAllowSignature; |
| 1857 if (ambient) type_flags |= typesystem::kAmbient; |
1840 | 1858 |
1841 value = this->ParseFunctionLiteral( | 1859 value = this->ParseFunctionLiteral( |
1842 *name, scanner()->location(), kSkipFunctionNameCheck, kind, | 1860 *name, scanner()->location(), kSkipFunctionNameCheck, kind, |
1843 RelocInfo::kNoPosition, FunctionLiteral::kAccessorOrMethod, | 1861 RelocInfo::kNoPosition, FunctionLiteral::kAccessorOrMethod, |
1844 language_mode(), type_flags, | 1862 language_mode(), type_flags, |
1845 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); | 1863 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); |
1846 | 1864 |
1847 // Return no property definition if just the signature was given. | 1865 // Return no property definition if just the signature was given. |
1848 if (this->IsEmptyExpression(value)) { | 1866 if (this->IsEmptyExpression(value)) { |
1849 // Don't count constructor signature as a constructor. | 1867 // Don't count constructor signature as a constructor. |
1850 if (in_class && !is_static && this->IsConstructor(*name)) { | 1868 if (in_class && !is_static && this->IsConstructor(*name)) { |
1851 // Note: we don't really need to unset has_seen_constructor | 1869 // Note: we don't really need to unset has_seen_constructor |
1852 checker->JustSignature(); | 1870 checker->JustSignature(); |
1853 } | 1871 } |
1854 return this->EmptyObjectLiteralProperty(); | 1872 return this->EmptyObjectLiteralProperty(); |
1855 } | 1873 } |
1856 | 1874 |
1857 return factory()->NewObjectLiteralProperty(name_expression, value, | 1875 return factory()->NewObjectLiteralProperty(name_expression, value, |
1858 ObjectLiteralProperty::COMPUTED, | 1876 ObjectLiteralProperty::COMPUTED, |
1859 is_static, *is_computed_name); | 1877 is_static, *is_computed_name); |
1860 } | 1878 } |
1861 | 1879 |
1862 if (in_class && name_token == Token::STATIC && !is_static) { | 1880 if (in_class && name_token == Token::STATIC && !is_static) { |
1863 // ClassElement (static) | 1881 // ClassElement (static) |
1864 // 'static' MethodDefinition | 1882 // 'static' MethodDefinition |
1865 *name = this->EmptyIdentifier(); | 1883 *name = this->EmptyIdentifier(); |
1866 ObjectLiteralPropertyT property = ParsePropertyDefinition( | 1884 ObjectLiteralPropertyT property = ParsePropertyDefinition( |
1867 checker, true, has_extends, true, is_computed_name, nullptr, classifier, | 1885 checker, true, has_extends, true, is_computed_name, nullptr, classifier, |
1868 name, ok); | 1886 name, ambient, ok); |
1869 Traits::RewriteNonPattern(classifier, ok); | 1887 Traits::RewriteNonPattern(classifier, ok); |
1870 return property; | 1888 return property; |
1871 } | 1889 } |
1872 | 1890 |
1873 if (is_get || is_set) { | 1891 if (is_get || is_set) { |
1874 // MethodDefinition (Accessors) | 1892 // MethodDefinition (Accessors) |
1875 // get PropertyName '(' ')' '{' FunctionBody '}' | 1893 // get PropertyName '(' ')' '{' FunctionBody '}' |
1876 // set PropertyName '(' PropertySetParameterList ')' '{' FunctionBody '}' | 1894 // set PropertyName '(' PropertySetParameterList ')' '{' FunctionBody '}' |
| 1895 if (ambient) { |
| 1896 *ok = false; |
| 1897 ReportMessage(MessageTemplate::kAmbientGetOrSet); |
| 1898 return this->EmptyObjectLiteralProperty(); |
| 1899 } |
| 1900 |
1877 *name = this->EmptyIdentifier(); | 1901 *name = this->EmptyIdentifier(); |
1878 bool dont_care = false; | 1902 bool dont_care = false; |
1879 name_token = peek(); | 1903 name_token = peek(); |
1880 | 1904 |
1881 name_expression = ParsePropertyName( | 1905 name_expression = ParsePropertyName( |
1882 name, &dont_care, &dont_care, is_computed_name, classifier, | 1906 name, &dont_care, &dont_care, is_computed_name, classifier, |
1883 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); | 1907 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); |
1884 | 1908 |
1885 if (!*is_computed_name) { | 1909 if (!*is_computed_name) { |
1886 checker->CheckProperty(name_token, kAccessorProperty, is_static, | 1910 checker->CheckProperty(name_token, kAccessorProperty, is_static, |
(...skipping 24 matching lines...) Expand all Loading... |
1911 | 1935 |
1912 // Allow member variable declarations in typed mode. | 1936 // Allow member variable declarations in typed mode. |
1913 if (in_class && scope_->typed()) { | 1937 if (in_class && scope_->typed()) { |
1914 // Parse optional type annotation. | 1938 // Parse optional type annotation. |
1915 typename TypeSystem::Type type = this->EmptyType(); | 1939 typename TypeSystem::Type type = this->EmptyType(); |
1916 if (Check(Token::COLON)) { // Braces required here. | 1940 if (Check(Token::COLON)) { // Braces required here. |
1917 type = ParseValidType(CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); | 1941 type = ParseValidType(CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); |
1918 } | 1942 } |
1919 USE(type); // TODO(nikolaos): really use it! | 1943 USE(type); // TODO(nikolaos): really use it! |
1920 // Parse optional initializer. | 1944 // Parse optional initializer. |
1921 if (Check(Token::ASSIGN)) { | 1945 if (!ambient && Check(Token::ASSIGN)) { |
1922 ExpressionClassifier rhs_classifier(this); | 1946 ExpressionClassifier rhs_classifier(this); |
1923 ExpressionT rhs = this->ParseAssignmentExpression( | 1947 ExpressionT rhs = this->ParseAssignmentExpression( |
1924 true, &rhs_classifier, CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); | 1948 true, &rhs_classifier, CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); |
1925 Traits::RewriteNonPattern(&rhs_classifier, | 1949 Traits::RewriteNonPattern(&rhs_classifier, |
1926 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); | 1950 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); |
1927 classifier->Accumulate(&rhs_classifier, | 1951 classifier->Accumulate(&rhs_classifier, |
1928 ExpressionClassifier::ExpressionProductions); | 1952 ExpressionClassifier::ExpressionProductions); |
1929 USE(rhs); // TODO(nikolaos): really use it! | 1953 USE(rhs); // TODO(nikolaos): really use it! |
1930 } | 1954 } |
1931 return this->EmptyObjectLiteralProperty(); | 1955 return this->EmptyObjectLiteralProperty(); |
(...skipping 24 matching lines...) Expand all Loading... |
1956 while (peek() != Token::RBRACE) { | 1980 while (peek() != Token::RBRACE) { |
1957 FuncNameInferrer::State fni_state(fni_); | 1981 FuncNameInferrer::State fni_state(fni_); |
1958 | 1982 |
1959 const bool in_class = false; | 1983 const bool in_class = false; |
1960 const bool is_static = false; | 1984 const bool is_static = false; |
1961 const bool has_extends = false; | 1985 const bool has_extends = false; |
1962 bool is_computed_name = false; | 1986 bool is_computed_name = false; |
1963 IdentifierT name = this->EmptyIdentifier(); | 1987 IdentifierT name = this->EmptyIdentifier(); |
1964 ObjectLiteralPropertyT property = this->ParsePropertyDefinition( | 1988 ObjectLiteralPropertyT property = this->ParsePropertyDefinition( |
1965 &checker, in_class, has_extends, is_static, &is_computed_name, NULL, | 1989 &checker, in_class, has_extends, is_static, &is_computed_name, NULL, |
1966 classifier, &name, CHECK_OK); | 1990 classifier, &name, false, CHECK_OK); |
1967 | 1991 |
1968 if (is_computed_name) { | 1992 if (is_computed_name) { |
1969 has_computed_names = true; | 1993 has_computed_names = true; |
1970 } | 1994 } |
1971 | 1995 |
1972 // Count CONSTANT or COMPUTED properties to maintain the enumeration order. | 1996 // Count CONSTANT or COMPUTED properties to maintain the enumeration order. |
1973 if (!has_computed_names && this->IsBoilerplateProperty(property)) { | 1997 if (!has_computed_names && this->IsBoilerplateProperty(property)) { |
1974 number_of_boilerplate_properties++; | 1998 number_of_boilerplate_properties++; |
1975 } | 1999 } |
1976 properties->Add(property, zone()); | 2000 properties->Add(property, zone()); |
(...skipping 1875 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3852 template <typename Traits> | 3876 template <typename Traits> |
3853 void ParserBase<Traits>::ClassLiteralChecker::JustSignature() { | 3877 void ParserBase<Traits>::ClassLiteralChecker::JustSignature() { |
3854 has_seen_constructor_ = false; | 3878 has_seen_constructor_ = false; |
3855 } | 3879 } |
3856 | 3880 |
3857 | 3881 |
3858 } // namespace internal | 3882 } // namespace internal |
3859 } // namespace v8 | 3883 } // namespace v8 |
3860 | 3884 |
3861 #endif // V8_PARSING_PARSER_BASE_H | 3885 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |