Commit 1e0f101a authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

[scanner] Reduce branches for IsIdentifier and IsContextualKeyword

Bug: v8:7926
Change-Id: I1c3b831bb19c70e1e7c99f45ae50dbd1351ce2a7
Reviewed-on: https://chromium-review.googlesource.com/1202105
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55637}
parent 2a237ed4
......@@ -149,18 +149,20 @@ namespace internal {
T(STRING, nullptr, 0) \
T(BIGINT, nullptr, 0) \
\
/* BEGIN AnyIdentifier. */ \
/* Identifiers (not keywords or future reserved words). */ \
T(IDENTIFIER, nullptr, 0) \
K(ASYNC, "async", 0) \
/* `await` is a reserved word in module code only */ \
K(AWAIT, "await", 0) \
K(ENUM, "enum", 0) \
K(LET, "let", 0) \
K(STATIC, "static", 0) \
K(YIELD, "yield", 0) \
/* Future reserved words (ECMA-262, section 7.6.1.2). */ \
T(FUTURE_STRICT_RESERVED_WORD, nullptr, 0) \
T(ESCAPED_STRICT_RESERVED_WORD, nullptr, 0) \
K(LET, "let", 0) \
K(STATIC, "static", 0) \
K(ENUM, "enum", 0) \
/* `await` is a reserved word in module code only */ \
K(AWAIT, "await", 0) \
K(YIELD, "yield", 0) \
/* END AnyIdentifier. */ \
K(CLASS, "class", 0) \
K(CONST, "const", 0) \
K(EXPORT, "export", 0) \
......@@ -214,31 +216,29 @@ class Token {
return name_[tok];
}
static char TypeForTesting(Value tok) { return token_type[tok]; }
// Predicates
static bool IsKeyword(Value tok) {
return token_type[tok] == 'K';
}
static bool IsContextualKeyword(Value tok) { return token_type[tok] == 'C'; }
static bool IsContextualKeyword(Value tok) {
return IsInRange(tok, GET, ANONYMOUS);
}
static bool IsIdentifier(Value tok, LanguageMode language_mode,
bool is_generator, bool disallow_await) {
switch (tok) {
case IDENTIFIER:
case ASYNC:
return true;
case ESCAPED_STRICT_RESERVED_WORD:
case FUTURE_STRICT_RESERVED_WORD:
case LET:
case STATIC:
return is_sloppy(language_mode);
case YIELD:
return !is_generator && is_sloppy(language_mode);
case AWAIT:
return !disallow_await;
default:
return false;
if (IsInRange(tok, IDENTIFIER, ASYNC)) return true;
if (IsInRange(tok, FUTURE_STRICT_RESERVED_WORD, STATIC)) {
return is_sloppy(language_mode);
}
UNREACHABLE();
if (tok == AWAIT) return !disallow_await;
if (tok == YIELD) return !is_generator && is_sloppy(language_mode);
return false;
}
static bool IsAnyIdentifier(Value tok) {
return IsInRange(tok, IDENTIFIER, YIELD);
}
static bool IsAssignmentOp(Value tok) {
......@@ -255,10 +255,6 @@ class Token {
static bool IsEqualityOp(Value op) { return IsInRange(op, EQ, EQ_STRICT); }
static bool IsAnyIdentifier(Value tok) {
return IsInRange(tok, IDENTIFIER, ESCAPED_STRICT_RESERVED_WORD);
}
static Value BinaryOpForAssignment(Value op) {
DCHECK(IsInRange(op, ASSIGN_BIT_OR, ASSIGN_EXP));
Value result = static_cast<Value>(op - ASSIGN_BIT_OR + BIT_OR);
......
......@@ -71,6 +71,14 @@ void MockUseCounterCallback(v8::Isolate* isolate,
} // namespace
TEST(IsContextualKeyword) {
for (int i = 0; i < Token::NUM_TOKENS; i++) {
Token::Value tok = static_cast<Token::Value>(i);
CHECK_EQ(Token::TypeForTesting(tok) == 'C',
Token::IsContextualKeyword(tok));
}
}
bool TokenIsAnyIdentifier(Token::Value tok) {
switch (tok) {
case Token::IDENTIFIER:
......@@ -95,6 +103,44 @@ TEST(AnyIdentifierToken) {
}
}
bool TokenIsIdentifier(Token::Value tok, LanguageMode language_mode,
bool is_generator, bool disallow_await) {
switch (tok) {
case Token::IDENTIFIER:
case Token::ASYNC:
return true;
case Token::ESCAPED_STRICT_RESERVED_WORD:
case Token::FUTURE_STRICT_RESERVED_WORD:
case Token::LET:
case Token::STATIC:
return is_sloppy(language_mode);
case Token::YIELD:
return !is_generator && is_sloppy(language_mode);
case Token::AWAIT:
return !disallow_await;
default:
return false;
}
UNREACHABLE();
}
TEST(IsIdentifier) {
for (int i = 0; i < Token::NUM_TOKENS; i++) {
Token::Value tok = static_cast<Token::Value>(i);
for (size_t raw_language_mode = 0; raw_language_mode < LanguageModeSize;
raw_language_mode++) {
LanguageMode mode = static_cast<LanguageMode>(raw_language_mode);
for (int is_generator = 0; is_generator < 2; is_generator++) {
for (int disallow_await = 0; disallow_await < 2; disallow_await++) {
CHECK_EQ(
TokenIsIdentifier(tok, mode, is_generator, disallow_await),
Token::IsIdentifier(tok, mode, is_generator, disallow_await));
}
}
}
}
}
bool TokenIsAssignmentOp(Token::Value tok) {
switch (tok) {
case Token::INIT:
......
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