Commit e5e46858 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[parser] Cache EmptyStatement and always kNoSourcePosition

Change-Id: I27e2e0529281008b8350e1dd219c0d38bdcb66f5
Reviewed-on: https://chromium-review.googlesource.com/c/1307424
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57136}
parent fd564737
......@@ -991,7 +991,7 @@ class DebuggerStatement final : public Statement {
class EmptyStatement final : public Statement {
private:
friend class AstNodeFactory;
explicit EmptyStatement(int pos) : Statement(pos, kEmptyStatement) {}
EmptyStatement() : Statement(kNoSourcePosition, kEmptyStatement) {}
};
......@@ -2869,7 +2869,9 @@ class AstVisitor {
class AstNodeFactory final {
public:
AstNodeFactory(AstValueFactory* ast_value_factory, Zone* zone)
: zone_(zone), ast_value_factory_(ast_value_factory) {}
: zone_(zone),
ast_value_factory_(ast_value_factory),
empty_statement_(new (zone) class EmptyStatement()) {}
AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
......@@ -3010,13 +3012,12 @@ class AstNodeFactory final {
return new (zone_) DebuggerStatement(pos);
}
EmptyStatement* NewEmptyStatement(int pos) {
return new (zone_) EmptyStatement(pos);
class EmptyStatement* EmptyStatement() {
return empty_statement_;
}
SloppyBlockFunctionStatement* NewSloppyBlockFunctionStatement() {
return new (zone_)
SloppyBlockFunctionStatement(NewEmptyStatement(kNoSourcePosition));
return new (zone_) SloppyBlockFunctionStatement(EmptyStatement());
}
CaseClause* NewCaseClause(Expression* label,
......@@ -3386,6 +3387,7 @@ class AstNodeFactory final {
// See ParseFunctionLiteral in parser.cc for preconditions.
Zone* zone_;
AstValueFactory* ast_value_factory_;
class EmptyStatement* empty_statement_;
};
......
......@@ -4662,8 +4662,8 @@ ParserBase<Impl>::ParseStatementList(StatementListT body,
while (peek() != end_token) {
StatementT stat = ParseStatementListItem();
RETURN_IF_PARSE_ERROR_CUSTOM(Return, kLazyParsingComplete)
if (impl()->IsNull(stat) || stat->IsEmptyStatement()) continue;
RETURN_IF_PARSE_ERROR_CUSTOM(Return, kLazyParsingComplete);
if (stat->IsEmptyStatement()) continue;
body->Add(stat, zone());
}
......@@ -4753,7 +4753,7 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseStatement(
return ParseBlock(labels);
case Token::SEMICOLON:
Next();
return factory()->NewEmptyStatement(kNoSourcePosition);
return factory()->EmptyStatement();
case Token::IF:
return ParseIfStatement(labels);
case Token::DO:
......@@ -4842,9 +4842,8 @@ typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseBlock(
while (peek() != Token::RBRACE) {
StatementT stat = ParseStatementListItem();
RETURN_IF_PARSE_ERROR_CUSTOM(NullStatement);
if (!impl()->IsNull(stat) && !stat->IsEmptyStatement()) {
body->statements()->Add(stat, zone());
}
if (stat->IsEmptyStatement()) continue;
body->statements()->Add(stat, zone());
}
Expect(Token::RBRACE);
......@@ -5011,7 +5010,7 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseIfStatement(
else_statement = ParseScopedStatement(labels);
else_range = SourceRange::ContinuationOf(then_range, end_position());
} else {
else_statement = factory()->NewEmptyStatement(kNoSourcePosition);
else_statement = factory()->EmptyStatement();
}
StatementT stmt =
factory()->NewIfStatement(condition, then_statement, else_statement, pos);
......@@ -5074,7 +5073,7 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseBreakStatement(
// empty statements, e.g. 'l1: l2: l3: break l2;'
if (!impl()->IsNull(label) && impl()->ContainsLabel(labels, label)) {
ExpectSemicolon();
return factory()->NewEmptyStatement(pos);
return factory()->EmptyStatement();
}
typename Types::BreakableStatement target = impl()->LookupBreakTarget(label);
if (impl()->IsNull(target)) {
......@@ -5296,6 +5295,7 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseSwitchStatement(
peek() != Token::RBRACE) {
StatementT stat = ParseStatementListItem();
RETURN_IF_PARSE_ERROR;
if (stat->IsEmptyStatement()) continue;
statements.Add(stat);
}
}
......
......@@ -923,7 +923,7 @@ Statement* Parser::ParseModuleItem() {
if ((!allow_harmony_dynamic_import() || peek_ahead != Token::LPAREN) &&
(!allow_harmony_import_meta() || peek_ahead != Token::PERIOD)) {
ParseImportDeclaration();
return factory()->NewEmptyStatement(kNoSourcePosition);
return factory()->EmptyStatement();
}
}
......@@ -943,9 +943,8 @@ void Parser::ParseModuleItemList(ZonePtrList<Statement>* body) {
while (peek() != Token::EOS) {
Statement* stat = ParseModuleItem();
RETURN_IF_PARSE_ERROR_VOID;
if (stat && !stat->IsEmpty()) {
body->Add(stat, zone());
}
if (stat->IsEmptyStatement()) continue;
body->Add(stat, zone());
}
}
......@@ -1301,7 +1300,7 @@ Statement* Parser::ParseExportDeclaration() {
case Token::MUL:
ParseExportStar();
return factory()->NewEmptyStatement(position());
return factory()->EmptyStatement();
case Token::LBRACE: {
// There are two cases here:
......@@ -1315,7 +1314,6 @@ Statement* Parser::ParseExportDeclaration() {
// pass in a location that gets filled with the first reserved word
// encountered, and then throw a SyntaxError if we are in the
// non-FromClause case.
int pos = position();
Scanner::Location reserved_loc = Scanner::Location::invalid();
ZoneChunkList<ExportClauseData>* export_data =
ParseExportClause(&reserved_loc);
......@@ -1345,7 +1343,7 @@ Statement* Parser::ParseExportDeclaration() {
zone());
}
}
return factory()->NewEmptyStatement(pos);
return factory()->EmptyStatement();
}
case Token::FUNCTION:
......@@ -1485,7 +1483,7 @@ Statement* Parser::DeclareFunction(const AstRawString* variable_name,
statement);
return statement;
}
return factory()->NewEmptyStatement(kNoSourcePosition);
return factory()->EmptyStatement();
}
Statement* Parser::DeclareClass(const AstRawString* variable_name,
......@@ -2315,7 +2313,7 @@ Statement* Parser::DesugarLexicalBindingsInForStatement(
if (cond) {
Statement* stop =
factory()->NewBreakStatement(outer_loop, kNoSourcePosition);
Statement* noop = factory()->NewEmptyStatement(kNoSourcePosition);
Statement* noop = factory()->EmptyStatement();
ignore_completion_block->statements()->Add(
factory()->NewIfStatement(cond, noop, stop, cond->position()),
zone());
......@@ -2378,7 +2376,7 @@ Statement* Parser::DesugarLexicalBindingsInForStatement(
}
Statement* stop =
factory()->NewBreakStatement(outer_loop, kNoSourcePosition);
Statement* empty = factory()->NewEmptyStatement(kNoSourcePosition);
Statement* empty = factory()->EmptyStatement();
Statement* if_flag_break =
factory()->NewIfStatement(compare, stop, empty, kNoSourcePosition);
inner_block->statements()->Add(IgnoreCompletion(if_flag_break), zone());
......@@ -2848,7 +2846,7 @@ Statement* Parser::BuildAssertIsCoercible(Variable* var,
IfStatement* if_statement = factory()->NewIfStatement(
condition,
factory()->NewExpressionStatement(throw_type_error, kNoSourcePosition),
factory()->NewEmptyStatement(kNoSourcePosition), kNoSourcePosition);
factory()->EmptyStatement(), kNoSourcePosition);
return if_statement;
}
......@@ -3713,7 +3711,7 @@ Statement* Parser::CheckCallable(Variable* var, Expression* error, int pos) {
Statement* throw_call = factory()->NewExpressionStatement(error, pos);
validate_var = factory()->NewIfStatement(
condition, factory()->NewEmptyStatement(nopos), throw_call, nopos);
condition, factory()->EmptyStatement(), throw_call, nopos);
}
return validate_var;
}
......@@ -3764,7 +3762,7 @@ void Parser::BuildIteratorClose(ZonePtrList<Statement>* statements,
Statement* return_input = BuildReturnStatement(value, nopos);
check_return = factory()->NewIfStatement(
condition, return_input, factory()->NewEmptyStatement(nopos), nopos);
condition, return_input, factory()->EmptyStatement(), nopos);
}
// output = %_Call(iteratorReturn, iterator, input);
......@@ -3808,8 +3806,7 @@ void Parser::BuildIteratorClose(ZonePtrList<Statement>* statements,
}
validate_output = factory()->NewIfStatement(
is_receiver_call, factory()->NewEmptyStatement(nopos), throw_call,
nopos);
is_receiver_call, factory()->EmptyStatement(), throw_call, nopos);
}
statements->Add(get_return, zone());
......@@ -3866,7 +3863,7 @@ void Parser::FinalizeIteratorUse(Variable* completion, Expression* condition,
factory()->NewSmiLiteral(Parser::kThrowCompletion, nopos), nopos);
Statement* statement = factory()->NewExpressionStatement(assignment, nopos);
set_completion_throw = factory()->NewIfStatement(
condition, statement, factory()->NewEmptyStatement(nopos), nopos);
condition, statement, factory()->EmptyStatement(), nopos);
}
// if (condition) {
......@@ -3880,7 +3877,7 @@ void Parser::FinalizeIteratorUse(Variable* completion, Expression* condition,
DCHECK_EQ(block->statements()->length(), 2);
maybe_close = IgnoreCompletion(factory()->NewIfStatement(
condition, block, factory()->NewEmptyStatement(nopos), nopos));
condition, block, factory()->EmptyStatement(), nopos));
}
// try { #try_block }
......@@ -4052,8 +4049,7 @@ void Parser::BuildIteratorCloseForCompletion(ZonePtrList<Statement>* statements,
}
Statement* check_return = factory()->NewIfStatement(
is_receiver_call, factory()->NewEmptyStatement(nopos), throw_call,
nopos);
is_receiver_call, factory()->EmptyStatement(), throw_call, nopos);
validate_return = factory()->NewBlock(2, false);
validate_return->statements()->Add(call_return, zone());
......@@ -4088,8 +4084,7 @@ void Parser::BuildIteratorCloseForCompletion(ZonePtrList<Statement>* statements,
factory()->NewNullLiteral(nopos), nopos);
maybe_call_return = factory()->NewIfStatement(
condition, factory()->NewEmptyStatement(nopos), call_return_carefully,
nopos);
condition, factory()->EmptyStatement(), call_return_carefully, nopos);
}
statements->Add(get_return, zone());
......
......@@ -499,8 +499,7 @@ void PatternRewriter::VisitArrayLiteral(ArrayLiteral* node,
if_not_done = factory()->NewIfStatement(
factory()->NewUnaryOperation(
Token::NOT, factory()->NewVariableProxy(done), kNoSourcePosition),
next_block, factory()->NewEmptyStatement(kNoSourcePosition),
kNoSourcePosition);
next_block, factory()->EmptyStatement(), kNoSourcePosition);
}
block_->statements()->Add(if_not_done, zone());
......@@ -608,7 +607,7 @@ void PatternRewriter::VisitArrayLiteral(ArrayLiteral* node,
maybe_store_and_unset_done = factory()->NewIfStatement(
factory()->NewUnaryOperation(Token::NOT, result_done, nopos), then,
factory()->NewEmptyStatement(nopos), nopos);
factory()->EmptyStatement(), nopos);
}
// index++;
......
......@@ -685,9 +685,7 @@ class PreParserFactory {
return PreParserExpression::Default();
}
PreParserStatement NewEmptyStatement(int pos) {
return PreParserStatement::Default();
}
PreParserStatement EmptyStatement() { return PreParserStatement::Default(); }
PreParserStatement NewBlock(
int capacity, bool ignore_completion_value,
......
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