mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
* interfaces, impls, and constrained generics (basics) * separate type checking into declare vs. type check, removing redundancy * external impls * added impl scopes to handle generics calling generics * cleanup * more cleanup * Update executable_semantics/testdata/interface/external_impl_point_vector.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * Update executable_semantics/testdata/interface/generic_call_generic.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * Update executable_semantics/testdata/interface/tuple_vector_add_scale.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * Update executable_semantics/testdata/interface/vector_point_add_scale.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * change ImplementationDeclaration to ImplDeclaration * remove impl_type_value * split NamedEntity into two * changed GetName to be a free function * adding comments * more edits to respond to review * introduce ImplBinding, remove punning on GenericBinding * new test case and some minor edits * refactor GetMember and GetField to move impl logic to interpreter * remove commennt * change EntityView to ImplBinding in FieldAccess... * move ImplBinding * review response * added example to impl_scope.h * minor edits * Update executable_semantics/interpreter/field_path.h Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/interpreter/value.cpp Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/ast/expression.h Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/ast/expression.h Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/ast/generic_binding.h Co-authored-by: Geoff Romer <gromer@google.com> * more edits from review * review response * Update executable_semantics/ast/static_scope.h Co-authored-by: Geoff Romer <gromer@google.com> * remove ImplType, renamed node_view to value_node Co-authored-by: josh11b <josh11b@users.noreply.github.com> Co-authored-by: Geoff Romer <gromer@google.com>
159 lines
5.6 KiB
C++
159 lines
5.6 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/return_term.h"
|
|
#include "executable_semantics/ast/statement.h"
|
|
#include "executable_semantics/common/error.h"
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
using llvm::cast;
|
|
|
|
namespace Carbon {
|
|
|
|
// Aggregate information about a function being analyzed.
|
|
struct FunctionData {
|
|
// The function declaration.
|
|
Nonnull<FunctionDeclaration*> declaration;
|
|
|
|
// True if the function has a deduced return type, and we've already seen
|
|
// a `return` statement in its body.
|
|
bool saw_return_in_auto = false;
|
|
};
|
|
|
|
// Resolves control-flow edges such as `Return::function()` and `Break::loop()`
|
|
// in the AST rooted at `statement`. `loop` is the innermost loop that
|
|
// statically encloses `statement`, or nullopt if there is no such loop.
|
|
// `function` carries information about the function body that `statement`
|
|
// belongs to, and that information may be updated by this call. `function`
|
|
// can be nullopt if `statement` does not belong to a function body, for
|
|
// example if it is part of a continuation body instead.
|
|
static void ResolveControlFlow(Nonnull<Statement*> statement,
|
|
std::optional<Nonnull<const Statement*>> loop,
|
|
std::optional<Nonnull<FunctionData*>> function) {
|
|
switch (statement->kind()) {
|
|
case StatementKind::Return: {
|
|
if (!function.has_value()) {
|
|
FATAL_COMPILATION_ERROR(statement->source_loc())
|
|
<< "return is not within a function body";
|
|
}
|
|
const ReturnTerm& function_return =
|
|
(*function)->declaration->return_term();
|
|
if (function_return.is_auto()) {
|
|
if ((*function)->saw_return_in_auto) {
|
|
FATAL_COMPILATION_ERROR(statement->source_loc())
|
|
<< "Only one return is allowed in a function with an `auto` "
|
|
"return type.";
|
|
}
|
|
(*function)->saw_return_in_auto = true;
|
|
}
|
|
auto& ret = cast<Return>(*statement);
|
|
ret.set_function((*function)->declaration);
|
|
if (ret.is_omitted_expression() != function_return.is_omitted()) {
|
|
FATAL_COMPILATION_ERROR(ret.source_loc())
|
|
<< ret << " should" << (function_return.is_omitted() ? " not" : "")
|
|
<< " provide a return value, to match the function's signature.";
|
|
}
|
|
return;
|
|
}
|
|
case StatementKind::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 StatementKind::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 StatementKind::If: {
|
|
auto& if_stmt = cast<If>(*statement);
|
|
ResolveControlFlow(&if_stmt.then_block(), loop, function);
|
|
if (if_stmt.else_block().has_value()) {
|
|
ResolveControlFlow(*if_stmt.else_block(), loop, function);
|
|
}
|
|
return;
|
|
}
|
|
case StatementKind::Block: {
|
|
auto& block = cast<Block>(*statement);
|
|
for (auto* block_statement : block.statements()) {
|
|
ResolveControlFlow(block_statement, loop, function);
|
|
}
|
|
return;
|
|
}
|
|
case StatementKind::While:
|
|
ResolveControlFlow(&cast<While>(*statement).body(), statement, function);
|
|
return;
|
|
case StatementKind::Match: {
|
|
auto& match = cast<Match>(*statement);
|
|
for (Match::Clause& clause : match.clauses()) {
|
|
ResolveControlFlow(&clause.statement(), loop, function);
|
|
}
|
|
return;
|
|
}
|
|
case StatementKind::Continuation:
|
|
ResolveControlFlow(&cast<Continuation>(*statement).body(), std::nullopt,
|
|
std::nullopt);
|
|
return;
|
|
case StatementKind::ExpressionStatement:
|
|
case StatementKind::Assign:
|
|
case StatementKind::VariableDefinition:
|
|
case StatementKind::Run:
|
|
case StatementKind::Await:
|
|
return;
|
|
}
|
|
}
|
|
|
|
void ResolveControlFlow(Nonnull<Declaration*> declaration) {
|
|
switch (declaration->kind()) {
|
|
case DeclarationKind::FunctionDeclaration: {
|
|
auto& function = cast<FunctionDeclaration>(*declaration);
|
|
if (function.body().has_value()) {
|
|
FunctionData data = {.declaration = &function};
|
|
ResolveControlFlow(*function.body(), std::nullopt, &data);
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ClassDeclaration: {
|
|
auto& class_decl = cast<ClassDeclaration>(*declaration);
|
|
for (Nonnull<Declaration*> member : class_decl.members()) {
|
|
ResolveControlFlow(member);
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::InterfaceDeclaration: {
|
|
auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
|
|
for (Nonnull<Declaration*> member : iface_decl.members()) {
|
|
ResolveControlFlow(member);
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ImplDeclaration: {
|
|
auto& impl_decl = cast<ImplDeclaration>(*declaration);
|
|
for (Nonnull<Declaration*> member : impl_decl.members()) {
|
|
ResolveControlFlow(member);
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ChoiceDeclaration:
|
|
case DeclarationKind::VariableDeclaration:
|
|
// do nothing
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ResolveControlFlow(AST& ast) {
|
|
for (auto declaration : ast.declarations) {
|
|
ResolveControlFlow(declaration);
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|