Commit ea8f7827 authored by adamk's avatar adamk Committed by Commit bot

Remove redundant/unnecessary variables and checks in ParseForStatement

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

Cr-Commit-Position: refs/heads/master@{#33748}
parent 07d05ddd
......@@ -831,6 +831,10 @@ class ForEachStatement : public IterationStatement {
FeedbackVectorSlotCache* cache) override;
FeedbackVectorSlot EachFeedbackSlot() const { return each_slot_; }
static const char* VisitModeString(VisitMode mode) {
return mode == ITERATE ? "for-of" : "for-in";
}
protected:
ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
: IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
......
......@@ -71,7 +71,6 @@ class CallSite {
int32_t pos_;
};
#define MESSAGE_TEMPLATES(T) \
/* Error */ \
T(None, "") \
......@@ -387,12 +386,10 @@ class CallSite {
T(DuplicateExport, "Duplicate export of '%'") \
T(DuplicateProto, \
"Duplicate __proto__ fields are not allowed in object literals") \
T(ForInLoopInitializer, \
"for-in loop variable declaration may not have an initializer.") \
T(ForInOfLoopInitializer, \
"% loop variable declaration may not have an initializer.") \
T(ForInOfLoopMultiBindings, \
"Invalid left-hand side in % loop: Must have a single binding.") \
T(ForOfLoopInitializer, \
"for-of loop variable declaration may not have an initializer.") \
T(IllegalAccess, "Illegal access") \
T(IllegalBreak, "Illegal break statement") \
T(IllegalContinue, "Illegal continue statement") \
......
......@@ -3331,9 +3331,8 @@ void Parser::InitializeForEachStatement(ForEachStatement* stmt,
}
}
Statement* Parser::DesugarLexicalBindingsInForStatement(
Scope* inner_scope, bool is_const, ZoneList<const AstRawString*>* names,
Scope* inner_scope, VariableMode mode, ZoneList<const AstRawString*>* names,
ForStatement* loop, Statement* init, Expression* cond, Statement* next,
Statement* body, bool* ok) {
// ES6 13.7.4.8 specifies that on each loop iteration the let variables are
......@@ -3438,7 +3437,6 @@ Statement* Parser::DesugarLexicalBindingsInForStatement(
ZoneList<Variable*> inner_vars(names->length(), zone());
// For each let variable x:
// make statement: let/const x = temp_x.
VariableMode mode = is_const ? CONST : LET;
for (int i = 0; i < names->length(); i++) {
VariableProxy* proxy = NewUnresolved(names->at(i), mode);
Declaration* declaration = factory()->NewVariableDeclaration(
......@@ -3588,7 +3586,6 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
// 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
int stmt_pos = peek_position();
bool is_const = false;
Statement* init = NULL;
ZoneList<const AstRawString*> lexical_bindings(1, zone());
......@@ -3605,22 +3602,18 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
if (peek() == Token::VAR || (peek() == Token::CONST && allow_const()) ||
(peek() == Token::LET && IsNextLetKeyword())) {
ParseVariableDeclarations(kForStatement, &parsing_result, CHECK_OK);
is_const = parsing_result.descriptor.mode == CONST;
int num_decl = parsing_result.declarations.length();
bool accept_IN = num_decl >= 1;
ForEachStatement::VisitMode mode;
int each_beg_pos = scanner()->location().beg_pos;
int each_end_pos = scanner()->location().end_pos;
if (accept_IN && CheckInOrOf(&mode, ok)) {
if (CheckInOrOf(&mode, ok)) {
if (!*ok) return nullptr;
if (num_decl != 1) {
const char* loop_type =
mode == ForEachStatement::ITERATE ? "for-of" : "for-in";
if (parsing_result.declarations.length() != 1) {
ParserTraits::ReportMessageAt(
parsing_result.bindings_loc,
MessageTemplate::kForInOfLoopMultiBindings, loop_type);
MessageTemplate::kForInOfLoopMultiBindings,
ForEachStatement::VisitModeString(mode));
*ok = false;
return nullptr;
}
......@@ -3630,14 +3623,10 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
(is_strict(language_mode()) || mode == ForEachStatement::ITERATE ||
IsLexicalVariableMode(parsing_result.descriptor.mode) ||
!decl.pattern->IsVariableProxy())) {
if (mode == ForEachStatement::ITERATE) {
ReportMessageAt(parsing_result.first_initializer_loc,
MessageTemplate::kForOfLoopInitializer);
} else {
// TODO(caitp): This should be an error in sloppy mode too.
ReportMessageAt(parsing_result.first_initializer_loc,
MessageTemplate::kForInLoopInitializer);
}
ParserTraits::ReportMessageAt(
parsing_result.first_initializer_loc,
MessageTemplate::kForInOfLoopInitializer,
ForEachStatement::VisitModeString(mode));
*ok = false;
return nullptr;
}
......@@ -3895,8 +3884,8 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
if (lexical_bindings.length() > 0) {
BlockState block_state(&scope_, for_scope);
result = DesugarLexicalBindingsInForStatement(
inner_scope, is_const, &lexical_bindings, loop, init, cond,
next, body, CHECK_OK);
inner_scope, parsing_result.descriptor.mode, &lexical_bindings, loop,
init, cond, next, body, CHECK_OK);
for_scope->set_end_position(scanner()->location().end_pos);
} else {
for_scope->set_end_position(scanner()->location().end_pos);
......
......@@ -917,9 +917,9 @@ class Parser : public ParserBase<ParserTraits> {
Expression* subject, Statement* body,
bool is_destructuring);
Statement* DesugarLexicalBindingsInForStatement(
Scope* inner_scope, bool is_const, ZoneList<const AstRawString*>* names,
ForStatement* loop, Statement* init, Expression* cond, Statement* next,
Statement* body, bool* ok);
Scope* inner_scope, VariableMode mode,
ZoneList<const AstRawString*>* names, ForStatement* loop, Statement* init,
Expression* cond, Statement* next, Statement* body, bool* ok);
void RewriteDoExpression(Expression* expr, bool* ok);
......
......@@ -924,29 +924,21 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
ParseVariableDeclarations(kForStatement, &decl_count, &is_lexical,
&is_binding_pattern, &first_initializer_loc,
&bindings_loc, CHECK_OK);
bool accept_IN = decl_count >= 1;
if (accept_IN && CheckInOrOf(&mode, ok)) {
if (CheckInOrOf(&mode, ok)) {
if (!*ok) return Statement::Default();
if (decl_count != 1) {
const char* loop_type =
mode == ForEachStatement::ITERATE ? "for-of" : "for-in";
PreParserTraits::ReportMessageAt(
bindings_loc, MessageTemplate::kForInOfLoopMultiBindings,
loop_type);
ForEachStatement::VisitModeString(mode));
*ok = false;
return Statement::Default();
}
if (first_initializer_loc.IsValid() &&
(is_strict(language_mode()) || mode == ForEachStatement::ITERATE ||
is_lexical || is_binding_pattern)) {
if (mode == ForEachStatement::ITERATE) {
ReportMessageAt(first_initializer_loc,
MessageTemplate::kForOfLoopInitializer);
} else {
// TODO(caitp): This should be an error in sloppy mode, too.
ReportMessageAt(first_initializer_loc,
MessageTemplate::kForInLoopInitializer);
}
PreParserTraits::ReportMessageAt(
first_initializer_loc, MessageTemplate::kForInOfLoopInitializer,
ForEachStatement::VisitModeString(mode));
*ok = false;
return Statement::Default();
}
......
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