diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index 5de3d9ee9c61..39eb8d17bc44 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -41,6 +41,7 @@ class Declaration { Declaration& operator=(const Member&) = delete; void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } // Returns the enumerator corresponding to the most-derived type of this // object. @@ -50,7 +51,7 @@ class Declaration { protected: // Constructs a Declaration representing syntax at the given line number. - // `tag` must be the enumerator corresponding to the most-derived type being + // `kind` must be the enumerator corresponding to the most-derived type being // constructed. Declaration(Kind kind, SourceLocation source_loc) : kind_(kind), source_loc_(source_loc) {} diff --git a/executable_semantics/ast/expression.cpp b/executable_semantics/ast/expression.cpp index f79aa261246b..de023d46e1d1 100644 --- a/executable_semantics/ast/expression.cpp +++ b/executable_semantics/ast/expression.cpp @@ -17,21 +17,21 @@ namespace Carbon { using llvm::cast; auto ExpressionFromParenContents( - Nonnull arena, SourceLocation loc, + Nonnull arena, SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull { std::optional> single_term = paren_contents.SingleTerm(); if (single_term.has_value()) { return *single_term; } else { - return TupleExpressionFromParenContents(arena, loc, paren_contents); + return TupleExpressionFromParenContents(arena, source_loc, paren_contents); } } auto TupleExpressionFromParenContents( - Nonnull arena, SourceLocation loc, + Nonnull arena, SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull { return arena->New( - loc, paren_contents.TupleElements(loc)); + source_loc, paren_contents.TupleElements(source_loc)); } static void PrintOp(llvm::raw_ostream& out, Operator op) { @@ -73,7 +73,7 @@ static void PrintFields(llvm::raw_ostream& out, } void Expression::Print(llvm::raw_ostream& out) const { - switch (Tag()) { + switch (kind()) { case Expression::Kind::IndexExpression: { const auto& index = cast(*this); out << *index.Aggregate() << "[" << *index.Offset() << "]"; @@ -127,7 +127,7 @@ void Expression::Print(llvm::raw_ostream& out) const { case Expression::Kind::CallExpression: { const auto& call = cast(*this); out << *call.Function(); - if (call.Argument()->Tag() == Expression::Kind::TupleLiteral) { + if (call.Argument()->kind() == Expression::Kind::TupleLiteral) { out << *call.Argument(); } else { out << "(" << *call.Argument() << ")"; diff --git a/executable_semantics/ast/expression.h b/executable_semantics/ast/expression.h index 6e48b9bce082..e3b503e83166 100644 --- a/executable_semantics/ast/expression.h +++ b/executable_semantics/ast/expression.h @@ -42,37 +42,38 @@ class Expression { IntrinsicExpression, }; - // Returns the enumerator corresponding to the most-derived type of this - // object. - auto Tag() const -> Kind { return kind; } - - auto SourceLoc() const -> SourceLocation { return loc; } - void Print(llvm::raw_ostream& out) const; LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + // Returns the enumerator corresponding to the most-derived type of this + // object. + auto kind() const -> Kind { return kind_; } + + auto source_loc() const -> SourceLocation { return source_loc_; } + protected: // Constructs an Expression representing syntax at the given line number. - // `tag` must be the enumerator corresponding to the most-derived type being + // `kind` must be the enumerator corresponding to the most-derived type being // constructed. - Expression(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {} + Expression(Kind kind, SourceLocation source_loc) + : kind_(kind), source_loc_(source_loc) {} private: - const Kind kind; - SourceLocation loc; + const Kind kind_; + SourceLocation source_loc_; }; // Converts paren_contents to an Expression, interpreting the parentheses as // grouping if their contents permit that interpretation, or as forming a // tuple otherwise. auto ExpressionFromParenContents( - Nonnull arena, SourceLocation loc, + Nonnull arena, SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull; // Converts paren_contents to an Expression, interpreting the parentheses as // forming a tuple. auto TupleExpressionFromParenContents( - Nonnull arena, SourceLocation loc, + Nonnull arena, SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull; // A FieldInitializer represents the initialization of a single tuple field. @@ -102,11 +103,12 @@ enum class Operator { class IdentifierExpression : public Expression { public: - explicit IdentifierExpression(SourceLocation loc, std::string name) - : Expression(Kind::IdentifierExpression, loc), name(std::move(name)) {} + explicit IdentifierExpression(SourceLocation source_loc, std::string name) + : Expression(Kind::IdentifierExpression, source_loc), + name(std::move(name)) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::IdentifierExpression; + return exp->kind() == Kind::IdentifierExpression; } auto Name() const -> const std::string& { return name; } @@ -117,15 +119,15 @@ class IdentifierExpression : public Expression { class FieldAccessExpression : public Expression { public: - explicit FieldAccessExpression(SourceLocation loc, + explicit FieldAccessExpression(SourceLocation source_loc, Nonnull aggregate, std::string field) - : Expression(Kind::FieldAccessExpression, loc), + : Expression(Kind::FieldAccessExpression, source_loc), aggregate(aggregate), field(std::move(field)) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::FieldAccessExpression; + return exp->kind() == Kind::FieldAccessExpression; } auto Aggregate() const -> Nonnull { return aggregate; } @@ -139,14 +141,15 @@ class FieldAccessExpression : public Expression { class IndexExpression : public Expression { public: - explicit IndexExpression(SourceLocation loc, Nonnull aggregate, + explicit IndexExpression(SourceLocation source_loc, + Nonnull aggregate, Nonnull offset) - : Expression(Kind::IndexExpression, loc), + : Expression(Kind::IndexExpression, source_loc), aggregate(aggregate), offset(offset) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::IndexExpression; + return exp->kind() == Kind::IndexExpression; } auto Aggregate() const -> Nonnull { return aggregate; } @@ -161,11 +164,11 @@ class IndexExpression : public Expression { class IntLiteral : public Expression { public: - explicit IntLiteral(SourceLocation loc, int val) - : Expression(Kind::IntLiteral, loc), val(val) {} + explicit IntLiteral(SourceLocation source_loc, int val) + : Expression(Kind::IntLiteral, source_loc), val(val) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::IntLiteral; + return exp->kind() == Kind::IntLiteral; } auto Val() const -> int { return val; } @@ -176,11 +179,11 @@ class IntLiteral : public Expression { class BoolLiteral : public Expression { public: - explicit BoolLiteral(SourceLocation loc, bool val) - : Expression(Kind::BoolLiteral, loc), val(val) {} + explicit BoolLiteral(SourceLocation source_loc, bool val) + : Expression(Kind::BoolLiteral, source_loc), val(val) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::BoolLiteral; + return exp->kind() == Kind::BoolLiteral; } auto Val() const -> bool { return val; } @@ -191,11 +194,11 @@ class BoolLiteral : public Expression { class StringLiteral : public Expression { public: - explicit StringLiteral(SourceLocation loc, std::string val) - : Expression(Kind::StringLiteral, loc), val(std::move(val)) {} + explicit StringLiteral(SourceLocation source_loc, std::string val) + : Expression(Kind::StringLiteral, source_loc), val(std::move(val)) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::StringLiteral; + return exp->kind() == Kind::StringLiteral; } auto Val() const -> const std::string& { return val; } @@ -206,24 +209,25 @@ class StringLiteral : public Expression { class StringTypeLiteral : public Expression { public: - explicit StringTypeLiteral(SourceLocation loc) - : Expression(Kind::StringTypeLiteral, loc) {} + explicit StringTypeLiteral(SourceLocation source_loc) + : Expression(Kind::StringTypeLiteral, source_loc) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::StringTypeLiteral; + return exp->kind() == Kind::StringTypeLiteral; } }; class TupleLiteral : public Expression { public: - explicit TupleLiteral(SourceLocation loc) : TupleLiteral(loc, {}) {} + explicit TupleLiteral(SourceLocation source_loc) + : TupleLiteral(source_loc, {}) {} - explicit TupleLiteral(SourceLocation loc, + explicit TupleLiteral(SourceLocation source_loc, std::vector fields) - : Expression(Kind::TupleLiteral, loc), fields(std::move(fields)) {} + : Expression(Kind::TupleLiteral, source_loc), fields(std::move(fields)) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::TupleLiteral; + return exp->kind() == Kind::TupleLiteral; } auto Fields() const -> const std::vector& { return fields; } @@ -248,7 +252,7 @@ class StructLiteral : public Expression { } static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::StructLiteral; + return exp->kind() == Kind::StructLiteral; } auto fields() const -> const std::vector& { @@ -272,7 +276,7 @@ class StructTypeLiteral : public Expression { : Expression(Kind::StructTypeLiteral, loc), fields_(std::move(fields)) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::StructTypeLiteral; + return exp->kind() == Kind::StructTypeLiteral; } auto fields() const -> const std::vector& { @@ -286,14 +290,14 @@ class StructTypeLiteral : public Expression { class PrimitiveOperatorExpression : public Expression { public: explicit PrimitiveOperatorExpression( - SourceLocation loc, Operator op, + SourceLocation source_loc, Operator op, std::vector> arguments) - : Expression(Kind::PrimitiveOperatorExpression, loc), + : Expression(Kind::PrimitiveOperatorExpression, source_loc), op(op), arguments(std::move(arguments)) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::PrimitiveOperatorExpression; + return exp->kind() == Kind::PrimitiveOperatorExpression; } auto Op() const -> Operator { return op; } @@ -311,14 +315,15 @@ class PrimitiveOperatorExpression : public Expression { class CallExpression : public Expression { public: - explicit CallExpression(SourceLocation loc, Nonnull function, + explicit CallExpression(SourceLocation source_loc, + Nonnull function, Nonnull argument) - : Expression(Kind::CallExpression, loc), + : Expression(Kind::CallExpression, source_loc), function(function), argument(argument) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::CallExpression; + return exp->kind() == Kind::CallExpression; } auto Function() const -> Nonnull { return function; } @@ -333,17 +338,17 @@ class CallExpression : public Expression { class FunctionTypeLiteral : public Expression { public: - explicit FunctionTypeLiteral(SourceLocation loc, + explicit FunctionTypeLiteral(SourceLocation source_loc, Nonnull parameter, Nonnull return_type, bool is_omitted_return_type) - : Expression(Kind::FunctionTypeLiteral, loc), + : Expression(Kind::FunctionTypeLiteral, source_loc), parameter(parameter), return_type(return_type), is_omitted_return_type(is_omitted_return_type) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::FunctionTypeLiteral; + return exp->kind() == Kind::FunctionTypeLiteral; } auto Parameter() const -> Nonnull { return parameter; } @@ -360,41 +365,41 @@ class FunctionTypeLiteral : public Expression { class BoolTypeLiteral : public Expression { public: - explicit BoolTypeLiteral(SourceLocation loc) - : Expression(Kind::BoolTypeLiteral, loc) {} + explicit BoolTypeLiteral(SourceLocation source_loc) + : Expression(Kind::BoolTypeLiteral, source_loc) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::BoolTypeLiteral; + return exp->kind() == Kind::BoolTypeLiteral; } }; class IntTypeLiteral : public Expression { public: - explicit IntTypeLiteral(SourceLocation loc) - : Expression(Kind::IntTypeLiteral, loc) {} + explicit IntTypeLiteral(SourceLocation source_loc) + : Expression(Kind::IntTypeLiteral, source_loc) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::IntTypeLiteral; + return exp->kind() == Kind::IntTypeLiteral; } }; class ContinuationTypeLiteral : public Expression { public: - explicit ContinuationTypeLiteral(SourceLocation loc) - : Expression(Kind::ContinuationTypeLiteral, loc) {} + explicit ContinuationTypeLiteral(SourceLocation source_loc) + : Expression(Kind::ContinuationTypeLiteral, source_loc) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::ContinuationTypeLiteral; + return exp->kind() == Kind::ContinuationTypeLiteral; } }; class TypeTypeLiteral : public Expression { public: - explicit TypeTypeLiteral(SourceLocation loc) - : Expression(Kind::TypeTypeLiteral, loc) {} + explicit TypeTypeLiteral(SourceLocation source_loc) + : Expression(Kind::TypeTypeLiteral, source_loc) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::TypeTypeLiteral; + return exp->kind() == Kind::TypeTypeLiteral; } }; @@ -409,7 +414,7 @@ class IntrinsicExpression : public Expression { intrinsic(intrinsic) {} static auto classof(const Expression* exp) -> bool { - return exp->Tag() == Kind::IntrinsicExpression; + return exp->kind() == Kind::IntrinsicExpression; } auto Intrinsic() const -> IntrinsicKind { return intrinsic; } diff --git a/executable_semantics/ast/expression_test.cpp b/executable_semantics/ast/expression_test.cpp index 95edbaff7e49..015f963218b2 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->Tag() == Expression::Kind::IntLiteral; + arg.expression->kind() == Expression::Kind::IntLiteral; } static auto FakeSourceLoc(int line_num) -> SourceLocation { @@ -40,8 +40,8 @@ TEST_F(ExpressionTest, EmptyAsExpression) { .has_trailing_comma = false}; Nonnull expression = ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1)); - ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral); + EXPECT_EQ(expression->source_loc(), FakeSourceLoc(1)); + ASSERT_EQ(expression->kind(), Expression::Kind::TupleLiteral); EXPECT_THAT(cast(*expression).Fields(), IsEmpty()); } @@ -50,8 +50,8 @@ TEST_F(ExpressionTest, EmptyAsTuple) { .has_trailing_comma = false}; Nonnull tuple = TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); - ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); + ASSERT_EQ(tuple->kind(), Expression::Kind::TupleLiteral); EXPECT_THAT(cast(*tuple).Fields(), IsEmpty()); } @@ -69,8 +69,8 @@ TEST_F(ExpressionTest, UnaryNoCommaAsExpression) { Nonnull expression = ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(2)); - ASSERT_EQ(expression->Tag(), Expression::Kind::IntLiteral); + EXPECT_EQ(expression->source_loc(), FakeSourceLoc(2)); + ASSERT_EQ(expression->kind(), Expression::Kind::IntLiteral); } TEST_F(ExpressionTest, UnaryNoCommaAsTuple) { @@ -81,8 +81,8 @@ TEST_F(ExpressionTest, UnaryNoCommaAsTuple) { Nonnull tuple = TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); - ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); + ASSERT_EQ(tuple->kind(), Expression::Kind::TupleLiteral); EXPECT_THAT(cast(*tuple).Fields(), ElementsAre(IntFieldNamed("0"))); } @@ -95,8 +95,8 @@ TEST_F(ExpressionTest, UnaryWithCommaAsExpression) { Nonnull expression = ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1)); - ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral); + EXPECT_EQ(expression->source_loc(), FakeSourceLoc(1)); + ASSERT_EQ(expression->kind(), Expression::Kind::TupleLiteral); EXPECT_THAT(cast(*expression).Fields(), ElementsAre(IntFieldNamed("0"))); } @@ -109,8 +109,8 @@ TEST_F(ExpressionTest, UnaryWithCommaAsTuple) { Nonnull tuple = TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); - ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); + ASSERT_EQ(tuple->kind(), Expression::Kind::TupleLiteral); EXPECT_THAT(cast(*tuple).Fields(), ElementsAre(IntFieldNamed("0"))); } @@ -125,8 +125,8 @@ TEST_F(ExpressionTest, BinaryAsExpression) { Nonnull expression = ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1)); - ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral); + EXPECT_EQ(expression->source_loc(), FakeSourceLoc(1)); + ASSERT_EQ(expression->kind(), Expression::Kind::TupleLiteral); EXPECT_THAT(cast(*expression).Fields(), ElementsAre(IntFieldNamed("0"), IntFieldNamed("1"))); } @@ -141,8 +141,8 @@ TEST_F(ExpressionTest, BinaryAsTuple) { Nonnull tuple = TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); - ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); + ASSERT_EQ(tuple->kind(), Expression::Kind::TupleLiteral); EXPECT_THAT(cast(*tuple).Fields(), ElementsAre(IntFieldNamed("0"), IntFieldNamed("1"))); } diff --git a/executable_semantics/ast/member.cpp b/executable_semantics/ast/member.cpp index dd447db47a6b..4aa479596523 100644 --- a/executable_semantics/ast/member.cpp +++ b/executable_semantics/ast/member.cpp @@ -12,7 +12,7 @@ namespace Carbon { using llvm::cast; void Member::Print(llvm::raw_ostream& out) const { - switch (Tag()) { + switch (kind()) { case Kind::FieldMember: const auto& field = cast(*this); out << "var " << *field.Binding() << ";\n"; diff --git a/executable_semantics/ast/member.h b/executable_semantics/ast/member.h index c52da9404640..409bf66fa54e 100644 --- a/executable_semantics/ast/member.h +++ b/executable_semantics/ast/member.h @@ -30,32 +30,34 @@ class Member { Member(const Member&) = delete; Member& operator=(const Member&) = delete; + void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + // Returns the enumerator corresponding to the most-derived type of this // object. - auto Tag() const -> Kind { return kind; } + auto kind() const -> Kind { return kind_; } - auto SourceLoc() const -> SourceLocation { return loc; } - - void Print(llvm::raw_ostream& out) const; + auto source_loc() const -> SourceLocation { return source_loc_; } protected: // Constructs a Member representing syntax at the given line number. - // `tag` must be the enumerator corresponding to the most-derived type being + // `kind` must be the enumerator corresponding to the most-derived type being // constructed. - Member(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {} + Member(Kind kind, SourceLocation source_loc) + : kind_(kind), source_loc_(source_loc) {} private: - const Kind kind; - SourceLocation loc; + const Kind kind_; + SourceLocation source_loc_; }; class FieldMember : public Member { public: - FieldMember(SourceLocation loc, Nonnull binding) - : Member(Kind::FieldMember, loc), binding(binding) {} + FieldMember(SourceLocation source_loc, Nonnull binding) + : Member(Kind::FieldMember, source_loc), binding(binding) {} static auto classof(const Member* member) -> bool { - return member->Tag() == Kind::FieldMember; + return member->kind() == Kind::FieldMember; } auto Binding() const -> Nonnull { return binding; } diff --git a/executable_semantics/ast/paren_contents.h b/executable_semantics/ast/paren_contents.h index 2a5e64eef4a0..73070b7069cc 100644 --- a/executable_semantics/ast/paren_contents.h +++ b/executable_semantics/ast/paren_contents.h @@ -41,7 +41,8 @@ struct ParenContents { // // TODO: Find a way to deduce TupleElement from Term. template - auto TupleElements(SourceLocation loc) const -> std::vector; + auto TupleElements(SourceLocation source_loc) const + -> std::vector; std::vector elements; bool has_trailing_comma; @@ -61,7 +62,7 @@ auto ParenContents::SingleTerm() const -> std::optional> { template template -auto ParenContents::TupleElements(SourceLocation loc) const +auto ParenContents::TupleElements(SourceLocation source_loc) const -> std::vector { std::vector result; int i = 0; @@ -72,7 +73,7 @@ auto ParenContents::TupleElements(SourceLocation loc) const result.push_back(TupleElement(*element.name, element.term)); } else { if (seen_named_member) { - FATAL_PROGRAM_ERROR(loc) + FATAL_PROGRAM_ERROR(source_loc) << "positional members must come before named members"; } result.push_back(TupleElement(std::to_string(i), element.term)); diff --git a/executable_semantics/ast/pattern.cpp b/executable_semantics/ast/pattern.cpp index c30efe341fce..0cd7cc1fce25 100644 --- a/executable_semantics/ast/pattern.cpp +++ b/executable_semantics/ast/pattern.cpp @@ -18,7 +18,7 @@ namespace Carbon { using llvm::cast; void Pattern::Print(llvm::raw_ostream& out) const { - switch (Tag()) { + switch (kind()) { case Kind::AutoPattern: out << "auto"; break; @@ -54,22 +54,24 @@ void Pattern::Print(llvm::raw_ostream& out) const { } } -auto PatternFromParenContents(Nonnull arena, SourceLocation loc, +auto PatternFromParenContents(Nonnull arena, SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull { std::optional> single_term = paren_contents.SingleTerm(); if (single_term.has_value()) { return *single_term; } else { - return TuplePatternFromParenContents(arena, loc, paren_contents); + return TuplePatternFromParenContents(arena, source_loc, paren_contents); } } -auto TuplePatternFromParenContents(Nonnull arena, SourceLocation loc, +auto TuplePatternFromParenContents(Nonnull arena, + SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull { return arena->New( - loc, paren_contents.TupleElements(loc)); + source_loc, + paren_contents.TupleElements(source_loc)); } // Used by AlternativePattern for constructor initialization. Produces a helpful @@ -77,17 +79,17 @@ auto TuplePatternFromParenContents(Nonnull arena, SourceLocation loc, // apply. static auto RequireFieldAccess(Nonnull alternative) -> FieldAccessExpression& { - if (alternative->Tag() != Expression::Kind::FieldAccessExpression) { - FATAL_PROGRAM_ERROR(alternative->SourceLoc()) + if (alternative->kind() != Expression::Kind::FieldAccessExpression) { + FATAL_PROGRAM_ERROR(alternative->source_loc()) << "Alternative pattern must have the form of a field access."; } return cast(*alternative); } -AlternativePattern::AlternativePattern(SourceLocation loc, +AlternativePattern::AlternativePattern(SourceLocation source_loc, Nonnull alternative, Nonnull arguments) - : Pattern(Kind::AlternativePattern, loc), + : Pattern(Kind::AlternativePattern, source_loc), choice_type(RequireFieldAccess(alternative).Aggregate()), alternative_name(RequireFieldAccess(alternative).Field()), arguments(arguments) {} diff --git a/executable_semantics/ast/pattern.h b/executable_semantics/ast/pattern.h index 737f663e3399..8fb52c40ce46 100644 --- a/executable_semantics/ast/pattern.h +++ b/executable_semantics/ast/pattern.h @@ -37,33 +37,35 @@ class Pattern { Pattern(const Pattern&) = delete; Pattern& operator=(const Pattern&) = delete; - // Returns the enumerator corresponding to the most-derived type of this - // object. - auto Tag() const -> Kind { return kind; } - - auto SourceLoc() const -> SourceLocation { return loc; } - void Print(llvm::raw_ostream& out) const; LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + // Returns the enumerator corresponding to the most-derived type of this + // object. + auto kind() const -> Kind { return kind_; } + + auto source_loc() const -> SourceLocation { return source_loc_; } + protected: // Constructs a Pattern representing syntax at the given line number. - // `tag` must be the enumerator corresponding to the most-derived type being + // `kind` must be the enumerator corresponding to the most-derived type being // constructed. - Pattern(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {} + Pattern(Kind kind, SourceLocation source_loc) + : kind_(kind), source_loc_(source_loc) {} private: - const Kind kind; - SourceLocation loc; + const Kind kind_; + SourceLocation source_loc_; }; // A pattern consisting of the `auto` keyword. class AutoPattern : public Pattern { public: - explicit AutoPattern(SourceLocation loc) : Pattern(Kind::AutoPattern, loc) {} + explicit AutoPattern(SourceLocation source_loc) + : Pattern(Kind::AutoPattern, source_loc) {} static auto classof(const Pattern* pattern) -> bool { - return pattern->Tag() == Kind::AutoPattern; + return pattern->kind() == Kind::AutoPattern; } }; @@ -71,12 +73,14 @@ class AutoPattern : public Pattern { // a name to it. class BindingPattern : public Pattern { public: - BindingPattern(SourceLocation loc, std::optional name, + BindingPattern(SourceLocation source_loc, std::optional name, Nonnull type) - : Pattern(Kind::BindingPattern, loc), name(std::move(name)), type(type) {} + : Pattern(Kind::BindingPattern, source_loc), + name(std::move(name)), + type(type) {} static auto classof(const Pattern* pattern) -> bool { - return pattern->Tag() == Kind::BindingPattern; + return pattern->kind() == Kind::BindingPattern; } // The name this pattern binds, if any. @@ -106,11 +110,11 @@ class TuplePattern : public Pattern { Nonnull pattern; }; - TuplePattern(SourceLocation loc, std::vector fields) - : Pattern(Kind::TuplePattern, loc), fields(std::move(fields)) {} + TuplePattern(SourceLocation source_loc, std::vector fields) + : Pattern(Kind::TuplePattern, source_loc), fields(std::move(fields)) {} static auto classof(const Pattern* pattern) -> bool { - return pattern->Tag() == Kind::TuplePattern; + return pattern->kind() == Kind::TuplePattern; } auto Fields() const -> llvm::ArrayRef { return fields; } @@ -123,13 +127,14 @@ class TuplePattern : public Pattern { // Converts paren_contents to a Pattern, interpreting the parentheses as // grouping if their contents permit that interpretation, or as forming a // tuple otherwise. -auto PatternFromParenContents(Nonnull arena, SourceLocation loc, +auto PatternFromParenContents(Nonnull arena, SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull; // Converts paren_contents to a TuplePattern, interpreting the parentheses as // forming a tuple. -auto TuplePatternFromParenContents(Nonnull arena, SourceLocation loc, +auto TuplePatternFromParenContents(Nonnull arena, + SourceLocation source_loc, const ParenContents& paren_contents) -> Nonnull; @@ -145,21 +150,23 @@ class AlternativePattern : public Pattern { // Constructs an AlternativePattern that matches a value of the type // specified by choice_type if it represents an alternative named // alternative_name, and its arguments match `arguments`. - AlternativePattern(SourceLocation loc, Nonnull choice_type, + AlternativePattern(SourceLocation source_loc, + Nonnull choice_type, std::string alternative_name, Nonnull arguments) - : Pattern(Kind::AlternativePattern, loc), + : Pattern(Kind::AlternativePattern, source_loc), choice_type(choice_type), alternative_name(std::move(alternative_name)), arguments(arguments) {} // Constructs an AlternativePattern that matches the alternative specified // by `alternative`, if its arguments match `arguments`. - AlternativePattern(SourceLocation loc, Nonnull alternative, + AlternativePattern(SourceLocation source_loc, + Nonnull alternative, Nonnull arguments); static auto classof(const Pattern* pattern) -> bool { - return pattern->Tag() == Kind::AlternativePattern; + return pattern->kind() == Kind::AlternativePattern; } auto ChoiceType() const -> Nonnull { return choice_type; } @@ -181,11 +188,11 @@ class AlternativePattern : public Pattern { class ExpressionPattern : public Pattern { public: ExpressionPattern(Nonnull expression) - : Pattern(Kind::ExpressionPattern, expression->SourceLoc()), + : Pattern(Kind::ExpressionPattern, expression->source_loc()), expression(expression) {} static auto classof(const Pattern* pattern) -> bool { - return pattern->Tag() == Kind::ExpressionPattern; + return pattern->kind() == Kind::ExpressionPattern; } auto Expression() const -> Nonnull { return expression; } diff --git a/executable_semantics/ast/pattern_test.cpp b/executable_semantics/ast/pattern_test.cpp index d68ee7dc210d..edeaa7a2ca2f 100644 --- a/executable_semantics/ast/pattern_test.cpp +++ b/executable_semantics/ast/pattern_test.cpp @@ -39,7 +39,7 @@ TEST_F(PatternTest, EmptyAsPattern) { .has_trailing_comma = false}; Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1)); + EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1)); ASSERT_TRUE(isa(*pattern)); EXPECT_THAT(cast(*pattern).Fields(), IsEmpty()); } @@ -49,7 +49,7 @@ TEST_F(PatternTest, EmptyAsTuplePattern) { .has_trailing_comma = false}; Nonnull tuple = TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); EXPECT_THAT(tuple->Fields(), IsEmpty()); } @@ -67,7 +67,7 @@ TEST_F(PatternTest, UnaryNoCommaAsPattern) { Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(2)); + EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(2)); ASSERT_TRUE(isa(*pattern)); } @@ -79,7 +79,7 @@ TEST_F(PatternTest, UnaryNoCommaAsTuplePattern) { Nonnull tuple = TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0"))); } @@ -91,7 +91,7 @@ TEST_F(PatternTest, UnaryWithCommaAsPattern) { Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1)); + EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1)); ASSERT_TRUE(isa(*pattern)); EXPECT_THAT(cast(*pattern).Fields(), ElementsAre(AutoFieldNamed("0"))); @@ -105,7 +105,7 @@ TEST_F(PatternTest, UnaryWithCommaAsTuplePattern) { Nonnull tuple = TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0"))); } @@ -119,7 +119,7 @@ TEST_F(PatternTest, BinaryAsPattern) { Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1)); + EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1)); ASSERT_TRUE(isa(*pattern)); EXPECT_THAT(cast(*pattern).Fields(), ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1"))); @@ -135,7 +135,7 @@ TEST_F(PatternTest, BinaryAsTuplePattern) { Nonnull tuple = TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents); - EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1)); + EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1)); EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1"))); } diff --git a/executable_semantics/ast/statement.cpp b/executable_semantics/ast/statement.cpp index 4d13ce4d990b..e05e480d8b48 100644 --- a/executable_semantics/ast/statement.cpp +++ b/executable_semantics/ast/statement.cpp @@ -17,7 +17,7 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const { out << " ... "; return; } - switch (Tag()) { + switch (kind()) { case Kind::Match: { const auto& match = cast(*this); out << "match (" << match.expression() << ") {"; diff --git a/executable_semantics/ast/statement.h b/executable_semantics/ast/statement.h index 99feea087692..c1e0c7a19473 100644 --- a/executable_semantics/ast/statement.h +++ b/executable_semantics/ast/statement.h @@ -36,34 +36,35 @@ class Statement { Await, // Pause execution of the continuation. }; - // Returns the enumerator corresponding to the most-derived type of this - // object. - auto Tag() const -> Kind { return kind; } - - auto SourceLoc() const -> SourceLocation { return loc; } - void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); } void PrintDepth(int depth, llvm::raw_ostream& out) const; LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + // Returns the enumerator corresponding to the most-derived type of this + // object. + auto kind() const -> Kind { return kind_; } + + auto source_loc() const -> SourceLocation { return source_loc_; } + protected: // Constructs an Statement representing syntax at the given line number. - // `tag` must be the enumerator corresponding to the most-derived type being + // `kind` must be the enumerator corresponding to the most-derived type being // constructed. - Statement(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {} + Statement(Kind kind, SourceLocation source_loc) + : kind_(kind), source_loc_(source_loc) {} private: - const Kind kind; - SourceLocation loc; + const Kind kind_; + SourceLocation source_loc_; }; class ExpressionStatement : public Statement { public: - ExpressionStatement(SourceLocation loc, Nonnull exp) - : Statement(Kind::ExpressionStatement, loc), exp(exp) {} + ExpressionStatement(SourceLocation source_loc, Nonnull exp) + : Statement(Kind::ExpressionStatement, source_loc), exp(exp) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::ExpressionStatement; + return stmt->kind() == Kind::ExpressionStatement; } auto Exp() const -> Nonnull { return exp; } @@ -75,11 +76,12 @@ class ExpressionStatement : public Statement { class Assign : public Statement { public: - Assign(SourceLocation loc, Nonnull lhs, Nonnull rhs) - : Statement(Kind::Assign, loc), lhs(lhs), rhs(rhs) {} + Assign(SourceLocation source_loc, Nonnull lhs, + Nonnull rhs) + : Statement(Kind::Assign, source_loc), lhs(lhs), rhs(rhs) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Assign; + return stmt->kind() == Kind::Assign; } auto Lhs() const -> Nonnull { return lhs; } @@ -94,12 +96,12 @@ class Assign : public Statement { class VariableDefinition : public Statement { public: - VariableDefinition(SourceLocation loc, Nonnull pat, + VariableDefinition(SourceLocation source_loc, Nonnull pat, Nonnull init) - : Statement(Kind::VariableDefinition, loc), pat(pat), init(init) {} + : Statement(Kind::VariableDefinition, source_loc), pat(pat), init(init) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::VariableDefinition; + return stmt->kind() == Kind::VariableDefinition; } auto Pat() const -> Nonnull { return pat; } @@ -114,16 +116,16 @@ class VariableDefinition : public Statement { class If : public Statement { public: - If(SourceLocation loc, Nonnull cond, + If(SourceLocation source_loc, Nonnull cond, Nonnull then_stmt, std::optional> else_stmt) - : Statement(Kind::If, loc), + : Statement(Kind::If, source_loc), cond(cond), then_stmt(then_stmt), else_stmt(else_stmt) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::If; + return stmt->kind() == Kind::If; } auto Cond() const -> Nonnull { return cond; } @@ -143,15 +145,16 @@ class If : public Statement { class Return : public Statement { public: - Return(Nonnull arena, SourceLocation loc) - : Return(loc, arena->New(loc), true) {} - Return(SourceLocation loc, Nonnull exp, bool is_omitted_exp) - : Statement(Kind::Return, loc), + Return(Nonnull arena, SourceLocation source_loc) + : Return(source_loc, arena->New(source_loc), true) {} + Return(SourceLocation source_loc, Nonnull exp, + bool is_omitted_exp) + : Statement(Kind::Return, source_loc), exp(exp), is_omitted_exp(is_omitted_exp) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Return; + return stmt->kind() == Kind::Return; } auto Exp() const -> Nonnull { return exp; } @@ -165,12 +168,12 @@ class Return : public Statement { class Sequence : public Statement { public: - Sequence(SourceLocation loc, Nonnull stmt, + Sequence(SourceLocation source_loc, Nonnull stmt, std::optional> next) - : Statement(Kind::Sequence, loc), stmt(stmt), next(next) {} + : Statement(Kind::Sequence, source_loc), stmt(stmt), next(next) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Sequence; + return stmt->kind() == Kind::Sequence; } auto Stmt() const -> Nonnull { return stmt; } @@ -185,11 +188,11 @@ class Sequence : public Statement { class Block : public Statement { public: - Block(SourceLocation loc, std::optional> stmt) - : Statement(Kind::Block, loc), stmt(stmt) {} + Block(SourceLocation source_loc, std::optional> stmt) + : Statement(Kind::Block, source_loc), stmt(stmt) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Block; + return stmt->kind() == Kind::Block; } auto Stmt() const -> std::optional> { return stmt; } @@ -201,11 +204,12 @@ class Block : public Statement { class While : public Statement { public: - While(SourceLocation loc, Nonnull cond, Nonnull body) - : Statement(Kind::While, loc), cond(cond), body(body) {} + While(SourceLocation source_loc, Nonnull cond, + Nonnull body) + : Statement(Kind::While, source_loc), cond(cond), body(body) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::While; + return stmt->kind() == Kind::While; } auto Cond() const -> Nonnull { return cond; } @@ -220,19 +224,21 @@ class While : public Statement { class Break : public Statement { public: - explicit Break(SourceLocation loc) : Statement(Kind::Break, loc) {} + explicit Break(SourceLocation source_loc) + : Statement(Kind::Break, source_loc) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Break; + return stmt->kind() == Kind::Break; } }; class Continue : public Statement { public: - explicit Continue(SourceLocation loc) : Statement(Kind::Continue, loc) {} + explicit Continue(SourceLocation source_loc) + : Statement(Kind::Continue, source_loc) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Continue; + return stmt->kind() == Kind::Continue; } }; @@ -253,14 +259,14 @@ class Match : public Statement { Nonnull statement_; }; - Match(SourceLocation loc, Nonnull expression, + Match(SourceLocation source_loc, Nonnull expression, std::vector clauses) - : Statement(Kind::Match, loc), + : Statement(Kind::Match, source_loc), expression_(expression), clauses_(std::move(clauses)) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Match; + return stmt->kind() == Kind::Match; } auto expression() const -> const Expression& { return *expression_; } @@ -280,14 +286,14 @@ class Match : public Statement { // } class Continuation : public Statement { public: - Continuation(SourceLocation loc, std::string continuation_variable, + Continuation(SourceLocation source_loc, std::string continuation_variable, Nonnull body) - : Statement(Kind::Continuation, loc), + : Statement(Kind::Continuation, source_loc), continuation_variable(std::move(continuation_variable)), body(body) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Continuation; + return stmt->kind() == Kind::Continuation; } auto ContinuationVariable() const -> const std::string& { @@ -306,11 +312,11 @@ class Continuation : public Statement { // __run ; class Run : public Statement { public: - Run(SourceLocation loc, Nonnull argument) - : Statement(Kind::Run, loc), argument(argument) {} + Run(SourceLocation source_loc, Nonnull argument) + : Statement(Kind::Run, source_loc), argument(argument) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Run; + return stmt->kind() == Kind::Run; } auto Argument() const -> Nonnull { return argument; } @@ -325,10 +331,11 @@ class Run : public Statement { // __await; class Await : public Statement { public: - explicit Await(SourceLocation loc) : Statement(Kind::Await, loc) {} + explicit Await(SourceLocation source_loc) + : Statement(Kind::Await, source_loc) {} static auto classof(const Statement* stmt) -> bool { - return stmt->Tag() == Kind::Await; + return stmt->kind() == Kind::Await; } }; diff --git a/executable_semantics/interpreter/action.cpp b/executable_semantics/interpreter/action.cpp index 86ba84c00f6c..bcde260be6fe 100644 --- a/executable_semantics/interpreter/action.cpp +++ b/executable_semantics/interpreter/action.cpp @@ -22,7 +22,7 @@ namespace Carbon { using llvm::cast; void Action::Print(llvm::raw_ostream& out) const { - switch (Tag()) { + switch (kind()) { case Action::Kind::LValAction: out << *cast(*this).Exp(); break; @@ -36,11 +36,11 @@ void Action::Print(llvm::raw_ostream& out) const { cast(*this).Stmt()->PrintDepth(1, out); break; } - out << "<" << pos << ">"; - if (results.size() > 0) { + out << "<" << pos_ << ">"; + if (results_.size() > 0) { out << "("; llvm::ListSeparator sep; - for (auto& result : results) { + for (auto& result : results_) { out << sep << *result; } out << ")"; diff --git a/executable_semantics/interpreter/action.h b/executable_semantics/interpreter/action.h index 70909175c286..f5737b63f586 100644 --- a/executable_semantics/interpreter/action.h +++ b/executable_semantics/interpreter/action.h @@ -29,48 +29,48 @@ class Action { Action(const Value&) = delete; Action& operator=(const Value&) = delete; - // The position or state of the action. Starts at 0 and goes up to the number - // of subexpressions. - // - // pos indicates how many of the entries in the following `results` vector - // will be filled in the next time this action is active. - // For each i < pos, results[i] contains a pointer to a Value. - auto Pos() const -> int { return pos; } - - // Results from a subexpression. - auto Results() const -> const std::vector>& { - return results; - } - - void SetPos(int pos) { this->pos = pos; } - - void AddResult(Nonnull result) { results.push_back(result); } + void AddResult(Nonnull result) { results_.push_back(result); } void Clear() { - pos = 0; - results.clear(); + pos_ = 0; + results_.clear(); } - // Returns the enumerator corresponding to the most-derived type of this - // object. - auto Tag() const -> Kind { return kind; } - static void PrintList(const Stack>& ls, llvm::raw_ostream& out); void Print(llvm::raw_ostream& out) const; LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + // Returns the enumerator corresponding to the most-derived type of this + // object. + auto kind() const -> Kind { return kind_; } + + // The position or state of the action. Starts at 0 and goes up to the number + // of subexpressions. + // + // pos indicates how many of the entries in the following `results` vector + // will be filled in the next time this action is active. + // For each i < pos, results[i] contains a pointer to a Value. + auto pos() const -> int { return pos_; } + + void set_pos(int pos) { this->pos_ = pos; } + + // Results from a subexpression. + auto results() const -> const std::vector>& { + return results_; + } + protected: - // Constructs an Action. `tag` must be the enumerator corresponding to the + // Constructs an Action. `kind` must be the enumerator corresponding to the // most-derived type being constructed. - explicit Action(Kind kind) : kind(kind) {} + explicit Action(Kind kind) : kind_(kind) {} private: - int pos = 0; - std::vector> results; + int pos_ = 0; + std::vector> results_; - const Kind kind; + const Kind kind_; }; class LValAction : public Action { @@ -79,7 +79,7 @@ class LValAction : public Action { : Action(Kind::LValAction), exp(exp) {} static auto classof(const Action* action) -> bool { - return action->Tag() == Kind::LValAction; + return action->kind() == Kind::LValAction; } auto Exp() const -> Nonnull { return exp; } @@ -94,7 +94,7 @@ class ExpressionAction : public Action { : Action(Kind::ExpressionAction), exp(exp) {} static auto classof(const Action* action) -> bool { - return action->Tag() == Kind::ExpressionAction; + return action->kind() == Kind::ExpressionAction; } auto Exp() const -> Nonnull { return exp; } @@ -109,7 +109,7 @@ class PatternAction : public Action { : Action(Kind::PatternAction), pat(pat) {} static auto classof(const Action* action) -> bool { - return action->Tag() == Kind::PatternAction; + return action->kind() == Kind::PatternAction; } auto Pat() const -> Nonnull { return pat; } @@ -124,7 +124,7 @@ class StatementAction : public Action { : Action(Kind::StatementAction), stmt(stmt) {} static auto classof(const Action* action) -> bool { - return action->Tag() == Kind::StatementAction; + return action->kind() == Kind::StatementAction; } auto Stmt() const -> Nonnull { return stmt; } diff --git a/executable_semantics/interpreter/exec_program.cpp b/executable_semantics/interpreter/exec_program.cpp index 86d8fb9bf166..86dcde83591f 100644 --- a/executable_semantics/interpreter/exec_program.cpp +++ b/executable_semantics/interpreter/exec_program.cpp @@ -17,21 +17,21 @@ namespace Carbon { // standardized, but is made available for printing state in tests. static void AddIntrinsics(Nonnull arena, std::vector>* declarations) { - SourceLocation loc("", 0); + SourceLocation source_loc("", 0); std::vector print_fields = {TuplePattern::Field( - "0", - arena->New( - loc, "format_str", - arena->New(arena->New(loc))))}; + "0", arena->New( + source_loc, "format_str", + arena->New( + arena->New(source_loc))))}; auto print_return = - arena->New(loc, + arena->New(source_loc, arena->New( IntrinsicExpression::IntrinsicKind::Print), false); auto print = arena->New(arena->New( - loc, "Print", std::vector(), - arena->New(loc, print_fields), - arena->New(arena->New(loc)), + source_loc, "Print", std::vector(), + arena->New(source_loc, print_fields), + arena->New(arena->New(source_loc)), /*is_omitted_return_type=*/false, print_return)); declarations->insert(declarations->begin(), print); } @@ -62,10 +62,10 @@ void ExecProgram(Nonnull arena, AST ast) { llvm::outs() << "********** starting execution **********\n"; } - SourceLocation loc("", 0); + SourceLocation source_loc("", 0); Nonnull call_main = arena->New( - loc, arena->New(loc, "main"), - arena->New(loc)); + source_loc, arena->New(source_loc, "main"), + arena->New(source_loc)); int result = Interpreter(arena).InterpProgram(new_decls, call_main); llvm::outs() << "result: " << result << "\n"; } diff --git a/executable_semantics/interpreter/heap.cpp b/executable_semantics/interpreter/heap.cpp index d400d386e974..7473f86e9673 100644 --- a/executable_semantics/interpreter/heap.cpp +++ b/executable_semantics/interpreter/heap.cpp @@ -20,21 +20,24 @@ auto Heap::AllocateValue(Nonnull v) -> Address { return a; } -auto Heap::Read(const Address& a, SourceLocation loc) -> Nonnull { - this->CheckAlive(a, loc); - return values[a.index]->GetField(arena, a.field_path, loc); +auto Heap::Read(const Address& a, SourceLocation source_loc) + -> Nonnull { + this->CheckAlive(a, source_loc); + return values[a.index]->GetField(arena, a.field_path, source_loc); } void Heap::Write(const Address& a, Nonnull v, - SourceLocation loc) { - this->CheckAlive(a, loc); - values[a.index] = values[a.index]->SetField(arena, a.field_path, v, loc); + SourceLocation source_loc) { + this->CheckAlive(a, source_loc); + values[a.index] = + values[a.index]->SetField(arena, a.field_path, v, source_loc); } -void Heap::CheckAlive(const Address& address, SourceLocation loc) { +void Heap::CheckAlive(const Address& address, SourceLocation source_loc) { if (!alive[address.index]) { - FATAL_RUNTIME_ERROR(loc) << "undefined behavior: access to dead value " - << *values[address.index]; + FATAL_RUNTIME_ERROR(source_loc) + << "undefined behavior: access to dead value " + << *values[address.index]; } } diff --git a/executable_semantics/interpreter/heap.h b/executable_semantics/interpreter/heap.h index e5bb27a088e4..52af9f90701a 100644 --- a/executable_semantics/interpreter/heap.h +++ b/executable_semantics/interpreter/heap.h @@ -25,11 +25,13 @@ class Heap { // Returns the value at the given address in the heap after // checking that it is alive. - auto Read(const Address& a, SourceLocation loc) -> Nonnull; + auto Read(const Address& a, SourceLocation source_loc) + -> Nonnull; // Writes the given value at the address in the heap after // checking that the address is alive. - void Write(const Address& a, Nonnull v, SourceLocation loc); + void Write(const Address& a, Nonnull v, + SourceLocation source_loc); // Put the given value on the heap and mark it as alive. auto AllocateValue(Nonnull v) -> Address; @@ -47,7 +49,7 @@ class Heap { private: // Signal an error if the address is no longer alive. - void CheckAlive(const Address& address, SourceLocation loc); + void CheckAlive(const Address& address, SourceLocation source_loc); Nonnull arena; std::vector> values; diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 40279d87bc93..2caebf5296e7 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -51,11 +51,11 @@ auto Interpreter::CurrentEnv() -> Env { } // Returns the given name from the environment, printing an error if not found. -auto Interpreter::GetFromEnv(SourceLocation loc, const std::string& name) +auto Interpreter::GetFromEnv(SourceLocation source_loc, const std::string& name) -> Address { std::optional
pointer = CurrentEnv().Get(name); if (!pointer) { - FATAL_RUNTIME_ERROR(loc) << "could not find `" << name << "`"; + FATAL_RUNTIME_ERROR(source_loc) << "could not find `" << name << "`"; } return *pointer; } @@ -76,7 +76,7 @@ void Interpreter::PrintState(llvm::raw_ostream& out) { auto Interpreter::EvalPrim(Operator op, const std::vector>& args, - SourceLocation loc) -> Nonnull { + SourceLocation source_loc) -> Nonnull { switch (op) { case Operator::Neg: return arena->New(-cast(*args[0]).Val()); @@ -98,7 +98,7 @@ auto Interpreter::EvalPrim(Operator op, return arena->New(cast(*args[0]).Val() || cast(*args[1]).Val()); case Operator::Eq: - return arena->New(ValueEqual(args[0], args[1], loc)); + return arena->New(ValueEqual(args[0], args[1], source_loc)); case Operator::Ptr: return arena->New(args[0]); case Operator::Deref: @@ -129,7 +129,7 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) { VarValues fields; VarValues methods; for (Nonnull m : class_def.members()) { - switch (m->Tag()) { + switch (m->kind()) { case Member::Kind::FieldMember: { Nonnull binding = cast(*m).Binding(); @@ -201,11 +201,11 @@ auto Interpreter::CreateTuple(Nonnull act, // { { (v1,...,vn) :: C, E, F} :: S, H} // -> { { `(v1,...,vn) :: C, E, F} :: S, H} const auto& tup_lit = cast(*exp); - CHECK(act->Results().size() == tup_lit.Fields().size()); + CHECK(act->results().size() == tup_lit.Fields().size()); std::vector elements; - for (size_t i = 0; i < act->Results().size(); ++i) { + for (size_t i = 0; i < act->results().size(); ++i) { elements.push_back( - {.name = tup_lit.Fields()[i].name, .value = act->Results()[i]}); + {.name = tup_lit.Fields()[i].name, .value = act->results()[i]}); } return arena->New(std::move(elements)); @@ -224,37 +224,39 @@ auto Interpreter::CreateStruct(const std::vector& fields, } auto Interpreter::PatternMatch(Nonnull p, Nonnull v, - SourceLocation loc) -> std::optional { - switch (p->Tag()) { + SourceLocation source_loc) + -> std::optional { + switch (p->kind()) { case Value::Kind::BindingPlaceholderValue: { const auto& placeholder = cast(*p); Env values(arena); if (placeholder.Name().has_value()) { - Address a = heap.AllocateValue(CopyVal(arena, v, loc)); + Address a = heap.AllocateValue(CopyVal(arena, v, source_loc)); values.Set(*placeholder.Name(), a); } return values; } case Value::Kind::TupleValue: - switch (v->Tag()) { + switch (v->kind()) { case Value::Kind::TupleValue: { const auto& p_tup = cast(*p); const auto& v_tup = cast(*v); if (p_tup.Elements().size() != v_tup.Elements().size()) { - FATAL_PROGRAM_ERROR(loc) + FATAL_PROGRAM_ERROR(source_loc) << "arity mismatch in tuple pattern match:\n pattern: " << p_tup << "\n value: " << v_tup; } Env values(arena); for (size_t i = 0; i < p_tup.Elements().size(); ++i) { if (p_tup.Elements()[i].name != v_tup.Elements()[i].name) { - FATAL_PROGRAM_ERROR(loc) + FATAL_PROGRAM_ERROR(source_loc) << "Tuple field name '" << v_tup.Elements()[i].name << "' does not match pattern field name '" << p_tup.Elements()[i].name << "'"; } - std::optional matches = PatternMatch( - p_tup.Elements()[i].value, v_tup.Elements()[i].value, loc); + std::optional matches = + PatternMatch(p_tup.Elements()[i].value, + v_tup.Elements()[i].value, source_loc); if (!matches) { return std::nullopt; } @@ -274,8 +276,9 @@ auto Interpreter::PatternMatch(Nonnull p, Nonnull v, Env values(arena); for (size_t i = 0; i < p_struct.elements().size(); ++i) { CHECK(p_struct.elements()[i].name == v_struct.elements()[i].name); - std::optional matches = PatternMatch( - p_struct.elements()[i].value, v_struct.elements()[i].value, loc); + std::optional matches = + PatternMatch(p_struct.elements()[i].value, + v_struct.elements()[i].value, source_loc); if (!matches) { return std::nullopt; } @@ -286,7 +289,7 @@ auto Interpreter::PatternMatch(Nonnull p, Nonnull v, return values; } case Value::Kind::AlternativeValue: - switch (v->Tag()) { + switch (v->kind()) { case Value::Kind::AlternativeValue: { const auto& p_alt = cast(*p); const auto& v_alt = cast(*v); @@ -294,23 +297,23 @@ auto Interpreter::PatternMatch(Nonnull p, Nonnull v, p_alt.AltName() != v_alt.AltName()) { return std::nullopt; } - return PatternMatch(p_alt.Argument(), v_alt.Argument(), loc); + return PatternMatch(p_alt.Argument(), v_alt.Argument(), source_loc); } default: FATAL() << "expected a choice alternative in pattern, not " << *v; } case Value::Kind::FunctionType: - switch (v->Tag()) { + switch (v->kind()) { case Value::Kind::FunctionType: { const auto& p_fn = cast(*p); const auto& v_fn = cast(*v); std::optional param_matches = - PatternMatch(p_fn.Param(), v_fn.Param(), loc); + PatternMatch(p_fn.Param(), v_fn.Param(), source_loc); if (!param_matches) { return std::nullopt; } std::optional ret_matches = - PatternMatch(p_fn.Ret(), v_fn.Ret(), loc); + PatternMatch(p_fn.Ret(), v_fn.Ret(), source_loc); if (!ret_matches) { return std::nullopt; } @@ -328,7 +331,7 @@ auto Interpreter::PatternMatch(Nonnull p, Nonnull v, // on the typechecker to ensure that `v` is a type. return Env(arena); default: - if (ValueEqual(p, v, loc)) { + if (ValueEqual(p, v, source_loc)) { return Env(arena); } else { return std::nullopt; @@ -338,18 +341,19 @@ auto Interpreter::PatternMatch(Nonnull p, Nonnull v, void Interpreter::PatternAssignment(Nonnull pat, Nonnull val, - SourceLocation loc) { - switch (pat->Tag()) { + SourceLocation source_loc) { + switch (pat->kind()) { case Value::Kind::PointerValue: - heap.Write(cast(*pat).Val(), CopyVal(arena, val, loc), loc); + heap.Write(cast(*pat).Val(), + CopyVal(arena, val, source_loc), source_loc); break; case Value::Kind::TupleValue: { - switch (val->Tag()) { + switch (val->kind()) { case Value::Kind::TupleValue: { const auto& pat_tup = cast(*pat); const auto& val_tup = cast(*val); if (pat_tup.Elements().size() != val_tup.Elements().size()) { - FATAL_RUNTIME_ERROR(loc) + FATAL_RUNTIME_ERROR(source_loc) << "arity mismatch in tuple pattern assignment:\n pattern: " << pat_tup << "\n value: " << val_tup; } @@ -357,10 +361,10 @@ void Interpreter::PatternAssignment(Nonnull pat, std::optional> value_field = val_tup.FindField(pattern_element.name); if (!value_field) { - FATAL_RUNTIME_ERROR(loc) + FATAL_RUNTIME_ERROR(source_loc) << "field " << pattern_element.name << "not in " << *val; } - PatternAssignment(pattern_element.value, *value_field, loc); + PatternAssignment(pattern_element.value, *value_field, source_loc); } break; } @@ -370,14 +374,14 @@ void Interpreter::PatternAssignment(Nonnull pat, break; } case Value::Kind::AlternativeValue: { - switch (val->Tag()) { + switch (val->kind()) { case Value::Kind::AlternativeValue: { const auto& pat_alt = cast(*pat); const auto& val_alt = cast(*val); CHECK(val_alt.ChoiceName() == pat_alt.ChoiceName() && val_alt.AltName() == pat_alt.AltName()) << "internal error in pattern assignment"; - PatternAssignment(pat_alt.Argument(), val_alt.Argument(), loc); + PatternAssignment(pat_alt.Argument(), val_alt.Argument(), source_loc); break; } default: @@ -386,7 +390,7 @@ void Interpreter::PatternAssignment(Nonnull pat, break; } default: - CHECK(ValueEqual(pat, val, loc)) + CHECK(ValueEqual(pat, val, source_loc)) << "internal error in pattern assignment"; } } @@ -395,20 +399,20 @@ auto Interpreter::StepLvalue() -> Transition { Nonnull act = stack.Top()->todo.Top(); Nonnull exp = cast(*act).Exp(); if (tracing_output) { - llvm::outs() << "--- step lvalue " << *exp << " (" << exp->SourceLoc() + llvm::outs() << "--- step lvalue " << *exp << " (" << exp->source_loc() << ") --->\n"; } - switch (exp->Tag()) { + switch (exp->kind()) { case Expression::Kind::IdentifierExpression: { // { {x :: C, E, F} :: S, H} // -> { {E(x) :: C, E, F} :: S, H} - Address pointer = - GetFromEnv(exp->SourceLoc(), cast(*exp).Name()); + Address pointer = GetFromEnv(exp->source_loc(), + cast(*exp).Name()); Nonnull v = arena->New(pointer); return Done{v}; } case Expression::Kind::FieldAccessExpression: { - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {e.f :: C, E, F} :: S, H} // -> { e :: [].f :: C, E, F} :: S, H} return Spawn{arena->New( @@ -416,41 +420,41 @@ auto Interpreter::StepLvalue() -> Transition { } else { // { v :: [].f :: C, E, F} :: S, H} // -> { { &v.f :: C, E, F} :: S, H } - Address aggregate = cast(*act->Results()[0]).Val(); + Address aggregate = cast(*act->results()[0]).Val(); Address field = aggregate.SubobjectAddress( cast(*exp).Field()); return Done{arena->New(field)}; } } case Expression::Kind::IndexExpression: { - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {e[i] :: C, E, F} :: S, H} // -> { e :: [][i] :: C, E, F} :: S, H} return Spawn{ arena->New(cast(*exp).Aggregate())}; - } else if (act->Pos() == 1) { + } else if (act->pos() == 1) { return Spawn{ arena->New(cast(*exp).Offset())}; } else { // { v :: [][i] :: C, E, F} :: S, H} // -> { { &v[i] :: C, E, F} :: S, H } - Address aggregate = cast(*act->Results()[0]).Val(); + Address aggregate = cast(*act->results()[0]).Val(); std::string f = - std::to_string(cast(*act->Results()[1]).Val()); + std::to_string(cast(*act->results()[1]).Val()); Address field = aggregate.SubobjectAddress(f); return Done{arena->New(field)}; } } case Expression::Kind::TupleLiteral: { - if (act->Pos() < + if (act->pos() < static_cast(cast(*exp).Fields().size())) { // { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S, // H} // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S, // H} Nonnull elt = - cast(*exp).Fields()[act->Pos()].expression; + cast(*exp).Fields()[act->pos()].expression; return Spawn{arena->New(elt)}; } else { return Done{CreateTuple(act, exp)}; @@ -479,29 +483,29 @@ auto Interpreter::StepExp() -> Transition { Nonnull act = stack.Top()->todo.Top(); Nonnull exp = cast(*act).Exp(); if (tracing_output) { - llvm::outs() << "--- step exp " << *exp << " (" << exp->SourceLoc() + llvm::outs() << "--- step exp " << *exp << " (" << exp->source_loc() << ") --->\n"; } - switch (exp->Tag()) { + switch (exp->kind()) { case Expression::Kind::IndexExpression: { - if (act->Pos() == 0) { + if (act->pos() == 0) { // { { e[i] :: C, E, F} :: S, H} // -> { { e :: [][i] :: C, E, F} :: S, H} return Spawn{arena->New( cast(*exp).Aggregate())}; - } else if (act->Pos() == 1) { + } else if (act->pos() == 1) { return Spawn{ arena->New(cast(*exp).Offset())}; } else { // { { v :: [][i] :: C, E, F} :: S, H} // -> { { v_i :: C, E, F} : S, H} - auto* tuple = dyn_cast(act->Results()[0]); + auto* tuple = dyn_cast(act->results()[0]); if (tuple == nullptr) { FATAL_RUNTIME_ERROR_NO_LINE() - << "expected a tuple in field access, not " << *act->Results()[0]; + << "expected a tuple in field access, not " << *act->results()[0]; } std::string f = - std::to_string(cast(*act->Results()[1]).Val()); + std::to_string(cast(*act->results()[1]).Val()); std::optional> field = tuple->FindField(f); if (!field) { FATAL_RUNTIME_ERROR_NO_LINE() @@ -511,14 +515,14 @@ auto Interpreter::StepExp() -> Transition { } } case Expression::Kind::TupleLiteral: { - if (act->Pos() < + if (act->pos() < static_cast(cast(*exp).Fields().size())) { // { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S, // H} // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S, // H} Nonnull elt = - cast(*exp).Fields()[act->Pos()].expression; + cast(*exp).Fields()[act->pos()].expression; return Spawn{arena->New(elt)}; } else { return Done{CreateTuple(act, exp)}; @@ -526,93 +530,93 @@ auto Interpreter::StepExp() -> Transition { } case Expression::Kind::StructLiteral: { const auto& literal = cast(*exp); - if (act->Pos() < static_cast(literal.fields().size())) { + if (act->pos() < static_cast(literal.fields().size())) { Nonnull elt = - literal.fields()[act->Pos()].expression; + literal.fields()[act->pos()].expression; return Spawn{arena->New(elt)}; } else { - return Done{CreateStruct(literal.fields(), act->Results())}; + return Done{CreateStruct(literal.fields(), act->results())}; } } case Expression::Kind::StructTypeLiteral: { const auto& struct_type = cast(*exp); - if (act->Pos() < static_cast(struct_type.fields().size())) { + 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) { - fields.push_back({struct_type.fields()[i].name, act->Results()[i]}); + fields.push_back({struct_type.fields()[i].name, act->results()[i]}); } return Done{arena->New(std::move(fields))}; } } case Expression::Kind::FieldAccessExpression: { const auto& access = cast(*exp); - if (act->Pos() == 0) { + if (act->pos() == 0) { // { { e.f :: C, E, F} :: S, H} // -> { { e :: [].f :: C, E, F} :: S, H} 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->SourceLoc())}; + return Done{act->results()[0]->GetField( + arena, FieldPath(access.Field()), exp->source_loc())}; } } case Expression::Kind::IdentifierExpression: { - CHECK(act->Pos() == 0); + 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->SourceLoc(), ident.Name()); - return Done{heap.Read(pointer, exp->SourceLoc())}; + Address pointer = GetFromEnv(exp->source_loc(), ident.Name()); + return Done{heap.Read(pointer, exp->source_loc())}; } case Expression::Kind::IntLiteral: - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} return Done{arena->New(cast(*exp).Val())}; case Expression::Kind::BoolLiteral: - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} return Done{arena->New(cast(*exp).Val())}; 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->SourceLoc())}; + return Done{EvalPrim(op.Op(), act->results(), exp->source_loc())}; } } case Expression::Kind::CallExpression: - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {e1(e2) :: C, E, F} :: S, H} // -> { {e1 :: [](e2) :: C, E, F} :: S, H} return Spawn{arena->New( cast(*exp).Function())}; - } else if (act->Pos() == 1) { + } 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())}; - } else if (act->Pos() == 2) { + } else if (act->pos() == 2) { // { { v2 :: v1([]) :: C, E, F} :: S, H} // -> { {C',E',F'} :: {C, E, F} :: S, H} - switch (act->Results()[0]->Tag()) { + switch (act->results()[0]->kind()) { case Value::Kind::NominalClassType: { Nonnull arg = - CopyVal(arena, act->Results()[1], exp->SourceLoc()); - return Done{arena->New(act->Results()[0], arg)}; + CopyVal(arena, act->results()[1], exp->source_loc()); + return Done{arena->New(act->results()[0], arg)}; } case Value::Kind::AlternativeConstructorValue: { const auto& alt = - cast(*act->Results()[0]); + cast(*act->results()[0]); Nonnull arg = - CopyVal(arena, act->Results()[1], exp->SourceLoc()); + CopyVal(arena, act->results()[1], exp->source_loc()); return Done{arena->New(alt.AltName(), alt.ChoiceName(), arg)}; } @@ -621,46 +625,46 @@ auto Interpreter::StepExp() -> Transition { // TODO: Think about a cleaner way to cast between Ptr types. // (multiple TODOs) .function = Nonnull( - cast(act->Results()[0])), - .args = act->Results()[1], - .loc = exp->SourceLoc()}; + cast(act->results()[0])), + .args = act->results()[1], + .source_loc = exp->source_loc()}; default: - FATAL_RUNTIME_ERROR(exp->SourceLoc()) - << "in call, expected a function, not " << *act->Results()[0]; + FATAL_RUNTIME_ERROR(exp->source_loc()) + << "in call, expected a function, not " << *act->results()[0]; } } else { - FATAL() << "in handle_value with Call pos " << act->Pos(); + FATAL() << "in handle_value with Call pos " << act->pos(); } case Expression::Kind::IntrinsicExpression: - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} switch (cast(*exp).Intrinsic()) { case IntrinsicExpression::IntrinsicKind::Print: - Address pointer = GetFromEnv(exp->SourceLoc(), "format_str"); - Nonnull pointee = heap.Read(pointer, exp->SourceLoc()); - CHECK(pointee->Tag() == Value::Kind::StringValue); + Address pointer = GetFromEnv(exp->source_loc(), "format_str"); + Nonnull pointee = heap.Read(pointer, exp->source_loc()); + CHECK(pointee->kind() == Value::Kind::StringValue); // TODO: This could eventually use something like llvm::formatv. llvm::outs() << cast(*pointee).Val(); return Done{TupleValue::Empty()}; } case Expression::Kind::IntTypeLiteral: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); return Done{arena->New()}; } case Expression::Kind::BoolTypeLiteral: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); return Done{arena->New()}; } case Expression::Kind::TypeTypeLiteral: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); return Done{arena->New()}; } case Expression::Kind::FunctionTypeLiteral: { - if (act->Pos() == 0) { + if (act->pos() == 0) { return Spawn{arena->New( cast(*exp).Parameter())}; - } else if (act->Pos() == 1) { + } 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( @@ -669,23 +673,23 @@ auto Interpreter::StepExp() -> Transition { // { { rt :: fn pt -> [] :: C, E, F} :: S, H} // -> { fn pt -> rt :: {C, E, F} :: S, H} return Done{arena->New(std::vector(), - act->Results()[0], - act->Results()[1])}; + act->results()[0], + act->results()[1])}; } } case Expression::Kind::ContinuationTypeLiteral: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); return Done{arena->New()}; } case Expression::Kind::StringLiteral: - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} return Done{arena->New(cast(*exp).Val())}; case Expression::Kind::StringTypeLiteral: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); return Done{arena->New()}; } - } // switch (exp->Tag) + } // switch (exp->kind) } auto Interpreter::StepPattern() -> Transition { @@ -693,52 +697,52 @@ auto Interpreter::StepPattern() -> Transition { Nonnull pattern = cast(*act).Pat(); if (tracing_output) { llvm::outs() << "--- step pattern " << *pattern << " (" - << pattern->SourceLoc() << ") --->\n"; + << pattern->source_loc() << ") --->\n"; } - switch (pattern->Tag()) { + switch (pattern->kind()) { case Pattern::Kind::AutoPattern: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); return Done{arena->New()}; } case Pattern::Kind::BindingPattern: { const auto& binding = cast(*pattern); - if (act->Pos() == 0) { + if (act->pos() == 0) { return Spawn{arena->New(binding.Type())}; } else { return Done{arena->New(binding.Name(), - act->Results()[0])}; + act->results()[0])}; } } case Pattern::Kind::TuplePattern: { const auto& tuple = cast(*pattern); - if (act->Pos() < static_cast(tuple.Fields().size())) { + if (act->pos() < static_cast(tuple.Fields().size())) { // { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S, // H} // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S, // H} - Nonnull elt = tuple.Fields()[act->Pos()].pattern; + Nonnull elt = tuple.Fields()[act->pos()].pattern; return Spawn{arena->New(elt)}; } else { std::vector elements; for (size_t i = 0; i < tuple.Fields().size(); ++i) { elements.push_back( - {.name = tuple.Fields()[i].name, .value = act->Results()[i]}); + {.name = tuple.Fields()[i].name, .value = act->results()[i]}); } return Done{arena->New(std::move(elements))}; } } case Pattern::Kind::AlternativePattern: { const auto& alternative = cast(*pattern); - if (act->Pos() == 0) { + if (act->pos() == 0) { return Spawn{arena->New(alternative.ChoiceType())}; - } else if (act->Pos() == 1) { + } else if (act->pos() == 1) { return Spawn{arena->New(alternative.Arguments())}; } else { - CHECK(act->Pos() == 2); - const auto& choice_type = cast(*act->Results()[0]); + CHECK(act->pos() == 2); + const auto& choice_type = cast(*act->results()[0]); return Done{arena->New(alternative.AlternativeName(), choice_type.Name(), - act->Results()[1])}; + act->results()[1])}; } } case Pattern::Kind::ExpressionPattern: @@ -748,9 +752,9 @@ auto Interpreter::StepPattern() -> Transition { } static auto IsWhileAct(Nonnull act) -> bool { - switch (act->Tag()) { + switch (act->kind()) { case Action::Kind::StatementAction: - switch (cast(*act).Stmt()->Tag()) { + switch (cast(*act).Stmt()->kind()) { case Statement::Kind::While: return true; default: @@ -762,9 +766,9 @@ static auto IsWhileAct(Nonnull act) -> bool { } static auto HasLocalScope(Nonnull act) -> bool { - switch (act->Tag()) { + switch (act->kind()) { case Action::Kind::StatementAction: - switch (cast(*act).Stmt()->Tag()) { + switch (cast(*act).Stmt()->kind()) { case Statement::Kind::Block: case Statement::Kind::Match: return true; @@ -783,27 +787,27 @@ auto Interpreter::StepStmt() -> Transition { if (tracing_output) { llvm::outs() << "--- step stmt "; stmt->PrintDepth(1, llvm::outs()); - llvm::outs() << " (" << stmt->SourceLoc() << ") --->\n"; + llvm::outs() << " (" << stmt->source_loc() << ") --->\n"; } - switch (stmt->Tag()) { + switch (stmt->kind()) { case Statement::Kind::Match: { const auto& match_stmt = cast(*stmt); - if (act->Pos() == 0) { + if (act->pos() == 0) { // { { (match (e) ...) :: C, E, F} :: S, H} // -> { { e :: (match ([]) ...) :: C, E, F} :: S, H} frame->scopes.Push(arena->New(CurrentEnv())); return Spawn{arena->New(&match_stmt.expression())}; } else { - // Regarding act->Pos(): + // Regarding act->pos(): // * odd: start interpreting the pattern of a clause // * even: finished interpreting the pattern, now try to match // - // Regarding act->Results(): + // Regarding act->results(): // * 0: the value that we're matching // * 1: the pattern for clause 0 // * 2: the pattern for clause 1 // * ... - auto clause_num = (act->Pos() - 1) / 2; + auto clause_num = (act->pos() - 1) / 2; if (clause_num >= static_cast(match_stmt.clauses().size())) { DeallocateScope(frame->scopes.Top()); frame->scopes.Pop(); @@ -811,18 +815,18 @@ auto Interpreter::StepStmt() -> Transition { } auto c = match_stmt.clauses()[clause_num]; - if (act->Pos() % 2 == 1) { + if (act->pos() % 2 == 1) { // start interpreting the pattern of the clause // { {v :: (match ([]) ...) :: C, E, F} :: S, H} // -> { {pi :: (match ([]) ...) :: C, E, F} :: S, H} return Spawn{arena->New(&c.pattern())}; } else { // try to match - auto v = act->Results()[0]; - auto pat = act->Results()[clause_num + 1]; - std::optional matches = PatternMatch(pat, v, stmt->SourceLoc()); + auto v = act->results()[0]; + auto pat = act->results()[clause_num + 1]; + std::optional matches = PatternMatch(pat, v, stmt->source_loc()); if (matches) { // we have a match, start the body // Ensure we don't process any more clauses. - act->SetPos(2 * match_stmt.clauses().size() + 1); + act->set_pos(2 * match_stmt.clauses().size() + 1); for (const auto& [name, value] : *matches) { frame->scopes.Top()->values.Set(name, value); @@ -836,12 +840,12 @@ auto Interpreter::StepStmt() -> Transition { } } case Statement::Kind::While: - if (act->Pos() % 2 == 0) { + if (act->pos() % 2 == 0) { // { { (while (e) s) :: C, E, F} :: S, H} // -> { { e :: (while ([]) s) :: C, E, F} :: S, H} act->Clear(); return Spawn{arena->New(cast(*stmt).Cond())}; - } else if (cast(*act->Results().back()).Val()) { + } else if (cast(*act->results().back()).Val()) { // { {true :: (while ([]) s) :: C, E, F} :: S, H} // -> { { s :: (while (e) s) :: C, E, F } :: S, H} return Spawn{arena->New(cast(*stmt).Body())}; @@ -851,32 +855,32 @@ auto Interpreter::StepStmt() -> Transition { return Done{}; } case Statement::Kind::Break: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // { { break; :: ... :: (while (e) s) :: C, E, F} :: S, H} // -> { { C, E', F} :: S, H} auto it = std::find_if(frame->todo.begin(), frame->todo.end(), &IsWhileAct); if (it == frame->todo.end()) { - FATAL_RUNTIME_ERROR(stmt->SourceLoc()) + FATAL_RUNTIME_ERROR(stmt->source_loc()) << "`break` not inside `while` statement"; } ++it; return UnwindTo{*it}; } case Statement::Kind::Continue: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // { { continue; :: ... :: (while (e) s) :: C, E, F} :: S, H} // -> { { (while (e) s) :: C, E', F} :: S, H} auto it = std::find_if(frame->todo.begin(), frame->todo.end(), &IsWhileAct); if (it == frame->todo.end()) { - FATAL_RUNTIME_ERROR(stmt->SourceLoc()) + FATAL_RUNTIME_ERROR(stmt->source_loc()) << "`continue` not inside `while` statement"; } return UnwindTo{*it}; } case Statement::Kind::Block: { - if (act->Pos() == 0) { + if (act->pos() == 0) { const Block& block = cast(*stmt); if (block.Stmt()) { frame->scopes.Push(arena->New(CurrentEnv())); @@ -892,23 +896,23 @@ auto Interpreter::StepStmt() -> Transition { } } case Statement::Kind::VariableDefinition: - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {(var x = e) :: C, E, F} :: S, H} // -> { {e :: (var x = []) :: C, E, F} :: S, H} return Spawn{arena->New( cast(*stmt).Init())}; - } else if (act->Pos() == 1) { + } else if (act->pos() == 1) { return Spawn{ arena->New(cast(*stmt).Pat())}; } else { // { { v :: (x = []) :: C, E, F} :: S, H} // -> { { C, E(x := a), F} :: S, H(a := copy(v))} - Nonnull v = act->Results()[0]; - Nonnull p = act->Results()[1]; + Nonnull v = act->results()[0]; + Nonnull p = act->results()[1]; - std::optional matches = PatternMatch(p, v, stmt->SourceLoc()); + std::optional matches = PatternMatch(p, v, stmt->source_loc()); CHECK(matches) - << stmt->SourceLoc() + << stmt->source_loc() << ": internal error in variable definition, match failed"; for (const auto& [name, value] : *matches) { frame->scopes.Top()->values.Set(name, value); @@ -917,7 +921,7 @@ auto Interpreter::StepStmt() -> Transition { return Done{}; } case Statement::Kind::ExpressionStatement: - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {e :: C, E, F} :: S, H} // -> { {e :: C, E, F} :: S, H} return Spawn{arena->New( @@ -926,28 +930,28 @@ auto Interpreter::StepStmt() -> Transition { return Done{}; } case Statement::Kind::Assign: - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {(lv = e) :: C, E, F} :: S, H} // -> { {lv :: ([] = e) :: C, E, F} :: S, H} return Spawn{arena->New(cast(*stmt).Lhs())}; - } else if (act->Pos() == 1) { + } else if (act->pos() == 1) { // { { a :: ([] = e) :: C, E, F} :: S, H} // -> { { e :: (a = []) :: C, E, F} :: S, H} return Spawn{arena->New(cast(*stmt).Rhs())}; } else { // { { v :: (a = []) :: C, E, F} :: S, H} // -> { { C, E, F} :: S, H(a := v)} - auto pat = act->Results()[0]; - auto val = act->Results()[1]; - PatternAssignment(pat, val, stmt->SourceLoc()); + auto pat = act->results()[0]; + auto val = act->results()[1]; + PatternAssignment(pat, val, stmt->source_loc()); return Done{}; } case Statement::Kind::If: - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {(if (e) then_stmt else else_stmt) :: C, E, F} :: S, H} // -> { { e :: (if ([]) then_stmt else else_stmt) :: C, E, F} :: S, H} return Spawn{arena->New(cast(*stmt).Cond())}; - } else if (cast(*act->Results()[0]).Val()) { + } else if (cast(*act->results()[0]).Val()) { // { {true :: if ([]) then_stmt else else_stmt :: C, E, F} :: // S, H} // -> { { then_stmt :: C, E, F } :: S, H} @@ -963,7 +967,7 @@ auto Interpreter::StepStmt() -> Transition { return Done{}; } case Statement::Kind::Return: - if (act->Pos() == 0) { + if (act->pos() == 0) { // { {return e :: C, E, F} :: S, H} // -> { {e :: return [] :: C, E, F} :: S, H} return Spawn{arena->New(cast(*stmt).Exp())}; @@ -971,14 +975,14 @@ auto Interpreter::StepStmt() -> Transition { // { {v :: return [] :: C, E, F} :: {C', E', F'} :: S, H} // -> { {v :: C', E', F'} :: S, H} Nonnull ret_val = - CopyVal(arena, act->Results()[0], stmt->SourceLoc()); + CopyVal(arena, act->results()[0], stmt->source_loc()); return UnwindFunctionCall{ret_val}; } case Statement::Kind::Sequence: { // { { (s1,s2) :: C, E, F} :: S, H} // -> { { s1 :: s2 :: C, E, F} :: S, H} const Sequence& seq = cast(*stmt); - if (act->Pos() == 0) { + if (act->pos() == 0) { return Spawn{arena->New(seq.Stmt())}; } else { if (seq.Next()) { @@ -990,13 +994,13 @@ auto Interpreter::StepStmt() -> Transition { } } case Statement::Kind::Continuation: { - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // Create a continuation object by creating a frame similar the // way one is created in a function call. auto scopes = Stack>(arena->New(CurrentEnv())); Stack> todo; todo.Push(arena->New( - arena->New(arena, stmt->SourceLoc()))); + arena->New(arena, stmt->source_loc()))); todo.Push(arena->New(cast(*stmt).Body())); auto continuation_frame = arena->New("__continuation", scopes, todo); @@ -1014,7 +1018,7 @@ auto Interpreter::StepStmt() -> Transition { return ManualTransition{}; } case Statement::Kind::Run: - if (act->Pos() == 0) { + if (act->pos() == 0) { // Evaluate the argument of the run statement. return Spawn{arena->New(cast(*stmt).Argument())}; } else { @@ -1023,12 +1027,12 @@ auto Interpreter::StepStmt() -> Transition { // value from the continuation. auto ignore_result = arena->New(arena->New( - stmt->SourceLoc(), - arena->New(stmt->SourceLoc()))); + stmt->source_loc(), + arena->New(stmt->source_loc()))); frame->todo.Push(ignore_result); // Push the continuation onto the current stack. const std::vector>& continuation_vector = - cast(*act->Results()[0]).Stack(); + cast(*act->results()[0]).Stack(); for (auto frame_iter = continuation_vector.rbegin(); frame_iter != continuation_vector.rend(); ++frame_iter) { stack.Push(*frame_iter); @@ -1036,7 +1040,7 @@ auto Interpreter::StepStmt() -> Transition { return ManualTransition{}; } case Statement::Kind::Await: - CHECK(act->Pos() == 0); + CHECK(act->pos() == 0); // Pause the current continuation frame->todo.Pop(); std::vector> paused; @@ -1045,7 +1049,7 @@ auto Interpreter::StepStmt() -> Transition { } while (paused.back()->continuation == std::nullopt); // Update the continuation with the paused stack. heap.Write(*paused.back()->continuation, - arena->New(paused), stmt->SourceLoc()); + arena->New(paused), stmt->source_loc()); return ManualTransition{}; } } @@ -1057,7 +1061,7 @@ class Interpreter::DoTransition { void operator()(const Done& done) { Nonnull frame = interpreter->stack.Top(); - if (frame->todo.Top()->Tag() != Action::Kind::StatementAction) { + if (frame->todo.Top()->kind() != Action::Kind::StatementAction) { CHECK(done.result); frame->todo.Pop(); if (frame->todo.IsEmpty()) { @@ -1074,7 +1078,7 @@ class Interpreter::DoTransition { void operator()(const Spawn& spawn) { Nonnull frame = interpreter->stack.Top(); Nonnull action = frame->todo.Top(); - action->SetPos(action->Pos() + 1); + action->set_pos(action->pos() + 1); frame->todo.Push(spawn.child); } @@ -1086,7 +1090,7 @@ class Interpreter::DoTransition { void operator()(const RunAgain&) { Nonnull action = interpreter->stack.Top()->todo.Top(); - action->SetPos(action->Pos() + 1); + action->set_pos(action->pos() + 1); } void operator()(const UnwindTo& unwind_to) { @@ -1112,8 +1116,8 @@ class Interpreter::DoTransition { void operator()(const CallFunction& call) { interpreter->stack.Top()->todo.Pop(); - std::optional matches = - interpreter->PatternMatch(call.function->Param(), call.args, call.loc); + std::optional matches = interpreter->PatternMatch( + call.function->Param(), call.args, call.source_loc); CHECK(matches.has_value()) << "internal error in call_function, pattern match failed"; // Create the new frame and push it on the stack @@ -1148,7 +1152,7 @@ void Interpreter::Step() { } Nonnull act = frame->todo.Top(); - switch (act->Tag()) { + switch (act->kind()) { case Action::Kind::LValAction: std::visit(DoTransition(this), StepLvalue()); break; diff --git a/executable_semantics/interpreter/interpreter.h b/executable_semantics/interpreter/interpreter.h index 9cb1203defe8..ecb2d290089a 100644 --- a/executable_semantics/interpreter/interpreter.h +++ b/executable_semantics/interpreter/interpreter.h @@ -42,7 +42,7 @@ class Interpreter { // Attempts to match `v` against the pattern `p`. If matching succeeds, // returns the bindings of pattern variables to their matched values. auto PatternMatch(Nonnull p, Nonnull v, - SourceLocation loc) -> std::optional; + SourceLocation source_loc) -> std::optional; // Support TypeChecker allocating values on the heap. auto AllocateValue(Nonnull v) -> Address { @@ -102,7 +102,7 @@ class Interpreter { struct CallFunction { Nonnull function; Nonnull args; - SourceLocation loc; + SourceLocation source_loc; }; // Transition type which does nothing. @@ -131,7 +131,8 @@ class Interpreter { void InitGlobals(const std::vector>& fs); auto CurrentEnv() -> Env; - auto GetFromEnv(SourceLocation loc, const std::string& name) -> Address; + auto GetFromEnv(SourceLocation source_loc, const std::string& name) + -> Address; void DeallocateScope(Nonnull scope); void DeallocateLocals(Nonnull frame); @@ -143,10 +144,10 @@ class Interpreter { -> Nonnull; auto EvalPrim(Operator op, const std::vector>& args, - SourceLocation loc) -> Nonnull; + SourceLocation source_loc) -> Nonnull; void PatternAssignment(Nonnull pat, Nonnull val, - SourceLocation loc); + SourceLocation source_loc); void PrintState(llvm::raw_ostream& out); diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index 8d600733a51f..a1b6ebacc6ff 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -40,72 +40,75 @@ void PrintTypeEnv(TypeEnv types, llvm::raw_ostream& out) { } } -static void ExpectType(SourceLocation loc, const std::string& context, +static void ExpectType(SourceLocation source_loc, const std::string& context, Nonnull expected, Nonnull actual) { if (!TypeEqual(expected, actual)) { - FATAL_COMPILATION_ERROR(loc) << "type error in " << context << "\n" - << "expected: " << *expected << "\n" - << "actual: " << *actual; + FATAL_COMPILATION_ERROR(source_loc) << "type error in " << context << "\n" + << "expected: " << *expected << "\n" + << "actual: " << *actual; } } -static void ExpectPointerType(SourceLocation loc, const std::string& context, +static void ExpectPointerType(SourceLocation source_loc, + const std::string& context, Nonnull actual) { - if (actual->Tag() != Value::Kind::PointerType) { - FATAL_COMPILATION_ERROR(loc) << "type error in " << context << "\n" - << "expected a pointer type\n" - << "actual: " << *actual; + if (actual->kind() != Value::Kind::PointerType) { + FATAL_COMPILATION_ERROR(source_loc) << "type error in " << context << "\n" + << "expected a pointer type\n" + << "actual: " << *actual; } } -auto TypeChecker::ReifyType(Nonnull t, SourceLocation loc) +auto TypeChecker::ReifyType(Nonnull t, SourceLocation source_loc) -> Nonnull { - switch (t->Tag()) { + switch (t->kind()) { case Value::Kind::IntType: - return arena->New(loc); + return arena->New(source_loc); case Value::Kind::BoolType: - return arena->New(loc); + return arena->New(source_loc); case Value::Kind::TypeType: - return arena->New(loc); + return arena->New(source_loc); case Value::Kind::ContinuationType: - return arena->New(loc); + return arena->New(source_loc); case Value::Kind::FunctionType: { const auto& fn_type = cast(*t); return arena->New( - loc, ReifyType(fn_type.Param(), loc), ReifyType(fn_type.Ret(), loc), + source_loc, ReifyType(fn_type.Param(), source_loc), + ReifyType(fn_type.Ret(), source_loc), /*is_omitted_return_type=*/false); } case Value::Kind::TupleValue: { std::vector args; for (const TupleElement& field : cast(*t).Elements()) { args.push_back( - FieldInitializer(field.name, ReifyType(field.value, loc))); + FieldInitializer(field.name, ReifyType(field.value, source_loc))); } - return arena->New(loc, args); + return arena->New(source_loc, args); } case Value::Kind::StructType: { std::vector args; for (const auto& [name, type] : cast(*t).fields()) { - args.push_back(FieldInitializer(name, ReifyType(type, loc))); + args.push_back(FieldInitializer(name, ReifyType(type, source_loc))); } - return arena->New(loc, args); + return arena->New(source_loc, args); } case Value::Kind::NominalClassType: return arena->New( - loc, cast(*t).Name()); + source_loc, cast(*t).Name()); case Value::Kind::ChoiceType: - return arena->New(loc, cast(*t).Name()); + return arena->New(source_loc, + cast(*t).Name()); case Value::Kind::PointerType: return arena->New( - loc, Operator::Ptr, + source_loc, Operator::Ptr, std::vector>( - {ReifyType(cast(*t).Type(), loc)})); + {ReifyType(cast(*t).Type(), source_loc)})); case Value::Kind::VariableType: - return arena->New(loc, + return arena->New(source_loc, cast(*t).Name()); case Value::Kind::StringType: - return arena->New(loc); + return arena->New(source_loc); case Value::Kind::AlternativeConstructorValue: case Value::Kind::AlternativeValue: case Value::Kind::AutoType: @@ -128,78 +131,81 @@ auto TypeChecker::ReifyType(Nonnull t, SourceLocation loc) // inside the argument type. // The `deduced` parameter is an accumulator, that is, it holds the // results so-far. -static auto ArgumentDeduction(SourceLocation loc, TypeEnv deduced, +static auto ArgumentDeduction(SourceLocation source_loc, TypeEnv deduced, Nonnull param, Nonnull arg) -> TypeEnv { - switch (param->Tag()) { + switch (param->kind()) { case Value::Kind::VariableType: { const auto& var_type = cast(*param); std::optional> d = deduced.Get(var_type.Name()); if (!d) { deduced.Set(var_type.Name(), arg); } else { - ExpectType(loc, "argument deduction", *d, arg); + ExpectType(source_loc, "argument deduction", *d, arg); } return deduced; } case Value::Kind::TupleValue: { - if (arg->Tag() != Value::Kind::TupleValue) { - ExpectType(loc, "argument deduction", param, arg); + if (arg->kind() != Value::Kind::TupleValue) { + ExpectType(source_loc, "argument deduction", param, arg); } const auto& param_tup = cast(*param); const auto& arg_tup = cast(*arg); if (param_tup.Elements().size() != arg_tup.Elements().size()) { - ExpectType(loc, "argument deduction", param, arg); + ExpectType(source_loc, "argument deduction", param, arg); } for (size_t i = 0; i < param_tup.Elements().size(); ++i) { if (param_tup.Elements()[i].name != arg_tup.Elements()[i].name) { - FATAL_COMPILATION_ERROR(loc) + FATAL_COMPILATION_ERROR(source_loc) << "mismatch in tuple names, " << param_tup.Elements()[i].name << " != " << arg_tup.Elements()[i].name; } - deduced = ArgumentDeduction(loc, deduced, param_tup.Elements()[i].value, + deduced = ArgumentDeduction(source_loc, deduced, + param_tup.Elements()[i].value, arg_tup.Elements()[i].value); } return deduced; } case Value::Kind::StructType: { - if (arg->Tag() != Value::Kind::StructType) { - ExpectType(loc, "argument deduction", param, arg); + if (arg->kind() != Value::Kind::StructType) { + ExpectType(source_loc, "argument deduction", param, arg); } const auto& param_struct = cast(*param); const auto& arg_struct = cast(*arg); if (param_struct.fields().size() != arg_struct.fields().size()) { - ExpectType(loc, "argument deduction", param, arg); + ExpectType(source_loc, "argument deduction", param, arg); } for (size_t i = 0; i < param_struct.fields().size(); ++i) { if (param_struct.fields()[i].first != arg_struct.fields()[i].first) { - FATAL_COMPILATION_ERROR(loc) + FATAL_COMPILATION_ERROR(source_loc) << "mismatch in field names, " << param_struct.fields()[i].first << " != " << arg_struct.fields()[i].first; } - deduced = - ArgumentDeduction(loc, deduced, param_struct.fields()[i].second, - arg_struct.fields()[i].second); + deduced = ArgumentDeduction(source_loc, deduced, + param_struct.fields()[i].second, + arg_struct.fields()[i].second); } return deduced; } case Value::Kind::FunctionType: { - if (arg->Tag() != Value::Kind::FunctionType) { - ExpectType(loc, "argument deduction", param, arg); + if (arg->kind() != Value::Kind::FunctionType) { + ExpectType(source_loc, "argument deduction", param, arg); } const auto& param_fn = cast(*param); const auto& arg_fn = cast(*arg); // TODO: handle situation when arg has deduced parameters. + deduced = ArgumentDeduction(source_loc, deduced, param_fn.Param(), + arg_fn.Param()); deduced = - ArgumentDeduction(loc, deduced, param_fn.Param(), arg_fn.Param()); - deduced = ArgumentDeduction(loc, deduced, param_fn.Ret(), arg_fn.Ret()); + ArgumentDeduction(source_loc, deduced, param_fn.Ret(), arg_fn.Ret()); return deduced; } case Value::Kind::PointerType: { - if (arg->Tag() != Value::Kind::PointerType) { - ExpectType(loc, "argument deduction", param, arg); + if (arg->kind() != Value::Kind::PointerType) { + ExpectType(source_loc, "argument deduction", param, arg); } - return ArgumentDeduction(loc, deduced, cast(*param).Type(), + return ArgumentDeduction(source_loc, deduced, + cast(*param).Type(), cast(*arg).Type()); } // Nothing to do in the case for `auto`. @@ -214,7 +220,7 @@ static auto ArgumentDeduction(SourceLocation loc, TypeEnv deduced, case Value::Kind::BoolType: case Value::Kind::TypeType: case Value::Kind::StringType: - ExpectType(loc, "argument deduction", param, arg); + ExpectType(source_loc, "argument deduction", param, arg); return deduced; // The rest of these cases should never happen. case Value::Kind::IntValue: @@ -234,7 +240,7 @@ static auto ArgumentDeduction(SourceLocation loc, TypeEnv deduced, auto TypeChecker::Substitute(TypeEnv dict, Nonnull type) -> Nonnull { - switch (type->Tag()) { + switch (type->kind()) { case Value::Kind::VariableType: { std::optional> t = dict.Get(cast(*type).Name()); @@ -305,12 +311,12 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, interpreter.PrintEnv(values, llvm::outs()); llvm::outs() << "\n"; } - switch (e->Tag()) { + switch (e->kind()) { case Expression::Kind::IndexExpression: { auto& index = cast(*e); auto res = TypeCheckExp(index.Aggregate(), types, values); auto t = res.type; - switch (t->Tag()) { + switch (t->kind()) { case Value::Kind::TupleValue: { auto i = cast(*interpreter.InterpExp(values, index.Offset())) @@ -319,16 +325,16 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, std::optional> field_t = cast(*t).FindField(f); if (!field_t) { - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "field " << f << " is not in the tuple " << *t; } auto new_e = arena->New( - e->SourceLoc(), res.exp, - arena->New(e->SourceLoc(), i)); + e->source_loc(), res.exp, + arena->New(e->source_loc(), i)); return TCExpression(new_e, *field_t, res.types); } default: - FATAL_COMPILATION_ERROR(e->SourceLoc()) << "expected a tuple"; + FATAL_COMPILATION_ERROR(e->source_loc()) << "expected a tuple"; } } case Expression::Kind::TupleLiteral: { @@ -341,7 +347,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, new_args.push_back(FieldInitializer(arg.name, arg_res.exp)); arg_types.push_back({.name = arg.name, .value = arg_res.type}); } - auto tuple_e = arena->New(e->SourceLoc(), new_args); + auto tuple_e = arena->New(e->source_loc(), new_args); auto tuple_t = arena->New(std::move(arg_types)); return TCExpression(tuple_e, tuple_t, new_types); } @@ -355,7 +361,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, new_args.push_back(FieldInitializer(arg.name, arg_res.exp)); arg_types.push_back({arg.name, arg_res.type}); } - auto new_e = arena->New(e->SourceLoc(), new_args); + auto new_e = arena->New(e->source_loc(), new_args); auto type = arena->New(std::move(arg_types)); return TCExpression(new_e, type, new_types); } @@ -368,9 +374,9 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, new_types = arg_res.types; Nonnull type = interpreter.InterpExp(values, arg_res.exp); new_args.push_back( - FieldInitializer(arg.name, ReifyType(type, e->SourceLoc()))); + FieldInitializer(arg.name, ReifyType(type, e->source_loc()))); } - auto new_e = arena->New(e->SourceLoc(), new_args); + auto new_e = arena->New(e->source_loc(), new_args); Nonnull type; if (struct_type.fields().empty()) { // `{}` is the type of `{}`, just as `()` is the type of `()`. @@ -387,17 +393,17 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, auto& access = cast(*e); auto res = TypeCheckExp(access.Aggregate(), types, values); auto t = res.type; - switch (t->Tag()) { + switch (t->kind()) { case Value::Kind::StructType: { const auto& struct_type = cast(*t); for (const auto& [field_name, field_type] : struct_type.fields()) { if (access.Field() == field_name) { Nonnull new_e = arena->New( - access.SourceLoc(), res.exp, access.Field()); + access.source_loc(), res.exp, access.Field()); return TCExpression(new_e, field_type, res.types); } } - FATAL_COMPILATION_ERROR(access.SourceLoc()) + FATAL_COMPILATION_ERROR(access.source_loc()) << "struct " << struct_type << " does not have a field named " << access.Field(); } @@ -407,7 +413,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, for (auto& field : t_class.Fields()) { if (access.Field() == field.first) { Nonnull new_e = arena->New( - e->SourceLoc(), res.exp, access.Field()); + e->source_loc(), res.exp, access.Field()); return TCExpression(new_e, field.second, res.types); } } @@ -415,11 +421,11 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, for (auto& method : t_class.Methods()) { if (access.Field() == method.first) { Nonnull new_e = arena->New( - e->SourceLoc(), res.exp, access.Field()); + e->source_loc(), res.exp, access.Field()); return TCExpression(new_e, method.second, res.types); } } - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "class " << t_class.Name() << " does not have a field named " << access.Field(); } @@ -428,11 +434,11 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, for (const TupleElement& field : tup.Elements()) { if (access.Field() == field.name) { auto new_e = arena->New( - e->SourceLoc(), res.exp, access.Field()); + e->source_loc(), res.exp, access.Field()); return TCExpression(new_e, field.value, res.types); } } - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "tuple " << tup << " does not have a field named " << access.Field(); } @@ -441,18 +447,18 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, for (const auto& vt : choice.Alternatives()) { if (access.Field() == vt.first) { Nonnull new_e = arena->New( - e->SourceLoc(), res.exp, access.Field()); + e->source_loc(), res.exp, access.Field()); auto fun_ty = arena->New( std::vector(), vt.second, t); return TCExpression(new_e, fun_ty, res.types); } } - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "choice " << choice.Name() << " does not have a field named " << access.Field(); } default: - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "field access, expected a struct\n" << *e; } @@ -463,7 +469,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, if (type) { return TCExpression(e, *type, types); } else { - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "could not find `" << ident.Name() << "`"; } } @@ -483,49 +489,49 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, ts.push_back(res.type); } auto new_e = - arena->New(e->SourceLoc(), op.Op(), es); + arena->New(e->source_loc(), op.Op(), es); switch (op.Op()) { case Operator::Neg: - ExpectType(e->SourceLoc(), "negation", arena->New(), ts[0]); + ExpectType(e->source_loc(), "negation", arena->New(), ts[0]); return TCExpression(new_e, arena->New(), new_types); case Operator::Add: - ExpectType(e->SourceLoc(), "addition(1)", arena->New(), + ExpectType(e->source_loc(), "addition(1)", arena->New(), ts[0]); - ExpectType(e->SourceLoc(), "addition(2)", arena->New(), + ExpectType(e->source_loc(), "addition(2)", arena->New(), ts[1]); return TCExpression(new_e, arena->New(), new_types); case Operator::Sub: - ExpectType(e->SourceLoc(), "subtraction(1)", arena->New(), + ExpectType(e->source_loc(), "subtraction(1)", arena->New(), ts[0]); - ExpectType(e->SourceLoc(), "subtraction(2)", arena->New(), + ExpectType(e->source_loc(), "subtraction(2)", arena->New(), ts[1]); return TCExpression(new_e, arena->New(), new_types); case Operator::Mul: - ExpectType(e->SourceLoc(), "multiplication(1)", arena->New(), - ts[0]); - ExpectType(e->SourceLoc(), "multiplication(2)", arena->New(), - ts[1]); + ExpectType(e->source_loc(), "multiplication(1)", + arena->New(), ts[0]); + ExpectType(e->source_loc(), "multiplication(2)", + arena->New(), ts[1]); return TCExpression(new_e, arena->New(), new_types); case Operator::And: - ExpectType(e->SourceLoc(), "&&(1)", arena->New(), ts[0]); - ExpectType(e->SourceLoc(), "&&(2)", arena->New(), ts[1]); + ExpectType(e->source_loc(), "&&(1)", arena->New(), ts[0]); + ExpectType(e->source_loc(), "&&(2)", arena->New(), ts[1]); return TCExpression(new_e, arena->New(), new_types); case Operator::Or: - ExpectType(e->SourceLoc(), "||(1)", arena->New(), ts[0]); - ExpectType(e->SourceLoc(), "||(2)", arena->New(), ts[1]); + ExpectType(e->source_loc(), "||(1)", arena->New(), ts[0]); + ExpectType(e->source_loc(), "||(2)", arena->New(), ts[1]); return TCExpression(new_e, arena->New(), new_types); case Operator::Not: - ExpectType(e->SourceLoc(), "!", arena->New(), ts[0]); + ExpectType(e->source_loc(), "!", arena->New(), ts[0]); return TCExpression(new_e, arena->New(), new_types); case Operator::Eq: - ExpectType(e->SourceLoc(), "==", ts[0], ts[1]); + ExpectType(e->source_loc(), "==", ts[0], ts[1]); return TCExpression(new_e, arena->New(), new_types); case Operator::Deref: - ExpectPointerType(e->SourceLoc(), "*", ts[0]); + ExpectPointerType(e->source_loc(), "*", ts[0]); return TCExpression(new_e, cast(*ts[0]).Type(), new_types); case Operator::Ptr: - ExpectType(e->SourceLoc(), "*", arena->New(), ts[0]); + ExpectType(e->source_loc(), "*", arena->New(), ts[0]); return TCExpression(new_e, arena->New(), new_types); } break; @@ -533,7 +539,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, case Expression::Kind::CallExpression: { auto& call = cast(*e); auto fun_res = TypeCheckExp(call.Function(), types, values); - switch (fun_res.type->Tag()) { + switch (fun_res.type->kind()) { case Value::Kind::FunctionType: { const auto& fun_t = cast(*fun_res.type); auto arg_res = TypeCheckExp(call.Argument(), fun_res.types, values); @@ -541,12 +547,12 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, auto return_type = fun_t.Ret(); if (!fun_t.Deduced().empty()) { auto deduced_args = ArgumentDeduction( - e->SourceLoc(), TypeEnv(arena), parameter_type, arg_res.type); + e->source_loc(), TypeEnv(arena), parameter_type, arg_res.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. if (!deduced_args.Get(deduced_param.name)) { - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "could not deduce type argument for type parameter " << deduced_param.name; } @@ -554,14 +560,14 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, parameter_type = Substitute(deduced_args, parameter_type); return_type = Substitute(deduced_args, return_type); } else { - ExpectType(e->SourceLoc(), "call", parameter_type, arg_res.type); + ExpectType(e->source_loc(), "call", parameter_type, arg_res.type); } - auto new_e = arena->New(e->SourceLoc(), fun_res.exp, + auto new_e = arena->New(e->source_loc(), fun_res.exp, arg_res.exp); return TCExpression(new_e, return_type, arg_res.types); } default: { - FATAL_COMPILATION_ERROR(e->SourceLoc()) + FATAL_COMPILATION_ERROR(e->source_loc()) << "in call, expected a function\n" << *e; } @@ -573,8 +579,8 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeEnv types, auto pt = interpreter.InterpExp(values, fn.Parameter()); auto rt = interpreter.InterpExp(values, fn.ReturnType()); auto new_e = arena->New( - e->SourceLoc(), ReifyType(pt, e->SourceLoc()), - ReifyType(rt, e->SourceLoc()), + e->source_loc(), ReifyType(pt, e->source_loc()), + ReifyType(rt, e->source_loc()), /*is_omitted_return_type=*/false); return TCExpression(new_e, arena->New(), types); } @@ -608,7 +614,7 @@ auto TypeChecker::TypeCheckPattern( interpreter.PrintEnv(values, llvm::outs()); llvm::outs() << "\n"; } - switch (p->Tag()) { + switch (p->kind()) { case Pattern::Kind::AutoPattern: { return {.pattern = p, .type = arena->New(), .types = types}; } @@ -620,9 +626,9 @@ auto TypeChecker::TypeCheckPattern( interpreter.InterpPattern(values, binding_type_result.pattern); if (expected) { std::optional values = interpreter.PatternMatch( - type, *expected, binding.Type()->SourceLoc()); + type, *expected, binding.Type()->source_loc()); if (values == std::nullopt) { - FATAL_COMPILATION_ERROR(binding.Type()->SourceLoc()) + FATAL_COMPILATION_ERROR(binding.Type()->source_loc()) << "Type pattern '" << *type << "' does not match actual type '" << **expected << "'"; } @@ -631,8 +637,8 @@ auto TypeChecker::TypeCheckPattern( type = *expected; } auto new_p = arena->New( - binding.SourceLoc(), binding.Name(), - arena->New(ReifyType(type, binding.SourceLoc()))); + binding.source_loc(), binding.Name(), + arena->New(ReifyType(type, binding.source_loc()))); if (binding.Name().has_value()) { types.Set(*binding.Name(), type); } @@ -643,12 +649,12 @@ auto TypeChecker::TypeCheckPattern( std::vector new_fields; std::vector field_types; auto new_types = types; - if (expected && (*expected)->Tag() != Value::Kind::TupleValue) { - FATAL_COMPILATION_ERROR(p->SourceLoc()) << "didn't expect a tuple"; + if (expected && (*expected)->kind() != Value::Kind::TupleValue) { + FATAL_COMPILATION_ERROR(p->source_loc()) << "didn't expect a tuple"; } if (expected && tuple.Fields().size() != cast(**expected).Elements().size()) { - FATAL_COMPILATION_ERROR(tuple.SourceLoc()) + FATAL_COMPILATION_ERROR(tuple.source_loc()) << "tuples of different length"; } for (size_t i = 0; i < tuple.Fields().size(); ++i) { @@ -658,7 +664,7 @@ auto TypeChecker::TypeCheckPattern( const TupleElement& expected_element = cast(**expected).Elements()[i]; if (expected_element.name != field.name) { - FATAL_COMPILATION_ERROR(tuple.SourceLoc()) + FATAL_COMPILATION_ERROR(tuple.source_loc()) << "field names do not match, expected " << expected_element.name << " but got " << field.name; } @@ -671,7 +677,7 @@ auto TypeChecker::TypeCheckPattern( TuplePattern::Field(field.name, field_result.pattern)); field_types.push_back({.name = field.name, .value = field_result.type}); } - auto new_tuple = arena->New(tuple.SourceLoc(), new_fields); + auto new_tuple = arena->New(tuple.source_loc(), new_fields); auto tuple_t = arena->New(std::move(field_types)); return {.pattern = new_tuple, .type = tuple_t, .types = new_types}; } @@ -679,19 +685,19 @@ auto TypeChecker::TypeCheckPattern( auto& alternative = cast(*p); Nonnull choice_type = interpreter.InterpExp(values, alternative.ChoiceType()); - if (choice_type->Tag() != Value::Kind::ChoiceType) { - FATAL_COMPILATION_ERROR(alternative.SourceLoc()) + if (choice_type->kind() != Value::Kind::ChoiceType) { + FATAL_COMPILATION_ERROR(alternative.source_loc()) << "alternative pattern does not name a choice type."; } if (expected) { - ExpectType(alternative.SourceLoc(), "alternative pattern", *expected, + ExpectType(alternative.source_loc(), "alternative pattern", *expected, choice_type); } std::optional> parameter_types = FindInVarValues(alternative.AlternativeName(), cast(*choice_type).Alternatives()); if (parameter_types == std::nullopt) { - FATAL_COMPILATION_ERROR(alternative.SourceLoc()) + FATAL_COMPILATION_ERROR(alternative.source_loc()) << "'" << alternative.AlternativeName() << "' is not an alternative of " << *choice_type; } @@ -702,8 +708,8 @@ auto TypeChecker::TypeCheckPattern( auto arguments = Nonnull(cast(arg_results.pattern)); return {.pattern = arena->New( - alternative.SourceLoc(), - ReifyType(choice_type, alternative.SourceLoc()), + alternative.source_loc(), + ReifyType(choice_type, alternative.source_loc()), alternative.AlternativeName(), arguments), .type = choice_type, .types = arg_results.types}; @@ -732,7 +738,7 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, Env values, Nonnull return_type_context) -> TCStatement { - switch (s->Tag()) { + switch (s->kind()) { case Statement::Kind::Match: { auto& match = cast(*s); auto res = TypeCheckExp(&match.expression(), types, values); @@ -743,18 +749,18 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, &clause.statement(), types, values, return_type_context)); } - auto new_s = arena->New(s->SourceLoc(), res.exp, new_clauses); + auto new_s = arena->New(s->source_loc(), res.exp, new_clauses); return TCStatement(new_s, types); } case Statement::Kind::While: { auto& while_stmt = cast(*s); auto cnd_res = TypeCheckExp(while_stmt.Cond(), types, values); - ExpectType(s->SourceLoc(), "condition of `while`", arena->New(), - cnd_res.type); + ExpectType(s->source_loc(), "condition of `while`", + arena->New(), cnd_res.type); auto body_res = TypeCheckStmt(while_stmt.Body(), types, values, return_type_context); auto new_s = - arena->New(s->SourceLoc(), cnd_res.exp, body_res.stmt); + arena->New(s->source_loc(), cnd_res.exp, body_res.stmt); return TCStatement(new_s, types); } case Statement::Kind::Break: @@ -765,7 +771,7 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, if (block.Stmt()) { auto stmt_res = TypeCheckStmt(*block.Stmt(), types, values, return_type_context); - return TCStatement(arena->New(s->SourceLoc(), stmt_res.stmt), + return TCStatement(arena->New(s->source_loc(), stmt_res.stmt), types); } else { return TCStatement(s, types); @@ -777,7 +783,7 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, Nonnull rhs_ty = res.type; auto lhs_res = TypeCheckPattern(var.Pat(), types, values, rhs_ty); auto new_s = - arena->New(s->SourceLoc(), var.Pat(), res.exp); + arena->New(s->source_loc(), var.Pat(), res.exp); return TCStatement(new_s, lhs_res.types); } case Statement::Kind::Sequence: { @@ -793,7 +799,7 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, checked_types = next_res.types; } return TCStatement( - arena->New(s->SourceLoc(), stmt_res.stmt, next_stmt), + arena->New(s->source_loc(), stmt_res.stmt, next_stmt), checked_types); } case Statement::Kind::Assign: { @@ -802,20 +808,21 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, auto rhs_t = rhs_res.type; auto lhs_res = TypeCheckExp(assign.Lhs(), types, values); auto lhs_t = lhs_res.type; - ExpectType(s->SourceLoc(), "assign", lhs_t, rhs_t); - auto new_s = arena->New(s->SourceLoc(), lhs_res.exp, rhs_res.exp); + ExpectType(s->source_loc(), "assign", lhs_t, rhs_t); + auto new_s = + arena->New(s->source_loc(), lhs_res.exp, rhs_res.exp); return TCStatement(new_s, lhs_res.types); } case Statement::Kind::ExpressionStatement: { auto res = TypeCheckExp(cast(*s).Exp(), types, values); - auto new_s = arena->New(s->SourceLoc(), res.exp); + auto new_s = arena->New(s->source_loc(), res.exp); return TCStatement(new_s, types); } case Statement::Kind::If: { auto& if_stmt = cast(*s); auto cnd_res = TypeCheckExp(if_stmt.Cond(), types, values); - ExpectType(s->SourceLoc(), "condition of `if`", arena->New(), + ExpectType(s->source_loc(), "condition of `if`", arena->New(), cnd_res.type); auto then_res = TypeCheckStmt(if_stmt.ThenStmt(), types, values, return_type_context); @@ -825,8 +832,8 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, return_type_context); else_stmt = else_res.stmt; } - auto new_s = - arena->New(s->SourceLoc(), cnd_res.exp, then_res.stmt, else_stmt); + auto new_s = arena->New(s->source_loc(), cnd_res.exp, then_res.stmt, + else_stmt); return TCStatement(new_s, types); } case Statement::Kind::Return: { @@ -835,7 +842,7 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, if (return_type_context->is_auto()) { if (return_type_context->deduced_return_type()) { // Only one return is allowed when the return type is `auto`. - FATAL_COMPILATION_ERROR(s->SourceLoc()) + FATAL_COMPILATION_ERROR(s->source_loc()) << "Only one return is allowed in a function with an `auto` " "return type."; } else { @@ -843,17 +850,17 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, return_type_context->set_deduced_return_type(res.type); } } else { - ExpectType(s->SourceLoc(), "return", + ExpectType(s->source_loc(), "return", *return_type_context->deduced_return_type(), res.type); } if (ret.IsOmittedExp() != return_type_context->is_omitted()) { - FATAL_COMPILATION_ERROR(s->SourceLoc()) + FATAL_COMPILATION_ERROR(s->source_loc()) << *s << " should" << (return_type_context->is_omitted() ? " not" : "") << " provide a return value, to match the function's signature."; } return TCStatement( - arena->New(s->SourceLoc(), res.exp, ret.IsOmittedExp()), + arena->New(s->source_loc(), res.exp, ret.IsOmittedExp()), types); } case Statement::Kind::Continuation: { @@ -861,16 +868,16 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, TCStatement body_result = TypeCheckStmt(cont.Body(), types, values, return_type_context); auto new_continuation = arena->New( - s->SourceLoc(), cont.ContinuationVariable(), body_result.stmt); + s->source_loc(), cont.ContinuationVariable(), body_result.stmt); types.Set(cont.ContinuationVariable(), arena->New()); return TCStatement(new_continuation, types); } case Statement::Kind::Run: { TCExpression argument_result = TypeCheckExp(cast(*s).Argument(), types, values); - ExpectType(s->SourceLoc(), "argument of `run`", + ExpectType(s->source_loc(), "argument of `run`", arena->New(), argument_result.type); - auto new_run = arena->New(s->SourceLoc(), argument_result.exp); + auto new_run = arena->New(s->source_loc(), argument_result.exp); return TCStatement(new_run, types); } case Statement::Kind::Await: { @@ -882,42 +889,42 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, TypeEnv types, auto TypeChecker::CheckOrEnsureReturn( std::optional> opt_stmt, bool omitted_ret_type, - SourceLocation loc) -> Nonnull { + SourceLocation source_loc) -> Nonnull { if (!opt_stmt) { if (omitted_ret_type) { - return arena->New(arena, loc); + return arena->New(arena, source_loc); } else { - FATAL_COMPILATION_ERROR(loc) + FATAL_COMPILATION_ERROR(source_loc) << "control-flow reaches end of function that provides a `->` return " "type without reaching a return statement"; } } Nonnull stmt = *opt_stmt; - switch (stmt->Tag()) { + switch (stmt->kind()) { case Statement::Kind::Match: { auto& match = cast(*stmt); std::vector new_clauses; for (auto& clause : match.clauses()) { auto s = CheckOrEnsureReturn(&clause.statement(), omitted_ret_type, - stmt->SourceLoc()); + stmt->source_loc()); new_clauses.push_back(Match::Clause(&clause.pattern(), s)); } - return arena->New(stmt->SourceLoc(), &match.expression(), + return arena->New(stmt->source_loc(), &match.expression(), new_clauses); } case Statement::Kind::Block: return arena->New( - stmt->SourceLoc(), + stmt->source_loc(), CheckOrEnsureReturn(cast(*stmt).Stmt(), omitted_ret_type, - stmt->SourceLoc())); + stmt->source_loc())); case Statement::Kind::If: { auto& if_stmt = cast(*stmt); return arena->New( - stmt->SourceLoc(), if_stmt.Cond(), + stmt->source_loc(), if_stmt.Cond(), CheckOrEnsureReturn(if_stmt.ThenStmt(), omitted_ret_type, - stmt->SourceLoc()), + stmt->source_loc()), CheckOrEnsureReturn(if_stmt.ElseStmt(), omitted_ret_type, - stmt->SourceLoc())); + stmt->source_loc())); } case Statement::Kind::Return: return stmt; @@ -925,12 +932,12 @@ auto TypeChecker::CheckOrEnsureReturn( auto& seq = cast(*stmt); if (seq.Next()) { return arena->New( - stmt->SourceLoc(), seq.Stmt(), + stmt->source_loc(), seq.Stmt(), CheckOrEnsureReturn(seq.Next(), omitted_ret_type, - stmt->SourceLoc())); + stmt->source_loc())); } else { return CheckOrEnsureReturn(seq.Stmt(), omitted_ret_type, - stmt->SourceLoc()); + stmt->source_loc()); } } case Statement::Kind::Continuation: @@ -944,10 +951,10 @@ auto TypeChecker::CheckOrEnsureReturn( case Statement::Kind::Continue: case Statement::Kind::VariableDefinition: if (omitted_ret_type) { - return arena->New(stmt->SourceLoc(), stmt, - arena->New(arena, loc)); + return arena->New(stmt->source_loc(), stmt, + arena->New(arena, source_loc)); } else { - FATAL_COMPILATION_ERROR(stmt->SourceLoc()) + FATAL_COMPILATION_ERROR(stmt->source_loc()) << "control-flow reaches end of function that provides a `->` " "return type without reaching a return statement"; } @@ -1010,7 +1017,7 @@ auto TypeChecker::TypeOfFunDef(TypeEnv types, Env values, TypeCheckPattern(&fun_def->param_pattern(), types, values, std::nullopt); // Evaluate the return type expression auto ret = interpreter.InterpPattern(values, &fun_def->return_type()); - if (ret->Tag() == Value::Kind::AutoType) { + if (ret->kind() == Value::Kind::AutoType) { auto f = TypeCheckFunDef(fun_def, types, values); ret = interpreter.InterpPattern(values, &f->return_type()); } @@ -1023,17 +1030,17 @@ auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/, VarValues fields; VarValues methods; for (Nonnull m : sd->members()) { - switch (m->Tag()) { + switch (m->kind()) { case Member::Kind::FieldMember: { Nonnull binding = cast(*m).Binding(); if (!binding->Name().has_value()) { - FATAL_COMPILATION_ERROR(binding->SourceLoc()) + FATAL_COMPILATION_ERROR(binding->source_loc()) << "Struct members must have names"; } const auto* binding_type = dyn_cast(binding->Type()); if (binding_type == nullptr) { - FATAL_COMPILATION_ERROR(binding->SourceLoc()) + FATAL_COMPILATION_ERROR(binding->source_loc()) << "Struct members must have explicit types"; } auto type = interpreter.InterpExp(ct_top, binding_type->Expression()); @@ -1057,7 +1064,7 @@ static auto GetName(const Declaration& d) -> const std::string& { case Declaration::Kind::VariableDeclaration: { const BindingPattern& binding = cast(d).binding(); if (!binding.Name().has_value()) { - FATAL_COMPILATION_ERROR(binding.SourceLoc()) + FATAL_COMPILATION_ERROR(binding.source_loc()) << "Top-level variable declarations must have names"; } return *binding.Name(); @@ -1077,7 +1084,7 @@ auto TypeChecker::MakeTypeChecked(Nonnull d, const TypeEnv& types, cast(*d).definition(); std::vector> fields; for (Nonnull m : class_def.members()) { - switch (m->Tag()) { + switch (m->kind()) { case Member::Kind::FieldMember: // TODO: Interpret the type expression and store the result. fields.push_back(m); diff --git a/executable_semantics/interpreter/type_checker.h b/executable_semantics/interpreter/type_checker.h index 65104561403a..7eb80a2e4c19 100644 --- a/executable_semantics/interpreter/type_checker.h +++ b/executable_semantics/interpreter/type_checker.h @@ -140,11 +140,11 @@ class TypeChecker { void TopLevel(Nonnull d, TypeCheckContext* tops); auto CheckOrEnsureReturn(std::optional> opt_stmt, - bool omitted_ret_type, SourceLocation loc) + bool omitted_ret_type, SourceLocation source_loc) -> Nonnull; // Reify type to type expression. - auto ReifyType(Nonnull t, SourceLocation loc) + auto ReifyType(Nonnull t, SourceLocation source_loc) -> Nonnull; auto Substitute(TypeEnv dict, Nonnull type) diff --git a/executable_semantics/interpreter/value.cpp b/executable_semantics/interpreter/value.cpp index a13d972f3410..27d0c27c150d 100644 --- a/executable_semantics/interpreter/value.cpp +++ b/executable_semantics/interpreter/value.cpp @@ -67,14 +67,14 @@ auto TupleValue::FindField(const std::string& name) const namespace { auto GetMember(Nonnull arena, Nonnull v, - const std::string& f, SourceLocation loc) + const std::string& f, SourceLocation source_loc) -> Nonnull { - switch (v->Tag()) { + switch (v->kind()) { case Value::Kind::StructValue: { std::optional> field = cast(*v).FindField(f); if (field == std::nullopt) { - FATAL_RUNTIME_ERROR(loc) << "member " << f << " not in " << *v; + FATAL_RUNTIME_ERROR(source_loc) << "member " << f << " not in " << *v; } return *field; } @@ -82,7 +82,7 @@ auto GetMember(Nonnull arena, Nonnull v, std::optional> field = cast(*cast(*v).Inits()).FindField(f); if (field == std::nullopt) { - FATAL_RUNTIME_ERROR(loc) << "member " << f << " not in " << *v; + FATAL_RUNTIME_ERROR(source_loc) << "member " << f << " not in " << *v; } return *field; } @@ -90,14 +90,15 @@ auto GetMember(Nonnull arena, Nonnull v, std::optional> field = cast(*v).FindField(f); if (!field) { - FATAL_RUNTIME_ERROR(loc) << "field " << f << " not in " << *v; + FATAL_RUNTIME_ERROR(source_loc) << "field " << f << " not in " << *v; } return *field; } case Value::Kind::ChoiceType: { const auto& choice = cast(*v); if (!FindInVarValues(f, choice.Alternatives())) { - FATAL_RUNTIME_ERROR(loc) << "alternative " << f << " not in " << *v; + FATAL_RUNTIME_ERROR(source_loc) + << "alternative " << f << " not in " << *v; } return arena->New(f, choice.Name()); } @@ -109,10 +110,10 @@ auto GetMember(Nonnull arena, Nonnull v, } // namespace auto Value::GetField(Nonnull arena, const FieldPath& path, - SourceLocation loc) const -> Nonnull { + SourceLocation source_loc) const -> Nonnull { Nonnull value(this); for (const std::string& field : path.components) { - value = GetMember(arena, value, field, loc); + value = GetMember(arena, value, field, source_loc); } return value; } @@ -122,12 +123,12 @@ namespace { auto SetFieldImpl(Nonnull arena, Nonnull value, std::vector::const_iterator path_begin, std::vector::const_iterator path_end, - Nonnull field_value, SourceLocation loc) + Nonnull field_value, SourceLocation source_loc) -> Nonnull { if (path_begin == path_end) { return field_value; } - switch (value->Tag()) { + switch (value->kind()) { case Value::Kind::StructValue: { std::vector elements = cast(*value).elements(); auto it = std::find_if(elements.begin(), elements.end(), @@ -135,16 +136,16 @@ auto SetFieldImpl(Nonnull arena, Nonnull value, return element.name == *path_begin; }); if (it == elements.end()) { - FATAL_RUNTIME_ERROR(loc) + FATAL_RUNTIME_ERROR(source_loc) << "field " << *path_begin << " not in " << *value; } it->value = SetFieldImpl(arena, it->value, path_begin + 1, path_end, - field_value, loc); + field_value, source_loc); return arena->New(elements); } case Value::Kind::NominalClassValue: { return SetFieldImpl(arena, cast(*value).Inits(), - path_begin, path_end, field_value, loc); + path_begin, path_end, field_value, source_loc); } case Value::Kind::TupleValue: { std::vector elements = cast(*value).Elements(); @@ -153,11 +154,11 @@ auto SetFieldImpl(Nonnull arena, Nonnull value, return element.name == *path_begin; }); if (it == elements.end()) { - FATAL_RUNTIME_ERROR(loc) + FATAL_RUNTIME_ERROR(source_loc) << "field " << *path_begin << " not in " << *value; } it->value = SetFieldImpl(arena, it->value, path_begin + 1, path_end, - field_value, loc); + field_value, source_loc); return arena->New(elements); } default: @@ -169,14 +170,14 @@ auto SetFieldImpl(Nonnull arena, Nonnull value, auto Value::SetField(Nonnull arena, const FieldPath& path, Nonnull field_value, - SourceLocation loc) const -> Nonnull { + SourceLocation source_loc) const -> Nonnull { return SetFieldImpl(arena, Nonnull(this), path.components.begin(), path.components.end(), - field_value, loc); + field_value, source_loc); } void Value::Print(llvm::raw_ostream& out) const { - switch (Tag()) { + switch (kind()) { case Value::Kind::AlternativeConstructorValue: { const auto& alt = cast(*this); out << alt.ChoiceName() << "." << alt.AltName(); @@ -309,32 +310,34 @@ void Value::Print(llvm::raw_ostream& out) const { } auto CopyVal(Nonnull arena, Nonnull val, - SourceLocation loc) -> Nonnull { - switch (val->Tag()) { + SourceLocation source_loc) -> Nonnull { + switch (val->kind()) { case Value::Kind::TupleValue: { std::vector elements; for (const TupleElement& element : cast(*val).Elements()) { - elements.push_back({.name = element.name, - .value = CopyVal(arena, element.value, loc)}); + elements.push_back( + {.name = element.name, + .value = CopyVal(arena, element.value, source_loc)}); } return arena->New(std::move(elements)); } case Value::Kind::AlternativeValue: { const auto& alt = cast(*val); - Nonnull arg = CopyVal(arena, alt.Argument(), loc); + Nonnull arg = CopyVal(arena, alt.Argument(), source_loc); return arena->New(alt.AltName(), alt.ChoiceName(), arg); } case Value::Kind::StructValue: { std::vector elements; for (const TupleElement& element : cast(*val).elements()) { - elements.push_back({.name = element.name, - .value = CopyVal(arena, element.value, loc)}); + elements.push_back( + {.name = element.name, + .value = CopyVal(arena, element.value, source_loc)}); } return arena->New(std::move(elements)); } case Value::Kind::NominalClassValue: { const auto& s = cast(*val); - Nonnull inits = CopyVal(arena, s.Inits(), loc); + Nonnull inits = CopyVal(arena, s.Inits(), source_loc); return arena->New(s.Type(), inits); } case Value::Kind::IntValue: @@ -353,13 +356,13 @@ auto CopyVal(Nonnull arena, Nonnull val, return val; case Value::Kind::FunctionType: { const auto& fn_type = cast(*val); - return arena->New(fn_type.Deduced(), - CopyVal(arena, fn_type.Param(), loc), - CopyVal(arena, fn_type.Ret(), loc)); + return arena->New( + fn_type.Deduced(), CopyVal(arena, fn_type.Param(), source_loc), + CopyVal(arena, fn_type.Ret(), source_loc)); } case Value::Kind::PointerType: return arena->New( - CopyVal(arena, cast(*val).Type(), loc)); + CopyVal(arena, cast(*val).Type(), source_loc)); case Value::Kind::IntType: return arena->New(); case Value::Kind::BoolType: @@ -377,7 +380,7 @@ auto CopyVal(Nonnull arena, Nonnull val, case Value::Kind::StructType: { VarValues fields; for (const auto& [name, type] : cast(*val).fields()) { - fields.push_back({name, CopyVal(arena, type, loc)}); + fields.push_back({name, CopyVal(arena, type, source_loc)}); } return arena->New(fields); } @@ -392,10 +395,10 @@ auto CopyVal(Nonnull arena, Nonnull val, } auto TypeEqual(Nonnull t1, Nonnull t2) -> bool { - if (t1->Tag() != t2->Tag()) { + if (t1->kind() != t2->kind()) { return false; } - switch (t1->Tag()) { + switch (t1->kind()) { case Value::Kind::PointerType: return TypeEqual(cast(*t1).Type(), cast(*t2).Type()); @@ -458,7 +461,7 @@ auto TypeEqual(Nonnull t1, Nonnull t2) -> bool { // and returns false otherwise. static auto FieldsValueEqual(const std::vector& ts1, const std::vector& ts2, - SourceLocation loc) -> bool { + SourceLocation source_loc) -> bool { if (ts1.size() != ts2.size()) { return false; } @@ -469,7 +472,7 @@ static auto FieldsValueEqual(const std::vector& ts1, if (iter == ts2.end()) { return false; } - if (!ValueEqual(element.value, iter->value, loc)) { + if (!ValueEqual(element.value, iter->value, source_loc)) { return false; } } @@ -480,11 +483,11 @@ static auto FieldsValueEqual(const std::vector& ts1, // // This function implements the `==` operator of Carbon. auto ValueEqual(Nonnull v1, Nonnull v2, - SourceLocation loc) -> bool { - if (v1->Tag() != v2->Tag()) { + SourceLocation source_loc) -> bool { + if (v1->kind() != v2->kind()) { return false; } - switch (v1->Tag()) { + switch (v1->kind()) { case Value::Kind::IntValue: return cast(*v1).Val() == cast(*v2).Val(); case Value::Kind::BoolValue: @@ -501,10 +504,10 @@ auto ValueEqual(Nonnull v1, Nonnull v2, } case Value::Kind::TupleValue: return FieldsValueEqual(cast(*v1).Elements(), - cast(*v2).Elements(), loc); + cast(*v2).Elements(), source_loc); case Value::Kind::StructValue: return FieldsValueEqual(cast(*v1).elements(), - cast(*v2).elements(), loc); + cast(*v2).elements(), source_loc); case Value::Kind::StringValue: return cast(*v1).Val() == cast(*v2).Val(); case Value::Kind::IntType: diff --git a/executable_semantics/interpreter/value.h b/executable_semantics/interpreter/value.h index c079406f1f52..7c28dfe8a9e7 100644 --- a/executable_semantics/interpreter/value.h +++ b/executable_semantics/interpreter/value.h @@ -61,31 +61,31 @@ class Value { Value(const Value&) = delete; Value& operator=(const Value&) = delete; - // Returns the enumerator corresponding to the most-derived type of this - // object. - auto Tag() const -> Kind { return kind; } - void Print(llvm::raw_ostream& out) const; LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } // Returns the sub-Value specified by `path`, which must be a valid field // path for *this. auto GetField(Nonnull arena, const FieldPath& path, - SourceLocation loc) const -> Nonnull; + SourceLocation source_loc) const -> Nonnull; // Returns a copy of *this, but with the sub-Value specified by `path` // set to `field_value`. `path` must be a valid field path for *this. auto SetField(Nonnull arena, const FieldPath& path, - Nonnull field_value, SourceLocation loc) const - -> Nonnull; + Nonnull field_value, + SourceLocation source_loc) const -> Nonnull; + + // Returns the enumerator corresponding to the most-derived type of this + // object. + auto kind() const -> Kind { return kind_; } protected: - // Constructs a Value. `tag` must be the enumerator corresponding to the + // Constructs a Value. `kind` must be the enumerator corresponding to the // most-derived type being constructed. - explicit Value(Kind kind) : kind(kind) {} + explicit Value(Kind kind) : kind_(kind) {} private: - const Kind kind; + const Kind kind_; }; using VarValues = std::vector>>; @@ -115,7 +115,7 @@ class IntValue : public Value { explicit IntValue(int val) : Value(Kind::IntValue), val(val) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::IntValue; + return value->kind() == Kind::IntValue; } auto Val() const -> int { return val; } @@ -135,7 +135,7 @@ class FunctionValue : public Value { body(body) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::FunctionValue; + return value->kind() == Kind::FunctionValue; } auto Name() const -> const std::string& { return name; } @@ -155,7 +155,7 @@ class PointerValue : public Value { : Value(Kind::PointerValue), val(std::move(val)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::PointerValue; + return value->kind() == Kind::PointerValue; } auto Val() const -> const Address& { return val; } @@ -170,7 +170,7 @@ class BoolValue : public Value { explicit BoolValue(bool val) : Value(Kind::BoolValue), val(val) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::BoolValue; + return value->kind() == Kind::BoolValue; } auto Val() const -> bool { return val; } @@ -195,7 +195,7 @@ class StructValue : public Value { } static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::StructValue; + return value->kind() == Kind::StructValue; } auto elements() const -> const std::vector& { @@ -218,7 +218,7 @@ class NominalClassValue : public Value { : Value(Kind::NominalClassValue), type(type), inits(inits) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::NominalClassValue; + return value->kind() == Kind::NominalClassValue; } auto Type() const -> Nonnull { return type; } @@ -238,7 +238,7 @@ class AlternativeConstructorValue : public Value { choice_name(std::move(choice_name)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::AlternativeConstructorValue; + return value->kind() == Kind::AlternativeConstructorValue; } auto AltName() const -> const std::string& { return alt_name; } @@ -260,7 +260,7 @@ class AlternativeValue : public Value { argument(argument) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::AlternativeValue; + return value->kind() == Kind::AlternativeValue; } auto AltName() const -> const std::string& { return alt_name; } @@ -286,7 +286,7 @@ class TupleValue : public Value { : Value(Kind::TupleValue), elements(std::move(elements)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::TupleValue; + return value->kind() == Kind::TupleValue; } auto Elements() const -> const std::vector& { return elements; } @@ -311,7 +311,7 @@ class BindingPlaceholderValue : public Value { type(type) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::BindingPlaceholderValue; + return value->kind() == Kind::BindingPlaceholderValue; } auto Name() const -> const std::optional& { return name; } @@ -328,7 +328,7 @@ class IntType : public Value { IntType() : Value(Kind::IntType) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::IntType; + return value->kind() == Kind::IntType; } }; @@ -338,7 +338,7 @@ class BoolType : public Value { BoolType() : Value(Kind::BoolType) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::BoolType; + return value->kind() == Kind::BoolType; } }; @@ -348,7 +348,7 @@ class TypeType : public Value { TypeType() : Value(Kind::TypeType) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::TypeType; + return value->kind() == Kind::TypeType; } }; @@ -363,7 +363,7 @@ class FunctionType : public Value { ret(ret) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::FunctionType; + return value->kind() == Kind::FunctionType; } auto Deduced() const -> const std::vector& { return deduced; } @@ -383,7 +383,7 @@ class PointerType : public Value { : Value(Kind::PointerType), type(type) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::PointerType; + return value->kind() == Kind::PointerType; } auto Type() const -> Nonnull { return type; } @@ -398,7 +398,7 @@ class AutoType : public Value { AutoType() : Value(Kind::AutoType) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::AutoType; + return value->kind() == Kind::AutoType; } }; @@ -414,7 +414,7 @@ class StructType : public Value { : Value(Kind::StructType), fields_(std::move(fields)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::StructType; + return value->kind() == Kind::StructType; } auto fields() const -> const VarValues& { return fields_; } @@ -433,7 +433,7 @@ class NominalClassType : public Value { methods(std::move(methods)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::NominalClassType; + return value->kind() == Kind::NominalClassType; } auto Name() const -> const std::string& { return name; } @@ -455,7 +455,7 @@ class ChoiceType : public Value { alternatives(std::move(alternatives)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::ChoiceType; + return value->kind() == Kind::ChoiceType; } auto Name() const -> const std::string& { return name; } @@ -472,7 +472,7 @@ class ContinuationType : public Value { ContinuationType() : Value(Kind::ContinuationType) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::ContinuationType; + return value->kind() == Kind::ContinuationType; } }; @@ -483,7 +483,7 @@ class VariableType : public Value { : Value(Kind::VariableType), name(std::move(name)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::VariableType; + return value->kind() == Kind::VariableType; } auto Name() const -> const std::string& { return name; } @@ -499,7 +499,7 @@ class ContinuationValue : public Value { : Value(Kind::ContinuationValue), stack(std::move(stack)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::ContinuationValue; + return value->kind() == Kind::ContinuationValue; } auto Stack() const -> const std::vector>& { return stack; } @@ -514,7 +514,7 @@ class StringType : public Value { StringType() : Value(Kind::StringType) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::StringType; + return value->kind() == Kind::StringType; } }; @@ -525,7 +525,7 @@ class StringValue : public Value { : Value(Kind::StringValue), val(std::move(val)) {} static auto classof(const Value* value) -> bool { - return value->Tag() == Kind::StringValue; + return value->kind() == Kind::StringValue; } auto Val() const -> const std::string& { return val; } @@ -535,11 +535,11 @@ class StringValue : public Value { }; auto CopyVal(Nonnull arena, Nonnull val, - SourceLocation loc) -> Nonnull; + SourceLocation source_loc) -> Nonnull; auto TypeEqual(Nonnull t1, Nonnull t2) -> bool; auto ValueEqual(Nonnull v1, Nonnull v2, - SourceLocation loc) -> bool; + SourceLocation source_loc) -> bool; } // namespace Carbon diff --git a/executable_semantics/syntax/lexer.lpp b/executable_semantics/syntax/lexer.lpp index cd7e99afec93..cf4f34665ce7 100644 --- a/executable_semantics/syntax/lexer.lpp +++ b/executable_semantics/syntax/lexer.lpp @@ -251,7 +251,7 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\" // "Reading a token: ". llvm::errs() << "\n"; } - FATAL_COMPILATION_ERROR(context.SourceLoc()) + FATAL_COMPILATION_ERROR(context.source_loc()) << "Invalid escaping in string: " << yytext; } return ARG_TOKEN(string_literal, *unescaped); @@ -284,7 +284,7 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\" // "Reading a token: ". llvm::errs() << "\n"; } - FATAL_COMPILATION_ERROR(context.SourceLoc()) + FATAL_COMPILATION_ERROR(context.source_loc()) << "invalid character '\\x" << llvm::toHex(llvm::StringRef(yytext, 1)) << "' in source file."; } diff --git a/executable_semantics/syntax/parse_and_lex_context.cpp b/executable_semantics/syntax/parse_and_lex_context.cpp index a141c2b2eb0b..b4d26de184be 100644 --- a/executable_semantics/syntax/parse_and_lex_context.cpp +++ b/executable_semantics/syntax/parse_and_lex_context.cpp @@ -10,7 +10,7 @@ auto ParseAndLexContext::PrintDiagnostic(const std::string& message) -> void { // TODO: Do we really want this to be fatal? It makes the comment and the // name a lie, and renders some of the other yyparse() result propagation code // moot. - FATAL_COMPILATION_ERROR(SourceLoc()) << message; + FATAL_COMPILATION_ERROR(source_loc()) << message; } } // namespace Carbon diff --git a/executable_semantics/syntax/parse_and_lex_context.h b/executable_semantics/syntax/parse_and_lex_context.h index 5a7295b21356..7df72a2bd579 100644 --- a/executable_semantics/syntax/parse_and_lex_context.h +++ b/executable_semantics/syntax/parse_and_lex_context.h @@ -23,7 +23,7 @@ class ParseAndLexContext { // Writes a syntax error diagnostic containing message to standard error. auto PrintDiagnostic(const std::string& message) -> void; - auto SourceLoc() -> SourceLocation { + auto source_loc() -> SourceLocation { return SourceLocation(input_file_name, static_cast(current_token_position.begin.line)); } diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index 7d2ed8804d53..e09db7721cb2 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -275,126 +275,126 @@ api_or_impl: ; expression: identifier - { $$ = arena->New(context.SourceLoc(), $1); } + { $$ = arena->New(context.source_loc(), $1); } | expression designator - { $$ = arena->New(context.SourceLoc(), $1, $2); } + { $$ = arena->New(context.source_loc(), $1, $2); } | expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET - { $$ = arena->New(context.SourceLoc(), $1, $3); } + { $$ = arena->New(context.source_loc(), $1, $3); } | integer_literal - { $$ = arena->New(context.SourceLoc(), $1); } + { $$ = arena->New(context.source_loc(), $1); } | string_literal - { $$ = arena->New(context.SourceLoc(), $1); } + { $$ = arena->New(context.source_loc(), $1); } | TRUE - { $$ = arena->New(context.SourceLoc(), true); } + { $$ = arena->New(context.source_loc(), true); } | FALSE - { $$ = arena->New(context.SourceLoc(), false); } + { $$ = arena->New(context.source_loc(), false); } | sized_type_literal { int val; CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val)); CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1; - $$ = arena->New(context.SourceLoc()); + $$ = arena->New(context.source_loc()); } | STRING - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | BOOL - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | TYPE - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | CONTINUATION_TYPE - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | paren_expression { $$ = $1; } | struct_literal { $$ = $1; } | struct_type_literal { $$ = $1; } | expression EQUAL_EQUAL expression { $$ = arena->New( - context.SourceLoc(), Operator::Eq, + context.source_loc(), Operator::Eq, std::vector>({$1, $3})); } | expression PLUS expression { $$ = arena->New( - context.SourceLoc(), Operator::Add, + context.source_loc(), Operator::Add, std::vector>({$1, $3})); } | expression MINUS expression { $$ = arena->New( - context.SourceLoc(), Operator::Sub, + context.source_loc(), Operator::Sub, std::vector>({$1, $3})); } | expression BINARY_STAR expression { $$ = arena->New( - context.SourceLoc(), Operator::Mul, + context.source_loc(), Operator::Mul, std::vector>({$1, $3})); } | expression AND expression { $$ = arena->New( - context.SourceLoc(), Operator::And, + context.source_loc(), Operator::And, std::vector>({$1, $3})); } | expression OR expression { $$ = arena->New( - context.SourceLoc(), Operator::Or, + context.source_loc(), Operator::Or, std::vector>({$1, $3})); } | NOT expression { $$ = arena->New( - context.SourceLoc(), Operator::Not, + context.source_loc(), Operator::Not, std::vector>({$2})); } | MINUS expression %prec UNARY_MINUS { $$ = arena->New( - context.SourceLoc(), Operator::Neg, + context.source_loc(), Operator::Neg, std::vector>({$2})); } | PREFIX_STAR expression { $$ = arena->New( - context.SourceLoc(), Operator::Deref, + context.source_loc(), Operator::Deref, std::vector>({$2})); } | UNARY_STAR expression %prec PREFIX_STAR { $$ = arena->New( - context.SourceLoc(), Operator::Deref, + context.source_loc(), Operator::Deref, std::vector>({$2})); } | expression tuple - { $$ = arena->New(context.SourceLoc(), $1, $2); } + { $$ = arena->New(context.source_loc(), $1, $2); } | expression POSTFIX_STAR { $$ = arena->New( - context.SourceLoc(), Operator::Ptr, + context.source_loc(), Operator::Ptr, std::vector>({$1})); } | expression UNARY_STAR { $$ = arena->New( - context.SourceLoc(), Operator::Ptr, + context.source_loc(), Operator::Ptr, std::vector>({$1})); } | FNTY tuple return_type { auto [return_exp, is_omitted_exp] = $3; - $$ = arena->New(context.SourceLoc(), $2, return_exp, + $$ = arena->New(context.source_loc(), $2, return_exp, is_omitted_exp); } ; designator: PERIOD identifier { $$ = $2; } ; paren_expression: paren_expression_base - { $$ = ExpressionFromParenContents(arena, context.SourceLoc(), $1); } + { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); } ; tuple: paren_expression_base - { $$ = TupleExpressionFromParenContents(arena, context.SourceLoc(), $1); } + { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); } ; paren_expression_element: expression @@ -425,9 +425,9 @@ paren_expression_contents: struct_literal: LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } ; struct_literal_contents: designator EQUAL expression @@ -441,11 +441,11 @@ struct_literal_contents: struct_type_literal: LEFT_CURLY_BRACE RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } ; struct_type_literal_contents: designator COLON expression @@ -471,20 +471,20 @@ pattern: ; non_expression_pattern: AUTO - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | binding_lhs COLON pattern - { $$ = arena->New(context.SourceLoc(), $1, $3); } + { $$ = arena->New(context.source_loc(), $1, $3); } | paren_pattern { $$ = $1; } | expression tuple_pattern - { $$ = arena->New(context.SourceLoc(), $1, $2); } + { $$ = arena->New(context.source_loc(), $1, $2); } ; binding_lhs: identifier { $$ = $1; } | UNDERSCORE { $$ = std::nullopt; } ; paren_pattern: paren_pattern_base - { $$ = PatternFromParenContents(arena, context.SourceLoc(), $1); } + { $$ = PatternFromParenContents(arena, context.source_loc(), $1); } ; paren_pattern_base: LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS @@ -527,7 +527,7 @@ paren_pattern_element: { $$ = {.name = $1, .term = $3}; } ; tuple_pattern: paren_pattern_base - { $$ = TuplePatternFromParenContents(arena, context.SourceLoc(), $1); } + { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); } ; // Unlike most `pattern` nonterminals, this one overlaps with `expression`, // so it should be used only when prior context (such as an introducer) @@ -535,7 +535,7 @@ tuple_pattern: paren_pattern_base maybe_empty_tuple_pattern: LEFT_PARENTHESIS RIGHT_PARENTHESIS { - $$ = arena->New(context.SourceLoc(), + $$ = arena->New(context.source_loc(), std::vector()); } | tuple_pattern @@ -548,8 +548,8 @@ clause: | DEFAULT DOUBLE_ARROW statement { $$ = Match::Clause(arena->New( - context.SourceLoc(), std::nullopt, - arena->New(context.SourceLoc())), + context.source_loc(), std::nullopt, + arena->New(context.source_loc())), $3); } ; @@ -564,23 +564,23 @@ clause_list: ; statement: expression EQUAL expression SEMICOLON - { $$ = arena->New(context.SourceLoc(), $1, $3); } + { $$ = arena->New(context.source_loc(), $1, $3); } | VAR pattern EQUAL expression SEMICOLON - { $$ = arena->New(context.SourceLoc(), $2, $4); } + { $$ = arena->New(context.source_loc(), $2, $4); } | expression SEMICOLON - { $$ = arena->New(context.SourceLoc(), $1); } + { $$ = arena->New(context.source_loc(), $1); } | if_statement { $$ = $1; } | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block - { $$ = arena->New(context.SourceLoc(), $3, $5); } + { $$ = arena->New(context.source_loc(), $3, $5); } | BREAK SEMICOLON - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | CONTINUE SEMICOLON - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } | RETURN return_expression SEMICOLON { auto [return_exp, is_omitted_exp] = $2; - $$ = arena->New(context.SourceLoc(), return_exp, is_omitted_exp); + $$ = arena->New(context.source_loc(), return_exp, is_omitted_exp); } // We disallow empty blocks in places where an arbitrary statement can occur // in order to avoid ambiguity with the empty struct literal `{}`. We can @@ -593,17 +593,17 @@ statement: { $$ = $1; } | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE clause_list RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $3, $6); } + { $$ = arena->New(context.source_loc(), $3, $6); } | CONTINUATION identifier statement - { $$ = arena->New(context.SourceLoc(), $2, $3); } + { $$ = arena->New(context.source_loc(), $2, $3); } | RUN expression SEMICOLON - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } | AWAIT SEMICOLON - { $$ = arena->New(context.SourceLoc()); } + { $$ = arena->New(context.source_loc()); } ; if_statement: IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else - { $$ = arena->New(context.SourceLoc(), $3, $5, $6); } + { $$ = arena->New(context.source_loc(), $3, $5, $6); } ; optional_else: // Empty @@ -615,7 +615,7 @@ optional_else: ; return_expression: // Empty - { $$ = {arena->New(context.SourceLoc()), true}; } + { $$ = {arena->New(context.source_loc()), true}; } | expression { $$ = {$1, false}; } ; @@ -627,18 +627,18 @@ statement_list: ; nonempty_statement_list: statement statement_list - { $$ = arena->New(context.SourceLoc(), $1, $2); } + { $$ = arena->New(context.source_loc(), $1, $2); } ; block: LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } ; nonempty_block: LEFT_CURLY_BRACE nonempty_statement_list RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } return_type: // Empty - { $$ = {arena->New(context.SourceLoc()), true}; } + { $$ = {arena->New(context.source_loc()), true}; } | ARROW expression %prec FNARROW { $$ = {$2, false}; } ; @@ -671,15 +671,15 @@ function_definition: { auto [return_exp, is_omitted_exp] = $5; $$ = arena->New( - context.SourceLoc(), $2, $3, $4, + context.source_loc(), $2, $3, $4, arena->New(return_exp), is_omitted_exp, $6); } | FN identifier deduced_params maybe_empty_tuple_pattern ARROW AUTO block { // The return type is not considered "omitted" because it's `auto`. $$ = arena->New( - context.SourceLoc(), $2, $3, $4, - arena->New(context.SourceLoc()), + context.source_loc(), $2, $3, $4, + arena->New(context.source_loc()), /*is_omitted_exp=*/false, $7); } ; @@ -688,16 +688,16 @@ function_declaration: { auto [return_exp, is_omitted_exp] = $5; $$ = arena->New( - context.SourceLoc(), $2, $3, $4, + context.source_loc(), $2, $3, $4, arena->New(return_exp), is_omitted_exp, std::nullopt); } ; variable_declaration: identifier COLON pattern - { $$ = arena->New(context.SourceLoc(), $1, $3); } + { $$ = arena->New(context.source_loc(), $1, $3); } ; member: VAR variable_declaration SEMICOLON - { $$ = arena->New(context.SourceLoc(), $2); } + { $$ = arena->New(context.source_loc(), $2); } ; member_list: // Empty @@ -714,7 +714,7 @@ alternative: | identifier { $$ = ChoiceDeclaration::Alternative( - $1, arena->New(context.SourceLoc())); + $1, arena->New(context.source_loc())); } ; alternative_list: @@ -740,11 +740,11 @@ declaration: | function_declaration { $$ = arena->New($1); } | CLASS identifier LEFT_CURLY_BRACE member_list RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2, $4); } + { $$ = arena->New(context.source_loc(), $2, $4); } | CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE - { $$ = arena->New(context.SourceLoc(), $2, $4); } + { $$ = arena->New(context.source_loc(), $2, $4); } | VAR variable_declaration EQUAL expression SEMICOLON - { $$ = arena->New(context.SourceLoc(), $2, $4); } + { $$ = arena->New(context.source_loc(), $2, $4); } ; declaration_list: // Empty