Refactor Statement accessors (#890)

This commit is contained in:
Jon Meow
2021-10-18 10:25:35 -07:00
committed by GitHub
parent 9c17b72ddd
commit b278e4edcf
4 changed files with 174 additions and 154 deletions
+19 -19
View File
@@ -36,8 +36,8 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
}
case Kind::While: {
const auto& while_stmt = cast<While>(*this);
out << "while (" << *while_stmt.Cond() << ")\n";
while_stmt.Body()->PrintDepth(depth - 1, out);
out << "while (" << while_stmt.condition() << ")\n";
while_stmt.body().PrintDepth(depth - 1, out);
break;
}
case Kind::Break:
@@ -48,46 +48,46 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
break;
case Kind::VariableDefinition: {
const auto& var = cast<VariableDefinition>(*this);
out << "var " << *var.Pat() << " = " << *var.Init() << ";";
out << "var " << var.pattern() << " = " << var.init() << ";";
break;
}
case Kind::ExpressionStatement:
out << *cast<ExpressionStatement>(*this).Exp() << ";";
out << cast<ExpressionStatement>(*this).expression() << ";";
break;
case Kind::Assign: {
const auto& assign = cast<Assign>(*this);
out << *assign.Lhs() << " = " << *assign.Rhs() << ";";
out << assign.lhs() << " = " << assign.rhs() << ";";
break;
}
case Kind::If: {
const auto& if_stmt = cast<If>(*this);
out << "if (" << *if_stmt.Cond() << ")\n";
if_stmt.ThenStmt()->PrintDepth(depth - 1, out);
if (if_stmt.ElseStmt()) {
out << "if (" << if_stmt.condition() << ")\n";
if_stmt.then_statement().PrintDepth(depth - 1, out);
if (if_stmt.else_statement()) {
out << "\nelse\n";
(*if_stmt.ElseStmt())->PrintDepth(depth - 1, out);
(*if_stmt.else_statement())->PrintDepth(depth - 1, out);
}
break;
}
case Kind::Return: {
const auto& ret = cast<Return>(*this);
if (ret.IsOmittedExp()) {
if (ret.is_omitted_expression()) {
out << "return;";
} else {
out << "return " << *ret.Exp() << ";";
out << "return " << ret.expression() << ";";
}
break;
}
case Kind::Sequence: {
const auto& seq = cast<Sequence>(*this);
seq.Stmt()->PrintDepth(depth, out);
seq.statement().PrintDepth(depth, out);
if (depth < 0 || depth > 1) {
out << "\n";
} else {
out << " ";
}
if (seq.Next()) {
(*seq.Next())->PrintDepth(depth - 1, out);
if (seq.next()) {
(*seq.next())->PrintDepth(depth - 1, out);
}
break;
}
@@ -97,8 +97,8 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
if (depth < 0 || depth > 1) {
out << "\n";
}
if (block.Stmt()) {
(*block.Stmt())->PrintDepth(depth, out);
if (block.statement()) {
(*block.statement())->PrintDepth(depth, out);
if (depth < 0 || depth > 1) {
out << "\n";
}
@@ -111,18 +111,18 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
}
case Kind::Continuation: {
const auto& cont = cast<Continuation>(*this);
out << "continuation " << cont.ContinuationVariable() << " ";
out << "continuation " << cont.continuation_variable() << " ";
if (depth < 0 || depth > 1) {
out << "\n";
}
cont.Body()->PrintDepth(depth - 1, out);
cont.body().PrintDepth(depth - 1, out);
if (depth < 0 || depth > 1) {
out << "\n";
}
break;
}
case Kind::Run:
out << "run " << *cast<Run>(*this).Argument() << ";";
out << "run " << cast<Run>(*this).argument() << ";";
break;
case Kind::Await:
out << "await;";
+92 -78
View File
@@ -60,166 +60,180 @@ class Statement {
class ExpressionStatement : public Statement {
public:
ExpressionStatement(SourceLocation source_loc, Nonnull<Expression*> exp)
: Statement(Kind::ExpressionStatement, source_loc), exp(exp) {}
ExpressionStatement(SourceLocation source_loc,
Nonnull<Expression*> expression)
: Statement(Kind::ExpressionStatement, source_loc),
expression_(expression) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::ExpressionStatement;
}
auto Exp() const -> Nonnull<const Expression*> { return exp; }
auto Exp() -> Nonnull<Expression*> { return exp; }
auto expression() const -> const Expression& { return *expression_; }
auto expression() -> Expression& { return *expression_; }
private:
Nonnull<Expression*> exp;
Nonnull<Expression*> expression_;
};
class Assign : public Statement {
public:
Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
Nonnull<Expression*> rhs)
: Statement(Kind::Assign, source_loc), lhs(lhs), rhs(rhs) {}
: Statement(Kind::Assign, source_loc), lhs_(lhs), rhs_(rhs) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Assign;
}
auto Lhs() const -> Nonnull<const Expression*> { return lhs; }
auto Lhs() -> Nonnull<Expression*> { return lhs; }
auto Rhs() const -> Nonnull<const Expression*> { return rhs; }
auto Rhs() -> Nonnull<Expression*> { return rhs; }
auto lhs() const -> const Expression& { return *lhs_; }
auto lhs() -> Expression& { return *lhs_; }
auto rhs() const -> const Expression& { return *rhs_; }
auto rhs() -> Expression& { return *rhs_; }
private:
Nonnull<Expression*> lhs;
Nonnull<Expression*> rhs;
Nonnull<Expression*> lhs_;
Nonnull<Expression*> rhs_;
};
class VariableDefinition : public Statement {
public:
VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pat,
VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
Nonnull<Expression*> init)
: Statement(Kind::VariableDefinition, source_loc), pat(pat), init(init) {}
: Statement(Kind::VariableDefinition, source_loc),
pattern_(pattern),
init_(init) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::VariableDefinition;
}
auto Pat() const -> Nonnull<const Pattern*> { return pat; }
auto Pat() -> Nonnull<Pattern*> { return pat; }
auto Init() const -> Nonnull<const Expression*> { return init; }
auto Init() -> Nonnull<Expression*> { return init; }
auto pattern() const -> const Pattern& { return *pattern_; }
auto pattern() -> Pattern& { return *pattern_; }
auto init() const -> const Expression& { return *init_; }
auto init() -> Expression& { return *init_; }
private:
Nonnull<Pattern*> pat;
Nonnull<Expression*> init;
Nonnull<Pattern*> pattern_;
Nonnull<Expression*> init_;
};
class If : public Statement {
public:
If(SourceLocation source_loc, Nonnull<Expression*> cond,
Nonnull<Statement*> then_stmt,
std::optional<Nonnull<Statement*>> else_stmt)
If(SourceLocation source_loc, Nonnull<Expression*> condition,
Nonnull<Statement*> then_statement,
std::optional<Nonnull<Statement*>> else_statement)
: Statement(Kind::If, source_loc),
cond(cond),
then_stmt(then_stmt),
else_stmt(else_stmt) {}
condition_(condition),
then_statement_(then_statement),
else_statement_(else_statement) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::If;
}
auto Cond() const -> Nonnull<const Expression*> { return cond; }
auto Cond() -> Nonnull<Expression*> { return cond; }
auto ThenStmt() const -> Nonnull<const Statement*> { return then_stmt; }
auto ThenStmt() -> Nonnull<Statement*> { return then_stmt; }
auto ElseStmt() const -> std::optional<Nonnull<const Statement*>> {
return else_stmt;
auto condition() const -> const Expression& { return *condition_; }
auto condition() -> Expression& { return *condition_; }
auto then_statement() const -> const Statement& { return *then_statement_; }
auto then_statement() -> Statement& { return *then_statement_; }
auto else_statement() const -> std::optional<Nonnull<const Statement*>> {
return else_statement_;
}
auto else_statement() -> std::optional<Nonnull<Statement*>> {
return else_statement_;
}
auto ElseStmt() -> std::optional<Nonnull<Statement*>> { return else_stmt; }
private:
Nonnull<Expression*> cond;
Nonnull<Statement*> then_stmt;
std::optional<Nonnull<Statement*>> else_stmt;
Nonnull<Expression*> condition_;
Nonnull<Statement*> then_statement_;
std::optional<Nonnull<Statement*>> else_statement_;
};
class Return : public Statement {
public:
Return(Nonnull<Arena*> arena, SourceLocation source_loc)
: Return(source_loc, arena->New<TupleLiteral>(source_loc), true) {}
Return(SourceLocation source_loc, Nonnull<Expression*> exp,
bool is_omitted_exp)
Return(SourceLocation source_loc, Nonnull<Expression*> expression,
bool is_omitted_expression)
: Statement(Kind::Return, source_loc),
exp(exp),
is_omitted_exp(is_omitted_exp) {}
expression_(expression),
is_omitted_expression_(is_omitted_expression) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Return;
}
auto Exp() const -> Nonnull<const Expression*> { return exp; }
auto Exp() -> Nonnull<Expression*> { return exp; }
auto IsOmittedExp() const -> bool { return is_omitted_exp; }
auto expression() const -> const Expression& { return *expression_; }
auto expression() -> Expression& { return *expression_; }
auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
private:
Nonnull<Expression*> exp;
bool is_omitted_exp;
Nonnull<Expression*> expression_;
bool is_omitted_expression_;
};
class Sequence : public Statement {
public:
Sequence(SourceLocation source_loc, Nonnull<Statement*> stmt,
Sequence(SourceLocation source_loc, Nonnull<Statement*> statement,
std::optional<Nonnull<Statement*>> next)
: Statement(Kind::Sequence, source_loc), stmt(stmt), next(next) {}
: Statement(Kind::Sequence, source_loc),
statement_(statement),
next_(next) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Sequence;
}
auto Stmt() const -> Nonnull<const Statement*> { return stmt; }
auto Stmt() -> Nonnull<Statement*> { return stmt; }
auto Next() const -> std::optional<Nonnull<const Statement*>> { return next; }
auto Next() -> std::optional<Nonnull<Statement*>> { return next; }
auto statement() const -> const Statement& { return *statement_; }
auto statement() -> Statement& { return *statement_; }
auto next() const -> std::optional<Nonnull<const Statement*>> {
return next_;
}
auto next() -> std::optional<Nonnull<Statement*>> { return next_; }
private:
Nonnull<Statement*> stmt;
std::optional<Nonnull<Statement*>> next;
Nonnull<Statement*> statement_;
std::optional<Nonnull<Statement*>> next_;
};
class Block : public Statement {
public:
Block(SourceLocation source_loc, std::optional<Nonnull<Statement*>> stmt)
: Statement(Kind::Block, source_loc), stmt(stmt) {}
Block(SourceLocation source_loc, std::optional<Nonnull<Statement*>> statement)
: Statement(Kind::Block, source_loc), statement_(statement) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Block;
}
auto Stmt() const -> std::optional<Nonnull<const Statement*>> { return stmt; }
auto Stmt() -> std::optional<Nonnull<Statement*>> { return stmt; }
auto statement() const -> std::optional<Nonnull<const Statement*>> {
return statement_;
}
auto statement() -> std::optional<Nonnull<Statement*>> { return statement_; }
private:
std::optional<Nonnull<Statement*>> stmt;
std::optional<Nonnull<Statement*>> statement_;
};
class While : public Statement {
public:
While(SourceLocation source_loc, Nonnull<Expression*> cond,
While(SourceLocation source_loc, Nonnull<Expression*> condition,
Nonnull<Statement*> body)
: Statement(Kind::While, source_loc), cond(cond), body(body) {}
: Statement(Kind::While, source_loc),
condition_(condition),
body_(body) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::While;
}
auto Cond() const -> Nonnull<const Expression*> { return cond; }
auto Cond() -> Nonnull<Expression*> { return cond; }
auto Body() const -> Nonnull<const Statement*> { return body; }
auto Body() -> Nonnull<Statement*> { return body; }
auto condition() const -> const Expression& { return *condition_; }
auto condition() -> Expression& { return *condition_; }
auto body() const -> const Statement& { return *body_; }
auto body() -> Statement& { return *body_; }
private:
Nonnull<Expression*> cond;
Nonnull<Statement*> body;
Nonnull<Expression*> condition_;
Nonnull<Statement*> body_;
};
class Break : public Statement {
@@ -289,22 +303,22 @@ class Continuation : public Statement {
Continuation(SourceLocation source_loc, std::string continuation_variable,
Nonnull<Statement*> body)
: Statement(Kind::Continuation, source_loc),
continuation_variable(std::move(continuation_variable)),
body(body) {}
continuation_variable_(std::move(continuation_variable)),
body_(body) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Continuation;
}
auto ContinuationVariable() const -> const std::string& {
return continuation_variable;
auto continuation_variable() const -> const std::string& {
return continuation_variable_;
}
auto Body() const -> Nonnull<const Statement*> { return body; }
auto Body() -> Nonnull<Statement*> { return body; }
auto body() const -> const Statement& { return *body_; }
auto body() -> Statement& { return *body_; }
private:
std::string continuation_variable;
Nonnull<Statement*> body;
std::string continuation_variable_;
Nonnull<Statement*> body_;
};
// A run statement.
@@ -313,17 +327,17 @@ class Continuation : public Statement {
class Run : public Statement {
public:
Run(SourceLocation source_loc, Nonnull<Expression*> argument)
: Statement(Kind::Run, source_loc), argument(argument) {}
: Statement(Kind::Run, source_loc), argument_(argument) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Run;
}
auto Argument() const -> Nonnull<const Expression*> { return argument; }
auto Argument() -> Nonnull<Expression*> { return argument; }
auto argument() const -> const Expression& { return *argument_; }
auto argument() -> Expression& { return *argument_; }
private:
Nonnull<Expression*> argument;
Nonnull<Expression*> argument_;
};
// An await statement.