Mass rename SourceLoc and Tag (#860)

This does a mass rename of:

-   `SourceLoc()` -> `source_loc()` for property naming
    - `loc` -> `source_loc_` for underscore+consistency
    - Generally changing function args to `source_loc` for consistency
-   `Tag()` -> `kind()` for property naming and `Kind` parity
    - `tag` -> `kind_` for underscore

Also renames `Pos` and `Results` on `Action`. These are a bit of an exception in that most base classes only have `Tag` and maybe `SourceLoc`, whereas `Action` has a little more. I felt okay having `source_loc()` and `kind()` on the base class where children do `Exp()` and the like, but it felt weird to me to mix it on the same class.

The reason for doing this cross-class in one PR is so that I can do it efficiently with a global replace in the codebase, rather than e.g. changing `Expression` but having to read through compiler errors to determine where it's calling `Expression`'s `Tag` versus a different `Tag`. The end result should be equivalent.
This commit is contained in:
Jon Meow
2021-09-29 16:52:12 -07:00
committed by GitHub
parent c4e40aaa86
commit 70797e8bf8
27 changed files with 779 additions and 734 deletions
+2 -1
View File
@@ -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) {}
+6 -6
View File
@@ -17,21 +17,21 @@ namespace Carbon {
using llvm::cast;
auto ExpressionFromParenContents(
Nonnull<Arena*> arena, SourceLocation loc,
Nonnull<Arena*> arena, SourceLocation source_loc,
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
std::optional<Nonnull<Expression*>> 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*> arena, SourceLocation loc,
Nonnull<Arena*> arena, SourceLocation source_loc,
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
return arena->New<TupleLiteral>(
loc, paren_contents.TupleElements<FieldInitializer>(loc));
source_loc, paren_contents.TupleElements<FieldInitializer>(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<IndexExpression>(*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<CallExpression>(*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() << ")";
+66 -61
View File
@@ -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*> arena, SourceLocation loc,
Nonnull<Arena*> arena, SourceLocation source_loc,
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
// Converts paren_contents to an Expression, interpreting the parentheses as
// forming a tuple.
auto TupleExpressionFromParenContents(
Nonnull<Arena*> arena, SourceLocation loc,
Nonnull<Arena*> arena, SourceLocation source_loc,
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
// 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<Expression*> 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<const Expression*> { return aggregate; }
@@ -139,14 +141,15 @@ class FieldAccessExpression : public Expression {
class IndexExpression : public Expression {
public:
explicit IndexExpression(SourceLocation loc, Nonnull<Expression*> aggregate,
explicit IndexExpression(SourceLocation source_loc,
Nonnull<Expression*> aggregate,
Nonnull<Expression*> 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<const Expression*> { 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<FieldInitializer> 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<FieldInitializer>& { 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<FieldInitializer>& {
@@ -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<FieldInitializer>& {
@@ -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<Nonnull<Expression*>> 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<Expression*> function,
explicit CallExpression(SourceLocation source_loc,
Nonnull<Expression*> function,
Nonnull<Expression*> 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<const Expression*> { 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<Expression*> parameter,
Nonnull<Expression*> 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<const Expression*> { 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; }
+17 -17
View File
@@ -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<const Expression*> 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<TupleLiteral>(*expression).Fields(), IsEmpty());
}
@@ -50,8 +50,8 @@ TEST_F(ExpressionTest, EmptyAsTuple) {
.has_trailing_comma = false};
Nonnull<const Expression*> 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<TupleLiteral>(*tuple).Fields(), IsEmpty());
}
@@ -69,8 +69,8 @@ TEST_F(ExpressionTest, UnaryNoCommaAsExpression) {
Nonnull<const Expression*> 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<const Expression*> 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<TupleLiteral>(*tuple).Fields(),
ElementsAre(IntFieldNamed("0")));
}
@@ -95,8 +95,8 @@ TEST_F(ExpressionTest, UnaryWithCommaAsExpression) {
Nonnull<const Expression*> 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<TupleLiteral>(*expression).Fields(),
ElementsAre(IntFieldNamed("0")));
}
@@ -109,8 +109,8 @@ TEST_F(ExpressionTest, UnaryWithCommaAsTuple) {
Nonnull<const Expression*> 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<TupleLiteral>(*tuple).Fields(),
ElementsAre(IntFieldNamed("0")));
}
@@ -125,8 +125,8 @@ TEST_F(ExpressionTest, BinaryAsExpression) {
Nonnull<const Expression*> 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<TupleLiteral>(*expression).Fields(),
ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
}
@@ -141,8 +141,8 @@ TEST_F(ExpressionTest, BinaryAsTuple) {
Nonnull<const Expression*> 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<TupleLiteral>(*tuple).Fields(),
ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
}
+1 -1
View File
@@ -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<FieldMember>(*this);
out << "var " << *field.Binding() << ";\n";
+13 -11
View File
@@ -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<const BindingPattern*> binding)
: Member(Kind::FieldMember, loc), binding(binding) {}
FieldMember(SourceLocation source_loc, Nonnull<const BindingPattern*> 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<const BindingPattern*> { return binding; }
+4 -3
View File
@@ -41,7 +41,8 @@ struct ParenContents {
//
// TODO: Find a way to deduce TupleElement from Term.
template <typename TupleElement>
auto TupleElements(SourceLocation loc) const -> std::vector<TupleElement>;
auto TupleElements(SourceLocation source_loc) const
-> std::vector<TupleElement>;
std::vector<Element> elements;
bool has_trailing_comma;
@@ -61,7 +62,7 @@ auto ParenContents<Term>::SingleTerm() const -> std::optional<Nonnull<Term*>> {
template <typename Term>
template <typename TupleElement>
auto ParenContents<Term>::TupleElements(SourceLocation loc) const
auto ParenContents<Term>::TupleElements(SourceLocation source_loc) const
-> std::vector<TupleElement> {
std::vector<TupleElement> result;
int i = 0;
@@ -72,7 +73,7 @@ auto ParenContents<Term>::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));
+11 -9
View File
@@ -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*> arena, SourceLocation loc,
auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
const ParenContents<Pattern>& paren_contents)
-> Nonnull<Pattern*> {
std::optional<Nonnull<Pattern*>> 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*> arena, SourceLocation loc,
auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
SourceLocation source_loc,
const ParenContents<Pattern>& paren_contents)
-> Nonnull<TuplePattern*> {
return arena->New<TuplePattern>(
loc, paren_contents.TupleElements<TuplePattern::Field>(loc));
source_loc,
paren_contents.TupleElements<TuplePattern::Field>(source_loc));
}
// Used by AlternativePattern for constructor initialization. Produces a helpful
@@ -77,17 +79,17 @@ auto TuplePatternFromParenContents(Nonnull<Arena*> arena, SourceLocation loc,
// apply.
static auto RequireFieldAccess(Nonnull<Expression*> 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<FieldAccessExpression>(*alternative);
}
AlternativePattern::AlternativePattern(SourceLocation loc,
AlternativePattern::AlternativePattern(SourceLocation source_loc,
Nonnull<Expression*> alternative,
Nonnull<TuplePattern*> arguments)
: Pattern(Kind::AlternativePattern, loc),
: Pattern(Kind::AlternativePattern, source_loc),
choice_type(RequireFieldAccess(alternative).Aggregate()),
alternative_name(RequireFieldAccess(alternative).Field()),
arguments(arguments) {}
+33 -26
View File
@@ -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<std::string> name,
BindingPattern(SourceLocation source_loc, std::optional<std::string> name,
Nonnull<Pattern*> 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*> pattern;
};
TuplePattern(SourceLocation loc, std::vector<Field> fields)
: Pattern(Kind::TuplePattern, loc), fields(std::move(fields)) {}
TuplePattern(SourceLocation source_loc, std::vector<Field> 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<Field> { 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*> arena, SourceLocation loc,
auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
const ParenContents<Pattern>& paren_contents)
-> Nonnull<Pattern*>;
// Converts paren_contents to a TuplePattern, interpreting the parentheses as
// forming a tuple.
auto TuplePatternFromParenContents(Nonnull<Arena*> arena, SourceLocation loc,
auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
SourceLocation source_loc,
const ParenContents<Pattern>& paren_contents)
-> Nonnull<TuplePattern*>;
@@ -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<Expression*> choice_type,
AlternativePattern(SourceLocation source_loc,
Nonnull<Expression*> choice_type,
std::string alternative_name,
Nonnull<TuplePattern*> 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<Expression*> alternative,
AlternativePattern(SourceLocation source_loc,
Nonnull<Expression*> alternative,
Nonnull<TuplePattern*> arguments);
static auto classof(const Pattern* pattern) -> bool {
return pattern->Tag() == Kind::AlternativePattern;
return pattern->kind() == Kind::AlternativePattern;
}
auto ChoiceType() const -> Nonnull<const Expression*> { return choice_type; }
@@ -181,11 +188,11 @@ class AlternativePattern : public Pattern {
class ExpressionPattern : public Pattern {
public:
ExpressionPattern(Nonnull<Expression*> 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<const Expression*> { return expression; }
+8 -8
View File
@@ -39,7 +39,7 @@ TEST_F(PatternTest, EmptyAsPattern) {
.has_trailing_comma = false};
Nonnull<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1));
ASSERT_TRUE(isa<TuplePattern>(*pattern));
EXPECT_THAT(cast<TuplePattern>(*pattern).Fields(), IsEmpty());
}
@@ -49,7 +49,7 @@ TEST_F(PatternTest, EmptyAsTuplePattern) {
.has_trailing_comma = false};
Nonnull<const TuplePattern*> 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<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(2));
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(2));
ASSERT_TRUE(isa<AutoPattern>(*pattern));
}
@@ -79,7 +79,7 @@ TEST_F(PatternTest, UnaryNoCommaAsTuplePattern) {
Nonnull<const TuplePattern*> 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<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1));
ASSERT_TRUE(isa<TuplePattern>(*pattern));
EXPECT_THAT(cast<TuplePattern>(*pattern).Fields(),
ElementsAre(AutoFieldNamed("0")));
@@ -105,7 +105,7 @@ TEST_F(PatternTest, UnaryWithCommaAsTuplePattern) {
Nonnull<const TuplePattern*> 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<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1));
ASSERT_TRUE(isa<TuplePattern>(*pattern));
EXPECT_THAT(cast<TuplePattern>(*pattern).Fields(),
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
@@ -135,7 +135,7 @@ TEST_F(PatternTest, BinaryAsTuplePattern) {
Nonnull<const TuplePattern*> 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")));
}
+1 -1
View File
@@ -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<Match>(*this);
out << "match (" << match.expression() << ") {";
+58 -51
View File
@@ -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<Expression*> exp)
: Statement(Kind::ExpressionStatement, loc), exp(exp) {}
ExpressionStatement(SourceLocation source_loc, Nonnull<Expression*> 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<const Expression*> { return exp; }
@@ -75,11 +76,12 @@ class ExpressionStatement : public Statement {
class Assign : public Statement {
public:
Assign(SourceLocation loc, Nonnull<Expression*> lhs, Nonnull<Expression*> rhs)
: Statement(Kind::Assign, loc), lhs(lhs), rhs(rhs) {}
Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
Nonnull<Expression*> 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<const Expression*> { return lhs; }
@@ -94,12 +96,12 @@ class Assign : public Statement {
class VariableDefinition : public Statement {
public:
VariableDefinition(SourceLocation loc, Nonnull<Pattern*> pat,
VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pat,
Nonnull<Expression*> 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<const Pattern*> { return pat; }
@@ -114,16 +116,16 @@ class VariableDefinition : public Statement {
class If : public Statement {
public:
If(SourceLocation loc, Nonnull<Expression*> cond,
If(SourceLocation source_loc, Nonnull<Expression*> cond,
Nonnull<Statement*> then_stmt,
std::optional<Nonnull<Statement*>> 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<const Expression*> { return cond; }
@@ -143,15 +145,16 @@ class If : public Statement {
class Return : public Statement {
public:
Return(Nonnull<Arena*> arena, SourceLocation loc)
: Return(loc, arena->New<TupleLiteral>(loc), true) {}
Return(SourceLocation loc, Nonnull<Expression*> exp, bool is_omitted_exp)
: Statement(Kind::Return, loc),
Return(Nonnull<Arena*> arena, SourceLocation source_loc)
: Return(source_loc, arena->New<TupleLiteral>(source_loc), true) {}
Return(SourceLocation source_loc, Nonnull<Expression*> 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<const Expression*> { return exp; }
@@ -165,12 +168,12 @@ class Return : public Statement {
class Sequence : public Statement {
public:
Sequence(SourceLocation loc, Nonnull<Statement*> stmt,
Sequence(SourceLocation source_loc, Nonnull<Statement*> stmt,
std::optional<Nonnull<Statement*>> 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<const Statement*> { return stmt; }
@@ -185,11 +188,11 @@ class Sequence : public Statement {
class Block : public Statement {
public:
Block(SourceLocation loc, std::optional<Nonnull<Statement*>> stmt)
: Statement(Kind::Block, loc), stmt(stmt) {}
Block(SourceLocation source_loc, std::optional<Nonnull<Statement*>> 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<Nonnull<const Statement*>> { return stmt; }
@@ -201,11 +204,12 @@ class Block : public Statement {
class While : public Statement {
public:
While(SourceLocation loc, Nonnull<Expression*> cond, Nonnull<Statement*> body)
: Statement(Kind::While, loc), cond(cond), body(body) {}
While(SourceLocation source_loc, Nonnull<Expression*> cond,
Nonnull<Statement*> 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<const Expression*> { 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*> statement_;
};
Match(SourceLocation loc, Nonnull<Expression*> expression,
Match(SourceLocation source_loc, Nonnull<Expression*> expression,
std::vector<Clause> 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<Statement*> 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 <argument>;
class Run : public Statement {
public:
Run(SourceLocation loc, Nonnull<Expression*> argument)
: Statement(Kind::Run, loc), argument(argument) {}
Run(SourceLocation source_loc, Nonnull<Expression*> 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<const Expression*> { 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;
}
};