[parser] Make IsValidPattern a range-check over Object/Array literal

Change-Id: I53fc5b8ff39c91fe509e292cf32f54ff4f8e2eb7
Reviewed-on: https://chromium-review.googlesource.com/c/1335694
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57508}
This commit is contained in:
Toon Verwaest 2018-11-14 15:52:51 +01:00 committed by Commit Bot
parent 96cf84763f
commit 40b06bc450
3 changed files with 13 additions and 6 deletions

View File

@ -250,6 +250,11 @@ class Expression : public AstNode {
bool IsCompileTimeValue();
bool IsValidPattern() {
STATIC_ASSERT(kObjectLiteral + 1 == kArrayLiteral);
return IsInRange(node_type(), kObjectLiteral, kArrayLiteral);
}
protected:
Expression(int pos, NodeType type) : AstNode(pos, type) {}

View File

@ -1211,10 +1211,6 @@ class ParserBase {
return true;
}
bool IsValidPattern(ExpressionT expression) {
return expression->IsObjectLiteral() || expression->IsArrayLiteral();
}
// Due to hoisting, the value of a 'var'-declared variable may actually change
// even if the code contains only the "initial" assignment, namely when that
// assignment occurs inside a loop. For example:
@ -2689,7 +2685,7 @@ ParserBase<Impl>::ParseAssignmentExpression() {
}
// Destructuring assignmment.
if (V8_UNLIKELY(IsValidPattern(expression) && op == Token::ASSIGN)) {
if (V8_UNLIKELY(expression->IsValidPattern() && op == Token::ASSIGN)) {
ValidateAssignmentPattern();
// This is definitely not an expression so don't accumulate
@ -4453,7 +4449,7 @@ bool ParserBase<Impl>::IsValidReferenceExpression(ExpressionT expression) {
template <typename Impl>
void ParserBase<Impl>::CheckDestructuringElement(ExpressionT expression,
int begin, int end) {
if (!IsValidPattern(expression) && !expression->IsAssignment() &&
if (!expression->IsValidPattern() && !expression->IsAssignment() &&
!IsValidReferenceExpression(expression)) {
if (expression->IsProperty()) ValidateExpression();
classifier()->RecordAssignmentPatternError(

View File

@ -236,6 +236,12 @@ class PreParserExpression {
return TypeField::decode(code_) == kArrayLiteralExpression;
}
bool IsValidPattern() const {
STATIC_ASSERT(kObjectLiteralExpression + 1 == kArrayLiteralExpression);
return IsInRange(TypeField::decode(code_),
kObjectLiteralExpression, kArrayLiteralExpression);
}
bool IsStringLiteral() const {
return TypeField::decode(code_) == kStringLiteralExpression;
}