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