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

Unified Diff: src/parsing/preparser.cc

Issue 1841093002: Add optional types to function/method declarations (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@types-1817353007-typ-mod
Patch Set: Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: src/parsing/preparser.cc
diff --git a/src/parsing/preparser.cc b/src/parsing/preparser.cc
index d81bd6dded99d7ff8cbea42039232e324d2d9654..41b3d63206fe1d3c0183c94806e035c1fc3530bf 100644
--- a/src/parsing/preparser.cc
+++ b/src/parsing/preparser.cc
@@ -94,10 +94,10 @@ PreParserExpression PreParserTraits::ParseFunctionLiteral(
PreParserIdentifier name, Scanner::Location function_name_location,
FunctionNameValidity function_name_validity, FunctionKind kind,
int function_token_position, FunctionLiteral::FunctionType type,
- LanguageMode language_mode, bool* ok) {
+ LanguageMode language_mode, typesystem::TypeFlags type_flags, bool* ok) {
return pre_parser_->ParseFunctionLiteral(
name, function_name_location, function_name_validity, kind,
- function_token_position, type, language_mode, ok);
+ function_token_position, type, language_mode, type_flags, ok);
}
@@ -419,7 +419,7 @@ PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
is_generator ? FunctionKind::kGeneratorFunction
: FunctionKind::kNormalFunction,
pos, FunctionLiteral::kDeclaration, language_mode(),
- CHECK_OK);
+ typesystem::kAllowSignature, CHECK_OK);
return Statement::FunctionDeclaration();
}
@@ -546,8 +546,8 @@ PreParser::Statement PreParser::ParseVariableDeclarations(
is_pattern = pattern.IsObjectLiteral() || pattern.IsArrayLiteral();
- // Optional type annotation.
- if (scope_->typed() && Check(Token::COLON)) {
+ // Parse optional type annotation.
+ if (scope_->typed() && Check(Token::COLON)) { // Braces required here.
ParseValidType(CHECK_OK);
}
@@ -1009,7 +1009,7 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
Identifier function_name, Scanner::Location function_name_location,
FunctionNameValidity function_name_validity, FunctionKind kind,
int function_token_pos, FunctionLiteral::FunctionType function_type,
- LanguageMode language_mode, bool* ok) {
+ LanguageMode language_mode, typesystem::TypeFlags type_flags, bool* ok) {
// Function ::
// '(' FormalParameterList? ')' '{' FunctionBody '}'
@@ -1023,6 +1023,12 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
DuplicateFinder duplicate_finder(scanner()->unicode_cache());
ExpressionClassifier formals_classifier(this, &duplicate_finder);
+ // Parse optional type parameters.
+ if (scope_->typed() && !(type_flags & typesystem::kDisallowTypeParameters) &&
+ peek() == Token::LT) { // Braces required here.
+ ParseTypeParameters(CHECK_OK);
+ }
+
Expect(Token::LPAREN, CHECK_OK);
int start_position = scanner()->location().beg_pos;
function_scope->set_start_position(start_position);
@@ -1040,6 +1046,18 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
(outer_is_script_scope && allow_lazy() && !parenthesized_function_);
parenthesized_function_ = false;
+ // Parse optional type annotation.
+ if (scope_->typed() && Check(Token::COLON)) { // Braces required here.
+ ParseValidType(CHECK_OK);
+ }
+
+ // Allow for a function signature (i.e., a literal without body).
+ if (peek() != Token::LBRACE && scope_->typed() &&
+ (type_flags & typesystem::kAllowSignature)) {
+ ExpectSemicolon(CHECK_OK);
+ return this->EmptyExpression();
+ }
+
Expect(Token::LBRACE, CHECK_OK);
if (is_lazily_parsed) {
ParseLazyFunctionLiteralBody(CHECK_OK);

Powered by Google App Engine
This is Rietveld 408576698