diff --git a/executable_semantics/ast/expression.cpp b/executable_semantics/ast/expression.cpp index 88e0c81daa9d..784db8c36540 100644 --- a/executable_semantics/ast/expression.cpp +++ b/executable_semantics/ast/expression.cpp @@ -15,6 +15,7 @@ namespace Carbon { using llvm::cast; +using llvm::isa; auto ExpressionFromParenContents( Nonnull 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(*this); - out << *index.Aggregate() << "[" << *index.Offset() << "]"; + out << index.aggregate() << "[" << index.offset() << "]"; break; } case Expression::Kind::FieldAccessExpression: { const auto& access = cast(*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(*this).Val(); + out << cast(*this).value(); break; case Expression::Kind::BoolLiteral: - out << (cast(*this).Val() ? "true" : "false"); + out << (cast(*this).value() ? "true" : "false"); break; case Expression::Kind::PrimitiveOperatorExpression: { out << "("; PrimitiveOperatorExpression op = cast(*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(*this).Name(); + out << cast(*this).name(); break; case Expression::Kind::CallExpression: { const auto& call = cast(*this); - out << *call.Function(); - if (call.Argument()->kind() == Expression::Kind::TupleLiteral) { - out << *call.Argument(); + out << call.function(); + if (isa(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(*this).Val()); + out.write_escaped(cast(*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(*this); - out << "fn " << *fn.Parameter() << " -> " << *fn.ReturnType(); + out << "fn " << fn.parameter() << " -> " << fn.return_type(); break; } case Expression::Kind::IntrinsicExpression: out << "intrinsic_expression("; - switch (cast(*this).Intrinsic()) { - case IntrinsicExpression::IntrinsicKind::Print: + switch (cast(*this).intrinsic()) { + case IntrinsicExpression::Intrinsic::Print: out << "print"; } out << ")"; diff --git a/executable_semantics/ast/expression.h b/executable_semantics/ast/expression.h index 422b5bb44e2c..c5ae774c9521 100644 --- a/executable_semantics/ast/expression.h +++ b/executable_semantics/ast/expression.h @@ -101,8 +101,8 @@ class FieldInitializer { auto name() const -> const std::string& { return name_; } - auto expression() const -> Nonnull { return expression_; } - auto expression() -> Nonnull { 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 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 { return aggregate; } - auto Aggregate() -> Nonnull { 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 aggregate; - std::string field; + Nonnull aggregate_; + std::string field_; }; class IndexExpression : public Expression { @@ -169,66 +169,66 @@ class IndexExpression : public Expression { Nonnull aggregate, Nonnull 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 { return aggregate; } - auto Aggregate() -> Nonnull { return aggregate; } - auto Offset() const -> Nonnull { return offset; } - auto Offset() -> Nonnull { 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 aggregate; - Nonnull offset; + Nonnull aggregate_; + Nonnull 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> 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> { - return arguments; + auto op() const -> Operator { return op_; } + auto arguments() const -> llvm::ArrayRef> { + return arguments_; } - auto Arguments() -> llvm::MutableArrayRef> { - return arguments; + auto arguments() -> llvm::MutableArrayRef> { + return arguments_; } private: - Operator op; - std::vector> arguments; + Operator op_; + std::vector> arguments_; }; class CallExpression : public Expression { @@ -343,21 +343,21 @@ class CallExpression : public Expression { Nonnull function, Nonnull 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 { return function; } - auto Function() -> Nonnull { return function; } - auto Argument() const -> Nonnull { return argument; } - auto Argument() -> Nonnull { 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 function; - Nonnull argument; + Nonnull function_; + Nonnull argument_; }; class FunctionTypeLiteral : public Expression { @@ -367,24 +367,26 @@ class FunctionTypeLiteral : public Expression { Nonnull 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 { return parameter; } - auto Parameter() -> Nonnull { return parameter; } - auto ReturnType() const -> Nonnull { return return_type; } - auto ReturnType() -> Nonnull { 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 parameter; - Nonnull return_type; - bool is_omitted_return_type; + Nonnull parameter_; + Nonnull 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("", 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 diff --git a/executable_semantics/ast/expression_test.cpp b/executable_semantics/ast/expression_test.cpp index f983efa640c7..822bebd8ebf4 100644 --- a/executable_semantics/ast/expression_test.cpp +++ b/executable_semantics/ast/expression_test.cpp @@ -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 { diff --git a/executable_semantics/ast/pattern.cpp b/executable_semantics/ast/pattern.cpp index 0cd7cc1fce25..0c3a21d7f3e6 100644 --- a/executable_semantics/ast/pattern.cpp +++ b/executable_semantics/ast/pattern.cpp @@ -90,8 +90,8 @@ AlternativePattern::AlternativePattern(SourceLocation source_loc, Nonnull alternative, Nonnull 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, diff --git a/executable_semantics/interpreter/exec_program.cpp b/executable_semantics/interpreter/exec_program.cpp index 76d87bbae587..a1ef015152df 100644 --- a/executable_semantics/interpreter/exec_program.cpp +++ b/executable_semantics/interpreter/exec_program.cpp @@ -23,11 +23,10 @@ static void AddIntrinsics(Nonnull arena, source_loc, "format_str", arena->New( arena->New(source_loc))))}; - auto print_return = - arena->New(source_loc, - arena->New( - IntrinsicExpression::IntrinsicKind::Print), - false); + auto print_return = arena->New( + source_loc, + arena->New(IntrinsicExpression::Intrinsic::Print), + false); auto print = arena->New(arena->New( source_loc, "Print", std::vector(), arena->New(source_loc, print_fields), diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 138ca5dabf7b..c01338121137 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -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(*exp).Name()); + cast(*exp).name()); Nonnull v = arena->New(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( - cast(*exp).Aggregate())}; + &cast(*exp).aggregate())}; } else { // { v :: [].f :: C, E, F} :: S, H} // -> { { &v.f :: C, E, F} :: S, H } Address aggregate = cast(*act->results()[0]).Val(); Address field = aggregate.SubobjectAddress( - cast(*exp).Field()); + cast(*exp).field()); return Done{arena->New(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(cast(*exp).Aggregate())}; + arena->New(&cast(*exp).aggregate())}; } else if (act->pos() == 1) { - return Spawn{ - arena->New(cast(*exp).Offset())}; + return Spawn{arena->New( + &cast(*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 elt = - cast(*exp).fields()[act->pos()].expression(); - return Spawn{arena->New(elt)}; + return Spawn{arena->New( + &cast(*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( - cast(*exp).Aggregate())}; + &cast(*exp).aggregate())}; } else if (act->pos() == 1) { - return Spawn{ - arena->New(cast(*exp).Offset())}; + return Spawn{arena->New( + &cast(*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 elt = - cast(*exp).fields()[act->pos()].expression(); - return Spawn{arena->New(elt)}; + return Spawn{arena->New( + &cast(*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(*exp); if (act->pos() < static_cast(literal.fields().size())) { - Nonnull elt = - literal.fields()[act->pos()].expression(); - return Spawn{arena->New(elt)}; + return Spawn{arena->New( + &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(*exp); if (act->pos() < static_cast(struct_type.fields().size())) { return Spawn{arena->New( - 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(access.Aggregate())}; + return Spawn{arena->New(&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(*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(cast(*exp).Val())}; + return Done{arena->New(cast(*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(cast(*exp).Val())}; + return Done{arena->New(cast(*exp).value())}; case Expression::Kind::PrimitiveOperatorExpression: { const auto& op = cast(*exp); - if (act->pos() != static_cast(op.Arguments().size())) { + if (act->pos() != static_cast(op.arguments().size())) { // { {v :: op(vs,[],e,es) :: C, E, F} :: S, H} // -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H} - Nonnull arg = op.Arguments()[act->pos()]; + Nonnull arg = op.arguments()[act->pos()]; return Spawn{arena->New(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( - cast(*exp).Function())}; + &cast(*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( - cast(*exp).Argument())}; + &cast(*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(*exp).Intrinsic()) { - case IntrinsicExpression::IntrinsicKind::Print: + switch (cast(*exp).intrinsic()) { + case IntrinsicExpression::Intrinsic::Print: Address pointer = GetFromEnv(exp->source_loc(), "format_str"); Nonnull 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( - cast(*exp).Parameter())}; + &cast(*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( - cast(*exp).ReturnType())}; + &cast(*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(cast(*exp).Val())}; + return Done{arena->New(cast(*exp).value())}; case Expression::Kind::StringTypeLiteral: { CHECK(act->pos() == 0); return Done{arena->New()}; @@ -719,8 +716,8 @@ auto Interpreter::StepPattern() -> Transition { // H} // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S, // H} - Nonnull elt = tuple.Fields()[act->pos()].pattern; - return Spawn{arena->New(elt)}; + return Spawn{ + arena->New(tuple.Fields()[act->pos()].pattern)}; } else { std::vector elements; for (size_t i = 0; i < tuple.Fields().size(); ++i) { diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index e1d09b109722..5a900e3087b5 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -435,12 +435,12 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, switch (e->kind()) { case Expression::Kind::IndexExpression: { auto& index = cast(*e); - auto res = TypeCheckExp(index.Aggregate(), types, values); - Nonnull aggregate_type = index.Aggregate()->static_type(); + auto res = TypeCheckExp(&index.aggregate(), types, values); + Nonnull aggregate_type = index.aggregate().static_type(); switch (aggregate_type->kind()) { case Value::Kind::TupleValue: { auto i = - cast(*interpreter.InterpExp(values, index.Offset())) + cast(*interpreter.InterpExp(values, &index.offset())) .Val(); std::string f = std::to_string(i); std::optional> field_t = @@ -461,11 +461,11 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, std::vector arg_types; auto new_types = types; for (auto& arg : cast(*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(std::move(arg_types))); return TCResult(new_types); @@ -475,10 +475,10 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, VarValues arg_types; auto new_types = types; for (auto& arg : cast(*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(std::move(arg_types))); return TCResult(new_types); @@ -488,11 +488,11 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, std::vector 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 e, TypeEnv types, } case Expression::Kind::FieldAccessExpression: { auto& access = cast(*e); - auto res = TypeCheckExp(access.Aggregate(), types, values); - Nonnull aggregate_type = access.Aggregate()->static_type(); + auto res = TypeCheckExp(&access.aggregate(), types, values); + Nonnull aggregate_type = access.aggregate().static_type(); switch (aggregate_type->kind()) { case Value::Kind::StructType: { const auto& struct_type = cast(*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(*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(*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(*aggregate_type); for (const auto& vt : choice.Alternatives()) { - if (access.Field() == vt.first) { + if (access.field() == vt.first) { SetStaticType(&access, arena->New( std::vector(), vt.second, aggregate_type)); @@ -566,7 +566,7 @@ auto TypeChecker::TypeCheckExp(Nonnull 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 e, TypeEnv types, } case Expression::Kind::IdentifierExpression: { auto& ident = cast(*e); - std::optional> type = types.Get(ident.Name()); + std::optional> 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 e, TypeEnv types, std::vector> es; std::vector> ts; auto new_types = types; - for (Nonnull argument : op.Arguments()) { + for (Nonnull 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(), ts[0]); @@ -664,18 +664,18 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, } case Expression::Kind::CallExpression: { auto& call = cast(*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(*call.Function()->static_type()); - auto arg_res = TypeCheckExp(call.Argument(), fun_res.types, values); + cast(*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 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 e, TypeEnv types, } case Expression::Kind::FunctionTypeLiteral: { auto& fn = cast(*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()); return TCResult(types); } @@ -715,8 +715,8 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, SetStaticType(e, arena->New()); return TCResult(types); case Expression::Kind::IntrinsicExpression: - switch (cast(*e).Intrinsic()) { - case IntrinsicExpression::IntrinsicKind::Print: + switch (cast(*e).intrinsic()) { + case IntrinsicExpression::Intrinsic::Print: SetStaticType(e, TupleValue::Empty()); return TCResult(types); }