mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 10:01:06 +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:
@@ -45,6 +45,19 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
}
|
||||
|
||||
void ReturnTerm::Print(llvm::raw_ostream& out) const {
|
||||
switch (kind_) {
|
||||
case ReturnKind::Omitted:
|
||||
return;
|
||||
case ReturnKind::Auto:
|
||||
out << "-> auto";
|
||||
return;
|
||||
case ReturnKind::Expression:
|
||||
out << "-> " << **type_expression_;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
out << "fn " << name_ << " ";
|
||||
if (!deduced_parameters_.empty()) {
|
||||
@@ -60,10 +73,7 @@ void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
}
|
||||
out << "]";
|
||||
}
|
||||
out << *param_pattern_;
|
||||
if (!is_omitted_return_type_) {
|
||||
out << " -> " << *return_type_;
|
||||
}
|
||||
out << *param_pattern_ << return_term_;
|
||||
if (body_) {
|
||||
out << " {\n";
|
||||
(*body_)->PrintDepth(depth, out);
|
||||
|
||||
@@ -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_;
|
||||
};
|
||||
|
||||
@@ -352,12 +352,10 @@ class FunctionTypeLiteral : public Expression {
|
||||
public:
|
||||
explicit FunctionTypeLiteral(SourceLocation source_loc,
|
||||
Nonnull<Expression*> parameter,
|
||||
Nonnull<Expression*> return_type,
|
||||
bool is_omitted_return_type)
|
||||
Nonnull<Expression*> return_type)
|
||||
: Expression(Kind::FunctionTypeLiteral, source_loc),
|
||||
parameter_(parameter),
|
||||
return_type_(return_type),
|
||||
is_omitted_return_type_(is_omitted_return_type) {}
|
||||
return_type_(return_type) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::FunctionTypeLiteral;
|
||||
@@ -367,14 +365,10 @@ class FunctionTypeLiteral : public Expression {
|
||||
auto parameter() -> Expression& { return *parameter_; }
|
||||
auto return_type() const -> const Expression& { return *return_type_; }
|
||||
auto return_type() -> Expression& { return *return_type_; }
|
||||
auto is_omitted_return_type() const -> bool {
|
||||
return is_omitted_return_type_;
|
||||
}
|
||||
|
||||
private:
|
||||
Nonnull<Expression*> parameter_;
|
||||
Nonnull<Expression*> return_type_;
|
||||
bool is_omitted_return_type_;
|
||||
};
|
||||
|
||||
class BoolTypeLiteral : public Expression {
|
||||
|
||||
@@ -198,9 +198,10 @@ class Return : public Statement {
|
||||
// structure of the AST: the return value is not a child of this node,
|
||||
// but an ancestor.
|
||||
auto function() const -> const FunctionDeclaration& { return **function_; }
|
||||
auto function() -> FunctionDeclaration& { return **function_; }
|
||||
|
||||
// Can only be called once, by ResolveControlFlow.
|
||||
void set_function(Nonnull<const FunctionDeclaration*> function) {
|
||||
void set_function(Nonnull<FunctionDeclaration*> function) {
|
||||
CHECK(!function_.has_value());
|
||||
function_ = function;
|
||||
}
|
||||
@@ -208,7 +209,7 @@ class Return : public Statement {
|
||||
private:
|
||||
Nonnull<Expression*> expression_;
|
||||
bool is_omitted_expression_;
|
||||
std::optional<Nonnull<const FunctionDeclaration*>> function_;
|
||||
std::optional<Nonnull<FunctionDeclaration*>> function_;
|
||||
};
|
||||
|
||||
class While : public Statement {
|
||||
|
||||
Reference in New Issue
Block a user