mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-03 22:02:51 +01:00
Adds basic support for class functions and methods. (#1057)
* adding methods to the ast * pre commit stuff? * implementation of class functions * implemented methods * some cleanup * more cleanup * add newlines in test programs * pre-commit fixups * added include of return_term.h * a test of a method calling another method * replacing Member with Declaration * removing the member.h etc files * clarify a type annotation * update uses of FunctionDeclaration * remove ReturnTarget, no longer needed * more cleanup * more cleanup * yet more cleanup, playing with pre-commit * did a pre-commit run --all-files * fixed const issue * remove comment * checking dependencies in BUILD files and headers * pre-commit working now * refactor NominalClassType to just hold a pointer to the class declaration * remove Member from rtti * responding to Geoffreys review * change field_types to a non-member function
This commit is contained in:
@@ -77,8 +77,8 @@ cc_library(
|
||||
],
|
||||
deps = [
|
||||
":ast_node",
|
||||
":member",
|
||||
":pattern",
|
||||
":return_term",
|
||||
":source_location",
|
||||
":statement",
|
||||
":static_scope",
|
||||
@@ -89,6 +89,18 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "return_term",
|
||||
hdrs = ["return_term.h"],
|
||||
deps = [
|
||||
":source_location",
|
||||
"//common:check",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/common:nonnull",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "expression",
|
||||
srcs = ["expression.cpp"],
|
||||
@@ -119,20 +131,6 @@ cc_test(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "member",
|
||||
srcs = ["member.cpp"],
|
||||
hdrs = ["member.h"],
|
||||
deps = [
|
||||
":expression",
|
||||
":pattern",
|
||||
":source_location",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/common:arena",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "library_name",
|
||||
hdrs = ["library_name.h"],
|
||||
@@ -186,7 +184,6 @@ cc_library(
|
||||
":source_location",
|
||||
":value_category",
|
||||
"//common:check",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:error",
|
||||
"//executable_semantics/common:nonnull",
|
||||
],
|
||||
@@ -209,6 +206,7 @@ cc_library(
|
||||
":ast_node",
|
||||
":expression",
|
||||
":pattern",
|
||||
":return_term",
|
||||
":source_location",
|
||||
":static_scope",
|
||||
":value_category",
|
||||
|
||||
@@ -50,5 +50,3 @@ abstract class Expression : AstNode;
|
||||
class IdentifierExpression : Expression;
|
||||
class IntrinsicExpression : Expression;
|
||||
class UnimplementedExpression : Expression;
|
||||
abstract class Member : AstNode;
|
||||
class FieldMember : Member;
|
||||
|
||||
@@ -104,7 +104,7 @@ TEST(MatchesReturnTest, BasicUsage) {
|
||||
TEST(MatchesFunctionDeclarationTest, BasicUsage) {
|
||||
TuplePattern params(DummyLoc, {});
|
||||
Block body(DummyLoc, {});
|
||||
FunctionDeclaration decl(DummyLoc, "Foo", {}, ¶ms,
|
||||
FunctionDeclaration decl(DummyLoc, "Foo", {}, std::nullopt, ¶ms,
|
||||
ReturnTerm::Omitted(DummyLoc), &body);
|
||||
|
||||
EXPECT_THAT(decl, MatchesFunctionDeclaration());
|
||||
@@ -117,7 +117,7 @@ TEST(MatchesFunctionDeclarationTest, BasicUsage) {
|
||||
EXPECT_THAT(decl,
|
||||
Not(MatchesFunctionDeclaration().WithBody(MatchesLiteral(0))));
|
||||
|
||||
FunctionDeclaration forward_decl(DummyLoc, "Foo", {}, ¶ms,
|
||||
FunctionDeclaration forward_decl(DummyLoc, "Foo", {}, std::nullopt, ¶ms,
|
||||
ReturnTerm::Omitted(DummyLoc), std::nullopt);
|
||||
EXPECT_THAT(forward_decl, MatchesFunctionDeclaration().WithName("Foo"));
|
||||
EXPECT_THAT(forward_decl, Not(MatchesFunctionDeclaration().WithBody(_)));
|
||||
@@ -150,7 +150,7 @@ TEST(MatchesUnimplementedExpressionTest, BasicUsage) {
|
||||
TEST(ASTDeclarationsTest, BasicUsage) {
|
||||
TuplePattern params(DummyLoc, {});
|
||||
Block body(DummyLoc, {});
|
||||
FunctionDeclaration decl(DummyLoc, "Foo", {}, ¶ms,
|
||||
FunctionDeclaration decl(DummyLoc, "Foo", {}, std::nullopt, ¶ms,
|
||||
ReturnTerm::Omitted(DummyLoc), &body);
|
||||
AST ast = {.declarations = {&decl}};
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
case DeclarationKind::ClassDeclaration: {
|
||||
const auto& class_decl = cast<ClassDeclaration>(*this);
|
||||
out << "class " << class_decl.name() << " {\n";
|
||||
for (Nonnull<Member*> m : class_decl.members()) {
|
||||
for (Nonnull<Declaration*> m : class_decl.members()) {
|
||||
out << *m;
|
||||
}
|
||||
out << "}\n";
|
||||
@@ -41,7 +41,11 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
|
||||
case DeclarationKind::VariableDeclaration: {
|
||||
const auto& var = cast<VariableDeclaration>(*this);
|
||||
out << "var " << var.binding() << " = " << var.initializer() << "\n";
|
||||
out << "var " << var.binding();
|
||||
if (var.has_initializer()) {
|
||||
out << " = " << var.initializer();
|
||||
}
|
||||
out << ";\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -64,6 +68,31 @@ void ReturnTerm::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
}
|
||||
|
||||
// Look for the `me` parameter in the `deduced_parameters_`
|
||||
// and put it in the `me_pattern_`.
|
||||
void FunctionDeclaration::ResolveDeducedAndReceiver(
|
||||
const std::vector<Nonnull<AstNode*>>& deduced_params) {
|
||||
for (Nonnull<AstNode*> param : deduced_params) {
|
||||
switch (param->kind()) {
|
||||
case AstNodeKind::GenericBinding:
|
||||
deduced_parameters_.push_back(&cast<GenericBinding>(*param));
|
||||
break;
|
||||
case AstNodeKind::BindingPattern: {
|
||||
Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
|
||||
if (me_pattern_.has_value() || bp->name() != "me") {
|
||||
FATAL_COMPILATION_ERROR(source_loc())
|
||||
<< "illegal binding pattern in implicit parameter list";
|
||||
}
|
||||
me_pattern_ = bp;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
FATAL_COMPILATION_ERROR(source_loc())
|
||||
<< "illegal AST node in implicit parameter list";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
out << "fn " << name_ << " ";
|
||||
if (!deduced_parameters_.empty()) {
|
||||
|
||||
@@ -10,8 +10,9 @@
|
||||
#include <vector>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/member.h"
|
||||
#include "executable_semantics/ast/ast_node.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/return_term.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
#include "executable_semantics/ast/static_scope.h"
|
||||
@@ -34,8 +35,8 @@ class Declaration : public AstNode {
|
||||
public:
|
||||
~Declaration() override = 0;
|
||||
|
||||
Declaration(const Member&) = delete;
|
||||
auto operator=(const Member&) -> Declaration& = delete;
|
||||
Declaration(const Declaration&) = delete;
|
||||
auto operator=(const Declaration&) -> Declaration& = delete;
|
||||
|
||||
void Print(llvm::raw_ostream& out) const override;
|
||||
|
||||
@@ -126,100 +127,24 @@ class GenericBinding : public AstNode {
|
||||
std::optional<Nonnull<const Value*>> constant_value_;
|
||||
};
|
||||
|
||||
// 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;
|
||||
auto operator=(const ReturnTerm&) -> 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:
|
||||
using ImplementsCarbonNamedEntity = void;
|
||||
|
||||
FunctionDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<GenericBinding*>> deduced_params,
|
||||
std::vector<Nonnull<AstNode*>> deduced_params,
|
||||
std::optional<Nonnull<BindingPattern*>> me_pattern,
|
||||
Nonnull<TuplePattern*> param_pattern,
|
||||
ReturnTerm return_term,
|
||||
std::optional<Nonnull<Block*>> body)
|
||||
: Declaration(AstNodeKind::FunctionDeclaration, source_loc),
|
||||
name_(std::move(name)),
|
||||
deduced_parameters_(std::move(deduced_params)),
|
||||
me_pattern_(me_pattern),
|
||||
param_pattern_(param_pattern),
|
||||
return_term_(return_term),
|
||||
body_(body) {}
|
||||
body_(body) {
|
||||
ResolveDeducedAndReceiver(deduced_params);
|
||||
}
|
||||
|
||||
static auto classof(const AstNode* node) -> bool {
|
||||
return InheritsFromFunctionDeclaration(node->kind());
|
||||
@@ -235,6 +160,8 @@ class FunctionDeclaration : public Declaration {
|
||||
auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
|
||||
return deduced_parameters_;
|
||||
}
|
||||
auto me_pattern() const -> const BindingPattern& { return **me_pattern_; }
|
||||
auto me_pattern() -> BindingPattern& { return **me_pattern_; }
|
||||
auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
|
||||
auto param_pattern() -> TuplePattern& { return *param_pattern_; }
|
||||
auto return_term() const -> const ReturnTerm& { return return_term_; }
|
||||
@@ -254,9 +181,13 @@ class FunctionDeclaration : public Declaration {
|
||||
constant_value_ = value;
|
||||
}
|
||||
|
||||
bool is_method() const { return me_pattern_.has_value(); }
|
||||
|
||||
private:
|
||||
void ResolveDeducedAndReceiver(const std::vector<Nonnull<AstNode*>>&);
|
||||
std::string name_;
|
||||
std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
|
||||
std::optional<Nonnull<BindingPattern*>> me_pattern_;
|
||||
Nonnull<TuplePattern*> param_pattern_;
|
||||
ReturnTerm return_term_;
|
||||
std::optional<Nonnull<Block*>> body_;
|
||||
@@ -268,7 +199,7 @@ class ClassDeclaration : public Declaration {
|
||||
using ImplementsCarbonNamedEntity = void;
|
||||
|
||||
ClassDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<Member*>> members)
|
||||
std::vector<Nonnull<Declaration*>> members)
|
||||
: Declaration(AstNodeKind::ClassDeclaration, source_loc),
|
||||
name_(std::move(name)),
|
||||
members_(std::move(members)) {}
|
||||
@@ -278,7 +209,9 @@ class ClassDeclaration : public Declaration {
|
||||
}
|
||||
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
auto members() const -> llvm::ArrayRef<Nonnull<Member*>> { return members_; }
|
||||
auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
|
||||
return members_;
|
||||
}
|
||||
|
||||
auto value_category() const -> ValueCategory { return ValueCategory::Let; }
|
||||
auto constant_value() const -> std::optional<Nonnull<const Value*>> {
|
||||
@@ -294,7 +227,7 @@ class ClassDeclaration : public Declaration {
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::vector<Nonnull<Member*>> members_;
|
||||
std::vector<Nonnull<Declaration*>> members_;
|
||||
std::optional<Nonnull<const Value*>> constant_value_;
|
||||
};
|
||||
|
||||
@@ -367,7 +300,7 @@ class VariableDeclaration : public Declaration {
|
||||
public:
|
||||
VariableDeclaration(SourceLocation source_loc,
|
||||
Nonnull<BindingPattern*> binding,
|
||||
Nonnull<Expression*> initializer)
|
||||
std::optional<Nonnull<Expression*>> initializer)
|
||||
: Declaration(AstNodeKind::VariableDeclaration, source_loc),
|
||||
binding_(binding),
|
||||
initializer_(initializer) {}
|
||||
@@ -378,15 +311,17 @@ class VariableDeclaration : public Declaration {
|
||||
|
||||
auto binding() const -> const BindingPattern& { return *binding_; }
|
||||
auto binding() -> BindingPattern& { return *binding_; }
|
||||
auto initializer() const -> const Expression& { return *initializer_; }
|
||||
auto initializer() -> Expression& { return *initializer_; }
|
||||
auto initializer() const -> const Expression& { return **initializer_; }
|
||||
auto initializer() -> Expression& { return **initializer_; }
|
||||
|
||||
bool has_initializer() const { return initializer_.has_value(); }
|
||||
|
||||
private:
|
||||
// TODO: split this into a non-optional name and a type, initialized by
|
||||
// a constructor that takes a BindingPattern and handles errors like a
|
||||
// missing name.
|
||||
Nonnull<BindingPattern*> binding_;
|
||||
Nonnull<Expression*> initializer_;
|
||||
std::optional<Nonnull<Expression*>> initializer_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
// 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/ast/member.h"
|
||||
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
using llvm::cast;
|
||||
|
||||
Member::~Member() = default;
|
||||
|
||||
void Member::Print(llvm::raw_ostream& out) const {
|
||||
switch (kind()) {
|
||||
case MemberKind::FieldMember:
|
||||
const auto& field = cast<FieldMember>(*this);
|
||||
out << "var " << field.binding() << ";\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
@@ -1,70 +0,0 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_MEMBER_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_MEMBER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// Abstract base class of all AST nodes representing patterns.
|
||||
//
|
||||
// Member and its derived classes support LLVM-style RTTI, including
|
||||
// llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
|
||||
// class derived from Member must provide a `classof` operation, and
|
||||
// every concrete derived class must have a corresponding enumerator
|
||||
// in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
|
||||
// details.
|
||||
class Member : public AstNode {
|
||||
public:
|
||||
~Member() override = 0;
|
||||
|
||||
Member(const Member&) = delete;
|
||||
auto operator=(const Member&) -> Member& = delete;
|
||||
|
||||
void Print(llvm::raw_ostream& out) const override;
|
||||
|
||||
static auto classof(const AstNode* node) -> bool {
|
||||
return InheritsFromMember(node->kind());
|
||||
}
|
||||
|
||||
// Returns the enumerator corresponding to the most-derived type of this
|
||||
// object.
|
||||
auto kind() const -> MemberKind {
|
||||
return static_cast<MemberKind>(root_kind());
|
||||
}
|
||||
|
||||
protected:
|
||||
Member(AstNodeKind kind, SourceLocation source_loc)
|
||||
: AstNode(kind, source_loc) {}
|
||||
};
|
||||
|
||||
class FieldMember : public Member {
|
||||
public:
|
||||
FieldMember(SourceLocation source_loc, Nonnull<BindingPattern*> binding)
|
||||
: Member(AstNodeKind::FieldMember, source_loc), binding_(binding) {
|
||||
CHECK(binding->name() != AnonymousName);
|
||||
}
|
||||
|
||||
static auto classof(const AstNode* node) -> bool {
|
||||
return InheritsFromFieldMember(node->kind());
|
||||
}
|
||||
|
||||
auto binding() const -> const BindingPattern& { return *binding_; }
|
||||
auto binding() -> BindingPattern& { return *binding_; }
|
||||
|
||||
private:
|
||||
Nonnull<BindingPattern*> binding_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_MEMBER_H_
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/ast/static_scope.h"
|
||||
#include "executable_semantics/ast/value_category.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_RETURN_TERM_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_RETURN_TERM_H_
|
||||
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/common/nonnull.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class Value;
|
||||
class Expression;
|
||||
|
||||
// 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;
|
||||
auto operator=(const ReturnTerm&) -> 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_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_RETURN_TERM_H_
|
||||
@@ -9,8 +9,10 @@
|
||||
#include <vector>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/ast_node.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/return_term.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/ast/static_scope.h"
|
||||
#include "executable_semantics/ast/value_category.h"
|
||||
|
||||
Reference in New Issue
Block a user