Commit 2b90397d authored by nikolaos's avatar nikolaos Committed by Commit bot

Set up rewriting triggers

This patch implements eager expression rewriting when parsing.  It will
be used for desugaring spreads but may have other uses in the future.

We call Traits::RewriteExpression as soon as we realise that something
parsed as an expression is actually used as an expression (and not as
a pattern).  This patch adds a dummy implementation for this function,
doing no rewriting at all, and adds the trigers in the right places of
the parser.

R=rossberg@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1567603005

Cr-Commit-Position: refs/heads/master@{#33300}
parent 48a3227b
This diff is collapsed.
...@@ -1565,7 +1565,7 @@ Statement* Parser::ParseExportDefault(bool* ok) { ...@@ -1565,7 +1565,7 @@ Statement* Parser::ParseExportDefault(bool* ok) {
int pos = peek_position(); int pos = peek_position();
ExpressionClassifier classifier; ExpressionClassifier classifier;
Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK); Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK);
ValidateExpression(&classifier, CHECK_OK); expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK);
ExpectSemicolon(CHECK_OK); ExpectSemicolon(CHECK_OK);
result = factory()->NewExpressionStatement(expr, pos); result = factory()->NewExpressionStatement(expr, pos);
...@@ -2392,7 +2392,7 @@ void Parser::ParseVariableDeclarations(VariableDeclarationContext var_context, ...@@ -2392,7 +2392,7 @@ void Parser::ParseVariableDeclarations(VariableDeclarationContext var_context,
value = ParseAssignmentExpression(var_context != kForStatement, value = ParseAssignmentExpression(var_context != kForStatement,
&classifier, ok); &classifier, ok);
if (!*ok) return; if (!*ok) return;
ValidateExpression(&classifier, ok); value = ParserTraits::RewriteNonPattern(value, &classifier, ok);
if (!*ok) return; if (!*ok) return;
variable_loc.end_pos = scanner()->location().end_pos; variable_loc.end_pos = scanner()->location().end_pos;
...@@ -2503,7 +2503,7 @@ Statement* Parser::ParseExpressionOrLabelledStatement( ...@@ -2503,7 +2503,7 @@ Statement* Parser::ParseExpressionOrLabelledStatement(
} else { } else {
expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK); expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK);
} }
ValidateExpression(&classifier, CHECK_OK); expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK);
switch (peek()) { switch (peek()) {
case Token::SEMICOLON: case Token::SEMICOLON:
Consume(Token::SEMICOLON); Consume(Token::SEMICOLON);
...@@ -3724,7 +3724,8 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, ...@@ -3724,7 +3724,8 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
if (is_destructuring) { if (is_destructuring) {
ValidateAssignmentPattern(&classifier, CHECK_OK); ValidateAssignmentPattern(&classifier, CHECK_OK);
} else { } else {
ValidateExpression(&classifier, CHECK_OK); expression =
ParserTraits::RewriteNonPattern(expression, &classifier, CHECK_OK);
} }
if (is_for_each) { if (is_for_each) {
...@@ -4748,7 +4749,7 @@ ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name, ...@@ -4748,7 +4749,7 @@ ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name,
block_scope->set_start_position(scanner()->location().end_pos); block_scope->set_start_position(scanner()->location().end_pos);
ExpressionClassifier classifier; ExpressionClassifier classifier;
extends = ParseLeftHandSideExpression(&classifier, CHECK_OK); extends = ParseLeftHandSideExpression(&classifier, CHECK_OK);
ValidateExpression(&classifier, CHECK_OK); extends = ParserTraits::RewriteNonPattern(extends, &classifier, CHECK_OK);
} else { } else {
block_scope->set_start_position(scanner()->location().end_pos); block_scope->set_start_position(scanner()->location().end_pos);
} }
...@@ -4774,7 +4775,8 @@ ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name, ...@@ -4774,7 +4775,8 @@ ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name,
ObjectLiteral::Property* property = ParsePropertyDefinition( ObjectLiteral::Property* property = ParsePropertyDefinition(
&checker, in_class, has_extends, is_static, &is_computed_name, &checker, in_class, has_extends, is_static, &is_computed_name,
&has_seen_constructor, &classifier, &name, CHECK_OK); &has_seen_constructor, &classifier, &name, CHECK_OK);
ValidateExpression(&classifier, CHECK_OK); property = ParserTraits::RewriteNonPatternObjectLiteralProperty(
property, &classifier, CHECK_OK);
if (has_seen_constructor && constructor == NULL) { if (has_seen_constructor && constructor == NULL) {
constructor = GetPropertyValue(property)->AsFunctionLiteral(); constructor = GetPropertyValue(property)->AsFunctionLiteral();
...@@ -4825,7 +4827,7 @@ Expression* Parser::ParseV8Intrinsic(bool* ok) { ...@@ -4825,7 +4827,7 @@ Expression* Parser::ParseV8Intrinsic(bool* ok) {
ExpressionClassifier classifier; ExpressionClassifier classifier;
ZoneList<Expression*>* args = ZoneList<Expression*>* args =
ParseArguments(&spread_pos, &classifier, CHECK_OK); ParseArguments(&spread_pos, &classifier, CHECK_OK);
ValidateExpression(&classifier, CHECK_OK); args = RewriteNonPatternArguments(args, &classifier, CHECK_OK);
DCHECK(!spread_pos.IsValid()); DCHECK(!spread_pos.IsValid());
...@@ -5388,6 +5390,58 @@ void ParserTraits::RewriteDestructuringAssignments() { ...@@ -5388,6 +5390,58 @@ void ParserTraits::RewriteDestructuringAssignments() {
} }
Expression* ParserTraits::RewriteNonPattern(
Expression* expr, const ExpressionClassifier* classifier, bool* ok) {
return parser_->RewriteNonPattern(expr, classifier, ok);
}
ZoneList<Expression*>* ParserTraits::RewriteNonPatternArguments(
ZoneList<Expression*>* args, const ExpressionClassifier* classifier,
bool* ok) {
return parser_->RewriteNonPatternArguments(args, classifier, ok);
}
ObjectLiteralProperty* ParserTraits::RewriteNonPatternObjectLiteralProperty(
ObjectLiteralProperty* property, const ExpressionClassifier* classifier,
bool* ok) {
return parser_->RewriteNonPatternObjectLiteralProperty(property, classifier,
ok);
}
Expression* Parser::RewriteNonPattern(Expression* expr,
const ExpressionClassifier* classifier,
bool* ok) {
// For the time being, this does no rewriting at all.
ValidateExpression(classifier, ok);
return expr;
}
ZoneList<Expression*>* Parser::RewriteNonPatternArguments(
ZoneList<Expression*>* args, const ExpressionClassifier* classifier,
bool* ok) {
// For the time being, this does no rewriting at all.
ValidateExpression(classifier, ok);
return args;
}
ObjectLiteralProperty* Parser::RewriteNonPatternObjectLiteralProperty(
ObjectLiteralProperty* property, const ExpressionClassifier* classifier,
bool* ok) {
if (property != nullptr) {
Expression* key = RewriteNonPattern(property->key(), classifier, ok);
property->set_key(key);
Expression* value = RewriteNonPattern(property->value(), classifier, ok);
property->set_value(value);
}
return property;
}
void Parser::RewriteDestructuringAssignments() { void Parser::RewriteDestructuringAssignments() {
FunctionState* func = function_state_; FunctionState* func = function_state_;
if (!allow_harmony_destructuring_assignment()) return; if (!allow_harmony_destructuring_assignment()) return;
......
...@@ -649,6 +649,16 @@ class ParserTraits { ...@@ -649,6 +649,16 @@ class ParserTraits {
void SetFunctionNameFromIdentifierRef(Expression* value, void SetFunctionNameFromIdentifierRef(Expression* value,
Expression* identifier); Expression* identifier);
// Rewrite expressions that are not used as patterns
V8_INLINE Expression* RewriteNonPattern(
Expression* expr, const ExpressionClassifier* classifier, bool* ok);
V8_INLINE ZoneList<Expression*>* RewriteNonPatternArguments(
ZoneList<Expression*>* args, const ExpressionClassifier* classifier,
bool* ok);
V8_INLINE ObjectLiteralProperty* RewriteNonPatternObjectLiteralProperty(
ObjectLiteralProperty* property, const ExpressionClassifier* classifier,
bool* ok);
private: private:
Parser* parser_; Parser* parser_;
}; };
...@@ -1003,6 +1013,15 @@ class Parser : public ParserBase<ParserTraits> { ...@@ -1003,6 +1013,15 @@ class Parser : public ParserBase<ParserTraits> {
V8_INLINE void RewriteDestructuringAssignments(); V8_INLINE void RewriteDestructuringAssignments();
V8_INLINE Expression* RewriteNonPattern(
Expression* expr, const ExpressionClassifier* classifier, bool* ok);
V8_INLINE ZoneList<Expression*>* RewriteNonPatternArguments(
ZoneList<Expression*>* args, const ExpressionClassifier* classifier,
bool* ok);
V8_INLINE ObjectLiteralProperty* RewriteNonPatternObjectLiteralProperty(
ObjectLiteralProperty* property, const ExpressionClassifier* classifier,
bool* ok);
friend class InitializerRewriter; friend class InitializerRewriter;
void RewriteParameterInitializer(Expression* expr, Scope* scope); void RewriteParameterInitializer(Expression* expr, Scope* scope);
......
...@@ -932,6 +932,16 @@ class PreParserTraits { ...@@ -932,6 +932,16 @@ class PreParserTraits {
void SetFunctionNameFromIdentifierRef(PreParserExpression, void SetFunctionNameFromIdentifierRef(PreParserExpression,
PreParserExpression) {} PreParserExpression) {}
inline PreParserExpression RewriteNonPattern(
PreParserExpression expr, const ExpressionClassifier* classifier,
bool* ok);
inline PreParserExpression RewriteNonPatternArguments(
PreParserExpression args, const ExpressionClassifier* classifier,
bool* ok);
inline PreParserExpression RewriteNonPatternObjectLiteralProperty(
PreParserExpression property, const ExpressionClassifier* classifier,
bool* ok);
private: private:
PreParser* pre_parser_; PreParser* pre_parser_;
}; };
...@@ -1113,6 +1123,30 @@ PreParserExpression PreParserTraits::ParseDoExpression(bool* ok) { ...@@ -1113,6 +1123,30 @@ PreParserExpression PreParserTraits::ParseDoExpression(bool* ok) {
} }
PreParserExpression PreParserTraits::RewriteNonPattern(
PreParserExpression expr, const ExpressionClassifier* classifier,
bool* ok) {
pre_parser_->ValidateExpression(classifier, ok);
return expr;
}
PreParserExpression PreParserTraits::RewriteNonPatternArguments(
PreParserExpression args, const ExpressionClassifier* classifier,
bool* ok) {
pre_parser_->ValidateExpression(classifier, ok);
return args;
}
PreParserExpression PreParserTraits::RewriteNonPatternObjectLiteralProperty(
PreParserExpression property, const ExpressionClassifier* classifier,
bool* ok) {
pre_parser_->ValidateExpression(classifier, ok);
return property;
}
PreParserStatementList PreParser::ParseEagerFunctionBody( PreParserStatementList PreParser::ParseEagerFunctionBody(
PreParserIdentifier function_name, int pos, PreParserIdentifier function_name, int pos,
const PreParserFormalParameters& parameters, FunctionKind kind, const PreParserFormalParameters& parameters, FunctionKind kind,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment