mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 11:51:04 +01:00
Initial implementation of returned var. (#1348)
This PR implements the `returned var` for the explorer. - Enabled `returned var` and `return var` syntax in lexer and parser. - Split `Return` statement into `ReturnVar` and `ReturnExpression` to distinguish the two types of returns. - Added logic in name resolution, type checking and interpretation to process `returned var` definition and `ReturnVar` statement.
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "explorer/ast/static_scope.h"
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "explorer/common/error_builders.h"
|
||||
#include "llvm/Support/Error.h"
|
||||
|
||||
@@ -71,4 +73,30 @@ auto StaticScope::TryResolve(const std::string& name,
|
||||
return result;
|
||||
}
|
||||
|
||||
auto StaticScope::AddReturnedVar(ValueNodeView returned_var_def_view)
|
||||
-> ErrorOr<Success> {
|
||||
std::optional<ValueNodeView> resolved_returned_var = ResolveReturned();
|
||||
if (resolved_returned_var.has_value()) {
|
||||
return CompilationError(returned_var_def_view.base().source_loc())
|
||||
<< "Duplicate definition of returned var also found at "
|
||||
<< resolved_returned_var->base().source_loc();
|
||||
}
|
||||
returned_var_def_view_ = std::move(returned_var_def_view);
|
||||
return Success();
|
||||
}
|
||||
|
||||
auto StaticScope::ResolveReturned() const -> std::optional<ValueNodeView> {
|
||||
if (returned_var_def_view_.has_value()) {
|
||||
return returned_var_def_view_;
|
||||
}
|
||||
for (Nonnull<const StaticScope*> parent : parent_scopes_) {
|
||||
std::optional<ValueNodeView> parent_returned_var =
|
||||
parent->ResolveReturned();
|
||||
if (parent_returned_var.has_value()) {
|
||||
return parent_returned_var;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
Reference in New Issue
Block a user