mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 14:10:11 +01:00
Refactor Expression accessor/mutator style (#883)
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
namespace Carbon {
|
||||
|
||||
using llvm::cast;
|
||||
using llvm::isa;
|
||||
|
||||
auto ExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
@@ -68,7 +69,7 @@ static void PrintFields(llvm::raw_ostream& out,
|
||||
std::string_view separator) {
|
||||
llvm::ListSeparator sep;
|
||||
for (const auto& field : fields) {
|
||||
out << sep << "." << field.name() << separator << *field.expression();
|
||||
out << sep << "." << field.name() << separator << field.expression();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,12 +77,12 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
switch (kind()) {
|
||||
case Expression::Kind::IndexExpression: {
|
||||
const auto& index = cast<IndexExpression>(*this);
|
||||
out << *index.Aggregate() << "[" << *index.Offset() << "]";
|
||||
out << index.aggregate() << "[" << index.offset() << "]";
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
const auto& access = cast<FieldAccessExpression>(*this);
|
||||
out << *access.Aggregate() << "." << access.Field();
|
||||
out << access.aggregate() << "." << access.field();
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::TupleLiteral:
|
||||
@@ -100,37 +101,43 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
out << "}";
|
||||
break;
|
||||
case Expression::Kind::IntLiteral:
|
||||
out << cast<IntLiteral>(*this).Val();
|
||||
out << cast<IntLiteral>(*this).value();
|
||||
break;
|
||||
case Expression::Kind::BoolLiteral:
|
||||
out << (cast<BoolLiteral>(*this).Val() ? "true" : "false");
|
||||
out << (cast<BoolLiteral>(*this).value() ? "true" : "false");
|
||||
break;
|
||||
case Expression::Kind::PrimitiveOperatorExpression: {
|
||||
out << "(";
|
||||
PrimitiveOperatorExpression op = cast<PrimitiveOperatorExpression>(*this);
|
||||
if (op.Arguments().size() == 0) {
|
||||
PrintOp(out, op.Op());
|
||||
} else if (op.Arguments().size() == 1) {
|
||||
PrintOp(out, op.Op());
|
||||
out << " " << *op.Arguments()[0];
|
||||
} else if (op.Arguments().size() == 2) {
|
||||
out << *op.Arguments()[0] << " ";
|
||||
PrintOp(out, op.Op());
|
||||
out << " " << *op.Arguments()[1];
|
||||
switch (op.arguments().size()) {
|
||||
case 0:
|
||||
PrintOp(out, op.op());
|
||||
break;
|
||||
case 1:
|
||||
PrintOp(out, op.op());
|
||||
out << " " << *op.arguments()[0];
|
||||
break;
|
||||
case 2:
|
||||
out << *op.arguments()[0] << " ";
|
||||
PrintOp(out, op.op());
|
||||
out << " " << *op.arguments()[1];
|
||||
break;
|
||||
default:
|
||||
FATAL() << "Unexpected argument count: " << op.arguments().size();
|
||||
}
|
||||
out << ")";
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::IdentifierExpression:
|
||||
out << cast<IdentifierExpression>(*this).Name();
|
||||
out << cast<IdentifierExpression>(*this).name();
|
||||
break;
|
||||
case Expression::Kind::CallExpression: {
|
||||
const auto& call = cast<CallExpression>(*this);
|
||||
out << *call.Function();
|
||||
if (call.Argument()->kind() == Expression::Kind::TupleLiteral) {
|
||||
out << *call.Argument();
|
||||
out << call.function();
|
||||
if (isa<TupleLiteral>(call.argument())) {
|
||||
out << call.argument();
|
||||
} else {
|
||||
out << "(" << *call.Argument() << ")";
|
||||
out << "(" << call.argument() << ")";
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -142,7 +149,7 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
break;
|
||||
case Expression::Kind::StringLiteral:
|
||||
out << "\"";
|
||||
out.write_escaped(cast<StringLiteral>(*this).Val());
|
||||
out.write_escaped(cast<StringLiteral>(*this).value());
|
||||
out << "\"";
|
||||
break;
|
||||
case Expression::Kind::StringTypeLiteral:
|
||||
@@ -156,13 +163,13 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
break;
|
||||
case Expression::Kind::FunctionTypeLiteral: {
|
||||
const auto& fn = cast<FunctionTypeLiteral>(*this);
|
||||
out << "fn " << *fn.Parameter() << " -> " << *fn.ReturnType();
|
||||
out << "fn " << fn.parameter() << " -> " << fn.return_type();
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::IntrinsicExpression:
|
||||
out << "intrinsic_expression(";
|
||||
switch (cast<IntrinsicExpression>(*this).Intrinsic()) {
|
||||
case IntrinsicExpression::IntrinsicKind::Print:
|
||||
switch (cast<IntrinsicExpression>(*this).intrinsic()) {
|
||||
case IntrinsicExpression::Intrinsic::Print:
|
||||
out << "print";
|
||||
}
|
||||
out << ")";
|
||||
|
||||
Reference in New Issue
Block a user