Commit de046f5f authored by Adam Klein's avatar Adam Klein Committed by Commit Bot

[ast] Make CaseClause a plain ZoneObject, not an Expression

CaseClause never made sense as an Expression; this CL allows us to
remove several UNREACHABLEs and slim down the representation of
CaseClause by removing its source position (which was only used
in prettyprinting).

The only real fallout of this change is that SourceRangeMap now
stores its keys as ZoneObject*, rather than AstNode*, but since
there's already compile time typechecking for inserting items
into the map this shouldn't cause any ill effects.

While modifying CaseClause, also removed the dead body_target()
accessor (and related member variable). Thus this CL overall
reduces the memory needed for each CaseClause by two words.

Bug: v8:6092
Change-Id: I0021c0590a69e29305c41ec6105c8824ae0cc25b
Reviewed-on: https://chromium-review.googlesource.com/639316Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarMythri Alle <mythria@chromium.org>
Commit-Queue: Adam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47722}
parent 730928eb
......@@ -116,9 +116,11 @@ void AstExpressionRewriter::VisitWithStatement(WithStatement* node) {
void AstExpressionRewriter::VisitSwitchStatement(SwitchStatement* node) {
AST_REWRITE_PROPERTY(Expression, node, tag);
ZoneList<CaseClause*>* clauses = node->cases();
for (int i = 0; i < clauses->length(); i++) {
AST_REWRITE_LIST_ELEMENT(CaseClause, clauses, i);
for (CaseClause* clause : *node->cases()) {
if (!clause->is_default()) {
AST_REWRITE_PROPERTY(Expression, clause, label);
}
VisitStatements(clause->statements());
}
}
......@@ -372,14 +374,6 @@ void AstExpressionRewriter::VisitSuperCallReference(SuperCallReference* node) {
}
void AstExpressionRewriter::VisitCaseClause(CaseClause* node) {
if (!node->is_default()) {
AST_REWRITE_PROPERTY(Expression, node, label);
}
VisitStatements(node->statements());
}
void AstExpressionRewriter::VisitEmptyParentheses(EmptyParentheses* node) {
NOTHING();
}
......
......@@ -400,20 +400,14 @@ void AstNumberingVisitor::VisitIfStatement(IfStatement* node) {
void AstNumberingVisitor::VisitSwitchStatement(SwitchStatement* node) {
Visit(node->tag());
ZoneList<CaseClause*>* cases = node->cases();
for (int i = 0; i < cases->length(); i++) {
VisitCaseClause(cases->at(i));
for (CaseClause* clause : *node->cases()) {
if (!clause->is_default()) Visit(clause->label());
VisitStatements(clause->statements());
ReserveFeedbackSlots(clause);
}
}
void AstNumberingVisitor::VisitCaseClause(CaseClause* node) {
if (!node->is_default()) Visit(node->label());
VisitStatements(node->statements());
ReserveFeedbackSlots(node);
}
void AstNumberingVisitor::VisitForStatement(ForStatement* node) {
node->set_osr_id(ReserveId());
if (node->init() != NULL) Visit(node->init()); // Not part of loop.
......
......@@ -230,7 +230,7 @@ class SourceRangeMap final : public ZoneObject {
public:
explicit SourceRangeMap(Zone* zone) : map_(zone) {}
AstNodeSourceRanges* Find(AstNode* node) {
AstNodeSourceRanges* Find(ZoneObject* node) {
auto it = map_.find(node);
if (it == map_.end()) return nullptr;
return it->second;
......@@ -246,7 +246,7 @@ class SourceRangeMap final : public ZoneObject {
#undef DEFINE_MAP_INSERT
private:
ZoneMap<AstNode*, AstNodeSourceRanges*> map_;
ZoneMap<ZoneObject*, AstNodeSourceRanges*> map_;
};
} // namespace internal
......
......@@ -210,11 +210,6 @@ void AstTraversalVisitor<Subclass>::VisitSwitchStatement(
}
}
template <class Subclass>
void AstTraversalVisitor<Subclass>::VisitCaseClause(CaseClause* clause) {
UNREACHABLE();
}
template <class Subclass>
void AstTraversalVisitor<Subclass>::VisitDoWhileStatement(
DoWhileStatement* stmt) {
......
......@@ -1045,9 +1045,8 @@ Call::CallType Call::GetCallType() const {
return OTHER_CALL;
}
CaseClause::CaseClause(Expression* label, ZoneList<Statement*>* statements,
int pos)
: Expression(pos, kCaseClause), label_(label), statements_(statements) {}
CaseClause::CaseClause(Expression* label, ZoneList<Statement*>* statements)
: label_(label), statements_(statements) {}
void CaseClause::AssignFeedbackSlots(FeedbackVectorSpec* spec,
LanguageMode language_mode,
......
......@@ -101,7 +101,6 @@ namespace internal {
V(ThisFunction) \
V(SuperPropertyReference) \
V(SuperCallReference) \
V(CaseClause) \
V(EmptyParentheses) \
V(GetIterator) \
V(DoExpression) \
......@@ -861,16 +860,14 @@ class WithStatement final : public Statement {
Statement* statement_;
};
class CaseClause final : public Expression {
class CaseClause final : public ZoneObject {
public:
bool is_default() const { return label_ == NULL; }
Expression* label() const {
CHECK(!is_default());
DCHECK(!is_default());
return label_;
}
void set_label(Expression* e) { label_ = e; }
Label* body_target() { return &body_target_; }
ZoneList<Statement*>* statements() const { return statements_; }
void AssignFeedbackSlots(FeedbackVectorSpec* spec, LanguageMode language_mode,
......@@ -881,11 +878,10 @@ class CaseClause final : public Expression {
private:
friend class AstNodeFactory;
CaseClause(Expression* label, ZoneList<Statement*>* statements, int pos);
CaseClause(Expression* label, ZoneList<Statement*>* statements);
FeedbackSlot feedback_slot_;
Expression* label_;
Label body_target_;
ZoneList<Statement*>* statements_;
};
......@@ -3292,9 +3288,9 @@ class AstNodeFactory final BASE_EMBEDDED {
SloppyBlockFunctionStatement(NewEmptyStatement(kNoSourcePosition));
}
CaseClause* NewCaseClause(Expression* label, ZoneList<Statement*>* statements,
int pos) {
return new (zone_) CaseClause(label, statements, pos);
CaseClause* NewCaseClause(Expression* label,
ZoneList<Statement*>* statements) {
return new (zone_) CaseClause(label, statements);
}
Literal* NewStringLiteral(const AstRawString* string, int pos) {
......
......@@ -126,16 +126,10 @@ void CallPrinter::VisitWithStatement(WithStatement* node) {
void CallPrinter::VisitSwitchStatement(SwitchStatement* node) {
Find(node->tag());
ZoneList<CaseClause*>* cases = node->cases();
for (int i = 0; i < cases->length(); i++) Find(cases->at(i));
}
void CallPrinter::VisitCaseClause(CaseClause* clause) {
if (!clause->is_default()) {
Find(clause->label());
for (CaseClause* clause : *node->cases()) {
if (!clause->is_default()) Find(clause->label());
FindStatements(clause->statements());
}
FindStatements(clause->statements());
}
......@@ -837,20 +831,15 @@ void AstPrinter::VisitSwitchStatement(SwitchStatement* node) {
IndentedScope indent(this, "SWITCH", node->position());
PrintLabelsIndented(node->labels());
PrintIndentedVisit("TAG", node->tag());
for (int i = 0; i < node->cases()->length(); i++) {
Visit(node->cases()->at(i));
}
}
void AstPrinter::VisitCaseClause(CaseClause* clause) {
if (clause->is_default()) {
IndentedScope indent(this, "DEFAULT", clause->position());
PrintStatements(clause->statements());
} else {
IndentedScope indent(this, "CASE", clause->position());
Visit(clause->label());
PrintStatements(clause->statements());
for (CaseClause* clause : *node->cases()) {
if (clause->is_default()) {
IndentedScope indent(this, "DEFAULT");
PrintStatements(clause->statements());
} else {
IndentedScope indent(this, "CASE");
Visit(clause->label());
PrintStatements(clause->statements());
}
}
}
......
......@@ -29,7 +29,7 @@ class BlockCoverageBuilder final : public ZoneObject {
static constexpr int kNoCoverageArraySlot = -1;
int AllocateBlockCoverageSlot(AstNode* node, SourceRangeKind kind) {
int AllocateBlockCoverageSlot(ZoneObject* node, SourceRangeKind kind) {
AstNodeSourceRanges* ranges = source_range_map_->Find(node);
if (ranges == nullptr) return kNoCoverageArraySlot;
......@@ -46,7 +46,7 @@ class BlockCoverageBuilder final : public ZoneObject {
builder_->IncBlockCounter(coverage_array_slot);
}
void IncrementBlockCounter(AstNode* node, SourceRangeKind kind) {
void IncrementBlockCounter(ZoneObject* node, SourceRangeKind kind) {
int slot = AllocateBlockCoverageSlot(node, kind);
IncrementBlockCounter(slot);
}
......
......@@ -1368,11 +1368,6 @@ void BytecodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
}
}
void BytecodeGenerator::VisitCaseClause(CaseClause* clause) {
// Handled entirely in VisitSwitchStatement.
UNREACHABLE();
}
void BytecodeGenerator::VisitIterationBody(IterationStatement* stmt,
LoopBuilder* loop_builder) {
loop_builder->LoopBody();
......
......@@ -5419,14 +5419,13 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseSwitchStatement(
default_seen = true;
}
Expect(Token::COLON, CHECK_OK);
int clause_pos = position();
StatementListT statements = impl()->NewStatementList(5);
while (peek() != Token::CASE && peek() != Token::DEFAULT &&
peek() != Token::RBRACE) {
StatementT stat = ParseStatementListItem(CHECK_OK);
statements->Add(stat, zone());
}
auto clause = factory()->NewCaseClause(label, statements, clause_pos);
auto clause = factory()->NewCaseClause(label, statements);
impl()->RecordCaseClauseSourceRange(clause, range_scope.Finalize());
cases->Add(clause, zone());
}
......
......@@ -731,7 +731,6 @@ NOT_A_PATTERN(BreakStatement)
NOT_A_PATTERN(Call)
NOT_A_PATTERN(CallNew)
NOT_A_PATTERN(CallRuntime)
NOT_A_PATTERN(CaseClause)
NOT_A_PATTERN(ClassLiteral)
NOT_A_PATTERN(CompareOperation)
NOT_A_PATTERN(CompoundAssignment)
......
......@@ -718,7 +718,7 @@ class PreParserFactory {
}
PreParserStatement NewCaseClause(const PreParserExpression& label,
PreParserStatementList statements, int pos) {
PreParserStatementList statements) {
return PreParserStatement::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