mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Refactor Expression accessor/mutator style (#883)
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
namespace Carbon {
|
||||
|
||||
using llvm::cast;
|
||||
using llvm::isa;
|
||||
|
||||
auto ExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
@@ -68,7 +69,7 @@ static void PrintFields(llvm::raw_ostream& out,
|
||||
std::string_view separator) {
|
||||
llvm::ListSeparator sep;
|
||||
for (const auto& field : fields) {
|
||||
out << sep << "." << field.name() << separator << *field.expression();
|
||||
out << sep << "." << field.name() << separator << field.expression();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,12 +77,12 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
switch (kind()) {
|
||||
case Expression::Kind::IndexExpression: {
|
||||
const auto& index = cast<IndexExpression>(*this);
|
||||
out << *index.Aggregate() << "[" << *index.Offset() << "]";
|
||||
out << index.aggregate() << "[" << index.offset() << "]";
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
const auto& access = cast<FieldAccessExpression>(*this);
|
||||
out << *access.Aggregate() << "." << access.Field();
|
||||
out << access.aggregate() << "." << access.field();
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::TupleLiteral:
|
||||
@@ -100,37 +101,43 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
out << "}";
|
||||
break;
|
||||
case Expression::Kind::IntLiteral:
|
||||
out << cast<IntLiteral>(*this).Val();
|
||||
out << cast<IntLiteral>(*this).value();
|
||||
break;
|
||||
case Expression::Kind::BoolLiteral:
|
||||
out << (cast<BoolLiteral>(*this).Val() ? "true" : "false");
|
||||
out << (cast<BoolLiteral>(*this).value() ? "true" : "false");
|
||||
break;
|
||||
case Expression::Kind::PrimitiveOperatorExpression: {
|
||||
out << "(";
|
||||
PrimitiveOperatorExpression op = cast<PrimitiveOperatorExpression>(*this);
|
||||
if (op.Arguments().size() == 0) {
|
||||
PrintOp(out, op.Op());
|
||||
} else if (op.Arguments().size() == 1) {
|
||||
PrintOp(out, op.Op());
|
||||
out << " " << *op.Arguments()[0];
|
||||
} else if (op.Arguments().size() == 2) {
|
||||
out << *op.Arguments()[0] << " ";
|
||||
PrintOp(out, op.Op());
|
||||
out << " " << *op.Arguments()[1];
|
||||
switch (op.arguments().size()) {
|
||||
case 0:
|
||||
PrintOp(out, op.op());
|
||||
break;
|
||||
case 1:
|
||||
PrintOp(out, op.op());
|
||||
out << " " << *op.arguments()[0];
|
||||
break;
|
||||
case 2:
|
||||
out << *op.arguments()[0] << " ";
|
||||
PrintOp(out, op.op());
|
||||
out << " " << *op.arguments()[1];
|
||||
break;
|
||||
default:
|
||||
FATAL() << "Unexpected argument count: " << op.arguments().size();
|
||||
}
|
||||
out << ")";
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::IdentifierExpression:
|
||||
out << cast<IdentifierExpression>(*this).Name();
|
||||
out << cast<IdentifierExpression>(*this).name();
|
||||
break;
|
||||
case Expression::Kind::CallExpression: {
|
||||
const auto& call = cast<CallExpression>(*this);
|
||||
out << *call.Function();
|
||||
if (call.Argument()->kind() == Expression::Kind::TupleLiteral) {
|
||||
out << *call.Argument();
|
||||
out << call.function();
|
||||
if (isa<TupleLiteral>(call.argument())) {
|
||||
out << call.argument();
|
||||
} else {
|
||||
out << "(" << *call.Argument() << ")";
|
||||
out << "(" << call.argument() << ")";
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -142,7 +149,7 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
break;
|
||||
case Expression::Kind::StringLiteral:
|
||||
out << "\"";
|
||||
out.write_escaped(cast<StringLiteral>(*this).Val());
|
||||
out.write_escaped(cast<StringLiteral>(*this).value());
|
||||
out << "\"";
|
||||
break;
|
||||
case Expression::Kind::StringTypeLiteral:
|
||||
@@ -156,13 +163,13 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
break;
|
||||
case Expression::Kind::FunctionTypeLiteral: {
|
||||
const auto& fn = cast<FunctionTypeLiteral>(*this);
|
||||
out << "fn " << *fn.Parameter() << " -> " << *fn.ReturnType();
|
||||
out << "fn " << fn.parameter() << " -> " << fn.return_type();
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::IntrinsicExpression:
|
||||
out << "intrinsic_expression(";
|
||||
switch (cast<IntrinsicExpression>(*this).Intrinsic()) {
|
||||
case IntrinsicExpression::IntrinsicKind::Print:
|
||||
switch (cast<IntrinsicExpression>(*this).intrinsic()) {
|
||||
case IntrinsicExpression::Intrinsic::Print:
|
||||
out << "print";
|
||||
}
|
||||
out << ")";
|
||||
|
||||
@@ -101,8 +101,8 @@ class FieldInitializer {
|
||||
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
|
||||
auto expression() const -> Nonnull<const Expression*> { return expression_; }
|
||||
auto expression() -> Nonnull<Expression*> { return expression_; }
|
||||
auto expression() const -> const Expression& { return *expression_; }
|
||||
auto expression() -> Expression& { return *expression_; }
|
||||
|
||||
private:
|
||||
// The field name. Cannot be empty.
|
||||
@@ -129,16 +129,16 @@ class IdentifierExpression : public Expression {
|
||||
public:
|
||||
explicit IdentifierExpression(SourceLocation source_loc, std::string name)
|
||||
: Expression(Kind::IdentifierExpression, source_loc),
|
||||
name(std::move(name)) {}
|
||||
name_(std::move(name)) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::IdentifierExpression;
|
||||
}
|
||||
|
||||
auto Name() const -> const std::string& { return name; }
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
std::string name_;
|
||||
};
|
||||
|
||||
class FieldAccessExpression : public Expression {
|
||||
@@ -147,20 +147,20 @@ class FieldAccessExpression : public Expression {
|
||||
Nonnull<Expression*> aggregate,
|
||||
std::string field)
|
||||
: Expression(Kind::FieldAccessExpression, source_loc),
|
||||
aggregate(aggregate),
|
||||
field(std::move(field)) {}
|
||||
aggregate_(aggregate),
|
||||
field_(std::move(field)) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::FieldAccessExpression;
|
||||
}
|
||||
|
||||
auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
|
||||
auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
|
||||
auto Field() const -> const std::string& { return field; }
|
||||
auto aggregate() const -> const Expression& { return *aggregate_; }
|
||||
auto aggregate() -> Expression& { return *aggregate_; }
|
||||
auto field() const -> const std::string& { return field_; }
|
||||
|
||||
private:
|
||||
Nonnull<Expression*> aggregate;
|
||||
std::string field;
|
||||
Nonnull<Expression*> aggregate_;
|
||||
std::string field_;
|
||||
};
|
||||
|
||||
class IndexExpression : public Expression {
|
||||
@@ -169,66 +169,66 @@ class IndexExpression : public Expression {
|
||||
Nonnull<Expression*> aggregate,
|
||||
Nonnull<Expression*> offset)
|
||||
: Expression(Kind::IndexExpression, source_loc),
|
||||
aggregate(aggregate),
|
||||
offset(offset) {}
|
||||
aggregate_(aggregate),
|
||||
offset_(offset) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::IndexExpression;
|
||||
}
|
||||
|
||||
auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
|
||||
auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
|
||||
auto Offset() const -> Nonnull<const Expression*> { return offset; }
|
||||
auto Offset() -> Nonnull<Expression*> { return offset; }
|
||||
auto aggregate() const -> const Expression& { return *aggregate_; }
|
||||
auto aggregate() -> Expression& { return *aggregate_; }
|
||||
auto offset() const -> const Expression& { return *offset_; }
|
||||
auto offset() -> Expression& { return *offset_; }
|
||||
|
||||
private:
|
||||
Nonnull<Expression*> aggregate;
|
||||
Nonnull<Expression*> offset;
|
||||
Nonnull<Expression*> aggregate_;
|
||||
Nonnull<Expression*> offset_;
|
||||
};
|
||||
|
||||
class IntLiteral : public Expression {
|
||||
public:
|
||||
explicit IntLiteral(SourceLocation source_loc, int val)
|
||||
: Expression(Kind::IntLiteral, source_loc), val(val) {}
|
||||
explicit IntLiteral(SourceLocation source_loc, int value)
|
||||
: Expression(Kind::IntLiteral, source_loc), value_(value) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::IntLiteral;
|
||||
}
|
||||
|
||||
auto Val() const -> int { return val; }
|
||||
auto value() const -> int { return value_; }
|
||||
|
||||
private:
|
||||
int val;
|
||||
int value_;
|
||||
};
|
||||
|
||||
class BoolLiteral : public Expression {
|
||||
public:
|
||||
explicit BoolLiteral(SourceLocation source_loc, bool val)
|
||||
: Expression(Kind::BoolLiteral, source_loc), val(val) {}
|
||||
explicit BoolLiteral(SourceLocation source_loc, bool value)
|
||||
: Expression(Kind::BoolLiteral, source_loc), value_(value) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::BoolLiteral;
|
||||
}
|
||||
|
||||
auto Val() const -> bool { return val; }
|
||||
auto value() const -> bool { return value_; }
|
||||
|
||||
private:
|
||||
bool val;
|
||||
bool value_;
|
||||
};
|
||||
|
||||
class StringLiteral : public Expression {
|
||||
public:
|
||||
explicit StringLiteral(SourceLocation source_loc, std::string val)
|
||||
: Expression(Kind::StringLiteral, source_loc), val(std::move(val)) {}
|
||||
explicit StringLiteral(SourceLocation source_loc, std::string value)
|
||||
: Expression(Kind::StringLiteral, source_loc), value_(std::move(value)) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::StringLiteral;
|
||||
}
|
||||
|
||||
auto Val() const -> const std::string& { return val; }
|
||||
auto value() const -> const std::string& { return value_; }
|
||||
|
||||
private:
|
||||
std::string val;
|
||||
std::string value_;
|
||||
};
|
||||
|
||||
class StringTypeLiteral : public Expression {
|
||||
@@ -317,24 +317,24 @@ class PrimitiveOperatorExpression : public Expression {
|
||||
SourceLocation source_loc, Operator op,
|
||||
std::vector<Nonnull<Expression*>> arguments)
|
||||
: Expression(Kind::PrimitiveOperatorExpression, source_loc),
|
||||
op(op),
|
||||
arguments(std::move(arguments)) {}
|
||||
op_(op),
|
||||
arguments_(std::move(arguments)) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::PrimitiveOperatorExpression;
|
||||
}
|
||||
|
||||
auto Op() const -> Operator { return op; }
|
||||
auto Arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
|
||||
return arguments;
|
||||
auto op() const -> Operator { return op_; }
|
||||
auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
|
||||
return arguments_;
|
||||
}
|
||||
auto Arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
|
||||
return arguments;
|
||||
auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
|
||||
return arguments_;
|
||||
}
|
||||
|
||||
private:
|
||||
Operator op;
|
||||
std::vector<Nonnull<Expression*>> arguments;
|
||||
Operator op_;
|
||||
std::vector<Nonnull<Expression*>> arguments_;
|
||||
};
|
||||
|
||||
class CallExpression : public Expression {
|
||||
@@ -343,21 +343,21 @@ class CallExpression : public Expression {
|
||||
Nonnull<Expression*> function,
|
||||
Nonnull<Expression*> argument)
|
||||
: Expression(Kind::CallExpression, source_loc),
|
||||
function(function),
|
||||
argument(argument) {}
|
||||
function_(function),
|
||||
argument_(argument) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::CallExpression;
|
||||
}
|
||||
|
||||
auto Function() const -> Nonnull<const Expression*> { return function; }
|
||||
auto Function() -> Nonnull<Expression*> { return function; }
|
||||
auto Argument() const -> Nonnull<const Expression*> { return argument; }
|
||||
auto Argument() -> Nonnull<Expression*> { return argument; }
|
||||
auto function() const -> const Expression& { return *function_; }
|
||||
auto function() -> Expression& { return *function_; }
|
||||
auto argument() const -> const Expression& { return *argument_; }
|
||||
auto argument() -> Expression& { return *argument_; }
|
||||
|
||||
private:
|
||||
Nonnull<Expression*> function;
|
||||
Nonnull<Expression*> argument;
|
||||
Nonnull<Expression*> function_;
|
||||
Nonnull<Expression*> argument_;
|
||||
};
|
||||
|
||||
class FunctionTypeLiteral : public Expression {
|
||||
@@ -367,24 +367,26 @@ class FunctionTypeLiteral : public Expression {
|
||||
Nonnull<Expression*> return_type,
|
||||
bool is_omitted_return_type)
|
||||
: Expression(Kind::FunctionTypeLiteral, source_loc),
|
||||
parameter(parameter),
|
||||
return_type(return_type),
|
||||
is_omitted_return_type(is_omitted_return_type) {}
|
||||
parameter_(parameter),
|
||||
return_type_(return_type),
|
||||
is_omitted_return_type_(is_omitted_return_type) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::FunctionTypeLiteral;
|
||||
}
|
||||
|
||||
auto Parameter() const -> Nonnull<const Expression*> { return parameter; }
|
||||
auto Parameter() -> Nonnull<Expression*> { return parameter; }
|
||||
auto ReturnType() const -> Nonnull<const Expression*> { return return_type; }
|
||||
auto ReturnType() -> Nonnull<Expression*> { return return_type; }
|
||||
auto IsOmittedReturnType() const -> bool { return is_omitted_return_type; }
|
||||
auto parameter() const -> const Expression& { return *parameter_; }
|
||||
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;
|
||||
Nonnull<Expression*> parameter_;
|
||||
Nonnull<Expression*> return_type_;
|
||||
bool is_omitted_return_type_;
|
||||
};
|
||||
|
||||
class BoolTypeLiteral : public Expression {
|
||||
@@ -429,22 +431,22 @@ class TypeTypeLiteral : public Expression {
|
||||
|
||||
class IntrinsicExpression : public Expression {
|
||||
public:
|
||||
enum class IntrinsicKind {
|
||||
enum class Intrinsic {
|
||||
Print,
|
||||
};
|
||||
|
||||
explicit IntrinsicExpression(IntrinsicKind intrinsic)
|
||||
explicit IntrinsicExpression(Intrinsic intrinsic)
|
||||
: Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
|
||||
intrinsic(intrinsic) {}
|
||||
intrinsic_(intrinsic) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->kind() == Kind::IntrinsicExpression;
|
||||
}
|
||||
|
||||
auto Intrinsic() const -> IntrinsicKind { return intrinsic; }
|
||||
auto intrinsic() const -> Intrinsic { return intrinsic_; }
|
||||
|
||||
private:
|
||||
IntrinsicKind intrinsic;
|
||||
Intrinsic intrinsic_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -23,7 +23,7 @@ using testing::IsEmpty;
|
||||
// `IntLiteral`
|
||||
MATCHER_P(IntFieldNamed, name, "") {
|
||||
return arg.name() == std::string(name) &&
|
||||
arg.expression()->kind() == Expression::Kind::IntLiteral;
|
||||
arg.expression().kind() == Expression::Kind::IntLiteral;
|
||||
}
|
||||
|
||||
static auto FakeSourceLoc(int line_num) -> SourceLocation {
|
||||
|
||||
@@ -90,8 +90,8 @@ AlternativePattern::AlternativePattern(SourceLocation source_loc,
|
||||
Nonnull<Expression*> alternative,
|
||||
Nonnull<TuplePattern*> arguments)
|
||||
: Pattern(Kind::AlternativePattern, source_loc),
|
||||
choice_type(RequireFieldAccess(alternative).Aggregate()),
|
||||
alternative_name(RequireFieldAccess(alternative).Field()),
|
||||
choice_type(&RequireFieldAccess(alternative).aggregate()),
|
||||
alternative_name(RequireFieldAccess(alternative).field()),
|
||||
arguments(arguments) {}
|
||||
|
||||
auto ParenExpressionToParenPattern(Nonnull<Arena*> arena,
|
||||
|
||||
@@ -23,11 +23,10 @@ static void AddIntrinsics(Nonnull<Arena*> arena,
|
||||
source_loc, "format_str",
|
||||
arena->New<ExpressionPattern>(
|
||||
arena->New<StringTypeLiteral>(source_loc))))};
|
||||
auto print_return =
|
||||
arena->New<Return>(source_loc,
|
||||
arena->New<IntrinsicExpression>(
|
||||
IntrinsicExpression::IntrinsicKind::Print),
|
||||
false);
|
||||
auto print_return = arena->New<Return>(
|
||||
source_loc,
|
||||
arena->New<IntrinsicExpression>(IntrinsicExpression::Intrinsic::Print),
|
||||
false);
|
||||
auto print = arena->New<FunctionDeclaration>(arena->New<FunctionDefinition>(
|
||||
source_loc, "Print", std::vector<GenericBinding>(),
|
||||
arena->New<TuplePattern>(source_loc, print_fields),
|
||||
|
||||
@@ -406,7 +406,7 @@ auto Interpreter::StepLvalue() -> Transition {
|
||||
// { {x :: C, E, F} :: S, H}
|
||||
// -> { {E(x) :: C, E, F} :: S, H}
|
||||
Address pointer = GetFromEnv(exp->source_loc(),
|
||||
cast<IdentifierExpression>(*exp).Name());
|
||||
cast<IdentifierExpression>(*exp).name());
|
||||
Nonnull<const Value*> v = arena->New<PointerValue>(pointer);
|
||||
return Done{v};
|
||||
}
|
||||
@@ -415,13 +415,13 @@ auto Interpreter::StepLvalue() -> Transition {
|
||||
// { {e.f :: C, E, F} :: S, H}
|
||||
// -> { e :: [].f :: C, E, F} :: S, H}
|
||||
return Spawn{arena->New<LValAction>(
|
||||
cast<FieldAccessExpression>(*exp).Aggregate())};
|
||||
&cast<FieldAccessExpression>(*exp).aggregate())};
|
||||
} else {
|
||||
// { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { &v.f :: C, E, F} :: S, H }
|
||||
Address aggregate = cast<PointerValue>(*act->results()[0]).Val();
|
||||
Address field = aggregate.SubobjectAddress(
|
||||
cast<FieldAccessExpression>(*exp).Field());
|
||||
cast<FieldAccessExpression>(*exp).field());
|
||||
return Done{arena->New<PointerValue>(field)};
|
||||
}
|
||||
}
|
||||
@@ -430,11 +430,11 @@ auto Interpreter::StepLvalue() -> Transition {
|
||||
// { {e[i] :: C, E, F} :: S, H}
|
||||
// -> { e :: [][i] :: C, E, F} :: S, H}
|
||||
return Spawn{
|
||||
arena->New<LValAction>(cast<IndexExpression>(*exp).Aggregate())};
|
||||
arena->New<LValAction>(&cast<IndexExpression>(*exp).aggregate())};
|
||||
|
||||
} else if (act->pos() == 1) {
|
||||
return Spawn{
|
||||
arena->New<ExpressionAction>(cast<IndexExpression>(*exp).Offset())};
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
&cast<IndexExpression>(*exp).offset())};
|
||||
} else {
|
||||
// { v :: [][i] :: C, E, F} :: S, H}
|
||||
// -> { { &v[i] :: C, E, F} :: S, H }
|
||||
@@ -452,9 +452,8 @@ auto Interpreter::StepLvalue() -> Transition {
|
||||
// H}
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
Nonnull<const Expression*> elt =
|
||||
cast<TupleLiteral>(*exp).fields()[act->pos()].expression();
|
||||
return Spawn{arena->New<LValAction>(elt)};
|
||||
return Spawn{arena->New<LValAction>(
|
||||
&cast<TupleLiteral>(*exp).fields()[act->pos()].expression())};
|
||||
} else {
|
||||
return Done{CreateTuple(act, exp)};
|
||||
}
|
||||
@@ -491,10 +490,10 @@ auto Interpreter::StepExp() -> Transition {
|
||||
// { { e[i] :: C, E, F} :: S, H}
|
||||
// -> { { e :: [][i] :: C, E, F} :: S, H}
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
cast<IndexExpression>(*exp).Aggregate())};
|
||||
&cast<IndexExpression>(*exp).aggregate())};
|
||||
} else if (act->pos() == 1) {
|
||||
return Spawn{
|
||||
arena->New<ExpressionAction>(cast<IndexExpression>(*exp).Offset())};
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
&cast<IndexExpression>(*exp).offset())};
|
||||
} else {
|
||||
// { { v :: [][i] :: C, E, F} :: S, H}
|
||||
// -> { { v_i :: C, E, F} : S, H}
|
||||
@@ -520,9 +519,8 @@ auto Interpreter::StepExp() -> Transition {
|
||||
// H}
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
Nonnull<const Expression*> elt =
|
||||
cast<TupleLiteral>(*exp).fields()[act->pos()].expression();
|
||||
return Spawn{arena->New<ExpressionAction>(elt)};
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
&cast<TupleLiteral>(*exp).fields()[act->pos()].expression())};
|
||||
} else {
|
||||
return Done{CreateTuple(act, exp)};
|
||||
}
|
||||
@@ -530,9 +528,8 @@ auto Interpreter::StepExp() -> Transition {
|
||||
case Expression::Kind::StructLiteral: {
|
||||
const auto& literal = cast<StructLiteral>(*exp);
|
||||
if (act->pos() < static_cast<int>(literal.fields().size())) {
|
||||
Nonnull<const Expression*> elt =
|
||||
literal.fields()[act->pos()].expression();
|
||||
return Spawn{arena->New<ExpressionAction>(elt)};
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
&literal.fields()[act->pos()].expression())};
|
||||
} else {
|
||||
return Done{CreateStruct(literal.fields(), act->results())};
|
||||
}
|
||||
@@ -541,7 +538,7 @@ auto Interpreter::StepExp() -> Transition {
|
||||
const auto& struct_type = cast<StructTypeLiteral>(*exp);
|
||||
if (act->pos() < static_cast<int>(struct_type.fields().size())) {
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
struct_type.fields()[act->pos()].expression())};
|
||||
&struct_type.fields()[act->pos()].expression())};
|
||||
} else {
|
||||
VarValues fields;
|
||||
for (size_t i = 0; i < struct_type.fields().size(); ++i) {
|
||||
@@ -555,40 +552,40 @@ auto Interpreter::StepExp() -> Transition {
|
||||
if (act->pos() == 0) {
|
||||
// { { e.f :: C, E, F} :: S, H}
|
||||
// -> { { e :: [].f :: C, E, F} :: S, H}
|
||||
return Spawn{arena->New<ExpressionAction>(access.Aggregate())};
|
||||
return Spawn{arena->New<ExpressionAction>(&access.aggregate())};
|
||||
} else {
|
||||
// { { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { v_f :: C, E, F} : S, H}
|
||||
return Done{act->results()[0]->GetField(
|
||||
arena, FieldPath(access.Field()), exp->source_loc())};
|
||||
arena, FieldPath(access.field()), exp->source_loc())};
|
||||
}
|
||||
}
|
||||
case Expression::Kind::IdentifierExpression: {
|
||||
CHECK(act->pos() == 0);
|
||||
const auto& ident = cast<IdentifierExpression>(*exp);
|
||||
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
|
||||
Address pointer = GetFromEnv(exp->source_loc(), ident.Name());
|
||||
Address pointer = GetFromEnv(exp->source_loc(), ident.name());
|
||||
return Done{heap.Read(pointer, exp->source_loc())};
|
||||
}
|
||||
case Expression::Kind::IntLiteral:
|
||||
CHECK(act->pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
return Done{arena->New<IntValue>(cast<IntLiteral>(*exp).Val())};
|
||||
return Done{arena->New<IntValue>(cast<IntLiteral>(*exp).value())};
|
||||
case Expression::Kind::BoolLiteral:
|
||||
CHECK(act->pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
return Done{arena->New<BoolValue>(cast<BoolLiteral>(*exp).Val())};
|
||||
return Done{arena->New<BoolValue>(cast<BoolLiteral>(*exp).value())};
|
||||
case Expression::Kind::PrimitiveOperatorExpression: {
|
||||
const auto& op = cast<PrimitiveOperatorExpression>(*exp);
|
||||
if (act->pos() != static_cast<int>(op.Arguments().size())) {
|
||||
if (act->pos() != static_cast<int>(op.arguments().size())) {
|
||||
// { {v :: op(vs,[],e,es) :: C, E, F} :: S, H}
|
||||
// -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H}
|
||||
Nonnull<const Expression*> arg = op.Arguments()[act->pos()];
|
||||
Nonnull<const Expression*> arg = op.arguments()[act->pos()];
|
||||
return Spawn{arena->New<ExpressionAction>(arg)};
|
||||
} else {
|
||||
// { {v :: op(vs,[]) :: C, E, F} :: S, H}
|
||||
// -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H}
|
||||
return Done{EvalPrim(op.Op(), act->results(), exp->source_loc())};
|
||||
return Done{EvalPrim(op.op(), act->results(), exp->source_loc())};
|
||||
}
|
||||
}
|
||||
case Expression::Kind::CallExpression:
|
||||
@@ -596,12 +593,12 @@ auto Interpreter::StepExp() -> Transition {
|
||||
// { {e1(e2) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: [](e2) :: C, E, F} :: S, H}
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
cast<CallExpression>(*exp).Function())};
|
||||
&cast<CallExpression>(*exp).function())};
|
||||
} else if (act->pos() == 1) {
|
||||
// { { v :: [](e) :: C, E, F} :: S, H}
|
||||
// -> { { e :: v([]) :: C, E, F} :: S, H}
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
cast<CallExpression>(*exp).Argument())};
|
||||
&cast<CallExpression>(*exp).argument())};
|
||||
} else if (act->pos() == 2) {
|
||||
// { { v2 :: v1([]) :: C, E, F} :: S, H}
|
||||
// -> { {C',E',F'} :: {C, E, F} :: S, H}
|
||||
@@ -637,8 +634,8 @@ auto Interpreter::StepExp() -> Transition {
|
||||
case Expression::Kind::IntrinsicExpression:
|
||||
CHECK(act->pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
switch (cast<IntrinsicExpression>(*exp).Intrinsic()) {
|
||||
case IntrinsicExpression::IntrinsicKind::Print:
|
||||
switch (cast<IntrinsicExpression>(*exp).intrinsic()) {
|
||||
case IntrinsicExpression::Intrinsic::Print:
|
||||
Address pointer = GetFromEnv(exp->source_loc(), "format_str");
|
||||
Nonnull<const Value*> pointee = heap.Read(pointer, exp->source_loc());
|
||||
CHECK(pointee->kind() == Value::Kind::StringValue);
|
||||
@@ -662,12 +659,12 @@ auto Interpreter::StepExp() -> Transition {
|
||||
case Expression::Kind::FunctionTypeLiteral: {
|
||||
if (act->pos() == 0) {
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
cast<FunctionTypeLiteral>(*exp).Parameter())};
|
||||
&cast<FunctionTypeLiteral>(*exp).parameter())};
|
||||
} else if (act->pos() == 1) {
|
||||
// { { pt :: fn [] -> e :: C, E, F} :: S, H}
|
||||
// -> { { e :: fn pt -> []) :: C, E, F} :: S, H}
|
||||
return Spawn{arena->New<ExpressionAction>(
|
||||
cast<FunctionTypeLiteral>(*exp).ReturnType())};
|
||||
&cast<FunctionTypeLiteral>(*exp).return_type())};
|
||||
} else {
|
||||
// { { rt :: fn pt -> [] :: C, E, F} :: S, H}
|
||||
// -> { fn pt -> rt :: {C, E, F} :: S, H}
|
||||
@@ -683,7 +680,7 @@ auto Interpreter::StepExp() -> Transition {
|
||||
case Expression::Kind::StringLiteral:
|
||||
CHECK(act->pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
return Done{arena->New<StringValue>(cast<StringLiteral>(*exp).Val())};
|
||||
return Done{arena->New<StringValue>(cast<StringLiteral>(*exp).value())};
|
||||
case Expression::Kind::StringTypeLiteral: {
|
||||
CHECK(act->pos() == 0);
|
||||
return Done{arena->New<StringType>()};
|
||||
@@ -719,8 +716,8 @@ auto Interpreter::StepPattern() -> Transition {
|
||||
// H}
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
Nonnull<const Pattern*> elt = tuple.Fields()[act->pos()].pattern;
|
||||
return Spawn{arena->New<PatternAction>(elt)};
|
||||
return Spawn{
|
||||
arena->New<PatternAction>(tuple.Fields()[act->pos()].pattern)};
|
||||
} else {
|
||||
std::vector<TupleElement> elements;
|
||||
for (size_t i = 0; i < tuple.Fields().size(); ++i) {
|
||||
|
||||
@@ -435,12 +435,12 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
switch (e->kind()) {
|
||||
case Expression::Kind::IndexExpression: {
|
||||
auto& index = cast<IndexExpression>(*e);
|
||||
auto res = TypeCheckExp(index.Aggregate(), types, values);
|
||||
Nonnull<const Value*> aggregate_type = index.Aggregate()->static_type();
|
||||
auto res = TypeCheckExp(&index.aggregate(), types, values);
|
||||
Nonnull<const Value*> aggregate_type = index.aggregate().static_type();
|
||||
switch (aggregate_type->kind()) {
|
||||
case Value::Kind::TupleValue: {
|
||||
auto i =
|
||||
cast<IntValue>(*interpreter.InterpExp(values, index.Offset()))
|
||||
cast<IntValue>(*interpreter.InterpExp(values, &index.offset()))
|
||||
.Val();
|
||||
std::string f = std::to_string(i);
|
||||
std::optional<Nonnull<const Value*>> field_t =
|
||||
@@ -461,11 +461,11 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
std::vector<TupleElement> arg_types;
|
||||
auto new_types = types;
|
||||
for (auto& arg : cast<TupleLiteral>(*e).fields()) {
|
||||
auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
|
||||
auto arg_res = TypeCheckExp(&arg.expression(), new_types, values);
|
||||
new_types = arg_res.types;
|
||||
new_args.push_back(FieldInitializer(arg.name(), arg.expression()));
|
||||
new_args.push_back(FieldInitializer(arg.name(), &arg.expression()));
|
||||
arg_types.push_back(
|
||||
{.name = arg.name(), .value = arg.expression()->static_type()});
|
||||
{.name = arg.name(), .value = arg.expression().static_type()});
|
||||
}
|
||||
SetStaticType(e, arena->New<TupleValue>(std::move(arg_types)));
|
||||
return TCResult(new_types);
|
||||
@@ -475,10 +475,10 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
VarValues arg_types;
|
||||
auto new_types = types;
|
||||
for (auto& arg : cast<StructLiteral>(*e).fields()) {
|
||||
auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
|
||||
auto arg_res = TypeCheckExp(&arg.expression(), new_types, values);
|
||||
new_types = arg_res.types;
|
||||
new_args.push_back(FieldInitializer(arg.name(), arg.expression()));
|
||||
arg_types.push_back({arg.name(), arg.expression()->static_type()});
|
||||
new_args.push_back(FieldInitializer(arg.name(), &arg.expression()));
|
||||
arg_types.push_back({arg.name(), arg.expression().static_type()});
|
||||
}
|
||||
SetStaticType(e, arena->New<StructType>(std::move(arg_types)));
|
||||
return TCResult(new_types);
|
||||
@@ -488,11 +488,11 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
std::vector<FieldInitializer> new_args;
|
||||
auto new_types = types;
|
||||
for (auto& arg : struct_type.fields()) {
|
||||
auto arg_res = TypeCheckExp(arg.expression(), new_types, values);
|
||||
auto arg_res = TypeCheckExp(&arg.expression(), new_types, values);
|
||||
new_types = arg_res.types;
|
||||
ExpectIsConcreteType(arg.expression()->source_loc(),
|
||||
interpreter.InterpExp(values, arg.expression()));
|
||||
new_args.push_back(FieldInitializer(arg.name(), arg.expression()));
|
||||
ExpectIsConcreteType(arg.expression().source_loc(),
|
||||
interpreter.InterpExp(values, &arg.expression()));
|
||||
new_args.push_back(FieldInitializer(arg.name(), &arg.expression()));
|
||||
}
|
||||
if (struct_type.fields().empty()) {
|
||||
// `{}` is the type of `{}`, just as `()` is the type of `()`.
|
||||
@@ -507,57 +507,57 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
}
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
auto& access = cast<FieldAccessExpression>(*e);
|
||||
auto res = TypeCheckExp(access.Aggregate(), types, values);
|
||||
Nonnull<const Value*> aggregate_type = access.Aggregate()->static_type();
|
||||
auto res = TypeCheckExp(&access.aggregate(), types, values);
|
||||
Nonnull<const Value*> aggregate_type = access.aggregate().static_type();
|
||||
switch (aggregate_type->kind()) {
|
||||
case Value::Kind::StructType: {
|
||||
const auto& struct_type = cast<StructType>(*aggregate_type);
|
||||
for (const auto& [field_name, field_type] : struct_type.fields()) {
|
||||
if (access.Field() == field_name) {
|
||||
if (access.field() == field_name) {
|
||||
SetStaticType(&access, field_type);
|
||||
return TCResult(res.types);
|
||||
}
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(access.source_loc())
|
||||
<< "struct " << struct_type << " does not have a field named "
|
||||
<< access.Field();
|
||||
<< access.field();
|
||||
}
|
||||
case Value::Kind::NominalClassType: {
|
||||
const auto& t_class = cast<NominalClassType>(*aggregate_type);
|
||||
// Search for a field
|
||||
for (auto& field : t_class.Fields()) {
|
||||
if (access.Field() == field.first) {
|
||||
if (access.field() == field.first) {
|
||||
SetStaticType(&access, field.second);
|
||||
return TCResult(res.types);
|
||||
}
|
||||
}
|
||||
// Search for a method
|
||||
for (auto& method : t_class.Methods()) {
|
||||
if (access.Field() == method.first) {
|
||||
if (access.field() == method.first) {
|
||||
SetStaticType(&access, method.second);
|
||||
return TCResult(res.types);
|
||||
}
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(e->source_loc())
|
||||
<< "class " << t_class.Name() << " does not have a field named "
|
||||
<< access.Field();
|
||||
<< access.field();
|
||||
}
|
||||
case Value::Kind::TupleValue: {
|
||||
const auto& tup = cast<TupleValue>(*aggregate_type);
|
||||
for (const TupleElement& field : tup.Elements()) {
|
||||
if (access.Field() == field.name) {
|
||||
if (access.field() == field.name) {
|
||||
SetStaticType(&access, field.value);
|
||||
return TCResult(res.types);
|
||||
}
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(e->source_loc())
|
||||
<< "tuple " << tup << " does not have a field named "
|
||||
<< access.Field();
|
||||
<< access.field();
|
||||
}
|
||||
case Value::Kind::ChoiceType: {
|
||||
const auto& choice = cast<ChoiceType>(*aggregate_type);
|
||||
for (const auto& vt : choice.Alternatives()) {
|
||||
if (access.Field() == vt.first) {
|
||||
if (access.field() == vt.first) {
|
||||
SetStaticType(&access, arena->New<FunctionType>(
|
||||
std::vector<GenericBinding>(),
|
||||
vt.second, aggregate_type));
|
||||
@@ -566,7 +566,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(e->source_loc())
|
||||
<< "choice " << choice.Name() << " does not have a field named "
|
||||
<< access.Field();
|
||||
<< access.field();
|
||||
}
|
||||
default:
|
||||
FATAL_COMPILATION_ERROR(e->source_loc())
|
||||
@@ -576,13 +576,13 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
}
|
||||
case Expression::Kind::IdentifierExpression: {
|
||||
auto& ident = cast<IdentifierExpression>(*e);
|
||||
std::optional<Nonnull<const Value*>> type = types.Get(ident.Name());
|
||||
std::optional<Nonnull<const Value*>> type = types.Get(ident.name());
|
||||
if (type) {
|
||||
SetStaticType(&ident, *type);
|
||||
return TCResult(types);
|
||||
} else {
|
||||
FATAL_COMPILATION_ERROR(e->source_loc())
|
||||
<< "could not find `" << ident.Name() << "`";
|
||||
<< "could not find `" << ident.name() << "`";
|
||||
}
|
||||
}
|
||||
case Expression::Kind::IntLiteral:
|
||||
@@ -596,13 +596,13 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
std::vector<Nonnull<Expression*>> es;
|
||||
std::vector<Nonnull<const Value*>> ts;
|
||||
auto new_types = types;
|
||||
for (Nonnull<Expression*> argument : op.Arguments()) {
|
||||
for (Nonnull<Expression*> argument : op.arguments()) {
|
||||
auto res = TypeCheckExp(argument, types, values);
|
||||
new_types = res.types;
|
||||
es.push_back(argument);
|
||||
ts.push_back(argument->static_type());
|
||||
}
|
||||
switch (op.Op()) {
|
||||
switch (op.op()) {
|
||||
case Operator::Neg:
|
||||
ExpectExactType(e->source_loc(), "negation", arena->New<IntType>(),
|
||||
ts[0]);
|
||||
@@ -664,18 +664,18 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
}
|
||||
case Expression::Kind::CallExpression: {
|
||||
auto& call = cast<CallExpression>(*e);
|
||||
auto fun_res = TypeCheckExp(call.Function(), types, values);
|
||||
switch (call.Function()->static_type()->kind()) {
|
||||
auto fun_res = TypeCheckExp(&call.function(), types, values);
|
||||
switch (call.function().static_type()->kind()) {
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fun_t =
|
||||
cast<FunctionType>(*call.Function()->static_type());
|
||||
auto arg_res = TypeCheckExp(call.Argument(), fun_res.types, values);
|
||||
cast<FunctionType>(*call.function().static_type());
|
||||
auto arg_res = TypeCheckExp(&call.argument(), fun_res.types, values);
|
||||
auto parameter_type = fun_t.Param();
|
||||
auto return_type = fun_t.Ret();
|
||||
if (!fun_t.Deduced().empty()) {
|
||||
auto deduced_args = ArgumentDeduction(
|
||||
e->source_loc(), TypeEnv(arena), parameter_type,
|
||||
call.Argument()->static_type());
|
||||
call.argument().static_type());
|
||||
for (auto& deduced_param : fun_t.Deduced()) {
|
||||
// TODO: change the following to a CHECK once the real checking
|
||||
// has been added to the type checking of function signatures.
|
||||
@@ -689,7 +689,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
return_type = Substitute(deduced_args, return_type);
|
||||
} else {
|
||||
ExpectType(e->source_loc(), "call", parameter_type,
|
||||
call.Argument()->static_type());
|
||||
call.argument().static_type());
|
||||
}
|
||||
SetStaticType(&call, return_type);
|
||||
return TCResult(arg_res.types);
|
||||
@@ -704,10 +704,10 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
}
|
||||
case Expression::Kind::FunctionTypeLiteral: {
|
||||
auto& fn = cast<FunctionTypeLiteral>(*e);
|
||||
ExpectIsConcreteType(fn.Parameter()->source_loc(),
|
||||
interpreter.InterpExp(values, fn.Parameter()));
|
||||
ExpectIsConcreteType(fn.ReturnType()->source_loc(),
|
||||
interpreter.InterpExp(values, fn.ReturnType()));
|
||||
ExpectIsConcreteType(fn.parameter().source_loc(),
|
||||
interpreter.InterpExp(values, &fn.parameter()));
|
||||
ExpectIsConcreteType(fn.return_type().source_loc(),
|
||||
interpreter.InterpExp(values, &fn.return_type()));
|
||||
SetStaticType(&fn, arena->New<TypeType>());
|
||||
return TCResult(types);
|
||||
}
|
||||
@@ -715,8 +715,8 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
SetStaticType(e, arena->New<StringType>());
|
||||
return TCResult(types);
|
||||
case Expression::Kind::IntrinsicExpression:
|
||||
switch (cast<IntrinsicExpression>(*e).Intrinsic()) {
|
||||
case IntrinsicExpression::IntrinsicKind::Print:
|
||||
switch (cast<IntrinsicExpression>(*e).intrinsic()) {
|
||||
case IntrinsicExpression::Intrinsic::Print:
|
||||
SetStaticType(e, TupleValue::Empty());
|
||||
return TCResult(types);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user