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

Side by Side Diff: src/parsing/parser-base.h

Issue 1853763002: Allow constructor and method signatures in classes (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@types-1849803002-intf-decl
Patch Set: Fix method type annotations in the preparser Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_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 880 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 891
892 class ObjectLiteralCheckerBase { 892 class ObjectLiteralCheckerBase {
893 public: 893 public:
894 explicit ObjectLiteralCheckerBase(ParserBase* parser) : parser_(parser) {} 894 explicit ObjectLiteralCheckerBase(ParserBase* parser) : parser_(parser) {}
895 895
896 virtual void CheckProperty(Token::Value property, PropertyKind type, 896 virtual void CheckProperty(Token::Value property, PropertyKind type,
897 bool is_static, bool is_generator, bool* ok) = 0; 897 bool is_static, bool is_generator, bool* ok) = 0;
898 898
899 virtual ~ObjectLiteralCheckerBase() {} 899 virtual ~ObjectLiteralCheckerBase() {}
900 900
901 virtual void JustSignature() = 0;
902
901 protected: 903 protected:
902 ParserBase* parser() const { return parser_; } 904 ParserBase* parser() const { return parser_; }
903 Scanner* scanner() const { return parser_->scanner(); } 905 Scanner* scanner() const { return parser_->scanner(); }
904 906
905 private: 907 private:
906 ParserBase* parser_; 908 ParserBase* parser_;
907 }; 909 };
908 910
909 // Validation per ES6 object literals. 911 // Validation per ES6 object literals.
910 class ObjectLiteralChecker : public ObjectLiteralCheckerBase { 912 class ObjectLiteralChecker : public ObjectLiteralCheckerBase {
911 public: 913 public:
912 explicit ObjectLiteralChecker(ParserBase* parser) 914 explicit ObjectLiteralChecker(ParserBase* parser)
913 : ObjectLiteralCheckerBase(parser), has_seen_proto_(false) {} 915 : ObjectLiteralCheckerBase(parser), has_seen_proto_(false) {}
914 916
915 void CheckProperty(Token::Value property, PropertyKind type, bool is_static, 917 void CheckProperty(Token::Value property, PropertyKind type, bool is_static,
916 bool is_generator, bool* ok) override; 918 bool is_generator, bool* ok) override;
917 919
920 void JustSignature() override;
921
918 private: 922 private:
919 bool IsProto() { return this->scanner()->LiteralMatches("__proto__", 9); } 923 bool IsProto() { return this->scanner()->LiteralMatches("__proto__", 9); }
920 924
921 bool has_seen_proto_; 925 bool has_seen_proto_;
922 }; 926 };
923 927
924 // Validation per ES6 class literals. 928 // Validation per ES6 class literals.
925 class ClassLiteralChecker : public ObjectLiteralCheckerBase { 929 class ClassLiteralChecker : public ObjectLiteralCheckerBase {
926 public: 930 public:
927 explicit ClassLiteralChecker(ParserBase* parser) 931 explicit ClassLiteralChecker(ParserBase* parser)
928 : ObjectLiteralCheckerBase(parser), has_seen_constructor_(false) {} 932 : ObjectLiteralCheckerBase(parser), has_seen_constructor_(false) {}
929 933
930 void CheckProperty(Token::Value property, PropertyKind type, bool is_static, 934 void CheckProperty(Token::Value property, PropertyKind type, bool is_static,
931 bool is_generator, bool* ok) override; 935 bool is_generator, bool* ok) override;
932 936
937 void JustSignature() override;
938
933 private: 939 private:
934 bool IsConstructor() { 940 bool IsConstructor() {
935 return this->scanner()->LiteralMatches("constructor", 11); 941 return this->scanner()->LiteralMatches("constructor", 11);
936 } 942 }
937 bool IsPrototype() { 943 bool IsPrototype() {
938 return this->scanner()->LiteralMatches("prototype", 9); 944 return this->scanner()->LiteralMatches("prototype", 9);
939 } 945 }
940 946
941 bool has_seen_constructor_; 947 bool has_seen_constructor_;
942 }; 948 };
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after
1810 typesystem::TypeFlags type_flags = (is_get || is_set) 1816 typesystem::TypeFlags type_flags = (is_get || is_set)
1811 ? typesystem::kDisallowTypeParameters 1817 ? typesystem::kDisallowTypeParameters
1812 : typesystem::kNormalTypes; 1818 : typesystem::kNormalTypes;
1813 1819
1814 if (in_class && !is_static && this->IsConstructor(*name)) { 1820 if (in_class && !is_static && this->IsConstructor(*name)) {
1815 *has_seen_constructor = true; 1821 *has_seen_constructor = true;
1816 kind = has_extends ? FunctionKind::kSubclassConstructor 1822 kind = has_extends ? FunctionKind::kSubclassConstructor
1817 : FunctionKind::kBaseConstructor; 1823 : FunctionKind::kBaseConstructor;
1818 type_flags = typesystem::kConstructorTypes; 1824 type_flags = typesystem::kConstructorTypes;
1819 } 1825 }
1826 // Allow signatures when in a class.
1827 if (in_class) type_flags |= typesystem::kAllowSignature;
1820 1828
1821 value = this->ParseFunctionLiteral( 1829 value = this->ParseFunctionLiteral(
1822 *name, scanner()->location(), kSkipFunctionNameCheck, kind, 1830 *name, scanner()->location(), kSkipFunctionNameCheck, kind,
1823 RelocInfo::kNoPosition, FunctionLiteral::kAccessorOrMethod, 1831 RelocInfo::kNoPosition, FunctionLiteral::kAccessorOrMethod,
1824 language_mode(), type_flags, 1832 language_mode(), type_flags,
1825 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty)); 1833 CHECK_OK_CUSTOM(EmptyObjectLiteralProperty));
1826 1834
1835 // Return no property definition if just the signature was given.
1836 if (this->IsEmptyExpression(value)) {
1837 // Don't count constructor signature as a constructor.
1838 if (in_class && !is_static && this->IsConstructor(*name)) {
1839 // Note: we don't really need to unset has_seen_constructor
1840 checker->JustSignature();
1841 }
1842 return this->EmptyObjectLiteralProperty();
1843 }
1844
1827 return factory()->NewObjectLiteralProperty(name_expression, value, 1845 return factory()->NewObjectLiteralProperty(name_expression, value,
1828 ObjectLiteralProperty::COMPUTED, 1846 ObjectLiteralProperty::COMPUTED,
1829 is_static, *is_computed_name); 1847 is_static, *is_computed_name);
1830 } 1848 }
1831 1849
1832 if (in_class && name_token == Token::STATIC && !is_static) { 1850 if (in_class && name_token == Token::STATIC && !is_static) {
1833 // ClassElement (static) 1851 // ClassElement (static)
1834 // 'static' MethodDefinition 1852 // 'static' MethodDefinition
1835 *name = this->EmptyIdentifier(); 1853 *name = this->EmptyIdentifier();
1836 ObjectLiteralPropertyT property = ParsePropertyDefinition( 1854 ObjectLiteralPropertyT property = ParsePropertyDefinition(
(...skipping 1971 matching lines...) Expand 10 before | Expand all | Expand 10 after
3808 this->parser()->ReportMessage(MessageTemplate::kDuplicateConstructor); 3826 this->parser()->ReportMessage(MessageTemplate::kDuplicateConstructor);
3809 *ok = false; 3827 *ok = false;
3810 return; 3828 return;
3811 } 3829 }
3812 has_seen_constructor_ = true; 3830 has_seen_constructor_ = true;
3813 return; 3831 return;
3814 } 3832 }
3815 } 3833 }
3816 3834
3817 3835
3836 template <typename Traits>
3837 void ParserBase<Traits>::ObjectLiteralChecker::JustSignature() {}
3838
3839
3840 template <typename Traits>
3841 void ParserBase<Traits>::ClassLiteralChecker::JustSignature() {
3842 has_seen_constructor_ = false;
3843 }
3844
3845
3818 } // namespace internal 3846 } // namespace internal
3819 } // namespace v8 3847 } // namespace v8
3820 3848
3821 #endif // V8_PARSING_PARSER_BASE_H 3849 #endif // V8_PARSING_PARSER_BASE_H
OLDNEW
« no previous file with comments | « src/parsing/parser.h ('k') | src/parsing/preparser.h » ('j') | test/cctest/test-parsing.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698