mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-03 10:35:49 +01:00
I'm doing this to avoid macro name conflicts, following https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros: "If you do export a macro from a header, it must have a globally unique name. To achieve this, it must be named with a prefix consisting of your project's namespace name (but upper case)." Commands run: ``` sed -i 's/\(DCHECK\|CHECK\|FATAL\|MAKE_UNIQUE_NAME\|MAKE_UNIQUE_NAME_IMPL\|RAW_EXITING_STREAM\|RETURN_IF_ERROR\|RETURN_IF_ERROR_IMPL\|ASSIGN_OR_RETURN\|ASSIGN_OR_RETURN_IMPL\|DIAGNOSTIC_KIND\|RETURN_IF_STACK_LIMITED\)(/CARBON_\1(/g' $(git ls-files *.cpp *.h *.lpp *.ypp *.def ':!third_party') sed -i 's/#undef DIAGNOSTIC_KIND/#undef CARBON_DIAGNOSTIC_KIND/' toolchain/diagnostics/diagnostic_registry.def ``` Note this isn't *quite* everything, but it's intended to be a large pass at everything: ``` ╚╡git grep '#define ' *.cpp *.h *.lpp *.ypp *.def ':!third_party' | grep -v '#define CARBON' | grep -v _H_ explorer/syntax/lexer.lpp: #define YY_USER_ACTION \ explorer/syntax/lexer.lpp: #define SIMPLE_TOKEN(name) \ explorer/syntax/lexer.lpp: #define ARG_TOKEN(name, arg) \ explorer/syntax/parse_and_lex_context.h:#define YY_DECL \ migrate_cpp/cpp_refactoring/var_decl.cpp:#define ABSTRACT_TYPE(Class, Base) migrate_cpp/cpp_refactoring/var_decl.cpp:#define TYPE(Class, Base) \ ``` We may in particular want to do a pass to clean up #ifdef guards and make them be CARBON_ rooted.
171 lines
6.2 KiB
C++
171 lines
6.2 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 "explorer/interpreter/resolve_control_flow.h"
|
|
|
|
#include "explorer/ast/declaration.h"
|
|
#include "explorer/ast/return_term.h"
|
|
#include "explorer/ast/statement.h"
|
|
#include "explorer/common/error_builders.h"
|
|
#include "llvm/Support/Casting.h"
|
|
#include "llvm/Support/Error.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 auto ResolveControlFlow(Nonnull<Statement*> statement,
|
|
std::optional<Nonnull<const Statement*>> loop,
|
|
std::optional<Nonnull<FunctionData*>> function)
|
|
-> ErrorOr<Success> {
|
|
switch (statement->kind()) {
|
|
case StatementKind::Return: {
|
|
if (!function.has_value()) {
|
|
return CompilationError(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) {
|
|
return CompilationError(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()) {
|
|
return CompilationError(ret.source_loc())
|
|
<< ret << " should"
|
|
<< (function_return.is_omitted() ? " not" : "")
|
|
<< " provide a return value, to match the function's signature.";
|
|
}
|
|
return Success();
|
|
}
|
|
case StatementKind::Break:
|
|
if (!loop.has_value()) {
|
|
return CompilationError(statement->source_loc())
|
|
<< "break is not within a loop body";
|
|
}
|
|
cast<Break>(*statement).set_loop(*loop);
|
|
return Success();
|
|
case StatementKind::Continue:
|
|
if (!loop.has_value()) {
|
|
return CompilationError(statement->source_loc())
|
|
<< "continue is not within a loop body";
|
|
}
|
|
cast<Continue>(*statement).set_loop(*loop);
|
|
return Success();
|
|
case StatementKind::If: {
|
|
auto& if_stmt = cast<If>(*statement);
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveControlFlow(&if_stmt.then_block(), loop, function));
|
|
if (if_stmt.else_block().has_value()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveControlFlow(*if_stmt.else_block(), loop, function));
|
|
}
|
|
return Success();
|
|
}
|
|
case StatementKind::Block: {
|
|
auto& block = cast<Block>(*statement);
|
|
for (auto* block_statement : block.statements()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveControlFlow(block_statement, loop, function));
|
|
}
|
|
return Success();
|
|
}
|
|
case StatementKind::While:
|
|
CARBON_RETURN_IF_ERROR(ResolveControlFlow(&cast<While>(*statement).body(),
|
|
statement, function));
|
|
return Success();
|
|
case StatementKind::Match: {
|
|
auto& match = cast<Match>(*statement);
|
|
for (Match::Clause& clause : match.clauses()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveControlFlow(&clause.statement(), loop, function));
|
|
}
|
|
return Success();
|
|
}
|
|
case StatementKind::Continuation:
|
|
CARBON_RETURN_IF_ERROR(ResolveControlFlow(
|
|
&cast<Continuation>(*statement).body(), std::nullopt, std::nullopt));
|
|
return Success();
|
|
case StatementKind::ExpressionStatement:
|
|
case StatementKind::Assign:
|
|
case StatementKind::VariableDefinition:
|
|
case StatementKind::Run:
|
|
case StatementKind::Await:
|
|
return Success();
|
|
}
|
|
}
|
|
|
|
auto ResolveControlFlow(Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
|
|
switch (declaration->kind()) {
|
|
case DeclarationKind::FunctionDeclaration: {
|
|
auto& function = cast<FunctionDeclaration>(*declaration);
|
|
if (function.body().has_value()) {
|
|
FunctionData data = {.declaration = &function};
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveControlFlow(*function.body(), std::nullopt, &data));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ClassDeclaration: {
|
|
auto& class_decl = cast<ClassDeclaration>(*declaration);
|
|
for (Nonnull<Declaration*> member : class_decl.members()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::InterfaceDeclaration: {
|
|
auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
|
|
for (Nonnull<Declaration*> member : iface_decl.members()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ImplDeclaration: {
|
|
auto& impl_decl = cast<ImplDeclaration>(*declaration);
|
|
for (Nonnull<Declaration*> member : impl_decl.members()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ChoiceDeclaration:
|
|
case DeclarationKind::VariableDeclaration:
|
|
case DeclarationKind::SelfDeclaration:
|
|
// do nothing
|
|
break;
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
auto ResolveControlFlow(AST& ast) -> ErrorOr<Success> {
|
|
for (auto declaration : ast.declarations) {
|
|
CARBON_RETURN_IF_ERROR(ResolveControlFlow(declaration));
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
} // namespace Carbon
|