mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 07:11:04 +01:00
Factor out AST node for function return types. (#912)
This enables us to stop treating the return type as a Pattern (which is really isn't), treat return types more consistently with other static types in the typechecker, and drop ReturnTypeContext. Additional changes: - Merge TypeCheckFunDef with TypeOfFunDef. - Handle implicit conversions in `return` statements. - Require function type literals to have an explicit `->`. - Move consistency check for omitted returns from TypeChecker to ResolveControlFlow.
This commit is contained in:
@@ -104,20 +104,97 @@ struct GenericBinding : public NamedEntityInterface {
|
||||
Nonnull<Expression*> type_;
|
||||
};
|
||||
|
||||
// The syntactic representation of a function declaration's return type.
|
||||
// This syntax can take one of three forms:
|
||||
// - An _explicit_ term consists of `->` followed by a type expression.
|
||||
// - An _auto_ term consists of `-> auto`.
|
||||
// - An _omitted_ term consists of no tokens at all.
|
||||
// Each of these forms has a corresponding factory function.
|
||||
class ReturnTerm {
|
||||
public:
|
||||
ReturnTerm(const ReturnTerm&) = default;
|
||||
ReturnTerm& operator=(const ReturnTerm&) = default;
|
||||
|
||||
// Represents an omitted return term at `source_loc`.
|
||||
static auto Omitted(SourceLocation source_loc) -> ReturnTerm {
|
||||
return ReturnTerm(ReturnKind::Omitted, source_loc);
|
||||
}
|
||||
|
||||
// Represents an auto return term at `source_loc`.
|
||||
static auto Auto(SourceLocation source_loc) -> ReturnTerm {
|
||||
return ReturnTerm(ReturnKind::Auto, source_loc);
|
||||
}
|
||||
|
||||
// Represents an explicit return term with the given type expression.
|
||||
static auto Explicit(Nonnull<Expression*> type_expression) -> ReturnTerm {
|
||||
return ReturnTerm(type_expression);
|
||||
}
|
||||
|
||||
// Returns true if this represents an omitted return term.
|
||||
auto is_omitted() const -> bool { return kind_ == ReturnKind::Omitted; }
|
||||
|
||||
// Returns true if this represents an auto return term.
|
||||
auto is_auto() const -> bool { return kind_ == ReturnKind::Auto; }
|
||||
|
||||
// If this represents an explicit return term, returns the type expression.
|
||||
// Otherwise, returns nullopt.
|
||||
auto type_expression() const -> std::optional<Nonnull<const Expression*>> {
|
||||
return type_expression_;
|
||||
}
|
||||
auto type_expression() -> std::optional<Nonnull<Expression*>> {
|
||||
return type_expression_;
|
||||
}
|
||||
|
||||
// The static return type this term resolves to. Cannot be called before
|
||||
// typechecking.
|
||||
auto static_type() const -> const Value& { return **static_type_; }
|
||||
|
||||
// Sets the value of static_type(). Can only be called once, during
|
||||
// typechecking.
|
||||
void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
|
||||
|
||||
// Returns whether static_type() has been set. Should only be called
|
||||
// during typechecking: before typechecking it's guaranteed to be false,
|
||||
// and after typechecking it's guaranteed to be true.
|
||||
auto has_static_type() const -> bool { return static_type_.has_value(); }
|
||||
|
||||
auto source_loc() const -> SourceLocation { return source_loc_; }
|
||||
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
||||
|
||||
private:
|
||||
enum class ReturnKind { Omitted, Auto, Expression };
|
||||
|
||||
explicit ReturnTerm(ReturnKind kind, SourceLocation source_loc)
|
||||
: kind_(kind), source_loc_(source_loc) {
|
||||
CHECK(kind != ReturnKind::Expression);
|
||||
}
|
||||
|
||||
explicit ReturnTerm(Nonnull<Expression*> type_expression)
|
||||
: kind_(ReturnKind::Expression),
|
||||
type_expression_(type_expression),
|
||||
source_loc_(type_expression->source_loc()) {}
|
||||
|
||||
ReturnKind kind_;
|
||||
std::optional<Nonnull<Expression*>> type_expression_;
|
||||
std::optional<Nonnull<const Value*>> static_type_;
|
||||
|
||||
SourceLocation source_loc_;
|
||||
};
|
||||
|
||||
class FunctionDeclaration : public Declaration {
|
||||
public:
|
||||
FunctionDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<GenericBinding*>> deduced_params,
|
||||
Nonnull<TuplePattern*> param_pattern,
|
||||
Nonnull<Pattern*> return_type,
|
||||
bool is_omitted_return_type,
|
||||
ReturnTerm return_term,
|
||||
std::optional<Nonnull<Block*>> body)
|
||||
: Declaration(Kind::FunctionDeclaration, source_loc),
|
||||
name_(std::move(name)),
|
||||
deduced_parameters_(std::move(deduced_params)),
|
||||
param_pattern_(param_pattern),
|
||||
return_type_(return_type),
|
||||
is_omitted_return_type_(is_omitted_return_type),
|
||||
return_term_(return_term),
|
||||
body_(body) {}
|
||||
|
||||
static auto classof(const Declaration* decl) -> bool {
|
||||
@@ -133,11 +210,8 @@ class FunctionDeclaration : public Declaration {
|
||||
}
|
||||
auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
|
||||
auto param_pattern() -> TuplePattern& { return *param_pattern_; }
|
||||
auto return_type() const -> const Pattern& { return *return_type_; }
|
||||
auto return_type() -> Pattern& { return *return_type_; }
|
||||
auto is_omitted_return_type() const -> bool {
|
||||
return is_omitted_return_type_;
|
||||
}
|
||||
auto return_term() const -> const ReturnTerm& { return return_term_; }
|
||||
auto return_term() -> ReturnTerm& { return return_term_; }
|
||||
auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
|
||||
auto body() -> std::optional<Nonnull<Block*>> { return body_; }
|
||||
|
||||
@@ -149,8 +223,7 @@ class FunctionDeclaration : public Declaration {
|
||||
std::string name_;
|
||||
std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
|
||||
Nonnull<TuplePattern*> param_pattern_;
|
||||
Nonnull<Pattern*> return_type_;
|
||||
bool is_omitted_return_type_;
|
||||
ReturnTerm return_term_;
|
||||
std::optional<Nonnull<Block*>> body_;
|
||||
StaticScope static_scope_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user