mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 17:30:16 +01:00
This was born out of wanting FunctionDeclaration to explicitly have a Block for a body, and became a bit more of specifying types around. Note this forces exec_program to generate a Block for print()'s body, which is probably more correct as now we can expect a standard FunctionDeclaration AST structure, even for the built-ins. There is a syntactic change here: a continuation's body is now a Block, not just a Statement. I've added an example disallowed test case. I think this is more reasonable syntax. Other than that, note that optional_else now generates a valid Block. This has me thinking about whether we can eliminate Sequence, but that seemed well out of scope for this.
105 lines
3.5 KiB
C++
105 lines
3.5 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "executable_semantics/interpreter/resolve_control_flow.h"
|
|
|
|
#include "executable_semantics/ast/declaration.h"
|
|
#include "executable_semantics/ast/statement.h"
|
|
#include "executable_semantics/common/error.h"
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
using llvm::cast;
|
|
|
|
namespace Carbon {
|
|
|
|
// Resolves control-flow edges in the AST rooted at `statement`. `return`
|
|
// statements will resolve to `*function`, and `break` and `continue`
|
|
// statements will resolve to `*loop`. If either parameter is nullopt, that
|
|
// indicates a context where the corresponding statements are not permitted.
|
|
static void ResolveControlFlow(
|
|
Nonnull<Statement*> statement,
|
|
std::optional<Nonnull<const FunctionDeclaration*>> function,
|
|
std::optional<Nonnull<const Statement*>> loop) {
|
|
switch (statement->kind()) {
|
|
case Statement::Kind::Return:
|
|
if (!function.has_value()) {
|
|
FATAL_COMPILATION_ERROR(statement->source_loc())
|
|
<< "return is not within a function body";
|
|
}
|
|
cast<Return>(*statement).set_function(*function);
|
|
return;
|
|
case Statement::Kind::Break:
|
|
if (!loop.has_value()) {
|
|
FATAL_COMPILATION_ERROR(statement->source_loc())
|
|
<< "break is not within a loop body";
|
|
}
|
|
cast<Break>(*statement).set_loop(*loop);
|
|
return;
|
|
case Statement::Kind::Continue:
|
|
if (!loop.has_value()) {
|
|
FATAL_COMPILATION_ERROR(statement->source_loc())
|
|
<< "continue is not within a loop body";
|
|
}
|
|
cast<Continue>(*statement).set_loop(*loop);
|
|
return;
|
|
case Statement::Kind::If: {
|
|
auto& if_stmt = cast<If>(*statement);
|
|
ResolveControlFlow(&if_stmt.then_block(), function, loop);
|
|
if (if_stmt.else_block().has_value()) {
|
|
ResolveControlFlow(*if_stmt.else_block(), function, loop);
|
|
}
|
|
return;
|
|
}
|
|
case Statement::Kind::Sequence: {
|
|
auto& seq = cast<Sequence>(*statement);
|
|
ResolveControlFlow(&seq.statement(), function, loop);
|
|
if (seq.next().has_value()) {
|
|
ResolveControlFlow(*seq.next(), function, loop);
|
|
}
|
|
return;
|
|
}
|
|
case Statement::Kind::Block: {
|
|
auto& block = cast<Block>(*statement);
|
|
if (block.sequence().has_value()) {
|
|
ResolveControlFlow(*block.sequence(), function, loop);
|
|
}
|
|
return;
|
|
}
|
|
case Statement::Kind::While:
|
|
ResolveControlFlow(&cast<While>(*statement).body(), function, statement);
|
|
return;
|
|
case Statement::Kind::Match: {
|
|
auto& match = cast<Match>(*statement);
|
|
for (Match::Clause& clause : match.clauses()) {
|
|
ResolveControlFlow(&clause.statement(), function, loop);
|
|
}
|
|
return;
|
|
}
|
|
case Statement::Kind::Continuation:
|
|
ResolveControlFlow(&cast<Continuation>(*statement).body(), std::nullopt,
|
|
std::nullopt);
|
|
return;
|
|
case Statement::Kind::ExpressionStatement:
|
|
case Statement::Kind::Assign:
|
|
case Statement::Kind::VariableDefinition:
|
|
case Statement::Kind::Run:
|
|
case Statement::Kind::Await:
|
|
return;
|
|
}
|
|
}
|
|
|
|
void ResolveControlFlow(AST& ast) {
|
|
for (auto declaration : ast.declarations) {
|
|
if (declaration->kind() != Declaration::Kind::FunctionDeclaration) {
|
|
continue;
|
|
}
|
|
auto& function = cast<FunctionDeclaration>(*declaration);
|
|
if (function.body().has_value()) {
|
|
ResolveControlFlow(*function.body(), &function, std::nullopt);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|