Switch Statement to use inheritance+cast (#718)

This commit is contained in:
Jon Meow
2021-08-10 13:10:05 -07:00
committed by GitHub
parent 9168375978
commit d5564280ab
5 changed files with 535 additions and 567 deletions
@@ -805,8 +805,8 @@ void StepPattern() {
auto IsWhileAct(Action* act) -> bool {
switch (act->Tag()) {
case Action::Kind::StatementAction:
switch (cast<StatementAction>(*act).Stmt()->tag()) {
case StatementKind::While:
switch (cast<StatementAction>(*act).Stmt()->Tag()) {
case Statement::Kind::While:
return true;
default:
return false;
@@ -819,8 +819,8 @@ auto IsWhileAct(Action* act) -> bool {
auto IsBlockAct(Action* act) -> bool {
switch (act->Tag()) {
case Action::Kind::StatementAction:
switch (cast<StatementAction>(*act).Stmt()->tag()) {
case StatementKind::Block:
switch (cast<StatementAction>(*act).Stmt()->Tag()) {
case Statement::Kind::Block:
return true;
default:
return false;
@@ -842,13 +842,13 @@ void StepStmt() {
stmt->PrintDepth(1, llvm::outs());
llvm::outs() << " --->\n";
}
switch (stmt->tag()) {
case StatementKind::Match:
switch (stmt->Tag()) {
case Statement::Kind::Match:
if (act->Pos() == 0) {
// { { (match (e) ...) :: C, E, F} :: S, H}
// -> { { e :: (match ([]) ...) :: C, E, F} :: S, H}
frame->todo.Push(
global_arena->New<ExpressionAction>(stmt->GetMatch().exp));
global_arena->New<ExpressionAction>(cast<Match>(*stmt).Exp()));
act->IncrementPos();
} else {
// Regarding act->Pos():
@@ -861,11 +861,12 @@ void StepStmt() {
// * 2: the pattern for clause 1
// * ...
auto clause_num = (act->Pos() - 1) / 2;
if (clause_num >= static_cast<int>(stmt->GetMatch().clauses->size())) {
if (clause_num >=
static_cast<int>(cast<Match>(*stmt).Clauses()->size())) {
frame->todo.Pop(1);
break;
}
auto c = stmt->GetMatch().clauses->begin();
auto c = cast<Match>(*stmt).Clauses()->begin();
std::advance(c, clause_num);
if (act->Pos() % 2 == 1) {
@@ -880,12 +881,12 @@ void StepStmt() {
auto values = CurrentEnv(state);
std::list<std::string> vars;
std::optional<Env> matches =
PatternMatch(pat, v, values, &vars, stmt->line_num);
PatternMatch(pat, v, values, &vars, stmt->LineNumber());
if (matches) { // we have a match, start the body
auto* new_scope = global_arena->New<Scope>(*matches, vars);
frame->scopes.Push(new_scope);
const Statement* body_block =
Statement::MakeBlock(stmt->line_num, c->second);
global_arena->New<Block>(stmt->LineNumber(), c->second);
Action* body_act = global_arena->New<StatementAction>(body_block);
body_act->IncrementPos();
frame->todo.Pop(1);
@@ -896,26 +897,26 @@ void StepStmt() {
act->IncrementPos();
clause_num = (act->Pos() - 1) / 2;
if (clause_num ==
static_cast<int>(stmt->GetMatch().clauses->size())) {
static_cast<int>(cast<Match>(*stmt).Clauses()->size())) {
frame->todo.Pop(1);
}
}
}
}
break;
case StatementKind::While:
case Statement::Kind::While:
if (act->Pos() == 0) {
// { { (while (e) s) :: C, E, F} :: S, H}
// -> { { e :: (while ([]) s) :: C, E, F} :: S, H}
frame->todo.Push(
global_arena->New<ExpressionAction>(stmt->GetWhile().cond));
global_arena->New<ExpressionAction>(cast<While>(*stmt).Cond()));
act->IncrementPos();
} else if (cast<BoolValue>(*act->Results()[0]).Val()) {
// { {true :: (while ([]) s) :: C, E, F} :: S, H}
// -> { { s :: (while (e) s) :: C, E, F } :: S, H}
frame->todo.Top()->Clear();
frame->todo.Push(
global_arena->New<StatementAction>(stmt->GetWhile().body));
global_arena->New<StatementAction>(cast<While>(*stmt).Body()));
} else {
// { {false :: (while ([]) s) :: C, E, F} :: S, H}
// -> { { C, E, F } :: S, H}
@@ -923,41 +924,41 @@ void StepStmt() {
frame->todo.Pop(1);
}
break;
case StatementKind::Break:
case Statement::Kind::Break:
CHECK(act->Pos() == 0);
// { { break; :: ... :: (while (e) s) :: C, E, F} :: S, H}
// -> { { C, E', F} :: S, H}
frame->todo.Pop(1);
while (!frame->todo.IsEmpty() && !IsWhileAct(frame->todo.Top())) {
if (IsBlockAct(frame->todo.Top())) {
DeallocateScope(stmt->line_num, frame->scopes.Top());
DeallocateScope(stmt->LineNumber(), frame->scopes.Top());
frame->scopes.Pop(1);
}
frame->todo.Pop(1);
}
frame->todo.Pop(1);
break;
case StatementKind::Continue:
case Statement::Kind::Continue:
CHECK(act->Pos() == 0);
// { { continue; :: ... :: (while (e) s) :: C, E, F} :: S, H}
// -> { { (while (e) s) :: C, E', F} :: S, H}
frame->todo.Pop(1);
while (!frame->todo.IsEmpty() && !IsWhileAct(frame->todo.Top())) {
if (IsBlockAct(frame->todo.Top())) {
DeallocateScope(stmt->line_num, frame->scopes.Top());
DeallocateScope(stmt->LineNumber(), frame->scopes.Top());
frame->scopes.Pop(1);
}
frame->todo.Pop(1);
}
break;
case StatementKind::Block: {
case Statement::Kind::Block: {
if (act->Pos() == 0) {
if (stmt->GetBlock().stmt) {
if (cast<Block>(*stmt).Stmt()) {
auto* scope = global_arena->New<Scope>(CurrentEnv(state),
std::list<std::string>());
frame->scopes.Push(scope);
frame->todo.Push(
global_arena->New<StatementAction>(stmt->GetBlock().stmt));
global_arena->New<StatementAction>(cast<Block>(*stmt).Stmt()));
act->IncrementPos();
act->IncrementPos();
} else {
@@ -965,22 +966,22 @@ void StepStmt() {
}
} else {
Scope* scope = frame->scopes.Top();
DeallocateScope(stmt->line_num, scope);
DeallocateScope(stmt->LineNumber(), scope);
frame->scopes.Pop(1);
frame->todo.Pop(1);
}
break;
}
case StatementKind::VariableDefinition:
case Statement::Kind::VariableDefinition:
if (act->Pos() == 0) {
// { {(var x = e) :: C, E, F} :: S, H}
// -> { {e :: (var x = []) :: C, E, F} :: S, H}
frame->todo.Push(global_arena->New<ExpressionAction>(
stmt->GetVariableDefinition().init));
cast<VariableDefinition>(*stmt).Init()));
act->IncrementPos();
} else if (act->Pos() == 1) {
frame->todo.Push(global_arena->New<PatternAction>(
stmt->GetVariableDefinition().pat));
cast<VariableDefinition>(*stmt).Pat()));
act->IncrementPos();
} else if (act->Pos() == 2) {
// { { v :: (x = []) :: C, E, F} :: S, H}
@@ -990,52 +991,53 @@ void StepStmt() {
std::optional<Env> matches =
PatternMatch(p, v, frame->scopes.Top()->values,
&frame->scopes.Top()->locals, stmt->line_num);
&frame->scopes.Top()->locals, stmt->LineNumber());
CHECK(matches)
<< stmt->line_num
<< stmt->LineNumber()
<< ": internal error in variable definition, match failed";
frame->scopes.Top()->values = *matches;
frame->todo.Pop(1);
}
break;
case StatementKind::ExpressionStatement:
case Statement::Kind::ExpressionStatement:
if (act->Pos() == 0) {
// { {e :: C, E, F} :: S, H}
// -> { {e :: C, E, F} :: S, H}
frame->todo.Push(global_arena->New<ExpressionAction>(
stmt->GetExpressionStatement().exp));
cast<ExpressionStatement>(*stmt).Exp()));
act->IncrementPos();
} else {
frame->todo.Pop(1);
}
break;
case StatementKind::Assign:
case Statement::Kind::Assign:
if (act->Pos() == 0) {
// { {(lv = e) :: C, E, F} :: S, H}
// -> { {lv :: ([] = e) :: C, E, F} :: S, H}
frame->todo.Push(global_arena->New<LValAction>(stmt->GetAssign().lhs));
frame->todo.Push(
global_arena->New<LValAction>(cast<Assign>(*stmt).Lhs()));
act->IncrementPos();
} else if (act->Pos() == 1) {
// { { a :: ([] = e) :: C, E, F} :: S, H}
// -> { { e :: (a = []) :: C, E, F} :: S, H}
frame->todo.Push(
global_arena->New<ExpressionAction>(stmt->GetAssign().rhs));
global_arena->New<ExpressionAction>(cast<Assign>(*stmt).Rhs()));
act->IncrementPos();
} else if (act->Pos() == 2) {
// { { v :: (a = []) :: C, E, F} :: S, H}
// -> { { C, E, F} :: S, H(a := v)}
auto pat = act->Results()[0];
auto val = act->Results()[1];
PatternAssignment(pat, val, stmt->line_num);
PatternAssignment(pat, val, stmt->LineNumber());
frame->todo.Pop(1);
}
break;
case StatementKind::If:
case Statement::Kind::If:
if (act->Pos() == 0) {
// { {(if (e) then_stmt else else_stmt) :: C, E, F} :: S, H}
// -> { { e :: (if ([]) then_stmt else else_stmt) :: C, E, F} :: S, H}
frame->todo.Push(
global_arena->New<ExpressionAction>(stmt->GetIf().cond));
global_arena->New<ExpressionAction>(cast<If>(*stmt).Cond()));
act->IncrementPos();
} else if (cast<BoolValue>(*act->Results()[0]).Val()) {
// { {true :: if ([]) then_stmt else else_stmt :: C, E, F} ::
@@ -1043,48 +1045,48 @@ void StepStmt() {
// -> { { then_stmt :: C, E, F } :: S, H}
frame->todo.Pop(1);
frame->todo.Push(
global_arena->New<StatementAction>(stmt->GetIf().then_stmt));
} else if (stmt->GetIf().else_stmt) {
global_arena->New<StatementAction>(cast<If>(*stmt).ThenStmt()));
} else if (cast<If>(*stmt).ElseStmt()) {
// { {false :: if ([]) then_stmt else else_stmt :: C, E, F} ::
// S, H}
// -> { { else_stmt :: C, E, F } :: S, H}
frame->todo.Pop(1);
frame->todo.Push(
global_arena->New<StatementAction>(stmt->GetIf().else_stmt));
global_arena->New<StatementAction>(cast<If>(*stmt).ElseStmt()));
} else {
frame->todo.Pop(1);
}
break;
case StatementKind::Return:
case Statement::Kind::Return:
if (act->Pos() == 0) {
// { {return e :: C, E, F} :: S, H}
// -> { {e :: return [] :: C, E, F} :: S, H}
frame->todo.Push(
global_arena->New<ExpressionAction>(stmt->GetReturn().exp));
global_arena->New<ExpressionAction>(cast<Return>(*stmt).Exp()));
act->IncrementPos();
} else {
// { {v :: return [] :: C, E, F} :: {C', E', F'} :: S, H}
// -> { {v :: C', E', F'} :: S, H}
const Value* ret_val = CopyVal(act->Results()[0], stmt->line_num);
DeallocateLocals(stmt->line_num, frame);
const Value* ret_val = CopyVal(act->Results()[0], stmt->LineNumber());
DeallocateLocals(stmt->LineNumber(), frame);
state->stack.Pop(1);
frame = state->stack.Top();
frame->todo.Push(global_arena->New<ValAction>(ret_val));
}
break;
case StatementKind::Sequence:
case Statement::Kind::Sequence:
CHECK(act->Pos() == 0);
// { { (s1,s2) :: C, E, F} :: S, H}
// -> { { s1 :: s2 :: C, E, F} :: S, H}
frame->todo.Pop(1);
if (stmt->GetSequence().next) {
if (cast<Sequence>(*stmt).Next()) {
frame->todo.Push(
global_arena->New<StatementAction>(stmt->GetSequence().next));
global_arena->New<StatementAction>(cast<Sequence>(*stmt).Next()));
}
frame->todo.Push(
global_arena->New<StatementAction>(stmt->GetSequence().stmt));
global_arena->New<StatementAction>(cast<Sequence>(*stmt).Stmt()));
break;
case StatementKind::Continuation: {
case Statement::Kind::Continuation: {
CHECK(act->Pos() == 0);
// Create a continuation object by creating a frame similar the
// way one is created in a function call.
@@ -1094,10 +1096,10 @@ void StepStmt() {
scopes.Push(scope);
Stack<Action*> todo;
todo.Push(global_arena->New<StatementAction>(
Statement::MakeReturn(stmt->line_num, nullptr,
/*is_omitted_exp=*/true)));
global_arena->New<Return>(stmt->LineNumber(), nullptr,
/*is_omitted_exp=*/true)));
todo.Push(
global_arena->New<StatementAction>(stmt->GetContinuation().body));
global_arena->New<StatementAction>(cast<Continuation>(*stmt).Body()));
Frame* continuation_frame =
global_arena->New<Frame>("__continuation", scopes, todo);
Address continuation_address =
@@ -1107,25 +1109,26 @@ void StepStmt() {
continuation_frame->continuation = continuation_address;
// Bind the continuation object to the continuation variable
frame->scopes.Top()->values.Set(
stmt->GetContinuation().continuation_variable, continuation_address);
cast<Continuation>(*stmt).ContinuationVariable(),
continuation_address);
// Pop the continuation statement.
frame->todo.Pop();
break;
}
case StatementKind::Run:
case Statement::Kind::Run:
if (act->Pos() == 0) {
// Evaluate the argument of the run statement.
frame->todo.Push(
global_arena->New<ExpressionAction>(stmt->GetRun().argument));
global_arena->New<ExpressionAction>(cast<Run>(*stmt).Argument()));
act->IncrementPos();
} else {
frame->todo.Pop(1);
// Push an expression statement action to ignore the result
// value from the continuation.
Action* ignore_result = global_arena->New<StatementAction>(
Statement::MakeExpressionStatement(
stmt->line_num,
global_arena->New<TupleLiteral>(stmt->line_num)));
global_arena->New<ExpressionStatement>(
stmt->LineNumber(),
global_arena->New<TupleLiteral>(stmt->LineNumber())));
frame->todo.Push(ignore_result);
// Push the continuation onto the current stack.
const std::vector<Frame*>& continuation_vector =
@@ -1136,7 +1139,7 @@ void StepStmt() {
}
}
break;
case StatementKind::Await:
case Statement::Kind::Await:
CHECK(act->Pos() == 0);
// Pause the current continuation
frame->todo.Pop();
@@ -1147,7 +1150,7 @@ void StepStmt() {
// Update the continuation with the paused stack.
state->heap.Write(*paused.back()->continuation,
global_arena->New<ContinuationValue>(paused),
stmt->line_num);
stmt->LineNumber());
break;
}
}
+133 -122
View File
@@ -641,128 +641,135 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
if (!s) {
return TCStatement(s, types);
}
switch (s->tag()) {
case StatementKind::Match: {
auto res = TypeCheckExp(s->GetMatch().exp, types, values);
switch (s->Tag()) {
case Statement::Kind::Match: {
const auto& match = cast<Match>(*s);
auto res = TypeCheckExp(match.Exp(), types, values);
auto res_type = res.type;
auto new_clauses =
global_arena
->New<std::list<std::pair<const Pattern*, const Statement*>>>();
for (auto& clause : *s->GetMatch().clauses) {
for (auto& clause : *match.Clauses()) {
new_clauses->push_back(TypecheckCase(res_type, clause.first,
clause.second, types, values,
ret_type, is_omitted_ret_type));
}
const Statement* new_s =
Statement::MakeMatch(s->line_num, res.exp, new_clauses);
global_arena->New<Match>(s->LineNumber(), res.exp, new_clauses);
return TCStatement(new_s, types);
}
case StatementKind::While: {
auto cnd_res = TypeCheckExp(s->GetWhile().cond, types, values);
ExpectType(s->line_num, "condition of `while`",
case Statement::Kind::While: {
const auto& while_stmt = cast<While>(*s);
auto cnd_res = TypeCheckExp(while_stmt.Cond(), types, values);
ExpectType(s->LineNumber(), "condition of `while`",
global_arena->New<BoolType>(), cnd_res.type);
auto body_res = TypeCheckStmt(s->GetWhile().body, types, values, ret_type,
auto body_res = TypeCheckStmt(while_stmt.Body(), types, values, ret_type,
is_omitted_ret_type);
auto new_s =
Statement::MakeWhile(s->line_num, cnd_res.exp, body_res.stmt);
global_arena->New<While>(s->LineNumber(), cnd_res.exp, body_res.stmt);
return TCStatement(new_s, types);
}
case StatementKind::Break:
case StatementKind::Continue:
case Statement::Kind::Break:
case Statement::Kind::Continue:
return TCStatement(s, types);
case StatementKind::Block: {
auto stmt_res = TypeCheckStmt(s->GetBlock().stmt, types, values, ret_type,
is_omitted_ret_type);
return TCStatement(Statement::MakeBlock(s->line_num, stmt_res.stmt),
types);
}
case StatementKind::VariableDefinition: {
auto res = TypeCheckExp(s->GetVariableDefinition().init, types, values);
const Value* rhs_ty = res.type;
auto lhs_res = TypeCheckPattern(s->GetVariableDefinition().pat, types,
values, rhs_ty);
const Statement* new_s = Statement::MakeVariableDefinition(
s->line_num, s->GetVariableDefinition().pat, res.exp);
return TCStatement(new_s, lhs_res.types);
}
case StatementKind::Sequence: {
auto stmt_res = TypeCheckStmt(s->GetSequence().stmt, types, values,
case Statement::Kind::Block: {
auto stmt_res = TypeCheckStmt(cast<Block>(*s).Stmt(), types, values,
ret_type, is_omitted_ret_type);
auto types2 = stmt_res.types;
auto next_res = TypeCheckStmt(s->GetSequence().next, types2, values,
ret_type, is_omitted_ret_type);
auto types3 = next_res.types;
return TCStatement(
Statement::MakeSequence(s->line_num, stmt_res.stmt, next_res.stmt),
types3);
global_arena->New<Block>(s->LineNumber(), stmt_res.stmt), types);
}
case StatementKind::Assign: {
auto rhs_res = TypeCheckExp(s->GetAssign().rhs, types, values);
auto rhs_t = rhs_res.type;
auto lhs_res = TypeCheckExp(s->GetAssign().lhs, types, values);
auto lhs_t = lhs_res.type;
ExpectType(s->line_num, "assign", lhs_t, rhs_t);
auto new_s = Statement::MakeAssign(s->line_num, lhs_res.exp, rhs_res.exp);
case Statement::Kind::VariableDefinition: {
const auto& var = cast<VariableDefinition>(*s);
auto res = TypeCheckExp(var.Init(), types, values);
const Value* rhs_ty = res.type;
auto lhs_res = TypeCheckPattern(var.Pat(), types, values, rhs_ty);
const Statement* new_s = global_arena->New<VariableDefinition>(
s->LineNumber(), var.Pat(), res.exp);
return TCStatement(new_s, lhs_res.types);
}
case StatementKind::ExpressionStatement: {
auto res = TypeCheckExp(s->GetExpressionStatement().exp, types, values);
auto new_s = Statement::MakeExpressionStatement(s->line_num, res.exp);
case Statement::Kind::Sequence: {
const auto& seq = cast<Sequence>(*s);
auto stmt_res = TypeCheckStmt(seq.Stmt(), types, values, ret_type,
is_omitted_ret_type);
auto types2 = stmt_res.types;
auto next_res = TypeCheckStmt(seq.Next(), types2, values, ret_type,
is_omitted_ret_type);
auto types3 = next_res.types;
return TCStatement(global_arena->New<Sequence>(
s->LineNumber(), stmt_res.stmt, next_res.stmt),
types3);
}
case Statement::Kind::Assign: {
const auto& assign = cast<Assign>(*s);
auto rhs_res = TypeCheckExp(assign.Rhs(), types, values);
auto rhs_t = rhs_res.type;
auto lhs_res = TypeCheckExp(assign.Lhs(), types, values);
auto lhs_t = lhs_res.type;
ExpectType(s->LineNumber(), "assign", lhs_t, rhs_t);
auto new_s =
global_arena->New<Assign>(s->LineNumber(), lhs_res.exp, rhs_res.exp);
return TCStatement(new_s, lhs_res.types);
}
case Statement::Kind::ExpressionStatement: {
auto res =
TypeCheckExp(cast<ExpressionStatement>(*s).Exp(), types, values);
auto new_s =
global_arena->New<ExpressionStatement>(s->LineNumber(), res.exp);
return TCStatement(new_s, types);
}
case StatementKind::If: {
auto cnd_res = TypeCheckExp(s->GetIf().cond, types, values);
ExpectType(s->line_num, "condition of `if`",
case Statement::Kind::If: {
const auto& if_stmt = cast<If>(*s);
auto cnd_res = TypeCheckExp(if_stmt.Cond(), types, values);
ExpectType(s->LineNumber(), "condition of `if`",
global_arena->New<BoolType>(), cnd_res.type);
auto thn_res = TypeCheckStmt(s->GetIf().then_stmt, types, values,
ret_type, is_omitted_ret_type);
auto els_res = TypeCheckStmt(s->GetIf().else_stmt, types, values,
ret_type, is_omitted_ret_type);
auto new_s = Statement::MakeIf(s->line_num, cnd_res.exp, thn_res.stmt,
els_res.stmt);
auto then_res = TypeCheckStmt(if_stmt.ThenStmt(), types, values, ret_type,
is_omitted_ret_type);
auto else_res = TypeCheckStmt(if_stmt.ElseStmt(), types, values, ret_type,
is_omitted_ret_type);
auto new_s = global_arena->New<If>(s->LineNumber(), cnd_res.exp,
then_res.stmt, else_res.stmt);
return TCStatement(new_s, types);
}
case StatementKind::Return: {
const auto& ret = s->GetReturn();
auto res = TypeCheckExp(ret.exp, types, values);
case Statement::Kind::Return: {
const auto& ret = cast<Return>(*s);
auto res = TypeCheckExp(ret.Exp(), types, values);
if (ret_type->Tag() == Value::Kind::AutoType) {
// The following infers the return type from the first 'return'
// statement. This will get more difficult with subtyping, when we
// should infer the least-upper bound of all the 'return' statements.
ret_type = res.type;
} else {
ExpectType(s->line_num, "return", ret_type, res.type);
ExpectType(s->LineNumber(), "return", ret_type, res.type);
}
if (ret.is_omitted_exp != is_omitted_ret_type) {
FATAL_COMPILATION_ERROR(s->line_num)
if (ret.IsOmittedExp() != is_omitted_ret_type) {
FATAL_COMPILATION_ERROR(s->LineNumber())
<< *s << " should" << (is_omitted_ret_type ? " not" : "")
<< " provide a return value, to match the function's signature.";
}
return TCStatement(
Statement::MakeReturn(s->line_num, res.exp, ret.is_omitted_exp),
types);
return TCStatement(global_arena->New<Return>(s->LineNumber(), res.exp,
ret.IsOmittedExp()),
types);
}
case StatementKind::Continuation: {
TCStatement body_result =
TypeCheckStmt(s->GetContinuation().body, types, values, ret_type,
is_omitted_ret_type);
const Statement* new_continuation = Statement::MakeContinuation(
s->line_num, s->GetContinuation().continuation_variable,
body_result.stmt);
types.Set(s->GetContinuation().continuation_variable,
case Statement::Kind::Continuation: {
const auto& cont = cast<Continuation>(*s);
TCStatement body_result = TypeCheckStmt(cont.Body(), types, values,
ret_type, is_omitted_ret_type);
const Statement* new_continuation = global_arena->New<Continuation>(
s->LineNumber(), cont.ContinuationVariable(), body_result.stmt);
types.Set(cont.ContinuationVariable(),
global_arena->New<ContinuationType>());
return TCStatement(new_continuation, types);
}
case StatementKind::Run: {
case Statement::Kind::Run: {
TCExpression argument_result =
TypeCheckExp(s->GetRun().argument, types, values);
ExpectType(s->line_num, "argument of `run`",
TypeCheckExp(cast<Run>(*s).Argument(), types, values);
ExpectType(s->LineNumber(), "argument of `run`",
global_arena->New<ContinuationType>(), argument_result.type);
const Statement* new_run =
Statement::MakeRun(s->line_num, argument_result.exp);
global_arena->New<Run>(s->LineNumber(), argument_result.exp);
return TCStatement(new_run, types);
}
case StatementKind::Await: {
case Statement::Kind::Await: {
// nothing to do here
return TCStatement(s, types);
}
@@ -773,69 +780,73 @@ static auto CheckOrEnsureReturn(const Statement* stmt, bool omitted_ret_type,
int line_num) -> const Statement* {
if (!stmt) {
if (omitted_ret_type) {
return Statement::MakeReturn(line_num, nullptr,
/*is_omitted_exp=*/true);
return global_arena->New<Return>(line_num, nullptr,
/*is_omitted_exp=*/true);
} else {
FATAL_COMPILATION_ERROR(line_num)
<< "control-flow reaches end of function that provides a `->` return "
"type without reaching a return statement";
}
}
switch (stmt->tag()) {
case StatementKind::Match: {
switch (stmt->Tag()) {
case Statement::Kind::Match: {
const auto& match = cast<Match>(*stmt);
auto new_clauses =
global_arena
->New<std::list<std::pair<const Pattern*, const Statement*>>>();
for (auto i = stmt->GetMatch().clauses->begin();
i != stmt->GetMatch().clauses->end(); ++i) {
auto s =
CheckOrEnsureReturn(i->second, omitted_ret_type, stmt->line_num);
new_clauses->push_back(std::make_pair(i->first, s));
for (const auto& clause : *match.Clauses()) {
auto s = CheckOrEnsureReturn(clause.second, omitted_ret_type,
stmt->LineNumber());
new_clauses->push_back(std::make_pair(clause.first, s));
}
return Statement::MakeMatch(stmt->line_num, stmt->GetMatch().exp,
new_clauses);
return global_arena->New<Match>(stmt->LineNumber(), match.Exp(),
new_clauses);
}
case StatementKind::Block:
return Statement::MakeBlock(
stmt->line_num,
CheckOrEnsureReturn(stmt->GetBlock().stmt, omitted_ret_type,
stmt->line_num));
case StatementKind::If:
return Statement::MakeIf(
stmt->line_num, stmt->GetIf().cond,
CheckOrEnsureReturn(stmt->GetIf().then_stmt, omitted_ret_type,
stmt->line_num),
CheckOrEnsureReturn(stmt->GetIf().else_stmt, omitted_ret_type,
stmt->line_num));
case StatementKind::Return:
case Statement::Kind::Block:
return global_arena->New<Block>(
stmt->LineNumber(),
CheckOrEnsureReturn(cast<Block>(*stmt).Stmt(), omitted_ret_type,
stmt->LineNumber()));
case Statement::Kind::If: {
const auto& if_stmt = cast<If>(*stmt);
return global_arena->New<If>(
stmt->LineNumber(), if_stmt.Cond(),
CheckOrEnsureReturn(if_stmt.ThenStmt(), omitted_ret_type,
stmt->LineNumber()),
CheckOrEnsureReturn(if_stmt.ElseStmt(), omitted_ret_type,
stmt->LineNumber()));
}
case Statement::Kind::Return:
return stmt;
case StatementKind::Sequence:
if (stmt->GetSequence().next) {
return Statement::MakeSequence(
stmt->line_num, stmt->GetSequence().stmt,
CheckOrEnsureReturn(stmt->GetSequence().next, omitted_ret_type,
stmt->line_num));
case Statement::Kind::Sequence: {
const auto& seq = cast<Sequence>(*stmt);
if (seq.Next()) {
return global_arena->New<Sequence>(
stmt->LineNumber(), seq.Stmt(),
CheckOrEnsureReturn(seq.Next(), omitted_ret_type,
stmt->LineNumber()));
} else {
return CheckOrEnsureReturn(stmt->GetSequence().stmt, omitted_ret_type,
stmt->line_num);
return CheckOrEnsureReturn(seq.Stmt(), omitted_ret_type,
stmt->LineNumber());
}
case StatementKind::Continuation:
case StatementKind::Run:
case StatementKind::Await:
}
case Statement::Kind::Continuation:
case Statement::Kind::Run:
case Statement::Kind::Await:
return stmt;
case StatementKind::Assign:
case StatementKind::ExpressionStatement:
case StatementKind::While:
case StatementKind::Break:
case StatementKind::Continue:
case StatementKind::VariableDefinition:
case Statement::Kind::Assign:
case Statement::Kind::ExpressionStatement:
case Statement::Kind::While:
case Statement::Kind::Break:
case Statement::Kind::Continue:
case Statement::Kind::VariableDefinition:
if (omitted_ret_type) {
return Statement::MakeSequence(
stmt->line_num, stmt,
Statement::MakeReturn(line_num, nullptr,
/*is_omitted_exp=*/true));
return global_arena->New<Sequence>(
stmt->LineNumber(), stmt,
global_arena->New<Return>(line_num, nullptr,
/*is_omitted_exp=*/true));
} else {
FATAL_COMPILATION_ERROR(stmt->line_num)
FATAL_COMPILATION_ERROR(stmt->LineNumber())
<< "control-flow reaches end of function that provides a `->` "
"return type without reaching a return statement";
}